
    vh                         d dl Zd dlm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y)    N)lax)dtypes)tree_flattentree_unflatten)safe_zipunzip2HashablePartialc                 `    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)pytreeleavestreedefflatunravel_lists        P/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/flatten_util.pyravel_pytreer      s5    & !(/&'"6*$	~wE	EE    c                 &    t        |  ||            S N)r   )r   r   r   s      r   r   r   3   s    	d!3	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              rK~	t        j                  | D cg c]  }t        j                  |       c}      }|t        t        ||      fS 	fd}t        j                  | D cg c]
  } ||       c}      }t        t        |||	      }||fS c c}w c c}w )Nc                     g S r    )_s    r   <lambda>z_ravel_list.<locals>.<lambda>7   s    2 r   c              3   F   K   | ]  }t        j                  |        y wr   )r   dtype).0ls     r   	<genexpr>z_ravel_list.<locals>.<genexpr>8   s     3!fll1o3s   !c              3   p   K   | ].  }t        j                  |      t        j                  |      f 0 y wr   )jnpsizeshape)r   xs     r   r    z_ravel_list.<locals>.<genexpr>:   s%     B#((1+syy|4Bs   46c              3   (   K   | ]	  }|k(    y wr   r   )r   dtto_dtypes     r   r    z_ravel_list.<locals>.<genexpr>=   s     .Bx.s   c                 V    t        j                  t        j                  |             S r   )r"   ravelr   convert_element_type)er(   s    r   r   z_ravel_list.<locals>.<lambda>F   s    CIIc66q(CD r   )r"   arrayfloat32tupler   result_typer   npcumsumallconcatenater*   r	   _unravel_list_single_dtype_unravel_list)
lstfrom_dtypessizesshapesindicesr,   raveledr*   unravr(   s
            @r   r   r   6   s    	SYYr3;;/==3s33+-(BcBB-%"))E"#'.+.. 	XooS9syy|9:GO$>PPP E%OOs3!U1X34'
-&+x
P%	% : 4s   +D&9D+c                     t        j                  || d d       }t        ||      D cg c]  \  }}|j                  |       c}}S c c}}w )N)r"   splitzipreshape)r;   r:   arrchunkschunkr$   s         r   r5   r5   K   s@    99S'#2,'&36vv3F	G<5%%--
	GG	Gs   Ac           
      (   t        j                  |      }||k7  rt        d| d|       t        j                  || d d       }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 r?   F)warn_on_complex_to_real_cast)	r   r   	TypeErrorr"   r@   rA   lax_internal_convert_element_typerB   )
r;   r:   r8   r(   rC   	arr_dtyperD   rE   r$   r   s
             r   r6   r6   O   s    ll3)(
<YK H**25 6 699S'#2,'&  #66;?
 
 	ue &&u}}U';UDIK
  
s   2B)numpyr1   jaxr   	jax.numpyr"   jax._src.laxrI   jax._srcr   jax._src.tree_utilr   r   jax._src.utilr   r   r	   rA   r   r   r   r5   r6   r   r   r   <module>rS      s?       ,  ; ; ;F.5*H
r   