
    Vpf             	      
   d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	m
Z
 d dlZd dlZd dlmZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl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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, d dl!m-Z- d dl!m.Z. d dl/m0Z0 d dl1m2Z2 d dl1m3Z3 d dl4m5Z6 d dl7m8Z8m9Z9 d dl:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF d dlGmHZH d dlImJZJ d dlKmLZLmMZMmNZNmOZO d dlPZQd dlRmSZT  e#jU        ddd           ZV e#jW        d! eX ejY        d"d#                    d$           ZZ e#jW        d% eX ejY        d&d'                    d(          Z[ e#j\        d) e#j]        d*d+          d,           Z^ e#jU        d- ejY        d.d          d/          Z_ e#jU        d0 ejY        d1d          d2          Z` e#j\        d3 e#j]        d4d+          d5           Za e#jU        d6 ejY        d7d8          d9           Zb ejc        d:          Zddd>Zed? Zfd@ ZgddBZhdC ZidD ZjdE ZkddFZle
ddJ            Zm eemejn                  Zo eemejp                  Zqe
dK             Zre
dL             Zse
dM             Zte
dN             Zue
dO             Zve
dP             Zwe
dQ             Zxe
dR             Zye
dS             Zze
ddU            Z{e
dV             Z|dW Z}dX Z~dY ZdZ Zd[ Zdd^ZddbZddcZddfZdddiZddkZdl ZddoZd+e_        dp Zdq Zdr Zds Ze
ddu            Zdv ZddxZdy Zdd{Zd| Z G d} d~          Z G d de          Z G d de          Z e            Z e            Zd Zd Zd Zd Zd Z ej        d          d             Zdd fdZddZddZd Zd Zd ZddZd ZddZd Zd Zd Zd Zd ZddZddZd Zd Zd Zd Zd Zd Zd Zd Zej        d             Zd Zd Z G d dej                  Zd ZddZe
d             Z G d d          Ze
d             Z G d dej                  Zej        Zde_        d Zde_        eefZ G d deŦ          Ze
dedd            ZeeeeXf                  Ze
dd            Zd Zd Zd Z G dÄ dĦ          Z G dń dƦ          Z eצ            Z&dǄ Z ejc        dȦ          Zdd˄Zd̄ Zddd͜ddԄZe
dՄ             Ze
dք             Zdׄ Zd؄ ZddڄZ G dۄ deQj                  Z G d݄ dަ          ZdddZdS )    )annotationsN)Callable	GeneratorIterableSequence)	ExitStackcontextmanager)partial)AnyTextIO)absltest)parameterized)lax)api)config)core)dispatchdtypes)linear_util)
monitoring)pjit)stages)
xla_bridgerunning_in_cloud_tpu_vm)mlir)pxla)
xla_client)promote_dtypespromote_dtypes_inexact)_assert_numpy_allclose_check_dtypes_match_default_tolerance_dtypecheck_closecheck_grads	check_jvp	check_vjpdefault_gradient_tolerancedefault_tolerance	rand_like	tolerance)unzip2)compilation_cache)tree_alltree_flattentree_maptree_unflattenjax_test_dut zJDescribes the device under test in case special consideration is required.)helpjax_num_generated_casesJAX_NUM_GENERATED_CASES10z!Number of generated cases to testmax_cases_sampling_retriesJAX_MAX_CASES_SAMPLING_RETRIES100zNumber of times a failed test sample should be retried. When an unseen case cannot be generated in this many trials, the sampling process is terminated.jax_skip_slow_testsJAX_SKIP_SLOW_TESTSFz$Skip tests marked as slow (> 5 sec).test_targetsJAX_TEST_TARGETSzRegular expression specifying which tests to run, called via re.search on the test name. If empty or unspecified, run all tests.exclude_test_targetsJAX_EXCLUDE_TEST_TARGETSzRegular expression specifying which tests NOT to run, called via re.search on the test name. If empty or unspecified, run all tests.*jax_test_with_persistent_compilation_cache*JAX_TEST_WITH_PERSISTENT_COMPILATION_CACHEzIf enabled, the persistent compilation cache will be enabled for all test cases. This can be used to increase compilation cache coverage.hypothesis_profileJAX_HYPOTHESIS_PROFILEdeterministicz^Select the hypothesis profile to use for testing. Available values: deterministic, interactivez[ \"'\[\](){}<>=,._]+sstrreturnc                8    t                               d|           S )N_)kSanitizeNameREsub)rH   s    R/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/test_util.pysanitize_test_namerP   z   s    			S!	$	$$    c                X    t          j        t          j        |                     j        S N)_dtypesfinfocanonicalize_dtypebitsdtypes    rO   num_float_bitsrZ   }   s     	w1%88	9	9	>>rQ   c                    t          j        |           } t          j                            | j        j                  }|r'|                     t          j        |                    n| S )a  Convert a value to an array with JAX's default dtype.

  This is generally used for type conversions of values returned by numpy functions,
  to make their dtypes take into account the state of the ``jax_enable_x64`` and
  ``jax_default_dtype_bits`` flags.
  )	npasarrayrT   _default_typesgetrY   kindastyperV   )arrrY   s     rO   to_default_dtyperc      sR     	
3#

 
$
$SY^
4
4%:?	HG.u55	6	6	6SHrQ   Tc                J     t          j                    fd            }|S )aE  Return a version of a function with outputs that match JAX's default dtypes.

  This is generally used to wrap numpy functions within tests, in order to make
  their default output dtypes match those of corresponding JAX functions, taking
  into account the state of the ``jax_enable_x64`` and ``jax_default_dtype_bits``
  flags.

  Args:
    use_defaults : whether to convert any given output to the default dtype. May be
      a single boolean, in which case it specifies the conversion for all outputs,
      or may be a pytree with the same structure as the function output.
  c                      | i |}t          t                    rrt          t          |          n|S d }t          ||          S )Nc                (    |rt          |           n| S rS   )rc   )rb   use_defaults     rO   <lambda>z:with_jax_dtype_defaults.<locals>.wrapped.<locals>.<lambda>   s    K#P#3C#8#8#8S rQ   )
isinstanceboolr2   rc   )argskwargsresultffuncuse_defaultss       rO   wrappedz(with_jax_dtype_defaults.<locals>.wrapped   s^    T4"6""F,%% /3?KX&///VK
P
Paa...rQ   	functoolswraps)ro   rp   rq   s   `` rO   with_jax_dtype_defaultsru      s@     ?4/ / / / / / 
.rQ   c                H    	 t          |            dS # t          $ r Y dS w xY w)NTF)iter	TypeErrorxs    rO   is_sequencer{      s=    GGG 4 
   55s    
!!c                      pd t           t                    rd                                  D             S  fdt          D             S )Nr   c                >    i | ]\  }}t          j        |          |S  r\   rY   .0kvs      rO   
<dictcomp>z(_normalize_tolerance.<locals>.<dictcomp>   s&    333tq!BHQKK333rQ   c                    i | ]}|S r~   r~   )r   r   tols     rO   r   z(_normalize_tolerance.<locals>.<dictcomp>   s    ///qAs///rQ   )ri   dictitemsr$   )r   s   `rO   _normalize_tolerancer      sU    q#T 033syy{{3333////.////rQ   c                    t          |           } t          |          }| }|                                D ],\  }}t          ||                     |d                    ||<   -|S Nr   )r   r   maxr_   )tol1tol2outr   r   s        rO   join_tolerancer      sb    	d	#	#$	d	#	#$#jjll $ $daDHHQNN##CFF	*rQ   c                n    t          t          |          }t          t          || |                     d S )N)err_msg)r
   r"   r0   r2   )xsysr   assert_closes       rO   check_eqr      s5    /AAA,
8L"b))*****rQ   fpr   (Generator[Callable[[], str], None, None]c              #  x  K   dd	fd}t          j        dd          5 }t          j        |                                           }t          j        |                                |                                            	 |V  |                                  t          j        |                                            |                    d           |	                                t          j        ||                                            n# |                                  t          j        |                                            |                    d           |	                                t          j        ||                                            w xY w	 ddd           dS # 1 swxY w Y   dS )
a  Context manager to capture all output written to a given file object.

  Unlike ``contextlib.redirect_stdout``, this context manager works for
  any file object and also for both pure Python and native code.

  Example::

    with capture_output(sys.stdout) as get_output:
      print(42)
    print("Captured": get_output())

  Yields:
    A function returning the captured output. The function must be called
    *after* the context is no longer active.
  NrJ   rI   c                 *     t          d           S )Nz0get_output() called while the context is active.)
