
    ukil                     f   d dl mZ d dlmZmZ d dlZd dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZ eedf   Zej0                  Zeedf   Zee   Z e	d	d
      	 	 	 	 	 	 dd       Z e	d	d
      	 	 	 	 	 	 dd       Z e	d	d
      dd       Z eej>                         G d d             Zy)    )annotations)MappingSequenceN)safe_zipuse_cpp_classcache)
xla_bridge)
xla_client)are_hlo_shardings_equalget_num_ways_dim_shardedis_hlo_sharding_replicatedop_sharding_to_indices.i   F)max_sizetrace_context_in_keyShardingc                    | j                  |      }| j                  r|S | j                  j                  D ci c]  }|||   
 c}S c c}w N)devices_indices_mapis_fully_addressable_internal_device_listaddressable_device_list)shardingglobal_shape
global_mapds       L/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/sharding.py _addressable_devices_indices_mapr   !   s\     ++L9*""11II
K Z]
 
K K 
Ks   Ac                t   | j                  |       | j                  t        |            }t        j                  j
                  j                  |j                         v s|j                         rt        d      t        ||t        | j                              }t        t        | j                  |            S )Nzidevice_indices_map doesn't work with unreduced. Please file a bug at https://github.com/jax-ml/jax/issues)shard_shape_to_xla_hlo_shardinglenxc
OpShardingType	UNREDUCEDsubgroup_typesis_unreducedNotImplementedErrorr   _device_assignmentdictr   )sr   hlo_shardingindicess       r   common_devices_indices_mapr.   *   s     --''L(9:,mm""l&A&A&CC!
	01 1 #<#&q';';#<>'	hq++W5	66    c                   | j                  t        |            }t        |      r|S |j                         r|S t	        |      \  }}t        |      t        |      k(  sJ t        |      t        |      f       g }t        t        ||            D ]L  \  }\  }}	 t        ||      \  }	}
|
dk7  rt        d|  d| d| d| d| d| d      |j                  |	       N t        |      S # t        $ r t        w xY w)	Nr   z	Sharding z implies that array axis z is partitioned z" times, but the dimension size is z (full shape: z , per-dimension tiling factors: z  should evenly divide the shape))r    r!   r   r'   r   	enumerater   divmod	TypeErrorr(   
ValueErrorappendtuple)selfr   r,   
partitions_outdimr+   pquotient	remainders              r   _common_shard_shaper?   9   s.   **3|+<=,- *<8-*a	ZC-	-SJ\AR/SS	-
#xjAB kc6Aq "1aLh	 A~dV4SE9IC1! 5& (++5, 7  JJx 
s    s   C++C;c                  >   e Zd ZdZedd       Zedd       Zedd       Zedd       Zedd       Z	ddZ
edd       Zedd	       Zdd
ZddZedd       Zej"                  dd       Z	 	 	 	 ddZddZedd       Zej"                  dd       Zd dZd!dZy)"r   zADescribes how a :class:`jax.Array` is laid out across devices.
  c                    t        d      )zThe set of devices that this :class:`Sharding` spans.

    In multi-controller JAX, the set of devices is global, i.e., includes
    non-addressable devices from other processes.
    (Subclasses should implement this method.r(   r7   s    r   
device_setzSharding.device_setZ        H
IIr/   c                    t        d      )zIs this sharding fully replicated?

    A sharding is fully replicated if each device has a complete copy of the
    entire data.
    rB   rC   rD   s    r   is_fully_replicatedzSharding.is_fully_replicatedc   rF   r/   c                    t        d      )zIs this sharding fully addressable?

    A sharding is fully addressable if the current process can address all of
    the devices named in the :class:`Sharding`. ``is_fully_addressable`` is
    equivalent to "is_local" in multi-process JAX.
    rB   rC   rD   s    r   r   zSharding.is_fully_addressablel   s     H
