
    Vpfaa                       d Z ddlmZ ddlZddlmZ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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/m0Z0  e	j1        e2          Z3 G d dej4                  Z5 e5            Z6 G d dej4                  Z7 e7            Z8ej9        :                    e7           ej;        :                    e5           ej;        :                    e7           ej<        :                    e5           ej<        :                    e7           ej=        :                    e5           ej=        :                    e7           ej>        :                    e5           ej>        :                    e7            ej?        d          Z@de@_A        ejB        eCcZCZDe@jE        dhd             ZFe@jG        dhd!            ZHd" ZIeIe!jJ        e@<   d# ZKeKe jL        e@<   dhd$ZMeMe jN        e@<   d% ZO e"jP        e@eOd&'            e"jP        e@eOd('            e"jP        e@eOd)'           d* ZQeQe$jR        e@<   d+d,did5ZS G d6 d7e
jT                  ZU eU            ZVdjd:ZWd+d,dkd;ZX ej?        d<          ZYdeY_A        ejZ        [                    eY           d= Z\eYE                    e\           d> Z]eYG                    e]           d? Z^e^e!jJ        eY<   d@ Z_e_e jL        eY<    ej`                    ZadAZb G dB dC          ZcdldFZd e"jP        eYed           dG ZedH ZfdmdMZgdndOZheeiejejejf         ekf         Zleejgeiejejejejf         f         ZmdodRZndpdSZodT ZpddUdVdWddXdqddZqdrdfZrdg ZsdS )sz>Module for JAX debugging primitives and related functionality.    )annotationsN)CallableSequence)AnyUnion)lax)core)effects)mesh)sharding_impls)dispatch)	tree_util)util)ad)batching)mlir)partial_eval)
xla_client)ir)hlo)Sharding)NamedShardingparse_flatten_op_shardingc                      e Zd Zd ZdS )DebugEffectc                    dS )NDebug selfs    R/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/debugging.py<lambda>zDebugEffect.<lambda>5   s         N__name__
__module____qualname____str__r   r#   r!   r   r   4   s          '''r#   r   c                      e Zd Zd ZdS )OrderedDebugEffectc                    dS )NOrderedDebugr   r   s    r!   r"   zOrderedDebugEffect.<lambda>9   s     r#   Nr$   r   r#   r!   r*   r*   8   s        '''''r#   r*   debug_callbackTcallbackCallable[..., Any]effectc                b   ~	 t          j        d          ^}}n"# t          $ r}t          d          |d }~ww xY wt          j        ||          }t          j        |          5  	  | |  n)# t
          $ r t                              d            w xY w	 d d d            n# 1 swxY w Y   dS )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   )jaxlocal_devicesRuntimeError
device_putdefault_deviceBaseExceptionlogger	exception)r.   r0   args
cpu_device_es         r!   debug_callback_implr@   L   s*    &u555NJ	   
	/  	 
j	)	)$
*%%  hooo   2333                
s:    
:5:&B$(A.-B$.&BB$$B(+B(c                    ~~ g |hfS Nr   )r.   r0   
flat_avalss      r!   debug_callback_abstract_evalrD   a   s     (	fXr#   c                \   t          d t          | |          D                       }d }g }t          |          D ]J}t          t	          j        ||          ||           }|                    t          j        |i |           Kd t          | D             }|dt          |          z  fS )z2Unrolls the debug callback across the mapped axis.c              3  :   K   | ]\  }}||j         |         V  d S rB   )shape).0xis      r!   	<genexpr>z/debug_callback_batching_rule.<locals>.<genexpr>i   s9       % %$!Qm 71:#mmm% %r#   c                R    |t           j        u r|S t          j        || |d          S )NF)axiskeepdims)r   
not_mappedr   index_in_dim)rJ   dimargs      r!   get_arg_at_dimz4debug_callback_batching_rule.<locals>.get_arg_at_dimm   s/    
h!!!jCu====r#   c                6    g | ]}t          j        |          S r   )jnpstack)rH   xss     r!   
<listcomp>z0debug_callback_batching_rule.<locals>.<listcomp>v   s     	-	-	-B#)B--	-	-	-r#   )r   )
nextziprangemap	functoolspartialappenddebug_callback_pbindlen)r<   dimsparams	axis_sizerS   outsrJ   args_idxs           r!   debug_callback_batching_rulerh   g   s     % %#dD// % % % % %)> > >
 
$ < <a9$^Q77tDDHKK %x:6::;;;;	-	-#t*	-	-	-$	tc$ii	r#   c                (    t          j        | i |g fS rB   )r`   ra   )primalstangentsrd   s      r!   debug_callback_jvp_rulerl   z   s    			26	2	2B	66r#   c                &    ~~ ~t          d          )Nz.Transpose doesn't support debugging callbacks.)
ValueError)r.   r0   	flat_argss      r!   debug_callback_transpose_rulerp   ~   s    6CDDDr#   c          
        | j         j        }t          |t          j                  r^t          |j                  t          |j        j                  k    r/t          j
                    }t          j
        j        j        |_        ngt          |t          j        t          j        f          r?t          j
                    }t          j
        j        j        |_        dg|_        dg|_        nd }fd}t$          j                                      rz| j                                      }t/          j        | ||t3          |          | j        | j        d          \  }	}}
