
    ukiQ             	         d dl mZ d dlZd dlmZ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Zd dlZd dlmZmZ d dlZd dlZd dlmZ d dl m!Z! d d	l m"Z" d d
l m#Z# d dl m$Z$ d dl m%Z% d dl m&Z& d dl m'Z( d dl m)Z* 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l m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZL d dlMmNZN d dlMmOZO d dlMmPZP d d lMmQZQ d d!lRmSZSmTZTmUZUmVZV d d"lWmXZXmYZY d d#lZm[Z[ d dl\Z]d dl^m_Z`  e1j                           e#j                  d$d%d&'      Zc e#j                  d( ee ej                  d)d*            d+'      Zg e#j                  d, ee ej                  d-d.            d/      Zh e#j                  d0 e#j                  d1d2      d3'      Zk e#j                  d4 e#j                  d5d2      d6'      Zl e#j                  d7 ej                  d8d9      d:'      Zm ej                  d;      Zodd<Zpdd=Zqdd>Zrddd?Zsdd@ZtddAZu	 	 	 	 	 	 ddBZvdddCZweddD       Zx eexej                        Zz eexej                        Z| G dE dFej                        Z~ e~       ZdG Zee1_        dH Z edI      Z edJ      Z edK      Z edL      Z edM      Z edN      Z edO      Z edP      Z edQ      ZedR        ZedS        ZedT        ZeddU       ZedV        ZedW        ZddXZdY ZdZ Zd[ Zd\ Zd] Zd^ Zd_ Zd` Zda Zdb ZddcZdddZddeZddfZddgZdddhZddiZddjZ	 	 	 	 	 	 ddkZ ej                  edldm n      Z ej                  edodp n      Zdq ZddrZddsZddtZ G du dv      Zdw ZddxZd2e_        dy Zdz Zd{ Zd| Zdd}Zd~ ZddZd 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 Zd Zej                  d        Zd Zd Zd Zd ZddZed        Zed        Z G d d      Zed        Z G d dej                        Ze!j                  Zde_        d Zde_        eefZ G d de      Ze/j                  Zeeeeef      Zedd       Zd Zd ZddZddZ G dÄ dī      Z G dń dƫ      Z e       Z'dǄ Z ej                  dȫ      ZddɄZ dʄ Zddd˜	 	 	 	 	 dd̄Zed̈́        Zed΄        Zdτ Zd dЄZ G dф de]j                        Z G dӄ dԫ      Z	ddՄZ
dddքZddׄZy(      )annotationsN)Callable	GeneratorIterableIteratorSequence)	ExitStackcontextmanager)partial)AnyTextIO)parameterized)api)compilation_cache)config)core)deprecations)dispatchdtypes)lib)mesh)
monitoring)sharding_impls)test_warning_util)
xla_bridge)utilrunning_in_cloud_tpu_vm)mlir)lax)cuda_versions)hlo)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ToleranceDict)thread_unsafe_test)thread_unsafe_test_class)JaxTestLoader)TEST_NUM_THREADS)tree_alltree_flattentree_maptree_unflatten)	ArrayLike	DTypeLike)unzip2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).*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[ \"'\[\](){}<>=,._]+c                .    t         j                  d|       S )N_)kSanitizeNameREsub)ss    M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/test_util.pysanitize_test_namerT   |   s    			S!	$$    c                f    t        j                  t        j                  |             j                  S N)_dtypesfinfocanonicalize_dtypebitsdtypes    rS   num_float_bitsr^      s"    	w11%8	9	>	>>rU   c                    t        j                  |       } t        j                  j	                  | j
                  j                        }|r| j                   |             S | 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asarrayrX   default_typesgetr]   kindastype)arrdtype_fns     rS   to_default_dtyperh      sG     	

3#""&&syy~~6(#+HJ	44rU   c                F     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        |      S |S d }t        ||      S )Nc                     |rt        |       S | S rW   )rh   )rf   use_defaults     rS   <lambda>z:with_jax_dtype_defaults.<locals>.wrapped.<locals>.<lambda>   s    K#3C#8 S rU   )
isinstanceboolr9   rh   )argskwargsresultffuncuse_defaultss       rS   wrappedz(with_jax_dtype_defaults.<locals>.wrapped   sE    4"6"F,%3?X&/KVK
Paa..rU   	functoolswraps)rt   ru   rv   s   `` rS   with_jax_dtype_defaultsrz      s'     ??4/ / 
.rU   c                :    	 t        |        y# t        $ r Y yw xY w)NTF)iter	TypeErrorxs    rS   is_sequencer      s'    G  
 s    	c                    | xs d} t        | t              r7| j                         D ci c]  \  }}t        j                  |      | c}}S t        j                  t        |       S c c}}w Nr   )rn   dictitemsr`   r]   fromkeysr(   )tolkvs      rS   _normalize_tolerancer      sS    q#T'*yy{3tq!BHHQKN33==+S11 4s    A(c                    t        |       } t        |      }| }|j                         D ]$  \  }}t        || j                  |d            ||<   & |S r   )r   r   maxrc   )tol1tol2outr   r   s        rS   join_tolerancer      sU     
d	#$	d	#$#jjl $daDHHQN#CF$	*rU   c                R    t        t        |      }t        t        || |             y )N)err_msg)r   r&   r7   r9   )xsysr   assert_closes       rS   check_eqr      s     /A,
8L"b)*rU   c              #    K   ddfd}t        j                  dd      5 }t        j                  | j	                               }t        j
                  |j	                         | j	                                	 | | j                          t        j                  | j	                                |j                  d       |j                         t        j
                  || j	                                	 ddd       y# | j                          t        j                  | j	                                |j                  d       |j                         t        j
                  || j	                                w xY w# 1 sw Y   yxY ww)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.
  Nc                 "     t        d       S )Nz0get_output() called while the context is active.)
ValueError)captureds   rS   
get_outputz#_capture_output.<locals>.get_output   s    IJJOrU   zw+zutf-8)modeencodingr   )returnstr)
tempfileNamedTemporaryFileosdupfilenodup2flushfsyncseekread)fpr   rs   original_fdr   s       @rS   _capture_outputr      s	    $ (
 ""w? (1&&%KGGAHHJ		$( 	hhjhhryy{ffQihggk299;'( ( 	hhjhhryy{ffQihggk299;'( (s7    F	AE=:D >A8E=7	F	 A:E::E==FF	c                      e Zd Zd Zy)EventThreadLocalStatec                J    i | _         d| _        d | _        d | _        d | _        y r   )countsnested_device_put_countinfer_params_fun_countslower_jaxpr_to_fun_countscollect_lowered_jaxprsselfs    rS   __init__zEventThreadLocalState.__init__   s+    DK#$D  $(D %)D""&DrU   N)__name__
__module____qualname__r    rU   rS   r   r      s    'rU   r   c                "   t         j                  }|j                  | d      dz   || <   | dk(  rEt         j                  dk(  r|j                  dd      dz   |d<   t         xj                  dz  c_        y | dk(  rt         xj                  dz  c_        y | dk(  r%t         j                  }||\  }||xx   dz  cc<   y y | dk(  r%t         j
                  }||\  }||xx   dz  cc<   y y | dk(  r%t         j                  }||j                  |       y y y )	Nr      batched_device_put_startbatched_device_putbatched_device_put_endzpjit._infer_params_impllower_jaxpr_to_funzmlir.collect_lowered_jaxprs)thread_local_stater   rc   r   r   r   r   append)namerp   r   infer_countsfunlower_counts
collections          rS   event_listenerr      s,   $$&D!$q(&, 
''11Q6%+ZZ0Da%H1%Lf!"..!3.''..!3.(( &==Lfs31   ## &??Lfs31   ,,#::J  -rU   c                $     t          fd       }|S )zJReturns a context-manager that yields a function that counts a test event.c               3  `    K   t         j                  j                  d        fd y w)Nr   c                 J    t         j                  j                  d       z
  S r   r   r   rc   beforeevents   rS   rm   z3count_events.<locals>.count_event.<locals>.<lambda>'  s     $++//q9FB rU   r   r   s   @rS   count_eventz!count_events.<locals>.count_event$  s%     &&**5!4F
BBs   *.)r
   )r   r   s   ` rS   count_eventsr   "  s    C C 
rU   r   batched_copy_arrayjit_cpp_cache_misscreate_pjit_jaxprstages_compiled_calllower_jaxpr_to_modulepxla_cached_compilation)compile_after_persistent_compilation_miss_array_shard_argc               #     K   t         j                  j                          dg 	  fd t         j                  j                         j                   d<   y # t         j                  j                         j                   d<   w xY ww)Nc                      d   S r   r   counts   rS   rm   z*count_primitive_compiles.<locals>.<lambda>;  s    %( rU   r   )r   xla_primitive_callablecache_clear
cache_infomissesr   s   @rS   count_primitive_compilesr   5  sh     
!!--/$%C
..99;BBE!Hx..99;BBE!Hs   "B	A ,B	-BB	c               #     K   t         j                  J t        j                         } | t         _        	 |  d t         _        y # d t         _        w xY wwrW   )r   r   collectionsCounterr   s    rS   !count_jit_infer_params_cache_missr   ?  sK     		3	3	;;	; &/5,6
L15..s   2AA AAAc              #      K   t         j                  J t        j                         t         _        	  fd d t         _        y # d t         _        w xY ww)Nc                         S rW   r   )r   fun_names   rS   rm   z2count_subjaxpr_to_hlo_conversion.<locals>.<lambda>O  s    &" rU   )r   r   r   r   )r   r   s   `@rS    count_subjaxpr_to_hlo_conversionr   I  sM     		5	5	==	= &17.8
""37040s   2AA AAAc               #     K   t         j                  J g } | t         _        	 |  dt         _        y# dt         _        w xY ww)zF
  Collects all the pairs of (jaxpr, mlir_module) that are lowered.
  N)r   r   )r   s    rS   r   r   T  sE      
	2	2	::	:>@*.8+5
04--s    A3 AA  Ac              #     K   t               5 }d  d d d                | k7  rt        d|  d |              y # 1 sw Y   *xY ww)NzExpected exactly z  XLA compilations, but executed )count_jit_and_pmap_loweringsAssertionError)timesr   s     rS   $assert_num_jit_and_pmap_compilationsr   b  sY     #% 
	

W
,UG 4)).	3 4 4 
 
s   A;)AA Ac               #    K   t         j                  j                         } i 	 fd t         j                  j                         }|j                         D ]%  \  }}|| j	                  |d      z
  }|dk7  s!||<   ' y # t         j                  j                         }|j                         D ]%  \  }}|| j	                  |d      z
  }|dk7  s!||<   ' w xY ww)Nc                      S rW   r   r   s   rS   rm   z,count_internal_device_puts.<locals>.<lambda>o  s    & rU   r   )_jaxlib_jaxget_internal_device_put_infor   rc   )r   afterr   r   diffr   s        @rS   count_internal_device_putsr   j  s     <<446&&
