
    ukiC                    "   d dl mZ d dlmZ d dlmZmZ d dlZd dl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 d dlmZmZmZ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* ee+cZ+Z,ee-cZ-Z. ed      Z/ej`                  Z1d6dZ2d7dZ3d8dZ4d8dZ5d8dZ6d8dZ7e
jp                  e$ejr                  fZ:d9dZ;d:dZ<d;dZ=ed<d       Z>ed=d       Z>ed>d       Z>ed?d       Z>ed@d       Z>dAd Z>dBd!Z?d"d#d$dCd%Z@dCd&ZAdCd'ZBeBZCdCd(ZDd6d)ZEd6d*ZFd6d+ZG ej                  d,-      d8d.       ZIdD	 dEd/ZJej                  dFd0       ZK	 	 dGd1ZL	 	 	 	 	 	 dHd2ZMe/dId3       ZNe/dJd4       ZOe/dDdKd5       ZPy)L    )annotations)Sequence)AnyoverloadN)api)config)core)dtypes)literals)lax)
xla_client)SingleDeviceSharding)safe_zipsafe_map
set_modulecanonicalize_axis_tuple)Sharding)NamedShardingPartitionSpeccanonicalize_sharding)Array	ArrayLikeDimSizeShapeSupportsNdimSupportsShapeSupportsSizez	jax.numpyc                   t        |      dk  r#|D cg c]  }t        j                  |       c}S |D cg c]  }t        j                  |       c}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 )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/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/numpy/util.py	<genexpr>z!promote_shapes.<locals>.<genexpr>4   s"     
83vay>SV#
8s   $'   Nallow)r"   r   asarraynpshapeallr   numpy_rank_promotionvalue _rank_promotion_warning_or_errorbroadcast_shapesbroadcast_to_rank)fun_nameargsargshpnonscalar_ranksresult_rankr&   s         @r'   promote_shapesr:   .   s   Y](,-CKK--'+,bhhsm,F

8VABZ
88*./3ckk#//+19CSs3x9O9
?a*./3ckk#//		$	$	*	*g	5(6:,,f56kAEF#c##C5FF .,/9/
 Gs(   EE	1EEE=E$E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   r/   r0   warningsr<   formatjoinmapstr
ValueError)r4   r&   msgs      r'   r1   r1   @   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                  |  \  }}| D cg c]  }t        j
                  |||       c}S c c}w c c}w )z=Convenience function to apply Numpy argument dtype promotion.r   )r"   r   r+   r
   lattice_result_type_convert_element_type)r5   r6   to_dtype	weak_typexs        r'   promote_dtypesrM   O   si     	Y](,-CKK-- 44d;HiGKL!C%%a9=LL . Ms   A,A1c                     t        j                  |  \  }}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.)r
   rH   to_inexact_dtyper   rI   )r5   rJ   rK   to_dtype_inexactrL   s        r'   promote_dtypes_inexactrQ   Y   Z     22D9(I,,X6
 
#
#A'7
C 
  
   Ac                     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
   rH   to_numeric_dtyper   rI   )r5   rJ   rK   to_dtype_numericrL   s        r'   promote_dtypes_numericrW   c   rR   rS   c                     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
   rH   to_complex_dtyper   rI   )r5   rJ   rK   to_dtype_complexrL   s        r'   promote_dtypes_complexr[   m   rR   rS   c                l    t        | t              xs# t        | d      xs t        j                  |       S N__jax_array__)
