
    uki              	       B   d 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 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j<                  dedeej@                  gef   dej<                  fdZ!defdZ"defdZ#defdZ$y)z)Utilities for tracing stateful functions.    )partial)Callable)api)core)dtypes)linear_util)partial_eval)lax)ref_getAbstractRef)	DTypeLike)safe_mapsafe_zip
split_listc                     t        |       S )Nr   )avals    O/home/cdr/jupyterlab/.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                  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>._hoistA   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*   with_unknown_names)r   r   r   varr%   in_avalsr)   hoisted_jaxpr_constsr'   r(   s   ``        @@r   hoist_consts_to_refsr8   "   s   $ 
L 49??,/j;',
 \5??;
&# chh/99+ #(,,/3chh/(/%(5	>  66ll6e&6&6&I&I&KL-F DDD	3 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?   S   s@    	q	$	$Zt'''
*1#.
//	T	r   dtypec                 8   t        j                  | j                        }t        j                  |      }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                  | |      }||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.)r   itemsize_bitsr@   listshaper   NotImplementedError
ValueErrorreshapeswapaxesr
   bitcast_convert_type)r>   r@   
x_bitwidth
y_bitwidthrG   ratioys          r   bitcastrQ   Z   s0   ##AGG,*##E**
qww-%:
5zA~
A  Ry:
*a/:  Bi*$
2%)**$E		A1773B<A!5AuAbAJJ
B	A 
q%(!*	

2r""5)A	
(r   c                     t        t        |      }t        j                  |t        j                  | j
                  | j                              j
                  S )N)r@   )r   rQ   r   
eval_shapeShapeDtypeStructrG   r@   )r>   r@   fs      r   eval_bitcast_shaperV   u   s:    gU#!	3//A	B	H	HHr   N)%__doc__	functoolsr   collections.abcr   jax._srcr   r   r   r   r0   jax._src.interpretersr	   r.   jax._src.laxr
   jax._src.state.primitivesr   jax._src.state.typesr   jax._src.typingr   jax._src.utilr   r   r   map
unsafe_mapr   
unsafe_zipJaxprintAbstractValuer8   r?   rQ   rV   r   r   r   <module>rg      s    0  $    & 4  - , % 8 8CZCZ Ec	.::. .  !3!3 4k AB	.
 
ZZ.b+ i 6I Ir   