
    uki                    4   d Z ddlmZ ddlmZmZ ddlmZ ddlZddlZddl	Z	ddl
Z
ddlmZ ddlmc 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  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-m0Z0 ddl-m1Z2 ddl-m3Z4 ddl5m6Z6 ddlm7Z8 e0jr                  Z9e0jt                  Z:e jv                  e<cZ<Z=e j|                  e?cZ?Z@ ej                  d      ZBdd ZCd! ZDeBj                  eD       eBj                  d"        ZG ej                  d#      ZHdd$ZId% ZJeHj                  eJ       eHj                  d&        ZK G d' d(ej                        ZM ej                  d)      ZN	 	 dd*ZO  e/j                  eN      eO       eNj                  dd+       ZRdd,	 dd-ZSdd,dd.ZTdd,dd/ZUdd,dd0ZVdd,dd1ZWdd,dd2ZXdd,dd3ZYdd,dd4ZZ ej                  d5      Z[e[j                  d6        Z\d7 Z] e/j                  e[      d8        Z^ ej                  d9      Z_e_j                  d:         e6j                  e_d;        d< Zbe_j                  d=        Zc ej                  d>      Zdedj                  d?         e6j                  edd@        ddAZeedj                  dB        Zf ej                  dC      Zgegj                  dD        ZhdE Zieiej                  eg<   dF Zkeke"j                  eg<   dG Zm	 ddIZn ej                  endJK      Zp e/j                  eg      dL        Zq ej                  dM      Zrerj                  dN        ZsdO Ztetej                  er<   dP Zueue"j                  er<    e/j                  er      dQ        ZvdddddHdR	 ddSZwdddTdU	 ddVZxdddWddXZyddYZz	 	 d	 ddZZ{ ej                  d[      Z|dHd\d]Z}e|j                  d^        Z~dHd\	 dd_Z e6j                  e|e       dd`Z	 	 	 	 ddaZej                  db        Z ej                  dc      ZdJe_        dd Zee_        de Zee_        dfdg	 	 	 	 	 	 	 	 	 ddhZej                  di        Zdj Z  e/j                  e      e        ej                  e6j                  e      dk        Z ej                  dl      ZdHe_        dJe_        ej"                  j%                  e       ddmZej                  dn        Zdo Z  e/j                  e      e       dp Z G dq drej                        Z	 ddsZdt Z ej                  du      ZdHe_        ddvZej                  dw        Zdx Z  e/j                  e      e       eej>                  z  dz  eeej>                  z  dyf   z  eeeej>                  z  f   z  Z ej                  dz      ZdJe_        	 ddejH                  dd{	 	 	 	 	 	 	 dd|Zej                  	 	 dd}       Z	 	 	 	 dd~Zeej                  e<   d Z  e/j                  e      e        ej                  d      ZdJe_        	 ddJd	 	 	 ddZej                  d        Z	 	 	 	 ddZeej                  e<   d Z  e/j                  e      e       ddZ	 	 	 	 	 	 	 	 ddZ ej                  d      ZdJe_        ej                  d        ZddZy)zPallas-specific JAX primitives.    )annotations)CallableSequence)HashableN)Any)	tree_util)ad_util)api_util)core)config)	debugging)dtypes)typing)effects)linear_util)pretty_printer)state)util)ad)partial_eval)ir)arith)utils)	discharge)indexing)
primitives)types)mlir)numpy
program_idc                .    t         j                  |       S )a  Returns the kernel execution position along the given axis of the grid.

  For example, with a 2D ``grid`` in the kernel execution corresponding to the
  grid coordinates ``(1, 2)``,
  ``program_id(axis=0)`` returns ``1`` and ``program_id(axis=1)`` returns ``2``.

  The returned value is an array of shape ``()`` and dtype ``int32``.

  Args:
    axis: the axis of the grid along which to count the program.
  axis)program_id_pbindr"   s    U/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/pallas/primitives.pyr    r    >   s     
				%%    c                   |j                  d      }t        j                         }|r||   j                  S t        j                         }|j                  |      }t        j                  j                  t        | dt        |            S Nr#    r"   )poppallas_corecurrent_grid_envindex
axis_framesizejax_core	Primitivebind_with_tracer$   dict)trace_paramsr#   grid_envframes         r&   program_id_bind_with_tracer:   L   sp    	F	$))+(D>

 
 
"% jj!				+	+L%Tt_	UUr'   c                 J    t        j                  dt        j                        S Nr*   r1   ShapedArrayjnpint32r6   s    r&   _program_id_abstract_evalrB   Y       			b#))	,,r'   num_programsc                .    t         j                  |       S )z2Returns the size of the grid along the given axis.r"   )num_programs_pr%   r"   s    r&   rD   rD   _   s    			$		''r'   c                @   |j                  d      }t        j                         }|r||   j                  S t        j                         }|j                  |      }|t        j
                  u r0t        j                  j                  t        | dt        |            S |S r)   )r+   r,   r-   r0   r/   dynamic_grid_dimr1   r2   r3   rF   r4   )r5   r6   r7   r#   r8   r9   r0   s          r&   _num_programs_bind_with_tracerI   c   s    	F	$))+(D>

 
 
"%	D	$	[)))--neRSWYY	+r'   c                 J    t        j                  dt        j                        S r<   r=   rA   s    r&   _num_programs_abstract_evalrK   q   rC   r'   c                  (    e Zd ZdZdZdZdZdZdZdZ	y)	AtomicOpTypexchgaddmaxminandorxorN)
__name__
__module____qualname__XCHGADDMAXMINANDORXORr*   r'   r&   rM   rM   u   s%    	$####"#r'   rM   
atomic_rmwc               B   ~|j                  |      \  }}}}|^ }	}
t        j                  ||	      }|t        |t        j
                  k(  rd }nS|t        j                  k(  rt        j                  }n/|t        j                  k(  rt        j                  }nt        |      t        d |
j                  D              r|
j                  }|D cg c]"  }t        |t               xr |j                   $ }}|D cg c]   }t        |t              r|j                   n|" }}t#        d |D              }t%        j&                  |||      }t#        d |D              }||   } |||      }t%        j(                  |||      }t#        d |D              }||   }nat        d |
j                  D              r?||
j                     }|j*                  |
j                     j-                   |||            }nt        |fd	t/        |       d
z
  z  z   |fS c c}w c c}w )Nc                    | |z   S Nr*   )xys     r&   <lambda>z,_atomic_rmw_discharge_rule.<locals>.<lambda>   s
    !a% r'   c              3  Z   K   | ]#  }t        |t              xs |j                    % y wrb   
isinstanceSliceshape.0ss     r&   	<genexpr>z-_atomic_rmw_discharge_rule.<locals>.<genexpr>   %     D1*Q