isinstance_arraylike_typeshasattrr,   isscalarrL   s    r'   
_arraylikerd   y   s1    
Q(
) 8
!_
%8)+Q9rF   c                d    t        | d      r| j                         } t        j                  |       S )z)Convert an array-like object to an array.r^   )ra   r^   r   r+   rc   s    r'   _arraylike_asarrayrf   ~   s&    Q 	A	QrF   c                >    t        | d      r| j                         S | S r]   )ra   r^   rc   s    r'   _check_jax_array_protocolrh      s    %a9	@q@rF   c                    y N )r4   s    r'   ensure_arraylikerl      s    58rF   c                    y rj   rk   )r4   a1s     r'   rl   rl      s    :=rF   c                    y rj   rk   )r4   rn   a2s      r'   rl   rl      s    QTrF   c                    y rj   rk   )r4   rn   rp   a3s       r'   rl   rl      s    adrF   c                    y rj   rk   )r4   rn   rp   rr   a4r5   s         r'   rl   rl      s    mprF   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rj   rf   r$   r6   s     r'   r(   z#ensure_arraylike.<locals>.<genexpr>   s     73!#&7   )check_arrayliker"   rf   tupler4   r5   s     r'   rl   rl      s:    ("T"Y!^d1g&&	7$7	77rF   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rj   rw   rx   s     r'   r(   z)ensure_arraylike_tuple.<locals>.<genexpr>   s     63!#&6ry   )rz   r{   )r4   tups     r'   ensure_arraylike_tupler      s"    
 (!S!	6#6	66rF   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rj   rd   rx   s     r'   r(   z"check_arraylike.<locals>.<genexpr>   s     -Z_	-s   c              3  @   K   | ]  \  }}t        |      s||f  y wrj   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)r_   rC   anynext	enumeratetyper?   r<   DeprecationWarning	TypeErrorr@   )r4   r   r   r5   posr6   rE   s          r'   rz   rz      s    	Hc	"P&EhZ$PP	"--- ,9T? , ,HCJA$s)MZ]Y^^_
`CmmCKK/JH cjj49c:;; .rF   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rj   r   rx   s     r'   r(   z*check_arraylike_or_none.<locals>.<genexpr>   s      >#jo,	->r   c              3  D   K   | ]  \  }}t        |      s|||f  y wrj   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 {}.)r_   rC   r   r   r   r   r@   r   )r4   r5   r   r6   rE   s        r'   check_arraylike_or_noner      sq    	Hc	"P&EhZ$PP	">>> =9T? = =HC
RC
CJJxcC8
99	 ?rF   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rj   )r
   dtypefloat0rx   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 =rF   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rj   )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   rC   rA   rB   )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rj   rh   rx   s     r'   r(   zpromote_args.<locals>.<genexpr>        >#(->ry   )rz   r{   _check_no_float0sr   r:   rM   r|   s     r'   promote_argsr      sN    ("T"	>>	>$H$t$X%%		9>4#8	99rF   c                    t        | g|  t        d |D              }t        | g|  t        | g|  t	        | gt        |  S )Nc              3  2   K   | ]  }t        |        y wrj   r   rx   s     r'   r(   z'promote_args_numeric.<locals>.<genexpr>   r   ry   )rz   r{   r   r   r:   rW   r|   s     r'   promote_args_numericr      sO    ("T"	>>	>$H$t$X%%		A#94#@	AArF   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rj   r   rx   s     r'   r(   z'promote_args_inexact.<locals>.<genexpr>   r   ry   )rz   r{   r   r   r:   rQ   r|   s     r'   promote_args_inexactr      sQ     ("T"	>>	>$H$t$X%%		A#94#@	AArF   T)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-   r.   r   r+   r2   broadcast_shardings_broadcast_to)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&|"t        j                  |       j                  |k(  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)rl   r_   r   r   r+   r{   r,   ndimr	   canonicalize_shaper-   r   typeofshardingr"   rD   r.   r   r@   broadcast_in_dimrange)arrr-   r   	arr_shapenlead
shape_tail
compatiblerE   s           r'   r   r      sO   -##u%3;;s+;#	E5	!bggen&9HE

!
!%
(%hhsm)
!!)U34;;s+44@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 7rF   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)rl   rD   r@   r,   r   _dtypebool_r   ne_zerorM   r   r   r	   is_empty_shaper-   select)	conditionrL   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 rj   )r_   xcDevicer   )devices    r'   canonicalize_device_to_shardingr   2  s     		"''	-rF   c                l    | |t        d| d|      | |t        |       S | |t        ||      S y )Nz>Only one of `device` or `out_sharding` can be set. Got device=z and out_sharding=)rD   r   r   )r   r   names      r'   choose_device_or_out_shardingr   8  se     L4

I& K?	  L0*622^0 t44	rF   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   r^   )ra   r   rz   r^   r,   r   s    r'   r   r   F  sH    D Q66M&!$/Q 	A	rF   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   r^   )ra   r-   rz   r^   r,   r   s    r'   r-   r-   r  sH    D Q77N'140Q 	A	!rF   c                    t        d| d       |t        | d      r| j                  S t        |       t	        |t              d      }t        j                  fd|D              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 or sequence of integers indicating which axis or axes to count
      elements along. ``None`` (the default) returns 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
    >>> jnp.size(y, axis=(1,))
    3
    >>> jnp.size(y, axis=(0, 1))
    6

    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
  sizeTr   F)allow_duplicatec              3  (   K   | ]	  }|     y wrj   rk   )r$   r   _shapes     r'   r(   zsize.<locals>.<genexpr>  s     +6!9+s   )rz   ra   r   r-   r   r"   mathprod)r   axisr   s     @r'   r   r     sY    V &!$/	\ga(66M8&	 s6{E	J$	+d+	++rF   )r4   rC   r5   r   returnlist[Array])r4   rC   r&   zSequence[Shape])r5   r   r   r   )rL   r   r   bool)rL   r   r   r   )rL   r   r   r   )r4   rC   r   z	tuple[()])r4   rC   rn   r   r   r   )r4   rC   rn   r   rp   r   r   ztuple[Array, Array])
r4   rC   rn   r   rp   r   rr   r   r   ztuple[Array, Array, Array])r4   rC   rn   r   rp   r   rr   r   rt   r   r5   r   r   tuple[Array, ...])r4   rC   r5   r   r   zArray | tuple[Array, ...])r4   rC   r   zSequence[Any]r   r   )r4   rC   r5   r   rj   )r   r   r-   zDimSize | Shaper   r   )r   r   rL   r   r   r   r   r   )r   xc.Device | Sharding | Noner   zSharding | None)r   r   r   zNamedSharding | P | Noner   rC   r   zSharding | NamedSharding | None)r   zArrayLike | SupportsNdimr   int)r   zArrayLike | SupportsShaper   ztuple[int, ...])r   z(ArrayLike | SupportsSize | SupportsShaper   zint | Sequence[int] | Noner   r   )Q
__future__r   collections.abcr   typingr   r   r   r?   numpyr,   jax._srcr   r   r	   r
   r   jax._src.laxr   jax._src.libr   r   jax._src.sharding_implsr   jax._src.utilr   r   r   r   jax._src.shardingr   r   r   Pr   jax._src.typingr   r   r   r   r   r   r   zip
unsafe_ziprB   
unsafe_mapexportr   r   r:   r1   rM   rQ   rW   r[   ndarrayTypedNdArrayr`   rd   rf   rh   rl   r   rz   r   r   r   r   r   r   r   jitr   r   r   r   r   r   r-   r   rk   rF   r'   <module>r     s   # $            ) 8 Q Q &< <Q Q Q CZCZ	K	 	G$GM JJx'<'<= 9
A 
 8 
 8	 = 
 =	 T 
 T	 d 
 d	 p 
 p87 =Ba <:
A % :BB 	M M77: O 	O()80H(+0O ( (V ( (V /, /,rF   