
    uki                       d Z ddlmZ ddl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mZm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)Z* ddl+m,Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z4 ddl5m6Z6 ddl7m8Z8m9Z:m;Z; ddl<m=Z>  ej~                  e@      ZA G d  d!ej                        ZC eC       ZD G d" d#ej                        ZE eE       ZFej                  j                  eE       ej                  j                  eC       ej                  j                  eE       ej                  j                  eC       ej                  j                  eE       ej                  j                  eC       ej                  j                  eE       ej                  j                  eC       ej                  j                  eE       ej                  j                  eC       ej                  j                  eE        ej                  d$      ZOd%eO_P        e#j                  eRcZRZSeOj                  	 	 	 	 dcd&       ZUeOj                  	 	 	 	 dcd'       ZWd( ZX eeXeO)      e'j                  eO<   d* ZZeZe&j                  eO<   	 	 ddd+Z\e\e&j                  eO<   d, Z^d- Z_ e(j                  eOe_d./        e(j                  eOe_d0/        e(j                  eOe_d1d23       d4 Za eeaeO)      e*j                  eO<    e>j                  eO      d5        Zdd6 Zed7 Zfd8 Zgd9 Zh ej                  d:      Zid%ei_P        eij                  	 	 	 	 ded;       Zjeij                  dfd<       Zk eeXei)      e'j                  ei<   d= Zlele&j                  ei<   d> Zmeme&j                  ei<   d? Zn e(j                  eiend./        e(j                  eiend0/        e(j                  eiend1d23        eeaei)      e*j                  ei<    e>j                  ei      d@        Zod2d2dA	 	 	 	 	 	 	 	 	 	 	 dgdBZp G dC dDej                        Zr er       Zs	 	 dhdEZtdF Zud2d2d2d2dG	 	 	 	 	 	 	 	 	 	 	 didHZv eevd%I      Zw ej                  dJ      Zxd%ex_P        ej                  j                  ex       dK Z{exj                  e{       dL Z|exj                  e|       dM Z}e}e'j                  ex<   dN Z~e~e&j                  ex<    ej                         ZdOZ G dP dQ      ZdjdRZ e(j                  exe       dS ZdT ZdkdUZdldVZeeeeef   ef   Zeegeeeeef   f   ZdmdWZdndXZdY Zd%dZd[d\dd]	 	 	 	 	 	 	 	 	 dod^Zdpd_Zd` Z	 	 	 	 	 	 dcdaZeej(                  eO<   	 	 dhdbZeej(                  ei<   y)qz>Module for JAX debugging primitives and related functionality.    )annotations)CallableSequenceN)partial)AnyUnion)apicallback)config)core)dispatch)effects)lax)mesh)	shard_map)sharding_impls)source_info_util)	tree_util)util)
xla_bridge)ad)batching)mlir)partial_eval)
xla_client)ir)hlo)	lax_numpy)Sharding)NamedShardingPartitionSpecparse_flatten_op_sharding)	dischargec                      e Zd Zd Zy)DebugEffectc                     y)NDebug selfs    M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/debugging.py<lambda>zDebugEffect.<lambda><           N__name__
__module____qualname____str__r)   r/   r,   r&   r&   ;   s     'r/   r&   c                      e Zd Zd Zy)OrderedDebugEffectc                     y)NOrderedDebugr)   r*   s    r,   r-   zOrderedDebugEffect.<lambda>@   r.   r/   Nr0   r)   r/   r,   r6   r6   ?   s    ''r/   r6   debug_callbackTc                (   ~~	 t        j                  d      ^}}t        j                  ||      }t        j                  |      5  t        j                  t        j                        5  t        j                  t        j                        5  	  | |  	 d d d        d d d        d d d        y# t        $ r}t        d      |d }~ww xY w# t        $ r t        j                  d        w xY w# 1 sw Y   ZxY w# 1 sw Y   ^xY w# 1 sw Y   yxY w)Ncpu)backendzjax.debug.callback failed to find a local CPU device to place the inputs on. Make sure "cpu" is listed in --jax_platforms or the JAX_PLATFORMS environment variable.zjax.debug.callback failedr)   )r   local_devicesRuntimeErrorr	   
device_putr   default_devicer   _internal_use_concrete_meshmesh_libempty_concrete_meshuse_abstract_meshempty_abstract_meshBaseExceptionlogger	exception)r   effectpartitionedargs
cpu_device_es          r,   debug_callback_implrO   U   s    k--e<NJ 
j	)$j) 2283O3OP""8#?#?@o	   
 
 
	/ 	  23     
se   B0 $D+$C<C0CC<D0	C
9CC
 C-	-C00C95C<<D	DDc                    ~~ ~g |hfS Nr)   )r   rI   rJ   
