
    ukiR                       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ZddlZddlmZmZ ddlZddlmZ ddlmZ ddlmZmZmZ dd	lmZ eecZZej@                  j&                  Z!eZ"eZ#d
 Z$ G d de      Z% edd      d*d       Z& G d de	jN                        Z(d Z)d+dZ*d+dZ+ G d d      Z,d Z-i Z. G d de,ej^                        Z0 e% e0 ejb                  de2      d            Z3 G d dejh                        Z5 e5       Z6 ejn                  d        G d! d"             Z8 G d# d$e,      Z9d% Z: e9dd      Z; e0 ejb                  de2      d      Z< G d& d'      Z=d,d(Z>d-d)Z?y).z$Definitions of Mesh and AbstractMesh    )annotationsN)HashableSequence)Any
NamedTuple)config)
xla_bridge)safe_zipcachetuple_delete)
xla_clientc                D    dj                  t        d | D                    S )N, c              3  (   K   | ]
  }d | d   yw)`N .0as     H/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/mesh.py	<genexpr>zshow_axes.<locals>.<genexpr>+   s     1qAaS(1s   )joinsorted)axess    r   	show_axesr   *   s    	61D11	22    c                  j    e Zd ZU ded<   d
dZedd       Zedd       Zed        Zed        Z	d Z
y	)ResourceEnvMeshphysical_meshc                    t        |j                        | j                  t        | j                  j                        z
  z  }|rt	        dt        |             | j                  |      S )NzfCannot update the mesh of the current resource environment. The new mesh shadows already defined axes )r    )set
axis_namesresource_axesr    
ValueErrorr   _replace)selfmeshoverlaps      r   	with_meshzResourceEnv.with_mesh1   sj    $//"d&8&83t?Q?Q?\?\;]&]^G Q#G,-/ 0 0 ==t=,,r   c                @    t        | j                  j                        S N)r"   r    r#   r'   s    r   physical_resource_axesz"ResourceEnv.physical_resource_axes9   s    t!!,,--r   c                    | j                   S r,   )r.   r-   s    r   r$   zResourceEnv.resource_axes=   s    &&&r   c                .    | j                   j                  S r,   )r    shaper-   s    r   r1   zResourceEnv.shapeA   s    ###r   c                B    | j                   j                  j                  S r,   )r    
local_meshr1   r-   s    r   local_shapezResourceEnv.local_shapeE   s    ((...r   c                    dj                  d | j                  j                  j                         D              }d| dS )Nr   c              3  2   K   | ]  \  }}d | d|   yw'z': Nr   r   kvs      r   r   z'ResourceEnv.__repr__.<locals>.<genexpr>J   s'      D!Q!A3c!D   zResourceEnv(mesh=Mesh(z)))r   r    r1   items)r'   	mesh_reprs     r   __repr__zResourceEnv.__repr__I   sD    		 D"&"4"4":":"@"@"BD DI#I;b11r   N)r(   r   )returnzset[ResourceAxisName])__name__
__module____qualname____annotations__r*   propertyr.   r$   r1   r4   r?   r   r   r   r   r   .   sb    - . . ' ' $ $ / /2r   r      F)max_sizetrace_context_in_keyr   c                   | j                   r| S  t        j                  fdt        g      | j                        }g }t        | j                  j                        D ]  }t        t        t        | j                  j                              |      }|j                  |d      }t        j                  |      }t        t        j                  |            t        t        j                  |            }	}|j                  t        ||	dz                 t        |      }
||
   j!                         st#        d      t%        | j                  |
   | j&                        S )Nc                "    | j                   k(  S r,   )process_index)drK   s    r   <lambda>z!_get_local_mesh.<locals>.<lambda>T   s    =0 r   otypesF)keepdims   zWhen passing host local inputs to pjit, devices connected to a single host must form a contiguous subcube of the global device mesh)emptynp	vectorizebooldevicesrangendimr   tupleanyflatnonzerointminmaxappendsliceallr%   r   r#   )global_meshrK   is_local_devicesubcube_indicesaxis
other_axeslocal_slicesnonzero_indicesstartendsubcube_indices_tuples    `         r   _get_local_meshrl   O   s;   ABLL0$AALATATV//K'',,- 2deE+*=*=*B*B$CDdKJ #&&zE&BLnn\2ORVVO,-s266/3J/K3E5a012  0
 
