
    Vpf                      U d dl mZ d dlmZ d dlmZmZmZ d dl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Zd dlmZmZmZmZ d dlZd dl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+ d dlm,Z, d dlm-Z- d dlm.Z. d dlm/Z0 d dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> d dl?m@ZA d dlBmCZC d dl?mDZD d dl?mEZE d dl?mFZF d dl?mGZG d d l?mHZH d d!lImJZJ d d"lKmLZM d d#lNmOZO d d$lNmPZQ d d%lNmRZR d d&lmSZS d d'lTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZa d d(lbmcZcmdZdmeZe d d)lfmgZhmiZimjZj d d*lkmlZl d d+lmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZz d d,l{m|Z|m}Z}m~Z~mZmZmZmZmZmZmZmZ e}ecZZe~ecZZ e,j        e           eeVe[eYf         ZeeVeYf         ZeeUe[eYf         ZeeUeYf         Z ej        e          Zd- Zd. Z G d/ d0e          Zd1 Zd2 Zd3 Zd4 Zd	d7Z G d8 d9ej                  Z e            ad: Zd; Zd< ZeQj                            d=>          Zd? Zd
dCZddEZdF ZddYZd
dZZdd[Z G d\ d]e          ZddjZ G dk dl          Ze.j        ddp            ZddqZddwZddyZ G dz d{e+j                  ZeZeZddddd|ddd|dfdd}Zd~ Zd Zd Zd Z G d d          Z e.j        dd|          d             Z e            Zded<   ddZ e
e$j        e          dd            Z e.j        dd|          d             Zeeeexee j                 f         Z ej                    ZʐddZːddZd Z ej        d           G d d                      ZϐddZАddZ e j        d          Zde_        d Z	 dddZd Zd Zed             Zd ZeӠ                    eڦ           d ZeddÄ            ZdĄ ZeeAj        e<   dń ZdƄ ZeeAj        e<   ddȄZdɄ Zee j        e<   dʄ ZeӠ                    e           d˄ Zd̄ Z eGj        ee           d̈́ ZeeFj        e<    e
ed          eFj        e<   ddӄZdԄ ZeeEj        e<   edd؄            Zdل ZeeAj        e<   d dZ e
eAj        de          eAj        e<   e$j        d             Zd ZeeEj        e<   ed!d            Zd"dZeeAj        e<   d Zee j        e<   d Z  ehj         eӦ          e           d Zd Zd Z e j        d          Ze                    e           e                    d             eEj        ed            d Z eGj        ee           d Z	e	eFj        e<    e
e	d          eFj        e<   d#dZ
d$d Zd%dZd&dZd'dZdS ((      )annotations)defaultdict)CallableSequenceIterableN)partial)
NamedTupleAnyUnioncast)api)ad_util)api_util)config)core)dispatch)dtypes)linear_util)mesh)op_shardings)profiler)sharding_impls)source_info_util)stages)traceback_util)	tree_util)util)
xla_bridge)argnums_partial_exceptflatten_axesflatten_funflatten_fun_nokwargsdonation_vectorshaped_abstractifycheck_callableresolve_argnumsargnames_partial_except
debug_inforesult_pathsjaxpr_debug_infohoist_obj_attrs)partial_eval)PartitionSpec)xla)ad)batching)mlir)pxla)ir)func)jax_jit)
xla_client)xla_extension_versionsharding)NamedShardingGSPMDShardingSingleDeviceShardingPmapShardingAUTOUNSPECIFIEDUnspecifiedValueParsedPartitionSpecget_single_pspecis_unspecifiedis_unspecified_or_autoprepare_axis_resourcesparse_flatten_op_sharding)LayoutDeviceLocalLayout
AutoLayout)	discharge	RefEffectAbstractRef)api_boundary)tree_flattentree_unflattentreedef_is_leaftree_structuretree_leavestreedef_childrenbroadcast_prefix
all_leavesprefix_errorskeystr	PyTreeDefnone_leaf_registry)HashableFunctionsafe_mapsafe_zipwrapsdistributed_debug_log
split_listweakref_lru_cachemerge_lists	subs_listfun_namefun_qual_namec                T     g  fd}|\  }} ||            ||           S )Nc                    D ]f\  }}}| j         t          j        j        k    rF| j        |k    r;                    d| d d|                                 d| j                     d S gd S )Nz	argument z of z with shape  and )m_typer2   MismatchTypeARG_SHARDINGdaappend	str_short_dev_ids_plat_str)errnameinp_daavalarg_listrc   mismatched_args_msgs       M/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/pjit.pymismatchz$_find_arg_mismatch.<locals>.mismatchd   s    &  fd	t(5	5	5#&F:J:J""' ' '( ' '8H8H ' '$' '	( 	( 	( 	      )rs   failsrc   rv   	first_err
second_errrt   s   ` `   @ru   _find_arg_mismatchr|   b   s`            )Z
(9
(:	rw   c                r   g }|dgt          |          z  }t          ||          D ]I\  }}t          |dd           |j        j        nd }|                    ||t          |          f           Jt          |||           }	t          |	          dk    r|	\  }
}d|
 d| }nt          |	          dk    rE|\  }
}|
j        t          j
        j        k    r|n|
}d|	d          d|                    |           }n3|\  }
}d	|
                    |           d|                    |           }d
| d| }|S )N r9      z Got rg      r   z andz Gotz"Received incompatible devices for zted computation.)lenzipgetattrr9   _device_assignmentrl   r$   r|   rh   r2   ri   rj   _str)rc   ry   	args_flatapi_name	arg_namesrs   anrk   rt   firstsecond	extra_msgleftmsgs                  ru   !_device_assignment_mismatch_errorr   q   s   (s9~~%I)Y'' 4 4daQ
D))5 *
'
';? OOQ.q1123333*8UHEE	""'ME6,,,F,,II
1$$ME6\T%6%CCC66DI+A.IIDIIh4G4GIIIIME6Huzz(++HHX1F1FHHI	Sh	S	S		S	S#	*rw   c                      e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   d
ed<   ded<   d
ed<   ded<   d
ed<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded <   d! Zd" Zd#S )$PjitInfozThings that we know about a jit instance before it is called.

  In other words, this structure contains arguments to jit()/pjit(),
  preprocessed and validated.
  
str | Nonefun_sourceinfozinspect.Signature | Nonefun_signaturebooluser_specified_in_shardingsrX   in_shardings_treedeftuple[Any, ...]in_shardings_leavesout_shardings_treedefout_shardings_leavesin_layouts_treedefin_layouts_leavesout_layouts_treedefout_layouts_leavesztuple[int, ...]static_argnumsztuple[str, ...]static_argnamesdonate_argnumsdonate_argnamesxc.Device | Nonedevicebackendkeep_unusedinline
Any | Noneabstracted_axeshas_explicit_shardinguse_resource_envc                     t          |           S Nidselfs    ru   __hash__zPjitInfo.__hash__   s    d88Orw   c                
    | |u S r   rx   r   others     ru   __eq__zPjitInfo.__eq__   s    5=rw   N)__name__
__module____qualname____doc____annotations__r   r   rx   rw   ru   r   r      sG         
 )))) $###!!!!&&&&""""''''$$$$    %%%%!!!!""""!!!!"""",,,      rw   r   c                   t          | |||          \  }}|D ]}t          j        |           |j        rt	          |j                  }g ||}	 t          j        |i |j        }nx# t          j	        $ rt}	|	j
        \  }
|j        d         dnd}t          | dt          | dt          |                               }t          ||
|||j                  }t          |          d d }	~	wt           j        $ r}	|j        dgt%          |          z  n|j        }|j        d         j        rt)          |	j
        d                   |	t+          |||j                  D ]k\  }}}	 t!          j        |           # t           j        $ r>}t)          d	| d
|                                 dt3          |           d          |	d }~ww xY wt5          d          |	d }	~	ww xY w|j        rGt7          d |j        D                       }t9          ||g          \  }}t;          |j        |           t=          |j        |          }|||j        ||j        d         |j        fS )Nresource_envjitpjitr   r   r~   jaxprr   z
Argument 'z' of shape z	 of type z is not a valid JAX type.Unreachablec              3  ,   K   | ]\  }}}|j         V  d S r   
num_leaves).0_end_trees      ru   	<genexpr>z&_python_pjit_helper.<locals>.<genexpr>   s+      SSHa,SSSSSSrw   ) _infer_paramsr   	check_argattrs_tracked_get_statespjit_pbindparamsr2   DeviceAssignmentMismatchErrorargsr   strr   r   
ValueErrorr.   InvalidInputExceptionr   consts	TypeErrorr   in_avalscanonicalize_dtyperm   typeAssertionErrorsumr_   _set_statesrO   out_tree)funjit_infor   kwargspr   arginit_statesout_flatery   r   rc   r   r   rp   rr   r   num_states_outfinal_statesoutss                        ru   _python_pjit_helperr      s   sHdF;;,!Y  cs_ +ao..K*+*	*I1{I222HH		+ $ $ $VFE08uufHsNGCSXX,N,NOOH
+%Hak; ;C
S//t#		" 1 1 1)*)<s9~~%%!+Ix 1afQi  a' IqzBB ? ?/#tT	?

 
%
%
%
%( 	? 	? 	?74 7 7DNN,<,< 7 7s))7 7 78 8=>?	?
 =))q01  _ /SS1?SSSSSN'>2BCCL(...	
H	-	-$	xY0A1?	RRsJ   A) )G8A/C''G9A*G$E98G9G9GGGGc                    ddl m} t          |d | d d         D                       }t          | |          D ])\  \  }}\  }}}t	          ||          }	 ||||	           *d S )Nr   )jax_setattrc                $    g | ]\  }}}|j         S rx   r   )r   r   tds      ru   
<listcomp>z_set_states.<locals>.<listcomp>   s     MMMhaQBMMMMrw   )jax.experimental.attrsr   r_   r   rO   )
r   valsr   valssr   treedefobjattrleavesvals
             ru   r   r      s    000000
TMM-:LMMM
N
N%-0-F-F    ) 7KS$&
&
)
)CKT3   rw   c                    ddl m} g }| D ]D\  }}\  }} |||          }t          |          \  }}	||	k    sJ |                    |           E|S Nr   jax_getattr)r   r   rN   extend)
r   r   r   r   r   r   r   treer   treedef_s
             ru   r   r      s    000000	$!.  gq+3;sD!!D#D))FHhKK	+rw   c                \    | d uo(|                                  o|                                  S r   )
is_enabledis_fdo_consumed)pgle_profilers    ru   _need_to_rebuild_with_fdor     s9    
t
# 2(@(@(B(B 2//1113rw   return&pxla.MeshExecutableFastpathData | Nonec	                R    t          j        ||          \  }	} d uot           t           j                  ot           j        t           j                  o j        j         o j        j         o j        j         o{t          d |	D                       ob|d u o^| o[t          d |D                        oAt          j        j        ot          d g |||R D                        ot          |           }
|
rd |	D             }d |	D             } fdt          t!          |                    D             }d t#           j         j                  D             }t          j         j        || j        ||| j        j        j         j        j        j        	  	        }nd }|S )Nc              3  J   K   | ]}t          |t          j                  V  d S r   )
isinstancexc	ArrayImplr   xs     ru   r   z%_get_fastpath_data.<locals>.<genexpr>  s.      CCajBL))CCCCCCrw   c              3  @   K   | ]}t          |t                    V  d S r   )r
  rK   )r   r   s     ru   r   z%_get_fastpath_data.<locals>.<genexpr>  s,      <<q*Q	**<<<<<<rw   c              3  ~   K   | ]8}t          |d           o#t          j        |j        t          j                  V  9dS )dtypeN)hasattrr   
issubdtyper  prng_key)r   r   s     ru   r   z%_get_fastpath_data.<locals>.<genexpr>	  sV       45 45 	WO&"3CIv"O"O45 45 45 45 45 45rw   c                    g | ]	}|j         
S rx   )rr   r   os     ru   r   z&_get_fastpath_data.<locals>.<listcomp>  s    111A111rw   c                    g | ]	}|j         
S rx   )
_committedr  s     ru   r   z&_get_fastpath_data.<locals>.<listcomp>  s    ;;;aQ\;;;rw   c                "    g | ]}|j         v S rx   )_kept_var_idx)r   i
executables     ru   r   z&_get_fastpath_data.<locals>.<listcomp>  s1     7 7 7 J44 7 7 7rw   c                    g | ]N\  }}|t           j        ur9t          j        |j        t          j                  rt          j        ||          n|OS rx   )r   abstract_tokenr   r  r  extendedr   physical_sharding)r   sr   s      ru   r   z&_get_fastpath_data.<locals>.<listcomp>  sg        Aq D'''F,=agv,W,W' 	(A...  rw   )r2   reflatten_outputs_for_dispatchr
  MeshExecutableunsafe_callExecuteReplicatedordered_effectshas_unordered_effectshas_host_callbacksallanyr   debug_key_reusevaluer  ranger   r   _in_shardingsr   MeshExecutableFastpathDataxla_executable_out_shardings
in_handlerlocal_devicesinput_indices)r  r   r   r   r   effectsr   r   r  out_reflatteneduse_fastpath	out_avalsout_committedkept_var_bitvecin_shardingsfastpath_datas   `               ru   _get_fastpath_datar>     sB    #A(HUU/8  7
Z!4
5
57
Z+T-C
D
D7 $4
4	7
 $:
:7 $7
77 CC?CCC
C
C7 T
!7 
7 <<G<<<<<
<7 %+ 5 45 453Y33F3345 45 45 15 15 67" (66
6% *  11111I;;?;;;M7 7 7 7 %c)nn 5 57 7 7O  
0*2EFF	  L 3!8\!9m_)7)7	9 9MM M	rw   c                      e Zd Zd ZdS )_MostRecentPjitCallExecutablec                f    t          j                    | _        t          j                    | _        d S r   )weakrefWeakKeyDictionaryweak_key_dictweak_pgle_profiler_dictr   s    ru   __init__z&_MostRecentPjitCallExecutable.__init__%  s*     244D#*#<#>#>D   rw   N)r   r   r   rF  rx   rw   ru   r@  r@  $  s#        ? ? ? ? ?rw   r@  c                B    t           j                            | d           S r   )!_most_recent_pjit_call_executablerD  getr   s    ru   &_read_most_recent_pjit_call_executablerK  ,  s    	*	8	<	<UD	I	IIrw   c                B    t           j                            | d           S r   )rH  rE  rI  rJ  s    ru   _read_pgle_profilerrM  0  s"    	*	B	F	FT
 
 rw   c                    |                                   t                              | j                   t                                           d S r   )_clear_cache_create_pjit_jaxprevict_function_fun_infer_params_cachedcache_clearr   s    ru   _cpp_pjit_evict_fnrU  5  sB    ##DI...""$$$$$rw   i    )capacityc                P    | rt           j                                        S t          S r   )r  _xlaPjitFunctionCache_cpp_pjit_cache)pjit_has_explicit_shardings    ru   _get_cpp_global_cacher\  A  s$     7$$&&&rw   r   r   r   c                `    t            fd            }t          j                            t	                      |j        j        j        t          j	        d t          j                  	  	        } t                     |          } |_        t          t          |          _        |S )Nc                     t          g| R i |\  }}}}}}t          |          }t          |          }	t          ||||||j        |j        j        |		  	        }