-agg+
-D   )+c              3  X   K   | ]"  }t        |t              r|j                  nd  $ yw   Nrh   ri   r0   rk   s     r&   rn   z-_atomic_rmw_discharge_rule.<locals>.<genexpr>   "     O!*Q"6A=O   (*slice_sizesc              3  :   K   | ]  }|rd n
t        d         y wrb   slicerl   scalars     r&   rn   z-_atomic_rmw_discharge_rule.<locals>.<genexpr>   s     RFE$K7R   start_indicesc              3  :   K   | ]  }|rd n
t        d        ywr   Nrz   r|   s     r&   rn   z-_atomic_rmw_discharge_rule.<locals>.<genexpr>        OVt4Or~   c              3  >   K   | ]  }t        |t                 y wrb   rh   ri   rk   s     r&   rn   z-_atomic_rmw_discharge_rule.<locals>.<genexpr>        
9z!U##
9   rb   rs   )	unflattenstate_dischargetransform_arrayNotImplementedErrorrM   rY   rZ   r?   maximumr[   minimumallindicesrh   ri   rj   starttuplelaxdynamic_slicedynamic_update_sliceatsetlen)in_avals	out_avals	args_treeatomic_type	args_flatref
transformsvalmaskprev_transformsidxmonoidr   rm   scalar_dimsslice_startsrx   out_onesval_indexerx_newout_indexerouts                         r&   _atomic_rmw_discharge_ruler      s    (229=#z3$?C''_=#	
L$$$Fl&&&[[Fl&&&[[F
k
**DDDkkGELMz!U++;AGG;MKMDKLqz!U3AGG:LLLOwOOK  lLHRkRRK
k
C
h
C$$S#\JEO;OOK
;
C

9S[[
99
ckk
CFF3;;##F3$45E

Gs8}q01	13	66 NLs   'H>%Hc                   | j                  |      \  }}}}|j                  t        j                  d      k(  r,|t        j                  k7  rt        d|j                   d      |j                  t        j                  d      t        j                  d      t        j                  d      t        j                  hv r&t        d|j                   d|j                   d      t        |d	| iS )
Nfloat16z`atomic_z` does not support f16.boolint8int16z` does not support .r   )	r   dtyper?   rM   rY   
ValueErrorvaluebfloat16_swap_abstract_eval)r   r   
avals_flatr   r6   s        r&   _atomic_abstract_evalr      s    $$Z0,#q!QYY#))I&&;,:J:J+J
x 1 122IJ
KKYY	ii	ii	ii	ll	  
;$$%%81E  
j	>I	>>r'   )r   c                   t        j                  | |d      \  }}t        j                  ||||f      \  }}t	        j
                  |||dS )Nr_   )r   r   )spget_ref_and_transformsr   tree_flattenatomic_rmw_pr%   )	x_ref_or_viewr   r   r   r   x_refr   r   r   s	            r&   _atomic_rmwr      sX    //S,% #//
C0NO)Y			I;
 r'   c               >    t        | |||t        j                        S )zAtomically exchanges the given value with the value at the given index.

  Args:
    x_ref_or_view: The ref to operate on.
    idx: The indexer to use.
    mask: TO BE DOCUMENTED.

  Returns:
    The value at the given index prior to the aupdate.
  r   r   )r   rM   rX   r   r   r   r   s       r&   atomic_xchgr      s"     
S#Dl6G6G
 r'   c               >    t        | |||t        j                        S )zAtomically computes ``x_ref_or_view[idx] += val``.

  Args:
    x_ref_or_view: The ref to operate on.
    idx: The indexer to use.
    mask: TO BE DOCUMENTED.

  Returns:
    The value at the given index prior to the atomic operation.
  r   )r   rM   rY   r   s       r&   
atomic_addr      "     
S#Dl6F6F
 r'   c               >    t        | |||t        j                        S )a  Atomically computes ``x_ref_or_view[idx] = max(x_ref_or_view[idx], val)``.

  Args:
    x_ref_or_view: The ref to operate on.
    idx: The indexer to use.
    mask: TO BE DOCUMENTED.

  Returns:
    The value at the given index prior to the atomic operation.
  r   )r   rM   rZ   r   s       r&   
atomic_maxr      r   r'   c               >    t        | |||t        j                        S )a  Atomically computes ``x_ref_or_view[idx] = min(x_ref_or_view[idx], val)``.

  Args:
    x_ref_or_view: The ref to operate on.
    idx: The indexer to use.
    mask: TO BE DOCUMENTED.

  Returns:
    The value at the given index prior to the atomic operation.
  r   )r   rM   r[   r   s       r&   
atomic_minr      r   r'   c               >    t        | |||t        j                        S )zAtomically computes ``x_ref_or_view[idx] &= val``.

  Args:
    x_ref_or_view: The ref to operate on.
    idx: The indexer to use.
    mask: TO BE DOCUMENTED.

  Returns:
    The value at the given index prior to the atomic operation.
  r   )r   rM   r\   r   s       r&   
atomic_andr     r   r'   c               >    t        | |||t        j                        S )zAtomically computes ``x_ref_or_view[idx] |= val``.

  Args:
    x_ref_or_view: The ref to operate on.
    idx: The indexer to use.
    mask: TO BE DOCUMENTED.

  Returns:
    The value at the given index prior to the atomic operation.
  r   )r   rM   r]   r   s       r&   	atomic_orr     s      
S#Dloo
 r'   c               >    t        | |||t        j                        S )zAtomically computes ``x_ref_or_view[idx] ^= val``.

  Args:
    x_ref_or_view: The ref to operate on.
    idx: The indexer to use.
    mask: TO BE DOCUMENTED.

  Returns:
    The value at the given index prior to the atomic operation.
  r   )r   rM   r^   r   s       r&   
atomic_xorr   '  r   r'   
atomic_casc                   |j                   |j                   k7  s|j                  |j                  k7  rt        d      | j                  rt        d      |j                  rt        d      |j                  rt        d      t        j                  |j                  |j                         t        j                  d      hfS )Nz1cmp and val must have identical dtypes and shapeszref must be scalar.zcmp must be scalar.zval must be scalar.r   )r   rj   r   r1   r>   r   WriteEffect)ref_avalcmp_avalval_avals      r&   _atomic_cas_abstract_evalr   8  s    ^^x~~%8>>)I
H
II^^
*
++^^
*
++^^
*
++			hnnhnn	=@Q@QRS@T?U	UUr'   c                0    t         j                  | ||      S )a  Performs an atomic compare-and-swap of the value in the ref with the
  given value.

  Args:
    ref: The ref to operate on.
    cmp: The expected value to compare against.
    val: The value to swap in.

  Returns:
    The value at the given index prior to the atomic operation.
  )atomic_cas_pr%   )r   cmpr   s      r&   r   r   E  s     
		3S	))r'   c                H    ~ ~t        j                  ||k(  ||      }|d d f|fS rb   )r?   where)r   r   r   r   r   new_vals         r&   _atomic_cas_discharge_ruler   S  s/    	IIcSj#s+'
4		##r'   max_contiguousc                    | S rb   r*   rc   r6   s     r&   re   re   [  s     r'   c                    |gS rb   r*   r6   rc   __s      r&   re   re   \  s    QC r'   c                t    t        |t        t        f      s|f}t        j	                  | t        |            S )zTA compiler hint that asserts the ``values`` first values of ``x`` are contiguous.
  values)rh   listr   max_contiguous_pr%   rc   r   s     r&   r   r   ^  s3     
FT5M	*YF			qv		77r'   c                    | S rb   r*   avalr6   s     r&   _max_contiguous_abstract_evalr   e      	+r'   multiple_ofc                    | S rb   r*   r   s     r&   re   re   k  s    a r'   c                    |gS rb   r*   r   s      r&   re   re   l  s    ! r'   c                l    t        |t              r|fn
t        |      }t        j	                  | |      S )aT  A compiler hint that asserts a value is a static multiple of another.

  Note that misusing this function, such as asserting ``x`` is a multiple of
  ``N`` when it is not, can result in undefined behavior.

  Args:
    x: The input array.
    values: A set of static divisors that ``x`` is a multiple of.

  Returns:
    A copy of ``x``.
  r   )rh   intr   multiple_of_pr%   r   s     r&   r   r   n  s0     #63/F9U6]&			Af		--r'   c                    | S rb   r*   r   s     r&   _multiple_of_abstract_evalr   ~  r   r'   masked_loadc                    | j                  |      \  }}}}|J t        j                  ||      }t        j                  |j
                  |j                        t        j                  d      hfS )Nr   )	r   r,   TransformedRefr1   r>   rj   r   r   
ReadEffect)r   r   r6   r   r   transformed_refs         r&   _load_abstract_evalr     sp    #--j9#z1a			..sJ?/?00/2G2GH
 r'   c           	        | j                   \  }t        j                  | j                  d   | j                        \  }}}}t        j                  |g||j                        }|t        j                  d      t        j                  |||      g}	|U|	t        j                  d      t        j                  d      t        j                  t        j                  ||            gz  }	|U|	t        j                  d      t        j                  d      t        j                  t        j                  ||            gz  }	t        j                  |	      S )Nr   print_shapes <-  mask=zother=)outvarsr   tree_unflattenr7   invarsr1   pp_varsr  pptextr   pp_ref_transformspp_varconcat)
eqncontextsettingsrd   rc   r   r   otherlhsresults
             r&   _load_pp_ruler    s   
{{"!(77	jjszz !Zu 	!gH4I4IJ#""6"6w:"NO&	



g./ F
 



w/0 F
 
6	r'   c           	     @   |j                  |       \  }}}}|j                  |      \  }}	}	}
|
t        j                  |
      }
t        j                  t        j                  ||||f      d|i|t        j                  t        j                  ||||
f      d|i|fS Nr   )r   r	   instantiateload_pr%   r   tree_leaves)primalstangentsr   r7   
ref_primalr   r   other_primalref_tangentr6   other_tangents              r&   	_load_jvpr     s    /8/B/B7/K,*j$%.%8%8%B"+q!]''6Mkk  *j$!MN 
 kk  Jm< 	
 
 r'   c                   t        j                  |t         j                        r%t        j                  | t         j                  |      S t        j                  |t
        j                        rt        j                  | d|      S t        j                  |t         j                        r4t        j                  | t        j                  |      j                  |      S t        j                  |t         j                        rt        j                  | d|      S t        j                  |t
        j                        rt        j                  | d|      S t        |      )Nr   F)r?   
issubdtypefloatingfullnanr,   SEMAPHORE_INTERPRET_DTYPEintegeriinforQ   r   semaphore_dtyper   rj   r   s     r&   uninitialized_valuer+    s    ^^E3<<(88E377E** ~~e[BBC88E1e$$
~~eS[[)88E399U+//77
~~eSXX&88E5%((
~~e[88988E1e$$E""r'   Fc                    t        d |D              }|rt        d |D              }t        d| j                        }t        j                  | ||      } | S )a  
  DynamicSlice and DynamicUpdateSlice adjust the start index in cases where the
  requested slice overruns the bounds of the array. This pads the array with
  uninitialised values such that the requested slice will never overrun.

  For example, if arr is [1.,2.,3.,4.] and a slice of size 4, start index 2 is
  requested then the result will be [3.,4.,NaN,NaN] after padding, rather than
  [1.,2.,3.,4.] from the unpadded array

  unpad=True performs the inverse operation
  c              3  &   K   | ]	  }d |d f  ywr   r*   )rl   
slice_sizes     r&   rn   z?_pad_values_to_avoid_dynamic_slice_oob_shift.<locals>.<genexpr>  s     J
!Z+Js   c              3  4   K   | ]  \  }}}| | | f  y wrb   r*   )rl   lowhighinteriors       r&   rn   z?_pad_values_to_avoid_dynamic_slice_oob_shift.<locals>.<genexpr>  s-      H4T8 !D4%(3 Hs   r*   r*  )padding_configpadding_value)r   r+  r   r   pad)r   rx   unpadr3  r4  s        r&   ,_pad_values_to_avoid_dynamic_slice_oob_shiftr7    s^     JkJJ.
 H8FH HN%BekkB-
''%!/ -/% 
,r'   T)r6  c          
        ~|j                  |      \  }}}}t        |      }|rt        |d   t        j                        sQt        j                  || d   j                        }	|j                  t        j                  j                  |	             |^ }
}t        |t              sJ t        j                  ||
      }t        d |j                  D              r$|j                  D ]-  }t        |t              s|j                  dkD  s$t!        d       |j                  }|D cg c]"  }t        |t               xr |j                   $ }}|D cg c]   }t        |t              r|j"                  n|" }}t%        d |D              }t'        ||      }t)        j*                         }t-        j.                  ||D cg c]  }t1        j2                  ||       c}|      }t%        d |D              }||   }n2t        d	 |j                  D              r||j                     }nt         ||t1        j4                  |||      }d
t7        |       z  |fS c c}w c c}w c c}w )Nr   c              3  Z   K   | ]#  }t        |t              xs |j                    % y wrb   rg   rk   s     r&   rn   z'_load_discharge_rule.<locals>.<genexpr>  ro   rp   rs   Unimplemented stride support.c              3  X   K   | ]"  }t        |t              r|j                  nd  $ ywrr   rt   rk   s     r&   rn   z'_load_discharge_rule.<locals>.<genexpr>   ru   rv   rw   c              3  :   K   | ]  }|rd n
t        d        ywr   rz   r|   s     r&   rn   z'_load_discharge_rule.<locals>.<genexpr>  r   r~   c              3  >   K   | ]  }t        |t                 y wrb   r   rk   s     r&   rn   z'_load_discharge_rule.<locals>.<genexpr>  r   r   rb   )r   r   rh   r   	NDIndexerr   get_transforms_shaperj   appendmake_trivial_indexerr   r   r   r   ri   strider   r   r   r7  r   default_int_dtyper   r   r?   astyper   r   )r   r   r   r   r6   r   r   r   r  	ref_shaper   r   rm   r   r   r   rx   	idx_dtyper   r   r   s                        r&   _load_discharge_rulerH    s#   !*!4!4Y!?#z4J*	:jnh6H6HI**:x{7H7HIIh((==iHI$?C	C	##	#''_=#DDD[[ C	Au	!((Q,!"ABBC kkGELMz!U++;AGG;MKMDKLqz!U3AGG:LLLOwOOK 7sK
HC((*I  +78aAy	!8H
 O;OOK
;
C

9S[[
99
ckk
C
	%+
))D#u
%C	3x=	 #	%%+ NL 	9s   )'I%I#I(
masked_swapc           	        | j                  |      \  }}}}|J t        j                  ||      }|j                  }|j                  }	||j                  k7  r)t        d|j                   d|j                   d| d      |	|j                  k7  rt        d|	 d|j                   d      t        j                  ||	      t        j                  d      hfS )Nz%Invalid shape for `swap`. Ref shape: z. Value shape: z. Transforms: z. z%Invalid dtype for `swap`. Ref dtype: z. Value dtype: r   )
r   r,   r   rj   r   r   r1   r>   r   r   )
r   r   r6   r   r   r   r   r   expected_output_shapeexpected_output_dtypes
             r&   r   r     s    (22:>#z3			..sJ?/)//)//cii'

/		{ ;		{.B	@  cii'

/0E/F G		{"	& 
 02GH
 r'   c                n   | j                   \  }| j                  d   j                  | j                        \  }}}}t	        j
                  |||      }t        |t        j                        rRt        j                  |t        j                  d      t        j                  t        j                  ||            g      S t        j                  |g||j                        }|t        j                  d      |t        j                  d      |t        j                  d      t        j                  t        j                  ||            g}	|U|	t        j                  d      t        j                  d      t        j                  t        j                  ||            gz  }	t        j                  |	      S )Nr   r  r   z, r  r  )r  r7   r   r  r   r  rh   r1   DropVarr	  r  r
  r  r  r  )
r  r  r  rd   rc   r   r   r   x_ir  s
             r&   _swap_pp_rulerP  0  sR    {{"! ZZ4>>szzJ!Zd
Wa4#8##$99
g!>?A B B sG(2G2GH!ggdm	ggfo	ggdmgghooc7+,& 




g./ F
 
6	r'   c          	     <   |j                  |       \  }}}}|j                  |      \  }}	}
}	t        j                  |
      }
t        j                  t        j                  ||||f      d|i|t        j                  t        j                  |||
|f      d|i|fS r  )r   r	   r  swap_pr%   r   r  )r  r  r   r7   r  r   
val_primalr   r  r6   val_tangents              r&   	_swap_jvprU  O  s    -6-@-@-I**j*d#,#6#6x#@ +q+q##K0+kk  *j*d!KL 
 kk  +z;!MN 
 r'   c                  ~|j                  |      \  }}}}t        |      }|rt        |d   t        j                        sQt        j                  || d   j                        }	|j                  t        j                  j                  |	             |^ }
}t        |t              sJ t        j                  ||
      }t        d |j                  D              re|j                  D ]-  }t        |t              s|j                  dkD  s$t!        d       |j                  }t#        |      D cg c]#  \  }}t        |t              s|j                  s|% }}}|D cg c]   }t        |t              r|j$                  n|" }}t'        d |D              }t)        ||      }t+        j,                  |||      }t/        j0                  ||      }|0|}t/        j2                  |||      }t/        j2                  |||      }t/        j4                  ||      }t+        j6                  |||      }t9        ||      }nt        d	 |j                  D              rj||j                     }|0|}t/        j2                  |||      }t/        j2                  |||      }|j:                  |j                     j=                  |      }nt         |fd
t?        |       dz
  z  z   |fS c c}}w c c}w )Nr9  r   c              3  Z   K   | ]#  }t        |t              xs |j                    % y wrb   rg   rk   s     r&   rn   z'_swap_discharge_rule.<locals>.<genexpr>o  ro   rp   rs   r;  c              3  X   K   | ]"  }t        |t              r|j                  nd  $ ywrr   rt   rk   s     r&   rn   z'_swap_discharge_rule.<locals>.<genexpr>{  ru   rv   rw   r   c              3  >   K   | ]  }t        |t                 y wrb   r   rk   s     r&   rn   z'_swap_discharge_rule.<locals>.<genexpr>  r   r   rb   ) r   r   rh   r   r?  r   r@  rj   rA  rB  r   r   r   r   ri   rC  r   	enumerater   r   r7  r   r   r?   squeezer   expand_dimsr   ._unpad_values_to_avoid_dynamic_slice_oob_shiftr   r   r   )r   r   r   r   r6   r   r   r   r   rF  r   r   rm   r   ir   r   rx   r   out_r   s                        r&   _swap_discharge_ruler`  d  s   (229=#z3J*	:jnh6H6HI**:x{7H7HIIh((==iHI$?C	C	##	#''_=#DDD[[ C	Au	!((Q,!"ABBC kkG g&Aq!U#AGG 	
K 
 ELLqz!U3AGG:LLLOwOOK 7sK
HC


C;
GC
++c;
'CdIIdC%cIIdC&c
//#{
+C$$S#\JE:5+NE

9S[[
99
ckk
CdIIdC%cIIdC&cFF3;;##C(E

Gs8}q01	13	66;
 Ms   3(K%"%K+)r   r  cache_modifiereviction_policyvolatilec                   t        j                  | |d      \  }}t        j                  ||||f      \  }	}
t	        j
                  |	|
|||dS )av  Returns an array loaded from the given index.

  If neither ``mask`` nor ``other`` is specified, this function has the same
  semantics as ``x_ref_or_view[idx]`` in JAX.

  Args:
    x_ref_or_view: The ref to load from.
    idx: The indexer to use.
    mask: An optional boolean mask specifying which indices to load.
      If mask is ``False`` and ``other`` is not given, no assumptions can
      be made about the value in the resulting array.
    other: An optional value to use for indices where mask is ``False``.
    cache_modifier: TO BE DOCUMENTED.
    eviction_policy: TO BE DOCUMENTED.
    volatile: TO BE DOCUMENTED.
  load)r   ra  rb  is_volatile)r   r   r   r   r  r%   )r   r   r   r  ra  rb  rc  r   r   r   r   s              r&   re  re    s`    $ //sFK%"//j$&)Y 
#%
 r'   swapr   rb  _function_namec                   t        j                  | ||      \  }}t        j                  ||||f      \  }}	t	        j
                  ||	|dS )zSwaps the value at the given index and returns the old value.

  See :func:`~jax.experimental.pallas.load` for the meaning of the arguments.

  Returns:
    The value stored in the ref prior to the swap.
  )r   rb  )r   r   r   r   rR  r%   )
r   r   r   r   rb  ri  r   r   r   r   s
             r&   rg  rg    sX     //S.% #//
C0NO)Y	I
 r'   )r   rb  c               &    t        | ||||d      }y)ztStores a value at the given index.

  See :func:`~jax.experimental.pallas.load` for the meaning of the arguments.
  storerh  N)rg  )r   r   r   r   rb  r6   s         r&   rl  rl    s    
 =#s!#!r'   c                    | t         j                  k(  rt         j                  S | t         j                  k(  rt         j                  S | S )zFUgly workaround to support types that don't allow automatic promotion.)r?   int4r   float8_e4m3b11fnuzr   r   s    r&   _handle_smallrq    s4    
chh88O
c$$$<<	,r'   c                6   | j                   dk7  s|j                   dk7  rt        d      |rdnd}|sdnd}|C|t        d      |rt        j                  j                  nt        j                  j
                  }t        j                  t        | j                        t        |j                              }t        j                  |t        j                        rt        j                  nt        j                  }	t        j                  | ||f|ffdf||	      S )a  Computes the dot product of two arrays.

  The inputs can optionally be transposed before computing the
  product. Depending on the hardware, this can be cheaper than
  computing the transpose beforehand.

  Args:
    a: The left-hand size of the dot product, of shape ``(..., N)``.
    b: The right-hand size of the dot product, of shape ``(...N, M)``.
    trans_a: Whether to transpose ``a`` before the product.
    trans_b: Whether to transpose ``b`` before the product.
    allow_tf32: Whether to use tf32 precision.
      Mutually exclusive with ``precision``.
    precision: Specifies the precision of the dot product.

  See Also:
    :func:`jax.numpy.dot`
     z`a` and `b` must be 2D arrays.r   rs   z5Only one of allow_tf32 and precision can be specified)r*   r*   )dimension_numbers	precisionpreferred_element_type)ndimr   r   	PrecisionHIGHHIGHESTr?   promote_typesrq  r   r"  r'  r@   float32dot_general)
abtrans_atrans_b
allow_tf32ru  lhs_contract_dimrhs_contract_dimr   	out_dtypes
             r&   dotr    s    ( ffkqvv{
5
66!Qq%Q1NOO&0""cmm6K6KI


M!''2M!''4J
K%>>%=cii3;;)	+-0@/BCXN&
 r'   
reciprocalapproxc               0    t         j                  | |      S )Nr  )reciprocal_pr%   rc   r  s     r&   r  r     s    			1V		,,r'   c                   ~| S rb   r*   r  s     r&   _reciprocal_abstract_evalr    s
    	
(r'   c               N    ddd} t        j                  |d      | ||      S )NFr  c                   |rOt        j                  | j                  t         j                              j                  t         j                        S t        j                  |       S rb   )r?   r  rE  r   r|  r  s     r&   _reciprocalz._reciprocal_lowering_rule.<locals>._reciprocal  s?    ^^AHHS\\23::3;;GG>>!r'   multiple_results)r   	lower_fun)ctxrc   r  r  s       r&   _reciprocal_lowering_ruler  
  s/      % 
 
=e	<	1V
 r'   c                4    t        j                  | g|ddiS )a  Prints values from inside a Pallas kernel.

  Args:
    fmt: A format string to be included in the output. The restrictions on the
      format string depend on the backend:

      * On GPU, when using Triton, ``fmt`` must not contain any placeholders
        (``{...}``), since it is always printed before any of the values.
      * On GPU, when using the experimental Mosaic GPU backend, ``fmt`` must
        contain a placeholder for each value to be printed. Format specs and
        conversions are not supported. If a single value is provided, the value
        may be an array. Otherwise, all values must be scalars.
      * On TPU, if all inputs are scalars: If ``fmt`` contains placeholders,
        all values must be 32-bit integers. If there are no placeholders, the
        values are printed after the format string.
      * On TPU, if the input is a single vector, the vector is printed after
        the format string. The format string must end with a single placeholder
        ``{}``.
    *args: The values to print.
  skip_format_checkT)r   debug_print)fmtargss     r&   r  r    s    * 
		s	BT	BT	BBr'   c           
        d}t        j                         j                  |       D ]*  \  }}}}||dz  }|s|rt        d      |s!t        d       t	        |      |k7  r$t        d| d|dk(  rdnd d	t	        |             y )
Nr   rs   zDThe format string should not contain any format specs or conversionszDThe format string should not reference arguments by position or namezThe format string expects z	 argument rm   z
, but got )string	Formatterparser   r   	TypeError)r  r  n_placeholdersr6   fieldspec
conversions          r&   check_debug_print_formatr  2  s     .$*$4$4$6$<$<S$A 
 ajnz
P  
P 
 	Y. 

$^$4 5'1,2#6jT	M  !r'   c                D    t        d t        ||      D              } | | S )Nc              3  V   K   | ]!  \  }}|rt        j                  ||      n| # y wrb   )state_typesr   )rl   r~  ts      r&   rn   z'wrap_with_transforms.<locals>.<genexpr>O  s0      
!Q +,k  A&2s   '))r   zip)fr   r  new_argss       r&   wrap_with_transformsr  M  s,     dJ' ( 
Hr'   
run_scopedc                    ~~| j                   S rb   )is_high)jaxpravalsr7   s      r&   _run_scoped_is_highr  Y  s    V	r'   c                    t        j                  | |      }t        j                  |      }|j                  }t        j                  |d|j                  i|S )Nr  )r1   ClosedJaxprpelower_jaxprconstsrun_scoped_pr%   r  )r  r  r7   closed_hi_jaxprclosed_lo_jaxprr  s         r&   _run_scoped_to_lojaxr  ^  sN    ((5/NN?3/!!&			F	J/*?*?	J6	JJr'   r*   )collective_axesc                  t        |t              s|f}t        j                  ||f      \  }}t	        j
                  t        j                  | t	        j                  d| ||            |      \  }}|D cg c]  }|j                          }	}|	D cg c]*  }t        |t        j                        r|j                  n|, }
}t        d |	D              }t        ||      }t        j                  d      5  t!        j"                  ||
      \  }}}ddd       t%        j&                  |d}t        j(                   |       |      S c c}w c c}w # 1 sw Y   DxY w)a  Calls the function with allocated references and returns the result.

  The positional and keyword arguments describe which reference types
  to allocate for each argument. Each backend has its own set of reference
  types in addition to :class:`jax.experimental.pallas.MemoryRef`.

  When ``collective_axes`` is specified, the same allocation will be returned for
  all programs that only differ in their program ids along the collective axes.
  It is an error not to call the same ``run_scoped`` in all programs along that
  axis.
  zpallas run_scoped)
debug_infoc              3  l   K   | ],  }t        |t        j                        r|j                  nd  . yw)r*   N)rh   r  r   r   )rl   r  s     r&   rn   zrun_scoped.<locals>.<genexpr>  s0      
 !K$>$>?allRGs   24FNr  r  )rh   r   r   r   r
   flatten_funlu	wrap_initr  get_ref_avalr  r   r   r  r   mutable_array_checksr  trace_to_jaxpr_dynamicr  r%   r  )r  r  r   kw_types
flat_typesin_treeflat_funout_tree_thunkr  	ref_avalsr  ref_transformsr  r6   r  r   s                   r&   r  r  e  sb   " 
OU	+&(O!..x/@A*g%11ll1&112E23UHFG 	(N *44Aq~~4)4 
 ![778aeea?%    . "(N;( ""5) B005AE1fB6P#		!	!."2C	88% 5B Bs   6E/E7EEc                   ~~| j                   D ch c]@  }t        |t         j                        r"|j                  t	        | j
                        k\  s|B }}| j                  D cg c]  }|j                   c}|fS c c}w c c}w rb   )r   rh   JaxprInputEffectinput_indexr   	constvarsr  r   )r  r  r  effnonlocal_effectsvs         r&   _run_scoped_abstract_evalr    s{    
O 

S'22
3ooU__!55	 
   --	(Q!&&	(*:	:: 
)s   AA>&Bc          	        ~|rt        d      t        |      }t        j                  |      }t        |j                        }t        j                  |g | dgt        |j                        z  z         \  }	}
|
rt        d      t        j                  |	|      }	t        j                  ||	|d}|d | }||d  }t        | |      D cg c]4  \  }}|r+t        |t        j                        r|j                  d      nd 6 }}}t        |      t        |      k(  sJ t        |       dt        |              ||fS c c}}w )Nz:run_scoped discharge does not support collective_axes yet.Fshould_discharge4Cannot handle new consts created by state discharge.r  r   z != )r   r   r  convert_constvars_jaxprr  r   discharge_stater  convert_invars_to_constvarsr  r%   r  rh   r   AbstractRefr+   )r  r   r   r  r  r   
num_constsjaxpr_noconstnum_return_valuesdischarged_body
new_constsr   return_valuesref_outputsshouldr   updatess                    r&   _run_scoped_discharge_ruler    sn    
D  9~* ,,U3--//0 / ? ?'5'C4E*EE!/:
 
>@ @ 22?JO/ 		#
 (()-%&'+
 %((8($CE FD #z$8I8I'JkooaE' E 
WX	&L3w<.S]O(LL	&	-		Es   9E c                  |rt        d      t        j                  |      }t        |j                        t        j                  |g d      \  }|rt        d      fd} t        j                  |d      | g| S )NzGrun_scoped lowering outside of Pallas does not support collective_axes.Tr  r  c                    t        |       }j                  |d  D cg c]  }|j                   }}|D cg c]"  }t        |j                  |j
                        $ }}t        j                  g g| | }|d  S c c}w c c}w rb   )r   r  r   r+  rj   r   r1   
eval_jaxpr)	lower_fun_argsr  r  
body_avalsr   	init_valsr   r  r  s	          r&   
_lower_funz-_run_scoped_lowering_rule.<locals>._lower_fun  s    ^$J"1"8"8"EFQ!&&FJF,68$( %

DJJ  8I 8


or
ON
OY
OC!!""	 G8s
   A>'Br  )
r   r  r  r   r  r   r  r   r   r  )	r  r  r  r  r  r  r  r  r  s	          @@r&   _run_scoped_lowering_ruler    s    
	  ,,U3--//0 / ? ?R$!0/:->@ @# 
;
T	:3	F	FFr'   
get_globalc                N    | j                         }t        j                  |      S )a  Returns a global reference that persists across all kernel invocations.

  Each call to ``get_global`` returns a different and unique reference, but one that
  is stable across invocations of the kernel body.

  Args:
    what: The reference type to allocate. Each backend has its own set of
      reference types (e.g., :class:`jax.experimental.pallas.mosaic_gpu.SemaphoreType` for GPU).

  Example::

    sem_ref = pl.get_global(plgpu.SemaphoreType.REGULAR)
    pl.semaphore_signal(sem_ref)
    pl.semaphore_wait(sem_ref)
  what)r  get_global_pr%   )r  r   s     r&   r  r    s&       (					))r'   c                    | S rb   r*   r  s    r&   _get_global_abstract_evalr    r   r'   c                   ~ ~~t        d      )Nz8get_global discharge is not supported in interpret mode.)r   )r   r   r  s      r&   _get_global_discharge_ruler    s    	4@	 r'   c                n    t        | t        j                        r| j                  | j                  fS | dfS r<   )rh   r   r   r   r   )r   s    r&   _get_ref_and_transformsr    s.    U))*77CNN""	b.r'   c                      e Zd ZdZdZy)DeviceIdTypemeshlogicalN)rU   rV   rW   MESHLOGICALr*   r'   r&   r  r  "  s    	$'r'   r  c           	        |/t         j                  t         j                  t         j                  h}t	        | t
        j                        st        d| d|        | j                  }|r|d   j                         }|rt        d| d|       | j                  t        fd|D              st        d| d| d d	      y )
NzCannot z on a non-semaphore Ref: r9  z on a non-()-shaped semaphore: c              3  J   K   | ]  }t        j                  |        y wrb   )r?   r"  )rl   sem_type	sem_dtypes     r&   rn   z"check_sem_avals.<locals>.<genexpr>9  s$      
 
nnY)s    #zMust z$ semaphores of the following types: z. Got r   )r,   	semaphorebarrier_semaphorer&  rh   r   r  r   rj   get_indexer_shaper   any)sem_avalsem_transforms_avalsnameallowed_semaphore_types	sem_shaper  s        @r&   check_sem_avalsr  '  s     $%%--	 
He//	0
wtf$=hZH
IInn)$R(::<I
wtf$CI;O
PPnn)	 - 
 
v #$F9+Q	8 	
r'   c                    | j                   |j                   k(  rt        j                  | |      S t        | j                         dk(  r| S t	        d| j                    d|j                          )zEHelper function for indexing into a semaphore during state_discharge.r   zSemaphore value shape z does not match aval shape )rj   r   r   r   r   )	ref_valuer   r   s      r&   _transform_semaphorer  C  si    __&**9jAA
9??q 

  1 2NN	 r'   semaphore_readc                    t        |       \  }}||g}t        j                  |      \  }}t        j                  |d|iS )zReads the value of a semaphore.

  Args:
    sem_or_view: A Ref (or view) representing a semaphore.

  Returns:
    A scalar Array containing the value of the semaphore.
  r   )r  r   r   semaphore_read_pr%   )sem_or_viewr   r   r  	flat_argsr   s         r&   r  r  T  sG     ,K8/#z
z	$"//5)Y					?Y	??r'   c                X    ~~ t        j                  dt        j                  d            S )Nr*   r@   )r1   r>   r?   r   )r   r  s     r&   _semaphore_read_abstract_evalr  b  s%    
 Y			b#))G"4	55r'   c                   ~|j                  |      \  }}t        ||| d         }|j                  t        j                        }dt        |       z  |fS )Nr   rb   )r   r  rE  r?   r@   r   )r   r   r   r  r   r   	sem_values          r&   _semaphore_read_discharge_ruler  j  sV     )))43
"3
HQK@)syy))	3x=	 )	++r'   .semaphore_signal)	device_iddevice_id_type
core_indexc                   t        |       \  }}t        j                  |t        j                        }|||||g}t	        j
                  |      \  }}	t        j                  ||	|d y)a  Increments the value of a semaphore.

  This operation can also be performed remotely if ``device_id`` is specified,
  in which ``sem_or_view`` refers to a Ref located on another device.
  Note that it is assumed that ``sem_or_view`` is already allocated
  (e.g. through the proper use of barriers), or else this operation could
  result in undefined behavior.

  Args:
    sem_or_view: A Ref (or view) representing a semaphore.
    inc: The value to increment by.
    device_id (optional): Specifies which device to signal.
      If not specified, ``sem_or_view`` is assumed to be local.
    device_id_type (optional): The format in which
      ``device_id`` should be specified.
    core_index (optional): If on a multi-core device,
      specifies which core to signal.
  rp  )r   r   N)r  r?   asarrayr@   r   r   semaphore_signal_pr%   )
r  incr  r   r!  r   r   r  r  r   s
             r&   r  r    sb    4 ,K8/#zCsyy)#
z3	:	6$"//5)Y#r'   c                   t        j                  | |      \  }}}}}t        ||d       |j                  t	        j                  d      k7  rt        d|j                         t               }|t        j                  |      }	|	D ]<  }
|
j                  t	        j                  d      k7  s&t        d|
j                          |t        j                  u rYt        |t              rI|D ]@  }t        |t              s|f}|D ]&  }|j                  t        j                  |             ( B g |fS |j                  t         j"                         g |fS )Nsignalr@   z$Must signal an int32 value, but got z-`device_id`s must be an int32 value, but got )r   r  r  r   r?   r   r   r  r  r  rh   r4   r   rO   r1   NamedAxisEffectr,   comms_effect)r   r   r  r  r  
value_avaldevice_id_avalcore_index_avaleffsdevice_id_flat_avalsr   kk_s                r&   _semaphore_signal_abstract_evalr1    sJ    y%0(0(;7++
;J<L<L;MN
OO"u$$00@$ 
	syy)	);DJJ<H
 	


 ***z.$/O 1!!U#d! 	1B
((8++B/
0	11 
T/ hh{''(	T/r'   c                D   ~| j                   }| j                  d   }t        j                  ||      \  }}}}}	t	        j
                  t	        j                  d      t	        j                  d      t        j                  |||      t	        j                  d      t	        j                  t        j                  ||            g      }
|t        j                  |      }|s|
S t	        j                  t        j                  |d   |            g}|dd  D ]^  }|j                  t	        j                  d             |j                  t	        j                  t        j                  ||                   ` t	        j
                  |
t	        j
                  |      g      }
|
S )Nr   r  r  r   rs   )r  r7   r   r  r	  r  r
  r   r  r1   r  r  rA  )r  r  r  r  treesemsem_transformsr   
device_idsr6   r   flat_device_idsdevice_ids_ppr  s                 r&   _semaphore_signal_pp_eqnr9    sW    ::&	K	 $ tV,	
		gg !ggcl7C8ggclgghooeW-. 	# ++J7OjWWX___Q-?IJKM$QR( I	2773<(2778??9g#FGHI ))S"))M23
4C	*r'   c               .   ~~|j                  |      \  }}}}}	|t        d      |	t        d      t        ||| d         }
|j                  t        j
                        }t        j                  |||