LL557E 1Aq!!d	q	 LL557E 1Aq!!d	q	s)   !CB AC;CACCCc                 "    t         j                  S rW   )r   versionr   rU   rS   jaxlib_versionr   w  s    	rU   c                 b    t         j                  xs t        j                         j                  S rW   )	_TEST_DUTvaluer   get_backendplatformr   rU   rS   device_under_testr  {  s     		=J224===rU   c                 ^   t               dk(  rt        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"                  t        j$                  h} nPt               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                  t        j                  t        j*                  t        j                  t        j,                  t        j                   t        j$                  h} n?t               dk(  r1t        j                  t        j                  t        j                  h} n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                  t        j*                  t        j                  t        j,                  h} t.        j0                  j2                  sA| t        j(                  t        j&                  t        j*                  t        j,                  hz  } | S )NtpugpuMETAL)r  r`   bool_rX   int4int8int16int32uint4uint8uint16uint32bfloat16float16float32	complex64float8_e4m3fnfloat8_e4m3b11fnuzfloat8_e5m2int64uint64float64
complex128r   
enable_x64r   )typess    rS   supported_dtypesr  ~  s   E!XXw||RWWbhh]]BHHbiirzz2::r||""G$>$>  	"E
 e#XXrww"((BHHXXryy"))RYYrzz2::rzz\\2=='*?*?  	"E
 g%XXryy"**-EXXw||RWWbhh"((]]BHHbiiBIIrzz2::rzz\\2==*E 
			 	 	bii2::r}}==E	,rU   c                 B    dt        j                         j                  v S )Nrocmr   r  platform_versionr   rU   rS   is_device_rocmr#        	:))+<<	<<rU   c                 B    dt        j                         j                  v S )Ncudar!  r   rU   rS   is_device_cudar'    r$  rU   c                     t         S rW   r   r   rU   rS   is_cloud_tpur)    s    	  rU   c                 >    t         j                  j                         S rW   )r   r   is_optimized_buildr   rU   rS   r+  r+    s    		(	(	**rU   c                 >    t         j                  j                         S rW   )r   r   is_asanr   rU   rS   r-  r-        				rU   c                 >    t         j                  j                         S rW   )r   r   is_msanr   rU   rS   r0  r0    r.  rU   c                 >    t         j                  j                         S rW   )r   r   is_tsanr   rU   rS   r2  r2    r.  rU   c                 >    t         j                  j                         S rW   )r   r   is_sanitizedr   rU   rS   r4  r4    s    		"	"	$$rU   c                :   t        j                  | ||      }t               syt        j                         j
                  j                  d      d   }t        j                  d|      }t        |      dk7  ry|j                  t        |d   dd             }||k\  S )NT
r   z\(.*?\)r   r   )datetimedater)  r   r  r"  splitrefindalllenfromtimestampint)yearmonthdayr8  r"  results
build_dates          rS   is_cloud_tpu_at_leastrD    s    	tUC	($	
  ++->>DDTJ2NJJz#34'\Q!!#gaj2&6"78*	t	rU   c                >    t        j                         }|y|| |fk\  S )NT)r   backend_pjrt_c_api_version)major_versionminor_versionpjrt_c_api_versionss      rS   pjrt_c_api_version_at_leastrJ    s*    "==? 	 >	>>rU   c                    t        j                         }|yt        j                  dj	                  t        t        |            |       |k(  S )NT.)r   backend_stablehlo_versionr#   get_smaller_versionjoinmapr   )required_versionplugin_versions     rS   stablehlo_version_at_leastrS    sJ    779.		 	 	hhs3'(*:

 rU   c                     t               dk7  rt        d      t        j                         d   j                  } t        j                  d|       }|t        d|  d      t        |j                  d            S )Nr  zDevice is not TPUr   zTPU[^\d]*(\d+)zDevice kind z is not supportedr   )	r  r   r   devicesdevice_kindr:  matchr>  group)rd   rW  s     rS   get_tpu_versionrY    sp    E!
(
))				a	 	,	,$
(($d
+%
]
|D6):;
<<	U[[^	rU   c                8    t               dk7  ryt               | k\  S )Nr  F)r  rY  )r   s    rS   is_device_tpu_at_leastr[    s    E!		g	%%rU   c                    t               dk7  ry| yt        j                         d   j                  }d|  | }|dk(  rd|v S |dk(  rd	|v S |d
k(  r|j	                  d      S |dk(  rd|v S ||v S )Nr  FTr   r   v5ezv5 litev6ezv6 litev5pv5v7xTPU7x)r  r   rU  rV  endswith)r   variantrV  expected_versions       rS   is_device_tpurf    s    E!_""$Q'33+	'+##5 ##5 %%5 k!!	[	((rU   c                l    t        | t              s| f} | D ]  }t        j                  ||      s|c S  y rW   )rn   tupler:  search)patternsstringpatterns      rS   pattern_searchrm    s:    	He	${H g	yy&!n 
rU   c                `    t        j                         d   j                  }t        | |      }|S r   )r   rU  rV  rm  )device_patternsrV  matching_patterns      rS   device_kind_matchrq    s.    ""$Q'33+#O[A	rU   c                      fd}|S )zISkip if both error message and device kind match a corresponding pattern.c                J     t        j                          fd       }|S )Nc                (   t        j                         d   j                  }	  | g|i |S # t        $ rX}t	        	t        |            }t	        |      }|r/|r-t        
t
              s
 
||      }n
}| j                  |        d }~ww xY wr   )r   rU  rV  	Exceptionrm  r   rn   skipTest)r   rp   rq   rV  ematching_error_patternmatching_device_pattern
reason_strro  error_patternsreasontest_methods           rS   test_method_wrapperz9skip_if_errors.<locals>.skip.<locals>.test_method_wrapper  s    &&(+77k41$1&11 	!/A!G"0+"N!&=FC( 68OPJJ
--

#	s   0 	BABBrw   )r}  r~  ro  r{  r|  s   ` rS   skipzskip_if_errors.<locals>.skip  s&    __[! " rU   r   )r{  ro  r|  r  s   ``` rS   skip_if_errorsr    s    " 
+rU   z&kernel exceeds available shared memoryc                    d| S )Nz+Mosaic GPU kernel exceeds shared memory on r   errdevs     rS   rm   rm     s    GuM rU   )r{  r|  z!Shared memory size limit exceededc                    d| S )Nz'Triton kernel exceeds shared memory on r   r  s     rS   rm   rm      s    CC5I rU   c                 4    t        d      ryt        d      ryy)NzThor$   zGB10$      )rq  r   rU   rS   %get_cuda_nonportable_max_cluster_sizer  #  s     w! 
rU   c                    t               syt        j                  d      ^}}t        d | j	                  d      D              }t        d |j
                  j	                  d      D              }||k\  S )NFr  backendc              3  2   K   | ]  }t        |        y wrW   r>  .0r   s     rS   	<genexpr>z6is_cuda_compute_capability_at_least.<locals>.<genexpr>3       7AQ7   rL  c              3  2   K   | ]  }t        |        y wrW   r  r  s     rS   r  z6is_cuda_compute_capability_at_least.<locals>.<genexpr>4       BQ#a&Br  r'  r   local_devicesrh  r9  compute_capability
capabilitydrO   targetcurrents        rS   #is_cuda_compute_capability_at_leastr  /  f    		

"
"5
1%!a7!1!1#!677&B!"6"6"<"<S"ABB'	F	rU   c                    t               syt        j                  d      ^}}t        d | j	                  d      D              }t        d |j
                  j	                  d      D              }||k(  S )NFr  r  c              3  2   K   | ]  }t        |        y wrW   r  r  s     rS   r  z3is_cuda_compute_capability_equal.<locals>.<genexpr>;  r  r  rL  c              3  2   K   | ]  }t        |        y wrW   r  r  s     rS   r  z3is_cuda_compute_capability_equal.<locals>.<genexpr><  r  r  r  r  s        rS    is_cuda_compute_capability_equalr  7  r  rU   c                    d| k  sJ d|cxk  rdk  sJ  J t         d uxr  t        j                         | dz  |dz  z   k\  S )Nr   d   i  
   )r"   cuda_runtime_get_version)majorminors     rS   is_cuda_version_at_leastr  ?  sX    	
e	
e	c			4 P

0
0
2edlURZ6O
OrU   c                      e Zd ZdZd Zd Zy)CudaArchSpecificTestz:A mixin with methods allowing to skip arch specific tests.c                >    t        d      s| j                  d       y y )Nz9.0z'Only works on GPU with capability sm90ar  rv  r   s    rS   skip_unless_sm90az&CudaArchSpecificTest.skip_unless_sm90aK  s    +E2
mm=> 3rU   c                >    t        d      s| j                  d       y y )Nz10.0z(Only works on GPU with capability sm100ar  r   s    rS   skip_unless_sm100az'CudaArchSpecificTest.skip_unless_sm100aO  s    +F3
mm>? 4rU   N)r   r   r   __doc__r  r  r   rU   rS   r  r  H  s    B?@rU   r  c                     t               rt               dhS t               rt               dhS t               dk(  rt               dhS t               hS )z7returns a set of tags defined for the device under testr   r&  r  r  )r#  r  r'  r   rU   rS   _get_device_tagsr  T  sO    ((((g%''  rU   c                    t        | t              rJ d       t               }| D ]  }t        |t              sJ |       ||v s y y)Nz(device_types should be a list of stringsTF)rn   r   r  )device_typestagsdevice_types      rS   test_device_matchesr  _  s_    C 0/0 
 
	$! kk3'44'd 
rU   c                      fd}|S )Nc                F     t        j                          fd       }|S )Nc                    t               }        s(t        dd      }t        j                  | d| d       | g|i |S )Nr   [unknown test]z# not supported on device with tags rL  )r  getattrunittestSkipTest)r   rp   rq   device_tags	test_name	predicater}  s        rS   r~  z9_device_filter.<locals>.skip.<locals>.test_method_wrappern  sZ    $&k[K5EF	K:;-q
IK 	K////rU   rw   )r}  r~  r  s   ` rS   r  z_device_filter.<locals>.skipm  s&    __[!0 "0 rU   r   )r  r  s   ` rS   _device_filterr  l  s    	 
+rU   c                       t         fd      S )zAA decorator for test methods to skip the test on certain devices.c                     t                S rW   r  disabled_devicess   rS   rm   z!skip_on_devices.<locals>.<lambda>{  s    $78H$I I rU   r  r  s   `rS   skip_on_devicesr  y  s    	I	JJrU   c                       t         fd      S )zEA decorator for test methods to run the test only on certain devices.c                     t               S rW   r  enabled_devicess   rS   rm   z run_on_devices.<locals>.<lambda>  s     3O D rU   r  r  s   `rS   run_on_devicesr  }  s    	D	EErU   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 rW   )r  r    _platforms_with_donationr   rU   rS   rm   z1device_supports_buffer_donation.<locals>.<lambda>  s    !$"?"?@ rU   r  r   rU   rS   device_supports_buffer_donationr    s     
@
 rU   c                    t         j                  j                  | k  r6t         j                  j	                          	 t        j                  d|        yy# t        $ r Y yw xY w)a  Requests at least `nr_devices` CPU devices.

  request_cpu_devices should be called at the top-level of a test module before
  main() runs.

  It is not guaranteed that the number of CPU devices will be exactly
  `nr_devices`: it may be more or less, depending on how exactly the test is
  invoked. Test cases that require a specific number of devices should skip
  themselves if that number is not met.
  jax_num_cpu_devicesN)r   num_cpu_devicesr   r  r   r   updateRuntimeError)
