
    vhC                       d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	Z	d dl
mZ d dl
mZ d dl
mZ d d	l
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 d dlmZmZmZmZmZm Z m!Z! d dl"m#Z# d dl$Z%ee&cZ&Z'ee(cZ(Z) ed      Z* eejV                  d      Z,d6dZ-d7dZ.d8dZ/d8dZ0d8dZ1d8dZ2d9dZ3d:dZ4d;dZ5ed<d       Z6ed=d       Z6ed>d       Z6ed?d       Z6ed@d        Z6dAd!Z6dBd"Z7d#d$d%dCd&Z8dCd'Z9dCd(Z:e:Z;dCd)Z<d6d*Z=d6d+Z>d6d,Z?	 	 dDd-Z@ eej                  d.      d8d/       ZBdE	 dFd0ZCej                  dGd1       ZDdDd2ZEe*dHd3       ZFe*dId4       ZGe*dEdJd5       ZHy)K    )annotations)Sequence)partial)AnyoverloadN)api)config)core)dtypes)lax)
xla_client)SingleDeviceSharding)safe_zipsafe_map
set_module)Array	ArrayLikeDimSizeShapeSupportsNdimSupportsShapeSupportsSize)Shardingz	jax.numpyT)canonicalizec                z   t        |      dk  r#|D cg c]  }t        j                  |       c}S |D cg c]  }t        j                  |       c}t
        j                  j                  r;t        j                   }t        |      D cg c]  \  }}t        ||       c}}S t        fddd D              r#|D cg c]  }t        j                  |       c}S D ch c]  }|st        |       }}t        |      dk  r#|D cg c]  }t        j                  |       c}S t
        j                  j                  dk7  rt        |        t        t        j                         }|D cg c]  }t        j                  ||       c}S c c}w c c}w c c}}w c c}w c c}w c c}w c c}w )zHApply NumPy-style broadcasting, making args shape-compatible for lax.py.   c              3  R   K   | ]  }t        d          t        |      k(     ywr   N)len.0sshapess     N/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/numpy/util.py	<genexpr>z!promote_shapes.<locals>.<genexpr>8   s"     :!S^s1v%:s   $'   Nallow)r   r   asarraynpshaper	   dynamic_shapesvaluebroadcast_shapeszip_broadcast_toallnumpy_rank_promotion _rank_promotion_warning_or_errorbroadcast_to_rank)fun_nameargsarg	res_shapeshpnonscalar_ranksresult_rankr#   s          @r$   promote_shapesr;   +   sb   Y](,-CKK--'+,bhhsm,F"" &&/i<?f<MNSmC+NN	:vabz:	:,01SC 11-3;csS;o;	_		!,01SC 11&&,,7
*8V
<#..78CGHC%%c;7HH' ., O 2;1
 Is.   FFF#F))F.1F.F39F8c                v   t         j                  j                  dk(  rEd}t        j                  |j                  | dj                  t        t        |                         y t         j                  j                  dk(  r:d}t        |j                  | dj                  t        t        |                        y )NwarnzFollowing NumPy automatic rank promotion for {} on shapes {}. Set the jax_numpy_rank_promotion config option to 'allow' to disable this warning; for more information, see https://docs.jax.dev/en/latest/rank_promotion_warning.html. raisezOperands could not be broadcast together for {} on shapes {} and with the config option jax_numpy_rank_promotion='raise'. For more information, see https://docs.jax.dev/en/latest/rank_promotion_warning.html.)
