Ë
    ßGìj£  ã                  ó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       úUC:\Users\Stabi\Downloads\virtual-tryon(1)\venv\Lib\site-packages\jax/_src/sharding.pyÚ _addressable_devices_indices_mapr   !   sa   € ð ×+Ñ+¨LÓ9€*Ø×"Ò"ØÐà×1Ñ1×IÒIó
KÙIˆað ˆZ˜‰]Ñ
ØIñ
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×';Ñ';Ó#<ó>€'ä	Œh�q×+Ñ+¨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�k€c‰6ˆAˆqð Ü" 1 a›LÑ€h�	ð �A‚~ÜØ�d�VÐ4°S°EÐ9IØˆCÐ1°!°ð 5Ø&˜ð (+Ø+5¨,ð 7ðóð ð ‡J�JˆxÕð Cô 
ˆ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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«      ‚)z·The 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«      ‚)z†Is 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«      ‚)zöIs 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}   ó   € ä
ÐHÓ
IÐIr(   c                ó   — t        d«      ‚rM   r=   r>   s    r   r   zSharding._internal_device_list�   rN   r(   c                ó   — t        d«      ‚rM   r=   ©r1   Únum_dimensionss     r   r    zSharding._to_xla_hlo_sharding…   ó   € Ü
ÐHÓ
IÐIr(   c                ó   — t        d«      ‚rM   r=   rQ   s     r   Ú_to_sdy_shardingzSharding._to_sdy_shardingˆ   rS   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 )zÔA 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 )z¶Returns 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'   ra   s     r   r   zSharding.devices_indices_map¦   s   € ô & d¨LÓ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 )z­Returns 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   ra   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)z�Returns ``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à×,Ñ,°×0KÑ0KÑKò4ð ×Ñ %×"3Ñ"3Ñ3ð5øô ò Ø�U‰]Òðús   ‚AA" Á"A3Á2A3N)Úreturnzset[Device])rk   Úbool)rk   Úint)rk   z
str | None)rJ   Ústrrk   r   )rk   ÚXLADeviceAssignment)rk   zxc.DeviceList)rR   rm   rk   zxc.HloSharding)rR   rm   )r   ÚShaperk   úMapping[Device, Index | None])r   rp   rk   úMapping[Device, Index]©r   rp   rk   rp   )r1   r   rh   r   ri   rm   rk   rl   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr?   rB   r   rE   rH   rK   r"   r   r    rU   rX   Ú	functoolsÚcached_propertyr_   rb   r   re   r   rj   rW   r(   r   r   r   M   s$  „ ñð òJó ðJð òJó ðJð òJó ðJð òJó ðJð òJó ðJóIð òJó ðJð òJó ðJóJóJð òó ðð ×Ñò<ó ð<ð@Øð@Ø$Aó@ó:ð ×ÑòEó ðEó
3ôr(   )r   r   r   rp   rk   rq   )r$   r   r   rp   rk   rr   rs   ) Ú
__future__r   Úcollections.abcr   r   ry   Újax._src.utilr   r   r   Újax._srcr	   r[   Újax._src.libr
   ÚxcÚjax._src.op_shardingsr   r   r   r   r0   rm   rp   ÚDeviceÚsliceÚIndexro   r   r'   r9   r   rW   r(   r   Ú<module>r…      s  ðõ #ç -Û ç 8Ñ 8Ý %Ý )÷ó ð 	ˆc�3ˆh‰€Ø	�‰€Øˆe�SˆjÑ€Ø˜vÑ&Ð ñ �¨5Ô1ðKØðKØ&+ðKØ0MòKó 2ðKñ �¨5Ô1ð7Øð7Ø$ð7Ø)?ò7ó 2ð7ñ �¨5Ô1òó 2ðñ0 ˆr�{‰{Ó÷}ð }ó ñ}r(   