|z         \  }}|fdt        |       dz
  z  z   dfS )NzRemote signal not implemented.z&Multiple core support not implemented.r   rb   rs   r*   )	r   r   r  rE  r,   r&  r   transform_swap_arrayr   )r   r   r   r   r  r   r   r%  r  r!  r  r6   new_sem_values                r&    _semaphore_signal_discharge_ruler=    s    
 2;2E2Ei2P/3
CJ
>
??
F
GG"3
HQK@)

;889#$99	:y3!] 	Gs8}q'89	92	==r'   semaphore_wait)	decrementc                   t        |       \  }}t        j                  |t        j                        }||||g}t	        j
                  |      \  }}t        j                  |d|i y)a=  Blocks execution of the current thread until a semaphore reaches a value.

  Args:
    sem_or_view: A Ref (or view) representing a semaphore.
    value: The target value that the semaphore should reach before unblocking.
    decrement: Whether to decrement the value of the semaphore after
      a successful wait.
  rp  r   N)r  r?   r#  r@   r   r   semaphore_wait_pr%   )r  r   r?  r   r   r  r  r   s           r&   r>  r>    s\     ,K8/#z
++e399
-%
z5)	,$"//5)Y8i8r'   c                    t        j                  | |      \  }}}}t        ||d       |j                  t	        j                  d      k7  rt        d      g S )Nwaitr@   zMust wait an int32 value.)r   r  r  r   r?   r   )r   r  r  r  r*  r6   s         r&   _semaphore_wait_abstract_evalrD    sX    2;2J2J3/( *a (0&97++
0
11	)r'   c           
        ~| j                   }| j                  d   }t        j                  ||      \  }}}}t	        j
                  d      g}	|r$|	j                  t	        j
                  d             |	t	        j
                  d      t        j                  |||      t	        j
                  d      t	        j
                  t        j                  ||            gz  }	t	        j                  |	      S )Nr   r>  z[dec]r  )r  r7   r   r  r	  r
  rA  r   r  r1   r  r  )