||
t          |	          fS r   )r   rK  rM  r>  r6  r   r   r  )r   r   r   r   r   r   r   r   r  r  maybe_fastpath_datar   r   s              ru   
cache_missz_cpp_pjit.<locals>.cache_missJ  s    @SXA(A( A( A( &A( A(=D(Hi7>>J'..M,Hi=%-h. 
 $&?&N&NNNrw   c                <    t          j        |g| g          d         S Nr   r2   
shard_argsr  r9   s     ru   <lambda>z_cpp_pjit.<locals>.<lambda>[  s    
QC88; rw   )rM   r  rX  r   rc   r   r   r   r   dispatch_registryr\  r   r]   rR  rU  r   clear_cache)r   r   r`  
cpp_pjit_fcpp_pjitted_fs   ``   ru   	_cpp_pjitrk  H  s    
O 
O 
O 
O 
O <
O w||SMMX,h.FY8;;(899; ;* %**Z((--$6$}!	rw   r   c                    t          |           \  }}t          |          \  }}|d up5|d up1t          d |D                       pt          d |D                       S )Nc              3  6   K   | ]}t          |           V  d S r   rC   r   r  s     ru   r   z*_pjit_explicit_sharding.<locals>.<genexpr>j  s-      ??.###??????rw   c              3  6   K   | ]}t          |           V  d S r   rn  ro  s     ru   r   z*_pjit_explicit_sharding.<locals>.<genexpr>k  s-      @@.###@@@@@@rw   )rN   r+  )r<  out_shardingsr   r   in_shardings_flatr   out_shardings_flats          ru   _pjit_explicit_shardingrt  d  s    %l33Q&}55a

 A

A
??->???
?
?A @@-?@@@
@
@Brw   c                   t          | d           \  }}g g }}|D ]}t          |t                    r5|                    |j                   |                    |j                   Lt          |t          t          f          rt          d|           |                    d            |                    |           t          |          t          |          k    sJ t          ||          t          ||          fS )Nc                
    | d u S r   rx   r  s    ru   rf  z,_split_layout_and_sharding.<locals>.<lambda>o  s
    !t) rw   )is_leafz`jax.jit` does not accept device-local layouts directly. Create a `Layout` instance wrapping this device-local layout and pass that to `jit` instead. Got )rN   r
  rG   rl   device_local_layoutr9   rH   rI   r   r   rO   )entriesentries_flatr   layouts	shardingsr   s         ru   _split_layout_and_shardingr~  n  s   &w8K8KLLL,29'  a!V 
nnQ*+++qz""""	A):6	7	7 ,(), ,- - -
 nnTq	WY	'	'	'	'		)	)>'9+M+M	MMrw   r<  r
   rq  r   int | Sequence[int] | Noner   str | Iterable[str] | Noner   r   r   r   r   r   r   r   r   r   r   c                   |	r t           j        j        st          d          t	          |            ||st          j        dt                     ||t          d|d|          |t          |          st          d          |t          |          st          d          t          |t                    rt          |          }t          |          \  }}t          |          \  }}t          |d          }t          |d	          }|duot          |           }t          j        |          \  }}t          j        |          \  }}t          j        |          \  }}t          j        |          \  }}t!          j        |           }t!          j        |           }t'          | |||||          \  }}}}t)          ||||          }t+          d i d
|d|d|d|dt          |          d|dt          |          d|dt          |          d|dt          |          d|d|d|d|d|d|d|
d|d|	d|d|S )!zParses the arguments to jit/pjit.

  Performs any preprocessing and validation of the arguments that we can do
  ahead of time before the jit()-ed function is invoked.
  z6abstracted_axes must be used with --jax_dynamic_shapesNzbackend and device argument on jit is deprecated. You can use `jax.device_put(..., jax.local_devices("cpu")[0])` on the inputs to the jitted function to get the same behavior.z>can't specify both a device and a backend for jit, got device=z and backend=zTIf backend or device is specified on jit, then in_shardings should not be specified.zUIf backend or device is specified on jit, then out_shardings should not be specified.r<  rq  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rx   )r   dynamic_shapesr-  r   r%   warningswarnDeprecationWarningrC   r
  listtupler~  rE   none_lrflattenr   r   r   r&   rt  r   )r   r<  rq  r   r   r   r   r   r   r   r   r   r   
in_layoutsout_layoutsr   r   r   r   r   r   r   r   r   r   r   r   s                              ru   _parse_jit_argumentsr    s     OV28 O
M
N
NNF.M	9:LN N N g1 7$7 7,37 7 8 8 8|(D(D ? @ @ @ )F)F  @ A A A d## ' &&L7EE*l9-HH+}'nEE,(HH-!-T!9 "B%3L%A%A!A  /6ol.K.K++070N0N--*1/**E*E'',3OK,H,H))*3//.(---ET	=./>F FB./>? 2M674 4 
 
+ 
+ 
+%~
+#m
+ %@$?
+ 21	
+
 ""5666
+ 43
+ ##7888
+ .-
+   1222
+ 0/
+ !!3444
+ &~
+ (
+ 9G
+ (
+ 17
+ AH
+  K
+ )/
+  (!
+" 43#
+$ *)%
+ +rw   c                     t            fd            }t            fd            }t           d fd            t                     }||_        ||_        |_        |S )Nc                 2    | i |}	 |                                 S # t          j        $ rh}|j        \  }t	          dt	          dt                                        }t          |||j        d|j                  }t          |          d d }~ww xY w)Nr   r   r   )
lowerr2   r   r   r   r   r   
_args_flat
_arg_namesr   )	r   r   tracedr   ry   rc   r   r   traces	          ru   r  z _make_jit_wrapper.<locals>.lower  s    UD#F##F&\\^^- & & &vfen j#c((;;= =h-
E6,eV5FH HcsOO%&s    BA#BBc                     t          | |          \  }}d |j        d         D             }d t          |j        d         j        |          D             }t	          |j        |          S )Nc                4    g | ]}t          |          rd n|S r   rn  r   r"  s     ru   r   z9_make_jit_wrapper.<locals>.eval_shape.<locals>.<listcomp>  s)    QQQ!^A&&-TTAQQQrw   rq  c                V    g | ]&\  }}t          j        |j        |j        |           'S )r8   )r   ShapeDtypeStructshaper  )r   r  r"  s      ru   r   z9_make_jit_wrapper.<locals>.eval_shape.<locals>.<listcomp>  sH     @ @ @q! 1=== @ @ @rw   r   )r   r   r   r9  rO   r   )r   r   r   r   out_soutr   r   s         ru   
eval_shapez%_make_jit_wrapper.<locals>.eval_shape  s}    hf55DAqQQqx7PQQQE@ @18G,6>>@ @ @C!*c***rw   r  stages.Tracedc            
     |   t          | |          \  }}t          d t          |j                  D                       }t	          j        |j        |j        |          }t          t          |fi |j
        dd i}t	          j        |j
        d         ||j
        d         |j        |||j        |j                  S )Nc              3  $   K   | ]\  }}||V  d S r   rx   r   r  ds      ru   r   z3_make_jit_wrapper.<locals>.trace.<locals>.<genexpr>  s+      JJAJ1JJJJJJrw   r  r   rp   )r   r  	enumeratedonated_invarsr   make_args_infoin_treer   r   _resolve_and_lowerr   Tracedr   r   
num_consts)	r   r   r   r   r   	args_infolower_callabler   r   s	          ru   r  z _make_jit_wrapper.<locals>.trace  s     hf==LAyJJ13C)D)DJJJJJN%ai^LLI/ 1 1ah 1 1+/1 1 1N=	9ahv&6qz	1;> > >rw   r  r  )rM   rk  r  r  r  )r   r   r  r  wrappedr  s   ``   @ru   _make_jit_wrapperr    s    
& 
& 
& 
& 
& <
& + + + + + <+ > > > > > > <> c8$$''-!''-	.rw   c                X    t          | |||||||||	|
||          }t          | |          S )z8jit() and pjit() are thin wrappers around this function.)r  r  )r   r<  rq  r   r   r   r   r   r   r   r   r   r   r   s                 ru   make_jitr    sC     "\=.//V-/ /( 
3	)	))rw   c                  j    e Zd ZU ded<   ded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   dS )
PjitParamsz	list[Any]r   dict[str, Any]r   tuple[core.AbstractValue, ...]r   rX   r  r   tuple[bool, ...]r  tuple[str, ...] | Noner   intr  z2list[tuple[PyTreeDef, PyTreeDef, tuple[Any, str]]]r   N)r   r   r   r   rx   rw   ru   r  r    s{         ****""""####///CCCCCCrw   r  ji	pjit_meshmesh_lib.Mesh | Noner   mesh_lib.ResourceEnv | Noner   r   r   r  r   %tuple[core.AbstractValue, ...] | Nonetuple[PjitParams, list[Any]]c                
  / t          |          }|r|j        rt          d          'd/|j        s|j        rj        st          d          nd/t          |j        g|R i |}t          /|j	        |j
        |||j        |j                  }	t          j        |           }
t          |
          \  }
}t!          |
|j        |d          \  }
}~t#          |
|j        |          \  }
}t%          ||f          \  }}t'          |
|          \  }}t)          ||          \  }}|j        s|j        r-t.          j        j        st5          |j        |j        |          }ndt7          |          z  }t          |j        p|j                  }|rBt9          |j        |j                  }t%          |          \  }}t;          |          x}}|x}}nPt;          /fd|j        D                       }|j        }t;          /fd	|j         D                       }|j!        }d |vsJ d |vsJ t.          j"        j        r/tG          j$        ||          }t;          d
 |D                       }n|g }tK          |          D ]e\  }}	 |&                    tO          |                     )# tP          $ r0}|	rd|	j)        |          nd| } tQ          d|  d          |d }~ww xY wt;          |          x}}n|}tU          |||j+        |j,        |||	||	  	        \  }!}"t[          ||          }#t]          |||#|	t_          |d          ta          |j1                            \  }$}%}&}'te          |||#|'           tg          |||j4        |j5        t_          |d          t;          |&          |$j6        j        |          \  }(})t7          |          t7          |!          cxk    rt7          |"          k    sn J t.          j"        j        r)to          tq          tr          j:        |          |          }*ng }*g |*|}+tw          d |'D                       },t7          |*          |,z   t7          |%          z   }-tx          f|-z  |!z   }!d|-z  |"z   }"d|-z  |z   }t7          |!          t7          |"          cxk    r:t7          |          cxk    r&|,t7          |%          z   t7          |+          z   k    sn J t{          |$|!|(|"|)||t}          |          |j?        |j1        
  
        }.t          |%|.|| |            ||	r|	j)        nd t7          |%          |'	  	        |+fS )Nz<pjit does not support kwargs when in_shardings is specified.r   zpMesh context manager should not be used with jit when backend or device is also specified as an argument to jit.r   T)allow_invalidFc              3  <   K   | ]}t          |d           V  dS )r<  N_create_sharding_for_arrayr   r  jit_namer  s     ru   r   z%_infer_params_impl.<locals>.<genexpr>S  sE        )  ) 	#9aJJ )  )  )  )  )  )rw   c              3  <   K   | ]}t          |d           V  dS )rq  Nr  r  s     ru   r   z%_infer_params_impl.<locals>.<genexpr>W  sE       !* !* 	#9a(KK!* !* !* !* !* !*rw   c              3  $   K   | ]\  }}||V  d S r   rx   )r   r   r   s      ru   r   z%_infer_params_impl.<locals>.<genexpr>b  s+      0041aa0Q000000rw   zargument path is zflattened argument number is zUAn overflow was encountered while parsing an argument to a jitted computation, whose .rx   closurec              3  ,   K   | ]\  }}}|j         V  d S r   r   )r   	init_treer   s      ru   r   z%_infer_params_impl.<locals>.<genexpr>  s+      PPy!Qi*PPPPPPrw   r   
r   r<  rq  r  r  r   r  rp   r   r   )Ar   r   r   r   r   empty_flat_axes_specsr   r(   r   r   r   r   lu	wrap_initr)   r   r'   rN   r!   r+   r   r   r   
debug_nansr-  r#   r   $_create_sharding_with_device_backendr  r   r   r   r   r  peinfer_lambda_input_typer  rl   r$   OverflowErrorr   _process_in_axis_resourcesr   r   _attr_tokenrP  rZ   	IgnoreKeyr   _attr_update%_check_and_canonicalize_out_shardingsr   r   r   _extract_implicit_argsr   r   	InputTyper   r?   dictrd   r   r  )0r   r  r  r   r   r   r   have_kwargs
axes_specsdbgf	res_pathsdyn_args
dyn_kwargsexplicit_argsr  flat_funr   r  device_or_backend_setr9   r   r   r   r   r   r   in_typeavalsr  r   r   arg_pathrr  in_layouts_flat
attr_tokenr   r   r9  r   rs  out_layouts_flatimplicit_argsr   num_states_innum_extra_argsr   r  s0     `                                            @ru   _infer_params_implr    s    V+ HR3 H
FH H H H

 =bi = =<= = = H 2DTDDDVDD*8R.0@$$b&8	: 	:#l3!a,!Y&q"*;TQUVVV+!X
)!R-?HH-!Z':(>??-"1g..(H+HmDD(M 32- 3v7H7N 3$R%68JGTTNNM 2 22N
 rz6RY77 53BIrzJJH"8,,OFG16v>.3::00  )  )  )  )  )' )  )  ) ) ) 2  !* !* !* !* !*(!* !* !* * * 4	(	(	(	(	(	)	)	)	)	)   (]CCG00700000HHE-(( 	 	1'**++++   >A =:a(8:::<<< 	, (, , ,
 
 	 u%GhhG'A/R13[(B (B$_
 8W--*,>Sy"---	- -)%M x*m<<<)N123I-hCCCI.0E*G *G&&
 
]		s#455	M	M	M	M_9M9M	M	M	M	M	M	M  *T^W%%}6 6MM M...)PP-PPPPP-}%%5FC."n~58IIn,>/n,~=.

 
 C$8$8 N N N N
n

N N N N!.V!<s9~~!MN N N N N N $& "#"".Y  & 
FFHgxxzz"S$BCMMdCKK!
# 
#$-
. .s   	"J,,
K&6+K!!K&c                  *    e Zd ZU dZdgZded<   d ZdS )InferParamsCacheEntryz.Mutable value object for _infer_params_cached.pjit_paramszPjitParams | Nonec                    d | _         d S r   )r  r   s    ru   rF  zInferParamsCacheEntry.__init__  s    Drw   N)r   r   r   r   	__slots__r   rF  rx   rw   ru   r  r    s>         66o)        rw   r  	signaturejax_jit.ArgumentSignaturer  c                    t                      S r   )r  )r   r   r  r   r  r   s         ru   rS  rS    s     
	 	  rw   c           	        |j         rt          j        j        }|j        }nd }d }t
          dk     pt          j        j        }|st          j
        |t          |                                          t          |                                          |j        |j        t           j                  \  }}	 t          d |D                       }	n# t$          t&          f$ r d}Y nw xY w|r%t)          | |||||d           \  }
}|
|
j        |z   fS t-          | |||	||          }|j        3t)          | ||||||	          \  }
}|
j        r|
|
j        |z   fS |
|_        |j        |j        j        |z   fS )Ni  c              3  4   K   | ]}t          |          V  d S r   )r$   )r   r   s     ru   r   z _infer_params.<locals>.<genexpr>  s+      ;;a&q));;;;;;rw   T)r   )r   mesh_libthread_resourcesenvphysical_meshr7   r   r  r-  r5   parse_argumentsr  valueskeysr   r   r   default_registryr  r   r  r   rS  r  r   )r   r  r   r   r   r  
skip_cacher  dynargsr  r   r   entrys                ru   r   r     s      ,0L*IILI$s*If.C.I*	  0eFMMOO$$eFKKMM&:&:B<M
I68 8Iw;;7;;;;;ee9%   jjj  #%c2y,&,t= = =LAyah"""
	2y%L: :%