.	/	3	3	5
	I  
k!!"78+:P:P	QQr   c                  x    e Zd Z ej                         Z ej                         Z ej                         Zd Zy)AxisTypec                    | j                   S r,   namer-   s    r   r?   zAxisType.__repr__q   s    99r   N)	rA   rB   rC   enumautoAutoExplicitManualr?   r   r   r   rn   rn   l   s.    	$TYY[(499;&r   rn   c                *   |t         j                  ft        |       z  n|}t        |t              s|f}t        d |D              s$t        d| d| dt	        d |D                     t        |       t        |      k7  rt        d|  d|       |S )Nc              3  <   K   | ]  }t        |t                y wr,   )
isinstancern   r   s     r   r   z(_normalize_axis_types.<locals>.<genexpr>z   s     9Z8$9s   zaxis_types passed to z. must be of type `jax.sharding.AxisType`. Got z	 of type c              3  2   K   | ]  }t        |        y wr,   )typer   s     r   r   z(_normalize_axis_types.<locals>.<genexpr>}   s     *Gq47*Gr<   zKNumber of axis names should match the number of axis_types. Got axis_names=z and axis_types=)rn   rt   lenry   rY   ra   	TypeErrorr%   )r#   
axis_typesrq   s      r   _normalize_axis_typesr   t   s    %  3z?2+5 	J	&J	9j9	9

v &|9U*GJ*G%G$H	JK K 	_J'
	!l"2:,	@A A 
