
    Vpf!                    t   d dl mZ d dlmZ d dlZd dlZd dlmZ d dlZ	d dl
mZ d dl
mZ d dlmZ eej        cZZej        Zej        Zej        Z e            Zej        Zej        Zeeef         Zej        ZddZd Zee_        ee_        eeee eeef         df         f         Z!ddZ"ddZ#	 	 dd dZ$dS )!    )annotations)SequenceN)Union)config)util)pmap_libshapetuple[int, ...]return
np.ndarrayc                   t          |          t          | j                  k    sJ || j        f            g }g }t          | j                  D ]a\  }}||         }t          |t                    r$|                    t          d          g           Gt          |t                    rW||j        k    sJ | d|j                     |                    t          |                     |                    |           t          |t                    rt          j        |j                  }t          ||          \  }|rJ |||f            |                    fdt          |          D                        |                    |j                   Mt!          j        |           ct%          j        t          j        |          gt$          j                  }	t          t+          j        |           D ]
\  }
}||	|
<   |	                    |          }	t          |          }d | j        D             }t3          |          t3          t          |                    k    rt          |          |k    sJ t5          d | j        D                       t+          j        d          t9          |          cfd| j        D             }t%          j        |	|	j        z                                 |          S )	a  Returns NumPy-style indices corresponding to a sharding spec.

  Args:
    shape: The shape of the logical array being sharded.

  Returns:
    An ndarray with the same shape as the logical mesh (as derived form
    `mesh_mapping`). Each entry is a NumPy-style index selecting the subset of
    the data array to be placed on a corresponding device. The indices can be
    ints, slice objects with step=1, or tuples of those.
  Nz != c                B    g | ]}t          |z  |d z   z            S )   )slice).0i