nr_devicess    rS   request_cpu_devicesr    sW     %%
2&&(mm):6 3  
s   A 	A! A!c                      fd}|S )zAA decorator for test methods to skip the test when flags are set.c                H     t        j                          fd       }|S )Nc                    t        j                        }|k(  r*t        dd      }t        j                  | d d|        | g|i |S )Nr   r  z not supported when FLAGS.z is )r   _readr  r  r  )r   rp   rq   
flag_valuer  	flag_name
skip_valuer}  s        rS   r~  z7skip_on_flag.<locals>.skip.<locals>.test_method_wrapper  sg    <<	*j	z	!K5EF	K1)D
MO 	O////rU   rw   )r}  r~  r  r  s   ` rS   r  zskip_on_flag.<locals>.skip  s&    __[!0 "0 rU   r   )r  r  r  s   `` rS   skip_on_flagr    s    	 
+rU   c                      fd}|S )zDA decorator for test classes or methods to pytest.mark if installed.c                h    	 dd l } t        |j                        |       S # t        $ r | cY S w xY wr   )pytestImportErrorr  mark)func_or_classr  markers     rS   wrapz&pytest_mark_if_available.<locals>.wrap  s<     (76;;'66  s   # 11r   )r  r  s   ` rS   pytest_mark_if_availabler    s    7 
+rU   c                 &    dt         j                  v S )Nr  )sysmodulesr   rU   rS   is_running_under_pytestr    s    	S[[	  rU   c                     d z     fd}|S )zDA decorator for test methods to skip the test when run under pytest.zRunning under pytest: c                L     t        j                  t                     |       S rW   )r  skipIfr  )r}  r|  s    rS   r  zskip_under_pytest.<locals>.skip  s    =8??24f=kJJrU   r   )r|  r  s   ` rS   skip_under_pytestr    s    #f,&K	+rU   c                    d t        ||      D        }dj                  | j                         dj                  |            S )Nc              3  :   K   | ]  \  }}t        ||        y wrW   )format_shape_dtype_string)r  shaper]   s      rS   r  z*format_test_name_suffix.<locals>.<genexpr>  s#      ?&% 0u= ?s   z{}_{}rO   )zipformat
capitalizerO  )opnameshapesr   arg_descriptionss       rS   format_test_name_suffixr     s=    ?*-ff*=?	))+SXX6F-G	HHrU   c                      e Zd Zd Zd Zy)ScalarShapec                     yr   r   r   s    rS   __len__zScalarShape.__len__  s    ArU   c                     t        d| d      )Nzindex z out of range.)
IndexError)r   is     rS   __getitem__zScalarShape.__getitem__  s    *vaS-G"HHrU   N)r   r   r   r  r  r   rU   rS   r  r    s    HrU   r  c                      e Zd Zy)_NumpyScalarNr   r   r   r   rU   rS   r
  r
        rU   r
  c                      e Zd Zy)_PythonScalarNr  r   rU   rS   r  r    r  rU   r  c           	         | t         k(  rD|t        j                  t        t        j                  d|      j                                     k(  S y)Nr   r\   T)PYTHON_SCALAR_SHAPEr`   r]   typearrayitemr  r]   s     rS   is_valid_shaper    s;    
!!BHHT"((1E":"?"?"ABCCC	rU   c                    t        |       t        t        fv r| S t        | t              ryt        j                  |       dk(  r| fS t        t        |             )z*Converts `shape` to a tuple of dimensions.r   r   )r  listrh  rn   r  r`   ndimr}   )r  s    rS   _dims_of_shaper    sJ    	%[T5M!L%%	wwu~8O
DK
  rU   c                   |t         u r$t        j                  |      j                  |       S |t        u r#t        j
                  |       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`   r]   r  r  ra   r  r  rh  r  r  r}   )r   r  r]   s      rS   _cast_to_shaper    s    
  88E?&&##::e!!##E{tUm#88E?eEl***L	wwu~88E?uh&&&L
DK
  rU   c                @    t        j                  |       j                  S rW   )r`   r]   r   r\   s    rS   	dtype_strr    s    	%		rU   c                    t        | t        j                        rt        |       d|  dS t        | t              rt        |       } t        | |      S )N[])rn   r`   ndarrayr  r  rh  _format_shape_dtype_stringr  s     rS   r  r    sI    rzz"qq))%%LE	#E5	11rU   @   )maxsizec                4   | t         u rt        |      S | t        u rdt        |      z   S t        |       t        u r)dj                  d | D              }t        |       d| dS t        |       t        u rt        |       d|  dS t        t        |             )Npy,c              3  2   K   | ]  }t        |        y wrW   )r   )r  dims     rS   r  z-_format_shape_dtype_string.<locals>.<genexpr>  s     2SC2r  r   r!  z,])r  r  r  r  rh  rO  r>  r}   )r  r]   shapestrs      rS   r#  r#    s    
  U##)E"""E{exx2E22Hq
!,,E{cqr**
DK
  rU         ?c                    | S rW   r   r~   s    rS   rm   rm     s    Q rU   c                >    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        j	                         S rW   )r`   ra   absr  re   r]   randscaler  s   rS   rm   z_rand_dtype.<locals>.<lambda>%  s/    

53t^E-B'C#DDELLUS rU   c                 j    t        j                   t               z        j                         S rW   )r`   ra   r  re   r1  s   rS   rm   z_rand_dtype.<locals>.<lambda>'  s*    

54)>#??@GGN rU                 ?)rX   
issubdtyper`   unsignedintegercomplexfloatingr  ra   )r2  r  r]   r3  postrvalss   ````   rS   _rand_dtyper<    sr      r112SANAr1123D3D	

4:u5ue	DDrU   c                     d f fd	}|S )z:Random numbers that span the full range of available bits.c                    | S rW   r   r~   s    rS   rm   z rand_fullrange.<locals>.<lambda>1  s    q rU   c                0   t        j                  |      }|j                  t        j                  t        |             z  }j                  dt        j                  t         j                        j                  |t         j                        } ||      j                  |      }| t        u r|t         j                  k(  r |j                  t         j                        }nL|t         j                  k(  r9t         j"                  j$                  s|j                  t         j&                        }|j)                  |       }rJt        j*                  |t         j,                        r&t         j.                  |t        j0                  |      <   t3        || |      S )Nr   )sizer]   )r`   r]   itemsizemathprodr  randintiinfor  r   viewr  r  re   r  r  r   r  r   r  reshaper6  floatingnanisnanr  )r  r]   r9  r@  r;  rngstandardize_nanss        rS   genzrand_fullrange.<locals>.gen1  s   HHUOE>>DIInU&;<<D;;q"((288,00t288;LD:??5!D## 
"))	{{288$BIIf&7&7&=&={{288$<<DBMM%=VVd288D>$u--rU   r   )rK  rL  rM  s   `` rS   rand_fullrangerN  /  s    ( ." 
*rU   c                :    t        t        | j                  |      S )Nr3  r   r<  randn)rK  r3  s     rS   rand_defaultrS  E  s    	ciiu	55rU   c                B    d }t        t        | j                  d|      S )Nc                t    t        j                  | dk(  t        j                  d| j                        |       S )Nr   r   r\   )r`   wherer  r]   r~   s    rS   rm   zrand_nonzero.<locals>.<lambda>J  s&    288AFBHHQagg$>B rU      r3  r9  rQ  rK  r9  s     rS   rand_nonzerorZ  I  s    	B$	ciiqt	<<rU   c                B    d }t        t        | j                  d|      S )Nc                    | dz   S )Nr   r   r~   s    rS   rm   zrand_positive.<locals>.<lambda>O  s
    1q5 rU      rX  r   r<  r2  rY  s     rS   rand_positiver_  N  s    	$	chhad	;;rU   c                :    t        t        | j                  d      S )NgMbP?rP  rQ  rK  s    rS   
rand_smallrb  S  s    	ciit	44rU   c                H    fd}t        t        | j                  d|      S )Nc                @    | t        j                  | dkD         z   S r   )r`   rV  )r   offsets    rS   rm   z rand_not_small.<locals>.<lambda>X  s    1rxxAvw77 rU   g      @rX  rQ  )rK  re  r9  s    ` rS   rand_not_smallrf  W  s    	7$	ciir	==rU   c                :    t        t        | j                  d      S )Ngh㈵>rP  r^  ra  s    rS   rand_small_positiverh  \  s    	chhd	33rU   c                X    k  sJ fd}t        t        | j                  |      S )Nc                    | z
  z  z   S rW   r   )r   highlows    rS   rm   zrand_uniform.<locals>.<lambda>a  s    1s
#c) rU   )r9  r^  )rK  rl  rk  r9  s    `` rS   rand_uniformrm  _  s'    	t	)$	chhT	22rU   c                H      fd}t        t         j                  d|      S )Nc                    | j                         }t        |      dk(  r| S  j                  t        j                  |        dk  }t        j
                  ||d   |       S )Nr         ?)ravelr<  r2  r`   r  rV  )r   x_ravelflipsrK  s      rS   r9  zrand_some_equal.<locals>.postg  sS    ggiG
7|qhCHHbhhqk"S(E88E71:q))rU   g      Y@rX  rQ  rY  s   ` rS   rand_some_equalrt  e  s    * 
ciit$	??rU   c                0     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                        rkt        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   r\   r5  皙?)rX   r6  r`   rH  r8  realr  r]   r  r  r2  rV  infra   )r  r]   
base_dtyper   dimsposinf_flipsneginf_flipsr;  	base_randr2  rK  s           rS   r2  zrand_some_inf.<locals>.randw  s/   eR[[1ue$$%!3!3477288AU34::j%$XXb% 4z#::;cC..% D388T?S(L388T?S(LUE"D88L"((266"?FD88L"((BFF7%"@$GD"**T7FFrU   rS  rK  r  r2  s   `@@rS   rand_some_infr  q  s    3)G, 
+rU   c                0     t                fdS )z=Return a random sampler that produces nans in floating types.c                   t        j                  |t        j                        rkt        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 )ry  r   r\   r5  rz  g?)rX   r6  r`   r8  r{  r  r]   r  rH  r  r2  rV  rI  ra   )r  r]   r}  r   r~  r:  	nan_flipsneg_nan_flipsr;  r  r2  rK  s            rS   r2  zrand_some_nan.<locals>.rand  s(   %!3!3477288AU34::j%$XXb% 4z#::;cC..eR[[1ue$$% D$ACIHMUE"D88Irxxe<dCD88M288RVVG5#A4HD"**T7FFrU   r  r  s   `@@rS   rand_some_nanr    s    3)G. 
+rU   c                0     t                fdS )rv  c                   t        j                  |t        j                        s	 	| |      S t        j                  |t        j                        rkt        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 rx  )rX   r6  r`   rH  r8  r{  r  r]   r  r  r2  rV  r|  rI  ra   )r  r]   r}  r   r~  r  r  r  r;  r  r2  rK  s            rS   r2  z#rand_some_inf_and_nan.<locals>.rand  sd   eR[[1ue$$%!3!3477288AU34::j%$XXb% 4z#::;cC..% D388T?S(L388T?S(L$#%IUE"D88L"((266"?FD88L"((BFF7%"@$GD88Irxxe<dCD"**T7FFrU   r  r  s   `@@rS   rand_some_inf_and_nanr    s    3)G0 
+rU   c                ,     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 )ry  rp  r   r\   )r  r2  r`   rV  r  r  ra   )r  r]   r~  zerosr;  r  rK  s        rS   r2  zrand_some_zero.<locals>.rand  sd    % DCHHdOc!EUE"D88E288AU3T:D"**T7FFrU   r  )rK  r2  r  s   ` @rS   rand_some_zeror    s    3)G 
+rU   c                      fd}|S )Nc                d   t        j                  |t         j                        r|nt         j                  }dk(  rQOt        j                  |t         j                        r t        j                  |      j
                  nt        d      j                  | |      j                  |      S )Nr   zArand_int requires an explicit `high` value for non-integer types.)rk  r@  r]   )	r`   r6  integerr  rE  r   r   rD  re   )r  r]   	gen_dtyperk  rl  rK  s      rS   fnzrand_int.<locals>.fn  s    ubjj9rxxI
axDL	ubjj	)xx"" . / 	/;;sE&  ((.u6rU   r   )rK  rl  rk  r  s   ``` rS   rand_intr    s    
6 
)rU   c                      fd}|S )Nc                    j                  t        j                  xs t        j                  |       |      | d      S )Nr\   Fr@  replace)choicer`   arangerB  rC  )r  r]   rk  rK  s     rS   r  zrand_unique_int.<locals>.fn  s9    ::bii 8		%(8F %  1 1rU   r   )rK  rk  r  s   `` rS   rand_unique_intr    s    1 
)rU   c                     d fd	}|S )zSample an array of given shape containing indices up to dim (exclusive),
  such that the indices are unique along the given axis.
  Optionally, convert some of the resulting indices to negative indices.c                X   t        j                  |d | ||d  dd  z         }t        |      D cg c]  }	j                  | ||   d       }}t	        j
                  |      j                  |||         }|j                  |d | ||d  dd  z   ||   fz         }t	        j                  |d|      }t        t        j                  |       }t	        j                  |||      dk  j                         sJ |r+	j                  ddg|j                        }||xx   | z  cc<   |S c c}w )Nr   Fr  r   )	minlengthT)rB  rC  ranger  r`   r  rG  moveaxisr   bincountapply_along_axisallr  )