ValueError)captureds   rO   
get_outputz#_capture_output.<locals>.get_output   s    IJJJOrQ   zw+zutf-8)modeencodingr   )rJ   rI   )
tempfileNamedTemporaryFileosdupfilenodup2flushfsyncseekread)r   r   rn   original_fdr   s       @rO   _capture_outputr      s     $ (     
 "w??? (1&%%KGAHHJJ		$$$( 	hhjjjhryy{{ffQiiihgk299;;''''	 	hhjjjhryy{{ffQiiihgk299;;'''''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s,   A F/DBF/BFF//F36F3c               #     K   t           j        dgfd}  |           t           _        	 V  t           _        d S # t           _        w xY w)Nr   c                      fd}|S )Nc                     dxx         dz  cc<   t           _        	  | i |t           _        S # t           _        w xY wNr      r   batched_device_put)rk   rl   r   batched_device_put_and_countcountfns     rO   fn_and_countzAcount_device_put.<locals>.make_fn_and_count.<locals>.fn_and_count   sY    Ahhh!mhhh
 !3d?r4"6""">">>>>>s	   3 Ar~   )r   r   r   r   r   s   ` rO   make_fn_and_countz+count_device_put.<locals>.make_fn_and_count   s4    
? 
? 
? 
? 
? 
? 
? 
? rQ   r   )r   r   r   r   s    @@@rO   count_device_putr      s      .#%       "3!23E!F!F8$1
KKK0D0D0000s   A Ac               #     K   t           j                                         dg} 	 | V  t           j                                        j        | d<   d S # t           j                                        j        | d<   w xY w)Nr   )r   xla_primitive_callablecache_clear
cache_infomisses)r   s    rO   count_primitive_compilesr     sw      
!--///$%C
KKK.99;;BE!HHHx.99;;BE!HBBBBs   A (A9c               #     K   t           j        dgfd} | t           _        	 V  t           _        d S # t           _        w xY w)Nr   c                 4    dxx         dz  cc<    | i |S r   r~   )rk   rl   r   original_fns     rO   5batched_copy_array_to_devices_with_sharding_and_countz]count_device_put_fast_path_hit.<locals>.batched_copy_array_to_devices_with_sharding_and_count  s-    	!HHHMHHH;''''rQ   )xc+batched_copy_array_to_devices_with_sharding)r   r   r   s    @@rO   count_device_put_fast_path_hitr     sq      >+#%( ( ( ( ( ( 4i"0A
KKK5@B222[B2@@@@	   9 Ac               #     K   t           j        dgfd} | t           _        	 V  t           _        d S # t           _        w xY w)Nr   c                 4    dxx         dz  cc<    | i |S r   r~   )rk   rl   r   original_pjit_lowers     rO   pjit_lower_and_countz7count_pjit_cpp_cache_miss.<locals>.pjit_lower_and_count&  s.    	!HHHMHHH////rQ   )pjit_lib_pjit_lower)r   r   r   s    @@rO   count_pjit_cpp_cache_missr   !  sq       ,#%0 0 0 0 0 0 .(/
KKK.H.H....r   c               #     K   t           j        dgfd} | t           _        	 V  t           _        d S # t           _        w xY w)Nr   c                 4    dxx         dz  cc<    | i |S r   r~   )rk   rl   r   original_cached_compilations     rO   cached_compilation_and_countzIcount_cached_compilation_cache_miss.<locals>.cached_compilation_and_count5  s.    	!HHHMHHH&&7777rQ   )r   _cached_compilation)r   r   r   s    @@rO   #count_cached_compilation_cache_missr   0  sq       $ 8#%8 8 8 8 8 8 :$;
KKK:D:D::::r   c               #     K   t           j        dgt          j        fd            } | t           _        	 V  t           _        d S # t           _        w xY w)Nr   c                 .    dxx         dz  cc<    |  S r   r~   )rk   r   original_create_pjit_jaxprs    rO   create_pjit_jaxpr_and_countzAcount_jit_tracing_cache_miss.<locals>.create_pjit_jaxpr_and_countD  s&    	!HHHMHHH%%t,,rQ   )r   _create_pjit_jaxprlucache)r   r   r   s    @@rO   count_jit_tracing_cache_missr   ?  s}      ':#%8- - - - - 8- !<(=
KKK"<H"<H<<<<s   A Ac               #     K   t           j        t          j        t                    fd} | t           _        	 V  t           _        d S # t           _        w xY w)Nc                <    | xx         dz  cc<    | g|R i |S Nr   r~   )funrk   kwr   original_infer_params_impls      rO   infer_params_impl_and_countzFcount_jit_infer_params_cache_miss.<locals>.infer_params_impl_and_countT  s9    	#JJJ!OJJJ%%c7D777B777rQ   )r   _infer_params_implcollectionsdefaultdictint)r   r   r   s    @@rO   !count_jit_infer_params_cache_missr   O  sz      ':

!#
&
&%8 8 8 8 8 8 !<(=
KKK"<H"<H<<<<s   A Ac               #     K   t           j        j        dgfd} | t           j        _        	 V  t           j        _        d S # t           j        _        w xY w)Nr   c                 4    dxx         dz  cc<    | i |S r   r~   )rk   rl   r   original_calls     rO   compiled_call_countz9count_aot_jit_cpp_cache_miss.<locals>.compiled_call_countd  s-    	!HHHMHHH=$)&)))rQ   )r   Compiledcall)r   r   r   s    @@rO   count_aot_jit_cpp_cache_missr   _  sw      /&-#%* * * * * * -&/)
KKK(FO=FO((((s   A Ac               #     K   t           j        dgfd} | t           _        	 V  t           _        d S # t           _        w xY w)Nr   c                 4    dxx         dz  cc<    | i |S r   r~   )rk   rl   r   
mlir_lowers     rO   mlir_lower_and_countz9count_jit_and_pmap_compiles.<locals>.mlir_lower_and_countw  s-    	!HHHMHHH:t&v&&&rQ   )r   lower_jaxpr_to_module)r   r   r   s    @@rO   count_jit_and_pmap_compilesr   o  sq      
 )*#%' ' ' ' ' '  4$,
KKK!+DD++++r   fun_namec              #      K   t           j        dg fd}|t           _        	 V  t           _        d S # t           _        w xY w)Nr   c                J    |k    rdxx         dz  cc<    | |g|R i |S r   r~   )ctxnamerk   rl   r   r   r   s       rO   r   z>count_subjaxpr_to_hlo_conversion.<locals>.mlir_lower_and_count  sE    xAhhh!mhhh:c41$111&111rQ   )r   lower_jaxpr_to_fun)r   r   r   r   s   ` @@rO    count_subjaxpr_to_hlo_conversionr     sw      
 &*#%2 2 2 2 2 2 2
 1$)
KKK(DjD((((s	   ; A	c              #     K   t                      5 }d V  d d d            n# 1 swxY w Y   |d         | k    rt          d|  d|d                    d S )Nr   zExpected exactly z  XLA compilations, but executed )r   AssertionError)timesr   s     rO   $assert_num_jit_and_pmap_compilationsr     s      "$$ 
	EEE
 
 
 
 
 
 
 
 
 
 
 
 
 
 

1X
 4U 4 4).q4 4 5 5 5 s   "&&c                 J    t           j        pt          j                    j        S rS   )	_TEST_DUTvaluer   get_backendplatformr~   rQ   rO   device_under_testr    s    		=J244==rQ   c                 `   t                      dk    r|t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j
        t          j        t          j        t          j        h} nt                      dk    r$t          j        t          j        t          j        h} nt          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j
        t          j        t          j        t          j        t          j        t          j        h} t$          j        j        s1| t          j        t          j        t          j        t          j        hz  } | S )NtpuMETAL)r  r\   bool_int8int16int32uint8uint16uint32rT   bfloat16float16float32	complex64int64uint64float64
complex128r   
enable_x64r  )typess    rO   supported_dtypesr    s    E!!Xrw"(BHbiY("*bj",PEEg%%Xry"*-EEXrw"(BHXry")RYrz2:rz\2=*E 
		  >	bi2:r}==E	,rQ   c                 6    dt          j                    j        v S )Nrocmr   r  platform_versionr~   rQ   rO   is_device_rocmr         	:)++<	<<rQ   c                 6    dt          j                    j        v S )Ncudar  r~   rQ   rO   is_device_cudar$    r!  rQ   c                     t           S rS   r   r~   rQ   rO   is_cloud_tpur&    s    	  rQ   datedatetime.datec                D   t                      sdS t          j                    j                            d          d         }t          j        d|          }t          |          dk    rdS |                     t          |d         dd                             }|| k    S )NT
r   z\(.*?\)r   r   )
r&  r   r  r  splitrefindalllenfromtimestampr   )r'  r  results
build_dates       rO   if_cloud_tpu_at_leastr2    s    	 4
  +-->DDTJJ2NJz#344'\\Q4!!#gaj2&6"7"788*	t	rQ   major_versionr   minor_versionc                @    t          j                    }|dS || |fk    S )NT)r   backend_pjrt_c_api_version)r3  r4  pjrt_c_api_versionss      rO   pjrt_c_api_version_at_leastr8    s,    "=?? 4	 >	>>rQ   c                 4   t                      dk    rt          d          t          j                    d         j        } |                     d          r| d t          d                    } | d d         dk    s
J |             t          | d                   S )Nr  zDevice is not TPUr   z liter   zTPU v)r  r   jaxdevicesdevice_kindendswithr.  r   )r`   s    rO   get_tpu_versionr>    s    E!!
(
)
))	q		%$	]]7  #g,,D	crcg			t				T"XrQ   versionrj   c                N    t                      dk    rdS t                      | k    S )Nr  F)r  r>  )r?  s    rO   is_device_tpu_at_leastrA    s)    E!!5			g	%%rQ   
int | Nonevariantc                    t                      dk    rdS | dS t          j                    d         j        }d|  | }|dk    rd|v S ||v S )Nr  FTr   r   v5ezv5 lite)r  r:  r;  r<  )r?  rC  r<  expected_versions       rO   is_device_tpurG    si    E!!5_4a ,+++'++##	[	((rQ   
capabilityc                f    t                      sdS t          j        d          ^}}|j        | k    S )NFgpu)backend)r$  r:  local_devicescompute_capability)rH  drL   s      rO   #is_cuda_compute_capability_at_leastrO    s:    			 5

E
*
*
*%!a	
		++rQ   c                     t                      rt                      dh} nQt                      rt                      dh} n2t                      dk    rt                      dh} nt                      h} | S )z7returns a set of tags defined for the device under testr  r#  r	  rJ  )r   r  r$  )device_tagss    rO   _get_device_tagsrR    sy     ($&&/KK ($&&/KKg%%$&&.KK$&&'K	rQ   device_typesIterable[str]c                    t          | t                    r
J d            t                      }| D ](}t          |t                    s
J |            ||v r dS )dS )Nz(device_types should be a list of stringsTF)ri   rI   rR  )rS  tagsdevice_types      rO   test_device_matchesrX    s    C  0 0/0 0 0 
		$!  kk3''44444dTT 	rQ   c                      fd}|S )Nc                J     t          j                    fd            }|S )Nc                    t                      }             s+t          dd          }t          j        | d| d           | g|R i |S )N__name__[unknown test]z# not supported on device with tags .)rR  getattrunittestSkipTest)selfrk   rl   rQ  	test_name	predicatetest_methods        rO   test_method_wrapperz9_device_filter.<locals>.skip.<locals>.test_method_wrapper  s{    $&&kY[[ KK5EFF	
I
I;
I
I
IK K 	K[///////rQ   rr   )re  rf  rd  s   ` rO   skipz_device_filter.<locals>.skip  s?    _[!!0 0 0 0 0 "!0 rQ   r~   )rd  rg  s   ` rO   _device_filterrh    s#    	 	 	 	 	 
+rQ   c                 (     t           fd          S )zAA decorator for test methods to skip the test on certain devices.c                 $    t                      S rS   rX  disabled_devicess   rO   rh   z!skip_on_devices.<locals>.<lambda>  s    $78H$I$I I rQ   rh  rl  s   `rO   skip_on_devicesro    s    	IIII	J	JJrQ   c                 (     t           fd          S )zEA decorator for test methods to run the test only on certain devices.c                 "    t                     S rS   rk  enabled_devicess   rO   rh   z run_on_devices.<locals>.<lambda>  s     3O D D rQ   rn  rr  s   `rO   run_on_devicesrt    s    	DDDD	E	EErQ   c                 "    t          d           S )z\A decorator for test methods to run the test only on devices that support
  buffer donation.c                 4    t          t          j                  S rS   )rX  r   _platforms_with_donationr~   rQ   rO   rh   z1device_supports_buffer_donation.<locals>.<lambda>$  s    !$"?@@ rQ   rn  r~   rQ   rO   device_supports_buffer_donationrx     s     
@@
 
 rQ   
nr_devicesc              #    K   t          j        d          }|pd}d|vr|d|  z   t           j        d<   t          j                                         	 dV  |t           j        d= n|t           j        d<   t          j                                         dS # |t           j        d= n|t           j        d<   t          j                                         w xY w)zContext manager to set host platform device count if not specified by user.

  This should only be used by tests at the top level in setUpModule(); it will
  not work correctly if applied to individual test cases.
  	XLA_FLAGSr5   $xla_force_host_platform_device_countz( --xla_force_host_platform_device_count=N)r   getenvenvironr   r  r   )ry  prev_xla_flags	flags_strs      rO   set_host_platform_device_countr  (  s       9[)).")+9<<(V*VV WBJ{ $$&&&)	EEE
*[
!
! .bj&&(((((	 
*[
!
! .bj&&((((s   B ?Cc                      fd}|S )zAA decorator for test methods to skip the test when flags are set.c                L     t          j                    fd            }|S )Nc                    t          j                  }|k    r-t          dd          }t          j        | d d|            | g|R i |S )Nr\  r]  z not supported when FLAGS.z is )r   _readr_  r`  ra  )rb  rk   rl   
flag_valuerc  	flag_name
skip_valuere  s        rO   rf  z7skip_on_flag.<locals>.skip.<locals>.test_method_wrapperD  s    <	**j	z	!	!K5EFF	
M
M)
M
M
M
MO O 	O[///////rQ   rr   )re  rf  r  r  s   ` rO   rg  zskip_on_flag.<locals>.skipC  sD    _[!!0 0 0 0 0 0 "!0 rQ   r~   )r  r  rg  s   `` rO   skip_on_flagr  A  s)    	 	 	 	 	 	 
+rQ   markerc                      fd}|S )zDA decorator for test classes or methods to pytest.mark if installed.c                p    	 dd l }n# t          $ r | cY S w xY w t          |j                  |           S r   )pytestImportErrorr_  mark)func_or_classr  r  s     rO   wrapz&pytest_mark_if_available.<locals>.wrapR  sV    mmmm   '76;''666s    r~   )r  r  s   ` rO   pytest_mark_if_availabler  P  s#    7 7 7 7 7 
+rQ   c                     dt           j        v S )Nr  )sysmodulesr~   rQ   rO   is_running_under_pytestr  [  s    	S[	  rQ   reasonc                     d z     fd}|S )zDA decorator for test methods to skip the test when run under pytest.zRunning under pytest: c                X     t          j        t                                |           S rS   )r`  skipIfr  )re  r  s    rO   rg  zskip_under_pytest.<locals>.skipb  s&    =8?244f==kJJJrQ   r~   )r  rg  s   ` rO   skip_under_pytestr  _  s0    #f,&K K K K K	+rQ   c                    d t          ||          D             }d                    |                                 d                    |                    S )Nc              3  <   K   | ]\  }}t          ||          V  d S rS   )format_shape_dtype_string)r   shaperY   s      rO   	<genexpr>z*format_test_name_suffix.<locals>.<genexpr>h  sD       ? ?&% 0u== ? ? ? ? ? ?rQ   z{}_{}rL   )zipformat
capitalizejoin)opnameshapesr   arg_descriptionss       rO   format_test_name_suffixr  g  sX    ? ?*-ff*=*=? ? ?	))++SXX6F-G-G	H	HHrQ   c                      e Zd Zd Zd ZdS )ScalarShapec                    dS r   r~   rb  s    rO   __len__zScalarShape.__len__p  s    AArQ   c                (    t          d| d          )Nzindex z out of range.)
IndexError)rb  is     rO   __getitem__zScalarShape.__getitem__q  s    *-Ga-G-G-G"H"HHrQ   N)r\  
__module____qualname__r  r  r~   rQ   rO   r  r  o  s"        HHHHHrQ   r  c                      e Zd ZdS )_NumpyScalarNr\  r  r  r~   rQ   rO   r  r  r            rQ   r  c                      e Zd ZdS )_PythonScalarNr  r~   rQ   rO   r  r  s  r  rQ   r  c           	         | t           k    rK|t          j        t          t          j        d|                                                              k    S dS )Nr   rX   T)PYTHON_SCALAR_SHAPEr\   rY   typearrayitemr  rY   s     rO   is_valid_shaper  y  sK    
!!!BHT"(1E":":":"?"?"A"ABBCCCC	rQ   c                    t          |           t          t          fv r| S t          | t                    rdS t          j        |           dk    r| fS t          t          |                     )z*Converts `shape` to a tuple of dimensions.r~   r   )r  listtupleri   r  r\   ndimrx   )r  s    rO   _dims_of_shaper    sa    	%[[T5M!!L%%% !2	wu~~8O
DKK
 
  rQ   c                   |t           u r't          j        |                              |           S |t          u r&t          j        |                                           S t          |          t          t          fv r)t          j	        |           t          |          k    sJ | S t          j
        |          dk    rt          j	        |           |fk    sJ | S t          t          |                    )zACasts `value` to the correct Python type for `shape` and `dtype`.r   )NUMPY_SCALAR_SHAPEr\   rY   r  r  r]   r  r  r  r  r  rx   )r  r  rY   s      rO   _cast_to_shaper    s    
   8E??&&&###:e!!###E{{tUm##8E??eEll****L	wu~~8E??uh&&&&L