r   c                2    | syt        fd| D              S )NFc              3  (   K   | ]	  }|k(    y wr,   r   r   ttys     r   r   z'all_axis_types_match.<locals>.<genexpr>        )Q"W)   )ra   r~   r   s    `r   all_axis_types_matchr          		)j)	))r   c                2    | syt        fd| D              S )NFc              3  (   K   | ]	  }|k(    y wr,   r   r   s     r   r   z'any_axis_types_match.<locals>.<genexpr>   r   r   )rZ   r   s    `r   any_axis_types_matchr      r   r   c                     e Zd ZU ded<   ded<   ded<   ej
                  dd       Zej
                  dd       Zej
                  dd	       Zej
                  dd
       Z	ej
                  dd       Z
ej
                  dd       Zej
                  dd       Zej
                  dd       Zej
                  dd       Zej
                  d        Zej
                  d        Zej
                  d        Zej
                  d        Zy)BaseMeshtuple[MeshAxisName, ...]r#   ztuple[tuple[str, int], ...]shape_tupleztuple[AxisType, ...]r~   c                J    t        | j                  t        j                        S r,   )r   r~   rn   rv   r-   s    r   are_all_axes_manualzBaseMesh.are_all_axes_manual       AAr   c                J    t        | j                  t        j                        S r,   )r   r~   rn   rt   r-   s    r   are_all_axes_autozBaseMesh.are_all_axes_auto       ??r   c                J    t        | j                  t        j                        S r,   )r   r~   rn   ru   r-   s    r   are_all_axes_explicitzBaseMesh.are_all_axes_explicit       1B1BCCr   c                T    | j                   syt        d | j                   D              S )NFc              3  l   K   | ],  }|t         j                  k(  xs |t         j                  k(   . y wr,   rn   rt   rv   r   r   s     r   r   z8BaseMesh._are_all_axes_auto_or_manual.<locals>.<genexpr>   2      ) HMM!9Q(//%99 )   24r~   ra   r-   s    r   _are_all_axes_auto_or_manualz%BaseMesh._are_all_axes_auto_or_manual   *    ?? )) ) )r   c                T    | j                   syt        d | j                   D              S )NFc              3  l   K   | ],  }|t         j                  k(  xs |t         j                  k(   . y wr,   )rn   ru   rv   r   s     r   r   z<BaseMesh._are_all_axes_explicit_or_manual.<locals>.<genexpr>   s4      ) H%%%=hoo)== )r   r   r-   s    r    _are_all_axes_explicit_or_manualz)BaseMesh._are_all_axes_explicit_or_manual   r   r   c                J    t        | j                  t        j                        S r,   )r   r~   rn   rv   r-   s    r   _any_axis_manualzBaseMesh._any_axis_manual   r   r   c                J    t        | j                  t        j                        S r,   )r   r~   rn   rt   r-   s    r   _any_axis_autozBaseMesh._any_axis_auto   r   r   c                J    t        | j                  t        j                        S r,   )r   r~   rn   ru   r-   s    r   _any_axis_explicitzBaseMesh._any_axis_explicit   r   r   c                T    | j                   syt        d | j                   D              S )NFc              3  l   K   | ],  }|t         j                  k(  xs |t         j                  k(   . y wr,   r   r   s     r   r   z4BaseMesh._any_axis_auto_or_manual.<locals>.<genexpr>   r   r   )r~   rZ   r-   s    r   _any_axis_auto_or_manualz!BaseMesh._any_axis_auto_or_manual   r   r   c                b    t        d t        | j                  | j                        D              S )Nc              3  L   K   | ]  \  }}|t         j                  k(  r|  y wr,   )rn   rt   r   nr   s      r   r   z%BaseMesh.auto_axes.<locals>.<genexpr>   s'      (tq!&  (   "$rY   r
   r#   r~   r-   s    r   	auto_axeszBaseMesh.auto_axes   s*     (xI ( ( (r   c                b    t        d t        | j                  | j                        D              S )Nc              3  L   K   | ]  \  }}|t         j                  k(  r|  y wr,   )rn   ru   r   s      r   r   z)BaseMesh.explicit_axes.<locals>.<genexpr>   s)      ,tq!***  ,r   r   r-   s    r   explicit_axeszBaseMesh.explicit_axes   s*     ,xI , , ,r   c                b    t        d t        | j                  | j                        D              S )Nc              3  L   K   | ]  \  }}|t         j                  k(  r|  y wr,   )rn   rv   r   s      r   r   z'BaseMesh.manual_axes.<locals>.<genexpr>   s'      *tq!(  *r   r   r-   s    r   manual_axeszBaseMesh.manual_axes   s*     *xI * * *r   c                T    t        t        | j                  | j                              S r,   )dictr
   r#   r~   r-   s    r   _name_to_typezBaseMesh._name_to_type   s    $//:;;r   N)r@   rU   )rA   rB   rC   rD   	functoolscached_propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sp   &&**""B B @ @ D D ) ) ) ) B B @ @ D D ) ) ( ( , , * * < <r   r   c                    t        | ||      S r,   )r   )rV   r#   r~   s      r   _unpicke_meshr      s    	gz:	..r   c                  T    e Zd ZU dZded<   ded<   	 d 	 	 	 d! fdZd Zd Zd	 Z fd
Z	d Z
d Zd"dZej                  d        Zej                  d        Zed#d       Zed        Zed        Zej                  d        Zed        Zd Zej                  d        Zej                  d        Zej                  d        Zej                  d        Zej                  d        Zd Zej                  d        Zd Zej                  d        Z ej                  d        Z! xZ"S )$r   ay  Declare the hardware resources available in the scope of this manager.

  See `Distributed arrays and automatic parallelization`_ and
  `Explicit Sharding`_ tutorials.

  Args:
    devices: A NumPy ndarray object containing JAX device objects (as
      obtained e.g. from :py:func:`jax.devices`).
    axis_names: A sequence of resource axis names to be assigned to the
      dimensions of the ``devices`` argument. Its length should match the
      rank of ``devices``.
    axis_types: and optional tuple of :class:`jax.sharding.AxisType` entries corresponding to
      the ``axis_names``. See `Explicit Sharding`_ for more information.

  Examples:

    >>> from jax.sharding import Mesh
    >>> from jax.sharding import PartitionSpec as P, NamedSharding
    >>> import numpy as np
    ...
    >>> # Declare a 2D mesh with axes `x` and `y`.
    >>> devices = np.array(jax.devices()).reshape(4, 2)
    >>> mesh = Mesh(devices, ('x', 'y'))
    >>> inp = np.arange(16).reshape(8, 2)
    >>> arr = jax.device_put(inp, NamedSharding(mesh, P('x', 'y')))
    >>> out = jax.jit(lambda x: x * 2)(arr)
    >>> assert out.sharding == NamedSharding(mesh, P('x', 'y'))

  .. _Distributed arrays and automatic parallelization: https://docs.jax.dev/en/latest/notebooks/Distributed_arrays_and_automatic_parallelization.html
  .. _Explicit Sharding:  https://docs.jax.dev/en/latest/notebooks/explicit-sharding.html
  z
np.ndarrayrV   r   r#   c                0   t        |t        j                        st        j                  |      }t        |t              r|f}t        |      }t        d |D              rt        d|       |j                  t        |      k7  r%t        d|j                   dt        |       d      t        ||d      }||j                  t        |j                        |f}t        j                  |d       }||S t        | A  |       }|j#                         |_        d|j$                  j&                  _        ||_        ||_        |j$                  j                  r-t/        j0                  |j                  j3                               nd|_        |t        |<   |S )	Nc              3  $   K   | ]  }|d u  
 y wr,   r   )r   is     r   r   zMesh.__new__.<locals>.<genexpr>  s     
)19
)s   z%Mesh axis names cannot be None. Got: zMesh requires the ndim of its first argument (`devices`) to equal the length of its second argument (`axis_names`), but got devices.ndim == z and len(axis_names) == .r   Fr   )ry   rS   ndarrayarraystrrY   rZ   r%   rX   r|   r   r1   flat_mesh_object_dictgetsuper__new__copyrV   flags	writeabler#   r~   mathprodvalues_size)clsrV   r#   r~   keyvalr'   	__class__s          r   r   zMesh.__new__   s[    grzz*!g*c"=jz"J

)j
))>zlKLL||s:&$\\N +  #J034 4 'z:vFJw}}eGLL&9:
FC


T
*C
j7?3D<<>DL#(DLL  DO DO37<<3D3D4::,,./!DJ!cKr   c                T    t         | j                  | j                  | j                  ffS r,   )r   rV   r#   r~   r-   s    r   
__reduce__zMesh.__reduce__!  s     DLL$//4??KLLr   c                *   | |u ryt        |t              sy| j                  |j                  k(  xrc | j                  j                  |j                  j                  k(  xr4 | j
                  |j
                  k(  xr | j                  |j                  k(  S NTF)ry   r   r#   rV   r1   r~   _internal_device_listr'   others     r   __eq__zMesh.__eq__$  s     u}eT"OOu/// FLL%--"5"55FOOu///F &&%*E*EEGr   c                    t        | d      sFt        | j                  | j                  | j                  j
                  | j                  f      | _        | j                  S )N_hash)hasattrhashr#   r   rV   r1   r~   r   r-   s    r   __hash__zMesh.__hash__0  sK    4!??D668J8J??dj ::r   c                |    t        | |      rt        | |      |k(  ry t        d| d      t        |   ||       y )NzCannot reassign attributes (z) of immutable mesh objects)r   getattrRuntimeErrorr   __setattr__)r'   rq   valuer   s      r   r   zMesh.__setattr__7  sL    tT	t		% 	(.I
J  
Ge$r   c                b   t         j                  j                  rt        d      t        j
                  d   j                  |       }t        j
                  j                  |       |t        _        t         j                  j                  t        d t        j
                  D                     | S )Nz!Mesh context manager is disabled.c              3  `   K   | ]&  }|j                   j                  s|j                    ( y wr,   r    rR   r   s     r   r   z!Mesh.__enter__.<locals>.<genexpr>I  (      ,!__** oo ,   ,.)
jax_configdisallow_mesh_context_managerr   r   thread_resourcesstackr*   r_   envmesh_context_manager	set_localrY   )r'   new_envs     r   	__enter__zMesh.__enter__B  s    //55<==$$R(2248G!!'*"##-- ,'7'='= , 	,- Kr   c                    t         j                  j                          t         j                  d   t         _        t        j
                  j                  t        d t         j                  D                     y)Nr   c              3  `   K   | ]&  }|j                   j                  s|j                    ( y wr,   r   r   s     r   r   z Mesh.__exit__.<locals>.<genexpr>Q  r   r   F)r   r   popr   r   r   r   rY   r'   exc_type	exc_value	tracebacks       r   __exit__zMesh.__exit__M  s\     +11"5##-- ,'7'='= , 	,- r   c                p    || j                   }|| j                  }|| j                  }t        |||      S r,   )rV   r#   r~   r   )r'   rV   r#   r~   s       r   updatezMesh.updateU  s=    g??j??jZ00r   c                    t        j                  d t        | j                  | j                  j
                        D              S )Nc              3  *   K   | ]  \  }}||f  y wr,   r   r   rq   sizes      r   r   zMesh.shape.<locals>.<genexpr>`  s"      #ID$ 