r*  r  axisallow_negative
batch_sizerO   idxr   maskrK  s
            rS   r  z*rand_indices_unique_along_axis.<locals>.fn  s.   5$<%,qr*::;J Z 
 
jj5;j6C  ((3-


E$K
8C
++eETlU45\!"%55tF
GC
++c2t
$C BKK3/EtS1Q6;;===ZZsyy1d	$i3iJs   D'Tr   )rK  r  s   ` rS   rand_indices_unique_along_axisr    s    & 
)rU   c                      fd}|S )Nc           	     z    t        t        j                   j                  t	        |        dk  |      | |      S )Nrp  r\   )r  r`   ra   r2  r  )r  r]   rK  s     rS   	generatorzrand_bool.<locals>.generator  s8    jj>%01C7uEU rU   r   )rK  r  s   ` rS   	rand_boolr    s     
rU   c                    	  |         J # |$ r2}t        |      j                  |      sJ d| d| d       Y d }~y d }~ww xY w)Nr6  

)r   
startswith)thunkerr_typemsgrw  s       rS   check_raisesr    sM    7	G5	 7q6S!6Rs$se2#66!7s   	 A(=Ac                    	  |         J # |$ r6}t        j                  |t        |            sJ | d| d       Y d }~y d }~ww xY w)Nr  r6  )r:  rW  r   )r  r  rl  rw  s       rS   check_raises_regexpr    sK    <	G5	 <88GSV$;4y&;;$<s   	 A,AAc              #     K   | j                   E d {    t        j                  |       D ]  }t        |      E d {     y 7 27 	wrW   )eqnsr   	subjaxprs	iter_eqns)jaxprsubjaxprs     rS   r  r  !  sB     ZZ..' #h"""# "s   A	A*A	AA	A	c                j    t        j                  |      | }t        |j                        D cg c].  }|j                  t
        j                  k(  r|j                  d   0 }}|D ]E  }d|  d| }t        |t              r|d   | k(  sJ |       |d   | k(  r3J |       || k(  r@J |        y c c}w )N	precisionzUnexpected precision:  != r   r   )
r   
make_jaxprr  r  	primitiver!   dot_general_pparamsrn   rh  )expected_precisionr   rp   r  eqn
precisionsr  r  s           rS   assert_dot_precisionr  '  s    
#..
t
$%3<U[[3I 7C==C$5$55 

;' 7* 7 2i"#5"6d9+
FC)U#q\//44/q\//44/,,1c1,27s   3B0c                     t        j                  t        |fi |      | }t        |j                        D cg c].  }|j
                  t        j                  k(  r|j                  d   0 }}|D ]  }d|  d| }| |k(  rJ |        y c c}w )Npreferred_element_typez#Unexpected preferred_element_type: r  )	r   r  r   r  r  r  r!   r  r  )	expectedr   rp   rq   r  r  pref_eltypespref_eltyper  s	            rS   !assert_dot_preferred_element_typer  3  s    
0#..//
0$
7%BKEKKBX :3mms'8'88 **56 :, :! (k/zk]
KC{"'C'"(:s   3Bc               '     K   g d}t        t        j                  t        |      z        dz   }|D ]2  t	        |      D ]"  }d d| ft        fd| D              z    $ 4 y w)N)r   rW  r  r   rO   c              3  .   K   | ]  } |        y wrW   r   )r  rM  r@  s     rS   r  z"cases_from_gens.<locals>.<genexpr>@  s     %@Cc$i%@   )r>  NUM_GENERATED_CASESr   r<  r  rh  )genssizescases_per_sizer  r@  s       @rS   cases_from_gensr  ;  sw     
%*003u:=>B. Ad>" Aas^%@4%@ @@@AAs   A%A(c              #    K   t               }d}t        j                  d      fd}t        |      t        j
                  k  r|t        j
                  k  r|dz  }t         | |            }|sHt        |      dkD  rt        d      |d   }|d   |v rnd}|j                  |d          | t        |      t        j
                  k  r|t        j
                  k  ry y y y w)Nr   *   c                    t        | t        t        f      st        |       } | j                  t	        |                gS rW   )rn   r  rh  rD  r<  )r   rK  s    rS   
choose_onez,named_cases_from_sampler.<locals>.choose_oneF  s5    a$'
q'ackk#a&!"##rU   r   z@Generator is expected to only return a single case when samplingtestcase_name)
setnprRandomStater<  r  r   _MAX_CASES_SAMPLING_RETRIESr  r  add)rM  seenretriesr  casescaserK  s         @rS   named_cases_from_samplerr  B  s     	$'#$ 
T(..	.	.44	4qLGZ!E
5zA~[\\8DO$GHHT/"#
J 
T(..	.	.44	4 
/	4 
/s   CC&"C&c                h    t         j                  j                  d      }|j                  | |d      S )Nr  Fr  )r`   randomr  r  )nmrK  s      rS   _choicer  \  s+    
		b!#	AAu	--rU   c            	     V   | D cg c]  }t        |       } }|j                         D cg c]  \  }}|t        |      f }}}t        j                  d | D              t        j                  d |D              z  }g }t	        |t        |t        j                              D ]v  }i }| D ]0  }	|j                  |	|t        |	      z            |t        |	      z  }2 |D ]'  \  }}||t        |      z     ||<   |t        |      z  }) |j                  |       x |S c c}w c c}}w )z%Non-decorator form of sample_product.c              3  2   K   | ]  }t        |        y wrW   r<  )r  as     rS   r  z+sample_product_testcases.<locals>.<genexpr>e  s     %1A%r  c              3  8   K   | ]  \  }}t        |        y wrW   r  )r  rO   r   s      rS   r  z+sample_product_testcases.<locals>.<genexpr>e  s     1HTQ#a&1Hs   )r  r   rB  rC  r  minr  r   r  r<  r   )
rp   kwargr   r   r  	testcasesr  testcaser  s
             rS   sample_product_testcasesr  a  s   #	$$s)	$$	$!#,ADG,",
ii%%%		1HR1H(HH!)1c!06678 aH ooaCF
m$CFla  1a#a&jMhqkCFla X 
 
%,s
   D D%c                 8    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  )rp   r  s     rS   sample_productr  r  s      
	!	!#;T#HR#H	IIrU   c                       fd}|S )1Test case decorator for subclasses of JaxTestCasec                   t        j                  |       rt        | t              sJ d       i | _        | j
                  D ]'  }| j                  j                  |j                         ) | j                  j                         | S Nz9@with_config can only wrap JaxTestCase class definitions.)inspectisclass
issubclassJaxTestCase_default_thread_local_config	__bases__r  clsbkwdss     rS   	decoratorzwith_config.<locals>.decorator  st    ??3JsK$@}B}}@')C$]] N	&&--a.L.LMN$$++D1JrU   r   r  r	  s   ` rS   with_configr         
rU   c                       fd}|S )r  c                   t        j                  |       rt        | t              sJ d       i | _        | j
                  D ]'  }| j                  j                  |j                         ) | j                  j                         | S r  )r  r   r  r  _default_global_configr  r  r  s     rS   r	  z%with_global_config.<locals>.decorator  st    ??3JsK$@}B}}@!#C]] B	  ''(@(@AB%%d+JrU   r   r
  s   ` rS   with_global_configr    r  rU   c                2     |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                 N    t        |       \  }}t        | |       }  | i |S rW   )r8   r:   )rp   r  	flat_argstree_promoter   s       rS   wrapperz!promote_like_jnp.<locals>.wrapper  s2    "4(OIt$) 45DrU   )r%   r$   )r   inexactr  r  s   `  @rS   promote_like_jnpr    s     (/#N( 
.rU   c               +  h  K   i }	 | j                         D ]3  \  }}t        j                  |      ||<   t        j                  ||       5 d  |j                         D ]  \  }}t        j                  ||        y # |j                         D ]  \  }}t        j                  ||        w xY wwrW   )r   r   r  r  )r  original_configkeyr   s       rS   global_config_contextr    s     / jjl  
U#\\#.ocmmC  
%++-  
UmmC o++-  
UmmC s   B2A
A? /B2?0B//B2c               +     K   t               }| j                         D ],  \  }}|j                  t        j                  |   |             . 	 d  |j                          y # |j                          w xY wwrW   )r	   r   enter_contextr   config_statesclose)r  stackconfig_namer   s       rS   thread_local_config_contextr#    sa     
+% JJL Bk5	,,[9%@AB		KKMEKKMs   A
A7A" A7"A44A7c                      e Zd Zd Zy)
NotPresentc                     y)Nz<not present>r   r   s    rS   __repr__zNotPresent.__repr__  s    rU   N)r   r   r   r'  r   rU   rS   r%  r%    s    rU   r%  c            
   #    K   t         j                  } t        j                  j                  j	                         }d  t        j                  j                  }t         j                  }||k7  r|j                         |j                         z  D ci c]J  }||vs||vs||   ||   k7  r5||j                  |t                     |j                  |t                     fL }}t        d|       | |urt        d|  d|       y c c}w w)Nz9Test changed global config values. Differing values are: z>Test changed the compilation cache object: before test it was z, now it is )	r   _cacher   valuescopykeysrc   r%  r   )starting_cachestarting_configending_configending_cacher   	differings         rS   assert_global_configs_unchangedr2    s    $++.MM((--//--&&-")),%+002]5G5G5IIB.!=2H(+}Q/?? _((JL9=;L;LQPZP\;]^^ BI B TU^T_`
aa<'

H
,|n	6  (Bs   BDAD +Dc                  (    e Zd ZU dZi Zded<   dddddd	Z fd
Zd# fdZd Z	d$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eddd       Zed        ZdddddddZ	 	 d%d Zdddddd!	 	 	 	 	 	 	 	 	 	 	 d&d"Z xZS )'r  zDBase class for JAX tests including numerical checks and boilerplate.zdict[str, Any]r  Tstrictraiseofferror)jax_enable_checksjax_numpy_dtype_promotionjax_numpy_rank_promotionjax_traceback_filteringjax_legacy_prng_keyc                   t         |           | j                  t                      t	        j
                  t        j                  | j                  j                                     | _
        | j                  t        di | j                         | j                  j                         D ],  \  }}| j                  t        j                   |   |             . t"        j$                  rt&        j$                  dk  sJ d       | j                  t        j(                  d             | j                  t        j*                  d             | j                  t        j,                  d             | j                  t        j.                  d             | j                  t1        j2                               }| j                  t        j4                  |             | j7                  t8        j:                         y y )Nr   z0Persistent compilation cache is not thread-safe.Tr   r   )supersetUpenterContextr2  r  r  zlibadler32_testMethodNameencode_rngr  r  r  r   r   r  &TEST_WITH_PERSISTENT_COMPILATION_CACHEr   r6   enable_compilation_cacheraise_persistent_cache_errors&persistent_cache_min_compile_time_secs%persistent_cache_min_entry_size_bytesr   TemporaryDirectorycompilation_cache_dir
addCleanupr   reset_cache)r   r"  r   tmp_dir	__class__s       rS   r?  zJaxTestCase.setUp  s{   	GMO578
 T-A-A-H-H-J KLDI+Jd.I.IJK"??EEG BU
,,[9%@AB .33##q(\*\\(
77=>
<<TBC
EEaHI
DDQGH!!("="="?@g
44W=>
oo'334 4rU   c                N    t        j                         sJ t        |           y rW   )r   reset_trace_stater>  tearDown)r   rP  s    rS   rS  zJaxTestCase.tearDown  s     !!###	GrU   c                    | j                   S rW   )rE  r   s    rS   rK  zJaxTestCase.rng  s    99rU   c                    t        j                  |      r| j                  t        |      S | j	                  t
        |      S )zAssert warning or error, depending on deprecation state.

    For use with functions that call :func:`jax._src.deprecations.warn`.
    )r   is_acceleratedassertRaisesRegexr   assertWarnsRegexDeprecationWarning)r   deprecation_idmessages      rS   assertDeprecationWarnsOrRaisesz*JaxTestCase.assertDeprecationWarnsOrRaises  s9    
 "">2##J88""#5w??rU   r?   F)check_dtypesr   allow_object_dtypeverbosec                  |r| j                  ||       t        j                  |      }t        j                  |      }|s1|j                  t        k(  s|j                  t        k(  rt        d      t        j                  d      5  t        j                  j                  ||||       ddd       y# 1 sw Y   yxY w)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`   ra   r]   objectr}   errstatetestingassert_array_equal)r   actualdesiredr]  r   r^  r_  s          rS   assertArraysEqualzJaxTestCase.assertArraysEqual  s     