shard_sizes     W/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/sharding_specs.py
<listcomp>z*_sharding_spec_indices.<locals>.<listcomp>X   s@     9 9 9  !Z!a%:1EFF 9 9 9    )dtypec                F    g | ]}t          |t                    |j        S  
isinstanceShardedAxisaxisr   as     r   r   z*_sharding_spec_indices.<locals>.<listcomp>i   s)    VVV:a;U;UVafVVVr   c              3  N   K   | ] }t          |t                    |j        V  !d S N)r   
Replicatedreplicasr   s     r   	<genexpr>z)_sharding_spec_indices.<locals>.<genexpr>m   s3      [[qAzAZAZ[
[[[[[[r   r   c                    g | ]E}t          |t                    rt                    nt                    t                    z   FS r   )r   r"   nextlen)r   r   replica_dimreplica_sizessharded_dims     r   r   z*_sharding_spec_indices.<locals>.<listcomp>o   s]     
& 
& 
&  *!Z88 1${



m

tK00
0
& 
& 
&r   ) r'   sharding	enumerater   
NoShardingappendr   	UnstackedsizerangeChunkedmathprodchunksdivmodextendr   assert_unreachablenpemptyobject_	itertoolsproductreshapemesh_mappingsettuplecountiterbroadcast_tor	   	transpose)selfr	   axis_indicesshard_indices_shapedimr+   	axis_sizetotal_chunksraggedshard_indicesr   idxsnum_sharded_dimsharded_dim_permpermr(   r)   r   r*   s                  @@@@r   _sharding_spec_indicesrR   :   s<    
Us4=))	)	)	)E4=+A	)	)	)(*, // ( (mc8c
I(J'' (5;;-(((( 
Hi	(	( ((-'''I)J)J8=)J)J'''%	**+++  ++++	Hg	&	& (Yx//l!)\::j&77)\37777 9 9 9 9$),$7$79 9 9 : : :  1111
h'''' (DI&9::;2:NNN-9,l;<<  gaM!''(;<<- +,,/VVd&7VVV


3u_'='=#>#>
>
>


?
2
2
2
2[[D,=[[[[[-&_Q//6F1G1G+{
& 
& 
& 
& 
& 
&$
& 
& 
&$ /-9L)L
M
MYt__r   c                (    d| j          d| j         dS )NzShardingSpec(z, )r+   r?   )rF   s    r   _sharding_spec_reprrV   u   s     	>	>	>$*;	>	>	>>r   .Sequence[int]specShardingSpectuple[Index, ...]c                P    t          |                    |           j                  S )a  Returns numpy-style indices corresponding to a sharding spec.

  Each index describes a shard of the array. The order of the indices is the
  same as the device_buffers of a Array sharded using PmapSharding (i.e. the
  data is laid out row-major).

  Args:
    shape: The shape of the logical array being sharded.
    spec: Describes how the array is sharded and how the shards are assigned to
      the logical mesh.

  Returns:
    A tuple of length equal to the size of the mesh (inferred as the product of
    sharded dimension sizes and all replication factors).  Each element is an
    int, a slice object with step=1, or a tuple thereof, to be treated as an
    index into the full logical array.
  )rA   indicesflat)r	   rX   s     r   spec_to_indicesr^      s!    & 
t||E""'	(	((r   sharded_shapemap_axis
int | Nonec                  
 t          | |          \  }}|rJ t          t          gt          |          z  d          }|dk    rdnt	          |          f}|t          d |j        d|         D                       
d	
fd}t          j        j	        r*t          j        |j        |t          |g                    }	n(t          j        |j        |t          |                    }	t          |	t          j        t#          
          g|t%          ||j                                      S t          |j        t	          |          f|z   |j        z             S )
az  Sharding spec for arguments or results of a pmap.
  Args:
    nrep: number of local XLA replicas (product of local axis sizes)
    axis_size: local axis size for outer pmap
    sharded_aval: the aval of the value inside the outer pmap, an instance of
      a ShapedArray.
    map_axis: the axis along which the value is mapped in the outer pmap
  Returns:
    A ShardingSpec.
  r   rU   r   Nc              3  B   K   | ]}t          |t                     V  d S r!   )r   r-   )r   ss     r   r$   z%pmap_sharding_spec.<locals>.<genexpr>   s/      [[AjJ777[[[[[[r   r   MeshDimAssignmentc                v    t          | t                    r"| j        k    rt          | j        dz             S | S Nr   r   )r   sharded_in_axiss    r   shift_sharded_axisz.pmap_sharding_spec.<locals>.shift_sharded_axis   s:    	A{	#	# '/(A(A16A:&&&hr   )r   re   )r6   rY   _UNSHARDED_INSTANCEr'   r"   sumr+   r   pmap_no_rank_reductionvaluer   tuple_updater2   tuple_insertr/   r<   chainr   mapr?   )nreprJ   r_   r`   replication_factorrL   pspecmaybe_replicateri   r+   rh   s             @r   pmap_sharding_specrv      s     &dI66f
!4 5M8J8J J$&( ( (%,11BB
CU8V8V7X/[[PYQYPYAZ[[[[[O      $* :"
.(GYK$8$8: :hh "
.(Ii$8$8: :h?''
(/
 %"4
5
57 78 8 8 8 ~y))+o=@RRT T T Tr   r*   intsharded_dim_sizec                    |Ht           j        j        rt          j        | |d          }nt          j        | |          }|| |         }n|J | }t          ||||          S rg   )r   rl   rm   r   rn   tuple_deleterv   )r	   r*   rx   r_   s       r   create_pmap_sharding_specr{      s~    $* <'{A>>mm'{;;m{+'''M	,.>'
) 
) )r   )r	   r
   r   r   )r	   rW   rX   rY   r   rZ   )r_   rW   r`   ra   r   rY   )r   N)r	   r
   r*   rw   rx   ra   )%
__future__r   collections.abcr   r<   r3   typingr   numpyr9   jax._srcr   r   jax._src.libr   rq   safe_map
unsafe_mapr-   r2   r/   rj   r   r"   re   rY   rR   rV   r\   __repr__rw   r   rA   Indexr^   rv   r{   r   r   r   <module>r      s  < # " " " " " $ $ $ $ $ $                            ! ! ! ! ! !t}
C 


	 jll " 
+z12 $9 9 9 9v? ? ? . +  	c5%c5j 13 6778) ) ) ),'T 'T 'T 'TT JK=A) ) ) ) ) ) )r   