
    Vpf7                        d Z ddlZddlZddlmZ ddlZddlmZ ddlm	Z	 ddlm
Z
 ddl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ZddlZ ej                     ddlmZ ddlm Z  ddl!m"Z" eZ#ej$        ej%        ej&        ej'        ej(        ej)        fZ* G d dej+                  Z,e-dk    r  e	j.         ej/                               dS dS )a  Tests for JAX primitive coverage.

The bulk of the testing is done by `test_prim`, which is parameterized by
about 3500+ test harnesses. See `test_harnesses.py` docstring for a
description of test harnesses. That module contains also the definitions
of all the test harnesses, and a specification of which are only partially
implemented for JAX.

For each harness, we convert the JAX function to Tensorflow and then we run
it on the same inputs in "eager", "graph", or "compiled" mode and we check
that we get the same result as in JAX
(see `tf_test_util.ConvertAndCompare`).

Some harnesses need specific tolerances, or even custom equality assertions.
Also, for some harnesses we need to specify some data types that result
in Tensorflow errors (for some devices and compilation modes). These limitations
are captured as jax2tf_limitations.Jax2TfLimitation objects.

From the limitations objects, we generate a
[report](https://github.com/google/jax/blob/main/jax/experimental/jax2tf/g3doc/primitives_with_limited_support.md).
The report has instructions for how to re-generate it.

If a harness run fails with error, and a limitation that matches the device
and data types is found,
the error is logged but does not abort the test. If a harness run succeeds
and there are matching limitations, the test emits a warning. If you want to
turn these warnings into errors, you'd have to uncomment an assertion
in `tf_test_util.ConvertAndCompare`.

IMPORTANT: If you need to customize the testing of a particular primitive
conversion, you must create a class method in jax2tf_limitations.jax2tf_limitations,
with the same name as the harness.group_name (typically the same as the
primitive name). That class method should return the list of Jax2TfLimitation
objects for the harness.
See `jax2tf_limitations.limitations_for_harness`. If a group name does
not need limitations, then it must be listed in the
`jax2tf_limitations.harness_groups_no_limitations`.

    N)Any)logging)absltest)parameterized)dtypes)numpy)config)	test_util)jax2tf)mlir)xla)tf_test_util)Jax2TfLimitation)test_harnessesc                   0   e Zd Z ej        ej        d           ej        ed          dej	        fd                        Z
d Zd Z ej        d	 ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        fD                       ej        fd
            Zd Zd Zd Z ej        e           d             Z! ej        g d          d             Z" ej        e           d             Z#dS )JaxPrimitiveTestFinclude_jax_unimplz&Using reduced precision for gradient.*)categorymessageharnessc                     t          j                  }t          j                    t	          t          fd|                    }j        }                    |                                           }dj	        k    r/t          j        j        k    rdk    rt          j        d          t          j        j        r#dk    rdj        v rt          j        d          dt&          fd	}t          j        j        rBdk    r<d
j        v r |d           dj        v r |d           dj        v r |d           j                            dd          }	 t-          j        |          5   | j        |g|R d|i d d d            d S # 1 swxY w Y   d S # t2          $ rR}t          j        j        r:dt'          |          v r)t5          j        d|           t          j        d          |d }~ww xY w)Nc                 <    |                      j                  S )N)devicedtype)filterr   )lr   r   s    m/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/experimental/jax2tf/tests/primitives_test.py<lambda>z,JaxPrimitiveTest.test_prim.<locals>.<lambda>p   s#    8? *2 *G *G     eightpuz-b/264716764: error on tf.cast from c64 to f32gpuluzb/269388847: lu failures on GPUtargetc                 0    t          j        d|            )Nz=TODO(b/272239584): custom call target not guaranteed stable: )unittestSkipTest)r%   s    r   skipCustomCallTestz6JaxPrimitiveTest.test_prim.<locals>.skipCustomCallTest   s%    
R&
R
RT T Tr    custom_linear_solve_z$cusolver_geqrf, cublas_geqrf_batched	svd_shapecusolver_gesvdjtridiagonal_solve_shapez&cusparse_gtsv2_f32, cusparse_gtsv2_f64associative_scan_reductionsFlimitationszdoes not work with custom callszSuppressing error %sz<b/264596006: custom calls in native serialization fail in TF)r   limitations_for_harnessjtudevice_under_testtupler   dyn_fundyn_args_makerrng
group_namenp	complex64r   r'   r(   r	   #jax2tf_default_native_serializationvaluefullnamestrparamsgetjax"jax2tf_associative_scan_reductionsConvertAndCompare	Exceptionr   warning)	selfr   r/   func_jaxargsr)   r.   er   s	    `      @r   	test_primzJaxPrimitiveTest.test_primf   s    #:7CCK"$$F  G  G  G  G  GHSU U V VKH!!$((**--D'$$$
%%%MNNN28 A%   ?@@@T3 T T T T 17 G	5!W%555