VW-ZZFjj!GV\\V%;w}}PV?V	IJ J 
(	# 5jj##FGW,3 $ 55 5 5s   $B55B>Nr]  atolrtolr   c               b   | j                  |j                  |j                         t        t        t	        |      |      t        t	        |      |            }t        t        t	        |      |      t        t	        |      |            }t        |||||       |r| j                  ||       yy)zFAssert that actual and desired are close (up to numerical tolerances).)rl  rm  r   N)assertEqualr  r   r1   r)   r&   rc  )r   rh  ri  r]  rl  rm  r   s          rS   assertArraysAllClosez JaxTestCase.assertArraysAllClose   s     	V\\7==1y.	&/40PQDy.	&/40PQD67D'R
VW- rU   canonicalize_dtypesc               "   t         j                  j                  sQ|rO| j                  t	        j
                  t        |      d      t	        j
                  t        |      d             y | j                  t        |      t        |             y )NT)allow_extended_dtype)r   r  r   ro  rX   rZ   r)   )r   rh  ri  rr  s       rS   rc  zJaxTestCase.assertDtypesMatch,  sf    ""':
w11&.W[\11&/X\]_ vf~vg7rU   r]  rl  rm  rr  r   c          
        t        |t              r| j                  |t               | j                  t	        |j                               t	        |j                                      |j                         D ]   }| j                  ||   ||   |||||       " y	t        |      rt        |d      s| j                  t        |      xr t        |d       d|        | j                  t        |      t        |             t        ||      D ]  \  }	}
| j                  |	|
|||||        y	t        |d      st        j                  |      r| j                  t        |d      xs t        j                  |      d|        |r| j                  |||       t        j                  |      }t        j                  |      }| j!                  ||d|||       y	||k(  ry	t#        t%        |      t%        |      f      )
zPAssert that actual and desired, either arrays or nested tuples/lists, are close.ru  	__array__zExpected sequence, got r  zExpected array-like, got rq  Frk  N)rn   r   assertIsInstancero  r  r,  assertAllCloser   hasattr
assertTruer<  r  r`   isscalarrc  ra   rp  r}   r  )r   rh  ri  r]  rl  rm  rr  r   r   
actual_eltdesired_elts              rS   rz  zJaxTestCase.assertAllClose3  s    &$
GT*
s6;;=)3w||~+>?{{} -!F1IwqzSW!%;N$+ 	 	-- 
V	WV[%A
ook'*P77K3P/P3G9=  ?
s6{CL1%(%9 -
!*kJ,UY!%;N$+ 	 	-- 
	%V)<
oogg{3Kr{{77K5gY?  A	vwDWXzz&!f

7#g
e$UY(/   1	7	tF|T']344rU   c                r   t        j                  |      }t        j                  |      }t        j                  d      }t        j                  |d|j                               }t        j                  |d|j                               }||k7  rt        d| d       | j                  ||d| d|        y)zGAsserts two strings are equal, after dedenting and stripping each line.z\s*\n\s*r6  zFound
z
Expecting
rx  N)textwrapdedentr:  compilerQ   stripprintassertMultiLineEqual)r   r  whatignore_space_reexpected_clean
what_cleans         rS   assertMultiLineStrippedEqualz(JaxTestCase.assertMultiLineStrippedEqualS  s    x(H??4 Djj-OVVOT8>>3CDNtzz|<J^#gdV2nj$+D6xj"I  KrU   c              #  f   K   t        j                         5  d  d d d        y # 1 sw Y   y xY wwrW   )r   raise_on_warningsr   s    rS   assertNoWarningszJaxTestCase.assertNoWarnings`  s)     		,	,	.   s   1%	1.1rx  c             #    K   t        j                         5 }d  d d d        D ]4  }t        |j                  |      s||t	        |j                        vr4 y  | j                  d| d| d|        y # 1 sw Y   ]xY ww)NExpected warning not found :'z', got )r   record_warningsrn   r[  r   fail)r   warningr  wsws        rS   assertWarnszJaxTestCase.assertWarnsh  s     		*	*	,  		7+	SAII6 	II+G9Bse7  s   BA8AB8B=Bc              #    K   |/t        |t        j                        st        j                  |      }t	        j
                         5 }d  d d d        D ]A  }t        |j                  |      s|%|j                  t        |j                              sA y  | j                  d| d| d|D cg c]  }t        |j                         c}        y # 1 sw Y   xY wc c}w w)Nr  r  z', got warnings: )
rn   r:  Patternr  r   r  r[  ri  r   r  )r   r  regexr  r  s        rS   rX  zJaxTestCase.assertWarnsRegexu  s     E2::!>

5!		*	*	,  		7+		5<<AII#? 	II+G9Bug >8:;1AII;<> ?   <s+   AC(CA$C(1C#
C(C C()r]  r   rm  rl  check_cache_missesc                    |       } fd}	d | }
t        d |
      }t        d |
      } j                  ||       t        j                  j	                         j
                  } | }
|r[t        j                  dk  rH j                  |t        j                  j	                         j
                  dj                               t        j                  |	      }d || }d || } j                  ||
||xs ||xs |        j                  ||
||xs ||xs |        |       }d | }
d || } j                  ||
||xs ||xs |       y	)
zBHelper method for running JAX compilation and allclose assertions.c                 0    j                          |  S rW   )r|  )rp   r   python_should_be_executingr   s    rS   wrapped_funz1JaxTestCase._CompileAndCheck.<locals>.wrapped_fun  s    
oo01$ZrU   Tc                ,    t        j                  |       S rW   )r`   r  r~   s    rS   rm   z.JaxTestCase._CompileAndCheck.<locals>.<lambda>  s    rxx{ rU   c                R    t        j                  t        j                  |             S rW   )r`   r  ra   r~   s    rS   rm   z.JaxTestCase._CompileAndCheck.<locals>.<lambda>  s    288BJJqM#: rU   r   z?Compilation detected during second call of {} in op-by-op mode.F)r]  rl  rm  N)r9   ro  r   r   r   r   r6   r   r  r   jitrz  )r   r   
args_makerr]  r   rm  rl  r  rp   r  
python_anspython_shapes	np_shapescache_missescfunmonitored_anscompiled_ansr  s   ``               @rS   _CompileAndCheckzJaxTestCase._CompileAndCheck  s}    <D "&dJ2J?M:JGI]I.22==?FFLdJ.449

77BBDKK&+
 77;D!%$KM!&;Lz![St{s  <j|![St{s  < <D!%dJ!&;Lj|![St{s  <rU   c	                d     |       }	 ||	 }
 ||	 }| j                  |
|||xs ||xs ||       y )N)r]  rl  rm  rr  )rz  )r   numpy_reference_oplax_opr  r]  r   rl  rm  rr  rp   lax_ans	numpy_anss               rS   _CheckAgainstNumpyzJaxTestCase._CheckAgainstNumpy  sH     <DdmG"D)I![St{s,?  ArU   )cppaot_calltracinglowering'compilation_after_persistent_cache_missc          
     D   t               5 }t               5 }t               5 }	t               5 }
t	               5 } |        d d d        d d d        d d d        d d d        d d d        || j                  |               || j                  |               || j                  | 	              || j                  | 
              || j                  |               y y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY wrW   )count_pjit_cpp_cache_misscount_aot_jit_cpp_cache_misscount_jit_tracing_cache_missr   -count_compilation_after_persistent_cache_missro  )r   rt   r  r  r  r  r  	cpp_countaot_call_counttracing_countlowering_countcompilation_counts               rS   assertCacheMisseszJaxTestCase.assertCacheMisses  s(    $
% 
&
(,:
&
(,9
&
( -;
7
9	 >O
f     
sIK(
x!12
w0
x!12.:
>(*, ;         sf   DD
C>C2	C&C2	C>D
D&C/+C2	2C;7C>>DD

D	DDr   None)rZ  r   r[  r   )TNNNT)rt   zCallable[[], Any]r  
int | Noner  r  r  r  r  r  r  r  )r   r   r   r  r  __annotations__r  r?  rS  rK  r\  rj  rp  rc  rz  r  r
   r  r  rX  r  r  r  __classcell__)rP  s   @rS   r  r    s?   L+-.-!) '$""5.@ @DR+0$5* CGT $b
. GK 8 =AtRV)-r5@K   (, 
 
 ? ?  ?C t-<` GK-1A +//3.2/3NR,/,', #-, ",	,
 #-, BL,rU   r  r  c                    | S rW   r   )r   rp   rq   s      rS   rm   rm     s    a rU   noopc                      e Zd Zd Zd Zy)BufferDonationTestCasec                B    | j                  |j                                y rW   )r|  
is_deletedr   r   s     rS   assertDeletedz$BufferDonationTestCase.assertDeleted  s    OOALLN#rU   c                B    | j                  |j                                y rW   )assertFalser  r  s     rS   assertNotDeletedz'BufferDonationTestCase.assertNotDeleted  s    Q\\^$rU   N)r   r   r   r  r  r   rU   rS   r  r    s    $%rU   r  c              #    K   t        |       \  }}t        j                  |      }t        t	        j
                               }t        |      |k  rt        j                  d| d      t        j                  |d|       j                  |      }t        j                  ||      5  d ddd       y# 1 sw Y   yxY ww)zDTest utility for setting up meshes given mesh data from `schedules`.Test requires z local devicesN)r=   rB  rC  r  r   r  r<  r  r  r`   r  rG  mesh_libMesh)named_shape
axis_namesr  r@  r  mesh_devicess         rS   	with_meshr    s      [)*e	5	$z//12-$


nTF.A
BB-./77>,}}\:. 
	
 
 
