
    VpfM)                        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	m
Z
 d dlZg dZdZd	 Zi ej        d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d  ej        ej                  d
 ej        ej                  d
 ej        ej                  d
 ej        ej                   d
 ej        ej!                  d
 ej        ej"                  d ej        ej#                  d ej        ej$                  d ej        ej%                  d ej        ej&                  d ej        ej'                  diZ(ej)        9ej*        J d e( ej        ej)                  <   d e( ej        ej*                  <   d Z+ ej        ej                  d
 ej        ej                  d
 ej        ej                  d
 ej        ej                   d
 ej        ej!                  d
 ej        ej"                  d
 ej        ej#                  d ej        ej$                  d ej        ej%                  d ej        ej&                  d ej        ej'                  diZ,d Z-d%dZ.d&dZ/d%dZ0d%dZ1d Z2d Z3d Z4d Z5 ee	 e5ej6                            Z6 ee	 e5ej7                            Z7 ee	d           Z8 ee	 e5ej9                            Z9d Z:d Z;efdZ<d  Z=ddedfd!Z>ddedfd"Z?	 d'd$Z@dS )(    )partialN)api)config)dtypes)tree_maptree_reduce)check_grads	check_jvp	check_vjpg-C6?c                     t          | d          r| j        S t          |           t          j        v r1t          j        t          j        t          |                              S t          j        |           j        S )Ndtype)hasattrr   type_dtypespython_scalar_dtypesnpasarray)xs    Y/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/public_test_util.py_dtyper   #   s^    Q 7NAww'...8G0a9::::a==    g?g{Gz?gMbP?gư>gV瞯<c                      t           S N)_default_tolerance r   r   default_tolerancer   K   s    	r   gMb`?gh㈵>c                     t          | t          j                   o't          | t          t          t
          t          f          S r   )
isinstancer   genericboolintfloatcomplex)vals    r   is_python_scalarr%   ]   s0    RZ((	(	YZdCPW=X-Y-YYr    c                 
   | j         |j         cxk    rt          j        k    r'n n$t          j                            | ||           d S t          j        t          j        t          j        t          j	        t          j
        t          j        gfd} ||           }  ||          }i }|r||d<   |r||d<   t          j        d          5  t          j        j        | |fi |d|i d d d            d S # 1 swxY w Y   d S )Nerr_msgc                    | j         v r|                     t          j                  S | j         t          j        v rM|                     t	          j        | j                   j        dk     rt          j        nt          j	                  S | S )Nr   )
r   astyper   float32r   _intn_dtypesiinfominint8uint8)r   custom_float_dtypess    r   maybe_upcastz,_assert_numpy_allclose.<locals>.maybe_upcastm   so    w%%%XXbj!!! 	w'&&&XXqw!7!7!;a!?!?bggRXNNNHr   atolrtolignoreinvalidr)   )r   r   float0r   testingassert_array_equalfloat8_e4m3b11fnuzfloat8_e4m3fnfloat8_e4m3fnuzfloat8_e5m2float8_e5m2fnuzbfloat16errstateassert_allclose)abr4   r5   r)   r3   kwr2   s          @r   _assert_numpy_allcloserG   `   sr   W))))7>)))))J!!!Q!888
F      l1oo!l1oo!	"	2f:	2f:	{8$$$ < < Jq!;;r;;7;;;;< < < < < < < < < < < < < < < < < <s   C88C<?C<c                    |i n|}t          |t                    s|S d |                                D             }t          j        t          j        |                     } |                    | t                      |                    S )Nc                 >    i | ]\  }}t          j        |          |S r   )r   r   ).0keyvalues      r   
<dictcomp>ztolerance.<locals>.<dictcomp>   s&    <<<*#u#<<<r   )	r   dictitemsr   canonicalize_dtyper   r   getr   )r   tols     r   	tolerancerS      sy    ks#	C		 J<<		<<<#