r  r  r  r  r3  r4  r5  r   r?  partss
             r&   _semaphore_wait_pp_eqnrG    s     ::&	K	 $ tV,	 	gg% 	LL!"ggcl7C8ggclgghooeW-.	 % 
5	r'   c                   ~|j                  |      \  }}}}t        ||| d         }|j                  t        j                        }|rt        j                  ||||z
        \  }	}
n|}
|
fdt        |       dz
  z  z   dfS )Nr   rb   rs   r*   )r   r  rE  r,   r&  r   r;  r   )r   r   r   r  r   r   r   r?  r  r6   r<  s              r&   _semaphore_wait_discharge_rulerI  6  s     (1(;(;I(F%3
E9"3
HQK@)
,,{<<
=%&;;ZU*A} M
	Gs8}q'89	92	==r'   c           
        t         j                  j                  d      }| i n)t        t	        | j
                  | j                              i }|j                         D ]  \  }}t        |t              rdt        fd|D              rOt        fd|D              st        | d      |D cg c]  }|   	 }}t        |      D ]
  \  }	}|   }
t        j                  ||	dz   d        }t!        j"                  ||      }||dz
  z  dk(  rB|| z  j%                         dz
  }t!        j&                  |t!        j"                  ||            }nt!        j(                  ||      }|
