
    vhOx                    ~   d Z ddlmZ ddlmZ ddlZddlmZ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 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!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z(m)Z) e(e*cZ*Z+e)e,cZ,Z- ej\                  e/        ej`                  d      Z1e1je                   eejf                  e1             ejh                  ejj                  e1<   ee6e7e&ejp                  f   Z9	 d?	 	 	 	 	 	 	 	 	 d@dZ:	 dA	 	 	 	 	 dBdZ; ej`                  d      Z<e<je                   eejf                  e<             d Z=e=ejj                  e<<   	 dC	 	 	 	 	 	 	 	 	 dDdZ>d Z?	 	 	 	 	 	 	 	 dEdZ@ ej`                  d      ZAdeA_B        eAje                   eejf                  eA             	 	 	 	 	 	 	 	 dFd ZC	 	 	 	 	 	 dGd!ZD	 	 	 	 	 	 dHd"ZEd# ZFdId$ZGe1j                  eG       	 	 	 	 dJd%ZIe<j                  eI       	 	 	 	 dJd&ZJeAj                  eJ        eej                  ej                  j                  ej                  j                  '      ZP	 	 	 	 dKd(ZQdLd)ZRdMd*ZSeSej                  e1<   dMd+ZUeUej                  e<<   dMd,ZVeVej                  eA<   dNd-ZWeWej                  e1<   dNd.ZYeYej                  e<<   dNd/ZZeZej                  eA<   d0 Z[e[ej                  e1<   d1 Z]e]ej                  e<<   d2 Z^e^ej                  eA<   d3 Z_ ee_e1      ej                  e1<    ee_e<      ej                  e<<    ee_eA      ej                  eA<   	 	 	 	 	 	 	 	 	 	 	 	 dOd4Zad5 Zbebej                  e1<   d6 Zdedej                  e<<   d7 Zeeeej                  eA<    ej`                  d8      ZfdPd9Zgefjd                  d:        Zhefj                  d;        Zj ej                  ef ej                  ehd<             d= Zmemej                  ej                  <    ej                  ej                  d>        y)QzModule for state primitives.    )annotations)partialN)AnyUnion)ad_util)core)dispatch)dtypes)pretty_printer)traceback_util)	tree_util)ad)batching)mlir)partial_eval)lax)indexing)AbstractRefAccumEffect
ReadEffect	TransformTransformedRefWriteEffect)Array)safe_mapsafe_zipgetTc                   t        | t              r| j                  | j                  }}n| d}}t	        j
                  |      }t        |t              st        d| d| d      t        |j                  t        j                        s|dfS ||t        u rd}nt        |t              s|f}|s|s||fS |s#|r!t        |d   t        j                        r||fS t        j                  j                  || j                        }|g ||fS )N zCan only call `z` on a `Ref`: .)
isinstancer   ref
transformsr   get_avalr   
ValueError
inner_avalShapedArrayEllipsistupler   	NDIndexerfrom_indices_shapeshape)ref_or_viewidxfunction_nameforce_trailing_indexerr#   r$   ref_aval
nd_indexers           T/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/state/primitives.pyget_ref_and_transformsr5   H   s    ^,!oo{'='=C!2C]]3(	Hk	*
}o^C5J
KK	H'')9)9	:7N[C8O
Cc5!&C	+
?	
:b>8;M;M N
?!!44S+:K:KL*	'
'J'	''    c                    t        | |d      \  }}t        j                  |      \  }}t        j                  |g|d|iS )z5Reads a value from a `Ref`, a.k.a. value <- ref[idx].ref_gettree)r5   r   tree_flattenget_pbind)r.   r/   r#   r$   flat_transformsr9   s         r4   r8   r8   e   sD     +;YG/#z#00</4	C	5/	5	55r6   swapc                D    t        |      dk(  sJ |d   }|d   }||gd gfS )N   r      )len)
eqn_paramsinvar_raggednessoutvarsinvar_raggedness_lhsinvar_raggedness_rhss        r4   swap_ragged_prop_rulerH      s?    		!	##	#)!,)!,
 4	5v	==r6   c                    t        | d      rt        | j                  |      }t        | ||      \  }}t	        j
                  |      \  }}t        j                  ||g|d|iS )z4Sets a `Ref`'s value and returns the original value.dtyper9   )hasattr_maybe_implicit_castrJ   r5   r   r:   swap_pr<   )r.   r/   value_function_namer#   r$   r=   r9   s           r4   ref_swaprP      sb     ['" !2!2E:E*;^L/#z#00</4	S%	=/	=	==r6   c                   t        j                  |      }|j                  rRt        j                  | t
        j                        r.t        j                  |j                  t
        j                        sRt        j                  | t
        j                        rDt        j                  |j                  t
        j                        rt        j                  ||       S |S N)r   typeof	weak_typer
   
