
    Vpf
              	           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mZmZ ddl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S )z)Utilities for tracing stateful functions.    )Callable)partial_eval)core)linear_utilAbstractRef)
split_listsafe_mapsafe_zipref_getc                      t          |           S )Nr   avals    T/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/state/utils.py<lambda>r   !   s    R]^bRcRc     )indexmake_abstract_refjaxprr   r   returnc                8   	  j         s S d  j         D             	fdt          	 j                   D             d  j        D             }|<   	 fd}t          j        t          j        |          |          \  }}}\   |r
J d            |S )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                 B    g | ]}t          |j        t                    S  )
isinstancer   r   .0vars     r   
<listcomp>z(hoist_consts_to_refs.<locals>.<listcomp>2   s1       ,/j;''  r   c                 H    g | ]\  }}|r|j         n |j                   S r   r   )r   is_refr   r   s      r   r   z(hoist_consts_to_refs.<locals>.<listcomp>5   sF       
&# 9chh//99  r   c                     g | ]	}|j         
S r   r   r   s     r   r   z(hoist_consts_to_refs.<locals>.<listcomp>9   s    ///3ch///r   c                      t          | t                    g          \  }}}d t          |          D             }t          j        |g||R  S )Nc                 <    g | ]\  }}|r|nt          |d           S )r   r   )r   r!   cs      r   r   z8hoist_consts_to_refs.<locals>._hoist.<locals>.<listcomp>A   s=       FA 'B  r   )r	   lenzipr   
eval_jaxpr)consts_argsargs0
all_constsargs1const_avalsr   is_const_refr   s       r   _hoistz$hoist_consts_to_refs.<locals>._hoist<   st    )eS--.   E:u \:66  J ?5*=u=u====r   z-All consts should have been converted to refs)	constvarsr'   invarspetrace_to_jaxpr_dynamiclu	wrap_init)
r   r   r   in_avalsr/   hoisted_jaxpr_constsr-   r.   s
   ```     @@r   hoist_consts_to_refsr:      s    $ 
 L 38?  ,   \5?;;  + 0/%,///(%(5;	> 	> 	> 	> 	> 	> 	> 	> "$!:l6H"& "&-FBDDDDDD	r   c                     t          j        t          j        |                     }t          |          t           j        urt          d|            t          |          S )Nzcan't make ref from )r   raise_to_shapedget_avaltypeShapedArray	TypeErrorr   )xr   s     r   val_to_ref_avalrB   M   sU    		dmA..	/	/$	$ZZt'''
.1..
/
//	T		r   N)__doc__typingr   jax._src.interpretersr   r2   jax._srcr   r   r4   jax._src.stater   jax._src.utilr	   r
   r   jax._src.state.primitivesr   map
unsafe_mapr'   
unsafe_zipJaxprintAbstractValuer:   rB   r   r   r   <module>rP      s6   0 /       4 4 4 4 4 4       & & & & & & & & & & & & 8 8 8 8 8 8 8 8 8 8 - - - - - -CZCZ EcEc	- - -:- -  !3 4k AB	-
 
Z- - - -`+      r   