|
dz
  z  dk(  r1t!        j*                  |t!        j"                  ||   dz
              }n*t!        j,                  |t!        j"                  ||
            }|||<    ||||<    g }D ]2  }||v r|j/                  ||          |j/                   ||             4 |j                         D ci c]  \  }}|vr|| }}}t        |      |fS c c}w c c}}w )N    c              3  &   K   | ]  }|v  
 y wrb   r*   rl   r~  mesh_axis_sizess     r&   rn   z*_device_id_dict_to_mesh.<locals>.<genexpr>U  s      ,!"_,   c              3  &   K   | ]  }|v  
 y wrb   r*   rM  s     r&   rn   z*_device_id_dict_to_mesh.<locals>.<genexpr>X  s     9!o%9rO  z) mixes JAX mesh and Pallas mesh grid axesrs   r   )r   IntegerTypeget_signlessr4   r  
axis_names
mesh_shapeitemsrh   r   r
  r   r   rZ  mathprodr   constant
bit_lengthshruidivsiandiremsirA  )mesh_contextdevice_id_dictget_axis_indexi32physical_axis_dict	axis_namer   r  axes_dimensions
axis_index	axis_sizeinner_mesh_sizeminor_divisor	shift_lenpartial_device_idx
device_idxr  r/  r  non_mesh_axesrN  s                       @r&   _device_id_dict_to_meshrm  J  s   
##B'#OL##\%<%<=O &,,.  *ni)U# ,&/, ) 9y99!kBC
 	
 <EE4.EoE#,Y#7 3
