
    vh!                    H   d dl mZ d dlmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ  ed      Zesedk\  rej                  Zej                  Zn G d d	      Z e       Zd
 Zd Zd Zd Zd ZeZ G d d      Zesedk\  r  e	ej.                        e      Zes	 ee      Zyy)    )annotations)AnyTYPE_CHECKING)jaxlib_extension_version)_jax)use_cpp_classuse_cpp_method
set_modulezjax.sharding`  c                      e Zd Zd Zd Zy)UnconstrainedSingletonc                     y)NUNCONSTRAINED selfs    R/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/partition_spec.py__repr__zUnconstrainedSingleton.__repr__    s        c                    t         dfS )Nr   )_get_default_unconstrainedr   s    r   
__reduce__z!UnconstrainedSingleton.__reduce__#   s    ("--r   N)__name__
__module____qualname__r   r   r   r   r   r   r      s    .r   r   c                     t         S N)_UNCONSTRAINED_PARTITIONr   r   r   r   r   ,   s    ##r   c                    | sy | t         u rt         S t        | t        t        f      rt	        |       dk(  r| d   S t        |       S | S )N   r   )r   
isinstancetuplelistlen)	partitions    r   _canonicalize_partitionr&   /   sL    ,,%%)eT]+	Y1	|9r   c                   | D ]H  }t        |t              r|n|f}|D ],  }||v rt        d|  d|       ||v st        d|  d|        J ||z  rt        d|d|      d |v rt        d      d |v rt        d      y )	NzWpartitions cannot overlap with unreduced axes passed to PartitionSpec. Got partitions: z and unreduced axes: zUpartitions cannot overlap with reduced axes passed to PartitionSpec. Got partitions: z and reduced axes: zR`unreduced` and `reduced` argument to PartitionSpec cannot overlap. Got unreduced=
, reduced=zWunreduced cannot contain None. All elements in unreduced should refer to the mesh axes.zSreduced cannot contain None. All elements in reduced should refer to the mesh axes.)r!   r"   
ValueError)
partitions	unreducedreducedprs        r   _checkr/   :   s     !U#!!a 
!	>11; =  <11; = 
 7Kwj*+ + y    w    r   c                    t        | ||dS )Nr+   r,   )PartitionSpec)r*   r+   r,   s      r   unpickle_pspecr3   U   s    	
i	IIr   c                    | r|rdt        |       dt        |      S | r|sdt        |       S | s|rdt        |      S J )Nz
unreduced=r(   zreduced=)setr1   s     r   _get_ur_strr6   X   sW    7I)CL3CDDI)**c'l%&&r   c                      e Zd ZdZedk  r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y)r2   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.
  r   )_partitionsr+   r,   r8   r1   c               |   t        d |D              | _        t        |t        t        f      st        dt        |             t        |t        t        f      st        dt        |             t	        |      | _        t	        |      | _        t        | j                  | j                  | j                         y )Nc              3  2   K   | ]  }t        |        y wr   r&   ).0r-   s     r   	<genexpr>z)PartitionSpec.__init__.<locals>.<genexpr>u   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 )
r"   r8   r!   r5   	frozenset	TypeErrortyper+   r,   r/   )r   r+   r,   r*   s       r   __init__zPartitionSpec.__init__s   s    LLLDi#y!12--1)_,=?@ @ gY/0--1']O=> > y)DNW%DL
4T^^T\\:r   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 )	Nr    zPartitionSpec() , z, )reprr8   r+   r,   r6   endswith)r   prur_strs      r   r   zPartitionSpec.__repr__   s~    	d	"	%B>>$,,bT##6Fr{{3'7bT8t2YBRD*+r   c                T    t         | j                  | j                  | j                  ffS r   )r3   r8   r+   r,   r   s    r   r   zPartitionSpec.__reduce__   s"    T--t~~t||LMMr   c                     | j                   |   S r   r9   )r   is     r   __getitem__zPartitionSpec.__getitem__   s    Ar   c                ,    t        | j                        S r   )iterr8   r   s    r   __iter__zPartitionSpec.__iter__   s      !!r   c                ,    t        | j                        S r   )r$   r8   r   s    r   __len__zPartitionSpec.__len__   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>   s     @Q-a0@r?   F)r!   r2   r8   r+   r,   r"   rA   )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   )hashr8   r+   r,   r   s    r   __hash__zPartitionSpec.__hash__   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        )Nr1   rX   rY   z- has unreduced in `__add__` of PartitionSpec.z+ has reduced in `__add__` of PartitionSpec.)r!   r2   r+   r,   r"   rA   NotImplementedErrorr   r\   s     r   __add__zPartitionSpec.__add__   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 )NrX   rY   z. has unreduced in `__radd__` of PartitionSpec.z, has reduced in `__radd__` of PartitionSpec.)r!   r"   rc   r+   rA   r,   r2   rd   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   )r8   indexr&   r   values     r   ri   zPartitionSpec.index       !!"9%"@AAr   c                J    | j                   j                  t        |            S r   )r8   countr&   rj   s     r   rn   zPartitionSpec.count   rl   r   c                    t        |j                  d| j                        |j                  d| j                        |j                  d| j                        dS )Nr*   r+   r,   r1   )r2   popr8   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*   )r8   r   r$   extendrr   )r   ndimr-   outs       r   _normalized_spec_for_avalz'PartitionSpec._normalized_spec_for_aval   sn    $$& 004a7 &C &
3x$	jj$4#c(?+,;;#;&&	&s   A%N)ru   intreturnr2   )r   r   r   __doc__r   	__slots____match_args__r   r   r	   r@   rC   r   r   rQ   rT   rV   r^   ra   re   rg   ri   rn   rr   rw   r   r   r   r2   r2   c   s     #7I#. +-,5K ; ;,N"!  & B B &(BBF
'r   r2   N)
__future__r   typingr   r   jax._src.libr   r   jax._src.utilr   r	   r
   exportUNCONSTRAINED_PARTITIONr   canonicalize_partitionr&   r   r   r/   r3   r6   AxisNamer2   r   r   r   <module>r      s    # % 1  C C	N	# 1S8!99 77. . 45$	 6J |' |'~ 1S83- 2 23MB- '- r   