C
D
D
D'***

.
/
/
/$(888

E
F
F
F")."4"45RTY"Z"Z
12MNN I IxH$HHHKHHHI I I I I I I I I I I I I I I I I I   

4
: 
+s1vv
5
5.222 ^___s=   !F! 5FF! FF! FF! !
G=+AG88G=c                    t          t          j                  t          t          j        d                   z  t          t          j        d                   z  t          t          j        d                   z  t          t          j                  z  t          t          j        d                   z  t          t          j        d                   z  t          t          j        d                   z  }t          t          j        j	        j	        j
                  t          t          j        j	        j	        j                  z  }t          t          j        j	        j	        j                  }t          t          |t                              }|D ]}|j        dk    r|j        dk    r|j        dk    r&|j        dk    s|j        d	k    r=|j        d
v rG|j        dk    rS|j        dk    r_|j        dk    rk|j        dv ru|j        |v r|                     ||           |                     ||           dS )z:Fail if there are JAX primitives that are not implemented.cpur#   r"   key
axis_indexsharding_constraintoptimization_barrierdebug_callbackdebug_print)max_contiguousmultiple_ofpallas_calltpu_custom_callcustom_partitioning)dot_product_attention_fwddot_product_attention_bwd!dot_product_attention_fwd_wrapper!dot_product_attention_bwd_wrapperN)setr   _translations_backend_specific_translationsr   
_lowerings_platform_specific_loweringsr@   experimentalr   tf_impltf_impl_with_avalstf_not_yet_implr3   sortedr=   nameassertNotInassertIn)rE   all_primitivesrb   rd   ps        r   test_primitive_coveragez(JaxPrimitiveTest.test_primitive_coverage   s=    	C
c07
8
8	9
c07
8
8	9 c07
8
8	9 do

		
 d/6
7
7	8 d/6
7
7	8 d/6
7
7	8  #")0899C&9=; =; ;G#*18HIIO6.c:::;;N " "	
<			
(	(	(	
)	)	)	
#	#	#qv'>'>	
4	4	4	
=	 	 	
$	$	$	
(	(	(	
  
 
 		
?	"	"w	 	 	 	 	a!!!!?" "r    c                 R   d t           j        D             }t          dt          |           d           dt           j        dt
          fdi }|D ]W}|j        D ]M}|j        rDt          d|j        z   |j	        |j
        dd	
          }||f|t           ||                    <   NX|D ]7}t          j        |          D ] }||f|t           ||                    <   !8t          dt          |           d           dg}t          |          }t          |                                fd          D ]\  }}d                    t          |j	                            }d                    t          |j                            }	|j        }
|j        rd|
z   }
|j        rd|
z   }
|j        rd|
z   }
|j        s|j        r|}n|j        s|j        r|}n|                    d|j         d|
 dt          j        |j
        d	           d| d|	 d           t2          j                            d          st9          j        d          |                     dt?          j                                | !                    tD          j#        j$        d           tK          t2          j&                            t2          j&        '                    tP                    d                    5 }|)                                }ddd           n# 1 swxY w Y   t2          j&                            t2          j&        '                    tP                    d          }tK          |d          5 }|*                    |+                    dtY          tZ          j.        /                                                    +                    d d!                    |                    +                    d"d!                    |                               ddd           dS # 1 swxY w Y   dS )#zUGenerates primitives_with_limited_support.md.

    See the doc for instructions.
    c                 @    g | ]}|                     |d           |S )Tr   )r   ).0hs     r   