flat_avalss       r,   debug_callback_abstract_evalrS   l   s     (K	fXr/   c               N   t        d t        | |      D              }d }g }t        |      D ]:  }t        t	        ||      ||       }|j                   |j                  |i |       < t        | D 	cg c]  }	t        j                  |	       }}	|dt        |      z  fS c c}	w )z2Unrolls the debug callback across the mapped axis.c              3  D   K   | ]  \  }}||j                   |     y wrQ   )shape).0xis      r,   	<genexpr>z&debug_batching_rule.<locals>.<genexpr>u   s(      %$!Qm 771: %s    c                \    |t         j                  u r|S t        j                  || |d      S )NF)axiskeepdims)r   
not_mappedr   index_in_dim)rY   dimargs      r,   get_arg_at_dimz+debug_batching_rule.<locals>.get_arg_at_dimx   s,    
h!!!jCu==r/   r   )
nextziprangemapr   appendbindjnpstacklen)
rK   dims	primitiveparams	axis_sizerb   outsrY   args_idxxss
             r,   debug_batching_rulert   s   s     %#dD/ % %)>
 
$ 5a7>1-tT:HKK	3F345 #&t*	-B#))B-	-$	-	tc$i	 
.s   4B")rn   c                0    t        j                  | i |g fS rQ   debug_callback_pri   primalstangentsro   s      r,   debug_callback_jvp_ruler{      s    				26	2B	66r/   c               2    ~~~|D  cg c]  } d  c} S c c} w rQ   r)   )rM   r   rI   rJ   	flat_argss        r,   debug_callback_transpose_ruler~      s    !	"1$	""	"s   	c                    t        t        | j                  j                        | j                  z
        fd} t        j
                  |dg              S )Nc                 f    t        j                   } t        j                  | dk(  fdd       S )Nr   c                 .    t        j                   i S rQ   rv   )rK   ro   s   r,   r-   z9_debug_callback_partial_auto.<locals>.f.<locals>.<lambda>   s    ,114B6B r/   c                     g S rQ   r)   r)   r/   r,   r-   z9_debug_callback_partial_auto.<locals>.f.<locals>.<lambda>   s    B r/   )r   
axis_indexcond)idxrK   ro   partial_autos    r,   fz'_debug_callback_partial_auto.<locals>.f   s.    
..,
'C88C1HB   r/   r)   )in_specs	out_specs)listsetr   
axis_namesmanual_axesr   )axis_contextrK   ro   r   r   s    `` @r,   _debug_callback_partial_autor      sJ    c,++667,:R:RRS, 
 
;		Qr	:	<<r/   c                  | j                   j                  }t        |t        j                        rt        |j                  j                        |j                  z
  }|r1t        t        |fd} t        j                  |      | g| S t        |j                        t        |j                  j                        k(  rxt        j                  j                  r t!        j"                  | j$                        }	nt'        j(                         }	t&        j(                  j*                  j,                  |	_        nJ t        |t        j0                        rt        j                  j                  r-t        j2                  t        j4                  dg d      g      }	nPt'        j(                         }	t&        j(                  j*                  j6                  |	_        dg|	_        dg|	_        nd }	fd}
t<        j>                  jA                        r| jB                  jE                        }t!        jF                  | |
|tI        |      | jJ                  | j$                  dd		      \  }}}| jM                  t        jN                  |i             |S t!        jF                  | |
d tI        |      | jJ                  | j$                  dd|	

      \  }}}|S )NrI   rJ   r   r)   rc   )
mesh_shapedim_shardingslogical_device_ids   r   c                 8    t        j                  | d y)Nr   r)   )rw   impl)r}   r   rI   ro   rJ   s    r,   	_callbackz*debug_callback_lowering.<locals>._callback   s,    		
  r/   T)has_side_effectreturns_tokenrJ   )r   r   rJ   sharding)(module_contextr   
isinstancer   SPMDAxisContextr   r   r   r   r   r   r   	lower_funr   use_shardy_partitionervaluecb!_get_sdy_array_list_for_callbacks	avals_outxc
OpShardingTypeMANUALtypeShardingContextSdyArrayListSdyArrayMAXIMALtile_assignment_dimensionstile_assignment_devicesr   ordered_effectscontains	tokens_ingetemit_python_callbackr   avals_inset_tokens_outTokenSet)ctxrI   rJ   r   rK   ro   r   r   lowerr   r   tokenresultrM   s    ``` `        r,   debug_callback_loweringr      s[   ##00,n<<=|((334|7O7OOL 
&
 ! e #T^^E"3...	\%%	&#l.?.?.J.J*K	K 
	&	&	,	,77F==?**11U, > >? $$**,,

!
!2$H.I Jh hmm((00hm-.Ch)*+h& H %%f-MMf%E..YtDz3<<DkKFE1 t}}fe_56 
-	 **Yd4j#,,DkLFAq 
-r/   r;   )platformgputpuF)r   	cacheablec                B   t        |      r2t        |j                  |      D cg c]
  \  }}|r	| }}}d |g g |fS  | |g|j                  D cg c]  }|j                   c}i |j                  rt        |      r||g g g fS |d g g g fS ||g g g fS c c}}w c c}w rQ   )anyre   invarsavalro   all)saveableunks_ininst_ineqnrn   vinstress           r,   _debug_partial_eval_customr      s     	\