|                     t/          j        |i                     n7t/          j        | |d t3          |          | j        | j        d|          \  }	}
}
|	S )N   r   c                 0    t          j        | d dS )N)r0   r.   r   )r`   impl)ro   r.   r0   rd   s    r!   	_callbackz*debug_callback_lowering.<locals>._callback   s6    	6H@ @8>@ @ @2r#   T)has_side_effect)rv   sharding)module_contextaxis_context
isinstancer   SPMDAxisContextsetmanual_axesr   
axis_namesxc
OpShardingTypeMANUALtypeShardingContextMAXIMALtile_assignment_dimensionstile_assignment_devicesr
   ordered_effectscontains	tokens_ingetr   emit_python_callbacklistavals_in	avals_outset_tokens_outTokenSet)ctxr0   r.   r<   rd   ry   rw   ru   tokenresultr>   s    `` `      r!   debug_callback_loweringr      s   #0,~=>> L$%%\->-I)J)JJJ }HM&-HMM%~'EF   }HM&.HM+,#H'()sH$$ H       %%f-- 	1Mf%%E0YtDzz3<  FE1 t}fe_556666,Yd4jj#,x1 1 1LFAq 
-r#   r2   )platformgputpuc                    t          |          r&d t          |j        |          D             }d |g g |fS  | t          gd |j        D             R i |j        rt          |          r||g g g fS |d g g g fS ||g g g fS )Nc                    g | ]	\  }}||
S r   r   )rH   vinsts      r!   rX   z7_debug_callback_partial_eval_custom.<locals>.<listcomp>   s!    
A
A
ADD
A1
A
A
Ar#   c                    g | ]	}|j         
S r   )aval)rH   r   s     r!   rX   z7_debug_callback_partial_eval_custom.<locals>.<listcomp>   s    !=!=!=Q!&!=!=!=r#   )anyrZ   invarsr`   rd   all)saveableunks_ininst_ineqnress        r!   #_debug_callback_partial_eval_customr      s     	\\ "
A
AC
G44
A
A
ACb"c!!XL!=!=#*!=!=!=LLLLL 	#
7|| # #r2r!! $B"" 
c2r2	r#   F)orderedCallable[..., None]r<   r   r   boolkwargsreturnNonec                    t                     st          dt                                t          j        ||f          \  }|rt
          nt          } fd}t          j        |||d dS )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.
    **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://jax.readthedocs.io/en/latest/notebooks/external_callbacks.html
  zQfirst argument to jax.debug.callback must be callable, but got an object of type c                 H    t          j        |           \  }} |i | dS )Nr   )r   tree_unflatten)ro   r<   r   r.   in_trees      r!   _flat_callbackz&debug_callback.<locals>._flat_callback   s3    +GY??LD&Hdf2r#   )r.   r0   N)	callable	TypeErrorr   r   tree_flattenordered_debug_effectdebug_effectr`   ra   )r.   r   r<   r   ro   r0   r   r   s   `      @r!   r-   r-      s    H 