t#I   )collectionsOrderedDictr
   r#   rV   r1   r-   s    r   r1   z
Mesh.shape^  s;    "" #I"4??DLL4F4FG#I I Ir   c                v    t        d t        | j                  | j                  j                        D              S )Nc              3  *   K   | ]  \  }}||f  y wr,   r   r  s      r   r   z#Mesh.shape_tuple.<locals>.<genexpr>f  s"      ID$ 
tIr
  )rY   r
   r#   rV   r1   r-   s    r   r   zMesh.shape_tupled  s5     I"4??DLL4F4FGI I Ir   c                .    | j                   j                  S r,   )rV   r1   r-   s    r   
axis_sizeszMesh.axis_sizesj  s    <<r   c                    | j                   S r,   )r   r-   s    r   r	  z	Mesh.sizen  s    ::r   c                     | j                   dk(  S Nr   r	  r-   s    r   rR   z
Mesh.emptyr      99>r   c                Z    | j                   j                  t        | j                        k7  S r,   )rV   r	  r|   local_devicesr-   s    r   is_multi_processzMesh.is_multi_processv  s"    <<D$6$6 777r   c                H    | j                  t        j                               S r,   )_local_meshxbrK   r-   s    r   r3   zMesh.local_meshz  s    B,,.//r   c                    t        | |      S r,   )rl   )r'   rK   s     r   r  zMesh._local_mesh~  s    4//r   c                x    | j                   rJ  t        j                  d t        g      | j                        S )Nc                    | j                   S r,   )id)rL   s    r   rM   z!Mesh.device_ids.<locals>.<lambda>  s
    !$$ r   rN   )rR   rS   rT   r\   rV   r-   s    r   