DKK
 
  rQ   c                4    t          j        |           j        S rS   )r\   rY   r   rX   s    rO   	dtype_strr    s    	%	rQ   c                    t          | t          j                  rt          |           d|  dS t          | t                    rt          |           } t          | |          S )N[])ri   r\   ndarrayr  r  r  _format_shape_dtype_stringr  s     rO   r  r    sb    rz"" ))))))% %LLE	#E5	1	11rQ   @   )maxsizec                   | t           u rt          |          S | t          u rdt          |          z   S t          |           t          u r4d                    d | D                       }t          |           d| dS t          |           t          u rt          |           d|  dS t          t          |                     )Npy,c              3  4   K   | ]}t          |          V  d S rS   )rI   )r   dims     rO   r  z-_format_shape_dtype_string.<locals>.<genexpr>  s(      22SC222222rQ   r  r  z,])r  r  r  r  r  r  r   rx   )r  rY   shapestrs      rO   r  r    s    
   U###)E""""E{{exx22E22222H,,,,,,E{{c******
DKK
 
  rQ         ?c                    | S rS   r~   ry   s    rO   rh   rh     s    Q rQ   c                H    t          j        t          j                  r	 fd}n fd}t          j        t          j                  r |            d |            z  z   }n
 |            }t          t          j         ||                              S )a  Produce random values given shape, dtype, scale, and post-processor.

  Args:
    rand: a function for producing random values of a given shape, e.g. a
      bound version of either np.RandomState.randn or np.RandomState.rand.
    shape: a shape value as a tuple of positive integers.
    dtype: a numpy dtype.
    scale: optional, a multiplicative scale for the random values (default 1).
    post: optional, a callable for post-processing the random values (default
      identity).

  Returns:
    An ndarray of the given shape and dtype using random values based on a call
    to rand but scaled, converted to the appropriate dtype, and post-processed.
  c            
         t          j        t           t                               z                                           S rS   )r\   r]   absr  ra   rY   randscaler  s   rO   rh   z_rand_dtype.<locals>.<lambda>  s;    
53tt^E-B-B'C#D#DDEELLUSS rQ   c                 x    t          j         t                     z                                           S rS   )r\   r]   r  ra   r  s   rO   rh   z_rand_dtype.<locals>.<lambda>  s3    
544)>)>#??@@GGNN rQ                 ?)rT   
issubdtyper\   unsignedintegercomplexfloatingr  r]   )r  r  rY   r  postrvalss   ````   rO   _rand_dtyper    s      r122 OSSSSSSSAANNNNNNNAr122 133DD133D	
44::u55ue	D	DDrQ   c                     d f fd	}|S )z:Random numbers that span the full range of available bits.c                    | S rS   r~   ry   s    rO   rh   z rand_fullrange.<locals>.<lambda>  s    q rQ   c                   t          j        |          }|j        t          j        t          |                     z  }                    dt          j        t           j                  j	        |t           j                  } ||          
                    |          }| t          u rp|t           j        k    r |                    t           j                  }n@|t           j        k    r0t           j        j        s|                    t           j                  }|                    |           }r@t          j        |t           j                  r!t           j        |t          j        |          <   t3          || |          S )Nr   )sizerY   )r\   rY   itemsizemathprodr  randintiinfor  r   viewr  r  ra   r  r  r   r  r  r  reshaper  floatingnanisnanr  )r  rY   r  r  r  rngstandardize_nanss        rO   genzrand_fullrange.<locals>.gen  s    HUOOE>DInU&;&;<<<D;;q"(28,,0t28;LLD4::??5!!D### 
")		{{28$$BIf&7&={{28$$<<D $BM%== $Vd28D>>$u---rQ   r~   )r  r  r  s   `` rO   rand_fullranger    s3    ([ . . . . . . ." 
*rQ      c                :    t          t          | j        |          S )Nr  r
   r  randn)r  r  s     rO   rand_defaultr     s    	ciu	5	5	55rQ   c                B    d }t          t          | j        d|          S )Nc                h    t          j        | dk    t          j        d| j                  |           S )Nr   r   rX   )r\   wherer  rY   ry   s    rO   rh   zrand_nonzero.<locals>.<lambda>  s)    28AFBHQag$>$>$>BB rQ   r  r  r  r  r  r  s     rO   rand_nonzeror    s#    	B	B$	ciqt	<	<	<<rQ   c                B    d }t          t          | j        d|          S )Nc                    | dz   S r   r~   ry   s    rO   rh   zrand_positive.<locals>.<lambda>  s
    1q5 rQ      r  r
   r  r  r  s     rO   rand_positiver    s"    	$	chad	;	;	;;rQ   c                :    t          t          | j        d          S )NgMbP?r  r  r  s    rO   
rand_smallr    s    	cit	4	4	44rQ         $@c                H    fd}t          t          | j        d|          S )Nc                @    | t          j        | dk               z   S r   )r\   r  )rz   offsets    rO   rh   z rand_not_small.<locals>.<lambda>  s     1rxAvw777 rQ   g      @r  r  )r  r  r  s    ` rO   rand_not_smallr    s*    	7	7	7	7$	cir	=	=	==rQ   c                :    t          t          | j        d          S )Ngh㈵>r  r
  r  s    rO   rand_small_positiver    s    	chd	3	3	33rQ           c                Z    k     sJ fd}t          t          | j        |          S )Nc                    | z
  z  z   S rS   r~   )rz   highlows    rO   rh   zrand_uniform.<locals>.<lambda>  s    1s
#c) rQ   )r  r
  )r  r  r  r  s    `` rO   rand_uniformr    s9    	t	)	)	)	)	)$	chT	2	2	22rQ   c                H      fd}t          t           j        d|          S )Nc                    |                                  }t          |          dk    r| S  j        t          j        |            dk     }t          j        ||d         |           S )Nr         ?)ravelr.  r  r\   r  r  )rz   x_ravelflipsr  s      rO   r  zrand_some_equal.<locals>.post
  sY    ggiiG
7||qhCHbhqkk"S(E8E71:q)))rQ   g      Y@r  r  r  s   ` rO   rand_some_equalr"    s7    * * * * * 
cit$	?	?	??rQ   c                8     t                      fdS )CReturn a random sampler that produces infinities in floating types.c                   t          j        |t          j                  s | |          S t          j        |t          j                  rmt          j        t          j        d|                    j        } 	| |          t          j        d|           	| |          z  z   }t          || |          S t          |           } 
j
        | dk     } 
j
        | dk     } | |          }t          j        |t          j        t          j        |          |          }t          j        |t          j        t          j         |          |          }t          t          j        ||          | |          S The random sampler function.r   rX   r  皙?)rT   r  r\   r  r  realr  rY   r  r  r  r  infr]   )r  rY   
base_dtyper   dimsposinf_flipsneginf_flipsr  	base_randr  r  s           rO   r  zrand_some_inf.<locals>.rand  sZ   eR[11 %Yue$$$%!344 /728AU33344:jT%$$Xb%  44z#:#::;cC...%  D38T?S(L38T?S(L9UE""D8L"(26"?"?"?FFD8L"(BF7%"@"@"@$GGD"*T777FFFrQ   r   r  r/  r  s   `@@rO   rand_some_infr2    sC    3)G G G G G G G, 
+rQ   c                8     t                      fdS )z=Return a random sampler that produces nans in floating types.c                   t          j        |t          j                  rmt          j        t          j        d|                    j        } 
| |          t          j        d|           
| |          z  z   }t          || |          S t          j        |t          j                  s 	| |          S t          |           } j
        | }|dk     }|dk     } 	| |          }t          j        |t          j        t          j        |          |          }t          j        |t          j        t          j         |          |          }t          t          j        ||          | |          S )r'  r   rX   r  r(  g?)rT   r  r\   r  r)  r  rY   r  r  r  r  r  r  r]   )r  rY   r+  r   r,  r  	nan_flipsneg_nan_flipsr  r/  r  r  s            rO   r  zrand_some_nan.<locals>.rand6  sU   %!344 /728AU33344:jT%$$Xb%  44z#:#::;cC...eR[11 %Yue$$$%  D$ACIHM9UE""D8Irxe<<<dCCD8M28RVG5#A#A#A4HHD"*T777FFFrQ   r0  r1  s   `@@rO   rand_some_nanr7  2  sC    3)G G G G G G G. 
+rQ   c                8     t                      fdS )r$  c                   t          j        |t          j                  s 	| |          S t          j        |t          j                  rmt          j        t          j        d|                    j        } 
| |          t          j        d|           
| |          z  z   }t          || |          S t          |           } j
        | dk     } j
        | dk     } j
        | dk     } 	| |          }t          j        |t          j        t          j        |          |          }t          j        |t          j        t          j         |          |          }t          j        |t          j        t          j        |          |          }t          t          j        ||          | |          S r&  )rT   r  r\   r  r  r)  r  rY   r  r  r  r  r*  r  r]   )r  rY   r+  r   r,  r-  r.  r5  r  r/  r  r  s            rO   r  z#rand_some_inf_and_nan.<locals>.randU  s   eR[11 %Yue$$$%!344 /728AU33344:jT%$$Xb%  44z#:#::;cC...%  D38T?S(L38T?S(L$#%I9UE""D8L"(26"?"?"?FFD8L"(BF7%"@"@"@$GGD8Irxe<<<dCCD"*T777FFFrQ   r0  r1  s   `@@rO   rand_some_inf_and_nanr:  O  sC    3)G G G G G G G0 