(		 C
 B15hB B C C C -tVn==)W#*<&      ^FKKKKKKr#   c                      e Zd Zd ZdS )_DebugPrintFormatCheckerc                    fdt          |          D             }fd|D             }|rt          d|           |rt          d| d          d S )Nc                "    g | ]\  }}|v	|S r   r   )rH   rJ   rR   	used_argss      r!   rX   z>_DebugPrintFormatChecker.check_unused_args.<locals>.<listcomp>  s'    KKK61c8J8J38J8J8Jr#   c                    g | ]}|v|	S r   r   )rH   kr   s     r!   rX   z>_DebugPrintFormatChecker.check_unused_args.<locals>.<listcomp>  s#    ===1!9*<*<Q*<*<*<r#   z2Unused 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.)	enumeratern   )r    r   r<   r   unused_argsunused_kwargss    `    r!   check_unused_argsz*_DebugPrintFormatChecker.check_unused_args  s    KKKKYt__KKKK=======M N
L{
L
LN N N LKM K K KL L LL Lr#   N)r%   r&   r'   r   r   r#   r!   r   r     s(        
L 
L 
L 
L 
Lr#   r   fmtstrc                `    t           j                             | j        |i |dz              d S )N
)sysstdoutwriteformat)r   r<   r   s      r!   _format_print_callbackr     s4    *:3:t.v..566666r#   c                   t          j        | g|R i | t          t          j        t
          |           g|R i |d|i dS )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.
    **kwargs: Additional keyword arguments to be formatted, as if passed to
      ``fmt.format``.
  r   N)	formatterr   r-   r]   r^   r   )r   r   r<   r   s       r!   debug_printr     s}    B 3((((((("#93?? ,$ , , ,, ,#*, , , , , ,r#   inspect_shardingc               &     || j                    g S rB   )rw   )valuer.   s     r!   _inspect_sharding_implr   A  s    
(5>	)r#   c                    ~ g t           hfS rB   )r   )r   r>   s     r!   _inspect_sharding_abstract_evalr   F  s    
	l^	r#   c               J    | \  }t                               ||           g g fS Nr.   inspect_sharding_pra   )r<   r>   r.   r   s       r!   _inspect_sharding_batching_ruler   L  s+    &%%(333	R-r#   c                (    t          j        | i |g fS rB   r   )rj   r>   rd   s      r!   _inspect_sharding_jvp_ruler   S  s    		 '	4V	4	4b	88r#   InspectShardingc                      e Zd Zd ZdS )ShardingCallbackInfoc                "    || _         || _        d S rB   )r.   rx   )r    r.   rx   s      r!   __init__zShardingCallbackInfo.__init__[  s    DM(Dr#   N)r%   r&   r'   r   r   r#   r!   r   r   Z  s#        ) ) ) ) )r#   r   r   mlir.LoweringRuleContextc               p   t           j        j        j        | j        j        }t          |t          j                  r|j	        t          d          nCt          |t          j                  r|j        j        nt          t          |                    J dfd}t!                    dk    r |t          j                               g S t%          j        |          }| j                            |           t+          j        |j        g|gt.          j                            t4                    t.          j                            d          t9          j        d          t.          j                            g           t.          j                            |          d d 	  	         g S )	Nz9Please file a bug at https://github.com/google/jax/issueshlo_shardingxc.HloShardingc                    j         r t          j        |                     S t          |           d                                         } t          |                    S Nr   )emptyr   _op_sharding_to_pos_shardingr   get_partition_specr   )r   pspecr.   devicesr   s     r!   _hlo_sharding_callbackz?_inspect_sharding_lowering_rule.<locals>._hlo_sharding_callbackr  so    z NX

5lG
L
LN N N%lD99!<OOQQE8M$..///r#   rr   T)call_target_namerv   api_versioncalled_computationsbackend_configoperand_layoutsresult_layouts)r   r   )mesh_libthread_resourcesenvphysical_meshrx   ry   rz   r   r   device_assignmentAssertionErrorr{   r   _flat_devices_tupleNotImplementedErrorr   rb   get_replicated_hlo_shardingr    encode_inspect_sharding_callbackadd_keepaliver   CustomCallOpr   
StringAttrr   _INSPECT_SHARDING_CALL_NAMEBoolAttrr   i32_attr	ArrayAttr)r   r   r.   ry   r   keyr   r   s     `   @@r!   _inspect_sharding_lowering_ruler  _  s    
	"	&	4$#0,n<== 2,G
EG G G  , >?? 23GG
d<00
1
11				0 0 0 0 0 0 0 0 	\\Q >EGGHHHI
+,BCC# ""#9:::EJ<%$&M$5$50%2 %2#%;??4#8#8#}Q//')|'7'7';';"$-"3"3C"8"8#'"&( ( ( ( 
)r#   c                
    ~ ~g S rB   r   )rw   backend_strings     r!   #inspect_sharding_prop_user_shardingr	    s    	)r#   c                    ~ ~~|d         S r   r   )
arg_shapesarg_shardingsrG   r  s       r!   -inspect_sharding_infer_sharding_from_operandsr    s    %	q	r#   sizeintslcslicec                    |j         |j        cxk    rd k    rn ndS |j         |j        z
  }|j        |z  dk    sJ | |z  dk    sJ |j        |z  S r   )stopstart)r  r  
slice_sizes      r!   _slice_to_chunk_idxr    sv    X""""d"""""1x#)#*	Z	1	$	$	$	$	
	a					j	  r#   slice | intc                V    t          | t                    rt          | | dz             S | S )Nrr   )rz   r  r  )r  s    r!   _raise_to_slicer    s-    S cAg	*r#   colorColorc                h    t          | t                    r| S d | D             \  }}}d|d|d|dS )Nc              3  :   K   | ]}t          |d z            V  dS )   Nr  rH   as     r!   rK   z&_canonicalize_color.<locals>.<genexpr>  s,      ))aSS\\))))))r#   #02X)rz   r   r  rgbs       r!   _canonicalize_colorr(    sS    s L))5)))'!Q	"Q	"	"A	"	"1	"	"	""r#   c                    t          d | dd         | dd         | dd         f          \  }}}|dz  |dz  z   |dz  z   d	k    rd
S dS )Nc                "    t          | d          S )N   r  )rI   s    r!   r"   z!_get_text_color.<locals>.<lambda>  s    #a** r#   rr            gA`"?gbX9?gv/?   z#000000z#ffffff)r\   r$  s       r!   _get_text_colorr0    se    $$uQqSz51:uQqSz&JKK'!Q%i!e)a%i'3..9	r#   c              #     K   ||z  }t          j        dd|          }d}t          |          D ]7} | ||                   V  ||dz  z   t          |dz  dk              z   |z  }8d S )Nr   rr      )nplinspacer[   r   )	color_mapnum_rowsnum_cols
num_colorscolor_valuesidxr>   s          r!   make_color_iterr;    s      ("*Q:..,	# K Ka
)L%
&
&&&&q 4
Q!(;#<#<<

JCCK Kr#   g      ?	   P   )	use_colorscale	min_width	max_widthr5  rG   Sequence[int]rw   r   r>  r?  floatr@  rA  r5  ColorMap | Nonec                  t           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k    st          |           dk     rt          d          |j                            |          }|o|j        du}|r'|s%	 ddl}	|	j        d	         }n# t           $ r d
}Y nw xY wt#          d|z            }
t          |           dk    r| d         nd| d         z  }t#          |
|z            }d}t%          t'          |j                            j                                        }|                    t1          |                     }i }i }i }t3          |                                          D ]V\  }\  }}|J t1          t7          t8          |                    }t1          t7          t:          | |                    }|t<          t          |          dk    rb|\  }}|j        |j        |j         z
  n| d         }|j        |j        |j         z
  n| d         }|| d         z  }|| d         z  }|||<   |||<   nL|\  }tC          ddd          }|j        |j        |j         z
  n| d         }dg|R }d||<   || d         z  ||<   |"                    |tG                                $                    |j%                   XtM          d |'                                D                       dz   }t          tQ          |'                                          d                   dk    rd}n.tM          d |'                                D                       dz   }tS          |||          }|j*        +                    d