*i#I.	))OJN4D$EFsO< o12a7&/)99EEG!K)${{3sI0NO
${{3>
	A&!+zz nnS/)"<q"@A*
 {{ %..i"@* )39%+3. '*#A *B )" 2i&&))45~i01	2 %**,
!Q	
/	! d- 
 
y	=	((I F>s   4I1Ic                   i }t        |t              r1|t        j                  urt	        d|d      t        | ||      \  }}|t        j                  u rt        j                  |      }| d}n| j                  }t        |      t        |      k7  r$t	        dt        |       dt        |       d      t        j                  j                  d      t        |      dk(  rt        j                  d      |fS t        j                   t        j"                  fd	t%        ||      D              |fS |t        j&                  u r||fS t)        d
|       )zNormalizes a device id into a logical device id and axes that don't correspond to JAX mesh axes.

  The indexing implied by the returned axis dict should be handled by the caller.
  zN`device_id_type` must be MESH if `device_id` is a dict, got: device_id_type = r   r*   zANumber of device ids must match the number of mesh axes, but got z ids for a zD mesh.rK  r   c              3  x   K   | ]1  \  }}t        j                  |t        j                  |             3 y wrb   )r   mulirX  )rl   r~  r  ra  s      r&   rn   z'device_id_to_logical.<locals>.<genexpr>  s3      	