%RL$H H HLAy 9$$$e		E-4w>	>>s   2C C"!C"r  )Sequence[tuple[core.AbstractValue, bool]]r  Sequence[Any]Sequence[core.Tracer]c                B  	 t          |          		fd| D             }t          	d          J ~	t          |           D ]\  }\  }}|rt          |t          j                  s%||         }|J t          |j        |j        j                  D ]X\  }}t          |t          j	                  r9||j
                 
|||j
        <   t	          j        ||j
                 |          sJ Yt          d |D                       sJ d t          ||           D             S )z
  Given an input type and explicitly-passed arguments (per the user-facing API
  calling convention), extract implicit axis size arguments from shapes of
  explicit arguments (for the trace-time / jaxpr-level calling convention).
  c                <    g | ]\  }}|rt                    nd S r   )next)r   r   explexplicit_args_s      ru   r   z*_extract_implicit_args.<locals>.<listcomp>  s/    	H	H	HWQ$
0$~


D	H	H	Hrw   Nc              3     K   | ]}|d uV  	d S r   rx   r  s     ru   r   z)_extract_implicit_args.<locals>.<genexpr>  s&      ))qQd]))))))rw   c                "    g | ]\  }\  }}|
|S rx   rx   )r   r  r   r   s       ru   r   z*_extract_implicit_args.<locals>.<listcomp>  s%    	9	9	9	6Aqq	9!	9	9	9rw   )iterr  r  r
  r   DShapedArrayr   r  rr   DBIdxr   same_referentr*  )
r  r  r   r  rr   explicitr   d1d2r  s
            @ru   r  r    sP    &&.	H	H	H	H	H	H	H$	nd	#	#	+	+	+^ 'w// 	4 	4a	$ :dD,=>> 
q'C???dj#(.11 4 4B	B
	#	# 4<$rv,!$rv,33333	4
 
))D)))	)	))))	9	9Sw//	9	9	99rw   "list[pe.AbstractedAxesSpec] | Nonec                D    | d S |rt           d }t          | ||          S )Nc                    t          | t                    r!t          |                                           p%t          | t                    ot          | d           S )Nc                
    | d u S r   rx   rw  s    ru   rf  z3_flat_axes_specs.<locals>.ax_leaf.<locals>.<lambda>  s
    Q$Y rw   )r
  r  rU   r  r  )ls    ru   ax_leafz!_flat_axes_specs.<locals>.ax_leaf  sP    q$:Jqxxzz$:$: Hq%  GZ3F3F%G%GIrw   )NotImplementedErrorrT   )r   r   r   r  s       ru   r  r    s>    TT&&&I I I 
/4	9	99rw   c                      e Zd Zd ZddZdS )
JitWrappedc                    t           )zSee ``jax.eval_shape``.r  r   r   r   s      ru   r  zJitWrapped.eval_shape  s    
rw   r  r  c                    t           r   r"  r#  s      ru   r  zJitWrapped.trace  s    
rw   Nr  )r   r   r   r  r  rx   rw   ru   r   r     s7               rw   r   Fc                :    t          | ||||||||	|||
d          S )u	&  Makes ``fun`` compiled and automatically partitioned across multiple devices.

  NOTE: This function is now equivalent to jax.jit please use that instead.
  The returned function has semantics equivalent to those of ``fun``, but is
  compiled to an XLA computation that runs across multiple devices
  (e.g. multiple GPUs or multiple TPU cores). This can be useful if the jitted
  version of ``fun`` would not fit in a single device's memory, or to speed up
  ``fun`` by running each operation in parallel across multiple devices.

  The partitioning over devices happens automatically based on the
  propagation of the input partitioning specified in ``in_shardings`` and
  the output partitioning specified in ``out_shardings``. The resources
  specified in those two arguments must refer to mesh axes, as defined by
  the :py:func:`jax.sharding.Mesh` context manager. Note that the mesh
  definition at :func:`~pjit` application time is ignored, and the returned function
  will use the mesh definition available at each call site.

  Inputs to a :func:`~pjit`'d function will be automatically partitioned across devices
  if they're not already correctly partitioned based on ``in_shardings``.
  In some scenarios, ensuring that the inputs are already correctly pre-partitioned
  can increase performance. For example, if passing the output of one
  :func:`~pjit`'d function to another :func:`~pjit`’d function (or the same
  :func:`~pjit`’d function in a loop), make sure the relevant
  ``out_shardings`` match the corresponding ``in_shardings``.

  .. note::
    **Multi-process platforms:** On multi-process platforms such as TPU pods,
    :func:`~pjit` can be used to run computations across all available devices across
    processes. To achieve this, :func:`~pjit` is designed to be used in SPMD Python
    programs, where every process is running the same Python code such that all
    processes run the same :func:`~pjit`'d function in the same order.

    When running in this configuration, the mesh should contain devices across
    all processes. All inputs arguments must be globally shaped.
    ``fun`` will still be executed across *all* devices in the mesh,
    including those from other processes, and will be given a global view of the
    data spread across multiple processes as a single array.

    The SPMD model also requires that the same multi-process :func:`~pjit`'d
    functions must be run in the same order on all processes, but they can be
    interspersed with arbitrary operations running in a single process.

  Args:
    fun: Function to be compiled. Should be a pure function, as side-effects may
      only be executed once. Its arguments and return value should be arrays,
      scalars, or (nested) standard Python containers (tuple/list/dict) thereof.
      Positional arguments indicated by ``static_argnums`` can be anything at
      all, provided they are hashable and have an equality operation defined.
      Static arguments are included as part of a compilation cache key, which is
      why hash and equality operators must be defined.
    in_shardings: Pytree of structure matching that of arguments to ``fun``,
      with all actual arguments replaced by resource assignment specifications.
      It is also valid to specify a pytree prefix (e.g. one value in place of a
      whole subtree), in which case the leaves get broadcast to all values in
      that subtree.

      The ``in_shardings`` argument is optional. JAX will infer the shardings
      from the input :py:class:`jax.Array`'s, and defaults to replicating the input
      if the sharding cannot be inferred.

      The valid resource assignment specifications are:

      - :py:class:`Sharding`, which will decide how the value
        will be partitioned. With this, using a mesh context manager is not
        required.
      - :py:obj:`None` is a special case whose semantics are:
          - if the mesh context manager is *not* provided, JAX has the freedom to
            choose whatever sharding it wants.
            For in_shardings, JAX will mark is as replicated but this behavior
            can change in the future.
            For out_shardings, we will rely on the XLA GSPMD partitioner to
            determine the output shardings.
          - If the mesh context manager is provided, None will imply that the
            value will be replicated on all devices of the mesh.
      - For backwards compatibility, in_shardings still supports ingesting
        :py:class:`PartitionSpec`. This option can *only* be used with the
        mesh context manager.

        - :py:class:`PartitionSpec`, a tuple of length at most equal to the rank
          of the partitioned value. Each element can be a :py:obj:`None`, a mesh
          axis or a tuple of mesh axes, and specifies the set of resources assigned
          to partition the value's dimension matching its position in the spec.

      The size of every dimension has to be a multiple of the total number of
      resources assigned to it.
    out_shardings: Like ``in_shardings``, but specifies resource
      assignment for function outputs.
      The ``out_shardings`` argument is optional. If not specified, :py:func:`jax.jit`
      will use GSPMD's sharding propagation to determine how to shard the outputs.
    static_argnums: An optional int or collection of ints that specify which
      positional arguments to treat as static (compile-time constant).
      Operations that only depend on static arguments will be constant-folded in
      Python (during tracing), and so the corresponding argument values can be
      any Python object.

      Static arguments should be hashable, meaning both ``__hash__`` and
      ``__eq__`` are implemented, and immutable. Calling the jitted function
      with different values for these constants will trigger recompilation.
      Arguments that are not arrays or containers thereof must be marked as
      static.

      If ``static_argnums`` is not provided, no arguments are treated as static.
    static_argnames: An optional string or collection of strings specifying
      which named arguments to treat as static (compile-time constant). See the
      comment on ``static_argnums`` for details. If not
      provided but ``static_argnums`` is set, the default is based on calling
      ``inspect.signature(fun)`` to find corresponding named arguments.
    donate_argnums: Specify which positional argument buffers are "donated" to
      the computation. It is safe to donate argument buffers if you no longer
      need them once the computation has finished. In some cases XLA can make
      use of donated buffers to reduce the amount of memory needed to perform a
      computation, for example recycling one of your input buffers to store a
      result. You should not reuse buffers that you donate to a computation, JAX
      will raise an error if you try to. By default, no argument buffers are
      donated.

      If neither ``donate_argnums`` nor ``donate_argnames`` is provided, no
      arguments are donated. If ``donate_argnums`` is not provided but
      ``donate_argnames`` is, or vice versa, JAX uses
      :code:`inspect.signature(fun)` to find any positional arguments that
      correspond to ``donate_argnames``
      (or vice versa). If both ``donate_argnums`` and ``donate_argnames`` are
      provided, ``inspect.signature`` is not used, and only actual
      parameters listed in either ``donate_argnums`` or ``donate_argnames`` will
      be donated.

      For more details on buffer donation see the
      `FAQ <https://jax.readthedocs.io/en/latest/faq.html#buffer-donation>`_.
    donate_argnames: An optional string or collection of strings specifying
      which named arguments are donated to the computation. See the
      comment on ``donate_argnums`` for details. If not
      provided but ``donate_argnums`` is set, the default is based on calling
      ``inspect.signature(fun)`` to find corresponding named arguments.
    keep_unused: If `False` (the default), arguments that JAX determines to be
      unused by `fun` *may* be dropped from resulting compiled XLA executables.
      Such arguments will not be transferred to the device nor provided to the
      underlying executable. If `True`, unused arguments will not be pruned.
    device: This argument is deprecated. Please put your arguments on the
      device you want before passing them to jit.
      Optional, the Device the jitted function will run on. (Available devices
      can be retrieved via :py:func:`jax.devices`.) The default is inherited
      from XLA's DeviceAssignment logic and is usually to use
      ``jax.devices()[0]``.
    backend: This argument is deprecated. Please put your arguments on the
      backend you want before passing them to jit.
      Optional, a string representing the XLA backend: ``'cpu'``, ``'gpu'``, or
      ``'tpu'``.

  Returns:
    A wrapped version of ``fun``, set up for just-in-time compilation and
    automatically partitioned by the mesh available at each call site.

  For example, a convolution operator can be automatically partitioned over
  an arbitrary set of devices by a single :func:`~pjit` application:

  >>> import jax
  >>> import jax.numpy as jnp
  >>> import numpy as np
  >>> from jax.sharding import Mesh, PartitionSpec
  >>> from jax.experimental.pjit import pjit
  >>>
  >>> x = jnp.arange(8, dtype=jnp.float32)
  >>> f = pjit(lambda x: jax.numpy.convolve(x, jnp.asarray([0.5, 1.0, 0.5]), 'same'),
  ...         in_shardings=None, out_shardings=PartitionSpec('devices'))
  >>> with Mesh(np.array(jax.devices()), ('devices',)):
  ...   print(f(x))  # doctest: +SKIP
  [ 0.5  2.   4.   6.   8.  10.  12.  10. ]
  T)r   )r  )r   r<  rq  r   r   r   r   r   r   r   r   r   s               ru   r   r     s9    l 

L-FT
3 
3 
3 3rw   c                v    t          |           \  t                    t          fdf          S )Nc                 $    t                     S r   )rO   )r   r   s   ru   rf  z!hashable_pytree.<locals>.<lambda>  s    .$"?"? rw   r  )rN   r  rZ   )pytreer   r   s    @@ru   hashable_pytreer)    sM    v&&-$	t$	?????#*D/
3 
3 
3 3rw   c                   || | j         rt          S t          |t          j                  st          |          r|S | 4d| d}|dk    r
|d| dz  }n|dk    r	|d| dz  }t          |          | j         rt          | d| d	| d
          |t          |t                    s
J |            |t          j	        | |          nt          j	        | |j
        |          S )Nz2jax.jit only supports `Sharding`s being passed to z_. Looks like you are passing either `PartitionSpec` or `None` which is not allowed in jax.jit.
r<  z
Note that z argument is optional. JAX will infer the shardings from the input jax.Array's and will default to replicating the input if the sharding cannot be inferred.rq  z is optional. If not specified, jax.jit will use GSPMD's sharding propagation to figure out what the sharding of the output(s) should be.zL requires a non-empty mesh if you are passing `PartitionSpec`s or `None` to zL! Is a mesh defined at the call site? Alternatively, provide `Sharding`s to z3 and then the mesh context manager is not required.)r  r?   r
  r9   ShardingrD   RuntimeErrorrA   r2   create_mesh_pspec_sharding	user_spec)r   r  rp   r   r   s        ru   r  r    sg   YDLDJL8$%% )?)B)B H	\22 2 2C ~	 ;4 ; ; ; <cc 
	 	 	 -4 - - - .c s

	Z ;
 	: 	:*.	: 	:8<	: 	: 	:; ; ; 
j$788!Y 
)$
2
2
2 ;D!+qQQSrw   c                    | |J t          |           }nO|>| J t          t          j        |                                          d                   }nt	          d          d|_        |S )Nr   zUnreachable!T)r<   xbget_backendr4  r   _device_backend)r   r   r  s      ru   r  r    st    ???
v
&
&CC>>>
r~g66DDFFqI
J
JCC

(
((#	*rw   c           
        	 t          t          | |||                    S # t          $ r Y nw xY wt          t	          |                    rJ |rNt          |          t           us3t          |          t          |                                          k    r|  d|  d}t          |          t           ur|d|  dt          |           dz  }nmt          |          t          |                                          k    r;|d|  dt          |           dt          |                                           d	z  }t          |                                          d
k    r,t          | ||f           |d|  dz  }n# t          $ r Y nw xY wt          |          |}t          |t                      g|j
        z            }t          ||          }|r|d         } ||           J d            )Ntupled_argsz specification must be a tree prefix of the positional arguments tuple passed to the `pjit`-decorated function. In particular, zj must either be a None, a PartitionSpec, or a tuple of length equal to the number of positional arguments.z But z is not a tuple: got z	 instead.z4 is the wrong length: got a tuple or list of length z for an args tuple of length r  r   z> Given the corresponding argument being passed, it looks like z/ might need to be wrapped in a singleton tuple.r   FzPlease open a bug report!)r  r    r   rP   rQ   r   r   childrenrO   