| d| d
|s|j,        j-        nd          } t]          |          D ]}g }!t]          |          D ]}"| dd/                    d ta          |||"f                   D                       z   }#|||"f         |||"f         }%}$t#          |$|z  |z            }$|%d}&nt#          |%|
z            }&tc          tM          |$|          |          }$te          |$t          |#          z
  dz
  d          \  }'}(|'|(z   })te          |&dz
  d          \  }*}(|*|(z   }+|rHtg          t%          |          dd                   },ti          |,          }-|*dz  }*|+dz  }+|'dz  }'|)dz  })nd},d}-|*|)|+|'f}.t1          d |.D                       }.|!5                    |j6        7                    |j8        9                    |#dd          |.|j:        ;                    |,|-                                | j<        |!  |=                    | d           dS )z)Visualizes a ``Sharding`` using ``rich``.richz5`visualize_sharding` requires `rich` to be installed.r   Nr2  rr   zC`visualize_sharding` only works for shapes with 1 and 2 dimensions.)widthtab20bF
   g      @c              3  &   K   | ]}|d          V  dS r   Nr   r   s     r!   rK   z%visualize_sharding.<locals>.<genexpr>  s&      --!1------r#   c              3  &   K   | ]}|d          V  dS )rr   Nr   r   s     r!   rK   z%visualize_sharding.<locals>.<genexpr>  s&      //A1Q4//////r#   )show_header
