
    uki                        U d dl mZ d dlZd dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZ d dlmZmZmZ d dlmZ eed	f   Zeed
<   eeed	f   d	f   Zeed<   dedeeeegef   f   fdZdedeegee   f   dedefdZdee   deeeegee   f   f   fdZ dedededee   fdZ!dededeejD                  d	f   dejD                  dedee   fdZ#y)    )IterableN)AnyCallable	TypeAlias)lax)dtypes)tree_flattentree_unflatten	PyTreeDefLeaf)safe_zipunzip2HashablePartial)Array.SizesShapespytreereturnc                 `    t        |       \  }}t        |      \  }}|t        t        ||      fS )a  Ravel (flatten) a pytree of arrays down to a 1D array.

  Args:
    pytree: a pytree of arrays and scalars to ravel.

  Returns:
    A pair where the first element is a 1D array representing the flattened and
    concatenated leaf values, with dtype determined by promoting the dtypes of
    leaf values, and the second element is a callable for unflattening a 1D
    vector of the same length back to a pytree of the same structure as the
    input ``pytree``. If the input pytree is empty (i.e. has no leaves) then as
    a convention a 1D empty array of dtype float32 is returned in the first
    component of the output.

  For details on dtype promotion, see
  https://docs.jax.dev/en/latest/type_promotion.html.

  )r	   _ravel_listr   unravel_pytree)r   leavestreedefflatunravel_lists        P/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/flatten_util.pyravel_pytreer      s5    & !(/&'"6*$	~wE	EE    r   r   r   c                 &    t        |  ||            S N)r
   )r   r   r   s      r   r   r   5   s    
 
d!3	44r   lstc                    | st        j                  dgdd      d fS t        d | D              }t        j                  | t        d | D              \  }}t        fd|D              rC~d }t        j                  | D cg c]
  } ||       c}d      }|t        t        ||      fS fd	}t        j                  | D cg c]
  } ||       c}d      }t        t        |||      }||fS c c}w c c}w )
Nr   float32c                     g S r     )_s    r   <lambda>z_ravel_list.<locals>.<lambda>?   s    " r   c              3   F   K   | ]  }t        j                  |        y wr    )r   dtype).0ls     r   	<genexpr>z_ravel_list.<locals>.<genexpr>@   s     3!fll1o3s   !c              3   p   K   | ].  }t        j                  |      t        j                  |      f 0 y wr    )npsizeshape)r*   xs     r   r,   z_ravel_list.<locals>.<genexpr>B   s%     @q"''!*bhhqk2@s   46c              3   (   K   | ]	  }|k(    y wr    r%   )r*   dtto_dtypes     r   r,   z_ravel_list.<locals>.<genexpr>D   s     .Bx.s   c                 V    t        j                  | t        j                  |       f      S r    )r   reshaper.   r/   )es    r   r'   z_ravel_list.<locals>.<lambda>H   s    ckk!bggaj]3 r   )	dimensionc                 L    t        j                  |       j                         S r    )r   convert_element_typeravel)r7   r4   s    r   r'   z_ravel_list.<locals>.<lambda>N   s    C,,Q9??A r   )r   fulltupler   result_typer   allconcatenater   _unravel_list_single_dtype_unravel_list)	r!   from_dtypessizesshapesr;   r7   raveledunravr4   s	           @r   r   r   =   s    	88QCI&443s33+-(@C@@-%.+.. 	X3Eoo5AuQx5CGO$>vNNN B%OOs3!U1X3qA'
-X
N%	% 6 4s   C6C;rD   rE   arrc                     t        j                  ||       }t        ||      D cg c]  \  }}|j                  |       c}}S c c}}w r    )r   splitzipr6   )rD   rE   rH   chunkschunkr0   s         r   rA   rA   T   s:    99S% &36vv3F	G<5%%--
	GG	Gs   ArC   r4   c           
      "   t        j                  |      }||k7  rt        d| d|       t        j                  ||       }t        |||      D 	cg c]-  \  }}}	t        j                  |j                  |      |	d      / c}	}}S c c}	}}w )Nz&unravel function given array of dtype z, but expected dtype F)warn_on_complex_to_real_cast)r   r)   	TypeErrorr   rJ   rK   _convert_element_typer6   )
rD   rE   rC   r4   rH   	arr_dtyperL   rM   r0   r)   s
             r   rB   rB   Y   s     ll3)(
.yk :$:'  99S% &
  #66;?	
 
 	ue mmEE
  
s   2B
)$collections.abcr   numpyr.   typingr   r   r   jax._src.laxr   jax._srcr   jax._src.tree_utilr	   r
   r   r   jax._src.utilr   rK   r   r   jax._src.typingr   r=   intr   __annotations__r   r   r   listr   rA   r)   rB   r%   r   r   <module>r^      sh   %  + +   L L B B !c?y "%S/3./	 /F Fuhw|.D'D!E F055%(4.015 	5 		5T#Y eE8UGT#Y<N3O,O&P .He HV H% HDQVK H
	
 RXXs]# HH	
  
%[r   