<listcomp>zBJaxPrimitiveTest.test_generate_limitations_doc.<locals>.<listcomp>   s>       88A$8//	  r    zFound z  test harnesses that work in JAXro   r   c                 p    | j         |j        |j        t          d |j        D                       |j        fS )Nc              3   H   K   | ]}t          j        |          j        V  d S N)r8   r   rf   )rn   ds     r   	<genexpr>zVJaxPrimitiveTest.test_generate_limitations_doc.<locals>.unique_hash.<locals>.<genexpr>   s-      77BHQKK$777777r    )r7   descriptiondevicesr3   r   modes)ro   r   s     r   unique_hashzCJaxPrimitiveTest.test_generate_limitations_doc.<locals>.unique_hash   s;    lAM1977ah77777B Br    zNot implemented in JAX: FT)rv   rw   r   expect_tf_errorskip_tf_runz unique limitationsz
| Affected primitive | Description of limitation | Affected dtypes | Affected devices | Affected compilation modes |
| --- | --- | --- | --- | --- |c                      |  S rs    )pairry   s    r   r   z@JaxPrimitiveTest.test_generate_limitations_doc.<locals>.<lambda>   s    kk46H r    rL   z, zNumeric comparison disabled: z
TF error: zTF test skipped: z| z | )empty_means_allz |JAX_OUTPUT_LIMITATIONS_DOCzNSet JAX_OUTPUT_LIMITATIONS_DOC=1 to enable the generation of the documentationrK   z:Documentation generation must be run with JAX_ENABLE_X64=1z4../g3doc/primitives_with_limited_support.md.templateNz+../g3doc/primitives_with_limited_support.mdwz{{generation_date}}z{{tf_error_table}}
z${{tf_numerical_discrepancies_table}})0r   all_harnessesprintlenHarnessr   jax_unimplementedenabledrv   rw   r   hashr0   listre   valuesjoinrx   skip_comparisonrz   r{   custom_assertappendr7   dtypes_to_strosenvironr?   r'   r(   assertEqualr1   r2   
assertTruer	   
enable_x64r;   openpathdirname__file__readwritereplacer=   datetimedatetoday)rE   	harnessesunique_limitationsro   r   tfltf_error_table tf_numerical_discrepancies_tablerw   rx   rv   to_tableftemplateoutput_filery   s                  @r   test_generate_limitations_docz.JaxPrimitiveTest.test_generate_limitations_doc   sB    !/  I 

C3y>>
C
C
CDDDB~- B2B B B B B VX 	C 	C" C C!9 	C -G!--W+,9*+(38/3	5 5 5#
 <=c(
T++a"5"566
7C  = =7:: = =!78!f4Aq 1 12233= 

?3)**
?
?
?@@@	#N
 (,N';';$!!##)H)H)H)HJ J J  1		&++,,giiqw((eMk	
	 D5C	
	 1"[0	
 8)K7	
 !+ !  3oodq| d d d d)!(DIIId dNUd dZ_d d d   
 :>>677 
Z   	UC133444OODF F F 

GOOH%%B	D 	D
E 
E  IJh	              
 ',,
!!57 7K 
k3		 1ggh4c(-:M:M:O:O6P6PQQw+TYY~-F-FGGw=tyyIi?j?jkk                   s%   0LLL.B!PP #P c              #   H   K   | ]}t          d |j         |          V  dS )_)testcase_namef_jaxN)dict__name__)rn   r   s     r   ru   zJaxPrimitiveTest.<genexpr>$  sW       "" ""  -U^--  "" "" "" "" "" ""r    c                    t           j        t          j        t          j        t          j        g}|D ]N}|D ]I}t          j        ddg|          }t          j        ddg|          }|                     |||           JOd S )N      r         )r   bfloat16r8   int32int64float32arrayrB   )rE   r   typesx_dtypey_dtypexys          r   test_type_promotionz$JaxPrimitiveTest.test_type_promotion$  s     _bh"*=E , , , ,'HaV7+++HaV7+++ua++++,, ,r    c                    t          j        g d          }t          j        d          }|                     t           j        ||           t          j        ||          }t          j        j        st          j
        j                                        5 }|                     t          j        t           j                  ||                    }|                     ||           d d d            d S # 1 swxY w Y   d S d S )N)r   r   r      r   )jnpr   r8   r   rB   floor_divider	   r:   r;   tfcompatv1Sessionrunr   convertassertAllClose)rE   r   r   expectedsesstf1_ress         r   test_integer_divz!JaxPrimitiveTest.test_integer_div5  s$   	***++A