s   B%C'B5,	C5B>:Cc                      fdS )Nc                 8      t        |d               | i |S )Nr   )r  )rp   rq   rs   s     rS   rm   z'with_mesh_from_kwargs.<locals>.<lambda>  s%    !=!:6&>!:1!=t!Nv!N rU   r   rs   s   `rS   with_mesh_from_kwargsr    s	    	NNrU   c                X     t        j                  d dD              t        |             S )Nc              3  0   K   | ]  \  }}}|||d   yw))r  r   axis_resourcesNr   )r  r   r   r  s       rS   r  z(with_and_without_mesh.<locals>.<genexpr>  s%      ("dN DNK(s   ))r?   r   r   )r  ))r   r]  ))r  r   )r   named_parametersr  r  s    rS   with_and_without_meshr     s7    
	'	' ('( 

 Q
! !rU   c                t     't         j                  j                  ft              z  n fd}|S )Nc                      fd}|S )Nc                     t              }t        j                  |      5   | i |d|icd d d        S # 1 sw Y   y xY w)N
axis_typesr   )create_meshr   set_mesh)rp   rq   r   r  r  
iota_ordernamesr  s      rS   mesh_fnz6with_explicit_mesh.<locals>.decorator.<locals>.mesh_fn  sH    zjId""4( .4-6--. . .s	   <Ar   )r  r  r  r  r  r  s   ` rS   r	  z%with_explicit_mesh.<locals>.decorator  s    . . NrU   )r  AxisTypeExplicitr<  )r  r  r  r  r	  s   ```` rS   with_explicit_meshr    s;    % ""++-E
:+5  
rU   c           	        t        j                  |       }t        t        j                               |k  r7t        j                  d| dt        t        j                                d      |r_t        t        j                         d       }t        j                  |d |       j                  |       }t        j                  |||      S |'t        j                  j                  ft        |       z  }t        j                   | ||      S )Nr  z global devices and found rL  c                    | j                   S rW   )id)r  s    rS   rm   zcreate_mesh.<locals>.<lambda>  s
     rU   )r  r  )rB  rC  r<  r   rU  r  r  sortedr`   r  rG  r  r  r  Autor   	make_mesh)
mesh_shaper  r  r  r@  rU  r  s          rS   r  r    s    	:	$			%


nTF2LSQ[QcQcQeMfLgghi
jjZ'')~>G88GETN+33J?L==zjII%%**,s:>j##J
JGGrU   c                  &    e Zd Z e       Zd Zd Zy)_cached_propertyc                4    || _         | j                  | _        y rW   )_methodnull_value)r   methods     rS   r   z_cached_property.__init__$  s    DL))DKrU   c                v    | j                   | j                  u r| j                  |      | _         | j                   S rW   )r  r  r  )r   objr  s      rS   __get__z_cached_property.__get__(  s,    {{diiLL%dk;;rU   N)r   r   r   rd  r  r   r   r   rU   rS   r  r  !  s    	$rU   r  c                      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y)_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                L    t                t        |      fd|D              S )Nc              3  ,   K   | ]  }|v s|  y wrW   r   )r  r  	supporteds     rS   r  z(_LazyDtypes.supported.<locals>.<genexpr>6  s     <aQ)^<s   	)r  r  )r   r   r  s     @rS   r  z_LazyDtypes.supported4  s"     "I4<<6<<<rU   c           
     T   t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  t         j                  t         j                  t         j                  g
}| j                  |      S rW   )rX   r  r  r  float8_e4m3fnuzr  float8_e5m2fnuzfloat8_e3m4float8_e4m3float8_e8m0fnufloat4_e2m1fnr  )r   float_dtypess     rS   custom_floatsz_LazyDtypes.custom_floats8  sv       L >>,''rU   c                `    | j                  t        j                  t        j                  g      S rW   )r  r`   r  r  r   s    rS   rH  z_LazyDtypes.floatingH  s    >>2::rzz233rU   c                    | j                  t        j                  t        j                  t        j
                  t        j                  g      S rW   )r  rX   r  r`   r  r  r  r   s    rS   all_floatingz_LazyDtypes.all_floatingL  s,    >>7++RZZRZZPQQrU   c                `    | j                  t        j                  t        j                  g      S rW   )r  r`   r  r  r   s    rS   r  z_LazyDtypes.integerP  s    >>288RXX.//rU   c                    | j                  t        j                  t        j                  t        j                  t        j
                  g      S rW   )r  r`   r
  r  r  r  r   s    rS   all_integerz_LazyDtypes.all_integerT  s*    >>277BHHbhhABBrU   c                `    | j                  t        j                  t        j                  g      S rW   )r  r`   r  r  r   s    rS   unsignedz_LazyDtypes.unsignedX  s    >>299bii011rU   c                    | j                  t        j                  t        j                  t        j                  t        j
                  g      S rW   )r  r`   r  r  r  r  r   s    rS   all_unsignedz_LazyDtypes.all_unsigned\  s*    >>288RYY		299EFFrU   c                `    | j                  t        j                  t        j                  g      S rW   )r  r`   r  r  r   s    rS   complexz_LazyDtypes.complex`  s    >>2<<788rU   c                B    | j                  t        j                  g      S rW   )r  r`   r  r   s    rS   booleanz_LazyDtypes.booleand  s    >>288*%%rU   c                4    | j                   | j                  z   S rW   )rH  r  r   s    rS   r  z_LazyDtypes.inexacth  s    ==4<<''rU   c                4    | j                   | j                  z   S rW   )r  r  r   s    rS   all_inexactz_LazyDtypes.all_inexactl  s    t||++rU   c                h    | j                   | j                  z   | j                  z   | j                  z   S rW   )rH  r  r  r  r   s    rS   numericz_LazyDtypes.numericp  s'    ==4<<'$--7$,,FFrU   c                    | j                   | j                  z   | j                  z   | j                  z   | j                  z   S rW   )r  r  r  r  r  r   s    rS   r  z_LazyDtypes.allt  s>     0 0043D3DDLL<<( )rU   N)r   r   r   r  r  r  r  rH  r  r  r  r  r  r  r  r  r  r!  r  r   rU   rS   r  r  .  s   
= ( ( 4 4 R R 0 0 C C 2 2 G G 9 9 & & ( ( , , G G ) )rU   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  .   K   | ]  }|d    k(    ywr   Nr   )r  r]   r   s     rS   r  z3strict_promotion_if_dtypes_match.<locals>.<genexpr>  s     0&)	0r  r4  standard)r  r   numpy_dtype_promotionr   s   `rS    strict_promotion_if_dtypes_matchr(  }  s5    
 	000''11		%	%j	11rU   z$([0-9]+(?:\.[0-9]+)*)(?:(rc|dev).*)?c                    t         j                  |       }|t        d|  d      t        d |j	                  d      j                  d      D              S )NzUnable to parse version ''c              3  2   K   | ]  }t        |        y wrW   r  r  s     rS   r  z parse_version.<locals>.<genexpr>  s     5!s1v5r  r   rL  )_version_regexrW  r   rh  rX  r9  )r   r  s     rS   parse_versionr-    sP    1!Y
015
66	5qwwqz//45	55rU   c                 4    t        t        j                        S rW   )r-  r`   __version__r   rU   rS   numpy_versionr0    s    	r~~	&&rU   )r  one_containingc                   |3| D cg c]'  }t        ddt        t         ||                  i|) }}n[| D ]T  j                  d      }|1dj	                  fdt        j                               D              }t        |      d<   V | }Zt        fd|D              }|s$J d ddj	                  d |D              z          |d	   d
d<   t        j                  g      S t        j                  | S c c}w )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``.
  r  rO   c              3  2   K   | ]  }| d |      yw)=Nr   )r  r   r  s     rS   r  z+parameterized_filterable.<locals>.<genexpr>  s'      !=%& %&3a1w !=s   c              3  0   K   | ]  }|d    v r|  ywr  Nr   )r  r  r1  s     rS   r  z+parameterized_filterable.<locals>.<genexpr>  s%      ?B&"_*==  ?s   zNo testcase_name contains 'z"'. The testcase_name values are
  z
  c              3  &   K   | ]	  }|d      ywr6  r   )r  r  s     rS   r  z+parameterized_filterable.<locals>.<genexpr>  s     J""_%Js   r   r?   r   )
r   rT   r   rc   rO  r  r,  rh  r   r  )rq   r  r1  r  kwargs_with_testcase_namefiltereds     ``  rS   parameterized_filterabler:    sC   B  !
 J+Cb0A,BCJrJ! !  >ff_-m		 !=*0*;!= =.}=b> !' ?"; ? ?H L#N#3 4) )kkJ0IJJKL8 
!BB))2$//))+DEE/!s   ,Dc              #     K   	 t        j                  |        d t        j                  |        y# t        j                  |        w xY ww)zFManages registering/unregistering an event duration listener callback.N)r   %register_event_duration_secs_listener"unregister_event_duration_listener)callbacks    rS    register_event_duration_listenerr?    s8     <44X>	11(;J11(;s   A3 AA

Ac            
   +    K   | D ci c]#  }|t         j                  j                  |d      % }}t         j                  j                  | j	                         D ci c]  \  }}|	|| c}}       	 d | D cg c]"  }t         j                  j                  |d      $ }}t         j                  j                  |j	                         D ci c]  \  }}|	|| c}}       yc c}w c c}}w c c}w c c}}w # | D cg c]"  }t         j                  j                  |d      $ nc c}w }}t         j                  j                  |j	                         D ci c]  \  }}|	|| nc c}}w c}}       w xY ww)a  Context manager to temporarily set/unset one or more environment variables.

  Caution: setting environment variables is not thread-safe. If you use this
  utility, you must annotate your test using, e.g., @thread_unsafe_test() or
  @thread_unsafe_test_class().

  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
  N)r   environpopr  r   )rq   r  originalr   r   rO   s         rS   set_envrD    s%    2 9??c2::>>#t,,?(?**fllnFdaQTFGL	.45sT	"5A5JJ(8J1AMq!tJK @F 	6J /55sT	"55A5JJ(8J1AMq!tJJKsu   F(C,.F
C1
(C1
-F6D :F>'C7%.F
C<
C<
#FF'D/.4F"
E3-E32FFc                      t        j                   fdd      | \  }\  }} t        j                  d       ||      }||fS )Nc                 0    t        j                  g|  S rW   )r   vjp)rp   rs   s    rS   rm   z fwd_bwd_jaxprs.<locals>.<lambda>  s    CGGA%% rU   T)return_shapec                     | |      S rW   r   )resoutss     rS   rm   z fwd_bwd_jaxprs.<locals>.<lambda>  s
    s4y rU   )r   r  )rs   example_args	fwd_jaxpry_shape	res_shape	bwd_jaxprs   `     rS   fwd_bwd_jaxprsrQ    sP    %:CNN%D%:;G%I!)!gy9cnn89)WM)	I	rU   c                  
 ||}t        j                  |       
t        j                         }|j	                  d      xs |j	                  d      }|rAt        j
                         dk(  r*t        j                  
j                  
j                        n
j                  
fd} ||      } ||      }|j                  ddd|z  z   f      j                  dd|z  z   d	      j                  |       }|j                  d      j                  |       }	d	|	j                  dd |	j                  dd|z  z   df      j                  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]]

  NaarcharmDarwinc                   d}j                   |z  x}}j                  |z  }t        j                  dd| z  z   j                        }t        t              5  t        j                  ||| j                         }|d d d    }|||  dz
  d ||d| dz    d d d        | dkD  rj                  |d<   j                  |d<   | d	kD  r	 || <   	||  dz
  <   t        j                   |d	<   t        j                  |d<   |S # 1 sw Y   ixY w)
Ngry	O
@rW  r]  r\   )categoryr   r   r   )maxexpminexpr`   r  r]   ignore_warningRuntimeWarninglogspacer  r   r|  )
r@  prec_dps_ratiologminlogmaxlogtinyaxis_pointshalf_neg_line	half_linerY   smallests
           rS   make_axis_pointsz.complex_plane_sample.<locals>.make_axis_points  s   'Nll^33FVll^+G((1q4x<u{{;K		0 .{{67DLLm 2&&i"+k4%!)B -k!D1H. axyyk!n		k"oax#)k$'k4%!)ffWKNffKO. .s   ?DDr   rW  r]  r   r   )r`   rY   r  machiner  system	nextaftertinyr   rG  repeatre   rF  r{  )r]   size_resize_imrg  
is_arm_cpurf  real_axis_pointsimag_axis_points	real_part	imag_partrY   re  s             @@rS   complex_plane_samplers    s>   , _G
((5/%'!!'*Gg.@.@.G*4>8??CTX`C`R\\%**eii0fkfpfp(. &g.%g.&&AGO'<=DDQW_VWX__`ef)%%a(--e4))..Q[" 56==a!g+oqQ)	Y	rU   c                      e Zd ZdZ eddddd      Zej                         D  ci c]  \  }}||
 c}}} 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 c c}}} w )vectorize_with_mpmathzLSame as numpy.vectorize but using mpmath backend for function evaluation.
  	complex32r  r  
complex256clongdouble)r  r  r  float128
longdouble   5   )r  r  iiii)r  r  r  ry  r     i   i @  c                   |j                  dd       }|t        d      |j                  dd      | _        |j                  dd      | _        || _        i | _        i | _        | j                  j                         D ]N  \  }}| j                  j                  j                         }||_        || j
                  |<   || j                  |<   P t        | 4  |i | y )Nmpmathz3vectorize_with_mpmath: no mpmath argument specifiedextra_prec_multiplierr   