PytreeLeafr   rV   )	whatr   r}  r5  r   	axis_tree
dummy_treeerrorsr   s	            ru   flatten_axis_resourcesr<    sO   	dD)MMMNNN	 	 	 	D	
 ^I6677777  d9ooU22)nnDMMOO(<(<<<  M MM M MC Ie##	JTJJYJJJJcc	Y3t}}//	/	/	 )d ) )Y) )T]]__%%) ) ) *c 4==??q  dYL111 	 &)-& & & 	'  S//) dZ\\NT_$DEE*J//& q	A
!D''M ,++++s   " 
//&F 
FFc                      e Zd Zd ZdS )r7  c                    dS )Nzpytree leafrx   r   s    ru   __repr__zPytreeLeaf.__repr__C  s    ]]rw   N)r   r   r   r?  rx   rw   ru   r7  r7  B  s        *****rw   r7  i   )max_sizetrace_context_in_keyc	                   |st          |          \  }}	t          | |          }
t          |
          r|
ft          |          z  }nt	          d||
d          }t          ||          }||ft          |          z  }nt	          d||d          }|o$t          |j                  t          |          k    }t          j        j        s:|s8t          |||d n|j        dd           t          |||d n|j        d           ||fS )	Nzpjit in_shardingsTr4  zpjit in_layoutszpjit argumentsFallow_uneven_shardingzjit arguments)rS   rO   rC   r   r<  r   r   r  r-  pjit_check_aval_shardingcheck_aval_layout_compatibility)r   r   r   r   r   r  r(   r  kwsr   orig_in_shardingsrr  r  r  r   s                  ru   r  r  F  sp   
 
 +!'**JGQ$%9;NOO %&& K*,s8}}<.W&7TK K K 02CDD*!mc(mm3OO,7JDB B BO KZ%9!:!:c(mm!K-			$ O] O.%/%7TTZ=Q-UL L L L $"
(<oO O O 
O	++rw   zset[str]	callsitesr  unseen_fcacher  keyr  c                Z  ,-./01 t           j        j        rd S d } ||          \  ./}}-|rd S g 11j        }1fd}t	          j        t	          j                              }	 |d|	 d           t          | d|           }
|=|j        r6|j        	                    d          ^}}}dd
                    |          z   }nd}|rX |d	|
 d
t          |            |            |	t          v r |d           t                              |	            |            S  |d|
 |            t          ||                                          }t!          .          \  ,0d |D             },0fd|D             }|s	t#          t!          ,                    }0                                \  }} |d| dt#          |           dd
                    t          t&          |                                ,0fd|D             }t)          |t+          |          j        d           }|s |d           nD |dt#          |           dd
                    t          t&          |                                 |            S -fd|D             }|s |d           -fd|D             }t)          |-fd          }d t/          t1          -|                    D             } |dd
                    t          t2          |                     d            |            S .fd|D             }|st#          t3          .                    d k     rd!. nd} |d"|            .fd#|D             }t)          |.fd$          }t5          t7          j        .|                    } |d%t#          |           d&           |D ]M\  }}}}|^} }d'd(g| j                 }! |d)|! t=          t?          |                     d*| d+| d,| 	           N |            S /fd-|D             }"|"st#          /          d.k     rFd/                     d0
                    d1 t1          |j!        /          D                                 }#nd}# |d2|#            /fd3|D             }t)          |/fd4          }$tE          t          tF          j$        |$/                    }% |d5|% d&           d6}&t1          |j!        |$/          D ]\  }'}(})|(|)k    rtK          |(          tK          |)          cxk    rtL          j'        k    rcn n`|((                    d7          |)(                    d7          }+}*|*|+k    r/|(j)        |)j)        z  sJ |*d8|(j)         d9z  }*|+d8|)j)         d9z  }+d7}&nt3          |(          t3          |)          }+}* |d)|' d*|* d:|+            |&r! |d;            |d<            |d=            |            S  |d>            |            S )?Nc                N    | ^}\   }\  }}}}}}}|^}\  }\  }}||||j         |fS r   )r   )rL  
transformsr   r  r(   r   ctxr  s           ru   unpackz*explain_tracing_cache_miss.<locals>.unpacko  sD    FICJA:J6Q'A:GGZS88rw   c                 t    t                               t          j        d                                         S )N
)loggerlogloggingWARNINGjoin)r   s   ru   rf  z,explain_tracing_cache_miss.<locals>.<lambda>y  s!    GOTYYs^^<< rw   zTRACING CACHE MISS at z	 because:r    z defined at r~   z  never seen function:
    z id=z~  but seen another function defined on the same line; maybe the function is
  being re-defined repeatedly, preventing caching?z  for c                0    g | ]^}}t          |          S rx   )rS   )r   kr   s      ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s%    BBBuq1'**BBBrw   c                $    g | ]}|gk    
|S rx   rx   r   t	args_treekwargs_trees     ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s2     9 9 9Q	;777 777rw   z  never seen passing z positional args and z keyword args with keys:
    z, c                z    g | ]7}|gk    
t          |d                                          d                    8S r   )set	node_datar]  s     ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  sM     4 4 4q9k222 adnn&&q)**222rw   )rL  defaultz)  closest seen is passing no keyword argsz  closest seen passes c                &    g | ]^ }}|k    |S rx   rx   r   r   crP  s      ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s"    555eq!AHHHHHrw   zF  tracing context doesn't match, e.g. due to config or context managerc                &    g | ]^ }}|k    |S rx   rx   rg  s      ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s"    777Aa3hh!hhhrw   c                T    t          t          t          j        |                     S r   r   mapopne)rh  rP  s    ru   rf  z,explain_tracing_cache_miss.<locals>.<lambda>  s    Cq#4F4F0G0G rw   )rL  c                *    g | ]\  }\  }}||k    |S rx   rx   )r   r  c1c2s       ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s%    NNN+!Xb"R2XXAXXXrw   z7  closest seen context tuple differs at positions:
    zN
  compare to tuple returned by config._trace_context() in jax/_src/config.py.c                ,    g | ]}|d          k    |S )r   rx   )r   r[  r  s     ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s"    999q1rw   L   z:
    z  never seen input pytreec                $    g | ]^}}|k    
|S rx   rx   )r   r^  r   r  s      ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s"    ;;;Aa7ll!lllrw   c                <    t          | j        j        z
            S r   )absr   )r^  r  s    ru   rf  z,explain_tracing_cache_miss.<locals>.<lambda>  s    Q\GDV5V1W1W rw   z   closest seen input pytree has z mismatches, including:r   r   z	    * at z, seen z but now given z
,      so c                ,    g | ]}|d          k    |S rb  rx   )r   r[  r  s     ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s"    ;;;q1Q47?????rw      z:
    {}z,  c              3  R   K   | ]"\  }}| d |                     d           V  #dS ): T)short_dtypesN)rm   )r   r   tys      ru   r   z-explain_tracing_cache_miss.<locals>.<genexpr>  sV       2; 2;a 
3
3",,D,11
3
32; 2; 2; 2; 2; 2;rw   z!  never seen input type signaturec                &    g | ]^}}}|k    |S rx   rx   )r   r   r^  r  s      ru   r   z.explain_tracing_cache_miss.<locals>.<listcomp>  s$    @@@1q1<<!<<<rw   c                T    t          t          t          j        |                     S r   rk  )r^  r  s    ru   rf  z,explain_tracing_cache_miss.<locals>.<lambda>  s    s3rua3I3I/J/J rw   z(  closest seen input type signature has FTz{weak_type=}z, but now given zEwhere weak_type=True often means a Python builtin numeric value, and z"weak_type=False means a jax.Array.zGSee https://jax.readthedocs.io/en/latest/type_promotion.html#weak-typeszNexplanation unavailable! please open an issue at https://github.com/google/jax)*r   check_tracer_leaksr-  rl   r   	summarizecurrentr   func_src_infosplitrX  r   rI  addrl  r  rS   r   rd  reprminrc  symmetric_differencer  r   r   r  r   equality_errors_pytreedefidxrW   r  formatr   r   rm  rn  r   r   ShapedArrayrm   	weak_type)2r  rJ  rK  rL  rQ  r(   r   r   donecallsiterc   r   restsrc_info	seen_keysargs_kwargs_treesargs_kwargs_matchnum_args
kwarg_keys
dont_matchclose_kwargs
ctxs_matchclosest_ctxidxstrees_matchin_tree_strclosest_treeerrspaththing1thing2explanationfstbasetypes_matchin_type_str
closest_tynum_mismatchadd_weak_type_hintrp   ty1ty2s1s2r_  rP  r  r  r`  r   s2                                               @@@@@@ru   explain_tracing_cache_missr  k  s2   $,ff9 9 9
 /5fSkk+'7JVV#	j!	<	<	<	<$'(8(@(B(BCC(!0X000111 Q**(
 8*0055KAq4$/HHH %ADXDD2a55D(DDEEE9a 	= > > >MM(466MA#x###$$$&%**,,'') ,G44)[BB	BBB9 9 9 9 9"3 9 9 9	 #I..//H))++MAzA 0h 0 0S__ 0 0YYs4,,--0 0 1 1 14 4 4 4 44E 4 4 4JJ<d  L  5a34444a 	4\!2!2 	4 	4yyT<0011	4 	4 5 5 5466M 6555y555*	 ANOOO7777777Jj&G&G&G&GHHHKNN3sK+@+@!A!ANNNDA VYYs3~~&&V V V W W W 466M :999I999+	 ),S\\):):R)?)?%G%%%RKA/+//000;;;;;;;Jz'W'W'W'WXXXL	3G\JJKKDAKTKKKLLL-1 # #)ffkjcDh(da 	"D 	"&t-- 	" 	"f 	" 	"V 	" 	"	" 	" # # # #466M <;;;K;;;+	 
7||a&&uzz 2; 2;:/992; 2; 2; (; (; < <kk kA7+77888@@@@K@@@JZ%J%J%J%JKKKJs25*g6677LAVVVVWWWj2JHH = =c3	99S		5555T%555555==&&d(;(;b"2XX=3=000022222B22222B!%s88SXXb"	
;d
;
;2
;
;r
;
;<<< SaOPPPa,---aQRRR466M !TUUU	-rw   )explainlu.WrappedFun-core.InputType | Sequence[core.AbstractValue]	attr_datar  r(   lu.TracingDebugInfo	out_pathsignored_inliner  ptuple[core.ClosedJaxpr, list[Any], list[core.AbstractValue], list[tuple[PyTreeDef, PyTreeDef, tuple[Any, str]]]]c           
        ~t          j        d| j        t           j                  5  |ot	          j        | |j                  }t          j        j	        rHt	          j
        t          j        | t          t          j        |                    |          \  }}}	g }
nt	          j        | ||          \  }}}	}
d d d            n# 1 swxY w Y   t          j        j	        s|
st#          || |                      }t          j        j	        rddlm}  ||           t+          d |	D                       r)t	          j        t	          j        |                    }|	}nt          j        ||	          }g }||||
fS )NzMFinished tracing + transforming {fun_name} for pjit in {elapsed_time:.9f} sec)rc   event)r(   r   )check_key_reuse_jaxprc              3  J   K   | ]}t          |t          j                  V  d S r   )r
  r   Tracerr   rh  s     ru   r   z%_create_pjit_jaxpr.<locals>.<genexpr>
  s.      44At{	#	#444444rw   )r   log_elapsed_timer   JAXPR_TRACE_EVENTr  debug_info_final
traced_forr   r  r-  trace_to_jaxpr_dynamic2r  annotater   r   r  trace_to_jaxpr_dynamicr*   r,   jax.experimental.key_reuse._corer  r+  close_jaxprconvert_constvars_jaxprClosedJaxpr)r   r  r  r(   r  r  pe_debugr   global_out_avalsr   r   r  closed_jaxprfinal_constss                 ru   rP  rP    s     U|8#=? ? ? 
- 
- Mb1#z7LMMH" -(*(B
+c488
9
9h)P )P )P%evmm797P
w88- 8- 8-4ev}
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
		$ =] =UJ		<<E! !FFFFFF%   44V44444 >""<U"C"CDDLLL#E622LL	|%5}	DDs   BCC
C
c                   t          | |          }t          |          st          |t          j                  r|ft          |          z  }	nt          d |            |d          }	t          ||          }
|
|
ft          |          z  }nt          d |            |
d          }t          j        j	        s8t          |	||d n|j        dd           t          |||d n|j        d           |	|fS )Nzpjit out_shardingsFr4  zpjit out_layoutszpjit outputsrC  zjit outputs)rO   rC   r
  r9   r+  r   r<  r   r  r-  rE  r)   rF  )r   r   r   r   r   r9  r(   r  orig_out_shardingsrs  r  r  s               ru   r  r    sL    &&;=QRR'(( #X%677,.Y?/hhjj*<   24FGG+#~I6-HHJJH H H 
		$ PI"
(?e5 5 5 5 $)"
(?P P P 
-	--rw   /core.InputType | tuple[core.AbstractValue, ...]r  c                    t                               | j        t          t                              }| j        | j        |k    sJ || j        | j        |f         S r   )_seen_attrs
setdefaultr  r   r  r  rO  r   )r   r  rK  s      ru   seen_attrs_getr  5  sS     
 
 D(9(9
:
:%		w 6 6 6 6	
G4	55rw   c                   ddl m} t          | |          }t          |          D ]T\  }}|D ]H\  }}}}	 |||          }
t	          |
          \  }}t          t          |          }||k    s|	|k    r nI|c S Ut          |          S r   )r   r   r  r  rN   rl  r$   r   )r   r  r   casesr  recordsr   r   r   r  r   r   r   avals_s                 ru   r  r  =  s     100000
g
&
&%e$$  ja%,  !T7EKT""c#C((ndH%t,,f	H		hhh 27 
Urw   c                    ddl m fdfd|D             }t          | |          }|t          |          k    r|                    |           d S |t          |          k     r||         |k    sJ d S )Nr   r   c                6    t           | |                    S r   )rR   )r   r   r   s     ru   rf  z_attr_update.<locals>.<lambda>O  s    [S$)?)?@@ rw   c                d    g | ],\  }}\  }}|||t          t           ||                    f-S rx   )rl  r$   )r   r  r   r   r   r   s        ru   r   z _attr_update.<locals>.<listcomp>P  sS     = = =*A{T 4C(:FF3<M<M$N$NO = = =rw   )r   r   r  r   rl   )r   r  r  r   r  r  r   r   s         @@ru   r  r  M  s    000000@@@@&= = = =.;= = ='
g
&
&%#e**__	LLs5zz>>eAh'111111rw   T)frozenc                  &    e Zd ZU ded<   d Zd ZdS )r  r
   r   c                *    t          | j                  S r   )hash	__class__r   s    ru   r   zIgnoreKey.__hash__\  s    rw   c                ,    t          |t                    S r   )r
  r  r   s     ru   r   zIgnoreKey.__eq__^  s    eY'''rw   N)r   r   r   r   r   r   rx   rw   ru   r  r  Y  s=         
(((     ( ( ( ( (rw   namesr  	what_avalr   rD  c                   |dgt          |           z  n|}t          || |          D ]*\  }}}t          |          r|rd| nd}	|j        }
	 t	          |d          r|                    |
           n"|                    t          |
                     n,# t          $ r}t          d| |	 d| d|           d }~ww xY w|                    t          |
                    }|J t          j	        |          \  }}t          |          D ]@\  }}|s9|
|         |z  dk    r*t          d| |	 d| d	| d
| d|
|          d|
 d          A,d S )Nr~    with pytree key path check_compatible_avalOne of z. is incompatible with its sharding annotation rz  r   z was given the sharding of z6, which implies that the global size of its dimension z should be divisible by z, but it is equal to z (full shape: ))r   r   rD   r  r  r  _to_xla_hlo_shardingr   r   get_num_ways_dim_shardedr  )r}  
flat_avalsr  r  rD  	new_namesrr   r"  rp   name_strr  r   hlo_shardingnum_ways_dim_shardedr   r  sizes                    ru   rE  rE  b  s     (-}rdS^^##%):y)<< 3 3mdAta   26>....BHJE
" 
+	,	, +	&&&&	s5zz*** " " "!I !x ! !! !! !" " "" ))#e**55L###*CLQQ!122 3 34" 3uQx$!';'; 29 2h 2 2 2 2=>2 2 *.2 2 EJ!H2 2 */	2 2 2 3 3 	33-3 3s   AB
C$B>>Cc                8   |dgt          |           z  n|}t          || |          D ]p\  }}}|t          |t                    r|rd| nd}|j        }		 |                    |	           E# t          $ r}
t          d| | d| d|
           d }
~
ww xY wd S )Nr~   r  r  z, is incompatible with its layout annotation rz  )r   r   r
  rI   r  r  r   )r|  r  r  r  r  rr   r  rp   r  r  r   s              ru   rF  rF    s    %*]rdS\\!!):w	:: 
" 
"mdAtyJq*--y26>....BHJE"e$$$$ " " "!I !x ! !! !! !" " ""
" 
"s   A..
B8BBr   c                   t          j        |          rdt          |          z  S g }t          | |||          D ]=\  }}}}t          j        t          |dd           |||          t          |dd          }
}	t          |          p#t          t          |dd           t                    }|F|
r.|r|	                    d            |	                    |	           |	                    d            |
rl|sj|	ht          j
        ||	          sSd}t          |t                    rd}t          d| d	|	 d
t          |                                           d|           |	                    |           ?t          |          S )Nr   layoutr  Tr9   r~   a   The layout given to `jax.jit` is `DeviceLocalLayout.AUTO` but the corresponding argument passed is a `jax.Array` with a concrete layout. Consider passing a `jax.ShapeDtypeStruct` instead of `jax.Array` as an argument to the jitted function  when using `DeviceLocalLayout.AUTO`.zWLayout passed to jit does not match the layout on the respective arg. Got pjit layout: z,
arg layout:  for arg shape: r  )r2   !check_device_backend_on_shardingsr   r\   _maybe_get_default_layoutr   rC   r
  r=   rl   is_user_xla_layout_equalrI   r   r$   rm   r  )r   jit_in_layoutsresolved_in_shardingsr   resolved_in_layoutsr   jit_in_lrsrr   
arg_layout	committedis_pmap_shardingr   s                ru   _resolve_in_layoutsr    s   
 
+,ABB )S((((!)
N18"= "= (+ (+c8R 	&wsHd'C'CX')4	1 	1\4(( J
 'r** Q"73
D#A#A<PP 	 ) 	1

$
$T
*
*
*
*

$
$Z
0
0
0
0""4((((
  *"*$/*EE %	h
++ 	6   ).6) ) *4) ) );3(?(?(I(I(K(K	) )
 ') ) * * 	*   ****	"	#	##rw   pjit_in_shardingsSequence[PjitSharding]pxla.Mesh | Nonecheck_device_assignmentc                   t          j        |          r|S g }| D ]d}t          |dd           }|t          |t                    r,t          |dd          r'|                    |t           j        j        d f           e|rt          j        t          j
        t          j        |          d t          j        |          D             d t          j        |          D                       ||j        rd nt          |j        j                             g }t#          | |          D ]U\  }	}
t%          |	d          r|	j        |	j        t          |	dd          fnt(          df\  }}t+          |
          rt+          |          r|                    |           u|rFt          |t                    r|                    t(                     |                    |           t-          j        |          r|                    t(                     t1          d          t          |	t2          j                  r8|
j        s1t9          j                    dk    rt=          d	|	j         d
|	           t+          |          s|
j         |j         k    rAt=          d|
j          d|j          dtC          |	          "                                           |rt          |t                    s|tG          j$        |
%                    |	j&                  |%                    |	j&                            s7t=          d|
 d| dtC          |	          "                                           |                    |
           WtO          |          S )Nr9   r  Tc              3  >   K   | ]}|t           j        j        d fV  d S r   )r2   ri   IN_SHARDINGro  s     ru   r   z(_resolve_in_shardings.<locals>.<genexpr>  sC       = = ".5 = = = = = =rw   c              3  >   K   | ]}|t           j        j        d fV  d S r   )r2   ri   OUT_SHARDINGr  s     ru   r   z(_resolve_in_shardings.<locals>.<genexpr>  sC       9 9 "/6 9 9 9 9 9 9rw   FzFHaving uncommitted Array sharded on multiple devices is not supported.r   a~  Passing non-trivial shardings for numpy inputs is not allowed. To fix this error, either specify a replicated sharding explicitly or use `jax.experimental.multihost_utils.host_local_array_to_global_array(...)` to convert your host local numpy inputs to a jax.Array which you can pass to pjit. If the numpy input is the same on each process, then you can use `jax.make_array_from_callback(...) to create a `jax.Array` which you can pass to pjit. Please see the jax.Array migration guide for more information https://jax.readthedocs.io/en/latest/jax_array_migration.html#handling-of-host-local-inputs-to-pjit-like-batch-etc. Got arg shape: z, arg value: zgMemory kinds passed to jax.jit does not match memory kind on the respective arg. Got pjit memory kind: z, arg memory kind: r  z^Sharding passed to pjit does not match the sharding on the respective arg. Got pjit sharding: z,
arg sharding: )(r2   r  r   r
  r=   rl   ri   rj    _get_and_check_device_assignmentitchainr   stable_uniquer  r  devicesflatr   r  r9   r?   rC   r   is_single_device_shardingr  npndarrayis_fully_replicatedr0  process_countr   r  memory_kindr$   rm   r   are_op_shardings_equalr  ndimr  )r   r   rq  r  r  committed_arg_shardingsr   arg_sr  r   	pjit_in_sr  s               ru   _resolve_in_shardingsr    sb    