issubdtypenpfloatingrJ   integerr   convert_element_type)rJ   rN   avals      r4   rL   rL      s    	U	$
nn,R[[1

+RZZ0##E511	,r6   c                "    t        | ||d       y)z/Sets a `Ref`'s value, a.k.a. ref[idx] <- value.ref_set)rO   NrP   )r.   r/   rN   s      r4   r\   r\      s     ;U9=r6   	addupdatec                    t        | |d      \  }}t        j                  |      \  }}t        j                  ||g|d|iS )z;Mutates a ref with an additive update i.e. `ref[idx] += x`.ref_addupdater9   )r5   r   r:   addupdate_pr<   )r.   r/   xr#   r$   r=   r9   s          r4   r`   r`      sH     +;_M/#z#00</4			#q	>?	>	>>r6   c                >    |D ]  }|j                  |       }  | J | S rR   )transform_shape)r-   r$   	transforms      r4   _shape_after_transformingrf      4      -i%%e,E-				,r6   c                >    |D ]  }|j                  |       }  | J | S rR   )transform_dtype)rJ   r$   re   s      r4   _dtype_after_transformingrj      rg   r6   c                >    |D ]  }|j                  |       }  | J | S rR   )transform_sharding)shardingr$   re   s      r4   _sharding_after_transformingrn      s2     6i++H5H6				/r6   c                  t        j                  ||      }t        | t              st	        d|  d      t        | j
                  t        j                        rat        | j                  |      }t        | j                  |      }t        | j                  |      }| j
                  j                  |||      }n|rt	        d      | j
                  }|t        d      hfS )Nz%`get` must be called on `Ref` types: r    )r-   rJ   rm   6Cannot index non-shaped array with nontrivial indices.r   )r   tree_unflattenr"   r   r&   r'   r   r(   rf   r-   rj   rJ   rn   rm   updater   )r2   r9   argsr$   	out_shape	out_dtypeout_shardingout_avals           r4   _get_abstract_evalrx      s    ''d3*	Hk	*
<XJaH
II##T%5%56)(..*EI)(..*EI/0A0A:NL""))y< * AH OPP""H
Z]O	$$r6   c                  t        j                  ||      }t        | t              st	        d|  d      t        | j
                  t        j                        rt        |t        j                        sJ t        | j                  |      }t        | j                  |      }||j                  k7  r,t	        d| j                   d| d|j                   d| d	      ||j                  k7  rt	        d| d	|j                   d      t        j                  ||      }n|rt	        d
      | j
                  }|t        d      hfS )Nz&`swap` must be called on `Ref` types: r    z%Invalid shape for `swap`. Ref shape: . Expected shape: . Value shape: . Transforms: . z%Invalid dtype for `swap`. Ref dtype: z. Value dtype: rp   r   )r   rq   r"   r   r&   r'   r   r(   rf   r-   rj   rJ   r   )r2   val_avalr9   rs   r$   expected_out_shapeexpected_out_dtyperw   s           r4   _swap_abstract_evalr      sT    ''d3*	Hk	*
=hZqI
JJ##T%5%56h 0 011128>>:N28>>:NX^^+ %%-^^$4 5**<)= >''/~~&6 7&&0\	5 6 6
 X^^+*+ ,"..)- 
  24FGHOPP""H