show_linespadding	highlightpad_edgebox ,c                ,    g | ]}t          |          S r   )r   )rH   ss     r!   rX   z&visualize_sharding.<locals>.<listcomp>  s    )O)O)OQ#a&&)O)O)Or#   r,  c              3  6   K   | ]}t          |d           V  dS rK  )max)rH   rI   s     r!   rK   z%visualize_sharding.<locals>.<genexpr>'  s*      11Ac!Qii111111r#   centermiddle)vertical)bgcolorr  )stylez

)end)>	importlibr   	find_specrn   
rich.alignrich.consolerich.boxrich.padding
rich.style
rich.tablerb   consoleConsolecolor_system
matplotlib	colormapsModuleNotFoundErrorr  rY   iter
device_setr   upperdevices_indices_maptupler   itemsr\   r  r  r   r  r  r  
setdefaultr|   addidrX  keysr   r;  tableTablerR  SQUAREr[   joinsortedmindivmodr(  r0  r_   rO  PaddingalignAlignr]  Styleadd_rowprint)/rG   rw   r>  r?  r@  rA  r5  rF  rg  mplbase_heightaspect_ratio
base_widthheight_to_width_ratiodevice_kinddevice_indices_mapslicesheightswidthsrJ   devslcs
chunk_idxsverthoriz	vert_size
horiz_sizechunk_heightchunk_widthr6  r7  
color_iterrw  coljentryrG  maybe_heightheightleft_padding	remainderright_paddingtop_paddingbottom_paddingr  
text_colorrO  s/                                                  r!   visualize_shardingr    s   
 
	!	!&	)	) N
L
M
MM /// 	ZZ!^^s5zzA~~
MO O OL  y 11'<G0<) y -)ii   iii BJ+!%jjAoo%((1a@,;-..* T(-..//8>>@@+33E%LLAA,.&13')+&!"4":":"<"<== 5 5na#t_d++,,Ds.t<<==J|
4yyA~~kdE26)2HTY$*,,q 27*2HUZ%+--q q)lq)k(gj&fZ fe1ad(-
(>5:##E!H #
##j gj&q1fZ
j#%%((,,SV4444--v{{}}-----1(fkkmm		Q	  A%%HH///////!3Hy(H==*
*

uY#$)2]U6?ItxT  K K% ??    a
C8__ " ")O)O&1:N:N)O)O)O P PPe"1a4L'!Q$-\e%*$'<<==e		\K/00#eY''33e &us5zz'9A'=q A AlI"Y.m%fqj!44k9"Y.n	 	#D$4$4RaR$899$U++
q!
m^\Jg1111111g	jj
,