G4
ADD1
AC
Ab"c!!iE3::6a1666E#**E
7| #r2r!! $B"" 
c2r2	 B6s   
BBB
c               @    ~ ~t        j                  ||||d|}||fS Nr   rv   )in_avals	out_avalsrI   rJ   r   rK   ro   outs           r,   $_debug_callback_state_discharge_ruler   	  s6     	Fh	JP	# 
sr/   c                    t        j                  | |f      \  }}i g }}t        |      D ],  \  }}	 t        j                  |       |j                  |       . |||fS # t        t        f$ r |||<   Y Jw xY wrQ   )r   tree_flatten	enumerater   shaped_abstractifyrh   AssertionError	TypeError)rK   kwargsr}   in_treestatic_argsdyn_argsrY   as           r,   _split_callback_argsr     s     --tVn=)Wbx+	" da
a ooa 
(K	'' I& k!ns   &AA54A5c                   t        |      }d gt        |      t        |      z   z  }t        |      }t        t        |            D ]  }||v r	||   ||<   t	        |      ||<    t	        |d       J t        j                  | |      S rQ   )dictrl   iterrf   rd   r   tree_unflatten)r   r   r   static_args_dictall_argsdirY   s          r,   merge_callback_argsr      s    +&Vs;'#h-78(H~"X a$Q'hqkHhqk	
 
b$				!	!'8	44r/   c                      fd}|S )Nc                 6    t        |       \  }} |i | yNr)   )r   )r   rK   r   r   r   r   s      r,   _flat_callbackz+_make_flat_callback.<locals>._flat_callback.  s%    &w+FLD&dfr/   r)   )r   r   r   r   s   ``` r,   _make_flat_callbackr   -  s     
r/   c                V    t        |||      \  }}t        j                  | g|i | y rQ   )r   	formatterformat)fmtr   r   r   rK   r   s         r,   _check_formatr   5  s,    $WhD,$3(((r/   debug_printc                    t        t        | t        |      ||      }	t        ||	|      }	|rt        nt
        }
t        ||	|
|d y)Nr   rI   rJ   r)   )r   _format_print_callbackr   r   ordered_debug_effectdebug_effectrO   )r   orderedrJ   r   r   np_printoptionshas_placeholderslogging_recordrK   r   rI   s              r,   debug_print_implr   >  sP     c4#8:J( !(K@(#*&hv; 
r/   c                .    ~~ ~|rt         nt        }g |hfS rQ   )r   r   )r   r   avalsr   rI   s        r,   debug_print_abstract_evalr   V  s    S&#*&	fXr/   c                0    t        j                  | i |g fS rQ   debug_print_pri   rx   s      r,   debug_print_jvp_ruler  b  s    			W	/	/	33r/   c                .    ~|D  cg c]  } d  c} S c c} w rQ   r)   )rM   rK   r   s      r,   debug_print_transpose_ruler  i  s    	1$		s   	c                   t        t        |t        |      ||      }
t        ||
|      }
|rt        nt
        }t        | g|	|||