[^$	%%r6   c               Z   t        j                  ||      }t        | t              st	        d|  d      t        | j
                  t        j                        rt        | j                  |      }t        | j                  |      }t        |t        j                        sJ ||j                  k7  r,t	        d| j                   d| d|j                   d| d	      ||j                  k7  r3t	        d| j                   d|j                   d      |rt	        d	      g t        d
      hfS )Nz+`addupdate` must be called on `Ref` types: r    z*Invalid shape for `addupdate`. Ref shape: rz   r{   r|   r}   z*Invalid dtype for `addupdate`. Ref dtype: rp   r   )r   rq   r"   r   r&   r'   r   r(   rf   r-   rj   rJ   r   )r2   r~   r9   rs   r$   rt   ru   s          r4   _addupdate_abstract_evalr     s6    ''d3*	Hk	*
B8*AN
OO##T%5%56)(..*EI)(..*EIh 0 0111HNN" ' (&K ("..) *#B	(  HNN" %%-^^$4 5''/~~&6b: ; ;
 OPP	k!n	r6   )	intensity
foregroundc                    |st        j                  d      S t        j                  |D cg c]  }|j                  |        c}      S c c}w )Nz[...])pptextconcatpretty_print)contextr$   re   s      r4   _pp_transformsr   <  sC     
777	8BC9yg&C
 Cs   A
c           
         t        t        j                  t        j                  t	        j
                  ||             t        | |      g            S rR   )
pp_ref_varr   r   r   r   pp_varr   )r   r#   r$   s      r4   pp_ref_transformsr   G  sA    	ii
''$++c7+
,
*
- 	
 r6   c           	     2   | j                   \  }| j                  ^}}t        j                  | j                  d   |      }t        j                  |g||j                        }t        j                  |t        j                  d      t        |||      g      S )Nr9   print_shapes <- )rE   invarsr   rq   paramsr   pp_varsr   r   r   r   r   )eqnr   settingsyrb   flat_idxr$   lhss           r4   _get_pp_ruler   P  s|    
{{"!,!h''

6(:HE*aS'0E0EF#	
BGGFO.w:FG
 r6   c                   | j                   \  }| j                  ^}}}t        j                  | j                  d   |      }t        |      t        j                  u r^~t        j                  t        |||      t        j                  d      t        j                  t        j                  ||            g      S t        |||      }t        j                  |g||j                        }t        j                  |t        j                  d      |t        j                  d      |t        j                  d      t        j                  t        j                  ||            g      S )Nr9   r   r   z, )rE   r   r   rq   r   typer   DropVarr   r   r   r   r   r   r   )	r   r   r   r   rb   vr   r$   x_is	            r4   _swap_pp_ruler   [  s   
{{"!JJ/!Q''

6(:HE*	!W 	
99'1j1

Aw'(   GQ

3CaS'0E0EFA99aRWWV_2774=ggdkk!W568 9 9r6   c           
     <   ~| j                   \   | j                  ^}}}t        j                  | j                  d   |      }t        j                  t        |||      t        j                  d      t        j                  t        j                  ||            g      S )Nr9   z += )rE   r   r   rq   r   r   r   r   r   r   r   )r   r   r   rb   r   r   r$   s          r4   _addupdate_pp_ruler   q  s|    
{{"JJ/!Q''

6(:HE*	J/ggfoggdkk!W%& 
 r6   c                    | ^}}t        |j                  t              sJ |^}}t        |j                  t              sJ t        j                  |g|i |t        j                  |g|i |fS rR   )r"   rZ   r   r;   r<   )primalstangentsr   