device_idszMesh.device_ids  s,    zz>52<<u5dllCCr   c                ,    t        | j                        S r,   )r"   r  r-   s    r   _local_devices_setzMesh._local_devices_set  s    t!!""r   c                @    t        | j                  j                        S r,   )rY   rV   r   r-   s    r   _flat_devices_tuplezMesh._flat_devices_tuple  s    ""##r   c                @    t        j                  | j                        S r,   )xc
DeviceListr$  r-   s    r   r   zMesh._internal_device_list  s    ==1122r   c                @    t        | j                  j                        S r,   )r"   rV   r   r-   s    r   _flat_devices_setzMesh._flat_devices_set  s    t||  !!r   c                    | j                   rydj                  d | j                  j                         D              }d| j                   }d| | dS )NzMesh()r   c              3  2   K   | ]  \  }}d | d|   ywr7   r   r9   s      r   r   zMesh.__str__.<locals>.<genexpr>  s      F41a1QCs1#Fr<   , axis_types=zMesh())rR   r   r1   r=   r~   )r'   mesh_stratrs      r   __str__zMesh.__str__  sQ    zzyyF4::3C3C3EFFH$//*
+C8*SE##r   c                    | j                   ryd| j                   }d| j                  j                   d| j                  | dS )Nz"Mesh(axis_sizes=(), axis_names=())r,  zMesh(axis_sizes=z, axis_names=r-  )rR   r~   r   r1   r#   )r'   r/  s     r   _reprz
Mesh._repr  sN    zz1$//*
+Ct445 6//,SE4 5r   c                    | j                   S r,   r2  r-   s    r   r?   zMesh.__repr__      ::r   c                    | j                   j                  D cg c]+  }|j                  |j                  j                         k(  r|- c}S c c}w r,   )rV   r   rK   client)r'   rL   s     r   r  zMesh.local_devices  sD    ||(( <!!(("8"8"::  < < <s   0Ac                >   | j                   j                  d   }|d }nS|j                  dk(  rt        |dd       }n|j                  dk(  rt        |dd       }nd }t	        |j
                  |      }t        | j                  | j                  | j                  |      S )Nr   tpu	num_coresgpu