dS r   )r   r   r   r   r   r   r   )r   r   r   rJ   r   r   r   r   r   r   r   rI   s               r,   debug_print_lowering_ruler  q  s`     	
?( !(K@(#*&	 	

#x
 r/   c                8    ~ ~t        j                  |i |}||fS rQ   r  )r   r   rK   r   r   s        r,   !_debug_print_state_discharge_ruler	    s&    	D+F+#	sr/   )r   rJ   c                   	 t               st        dt                      t        ||      \  }	 	fd}|rt        nt
        }t        j                  ||||d y)a  Calls a stageable Python callback.

  For more explanation, see `External Callbacks`_.

  ``jax.debug.callback`` enables you to pass in a Python function that can be called
  inside of a staged JAX program. A ``jax.debug.callback`` follows existing JAX
  transformation *pure* operational semantics, which are therefore unaware of
  side-effects. This means the effect could be dropped, duplicated, or
  potentially reordered in the presence of higher-order primitives and
  transformations.

  We want this behavior because we'd like ``jax.debug.callback`` to be "innocuous",
  i.e. we want these primitives to change the JAX computation as little as
  possible while revealing as much about them as possible, such as which parts
  of the computation are duplicated or dropped.

  Args:
    callback: A Python callable returning None.
    *args: The positional arguments to the callback.
    ordered: A keyword only argument used to indicate whether or not the
      staged out computation will enforce ordering of this callback w.r.t.
      other ordered callbacks.
    partitioned: If True, then print local shards only; this option avoids an
      all-gather of the operands. If False, print with logical operands; this
      option requires an all-gather of operands first.
    **kwargs: The keyword arguments to the callback.

  Returns:
    None

  See Also:
    - :func:`jax.experimental.io_callback`: callback designed for impure functions.
    - :func:`jax.pure_callback`: callback designed for pure functions.
    - :func:`jax.debug.print`: callback designed for printing.

  .. _External Callbacks: https://docs.jax.dev/en/latest/notebooks/external_callbacks.html
  zQfirst argument to jax.debug.callback must be callable, but got an object of type c                    d gt              t        |       z   z  }t        |       }t        t        |            D ]  }|v r	|   ||<   t        |      ||<    t        |d       J t	        j
                  |      \  }} |i | yr   )rl   r   rf   rd   r   r   )	r   r   r   rY   rK   r   r   r   r   s	         r,   r   z&debug_callback.<locals>._flat_callback  s    v[)CM9:H	hB3x=! 	
k	!!n2h	
 D>!!!++GX>LD&dfr/   r   N)callabler   r   r   r   r   rw   ri   )
r   r   rJ   rK   r   r   r   rI   r   r   s
   `       @@r,   r9   r9     sm    X 
(	
 115h0@B C C#7f#E '8[ $+&.[r/   c                      e Zd Zd Zd Zy)_DebugPrintFormatCheckerc                     ~~y)N r)   )r+   r   format_specs      r,   format_fieldz%_DebugPrintFormatChecker.format_field  s
    {r/   c                    t        |      D cg c]  \  }}||vs| }}}|D cg c]	  }||vs| }}|rt        d|       |rt        d| d      y c c}}w c c}w )Nz2Unused positional arguments to `jax.debug.print`: z/Unused keyword arguments to `jax.debug.print`: zt. You may be passing an f-string (i.e, `f"{x}"`) into `jax.debug.print` and instead should pass in a regular string.)r   
ValueError)	r+   	used_argsrK   r   rY   ra   unused_argskunused_kwargss	            r,   check_unused_argsz*_DebugPrintFormatChecker.check_unused_args  s    %.t_K61c8J3KKK &=1!9*<Q=M=>{m
LN N;M? KK KL L  L=s   AA	AAN)r1   r2   r3   r  r  r)   r/   r,   r  r    s    
Lr/   r  c                x   |r1t        j                  di |5   | j                  |i |}d d d        n%|rJ d       dj                  | gd |D              }|r2t	        j                  |      }|_        t        j                  |       y t        j                  j                  dz          y # 1 sw Y   `xY w)Nz3Format without placeholders should not have kwargs. c              3  2   K   | ]  }t        |        y wrQ   )strrW   r   s     r,   rZ   z)_format_print_callback.<locals>.<genexpr>  s     0a3q60s   
r)   )npprintoptionsr   joincopymsgrG   handlesysstdoutwrite)r   r   r   r   rK   r   r$  s          r,   r   r     s     		+?	+ (CJJ''c( ( LLL:
((C10401
2CYY~.NN
MM.!JJS4Z ( (s   B00B9c           	         t        j                         }t        j                  |j                        }d}d}|r|j                  }|j
                  }d}t        j                  t        j                  | ||d|d       S )Nz(unknown file)r   r)   r  )	r   current