ref_primalr/   ref_tangent_s          r4   _get_jvpr     su    *s	JOO[	11	1/+	K$$k	22	2
**Z
0#
0
0
**[
13
1&
1
3 3r6   c                   | ^}}}t        |j                  t              sJ |^}}}t        |j                  t              sJ t        j                  |      }t        j                  ||g|i |t        j                  ||g|i |fS rR   )r"   rZ   r   r   instantiaterM   r<   	r   r   r   r   x_primalr/   r   	x_tangentr   s	            r4   	_swap_jvpr     s    &*h	JOO[	11	1'+y1 
K$$k	22	2!!),)
++j(
;S
;F
;
++k9
=s
=f
=
? ?r6   c                    | ^}}}|^}}}t        j                  |      }t        j                  ||g|i | t        j                  ||g|i | g g fS rR   )r   r   ra   r<   r   s	            r4   addupdate_jvp_ruler     sg    &*h'+y1!!),):x8#88;	:C:6:	R-r6   c                    t        |       t        j                  urt        j                  || g|i | d gd gt        |      z  z   S rR   )r   r   Zerora   r<   rB   )gr#   r/   r   s       r4   _get_transposer     sD    	!WGLL S!,c,V,
4&3s8#	##r6   c                    ~t        j                  |t        j                  |       g|i |}d |gd gt	        |      z  z   S rR   )rM   r<   r   r   rB   )r   r#   rb   r/   r   x_bars         r4   _swap_transposer     sD    
++c7..q1
BC
B6
B%
$#c(*	**r6   c                `    ~ ~t        j                  |g|i |}d |gd gt        |      z  z   S rR   )r;   r<   rB   )cts_inr#   rb   r/   r   r   s         r4   addupdate_transposer     s9    ajj%s%f%!
dVc#h&	&&r6   c                   t        |      r`t        |j                  |      D cg c]
  \  }}|r	| }}}d |dgt        |j                        z  dgt        |j                        z  |fS  || g|j                  D cg c]  }|j
                   c}i |j                  r5|d dgt        |j                        z  dgt        |j                        z  g fS t        |j                  |      D cg c]
  \  }}|r	| }}}||dgt        |j                        z  dgt        |j                        z  |fS c c}}w c c}w c c}}w )NTF)anyzipr   rB   rE   rZ   r   )	primsaveableunks_ininst_inr   r   instresvars	            r4   _state_partial_eval_customr     s   \

G4
ADD1
AC
Atfs3;;//$#ckk:J1JCOOFCJJ7S7F3::FugCKK 005'C<L2LbPPcjj'2?wq$$?#?	cE7S--vCKK8H/H#	MM B7?s   
D9D9>D?
2
E=Ec           	        | j                   }|j                   }g }|g| j                  }	t        ||      D ]  \  }
}|rst        |
t        j
                        rst        t        j                  |
            dk(  }|r|j                  |
       Y|j                  }|t        j                  u r|j                  |
       t        d|
       t        |
d      sJ |
j                  sM|t        j                  ur)|
j                  |fk(  sJ t!        j"                  |
|	d      }
|j                  |
       |t        j                  u rCt%        t'        dt)        j*                  |
      dz               }t!        j"                  |
|	|      }
nt        j,                  |
|d      }
|j                  |
       }|t        j                  urvt        |
t        j
                        s\t        |
d      sJ |
j                  rBt%        t'        dt)        j*                  |
      dz               }t!        j"                  |
|	|      }
|j                  |
        |t        j                  ur<t!        j.                  t)        j0                  d      |	d      }|j3                  ||       t	        j4                  t%        |      ||	d      S )	a  Converts a batched indexer into an unbatched one.

  This function handles the complexity of `vmap`-style batching where either the
  `ref` being indexed, the indexer, or both may have batched dimensions. The
  goal is to produce a new indexer that acts as if applied in a batched context,
  but without actual batching, enabling downstream code to process it as usual.

  If any index in `indexer` is batched, all array indexers are normalized. If
  the array indexer contains a batched dimension, the dimension is moved to the
  front (axis 0). If the array indexer not batched, it is broadcasted to include
  a batch dimension at the front. This is to guarantee that all array indexers
  are still of the same shape.

  Slices are passed through unchanged unless they contain dynamic elements and
  are themselves batched, which is currently unsupported.

  If `ref` is batched (`ref_dim` is not `NotMapped`), we simulate per-example
  indexing by inserting a new iota array at the position corresponding to
  `ref_dim` in the indexer.

  It is worth noting that if the array indexers in the original indexer are
  contiguous, but become non-contiguous in the new indexer due to the insertion
  of the iota, the dimensions corresponding to the array indexers will be moved
  to the front in the indexing result. The batched dimension will be at axis 0,
  while the dimensions corresponding to the array indexers in the original
  indexer will start from axis 1. This behavior would cause a mismatch between
  the original indexer and the new indexer. Callers must take this behavior into
  account and properly transpose the arrays involved to avoid this mismatch.

  Args:
    indexer: An `NDIndexer` that indexes into `ref`.
    dims: A pytree with the same structure as `indexer`, indicating which
      dimension (if any) is batched for each array indexer.
    axis_size: Size of the batch dimension.
    ref_shape: Shape of `ref`.
    ref_dim: The dimension of `ref` that is batched (if any).
    idx_is_batched: Whether any index in the `indexer` is batched.
  r   z9No support for vmapping over nontrivial slices just yet: r-   )r   rA   int32T)validate)indicesint_indexer_shaper   r"   r   SlicerB   r   tree_leavesappendstartr   
