
    uki                        d dl mZ d dlmZ d dlmZ d dlmZmZ ej                  Z
ej                  Zd Zd ZeZ eej                          G d d             Zd	e_        eZy
)    )annotations)Any)_jax)use_cpp_classuse_cpp_methodc                    t        | ||dS )N	unreducedreduced)PartitionSpec)
partitionsr
   r   s      R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/partition_spec.pyunpickle_pspecr      s    	
i	II    c                    | r|rdt        |       dt        |      S | r|sdt        |       S | s|rdt        |      S J )Nz
unreduced=z
, reduced=zreduced=)setr	   s     r   _get_ur_strr      sW    7I)CL3CDDI)**c'l%&&r   c                      e Zd ZdZdZeZ e        e        e       dd       Z	d Z
d Zd Zd Zd	 Z e       d
        Z e       d        Zd Zd Zd Zd Zd ZddZd Zy)r   ah  Tuple describing how to partition an array across a mesh of devices.

  Each element is either ``None``, a string, or a tuple of strings.
  See the documentation of :class:`jax.sharding.NamedSharding` for more details.

  This class exists so JAX's pytree utilities can distinguish a partition
  specifications from tuples that should be treated as pytrees.
  _partitionsr	   c               &   t        d |D              | _        t        |t        t        f      st        dt        |             t        |t        t        f      st        dt        |             t	        |      | _        t	        |      | _        y )Nc              3  2   K   | ]  }t        |        y wN_canonicalize_partition).0ps     r   	<genexpr>z)PartitionSpec.__init__.<locals>.<genexpr>8   s     LA4Q7L   zW`unreduced` argument of PartitionSpec should be of type `frozenset` or `set`. Got type zU`reduced` argument of PartitionSpec should be of type `frozenset` or `set`. Got type )	tupler   
isinstancer   	frozenset	TypeErrortyper
   r   )selfr
   r   r   s       r   __init__zPartitionSpec.__init__6   s    LLLDi#y!12--1)_,=?@ @ gY/0--1']O=> > y)DNW%DLr   c                    t        | j                        dd }| j                  s| j                  sd| dS t	        | j                  | j                        }|sdn|j                  d      r| dn| d}d| | dS )	N   zPartitionSpec() , z, )reprr   r
   r   r   endswith)r%   prur_strs      r   __repr__zPartitionSpec.__repr__G   s~    	d	"	%B>>$,,bT##6Fr{{3'7bT8t2YBRD*+r   c                T    t         | j                  | j                  | j                  ffS r   )r   r   r
   r   r%   s    r   
__reduce__zPartitionSpec.__reduce__O   s"    T--t~~t||LMMr   c                     | j                   |   S r   r   )r%   is     r   __getitem__zPartitionSpec.__getitem__R   s    Ar   c                ,    t        | j                        S r   )iterr   r4   s    r   __iter__zPartitionSpec.__iter__U   s      !!r   c                ,    t        | j                        S r   )lenr   r4   s    r   __len__zPartitionSpec.__len__X   s    t  r   c                   t        |t              rO| j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S t        |t
              r]| j                  rt        d| d|  d      | j                  rt        d| d|  d      t        d |D              }| j                  |k(  S y)Nother ) cannot be of instance `tuple` when self z, has unreduced in `__eq__` of PartitionSpec.z* has reduced in `__eq__` of PartitionSpec.c              3  2   K   | ]  }t        |        y wr   r   )r   os     r   r   z'PartitionSpec.__eq__.<locals>.<genexpr>j   s     @Q-a0@r   F)r!   r   r   r
   r   r    r#   )r%   otherother_ps      r   __eq__zPartitionSpec.__eq__[   s    %'%"3"33 ,nn/,llemm+- 
E5	!	UGDTF K7 78 	8 
UGDTF K5 56 	6 @%@@g((r   c                Z    t        | j                  | j                  | j                  f      S r   )hashr   r
   r   r4   s    r   __hash__zPartitionSpec.__hash__o   s"    !!4>>4<<@AAr   c                b   t        |t              rAt        g | |h | j                  |j                  h | j                  |j                  dS t        |t              rI| j                  rt        d| d|  d      | j                  rt        d| d|  d      t        g | | S t        )Nr	   r@   rA   z- has unreduced in `__add__` of PartitionSpec.z+ has reduced in `__add__` of PartitionSpec.)r!   r   r
   r   r    r#   NotImplementedErrorr%   rD   s     r   __add__zPartitionSpec.__add__s   s    %' 3337dnn7u71DLL15==13 3 
E5	!	UGDTF K8 89 	9 
UGDTF K6 67 	7 )D)5))r   c                    t        |t              st        | j                  rt	        d| d|  d      | j
                  rt	        d| d|  d      t        g ||  S )Nr@   rA   z. has unreduced in `__radd__` of PartitionSpec.z, has reduced in `__radd__` of PartitionSpec.)r!   r    rK   r
   r#   r   r   rL   s     r   __radd__zPartitionSpec.__radd__   s    eU#~~5'B4& I7 78 8 ||5'B4& I5 56 6 '%'$''r   c                J    | j                   j                  t        |            S r   )r   indexr   r%   values     r   rQ   zPartitionSpec.index       !!"9%"@AAr   c                J    | j                   j                  t        |            S r   )r   countr   rR   s     r   rV   zPartitionSpec.count   rT   r   c                    t        |j                  d| j                        |j                  d| j                        |j                  d| j                        dS )Nr   r
   r   r	   )r   popr   r
   r   )r%   kwargss     r   updatezPartitionSpec.update   sH    &**\43C3CD#)::k4>>#J!'It||!DF Fr   c                    | j                   D cg c]  }|t        u rd n| }}t        |      |k  r!|j                  d g|t        |      z
  z         | j	                  |      S c c}w )N)r   )r   _UNCONSTRAINED_PARTITIONr=   extendrZ   )r%   ndimr   outs       r   _normalized_spec_for_avalz'PartitionSpec._normalized_spec_for_aval   sn    $$& 004a7 &C &
3x$	jj$4#c(?+,;;#;&&	&s   A%c           
         t        |      t        | j                        k  rEt        |      dk(  rdnd}t        d|  dt        | j                         dt        |       d|       y )Nr   z, For scalars the PartitionSpec should be P()r+   zPartitionSpec z+ is only valid for values of rank at least z%, but was applied to a value of rank .)r=   r   
ValueError)r%   shape	extra_msgs      r   _check_compatible_wrt_shapez)PartitionSpec._check_compatible_wrt_shape   sv    
5zC(())%jAo B+- 4& K!!"##HZL)&' ' *r   N)r^   intreturnr   )__name__
__module____qualname____doc____match_args__r\   UNCONSTRAINEDr   r"   r&   r2   r5   r8   r;   r>   rF   rI   rM   rO   rQ   rV   rZ   r`   rf    r   r   r   r   '   s     $. +-,5K & & ,N"!  & B B &(BBF
''r   r   zjax.shardingN)
__future__r   typingr   jax._src.libr   jax._src.utilr   r   UNCONSTRAINED_PARTITIONr\   canonicalize_partitionr   r   r   AxisNamer   rj   Pro   r   r   <module>rx      sr    #   777 55 J t!!"E' E' #E'N * r   