A3+Q2221%%H5; / 9<!! /T((;6>#*:;;AqAABBHg.../ / / / / / / / / / / / / / / / / /	/ /s   AC44C8;C8c                    t          j        ddgddgddggt           j                  }t          j        ddgt           j                  }dD ]0t	          j        fd          }|                     |||           1d S )NTFr   r   r   )r   r   c                 2    t          j        | |          S )N)axis)r   take)vir   s     r   r   z6JaxPrimitiveTest.test_boolean_gather.<locals>.<lambda>G  s    38Aqt#<#<#< r    )r8   r   bool_r   r@   jitrB   )rE   r   indicesr   r   s       @r   test_boolean_gatherz$JaxPrimitiveTest.test_boolean_gatherB  s    Xd|eT]UENCH& & &Fh1vRX...G 5 5g<<<<==e
UFG44445 5r    c                     t          j        g dg dg dg          t          j        g dg dg          }t          j        fd          }|                     ||           d S )N)g      ?g      ?       @)r   g      @      @)r   g      @g      @)r   r   r   )r   r   r   c                     |          S rs   r}   )r   r>   s    r   r   z:JaxPrimitiveTest.test_gather_rank_change.<locals>.<lambda>M  s    fQi r    )r   r   r@   r   rB   )rE   r   r   r>   s      @r   test_gather_rank_changez(JaxPrimitiveTest.test_gather_rank_changeJ  sv    Y///JKKFiIII.//GG''''((E5'*****r    )r   c                 v    t          j        g dt           j                  }|                     ||           d S )N)r   r   r   r   )r8   r   r   rB   rE   r   r   s      r   $test_reduce_ops_with_numerical_inputz5JaxPrimitiveTest.test_reduce_ops_with_numerical_inputP  s8    Xiiirz222F5&)))))r    )addmaxminmultiplyr\   )opc                     t          j        dt           j                  }t          j        d          }t          j        fd          }|                     |||           d S )N)   r   r   g      @c                 Z     t          | j        d d ddd f                   |          S )Nr   r   )getattrat)r   ur   s     r   r   z6JaxPrimitiveTest.test_scatter_static.<locals>.<lambda>Y  s0    !;ccc122g!;!;A!>!> r    )r8   onesr   r@   r   rB   )rE   r   r   updater   s    `   r   test_scatter_staticz$JaxPrimitiveTest.test_scatter_staticU  s]    WV2:...FZ^^FG>>>>??E5&&11111r    c                 v    t          j        g dt           j                  }|                     ||           d S )N)TFTr   )r8   r   r   rB   r   s      r   "test_reduce_ops_with_boolean_inputz3JaxPrimitiveTest.test_reduce_ops_with_boolean_input\  s;    X))):::F5&)))))r    N)$r   
__module____qualname__r   r   r   r1   ignore_warningUserWarningr   rI   rk   r   named_parametersr   r   subtractr   divideless
less_equalequalgreatergreater_equal	not_equalmaximumminimumr   r   r   r   sample_productREDUCEr   r   r   r}   r    r   r   r   ^   s         >"  
 3$LN N N)~5 ) ) )N N )V1" 1" 1"fU U Ur "=! "" "" GS\3<Hcnci%s}ckK!"" "" "" " " '*g , , ," ",/ / /5 5 5+ + + 3F###* * $#* 3AAABBB2 2 CB2 3F###* * $#* * *r    r   __main__)
testLoader)0__doc__r   r   typingr   r'   abslr   absl.testingr   r   r@   r   r   r   jax._srcr	   r
   r1   jax.experimentalr   jax.interpretersr   jax._src.interpretersr   r8   
tensorflowr   parse_flags_with_absljax.experimental.jax2tf.testsr   0jax.experimental.jax2tf.tests.jax2tf_limitationsr   jax._src.internal_test_utilr   DTypeallanyr   r   prodsumr  JaxToTfTestCaser   r   mainJaxTestLoaderr}   r    r   <module>r     s  & &P  				              ! ! ! ! ! ! & & & & & & 



                   % % % % % % # # # # # # ! ! ! ! ! ! % % % % % %              7 6 6 6 6 6 M M M M M M 6 6 6 6 6 6 GGGGHG
A* A* A* A* A*|3 A* A* A*F z(-,3,..////// r    