not_mappedNotImplementedErrorrK   r-   r   broadcast_in_dimr*   rangerV   ndimmoveaxisbroadcasted_iotarJ   insertr+   )indexerdims	axis_size	ref_shaperef_dimidx_is_batchedr   indices_dimsnew_indicesnew_integer_indexer_shaper/   dimis_static_slice
bcast_dimsiotas                  r4   _batch_indexerr     sj   \ OO',46+(E7+D+DEg|, +hc3 
C	( i33C89Q>


S
!
ii(%%%


S
!#I#OQ Q
 sG$$$yy+++99,,,&&s,EtLC


S
!H'''uQq(89:J&&s,E'13C ##Ca0C


S
!	++	+#x~~.g&
&&YYuQq(89:J&&s,E&02CW+X H''' 13LaPDw%			K)%>
 r6   c          	        t        | |      D ch c]&  \  }}|t        j                  ur|j                  |   ( c}}\  | ^}|^}t	        j
                  ||      }t	        j
                  ||      }t        d |D              t        |      dkD  rt        d      t        fdt        ||      D              }	t	        j                  |	      \  }
}t        j                  |d         \  }}}t        t        j                  t        j                   t        j"                  |      d         dk(              }t        j                  |	d         \  }}}t        t        j                  t        j                   t        j"                  |      d         dk(              }t%        j&                  g|
d|i}|sd}||fS |r|j)                  d      }||fS |j)                  d      }|	d   j*                  }t        |      }t-        t/        t        |j                                    }|d   g||||z    |d| |||z   d  }t1        j2                  ||      }d}||fS c c}}w )Nc              3  @   K   | ]  }|t         j                  u  y wrR   r   r   .0i_dims     r4   	<genexpr>z_get_vmap.<locals>.<genexpr>6  #      3  H$7$77 3   rA   .Batching with multiple indexers not supported.c           	   3  X   K   | ]!  \  }}t        ||j                         # y wrR   r   r-   r   r   r   r   r   r#   r   s      r4   r   z_get_vmap.<locals>.<genexpr>;  6      H&$ &gtY"%))WnF H   '*r   r9   T)r   r   r   r-   r   rq   r   rB   r   r*   r:   r   unpack_ndindexerboolrV   alldiffwherer;   r<   indexr   listr   r   	transpose)batched_argsbatched_dimsr9   rb   d	flat_idxsflat_idx_dimsindexersindexers_dimsnew_indexersflat_indexersis_int_indexingr   int_indexers_contiguousis_new_int_indexingnew_int_indexers_contiguousoutout_bdimoriginal_posarray_indexer_shapearray_indexer_lentranspose_orderr   r   r#   r   s                         @@@@r4   	_get_vmapr  .  s   '*<'F 1tq!H/// 
 1*) /#	('M%%dI6(**4?- 3$13 3.]Q
N
OO H*-h*FH H, "..|<-"33HQK@/1a ffRWWRXXo.q12a78 '77QHq! $ffRWWRXX12156!;<! 	

322T2#	 H& 
h# #$**40h  
h %**40l(O==12U3syy>23o
!
,->|-KL 1./ ,\9:;	o MM#/ch	h_1s   +I+c          	     H   t        | |      D ch c]&  \  }}|t        j                  ur|j                  |   ( c}}\  | ^}}|^}}t	        j
                  ||      }	t	        j
                  ||      }
t        j                  u}|t        j                  u}t        d |D              |s"t        dt        j                         d      t        |	      dkD  rt        d      t        fdt        |	|
      D              }t	        j                  |      \  }}t        j                  |	d         \  }}}t!        t#        j$                  t#        j&                  t#        j(                  |      d         dk(              }t        j                  |d         \  }}}t!        t#        j$                  t#        j&                  t#        j(                  |      d         dk(              }|sd}n|j+                  d      dz   }|s|sr/t        j,                  ||      }nt        j.                  |||      }d }|r|s|j+                  d      }|d   j0                  }t        |      }t3        t5        t        |j                                    }|d   g|d|z   d|z   |dz
  z    |dd|z    |d|z   |dz
  z   d  }|j7                  |      }t#        j8                  |      }t;        j<                  |g|d	|i}||j7                  |      }||fS c c}}w )
