
    ukiw              
       j   d dl Z d dlmZ d dl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 d dlmZmZ ed   Zdedz  defdZ	 d!dededz  deddfdZdefdZdefdZdeedf   deddfdZdeedf   dee   dee   dee   ddf
dZ ed   Z! G d d       Z"y)"    N)Literal)Sequence)config)dtypes)error_check)array_constructors)array_creation)	lax_numpy)ufuncs)
reductions)Array	ArrayLikenandivideoobcategoryreturnc                     | y| dk(  rt        d      | dk(  rt        d      | dk(  rt        j                  j                  dk(  S t        d|        )	zHCheck if the error checking behavior for the given category is disabled.Fr   z3nan is deprecated. Use `_set_error_if_nan` instead.r   zAdivide is deprecated. Use `_set_error_if_divide_by_zero` instead.r   ignorezInvalid category: )
ValueErrorr   error_checking_behavior_oobvalue)r   s    O/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/numpy/error.py_is_category_disabledr   "   sm     
J
KK
K  --33x??'z233    msgpredc                H    t        |      ryt        j                  | |       y)a  Set the internal error state if any element of `pred` is `True`.

  This function is similar to :func:`set_error_if`, but it also takes a category
  argument. The category can be "nan", "divide", or "oob". The error checking
  behavior for each category can be configured using
  :func:`set_error_checking_behavior`. If not provided, there will be no
  category.

  This function is intended for use in JAX internal APIs (e.g., `jax.numpy`)
  to perform category-specific runtime checks tied to the operation being
  performed.
  N)r   error_check_libset_error_if)r   r   r   s      r   _set_error_if_with_categoryr"   3   s    $ 8$
tS)r   c                    t         j                  j                  dk(  ryt        j                  | j
                  t        j                        syt        j                  t        j                  |       d       y)zSet the internal error state if any element of `pred` is `NaN`.

  This function is disabled if the `jax_error_checking_behavior_nan` flag is
  set to "ignore".
  r   NzNaN encountered)r   error_checking_behavior_nanr   r   
issubdtypedtypenpfloatingr    r!   r   isnan)r   s    r   _set_error_if_nanr*   K   sP     ''--9
			4::r{{	3
v||D13DEr   c                    t         j                  j                  dk(  ryt        j                  | d      }t        j                  | |k(  d       y)zSet the internal error state if any element of `pred` is zero.

  This function is intended for checking if the denominator of a division is
  zero.

  This function is disabled if the `jax_error_checking_behavior_divide` flag is
  set to "ignore".
  r   N )shapezDivision by zero encountered)r   error_checking_behavior_divider   r	   
zeros_liker    r!   )r   zeros     r   _set_error_if_divide_by_zeror1   Z   sB     **00H<
		"	"4r	2$tt|-KLr   r-   .gather_indicesc                    t         j                  j                  dk(  ryt        j                  |      syt        j                  |      }t        j                  | |j                        } t        j                  t        j                  t        j                  |      |  k  t        j                  |      | k\        d       y)z;Check for out of bounds errors before calling `lax.gather`.r   Nr&   z5Out of bounds encountered before calling `lax.gather`)r   r   r   r'   sizer   arrayr&   r    r!   r   
logical_orr   minmax)r-   r2   s     r   _check_precondition_oob_gatherr:   j   s     ''--9
		 
%++N;.

"
"50D0D
E%
..
(E6
1
..
(E
1 >r   start_indicesslice_sizesallow_negative_indicesc           
         t         j                  j                  dk(  ryt        j                  |      }t        j                  | |j
                        } t        j                  ||j
                        }t        j                  |d      }t        j                  ||  d      }t        j                  t        j                  t        j                  |||z         |k  t        j                  |||z         | k\        d       y)zBCheck for out of bounds errors before calling `lax.dynamic_slice`.r   Nr4   boolr   z<Out of bounds encountered before calling `lax.dynamic_slice`)r   r   r   r   r6   r&   r
   wherer    r!   r   r7   minimummaximum)r-   r;   r<   r=   lower_bounds        r   %_check_precondition_oob_dynamic_slicerD   ~   s     ''--9
$**=9-

"
"50C0C
D%"((M<O<OP+-334JRXY 6B+
..(C
D{
R
..(C
D
M Er   )r   raisec                   \    e Zd ZdZddddddedz  dedz  dedz  dedz  ddf
d	Zd
 Zd Zy)error_checking_behaviora  A context manager to set the error checking behavior.

  If both `all` and a category are provided, the category will override the
  `all` setting.

  When the error checking behavior is set to "ignore", all errors will be
  ignored. When set to "raise", errors will be detected and recorded, but an
  exception will not be raised immediately. Users must call
  :func:`raise_if_error` to at the end of the computation to raise the
  exception.
  N)allr   r   r   rH   r   r   r   r   c                    i }||x|d<   x|d<   |d<   |||d<   |||d<   |||d<   || _         t        j                         | _        y )Nr   r   r   )new_settings
contextlib	ExitStackstack)selfrH   r   r   r   rJ   s         r   __init__z error_checking_behavior.__init__   sr     L
KNNl5NL2\%5H
l5%l8
l5$D%%'DJr   c                     t         j                  t         j                  t         j                  d}| j                  j                         D ])  \  }}| j                  j                   ||   |             + | S )Nr   )r   r$   r.   r   rJ   itemsrM   enter_context)rN   config_flagskeyr   s       r   	__enter__z!error_checking_behavior.__enter__   sl    117711L
 ''--/ 9
U
jj0|C0789Kr   c                 8    | j                   j                          y N)rM   close)rN   exc_type	exc_value	tracebacks       r   __exit__z error_checking_behavior.__exit__   s    JJr   )__name__
__module____qualname____doc__BehaviorrO   rU   r\   r,   r   r   rG   rG      sf    
 "! $!( d?( d?	(
 o( d?( ((r   rG   rW   )#rK   typingr   collections.abcr   numpyr'   jax._srcr   r   r   r    jax._src.numpyr   r	   r
   r   r   jax._src.typingr   r   Categoryr?   r   strr"   r*   r1   tupleintr:   listrD   ra   rG   r,   r   r   <module>rm      s&     $    3 - ) $ ! % ,)*4o4	4* !%	* 
* o	*
*
 
*0FE FMu M c?,5	(c?I& c !J	
 
2 $%, ,r   