core_countdevice_kindr:  )r~   abstract_device)
rV   r   platformr   AbstractDevicer>  AbstractMeshr  r#   r~   )r'   rL   r?  r:  s       r   abstract_meshzMesh.abstract_mesh  s    !Ayo	
u	A{D1	::A|T2		&mmy:oT__') )r   r,   )rV   z np.ndarray | Sequence[xc.Device]r#   zstr | Sequence[MeshAxisName]r~   ztuple[AxisType, ...] | NoneNNN)r@   tuple[int, ...])#rA   rB   rC   __doc__rD   r   r   r   r   r   r   r  r  r   r   r1   r   rE   r  r	  rR   r  r3   r  r   r"  r$  r   r)  r0  r2  r?   r  rC  __classcell__)r   s   @r   r   r      s   @ && 9= 6 5 DM
G	%	1 I I
 I I
       8 8 0 00 D D # # $ $ 3 3 " "$ 5 5 < < ) )r   r   )dtypec                      e Zd Zd Zy)_ThreadResourcesLocalStatec                D    t         g| _        | j                  d   | _        y )Nr   )	EMPTY_ENVr   r   r-   s    r   __init__z#_ThreadResourcesLocalState.__init__  s    DJzz"~DHr   N)rA   rB   rC   rM  r   r   r   rJ  rJ    s    r   rJ  T)frozenc                  .    e Zd ZU ded<   ded<   d Zd Zy)rA  r   r>  z
int | Noner:  c                *    d| j                          dS )NzAbstractDevice(r-  r4  r-   s    r   r?   zAbstractDevice.__repr__  s    djjl^1-.r   c                :    d| j                    d| j                   S )Nzdevice_kind=z, num_cores=r=  r-   s    r   r2  zAbstractDevice._repr  s!    $**+<7GHHr   N)rA   rB   rC   rD   r?   r2  r   r   r   rA  rA    s    /Ir   rA  c                     e Zd ZdZ	 ddd	 ddZd Zd Zd ZddZe	j                  d	        Ze	j                  d
        Zed        Zed        Zed        ZddZed        Zed        Zed        Zed        Zed        Zd Zd Zy)rB  a  AbstractMesh contains only axis names and axis sizes.

  It does not contain concrete devices compared to `jax.sharding.Mesh`. You
  should use this as an input to the sharding passed to with_sharding_constraint
  and mesh passed to shard_map to avoid tracing and lowering cache misses when
  your mesh shape and axis names stay the same but the devices change.
  See the description of https://github.com/jax-ml/jax/pull/23022 for more
  details.

  Args:
    axis_sizes: A tuple of integers specifying the size of each resource axis.
    axis_names: A tuple of resource axis names to be assigned to the
      dimensions of the ``devices`` argument. Its length should match the
      rank of ``devices``.
    axis_types: and optional tuple of :class:`jax.sharding.AxisType` entries corresponding to
      the ``axis_names``. See `Explicit Sharding`_ for more information.

  .. _Explicit Sharding:  https://docs.jax.dev/en/latest/notebooks/explicit-sharding.html
  N)r?  c               B   || _         || _        t        | j                  |d      | _        || _        | j                   rt        j                  | j                         nd| _        t        | j                   | j                  | j                  | j                  f      | _	        y )NrB  r   )