extra_prec)rB  r   r  r  r  contextscontexts_inv
float_precr   mpcloneprecr>  r   )r   rp   rq   r  	fp_formatr  ctxrP  s          rS   r   zvectorize_with_mpmath.__init__[  s    ZZ$'F~LMM!',CQ!GDjjq1DODKDMD??002 )	4KKNN  "cch!$dmmI(d	) 
Gd%f%rU   c                8   t        |t        j                  t        j                  t        j                  f      r@t        |j                        }| j                  j                  ||      }| j                  |   S t        dt        |      j                   d      )Nzget mpmath context from z	 instance)rn   r`   r"  rH  r8  r   r]   map_complex_to_floatrc   r  NotImplementedErrorr  r   )r   r   r  s      rS   get_contextz!vectorize_with_mpmath.get_contextl  su    !bjj"++r/A/ABCagg,i++//	9Ei]]9%%
 8a9I9I8J)T
UUrU   c                j   t        |t        j                        rVt        j                  t	        | j
                  |j                               t              j                  |j                        S t        |t        j                        r| j                  }| j                  |      }|j                  \  }}t        j                  |      r%|j                  |j                   j"                        S t        j$                  |      r%|j                  |j                   j&                        S t        j(                  |      r%|j                  |j                   j*                        S t        j,                  |      rt        j.                  |      \  }}t1        t        j2                  ||            }t1        ||z
        }	|j                  |j                   j5                  ||	||            }
|j-                  |
      sJ |
j6                         |
S t        |t        j8                        rg| j                  |j:                        | j                  |j<                        }}|j>                  jA                  |j6                  |j6                  f      S tC        dtE        |      jF                   d      )zKConvert numpy array/scalar to an array/instance of mpmath number type.
    r\   convert z instance to mpmath number type)$rn   r`   r"  fromiterrP  nptompflattenrd  rG  r  rH  r  r  _prec_roundingisposinfmake_mpflibmpfinfisneginffninfrJ  fnanisfinitefrexpr>  ldexpfrom_man_exp_mpf_r8  r{  imagcontextmake_mpcr  r  r   )r   r   r  r  r  roundingmantissaexponentmanexpr:  r:  ims                rS   r  zvectorize_with_mpmath.nptomps  s    !RZZ [[T[[!))+6fEMMaggVV	Ar{{	#{{fQc))ndH	Q||FLL--..;;q>||FLL..//88A;||FLL--..;;q>XXa[("((8T*+(T/"LL223T8LM||A''	Ar))	*{{166"DKK$7"bZZ  "((BHH!566
a)9)9(::YZ
[[rU   c                   t        |t        j                        r|j                  j                  dk(  r|j                         }|d   }|j                  }| j                  |   }t        ||j                        rt        t        | j                  |         }n)t        ||j                        rt        t        |      }nd}|t        j                  t        | j                  |      |      j                  |j                         S t        || j"                  j$                  j&                        r|j                  }t        ||j                        r| j                  |   }t        t        | j                  |         } |       j                  d      j)                  t        t        |            }| j                  |j*                        |d<   | j                  |j,                        |d<   |j)                  |      d   S t        ||j                        r| j                  |   }t        t        |      }|j/                  |      r
 | j"                  j0                  j2                  g |j4                  |j6                   \  }}	}
}|dk\  sJ ||	|
||j4                  f       |
|z   | j8                  |   k  r|r|j:                   S |j:                  S |
|z   | j<                  |   kD  r|r|j>                  S |j@                  S  ||r|	 n|	      }	t        jB                  |	|
      }t        j.                  |      sJ |||j4                  |	f       |S |jE                  |      r |t        jF                        S |jI                  |      r: ||j4                  d   rt        j@                         S t        j@                        S tK        dtM        |       d      )z8Convert mpmath instance to numpy array/scalar type.
    Or   Nr\   r   r  z& instance to numpy floating point type)'rn   r`   r"  r]   rd   r  r  r  mpcr  map_float_to_complexmpfr  rP  mptonprG  r  r  ctx_mp	mpnumericrF  r{  r  r  r  	normalizer  r  float_minexpzerofloat_maxexpninfr|  r  rJ  rI  isinfr  r  )r   r   x_flatr  r  r  r]   r:  signr  r  bcs               rS   r  zvectorize_with_mpmath.mptonp  s    !RZZ QWW\\S%8yy{fAYdLLc##C(i	D#''	"D55i@AdCGG$I&		{{3t{{F35AII!''RR	At{{))33	4IIc	Asww	%%c*	D55i@AGOOA##GB	$:;{{166"!{{166"!vve}Qa!%%c*	I&<<?:t{{00::YAGGYcFXFXY
$S"q
74c2qww7
72X)))44 $CHH92#((22X)))44#38800dsd,#hhsC !Q
5!Q!5
5(YYq\rvv
YYq\!''!*w9
9"&&9
9
a	1WX
YYrU   c                2   g }d }|D ]  }t        |t        j                  t        j                  t        j                  f      rJ|j                  | j                  |             || j                  |      }p|| j                  |      u rJ |j                  |        t        |j                  | j                  z        | j                  z   }|j                  |      5  t        
| 8  |i |}d d d        t        t              rg }|D ]  }	t        |	t        j                        r|	j                   j"                  dk(  s*t        |	| j$                  j&                  j(                        r| j+                  |	      }	|j                  |	        t        |      S t        |t        j                        r|j                   j"                  dk(  s*t        || j$                  j&                  j(                        r| j+                  |      S |S # 1 sw Y   xY w)Nr  )rn   r`   r"  rH  r8  r   r  r  r>  r  r  r  	extraprecr>  __call__rh  r]   rd   r  r  r  r  )r   rp   rq   mp_argsr  r  r  rr   lstr:  rP  s             rS   r  zvectorize_with_mpmath.__call__  s   GG 	A

BKK1C1CD	Et{{1~&?$$Q''D,,Q//
//q W\\D$>$>>?$//QJ			:	& 4w3F3f4 &% c !2::&177<<3+>!T[[//99:kk!n!

1	
 3Z	FBJJ	'FLL,=,=,Dfdkk00::;[[  M!4 4s   !HH)r   r   r   r  r   r  r   r  r  r  r  r   r  r  r  r  r  )r  r   r   rP  s   000@rS   ru  ru  >  s     k;P\gs  AN  O+?+E+E+GHH41a!Q$H* 	, 	,&"V\6+ZZ q Is   A4ru  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 Zd Zd Zd Zy)!numpy_with_mpmathzNamespace of universal functions on numpy arrays that use mpmath
  backend for evaluation and return numpy arrays as outputs.
  )r0  absolutesqrtr  expm1exp2loglog1plog10log2sincostanarcsinarccosarctansinhcoshtanharcsinharccosharctanhsquarepositivenegative	conjugater  sincr  absminlnasinacosatanasinhacoshatanh)	r0  r  r  r  r  r  r  r  r  c                    || _         | j                  D ]V  }| j                  j                  ||      }t	        | |      rt        | |      }n|fd}t        | |t        ||||             X y )Nc                :     t        | j                  |      |       S rW   )r  r  )r   mp_names     rS   opz&numpy_with_mpmath.__init__.<locals>.op  s    ,G,Q/
/rU   )r  r  r  )r  	_provides	_mp_namesrc   r{  r  setattrru  )r   r  r  r  r   r  r  s          rS   r   znumpy_with_mpmath.__init__  sz    DK 
H""4.g	t	T4  " 	0 dD/6Yn  |F  G  H
HrU   c                    ||z  S rW   r   r  s     rS   r  znumpy_with_mpmath.square  s    q5LrU   c                    |S rW   r   r  s     rS   r  znumpy_with_mpmath.positive  s    HrU   c                    | S rW   r   r  s     rS   r  znumpy_with_mpmath.negative
  s	    2IrU   c                   |j                   }t        ||j                        rV|j                  |j                        r;|j                  |j                  j                  |j                  j                  f      S |j                  |      S rW   )	r  rn   r  r  r  r  r|  r  r  r   r   r  s      rS   r  znumpy_with_mpmath.sqrt  s]    
))C!SWW 
166	||SWW]]AFFLL9::88A;rU   c                8    |j                   j                  |      S rW   )r  r  r  s     rS   r  znumpy_with_mpmath.expm1  s    99??1rU   c                   |j                   }t        ||j                        r|j                  |j                        ry|j                  |j
                        r]|j                  }|j                  dkD  rC|j
                  dkD  r4|j                  |j                  j                  |dz  j                  f      S |j                  dkD  rD|j
                  dk  r5|j                  |j                  j                  | dz  j                  f      S |j                  dk  rG|j
                  dk  r8|j                  |j                   j                  d|z  dz  j                  f      S |j                  dk  rG|j
                  dkD  r8|j                  |j                   j                  d|z  dz  j                  f      S |j                  |      S )Nr      rW  )
r  rn   r  r  r{  r  pir  r  r  )r   r   r  r  s       rS   r  znumpy_with_mpmath.log1p  sG   
))C!SWW 
166	syy0VV66A:!&&1*qvv||b1f^^<=
=66A:!&&1*qvv||rcAg__=>
>66A:!&&1*b10C0CDE
E66A:!&&1*R!0B0BCD
D99Q<rU   c                   |j                   }t        ||j                        rH|j                  |j                        r|j                  |j
                        s|j                  |j
                        r|j                  dkD  r;|j                  |j                  j                  |j                  j                  f      S |j                  |j                  j                  |j                   j                  f      S |j                  |j
                        rV|j                  |j                        r;|j                  |j                  j                  |j                  j                  f      S |j                  |      S r   )r  rn   r  r  r  r{  r  r  r  r  onerI  r  r  s      rS   r  znumpy_with_mpmath.tan,  s    
))C!SWW 
166			!&& 1S\\!&&5I66A:sxx~~sww}}=>
>||SXX^^swwh-=-=>??	166	s||AFF3||SWW]]CGGMM:;;771:rU   c                   |j                   }t        ||j                        rH|j                  |j                        r|j                  |j
                        s|j                  |j
                        r|j                  dkD  r;|j                  |j                  j                  |j                  j                  f      S |j                  |j                  j                  |j                   j                  f      S |j                  |j
                        rV|j                  |j                        r;|j                  |j                  j                  |j                  j                  f      S |j                  |      S r   )r  rn   r  r  r  r{  r  r  r  r  r  rI  r  r  s      rS   r  znumpy_with_mpmath.tanh:  s    
))C!SWW 
166			!&& 1S\\!&&5I66A:sxx~~sww}}=>
>||SXX^^swwh-=-=>??	166	s||AFF3||SWW]]CGGMM:;;88A;rU   c                f    |j                   j                  |      |j                   j                  z  S rW   )r  r  ln2r  s     rS   r  znumpy_with_mpmath.log2H  s!    99<<?QYY]]**rU   c                f    |j                   j                  |      |j                   j                  z  S rW   )r  r  ln10r  s     rS   r  znumpy_with_mpmath.log10K  s!    99<<?QYY^^++rU   c                f    |j                   j                  ||j                   j                  z        S rW   )r  r  r  r  s     rS   r  znumpy_with_mpmath.exp2N  s"    99==QYY]]*++rU   c                   |j                   }t        ||j                        r;|j                  }|j                  }|j
                  }|j                  |j                        rs|j                  dk  rdnd}||z  |j                  |j                        rdndz  }|j                  dk  r| n|}|j                  |j                  |j                  f      S |j                  |j                        r1|j                  |j                  |j                  j                  f      S |j                  dkD  r.|j                  dk(  r|j                  |      j                         S |j                  |      S Nr   r   r   r  r]  )r  rn   r  r  r|  r  r  r{  r  r  r  r  r  )	r   r   r  r  r|  r  	sign_realr{  r  s	            rS   r  znumpy_with_mpmath.arcsinQ  s   
))C!SWW
 66bGGcXXd	166	&&1*B!	2~cii&7Q?vvzts||TZZ45599QVV||TZZ677 