$RXe__
5
5%	)++E2	3	33r   c                    t          j        |           t          j        |          }} | j        |j        k    sJ t          t	          | j        |          t	          |j        |                    }t          t	          | j        |          t	          |j        |                    }t          | ||| j        z  ||j        z  |           d S Nr4   r5   r)   )r   r   shapemaxrS   r   rG   size)rD   rE   r4   r5   r)   s        r   _assert_numpy_closerZ      s    	A
1Q!	
AG					Yqw%%y$'?'?	@	@$	Yqw%%y$'?'?	@	@$AD16Mqv!(* * * * * *r   c                 X    t          t          |||          }t          || |           d S rU   )r   rZ   r   )xsysr4   r5   r)   assert_closes         r   check_closer_      s8    ,4d!(* * *,
<R     r   c                 .    d }t          || |           d S )Nc                     t           j        j        r$t          |           t          |          k    sJ d S t	          j        t          |                     t	          j        t          |                    k    sJ d S r   )r   
enable_x64rL   r   r   rP   r   ys     r   _assert_dtypes_matchz1_check_dtypes_match.<locals>._assert_dtypes_match   sy     5AYY&))######(33(334 4 4 4 4 4r   r   )r\   r]   re   s      r   _check_dtypes_matchrg      s,    5 5 5 R(((((r   c                 Z    d }t          t          j        t          || |                    S )Nc                     t          j        t          j        t          j        |                               d          |                    d                              S )N)r   realdotconjreshaperc   s     r   contractzinner_prod.<locals>.contract   s>    726"'!**,,R00!))B--@@AAAr   )r   r   addr   )r\   r]   ro   s      r   
inner_prodrq      s2    B B B	RVXhB77	8	88r   c          
         t          j        d          5  t          j        t          j        | |          t          j        d|          t          j        | ||                    cddd           S # 1 swxY w Y   dS )z1Subtraction that with `inf - inf == 0` semantics.r6   r7   r   r   N)r   rB   whereequalarraysubtract)r   rd   r   s      r   _safe_subtractrx      s    	{8$$$ 4 48BHQNNBHQ$6$6K1E2224 44 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4s   AA44A8;A8c                       fd}|S )Nc                      t          | d                   }t          j         |  |          }t          d | D                       r|                                }|S )Nr   rs   c              3   4   K   | ]}t          |          V  d S r   )r%   )rJ   args     r   	<genexpr>z9_preserve_input_types.<locals>.wrapped.<locals>.<genexpr>   s+      
1
1SC  
1
1
1
1
1
1r   )r   r   rv   allitem)argsr   resultfs      r   wrappedz&_preserve_input_types.<locals>.wrapped   s]    47OOEXaahe,,,F

1
1D
1
1
111 {{}}fMr   r   )r   r   s   ` r   _preserve_input_typesr      s#         
.r   c                 @    t          | |t          |                     S Nrs   )rx   r   rc   s     r   <lambda>r      s    q!6!99 E E E r   c                 .    fd}t          ||           S )Nc                     t          |           }t          j        | t          j        |          |          }t	          |           r|                                n|S r   )r   r   multiplyrv   r%   r   )r   r   r   rD   s      r   mulzscalar_mul.<locals>.mul   sR    1IIE[BHQe444EBBBF,Q//;6;;===V;r   rf   )r\   rD   r   s    ` r   
scalar_mulr      s.    < < < < < 
#r		r   c                 P    t          j        |          t          |           fd}t          j        t           j                  r, |                                d           |            z  z   }n
 |            }t          |          r|                                n|S )Nc                  @    t          j         j                    S r   )r   r   randn)r   rngrW   s   r   r   zrand_like.<locals>.<lambda>   s    "*YSY.e<<< r   y              ?)	r   rW   r   r   