1 JJq%..a01	
s   7:zUnsupported device id type: )rh   r4   r  r  r   rm  r   r  mesh_stridesr   r   rQ  rR  r   rX  	functoolsreduceaddir  r  r   )r^  r  r   r`  rl  r6  rq  ra  s          @r&   device_id_to_logicalru    sk    -	4 \...%!%Q(   7|YP^_I}|(((&&y1Jl!..l
:#l++*ok#l*;)<GE 
 ..
%
%b
)C
:!^^C#]22

	
J5	
   ---m##:>:JKLLr'   delayc                    ~ g S rb   r*   nanoss    r&   _delay_abstract_evalrz    s
    	)r'   c                .    t         j                  |        y)z+Sleeps for the given number of nanoseconds.N)delay_pr%   rx  s    r&   rv  rv    s    	,,ur'   )r#   r   returnjax_typing.Array)r#   r   r}  int | jax_typing.Array)r   rM   )r   
Any | Noner   rM   )r   r  )rc   r~  r   zSequence[int] | intr}  r~  )F)r}  r~  )r}  None)r   zjax_typing.DTypeLike)FFNN)r  r   r  r   r  zbool | None)r  zmlir.LoweringRuleContext)r  strr  zjax_typing.ArrayLike)
r  zCallable[..., Any]r   r   r  zHashable | tuple[Hashable, ...]r  r   r}  r   )r  zpallas_core.ScratchShaper}  r~  rb   )rs   )r%  r  r  DeviceIdr   r  r!  zint | jax_typing.Array | None)r   r  )r  zjax_core.JaxprEqnr  zjax_core.JaxprPpContextr  zjax_core.JaxprPpSettings)r   r  r?  r   )r^  pallas_utils.MeshInfo | None)r^  r  r  z5ir.Value | tuple[ir.Value, ...] | dict[Any, ir.Value]r   r  r}  z$tuple[ir.Value, dict[Any, ir.Value]])ry  r  r}  r  )__doc__
__future__r   collections.abcr   r   r   enumrr  rV  r  r   r   jax._src.lax_srcr   jax._srcr   r	   r
   r   r1   r   r   r   
jax_typingr   r   r  r   r	  r   r   jax._src.interpretersr   r   r  jax._src.lib.mlirr   jax._src.lib.mlir.dialectsr   jax._src.pallasr,   r   pallas_utilsjax._src.stater   r   r   r   r   r   r  jax.interpretersr   r   r?   ri   r?  safe_mapmap
unsafe_mapsafe_zipr  
unsafe_zipr2   r$   r    r:   def_bind_with_tracedef_abstract_evalrB   rF   rD   rI   rK   EnumrM   r   r   register_discharge_ruledef_effectful_abstract_evalr   r   r   r   r   r   r   r   r   r   r   r   r   r   def_implregister_loweringr   r   r   r   r   r  r   r  pp_eqn_rulesr   primitive_jvpsr+  r7  partialr]  rH  rR  r   rP  rU  r`  re  rg  rl  rq  r  r  r  r  r  r  r  transformation2r  r  r  r  r  r  to_lojaxr  r  r  register_partial_discharge_ruler  r  ref_primitive_ref_allocating_primitivesrO   r  r  r  r  r  r  r  r  r  r  r  r   Arrayr   r4   r  r$  r  r  r1  r9  r=  rA  r>  rD  rG  rI  rm  ru  r|  rz  rv  r*   r'   r&   <module>r     s)	   & " . $           %    )  & )   $ 4   , / 1 7 # + / ! !	--Z--Z!x!!,/&	V     !; <-  - $##N3(  " "#@ A!!- "-499  "x!!,/%7=I%7P 6 ' ' ' 56P Q ))? *?  @D ) @D   ?C   ?C   ?C   ?C   >B   ?C  "x!!,/))	V *	V* )((6$ 7$
 &8%%&67    * +   ')? @8 ## $ #""=1   ' (   }&< =.     ! 
		M	* ## $, !.  f * &  & #" 7<0 2C1B1B02 .
 )((0%& 1%&P 
		M	* ## $,8 !.  f $ &  &  )((0-7 1-7` &*d2B< +/#3" ,0 # 6;26%%N "x!!,/ " -    16
	!
   |%> ?C0	)6   "x!!,/ $  + K
 - 
 8:,9,9,9 5,9 	,9
 	,9^ )); *; , ^ > / / / =  4))<8G 9G2 "x!!,/ % !   # # ' ' 5*(    6 ' ' ' 5
499  CG8
 &8%%&67 $)  !@ ##6 $6, : ' ' '(8 9"
 !!!D(5z7G7G1G1L+MMPTUXZ]`j`p`pZpUpPqq (X''(:; &*  #
 #$" #/#4#404"	" 	"
 !" ."J // ! 0B&='?< -E  ( )>" < ' ' '(: ;$
 &8%%&67 $(  ! 239IM9.9BF9" ## $&='?0 +A  & '> : ' ' '(8 9"
6)r,M.,MD,M !,M
 *,M^ (

W
%  	 
r'   