IIr/   c                    t        d      )z-Number of devices that the sharding contains.rB   rC   rD   s    r   num_deviceszSharding.num_devicesv        H
IIr/   c                    t        d      )z(Returns the memory kind of the sharding.rB   rC   rD   s    r   memory_kindzSharding.memory_kind{   rL   r/   c                    t        d      )z?Returns a new Sharding instance with the specified memory kind.z'Subclasses should implement this methodrC   )r7   kinds     r   with_memory_kindzSharding.with_memory_kind   s    
G
HHr/   c                    t        d      NrB   rC   rD   s    r   r)   zSharding._device_assignment       
H
IIr/   c                    t        d      rS   rC   rD   s    r   r   zSharding._internal_device_list   rT   r/   c                    t        d      rS   rC   r7   num_dimensionss     r   r    zSharding._to_xla_hlo_sharding       
H
IIr/   c                    t        d      rS   rC   rW   s     r   _to_sdy_shardingzSharding._to_sdy_sharding   rY   r/   c                     y)NT rD   s    r   _is_concretezSharding._is_concrete   s    r/   c                    t        j                         dk(  r| j                  S | j                  D ch c]+  }|j                  |j                  j                         k(  r|- c}S c c}w )zdThe set of devices in the :class:`Sharding` that are addressable by the
       current process.
       )xbprocess_countrE   process_indexclient)r7   r   s     r   addressable_deviceszSharding.addressable_devices   s[     
Q__ <!!(("8"8"::  < < <s   0A%c                    t        | |      S )zA mapping from addressable devices to the slice of array data each contains.

    ``addressable_devices_indices_map`` contains that part of
    ``device_indices_map`` that applies to the addressable devices.
    )r   r7   r   s     r   addressable_devices_indices_mapz(Sharding.addressable_devices_indices_map   s     ,D,??r/   c                    t        | |      S )zReturns a mapping from devices to the array slices each contains.

    The mapping includes all global devices, i.e., including
    non-addressable devices from other processes.
    )r.   rg   s     r   r   zSharding.devices_indices_map   s     &dL99r/   c                F    t        | j                  j                        dkD  S )Nr   )r!   r   r   rD   s    r   has_addressable_devicesz Sharding.has_addressable_devices   s    t))AABQFFr/   c                p    | j                   r| j                  S t        | j                  j                        S r   )r   r)   r6   r   r   rD   s    r   _addressable_device_assignmentz'Sharding._addressable_device_assignment   s.      $$$++CCDDr/   c                    t        | |      S )zReturns the shape of the data on each device.

    The shard shape returned by this function is calculated from
    ``global_shape`` and the properties of the sharding.
    )r?   rg   s     r   r   zSharding.shard_shape   s     t\22r/   c                    	 t        | j                  |      |j                  |            xr4 | j                  |j                  k(  xr | j                  |j                  k(  S # t        $ r | |k(  cY S w xY w)zReturns ``True`` if two shardings are equivalent.

    Two shardings are equivalent if they place the same logical array shards on
    the same devices.
    )r   r    r   rN   r(   )r7   otherndims      r   is_equivalent_tozSharding.is_equivalent_to   s    	%d&?&?&E%*%?%?%EG 4,,0K0KK4 %"3"335  U]s   AA" "A32A3N)returnzset[Device])rs   bool)rs   int)rs   z
str | None)rP   strrs   r   )rs   XLADeviceAssignment)rs   zxc.DeviceList)rX   ru   rs   zxc.HloSharding)rX   ru   )r   Shapers   Mapping[Device, Index | None])r   rx   rs   Mapping[Device, Index]r   rx   rs   rx   )r7   r   rp   r   rq   ru   rs   rt   )__name__
__module____qualname____doc__propertyrE   rH   r   rK   rN   rQ   r)   r   r    r[   r^   	functoolscached_propertyre   rh   r   rk   rm   r   rr   r]   r/   r   r   r   T   s:    J J J J J J J J J JI J J J JJJ   < <@@$A@: G G E E
3r/   )r   r   r   rx   rs   ry   )r+   r   r   rx   rs   rz   r{   ) 
__future__r   collections.abcr   r   r   jax._src.utilr   r   r   jax._srcr	   ra   jax._src.libr
   r"   jax._src.op_shardingsr   r   r   r   r6   ru   rx   DevicesliceIndexrw   r   r.   r?   r   r]   r/   r   <module>r      s   # -  8 8 % )8 8 	c3h	eSjv&  51KK&+K0MK 2K 5177$7)?7 27 51 24 r{{A A Ar/   