
    Vpf                        d Z ddlmZ ddlmZ ddlZddlmZ ddlZ	ddl
mZ ddZddZddZeeeeeeef         df         f         Zd dZd!dZdS )"zSharding utilities    )annotations)SequenceN)Union)
xla_clienthlo_shardingxc.HloShardingreturntuple[list[int], int]c                V   |                                  rg dfS |                                 }|                                 }|t          j        j        j        gk    rd}n%|                                 }|rt          d          d}|r|d         }|d d         }t          |          |fS )N   Tz4Unhandled OpSharding type. Please open a bug report!)
is_replicatedtile_assignment_dimensionssubgroup_typesxc
OpShardingType
REPLICATEDreplicate_on_last_tile_dimNotImplementedErrorlist)r   
partitionsr   r   num_replicass        U/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/op_shardings.pyget_num_ways_dim_shardedr      s    !! q5L6688*..00.*5666!%!-!H!H!J!J B
@B B B, !b>LCRCJ	j		<	''    opxc.OpSharding | xc.HloShardingboolc                    t          | t          j                  rt          j                            |           } |                                 dk    rdS |                                 S )Nr   T)
isinstancer   r   HloSharding
from_protonum_devicesr   )r   s    r   is_op_sharding_replicatedr%   0   sW    BM"" '		"	"2	&	&B^^4					r   op1op2c                ~   t          |           t          |          k    rdS t          |           rt          |          rdS t          | t          j                  rt          j                            |           n| }t          |t          j                  rt          j                            |          n|}||k    S )NT)idr%   r!   r   r   r"   r#   )r&   r'   hc1hc2s       r   are_op_shardings_equalr,   8   s    WW34s## (A#(F(F 4*4S"-*H*HQ!!#&&&c#*4S"-*H*HQ!!#&&&c#	r   .shapeSequence[int]r$   int
np.ndarrayc                   t          j        |t           j                  }t          |           r5|                    t          d           ft          |          z             |S ||                                 k    sJ t          |           \  }}t          |          t          |          k    s&J t          |          t          |          f            g }t          ||          D ]\  }}|dk    r$|
                    t          d           g           /|dk    rPt          ||          \  }	|	rJ ||f            |
                    fdt          |          D                        t          d          t          |                                           }
t!          t#          j        |           D ])\  }}t          |          D ]}||t'          |
          <   *|S )N)dtyper   c                B    g | ]}t          |z  |d z   z            S )r   )slice).0i
shard_sizes     r   
<listcomp>z0op_sharding_to_numpy_indices.<locals>.<listcomp>^   s@     5 5 5  !Z!a%:1EFF 5 5 5r   z1Unrecognized number of shards. Please file a bug!)npemptyobject_r%   fillr4   lenr$   r   zipappenddivmodrangeAssertionErroritertile_assignment_devices	enumerate	itertoolsproductnext)r   r-   r$   indicesr   r   axis_indicesdimn_shardsragged	device_itr6   idxs_r7   s                 @r   op_sharding_to_numpy_indicesrQ   F   s    H[
333'
 |,, LL%++#e**,---N	0022	2	2	2	25lCC*l	ZCJJ	&	&	&Z#e**(E	&	&	&)+,5*-- 	P 	Pmc81}}5;;-((((	A!#x00j&((#x((( 5 5 5 5$)(OO5 5 5 6 6 6 6 NOOO<7799::)9,l;<< & &ga<   & &!%gd9oo&	.r   op_shardingtuple[tuple[slice, ...], ...]c                L    t          | ||          }t          |j                  S )N)rQ   tupleflat)rR   r-   r$   rI   s       r   op_sharding_to_indicesrW   k   s%     )e[II'	w|		r   )r   r   r	   r
   )r   r   r	   r   )r&   r   r'   r   r	   r   )r   r   r-   r.   r$   r/   r	   r0   )rR   r   r-   r.   r$   r/   r	   rS   )__doc__
__future__r   collections.abcr   rF   typingr   numpyr9   jax._src.libr   r   r   r%   r,   r/   r4   rU   _IndexrQ   rW    r   r   <module>r`      s     " " " " " " $ $ $ $ $ $               ) ) ) ) ) )( ( ( (*       
sE5sEz!2C!788	9" " " "J     r   