+rQ   c                4     t                      fd}|S )z1Return a random sampler that produces some zeros.c                    t          |           } j        | dk     } | |          }t          j        |t          j        d|          |          }t          t          j        ||          | |          S )r'  r  r   rX   )r  r  r\   r  r  r  r]   )r  rY   r,  zerosr  r/  r  s        rO   r  zrand_some_zero.<locals>.randt  sv    %  DCHdOc!E9UE""D8E28AU333T::D"*T777FFFrQ   r0  )r  r  r/  s   ` @rO   rand_some_zeror>  p  s<    3)G G G G G G 
+rQ   c                      fd}|S )Nc                V   t          j        |t           j                  r|nt           j        }dk    rJHt          j        |t           j                  rt          j        |          j        nt          d                              | |                              |          S )Nr   zArand_int requires an explicit `high` value for non-integer types.)r  r  rY   )	r\   r  integerr  r  r   r   r  ra   )r  rY   	gen_dtyper  r  r  s      rO   r   zrand_int.<locals>.fn  s    ubj99GrxI
axxDL	ubj	)	) /x" . / / 	/;;sE&  ( ((.u6rQ   r~   )r  r  r  r   s   ``` rO   rand_intrC    s/    
6 
6 
6 
6 
6 
6 
6 
)rQ   c                      fd}|S )Nc                                         t          j        pt          j        |           |          | d          S )NrX   Fr  replace)choicer\   aranger  r  )r  rY   r  r  s     rO   r   zrand_unique_int.<locals>.fn  sC    ::bi 8	%(8(8FFF %  1 1 1rQ   r~   )r  r  r   s   `` rO   rand_unique_intrJ    s)    1 1 1 1 1 1 
)rQ   c                      fd}|S )Nc           	         t          t          j         j        t	          |            dk     |          | |          S )Nr  rX   )r  r\   r]   r  r  )r  rY   r  s     rO   	generatorzrand_bool.<locals>.generator  sB    j>%001C7uEEEU  rQ   r~   )r  rM  s   ` rO   	rand_boolrN    s$         
rQ   c                    	  |              J # |$ r>}t          |                              |          sJ d| d| d            Y d }~d S d }~ww xY w)NFr*  

)rI   
startswith)thunkerr_typemsges       rO   check_raisesrV    s    7	EGGGL	 7 7 7q66S!!66#6#6#6s#6#6#66666666667s    A3AAc                    	  |              J # |$ r=}t          j        |t          |                    sJ | d| d            Y d }~d S d }~ww xY w)NFrP  r*  )r,  matchrI   )rR  rS  patternrU  s       rO   check_raises_regexprZ    s    <	EGGGL	 < < <8GSVV$$;;&;&;&;&;&;;;;;;;;;;<s    A2AAc              #  |   K   | j         E d {V  t          j        |           D ]}t          |          E d {V  d S rS   )eqnsr   	subjaxprs	iter_eqns)jaxprsubjaxprs     rO   r^  r^    sf      Z.'' # #h""""""""""# #rQ   c                .    t          j        |          | }d t          |j                  D             }|D ]\}d|  d| }t	          |t
                    r-|d         | k    s
J |            |d         | k    s
J |            L|| k    s
J |            ]d S )Nc                R    g | ]$}|j         t          j        k    |j        d          %S )	precision	primitiver   dot_general_pparamsr   eqns     rO   
<listcomp>z(assert_dot_precision.<locals>.<listcomp>  s8     7 7 7C=C$555 
;'555rQ   zUnexpected precision:  != r   r   )r   
make_jaxprr^  r_  ri   r  )expected_precisionr   rk   r_  
precisionsrc  rT  s          rO   assert_dot_precisionro    s    
#.

t
$%7 79U[3I3I 7 7 7* 2 2i
F#5
F
F9
F
FC)U## 2q\///////q\////////,,,,c,,,,2 2rQ   c                     t          j        t          |fi |          | }d t          |j                  D             }|D ]}d|  d| }| |k    s
J |            d S )Nc                R    g | ]$}|j         t          j        k    |j        d          %S )preferred_element_typerd  rh  s     rO   rj  z5assert_dot_preferred_element_type.<locals>.<listcomp>  s9     : : :3ms'888 *56888rQ   z#Unexpected preferred_element_type: rk  )r   rl  r
   r^  r_  )expectedr   rk   rl   r_  pref_eltypespref_eltyperT  s           rO   !assert_dot_preferred_element_typerv    s    
0#.////
0
0$
7%: :)EKBXBX : : :,! ( (k
K
K
Kk
K
KC{"""C""""( (rQ   c               '     K   g d}t          t          j        t          |          z            dz   }|D ];t	          |          D ])}d d| ft          fd| D                       z   V  *<d S )N)r   r  
   r   rL   c              3  .   K   | ]} |          V  d S rS   r~   )r   r  r  s     rO   r  z"cases_from_gens.<locals>.<genexpr>  s+      %@%@Ccc$ii%@%@%@%@%@%@rQ   )r   NUM_GENERATED_CASESr  r.  ranger  )genssizescases_per_sizer  r  s       @rO   cases_from_gensr    s      
**%*03u::=>>B. A Ad>"" A A^^^^%@%@%@%@4%@%@%@ @ @@@@@@AA ArQ   c              #  
  K   t                      }d}t          j        d          fd}t          |          t          j        k     r|t          j        k     r|dz  }t           | |                    }|sMt          |          dk    rt          d          |d         }|d         |v rd}|	                    |d                    |V  t          |          t          j        k     r|t          j        k     d S d S d S d S )Nr   *   c                    t          | t          t          f          st          |           } |                     t	          |                              gS rS   )ri   r  r  r  r.  )rz   r  s    rO   
choose_onez,named_cases_from_sampler.<locals>.choose_one  sD    a$'' 
q''ackk#a&&!!"##rQ   r   z@Generator is expected to only return a single case when samplingtestcase_name)
setnprRandomStater.  rz  r  _MAX_CASES_SAMPLING_RETRIESr  RuntimeErroradd)r  seenretriesr  casescaser  s         @rO   named_cases_from_samplerr    s-     	$'#$ $ $ $ $ 
T(.	.	.	.4	4	4qLGZ!!E 
5zzA~~[\\\8DO$$GHHT/"###
JJJ 
T(.	.	.	.4	4	4	4	4 
/	.	4	4 
/	.rQ   c                p    t           j                            d          }|                    | |d          S )Nr  FrF  )r\   randomr  rH  )nmr  s      rO   _choicer    s/    
	b!!#	AAu	-	--rQ   c            	     J   d | D             } d |                                 D             }t          j        d | D                       t          j        d |D                       z  }g }t          |t	          |t
          j                            D ]}i }| D ]?}|                    ||t          |          z                      |t          |          z  }@|D ]2\  }}||t          |          z           ||<   |t          |          z  }3|	                    |           |S )z%Non-decorator form of sample_product.c                ,    g | ]}t          |          S r~   r  )r   args     rO   rj  z,sample_product_testcases.<locals>.<listcomp>  s    	$	$	$$s))	$	$	$rQ   c                6    g | ]\  }}|t          |          fS r~   r  r   s      rO   rj  z,sample_product_testcases.<locals>.<listcomp>  s&    ,,,ADGG,,,rQ   c              3  4   K   | ]}t          |          V  d S rS   r.  )r   as     rO   r  z+sample_product_testcases.<locals>.<genexpr>  s(      %%1A%%%%%%rQ   c              3  :   K   | ]\  }}t          |          V  d S rS   r  )r   rL   r   s      rO   r  z+sample_product_testcases.<locals>.<genexpr>  s,      1H1HTQ#a&&1H1H1H1H1H1HrQ   )
r   r  r  r  minrz  r  updater.  append)	rk   r   r  	testcasesr  testcaser  r   r   s	            rO   sample_product_testcasesr    s5   	$	$t	$	$	$$,,,,,"
i%%%%%%%	1H1HR1H1H1H(H(HH!)1c!067788  aH  ooaCFF
m$$$CFFlaa  1a#a&&jMhqkCFFlaaX	rQ   c                 2    t          j        t          | i | S )a<  Decorator that samples from a cartesian product of test cases.

  Similar to absltest.parameterized.product(), except that it samples from the
  cartesian product rather than returning the whole thing.

  Arguments:
    *args: each positional argument is a list of dictionaries. The entries
      in a dictionary correspond to name=value argument pairs; one dictionary
      will be chosen for each test case. This allows multiple parameters to be
      correlated.
    **kw: each keyword argument is a list of values. One value will be chosen
      for each test case.
  )r   
parametersr  )rk   r   s     rO   sample_productr    s      
	!#;T#HR#H#H	IIrQ   c                       e Zd Z fdZ xZS )JaxTestLoaderc                2   t                                                    }t          j        r-t	          j        t          j                  fd|D             }t          j        r-t	          j        t          j                  fd|D             }|S )Nc                R    g | ]#}                     j         d |           !|$S r^  searchr\  r   r   rY  testCaseClasss     rO   rj  z2JaxTestLoader.getTestCaseNames.<locals>.<listcomp>  sQ     F F F..M$:!C!CT!C!CDDFt F F FrQ   c                R    g | ]#}                     j         d |           !|$S r  r  r  s     rO   rj  z2JaxTestLoader.getTestCaseNames.<locals>.<listcomp>  sQ     J J Jnn(>%G%G%G%GHHJt J J JrQ   )supergetTestCaseNames_TEST_TARGETSr  r,  compile_EXCLUDE_TEST_TARGETS)rb  r  namesrY  	__class__s    ` @rO   r  zJaxTestLoader.getTestCaseNames  s    GG$$]33E F
=.//gF F F F F F F Fe" J
0677gJ J J J J J J JeLrQ   )r\  r  r  r  __classcell__r  s   @rO   r  r    s8        
 
 
 
 
 
 
 
 
rQ   r  c                       fd}|S )z1Test case decorator for subclasses of JaxTestCasec                   t          j        |           rt          | t                    s
J d            i | _        | j        D ]!}| j                            |j                   "| j                                       | S )Nz9@with_config can only wrap JaxTestCase class definitions.)inspectisclass
issubclassJaxTestCase_default_config	__bases__r  )clsbkwdss     rO   	decoratorzwith_config.<locals>.decorator  s    ?3}JsK$@$@}}B}}}}C] 4 4	  !23333t$$$JrQ   r~   )r  r  s   ` rO   with_configr    s$         
rQ   c                6     |rt           nt           fd}|S )zDecorator that promotes the arguments of `fun` to `jnp.result_type(*args)`.

  jnp and np have different type promotion semantics; this decorator allows
  tests make an np reference implementation act more like a jnp
  implementation.
  c                 ^    t          |           \  }}t          | |           }  | i |S rS   )r1   r3   )rk   r   	flat_argstree_promoter   s       rO   wrapperz!promote_like_jnp.<locals>.wrapper/  s?    "4((OIt$) 455D3rQ   )r!   r    )r   inexactr  r  s   `  @rO   promote_like_jnpr  '  s;     (/B##N(      
.rQ   c               +  j  K   i }	 |                                  D ]1\  }}t          j        |          ||<   t          j        ||           2d V  |                                 D ]\  }}t          j        ||           d S # |                                 D ]\  }}t          j        ||           w xY wrS   )r   r   r  r  )r  original_configkeyr  s       rO   global_config_contextr  5  s      / jjll    
U#\#..ocmC	EEE%++--    
UmC   o++--    
UmC s   A
B 1B2c                      e Zd Zd ZdS )
NotPresentc                    dS )Nz<not present>r~   r  s    rO   __repr__zNotPresent.__repr__C  s    ?rQ   N)r\  r  r  r  r~   rQ   rO   r  r  B  s#            rQ   r  c               #    K   t           j        j                                        d V  t           j        j        k    rd S fd                                                                z  D             } t          d|            )Nc           	         i | ]a}|vs|vs|         |         k    |                     |t                                                     |t                                fbS r~   )r_   r  )r   r   ending_configstarting_configs     rO   r   z3assert_global_configs_unchanged.<locals>.<dictcomp>O  s     @ @ @_,,0F0F&q)]1-=== ?&&q*,,779J9J1jll9[9[\ >==rQ   z9Test changed global config values. Differing values are: )r:  r   valuescopykeysr   )	differingr  r  s    @@rO   assert_global_configs_unchangedr  G  s      J%**,,/%%%*#-%%
F@ @ @ @ @)..00=3E3E3G3GG@ @ @) 	^S\^^___rQ   c                       e Zd ZU dZddddddZdZd	ed
<    fdZed             Z	ed             Z
d ZddddddZddddddZdddZdddddddZd Zed             ZdddddddZ	 	 ddZ xZS )r  zDBase class for JAX tests including numerical checks and boilerplate.Tstrictraiseofferror)jax_enable_checksjax_numpy_dtype_promotionjax_numpy_rank_promotionjax_traceback_filteringjax_legacy_prng_keyNzExitStack | None_compilation_cache_exit_stackc                   t                                                       |                     t                                 t	          j        t          j        | j        	                                                    | _
        d S rS   )r  setUpenter_contextr  r  r  zlibadler32_testMethodNameencode_rng)rb  r  s    rO   r  zJaxTestCase.setUpg  s_    	GGMMOOO688999
 T-A-H-H-J-J K KLLDIIIrQ   c                v   t                      | _        | j        }|                    t          di | j                   t
          j        r|                    t          j        d                     |                    t          j	        d                     |                    t          j
        d                     |                    t          j        d                     |                    t          j                              }t          j        |           |                    d            d S d S )NTr   c                 (    t          j                    S rS   )r/   reset_cacher~   rQ   rO   rh   z(JaxTestCase.setUpClass.<locals>.<lambda>~  s    .:<< rQ   r~   )r   r  r  r  r  &TEST_WITH_PERSISTENT_COMPILATION_CACHEr  r   enable_compilation_cacheraise_persistent_cache_errors&persistent_cache_min_compile_time_secs%persistent_cache_min_entry_size_bytesr   TemporaryDirectoryr/   set_cache_dircallback)r  stacktmp_dirs      rO   