r  r#   r   r~   r?  r   r   r	  r   r   )r'   r  r#   r~   r?  s        r   rM  zAbstractMesh.__init__  sy     !DO DO+^5DO*D.2oo		$//*1DIt++- .DJr   c                    | j                   S r,   )r   r-   s    r   r   zAbstractMesh.__hash__  r5  r   c                   | |u ryt        |t              sy| j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S r   )ry   rB  r  r#   r~   r?  r   s     r   r   zAbstractMesh.__eq__  sy    u}e\*OOu/// :OOu///:OOu///:   E$9$99;r   c                    | j                   r"dj                  d | j                   D              nd}d| j                   }| j                  dnd| j                  j	                          }d| | | dS )Nr   c              3  2   K   | ]  \  }}d | d|   ywr7   r   )r   r   r;   s      r   r   z(AbstractMesh.__repr__.<locals>.<genexpr>  s      FTQQqcQC=Fr<   z()r,   zAbstractMesh(r-  )r   r   r~   r?  r2  )r'   r>   r/  ads       r   r?   zAbstractMesh.__repr__  s    $$ FT5E5EFF*. $//*
+C$$,"t##))+,
- 9+cU2$a00r   c                    || j                   }|| j                  }|| j                  }d|vr| j                  |d<   t	        |||fi |S )Nr?  )r  r#   r~   r?  rB  )r'   r  r#   r~   kwargss        r   r  zAbstractMesh.update  s\    ??j??j??j&"&"6"6f
J
EfEEr   c                @    t        j                  | j                        S r,   )r  r  r   r-   s    r   r1   zAbstractMesh.shape  s    ""4#3#344r   c                b    t        d t        | j                  | j                        D              S )Nc              3  *   K   | ]  \  }}||f  y wr,   r   r  s      r   r   z+AbstractMesh.shape_tuple.<locals>.<genexpr>  s"      FD$ 
tFr
  )rY   r
   r#   r  r-   s    r   r   zAbstractMesh.shape_tuple  s/     F"4??DOODF F Fr   c                     y r,   r   r-   s    r   r   z"AbstractMesh._internal_device_list"  s    r   c                     | j                   dk(  S r  r  r-   s    r   rR   zAbstractMesh.empty&  r  r   c                    | S r,   r   r-   s    r   rC  zAbstractMesh.abstract_mesh*  s    Kr   c                    t        fdt        | j                  | j                        D              }| j	                  |      S )Nc              3  :   K   | ]  \  }}|v r|   n|  y wr,   r   )r   r   r   name_to_types      r   r   z1AbstractMesh.update_axis_types.<locals>.<genexpr>/  s/      N#q! /0<.?<?QF Ns   )r~   )rY   zipr#   r~   r  )r'   rd  new_axis_typess    ` r   update_axis_typeszAbstractMesh.update_axis_types.  s=     N'*4??DOO'LN NN;;.;11r   c                    t        d       y )NrV   _raise_value_errorr-   s    r   rV   zAbstractMesh.devices3  s
    y!r   c                    t        d       y )Nr   ri  r-   s    r   r   zAbstractMesh.device_ids7  
    |$r   c                    t        d       y )Nr  ri  r-   s    r   r  zAbstractMesh.is_multi_process;  s    )*r   c                    t        d       y )Nr  ri  r-   s    r   r  zAbstractMesh.local_devices?  s
    'r   c                    t        d       y )Nr3   ri  r-   s    r   r3   zAbstractMesh.local_meshC  rl  r   c                    t        d       y )Nr   ri  r-   s    r   r   zAbstractMesh.__enter__G  s
    {#r   c                    t        d       y )Nr  ri  r   s       r   r  zAbstractMesh.__exit__J  s
    z"r   r,   )r  rE  r#   ztuple[str, ...]r~   z&AxisType | tuple[AxisType, ...] | NonerD  )rd  zdict[MeshAxisName, AxisType])rA   rB   rC   rF  rM  r   r   r?   r  r   r   r1   r   rE   r   rR   rC  rg  rV   r   r  r  r3   r   r  r   r   r   rB  rB    s   * EI
."&
.A
.;1	F 5 5 F F
      2
 " " % % + + ( ( % %$#r   rB  c                    t        d|        )Nz AbstractMesh does not implement )r%   rp   s    r   rj  rj  P  s    5dV<==r   c                  (    e Zd ZddgZddZd Zd Zy)use_abstract_meshr(   prevc                `    t        |t              st        dt        |             || _        y )Nz=Expected mesh of type `jax.sharding.AbstractMesh`. Got type: )ry   rB  r%   r{   r(   )r'   r(   s     r   rM  zuse_abstract_mesh.__init__Y  s5    dL)4j\  DIr   c           
     H   t         j                  j                  | j                        | _        | j                  t
        j                  ur| j                  j                  s| j                  j                  s| j                  j                  | j                  j                  k7  r|t         j                  j                  | j                         t        d| j                   d| j                  j                   d| j                   d| j                  j                         y y y y )NzDuse_abstract_mesh cannot change the size of the mesh. Got new mesh: z with size=z and prev mesh: )r   abstract_mesh_context_manager
swap_localr(   ru  
config_extunsetrR   r	  r   r%   r-   s    r   r   zuse_abstract_mesh.__enter__`  s    88CCDIINDI		)))IIOODIIOO		$))..(..88Cii[DIINN#3 4ii[DIINN#356 6 	) %4O 	*r   c                V    t         j                  j                  | j                         y r,   )r   rx  r   ru  r   s       r   r  zuse_abstract_mesh.__exit__k  s    ,,66tyyAr   N)r(   rB  )rA   rB   rC   	__slots__rM  r   r  r   r   r   rt  rt  V  s    v)	6Br   rt  c                 J    t         j                  j                  } | t        S | S r,   )r   rx  r   empty_abstract_meshr   s    r   get_abstract_meshr  o  s#    0066# #	44r   c                 J    t         j                  j                  } | t        S | S r,   )r   device_contextr   empty_concrete_meshr  s    r   get_concrete_meshr  s  s#    !!''# #	44r   )rb   r   rK   r\   r@   r   )r   rn   r@   rU   )r@   rB  )r@   r   )@rF  
__future__r   r  collections.abcr   r   
contextlibdataclassesrr   r   r   	threadingtypingr   r   numpyrS   jax._srcr   r   r	   r  jax._src.utilr
   r   r   jax._src.libr   r&  re  
unsafe_zip_xlarz  MeshAxisNameResourceAxisNamer   r   rl   Enumrn   r   r   r   r   r   r   ContextDecoratorr   rR   objectrL  localrJ  r   	dataclassrA  rB  rj  r  r  rt  r  r  r   r   r   <module>r     s   + "  .       "  ) % 7 7 )CZWW^^
 32* 2B %0R 1R8tyy  *
*C< C<L/  b)8Z00 b)J XRXXb7<=	  ./  d#I I $It#8 t#r> #2r* 8288Bf5r: B B255r   