+,=>>  T TaAz4((E } %&& q,%% T$$eT->-KT%RSSS  Z)
677= =():;;= = =9 9(779 9 9		: 	: "io"4	@Q@V;W;WZ Z Z D"344 ;. ;.nc9 #3
3328;8P wsL$'G'GHH)51 E9 i   5.			 L$$U++++ 	L|,, 0!((5555!((/////66 L!((5555% 'K L L L S"*
%
% =+=



q
 
 < "i< < 7:< <= = 	= E"" P  E$555B8A8MB B"'"3B B /s33==??B BC C C
  		P5,//		P3..sx88**38446 6		P
  O1:O O -2O O *<C)@)@)J)J)L)L	O O P P P
 ""9----	$	%	%%rw   c                    t          | ||||j        nd           }t          | |||j                  }t	          |||||||||	|
|||          }|S )N)lowering_platformslowering_parametersr  )r  r  r  r   _pjit_lower)r   r   r<  rq  r  r  r   r  rp   r   r   r  r  r  lowereds                  ru   r  r  8  s     '
L-$0$<l  $H H, #4\#(>3 3*\=*k<dK+-!# # #' 
.rw   c        
           d }d }t           j        }t          j        j        rqt          j        j        dk    r\| |vr6t          j        t          j        j        t          j        j                  || <   ||          }|	                                }|d|i}t          |
| |||||||||	d t          j                    |                              |          }|t           j        | <   |j        rBt          j        j        r1t#          j        |
|j        |j        | j        j        |j                   t          j        j        rd }t3          |                                d          r|                                j        }||                                }t;          d|fd|fd|fd|fd	|fd
t=          t>          j         |
          fd|f           	  |j!        |
 |fS # tD          $ ry}t          j#        j        st          j$        j        sJ tK          | j&                  dk    r tO          j(        |           |
 }tS          |           d}tE          |          d }~ww xY w)Nr   fdo_profile)r   r<  rq  r  r  r   r  rp   r   r   r  r  r  fingerprintzRunning pjit'd functionr<  rq  r  r  zabstract argsr   a  . Because jax_config.debug_nans.value and/or config.jax_debug_infs is set, the de-optimized function (i.e., the function as if the `jit` decorator were removed) was called in an attempt to get a more precise error message. However, the de-optimized function did not produce invalid values during its execution. This behavior can result from `jit` optimizations causing the invalid value to be produced. It may also arise from having nan/inf constants as outputs, like `jax.jit(lambda ...: jax.numpy.nan)(...)`. 

It may be possible to avoid the invalid value by removing the `jit` decorator, at the cost of losing optimizations. 

If you see this error, consider opening a bug report at https://github.com/google/jax.)*rH  rE  r   enable_pgler-  pgle_profiling_runsr   PGLEProfilerpgle_aggregation_percentileconsume_fdo_profiler  r1   LoweringParameterscompilerD  _auto_spmd_loweringenable_checksr2   %check_array_xla_sharding_layout_matchr/  _in_layoutsr   r(   r  distributed_debugr  runtime_executabler"  hexr^   rl  r.   abstractifyr%  FloatingPointErrorr  
debug_infsr   eqnsr   jaxpr_as_funr   )r   r<  rq  r  r  r   r  rp   r   r   r   compile_optionsr  pgle_profiler_dictr!  compiledr"  r   r   r   s                       ru   _pjit_call_impl_pythonr9  I  s   
 /-8P 5&"<"BQ"F"F&&&"*"7

$
*

,
2#4 #4 'u-M  3355K&4o
  
%l!jL#$K133!   GO  <D#1%8! 8f&:&@ 8.h$h&: 68 8 8 # 8 Kx**,,m<< >//11=kOO%%k4d;)<8*M:'4(+6*C,F,FG(+68 8 8"8&00	 " " ""=f&7&====
5:
"$
E
"
"D
)a !ff , , ,C S
!
!!/"s   G 
I"A4IIc
                6     t          j                     fdS )Nc                 B     t          j                               |  S r   )r   r5  )r   r   s    ru   rf  z#_get_jaxpr_as_fun.<locals>.<lambda>  s     1t(1148 rw   )rB  refr  s
   `         ru   _get_jaxpr_as_funr=    s#     +e

%	8	8	8	88rw   c        
        >   	
 
	 fd}t           	
  
        }d t                    D             }t          d d           }t          j                            ||g g |t          j        d t          |          	  	        
 S )Nc                     t          	
d
\  }}t                    }t          |t          |          |g j        j        d |	  	        }||t          |          fS )Nr  )r9  rM  r>  rQ   r6  r   r  )args_kwargs_r   r8  r  r=  r   r  r  r<  r   r   r   rp   r  rq  r   s         ru   call_impl_cache_missz-_pjit_call_impl.<locals>.call_impl_cache_miss  s    /	U#
l%Dk  Hh (..M&.**D(BdM+ +M ]$=m$L$LLLrw   c                    g | ]	\  }}||
S rx   rx   r  s      ru   r   z#_pjit_call_impl.<locals>.<listcomp>  s!    BBB41aBQBBBrw   c                <    t          j        |g| g          d         S rb  rc  re  s     ru   rf  z!_pjit_call_impl.<locals>.<lambda>  s    $/8*qc::1= rw   )	r=  r  rt  r  rX  r   r   rg  r\  )r   r<  rq  r  r  r   r  rp   r   r   r   rB  r  donated_argnumsr   s   ```````````    ru   _pjit_call_implrF    s   M M M M M M M M M M M M M M M \=*kND+v? ?! CB9^#<#<BBB/1M4/ /	
A#R_!==122	
4 
4 6:	
; ;rw   c                     t          | i |S r   )_pjit_lower_cached)r   r   s     ru   r  r    s    	T	,V	,	,,rw   r   core.ClosedJaxprr  pxla.MaybeLayoutr  rp   r  r  mlir.LoweringParametersr  profiler.PGLEProfiler | Nonec
                   |