user_frame	traceback	file_name
start_linerG   
makeRecordname)levelsir+  r-  line_norK   s         r,   _make_logging_recordr4    sr    !"**2<<8*)'$$I##G	$			kk5)Wb$
 r/   )r   rJ   skip_format_check_use_loggingc                  |st        j                  | g|i | d}| r<t        t        t	        j
                         j                  |                   ^}}	}|	du}t        ||      \  }
}}t        |j                               }t        t        j                         j                               }t        j                  || |||
||||rt        t        j                         ndd y)a~  Prints values and works in staged out JAX functions.

  This function does *not* work with f-strings because formatting is delayed.
  So instead of ``jax.debug.print(f"hello {bar}")``, write
  ``jax.debug.print("hello {bar}", bar=bar)``.

  This function is a thin convenience wrapper around :func:`jax.debug.callback`.
  The implementation is essentially::

    def debug_print(fmt: str, *args, **kwargs):
      jax.debug.callback(
          lambda *args, **kwargs: print(fmt.format(*args, **kwargs)),
          *args, **kwargs)

  It may be useful to call :func:`jax.debug.callback` directly instead of this
  convenience wrapper. For example, to get debug printing in logs, you might
  use :func:`jax.debug.callback` together with ``logging.log``.

  Args:
    fmt: A format string, e.g. ``"hello {x}"``, that will be used to format
      input arguments, like ``str.format``. See the Python docs on `string
      formatting <https://docs.python.org/3/library/stdtypes.html#str.format>`_
      and `format string syntax
      <https://docs.python.org/3/library/string.html#formatstrings>`_.
    *args: A list of positional arguments to be formatted, as if passed to
      ``fmt.format``.
    ordered: A keyword only argument used to indicate whether or not the staged
      out computation will enforce ordering of this ``jax.debug.print`` w.r.t.
      other ordered ``jax.debug.print`` calls.
    partitioned: If True, then print local shards only; this option avoids an
      all-gather of the operands. If False, print with logical operands; this
      option requires an all-gather of operands first.
    skip_format_check: If True, the format string is not checked. This is useful
      when using the function from inside a Pallas TPU kernel, where scalars
      args will be printed after the format string.
    **kwargs: Additional keyword arguments to be formatted, as if passed to
      ``fmt.format``.
  FN)r   r   rJ   r   r   r   r   r   )r   r   rd   r   string	Formatterparser   tupleitemsr   get_printoptionsr  ri   r4  loggingINFO)r   r   rJ   r5  r6  rK   r   r   rM   
field_namer   r   r   r   s                 r,   r   r     s    ^ 
S*4*6*T&"2"2"4":":3"?@AAzA!-#7f#E '8[k'')*+"--/5578/
%'<H*7<<8r/   )r6  inspect_shardingc               *     || j                          g S rQ   )r   )r   r   s     r,   _inspect_sharding_implrC  f  s    
5>>	)r/   c                    ~ g t         hfS rQ   )r   )r   rM   s     r,   _inspect_sharding_abstract_evalrE  k  s    
	l^	r/   c               @    | \  }t         j                  ||       g g fS Nr
   inspect_sharding_pri   )rK   rM   r   r   s       r,   _inspect_sharding_batching_rulerJ  q  s%    &%%(3	R-r/   c                0    t        j                  | i |g fS rQ   rH  )ry   rM   ro   s      r,   _inspect_sharding_jvp_rulerL  x  s    		 	 '	4V	4b	88r/   InspectShardingc                      e Zd Zd Zy)ShardingCallbackInfoc                     || _         || _        y rQ   )r   r   )r+   r   r   s      r,   __init__zShardingCallbackInfo.__init__  s    DM(Dr/   N)r1   r2   r3   rQ  r)   r/   r,   rO  rO    s    )r/   rO  c               l   t         j                  j                  j                  | j                  j
                  }t        |t        j                        rt|j                  t        d      |j                  }|t        j                  t        j                        j                  |j                         |j"                        nQt        |t        j$                        r#|j&                  |j&                  j(                  nt+        t-        |            J dfd}t/              dk(  r |t        j0                         g S t3        j4                  |      }| j                  j7                  |       t9        j:                  |j,                  g|gt<        j>                  jA                  tB              t<        jD                  jA                  d      tG        jH                  d      t<        jJ                  jA                  g       t<        j>                  jA                  |      d d 	       g S )Nz9Please file a bug at https://github.com/jax-ml/jax/issuesc                    j                   r t        j                  |             S | j                         r
t	               nt        |       d   } t        |            S Nr   )emptyr   GSPMDSharding	is_manualPr#   r!   )hlo_shardingpspecr   devicesr   s     r,   _hlo_sharding_callbackz?_inspect_sharding_lowering_rule.<locals>._hlo_sharding_callback  s]    zz