setUpClasszJaxTestCase.setUpClassp  s   (1C%-E	-DD0CDDEEE-3 >&9$??@@@&>tDDEEE&GJJKKK&FqIIJJJ##H$?$A$ABBg%g...nn<<=====> >rQ   c                8    | j                                          d S rS   )r  close)r  s    rO   tearDownClasszJaxTestCase.tearDownClass  s    %++-----rQ   c                    | j         S rS   )r  r  s    rO   r  zJaxTestCase.rng  s
    9rQ   r5   F)check_dtypesr   allow_object_dtypeverbosec                  |r|                      ||           t          j        |          }t          j        |          }|s/|j        t          k    s|j        t          k    rt          d          t          j        d          5  t          j                            ||||           ddd           dS # 1 swxY w Y   dS )z-Assert that x and y arrays are exactly equal.a  assertArraysEqual may be poorly behaved when np.asarray casts to dtype=object. If comparing PRNG keys, consider random_test.KeyArrayTest.assertKeysEqual. If comparing collections of arrays, consider using assertAllClose. To let this test proceed anyway, pass allow_object_dtype=True.ignore)over)r   r  N)	assertDtypesMatchr\   r]   rY   objectrx   errstatetestingassert_array_equal)rb  rz   yr  r   r  r  s          rO   assertArraysEqualzJaxTestCase.assertArraysEqual  s     #
Q"""

1A

1A JQW%6%6!'V:K:K	IJ J J 
(	#	#	# 5 5j##Aq',3 $ 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s   $B88B<?B<r  atolrtolr   c                  |                      |j        |j                   t          t          t	          |          |          t          t	          |          |                    }t          t          t	          |          |          t          t	          |          |                    }t          |||||           |r|                     ||           dS dS )z;Assert that x and y are close (up to numerical tolerances).)r  r  r   N)assertEqualr  r   r-   r%   r"   r  )rb  rz   r  r  r  r  r   s          rO   assertArraysAllClosez JaxTestCase.assertArraysAllClose  s     	QWag&&&yD))9VAYY+E+EFFDyD))9VAYY+E+EFFD1adwGGGG #
Q"""""# #rQ   canonicalize_dtypesc               @   t           j        j        s\|rZ|                     t	          j        t          |          d          t	          j        t          |          d                     d S |                     t          |          t          |                     d S )NT)allow_extended_dtype)r   r  r  r  rT   rV   r%   )rb  rz   r  r  s       rO   r  zJaxTestCase.assertDtypesMatch  s    " -': -
w1&))RVWWW1&))RVWWWY Y Y Y Y vayy&)),,,,,rQ   r  r  r  r  r   c          
        t          |t                    r|                     |t                     |                     t	          |                                          t	          |                                                     |                                D ]*}|                     ||         ||         |||||           +dS t          |          rt          |d          s| 	                    t          |          ot          |d                      |                     t          |          t          |                     t          ||          D ]!\  }	}
|                     |	|
|||||           "dS t          |d          st          j        |          r| 	                    t          |d          pt          j        |                     |r|                     |||           t          j        |          }t          j        |          }|                     ||d|||           dS ||k    rdS t#          t%          |          t%          |          f          )zEAssert that x and y, either arrays or nested tuples/lists, are close.r  	__array__r  Fr  N)ri   r   assertIsInstancer  r  r  assertAllCloser{   hasattr
assertTruer.  r  r\   isscalarr  r]   r  rx   r  )rb  rz   r  r  r  r  r  r   r   x_elty_elts              rO   r   zJaxTestCase.assertAllClose  sk    !T *
At$$$
s16688}}c!&&((mm444vvxx - -!AaD!A$\!%;N$+ 	 	- 	- 	- 	-- - 
Q *; 7 7 *
ook!nnDWQ-D-D)DEEE
s1vvs1vv&&&a)) - -,%E5|$!%;N$+ 	 	- 	- 	- 	-- - 
K	 	  *BKNN *
ooga--?Q@@@	 Nq!9LMMM
*Q--a
*Q--a
15t$(/   1 1 1 1 1	
aftAwwQ()))rQ   c                   t          j        |          }t          j        |          }t          j        d          }t          j        |d|                                          }t          j        |d|                                          }||k    rt          d| d           |                     ||d| d|            dS )zGAsserts two strings are equal, after dedenting and stripping each line.z\s*\n\s*r*  zFound
z
Expecting
)rT  N)textwrapdedentr,  r  rN   stripprintassertMultiLineEqual)rb  rs  whatignore_space_reexpected_clean
what_cleans         rO   assertMultiLineStrippedEqualz(JaxTestCase.assertMultiLineStrippedEqual  s    x((H?4  Dj--OVOT8>>3C3CDDNtzz||<<J^##dnj"ID"I"Ix"I"I  K K K K KrQ   c              #     K   t          j                    5  t          j        d           d V  d d d            d S # 1 swxY w Y   d S )Nr  )warningscatch_warningssimplefilterr  s    rO   assertNoWarningszJaxTestCase.assertNoWarnings  s      		 	"	"  G$$$eee                    <A A )r  r   r  r  check_cache_missesc                    |            } fd}	d | }
t          d |
          }t          d |
          }                     ||           t          j                                        j        } | }
|rK                     |t          j                                        j        d                                         t          j        |	          }d || }d || } 	                    |
|||p||p|            	                    |
|||p||p|            |            }d | }
d || } 	                    |
|||p||p|           dS )	zBHelper method for running JAX compilation and allclose assertions.c                 8                                     |  S rS   )r"  )rk   r   python_should_be_executingrb  s    rO   wrapped_funz1JaxTestCase._CompileAndCheck.<locals>.wrapped_fun  s"    
oo0111S$ZrQ   Tc                *    t          j        |           S rS   )r\   r  ry   s    rO   rh   z.JaxTestCase._CompileAndCheck.<locals>.<lambda>  s    rx{{ rQ   c                N    t          j        t          j        |                     S rS   )r\   r  r]   ry   s    rO   rh   z.JaxTestCase._CompileAndCheck.<locals>.<lambda>  s    28BJqMM#:#: rQ   z?Compilation detected during second call of {} in op-by-op mode.F)r  r  r  N)
r2   r  r   r   r   r   r  r   jitr   )rb  r   
args_makerr  r   r  r  r7  rk   r;  
python_anspython_shapes	np_shapescache_missescfunmonitored_anscompiled_ansr:  s   ``               @rO   _CompileAndCheckzJaxTestCase._CompileAndCheck  s    :<<D       "&dJ22J??M::JGGI]I...2==??FLdJ 

7BBDDK&++  
 7;D!%D$KM!&4;L
M![St{s  < < <
L|![St{s  < < < :<<D!%dJ!&4;L
L|![St{s  < < < < <rQ   c	                l     |            }	 ||	 }
 ||	 }|                      ||
||p||p||           d S )N)r  r  r  r  )r   )rb  numpy_reference_oplax_opr?  r  r   r  r  r  rk   lax_ans	numpy_anss               rO   _CheckAgainstNumpyzJaxTestCase._CheckAgainstNumpy  si     :<<DfdmG""D)I	7![St{s,?  A A A A ArQ   )TNNNT)r\  r  r  __doc__r  r  __annotations__r  classmethodr   r  r  r  r  r  r   r0  r	   r5  rG  rM  r  r  s   @rO   r  r  V  s        LL!) '$" / 598888M M M M M > > ;> . . ;.   59"+0$5 5 5 5 5* 8<$ $b
# 
# 
# 
# 
# <@ - - - - - 26Dt)-r* * * * *<K K K   >
 ?C t-< -< -< -< -<` GK-1A A A A A A A ArQ   r  r>  c                    | S rS   r~   )rz   rk   rl   s      rO   rh   rh     s    a rQ   noopc                      e Zd Zd Zd ZdS )BufferDonationTestCasec                T    |                      |                                           d S rS   )r"  
is_deletedrb  rz   s     rO   assertDeletedz$BufferDonationTestCase.assertDeleted$  s"    OOALLNN#####rQ   c                T    |                      |                                           d S rS   )assertFalserV  rW  s     rO   assertNotDeletedz'BufferDonationTestCase.assertNotDeleted'  s$    Q\\^^$$$$$rQ   N)r\  r  r  rX  r[  r~   rQ   rO   rT  rT  #  s2        $ $ $% % % % %rQ   rT  messagecategoryc              +     K   t          j                    5  t          j        d| |d| d V  d d d            d S # 1 swxY w Y   d S )Nr	  r\  )r	  )r2  r3  filterwarnings)r]  r^  r   s      rO   ignore_warningra  +  s         
 
OgOOBOOO	EEE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
r6  named_shapeMeshSpecGenerator[None, None, None]c              #    K   t          |           \  }}t          j        |          }t          t	          j                              }t          |          |k     rt          j        d| d          t          j
        |d|                                       |          }t          j                            ||          5  dV  ddd           dS # 1 swxY w Y   dS )zDTest utility for setting up meshes given mesh data from `schedules`.Test requires z local devicesN)r.   r  r  r  r:  rL  r.  r`  ra  r\   r  r  shardingMesh)rb  