JUHx@@'*""5 #      ! !" " " "
 EM3	--6-"""""s   'B9 9CCCallable[[Sharding], None]c               <    fd}t          j        ||            dS )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 ``pjit``-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 ``pjit``, the callback happens *at compile time* since the sharding is
  determined by XLA. You can trigger the callback by using JAX's AOT API
  (``pjit(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 ``pjit``-ted computation:

  >>> import jax
  >>> import jax.numpy as jnp
  >>> from jax.experimental.pjit import pjit
  >>> 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 = pjit(f_, in_shardings=PartitionSpec('dev'),
  ...          out_shardings=PartitionSpec('dev'))
  >>> with Mesh(jax.devices(), ('dev',)):
  ...   f.lower(x).compile()  # doctest: +SKIP
  ...
  NamedSharding(mesh={'dev': 8}, partition_spec=PartitionSpec(('dev',),))
  c                @    t                               |            d S r   r   )valr.   s    r!   _inspectz(inspect_array_sharding.<locals>._inspect^  s"    C(33333r#   N)r   tree_map)r   r.   r  s    ` r!   inspect_array_shardingr  0  s6    \4 4 4 4 4Xu%%%%%r#   c                8      fd}t           |           dS )zVisualizes an array's sharding.c                *    t          j        | fi S rB   )r  rG   )rw   arrr   s    r!   
_visualizez,visualize_array_sharding.<locals>._visualized  s    ci<<V<<<r#   r   N)r  )r  r   r  s   `` r!   visualize_array_shardingr  b  s;    = = = = = =z222222r#   )r.   r/   r0   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   )rG   rB  rw   r   r>  r   r?  rC  r@  r  rA  r  r5  rD  )r.   r  )t__doc__
__future__r   importlib.utilr_  collections.abcr   r   r]   loggingstringr   typingr   r   weakrefnumpyr3  r4   	jax.numpyrU   r   jax._srcr	   r
   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.shardingr   jax._src.sharding_implsr   r   	getLoggerr%   r:   Effectr   r   r*   r   r   add_typelowerable_effectscontrol_flow_allowed_effectsremat_allowed_effects"custom_derivatives_allowed_effects	Primitiver`   multiple_resultssafe_mapr\   
unsafe_mapdef_implr@   def_effectful_abstract_evalrD   rh   primitive_batchersrl   primitive_jvpsrp   primitive_transposesr   register_loweringr   partial_eval_jaxpr_custom_rulesr-   	Formatterr   r   r   r   r   %prim_requires_devices_during_loweringrt  r   r   r   r   WeakValueDictionarysharding_callbacksr  r   r  r	  r  r  r  rq  rC  r   r  ColorMapr(  r0  r;  r  r  r  r   r#   r!   <module>r     s   E D " " " " " "     . . . . . . . .       



              



                         % % % % % % # # # # # #                   $ $ $ $ $ $ * * * * * * & & & & & & 4 4 4 4 4 4 ) ) ) ) ) )             * * * * * * & & & & & & L L L L L L L L		8	$	$! ! ! ! !'. ! ! !{}}( ( ( ( ( ( ( ())++       !3 4 4 4   " "; / / /   " "#5 6 6 6  $ - -k : : :  $ - -.@ A A A   & &{ 3 3 3   & &'9 : : :  * 3 3K @ @ @  * 3 34F G G G "4>"233 $(  !-Z   ( -   .-
     " 1M , -7 7 7&= " #E E E E -J ( )' ' 'P  ')@ %' ' ' '  -? ? ? ?  -? ? ? ?  : (  "#3 4 $)-L -L -L -L -L -L^L L L L Lv/ L L L %$&&	7 7 7 7 27 $, $, $, $, $, $,R $T^$677 &*  #  . 2 23E F F F     2 3 3 3    . ./N O O O  
 $ 	 . /9 9 9(B $ %0W022 / ) ) ) ) ) ) ) )
. . . .^  )+J K K K    
! ! ! !   
 	eE5%'(#-.UGU5%#=>>?# # # #   K K K *.b()B48q# q# q# q# q# q#f0& 0& 0& 0&d3 3 3 3 3r#   