
    Wh                       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j        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dS )    )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shardingShardingglobal_shapeShapereturnMapping[Device, Index | None]c                    |                      |          | j        rS t          | d          rfd| j        j        D             S d                                 D             S )N_internal_device_listc                "    i | ]}||         S  r   ).0d
global_maps     W/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/jax/_src/sharding.py
<dictcomp>z4_addressable_devices_indices_map.<locals>.<dictcomp>(   s5     M M M z!} M M M    c                ^    i | ]*\  }}|j         |j                                         k    '||+S r   process_indexclient)r   r   inds      r   r   z4_addressable_devices_indices_map.<locals>.<dictcomp>*   sB     
: 
: 
:VQ_ 6 6 8 888 S888r    )devices_indices_mapis_fully_addressablehasattrr   addressable_device_listitems)r   r   r   s     @r    _addressable_devices_indices_mapr+   !   s     ++L99*" X.// MM M M M3KM M M M
: 
:z//11 
: 
: 
: :r    sMapping[Device, Index]c                    |                      |           |                     t          |                    }t          ||t          | j                            }t          t          | j        |                    S N)shard_shape_to_xla_hlo_shardinglenr   _device_assignmentdictr   )r,   r   hlo_shardingindicess       r   common_devices_indices_mapr7   -   sm     --''L(9(9::,"<#&q';#<#<> >'	hq+W55	6	66r    c                T   |                      t          |                    }t          |          r|S t          |          \  }}t          |          t          |          k    s&J t          |          t          |          f            g }t	          t          ||                    D ]o\  }\  }}	 t          ||          \  }	}
n# t          $ r t          w xY w|
dk    r"t          d|  d| d| d| d| d| d          |
                    |	           pt          |          S )	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))r1   r2   r   r   	enumerater   divmod	TypeErrorNotImplementedError
ValueErrorappendtuple)selfr   r5   
partitions_outdimr,   pquotient	remainders              r   _common_shard_shaperH   7   s~   **3|+<+<==,|,, *<88-*a	ZC--	-	-	-J\ARAR/S	-	-	-
#xjAABB  kc6Aq "1aLLh		        A~~d  S   23 &  ,6     JJx	ss   4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j        d#d            Zd+dZd,dZej        d(d            Zd-dZd.d!Zd"S )/r   zADescribes how a :class:`jax.Array` is laid out across devices.
  r   set[Device]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<   r@   s    r   
device_setzSharding.device_setV        H
I
IIr    boolc                     t          d          )zIs this sharding fully replicated?

    A sharding is fully replicated if each device has a complete copy of the
    entire data.
    rL   rM   rN   s    r   is_fully_replicatedzSharding.is_fully_replicated_   rP   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.
    rL   rM   rN   s    r   r'   zSharding.is_fully_addressableh   s     H
I
IIr    intc                     t          d          )z-Number of devices that the sharding contains.rL   rM   rN   s    r   num_deviceszSharding.num_devicesr        H
I
IIr    
str | Nonec                     t          d          )z(Returns the memory kind of the sharding.rL   rM   rN   s    r   memory_kindzSharding.memory_kindw   rX   r    kindstrc                     t          d          )z?Returns a new Sharding instance with the specified memory kind.z'Subclasses should implement this methodrM   )r@   r\   s     r   with_memory_kindzSharding.with_memory_kind|   s    
G
H
HHr    XLADeviceAssignmentc                     t          d          NrL   rM   rN   s    r   r3   zSharding._device_assignment   s    
H
I
IIr    num_dimensionsxc.HloShardingc                     t          d          rb   rM   r@   rc   s     r   r1   zSharding._to_xla_hlo_sharding       
H
I
IIr    c                     t          d          rb   rM   rf   s     r   _to_sdy_shardingzSharding._to_sdy_sharding   rg   r    c                `    t          j                    dk    r| j        S d | j        D             S )zdThe set of devices in the :class:`Sharding` that are addressable by the
       current process.
       c                V    h | ]&}|j         |j                                         k    $|'S r   r"   r   r   s     r   	<setcomp>z/Sharding.addressable_devices.<locals>.<setcomp>   s<     < < <!!("8"8":"::: :::r    )xbprocess_countrO   rN   s    r   addressable_deviceszSharding.addressable_devices   sB     
Q_< <t < < < <r    r   r   r   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+   r@   r   s     r   addressable_devices_indices_mapz(Sharding.addressable_devices_indices_map   s     ,D,???r    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.
    )r7   rs   s     r   r&   zSharding.devices_indices_map   s     &dL999r    c                    | j         r| j        S t          | d          rt          | j        j                  S t          d | j        D                       S )Nr   c              3  ^   K   | ](}|j         |j                                         k    $|V  )d S r/   r"   rm   s     r   	<genexpr>z:Sharding._addressable_device_assignment.<locals>.<genexpr>   sL       A AqOqx'='='?'??? ????A Ar    )r'   r3   r(   r?   r   r)   rN   s    r   _addressable_device_assignmentz'Sharding._addressable_device_assignment   sv      %$$t,-- G4-EFFF A AD3 A A A A A Ar    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.
    )rH   rs   s     r   r0   zSharding.shard_shape   s     t\222r    r@   otherndimc                    	 t          |                     |          |                    |                    o| j        |j        k    o| j        |j        k    S # t          $ r	 | |k    cY S w xY w)aC  Returns ``True`` if two shardings are equivalent.

    Two shardings are equivalent if they place the same logical array shards on
    the same devices.

    For example, a :class:`NamedSharding` may be equivalent
    to a :class:`PositionalSharding` if both place the same shards of the array
    on the same devices.
    )r   r1   r   r[   r<   )r@   r{   r|   s      r   is_equivalent_tozSharding.is_equivalent_to   s    	$T%>%>t%D%D%*%?%?%E%EG G 4,0KK4 %"335    U]s   AA A+*A+N)r   rJ   )r   rQ   )r   rU   )r   rY   )r\   r]   r   r   )r   r`   )rc   rU   r   rd   )rc   rU   )r   r   r   r   )r   r   r   r-   r   r   r   r   )r@   r   r{   r   r|   rU   r   rQ   )__name__
__module____qualname____doc__propertyrO   rS   r'   rW   r[   r_   r3   r1   ri   	functoolscached_propertyrq   rt   r&   ry   r0   r~   r   r    r   r   r   P   s         J J J 8J J J J 8J J J J 8J J J J 8J J J J 8JI I I I J J J 8JJ J J JJ J J J < < < <@ @ @ @: : : : A A A A3 3 3 3     r    )r   r   r   r   r   r   )r,   r   r   r   r   r-   r   ) 
__future__r   collections.abcr   r   r   jax._src.utilr   r   r   jax._srcr	   ro   jax._src.libr
   xcjax._src.op_shardingsr   r   r   r   r?   rU   r   DevicesliceIndexr`   r+   r7   rH   r   r   r    r   <module>r      s   # " " " " " - - - - - - - -     8 8 8 8 8 8 8 8 8 8 % % % % % % ) ) ) ) ) )            	c3h	eSjv&  5111	: 	: 	: 21	: 51117 7 7 217 5111   210 r{| | | | | | | | | |r    