|j         }d}nd }d}t          j        | ||||||t          |          |||j        rd nt          |j        j                  |
||          S )Nr   r   )r   devices_from_contextr  r  r  )r  r2   lower_sharding_computationr  r  r  r  r  )r   r<  rq  r  r  r   r  rp   r   r   r  r  r  r   r   s                  ru   rH  rH    s      %DHH DH		(Xt\=+u^44,$*,$$$t|7H2I2I+-!
# 
# 
# #rw   c                   t          |d         |d         |d                   \  }}}}t          ||||          }|d         rt          d |d         D                       rt          d |d         D                       rt          d	 |d
         D                       rrt          d |d         D                       rSt          j        j        r"t          j        |j        |j	        gR ddin>t          j        | |j        |j	        gR  nt          j        j        rt          j                    }g t          |          D ]}t          |          t          j        u rAfd|j        D             }	|                    t'          d |	D                                 }                    t          j        | ||                     t          j        t/          | j                  t/          | j                  t4          ||j        |          }
| j                            |
           nt=          d |j	        D                       rt?          j         |          \  }}t/          | j!        |          }g |d         R tD          ftG          |          z  z   }g |d
         R dtG          |          z  z   }g |d         R dtG          |          z  z   }t          |||||          }| $                    t4          g |R |          n| $                    t4          |          tK                    fd|D             tM          d           J S )Nr   rq  r  r   rq  r  r   c              3  4   K   | ]}t          |          V  d S r   rn  ro  s     ru   r   z$pjit_staging_rule.<locals>.<genexpr>  s*      	<	<.

	<	<	<	<	<	<rw   r<  c              3  4   K   | ]}t          |          V  d S r   rn  r  s     ru   r   z$pjit_staging_rule.<locals>.<genexpr>  s*      	=	=.

	=	=	=	=	=	=rw   c              3     K   | ]}|d u V  	d S r   rx   ro  s     ru   r   z$pjit_staging_rule.<locals>.<genexpr>  s&      	2	2!t)	2	2	2	2	2	2rw   r  c              3     K   | ]}|d u V  	d S r   rx   r  s     ru   r   z$pjit_staging_rule.<locals>.<genexpr>  s&      	3	3!t)	3	3	3	3	3	3rw   propagate_source_infoFc                    g | ]T}t          |          t          j        u r|j                 n)t          |          t          j        u r|j                 n|US rx   )r   r   InDBIdxr   OutDBIdx)r   r  r   out_tracerss     ru   r   z%pjit_staging_rule.<locals>.<listcomp>  se     ( ( ( !%Q4< 7 7ae'+Aww$-'?'?QU##( ( (rw   c              3  >   K   | ]}t          j        |          V  d S r   )r   get_referent)r   r  s     ru   r   z$pjit_staging_rule.<locals>.<genexpr>  s-      &K&Kt'8';';&K&K&K&K&K&Krw   r  c              3  J   K   | ]}t          |t          j                  V  d S r   r
  r   MutableArrayr  s     ru   r   z$pjit_staging_rule.<locals>.<genexpr>  s/      
B
B:a*++
B
B
B
B
B
Brw   r   r  r  )r   r<  r  r  c                j    g | ]/}t          |          t          u r|         nt                    0S rx   )r   r  r  )r   r  r   out_tracers_s     ru   r   z%pjit_staging_rule.<locals>.<listcomp>#  sG     " " " !GGsNNa\0B0B " " "rw   )'_pjit_forwardingr  r*  r   r  r-  r   
eval_jaxprr   r   r  inline_jaxpr_into_tracer   r  	_out_typer   r  r  updater  rl   DynamicJaxprTracernew_jaxpr_eqnrl  getvarmakevarr   r6  frameadd_eqnr+  r2   _move_mutable_constsinstantiate_constr?   r   default_process_primitiver  r  )r  r   r   r   in_fwdrq  r  source_inforr   r  eqnr   r<  r  r  
new_paramsrZ  rb  s    `              @@ru   pjit_staging_ruleru    s   .>Wovo.}0E/G /G+%e=') ) )& X (H		<	<VN%;	<	<	<<<(H		=	=VO%<	=	=	===(H 
	2	2VL1	2	2	222(H 
	3	3VM2	3	3	333	(H " 3 OEK A A A A:?A Akk .
el3-13 3 3kk" H"*,,KK%   J J	dt(	(	(( ( ( ( ( J( ( ( {{&K&KU&K&K&K!K!K{LL.udKHHIIII

	%,s5=+>>m[" "C 
K

B
BU\
B
B
BBB H-e44ME6(&11F-VN+--V0LLL)6,'))Gc&kk,AAJ1v./11Hs6{{4JJNfE!+NL L LJ11 $   *. .KK 11&$GGKk""," " " " " " " "+	lD	!	!	)	)	)	rw   c                    t          j        | j                  }d t          |||          D             }d |D             }t          j        | |          } d t          ||          D             }d t          ||          D             }| |||fS )Nc                @    g | ]\  }}}t          |          r||nd S r   rn  )r   fwdosols       ru   r   z$_pjit_forwarding.<locals>.<listcomp>,  s?     8 8 8;3B>"%%>"*CC$ 8 8 8rw   c                    g | ]}|d u S r   rx   r   r  s     ru   r   z$_pjit_forwarding.<locals>.<listcomp>.      	$	$	$!t)	$	$	$rw   c                    g | ]	\  }}||
S rx   rx   r   r  r[  s      ru   r   z$_pjit_forwarding.<locals>.<listcomp>0  !    ???AQ?1???rw   c                    g | ]	\  }}||
S rx   rx   r  s      ru   r   z$_pjit_forwarding.<locals>.<listcomp>1  r  rw   )r  _jaxpr_forwardingr   r   prune_closed_jaxpr_outputs)r   rq  r  rq  keeps        ru   rc  rc  *  s    1%+>>&8 86=+668 8 8&	$	$V	$	$	$$

't
4
4%??]D!9!9???-??[D!9!9???+	{	22rw   c                >    t           j        d          j        d          j        d                   \  }}}}d t           j        |          D             }t	           j        |g |R g |R           }                     ||          } fd|D             }||fS )Nr   rq  r  c                    g | ]	\  }}||
S r   rx   )r   vr  s      ru   r   z(pjit_forwarding_rule.<locals>.<listcomp>7  s    EEEtq!19999rw   rQ  )r   outvarsc                2    g | ]}|j         |         nd S r   )invars)r   r  rs  s     ru   r   z(pjit_forwarding_rule.<locals>.<listcomp>;  s'    GGG1q}cjmm$GGGrw   )rc  r   r   r  r  replace)	rs  r   rq  rq  r  new_outvarsrt  new_eqnfwd_varss	   `        ru   pjit_forwarding_ruler  4  s    .>	j3:o6
=8Q/S /S+%EEs3;77EEE+CJe;L];L;L /+1 1 1*KKz;K??'GGGGGGG(	7	rw   list[core.AbstractValue]c                   g }d t          | j        j                  D             d t          | j        j                  D             | j        j        D ]u}|j        }t          |          t          j        u r<fd|j        j        D             }|	                    t          |                    }|                    |           v|S )Nc                    i | ]\  }}||	S rx   rx   r   r  r  s      ru   
<dictcomp>z_out_type.<locals>.<dictcomp>D  s    ;;;TQAq;;;rw   c                P    i | ]#\  }}t          |          t          j        u  ||$S rx   )r   r   Var)r   r  r  s      ru   r  z_out_type.<locals>.<dictcomp>E  s:     % % %daQ48## ###rw   c                    g | ]@}|v rt          j        |                   n|v rt          j        |                   n|AS rx   )r   rX  rY  )r   r  in_idxout_idxs     ru   r   z_out_type.<locals>.<listcomp>J  sa     ( ( ( +,v++t|F1I&&&,-LLt}WQZ(((( ( (rw   r]  )r  r   r  r  rr   r   r   r  r  rg  r  rl   )r   r  r  rr   r  r  r  s        @@ru   rf  rf  B  s    
#;;Yu{'9::;;;&% %i(:;; % % %';  a6DDzzT&&&( ( ( ( (&,( ( (e [[uU||[,,dJJt	*rw   c          	     b    t          j        | t          |t          ||j                            S )N)
call_jaxpr)r   _check_callr   r  r   )ctx_factoryr   in_atomsr   s       ru   _pjit_typecheckr  R  s2    		+vxv%+>>>
@ 
@ @rw   c                    | j         | j        fS r   )r9  r6  )r   r   r   s      ru   _pjit_abstract_evalr  X  s    	%-	''rw   c	                   | j         }	| j         j        }
d }t          |
t          j                  r|
j        }n&t          |
t          j                  r|
j        j        }t          ||||t          j        ||j                  t          j        ||j                  |||f
}|	j                            |d           }|Fd |D             }d |D             }t!          j        |	|||| j        ||d|||          }||	j        |<   |S )Nc                4    g | ]}t          |          rd n|S r   rn  ro  s     ru   r   z3_pjit_cached_lower_jaxpr_to_fun.<locals>.<listcomp>n  s)    LLL!^A..5TTALLLrw   c                4    g | ]}t          |          rd n|S r   rn  r  s     ru   r   z3_pjit_cached_lower_jaxpr_to_fun.<locals>.<listcomp>o  s)    PPPQq 1 18qPPPrw   F)arg_shardingsresult_shardingsuse_sharding_annotationsr   arg_layoutsresult_layouts)module_contextaxis_contextr
  r   ShardingContextnum_devicesSPMDAxisContextr   r  r   r2   SemanticallyEqualShardingsr   r9  cached_primitive_loweringsrI  r1   lower_jaxpr_to_fun
name_stack)rP  rp   r   r6  r<  rq  r  r  r   mod_ctxaxis_ctxr  rL  r4   r  r  s                   ru   _pjit_cached_lower_jaxpr_to_funr  ]  s'    ',(+.899 %&KK(N:;; %-$K	ug{		(u~	F	F		(	H	H	[(	,#
 
	+	/	/T	:	:$	\LL|LLLMPP-PPP "ugs~#6F!&{	< < <D
 /3G&s+	+rw   c       
        n    t           j                                                  }t          t          j         j                  }t	          j                    gt          |          z  |z   }t	          j	        |          }t           ||t          |          |||||dnd	  	        } fd|D             }g  j        ||R }t          j        |t          j                            |j        j                  t	          j        |                    }t	          j         |           t	          j        |j        |          }t1          |t          |          g          \  }} j                            t	          j        t7          ||                              }                     |           |S )Nr   r   )r   c                D    g | ]}j                             |          S rx   )	tokens_inrI  )r   effrP  s     ru   r   z"_pjit_lowering.<locals>.<listcomp>  s)    999#s}  %%999rw   )r  r  r6  rl  r1   aval_to_ir_type	avals_out
token_typer   flatten_ir_typesr  r  dim_var_valuesfunc_dialectCallOpr3   FlatSymbolRefAttrrI  rp   r-  flatten_ir_valueswrap_compute_type_in_placeunflatten_ir_values_like_typesresultsr_   update_tokensTokenSetr   set_tokens_out)rP  rp   r   r<  rq  r  r  r   r  r   r   r   r6  output_typesflat_output_typesr4   r  call	out_nodestokens
tokens_outs   `                    ru   _pjit_loweringr  |  s    &&(())'T)3=99,/##$s7||3lB,+L99	(	4gZ%-6
< 
< 
<$
 :999999)	13	1		1D	1	1$		.155dioFF!3D99
; 
;$ !#t,,,1$,MM) S\\N;;&)}**4=Wf9M9M+N+NOO*Z   	rw   c                0    t          j        |          \  }}t          j        |||| |          \  }}||j        nd t	           fdt          |||j                  D                       }t	           fdt          |||j                  D                       }t          d |	D                       rt          d |
D                       st          d          t          j        |||||	|
|||||d
}t          j        |||          }||fS )N)	axis_namespmd_axis_name	main_typec              3  X   K   | ]$\  }}}|t          |||j                  n|V  %d S r   _pjit_batcher_for_shardingr  )r   axis_inr  rr   r   r  s       ru   r   z _pjit_batcher.<locals>.<genexpr>  sg       N N '1d 
	 !G^T49MMM"#N N N N N Nrw   c              3  X   K   | ]$\  }}}|t          |||j                  n|V  %d S r   r  )r   axis_outr  rr   r   r  s       ru   r   z _pjit_batcher.<locals>.<genexpr>  sg       R R (At 
	 !HndDINNN#$R R R R R Rrw   c              3     K   | ]}|d u V  	d S r   rx   r   r  s     ru   r   z _pjit_batcher.<locals>.<genexpr>  &      ,,Aa4i,,,,,,rw   c              3     K   | ]}|d u V  	d S r   rx   r  s     ru   r   z _pjit_batcher.<locals>.<genexpr>  &      --Aa4i------rw   z1Concrete layouts are not supported for vmap(jit).r  )r0   indirectify_ragged_axesbatch_jaxpr2r  r  r   r   r9  r*  r  r   r   *resolve_ragged_axes_against_inputs_outputs)r  	axis_sizer  r  vals_indims_inr   r<  rq  r  r  r   r  rp   r   r   segment_lens	new_jaxpraxes_outvals_outresolved_axes_outr   s   `                    @ru   _pjit_batcherr    s    #:7CC, -Y9#y: : :)X %DDD  N N N N N "'<9KLLN N N N N,  R R R R R  #8]I<OPPR R R R R-
 ,,,,,
,
, =
-----
-
-=
;= = = [
!	  ( Ix# #	$	$$rw   r"  $sharding.Sharding | UnspecifiedValuedimr  r  c           	        t          |           r| S |                     |          }|t          j        |          r| S |                                                                }t          |j                  }|                    |d           ||_        t          | j
        |t          | dd                     }t          j        |gd g| d           d         S t          | t                    r| j        }||j        rt%          d|           t'          ||          d         }	|	                    ||          }	t          j        ||	          S )Nr   _internal_device_list)_device_listr   aB  If you are using spmd_axis_name parameter of jax.vmap, please make sure to run your jitted function inside the mesh context manager. Only `jax.lax.with_sharding_constraint` with `jax.sharding.NamedSharding` as an input can be transformed with spmd_axis_name batching rules outside of an explicit mesh context manager scope)rC   r  r   is_op_sharding_replicatedto_protocloner  tile_assignment_dimensionsinsertr;   r   r   r2   $_get_out_sharding_from_orig_shardingr
  r:   r   r  r   rF   insert_axis_partitions_from_parsed_pspec)
r"  r  r  r   r  hlo_snew_optadnew_gsparsed_pspecs
             ru   r  r    sk    A H

 
 
&
&%/66 h^^##%%F
v0
1
1CJJsA(+F%	fQ 7>>@ @ @F 4fXvq$OOPQRR!]## Vd|tz|!
 ! !" " " -UD99!<L66sNKKL+D,???rw   c                   t          d |j        D                       rt          j        |          \  }}t	          t
          j        |          }g | |} g ||}g |R t          ft          |          z  z   }g |R dt          |          z  z   }g |R dt          |          z  z   }d t          ||j
                  D             }d |D             }t          j        ||d          \  }}d }t          ||          }t          ||          }t          j        g |  ||          R |g | ||          R g | ||          R g | ||          R g | ||          R |g | ||          R |	|
|d	
}t!          |t          |j        j                  g          \  }}t          |          t          |j        j                  k    sJ t'          |          |fd
t          ||j                  D             fS )Nc              3  J   K   | ]}t          |t          j                  V  d S r   r_  r  s     ru   r   z_pjit_jvp.<locals>.<genexpr>  s/      @@aAt(	)	)@@@@@@rw   r   r  c                j    g | ]0\  }}t          |t                    rt          j        |          n|1S rx   )r
  rL   r   zeros_like_aval)r   r  r   s      ru   r   z_pjit_jvp.<locals>.<listcomp>  sK     ? ? ?Q 0:![/I/IP(+++q ? ? ?rw   c                D    g | ]}t          |          t          j        uS rx   )r   r/   Zeror   r^  s     ru   r   z_pjit_jvp.<locals>.<listcomp>  s%    CCC!tAwwbg-CCCrw   Finstantiatec                6    d t          | |          D             S )Nc              3  $   K   | ]\  }}||V  d S r   rx   )r   nzr  s      ru   r   z3_pjit_jvp.<locals>._filter_zeros.<locals>.<genexpr>  s+      11%"ab1A111111rw   )r   )is_nz_lr  s     ru   _filter_zerosz _pjit_jvp.<locals>._filter_zeros  s    113w??1111rw   r  c                `    g | ]*\  }}|rt                    nt          j        |          +S rx   )r  r/   r  )r   r  rr   tangents_out_its      ru   r   z_pjit_jvp.<locals>.<listcomp>  sM     Q Q Q#2t 13EtO,,, Q Q Qrw   )r+  r   r2   rn  rl  r   zeros_like_jaxvalr?   r   r   r   r/   	jvp_jaxprr   r   r   r_   r   r  r  r9  )
primals_intangents_inr   r<  rq  r  r  r   r  rp   r   r   mut_primalsmut_tangentsis_nz_tangents_in	jaxpr_jvpis_nz_tangents_outr
  _filter_zeros_in_filter_zeros_outoutputsprimals_outtangents_outr  s                          @ru   	_pjit_jvpr    s    	@@5<@@@@@ F2599E;w0+>>L,:,,J/K/,/K#\##{ns;7G7G&GGL:'C,<,<"<<J'~''(S5E5E*EEN? ? en==? ? ?+ DC{CCC"$,E#3 #3 #3)2 2 2],=>>m-?@@K $$[11 C\C$4$4\$B$BCCGmG&7&7&F&FGG=:= 0 0 < <==AKA"3"3K"@"@AAI~I(8(8(H(HII  ' )3u{7J3K3K2LMM+|	[		S!455	5	5	5	5&&/	 Q Q Q Q'*+=u'O'OQ Q Q 
