
    Vpf                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZmZ eZd Zd Zd Zd	 Zd
 ZdS )    N)lax)dtypes)tree_flattentree_unflatten)safe_zipunzip2HashablePartialc                 z    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://jax.readthedocs.io/en/latest/type_promotion.html.

  )r   _ravel_listr	   unravel_pytree)pytreeleavestreedefflatunravel_lists        U/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/flatten_util.pyravel_pytreer      s>    & !((/&'"6**$	~wEE	EE    c                 4    t          |  ||                    S N)r   )r   r   r   s      r   r   r   4   s    	d!3!3	4	44r   c                 >   | s"t          j        g t           j                  d fS t          d | D                       }t	          j        | t          d | D                       \  }}t          t          j        |                    }t          fd|D                       r8~t          j
        d | D                       }|t          t          ||          fS fdt          j
        fd| D                       }t          t          |||          }||fS )Nc                     g S r    )_s    r   <lambda>z_ravel_list.<locals>.<lambda>8   s    2 r   c              3   >   K   | ]}t          j        |          V  d S r   )r   dtype).0ls     r   	<genexpr>z_ravel_list.<locals>.<genexpr>9   s*      33!fl1oo333333r   c              3   f   K   | ],}t          j        |          t          j        |          fV  -d S r   )jnpsizeshape)r   xs     r   r    z_ravel_list.<locals>.<genexpr>;   s7      BB#(1++sy||4BBBBBBr   c              3   $   K   | ]
}|k    V  d S r   r   )r   dtto_dtypes     r   r    z_ravel_list.<locals>.<genexpr>>   s'      ..Bx......r   c                 6    g | ]}t          j        |          S r   )r"   ravel)r   es     r   
<listcomp>z_ravel_list.<locals>.<listcomp>B   s     999sy||999r   c                 R    t          j        t          j        |                     S r   )r"   r*   r   convert_element_type)r+   r(   s    r   r   z_ravel_list.<locals>.<lambda>G   s    CIc6q(CCDD r   c                 &    g | ]} |          S r   r   )r   r+   r*   s     r   r,   z_ravel_list.<locals>.<listcomp>H   s!    333!UU1XX333r   )r"   arrayfloat32tupler   result_typer   npcumsumallconcatenater	   _unravel_list_single_dtype_unravel_list)	lstfrom_dtypessizesshapesindicesraveledunravr*   r(   s	          @@r   r   r   7   s;   	=SYr3;//==33s33333+-(BBcBBBBB-%")E""##'....+..... Q 	Xo99S999::GO$>PPPP E
D
D
D%O3333s33344'
-&+x
P
P%	%r   c                 p    t          j        || d d                   }d t          ||          D             S )Nc                 >    g | ]\  }}|                     |          S r   )reshape)r   chunkr$   s      r   r,   z._unravel_list_single_dtype.<locals>.<listcomp>N   s(    	G	G	G<5%%--

	G	G	Gr   )r"   splitzip)r>   r=   arrchunkss       r   r8   r8   L   s9    9S'#2#,''&	G	G3vv3F3F	G	G	GGr   c                 R   t          j        |          }||k    rt          d| d|           t          j        || d d                   }t          j                    5  t          j        d           d t          |||          D             cd d d            S # 1 swxY w Y   d S )Nz&unravel function given array of dtype z, but expected dtype rB   ignorec                 f    g | ].\  }}}t          j        |                    |          |          /S r   )r   r.   rD   )r   rE   r$   r   s       r   r,   z!_unravel_list.<locals>.<listcomp>X   sM     I I I#ue $U]]5%9%95AA I I Ir   )	r   r   	TypeErrorr"   rF   warningscatch_warningssimplefilterrG   )r>   r=   r;   r(   rH   	arr_dtyperI   s          r   r9   r9   P   s*   l3)(
 5Y 5 5*25 5 6 6 69S'#2#,''&   I I(###I I'*66;'G'GI I II I I I I I I I I I I I I I I I I Is    /BB #B )rN   numpyr4   jaxr   	jax.numpyr"   jax._srcr   jax._src.tree_utilr   r   jax._src.utilr   r   r	   rG   r   r   r   r8   r9   r   r   r   <module>rX      s                           ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;F F F.5 5 5  *H H H	I 	I 	I 	I 	Ir   