&
&w
=? ? **,QS&|T:1= 
M$.//r/   r   T)call_target_namer   api_versioncalled_computationsbackend_configoperand_layoutsresult_layouts)rY  zxc.HloSharding)&rB   thread_resourcesenvphysical_meshr   r   r   r   r   device_assignmentr   abstract_meshMeshr   arrayreshape
axis_sizesr   r   r   _flat_devices_tupleNotImplementedErrorr   rl   replicated_hlo_shardingr    encode_inspect_sharding_callbackadd_keepaliver   CustomCallOpr   
StringAttrr   _INSPECT_SHARDING_CALL_NAMEBoolAttrr   i32_attr	ArrayAttr)	r   r   r   r   amr\  keyr[  r   s	     `    @@r,   _inspect_sharding_lowering_rulery    s    
	"	"	&	&	4	4$##00,n<<=,,G
EG G		#	#B	~]]288G,44R]]C==*d, > >?D33G
d<0
11			0 	\Q >AABI
++,BC# ""#9:EJJ<%$&MM$5$50%2#%;;??4#8#}}Q/')||'7'7';"$--"3"3C"8#'"&( 
)r/   c                
    ~ ~g S rQ   r)   )r   backend_strings     r,   #inspect_sharding_prop_user_shardingr|    s    	)r/   c                    ~ ~~|d   S rT  r)   )
arg_shapesarg_shardingsrV   r{  s       r,   -inspect_sharding_infer_sharding_from_operandsr    s    %	q	r/   c                    |j                   |j                  cxk(  rd k(  ry |j                   |j                  z
  }|j                  |z  dk(  sJ | |z  dk(  sJ |j                  |z  S rT  )stopstart)sizeslc
slice_sizes      r,   _slice_to_chunk_idxr    sj    XX"d" #xx#))#*	Z	1	$$	$	
	a			j	  r/   c                D    t        | t              rt        | | dz         S | S )Nr   )r   intslice)r  s    r,   _raise_to_slicer    s"    ScAg	*r/   c                X    t        | t              r| S d | D        \  }}}d|d|d|dS )Nc              3  8   K   | ]  }t        |d z          yw)   Nr  r  s     r,   rZ   z&_canonicalize_color.<locals>.<genexpr>  s     )aSS\)s   #02X)r   r  colorrgbs       r,   _canonicalize_colorr    s<    sL)5)'!QQsGAc71S'	""r/   c                h    t        d | dd | dd | dd f      \  }}}|dz  |dz  z   |dz  z   d	kD  ry
y)Nc                    t        | d      S )N   r  )rX   s    r,   r-   z!_get_text_color.<locals>.<lambda>  s    #a* r/   r            gA`"?gbX9?gv/?   z#000000z#ffffff)rg   r  s       r,   _get_text_colorr    sR    $uQqz51:uQqz&JK'!Q%i!e)a%i'3.	r/   c              #     K   ||z  }t        j                  dd|      }d}t        |      D ],  } | ||          ||dz  z   t        |dz  dk(        z   |z  }. y w)Nr   r      )r   linspacerf   bool)	color_mapnum_rowsnum_cols
num_colorscolor_valuesr   rM   s          r,   make_color_iterr    ss     ("*Q:.,	# Ka
L%
&&q 4
Q!(;#<<

JCKs   AAg      ?	   P   )	use_colorscale	min_width	max_widthr  c               
   t         j                  j                  d      st        d      ddl}ddl}ddl}ddl}ddl}ddl	}t        |       dkD  st        |       dk  rt        d      |j                  j                  |      }|xr |j                  du}|r|s	 ddl}	|	j                  d	   }t#        d|z        }
t        |       dk(  r| d   nd| d   z  }t#        |
|z        }d}t%        t'        |j(                              j*                  j-                         }|j/                  t1        |             }i }i }i }t3        |j5                               D ]N  \  }\  }}|J t1        t7        t8        |            }t1        t7        t:        | |            }|t<        t        |      dk(  rt|\  }}|j>                  |j>                  |j@                  z
  n| d   }|j>                  |j>                  |j@                  z
  n| d   }|| d   z  }|| d   z  }|||<   |||<   nQ|\  }tC        ddd      }|j>                  |j>                  |j@                  z
  n| d   }dg|}d||<   || d   z  ||<   |jE                  |tG                     jI                  |jJ                         Q tM        d |jO                         D              dz   }t        tQ        |jO                               d         dk(  rd}n#tM        d |jO                         D              dz   }tS        |||      }|jT                  jW                  d
| d| d
|s|jX                  jZ                  nd      } t]        |      D ]  }g }!t]        |      D ]  }"| ddj_                  ta        |||"f         D #cg c]  }#tc        |#       c}#      z   }$|||"f   |||"f   }&}%t#        |%|z  |z        }%|&d}'nt#        |&|
z        }'te        tM        |%|      |      }%tg        |%t        |$      z
  dz
  d      \  }(})|(|)z   }*tg        |'dz
  d      \  }+})|+|)z   },|r7ti        t%        |      dd       }-tk        |-      }.|+dz  }+|,dz  },|(dz  }(|*dz  }*nd}-d}.tM        |+d      tM        |*d      tM        |,d      tM        |(d      f}/|!jm                  |jn                  jq                  |jr                  ju                  |$dd      |/|jv                  jy                  |-|.                     | jz                  |!   |j}                  | d       y# t         $ r d
}Y w xY wc c}#w )z)Visualizes a ``Sharding`` using ``rich``.richz5`visualize_sharding` requires `rich` to be installed.r   Nr  r   zC`visualize_sharding` only works for shapes with 1 and 2 dimensions.)widthtab20bF
   g      @c              3  &   K   | ]	  }|d      yw)r   Nr)   r  s     r,   rZ   z%visualize_sharding.<locals>.<genexpr>-  s     -!1-   c              3  &   K   | ]	  }|d      yw)r   Nr)   r  s     r,   rZ   z%visualize_sharding.<locals>.<genexpr>1  s     /A1Q4/r  )show_header