Nc              3  @   K   | ]  }|t         j                  u  y wrR   r   r   s     r4   r   z_swap_vmap.<locals>.<genexpr>k  r   r   zcperforming a set/swap operation with vmapped value on an unbatched mutable array reference of type zC. Move the mutable array to be an argument to the vmapped function?rA   r   c           	   3  X   K   | ]!  \  }}t        ||j                         # y wrR   r   r   s      r4   r   z_swap_vmap.<locals>.<genexpr>w  r   r   r   Tr9   )r   r   r   r-   r   rq   r   	Exceptionr   rS   rB   r   r*   r:   r   r   r   rV   r   r   r   r   	broadcastr   r   r   r   r   argsortrM   r<   )r   r  r9   rb   r  valr  val_dimr  r  r  ref_is_batchedval_is_batchedr  r  r	  r   r
  r  r  batched_dim_in_resulttranspose_order_inversedr  r  r  r  r  r   r   r#   r   s                              @@@@r4   
_swap_vmapr   a  s?   '*<'F 1tq!H/// 
 1*)%#sY%1"'7]%%dI6(**4?-("5"55.("5"55. 3$13 3. 

 #{{3/0 1;; < <
 	]Q
N
OO H*-h*FH H, "..|<-"33HQK@/1a ffRWWRXXo.q12a78 '77QHq! $ffRWWRXX12156!;<! 
%/55d;a?	sI/DEc


C*?
@C! %@"((.L&q/;;/05SYY01O	<<:KA:M(N	O 
1\>	* 
!L.+<Q+>?@	A	O --
(C!zz/:C8}848#
 )
--0
1C	#	##S1s   +Lc          	        t        | |      D ch c]&  \  }}|t        j                  ur|j                  |   ( c}}\  | ^}}|^}}t	        j
                  ||      }	t	        j
                  ||      }
t        j                  u}|t        j                  u}t        d |D              t        |	      dkD  rt        d      t        fdt        |	|
      D              }t	        j                  |      \  }}t        j                  |	d         \  }}}t        t        j                  t        j                   t        j"                  |      d         dk(              }t        j                  |d         \  }}}t        t        j                  t        j                   t        j"                  |      d         dk(              }|sd}n|j%                  d      }|s|sr/t        j&                  ||      }nt        j(                  |||      }|r|s|j%                  d      }|d   j*                  }t        |      }t-        t/        t        |j                                    }|d   g|d|z   d|z   |dz
  z    |dd|z    |d|z   |dz
  z   d  }|j1                  |      }t3        j4                  |g|d|ig fS c c}}w )Nc              3  @   K   | ]  }|t         j                  u  y wrR   r   r   s     r4   r   z"_addupdate_vmap.<locals>.<genexpr>  r   r   rA   r   c           	   3  X   K   | ]!  \  }}t        ||j                         # y wrR   r   r   s      r4   r   z"_addupdate_vmap.<locals>.<genexpr>  r   r   r   Tr9   )r   r   r   r-   r   rq   r   rB   r   r*   r:   r   r   r   rV   r   r   r   r   r  r   r   r   r   r   ra   r<   )r   r  r9   rb   r  r  r  r  r  r  r  r  r  r  r  r	  r   r
  r  r  r  r  r  r  r  r   r   r#   r   s                            @@@@r4   _addupdate_vmapr$    s   '*<'F 1tq!H/// 
 1*)%#sY%1"'7]%%dI6(**4?-("5"55.("5"55. 3$13 3.]Q
N
OO H*-h*FH H, "..|<-"33HQK@/1a ffRWWRXXo.q12a78 '77QHq! $ffRWWRXX12156!;<! 
%/55d;	sI/DEc


