
    vh              	       B   d Z ddlmZ ddlm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 dd	lmZ dd
lmZ ddlmZ ddlmZmZmZ eecZZeecZZdd ddej8                  dedeej<                  gef   dej8                  fdZdefdZ dedefdZ!defdZ"defdZ#y)z)Utilities for tracing stateful functions.    )partial)CallableN)core)dtypes)linear_util)partial_evalAbstractRef)ref_get)	DTypeLike)safe_mapsafe_zip
split_listc                     t        |       S )Nr	   )avals    O/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/state/utils.py<lambda>r   %   s    R]^bRc     )indexmake_abstract_refjaxprr   r   returnc                $   
  j                   s S  j                   D cg c]  }t        |j                  t               c}t	         j                         D cg c]%  \  }}|r|j                  n ||j                        ' c}}
 j
                  D cg c]  }|j                   }}
| 
 fd}t        j                  t        j                  | j                        |      \  }}}	\   |	rJ d       |S c c}w c c}}w c c}w )a  Hoists the constants in the given jaxpr into invars.

  Args:
    jaxpr: The jaxpr.
    index: The index where the invars for the constants should be inserted.
      By default, the new invars are inserted *before* any existing invars.
    make_abstract_ref: a callable to construct an AbstractRef, or subtype
      thereof, from a constant AbstractValue.

  Returns:
    A new jaxpr where the constants were hoisted into invars as ``Ref``s.
  c                      t        | t              g      \  }}}t        |      D cg c]  \  }}|r|nt        |d       }}}t	        j
                  	|g|| S c c}}w )N )r   lenzipr   r   
eval_jaxpr)
consts_argsargs0
all_constsargs1is_refcconst_avalsr   is_const_refr   s
         r   _hoistz$hoist_consts_to_refs.<locals>._hoist@   s|    )eS-. E:u \:6FA B'J  ??5*=u=u==	s   A")
debug_infoz-All consts should have been converted to refs)	constvars
isinstancer   r
   r   invarspetrace_to_jaxpr_dynamiclu	wrap_initr(   )r   r   r   varr#   in_avalsr'   hoisted_jaxpr_constsr%   r&   s   ``        @@r   hoist_consts_to_refsr5   !   s    $ 
L 49??,/j;',
 \5??;
&# chh/99+ #(,,/3chh/(/%(5	> "$!:!:ll6e&6&67"C-FBDDD	1 0s   !D*DDc                     t        j                  |       }t        |      t         j                  urt	        d|        t        |      S )Nzcan't make ref from )r   get_avaltypeShapedArray	TypeErrorr
   )xr   s     r   val_to_ref_avalr<   Q   s@    	q	$	$Zt'''
*1#.
//	T	r   dtypec                     t        j                  | d      rt        j                  |       j                  S t        j                  |       j
                  dz  S )Nintegral   )r   isdtypeiinfobitsr=   itemsizer=   s    r   dtype_bitwidthrF   X   s?    ^^E:&<<###	e		%	%	))r   c                 $   t        | j                        }t        |      }t        | j                        }||k7  r2t	        |      dk  rt        d      |d   |z  |z  dk7  rt        d      |d   |z  |z  |d<   ||k  rJ||z  } | j                  g | j                  d d | j                  d   |z  |d j                  dd      } t        j                  j                  | |      }||kD  r!|j                  dd      j                  |      }|S )N   z5Bitcast 1D ref with bitwidth change is not supported.r   zlExpected input and output shapes are the same after multiplying the second-minor dimension by the bitwidths.)rF   r=   listshaper   NotImplementedError
ValueErrorreshapeswapaxesjaxlaxbitcast_convert_type)r;   r=   
x_bitwidth
y_bitwidthrL   ratioys          r   bitcastrX   ^   s(   agg&*e$*
qww-%:
5zA~
A  Ry:
*a/:  Bi*$
2%)**$E		A1773B<A!5AuAbAJJ
B	A 
gg""1e,!*	

2r""5)A	
(r   c                     t        t        |      }t        j                  |t        j                  | j
                  | j                              j
                  S )NrE   )r   rX   rQ   
eval_shapeShapeDtypeStructrL   r=   )r;   r=   fs      r   eval_bitcast_shaper]   y   s:    gU#!	3//A	B	H	HHr   )$__doc__	functoolsr   typingr   rQ   jax._srcr   r   r   r.   jax._src.interpretersr   r,   jax._src.stater
   jax._src.state.primitivesr   jax._src.typingr   jax._src.utilr   r   r   map
unsafe_mapr   
unsafe_zipJaxprintAbstractValuer5   r<   rF   rX   r]   r   r   r   <module>rm      s    0   
   & 4 & - % 8 8CZCZ Ec	-::- -  !3!3 4k AB	-
 
ZZ-`+ *) * *i 6I Ir   