Q Qrw   known_jaxprrq  tuple[int | None, ...]c                    | j                             d t          | j         j        |          D                       }|                     |          S )Nc                    g | ]	\  }}||
S r   rx   )r   r  r  s      ru   r   z$_known_jaxpr_fwd.<locals>.<listcomp>  s(       TQ) ))rw   )r  rJ  )r   r  r   r  )r  rq  updated_jaxprs      ru   _known_jaxpr_fwdr!    se     #++ S!2!:FCC    ,  - 
		=		1	11rw   c       
        d   0 d |D             }t          d |D                       }t          d |D                       }t          j        ||d          \  }}}}t          |          }t          d |D                       }t          |          }t          f|z  }d|z  }d } |||          |z   } |||          |z   }t          |j                  |z
  }t          j        |j                  }t          ||g          \  }}d	 t           |||           |||          |          D             |z   }~~d
 |D             }t          j
        ||          } |||          } |||          }t          ||g          \  }} t          |          }~~~ t          |j        j        |g          \  }!}"d t          |!          D             0d g|z  0fd|"D             z   }#d |#D             }t          j
        ||          } |||          } |||          }~t          | |||          | |||          || |||          ||	|

  
        }$t          |$d                   t          |$d         j                  k    sJ t          |$d                   t          |$d         j                  k    sJ d |D             }%t          j        |%i |$}&t#          |#|&|&          }&t#          ||%|&          }&t          |&t          |&          |z
  g          \  }'}(t%           j        |(          })t          j        |dg|z  dgt          |          z  z             }t          | |||          |z    |||           |||          |z    |||          | |||          d|z  z   ||	|

  
        }*d |D             }+|j        }, fd|,D             }-t          j        g |+|)R |-t          |*|j        t/          j                              }.|-D ]	}/|.|/_        
t5          ||'|-          S )Nc                    g | ]	}|j         
S rx   )pvalr  s     ru   r   z&_pjit_partial_eval.<locals>.<listcomp>#  s    )))af)))rw   c              3  >   K   | ]}|                                 V  d S r   )is_knownr   pvs     ru   r   z%_pjit_partial_eval.<locals>.<genexpr>%  s*      55bBKKMM555555rw   c              3     K   | ]}| V  d S r   rx   )r   r[  s     ru   r   z%_pjit_partial_eval.<locals>.<genexpr>&  s$      //!e//////rw   Fr  c              3     K   | ]}| V  d S r   rx   )r   uks     ru   r   z%_pjit_partial_eval.<locals>.<genexpr>*  s$      33V333333rw   r   c                P    t          d t          | |          D                       S )Nc              3  $   K   | ]\  }}||V  d S r   rx   r   r  r  s      ru   r   z9_pjit_partial_eval.<locals>.keep_where.<locals>.<genexpr>0  s+      >>wq$>>>>>>>rw   r  r   )r  should_keeps     ru   
keep_wherez&_pjit_partial_eval.<locals>.keep_where/  s)    >>#a"5"5>>>>>>rw   c                @    g | ]\  }}}t          |          r||nd S r   rn  )r   ry  rz  rx  s       ru   r   z&_pjit_partial_eval.<locals>.<listcomp>:  sC       
"b# B8BJccD  rw   c                    g | ]}|d u S r   rx   r|  s     ru   r   z&_pjit_partial_eval.<locals>.<listcomp>B  r}  rw   c                4    i | ]\  }}t          |          |S rx   r   r  s      ru   r  z&_pjit_partial_eval.<locals>.<dictcomp>M  s$    666$!QRUUA666rw   c                T    g | ]$}                     t          |                    %S rx   )rI  r   )r   r  idx_maps     ru   r   z&_pjit_partial_eval.<locals>.<listcomp>N  s+    'M'M'MqBqEE(:(:'M'M'Mrw   c                    g | ]}|d u S r   rx   r|  s     ru   r   z&_pjit_partial_eval.<locals>.<listcomp>P  s    	%	%	%!t)	%	%	%rw   r  rq  r   r  c                ^    g | ]*}|                                 |                                +S rx   )r&  	get_knownr'  s     ru   r   z&_pjit_partial_eval.<locals>.<listcomp>a  s-    EEERr{{}}E",,..EEErw   Tr  c                D    g | ]}|j                                         |S rx   )r$  r&  r  s     ru   r   z&_pjit_partial_eval.<locals>.<listcomp>  s)    GGGaQV__5F5FGGGGrw   c                v    g | ]5}t          j        t           j                            |          d           6S r   )r  JaxprTracer
PartialValunknown)r   rr   r  s     ru   r   z&_pjit_partial_eval.<locals>.<listcomp>  sG       
 	nUBM11$77>>  rw   )r  r  partial_eval_jaxpr_nounitsr   r?   r9  r  r   r_   r   r  r   r  r  r  r   r   rb   rl  new_instantiated_constmove_binders_to_backnew_eqn_reciper6  r   r  recipera   )1r  r   r<  rq  r  r  r   r  rp   r   r   
in_tracersin_pvals	known_insunknown_insr  unknown_jaxprunknown_outs	res_avals
known_outsnum_residualsres_shardingsres_layoutsr1  known_out_shardingsknown_out_layoutsnum_out_primalsrq  in_fwd_primal
in_fwd_resr  kept_primalskept_resout_varsres_varsout_fwdknown_paramsknown_inputsall_known_outsknown_out_valsresidual_valsresidual_tracersunknown_paramsunknown_tracers_inunknown_out_avalsunknown_tracers_outrs  r^  r6  s1   `                                               @ru   _pjit_partial_evalrc    s`    *)j)))(55H55555)//Y/////+#E;EJJJ 6+}lI|$$,33l33333*i..-.=0--'+? ? ? #
=*==M jj99KG -..>/1+2CDD&(/1BCC- 
*]J
/
/
*[*
-
-}> >  
 & Z	$	$V	$	$	$$-k4@@+"
#6== j!2D99%d_,=>>,%%/
L( "+"3";o=NOO(H66)H"5"5666'F_$'M'M'M'MH'M'M'MM'	%	%W	%	%	%$-k4@@+"
#6== j!2D99
jjy&I&I'J	22#,Z	::[9 9 9, 
\/*	+	+s<3H3R/S/S	S	S	S	S	\-(	)	)Sg1F1P-Q-Q	Q	Q	Q	Q FE8EEE,;===.WnnEE.V\>BB. #n"5"5"E!FGG  .-5}EE )dVm+ugK8H8H.HHJ J- J|[99MIJ}l;;*Z55C*[,77 j==./  . HG:GGG#-   #   	B.B1ABB- ('/*244	6 	6# ..a3	\>3F	G	GGrw   unks_inSequence[bool]inst_inkept_outs_knownkept_outs_stagednum_res_out
num_res_inparams_knownparams_stagedtuple[dict, dict]c           
     (   t          j        | |d                   \  }}	t          j        | |d                   \  }
}	t          j        ||d                   \  }	}t          j        | |d                   \  }}	t          j        ||d                   \  }	}t          |t          |
          g |t          g|z  R t          |          g |d g|z  R t          |                    }t          |d                   t          |d         j                  k    sJ t          |d                   t          |d         j                  k    sJ t          |d                   t          |d         j                  k    sJ t          |d                   t          |d         j                  k    sJ t          j        ||d                   \  }	}dg|z  |z   }t          j        ||d                   \  }	}g t          g|z  |}t          j        ||d                   \  }	}t          j        ||d                   \  }	}g d g|z  |}t          j        ||d                   \  }	}t          |t          |          t          |          t          |          t          |          t          |                    }t          |d                   t          |d         j                  k    sJ t          |d                   t          |d         j                  k    sJ t          |d                   t          |d         j                  k    sJ t          |d                   t          |d         j                  k    sJ ||fS )	Nr  r<  rq  r  r  )r<  rq  r  r  r  r   F)r  partition_listr  r  r?   r   r   r9  )rd  rf  rg  rh  ri  rj  rk  rl  donated_invars_knownr   in_shardings_knownout_shardings_knownin_layouts_knownout_layouts_knownnew_params_knowndonated_invars_stagedin_shardings_stagedout_shardings_stagedin_layouts_stagedout_layouts_stagednew_params_stageds                        ru   (_pjit_partial_eval_custom_params_updaterr|    s    -g|DT7UVV+G\.5QRRa,_l?>[\\!)'<3MNNA*?L<WXX!,',-?'@'@)G*= )G+6-+*E)G )G%*+;%<%<&Q(9&QTF[<P&Q&Q)./C)D)DF F F 
n-	.	.#l76K6T2U2U	U	U	U	U	o.	/	/3|G7L7V3W3W	W	W	W	W	l+	,	,L4I4R0S0S	S	S	S	S	m,	-	-\'5J5T1U1U	U	U	U	U  .wFV8WXX! 'J.1FF,WmN6STT!K;-*4K7JK-.>o@^__!*7M,4OPP!@+@.?@+,<mM>Z[[!=(-.A(B(B)./C)D)D&+,=&>&>',-?'@'@*/0E*F*FH H H 
~.	/	/3}W7M7V3W3W	W	W	W	W	/	0	0Cg8N8X4Y4Y	Y	Y	Y	Y	|,	-	-]75K5T1U1U	U	U	U	U	}-	.	.#mG6L6V2W2W	W	W	W	W	,	,,rw   c                h    t          j        | |          \  }}}}t          j        ||          }||fS r   )r  r  r   r  )r   r   transpose_jaxprr   r   r   s         ru   _pjit_transpose_tracer    s?    .0.G	8/ /+/1fm$_f==/	-	''rw   c       
           d }t          j        t          j                  }t          j        ||d          }t          || f          \  }}t          ||          \  }}g  |t          j        ||           |t          j        ||           R }g  |t          j        ||           |t          j        ||           R }t          d |D                       }t          ||          \  }} |            } |t          j        |t          |t                      g|j        z                      } |t          j        |t          |t                      g|j        z                      }|rut          |          }g ||}t          ft!          |          z  |z   }t          ft!          |          z  |z   }dt!          |          z  |z   }dt!          |          z  |z   }t#          j        |||||||dt!          |          z  ||	|
d
}|r1t'          |t!          |          g          \  }}t)          ||           t          ||          S )Nc                V     t           fdt          ||          D                       S )Nc              3  D   K   | ]\  }}t          |          u|V  d S r   )r   )r   r  mzr|  s      ru   r   z6_pjit_transpose.<locals>.prune_type.<locals>.<genexpr>  s7      KKuq"R8J8J8J8J8J8JKKrw   r/  )r|  xsmaybe_zeross   `  ru   
prune_typez#_pjit_transpose.<locals>.prune_type  s0    KKKKB 4 4KKKKKKrw   Fc              3  b   K   | ]*}t          j        t          j        |                    V  +d S r   )r   raise_to_shapedget_aval)r   cts     ru   r   z"_pjit_transpose.<locals>.<genexpr>  sK       ? ?"$ #24=3D3DEE ? ? ? ? ? ?rw   r   r  r  )r  r  r/   closed_backward_passhashable_partialrN   r"   UndefinedPrimalr  r  r  rO   objectr   r   r?   r   r   r   r_   r   )cts_inr   r<  rq  r  r  r   r  rp   r   r   r  r  bodyprimals_and_nz_cts_in
in_treedefcts_out_treedef_thunktranspose_in_shardingstranspose_in_layoutsglobal_cts_in_avalsr~  r   cts_out_treedeftranspose_out_shardingstranspose_out_layoutsr   
nz_cts_outr   s                               ru   _pjit_transposer    s   L L L 
b-	.	.$		T5%	0	0$&2J3G&H&H# 4T: F F$Z"L*==Z// Z"J
;;Zf--   ? ?(=? ? ? ? ? $9
$! $! /=))++/&Jg_vxxj?3M&MNNP P %*g_vxxj?3M&MNNP P
  Q}--KBkB,AB)^c-.@.@@CYY*ns=/A/AAD[["S%7%77:NN#c-&8&88;PP{)+%'$9 : ::  *  -)*s;7G7G6HIIL*|,,,		4	44rw   used_outputsr  #tuple[core.ClosedJaxpr, list[bool]]c                t    t          j        | j        |          \  }}t          j        || j                  |fS r   )r  	dce_jaxprr   r   r  r   )r   r  r  used_inputss       ru   _dce_jaxpr_pjitr  	  s6     <\BB)[		)U\	2	2K	??rw   
list[bool]rs  core.JaxprEqn'tuple[list[bool], core.JaxprEqn | None]c                b   t          |j        d         t          |                     \  }}d }|j        }t          || ||d         |           ||d         |            ||d         |           ||d         |            ||d         |                    }t	          |          st	          |           s|j        s|d fS t          j        d	 t          |j	        |          D             d
 t          |j
        |           D             |j        ||j        |j        |j                  }||fS )Nr   c                P    t          d t          | |          D                       S )Nc              3  $   K   | ]\  }}||V  d S r   rx   r.  s      ru   r   z:dce_jaxpr_pjit_rule.<locals>.keep_where.<locals>.<genexpr>	  s+      99wq$D9999999rw   r/  )r  keepss     ru   r1  z'dce_jaxpr_pjit_rule.<locals>.keep_where	  s'    99#b%..999999rw   r<  rq  r  r  r  )r   r<  rq  r  r  r  c                    g | ]	\  }}||
S rx   rx   r   r  useds      ru   r   z'dce_jaxpr_pjit_rule.<locals>.<listcomp>)	  s!    ???wq$$????rw   c                    g | ]	\  }}||
S rx   rx   r  s      ru   r   z'dce_jaxpr_pjit_rule.<locals>.<listcomp>*	  s!    AAAwq$DAAAArw   )r  r   r  r  r+  r6  r   ri  r   r  r  	primitiverr  rP  )r  rs  
dced_jaxprr  r1  
eqn_paramsrt  r  s           ru   dce_jaxpr_pjit_ruler  	  sf   +	j5..0 0*k: : : z*:j8+FFJz/:LIIJ|4kBB*Z6EEZ
+; <kJJ  * 
[		  #l"3"3  J<N   ??#cj+66???AA#ck<88AAAz:#5sQ QG rw   c           	        t          | j                  }|d= t          |d                   s|d= t          d |d         D                       r|d= t          d |d         D                       r|d= t          d |d         D                       r|d= t          d	 |d
         D                       r|d
= |d         s|d= |d         |d         j        j        r|d= |d= t          j        | ||dgt          |          z             S )Nr   r  c              3  4   K   | ]}t          |          V  d S r   rn  r  s     ru   r   z _pjit_pp_rule.<locals>.<genexpr>6	  s*      ;;q		;;;;;;rw   r<  c              3  4   K   | ]}t          |          V  d S r   rn  r  s     ru   r   z _pjit_pp_rule.<locals>.<genexpr>8	  s*      <<q		<<<<<<rw   rq  c              3     K   | ]}|d u V  	d S r   rx   r  s     ru   r   z _pjit_pp_rule.<locals>.<genexpr>:	  s&      11qd111111rw   r  c              3     K   | ]}|d u V  	d S r   rx   r  s     ru   r   z _pjit_pp_rule.<locals>.<genexpr><	  s&      22qd222222rw   r  r   r   rp   )r   )	r  r   r+  r*  r  r  r   _pp_eqnsorted)rs  contextsettingsr   s       ru   _pjit_pp_ruler  1	  sR   
&X	V$%	&	& ! ;;F>$:;;;;; ~<<F?$;<<<<<  11F<011111 |22F=122222 }		 }^$^*0 %~ Vn	c7HfXv5N	O	O	OOrw   c          	        t          t          t          |                    r"t          t          t          |                    st          t          d |D                       rt          d |D                       st          |j        |j        }	}t          |j                  }
t          j	        ||	          \  }}t          j        ||          }t                      ft          |j                  z  }t                      ft          |j                  z  }dt          |j                  z  }dt          |j                  z  }t          j        ||||||d|}t!          ||
g          \  }}t#          |          t%          fd| D                       }t'                      }t)          |          |u sJ ||fS )Nc              3     K   | ]}|d u V  	d S r   rx   r  s     ru   r   z-_pjit_state_discharge_rule.<locals>.<genexpr>R	  r  rw   c              3     K   | ]}|d u V  	d S r   rx   r  s     ru   r   z-_pjit_state_discharge_rule.<locals>.<genexpr>S	  r  rw   r   )r   r<  rq  r  r  c              3  d   K   | ]*}t          |t                    rt                    nd V  +d S r   )r
  rL   r  )r   rr   ref_vals_iters     ru   r   z-_pjit_state_discharge_rule.<locals>.<genexpr>d	  sV       5 5#' -7t[,I,I T-(((5 5 5 5 5 5rw   )r*  rl  rC   r  r   r   r   r  state_dischargedischarge_stater   r  r@   r  r   r   r_   r  r  r  r  )r   r9  r   r<  rq  r  r  r   r   r   num_outsdischarged_jaxprdischarged_constsdischarged_closed_jaxprnew_in_shardingsnew_out_shardingsnew_in_layoutsnew_out_layoutsout_and_ref_valsout_valsref_vals