C*?
@C %@"((.L&q/;;/05SYY01O	<<:KA:M(N	O 
1\>	* 
!L.+<Q+>?@	A	O --
(C			#s	>]	>	>	BBo1s   +Kbroadcast_toc                    dd l m} |j                  |       } | j                  |k(  r| S t        j                  | |      S )Nr   )r-   )	jax.numpynumpyasarrayr-   broadcast_to_pr<   ar-   jnps      r4   r%  r%    s9    	kk!n!WWH			Qe		,,r6   c               2    dd l m} |j                  | |      S )Nr   )r'  r(  r%  r+  s      r4   _broadcast_to_implr/    s    			!U	##r6   c               B    t        j                  || j                        S rR   )r   r(   rJ   )rZ   r-   s     r4   _broadcast_to_abstract_evalr1    s    			%	,,r6   Fc                N   | |c\  }\  }t         j                  j                  |      }t        |      t        j
                  u r@t         j                  j                  t	        j                  |j                              }||fS t         j                  j                  |      }||fS rR   )r   mutable_array_pr<   r   r   r   zeros_like_avalrZ   )r   r   init_valinit_val_dot
primal_outtangent_outs         r4   _mut_jvpr9    s    !((+8##((2*	,7<<'&&++G,C,CLDUDU,VWK 
[	   &&++L9K	[	  r6   c                ,    t        j                  |       S rR   )r   freeze)r   r   s     r4   <lambda>r<    s    dkk!n r6   )T)
r.   r   r/   $Indexer | tuple[Indexer, ...] | Noner0   strr1   r   returnz!tuple[Any, tuple[Transform, ...]]rR   )r.   r   r/   r=  r?  r   r]   )
r.   AbstractRef | TransformedRefr/   r=  rN   r   rO   r>  r?  r   )r.   r@  r/   r=  rN   r   r?  None)r.   r   r/   r=  rb   r   r?  rA  )r-   tuple[int | Array, ...]r$   tuple[Transform, ...]r?  rB  )rJ   r   r$   rC  r?  r   )r2   r   )r2   r   r~   zcore.AbstractValuers   r   )r   core.JaxprPpContextr$   rC  )r   rD  )r?  zpp.Doc)r   	list[Any]r   rE  r   r   )r   indexing.NDIndexerr   intr   tuple[int, ...]r   zint | batching.NotMappedr   r   r?  rF  )r,  r   r-   rH  r?  r   )q__doc__
__future__r   	functoolsr   typestypingr   r   r(  rV   jax._srcr   r   r	   r
   r   r   r   r   jax._src.interpretersr   r   r   r   pejax._src.laxr   jax._src.stater   jax._src.state.typesr   r   r   r   r   r   jax._src.typingr   jax._src.utilr   r   map
unsafe_mapr   
unsafe_zipregister_exclusion__file__	Primitiver;   def_implapply_primitiveragged_mask_transfer_identityragged_prop_rulesrG  sliceEllipsisTypeIndexerr5   r8   rM   rH   rP   rL   r\   ra   multiple_resultsr`   rf   rj   rn   rx   def_effectful_abstract_evalr   r   color	IntensityNORMALColorGREENr   r   r   r   pp_eqn_rulesr   r   r   primitive_jvpsr   r   r   primitive_transposesr   r   r   partial_eval_jaxpr_custom_rulesr   r  primitive_batchersr   r$  r*  r%  r/  def_abstract_evalr1  register_lowering	lower_funr9  r3  defjvpfreeze_pr   r6   r4   <module>rt     st   # "         ) #  $ * & 4  #  " , CZCZ ! ! !( + 	u wx//7 8$,$J$J  5 !
UE5#5#55
6 $(	((	-( ( !	(
 '(< CG66?6
62 
	 00&9 :> &;  6 " %	>->	-> > 	>
 > >->	-> > 
	>( dnn[)#    WX55{C D??	-? ? 
	?"0E1%   ! !"4 5&"4&"&:  " "#6 7'9$'6  ' '(@ A RXX)<)<HHNN,
 % (  % 9( *  & 
 "4  + 3 $  % 	? &  &  "4  + $
 "0   +
 #2   '
 (;   $N -44N49-; " "5 )-45O5;.= " "6 *293- " "; /
cc c 	c
 &c c cJ0b &/  E "J$V '1  F #8Cr ,;  K (  /- $ $ !!- "-   NDNN#5u=! +3  $&& ' 			$--4 5r6   