r	   r1   r,   warningsr=   formatjoinmapstr
ValueError)r4   r#   msgs      r$   r2   r2   D   s      &&&0IC MM#**XsxxC0@'ABC""((G3IC SZZ#((3sF3C*DE
FF 4    c                    t        |       dk  r#| D cg c]  }t        j                  |       c}S t        j                  |  \  }}t        j
                  |d      }| D cg c]  }t        j                  |||       c}S c c}w c c}w )z=Convenience function to apply Numpy argument dtype promotion.r   Tallow_extended_dtype)r   r   r(   r   _lattice_result_typecanonicalize_dtype_convert_element_type)r5   r6   to_dtype	weak_typexs        r$   promote_dtypesrQ   S   s{     	Y](,-CKK-- 55t<Hi((MHGKL!C%%a9=LL	 . Ms   B"Bc                     t        j                  |  \  }}t        j                  |d      }t        j                  |      }| D cg c]  }t	        j
                  |||       c}S c c}w )zgConvenience function to apply Numpy argument dtype promotion.

  Promotes arguments to an inexact type.TrI   )r   rK   rL   to_inexact_dtyper   rM   )r5   rN   rO   to_dtype_inexactrP   s        r$   promote_dtypes_inexactrU   ^   sl     33T:(I&&xdK(,,X6
 
#
#A'7
C 
  
s   A'c                     t        j                  |  \  }}t        j                  |      }t        j                  |      }| D cg c]  }t	        j
                  |||       c}S c c}w )zqConvenience function to apply Numpy argument dtype promotion.

  Promotes arguments to a numeric (non-bool) type.)r   rK   rL   to_numeric_dtyper   rM   )r5   rN   rO   to_dtype_numericrP   s        r$   promote_dtypes_numericrY   i   j     33T:(I&&x0(,,X6
 
#
#A'7
C 
  
   A%c                     t        j                  |  \  }}t        j                  |      }t        j                  |      }| D cg c]  }t	        j
                  |||       c}S c c}w )zfConvenience function to apply Numpy argument dtype promotion.

  Promotes arguments to a complex type.)r   rK   rL   to_complex_dtyper   rM   )r5   rN   rO   to_dtype_complexrP   s        r$   promote_dtypes_complexr_   t   rZ   r[   c                    t        | t        j                        xs5 t        | t              xs# t	        | d      xs t        j
                  |       S N__jax_array__)
isinstancer)   ndarrayr   hasattrisscalarrP   s    r$   
_arraylikerh      sB    
Q


