
    Vpfw"                        d dl mZ d dlmZmZ d dl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 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j"         G d d                      Z#ej"         G 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)sdy)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     Q/var/www/html/nettyfy-visnx/env/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!   Mapping[Device, Index]c                    |                      |           |                     t          |                    }t          ||t          | j                            }t          t          | j        |                    S N)shard_shape_to_xla_hlo_shardinglenr   _device_assignmentdictr   )sr   hlo_shardingindicess       r   common_devices_indices_mapr8   /   sk    --''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   r6   
partitions_outdimr5   pquotient	remainders              r   _common_shard_shaperI   8   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U ded<   ded<   dZded<   dd
ZdS )SdyDimShardingzSequence[str]axesbool	is_closedNz
int | Nonepriorityr   sdy.DimensionShardingAttrc                x    t           j                            d | j        D             | j        | j                  S )WBuilds the attribute.

    NOTE: An MLIR context is required as a context manager.
    c                L    g | ]!}t           j                            |          "S r   )r   AxisRefAttrget)r   axiss     r   
<listcomp>z(SdyDimSharding.build.<locals>.<listcomp>]   s(    999t		T	"	"999r!   )rN   rO   )r   DimensionShardingAttrrU   rL   rN   rO   rA   s    r   buildzSdyDimSharding.buildW   sA    
 $((99ty999. )      r!   )r   rP   )__name__
__module____qualname____annotations__rO   rZ   r   r!   r   rK   rK   Q   sP         ///(           r!   rK   c                  ,    e Zd ZU ded<   ded<   d	dZdS )
SdyArrayShardingstr	mesh_namezSequence[SdyDimSharding]dimension_shardingsr   sdy.TensorShardingAttrc                j    t           j                            | j        d | j        D                       S )rR   c                6    g | ]}|                                 S r   )rZ   )r   dim_shardings     r   rW   z*SdyArraySharding.build.<locals>.<listcomp>n   s$    KKK,				KKKr!   )r   TensorShardingAttrrU   rb   rc   rY   s    r   rZ   zSdyArraySharding.buildg   s;    
 !%%KK$2JKKKM M Mr!   N)r   rd   )r[   r\   r]   r^   rZ   r   r!   r   r`   r`   b   sE         ...////M M M M M Mr!   r`   c                     e Zd Z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=   rY   s    r   
device_setzSharding.device_setw        H
I
IIr!   rM   c                     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   rY   s    r   is_fully_replicatedzSharding.is_fully_replicated   ro   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   rY   s    r   r(   zSharding.is_fully_addressable   s     H
I
IIr!   
str | Nonec                     t          d          )z(Returns the memory kind of the sharding.rl   rm   rY   s    r   memory_kindzSharding.memory_kind   s     H
I
IIr!   kindra   c                     t          d          )z?Returns a new Sharding instance with the specified memory kind.z'Subclasses should implement this methodrm   )rA   rv   s     r   with_memory_kindzSharding.with_memory_kind   s    
G
H
HHr!   XLADeviceAssignmentc                     t          d          Nrl   rm   rY   s    r   r3   zSharding._device_assignment   s    
H
I
IIr!   num_dimensionsintxc.HloShardingc                     t          d          r{   rm   rA   r|   s     r   r1   zSharding._to_xla_hlo_sharding       
H
I
IIr!   r`   c                     t          d          r{   rm   r   s     r   _to_sdy_shardingzSharding._to_sdy_sharding   r   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_countrn   rY   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,   rA   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.
    )r8   r   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#   r   s     r   	<genexpr>z:Sharding._addressable_device_assignment.<locals>.<genexpr>   sL       A AqOqx'='='?'??? ????A Ar!   )r(   r3   r)   r@   r   r*   rY   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.
    )rI   r   s     r   r0   zSharding.shard_shape   s     t\222r!   rA   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   ru   r=   )rA   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   rM   )r   rs   )rv   ra   r   r   )r   ry   )r|   r}   r   r~   )r|   r}   r   r`   )r   r   r   r   r   r   r   r-   r   r   r   r   )rA   r   r   r   r   r}   r   rM   )r[   r\   r]   __doc__propertyrn   rq   r(   ru   rx   r3   r1   r   	functoolscached_propertyr   r   r'   r   r0   r   r   r!   r   r   r   q   s         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   )&
__future__r   collections.abcr   r   dataclassesr   jax._src.utilr   r   r   jax._srcr	   r   jax._src.libr
   xcjax._src.lib.mlir.dialectsr   jax._src.op_shardingsr   r   r   r   r@   r}   r   DevicesliceIndexry   r,   r8   rI   	dataclassrK   r`   r   r   r!   r   <module>r      se   # " " " " " - - - - - - - -         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                  M M M M M M M M r{w w w w w w w w w wr!   