issubdtypecomplexfloatingr   r%   r   )r   r   r   r   r   rW   s   `   @@r   	rand_liker      s    
(1++%
))%
<
<
<
<
<
<%r122 UWWuzz$''%%''11FFUWWF*1--	969r   c                     t          ||          } | t          ||           } | t          ||           }t          t          ||          d|z            S )Ng      ?)r   rp   subsafe_sub)r   primalstangentsepsdeltaf_posf_negs          r   numerical_jvpr      s[    
Xs
#
#%
!S%  
!%
!S%  
!%	HUE**C#I	6	66r   c                     | |S t          | t                    s| S |                                }|                                 D ]\  }}||t	          j        |          <   |S r   )r   rN   copyrO   r   r   )rR   defaultoutkvs        r   _merge_tolerancer      se    [N	C		 J#iikk  daC	*r   c                    t          |t                    }t          |t                    }t          j                            d          }t          t          t          |          |          } |||          \  }	}
t          |	|
            | | }t          |	|           t          | |||          }t          |	||||r| dnd           t          |
||||r| dnd           d S )Nr   r    primalprimalrV   z tangenttangent)r   default_gradient_tolerancer   randomRandomStater   r   r   rg   r   r_   )r   f_jvpr   r4   r5   r   r)   r   r   v_outt_outv_out_expectedt_out_expecteds                r   r
   r
      s&   	$ :	;	;$	$ :	;	;$
	a  #WY,,d33'tW%%,%eU###1d8.e^,,, D's;;;. e^$T-4B))))(D D D De^$T.5D****9F F F F F Fr   c                 4   t          |t                    }t          |t                    }t          t          t          j                            d                    } || \  }}	 | | }
t          ||
|||r| dnd           t          ||          }t          | |||          }t          ||          }t           |	t          |                              }t          ||          }t          ||          }t          |||||r| dnd           d S )Nr   r   r   rV   r   z cotangent projectionzcotangent projection)r   r   r   r   r   r   r   r_   r   r   rm   rq   )r   f_vjpr   r4   r5   r   r)   
_rand_liker   vjpfunr   r   tangent_out	cotangentcotangent_outipip_expecteds                    r   r   r      sK   	$ :	;	;$	$ :	;	;$y")"7"7":":;;*%,-%1d8.e^$T-4B))))(D D D DZ&&'awC888+z5)))vvd9oo..//-'=))";	22+b+Dt!>'8888'=@ @ @ @ @ @r   fwdrevc                    	 t          |          }|pt          }t          t          |||          t          t          |||          	d	fd	 | ||           dS )a  Check gradients from automatic differentiation against finite differences.

  Gradients are only checked in a single randomly chosen direction, which
  ensures that the finite difference calculation does not become prohibitively
  expensive even for large input/output spaces.

  Args:
    f: function to check at ``f(*args)``.
    args: tuple of argument values.
    order: forward and backwards gradients up to this order are checked.
    modes: lists of gradient modes to check ('fwd' and/or 'rev').
    atol: absolute tolerance for gradient equality.
    rtol: relative tolerance for gradient equality.
    eps: step size used for finite differences.

  Raises:
    AssertionError: if gradients do not match.
  )r4   r5   r   r&   c                 |    d
v ra|rd| nd}  t          t          j                   ||           |dk    r+ t          t          j                   ||f|dz
  |           d
v rN|rd| nd} 	 t          t          j                   ||           |dk    r fd	} |||dz
  |           d S d S d S )
Nr   zJVP of JVPr(      r   zVJP of VJPc                  D    t          j        g| R  \  }} ||          S r   )r   vjp)r   out_primal_pyvjp_pyr   s      r   r   z0check_grads.<locals>._check_grads.<locals>.f_vjp1  s/    "%'!"3d"3"3"3
-&&
&r   )r   r   jvpr   )r   r   orderr)   fwd_msgrev_msgr   _check_grads
_check_jvp
_check_vjpmodess   `      r   r   z!check_grads.<locals>._check_grads&  s   ~~'.9#'###EgjGCGQ''w????	WSWa((4,	7KKK~~'.9#'###EgjGCGQ''w????		' 	' 	' 	' 	' 	UD%!)W55555 ~ 
r   N)r&   )tupleEPSr   r
   r   )
r   r   r   r   r4   r5   r   r   r   r   s
      `   @@@r   r	   r	     s    ( 
t$
s#yt$C@@@*yt$C@@@*6 6 6 6 6 6 6 6 6  ,q$r   )NNr&   r   )r   NNN)A	functoolsr   operatorjax._srcr   r   r   r   jax._src.tree_utilr   r   numpyr   __all__r   r   r9   r   bool_int4r0   int16int32int64uint4r1   uint16uint32uint64r<   r=   r>   r?   r@   rA   float16r,   float64	complex64
complex128r   int2uint2r   r   r%   rG   rS   rZ   r_   rg   rq   rx   r   rp   r   r   rm   r   r   r   r   r
   r   r	   r   r   r   <module>r      s                      & & & & & & 4 4 4 4 4 4 4 4     4
3
3   NABHRX BHW\A BHRWq	
 BHRX BHRX BHRX BHW]Q BHRX BHRY BHRY BHRY BHW'(($ BHW"##T BHW$%%t  BHW !!4!" BHW$%%t#$ BHWBHRZ$BHRZ$BHRZ%BHR\DBHR]U/  4 <		"	"	"/0XRXgl++,01XRXgm,,-  
 "(7%&&
"(7 !!4
"(7"##T
"(7
"(7"##T
"(7d
"(2:
"(2:
"(2:
"(2<$
"(2=4 Z Z Z <  <  <  <D4 4 4 4* * * *! ! ! !) ) )9 9 94 4 4   gh--hl;;<<gh--hl;;<<78EEG Gwx..rw7788  : : : -0 7 7 7 7   $(dR F F F F& $(dR @ @ @ @( AE* * * * * *r   