# 8z!U'; 8
!_
%8)+Q9rG   c                d    t        | d      r| j                         } t        j                  |       S )z)Convert an array-like object to an array.rb   )re   rb   r   r(   rg   s    r$   _arraylike_asarrayrj      s&    Q 	A	QrG   c                >    t        | d      r| j                         S | S ra   )re   rb   rg   s    r$   _check_jax_array_protocolrl      s    %a9	@q@rG   c                    y N )r4   s    r$   ensure_arraylikerp      s    58rG   c                    y rn   ro   )r4   a1s     r$   rp   rp      s    :=rG   c                    y rn   ro   )r4   rr   a2s      r$   rp   rp      s    QTrG   c                    y rn   ro   )r4   rr   rt   a3s       r$   rp   rp      s    adrG   c                    y rn   ro   )r4   rr   rt   rv   a4r5   s         r$   rp   rp      s    mprG   c               v    t        | g|  t        |      dk(  rt        |d         S t        d |D              S )z>Check that arguments are arraylike and convert them to arrays.r&   r   c              3  2   K   | ]  }t        |        y wrn   rj   r!   r6   s     r$   r%   z#ensure_arraylike.<locals>.<genexpr>   s     73!#&7   )check_arrayliker   rj   tupler4   r5   s     r$   rp   rp      s:    ("T"Y!^d1g&&	7$7	77rG   c                >    t        | g|  t        d |D              S )zCheck that argument elements are arraylike and convert to a tuple of arrays.

  This is useful because ensure_arraylike with a single argument returns a single array.
  c              3  2   K   | ]  }t        |        y wrn   r{   r|   s     r$   r%   z)ensure_arraylike_tuple.<locals>.<genexpr>   s     63!#&6r}   )r~   r   )r4   tups     r$   ensure_arraylike_tupler      s"    
 (!S!	6#6	66rG   F   )emit_warning
stacklevelc               N   t        | t              s
J d|         t        d |D              ryt        d t	        |      D              \  }}|  dt        |       d| d}|r t        j                  |dz   t        |       y	t        |j                  | t        |      |            y	)
z4Check if all args fit JAX's definition of arraylike.fun_name must be a string. Got c              3  4   K   | ]  }t        |         y wrn   rh   r|   s     r$   r%   z"check_arraylike.<locals>.<genexpr>   s     -Z_	-s   c              3  @   K   | ]  \  }}t        |      s||f  y wrn   r   r!   ir6   s      r$   r%   z"check_arraylike.<locals>.<genexpr>   s&      ,C%c? H ,   z+ requires ndarray or scalar arguments, got z at position .z/ In a future JAX release this will be an error.)categoryr   N)rc   rD   anynext	enumeratetyper@   r=   DeprecationWarning	TypeErrorrA   )r4   r   r   r5   posr6   rF   s          r$   r~   r~      s    	Hc	"P&EhZ$PP	"--- ,9T? , ,HCJA$s)MZ]Y^^_
`CmmCKK/JH cjj49c:;; .rG   c                    t        | t              s
J d|         t        d |D              rEt        d t	        |      D              \  }}d}t        |j                  | t        |      |            y )Nr   c              3  @   K   | ]  }t        |      xs |d u    y wrn   r   r|   s     r$   r%   z*check_arraylike_or_none.<locals>.<genexpr>   s      >#jo,	->r   c              3  D   K   | ]  \  }}t        |      s|||f  y wrn   r   r   s      r$   r%   z*check_arraylike_or_none.<locals>.<genexpr>   s*      =C&sOs{ H =s    zF{} requires ndarray, scalar, or None arguments, got {} at position {}.)rc   rD   r   r   r   r   rA   r   )r4   r5   r   r6   rF   s        r$   check_arraylike_or_noner      sq    	Hc	"P&EhZ$PP	">>> =9T? = =HC
RC
CJJxcC8
99	 ?rG   c                F    t        d |D              rt        d|  d      y)z,Check if none of the args have dtype float0.c              3  h   K   | ]*  }t        j                  |      t         j                  k(   , y wrn   )r   dtypefloat0r|   s     r$   r%   z#check_no_float0s.<locals>.<genexpr>   s"     <c	fmm	+<s   02zCalled a   with a float0 array. float0s do not support any operations by design because they are not compatible with non-trivial vector spaces. No implicit dtype conversion is done. You can use np.zeros_like(arr, dtype=np.float) to cast a float0 array to a regular zeros array. 
If you didn't expect to get a float0 you might have accidentally taken a gradient with respect to an integer argument.N)r   r   r   s     r$   check_no_float0sr      s7    <t<<

( @ 	@A A =rG   c           
     X   |D cg c]  }t        j                  |       }}t        t        |            dk  ryt	        d |D              rYt        |      dk(  rt        |  dt        |d          d      t        |  dd	j                  t        t        |             d      yc c}w )
zDCheck if args don't match and none of the args have typed prng dtyper   Nc              3  d   K   | ](  }t        j                  |t         j                         * y wrn   )r   
issubdtypeprng_key)r!   dts     r$   r%   z%check_for_prngkeys.<locals>.<genexpr>   s!     EB		2v	/Es   .0r&   z does not accept dtype r   r   z does not accept dtypes z, )	r   r   r   setr   r   rD   rB   rC   )r4   r5   r6   
arg_dtypess       r$   check_for_prngkeysr      s    -12cS!2*2ZA
E*EE
:!*+C
1,>+?qAC C *,TYYs3
7K-L,MQO  F 3s   B'c                    t        | g|  t        d |D              }t        | g|  t        | g|  t	        | gt        |  S )zGConvenience function to apply Numpy argument shape and dtype promotion.c              3  2   K   | ]  }t        |        y wrn   rl   r|   s     r$   r%   zpromote_args.<locals>.<genexpr>        >#(->r}   )r~   r   _check_no_float0sr   r;   rQ   r   s     r$   promote_argsr      sN    ("T"	>>	>$H$t$X%%		9>4#8	99rG   c                    t        | g|  t        d |D              }t        | g|  t        | g|  t	        | gt        |  S )Nc              3  2   K   | ]  }t        |        y wrn   r   r|   s     r$   r%   z'promote_args_numeric.<locals>.<genexpr>   r   r}   )r~   r   r   r   r;   rY   r   s     r$   promote_args_numericr      sO    ("T"	>>	>$H$t$X%%		A#94#@	AArG   c                    t        | g|  t        d |D              }t        | g|  t        | g|  t	        | gt        |  S )zyConvenience function to apply Numpy argument shape and dtype promotion.

  Promotes non-inexact types to an inexact type.c              3  2   K   | ]  }t        |        y wrn   r   r|   s     r$   r%   z'promote_args_inexact.<locals>.<genexpr>   r   r}   )r~   r   r   r   r;   rU   r   s     r$   promote_args_inexactr      sQ     ("T"	>>	>$H$t$X%%		A#94#@	AArG   c                P    t        | t        j                        rt        |       S | S rn   rc   xcDevicer   devices    r$   canonicalize_device_to_shardingr      s     		"''	-rG   )inlinec                    | D cg c]  }t        j                  |       }}|D cg c]  }|j                   c}rt        fdD              r#| D cg c]  }t	        j
                  |       c}S t	        j                   }t	        j                  | }| D cg c]  }t        |||       c}S c c}w c c}w c c}w c c}w )z7Like Numpy's broadcast_arrays but doesn't return views.c              3  P   K   | ]  }t        j                  d    |        ywr   )r
   definitely_equal_shaper    s     r$   r%   z$_broadcast_arrays.<locals>.<genexpr>  s"     QQt226!9a@Qs   #&)	r
   shaped_abstractifyr*   r0   r   r(   r-   broadcast_shardingsr/   )r5   r6   avalsaresult_shaperesult_shardingr#   s         @r$   _broadcast_arraysr     s     48
8C4""3'
8%
8"#AGG#&	3Q&QQ(,-CKK--%%v.,++U3/GK	L-\?
;	LL 9#- 
Ms   B6B;C Cc                   t        d|       } t        | t              r| nt        j                  |       } t        |t
              st        j                  |      dk(  r|f}t        j                  |      }t        j                  |       }t        j                  ||      r| S t        |      t        |      k  rt        d|d|      t        |      t        |      z
  }||d  }t        d t        ||      D              }|dk  s|sd}t        |j!                  ||            t        j"                  | |t        t%        |t        |                  |      S )Nbroadcast_tor   z;Cannot broadcast to shape with fewer dimensions: arr_shape=z shape=c              3  R   K   | ]  \  }}t        j                  |d |g       ! yw)r&   N)r
   definitely_equal_one_of_dim)r!   arr_dshape_ds      r$   r%   z _broadcast_to.<locals>.<genexpr>  s/      L' 55ea\J Ls   %'z?Incompatible shapes for broadcasting: {} and requested shape {})out_sharding)rp   rc   r   r   r(   r   r)   ndimr
   canonicalize_shaper*   r   r   rE   r0   r   rA   broadcast_in_dimrange)arrr*   sharding	arr_shapenlead
shape_tail
compatiblerF   s           r$   r/   r/     s3   -##u%3;;s+;#	E5	!bggen&9HE

!
!%
(%hhsm)	  E2J
5zC	N"
ST\V[U]^
__JY'EuvJ L+3Iz+JL LJqy
Mcszz)U344UE%s5z2J,K-57 7rG   c                (   t        d| ||      \  } }}||t        dj                  ||            t        j                  t        |       t        j                        s)t        j                  | t        j                  |             } t        ||      \  }}t        j                  |       dk(  rt        ||      \  }}nt        | ||      \  } }}	 t        j                  |j                        }|st        j                   | ||      S |S #  d}Y !xY w)NwherezeEither both or neither of the x and y arguments should be provided to jax.numpy.where, got {} and {}.r   F)rp   rE   rA   r)   r   _dtypebool_r   ne_zerorQ   r   r   r
   is_empty_shaper*   select)	conditionrP   yx_arry_arris_always_emptys         r$   _wherer   )  s    $WiA>/)QY!)
 FfQl$ $ 
vi("((	3y#))I"67I	1	$!QWWY1$Q*LE5/	1a@Iue))%++6O 5DIue	,NNOs   D Dc                P    t        | t        j                        rt        |       S | S rn   r   r   s    r$   normalize_device_to_shardingr   ?  s     		"''MrG   c                    t        | d      r| j                  S t        d| d       t        | d      r| j                         } t	        j                  |       S )a  Return the number of dimensions of an array.

  JAX implementation of :func:`numpy.ndim`. Unlike ``np.ndim``, this function
  raises a :class:`TypeError` if the input is a collection such as a list or
  tuple.

  Args:
    a: array-like object, or any object with an ``ndim`` attribute.

  Returns:
    An integer specifying the number of dimensions of ``a``.

  Examples:
    Number of dimensions for arrays:

    >>> x = jnp.arange(10)
    >>> jnp.ndim(x)
    1
    >>> y = jnp.ones((2, 3))
    >>> jnp.ndim(y)
    2

    This also works for scalars:

    >>> jnp.ndim(3.14)
    0

    For arrays, this can also be accessed via the :attr:`jax.Array.ndim` property:

    >>> x.ndim
    1
  r   Tr   rb   )re   r   r~   rb   r)   r   s    r$   r   r   F  sH    D Q66M&!$/Q 	A	rG   c                    t        | d      r| j                  S t        d| d       t        | d      r| j                         } t	        j                  |       S )a  Return the shape an array.

  JAX implementation of :func:`numpy.shape`. Unlike ``np.shape``, this function
  raises a :class:`TypeError` if the input is a collection such as a list or
  tuple.

  Args:
    a: array-like object, or any object with a ``shape`` attribute.

  Returns:
    An tuple of integers representing the shape of ``a``.

  Examples:
    Shape for arrays:

    >>> x = jnp.arange(10)
    >>> jnp.shape(x)
    (10,)
    >>> y = jnp.ones((2, 3))
    >>> jnp.shape(y)
    (2, 3)

    This also works for scalars:

    >>> jnp.shape(3.14)
    ()

    For arrays, this can also be accessed via the :attr:`jax.Array.shape` property:

    >>> x.shape
    (10,)
  r*   Tr   rb   )re   r*   r~   rb   r)   r   s    r$   r*   r*   r  sH    D Q77N'140Q 	A	!rG   c                    |t        | d      s|#t        | d      rt        j                  | |      S t        d| d       t        | d      r| j	                         } t        j                  | |      S )a  Return number of elements along a given axis.

  JAX implementation of :func:`numpy.size`. Unlike ``np.size``, this function
  raises a :class:`TypeError` if the input is a collection such as a list or
  tuple.

  Args:
    a: array-like object, or any object with a ``size`` attribute when ``axis`` is not
      specified, or with a ``shape`` attribute when ``axis`` is specified.
    axis: optional integer along which to count elements. By default, return
      the total number of elements.

  Returns:
    An integer specifying the number of elements in ``a``.

  Examples:
    Size for arrays:

    >>> x = jnp.arange(10)
    >>> jnp.size(x)
    10
    >>> y = jnp.ones((2, 3))
    >>> jnp.size(y)
    6
    >>> jnp.size(y, axis=1)
    3

    This also works for scalars:

    >>> jnp.size(3.14)
    1

    For arrays, this can also be accessed via the :attr:`jax.Array.size` property:

    >>> y.size
    6
  sizer*   )axisTr   rb   )re   r)   r   r~   rb   )r   r   s     r$   r   r     se    N lwq&)t/?GAwDW7714  &!$/Q 	A		rG   )r4   rD   r5   r   returnlist[Array])r4   rD   r#   zSequence[Shape])r5   r   r   r   )rP   r   r   bool)rP   r   r   r   )rP   r   r   r   )r4   rD   r   z	tuple[()])r4   rD   rr   r   r   r   )r4   rD   rr   r   rt   r   r   ztuple[Array, Array])
r4   rD   rr   r   rt   r   rv   r   r   ztuple[Array, Array, Array])r4   rD   rr   r   rt   r   rv   r   rx   r   r5   r   r   tuple[Array, ...])r4   rD   r5   r   r   zArray | tuple[Array, ...])r4   rD   r   zSequence[Any]r   r   )r4   rD   r5   r   )r   zxc.Device | Sharding | Noner   zSharding | Nonern   )r   r   r*   zDimSize | Shaper   r   )r   r   rP   r   r   r   r   r   )r   zArrayLike | SupportsNdimr   int)r   zArrayLike | SupportsShaper   ztuple[int, ...])r   z(ArrayLike | SupportsSize | SupportsShaper   z
int | Noner   r   )I
__future__r   collections.abcr   	functoolsr   typingr   r   r@   jax._srcr   r	   r
   r   jax._src.laxr   jax._src.libr   r   jax._src.sharding_implsr   jax._src.utilr   r   r   jax._src.typingr   r   r   r   r   r   r   jax.shardingr   numpyr)   r.   
unsafe_ziprC   
unsafe_mapexportr   r   r;   r2   rQ   rU   rY   r_   rh   rj   rl   rp   r   r~   r   r   r   r   r   r   r   r   jitr   r/   r   r   r   r*   r   ro   rG   r$   <module>r     s   # $          ) 8 8 8Q Q Q ! CZCZ	K	 	D	1I2GM9
A 
 8 
 8	 = 
 =	 T 
 T	 d 
 d	 p 
 p87 =Ba <:
A % :BB)8 	M M778 O 	O* ( (V ( (V . .rG   