
    vh                    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_op_shardings_equalget_num_ways_dim_shardedis_op_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/opt/face_recognition/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                    | j                  |       | j                  t        |            }t        ||t        | j                              }t        t        | j                  |            S r   )shard_shape_to_xla_hlo_shardinglenr   _device_assignmentdictr   )sr   hlo_shardingindicess       r   common_devices_indices_mapr'   *   sY     --''L(9:,"<#&q';';#<>'	hq++W5	66    c                   | j                  t        |            }t        |      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   	enumerater   divmod	TypeErrorNotImplementedError
ValueErrorappendtuple)selfr   r%   
partitions_outdimr$   pquotient	remainders              r   _common_shard_shaper9   4   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dd	Zdd
Zedd       Zej                   dd       Z	 	 	 	 ddZ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-   r1   s    r   
device_setzSharding.device_setS        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.
    r<   r=   r>   s    r   is_fully_replicatedzSharding.is_fully_replicated\   r@   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.
    r<   r=   r>   s    r   r   zSharding.is_fully_addressablee   s     H
IIr(   c                    t        d      )z-Number of devices that the sharding contains.r<   r=   r>   s    r   num_deviceszSharding.num_deviceso        H
IIr(   c                    t        d      )z(Returns the memory kind of the sharding.r<   r=   r>   s    r   memory_kindzSharding.memory_kindt   rF   r(   c                    t        d      )z?Returns a new Sharding instance with the specified memory kind.z'Subclasses should implement this methodr=   )r1   kinds     r   with_memory_kindzSharding.with_memory_kindy   s    
G
HHr(   c                    t        d      Nr<   r=   r>   s    r   r"   zSharding._device_assignment}   s    
H
IIr(   c                    t        d      rM   r=   r1   num_dimensionss     r   r    zSharding._to_xla_hlo_sharding       
H
IIr(   c                    t        d      rM   r=   rO   s     r   _to_sdy_shardingzSharding._to_sdy_sharding   rQ   r(   c                     y)NT r>   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_countr?   process_indexclient)r1   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   r1   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'   r_   s     r   r   zSharding.devices_indices_map   s     &dL99r(   c                p    | j                   r| j                  S t        | j                  j                        S r   )r   r"   r0   r   r   r>   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.
    )r9   r_   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   rH   r-   )r1   otherndims      r   is_equivalent_tozSharding.is_equivalent_to   s    	$T%>%>t%D%*%?%?%EG 4,,0K0KK4 %"3"335  U]s   AA" "A32A3N)returnzset[Device])ri   bool)ri   int)ri   z
str | None)rJ   strri   r   )ri   XLADeviceAssignment)rP   rk   ri   zxc.HloSharding)rP   rk   )r   Shaperi   Mapping[Device, Index | None])r   rn   ri   Mapping[Device, Index]r   rn   ri   rn   )r1   r   rf   r   rg   rk   ri   rj   )__name__
__module____qualname____doc__propertyr?   rB   r   rE   rH   rK   r"   r    rS   rV   	functoolscached_propertyr]   r`   r   rc   r   rh   rU   r(   r   r   r   M   s    J J J J J J J J J JI J JJJ   < <@@$A@: E E
3r(   )r   r   r   rn   ri   ro   )r$   r   r   rn   ri   rp   rq   ) 
__future__r   collections.abcr   r   rw   jax._src.utilr   r   r   jax._srcr	   rY   jax._src.libr
   xcjax._src.op_shardingsr   r   r   r   r0   rk   rn   DevicesliceIndexrm   r   r'   r9   r   rU   r(   r   <module>r      s   # -  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0 r{{y y yr(   