show_linespadding	highlightpad_edgeboxr  ,r  centermiddle)vertical)bgcolorr  )stylez

)end)?	importlibr   	find_specr  
rich.alignrich.consolerich.boxrich.padding
rich.style
rich.tablerl   consoleConsolecolor_system
matplotlib	colormapsModuleNotFoundErrorr  rd   r   
device_setr   upperdevices_indices_mapr;  r   r<  rg   r  r  rm  r  r  r  
setdefaultr   addidmaxkeysr   r  tableTabler  SQUARErf   r"  sortedr  mindivmodr  r  rh   r  PaddingalignAlignr  Styleadd_rowprint)0rV   r   r  r  r  r  r  r  r  mplbase_heightaspect_ratio
base_widthheight_to_width_ratiodevice_kinddevice_indices_mapslicesheightswidthsrY   devslcs
chunk_idxsverthoriz	vert_size
horiz_sizechunk_heightchunk_widthr  r  
color_iterr  coljsentryr  maybe_heightheightleft_padding	remainderright_paddingtop_paddingbottom_paddingr  
text_colorr  s0                                                   r,   visualize_shardingr    sq   
 
	!	!&	)
L
MM  	Z!^s5zA~
MO OLL  y 1'<G00<)y--)i BJ+!%jAo%(1a@,;-.* T(--./88>>@+33E%LA,.&13')+&!"4":":"<= 5na#t_d+,Ds.t<=J|
4yA~kdE26))2HTYY$**,q 27**2HUZZ%++-q q)lq)k(gj&fZ fe1ad(-

(>5::#E!H #
#j gj&q1fZ
j#%(,,SVV4354 -v{{}--1(fkkm	Q	 A%H///!3Hy(H=*
**

uY#$)2]U6?txxT  K% ? $a
C8_ !"Q&1:N)OQ#a&)O PPe"1a4L'!Q$-\e%*$'<<=e		\K/0#eY'3e &us5z'9A'=q AlI"Y.m%fqj!4k9"Y.n	#D$4Ra$89$U+
q!

k1

mQ

na
 
lA
	g 
jj
,,

JJUHx@'**""5 #    !";!"D EMM3I$J 
--6-"u  ip *Ps   T9 $U9UUc               <    fd}t        j                  ||        y)a  Enables inspecting array sharding inside JIT-ted functions.

  This function, when provided with a Pytree of arrays, calls back with each of
  their shardings and works in ``jax.jit``-ted computations, enabling inspecting
  the chosen intermediate shardings.

  The policy for when ``callback`` is called is *as early as possible* when the
  sharding information is available. This means if ``inspect_array_callback`` is
  called without any transformations, the callback will happen immediately
  since we have the array and its sharding readily available. Inside of a
  ``jax.jit``, the callback will happen at lowering time, meaning you can
  trigger the callback using the AOT API (``jit(f).lower(...)``). When inside of
  a ``jax.jit``, the callback happens *at compile time* since the sharding is
  determined by XLA. You can trigger the callback by using JAX's AOT API
  (``jax.jit(f).lower(...).compile()``). In all cases, the callback will be
  triggered by running the function, since running a function entails lowering
  and compiling it first. However, once the function is compiled and cached,
  the callback will no longer occur.

  This function is experimental and its behavior may change in the future.

  Args:
    value: A Pytree of JAX arrays.
    callback: A callable that takes in a ``Sharding`` and doesn't return a value.

  In the following example, we print out the sharding of an intermediate value
  in a ``jax.jit``-ted computation:

  >>> import jax
  >>> import jax.numpy as jnp
  >>> from jax.sharding import Mesh, PartitionSpec
  >>>
  >>> x = jnp.arange(8, dtype=jnp.float32)
  >>> def f_(x):
  ...   x = jnp.sin(x)
  ...   jax.debug.inspect_array_sharding(x, callback=print)
  ...   return jnp.square(x)
  >>> f = jax.jit(f_, in_shardings=PartitionSpec('dev'),
  ...             out_shardings=PartitionSpec('dev'))
  >>> with jax.set_mesh(Mesh(jax.devices(), ('dev',))):
  ...   f.lower(x).compile()  # doctest: +SKIP
  ...
  NamedSharding(mesh={'dev': 8}, partition_spec=PartitionSpec(('dev',),))
  c                4    t         j                  |        y rG  rH  )valr   s    r,   _inspectz(inspect_array_sharding.<locals>._inspect  s    C(3r/   N)r   tree_map)r   r   r  s    ` r,   inspect_array_shardingr  _  s    Z4Xu%r/   c                .      fd}t         |       y)zVisualizes an array's sharding.c                2    t        j                  | fi S rQ   )r  rV   )r   arrr   s    r,   
_visualizez,visualize_array_sharding.<locals>._visualize  s    cii<V<<r/   r
   N)r  )r  r   r  s   `` r,   visualize_array_shardingr    s    =z2r/   c                   ~t        j                         5  t        t        t        |       }d d d        t        t        j                  | j                              D ]  \  \  }}} ||   g S # 1 sw Y   DxY wrQ   )r   eval_contextre   rg   r   r   ndenumerater[  )	r   r   rI   rJ   rK   
all_blocksr   deviceblockss	            r,   _debug_callback_eager_ruler    sq      'c$o&J'"2>>$,,#?L msFVf	)	' 's   A00A9c               L   ~~t        t        |t        |      ||      }
t        ||
|      }
t	        j
                         5  t        t        t        |	       }d d d        t        t        j                  | j                              D ]  \  \  }}} |
|   g S # 1 sw Y   DxY wrQ   )r   r   r   r   r   r  re   rg   r   r   r  r[  )r   r   r   rJ   r   r   r   r   r   rK   r   r  r   r  r  s                  r,   _debug_print_eager_ruler    s     {c4#8:J( !(K@( 'c$o&J'"2>>$,,#?L msFVf	)	' 's    BB#)r   Callable[..., Any]rI   r&   rJ   r  )r   r  rI   r&   )rK   r   r   r  )r   r   r   r  )r   zCallable[..., None]rK   r   r   r  rJ   r  r   r   returnNone)r   r  )r   r  r   r  rJ   r  r5  r  r6  r  r  r  )r   zmlir.LoweringRuleContext)r  r  r  r  r  r  )r  zslice | int)r  Colorr  r  )r  r  r  r  )rV   zSequence[int]r   r    r  r  r  floatr  r  r  r  r  zColorMap | None)r   zCallable[[Sharding], None])__doc__
__future__r   collections.abcr   r   r#  	functoolsr   importlib.utilr  r>  r8  r&  typingr   r   weakrefnumpyr   jax._srcr	   r   r   r   r   r   r   r   r   rB   r   r   r   r   r   r   jax._src.interpretersr   r   r   r   pejax._src.libr   r   jax._src.lib.mlirr   jax._src.lib.mlir.dialectsr   jax._src.numpyr   rj   jax._src.shardingr    jax._src.sharding_implsr!   r"   rX  r#   jax._src.stater$   state_discharge	getLoggerr1   rG   Effectr&   r   r6   r   r   add_typelowerable_effectscontrol_flow_allowed_effectsremat_allowed_effects"custom_derivatives_allowed_effectspartial_eval_kept_effects	Primitiverw   multiple_resultssafe_maprg   
unsafe_mapdef_implrO   def_effectful_abstract_evalrS   rt   primitive_batchersr{   primitive_jvpsr~   primitive_transposesr   r   register_loweringr   partial_eval_jaxpr_custom_rulesregister_discharge_ruler   r   r   r   r   r  r   r   r  r  r  r	  r9   r9  r  r   r   r4  r   	debug_logrI  %prim_requires_devices_during_loweringr  rC  rE  rJ  rL  WeakValueDictionarysharding_callbacksrs  rO  ry  r|  r  r  r  r;  r   r  r  ColorMapr  r  r  r  r  r  r  eager_rulesr  r)   r/   r,   <module>rN     s   E " .      
     #      %  # %    $ * & 4 )   * + &B B 7			8	$!'.. !}( ( *+        !3 4    " "; /    " "#5 6  $ $ - -k :  $ $ - -.@ A    & &{ 3    & &'9 :  * * 3 3K @  * * 3 34F G  ! ! * *; 7  ! ! * *+= > "4>>"23 $(  !--Z +:> , --)4CG . $ 18#31  , -7&=  " ##*5# -J  ( )=@B   ')@ %'   -?   -
< 8?*:8 " "#3 4 )(()9: ;	(
5)
 }-!%  	 . ** + .5=.  M *
4 $8  -  
 *D   &4   }&?% P   }&?% P   ,u 5<-5 " "= 1
 )((7 8 	A!AA A 	A
 A 
AHLv// L$ %&	!	!"$ #E	E E 	E
 E E 
EP Kd3	 $T^^$67 &*  #  . . 2 23E F   2 3  . ./N O
 $ 	  . /9(B  $ %0W002 / ) )
4j   )+J K
!
 	eE5%'(#-.UGU5%#=>>?#K *.b()B48u#"&u#6;u#"%u#69u# #2u#n/&b3 ! 	
  +E	  & ' 
2 (?	  m $r/   