axis_namesr  r  rL  mesh_devicess         rO   	with_meshrk  5  s      [))*e	5		$s(**++-$

ATAAA
B
BB-.//77>>,
|z22 
 
	EEE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   CCCc                      fdS )Nc                 L      t          |d                             | i |S )Nmesh)rk  )rk   rl   rn   s     rO   rh   z'with_mesh_from_kwargs.<locals>.<lambda>C  s.    !=!:6&>!:!:1!=!=t!Nv!N!N rQ   r~   rn   s   `rO   with_mesh_from_kwargsrp  B  s    	N	N	N	NNrQ   c                j     t          j        d dD                       t          |                     S )Nc              3  *   K   | ]\  }}}|||d V  dS ))r  rn  axis_resourcesNr~   )r   r   rn  rs  s       rO   r  z(with_and_without_mesh.<locals>.<genexpr>F  sF       ( ("dN DNKK( ( ( ( ( (rQ   ))r5   r~   r~   )rh  ))rz   r	  ))r  rz   )r   named_parametersrp  ro  s    rO   with_and_without_meshru  E  sP    
	' ( ('( ( ( 
 

 Q
! 
! !rQ   c                   t          j        |           }t          t          j                              |k     rt          j        d| d          t          t          j                    d           }t          j	        |d |                   
                    |           }t          j                            ||          }|S )Nrf  z global devices.c                    | j         S rS   )id)rN  s    rO   rh   z$create_global_mesh.<locals>.<lambda>Q  s     rQ   )r  )r  r  r.  r:  r;  r`  ra  sortedr\   r  r  rg  rh  )
mesh_shaperi  r  r;  rj  global_meshs         rO   create_global_meshr|  M  s    	:		$$

CTCCC
D
DD3;==nn555''%4%.))11*==,!!,
;;+	rQ   c                  .    e Zd Z e            Zd Zd ZdS )_cached_propertyc                ,    || _         | j        | _        d S rS   )_methodnull_value)rb  methods     rO   __init__z_cached_property.__init__Z  s    DL)DKKKrQ   c                `    | j         | j        u r|                     |          | _         | j         S rS   )r  r  r  )rb  objr  s      rO   __get__z_cached_property.__get__^  s,    {diLL%%dk;rQ   N)r\  r  r  r  r  r  r  r~   rQ   rO   r~  r~  W  s=        	$      rQ   r~  c                  6   e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zed             Zed             Zed             Zed             ZdS )_LazyDtypeszA class that unifies lists of supported dtypes.

  These could be module-level constants, but device_under_test() is not always
  known at import time, so we need to define these lists lazily.
  c                h    t                       t          |          fd|D                       S )Nc              3  $   K   | ]
}|v |V  d S rS   r~   )r   rN  	supporteds     rO   r  z(_LazyDtypes.supported.<locals>.<genexpr>l  s'      <<aQ)^^^^^^<<rQ   )r  r  )rb  r   r  s     @rO   r  z_LazyDtypes.supportedj  s9     ""I4<<<<<<6<<<<<<rQ   c                    d t           j        t           j        t           j        t           j        t           j        t           j        fD             S )Nc                6    g | ]}t          j        |          S r~   r   )r   ts     rO   rj  z-_LazyDtypes.custom_floats.<locals>.<listcomp>p  s,     5 5 5ABHQKK 5 5 5rQ   )rT   r  float8_e4m3b11fnuzfloat8_e4m3fnfloat8_e4m3fnuzfloat8_e5m2float8_e5m2fnuzr  s    rO   custom_floatsz_LazyDtypes.custom_floatsn  sF    5 52W472"4 5 5 5 5rQ   c                X    |                      t          j        t          j        g          S rS   )r  r\   r  r  r  s    rO   r  z_LazyDtypes.floatingu  s    >>2:rz2333rQ   c                    |                      t          j        t          j        t          j        t          j        g          S rS   )r  rT   r  r\   r  r  r  r  s    rO   all_floatingz_LazyDtypes.all_floatingy  s&    >>7+RZRZPQQQrQ   c                X    |                      t          j        t          j        g          S rS   )r  r\   r  r  r  s    rO   rA  z_LazyDtypes.integer}  s    >>28RX.///rQ   c                    |                      t          j        t          j        t          j        t          j        t          j        g          S rS   )r  rT   int4r\   r  r  r  r  r  s    rO   all_integerz_LazyDtypes.all_integer  s2    >>bgrx28= > > >rQ   c                X    |                      t          j        t          j        g          S rS   )r  r\   r  r  r  s    rO   unsignedz_LazyDtypes.unsigned  s    >>29bi0111rQ   c                    |                      t          j        t          j        t          j        t          j        t          j        g          S rS   )r  rT   uint4r\   r  r  r  r  r  s    rO   all_unsignedz_LazyDtypes.all_unsigned  s6    >>rxBIryB C C CrQ   c                X    |                      t          j        t          j        g          S rS   )r  r\   r  r  r  s    rO   complexz_LazyDtypes.complex  s    >>2<7888rQ   c                B    |                      t          j        g          S rS   )r  r\   r
  r  s    rO   booleanz_LazyDtypes.boolean  s    >>28*%%%rQ   c                     | j         | j        z   S rS   )r  r  r  s    rO   r  z_LazyDtypes.inexact  s    =4<''rQ   c                     | j         | j        z   S rS   )r  r  r  s    rO   all_inexactz_LazyDtypes.all_inexact  s    t|++rQ   c                @    | j         | j        z   | j        z   | j        z   S rS   )r  rA  r  r  r  s    rO   numericz_LazyDtypes.numeric  s    =4<'$-7$,FFrQ   c                P    | j         | j        z   | j        z   | j        z   | j        z   S rS   )r  r  r  r  r  r  s    rO   allz_LazyDtypes.all  s1     0043DDL<( )rQ   N)r\  r  r  rN  r  r~  r  r  r  rA  r  r  r  r  r  r  r  r  r  r~   rQ   rO   r  r  d  s        
= = = 5 5 5 4 4 4 R R R 0 0 0 > > > 2 2 2 C C C 9 9 9 & & & ( ( ( , , , G G G ) ) ) ) )rQ   r  c                     t           fd D                       rt          j        d          S t          j        d          S )zv
  Context manager to enable strict promotion if all dtypes match,
  and enable standard dtype promotion otherwise.
  c              3  0   K   | ]}|d          k    V  dS r   Nr~   )r   rY   r   s     rO   r  z3strict_promotion_if_dtypes_match.<locals>.<genexpr>  s,      00&)	000000rQ   r  standard)r  r:  numpy_dtype_promotionr   s   `rO    strict_promotion_if_dtypes_matchr    sL    
 	000000000 /$X...		":	.	..rQ   z$([0-9]+(?:\.[0-9]+)*)(?:(rc|dev).*)?r   tuple[int, ...]c                    t                               |           }|t          d|  d          t          d |                    d                              d          D                       S )NzUnable to parse version ''c              3  4   K   | ]}t          |          V  d S rS   )r   )r   rz   s     rO   r  z parse_version.<locals>.<genexpr>  s(      55!s1vv555555rQ   r   r^  )_version_regexrX  r   r  groupr+  )r   r  s     rO   parse_versionr    si    1!Y
5555
6
66	55qwwqzz//44555	5	55rQ   c                 4    t          t          j                  S rS   )r  r\   __version__r~   rQ   rO   numpy_versionr    s    	r~	&	&&rQ   )r  one_containingrl   Sequence[dict[str, Any]]r  &Callable[[dict[str, Any]], str] | Noner  
str | Nonec                   fd| D             }np| D ]k                     d          @d                    fdt                                                    D                       t	                    d<   l| }mt          fd|D                       }|s.J d dd	                    d
 |D                       z               |d         dd<   t          j        g          S t          j        | S )a  Decorator for named parameterized tests, with filtering support.

  Works like ``parameterized.named_parameters``, except that it sanitizes the test
  names so that we can use ``pytest -k`` and ``python test.py -k`` test filtering.
  This means, e.g., that many special characters are replaced with `_`.
  It also supports the ``one_containing`` arg to select one of the tests, while
  leaving the name unchanged, which is useful for IDEs to be able to easily
  pick up the enclosing test name.

  Usage:
     @jtu.parameterized_filterable(
       # one_containing="a_4",
       [dict(a=4, b=5),
        dict(a=5, b=4)])
     def test_my_test(self, *, a, b): ...

  Args:
    kwargs: Each entry is a set of kwargs to be passed to the test function.
    testcase_name: Optionally, a function to construct the testcase_name from
      one kwargs dict. If not given then ``kwargs`` may contain ``testcase_name`` and
      otherwise the test case name is constructed as ``str(kwarg)``.
      We sanitize the test names to work with -k test filters. See
      ``sanitize_test_name``.
    one_containing: If given, then leaves the test name unchanged, and use
      only one of the ``kwargs`` whose `testcase_name` includes ``one_containing``.
  Nc                r    g | ]3}t          dd t          t           |                              i|4S )r  r~   )r   rP   rI   )r   r   r  s     rO   rj  z,parameterized_filterable.<locals>.<listcomp>  sW     ! ! !
 JJ+Cb0A0A,B,BCCJrJJ! ! !rQ   r  rL   c              3  2   K   | ]}| d |          V  dS )=Nr~   )r   r   r   s     rO   r  z+parameterized_filterable.<locals>.<genexpr>  sE       != !=%& %&1 != != != != != !=rQ   c              3  0   K   | ]}|d          v |V  dS r  Nr~   )r   r   r  s     rO   r  z+parameterized_filterable.<locals>.<genexpr>  s>       ? ?B&"_*=== ====? ?rQ   zNo testcase_name contains 'z"'. The testcase_name values are
  z
  c              3  &   K   | ]}|d          V  dS r  r~   )r   r   s     rO   r  z+parameterized_filterable.<locals>.<genexpr>  s'      JJ""_%JJJJJJrQ   r   r5   )r_   r  ry  r  rP   r  r   rt  )rl   r  r  kwargs_with_testcase_namefilteredr   s    ``  @rO   parameterized_filterabler    s   B ! ! ! !! ! !  > >ff_--m		 != != != !=*0*;*;!= != != = =.}==b & ? ? ? ?"; ? ? ? ? ?H L L)N ) ) )kkJJ0IJJJJJKL L L 
!BB)2$///)+DEErQ   c              #     K   	 t          j        |            dV  t          j        |            dS # t          j        |            w xY w)zFManages registering/unregistering an event duration listener callback.N)r   %register_event_duration_secs_listener/_unregister_event_duration_listener_by_callback)r  s    rO    register_event_duration_listenerr    sT      I4X>>>	EEE>xHHHHHJ>xHHHHs	   2 Ac               +    K   d | D             }t           j                            d |                                 D                        	 dV  d | D             }t           j                            d |                                D                        dS # d | D             }t           j                            d |                                D                        w xY w)a  Context manager to temporarily set/unset one or more environment variables.

  Examples:

    >>> import os
    >>> os.environ['my_var'] = 'original'

    >>> with set_env(my_var=None, other_var='some_value'):
    ...   print("my_var is set:", 'my_var' in os.environ)
    ...   print("other_var =", os.environ['other_var'])
    ...
    my_var is set: False
    other_var = some_value

    >>> os.environ['my_var']
    'original'
    >>> 'other_var' in os.environ
    False
  c                P    i | ]#}|t           j                            |d           $S rS   r   r~  popr   r  s     rO   r   zset_env.<locals>.<dictcomp>  s*    ???c2:>>#t,,???rQ   c                    i | ]
\  }}|||S rS   r~   r   s      rO   r   zset_env.<locals>.<dictcomp>  s    FFFdaQrQ   Nc                N    g | ]"}t           j                            |d           #S rS   r  r  s     rO   rj  zset_env.<locals>.<listcomp>  s(    555sT	"	"555rQ   c                    i | ]
\  }}|||S rS   r~   r   s      rO   r   zset_env.<locals>.<dictcomp>  s    JJJ1AMq!MMMrQ   )r   r~  r  r   )rl   originalrL   s      rO   set_envr    s      * @????(*FFfllnnFFFGGGL	EEE55f555AJJJ(8(8JJJKKKKK 	65f555AJJJ(8(8JJJKKKKs   B A	C!c                      t          j         fdd          | \  }\  }} t          j        d           ||          }||fS )Nc                 (    t          j        g| R  S rS   )r:  vjp)rk   rn   s    rO   rh   z fwd_bwd_jaxprs.<locals>.<lambda>#  s    CGA%%%% rQ   T)return_shapec                     | |          S rS   r~   )resoutss     rO   rh   z fwd_bwd_jaxprs.<locals>.<lambda>$  s    ss4yy rQ   )r:  rl  )rn   example_args	fwd_jaxpry_shape	res_shape	bwd_jaxprs   `     rO   fwd_bwd_jaxprsr  !  sh    %:CN%%%%D%: %: %:;G%I!)!gy9cn8899)WMM)	I	rQ   c           	     H   t                      dk    rt          d          t          j        | |d          } t          j        ||d          }t          j        | |          \  } }t          j        t          j        | ddddf                   |d                   d         S )z;Implementation of numpy.vecdot for testing on numpy < 2.0.0)r	  r   r   z0should be calling vecdot directly on numpy 2.0.0r   .N).N).r   r   )r  r   r\   moveaxisbroadcast_arraysmatmulconj)rz   r  axiss      rO   numpy_vecdotr  (  s    __	!!
G
H
HHk!T2!k!T2!		Q	"	"$!Q	271S$\?++Qy\	:	:9	EErQ   rx  c                   ||}t          j        |           fd} ||          } ||          }|                    ddd|z  z   f                              dd|z  z   d                              |           }|                    d                              |           }d|j        dd<   |                    dd|z  z   df                              dd|z  z   d          }||z   S )a  Return a 2-D array of complex numbers that covers the complex plane
     with a grid of samples.

     The size of the grid is (3 + 2 * size_im) x (3 + 2 * size_re)
     that includes infinity points, extreme finite points, and the
     specified number of points from real and imaginary axis.

     For example:

     >>> print(complex_plane_sample(np.complex64, 0, 3))
     [[-inf          -infj   0.          -infj  inf          -infj]
      [-inf-3.4028235e+38j   0.-3.4028235e+38j  inf-3.4028235e+38j]
      [-inf-2.0000000e+00j   0.-2.0000000e+00j  inf-2.0000000e+00j]
      [-inf-1.1754944e-38j   0.-1.1754944e-38j  inf-1.1754944e-38j]
      [-inf+0.0000000e+00j   0.+0.0000000e+00j  inf+0.0000000e+00j]
      [-inf+1.1754944e-38j   0.+1.1754944e-38j  inf+1.1754944e-38j]
      [-inf+2.0000000e+00j   0.+2.0000000e+00j  inf+2.0000000e+00j]
      [-inf+3.4028235e+38j   0.+3.4028235e+38j  inf+3.4028235e+38j]
      [-inf          +infj   0.          +infj  inf          +infj]]

  Nc                &   d}j         |z  x}}j        |z  }t          j        dd| z  z   j                  }t          j                    5  t          j        d           t          j        ||| j                   }|d d d          }|||  dz
  d<   ||d| dz   <   d d d            n# 1 swxY w Y   | dk    rj	        |d<   j
        |d<   | d	k    rj         || <   j        ||  dz
  <   t          j         |d	<   t          j        |d<   |S )