!!xx{$$&&88A;rU   c                (   |j                   }t        ||j                        r_|j                  }|j                  }|j
                  }|j                  |j                        rx|j                  |j                        r|j                  dkD  r|dz  nd|z  dz  }n|dz  }|j                  dk  r|n| }|j                  |j                  |j                  f      S |j                  |j                        r]|j                  }|j                  dk  rdnd}|j                  dkD  r|n|}|j                  |j                  | |z  j                  f      S |j                  dk(  r!|j                  dkD  r|j                  |       S |j                  |      S )Nr   r  rW  r]  r   r   )r  rn   r  r  r|  r  r  r  r{  r  r  r  )	r   r   r  r  r|  r  r{  r  	sign_imags	            rS   r  znumpy_with_mpmath.arccosl  s@   
))C!SWW
 66bGGcXXd	166	99QVV66A:a1r6A:$a$ffqjssd||TZZ45599QVVgg&&1*B!	vvztr||TZZ9*s*:)A)ABCC
 
1!|88A;rU   c                   |j                   }t        ||j                        r<|j                  }|j                  }|j
                  }|j                  |j                        rs|j                  dk  rdnd}|j                  dk  r| n|}||z  |j                  |j                        rdndz  }|j                  |j                  |j                  f      S |j                  |j                        r1|j                  |j                  j                  |j                  f      S |j                  dk(  r/|j                  dk  r |j                  |       j                         S |j                  |      S r  )r  rn   r  r  r|  r  r  r  r{  r  r  r  r  )	r   r   r  r  r|  r  r  r{  r  s	            rS   r  znumpy_with_mpmath.arcsinh  s   
))C!SWW
 66bGGcXXd	166	&&1*B!	vvzts2~cii&7Q?||TZZ45599QVV||QVV\\4::677 
1"1((**99Q<rU   c                   |j                   }t        ||j                        r#|j                  }|j                  }|j
                  }|j                  |j                        r|j                  dk  rdnd}|j                  |j                        r|j                  dk  rdnd|z  |z  dz  n|j                  dk  r||z  n|}|j                  |j                  |j                  f      S |j                  |j                        rB|j                  dk  rdnd}||z  dz  }|j                  |j                  |j                  f      S |j                  |      S )Nr   r   r   rW  r  r]  )r  rn   r  r  r|  r  r  r{  r  r  r  r  )r   r   r  r  r|  r  r  r  s           rS   r  znumpy_with_mpmath.arccosh  s   
))C!SWW 66bGGcXXd	166	&&1*B!	 YYqvv 
1Y
.
3a
7"#&&1*	B$ 	
 ||SYY

34499QVV&&1*B!	2~!||SYY

34499Q<rU   c                H   |j                   }t        ||j                        r|j                  }|j                  }|j                  |j                        s|j                  |j                        rd|j                  dk  r+|j                  | dz  j                  |j                  f      S |j                  |dz  j                  |j                  f      S |j                  dk(  r/|j                  dk  r |j                  |       j                         S |j                  |      S )Nr   r]  r   )r  rn   r  r  r  r  r{  r  r  r  r  r  r   r   r  r  r  s        rS   r  znumpy_with_mpmath.arctan  s    
))C!SWW
 66bXXd	166	cii/66A:a

;<
<||b1f^^TZZ899 
1"!''))88A;rU   c                F   |j                   }t        ||j                        r|j                  }|j                  }|j                  |j                        s|j                  |j                        rd|j                  dk  r+|j                  |j                  | dz  j                  f      S |j                  |j                  |dz  j                  f      S |j                  dk(  r.|j                  dkD  r|j                  |      j                         S |j                  |      S )Nr   r]  r   )r  rn   r  r  r  r  r{  r  r  r  r  r  r  s        rS   r  znumpy_with_mpmath.arctanh  s    
))C!SWW
 66bXXd	166	cii/66A:tzzRC!G??;<
<||TZZ"q&899
 
1!yy|%%''99Q<rU   c                   d }|j                   }t        |      }t        ||j                        r ||||j                  |j                  |j                        \  }} ||||j
                  |j
                  |j
                        \  }	}
|j                  |j                  |	j                  f      |j                  |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                   \  }}}}| j                  |      r"| j                  |      r| j                  |      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 ]  \  } }!| |!k7  r n|dz  } ||z
  }"||"z	  }#||"z	  }$|#r+| j                  ||#| t        t        |#            dz
  f      n|r| j                   n| j                  }%|$r+| j                  ||$| t        t        |$            dz
  f      n|r| j                   n| j                  }&|%|&fS )Nr]  r?   0r   r   )	r  r  r  binr   r<  r  r  r  )'r  rR   rw  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'                                          rS   workerz+numpy_with_mpmath.normalize.<locals>.worker  s%   ob"b#ob"b#ob"b#ob"b#ll1o#,,q/cll1o!tr2r2b BG_dBG_dBG_dBG_d #SYqr]d"SYqr]d"SYqr]d"SYqr]d
c$iTCIs4y
9aAD	M"T)dAD	M"T)dbdO &"b8

a r6b RZdRZd AE3<<TB3CI(:;<XZCHH9`c`h`hb@D3<<TB3CI(:;<XZCHH9`c`h`hbVmrU   )	r  r0  rn   r  r{  r  r  r  r  )r   exact	referencer   r)  r  r3  rrrvirivs              rS   r  znumpy_with_mpmath.normalize  s    *X --CJE%!c5%**innejjIfb"c5%**innejjIfb"\\288RXX./rxx>R1SSS	E377	#Cy%88QrU   N)r   r   )r   r   r   r  r  r   r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   rU   rS   r  r    s    ) 8&Wg	)H&$+,,6B6..,9rU   r  c                     	 ddl } t        d | j                  j                  d      D              dk\  S # t        t
        f$ r Y yw xY w)zvReturns True if the installed hypothesis version is thread-safe.

  Hypothesis versions >= 6.136.9 are thread-safe.
  r   Nc              3  2   K   | ]  }t        |        y wrW   r  r  s     rS   r  z,hypothesis_is_thread_safe.<locals>.<genexpr>/	  s     ;AQ;r  rL  )      	   T)
hypothesisrh  r/  r9  ModuleNotFoundErrorr  )hps    rS   hypothesis_is_thread_safer8  (	  sF    
;!5!5c!:;;{JJ
{	+ s   14 AAc                   	 ddl }g }t        |j                  d      r%|j                  |j                  j                         |j                  j                  dddd| d|       |j                  j                  d|j                  j                  d      dd	|j                  j                  |j                  j                  |j                  j                  |j                  j                  |j                  j                   |j                  j"                  f
       t$        j&                  }t)        j*                  d|       |j                  j                  |       y# t        t        f$ r Y yw xY w)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   Ndiffering_executorsrM   T)databasederandomizedeadlinemax_examples
print_blobsuppress_health_checkinteractiver   F)parentr>  report_multiple_bugs	verbosityphaseszUsing hypothesis profile: %s)r5  r6  r  r{  HealthCheckr   r:  settingsregister_profileload_profile	Verbosityr_  Phaseexplicitreusegenerater  explainHYPOTHESIS_PROFILEr   logginginfo)r>  r7  suppressed_checksprofiles       rS   setup_hypothesisrU  3	  s2     R^^23R^^??@++-   ++[[%%o6 $$ ((


((..
((


((//
((

   $$'	,,-w7++7#[ {	+ 
s   E E0/E0c                 L    t         j                  ydt         j                  v ryy)z"Returns None, "bazel" or "pytest".Nz	bazel-outbazelr  )r  
executabler   rU   rS   runtime_environmentrY  p	  s!    ^^cnn$rU   )rR   r   r   r   )r]   r<   r   r>  )rf   r;   r   z
np.ndarrayr  )rt   zCallable[..., Any]ru   ro   )r   r   r   ro   )r   "int | float | ToleranceDict | Noner   r2   )r   rZ  r   rZ  r   r2   )r?   )r   r   r   r   r   r   r   r  )r   r   r   z(Generator[Callable[[], str], None, None])r   z;Iterator[Sequence[tuple[core.ClosedJaxpr, mlir.ir.Module]]])r   tuple[int, ...])r?  r>  r@  r>  rA  r>  )rG  r>  rH  r>  )rQ  r   )r   r>  )r   r>  r   ro   )Nr?   )r   r  rd  r   r   ro   )rj  str | Sequence[str]rk  r   )ro  r\  )r{  r\  ro  r\  r|  zstr | Callable[[str, str], str])r  r   r   ro   )r  r>  r  r>  )r  zIterable[str]r   ro   )r  r>  )r  r   )r|  r   )F)rW  )g      $@)g        r,  r%  rW   )r  MeshSpecr   zGenerator[None, None, None])NF)FN)r   r   r   r[  )rq   zSequence[dict[str, Any]]r  z&Callable[[dict[str, Any]], str] | Noner1  
str | None)r  N)r   ro   )   r  )r   r^  (  
__future__r   r   collections.abcr   r   r   r   r   
contextlibr	   r
   r7  rx   r   r  rQ  rB  r   r  r:  r  r   r  	threadingtypingr   r   r  rA  absl.testingr   jax._srcr   r   r   r   r   r   r   rX   r   r   r   r  r   r   r   r   r   jax._src.cloud_tpu_initr   jax._src.interpretersr    jax._src.laxr!   jax._src.libr"   jax._src.lib.mlir.dialectsr#   jax._src.numpy.utilr$   r%   jax._src.public_test_utilr&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   jax._src.test_loaderr3   r4   r5   r6   jax._src.tree_utilr7   r8   r9   r:   jax._src.typingr;   r<   jax._src.utilr=   numpyr`   numpy.randomr  r  install_failure_signal_handlerstring_flagr   int_flagr>  getenvr  r  	bool_flagbool_envSKIP_SLOW_TESTSrF  rP  r  rP   rT   r^   rh   rz   r   r   r   r   r   stdoutcapture_stdoutstderrcapture_stderrlocalr   r   r   test_event_listenerr   count_device_putcount_device_put_fast_path_hitr  r  r  r    count_jit_compilation_cache_missr  count_jax_array_shard_arg_callsr   r   r   r   r   r   r   r  r  r#  r'  r)  r+  r-  r0  r2  r4  rD  rJ  rS  rY  r[  rf  rm  rq  r  (skip_if_mosaic_gpu_exceeds_shared_memory$skip_if_triton_exceeds_shared_memoryr  r  r  r  r  r  r  __test__r  r  r  r  r  r  r  r  r  r   r  r
  r  r  r  r  r  r  r  r  	lru_cacher#  r<  rN  rS  rZ  r_  rb  rf  rh  rm  rt  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  cacher  r  r  r  r  r  r  r#  r%  r2  TestCaser  r  _PJIT_IMPLEMENTATION_name_NOOP_JIT_IMPLEMENTATIONJIT_IMPLEMENTATIONr  r[  r  rh  r   r]  r  r  r  r  r  r  r  r(  r,  r-  r0  r:  r?  rD  rQ  rs  	vectorizeru  r  r8  rU  rY  r   rU   rS   <module>r     sv    #  M M 0       	  	 
       &  &   !  & # %  # &   ; &  & * Fn n n n J U ? E O O 0     $ # # % FBP	 &fooibii)401*, 
 .fooibii0%89$  #&""FOO)51	/ *:)9)90FOO@%H
K*L & (V''BII&8'  "**56%?	5.2
,
,1>+
 #( #(L #**5#**5	'IOO 	' +,  F *    45 !-.B!C ()=> +,?@ +,BC +,CD #/0I#J  0<=h0i -"./A"B  C C 6 6 8 8 5 5 4 4 
 
>0==!+   %?&
)$
' ) ,	4 ,=9+<+<9	M, ( (9y'8'84	I( $
	@ 	@	!	  %  KF*!II I &; % &K &!^ #o 	!!$2 R ! !! +-; E6,6=
<
5>
43	@<:B"27<#
2(A4 . ."J"		 	  	    
  (~,-(( ~,@ ww "  7 !'    
%[ % #11 c3h 

 

O!	H
 
I) I)X 
2 CD6'
 =A!%9F$9F99F 9Fv < < L L@>B[BLL [|I IX
	:$zrU   