new_invalssentinelr  s                          @ru   _pjit_state_discharge_ruler  K	  s    c.,//
0
0 
c.-00
1
1

,,,,,
,
, 
-----
-
-
+u|%((7(Gv(V(V%% ,-=?PQQ&((*S1A1H-I-II'))+c2B2J.K.KKS!1!8999.c"2":;;;/[*9I%.!- - &,- - ""2XJ??(Hx..- 5 5 5 5+35 5 5 5 5*XX(	mX	&	&(	2	2	2	2	X	rw   c                   t          |           \  }}t          |          \  }}t          |dd          }~t          t	          d||                    }~t          t	          d||                    }~t
          j        j        j        fd|D             }d |D             }	~t          ||dd	d
           t          ||dd	           fdt          ||||	          D             }
t          ||
          S )a  Mechanism to constrain the sharding of an Array inside a jitted computation

  This is a strict constraint for the GSPMD partitioner and not a hint. For examples
  of how to use this function, see `Distributed arrays and automatic parallelization`_.

  Args:
    x: PyTree of jax.Arrays which will have their shardings constrained
    shardings: PyTree of sharding specifications. Valid values are the same as for
      the ``in_shardings`` argument of :func:`jax.experimental.pjit`.
  Returns:
    x_with_shardings: PyTree of jax.Arrays with specified sharding constraints.

  .. _Distributed arrays and automatic parallelization: https://jax.readthedocs.io/en/latest/notebooks/Distributed_arrays_and_automatic_parallelization.html
  r}  T)allow_unconstrained_dimsz"with_sharding_constraint shardingsz with_sharding_constraint layoutsc                4    g | ]}t          |d d          S )r}  with_sharding_constraintr  )r   r   r   s     ru   r   z,with_sharding_constraint.<locals>.<listcomp>	  s>     2 2 2 /tQ/IK K 2 2 2rw   c                Z    g | ](}t          |t                    rt          |          ni )S rx   )r
  r:   get_unconstrained_dimsr  s     ru   r   z,with_sharding_constraint.<locals>.<listcomp>	  sI     1 1 1 &a77@.q111=?1 1 1rw   Nz"with_sharding_constraint argumentsrC  c           	     X    g | ]&\  }}}}t                               ||||           'S )r9   r  r   unconstrained_dimssharding_constraint_pr   )r   xfr"  r  udr   s        ru   r   z,with_sharding_constraint.<locals>.<listcomp>	  sT     
7 
7 
7 b!Q  
$
$R!A2>8: % < < 
7 
7 
7rw   )rN   r~  rE   r  r    r  r  r  r  rE  rF  r   rO   )r  r}  x_flatr   r|  user_shardingsuser_shardings_flatuser_layouts_flatshardings_flatr  r   r   r   s              @@ru   r  r  n	  s    a,&$1)<<'9)t= = =.7~NNP P5tWEEG G*.,		#$2 2 2 202 2 2.
1 1!/1 1 1 fd$H " " " " ""3VT"FH H H
7 
7 
7 
7 "&.:K"46 6
7 
7 
7$
 
d	#	##rw   c                    | S r   rx   rw  s    ru   _identity_fnr  	  s    AXrw   c                   |Vt          | d          r"| j                            || j                  r| S  t	          j        t          |          |           S t          | d          r2| j        j        |k    r"| j                            || j                  r| S  t	          j        t          t          ||                    |           S )Nr9   )rq  r  )
r  r9   is_equivalent_tor  r   r   r  r  ry  rG   )r  r9   r  r   r  s        ru   _sharding_constraint_implr  	  s    ^q* !*"="=h"O"O h837<x888;;;8 !=!G!G	
##Haf55 "HhH37<vfh/G/GHHHKKKrw   sharding_constraintc                    | S r   rx   )r  r   s     ru   rf  rf  	  s    q rw   c                (    t          j        | fi |fS r   r  )r  r   r   s      ru   rf  rf  	  s    '<'A"'O'O'O'O&Q rw   c                  | j         \  }| j        \  }| j        j        }t	          |t
          j                  r"|j        rt          j	        |||j
                  }t          j        j        r |j        |j
                  }n' |j        |j
                                                  }t          j        | ||||          }	|t          j        | |	|||          }	|	gS )N)unspecified_dims)avals_inr  r  r  r
  r   r  manual_axesr1   add_manual_axesr  r   use_shardy_partitionerr-  _to_sdy_shardingr  r  wrap_with_sharding_opwrap_with_layout_op)
rP  x_noder9   r  r   r  rr   out_avalr  r  s
             ru   !_sharding_constraint_hlo_loweringr  	  s    
,%$m)(,(>9:: CC#Hh	BBH"( C(x(33HH,x,TY77@@BBH"	68X8J	L 	L 	L#

"3Xvt
D
DC
,rw   c
                   | Rt          |t                    r=d |j        D             }
t          |           |
z  rt	          d|  d|j                   |\  }|\  fd|	D             }	| |	                               t          || |j        |j                  }|	rzt          |t                    ret          |j                  d g|j        t          |j                  z
  z  z   }|	D ]}t          j        ||<   t          |j        t          |           }|t          d|           t                              |||||	          }|fS )Nc                L    h | ]!}t          |t                    r|n|fD ]}|"S rx   )r
  r  )r   nsr   s      ru   	<setcomp>z/_sharding_constraint_batcher.<locals>.<setcomp>	  sQ     ? ? ?"&r511<bbu? ?  ? ? ? ?rw   zvmap spmd_axis_name z> cannot appear in with_sharding_constraint spec, but got spec c                "    h | ]}||k    z   S rx   rx   )r   r  r  s     ru   r  z/_sharding_constraint_batcher.<locals>.<setcomp>	  s!    DDD2a2gDDDrw   zPConcrete layout is not supported for vmap(with_sharding_constraint). Got layout r  )r
  r:   specrc  r   r  r  r  r  r  r   r-   UNCONSTRAINEDr   r  r  r   )r  r  r  r  r  r  r9   r  r   r  r  r  vmapped_shardingnew_specuyr  s                   @ru   _sharding_constraint_batcherr  	  s    Jx$G$G? ? ? ? ?D
>T! + *n * *"-* * + + + "!"!DDDD1CDDD1/><#=qvG G 9J'7GG 9$)**dVqvDTDY@Z@Z7Z-[[H 0 0!/hqkk$}h79 9 
		 	        + ! - -! 
A+rw   r9   r:   c                P    | j         J d t          | j                   D             S )Nc                    h | ]	\  }}||
S r   rx   )r   r  axess      ru   r  z)get_unconstrained_dims.<locals>.<setcomp>
  s(     
 
 
4\ \\rw   )_parsed_pspecr  r8   s    ru   r  r   
  s<    			+	+	+
 
9X%;<< 
 
 
 rw   ppspecSequence[ParsedPartitionSpec]Sequence[PartitionSpec]c                    d | D             S )Nc                ,    g | ]}t          |          S rx   )rB   )r   r   s     ru   r   z'_get_partition_spec.<locals>.<listcomp>
  s!    	.	.	.!
1

	.	.	.rw   rx   )r  s    ru   _get_partition_specr  
  s    	.	.v	.	.	..rw   7tuple[Sequence[xc.OpSharding], Sequence[xc.OpSharding]]c                r    g }|                                  }||}g }|                                 }||}||fS r   )get_parameter_shardingsget_output_shardings)r  in_op_shardingsparameter_shardings_from_xlaout_op_shardingsoutput_shardings_from_xlas        ru   get_op_sharding_from_executabler  
  sT    )+/!+!C!C!E!E!-2O*,(==??*0	*	**rw   Ctuple[Sequence[ParsedPartitionSpec], Sequence[ParsedPartitionSpec]]c                    t          |           \  }}g }|D ]%}|                    t          ||                     &g }|D ]%}|                    t          ||                     &||fS r   )r  r   rF   )r  r   input_op_shardingsoutput_op_sharding	in_ppspecr"  
out_ppspecs          ru   _get_ppspec_from_executabler  
  s     ,K, ,(( *,) 9 9a.q$778888*,* : :a/4889999	J	rw   r   	pxla.Mesh;tuple[tuple[PartitionSpec, ...], tuple[PartitionSpec, ...]]c                    t          | |          \  }}t          |          }t          |          }t          |          t          |          fS r   )r  r  r  )r  r   r  r  out_partition_specin_partition_specs         ru   get_pspec_from_executabler!  *
  sQ     6j$GG)Z*:66))44	 	!	!5);#<#<	<<rw   )r  r  )r   r   r   r   )r  r   )r   r   r<  r
   rq  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   r<  r
   rq  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   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   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  r   r  r   r  r   r  r   r   r   r   r   r   r   r   r   r   r  r   )r  r   rJ  r   rK  r  rL  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  r  r  )r  r  r  r   rD  r   )r  r  r  r   )T)
r   r  rq  r  r  r  r  r   r  r  )r   rI  r  rJ  r  rJ  rp   r   r   r   r   r   r  r  r  rK  r  rL  )r   rI  r  r  )r"  r  r  r  r  r  r  r  )r  rI  rq  r  r  rI  )rd  re  rf  re  rg  re  rh  re  ri  r  rj  r  rk  r  rl  r  r  rm  )r   rI  r  r  r  r  )r  r  rs  r  r  r  )r9   r:   )r  r  r  r  )r  r  )r  r  )r   r  r  r  (  
__future__r   collectionsr   collections.abcr   r   r   dataclasses	functoolsr   inspect	itertoolsr
  rV  operatorrm  rB  typingr	   r
   r   r   	threadingr  numpyr  jax._srcr   r   r   r   r   r   r   r   r  r   r  r   r   r   r   r   r   r   r   r   r0  jax._src.api_utilr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   jax._src.interpretersr,   r  jax._src.partition_specr-   r.   r/   r0   r1   r2   jax._src.lib.mlirr3   jax._src.lib.mlir.dialectsr4   r  jax._src.libr5   r6   r  r7   r9   jax._src.sharding_implsr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   jax._src.layoutrG   rH   rI   jax._src.staterJ   r  rK   rL   jax._src.traceback_utilrM   jax._src.tree_utilrN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r  jax._src.utilrZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   rl  
unsafe_mapr   
unsafe_zipregister_exclusion__file__PjitShardingPjitShardingMinusUnspecifiedMeshShardingMeshShardingMinusUnspecified	getLoggerr   rT  r|   r   r   r   r   r   r  r>  localr@  rH  rK  rM  rU  rX  rY  rZ  r\  rk  rt  r~  r  r  r  r  r  r  rS  r   r  r  Wrappedr   r   r)  r  r  r<  r7  rK  r  rc  rI  r   r  rP  r  r  r  r   r  AbstractValue
AttrRecordrC  r  r  r  r  	dataclassr  rE  rF  AxisPrimitiver   multiple_resultsr  r  r  r9  r=  rF  def_implr  rH  ru  custom_staging_rulesrc  r  forwarding_rulesrf  r  custom_typechecksr  def_effectful_abstract_evalr  r  register_loweringr  spmd_axis_primitive_batchersaxis_primitive_batchersr  r  primitive_jvpsr!  rc  custom_partial_eval_rulesr|  $closed_call_partial_eval_custom_rulepartial_eval_jaxpr_custom_rulesr  r  reducing_transposesr  r  	dce_rulesr  pp_eqn_rulesr  register_discharge_ruler  r  r  	Primitiver  def_abstract_eval
deflinear2r  r  r  r  r  r  r!  rx   rw   ru   <module>r]     s&   # " " " " " " # # # # # # 8 8 8 8 8 8 8 8 8 8                      / / / / / / / / / / / /                                                    & & & & & & % % % % % % ! ! ! ! ! !       # # # # # # % % % % % %       # # # # # #             % % % % % %                             
 5 4 4 4 4 4 1 1 1 1 1 1 % % % % % % $ $ $ $ $ $ * * * * * * & & & & & & & & & & & &             ; ; ; ; ; ;             ) ) ) ) ) ) . . . . . .      O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O
 B A A A A A A A A A O O O O O O O O O O 0 0 0 0 0 0. . . . . . . . . . . . . . . . . . . . . . . . . . . .5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
 CZCZ ! !( + + +]$4d:;$]D%89 ]$4d:;$]D%89 		8	$	$    6$ $ $ $ $z $ $ $N)S )S )SX       3 3 3- - - -`? ? ? ? ?IO ? ? ?
 %B$A$C$C !J J J  
% % % '++T+::     8B B B BN N N*S+ S+ S+ S+l' ' ' 'T* * * * 	D 	D 	D 	D 	D 	D 	D 	DD. D. D. D.N        ! ! ! !&? &? &? &?R: : : :<: : : :        15261526#"&y3 y3 y3 y3 y3x3 3 3S S S<
 
 
4, 4, 4,l+ + + + + + + + T666 ,  , 76 ,D cee	    | | | || 	5666&E &E &E 76&ER T666. . 76.< 63	40B+CCD
'g'))6 6 6 6    	2 	2 	2 d###( ( ( ( ( ( ( $#( 3  3  3  3F" " " "$ 
	F	#	# 2$ 2$ 2$r %)	e& e& e& e& e&P  "Q" Q" Q"h 
9 
9 
9; ; ;:       - - - # # # #D4 4 4j #4  3 3 3   3 F 
    @ @ @ "1 v ( ( (  " "#6 7 7 7  >  2  v~ . . .,% ,% ,%\ 1> %f -+27=$+G+G   (@ @ @ @B(Q (Q (QR & &  2 2 2 2mH mH mH^ (: V $,- ,- ,- ,-^ GB3W46 6  "6 *
 ( ( 
(;5 ;5 ;5x "1 v  @ @ @ @       6 +V P P P. * &   < 0 ' ' / /0J K K K
8$ 8$ 8$t   L L L ''<==    8 9 9 9  ' '(8(8 9 9 9 #QQS S S  "  ,8: : :& & &N @\ %&; <:A' $;( ;(  !6 7
   / / / /
+ + + +    = = = = = =rw   