Ngry	O
@r  r	  rX   r	  r   r   r   )maxexpminexpr\   r=  rY   r2  r3  r4  logspacer  r   tinyr*  )	r  prec_dps_ratiologminlogmaxlogtinyaxis_pointshalf_neg_line	half_linerU   s	           rO   make_axis_pointsz.complex_plane_sample.<locals>.make_axis_pointsL  sv   'Nl^33FVl^+G(1q4x<u{;;;K		 	"	" . .H%%%{67DLLLLm 2&&i"+k4%!)B, -k!D1H*. . . . . . . . . . . . . . . axxyk!n	k"oaxx :+k$$zk4%!)fWKNfKOs   AB..B25B2r   r  r	  r   r   )r\   rU   r  repeatra   r  r)  )	rY   size_resize_imr  real_axis_pointsimag_axis_points	real_part	imag_partrU   s	           @rO   complex_plane_sampler  2  s   , _G
(5//%    0 &%g..%%g..&&AGO'<==DDQW_VWXX__`eff)%%a((--e44)).Q[" 566==a!g+oqQQ)	Y	rQ   c                       e Zd ZdZ eddddd          Zd e                                D             Z ed	d
          Z edddd          Z	 edddd          Z
 fdZd Zd Zd Z fdZ xZS )vectorize_with_mpmathzLSame as numpy.vectorize but using mpmath backend for function evaluation.
  	complex32r  r  
complex256clongdouble)r  r  r  float128
longdoublec                    i | ]\  }}||	S r~   r~   r   s      rO   r   z vectorize_with_mpmath.<dictcomp>u  s    HHH41a!QHHHrQ      5   )r  r  iiii)r  r  r  r	        i   i @  c                   |                     dd           }|t          d          |                     dd          | _        |                     dd          | _        || _        t                      | _        t                      | _        | j        	                                D ]>\  }}| j        j
                                        }||_        || j        |<   || j        |<   ? t                      j        |i | d S )Nmpmathz3vectorize_with_mpmath: no mpmath argument specifiedextra_prec_multiplierr   
extra_prec)r  r   r  r  r  r   contextscontexts_inv
float_precr   mpcloneprecr  r  )rb  rk   rl   r  	fp_formatr  r   r  s          rO   r  zvectorize_with_mpmath.__init__  s    ZZ$''F~LMMM!',CQ!G!GDjjq11DODKFFDMD?0022 ) )	4KN  ""cch!$dmI(dEGGd%f%%%%%rQ   c                &   t          |t          j        t          j        t          j        f          r<t          |j                  }| j                            ||          }| j	        |         S t          dt          |          j         d          )Nzget mpmath context from z	 instance)ri   r\   r  r  r  rI   rY   map_complex_to_floatr_   r  NotImplementedErrorr  r\  )rb  rz   r  s      rO   get_contextz!vectorize_with_mpmath.get_context  sw    !bj"+r/ABCC &ag,,i+//	9EEi]9%%
Ta9ITTT
U
UUrQ   c                8   t          |t          j                  rXt          j        t	          | j        |                                          t                                        |j	                  S t          |t          j
                  ru| j        }|                     |          }|j        \  }}t          j        |          r|                    |j        j                  S t          j        |          r|                    |j        j                  S t          j        |          r|                    |j        j                  S t          j        |          rt          j        |          \  }}t1          t          j        ||                    }t1          ||z
            }	|                    |j                            ||	||                    }
|                    |
          sJ |
j                    |
S ntt          |t          j                  rZ|                     |j                  |                     |j                  }}|j                             |j        |j        f          S tC          dtE          |          j#         d          )zKConvert numpy array/scalar to an array/instance of mpmath number type.
    rX   convert z instance to mpmath number type)$ri   r\   r  fromitermapnptompflattenr  r  r  r  r  r  _prec_roundingisposinfmake_mpflibmpfinfisneginffninfr  fnanisfinitefrexpr   ldexpfrom_man_exp_mpf_r  r)  imagcontextmake_mpcr  r  r\  )rb  rz   r  r   r  roundingmantissaexponentmanexpr  r,  ims                rO   r#  zvectorize_with_mpmath.nptomp  s    !RZ   7[T[!))++66fEEEMMagVVV	Ar{	#	# 7{fQc)ndH	Q ||FL-...;q>> 
||FL.///8A;; ||FL-...;q>> Xa[[("(8T**++(T/""LL223T8LLMM||A''''' 
Ar)	*	* 7{{16""DKK$7$7"bZ  "(BH!5666
Za)9ZZZ
[
[[rQ   c                p   t          |t          j                  r|j        j        dk    r|                                }|d         }|j        }| j        |         }t          ||j                  r!t          t          | j
        |                   }n-t          ||j                  rt          t          |          }nd}|At          j        t          | j        |          |                              |j                  S nt          || j        j        j                  ru|j        }t          ||j                  r| j        |         }t          t          | j
        |                   } |                                d                              t          t          |                    }|                     |j                  |d<   |                     |j                  |d<   |                    |          d         S t          ||j                  r~| j        |         }t          t          |          }|                    |          r | j        j        j        g |j        |j        R  \  }}	}
}|dk    sJ ||	|
||j        f            |
|z   | j        |         k     r|r|j         n|j        S |
|z   | j        |         k    r|r|j        n|j         S  ||r|	 n|	          }	t          j!        |	|
          }t          j        |          sJ |||j        |	f            |S |"                    |          r |t          j#                  S |$                    |          r/ ||j        d         rt          j          nt          j                   S tK          dtM          |           d          )z8Convert mpmath instance to numpy array/scalar type.
    Or   NrX   r   r   z& instance to numpy floating point type)'ri   r\   r  rY   r`   r$  r3  r  mpcr_  map_float_to_complexmpfr!  r"  mptonpr  r  r  ctx_mp	mpnumericr  r)  r2  r-  r(  	normalizer1  r%  float_minexpzerofloat_maxexpninfr*  r/  r  r  isinfr  r  )rb  rz   x_flatr  r   r  rY   r  signr8  r9  bcs               rO   r@  zvectorize_with_mpmath.mptonp  sf    !RZ   ':QW\S%8%8yy{{fAYdLc#C(i	D#'	"	" D5i@AAdCG$$ I&&		{3t{F335AAAII!'RRR 
	At{)3	4	4 :Ic	Asw		 :%c*	D5i@AAEGGOOA##GB	$:$:;;{{16""!{{16""!vve}}Qa!! :%c*	I&&<<?? 	::t{0:YAGYcFXYYY
$S"q4c2qw72X))444 $2CH99#(22X))444#03880d+sdd,,#hsC  !Q
5
5!Q!5
5
5
5(YYq\\ 	:rv
YYq\\ 	:!'!*8ww"&99
9
XaXXX
Y
YYrQ   c                &   g }d }|D ]}t          |t          j        t          j        t          j        f          rZ|                    |                     |                     ||                     |          }s||                     |          u sJ |                    |           t          |j	        | j
        z            | j        z   }|                    |          5   t                      j        |i |}d d d            n# 1 swxY w Y   t          |t                    rg }|D ]u}	t          |	t          j                  r|	j        j        dk    st          |	| j        j        j                  r|                     |	          }	|                    |	           vt          |          S t          |t          j                  r|j        j        dk    st          || j        j        j                  r|                     |          S |S )Nr<  )ri   r\   r  r  r  r  r#  r  r   r  r  r  	extraprecr  __call__r  rY   r`   r  rA  rB  r@  )rb  rk   rl   mp_argsr3  r  r  rm   lstr  r  s             rO   rN  zvectorize_with_mpmath.__call__  s0   GG  	A
BK1CD	E	E t{{1~~&&&?$$Q''''D,,Q///////qW\D$>>??$/QJ			:	&	& 4 4uww3F33f4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 &%   c  !2:&& 	17<3+>+>!T[/9:: ,?kk!nn!

13ZZ	FBJ	'	' !FL,=,D,Dfdk0:;; -E[[   Ms   $D

DD)r\  r  r  rN  r   r>  r   r  r  rD  rF  r  r  r#  r@  rN  r  r  s   @rO   r  r  p  sD         k;P\gs  AN  O  O  OHH+?+E+E+G+GHHHt  * 	  , 	  ,& & & & &"V V V\ \ \6+Z +Z +ZZ        rQ   r  c                      e Zd ZdZg dZ edddddddd	d
	  	        ZddZd Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )numpy_with_mpmathzNamespace of universal functions on numpy arrays that use mpmath
  backend for evaluation and return numpy arrays as outputs.
  )r  absolutesqrtr9  expm1exp2loglog1plog10log2sincostanarcsinarccosarctansinhcoshtanharcsinharccosharctanhsquarepositivenegative	conjugaterJ  sincrC  absminlnasinacosatanasinhacoshatanh)	r  rS  rW  r^  r_  r`  rd  re  rf  r   c                    || _         | j        D ]e}| j                            ||          }t	          | |          rt          | |          }n|fd}t          | |t          ||||                     fd S )Nc                >     t          | j        |          |           S rS   )r_  r3  )rz   mp_names     rO   opz&numpy_with_mpmath.__init__.<locals>.op-  s    ,G,,Q//
/rQ   )r  r  r  )r  	_provides	_mp_namesr_   r!  r_  setattrr  )rb  r  r  r  r   rv  rw  s          rO   r  znumpy_with_mpmath.__init__#  s    DK 
H 
H""4..g	t		 0T4   " 	0 	0 	0 	0 dD/6Yn  |F  G  G  G  H  H  H  H
H 
HrQ   c                    ||z  S rS   r~   rW  s     rO   rg  znumpy_with_mpmath.square6  s    q5LrQ   c                    |S rS   r~   rW  s     rO   rh  znumpy_with_mpmath.positive9  s    HrQ   c                    | S rS   r~   rW  s     rO   ri  znumpy_with_mpmath.negative<  s	    2IrQ   c                    |j         }t          ||j                  rE|                    |j                  r+|                    |j        j        |j        j        f          S |                    |          S rS   )	r3  ri   r=  rH  r2  r4  r*  r1  rT  rb  rz   r   s      rO   rT  znumpy_with_mpmath.sqrt?  sd    
)C!SW ; 
16		 ;||SW]AFL9:::88A;;rQ   c                6    |j                             |          S rS   )r3  rU  rW  s     rO   rU  znumpy_with_mpmath.expm1I  s    9??1rQ   c                   |j         }t          ||j                  rB|                    |j                  r'|                    |j                  r|j        }|j        dk    r4|j        dk    r)|                    |j        j        |dz  j        f          S |j        dk    r5|j        dk     r*|                    |j        j        | dz  j        f          S |j        dk     r8|j        dk     r-|                    |j         j        d|z  dz  j        f          S |j        dk     r8|j        dk    r-|                    |j         j        d|z  dz  j        f          S |	                    |          S )Nr      r  )
r3  ri   r=  rH  r)  r2  pir4  r1  rX  )rb  rz   r   r  s       rO   rX  znumpy_with_mpmath.log1pL  sL   
)C!SW E 
16		 	Esyy00 	EV6A::!&1**qv|b1f^<==
=6A::!&1**qv|rcAg_=>>
>6A::!&1**b10CDEE
E6A::!&1**R!0BCDD
D99Q<<rQ   c                   |j         }t          ||j                  r|                    |j                  r|                    |j                  s|                    |j                  rb|j        dk    r+|                    |j        j	        |j
        j	        f          S |                    |j        j	        |j
         j	        f          S |                    |j                  rE|                    |j                  r+|                    |j        j	        |j        j	        f          S |                    |          S r   )r3  ri   r=  rH  r2  r)  r-  r4  rE  r1  oner  r]  r  s      rO   r]  znumpy_with_mpmath.tan^  s   
)C!SW 	< 
16		 @		!& 1 1 @S\\!&5I5I @6A::sx~sw}=>>
>||SX^swh-=>???	16		 <s||AF33 <||SW]CGM:;;;771::rQ   c                   |j         }t          ||j                  r|                    |j                  r|                    |j                  s|                    |j                  rb|j        dk    r+|                    |j        j	        |j
        j	        f          S |                    |j        j	        |j
         j	        f          S |                    |j                  rE|                    |j                  r+|                    |j        j	        |j        j	        f          S |                    |          S r   )r3  ri   r=  rH  r2  r)  r-  r4  rE  r1  r  r  rc  r  s      rO   rc  znumpy_with_mpmath.tanhl  s   
)C!SW 	< 
16		 @		!& 1 1 @S\\!&5I5I @6A::sx~sw}=>>
>||SX^swh-=>???	16		 <s||AF33 <||SW]CGM:;;;88A;;rQ   c                P    |j                             |          |j         j        z  S rS   )r3  rm  ln2rW  s     rO   rZ  znumpy_with_mpmath.log2z  s    9<<??QY]**rQ   c                P    |j                             |          |j         j        z  S rS   )r3  rm  ln10rW  s     rO   rY  znumpy_with_mpmath.log10}  s    9<<??QY^++rQ   c                P    |j                             ||j         j        z            S rS   )r3  r9  r  rW  s     rO   rV  znumpy_with_mpmath.exp2  s    9==QY]*+++rQ   c                   |j         }t          ||j                  r|j        }|j        }|j        }|                    |j                  rd|j        dk     rdnd}||z  |                    |j                  rdndz  }|j        dk     r| n|}|	                    |j
        |j
        f          S |                    |j                  r&|	                    |j
        |j        j
        f          S |j        dk    r2|j        dk    r'|                    |                                          S |                    |          S Nr   r   r   r  r	  )r3  ri   r=  r  r*  rE  rH  r)  r2  r4  r1  rn  rj  )	rb  rz   r   r  r*  rE  	sign_realr)  r2  s	            rO   r^  znumpy_with_mpmath.arcsin  s(   
)C!SW '
 6bGcXd	16		 8&1**BB!	2~cii&7&7!>Q?vzztts||TZ455599QV 8||TZ6777 
!!xx{{$$&&&88A;;rQ   c                   |j         }t          ||j                  r|j        }|j        }|j        }|                    |j                  rd|j        dk     rdnd}|j        dk     r| n|}||z  |                    |j                  rdndz  }|	                    |j
        |j
        f          S |                    |j                  r&|	                    |j        j
        |j
        f          S |j        dk    r3|j        dk     r(|                    |                                           S |                    |          S r  )r3  ri   r=  r  r*  rE  rH  r2  r)  r4  r1  rq  rj  )	rb  rz   r   r  r*  rE  	sign_imagr)  r2  s	            rO   rd  znumpy_with_mpmath.arcsinh  s*   
)C!SW +
 6bGcXd	16		 8&1**BB!	vzztts2~cii&7&7!>Q?||TZ455599QV 8||QV\4:6777 
1"1((***99Q<<rQ   c                   d }|j         }t          |          }t          ||j                  r ||||j        |j        |j                  \  }} ||||j        |j        |j                  \  }	}
|                    |j        |	j        f          |                    |j        |
j        f          fS t          ||j                  r ||||||          S J )zhNormalize reference and value using precision defined by the
    difference of exact and reference.
    c           
        |j         \  }}}}|j         \  }	}
}}|j         \  }}}}|j         \  }}}}|                     |          r*|                     |          r|                     |          s||fS t          ||||          }|||z
  z  }|
||z
  z  }|||z
  z  }|||z
  z  }|rt          |          dd          nd}|rt          |          dd          nd}|rt          |          dd          nd}|rt          |          dd          nd}t	          t          |          t          |          t          |          t          |                    }d|t          |          z
  z  |z   }d|t          |          z
  z  |z   }d}t          ||          D ]\  } }!| |!k    r n|dz  }||z
  }"||"z	  }#||"z	  }$|#r7|                     ||#| t          t          |#                    dz
  f          n|r| j         n| j        }%|$r7|                     ||$| t          t          |$                    dz
  f          n|r| j         n| j        }&|%|&fS )Nr	  r5   0r   r   )	r1  r-  r  binr   r.  r  r'  rE  )'r   rH   rU  r  r   sssmsesbcesemeeebcrsrmr,  rbcvsvmvevbcmesm_eem_erm_evm_esm_bem_brm_bvm_br  c1b0b1c0rm_mvm_mnrnvs'                                          rO   workerz+numpy_with_mpmath.normalize.<locals>.worker  s   ob"b#ob"b#ob"b#ob"b#ll1oo #,,q// cll1oo !tr2r2b BG_dBG_dBG_dBG_d #*SYYqrr]]d"*SYYqrr]]d"*SYYqrr]]d"*SYYqrr]]d
c$iiTCIIs4yy
9
9aAD		M"T)dAD		M"T)dbdOO  &"b88
%
ar6b RZdRZd AEi3<<TB3CII(:;<<<XZKhCH99`c`hb@Di3<<TB3CII(:;<<<XZKhCH99`c`hbVmrQ   )	r3  r  ri   r=  r)  r2  r4  r1  r?  )rb  exact	referencer  r  r   r  rrrvirivs              rO   rC  znumpy_with_mpmath.normalize  s    * * *X -CJJE%!! vc5%*inejIIfb"vc5%*inejIIfb"\\28RX.//rx>R1S1SSS	E37	#	# VCy%888hrQ   N)r   r   )r\  r  r  rN  rx  r   ry  r  rg  rh  ri  rT  rU  rX  r]  rc  rZ  rY  rV  r^  rd  rC  r~   rQ   rO   rR  rR    sG          ) d8&Wg	  )H H H H&            $    + + +, , ,, , ,  6  69 9 9 9 9rQ   rR     Nonec                   	 ddl }n# t          t          f$ r Y dS w xY w|j                            dddd| d           |j                            d|j                            d          dd|j        j        |j        j	        |j        j
        |j        j        |j        j        |j        j        f	           t          j        }t!          j        d
|           |j                            |           dS )a_  Sets up the hypothesis profiles.

  Sets up the hypothesis testing profiles, and selects the one specified by
  the ``JAX_HYPOTHESIS_PROFILE`` environment variable (or the
  ``--jax_hypothesis_profile`` configuration.

  Args:
    max_examples: the maximum number of hypothesis examples to try, when using
      the default "deterministic" profile.
  r   NrG   T)databasederandomizedeadlinemax_examples
print_blobinteractiver   F)parentr  report_multiple_bugs	verbosityphaseszUsing hypothesis profile: %s)
hypothesisModuleNotFoundErrorr  settingsregister_profileload_profile	Verbosityr  PhaseexplicitreusegeneratetargetexplainHYPOTHESIS_PROFILEr  logginginfo)r  hpprofiles      rO   setup_hypothesisr    s   
{	+   
FF +     +[%%o66 $ (

(.
(

(/
(
     $'	,-w777+7#####s    )rH   rI   rJ   rI   )T)r5   )r   r   rJ   r   )r   rI   )r'  r(  )r3  r   r4  r   )rJ   r   )r?  r   rJ   rj   )Nr5   )r?  rB  rC  rI   rJ   rj   )rH  rI   rJ   rj   )rS  rT  rJ   rj   )ry  r   )r  rI   )r  rI   )F)r  )r  )r  r  r  rS   )rb  rc  rJ   rd  )r   rI   rJ   r  )rl   r  r  r  r  r  )rx  N)r  )rJ   r  )
__future__r   r   collections.abcr   r   r   r   
contextlibr   r	   datetimers   r
   r  r  r  r   r,  r  r   r'  typingr   r   r`  r2  r  absl.testingr   r   r:  r   jax._srcr   r   r   r   r   rT   r   r   r   r   r   r   r   jax._src.cloud_tpu_initr   jax._src.interpretersr   r   jax._src.libr   r   jax._src.numpy.utilr    r!   jax._src.public_test_utilr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   jax._src.utilr.   "jax.experimental.compilation_cacher/   jax.tree_utilr0   r1   r2   r3   numpyr\   numpy.randomr  r  string_flagr  int_flagr   r}  rz  r  	bool_flagbool_env_SKIP_SLOW_TESTSr  r  r  r  r  rM   rP   rZ   rc   ru   r{   r   r   r   r   stdoutcapture_stdoutstderrcapture_stderrr   r   r   r   r   r   r   r   r   r   r   r  r  r   r$  r&  r2  r8  r>  rA  rG  rO  rR  rX  __test__rh  ro  rt  rx  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  	lru_cacher  r  r  r   r  r  r  r  r  r  r"  r2  r7  r:  r>  rC  rJ  rN  rV  rZ  r^  ro  rv  r  r  r   r  r  r  
TestLoaderr  r  r  r  r  r  TestCaser  r>  _PJIT_IMPLEMENTATION_name_NOOP_JIT_IMPLEMENTATIONJIT_IMPLEMENTATIONrT  Warningra  r  r  rI   rc  rk  rp  ru  r|  r~  r  r  r  r  r  r  r  r  r  r  r  	vectorizer  rR  r  r~   rQ   rO   <module>r      sO    # " " " " "     C C C C C C C C C C C C 0 0 0 0 0 0 0 0               				 				 



              ! ! ! ! ! ! & & & & & & 



                               & & & & & & & & & & & &       % % % % % %             ; ; ; ; ; ; & & & & & & & & & & & & ) ) ) ) ) ) F F F F F F F F_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ !           @ @ @ @ @ @ J J J J J J J J J J J J           FBP  	 &fo#ibi)40011*, , , 
 .fo#ibi0%8899$   $6#FO)511	/    #")").33; 
 +*)")$>CC>  
 *:)90FO@%HH
K*L *L *L & (V'BI&88'    "*566% % % %? ? ?	I 	I 	I   .  0 0 0  + + + +
 #( #( #( #(L #*55#*55 1 1 16 C C C A A A / / / ; ; ; = = = = = = ) ) ) , , ,$ ) ) ) )& 5 5 5> > >  = = == = =! ! !   ? ? ? ?   & & & &

) 
) 
) 
) 
), , , ,
 
 
	 	 	 	  %    K K KF F F   ) ) ) )0     ! ! !   I I II I I I I I I I & % % % %; % % % & & & & &K & & &!\^^ #moo   	! 	! 	!! ! !$  2 2 2 R   ! ! ! ! +-;; E E E E6   ,6 6 6 6= = =
< < <
5 5 5> > > >
4 4 43 3 3 3	@ 	@ 	@  <  :  B  "        7 7 7< < <# # #
2 
2 
2( ( (A A A  4 . . .  "J J J"    H'   	 	 	    	  	  	        
 ` ` `AA AA AA AA AA-( AA AA AAF w "  77 !'    
% % % % %[ % % %  7 
 
 
 
 
 c3h 

 

 

 

O O O! ! !  
 
 
 
 
 
 
 
B) B) B) B) B) B) B) B)J 
/ / / CDD6 6 6 6' ' '
 =A!%9F 9F 9F 9F 9F 9Fv I I I L L L8  F F F; ; ; ;|[ [ [ [ [BL [ [ [|d d d d d d d dN)$ )$ )$ )$ )$ )$ )$rQ   