
    `iܩ                    0   U d 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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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m"Z"m#Z# ddlm$Z$ ddl%m&Z& ddl'm(Z( ddlm)Z)m*Z* dd	l+m,Z,m-Z- dd
lm.Z. ddl/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6 ddlm7Z7m8Z8 ddl9m:Z: ddl;Z;ddl<Z=ddl>Z>ddl?Z?ddl@Z?ddlAZ?ddlBZ?ddlCZ?ddlDZ?ddl?mEZE ddlFmGZGmHZH ddlImJZJ ddlKmLZL ddlMmNZN ddlOmPZPmQZQmRZRmSZSmTZT ddlUmVZVmWZW ddlXmYZY ddlZm[Z[m\Z\m]Z]m^Z^m_Z_ ddlZm`Z` ddlambZb ddlcmdZd ddlemfc mgZh ddlimjZj 	 ddlkZkdZln# em$ r dZlY nw xY wdZnd Zo G d  d!          Zp ejq        er          Zse?jt        u                                 d"Zvej        d#k    rd$Zvepw                    d%d&dd' (          Zxeyezd%<   epw                    d)d*d+ d,          Z{eyezd)<   ej|        }                    d-          duZ~eyezd.<    ee?j        d/          oe?j        j        Zepw                    d0d1ed2          Zeyezd0<   epw                    d3d4d5          Zeyezd3<   epw                    d6d7d5          Zeyezd6<   epw                    d8d9e d2          Zeyezd8<   ep                    d:d;ddd< =          Ze4e         ezd:<   d>Zd?Zi ai ad@ Z ej        dAdB          r e ej        dAdB                    a ej        dCdB          r e ej        dCdB                    adDdEdFdGdHe?j                                        fZdIZdZg dJZ edK eD                       ZdL ZdMe4e3         fdNZe& G dO dP                      ZdMe4e         fdQZddRZ G dS dT          Z G dU dV          ZdW ZdX Z G dY dZ          Z G d[ d\e          Z G d] d^e          Z G d_ d`e          Z G da dbe(          Zdc Ze#dd             Ze#de             Ze#df             Ze?j        j        j        Ze?j        j        j        Zdg Zdh Zdi Zdj Zee?j        j        _        ee?j        j        _        dk Zdl ee>dmdB          v Z ej        e dn          Ze                    dodpdqr           e                    dsedtu           e                    dvdpw           e                    dxdyez           e                    d{ed|u           e                    d}d~dpw           e                    ddpw           e                    dde e            exr
 e            nd           e                    ddpw           e                    dedBu           e                    ded|u           e                    dede           e                    dede           e                    ddpw           e                    ded|           e                    dej        d           d Zdej        v s	dej        v r@ ej        eej        f          Ze                                 e                                 e                                \  ZZej        dk    rej        Zn0ej        dk    rej        Znej        dk    rej        Zn
 e            Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Z ee;dd          sej        e;_        ej        d         gez   Z e?j        e            e e. ej                                        Z e e. ej                              j                  Z eej                             e. ej                              j        d                    ZddZddZ	 	 	 	 	 	 	 ddMeeeyf         fdZd Zd Zd Zd Zd Zd ZedfdZdefdZdee         dMee         fdZefdZej        dk    Zej        d#k    Zej        dk    Z ej                     dk    Z ej                     dv Z ej                     dv Z ej                     dk    Zd Z e            Zere#d             Zne#d             Zere#dd            Zne#dd            Zd Z	 ej
                    dk    Z edd          Z edd          Z edd          Ze?jt        j                                        Ze?jt        j                                        o e?j        j                                        Ze?jt        j                                        Z ed                     ej                    d                             d          dd                   pd          Ze?j                                        Z ee?d          re?j                                        rdndZe?j                                         Z! ee?e?j                                        d          Z" e	            Z#e?j                                        Z$ edd          Z% edd          Z& edd          Z' edd          oe Z( edd          Z) edd          Z*defdZ+dD e+ ej        ddB                    v Z, ej-        e' dæ          Z.dZ/eyezd<   epw                    ddƬǦ          Z0eyezd<   epw                    ddɬǦ          Z1eyezd<   epw                    ddˬǦ          Z2eyezd<   epw                    ddͬǦ          Z3eyezd<   epw                    ddϬǦ          Z4eyezd<    ej        ddѦ          dk    Z5epw                    ddԬǦ          Z6eyezd<   epw                    dd֬Ǧ          Z7eyezd<   epw                    ddجǦ          Z8eyezd<   epw                    ddڬǦ          Z9eyezd<   e!oje9 ofe?j:        j         pXe?j:        j;        oJ ed                    e?j:        j;                            d          dd                             dk    Z<e!oBe?j:        j         o4 ee?j:        j                             d          d                   dk    Z= eddݦ          oBe?j:        j         o4 ee?j:        j                             d          d                   dk    Z>e>rdބ Z?ndZ?d߄ Z@epw                    dd ej)        e@d                    ZAeyezd<   e!rdej|        v rw e ej        dd                    ZBe?j         C                                d|         dz  ZDe?j         E                     eFeDeBdz  z
  eDz  eBz  d                      ejG        e?j                                         d          ZHd ZI G d de?jJ        jK                  ZLepw                    ddǦ          ZMeyezd<   epw                    ddǦ          ZNeyezd<   epw                    ddd           ZOeyezd<   epw                    ddǦ          ZPeyezd<   eOrtddlQZ?de?jR        jS        _T        de?jR        jS        _U        de?jR        jS        _V        eMrddlWZ?de?jX        jS        _Y        neP e?jR        jS        _Z        d Z[d Z\d Z]d Z^d Z_ddZ`deMfd ZadܐdZbdݐdZcdؐdZddސdZedߐdZfdd	Zg	 dؐd
e4e3de2f                  dMe3de2f         fdZh ej        d          dk    Zieirde?jj        jk        jl        _m        epw                    ddǦ          Zneyezd<   e=jo        e?jy        e=jp        e?jp        e=jq        e?jq        e=jr        e?jr        e=js        e?js        e=jt        e?jt        e=ju        e?ju        e=jv        e?jv        e=jw        e?jw        e=jx        e?jx        e=jy        e?jy        e=jz        e?jz        e=j{        e?j{        e=j|        e?j|        iZ}d Z~d Zere?j        e}e=j        <   d e}                                D             Ze                    e?j        e=jy        e?j        e=j{        i           de?jR        jS        j        fdZdؐdddZdeedf         fdZd Zdeedf         fdZd Zd ZdؐdddZd  Zd! Zd" Zdؐd#Zd$ Zdؐd%dd&Zd' Zd( Zd) Z G d* d+          Z G d, d-          Z G d. d/          Z G d0 d1          Zd2 Zdؐd3Z G d4 d5e          Zd6 Zd7 Zd8 Zd9 Zd: Zd; Zd< Zd= Zd> Zd? Zd@ ZdA ZdB ZdؐdCZdD ZdE Zej#        dF             Zej#        dG             ZdH ZdI ZddJZ G dK dL          Z G dM dN          Ze#dO             Ze#dP             Z	 ddlZdQ Zej                            dR edej        j        gddSej        j        T                     ej                            dU eej        j        gddVej        j        W                     ej                            dX eej        j        gddYej        j        W                     ej                            exrdRn ej        dZdU                     n# em$ r  ej        d[e¦           Y nw xY wd\edMefd]ZÐd^ej        fd_Z G d` dae[          Z G db dce]          Z G dd dee_          Z G df dge_          Z G dh die^          Z G dj dkeʦ          Z G dl dmeʦ          Z G dn doeʦ          Z G dp dqeʦ          Z G dr dsej        j                  Ze#dteyfdu            Z G dv dw          Z G dx dye;j                  Z G dz d{eĦ          ZԐdݐd|ZՐd} Z֐d~ZאdZdefdZِddZe?j        dDfdZܐddddZݐd Zސd Zߐd Zd Zd Ze?j        dDddZd Zd Zd ZdddZd Zd ZdedededMe?jE        fdZddZddZd Zd Zdad Zd Zd Z G d de
j                  ZdZepw                    ddǦ          Zeyezd<    ej-        ed          Zd ZdؐdZd Ze#dedMe8d         fd            Ze?j        de?j        de?j        de?j        diZd Zd Zej#        d             Z dedMe.fdZd Zd Zd Zdee         de?j        de?j        fdZd Zd Z	d Z
d Zej        dMefd            Z e5d          Zdej        de7e         dMefdÄZdĄ Ze#dń             ZdƄ ZdǄ Z G dȄ dɐeĦ          Zdʄ Z G d˄ d̐eĦ          Ze G d̈́ dΦ                      ZdddddϜdЄZe#d|e?jE        fdф            Zd҄ Zdӄ ZdԄ ZdՄ Zdք ZdS (  a=  Importing this file must **not** initialize CUDA context. test_distributed
relies on this assumption to properly run. This means that when this is imported
no CUDA calls shall be made, including torch.cuda.device_count(), etc.

torch.testing._internal.common_cuda.py can freely initialize CUDA context when imported.
    N)MappingSequence)closingcontextmanager)deepcopy)	dataclass)Enum)partialwraps)productchain)Path)mean)AnyCallableOptionalTypeVarUnion)IterableIterator)	MagicMock)Tensor)
ScriptDict
ScriptList)_as_posix_path)get_writable_path)open_source_signpost)
ModuleDict
ModuleListParameterDictParameterList
Sequentialregister_custom_op_symbolicunregister_custom_op_symbolic)make_tensor)BooleanPairNonePair
NumberPairPairTensorLikePair)not_close_error_metas)get_all_dtypes)_check_module_exists)cpp_extensionTF)gfx942c                  8    t          j        j        j        | i |S N)torchtesting_utilsfreeze_rng_stateargskwargss     x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/testing/_internal/common_utils.pyr6   r6   l   s    =0$A&AAA    c                       e Zd ZU i Zeed<   edddd d fd            Zedddd fd	            Zed
e	fd            Z
dS )TestEnvironmentrepro_env_varsNFTc                     |r| dk    n| dk    S )N01 )env_var_valdefaults     r:   <lambda>zTestEnvironment.<lambda>   s    $+E[C+2D r;   c                      dS NFrB   rB   r;   r:   rE   zTestEnvironment.<lambda>   s    e r;   c                    |}d }| t          j        |          } |||          } |            }|p|}|r|||k    r|s|t          j        |<   | t	                      vsJ d|  d            |t	                      | <   |S )Nzduplicate definition of flag ''osgetenvr=   r>   globals)	nameenv_varrD   include_in_repro
enabled_fnimplied_by_fnenabledrC   implieds	            r:   def_flagzTestEnvironment.def_flag   s     )G,,K jg66G-//$W 	B!47g;M;MW^;M6AO*73 799$$$&Nt&N&N&N$$$!		$r;   c                     | S r2   rB   )maybe_val_strs    r:   rE   zTestEnvironment.<lambda>   s    } r;   c                     ||nt          j        |          } ||          }|r||k    r|t          j        |<   | t	                      vsJ d|  d            |t	                      | <   |S )Nz!duplicate definition of setting 'rI   rJ   )rN   rO   rD   rP   parse_fnvalues         r:   def_settingzTestEnvironment.def_setting   s     #?	'0B0B 	<'!1!16;O*73 799$$$&Q$&Q&Q&Q$$$		$r;   returnc                  x    d                     d t          j                                        D                       S )N c                 "    g | ]\  }}| d | S )=rB   ).0rO   rZ   s      r:   
<listcomp>z8TestEnvironment.repro_env_var_prefix.<locals>.<listcomp>   s@     W W W+We $--e-- W W Wr;   )joinr=   r>   itemsrB   r;   r:   repro_env_var_prefixz$TestEnvironment.repro_env_var_prefix   sK    xx W W/>/M/S/S/U/UW W W X X 	Xr;   )__name__
__module____qualname__r>   dict__annotations__staticmethodrU   r[   strre   rB   r;   r:   r=   r=   t   s          ND0  G G#m   \R  44   \* X# X X X \X X Xr;   r=   zfile://win32zfile:///IS_CICIc                      t          |           S r2   )bool)env_var_value_s     r:   rE   rE      s    ](;(; r;   )rO   rP   rQ   IS_SANDCASTLE
SANDCASTLEc                  2    t          j        d          dk    S )NTW_JOB_USER
sandcastle)rK   rL   rB   r;   r:   rE   rE      s    ")M22lB r;   )rO   rR   rP   INSIDE_RE_WORKERIN_RE_WORKERIS_FBSOURCE	IS_FBCODEPYTORCH_TEST_FBCODE)rO   rD   rP   IS_REMOTE_GPUPYTORCH_TEST_REMOTE_GPU)rO   rP   DISABLE_RUNNING_SCRIPT_CHK"PYTORCH_DISABLE_RUNNING_SCRIPT_CHKPRINT_REPRO_ON_FAILUREPYTORCH_PRINT_REPRO_ON_FAILUREOPINFO_SAMPLE_INPUT_INDEX!PYTORCH_OPINFO_SAMPLE_INPUT_INDEXc                 (    | d nt          |           S r2   int)vals    r:   rE   rE     s    #c(( r;   )rO   rD   rP   rY   z.pytorch-disabled-tests.jsonzslow_tests.jsonc                     t           j                            |           r;t          |           5 }t	          j        |          cd d d            S # 1 swxY w Y   t                              d|            i S )Nz7Attempted to load json file '%s' but it does not exist.)rK   pathisfileopenjsonloadlogwarning)filenamefps     r:   maybe_load_jsonr     s    	w~~h !(^^ 	!r9R==	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!KKI8TTTIs   AAASLOW_TESTS_FILE DISABLED_TESTS_FILEcpucudaxpumetamps)r   r   r   )orinconcordgalenxaviernanojetsontegrathorc              #   @   K   | ]}|t          j                     v V  d S r2   )platform)ra   rN   s     r:   	<genexpr>r   4  s0      DD)+++DDDDDDr;   c                 F     t          j                    fd            }|S )Nc                      t           r1t          j                     t          j                                          | i | d S r2   )	IS_JETSONgccollectr3   r   empty_cacher8   r9   fns     r:   wrapperzgcIfJetson.<locals>.wrapper8  sE     	%JLLLJ""$$$
DFr;   )	functoolsr   r   r   s   ` r:   
gcIfJetsonr   6  s8    _R    
 Nr;   r\   c                     	 t          j                    } | D ]}|j        }d|j        vr|j        d         }t	          |t
          j                  rt|                                }|                    dd          ^ }}}|t          |          j
        k    r.|                    d          rt          ||          j        }|c S n# t          $ r Y nw xY wd S )Nself.   test)inspectstackframef_locals
isinstanceunittestTestCaseidrsplittyperf   
startswithgetattr__func__	Exception)	r   
frame_infor   self_valtest_idrs   cls_name	test_nametest_fns	            r:   extract_test_fnr   B  s     
	# 
	#J$EU^++~f-H(H$566 #"++--*1..a*@*@'HitH~~6669;O;OPV;W;W6%h	::CG"NNN
	#    4s   CC C 
CCc                   .    e Zd ZU eed<   eed<   eed<   dS )TrackedInputindexr   	type_descN)rf   rg   rh   r   rj   r   rl   rB   r;   r:   r   r   U  s+         JJJ	HHHNNNNNr;   r   c                  H    t                      } | d S t          | dd           S Ntracked_input)r   r   r   s    r:   get_tracked_inputr   ]  s)    Gt7OT222r;   c                  \    t                      } | d S t          | d          sd S d | _        d S r   )r   hasattrr   r   s    r:   clear_tracked_inputr   c  s<    G7O,,  Gr;   c                   6    e Zd Z	 	 	 	 ddZd Zd ZdefdZdS )	TrackedInputIterNTc                     t          |          | _        || _        || _        | j        d | _        || _        | j        d | _        t                      | _        || _        || _        d S )Nc                     | S r2   rB   )xis     r:   rE   z+TrackedInputIter.__init__.<locals>.<lambda>  s    a r;   c                     | S r2   rB   r   s    r:   rE   z+TrackedInputIter.__init__.<locals>.<lambda>  s    A r;   )		enumerate
child_iterinput_type_descitem_callbacktrack_callbackr   r   set_seedrestrict_to_index)r   r   r   r   r   r   r   s          r:   __init__zTrackedInputIter.__init__n  sw     $J//. +%!/D,&"-+D&((  "3r;   c                     | S r2   rB   r   s    r:   __iter__zTrackedInputIter.__iter__      r;   c                    	 | j         r| j        dt                              t	          j        | j        j                            d                                                    d d         d          nt          }t          |           t          | j                  \  }}| j        || j        k    rn|                     t          ||                     |          | j                             |                     ||          S )NTutf-8   little)r   r   r   )r   r   r   
from_byteshashlibsha256rh   encodedigestSEEDset_rng_seednextr   r   _set_tracked_inputr   r   r   r   )r   seed	input_idx	input_vals       r:   __next__zTrackedInputIter.__next__  s   	} #
 |/ NN7>188AA$C $CCI688BQB$PQY[ [ [59 
 T""" $(#8#8 Iy&.IAW4W4W	 	T%8%8%C%CtOc  	
 	
 	

 !!)Y777r;   r   c                 ^    | j         d S t          | j         d          sd S || j         _        d S r   )r   r   r   )r   r   s     r:   r   z#TrackedInputIter._set_tracked_input  s8    <Ft|_55 	F%2"""r;   )NNTN)rf   rg   rh   r   r   r   r   r   rB   r;   r:   r   r   m  so        
 3 3 3 3>  8 8 8.3 3 3 3 3 3 3r;   r   c                       e Zd ZdZd Zd ZdS )_TestParametrizera  
    Decorator class for parametrizing a test function, yielding a set of new tests spawned
    from the original generic test, each specialized for a specific set of test inputs. For
    example, parametrizing a test across the set of ops will result in a test function per op.

    The decision of how to parametrize / what to parametrize over is intended to be implemented
    by each derived class.

    In the details, the decorator adds a 'parametrize_fn' property to the test function. This function
    is intended to be called later by one of:
      * Device-specific test instantiation via instantiate_device_type_tests(). Note that for this
        case there is no need to explicitly parametrize over device type, as that is handled separately.
      * Device-agnostic parametrized test instantiation via instantiate_parametrized_tests().

    If the decorator is applied to a test function that already has a 'parametrize_fn' property, a new
    composite 'parametrize_fn' will be created that generates tests with the product of the parameters
    generated by the old and new parametrize_fns. This allows for convenient composability of decorators.
    c                     t           )a  
        Parametrizes the given test function across whatever dimension is specified by the derived class.
        Tests can be parametrized over any arbitrary dimension or combination of dimensions, such as all
        ops, all modules, or all ops + their associated dtypes.

        Args:
            test (fn): Test function to parametrize over
            generic_cls (class): Generic test class object containing tests (e.g. TestFoo)
            device_cls (class): Device-specialized test class object (e.g. TestFooCPU); set to None
                if the tests are not part of a device-specific set

        Returns:
            Generator object returning 4-tuples of:
                test (fn): Parametrized test function; must support a device arg and args for any params
                test_name (str): Parametrized suffix for the test (e.g. opname_int64); will be appended to
                    the base name of the test
                param_kwargs (dict): Param kwargs to pass to the test (e.g. {'op': 'add', 'dtype': torch.int64})
                decorator_fn (callable): Callable[[Dict], List] for list of decorators to apply given param_kwargs
        )NotImplementedError)r   r   generic_cls
device_clss       r:   _parametrize_testz#_TestParametrizer._parametrize_test  s
    ( "!r;   c                     t          |d          r$|j        }| j        }t          ||          |_        n| j        |_        |S )Nparametrize_fn)r   r  r  compose_parametrize_fns)r   r   old_parametrize_fnnew_parametrize_fns       r:   __call__z_TestParametrizer.__call__  sN    2'(( 	7!#!2!%!7 78JL^ _ _B $ 6B	r;   N)rf   rg   rh   __doc__r  r  rB   r;   r:   r  r    s<         $" " ",    r;   r  c                     | |fd}|S )a  
    Returns a parametrize_fn that parametrizes over the product of the parameters handled
    by the given parametrize_fns. Each given parametrize_fn should each have the signature
    f(test, generic_cls, device_cls).

    The test names will be a combination of the names produced by the parametrize_fns in
    "<new_name>_<old_name>" order. This order is done to match intuition for constructed names
    when composing multiple decorators; the names will be built in top to bottom order when stacking
    parametrization decorators.

    Args:
        old_parametrize_fn (callable) - First parametrize_fn to compose.
        new_parametrize_fn (callable) - Second parametrize_fn to compose.
    c              3     K   t           || ||                    }|D ]\  }}}}	 ||||          D ]\  }
}}}t          |                                                              |                                          }|rt	          d| j         d|           i ||}d                    ||dk    r|dk    rdnd|          }|	|fd}|
|||fV  d S )NzkParametrization over the same parameter by multiple parametrization decorators is not supported. For test "z8", the following parameters are handled multiple times: z{}{}{}r   rs   c                 d    t           ||                     t           ||                     z   S r2   )list)param_kwargs
old_dec_fn
new_dec_fns      r:   merged_decorator_fnzJcompose_parametrize_fns.<locals>.composite_fn.<locals>.merged_decorator_fn  s1    

< 8 899DLAYAY<Z<ZZZr;   )r  setkeysintersectionRuntimeErrorrf   format)r   r  r  r
  r  	old_testsold_testold_test_nameold_param_kwargsr  new_testnew_test_namenew_param_kwargsr  redundant_paramsfull_param_kwargsmerged_test_namer  s                     r:   composite_fnz-compose_parametrize_fns.<locals>.composite_fn  s      ++D+zJJKK	GP 	[ 	[CX}&6
&&xjII[ [G=*:J#&'7'<'<'>'>#?#?#L#LM]MbMbMdMd#e#e # Z& (YQUQ^(Y (YFV(Y (Y Z Z Z %M'7$L;K$L!#+??=:G2:M:MR_ceReRe33km3@$B $B  BLXb [ [ [ [  !13DFYZZZZZ[	[ 	[r;   rB   )r
  r  r%  s      r:   r	  r	    s*    " );(:[ [ [ [, r;   c                 Z    t          t                               D ]}t           |          }t          |d          s#t	           |            fd}|                    | d          D ];\  }}}}|j         d| } ||          D ]}	 |	|          } | |||           < S )ai  
    Instantiates tests that have been decorated with a parametrize_fn. This is generally performed by a
    decorator subclass of _TestParametrizer. The generic test will be replaced on the test class by
    parametrized tests with specialized names. This should be used instead of
    instantiate_device_type_tests() if the test class contains device-agnostic tests.

    You can also use it as a class decorator. E.g.

    ```
    @instantiate_parametrized_tests
    class TestFoo(TestCase):
        ...
    ```

    Args:
        generic_cls (class): Generic test class object containing tests (e.g. TestFoo)
    r  c                     t                    |ffd	            }t          |          rJ d|             t          ||           d S )Nc                      | fi | d S r2   rB   )r   r  r   s     r:   instantiated_testzZinstantiate_parametrized_tests.<locals>.instantiate_test_helper.<locals>.instantiated_test'  s!    T**\*****r;   zRedefinition of test )r   r   setattr)clsrN   r   r  r)  r  s     `  r:   instantiate_test_helperz?instantiate_parametrized_tests.<locals>.instantiate_test_helper&  sw    4[[5A + + + + + [+ {D11QQ3Q43Q3QQQ1K'899999r;   N)r  r  rs   )r+  rN   r   r  )tupledirr   r   delattrr  rf   )
r  	attr_name
class_attrr,  r   test_suffixr  decorator_fn	full_name	decorators
   `         r:   instantiate_parametrized_testsr6    s!   $ 3{++,, k k	[)44
z#344 	 	Y'''	: 	: 	: 	: 	: @J?X?X @Y @F @F 	k 	k;T;l=88;88I *\,77 ' '	 y##)$]ijjjjj	k r;   c                   "    e Zd ZdZg dZddZdS )subtesta  
    Explicit subtest case for use with test parametrization.
    Allows for explicit naming of individual subtest cases as well as applying
    decorators to the parametrized test.

    Args:
        arg_values (iterable): Iterable of arg values (e.g. range(10)) or
            tuples of arg values (e.g. [(1, 2), (3, 4)]).
        name (str): Optional name to use for the test.
        decorators (iterable): Iterable of decorators to apply to the generated test.
    
arg_valuesrN   
decoratorsNc                 8    || _         || _        |r|ng | _        d S r2   r9  )r   r:  rN   r;  s       r:   r   zsubtest.__init__H  s$    $	(2:**r;   NN)rf   rg   rh   r  	__slots__r   rB   r;   r:   r8  r8  :  s?        
 
 544I; ; ; ; ; ;r;   r8  c                   4    e Zd ZdZddZd Zd ZddZd ZdS )	parametrizeax
  
    Decorator for applying generic test parametrizations.

    The interface for this decorator is modeled after `@pytest.mark.parametrize`.
    Basic usage between this decorator and pytest's is identical. The first argument
    should be a string containing comma-separated names of parameters for the test, and
    the second argument should be an iterable returning values or tuples of values for
    the case of multiple parameters.

    Beyond this basic usage, the decorator provides some additional functionality that
    pytest does not.

    1. Parametrized tests end up as generated test functions on unittest test classes.
    Since this differs from how pytest works, this decorator takes on the additional
    responsibility of naming these test functions. The default test names consists of
    the test's base name followed by each parameter name + value (e.g. "test_bar_x_1_y_foo"),
    but custom names can be defined using `name_fn` or the `subtest` structure (see below).

    2. The decorator specially handles parameter values of type `subtest`, which allows for
    more fine-grained control over both test naming and test execution. In particular, it can
    be used to tag subtests with explicit test names or apply arbitrary decorators (see examples
    below).

    Examples::

        @parametrize("x", range(5))
        def test_foo(self, x):
            ...

        @parametrize("x,y", [(1, 'foo'), (2, 'bar'), (3, 'baz')])
        def test_bar(self, x, y):
            ...

        @parametrize("x,y", [(1, 'foo'), (2, 'bar'), (3, 'baz')],
                     name_fn=lambda x, y: '{}_{}'.format(x, y))
        def test_bar_custom_names(self, x, y):
            ...

        @parametrize("x, y", [subtest((1, 2), name='double'),
                              subtest((1, 3), name='triple', decorators=[unittest.expectedFailure]),
                              subtest((1, 4), name='quadruple')])
        def test_baz(self, x, y):
            ...

    To actually instantiate the parametrized tests, one of instantiate_parametrized_tests() or
    instantiate_device_type_tests() should be called. The former is intended for test classes
    that contain device-agnostic tests, while the latter should be used for test classes that
    contain device-specific tests. Both support arbitrary parametrizations using the decorator.

    Args:
        arg_str (str): String of arg names separate by commas (e.g. "x,y").
        arg_values (iterable): Iterable of arg values (e.g. range(10)) or
            tuples of arg values (e.g. [(1, 2), (3, 4)]).
        name_fn (Callable): Optional function that takes in parameters and returns subtest name.
    Nc                 j    d |                     d          D             | _        || _        || _        d S )Nc                 B    g | ]}|d k    |                                 S r   stripra   ss     r:   rb   z(parametrize.__init__.<locals>.<listcomp>  s&    $V$V$V1aSUggQWWYYgggr;   ,)split	arg_namesr:  name_fn)r   arg_strr:  rK  s       r:   r   zparametrize.__init__  s5    $V$Vc8J8J$V$V$V$r;   c                    t          |t          j                  rt          |          S t          |t          j                  rt          |          S t          |          j        dv r|j        S t          |t          t          t
          f          r(| dt          |                              dd           S | | S )zd Returns a string representation for the given arg that is suitable for use in test function names. >   OpInfo
ModuleInfors   r   )r   r3   dtype
dtype_namedevicerl   r   rf   formatted_namer   floatreplace)r   idxrN   rZ   s       r:   _formatted_str_reprzparametrize._formatted_str_repr  s    eU[)) 
	"e$$$u|,, 	"u::%[[!%===''UC011 	";;SZZ//S99;;;>C>>!r;   c                 p     d                      fdt           j        |          D                       S )Nrs   c                 D    g | ]\  }}                     ||          S rB   )rW  )ra   avrV  r   s      r:   rb   z5parametrize._default_subtest_name.<locals>.<listcomp>  s/    eeeA11#q!<<eeer;   )rc   ziprJ  )r   rV  valuess   `` r:   _default_subtest_namez!parametrize._default_subtest_name  s;    xxeeeeeT^]cIdIdeeefffr;   c                 `    |r|}n(| j         r | j         | }n|                     ||          }|S r2   )rK  r^  )r   rV  r]  explicit_namesubtest_names        r:   _get_subtest_namezparametrize._get_subtest_name  sH     	C(LL\ 	C'4<0LL55c6BBLr;   c           
   #     K   t          | j                  dk    rd}|i d fV  d S t                      x}}t          | j                  D ](\  }}d }g }	t          |t                    r4|}
|
j        }|
j        }t                    fd            }|
j	        }	|}n}t          | j                  dk    rt          |          n|g}t          |          t          | j                  k    r=t          dt          |           dt          | j                   dj         d	          t          t          | j        |                    }|                     |||
          }|	fd}||||fV  *||u rt!           d          d S )Nr   r   c                     g S r2   rB   rs   s    r:   rE   z/parametrize._parametrize_test.<locals>.<lambda>  s    " r;   c                       | i |S r2   rB   r8   r9   r   s     r:   test_wrapperz3parametrize._parametrize_test.<locals>.test_wrapper  s    #tT4V444r;      z+Expected # values == # arg names, but got: z values and z names for test "")r`  c                     |S r2   rB   )rs   r;  s     r:   r3  z3parametrize._parametrize_test.<locals>.decorator_fn  s    %%r;   zf: An empty arg_values was passed to @parametrize. Note that this may result from reuse of a generator.)lenrJ  objectr   r:  r   r8  rN   r   r;  r  r  rf   ri   r\  rb  
ValueError)r   r   r  r  r   r]  check_exhausted_iteratorrV  
maybe_namer;  subrh  gen_testr  r3  s    `             r:   r  zparametrize._parametrize_test  s"     t~!##IB555555
 178F-(99 H HV!
(*
fg.. $ C ^F!$J4[[5 5 5 5 ![5 "%J+HH#H),T^)<)<q)@)@fvhv;;#dn"5"555& (lUXY_U`U` (l (l585H5H(l (l[_[h(l (l (l m m m  $C$?$?@@ 223j2YY	/9 & & & &  L,GGGGG111 D "X "X "X Y Y Y 21r;   r2   )	rf   rg   rh   r  r   rW  r^  rb  r  rB   r;   r:   r@  r@  N  s|        6 6n   
" " "g g g   ,Y ,Y ,Y ,Y ,Yr;   r@  c                       e Zd ZdZd Zd ZdS )reparametrizea  
    Decorator for adjusting the way an existing parametrizer operates. This class runs
    the given adapter_fn on each parametrization produced by the given parametrizer,
    allowing for on-the-fly parametrization more flexible than the default,
    product-based composition that occurs when stacking parametrization decorators.

    If the adapter_fn returns None for a given test parametrization, that parametrization
    will be excluded. Otherwise, it's expected that the adapter_fn returns an iterable of
    modified parametrizations, with tweaked test names and parameter kwargs.

    Examples::

        def include_is_even_arg(test_name, param_kwargs):
            x = param_kwargs["x"]
            is_even = x % 2 == 0
            new_param_kwargs = dict(param_kwargs)
            new_param_kwargs["is_even"] = is_even
            is_even_suffix = "_even" if is_even else "_odd"
            new_test_name = f"{test_name}{is_even_suffix}"
            yield (new_test_name, new_param_kwargs)

        ...

        @reparametrize(parametrize("x", range(5)), include_is_even_arg)
        def test_foo(self, x, is_even):
            ...

        def exclude_odds(test_name, param_kwargs):
            x = param_kwargs["x"]
            is_even = x % 2 == 0
            yield None if not is_even else (test_name, param_kwargs)

        ...

        @reparametrize(parametrize("x", range(5)), exclude_odds)
        def test_bar(self, x):
            ...

    c                 "    || _         || _        d S r2   )parametrizer
adapter_fn)r   rv  rw  s      r:   r   zreparametrize.__init__  s    ($r;   c              #      K   | j                             |||          D ]3\  }}}}|                     ||          }||D ]}	|	|	\  }
}||
||fV  4d S r2   )rv  r  rw  )r   r   r  r  rr  r   r  r3  adaptedadapted_itemr   r!  s               r:   r  zreparametrize._parametrize_test   s      !33D+zRR	X 	X=Xy,ooi>>G"$+ X XL#/:F7'7'8H,WWWW	X 	Xr;   Nrf   rg   rh   r  r   r  rB   r;   r:   rt  rt    sB        & &N% % %X X X X Xr;   rt  c                       e Zd ZdZd Zd ZdS )
decorateIfa[  
    Decorator for applying parameter-specific conditional decoration.
    Composes with other test parametrizers (e.g. @modules, @ops, @parametrize, etc.).

    Examples::

        @decorateIf(unittest.skip, lambda params: params["x"] == 2)
        @parametrize("x", range(5))
        def test_foo(self, x):
            ...

        @parametrize("x,y", [(1, 'foo'), (2, 'bar'), (3, 'baz')])
        @decorateIf(
            unittest.expectedFailure,
            lambda params: params["x"] == 3 and params["y"] == "baz"
        )
        def test_bar(self, x, y):
            ...

        @decorateIf(
            unittest.expectedFailure,
            lambda params: params["op"].name == "add" and params["dtype"] == torch.float16
        )
        @ops(op_db)
        def test_op_foo(self, device, dtype, op):
            ...

        @decorateIf(
            unittest.skip,
            lambda params: params["module_info"].module_cls is torch.nn.Linear and                 params["device"] == "cpu"
        )
        @modules(module_db)
        def test_module_foo(self, device, dtype, module_info):
            ...

    Args:
        decorator: Test decorator to apply if the predicate is satisfied.
        predicate_fn (Callable): Function taking in a dict of params and returning a boolean
            indicating whether the decorator should be applied or not.
    c                 "    || _         || _        d S r2   )r5  predicate_fn)r   r5  r  s      r:   r   zdecorateIf.__init__5  s    "(r;   c              #   t   K   | j         | j        fd}t                    fd            }d}||i |fV  d S )Nc                 "     ||           r|gS g S r2   rB   )paramsr5  r  s      r:   r3  z2decorateIf._parametrize_test.<locals>.decorator_fn<  s!    |F## !{"	r;   c                       | i |S r2   rB   rg  s     r:   rh  z2decorateIf._parametrize_test.<locals>.test_wrapperB  s    4((((r;   r   )r5  r  r   )r   r   r  r  r3  rh  r   s    `     r:   r  zdecorateIf._parametrize_test9  st       ,0>HY 	 	 	 	 
t	) 	) 	) 	) 
	) 	YL999999r;   Nr{  rB   r;   r:   r}  r}    s=        ( (R) ) ): : : : :r;   r}  c                       e Zd ZdZdZdZdS )ProfilingModeri  r      N)rf   rg   rh   LEGACYSIMPLE	PROFILINGrB   r;   r:   r  r  J  s        FFIIIr;   r  c                  J   t           j                            d          } t           j                            d          }t           j                            |            t           j                            |           | r|rt          j        S t          j        S t          j        S NT)r3   _C_jit_set_profiling_executor_get_graph_executor_optimizer  r  r  r  old_prof_exec_stateold_prof_mode_states     r:    cppProfilingFlagsToProfilingModer  O  s    (>>tDD(??EE	H(()<===	H))*=>>> $ 	( ** ''##r;   c               #     K   d} d}t           t          j        k    r>t          j                            d          } t          j                            d          }	 d V  t           t          j        k    r@t          j                            |            t          j                            |           d S d S # t           t          j        k    r?t          j                            |            t          j                            |           w w xY w)NFT)GRAPH_EXECUTORr  r  r3   r  r  r  r  s     r:   )enable_profiling_mode_for_profiling_testsr  ]  s      000#hBB4HH#hCCDIIG]444H001DEEEH112EFFFFF 54>]444H001DEEEH112EFFFF 5s   B6 6ADc               #     K   t           j                            d          } t           j                            d          }	 d V  t           j                            |            t           j                            |           d S # t           j                            |            t           j                            |           w xY wr  )r3   r  r  r  r  s     r:   enable_profiling_moder  k  s      (>>tDD(??EEC,,-@AAA--.ABBBBB 	,,-@AAA--.ABBBBs   B A Cc              #      K   t           j                            |           }	 d V  t           j                            |           d S # t           j                            |           w xY wr2   )r3   r  _jit_set_num_profiled_runs)num_runsold_num_runss     r:   num_profiled_runsr  u  sa      866x@@L:++L99999++L9999s   A !A)c                     d|v rN|d= t           t          j        k    r6t                      5   | |i |  | |i |cd d d            S # 1 swxY w Y    | |i |S )Nprofile_and_replay)r  r  r  r  )callabler8   r9   s      r:   prof_callabler    s    v%%'(]444:<< 1 1$)&)))x0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8T$V$$$s   AAAc                 (    t          d|  d          )Nz>This test file is not meant to be run directly, use:

	python z TESTNAME

instead.)r  )file_to_calls    r:   raise_on_run_directlyr    s+    
 "+7" " " # # #r;   c                  ,    t          t          g| R i |S r2   )r  	func_callr7   s     r:   prof_func_callr         4T444V444r;   c                  ,    t          t          g| R i |S r2   )r  	meth_callr7   s     r:   prof_meth_callr    r  r;   c                      t           j                            d          } | | nd}t           j                            d|          S )NTEST_REPORT_SOURCE_OVERRIDEpython-unittestztest-reports)rK   environgetr   rc   )overridetest_sources     r:   _get_test_report_pathr    s=     z~~;<<H&2((8IK7<<444r;   zrun_test.py__file__)add_helpallow_abbrevz--subprocess
store_truez(whether to run each test in a subprocess)actionhelpz--seedi  )r   rD   z--accept)r  z--jit-executorz--jit_executor)r   z--repeatri  z--test-bailoutsz--test_bailouts--use-pytest
--save-xml?)nargsr   constrD   z--discover-testsz--log-suffixz--run-parallel--import-slow-tests)r   r  r  --import-disabled-tests--rerun-disabled-tests--pytest-single-test)r   r  --showlocals)r  rD   c                 0    t          j        |            d S )Nargv)r   mainr  s    r:   run_unittest_helpr    s    Mtr;   z-hz--help)targetr8   legacy	profilingsimpleACCEPT	functorchc                 p   	 |                      |          |                                   S # t          $ rF |                      d          }||cY |                                   S |                                   t          j        $ r~ |                     t          j                   d }	 |                      d          }n# t          j        $ r Y nw xY w||cY |                                   S |                                    |                                   xY w# |                                   w xY w)Ntimeout   )waitKeyboardInterruptkill
subprocessTimeoutExpiredsend_signalsignalSIGINT)pr  exit_statuss      r:   wait_for_processr    sQ   vvgv&&< 	
;     ffQf''"0 	
- FFHHH$   	fm$$$	&&&++KK ( 	 	 	D	" 	
 FFHHH	 	
sR   , $DD &AD+CDCDCDD 1+DD D5c                    t           j                                         t           j                                         t	          | t
                    r
J d            t          j        | d||||          }t          ||          S )Nz4Command to shell should be a list or tuple of tokensT)universal_newlinescwdenvstdoutstderrr  )	sysr  flushr  r   rl   r  Popenr  )commandr  r  r  r  r  r  s          r:   shellr    s}    JJ '3''__)___'TsTZcijjjAAw////r;   c           
         |dk    sJ d|             	 t          | |||||          }|dk    s|dk    r||fS t          d| d| d|d           nV# t          j        $ rD |dk    rt          d	|d
z   d|d           d|fcY S t          d	|d
z   d| d|d           Y nw xY wt	          | ||||||dz
  d          S )Nr   z9Expecting non negative number for number of retries, got )r  r  r  r  r  zGot exit code z, retrying (retries left=)T)filer  zCommand took ><   zmin, returning 124|   zmin, retrying (retries left=ri  )r  r  r  r  r  retries	was_rerun)r  printr  r  retry_shell)	r  r  r  r  r  r  r  r  	exit_codes	            r:   r  r    s    	1L7LL 	
#fVW
 
 
	 >>W\\i''KYKKKKK	
 	
 	
 	
 	

 $ 
 
 
a<<BBBBB   
 	>!!!RW]RRRRR	
 	
 	
 	
 	
 	

 !	 	 	 	s   $A A 2B'B'&B'c                     t          | t          j                  r| gS g }| D ]3}t          |           |                    t          |                     4|S r2   )r   r   r   r  extenddiscover_test_cases_recursively)suite_or_casercelements      r:   r  r  F  sa    -!233 	B  < <g
		1'::;;;;Ir;   c                     d | D             S )Nc                     g | ]D}d                      |                                                    d           dd                   ES )r   N)rc   r   rI  ra   cases     r:   rb   z"get_test_names.<locals>.<listcomp>P  sA    GGGDCHHTWWYY__S))"##.//GGGr;   rB   )
test_casess    r:   get_test_namesr  O  s    GGJGGGGr;   c                      t          j                                        t                    } t	          |           }t          |          D ]}t          |           d S r2   )r   
TestLoaderloadTestsFromModule__main__r  r  r  )suiter  rN   s      r:   _print_test_namesr  R  sY    !!55h??E077Jz**  d r;   c                 >      fdt                    D             S )Nc                 &    g | ]}|d          S r2   rB   )ra   r   lstnchunkss     r:   rb   zchunk_list.<locals>.<listcomp>Y  s#    444C
7
O444r;   range)r  r  s   ``r:   
chunk_listr  X  s'    44444U7^^4444r;   c                     |                      t                    r| t          t                    dz   d          } t          j        dd|           }t          j        dd|          S )Nri  z.py$r   /r   )r   CI_TEST_PREFIXrl  rerq  )r   strip_pys     r:   sanitize_test_filenamer  \  s\    >** 6C//!3445vgr8,,H6#tX&&&r;   c                    d}| D ]}|}t          |t          j                  rXt          |j                  dk    r|j        d         nd }|-t          |t          j                  r|ot          |          c S |}|tt          |t                    s_|                                                    dd          d                             d          d         }d}t          | d|            d}|S )NTr   r   ri  z`This test class should extend from torch.testing._internal.common_utils.TestCase but it doesn't.z - failed. F)
r   r   	TestSuiterl  _testslint_test_case_extensionr   r   rI  r  )r  succeedtest_case_or_suite	test_case
first_test
test_classerrs          r:   r  r  c  s   G#    &	((*<== 	#9<=O=V9W9WZ[9[9[+2155aeJ%*ZAS*T*T%O#;<N#O#OOOO"I i22  &\\^^11#q99!<BB3GGJ
x5555666Nr;   c                    t          | d                   }t          t          z   }t          j                            ||          }|rx|                    dd          }t          j        |d           t          j                            || dt          j        d          	                                 d          }|S t          j        |d           |S )	Nr   r  zpython-pytestT)exist_ok-   z.xml)
r  TEST_SAVE_XML
LOG_SUFFIXrK   r   rc   rU  makedirsurandomhex)r  pytesttest_filenametest_report_paths       r:   get_report_pathr(  v  s    *4733M$z1w||$4mDD  +334EWW
$t44447<<(8]:f:fRZXY]]M^M^M`M`:f:f:fggK 40000r;   xml_filec                    dd l mc m} |                    |           }|                    d          D ]}|j                            d          }|t          j        d|          }|7|	                    d          }|	                    d          
                    dd          }|                    d|           |                    d| d           |                    |            d S )	Nr   testcase	classnamez.^(test\.)?(?P<file>.*)\.(?P<classname>[^\.]*)$r  r   r  .py)xml.etree.ElementTreeetreeElementTreeparseiterattribr  r  searchgrouprU  r  write)r)  ETtreer+  full_classnameregex_resultr,  r  s           r:   sanitize_pytest_xmlr;    s    '&&&&&&&&88HDIIj)) + +!,,[99!y!RTbcc &&{33	!!&))11#s;;[),,,V\\\****JJxr;   r  c                      G d d          } |            }dd l } |j        d | D             g dz   |g           |j        S )Nc                       e Zd ZddZd ZdS )2get_pytest_test_cases.<locals>.TestCollectorPluginr\   Nc                     g | _         d S r2   )testsr   s    r:   r   z;get_pytest_test_cases.<locals>.TestCollectorPlugin.__init__  s    $&DJJJr;   c                     |j         D ]9}| j                            |j                            |j                             :d S r2   )rd   r@  appendconfigcwd_relative_nodeidnodeid)r   sessionitems      r:   pytest_collection_finishzKget_pytest_test_cases.<locals>.TestCollectorPlugin.pytest_collection_finish  sL     S S
!!'."D"DT["Q"QRRRRS Sr;   r\   N)rf   rg   rh   r   rH  rB   r;   r:   TestCollectorPluginr>    s<        	' 	' 	' 	'	S 	S 	S 	S 	Sr;   rJ  r   c                     g | ]
}|d k    |S )z-vvrB   ra   args     r:   rb   z)get_pytest_test_cases.<locals>.<listcomp>  s    ---ur;   )z--collect-onlyz-qq--use-main-module)plugins)r%  r  r@  )r  rJ  test_collector_pluginr%  s       r:   get_pytest_test_casesrQ    s    S S S S S S S S 0/11MMMFK-----0^0^0^^&'    !&&r;   c                    t           rt          j                            t                     rUt	          t                     5 }t          j        |          at           t          j        d<   d d d            n# 1 swxY w Y   nt          j
        dt                       t          rt          j                            t                    rUt	          t                    5 }t          j        |          at          t          j        d<   d d d            n# 1 swxY w Y   nt          j
        dt                      t          rt                       d S t          j                                        t$                    }t'          |          st)          j        d           t,          r!| d         gt.          rg dndg| dd          } t0          rg }t          r|                    d	           t           r|                    d
           t.          r|                    d           t4          r|                    d           t6          r|dt6          gz  }t.          rt9          |           nd t;          |          D             }g }|D ]L}t(          j        g| d         gz   |z   | dd          z   t.          rdgng z   |gz   }d                    |          }t4          rd nd}	tA          ||	t4          rdnd          \  }
}|
dk    r~d|v rPt          j        !                    dd          }t          j        !                    dd          }d| d| }|dz   |z   }tE          d|
 d|            |                    |           tG          |          dk    s>J d$                    tG          |          d                    |                                Nd S tJ          dk    rt;          |          }tM          tO          |          tJ                    }g }tQ          tJ                    D ]N}t(          j        g| z   d|dz    gz   ||         z   }|                    tS          j*        |d                     Od }|D ]}|tW          |          dk    z  }|r
J d!            d S t.          r| d"gz   }d}t6          r:tY          d#          }tE          d$|            |                    d%|            tZ          rtZ          |dd          z   }dd l.}d&t          j        d'<    |j/        |(          }t6          rta          |           t)          j        |d)k    rdn|           d S t6          rtdd l1}dd*l2m3}  G d+ d,|          }tY                      }d-| v pd.| v }|rtE          d$|            t          j/        | |4                    ||rd/nd|0          1           d S tj          dk    rZtQ          tj                    D ]C}t          j/        d | 2          j6        7                                st)          j        d3           Dd S t          j/        | 4           d S )5Nr   z'slow test file provided but not found: r   z+disabled test file provided but not found: ri  r   )r  z	--tb=longz--color=yesz--localsr  r  r  r  r  c                 j    g | ]0}|                                                     d d          d         1S )r   ri  )r   rI  r  s     r:   rb   zrun_tests.<locals>.<listcomp>  s3    [[[DTWWYY__S!$$Q'[[[r;   r  r^   i  )r  r  TestDistBackendWithSpawnBACKENDr   
WORLD_SIZEzBACKEND=z WORLD_SIZE=z#Test exited with non-zero exitcode z. Command to reproduce: z{} unit test(s) failed:
	{}z
	z--log-suffix=-shard-T)r  FzSome test shards have failedrN  )r%  zTest results will be stored in z--junit-xml-reruns=rA   NO_COLOR)r8   r  )_XMLTestResultc                   8     e Zd ZdZ fdZ fdZd fdZ xZS )'run_tests.<locals>.XMLTestResultVerboseaZ  
            Adding verbosity to test outputs:
            by default test summary prints 'skip',
            but we want to also print the skip reason.
            GH issue: https://github.com/pytorch/pytorch/issues/69014

            This works with unittest_xml_reporting<=3.2.0,>=2.0.0
            (3.2.0 is latest at the moment)
            c                 :     t                      j        |i | d S r2   )superr   )r   r8   r9   	__class__s      r:   r   z0run_tests.<locals>.XMLTestResultVerbose.__init__)  s%      $1&11111r;   c                     t                                          ||           | j        j        D ]1}t	          |j        t                    r|j        dk    r
d| |_        2d S )Nskipzskip: )r\  addSkipcallback__closure__r   cell_contentsrl   )r   r   reasoncr]  s       r:   r`  z/run_tests.<locals>.XMLTestResultVerbose.addSkip,  sk    f---2 < <A!!/377 <AOv<U<U +<6*;*;	< <r;   r\   Nc                 ~    t                                                       |                     d| j                   d S )NXPASS)r\  printErrorsprintErrorListunexpectedSuccesses)r   r]  s    r:   rh  z3run_tests.<locals>.XMLTestResultVerbose.printErrors4  s8    ##%%%##GT-EFFFFFr;   rI  )rf   rg   rh   r  r   r`  rh  __classcell__r]  s   @r:   XMLTestResultVerboserZ    s         2 2 2 2 2< < < < <G G G G G G G G G Gr;   rm  z	--verbosez-vr   )output	verbosityresultclass)r  
testRunner)exitr  r  )8r   rK   r   existsr   r   r   slow_tests_dictr  warningswarnr   disabled_tests_dictTEST_DISCOVERr  r   r   r  r  r  r  rr  
SHOWLOCALS
USE_PYTESTTEST_IN_SUBPROCESSrB  RERUN_DISABLED_TESTSr   rQ  r  
executablerc   r  r  r  rl  r  RUN_PARALLELr  r  r
  r  r  r  r(  PYTEST_SINGLE_TESTr%  r  r;  	xmlrunnerxmlrunner.resultrX  XMLTestRunnerREPEAT_COUNTresultwasSuccessful)r  r   r  
other_argsr  failed_teststest_case_full_namecmd
string_cmdr  exitcoders   backend
world_size
env_prefixtest_batches	processesr   r  failedr  pytest_argsr'  r%  r  r  rX  rm  verboses                                r:   	run_testsr    s    W7>>/** 	Wo&& @""&)B--0?
,-	@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ MUOUUVVV _7>>-.. 	_)** Hb&*imm#4G
01H H H H H H H H H H H H H H H
 M]H[]]^^^  !!55h??E#E**  
G
>HZ::::zl
 !""X
  w!
 	97888 	53444 	.n--- 	86777 	8<77J ,6 \!$'''[[4STY4Z4Z[[[ 	
 #- 	> 	>  DG9,z9DHD-7?())RA$%& 
 #J2?ddG%c7I]DdAAcdeeeKHa1}}-1DDD jnnY;;G!#b!A!AJ!MG!M!M!M!MJ!+c!1J!>JjHjj^hjjkkk##$7888|$$)))+J+Q+QL!!6;;|#<#<,> ,>))))1	> 	>6 
		4U;;
!."<"<lKK	|$$ 	Q 	QA~&-1OA1O1O0PPS_`aSbbGZ-g$OOOPPPP 	/ 	/A&q))Q..FF99999z99	 =!122 	I.d;;;F4DFFGGGG5EGGHHH 	?,{122>K!$
:FK[111	 	2 0111 	i1nn)44444	 *!333333	G 	G 	G 	G 	G> 	G 	G 	G0 +,,%5 	HF4DFFGGG4I,C,C#")aa, -D -. -. 	/ 	/ 	/ 	/ 	/ 	/ 
		|$$ 	 	A=e$777>LLNN 	 	 	4      s$    )A55A9<A9)DDDlinuxdarwinppc64le)x86_64i386)arm64aarch64s390xc                      t           j        dk    rdS t          dd          5 } |                                 }d d d            n# 1 swxY w Y   d|v S )Nr  Fz/proc/cpuinfoascii)encodingvnni)r  r   r   read)fliness     r:   is_avx512_vnni_supportedr  N  s    
|wu	o	0	0	0 A              U?s   AA	A	c               /     K   d|v r|d         durt          d          nd|d<   t          j        | i |}	 |                                 |j        V  t          j        |j                   d S # t          j        |j                   w xY w)NdeleteFzAonly TemporaryFileName with delete=False is supported on Windows.)UserWarningtempfileNamedTemporaryFilecloserN   rK   unlinkr8   r9   r  s      r:   TemporaryFileNamer  X  s      
 vhu,,!"efff -  %F8'888	GGIII&LLLIafBIafs   A0 0Bc               /   p   K   t          j        | i |5 }|j        V  d d d            d S # 1 swxY w Y   d S r2   )r  r  rN   r  s      r:   r  r  i  s      ($9&99 	Q&LLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   
+//c              #      K   	 t          j        |           }|V  t          j        |           d S # t          j        |           w xY wN)suffix)r  mkdtempshutilrmtree)r  dir_names     r:   TemporaryDirectoryNamer  o  sS      	$'v666HNNNM(#####FM(####s	   3 A	c              #   n   K   t          j        |           5 }|V  d d d            d S # 1 swxY w Y   d S r  )r  TemporaryDirectory)r  ds     r:   r  r  y  s      (777 	1GGG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   *..c                      t           j                                        } t          t           | d           }t          |dd           x}o	 |            S )Nis_available)r3   r  _get_privateuse1_backend_namer   )privateuse1_backend_nameprivateuse1_backend_moduler  s      r:    is_privateuse1_backend_availabler    sQ    $xEEGG!(0H$!O!O#$>PTUUULi[g[g[i[iir;   r   numpyfairseqscipyr   r   rs  hpunumbatransformersdilllibrosa
opt_einsumz3r   c                 V    t          |           dk    r|                     d          ng S )Nr   rH  )rl  rI  r   s    r:   split_if_not_emptyr    s$    q66Q;;1773<<<B.r;   !PYTORCH_TESTING_DEVICE_EXCEPT_FORzno dillNO_MULTIPROCESSING_SPAWNTEST_WITH_ASANPYTORCH_TEST_WITH_ASAN)rO   TEST_WITH_DEV_DBG_ASANPYTORCH_TEST_WITH_DEV_DBG_ASANTEST_WITH_TSANPYTORCH_TEST_WITH_TSANTEST_WITH_UBSANPYTORCH_TEST_WITH_UBSANTEST_WITH_ROCMPYTORCH_TEST_WITH_ROCMPYTORCH_MIOPEN_SUGGEST_NHWCr@   rA   TEST_WITH_SLOWPYTORCH_TEST_WITH_SLOWTEST_SKIP_FASTPYTORCH_TEST_SKIP_FASTTEST_WITH_CROSSREFPYTORCH_TEST_WITH_CROSSREFTEST_SKIP_CUDAGRAPHPYTORCH_TEST_SKIP_CUDAGRAPHg333333@   zcuda.bindingsc                     ddl }| ^}}||j        j        j        j        k    rt          d|           t          |          S )zMakes calls to cuda-python's cuda runtime functions more
        pythonic by throwing an exception if they return a status
        which is not cudaSuccess
        r   NzCUDA failure! )cuda.bindingsbindingsruntimecudaError_tcudaSuccessrn  r-  )function_call_outputr   errorotherss       r:   cuda_python_error_checkr    sR    
 	-DM)5AAA5e55666== r;   c                     | dS d| v r|                      d          n| g} t          d | D                       }||v r||         dk    rdS dS )NFrH  c                 8    g | ]}|                     d           S ):)rI  )ra   vars     r:   rb   z/allocator_option_enabled_fn.<locals>.<listcomp>  s"    ???sCIIcNN???r;   TrueT)rI  ri   )allocator_configrs   optionmappings       r:   allocator_option_enabled_fnr    sv    u69=M6M6M'--c222TdSe??.>???@@GWV_66tur;   EXPANDABLE_SEGMENTSPYTORCH_CUDA_ALLOC_CONFexpandable_segments)r  )rO   rQ   NUM_PARALLEL_PROCS2i   @g333333?zRequires CUDAc                 <     t                      fd            }|S )Nc                  N    t           rt          j        d           | i | d S )Nz$test doesn't currently with crossref)r  r   SkipTestr   s     r:   r   zskipIfCrossRef.<locals>.wrapper  s8     	 #$JKKKBr;   r   r   s   ` r:   skipIfCrossRefr    3    
2YY        Y 
 Nr;   c                       e Zd ZddZdS )CrossRefModerB   Nc                     |pi } ||i |}|S r2   rB   )r   functypesr8   r9   rs         r:   __torch_function__zCrossRefMode.__torch_function__  s$    2D$!&!!r;   )rB   N)rf   rg   rh   r  rB   r;   r:   r  r    s(             r;   r  TEST_WITH_TORCHINDUCTORPYTORCH_TEST_WITH_INDUCTORTEST_WITH_AOT_EAGERPYTORCH_TEST_WITH_AOT_EAGERTEST_WITH_TORCHDYNAMOPYTORCH_TEST_WITH_DYNAMOc                      t           pt          S r2   )r  r
  rB   r;   r:   rE   rE   .  s    1H5H r;   )rO   rR   TEST_WITHOUT_COMPILED_AUTOGRAD&PYTORCH_TEST_WITHOUT_COMPILED_AUTOGRAD@   c                     t           r3t          j        d         dk    r t          j        d          |           S t           r| nt          j        |           S )Nr   r  z'skipping numpy 2.0+ dynamo-wrapped test)r  np__version__r   r_  expectedFailurer  s    r:   xpassIfTorchDynamo_npr  F  sQ     N!2c!9!9Gx}FGGMMM(L44h.Ft.L.LLr;   c                 <    t           rt          j        |           n| S r2   )TEST_ACLr   r  r  s    r:   
xfailIfACLr  M      -5?8#D)))4?r;   c                 <    t           rt          j        |           n| S r2   )r  r   r  r  s    r:   xfailIfTorchDynamor  Q  s    -BL8#D)))Lr;   c                 N    t           j        dk    rt          j        |           n| S )N)r  r  )r  version_infor   r  r  s    r:   xfailIfPy312Plusr   U  s%    -0-=-H-H8#D)))dRr;   c                 X    t           r"t          st          st          j        |           n| S r2   )IS_LINUXr  r|   r   r  r  s    r:   xfailIfLinuxr#  Y  s)    -5hnhU^h8#D)))dhhr;   'test doesn't currently work with dynamoc                 P     t           t                    s
J d             fd}|S )zQ
    Usage:
    @skipIfTorchDynamo(msg)
    def test_blah(self):
        ...
    z*Are you using skipIfTorchDynamo correctly?c                      t           t                    st                      fd            }|S t           t                    sJ t          rd _         _         S )Nc                  N    t           rt          j                   | i | d S r2   )r  r   r  r8   r9   r   msgs     r:   r   z5skipIfTorchDynamo.<locals>.decorator.<locals>.wrapperh  s7    ( ("+C000B''''''r;   T)r   r   r   r  __unittest_skip____unittest_skip_why__r   r   r)  s   ` r:   r5  z$skipIfTorchDynamo.<locals>.decoratorf  s~    "d## 	2YY( ( ( ( ( Y(
 N"d#####  	+#'B '*B$	r;   r   rl   r)  r5  s   ` r:   skipIfTorchDynamor/  ]  sE     c3MM!MMM    " r;   z.test doesn't currently work with torchinductorc                       fd}|S )Nc                      t           t                    st                      fd            }|S t           t                    sJ rd _         _         S )Nc                  D    rt          j                   | i | d S r2   r   r  r8   r9   	conditionr   r)  s     r:   r   z7skipIfTorchInductor.<locals>.decorator.<locals>.wrapper}  7     ("+C000B''''''r;   Tr   r   r   r*  r+  r   r   r5  r)  s   ` r:   r5  z&skipIfTorchInductor.<locals>.decorator{  s    "d## 	2YY( ( ( ( ( ( Y(
 N"d##### 	+#'B '*B$	r;   rB   r)  r5  r5  s   `` r:   skipIfTorchInductorr:  y  s*         " r;   2test doesn't currently work with compiled autogradc                 :    t          | t                    sJ d }|S )zZ
    Usage:
    @runWithoutCompiledAutograd(msg)
    def test_blah(self):
        ...
    c                 <     t                      fd            }|S )Nc                      t           j        j                                        5   | i | d d d            d S # 1 swxY w Y   d S r2   )r3   _dynamocompiled_autograd_disable)r8   r9   r  s     r:   r   z>runWithoutCompiledAutograd.<locals>.decorator.<locals>.wrapper  s    099;; & &d%f%%%& & & & & & & & & & & & & & & & & &s   	;??r  r  r   s   ` r:   r5  z-runWithoutCompiledAutograd.<locals>.decorator  s3    	t	& 	& 	& 	& 
	& r;   r-  r.  s     r:   runWithoutCompiledAutogradrC    s1     c3   r;   c                 B     t                     t          u sJ  fd}|S )z@
    Decorator for running tests serially.  Requires pytest
    c                 X    t           r!rt          j                            |           S | S r2   )
has_pytestr%  markserialr   r5  s    r:   r5  zserialTest.<locals>.decorator  s-     	*) 	*;%%b)))	r;   )r   rq   )r5  r5  s   ` r:   
serialTestrJ    s=     	??d""""     r;   c                 &    d }| |S  ||           S )Nc                     d| _         | S rG   )dynamo_strictr+  s    r:   r5  z)unMarkDynamoStrictTest.<locals>.decorator  s    !
r;   rB   )r+  r5  s     r:   unMarkDynamoStrictTestrO    s,       {y~~r;   c                 ,    fd}| |S  ||           S )z
    Marks the test as 'strict'. In strict mode, we reset before and after the
    test, and run without suppress errors.

    Args:
    - nopython: if we should run torch._dynamo.optimize with nopython={True/False}.
    c                     t          j        |           rd| _        | _        | S | t	                    fd            }|S )NTc                      t           j                                         t          j                            dd          5   | i | d d d            n# 1 swxY w Y   t           j                                         d S )N$torch._dynamo.config.suppress_errorsF)r3   r?  resetr   mockpatchr   s     r:   r   z8markDynamoStrictTest.<locals>.decorator.<locals>.wrapper  s    M!!!$$%KUSS $ $D#F###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $M!!!!!s    	AAA)r   isclassrM  dynamo_strict_nopythonr   )cls_or_funcr   r   nopythons     @r:   r5  z'markDynamoStrictTest.<locals>.decorator  sa    ?;'' 	(,K%19K.	r	" 	" 	" 	" 
	"
 r;   rB   )rY  rZ  r5  s    ` r:   markDynamoStrictTestr[    s:          y%%%r;   @test doesn't currently work with torchinductor on the ROCm stackc                 <    t          | t          ot                    S )N)r)  r5  )r:  r  r  r)  s    r:   skipRocmIfTorchInductorr_    s    3.2\E\]]]]r;   4test doesn't currently work with legacy JIT executorc                       fd}|S )Nc                      t           t                    st                      fd            }|S t           t                    sJ t          t          j        k    rd _         _         S )Nc                  j    t           t          j        k    rt          j                   | i | d S r2   )r  r  r  r   r  r(  s     r:   r   z;skipIfLegacyJitExecutor.<locals>.decorator.<locals>.wrapper  s=    !]%999"+C000B''''''r;   T)r   r   r   r  r  r  r*  r+  r,  s   ` r:   r5  z*skipIfLegacyJitExecutor.<locals>.decorator  s    "d## 	2YY( ( ( ( ( Y(
 N"d#####]111#'B '*B$	r;   rB   r.  s   ` r:   skipIfLegacyJitExecutorrd    s$        $ r;   r   .c                      ddl mmm  d S dt          dt
          dt          f         ddffd	t          j                   dt          ddf fd
            }|S )z
    Decorator function to create a dynamo test case. A function annotate with
    this decorator takes as input a unittest object.
    r   )CompileCounterrT  optimize_assertNc                      t          |           S r2   )make_dynamo_testr   s    r:   rE   z"make_dynamo_test.<locals>.<lambda>  s    *2.. r;   r   r   .r\   c                      d fd}             } |                             |          |          } |                           d S )Nr\   c                        fi  d S r2   rB   )r   r9   r   s   r:   dummyz6make_dynamo_test.<locals>.standard_test.<locals>.dummy   s!    Btvr;   rI  rB   )	r   r   r9   rm  actualopt_fnrf  rg  rT  s	   ```   r:   standard_testz'make_dynamo_test.<locals>.standard_test  s    
	 	 	 	 	 	 	 	  !!(((//r;   c                       | |          S )N)r   r9   rB   )r   r9   r   rp  s     r:   r   z!make_dynamo_test.<locals>.test_fn  s%    }
 
 
 	
r;   )torch._dynamo.testingrf  rT  rg  r   r   r   r   )r   r   rf  rg  rT  rp  s   ` @@@@r:   ri  ri    s     MLLLLLLLLL	z...S#X 
	         _R
c 
 
 
 
 
 
 
 
 Nr;   PYTORCH_TEST_WITH_TVTEST_CUDA_MEM_LEAK_CHECK PYTORCH_TEST_CUDA_MEM_LEAK_CHECKc                 b    	 t           |          S # t          $ r t           | j                 cY S w xY wr2   )numpy_to_torch_dtype_dictKeyErrorr   np_dtypes    r:   numpy_to_torch_dtyper{  >  s@    8(22 8 8 8(77778s    ..c                 H    	 t          |            dS # t          $ r Y dS w xY wNTF)r{  rx  ry  s    r:   has_corresponding_torch_dtyper~  E  s<    X&&&t   uu    
!!c                     i | ]\  }}||	S rB   rB   )ra   keyrZ   s      r:   
<dictcomp>r  U  s    ___\c5US___r;   z3test doesn't currently work with nn module inliningc                       fd}|S )Nc                      t           t                    st                      fd            }|S t           t                    sJ rd _         _         S )Nc                  D    rt          j                   | i | d S r2   r3  r4  s     r:   r   z9skipIfNNModuleInlined.<locals>.decorator.<locals>.wrapperb  r6  r;   Tr7  r8  s   ` r:   r5  z(skipIfNNModuleInlined.<locals>.decorator_  s    "d## 		2YY( ( ( ( ( ( Y( N"d##### 	+#'B '*B$	r;   rB   r9  s   `` r:   skipIfNNModuleInlinedr  [  s*         & r;   z-test doesn't currently work on the ROCm stackr^  c                ,    fd}| r ||           S |S )Nc                 L     d t                      fd            }|S )NzskipIfRocm: c                  J    t           rt          j                   | i |S r2   r  r   r  r8   r9   r   rd  s     r:   r   z+skipIfRocm.<locals>.dec_fn.<locals>.wrapperx  s1     +'///r4*6***r;   r  r   r   rd  r)  s   ` @r:   dec_fnzskipIfRocm.<locals>.dec_fnu  sG    %%%	r	+ 	+ 	+ 	+ 	+ 
	+
 r;   rB   r  r)  r  s    ` r:   
skipIfRocmr  t  9    	 	 	 	 	  vd||Mr;   archc                       fd}|S )Nc                 @     t                      fd            }|S )Nc                     t           rZt          j                            d          }|j                            d          d         v rd }t          j        |           | g|R i |S )Nr   r  zskipIfRocm: test skipped on r  r3   r   get_device_propertiesgcnArchNamerI  r   r  r   r8   r9   proprd  r  r   s        r:   wrap_fnz/skipIfRocmArch.<locals>.dec_fn.<locals>.wrap_fn  s     4z77::#))#..q1T99BDBBF"+F3332d,T,,,V,,,r;   r  r   r  r  s   ` r:   r  zskipIfRocmArch.<locals>.dec_fn  9    	r	- 	- 	- 	- 	- 
	- r;   rB   r  r  s   ` r:   skipIfRocmArchr    #    	 	 	 	 	 Mr;   c                 <     t                      fd            }|S )Nc                  N    t           r
 | i | d S t          j        d          )Nz+test currently only works on the ROCm stackr  r   s     r:   r   zrunOnRocm.<locals>.wrapper  s9     	SB#$QRRRr;   r  r   s   ` r:   	runOnRocmr    s8    
2YYS S S S YS
 Nr;   c                       fd}|S )Nc                 @     t                      fd            }|S )Nc                     t           rZt          j                            d          }|j                            d          d         vrd }t          j        |           | g|R i |S )Nr   r  zskipIfRocm: test only runs on r  r  s        r:   r  z.runOnRocmArch.<locals>.dec_fn.<locals>.wrap_fn  s     4z77::#))#..q1==DdDDF"+F3332d,T,,,V,,,r;   r  r  s   ` r:   r  zrunOnRocmArch.<locals>.dec_fn  r  r;   rB   r  s   ` r:   runOnRocmArchr    r  r;   c                 <    t           rt          j        |           n| S r2   )IS_S390Xr   r  r  s    r:   xfailIfS390Xr    r  r;   c                       fd}|S )Nc                 4    rt          j        |           S | S r2   )r   r  )r  r5  s    r:   r   zxfailIf.<locals>.wrapper  s!     	+D111Kr;   rB   )r5  r   s   ` r:   xfailIfr    s#        
 Nr;   z,test doesn't currently work on the XPU stackc                ,    fd}| r ||           S |S )Nc                 L     d t                      fd            }|S )NzskipIfXpu: c                  J    t           rt          j                   | i |S r2   TEST_XPUr   r  r  s     r:   r   z*skipIfXpu.<locals>.dec_fn.<locals>.wrapper  s1     +'///r4*6***r;   r  r  s   ` @r:   r  zskipIfXpu.<locals>.dec_fn  sG    $s$$	r	+ 	+ 	+ 	+ 	+ 
	+
 r;   rB   r  s    ` r:   	skipIfXpur    r  r;   c                 <     t                      fd            }|S )Nc                  N    t           rt          j        d           | i | d S )Nz$test doesn't currently work with MPS)TEST_MPSr   r  r   s     r:   r   zskipIfMPS.<locals>.wrapper  8     	 #$JKKKBr;   r  r   s   ` r:   	skipIfMPSr    r   r;   c                 <     t                      fd            }|S )Nc                  ~    t           r,t          t                    dk    rt          j        d           | i | d S )N   z Test crashes MPSGraph on MacOS13)r  r   MACOS_VERSIONr   r  r   s     r:   r   z#skipIfMPSOnMacOS13.<locals>.wrapper  sJ     	 M**b00#$FGGGBr;   r  r   s   ` r:   skipIfMPSOnMacOS13r    r   r;   c                 <     t                      fd            }|S )Nc                  N    t           rt          j        d           | i | d S )Nz$test doesn't currently work with HPU)TEST_HPUr   r  r   s     r:   r   zskipIfHpu.<locals>.wrapper  r  r;   r  r   s   ` r:   	skipIfHpur    r   r;   c                       fd}|S )Nc                 @     t                      fd            }|S )Nc                 `   t           rt          t          j        j                  }|                    dd          d         }t          d |                    d          D                       }||t                    k     rd| d d	}t          j        |           | g|R i |S )
Nr  ri  )maxsplitr   c              3   4   K   | ]}t          |          V  d S r2   r   ra   r   s     r:   r   zMskipIfRocmVersionLessThan.<locals>.dec_fn.<locals>.wrap_fn.<locals>.<genexpr>  s(      *S*Sa3q66*S*S*S*S*S*Sr;   r   zROCm z is available but z	 required)	r  rl   r3   versionhiprI  r-  r   r  )r   r8   r9   rocm_versionrocm_version_tuplerd  r   r  s         r:   r  z:skipIfRocmVersionLessThan.<locals>.dec_fn.<locals>.wrap_fn  s     4"5=#455+11#1BB1E%**S*S<;M;Mc;R;R*S*S*S%S%S"%-DVY^_fYgYgDgDg]%7]]7]]]F"+F3332d,T,,,V,,,r;   r  )r   r  r  s   ` r:   r  z)skipIfRocmVersionLessThan.<locals>.dec_fn  s9    	r	- 	- 	- 	- 	- 
	- r;   rB   )r  r  s   ` r:   skipIfRocmVersionLessThanr    s#         Mr;   c                 <     t                      fd            }|S )Nc                  N    t           st          j        d           | i | d S )Nz:test doesn't currently work without MIOpen NHWC activation)TEST_WITH_MIOPEN_SUGGEST_NHWCr   r  r   s     r:   r   z+skipIfNotMiopenSuggestNHWC.<locals>.wrapper  s8    , 	 #$`aaaBr;   r  r   s   ` r:   skipIfNotMiopenSuggestNHWCr    r   r;   z0test doesn't currently work on the Windows stackc                ,    fd}| r ||           S |S )Nc                 L     d t                      fd            }|S )NzskipIfWindows: c                  J    t           rt          j                   | i |S r2   )
IS_WINDOWSr   r  r  s     r:   r   z.skipIfWindows.<locals>.dec_fn.<locals>.wrapper  s1     +'///r4*6***r;   r  r  s   ` @r:   r  zskipIfWindows.<locals>.dec_fn  sG    (3((	r	+ 	+ 	+ 	+ 	+ 
	+
 r;   rB   r  s    ` r:   skipIfWindowsr    r  r;   c                     t           j                                        oCt           j                                        dk    o!t           j                                        dk    } t           j                                        }t          |dz
            D ]@}t          |dz   |          D ]&}t           j                            ||          sd}  n'| s nAt          |  d          S )N)r  r   r   ri  Fz cuda p2p access is not available)r3   r   r  get_device_capabilitydevice_countr
  can_device_access_peerskip_but_pass_in_sandcastle_if)cuda_p2p_access_availablenum_devicesr   js       r:   requires_cuda_p2p_accessr    s    
!! 	+J,,..&8	+J##%%* 
 *))++K;?##  q1uk** 	 	A:44Q:: ,1) ) 	E	 *%%*  r;   c                 <     t                      fd            }|S )Nc                      t           j        j                                        }	  | i | t           j        j                            |           d S # t           j        j                            |           w xY wr2   )r3   backendsr   preferred_linalg_libraryr8   r9   _preferred_backendr   s      r:   _fnz.setLinalgBackendsToDefaultFinally.<locals>._fn  su    "^0IIKK	MBN889KLLLLLEN889KLLLL   A &A:r  r   r  s   ` r:   !setLinalgBackendsToDefaultFinallyr    s8    
2YYM M M M YM Jr;   c                 <     t                      fd            }|S )Nc                      t           j        j                                        }	  | i | t           j        j                            |           d S # t           j        j                            |           w xY wr2   )r3   r  r   preferred_blas_libraryr  s      r:   r  z,setBlasBackendsToDefaultFinally.<locals>._fn)  su    "^0GGII	KBN667IJJJJJEN667IJJJJr  r  r  s   ` r:   setBlasBackendsToDefaultFinallyr  (  s8    
2YYK K K K YK Jr;   c                   D    e Zd ZddddZed             Zd Zd Zd Zd	S )
DeterministicGuardFT	warn_onlyfill_uninitialized_memoryc                0    || _         || _        || _        d S r2   )deterministicr  r  )r   r  r  r  s       r:   r   zDeterministicGuard.__init__6  s    *")B&&&r;   c                      | t          j                    t          j                    t           j        j        j                  S )Nr  )r3   $are_deterministic_algorithms_enabled-is_deterministic_algorithms_warn_only_enabledutilsr  r  rN  s    r:   _current_statez!DeterministicGuard._current_state;  s>    s688IKK&+k&?&Y
 
 
 	
r;   c                 |    t          j        | j        | j                   | j        t           j        j        _        d S )Nr  )r3   use_deterministic_algorithmsr  r  r  r  r   s    r:   _updatezDeterministicGuard._updateC  s3    *4+=XXXX>B>\!;;;r;   c                 `    |                                  | _        |                                  d S r2   )r  _restorer  r   s    r:   	__enter__zDeterministicGuard.__enter__G  s&    ++--r;   c                 8    | j                                          d S r2   )r  r  r   exception_typeexception_value	tracebacks       r:   __exit__zDeterministicGuard.__exit__K  s    r;   N)	rf   rg   rh   r   classmethodr  r  r  r  rB   r;   r:   r  r  5  s        38TX C C C C C
 
 
 [
] ] ]           r;   r  c                        e Zd Zd Zd Zd ZdS )AlwaysWarnTypedStorageRemovalc                 B    t          |t                    sJ || _        d S r2   )r   rq   always_warn)r   r	  s     r:   r   z&AlwaysWarnTypedStorageRemoval.__init__O  s&    +t,,,,,&r;   c                     t           j                                        | _        t           j                            | j                   d S r2   )r3   storage&_get_always_warn_typed_storage_removalalways_warn_restore&_set_always_warn_typed_storage_removalr	  r   s    r:   r  z'AlwaysWarnTypedStorageRemoval.__enter__S  s7    #(=#W#W#Y#Y <<T=MNNNNNr;   c                 N    t           j                            | j                   d S r2   )r3   r  r  r  r   s       r:   r  z&AlwaysWarnTypedStorageRemoval.__exit__W  s!    <<T=UVVVVVr;   Nrf   rg   rh   r   r  r  rB   r;   r:   r  r  N  sI        ' ' 'O O OW W W W Wr;   r  c                        e Zd Zd Zd Zd ZdS )CudaSyncGuardc                     || _         d S r2   )mode)r   sync_debug_modes     r:   r   zCudaSyncGuard.__init___  s    #			r;   c                     t           j                                        | _        t           j                            | j                   d S r2   )r3   r   get_sync_debug_modedebug_mode_restoreset_sync_debug_moder  r   s    r:   r  zCudaSyncGuard.__enter__b  s6    "'*"@"@"B"B
&&ty11111r;   c                 N    t           j                            | j                   d S r2   )r3   r   r  r  r   s       r:   r  zCudaSyncGuard.__exit__f  s!    
&&t'>?????r;   Nr  rB   r;   r:   r  r  ^  sF        $ $ $2 2 2@ @ @ @ @r;   r  c                        e Zd Zd Zd Zd ZdS )SwapTensorsGuardc                     || _         d S r2   )use_swap_tensors)r   r  s     r:   r   zSwapTensorsGuard.__init__l  s     0r;   c                     t           j                                        | _        | j        &t           j                            | j                   d S d S r2   )r3   
__future__$get_swap_module_params_on_conversionswap_tensors_restorer  $set_swap_module_params_on_conversionr   s    r:   r  zSwapTensorsGuard.__enter__o  sJ    $)$4$Y$Y$[$[! ,AA$BWXXXXX -,r;   c                 N    t           j                            | j                   d S r2   )r3   r   r#  r"  r   s       r:   r  zSwapTensorsGuard.__exit__t  s"    ==d>WXXXXXr;   Nr  rB   r;   r:   r  r  k  sI        1 1 1Y Y Y
Y Y Y Y Yr;   r  c                 <     t                      fd            }|S )Nc                     t          t          j                    t          j                              5   G d d          } |            5   | i | d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr  c                       e Zd ZdZd Zd ZdS )HwrapDeterministicFlagAPITest.<locals>.wrapper.<locals>.CuBLASConfigGuardCUBLAS_WORKSPACE_CONFIGc                     t           j                            | j                  | _        dt           j        | j        <   d S )Nz:4096:8rK   r  r  cublas_var_namecublas_config_restorer   s    r:   r  zRwrapDeterministicFlagAPITest.<locals>.wrapper.<locals>.CuBLASConfigGuard.__enter__  s0    13@T1U1UD.7@BJt3444r;   c                     t           j                            | j                  }| j        |t           j        | j        = d S d S | j        t           j        | j        <   d S r2   r+  )r   r  r  r  cur_cublas_configs        r:   r  zQwrapDeterministicFlagAPITest.<locals>.wrapper.<locals>.CuBLASConfigGuard.__exit__  s^    (*
t7K(L(L%19,8 "
4+? @ @ @ 98 <@;U
4#7888r;   N)rf   rg   rh   r,  r  r  rB   r;   r:   CuBLASConfigGuardr(    s?        ";A A AV V V V Vr;   r0  )r  r3   r  r  )r8   r9   r0  r   s      r:   r   z-wrapDeterministicFlagAPITest.<locals>.wrapper  sB   :<<MOOQ Q Q 	$ 	$V V V V V V V V #"$$ $ $D#F###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $#	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s5   A:	A"A:"A&	&A:)A&	*A::A>A>r  r   s   ` r:   wrapDeterministicFlagAPITestr1    s3    
2YY$ $ $ $ Y$( Nr;   c                       fd}|S )Nc                 @     t                      fd            }|S )Nc                  h    t                    5   | i | d d d            d S # 1 swxY w Y   d S r2   )r  )r8   r9   r   swaps     r:   r   z4wrapSwapTensorsTest.<locals>.dec_fn.<locals>.wrapper  s    !$'' $ $D#F###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s   	'++r  )r   r   r5  s   ` r:   r  z#wrapSwapTensorsTest.<locals>.dec_fn  s9    	r	$ 	$ 	$ 	$ 	$ 
	$ r;   rB   )r5  r  s   ` r:   wrapSwapTensorsTestr6    s#         Mr;   c                   $     e Zd Z fdZd Z xZS )r5  c                 V    t                                                       || _        d S r2   )r\  r   swap_values)r   r9  r]  s     r:   r   zswap.__init__  s'    &r;   c              #   b   K   | j         D ]$} t          |          |          d| i d fV  %d S )Nswap_c                     g S r2   rB   re  s    r:   rE   z(swap._parametrize_test.<locals>.<lambda>  s    QS r;   )r9  r6  )r   r   r  r  r5  s        r:   r  zswap._parametrize_test  sY      $ 	T 	TD+%d++D11>4>>2||SSSSS	T 	Tr;   )rf   rg   rh   r   r  rk  rl  s   @r:   r5  r5    sN        ' ' ' ' 'T T T T T T Tr;   r5  c                      t           r<	 t          j        t          j        ddg                     n# t
          $ r dY nw xY wt                      fd            }|S )Nr   Fc                  D    st          j        d           | i | d S )Nz*PyTorch was compiled without numpy supportr3  )r8   r9   r   numpy_supports     r:   r   z+skipIfCompiledWithoutNumpy.<locals>.wrapper  s8     	 #$PQQQBr;   )
TEST_NUMPYr3   
from_numpyr  arrayr  r   )r   r   r?  s   ` @r:   skipIfCompiledWithoutNumpyrC    s     M "	" RXq!f--.... 	" 	" 	"!MMM	" 2YY          Y 
 Ns   (6 AAc                       fd}|S )Nc                      |           S r2   rB   )r   rR  r   s    r:   run_test_functionz)_test_function.<locals>.run_test_function  s    r$r;   rB   )r   rR  rF  s   `` r:   _test_functionrG    s(               r;   c                 <     t                      fd            }|S )Nc                  l    t           j        j        j        st	          j        d           | i | d S )NzIXNNPACK must be enabled for these tests. Please build with USE_XNNPACK=1.)r3   r  xnnpackrS   r   r  r   s     r:   r   z skipIfNoXNNPACK.<locals>.wrapper  s@    ~%- 	 #$opppBr;   r  r   s   ` r:   skipIfNoXNNPACKrK    r   r;   c                 <     t                      fd            }|S )Nc                  b    t           j        j        st          j        d           | i | d S )NzPyTorch compiled without Lapack)r3   r  
has_lapackr   r  r   s     r:   r   zskipIfNoLapack.<locals>.wrapper  s=    x" 	 #$EFFFBr;   r  r   s   ` r:   skipIfNoLapackrO    r   r;   c                 *    t          j        d          S )aH  Wraps the decorator to hide the import of the `core`.

    Args:
        op_name: Check if this op is registered in `core._REGISTERED_OPERATORS`.
        message: message to fail with.

    Usage:
        @skipIfNotRegistered('MyOp', 'MyOp is not linked!')
            This will check if 'MyOp' is in the caffe2.python.core
    z"Pytorch is compiled without Caffe2)r   r_  )op_namemessages     r:   skipIfNotRegisteredrS    s     ==>>>r;   c                 <     t                      fd            }|S )Nc                  N    t           st          j        d           | i | d S )Nz'test require SciPy, but SciPy not found)
TEST_SCIPYr   r  r   s     r:   r   zskipIfNoSciPy.<locals>.wrapper  s8     	 #$MNNNBr;   r  r   s   ` r:   skipIfNoSciPyrW    r   r;   c                 <     t                      fd            }|S )Nc                  X    dt           j        v rt          j        d           | i |S )NPYTEST_CURRENT_TESTzdoes not work under pytest)rK   r  r   r  r   s     r:   wrappedzskip_if_pytest.<locals>.wrapped	  s7     BJ..#$@AAAr4"6"""r;   r  )r   r[  s   ` r:   skip_if_pytestr\  	  s3    
2YY# # # # Y#
 Nr;   c                 <     t                      fd            }|S )Nc                  N    t           st          j        d           | i | d S )Nz)test required PyTorched compiled with XPUr  r   s     r:   r   zskipIfNoXPU.<locals>.wrapper	  s8     	 #$OPPPBr;   r  r   s   ` r:   skipIfNoXPUr_  	  r   r;   c                 P     t                      fd            }d|j        d<   |S )Nc                  N    t           st          j        d           | i | d S )N<test is slow; run with PYTORCH_TEST_WITH_SLOW to enable test)r  r   r  r   s     r:   r   zslowTest.<locals>.wrapper	  s8     	 #$bcccBr;   T	slow_test)r   __dict__r   s   ` r:   slowTestre  	  s@    
2YY        Y 
 %)G[!Nr;   c                     | rt           nd S )Nc                     | S r2   rB   rj  s    r:   rE   zslowTestIf.<locals>.<lambda>#	  s     r;   )re  )r5  s    r:   
slowTestIfrh  "	  s     388mm3r;   c                       fd}|S )Nc                 :    t          | dd          r | _        | S )N_do_cuda_memory_leak_checkT)r   rk  rI  s    r:   decz&skipCUDAMemoryLeakCheckIf.<locals>.dec'	  s'    23T:: 	:09MB)	r;   rB   r5  rl  s   ` r:   skipCUDAMemoryLeakCheckIfrn  &	  #         Jr;   c                       fd}|S )Nc                 :    t          | dd          r | _        | S )N_do_cuda_non_default_streamT)r   rr  rI  s    r:   rl  z'skipCUDANonDefaultStreamIf.<locals>.dec.	  s'    24d;; 	;1:]B*	r;   rB   rm  s   ` r:   skipCUDANonDefaultStreamIfrs  -	  ro  r;   c                 <     t                      fd            }|S )Nc                      t          j                    5  t          j        d            | i | d d d            d S # 1 swxY w Y   d S )Nignore)rv  catch_warningssimplefilterr   s     r:   r   z"suppress_warnings.<locals>.wrapper5	  s    $&& 	  	 !(+++B	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   ?AAr  r   s   ` r:   suppress_warningsry  4	  s3    
2YY        Y  Nr;   c                    i t          | t          j                  rz| j        sJ                     | j        | j                  }t          j                    5  |                     |dd          }| j        |_        d d d            n# 1 swxY w Y   |S t          j	        |           rL| 
                                                    |                                                               |           S t          | t                    rfd| D             S t          | t                    rt          fd| D                       S t!          |           S )Nr   T)rP  rR  copyc                 0    g | ]}t          |          S rB   to_gpura   otype_maps     r:   rb   zto_gpu.<locals>.<listcomp>J	  s#    111q(##111r;   c              3   8   K   | ]}t          |          V  d S r2   r}  r  s     r:   r   zto_gpu.<locals>.<genexpr>L	  s-      66QVAx((666666r;   )r   r3   r   is_leafr  rP  no_gradtorequires_grad
is_storagenewresize_sizecopy_r  r-  r   )objr  tress    `  r:   r~  r~  =	  s   #u|$$ {{LLCI..]__ 	2 	2&&qd&;;C # 1C	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 
		#		 wwyy  ,,223777	C		 1111S1111	C		 6666#666666}}s   %BBBc                 4    t          j        |           j        S r2   )r   getfullargspecr8   r  s    r:   get_function_arglistr  Q	  s    !$'',,r;   c                     t          j        |            t          j        |            t          r!t
          j                            |            d S d S r2   )r3   manual_seedrandomr   r@  r  )r   s    r:   r   r   U	  sO    	d
K 
	t r;   c              #      K   t          j                    }t          j        |            	 d V  t          j        |           d S # t          j        |           w xY wr2   )r3   get_default_dtypeset_default_dtype)rP  saved_dtypes     r:   r  r  \	  sa      )++K	E"""-,,,,,,,,,   A Ac              #      K   t          j        g                                           }t          j        |            	 d V  t          j        |           d S # t          j        |           w xY wr2   )r3   tensorr   set_default_tensor_type)tensor_typesaved_tensor_types     r:   r  r  e	  sq      R((--//	!+...9%&788888%&78888s   A A.c                    |                                  dk    rt          d          S |                                  dk    r"t          |                     d                    S t          d |                                 D              S )Nr   ri  c              3   4   K   | ]}t          |          V  d S r2   r	  rF  s     r:   r   ziter_indices.<locals>.<genexpr>s	  s(      55!U1XX555555r;   )dimr
  r  r   )r  s    r:   iter_indicesr  n	  sm    zz||qQxxzz||qV[[^^$$$55v{{}}55566r;   c                 H    	 t          |            dS # t          $ r Y dS w xY wr}  )r2  	TypeError)r  s    r:   is_iterabler  v	  s9    S			t   uur  c                     t          | t          j                  rdS 	 t          |           dk    r|S t	          |           D ]}t          |t          j                  s dS  n# t
          $ r Y dS w xY wdS )z Returns True if iterable is an iterable of tensors and False o.w.

        If the iterable is empty, the return value is :attr:`include_empty`
    Fr   T)r   r3   r   rl  r2  r  )iterableinclude_emptyr  s      r:   is_iterable_of_tensorsr  ~	  s     (EL)) u	x==A  h 	 	Aa.. uu	    uu 4s   A# ,A# !A# #
A10A1c                       e Zd Zd Zd ZdS )CudaNonDefaultStreamc                 8   t           j                                        }g | _        t	          t           j                                                  D ]}| j                            t           j                            |                     t           j                            |          }| j        d         	                                 t           j
                            |j        |j        |j                   t           j
                            |           d S )NrR  rs  	stream_iddevice_indexdevice_type)r3   r   current_devicebeforeStreamsr
  r  rB  current_streamStreamsynchronizer  _cuda_setStreamr  r  r  _cuda_setDevice)r   beforeDevicer  deviceStreams       r:   r  zCudaNonDefaultStream.__enter__	  s     z0022uz..0011 	K 	KA%%ej&?&?&B&BCCC :,,A,66Lr"..000H$$|/E2>2K1=1I % K K K K 	  .....r;   c                    t           j                                        }t          t           j                                                  D ]T}t           j                            | j        |         j        | j        |         j	        | j        |         j
                   Ut           j                            |           d S )Nr  )r3   r   r  r
  r  r  r  r  r  r  r  r  )r   exc_type	exc_valuer  r  r  s         r:   r  zCudaNonDefaultStream.__exit__	  s     z0022uz..0011 	T 	TAH$$t/A!/D/N262DQ2G2T151CA1F1R % T T T T 	  .....r;   Nrf   rg   rh   r  r  rB   r;   r:   r  r  	  s2        / / // / / / /r;   r  c                   "    e Zd ZddZd Zd ZdS )CudaMemoryLeakCheckNc                 n    ||                                 n|| _        || _        ddlm}  |             d S )Nr   )initialize_cuda_context_rng)r   rN   r+  #torch.testing._internal.common_cudar  )r   r+  rN   r  s       r:   r   zCudaMemoryLeakCheck.__init__	  sJ    %)\HKKMMMt	  	TSSSSS##%%%%%r;   c                    g | _         g | _        t          j                                        }t          |          D ]x}t          j                            |          }|dk    rQt          j                     t          j	        
                                 t          j                                          nyt          |          D ]z}| j                             t          j                            |                     t          j                            |          \  }}||z
  }| j                            |           {d S Nr   )caching_allocator_beforesdriver_beforesr3   r   r  r
  memory_allocatedr   r   r  _cuda_clearCublasWorkspacesr   rB  mem_get_info)r   r  r   caching_allocator_mem_allocated
bytes_freebytes_totaldriver_mem_allocateds          r:   r  zCudaMemoryLeakCheck.__enter__	  s(   )+&  j--//{## 	 	A.3j.I.I!.L.L+ /22
44666
&&(((	 3 {## 	= 	=A*11%*2M2Ma2P2PQQQ&+j&=&=a&@&@#J#.#; &&';<<<<		= 	=r;   c                    |d S d}t           j                                        }t          |          D ]T}t           j                                         t           j                            |          }|| j        |         k    rd} nU|sd S t          j	                     t           j        
                                 t          |          D ]}d}t          d          D ]z}t           j                            |          }t           j                            |          \  }	}
|
|	z
  }d}d}|| j        |         k    rd}|| j        |         k    rd}|s|sd} n{|s|rG|sEd| j         d| j        |          d| d| d| j        |          d	| d
}t          j        |           |rA|r?d| j         d| j        |          d| d| d| j        |          d	| d
}t!          |           d S )NFTr  zOCUDA caching allocator reports a memory leak not verified by the driver API in z)! Caching allocator allocated memory was z and is now reported as z on device z#. CUDA driver allocated memory was z and is now r   z$CUDA driver API confirmed a leak in )r3   r   r  r
  r  r  r  r  r   r   r   r  r  rN   rv  rw  r  )r   r  r  r  discrepancy_detectedr  r   r  rs   r  r  r  caching_allocator_discrepancydriver_discrepancyr)  s                  r:   r  zCudaMemoryLeakCheck.__exit__	  s   F
  %j--//{## 	 	AH00222.3j.I.I!.L.L+.1OPQ1RRR'+$ S
 $ 	F 	

   {## 1	( 1	(A#'  1XX  27*2M2Ma2P2P/*/**A*A!*D*D'
K'2Z'?$05-%*"2T5STU5VVV481'$*=a*@@@)-&5 9K +0(E' , (5G (w8<	w wAEA_`aAbw w 2Qw w %&	w w
 <@;Nq;Qw w
 `tw w w c"""". 	(3E 	(wdi w wAEA_`aAbw w1Pw w %&w w <@;Nq;Q	w w `t	w w w #3'''c1	( 1	(r;   r2   r  rB   r;   r:   r  r  	  sK        & & & &= = =.S( S( S( S( S(r;   r  c              #   ^   K   	 d V  d S # | $ r}t          j        d|           |d }~ww xY w)Nznot implemented: r3  )r  es     r:   skip_exception_typer  *
  sX      @ @ @ @ 7A 7 788a?@s   
 ,',c              #     K   	 d V  d S # t           j        $ r  t          $ r}d}t          |dd           }|
d|j         }d                    t          d |g| R                     }t          ddt          j	        dd                    t          d |g| R                     i                     d	| d
}t          |j                  dk    r(|j        d          d| g|j        dd          R |_         d }~ww xY w)Nr   _tracked_inputz"PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=r^   test_reprostest_failurerepro)	subsystemrN   
parameterszE
To execute this test, run the following from the base repo dir:
    zL

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0ri  r   
)r   r  r   r   r   rc   filterr   r   dumpsrl  r8   )repro_partsr  sample_isolation_prefixr   	repro_str	repro_msgs         r:   print_repro_on_failurer  1
  sb     !      "$#3T::$&`=K^&`&`#HHVD+B*Q[*Q*QRRSS	#zSXXfT4K3Zk3Z3Z&[&[\\ 	
 	
 	
 	
NN N N	 qv;;!11i11?AF122J??AF;s   
 D CC;;D c                      d|v r*t           j        j        dk    r|                    d           t          j        | i |S )Nmin_satisfying_examples)r  8   r   )
hypothesisr  __version_info__popsettingsr7   s     r:   r  r  [
  sG    $..:3E3VZd3d3dJJ0111"D3F333r;   
pytorch_ci2   )derandomizesuppress_health_checkdatabasemax_examplesro  dev
   )r  r  r  ro  debugi  PYTORCH_HYPOTHESIS_PROFILEzEFail to import hypothesis in common_utils, tests are not derandomizedr   c                 |   ddl m} d  |            D             }d t                      D             }|                     d          }t	          |          dk    ra|d         |v rWt	          |          dk    r'|d         |v rd                    |dd                   S d                    |dd                   S | S )	Nr   )get_device_type_test_basesc                     g | ]	}|j         
S rB   )r  r  s     r:   rb   z4remove_device_and_dtype_suffixes.<locals>.<listcomp>
  s    KKKq}KKKr;   c                 V    g | ]&}t          |          t          d           d         'S )ztorch.N)rl   rl  )ra   dts     r:   rb   z4remove_device_and_dtype_suffixes.<locals>.<listcomp>
  s-    III"c"ggc(mmnn-IIIr;   rs   rs  ri  r  )*torch.testing._internal.common_device_typer  r-   rI  rl  rc   )r   r  device_suffixesdtype_suffixestest_name_chunkss        r:    remove_device_and_dtype_suffixesr  
  s    UUUUUUKK.H.H.J.JKKKOII8H8HIIIN s++
q  %5b%9^%K%K  1$$)9")=)P)P88,QrT2333xx(2.///r;   r   c                    	
 t           j                                      d          d                             d          d         	t           j                  dt           f	 fd
t          
fdt                                          D                       r8dt            j                  j	        d	<   t          st          j        d
          t          sd}d}t                                          D ][\  }\  }} 
|          rFt           t           t"          t"          t$          t&          t(          t*          t,          t,          t.          t          dt1          t3          fd|                    }t5          |          dk    r|d                    |          }d                                                              }t9          d| dd| d| ddd|            t1          t3          fd|                    }|g k    st          fd|D                       r+d}d| d|g k    rdnd dd                    |           d} n]|rt:          st          j        |          |st:          rd}t          j        |          t<          rWt?            j                  rDt            j                  j	                             d	d          st          j        d           d S d S d S )!NrI   ri  r   rs  r  c                     |                                  }t          |          dk     rdS |d         }|d         dd                              d          d         }                    |          o
|j        fv S )Nr   Fr   ri  rs  r   )rI  rl  r   _testMethodName)r  target_test_partstarget_testnametarget_classnamer,  sanitized_testnamer   s       r:   matches_testz%check_if_enable.<locals>.matches_test
  s    "LLNN !!A%%5+A.,Q/"5;;C@@D ##$455y?tOcewNx;xyr;   c              3   .   K   | ]} |          V  d S r2   rB   )ra   r   r  s     r:   r   z"check_if_enable.<locals>.<genexpr>
  s+      
;
;q<<??
;
;
;
;
;
;r;   Trc  rb  Fr   )macmacoswinwindowsr  rocmr   asandynamodynamo_wrappedinductorslowc                     | vS r2   rB   r  platform_to_conditionals    r:   rE   z!check_if_enable.<locals>.<lambda>
  s    !CZ:Z r;   r   , zTest z# is disabled for some unrecognized zplatforms: [z]. Please edit issue z to fix the platforms zLassigned to this flaky test, changing "Platforms: ..." to a comma separated zDsubset of the following (or leave it blank to match all platforms): c                     | v S r2   rB   r  s    r:   rE   z!check_if_enable.<locals>.<lambda>
  s    a;R6R r;   c              3   (   K   | ]}|         V  d S r2   rB   )ra   r   r  s     r:   r   z"check_if_enable.<locals>.<genexpr>
  s*      )f)fPX*A(*K)f)f)f)f)f)fr;   z7Test is disabled because an issue exists disabling it:  for allzplatform(s) z. If you're seeing this on your local machine and would like to enable this test, please make sure CI is not set and you are not using the flag --import-disabled-tests.zcTest is enabled but --rerun-disabled-tests verification mode is set, so only disabled tests are runz8test is fast; we disabled it with PYTORCH_TEST_SKIP_FAST)!rl   r]  rI  r  r  anyru  r  r   rd  r  r   r  rt   rx  rd   IS_MACOSr  r"  r  r  r  r  r  r  r  rl  rc   r  r}  r  r   r  )r   should_skipskip_msgdisabled_test	issue_url	platformsinvalid_platformsinvalid_plats_strvalid_platsr,  r  r  r  s   `        @@@@r:   check_if_enabler%  
  s   DN##))#..q177<<R@I9$:NOO
zS 
z 
z 
z 
z 
z 
z 
z 
z 
;
;
;
;O$8$8$:$:
;
;
;;; dDHd*++4[A 	d#$bccc 2.5H5N5N5P5P $	 $	1M1Iy|M** ##%%)%*#*3&; 7*1 1' %)0Z0Z0Z0Z\e)f)f$g$g!())A--(,		2C(D(D%"&)),C,H,H,J,J"K"KKT-TTTr):rrQZrrrhnalnnp p p !%V,R,R,R,RT]%^%^ _ _I??c)f)f)f)f\e)f)f)f&f&f?"&K qYb  q  q)2bb q  qNRiiXaNbNb q  q  qH E 	.3 	.#H--- 	.3 	.*H#H--- `4-.. 	`wtTEY7Z7Z7c7g7ghsuz7{7{ 	`#$^___` `	` 	` 	` 	`r;   c                   J     e Zd ZdZ edd          j        Zd Z fdZ xZ	S )RelaxedBooleanPairzPair for boolean-like inputs.

    In contrast to the builtin :class:`BooleanPair`, this class also supports one input being a number or a single
    element tensor-like.
    r   c                :    t           j        t          j        f}g  j         j        |R }t          | j                  rt          ||          s9t          | j                  rt          ||          s                                   fd||fD             S )Nc                 >    g | ]}                     |           S r   )_to_boolra   inputr   r   s     r:   rb   z6RelaxedBooleanPair._process_inputs.<locals>.<listcomp>
  s)    LLLe++LLLr;   )r3   r   r  ndarray_supported_types_supported_number_typesr   _inputs_not_supportedr   rn  expectedr   tensor_or_array_typesother_supported_typess   `  `  r:   _process_inputsz"RelaxedBooleanPair._process_inputs
  s     49<2L o$"7 o$:V oYn o o 566	);EhPe;f;f	)8T%:;;	)@J6Sh@i@i	) &&(((LLLLL&(9KLLLLr;   c                P   t          |t          j                  r!t          |                                          S t          |          | j        v rt          |          S t          |t          j        t          j	        f          r}t          |t          j                  r|
                                n|j        }|dk    r!|                     t          d| d|           t          |                                          S t                                          ||          S )Nri  zHOnly single element tensor-likes can be compared against a boolean. Got  elements instead.r+  )r   r  numberrq   rG  r   r1  r3   r   r/  numelr  _failrn  r\  r,  )r   	bool_liker   r;  r]  s       r:   r,  zRelaxedBooleanPair._to_bool
  s   i++ 	6	(())))__ <<<	??"	EL"*#=>> 	6)3Iu|)L)L`IOO%%%R[R`Eqyy

5 5 5 5	     	(()))77##I"#555r;   )
rf   rg   rh   r  r)   r0  r1  r7  r,  rk  rl  s   @r:   r'  r'  
  sk         
 )jA..?M M M6 6 6 6 6 6 6 6 6r;   r'  c                   n     e Zd ZdZeej        eej        e	ej
        iZdddd	 d	 fdZd Z fdZ xZS )
RelaxedNumberPaira#  Pair for number-like inputs.

    In contrast to the builtin :class:`NumberPair`, this class also supports one input being a single element
    tensor-like or a :class:`enum.Enum`. (D)Type checks are disabled, meaning comparing 1 to 1.0 succeeds even when
    ``check_dtype=True`` is passed.

    In addition, this class uses looser default tolerances for :class:`float` and :class:`complex` inputs. Also
    supports overriding the absolute and relative tolerance through the ``@precisionOverride`` and
    ``@toleranceOverride`` decorators.
            N)rtol_overrideatol_overridecheck_dtyper\   c                     t                      j        ||fddi| t          | j        |          | _        t          | j        |          | _        d S )NrC  Fr\  r   maxrtolatol)r   rn  r4  rA  rB  rC  other_parametersr]  s          r:   r   zRelaxedNumberPair.__init__   sV     	QQuQ@PQQQ	=11		=11			r;   c                ,    t           j        t          j        f}g  j        |R }t          | j                  rt          ||          s9t          | j                  rt          ||          s                                   fd||fD             S )Nc                 >    g | ]}                     |           S r*  )
_to_numberr-  s     r:   rb   z5RelaxedNumberPair._process_inputs.<locals>.<listcomp>2  s)    NNN%"--NNNr;   )r3   r   r  r/  r0  r   r2  r3  s   `  `  r:   r7  z!RelaxedNumberPair._process_inputs'  s     49<2L P$"7 P:O P PFD$9::	)?I(Ti?j?j	)x)>??	)DNvWlDmDm	) &&(((NNNNNFH;MNNNNr;   c                   t          |t          j        t          j        f          rt          |t          j                  r|                                n|j        }|dk    r!|                     t          d| d|           |	                                }t          |t                    rt          |          }|S t          |t                    rt          |          S t                                          ||          }t          |          | j                                        vr|                                  |S )Nri  zGOnly single element tensor-likes can be compared against a number. Got r9  r+  )r   r3   r   r  r/  r;  r  r<  rn  rG  rq   r   r	   r\  rL  r   _TYPE_TO_DTYPEr  r2  )r   number_liker   r;  r:  r]  s        r:   rL  zRelaxedNumberPair._to_number4  s2   kEL"*#=>> 	+5k5<+P+PfK%%'''VaVfEqyy

5 5 5 5	     !%%''F&$'' %VMT** 	{###WW''';;FF||4#6#;#;#=#===**,,,Mr;   rI  )rf   rg   rh   r  r   r3   int64rT  float32complex	complex64rN  r   r7  rL  rk  rl  s   @r:   r?  r?    s        	 	 	U[u}N 69Y]2 2	2 2 2 2 2 2O O O        r;   r?  c                   0     e Zd ZdZddd fd
Zd Z xZS )TensorOrArrayPaira  Pair for tensor-like inputs.

    On the one hand this class is stricter than the builtin :class:`TensorLikePair` since it only allows instances of
    :class:`torch.Tensor` and :class:`numpy.ndarray` rather than allowing any tensor-like than can be converted into a
    tensor. On the other hand this class is looser since it converts all inputs into tensors with no regard of their
    relationship, e.g. comparing a :class:`torch.Tensor` to :class:`numpy.ndarray` is fine.

    In addition, this class supports overriding the absolute and relative tolerance through the ``@precisionOverride``
    and ``@toleranceOverride`` decorators.
    r@  rA  rB  c                     t                      j        ||fi | t          | j        |          | _        t          | j        |          | _        d S r2   rE  r   rn  r4  rA  rB  rI  r]  s         r:   r   zTensorOrArrayPair.__init__W  sO    >>-=>>>	=11		=11			r;   c                                           ||t          j        t          j        f            fd||fD             \  }}||fD ]}                     ||           ||fS )NrN  c              3   B   K   | ]}                     |          V  d S r2   )
_to_tensor)ra   r.  r   s     r:   r   z4TensorOrArrayPair._process_inputs.<locals>.<genexpr>_  s/      SSuDOOE22SSSSSSr;   r+  )_check_inputs_isinstancer3   r   r  r/  _check_supported)r   rn  r4  r   allow_subclassesr  s   `     r:   r7  z!TensorOrArrayPair._process_inputs\  s    %%fhU\2:<V%WWWSSSS@RSSSx( 	1 	1F!!&R!0000xr;   )rf   rg   rh   r  r   r7  rk  rl  s   @r:   rU  rU  L  sb        	 	 ;>S 2 2 2 2 2 2 2
             r;   rU  c                   0     e Zd ZdZddd fd
Zd Z xZS )TypedStoragePairz4Pair for :class:`torch.storage.TypedStorage` inputs.r@  rV  c                    |                      ||t          j        j                    t	                      j        ||fi | t          | j        |          | _        t          | j        |          | _        d S NrN  )	r\  r3   r  TypedStorager\  r   rF  rG  rH  rX  s         r:   r   zTypedStoragePair.__init__g  so    %%fhEM<V%WWW>>-=>>>	=11		=11			r;   c                 \   t          j        |j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j	        t           j
        i                    |j        |j                  |j                  S )NrP  rR  )r3   r  _untyped_storagequint8uint8quint4x2quint2x4qint32int32qint8int8r  rP  rR  )r   typed_storages     r:   r[  zTypedStoragePair._to_tensorm  sp    |*ekekUZ c-%}':;; '

 

 

 
	
r;   )rf   rg   rh   r  r   r[  rk  rl  s   @r:   r`  r`  e  s\        >>:=S 2 2 2 2 2 2 2
 
 
 
 
 
 
r;   r`  c                   n     e Zd ZU dZeeeedf         f         ed<   dZe	e
         ed<    fdZd Z xZS )UnittestPairav  Fallback ABC pair that handles non-numeric inputs.

    To avoid recreating the mismatch messages of :meth:`unittest.TestCase.assertEqual`, this pair simply wraps it in
    order to use it with the :class:`Pair` "framework" from :func:`are_equal`.

    Define the :attr:`UnittestPair.CLS` in a subclass to indicate which class(es) of the inputs the pair should support.
    .CLSN	TYPE_NAMEc                 x    |                      ||| j                    t                      j        ||fi | d S rb  )r\  rr  r\  r   )r   rn  r4  rI  r]  s       r:   r   zUnittestPair.__init__  sH    %%fhDH%EEE>>-=>>>>>r;   c                    t          j                    }	 |                    | j        | j                  S # |j        $ r}t          |          }Y d }~nd }~ww xY w| j        p2t          | j	        t                    r| j	        n| j	        d         j        }|                     t          |                                 d|            d S )Nr   z comparison failed: )r   r   assertEqualrn  r4  failureExceptionrl   rs  r   rr  r   rf   r<  AssertionErrortitle)r   r  r  r)  	type_names        r:   comparezUnittestPair.compare  s    %''		((dmDDD) 	 	 	e**CCCCCC	 Nh:dh3M3M'^txxSWS[\]S^&h	

>ioo&7&7#R#RS#R#RSSSSSs   5 
AAA)rf   rg   rh   r  r   r   r-  rj   rs  r   rl   r   r{  rk  rl  s   @r:   rq  rq  {  s           
tU49%%	&&&&#Ix}###? ? ? ? ?	T 	T 	T 	T 	T 	T 	Tr;   rq  c                       e Zd ZeefZdZdS )
StringPairstringN)rf   rg   rh   rl   bytesrr  rs  rB   r;   r:   r}  r}    s        ,CIIIr;   r}  c                       e Zd ZeZdS )SetPairN)rf   rg   rh   r  rr  rB   r;   r:   r  r    s        
CCCr;   r  c                       e Zd ZeZdS )TypePairN)rf   rg   rh   r   rr  rB   r;   r:   r  r    s        
CCCr;   r  c                       e Zd ZeZdS )
ObjectPairN)rf   rg   rh   rm  rr  rB   r;   r:   r  r    s        
CCCr;   r  c                        e Zd Z fdZ xZS ),AssertRaisesContextIgnoreNotImplementedErrorc                     |2t          |t                    r| j                            d|            t	                                          |||          S )Nznot_implemented: )
issubclassr  r  skipTestr\  r  )r   r  r  tbr]  s       r:   r  z5AssertRaisesContextIgnoreNotImplementedError.__exit__  sT    Jx9L$M$MN##$C	$C$CDDDww)R888r;   )rf   rg   rh   r  rk  rl  s   @r:   r  r    s8        9 9 9 9 9 9 9 9 9r;   r  new_valc              #      K   t          j                    }t          j        |            	 d V  t          j        |           d S # t          j        |           w xY wr2   )r3   is_warn_always_enabledset_warn_always)r  old_vals     r:   set_warn_always_contextr    sa      *,,G	'"""'g&&&&&g&&&&r  c                       e Zd ZdZdS )NoTestFN)rf   rg   rh   __test__rB   r;   r:   r  r    s        HHHr;   r  c            
           e Zd ZU dZeed<   dZeed<   dZeed<   e	j
        ZdZd Zedefd	            Zej        d
eddfd            Zedefd            Zej        d
eddfd            ZdZdZdZdS fd	ZdTdZd Zd Zd Zd ZdU fd	ZdddddZdV fd	ZdV fd	Zd Zd Z d Z!d Z"d Z#d  Z$dT fd!	Z%dTd"Z&d# Z'd$ Z(e)dd%d&            Z*d'dd(d)Z+dd*d+Z,dd*d,Z-dd*d-Z.dd*d.Z/d/ Z0	 	 	 	 	 	 	 	 	 	 	 	 	 dWd0Z1d1 Z2d2 Z3	 dVd3Z4dXd4Z5dXd5Z6	 dTddddddddd6d7e7e8e9e:e9ge9f         f                  d8e7e         d9e7e         fd:Z;dTddd;d7e7e9         d8e7e         d9e7e         ddfd<Z<dXd=Z=d>e>d?e?e>         ddfd@Z@ fdAZA fdBZBdC ZCdD ZDdYdFZEeFdYdG            ZGdTdHZHdTdIZIdZdJZJdYdKZKd[dLZLe)dTdM            ZMe)dN             ZN	 dTdOeOjP        dPe9dQe7e9         ddfdRZQ xZRS )\r   r   
_precision_rel_tolF_default_dtype_check_enabledNc                 <   t           j                                        r}	 t           j                                         n[# t          $ rN}t          dt          j                   t          t          |          t          j                   Y d }~dS d }~ww xY wdS dS )NzDTEST SUITE EARLY TERMINATION due to torch.cuda.synchronize() failurer  TF)	r3   r   is_initializedr  r  r  r  r  rl   )r   rtes     r:   _should_stop_test_suitez TestCase._should_stop_test_suite  s    :$$&& 	
&&((((   \cfcmnnnnc#hhSZ0000ttttt 55s   ? 
B	ABBr\   c                     | j         S r2   r  r   s    r:   	precisionzTestCase.precision  s
    r;   precc                     || _         d S r2   r  r   r  s     r:   r  zTestCase.precision  s    r;   c                     | j         S r2   r  r   s    r:   rel_tolzTestCase.rel_tol  s
    }r;   c                     || _         d S r2   r  r  s     r:   r  zTestCase.rel_tol  s    r;   runTestc                    |dk    r|}t                                          |           t          | |d           }|t          rH| xj        t          |dd          z  c_        | j        r"t
          s|                     || j                   | xj        t          |dd          z  c_        | j        r"t
          s|                     || j	                   | j
        r|                     |d            t          r	 d }t          j        t          |                     } ||          }t          |           j        }d| d| d	| }t"                                          }	|	|g}
|                     ||
fd
           d S # t&          $ r4}t(                              dt-          |                     Y d }~d S d }~ww xY wd S d S )Nr  rk  Trr  c                  *    t          t                    S r2   )r  r  rB   r;   r:   rE   z#TestCase.__init__.<locals>.<lambda>  s    ;NOb;c;c r;   c                 6   t          |           j        }t          |          D ]Q\  }}|dk    rF|dk    rt          j        j        |d |          nd}t          j                            | |          c S Rt          j                            |           d         S )Nr   r   r   )startri  )r   partsr   rK   r   rc   relpathrI  )abs_test_pathr  r   partbase_dirs        r:   _get_rel_test_pathz-TestCase.__init__.<locals>._get_rel_test_path  s     !%] 3 3 9'0'7'7 V VGAt#v~~GH1uu27<rr+C+CRT')w}H'U'U U U U  .  "w}}];;A>>r;   zpython r^   r   c                      t          |           S r2   )r  )r  s    r:   rE   z#TestCase.__init__.<locals>.<lambda>-  s    8N{8[8[ r;   zcould not print repro string)extra)r\  r   r   rt  rk  r  wrap_with_cuda_policyassertLeaksNoCudaTensorsrr  enforceNonDefaultStream_ignore_not_implemented_errorwrap_with_policyr   r   getfiler   rf   r=   re   r   r   inforl   )r   method_name
methodNametest_methodr  r  r&  
class_nametest_run_cmdenv_var_prefixr  r  r]  s               r:   r   zTestCase.__init__  s=    ""$K%%%dK66"' [//7;Hdfj3k3kk//2 [: [..{D<YZZZ ,,Ebdh0i0ii,,/ V
 V**;8TUUU1 e%%k3c3cddd% KK? ? ? %,ODJJ$?$?M$6$6}$E$EM!%d!4J#W]#W#WZ#W#W+#W#WL%4%I%I%K%KN#1<"@K))#+6[[[] ] ] ] ] ! K K KHH;3q66HJJJJJJJJJKS #" K Ks   <BF 
G )F;;G c                 R    ||                                  n|}t          | |          S r2   )r   r  )r   rN   s     r:   r  z!TestCase.assertLeaksNoCudaTensors2  s'     Ltwwyyyd"4...r;   c                     t                      S r2   )r  r   s    r:   r  z TestCase.enforceNonDefaultStream6  s    #%%%r;   c                 l    t          j        dt           j                  }|                    d|          S )NaF  
            \x1B  # ESC
            (?:   # 7-bit C1 Fe (except CSI)
                [@-Z\\-_]
            |     # or [ for CSI, followed by a control sequence
                \[
                [0-?]*  # Parameter bytes
                [ -/]*  # Intermediate bytes
                [@-~]   # Final byte
            )
        r   )r  compileVERBOSErq  )r   r.  ansi_escapes      r:   _remove_ansi_escapezTestCase._remove_ansi_escape9  s5    j 
" Z
 
 r5)))r;   c                 n    |                     d          }d |D             }d                    |          S )Nr  c                 `    g | ]+}|                                                     d           )|,S )#)rE  r   ra   lines     r:   rb   z1TestCase.remove_comment_lines.<locals>.<listcomp>J  s4    UUU4

8O8OPS8T8TU$UUUr;   rI  rc   r   input_stringr  filtered_liness       r:   remove_comment_lineszTestCase.remove_comment_linesH  s:    ""4((UU5UUUyy(((r;   c                 n    |                     d          }d |D             }d                    |          S )Nr  c                 B    g | ]}|                                 d k    |S rC  rD  r  s     r:   rb   z/TestCase.remove_empty_lines.<locals>.<listcomp>O  s*    KKK4

8J8J$8J8J8Jr;   r  r  s       r:   remove_empty_lineszTestCase.remove_empty_linesM  s:    ""4((KK5KKKyy(((r;   c                    t          |t                    r|nt          |          }|                     |          }|                     |          }|r*|                     |          }|                     |          }|r*|                     |          }|                     |          }t                                          t          |t                    r|nt          |          ||dz             S Nri  )r   rl   r  r  r  r\  assertExpectedInline)r   rn  expectr_  ignore_commentsignore_empty_linesr]  s         r:   r  zTestCase.assertExpectedInlineS  s    %fc22CF))&11))&11 	7..v66F..v66F 	5,,V44F,,V44Fww++j6M6M,^FFSVW]S^S^`fhlophpqqqr;   T)r_  suppress_suffix
post_mungec                    	  |             nP# |$ rH}t          |||dz             }|r ||          }|                     |||dz              Y d }~d S d }~ww xY w|                     d           d S )Nri  )r  r_  )r_  Did not raise when expected tor^  )	munge_excr  fail)	r   r  r  r  r_  r  r  r  mungeds	            r:   assertExpectedInlineMungedz#TestCase.assertExpectedInlineMungedb  s    		HJJJJ 	 	 	q/qQQQF ,#F++%%TAX &    FFFFF	 			6	77777s   
 A=AAc                 t    |t          j        d          }t                                          ||          S Nr3   )logging	getLoggerr\  
assertLogsr   loggerlevelr]  s      r:   r  zTestCase.assertLogsq  s2    >&w//Fww!!&%000r;   c                 t    |t          j        d          }t                                          ||          S r  )r  r  r\  assertNoLogsr  s      r:   r  zTestCase.assertNoLogsv  s2    >&w//Fww##FE222r;   c                     t          | |          }ddlm} |                                                                 }|r/d|v sd|v r)t          | ||                     ||                     d S d S d S )Nr   )	TEST_CUDAgpur   )r   r  r  r   lowerr*  wrap_method_with_policy)r   r  policyr  r  fullnames         r:   r  zTestCase.wrap_with_cuda_policy{  s    dK00 	BAAAAA7799??$$ 	Z%8++v/A/AD+t'C'CKQW'X'XYYYYY	Z 	Z/A/Ar;   c                 p    t          | |          }t          | ||                     ||                     d S r2   )r   r*  r  )r   r  r  r  s       r:   r  zTestCase.wrap_with_policy  s9    dK00k4#?#?V#T#TUUUUUr;   c                 f    t                    fd            }t          j        ||           S )Nc                 ^                 5   |i | d d d            d S # 1 swxY w Y   d S r2   rB   )r   r8   r9   methodr  s      r:   r   z1TestCase.wrap_method_with_policy.<locals>.wrapper  s     ( (''''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   	"&&)r   r  
MethodType)r   r  r  r   s    `` r:   r  z TestCase.wrap_method_with_policy  sI     
v	( 	( 	( 	( 	( 
	( ...r;   c                 8    |                      || j                  S r2   )r  r  )r   r  s     r:   wrap_with_cuda_memory_checkz$TestCase.wrap_with_cuda_memory_check  s    ++FD4QRRRr;   c                 .    | j         j         d| j         S )Nr   )r]  rf   r  r   s    r:   _dynamo_test_keyzTestCase._dynamo_test_key  s    .)BBD,@BBBr;   c                 T    t          j                            ||          |          S )N)rZ  )r3   r?  optimize)r   r   r  rZ  s       r:   
compile_fnzTestCase.compile_fn  s%    }%%g%AA"EEEr;   c           
      B
    t          |t          j                  }t                      j        }|j        }t          pt          pt          }d}d}d}|r	 t          j
        t          |                    }	t          j                            |	          }
t          j        d|
          }|3|                    d          }t          rddlm} ||v}d}|dk    rd}nd}n# t(          t*          f$ r Y nw xY wdt          j        v rt          j        d         dk    rd}d}|rGt/            j                  }t3          |d	          r|j        }nt3          |d	          r|j        }n|}t/          |d
d          o|}|s|rt6          j                                         t6          j                            d           |r| }nt6          j        j         j!        }|r%t6          j"        j         #                    d          ntI          j%                    }t          j&        #                    d|          5  |5  t          r '                    |d|          }n|t          st          rmt          r '                    |d|          }n '                    |d|          } (                                } fd}t          r	d}ddlm)} nd}ddlm*} ||v rUt/            j                  }t          j        +                    ||          }tY            j         |||                      fd}t          r	d}ddlm-} nd}ddlm.} ||v rUt/            j                  }t          j        +                    ||          }tY            j         |||                     ddlm/} t6          j        j         j0        r||v r tc                      |          } ||           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |s|rt6          j                                         n9t6          j        j         j0        r#t6          j        j0                                         |r 2                                r|3                                ruti                      }tj          ,ddl6m7}  |||          }|j8        |_9        d|_:        d |_;        |j<        =                    |d!f           |3                                du sJ |>                                 d S d S d S )"NFz.*/test/(.*).pyri  )FIXME_inductor_non_strictTtest_opsSTRICT_DEFAULTrA   rM  rX  rD   )size_assertsrS  aot_eager_decomp_partitionr  eager_noexceptc                 D     t                      fd            }|S )Nc                      	  | i | n,# t           $ r}                    |           Y d }~nd }~ww xY wt          d d          )Nz#Unexpected success, please remove ``)BaseExceptionr  r  )r8   r9   r  r  	file_namer   s      r:   r   z=TestCase._run_custom.<locals>.expect_failure.<locals>.wrapper  s{    -At.v...., - - - MM!,,,,,,,,-*+]QZ+]+]+]^^^    
505r  r  r  r   r   s   `` r:   expect_failurez,TestCase._run_custom.<locals>.expect_failure  sF    1XX_ _ _ _ _ _ X_ #Nr;   ztest/inductor_expected_failures)inductor_expected_failuresztest/dynamo_expected_failures)dynamo_expected_failuresc                 D     t                      fd            }|S )Nc                      	  | i | n,# t           $ r}                    |           Y d }~nd }~ww xY wt          j                  }t          |dd          r                    d           d S                     d d           d S )N__unittest_expecting_failure__Fzunexpected successz'This test passed, maybe we can remove `r
  )r  r  r   r  )r8   r9   r  r  r  r  r   s       r:   r   z=TestCase._run_custom.<locals>.ignore_failure.<locals>.wrapper  s    -At.v...., - - - MM!,,,,,,,,-!(t/C!D!D"6+KUSS b MM*>????? MM*`T]*`*`*`aaaaar  r  r  s   `` r:   ignore_failurez,TestCase._run_custom.<locals>.ignore_failure  sF    1XX	b 	b 	b 	b 	b 	b X	b #Nr;   ztest/inductor_skips)inductor_skipsztest/dynamo_skips)dynamo_skips)compiled_autograd_skipsr  r   )	_TestInfor@  TestSuiteEarlyFailurez%TestSuite execution was aborted early)?r   r   
TestResultr\  run__self__r  r
  r  r   r  r   rK   r   abspathr  matchr5  dynamo_test_failuresr  OSErrorr  r  r   r  r   rM  r3   r?  rT  compiler
set_stancerC  suppress_errors	_inductorrV  
contextlibnullcontextrU  r   r  r  r  rc   r*  r  r  r  r@  rC  r  r  r   r   r  r  ERRORrn  elapsed_timetest_descriptionfailuresrB  stop)r   r  using_unittest	super_runtest_clscompiledstrict_defaultshould_reset_dynamoshould_disable_size_assertsr   	full_pathr   r   r  strict_moder  rZ  r%  maybe_disable_size_assertsr  r  subdirexpected_failuresr  r  r  skipsr  r  r  r]  s   `                             r:   _run_customzTestCase._run_custom  s|   #FH,?@@GGK	% )Z,?ZCZ# ',# 	*tH~~66GOOD11	!3Y??$${{1~~H. .SSSSSS)19R)R.2+#z11:>7)-Y'   2:--:./366%)N 	-!$(<==K{O44 -)7?33 -&4,8%=uEER( 	"- 	"M!!!!!),,,
  	C"-oOO#m2BO +*EO"((e(<<<')) 	# ]  !GYY B	% B	%[u B	% B	%" ?H OOI7SU]^^		& =H*A =H* W $	:x P PII !%	;KX V VI++--# # # # # + d>Feeeeeee<Fcccccc+++$T4+?@@F "VS 9 9ID$"6vy8Y8YZZZ# # # # # + L2FMMMMMMM0FKKKKKK%<<$T4+?@@F "VS 9 9ID$"6vy8Y8YZZZIIIIII='9 HcE\>\>\ < : < <Y G GIIV$$$$EB	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	% B	%H  	4- 	4M!!!!]!3 	4M+11333  	d::<< 	##%% 7zz , ;:::::$9VT22D"+/DK(+D%,CD) &&.U'VWWW++--6666KKMMMMM	 	 	 	sJ   B
C   C43C42P5F)O*P*O.	.P1O.	2PPPc                     t          j                    5 }t          r!|                    t	                                 |                     |           d d d            d S # 1 swxY w Y   d S )Nr  )r'  	ExitStackr  enter_contextr  r;  )r   r  r   s      r:   r  zTestCase.runJ  s    !## 	u! 4##LNN333    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ?A  A$'A$c                 f   t          |            t          t                     t          j        j                                        | _        t          j        j                                         | j	        r#t          j
                    t          j        k    sJ t          j                    | _        d S r2   )r%  r   r   r3   sparsecheck_sparse_tensor_invariants
is_enabled_check_invariantsenabler  r  rT  is_grad_enabled_prev_grad_stater   s    r:   setUpzTestCase.setUpR  s    T "'!L!W!W!Y!Y 	3::<<<, 	<*,,;;;; !& 5 7 7r;   c                 l   t          | d          rN| j        r$t          j        j                                         n#t          j        j                                         | j        r#t          j                    t          j	        k    sJ t          | d          rt          j
        | j                   d S d S )NrC  rF  )r   rC  r3   r@  rA  rD  disabler  r  rT  set_grad_enabledrF  r   s    r:   tearDownzTestCase.tearDownh  s     4,-- 	F% F;BBDDDD;CCEEE, 	<*,,;;;; 4+,, 	:"4#899999	: 	:r;   )r  c                H    d|cxk    r	 z  k    sn J | f             fd}t          j         dz   |t          j        d                    }dx}}	 |||	          }
|
r|t          |
          k    r|} dz
  } |||	          }||z
  dk    rD||z   dz  } |||	          }|dk    s||z  z
  t          |          k     r||}}n|}||z
  dk    D||}
}|sJ || d                                        |
r||z  z
  t          |
 |z
            k    r|	}dz
  } |||          }||z
  dk    rP||z   dz  } |||          }|dk    s&||z  z
  | |z
  z  z
  t          | |z
            k     r||}}n|}||z
  dk    P||}
}	|	sJ |d |z
  dz                                |	           |
rt          ||z  z
  |	 |z
  z  z
  |	z
  |	z
  dz   z  dz            \  }}d||	z
  dz   z  z   }t          j        d|z            }||dz   z  d|z  k    r|dz  }|||dz   z  dz  z
  }|dk    r|	dk    rJ t          j        |dz
  ||j                  |	z
  dz   z  |d|<   ||||z   dz   xx         t          j        |dz   ||j                  z  cc<   nd}||z  z
  |	 |z
  z  z
  }||xx         |z  cc<   |r0t          j	         |j                  }|dd         |         |dd<   |}|
                    d	           |                    |          S )
a  Return crow_indices of a CSR tensor with size (n_rows, n_cols) and
        the number of specified elements nnz.

        If random is True, the column counts of rows are in random
        order. Otherwise, the column counts of rows are defined by the
        used sampling method.

        Sampling method
        ---------------

        The used sampling method was introduced in
        https://pearu.github.io/csr_sampling.html, and here we give
        only an overall description of the method.

        Notice that crow_indices can be defined as cumsum(counts)
        where counts is a sequence of non-negative integers satisfying
        the following conditions:

          len(counts) == n_rows + 1
          counts.max() <= n_cols

        while counts[i + 1] is interpreted as the number of specified
        elements in the i-th row.

        The used sampling method aims at increasing the diversity of
        CSR samples, that is, a CSR sample should contain (i) rows
        that are all filled, (ii) rows with no elements at all, and
        (iii) rows that are partially filled. At the same time and for
        the given total number of specified elements (nnz), there
        should be minimal preference to rows with a given number of
        elements.  To achieve this, the sampling method is built-up on
        using a sawteeth model for counts. In the simplest case, we
        would have

          counts = arange(n_rows + 1) % (n_cols + 1)

        that has equal number of all possible column counts per row.
        This formula can be used only for specific input values of
        n_rows, n_cols, and nnz. To generalize this model to any
        combinations of inputs, the counts model above is extended
        with an incomplete sawtooth, and the right and lower
        rectangular parts that will guarantee that

          counts.sum() == nnz

        for any combination of n_rows, n_cols, and nnz. Basically,
        we'll find a maximal window in (n_rows + 1, n_cols + 1)-grid
        that is able to hold a sequence of sawteeth and so-called
        final correction, while the external part of the window is
        filled with counts to meet the nnz constraint exactly.
        r   c                 |    |z
  |z
  dz   z  dz  }| z
  |z
  dz   z  }|| z
  |z
  dz   z  z  ||dz
  z  dz  z   S )Nri  r   rB   )nmMKn_colsn_rowss       r:   sawteethz-TestCase._make_crow_indices.<locals>.sawteeth  sd    
 !
Q/14A!
Q/A!!a89AQK1<LLLr;   ri  r   re  r   Nr  )r  )r3   zerosrR  rF  fill_divmodmathisqrtarangerandpermcumsum_r  )rS  rR  nnzrR  rP  r  rT  countsrN  rO  Nn_leftn_rightN_rightn_middleN_middlem_leftm_rightm_middleqr  r  kcorrpermcrow_indicess   ``                        r:   _make_crow_indiceszTestCase._make_crow_indicesz  s   l C****6F?*****S&&,A***	M 	M 	M 	M 	M 	M VaZuU\%=P=PQQQ	AHQNN 	&Av&& FqjGhw**GF"Q&&"W,2#8Ha00q==C(V*;$;c(F>S>S$S$S'/WGG%F F"Q&& GqAHH1A233Kf%%% 	.q6z!SFQJ%7%777 FqjGhq'**GF"Q&&"W,2#8Ax00q==C!f*$4x6A:7N$NQTU]_ehi_iQjQj$j$j'/WGG%F F"Q&& GqAHH11VaZ!^#$**1--- 	7#F
*Q&1*-==!A:&1*q.9Q>@ @DAqA!a((A
1q5!!AAE{QU""QqAE{a''DQQUUU,,q1uE&-PPPTZ]^T^abTbcF1Q3K1QUQY;5<AU6=#Y#Y#YY AV#a6A:&66D 	q			T			 	* >&???DD)F122J ###f---r;   rB   )	blocksize
dense_dimsc          	          ddl m}	 ddlm}
 d}t	          fdt          t                              D                       s|dk    s
J d            t                    |k    sJ rmt                    dk    sJ f            d|z
           d         z  dk    sJ f            d|z
           d	         z  dk    sJ f            \  }}nd	x}}t                    t                    |z
  d           fd
d d|z
           } |
|	|d	          }|t          j	        t          j
        hv rd|z
           |z  d|z
           |z  cnd|z
           |z  d|z
           |z  c|||z  z  fdt          |          D             }t          t          t          |           } t          j        t          |                    j        g |R  } t          j        t          |                    j        g |dR  } t          j        t          |                    j        g |dR  }t          j        ||||          S )Nr   )mul)reducer   c              3   0   K   | ]}|         d k    V  dS r   NrB   ra   r  r  s     r:   r   z5TestCase.genSparseCompressedTensor.<locals>.<genexpr>  s+      99147Q;999999r;   invalid argumentsr  rs  ri  c                                         | ||          }t          j        |          }t          |           D ]^}||dz            ||         z
  }t          j        t          j        |          d |                   \  |||         ||dz            <   }_t          j        k    rdnd}t          j        k    rdnd}	t          |fz   z   ||	          }
|
||fS )NrR  rP  re  ri  rs  r   r   rR  rP  lowhigh)rm  r3   rU  r
  sortr[  rh  r&   )n_compressed_dimsn_plain_dimsr]  compressed_indicesplain_indicesr   countrs   rz  r{  r]  rn  
dense_sizerR  rP  index_dtyper   s              r:   random_sparse_compressedzDTestCase.genSparseCompressedTensor.<locals>.random_sparse_compressed  s-   !%!8!89JLZ]flt!8  "A  "A!K;vNNNM,-- \ \*1q514Fq4IITYT^N<{6RRRSYTYSYZU\ U\Q034Fq1u4MMNPQPQ,,""!C,,11!D #)!3j!@W\belpqqqF-}<<r;   c                 *    g | ]}           S rB   rB   )ra   rs   blocknnzr}  r~  r  s     r:   rb   z6TestCase.genSparseCompressedTensor.<locals>.<listcomp>-  s,    vvvbc223DlT\]]vvvr;   )r  rP  layoutrR  )operatorrq  r   rr  r  r
  rl  r-  r3   
sparse_csr
sparse_bsrmapr  r\  r   r   reshapesparse_compressed_tensor)r   r  r]  r  rR  rP  r  rn  ro  rq  rr  
sparse_dim
blocksize0
blocksize1batch_shapen_batchsparse_tensorssparse_tensors_itr]  r  r  r  r  r}  r~  r  s   ``  ````             @@@@@r:   genSparseCompressedTensorz"TestCase.genSparseCompressedTensor  sH         $$$$$$
9999c$ii(8(899999ZSAXXXGZXXE4yyJ&&&& 	(y>>Q&&&y(9&&&Z(9Q<71<<<tY>O<<<Z(9Q<71<<<tY>O<<<%."J

&''JT{{3t99z1334

	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= +BO+,&k1--e&(8999.22
?.Cz.QSWXZ]gXgShlvSv+||.22
?.Cz.QSWXZ]gXgShlvSv+|:
23vvvvvvvglmtguguvvvc>&:;;=T"34455=n{nHnW`ncmnnnIU[.?)@)@AAI[;[XZ[[[DD):$;$;<<DVkVSUVVV-.@-.44uU[djl l l 	lr;   )ro  c          
      P    |                      ||t          j        |||d|          S )NrB   r  rR  rP  r  rn  ro  )r  r3   r  r   r  r]  rR  rP  r  ro  s          r:   genSparseCSRTensorzTestCase.genSparseCSRTensor6  s9    --dC@PY_49{^`mw . y y 	yr;   c          
      P    |                      ||t          j        |||dd          S )NrB   r   r  )r  r3   
sparse_cscr  s          r:   genSparseCSCTensorzTestCase.genSparseCSCTensor:  s9    --dC@PY_49{^`mn . p p 	pr;   c          
      z    t          |          dk    sJ |                     ||t          j        |||||          S Nr   r  )rl  r  r3   r  r   r  rn  r]  rR  rP  r  ro  s           r:   genSparseBSRTensorzTestCase.genSparseBSRTensor>  P    9~~""""--dC@PY_49{^gt~ . @ @ 	@r;   c          
      z    t          |          dk    sJ |                     ||t          j        |||||          S r  )rl  r  r3   
sparse_bscr  s           r:   genSparseBSCTensorzTestCase.genSparseBSCTensorC  r  r;   c                    t          fdt          |          D                       s|dk    s
J d            |gt          |d                    z   }t          |||dd          }t	          j        |||          }	|	                    t	          j        d |                                       d          	                    |	                     |		                    t          j
                  }	|r>|	d d d |dz  d	f         }
|	d d d |dz   dz  d	f         }t	          j        |
|gd          }	t	          j        |	|t	          j                  ||
          }|s|                                }n9|                                                                                    d          }||                                                                |                                                                fS )Nc              3   0   K   | ]}|         d k    V  dS rt  rB   ru  s     r:   r   z+TestCase.genSparseTensor.<locals>.<genexpr>K  s+      ::147Q;::::::r;   r   rv  rs  ri  ry  r  r   .re  F)r  r
  r  r&   r3   randmul_r  	unsqueezer  longcatsparse_coo_tensorSizecoalescedetachclone_coalesced__indices_values)r   r  r  r]  is_uncoalescedrR  rP  v_sizer[  r   i1i2r   s    `           r:   genSparseTensorzTestCase.genSparseTensorH  s    ::::j(9(9:::::[cQhhhH[hhFd:;;/000vUKKKJz3v666	u|D*-..88;;>>qAABBBDD 	'111kqk3&'B111'qQ',-B	2r(A&&A#Aq%*T*:*:%PVWWW 		6

AA 

  ""..u55A!**,,$$&&		(9(9(;(;;;r;   c              #      "#$%&'()K   t           j        |t           j        t           j        t           j        t           j        hv }|r                      ||||||	|
||d          D ]\  }}|rt          d |D                       }|t           j        u rt          |          dk    sJ |
                    dd          }|J |r0|d                             |                                          V  |d                             |          V  |t           j        u rt          j        |i |V  |r*|                    |           t          j        |i |V  J dS d	 %d
 &%&fd'$'fd$#fd#|{g dg dgddgg dfg dg dgg dg dgg dg dggg dg dgg dg dgg dg dgggddgddgfg dg dg dg dg dg dg dg d gdgdd!gfdd"gd#d$ggddgddgggd%g|rdgng fg}d7 fd'	(|D ]\  }}}|sd( |D             }|r|st          j        |t           j        )          }|s|j        d"k    rH|D ]E} $||          |         }|D ],}fd*|dd&         D             } #|d&         |                              |+          }t)          ||j        |z   ,          }||                    |-           g ||R |                                fV  |
rm|j        d"k    rbd.D ]_\  ")"()fd/|D             }g ||R |                                fV  |r+ (|d&d0          }g ||R |                                fV  `|r+ (|d&d0          }g ||R |                                fV  .G|	rd1d2g|rg d3ndgfd4g d5dgfd6d2gdgffD ]\  }}}|D ]}|D ]}|t           j        k    rd}t          j        ||z   |+          }n|t           j        k    rCt          j        t          |          d+          f}t          j        dg|R |+          }n|t           j        k    r\t          j        dg|d         dz   z  +          }t          j        d+          }||f}t          j        dg|R |+          }nY|t           j        k    r[t          j        dg|d         dz   z  +          } t          j        d+          }!| |!f}t          j        dg|R |+          }n|t           j        k    rft          j        dg|d         |d         z  dz   z  +          }t          j        d+          }||f}t          j        dg||R |+          }nx|t           j        k    rft          j        dg|d         |d         z  dz   z  +          } t          j        d+          }!| |!f}t          j        dg||R |+          }nJ t)          |||z   ,          }||                    |-           g ||R |fV  dS dS )8a  Generator of simple inputs for tensor constructors of the given layout.

        The generated tensor inputs have the following properties:

        - tensor shapes are minimal but not trivial
        - tensor values are sorted sequences for COO and CSR formats, e.g. [1, 2, 3, 4]
        - the generated tensors represent the same mathematical tensor for all layouts
        - the generated tensors include regular, zero-sized, and optionally, batched or/and hybrid tensors.
        - the generated tensors include contiguous or non-contiguous tensors both in indices and values

        If output_tensor is True, yield tensors with the given
        layout. Otherwise, yield inputs to the corresponding tensor
        constructors:

          - sparse compressed input is defined as
            (compressed_indices, plain_indices, values), dict(size=expected_size_from_shape_inference, device=device, dtype=dtype,
                                                              pin_memory=pin_memory)

          - sparse COO input is defined as
            (indices, values), dict(size=expected_size_from_shape_inference, device=device, dtype=dtype, pin_memory=pin_memory)

          - strided input is defined as
            (values,), dict(device=device, dtype=dtype)
        NF)rR  rP  r  
pin_memoryenable_batchenable_hybridenable_zero_sizedenable_non_contiguous_indicesenable_non_contiguous_valuesenable_batch_variable_nseoutput_tensorc              3   >   K   | ]}|                                 V  d S r2   r  )ra   rZ  s     r:   r   z2TestCase.generate_simple_inputs.<locals>.<genexpr>  s*       > >A > > > > > >r;   ri  r  r   )r  c                    | j         }|d         |d         z  dk    sJ ||f            |d         |d         z  dk    sJ ||f            |                     d|d         |d         |d         z  |d                                       dd                              d                              d          }t	          j        d|                                dz                                 |j                   }|dk    |z  S )Nr   ri  rs  r  )shaper  	transposer  r3   rZ  r;  )patternrn  basesizeblockpattern	block_idss        r:   get_blockpatternz9TestCase.generate_simple_inputs.<locals>.get_blockpattern  s   }HA;1-222Xy4I222A;1-222Xy4I222"??2+4Q<+3A;)A,+F+4Q<9 9 :C2r9J9J33r77SVSVWYSZSZ  Q(:(:(<(<q(@AAII,J\]]I A%22r;   c                 4   | j         }t          |          dk    s
J |            t          j        | dk              }t          j        |          }t          j        |d         dz   t          j                  }t          j        |d                             |d                   d          |dd <   |d         }t          j        |t          j                  }t          j	        ddt          |d                   z   t          j                  }|||<   t          j        | 
                    dd          dk              }t          j        |          }	t          j        |d         dz   t          j                  }
t          j        |	d                             |d                   d          |
dd <   |	d         }|
                    dd          |         }t          j        ||ft          j        |||ft          j        |
||ft          j        |fiS )Nr   r   ri  rP  )	minlength)r  rl  r3   wherer   rU  rP  cumsumbincountrZ  r  
sparse_coor  r  strided)r  r  indicescoo_indicesrl  col_indicesstrided_valuesr]  	indices_Tcoo_indices_Tccol_indicesrow_indices
csc_valuess                r:   get_sparse_dataz8TestCase.generate_simple_inputs.<locals>.get_sparse_data  s   }Hx==A%%%x%%% k'Q,//G+g..K ;x{QekJJJL$|KN,C,ChWXk,C,Z,Z\]^^L%a.K"[EEEN
 \!QWQZ%8LLLF&,N7#G$5$5a$;$;q$@AAI!K	22M ;x{QekJJJL$|M!,<,E,EPXYZP[,E,\,\^_``L'*K'11!Q77	BJ${F&;$|[&&I$|[*&MMN#46 6r;   c                     
|           } 	|           } 
|          }|t           j                 d         |t           j                 d         }t          j        fd|                    dd          D                       }||t           j                 d         dz
           }t           j        g |t           j                 d d         |R t           j        g |t           j                 d d         |R i|S )Nr   c                     g | ]=\  }}|d          z  |dz   d          z  |d         z  |dz   d         z  f         >S r   ri  rB   )ra   bibjrn  r  s      r:   rb   zWTestCase.generate_simple_inputs.<locals>.get_sparse_data_with_block.<locals>.<listcomp>  s     &S &S &S*0"b '5R)A,5FQR[\]R^G^5^57)A,5FQR[\]R^G^5^6_ '` &S &S &Sr;   ri  r   )	r3   r  r  r   r  r  r  r  r  )r  rn  nonblock_datar  
block_datablock_indices
bsr_values
bsc_valuesr  r  r  s    `      @r:   get_sparse_data_with_blockzCTestCase.generate_simple_inputs.<locals>.get_sparse_data_with_block  s,   +OG44M++GY??L(66J*5=9!<N&u'78;M &S &S &S &S &S4A4K4KAq4Q4Q&S &S &S T TJ $Ju/?$@$Ca$GHJ$&U
53C(DRaR(H&U*&U&U$&U
53C(DRaR(H&U*&U&U%#% %r;   c                   
 | j         }t          |          dk    r | |          S i }t          |           D ]\  }} ||                                          D ]\  }
|                    |          }|t
          j        u rt          j        t          j        dt          
d                   f|t
          j	                  
d         f          }||
d         fx}||<   |d         
                    t          j        |d         |fd                     |d         
                    t          j        |d         
d         f                     |<t          
fdt          t          
                    D                       x}||<   ;t          t          
                    D ]P}	||	         
                    t          j        ||	         
|	                             d          f                     Q|S )Nr   ri  r  r   c              3   N   K   | ]}|                              d           V   dS rt  )r  )ra   r  r  s     r:   r   zQTestCase.generate_simple_inputs.<locals>.get_batch_sparse_data.<locals>.<genexpr>  s4      ?i?iVW!q@Q@Q?i?i?i?i?i?ir;   )r  rl  r   rd   r  r3   r  r  fullrP  set_r-  r
  r  )r  rn  r  
batch_datar   rG  r  r  ext_coo_indices1r  r  get_batch_sparse_datar  s             @r:   r  z>TestCase.generate_simple_inputs.<locals>.get_batch_sparse_data  s   =D4yyA~~11'9EEE J$W-- Z Z4!6!6tY!G!G!M!M!O!O Z ZIFA'^^F33F!111 ,19ej!S1YYQRZ_Ze6f6f6fhijkhl5m+n+n(!>;KQqT:RRFZ%7%7"1INN59fQiAQ5RTU+V+VWWW"1INN59fQi15F+G+GHHHH!>:??i?i?i?i[`adefagag[h[h?i?i?i:i:iiFZ%7%7%*3q66]] Z Z &q	uy&)QqT^^TUEVEV9W/X/X Y Y Y YZ!Z$ r;   c                    s| S t          | t                    s,| j        dk    r!t          j        fd| D                       S | dk    r t          j        t          j                  S t          j        d         t          j                  }t          dd                   D ]?\  }}t          j        |t          j                  d|dz   z  z  }|d         |d         z   }@|	                    |            |S )	aq  Generates a tensor of shape densesize with values equal to

              base + i_1 * 10^0 + ... + i_d * 10^{d - 1}

            at indices i_1, ..., i_d (with 0 <= i_j < densesize[j] for any 1 <= j <=
            len(densesize))

            This mapping produces unique values as long as
            densesize[i] < 10 for all i in range(len(densesize)).
            r   c                 (    g | ]} |          S rB   rB   )ra   b	densesizegenerate_valuess     r:   rb   zLTestCase.generate_simple_inputs.<locals>.generate_values.<locals>.<listcomp>  s%    #P#P#PaOOAy$A$A#P#P#Pr;   r  ri  Nr  ).N)N.)
r   r   ndimr3   r   rU  rP  rZ  r   add_)baser  r  r   r  yr  s    `    r:   r  z8TestCase.generate_simple_inputs.<locals>.generate_values
  s      dC(( RTY]]{#P#P#P#P#P4#P#P#PQQQqyy{9EK@@@@Yq\===A!)ABB-00 0 01L%+666"Q-HiL1Y</FF4LLLHr;   )ri  r   r   )ri  r   r  r   ri  )ri  r  )rB   r   r   r  )ri  r   r  )ri  r   r   )r   r   r   r   r  rB   r  )r   ri  r   r   r   r   )r   ri  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   r   r   r  r  )   r  r  r   r  r  r  r   r  r   )ri  ri  rs  c                 f                        |                                            |dk     r
|| j        z   }|dk    r|| j        k     sJ t          d|dz             }t	          j        g | j        d |         | j        |         |z  | j        |dz   d          R | j        | j                  }g t          d           f|z  t          |d |          R }||         
                    |           }                    |                                                               | |           |S )Nr   r   ri  re  )
assertTrueis_contiguousr  rF  r3   rU  r  rP  rR  slicer  assertFalserv  )r  r  offsetsteptmp
dim_slicesr  r   s          r:   non_contiguous_copyz<TestCase.generate_simple_inputs.<locals>.non_contiguous_copy\  s1    OOAOO--...QwwAFl!88af,q&1*%%D+WWqws|d/BWQWSSTWXXEVWW_`_fopowxxxCMU4[[NS0M5t3L3LMMJJ%%a((AQ__..///Q"""Hr;   c                     g | ]}||S rB   rB   rF  s     r:   rb   z3TestCase.generate_simple_inputs.<locals>.<listcomp>n  s    ===A1=a===r;   r  c                 >    g | ]}|                                S )rx  )r  )ra   rZ  rR  r  s     r:   rb   z3TestCase.generate_simple_inputs.<locals>.<listcomp>w  s)    YYY!qtt6tEEYYYr;   rx  )rR  rP  r  r  >   r  r   r  c                 ,    g | ]} |           S )r  r  rB   )ra   rZ  r  r	  r  s     r:   rb   z3TestCase.generate_simple_inputs.<locals>.<listcomp>  s.    +l+l+l_`,?,?sSY,Z,Z,Z+l+l+lr;   r  )r   r   ri  r   )rB   r  r  )r   r   )r  r  )r  r   )r   r   )rs  r   )r3   rP  r  r  r  r  generate_simple_inputsr-  r  rl  r  r  r  r  r  updater  r  r  r  ri   r  r{  empty)*r   r  rR  rP  r  r  members_pin_memoryr  r  r  r  r  r  r  patternsis_compressed_sparse_layoutr8   r9   r  r  
blocksizes
densesizesrn  datar  r  r]  indices_copyvalues_copyr  rl  r  r  r  r  r  r  r  r  r  r	  r  s*   ` ` `                             @@@@@@@@r:   r  zTestCase.generate_simple_inputsd  s	     L +K&,1A5CSUZUeglgw0x&x# 	 $ ; ;F6Y^lwGQIUerN_ZwYuVoJO !< !Q !Q  f & ?  > > > > >>>DU]**t99>>>>!::fd33D+++! 4"1good33>>@@@@@@"1good333333u///14B6BBBBBB0 MMM0008$I&IIIIII1F		3 		3 		3"	6 "	6 "	6H	% 	% 	% 	% 	% 	%(	 	 	 	 	 	8	 	 	 	 	2 4 ))))%v.0B0B0BD IIIIIIIIIIII	  IIIIIIIIIIII	 ! $*6"2RJ@ %$$$$$$$$$$$$$$$$$$$$$$$& )/x"f? q6q6q6q6 %X0I(QrT9UHB	 	 	 	 	 	  08 	E 	E+GZ  >=====
 : l7%+>>>G GL1$4$4' E E	,,Wi@@H!+ E EIYYYYYtTWUWTWyYYYG,_T"XyAADDFZ_D``F!u7=S\C\]]]F!-<<<,G,V,,fkkmm;;;;4 R9I9I-> R RMS&+l+l+l+l+l+ldk+l+l+lL"9L"9&"9"96;;=="HHHH; R.A.A&bYZ.[.[.[&B&Bk&B&BFKKMM&Q Q Q Q3 E&9&9&bQR&S&S&S5555v{{}}DDDD)EE2  '	9fX]'T'9'9'9'9QSPTU555t<fXt,5 &9 &90*j
 ", !9 !9I%/  9  9	!U]22&(G%*[(Y2FPV^c%d%d%dFF#u'777',{3x==!FZe'f'f'f&hG%*[!iW\%]%]%]FF#u'777+0<x{Q8OX^fq+r+r+rL*/+ak*Z*Z*ZK'3[&AG%*[!iW\%]%]%]FF#u'777+0<x{Q8OX^fq+r+r+rL*/+ak*Z*Z*ZK'3[&AG%*[!iW\%]%]%]FF#u'777+0<x{iXYl?Z]^?^8_hn  wB  ,C  ,C  ,CL*/+ak*Z*Z*ZK'3[&AG%*[!1Li1L)1L1LU[ch%i%i%iFF#u'777+0<x{iXYl?Z]^?^8_hn  wB  ,C  ,C  ,CL*/+ak*Z*Z*ZK'3[&AG%*[!1Li1L)1L1LU[ch%i%i%iFF$1!%V5xR[G[!\!\!\%1"MMZM@@@0000&88888A 9!9'	9 '	9&9 &9r;   c                 |    |j         t          j        k    r|                                }|                                S r2   )r  r3   r  r  to_dense)r   r  s     r:   safeToDensezTestCase.safeToDense  s-    8u'''

Azz||r;   c                     |                                 }|j        |j        |j        }}}|j        |j        |j        }}
}	 ||	g|
R i |} ||g|R i |} | j        ||fddi| d S )Nexact_deviceF)r  r.  r8   r9   rv  )r   torch_fnref_fnsample_inputr9   numpy_samplen_inpn_argsn_kwargst_inpt_argst_kwargsrn  r4  s                 r:   compare_with_referencezTestCase.compare_with_reference  s    #))++"."4l6GI\xv"."4l6GI\xv%5&555H556%5&555H55HHHHHHHHr;   c                    t           sJ t          |t          j                  rl|J |J |                                                                }|j        t          j        u r|                                }|	                                }|}	net          j
        t                    }
t          j        |
t          j        <   t          j        ||
|                   }t          j        |||          }	 ||          } ||	                                          }t          |t          j                  r	 t          j        |          }n6# t$          $ r) t          j        |
                                          }Y nw xY w|	j        t          j        u rE|j        t          j        u r2|j        t          j        u r|                    t          j                  } | j        ||fi | d S )Nr  rx  )r@  r   r3   r   r  r   rP  bfloat16rT  r  r{  torch_to_numpy_dtype_dictr  rQ  rB  r  r/  rA  r   r  rv  )r   r   np_fntensor_likerR  rP  r9   t_cpurZ  r  r  	np_resulttorch_results                r:   compare_with_numpyzTestCase.compare_with_numpy  s   zk5<00 	F>>>===&&((,,..E{en,,AAA	344A "
AenAeH555A[uEEEAE!HH	x{{(( i,, 	<?!,Y77		 ? ? ? ",Y^^-=-=>>			? w%.((\-?5>-Q-QV_VeinitVtVt+u{;;L;;F;;;;;s   8E 0F ?F c                       | j         |ddi|S )Nexact_dtypeF)rv  )r   r8   r9   s      r:   assertEqualIgnoreTypezTestCase.assertEqualIgnoreType  s!      tC5CFCCCr;   c                    t          |t                    st          j        |          |z  }t          |t          j                  s)t          j        |          t          j        |          z  } | j        ||g|R i |S )zHTests if tensor x equals to y, if y to be broadcast to x.shape.
        )r   r   r3   	ones_liker   r  rv  )r   r   r  r8   r9   s        r:   assertEqualBroadcastingz TestCase.assertEqualBroadcasting  s     !X&& 	'""Q&A!U\** 	5""U\!__4At16t666v666r;   )rH  rG  	equal_nanr5  r  exact_layoutexact_strideexact_is_coalescedr)  rH  rG  c                   d}t          d ||fD                       rd } ||          } ||          }nt          |t          j                  r7t          |t                    r"t          j        ||j        |j                  }nPt          |t                    r;t          |t          j                  r!t          j        ||j        |j                  }t          |t          j                  r0|j        r)|j	        t          j
        k    r|                                }t          |t          j                  r0|j        r)|j	        t          j
        k    r|                                }t          ||t          t          t          t           t"          t$          t&          t(          t*          f	t          t,          t.          t0          t2          t          j        j        j        j        ft<          t>          t@          tB          f|| j"        || j#        ||||	|
|          }|rQ|g}|$                                d         %                    t          tL                    r| j'        rfdn          d S )NTc              3   t   K   | ]3}t          |t          j                  ot          |j                   V  4d S r2   )r   r  r/  r~  rP  )ra   r.  s     r:   r   z'TestCase.assertEqual.<locals>.<genexpr>  sT       
 
afJubj))\2OPUP[2\2\.\
 
 
 
 
 
r;   c                     t          | t          j        t          j        f          r|                                 nt          |           S r2   )r   r3   r   r  r/  tolistr  r.  s    r:   to_listz%TestCase.assertEqual.<locals>.to_list  s6    )3EEL"*;U)V)Vgu||~~~\`af\g\ggr;   re  )
pair_typessequence_typesmapping_typesrG  rA  rH  rB  r:  check_devicerC  check_layoutcheck_stridecheck_is_coalescedr   c                     |  d S )Nr  rB   )generated_msgr)  s    r:   rE   z&TestCase.assertEqual.<locals>.<lambda>W  s    -'@'@3'@'@ r;   )(r  r   r3   r   r   	as_tensorrP  rR  	is_nestedr  r  unbindr,   r(   r'  r?  rU  r`  r}  r  r  r  r"   r   r!   r   r  r  datasetSubsetr   r   r    r   r  r  r  to_errorrl   longMessage)r   r   r  r)  rH  rG  r:  r5  r  r;  r<  r=  __tracebackhide__rC  error_metass      `           r:   rv  zTestCase.assertEqual  sm   " !  
 
klnojp
 
 
 
 
 	Ch h h 

A

AA 5<(( 	CZ8-D-D 	CBBBAA8$$ 	CAu|)D)D 	CBBBA a&& 	1; 	18u};T;T

Aa&& 	1; 	18u};T;T

A+"!! 
  (/ #JzJ,.%#%%1A!
 !
 !
F  		&-K//##A&// FPPSUXEYEYw^b^nw@@@@@tw	  			 		r;   rH  rG  c                    |                      t          |          5   | j        |||f||d| d d d            d S # 1 swxY w Y   d S )Nr^  rV  )assertRaisesrx  rv  )r   r   r  r)  rH  rG  r9   s          r:   assertNotEqualzTestCase.assertNotEqualZ  s    ~377 	H 	HDQ3GTGGGGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	Hs   >AAc                     |                      |j        |j                   |                      |j        |j                   |                      |j        |j                   d S r2   )rv  rR  rP  	is_sparse)r   r   r  s      r:   assertEqualTypeStringzTestCase.assertEqualTypeString_  sV    18,,,!'***ak22222r;   r  r  c                 p    |D ]%}t          |          t          |          k    r d S &t          d          )Nzobject not found in iterable)r   rx  )r   r  r  elems       r:   assertObjectInzTestCase.assertObjectIne  sC     	 	D#ww"T(("" #;<<<r;   c                     | j         r1t          ||           }	 |                    d||          d }S # d }w xY w t                      j        |g|R i |S )NrX  )r  r  handler\  rX  )r   expected_exceptionr8   r9   contextr]  s        r:   rX  zTestCase.assertRaisesm  sy    - 		M<=OQUVV ~~ndFCC $'577'(:LTLLLVLLLs   3 7c                     t          | d          r| j        t          vr| j        dk    rd}| j        r(t	          || |          }|                    d||          S  t                      j        ||g|R i |S )Nr  r   r   assertRaisesRegex)r   r  NATIVE_DEVICESr  r  ra  r\  re  )r   rb  expected_regexr8   r9   rc  r]  s         r:   re  zTestCase.assertRaisesRegex{  s     4'' 	 D,<N,R,RW[WgkpWpWpN- 	bB"D.: :G>>"5tVDDD,577,-?aRVaaaZ`aaar;   c                 p   d fd}t          j                    }t          j                     	 t          j                            d|          5   ||i | d d d            n# 1 swxY w Y   |rt          j                     n# |rt          j                     w w xY w|                                d S )Nc                     | d S r2   rB   )
unraisableraiseds    r:   record_unraisablez6TestCase.assertNoUnraisable.<locals>.record_unraisable  s    FFFr;   zsys.unraisablehook)r   	isenabledrI  r   rU  rV  rD  assertIsNone)r   r  r8   r9   rl  prevrk  s         @r:   assertNoUnraisablezTestCase.assertNoUnraisable  s   	  	  	  	  	  |~~

	$$%9;LMM * *$)&)))* * * * * * * * * * * * * * *  	  	 	&!!!!!s/    B 	A%B %A))B ,A)-B Bc                     d }d|v r|d         }|d= 	  ||i | n6# |$ r.}|                      t          |          |           Y d }~d S d }~ww xY w|                     d           d S )Nsubnamer  r^  )assertExpectedrl   r  )r   r  r  r8   r9   rr  r  s          r:   assertExpectedRaiseszTestCase.assertExpectedRaises  s    Y'Gy!	Hd%f%%%% 	 	 	A000FFFFF	 			6	77777s    A#A

Ar   c                 :   t          j        d          5 }t          j        d           t          d          5   |             ddd           n# 1 swxY w Y   |                     t          |          dk    |           ddd           dS # 1 swxY w Y   dS )zD
        Test if :attr:`callable` does not raise a warning.
        TrecordalwaysNr   )rv  rw  rx  r  r  rl  )r   r  r)  wss       r:   assertNotWarnzTestCase.assertNotWarn  s    $D111 	/R!(+++(..  


              OOCGGqL#...		/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s4   $BABA	BA	*BBBc              #   <  K   t          j        |          t          j        d          5 }t          j        d           t          d          5  dV  ddd           n# 1 swxY w Y   t          |          dk    r|                     d           |                     t          fd|D                                  |                     t          fd|D                        d	fd
|D                         ddd           dS # 1 swxY w Y   dS )zContext manager for code that *must always* warn

        This filters expected warnings from the test and fails if
        the expected warning is not caught. It uses set_warn_always() to force
        TORCH_WARN_ONCE to behave like TORCH_WARN
        Trv  rx  Nr   zno warning caughtc              3   D   K   | ]}t          |j                  u V  d S r2   r   rR  ra   wcategorys     r:   r   z0TestCase.assertWarnsOnceRegex.<locals>.<genexpr>  s0      HHQY8 ;HHHHHHr;   c              3   f   K   | ]+}t          j        t          |j                            V  ,d S r2   )r  r   rl   rR  )ra   r  r  s     r:   r   z0TestCase.assertWarnsOnceRegex.<locals>.<genexpr>  s7      BB!BHWc!)nn55BBBBBBr;   r  c                 J    g | ]}t          |j                  u |j         S rB   r}  r~  s     r:   rb   z1TestCase.assertWarnsOnceRegex.<locals>.<listcomp>  s-    TTTQQY88S8Sqy8S8S8Sr;   )
r  r  rv  rw  rx  r  rl  r  r  r  )r   r  regexry  r  s    `  @r:   assertWarnsOnceRegexzTestCase.assertWarnsOnceRegex  s      *U##$D111 		XR!(+++(..                2ww!||		-...OOCHHHHRHHHHHIIIOOBBBBrBBBBBVVTTTT"TTTVVX X X		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		X 		Xs6   $DA#D#A'	'D*A'	+BDDDc                   
 t          t                    st          d          d }| j        j        } ||                                 |dz             t          j                            t          j
        |         j                  }t          j                            t          j                            |          d          
d|r
d|z   z  
d| d
d	z  
d
}
fd}	 t          
          5 }|                                }d
d
d
           n# 1 swxY w Y   nm# t           $ r`}	|	j        t"          j        k    r t&          j        r |d          cY d
}	~	S t+          d  d dt,          j         d d
          d
d
}	~	ww xY wt.          r,t1          j        dd|          }t1          j        dd          |                    ddt6          j        j         d          }t&          j        r|k    r |d          S d
S t=          | d          r|                     |           d
S |                      |           d
S )a  
        Test that a string matches the recorded contents of a file
        derived from the name of this test and subname.  This file
        is placed in the 'expect' directory in the same directory
        as the test script. You can automatically update the recorded test
        output using --accept.

        If you call this multiple times in a single function, you must
        give a unique subname each time.
        zassertExpected is strings onlyc                 ^    |                      |          r| t          |          d          S | S r2   )r   rl  )textprefixs     r:   remove_prefixz.TestCase.assertExpected.<locals>.remove_prefix  s0    v&& *CKKLL))Kr;   r   r  r   r  z (r  z.expectNc           	          t          d|  d  d            t          d          5 }t          j        dd          }|                    |           d d d            d S # 1 swxY w Y   d S )Nz
Accepting r  :

r  z(producer_version): "[0-9.]*"z\1: "CURRENT_VERSION")r  r   r  rq  r6  )update_typer  s_tagexpected_file	munged_idrG  subname_outputs      r:   accept_outputz.TestCase.assertExpected.<locals>.accept_output  s    T{TTTNTTQRTTUUUmS)) Q?7< <	                 s   ,A%%A),A)rn  zI got this output for r  zC

No expect file exists; to accept the current output, run:
python r^   z	 --acceptzCppOp\[(.+?)\]zCppOp[]z#producer_version: "CURRENT_VERSION"zproducer_version: "rj  zupdated outputassertMultiLineEqual)!r   rl   r  r]  rg   r   rK   r   realpathr  modulesr  rc   dirnamer   r  r"  errnoENOENT
expecttestr  r  r  r  r  rq  rU  r3   onnxproducer_versionr   r  rv  )r   rG  rr  r  	module_id	test_filer4  r  r  r  r  r  r  s    `        @@@r:   rs  zTestCase.assertExpected  s)    !S!! 	><===	 	 	 N-	!M$''))Y_==	G$$S[%;%DEE	RW__Y%?%?%-%.0 0  	-S7]*M,'___N"	 	 	 	 	 	 	 		Tm$$ $6688$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 		T 		T 		Tw%,&&" T$}X........"Iy I. I Iq I I ( 1I I4=I I IJ J PTT		T  	8v/HEEH()Q77A ##1@%*"=@@@
 
  		.1}}$}%5666 } t344 . ))(A66666  H-----sH   -D) <DD) D!!D) $D!%D) )
F3,FF%)FFc                 ^    t          j        dd|          }|                     ||           d S )Nz__torch__[^ ]+r   )r  rq  rs  )r   rG  rr  s      r:   assertExpectedStripMangledz#TestCase.assertExpectedStripMangled$  s2    F$b!,,Aw'''''r;   c                    ||t          d          ||k    rdS ||z
  }|||k    rdS | d| d| d}n%|d}t          ||          dk    rdS | d| d| d}|                     ||          }|                     |          )	zAssert that ``first`` is greater than or almost equal to ``second``.

        The equality of ``first`` and ``second`` is determined in a similar way to
        the ``assertAlmostEqual`` function of the standard library.
        Nz specify delta or places not bothz not greater than or equal to z within z deltar  r   z places)r  round_formatMessagerw  )r   firstsecondplacesr)  deltadiffstandardMsgs           r:   assertGreaterAlmostEqualz!TestCase.assertGreaterAlmostEqual(  s     !3>???F??F~u}}"__&__RW___KK~T6""a''"aa&aaRXaaaK!!#{33##C(((r;   c                 J   d |j         j        D             }|                     |           |D ].}d |j        D             }|                    d          |k    r n/|                     |d         |           |                     |                    dd          |           d S )Nc                 >    g | ]}|j         d k    |j        dk    |S )ATenzorg.pytorch.aten)op_typedomain)ra   r  s     r:   rb   z)TestCase.assertAtenOp.<locals>.<listcomp>G  s>     U U UY&00QXAS5S5S 5S5S5Sr;   c                 L    i | ]!}|j         |j                                        "S rB   )rN   rG  decode)ra   attrs     r:   r  z)TestCase.assertAtenOp.<locals>.<dictcomp>L  s&    IIIDTYIIIr;   r  overload_namer   )graphnoder  	attributer  rv  )r   
onnx_modelr  r  all_aten_nodesopattrss          r:   assertAtenOpzTestCase.assertAtenOpF  s    U UZ%5%: U U U'''  	 	BIIBLIIIEyy$$00 1 	z*H555?B77GGGGGr;   c                    d|z   dz   }t          d          5  |r@|                     t          |d          5   |             ddd           n# 1 swxY w Y   nX	  |             nL# t          $ r?}dt          |          v r(|                     dt          |          z   d	z               d}~ww xY wddd           n# 1 swxY w Y   t          dd
          5  |r>|                     t          |          5   |             ddd           n# 1 swxY w Y   nt          j        d          5 }t          j	        d            |             |D ]N}t          |t                    r7|                     t          j        |t          |                    du            O	 ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )a  Checks that an operation produces a nondeterministic alert when
        expected while `torch.use_deterministic_algorithms(True)` is set.

        Args:
          fn (callable): Function to check for a nondeterministic alert

          caller_name (str): Name of the operation that produces the
              nondeterministic alert. This name is expected to appear at the
              beginning of the error/warning message.

          should_alert (bool, optional): If True, then the check will only pass
              if calling `fn` produces a nondeterministic error/warning with the
              expected message. If False, then the check will only pass if
              calling `fn` does not produce an error. Default: `True`.
        ^z: does not have a deterministic implementation, but you setTz9expected a non-deterministic error, but it was not raisedr^  Nz,does not have a deterministic implementationzEdid not expect non-deterministic error message, but got one anyway: "rj  r  rv  rx  )r  re  r  rl   r  assertWarnsRegexr  rv  rw  rx  r   r  r  r4  )r   r   caller_nameshould_alertalert_messager  r  r   s           r:   check_nondeterministic_alertz%TestCase.check_nondeterministic_alertS  s   " k),hh  %% 	 	 ++$%W , Y Y   BDDD	              BDDDD#   EQOO		68;A?ADEF F F 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	,  555 	\ 	\ \**#%' '   BDDD              
 ,D999 \Q)(333BDDD#$ \ \%g{;; \ OOBImS\\,R,RVZ,Z[[[\\ \ \ \ \ \ \ \ \ \ \ \ \ \ \	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\s    B?AB?A	B?A	B?
A'&B?'
B01:B++B00B??CCG9DGD	GD	G1A0F."G.F2	2G5F2	6GG
G
c                     dd l } |j        t          j        d| g|j        |j        |          }|                                \  }}||fS )Nr   -c)r  r  r  )r  r  r  r~  PIPEcommunicate)coder  r  popenr  r  s         r:   run_process_no_exceptionz!TestCase.run_process_no_exception  sc     
 ^T4(??	  
 !,,..r;   c                    t           j                                        }d|d<   |                    dd            |                    dd            t                              | |          \  }}|                    d          S )NrA   PYTORCH_API_USAGE_STDERRro   TEST_SHOWLOCALS)r  r  )rK   r  r{  r  r   r  r  )r  r  _stdoutr  s       r:   runWithPytorchAPIUsageStderrz%TestCase.runWithPytorchAPIUsageStderr  sw    joo*-&' 	d!4(((";;Dc;JJ}}W%%%r;   r  import_stringexpected_failure_messagec           	      0   d| d| d}|r-|                      t          t          j        |                    nt	          j                    }|5  	 t          j        t          j	        d|gt          j                            t          j                            t                              t          j                   n?# t          j        $ r-}t          |j                            d                    dd}~ww xY w	 ddd           dS # 1 swxY w Y   dS )a  
        Attempts weights_only `torch.load` in a subprocess. This is used to test that
        weights_only `torch.load` works as expected without global imports.

        Args:
            file (pathlib.Path): The path to the checkpoint to load.
            import_string (str): import string to add to the script
            exected_failure_message (str, optional): The expected failure message if the
                checkpoint fails to load. If None, the test will pass
        zimport torch;ztorch.load(r'z', weights_only=True)r  )r  r  r   N)re  r  r  escaper'  r(  r  check_outputr  r~  rK   r   r  r  r  STDOUTCalledProcessErrorrn  r  )r   r  r  r  scriptcmr  s          r:   _attempt_load_from_subprocessz&TestCase._attempt_load_from_subprocess  s~     YXXTXXX (FD""<;S1T1TUUU-7-C-E-E 	  
	G 
	G	G'^T62 (8(8(B(BCC%,     0 G G G"18??7#;#;<<$FG
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	G 
	Gs7   DA-B>=D>C:(C55C::DDD)r  r  r2   )r   FFr=  )NNNNNTTTTTFTNrI  rC  )NNNT)Srf   rg   rh   r  rT  rj   r  r  rq   r  maxsize_diffThresholdmaxDiffr  propertyr  setterr  rk  rr  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r;  r  rG  rK  rk   rm  r  r  r  r  r  r  r  r  r*  r3  r6  r9  r   r   rl   r   rv  rY  r\  r   r   r_  rX  re  rp  rt  rz  r   r  rs  r  r  r  r  r  r  pathlibr   r  rk  rl  s   @r:   r   r     sW         JHe */ $... [NG   5    X e          X ^E d    ^ "'"' %*!3K 3K 3K 3K 3K 3Kj/ / / /& & &* * *) ) )
) ) )r r r r r r  34TVZ8 8 8 8 81 1 1 1 1 1
3 3 3 3 3 3

Z 
Z 
ZV V V/ / /S S SC C CF F Fb b b b b bJ   8 8 8,: : :$ 48K. K. K. K. \K.Z egst ,l ,l ,l ,l ,l\ WX y y y y y WX p p p p p bc @ @ @ @ @
 bc @ @ @ @ @
< < <: '+%)+/*.26,0-115=A<@9>-1(,P9 P9 P9 P9d
  I I I  /3 <  <  <  <DD D D D
	7 	7 	7 	7 ?C	Y %)$($Y Y Y %XseSj%9 9:;	Y 5/Y 5/Y Y Y YvH/3TH H H H%e_H;CE?H`dH H H H
3 3 3 3=# =# =4 = = = =
M 
M 
M 
M 
Mb b b b b." " ".8 8 8/ / / / X X X ^X&N. N. N. N.`( ( ( () ) ) )<H H H H6\ 6\ 6\ 6\r 	  	  	  \	  & & \& 37	G GlG G #+3-	G
 
G G G G G G G Gr;   r   c                       e Zd ZdS )TestCaseBaseNrf   rg   rh   rB   r;   r:   r  r    s        
 	Dr;   r  c                    ddl m} ddlm}m} t
          j                             ||           d                   }t          t
          j        	                    t
          j        
                    t                    d                    }t
          j        	                    ||          }t
          j                            |          r|S 	 |                    | d                                          }t          ||rdnd	          5 }	|	                    |           d d d            n# 1 swxY w Y   |S # |j        $ r:}
d
|  d}t%          j        |t(                     t+          j        |          |
d }
~
ww xY w)Nr   )urlsplit)requestr  r   r     r  wbr  zcould not download test file 'rI   )urllib.parser  urllibr  r  rK   r   basenamer   rc   r  r  rt  urlopenr  r   r6  URLErrorrv  rw  RuntimeWarningr   r  )urlbinaryr  r  r  r   data_dirr   r  r  r  r)  s               r:   download_filer    s   %%%%%%%%%%%%%%wa 011H bgooh.G.G!P!PQQH7<<(++D	w~~d ,sB//4466$/C00 	AGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	> , , ,5s555c>***$$!+,s<   =D: D-!D: -D11D: 4D15D: :
E>5E99E>c                  T   t          t          j        t          j        t          j                            5 } |                     t          j        t          j        d           |                     d           |                                 \  }}|cddd           S # 1 swxY w Y   dS )a  
    Finds an available port and returns that port number.

    NOTE: If this function is being used to allocate a port to Store (or
    indirectly via init_process_group or init_rpc), it should be used
    in conjunction with the `retry_on_connect_failures` decorator as there is a potential
    race condition where the allocated port may become unavailable before it can be used
    ri  )	localhostr   N)	r   socketAF_INETSOCK_STREAM
setsockopt
SOL_SOCKETSO_REUSEADDRbindgetsockname)sockrs   ports      r:   find_free_portr    s     
v~v/ABB	C	C t)6+>BBB		"###""$$4	                 s   ABB!$B!zAddress already in usezconnect() timed out.c                 p      t          t                    S t                      fd            }|S )zuReruns a test if the test returns a RuntimeError and the exception
    contains one of the strings in connect_errors.N)connect_errorsc            	      .   d}|}	 	  | i |S # t           $ ryt          fdD                       rX|dz  }|dk    r#t          d| dt                               t          j        t          j                               Y d  d ww xY w)Nr  Tc              3   :   K   | ]}|t                    v V  d S r2   )rl   )ra   connect_errorr  s     r:   r   z=retry_on_connect_failures.<locals>.wrapper.<locals>.<genexpr>  s.      WW}}E

2WWWWWWr;   ri  r   zFailing after z retries with error: )r  r  rl   timesleepr  )r8   r9   	n_retriestries_remainingr  r  r  s       @r:   r   z*retry_on_connect_failures.<locals>.wrapper  s    	#
		tT,V,,,   WWWWWWWWW #q(O&!++*+hI+h+h\_`e\f\f+h+hiiottJv}///HHHHs    
BA.BBB)r
   retry_on_connect_failuresr   )r  r  r   s   `` r:   r  r    sT     |0PPPP
4[[     [ Nr;   r  c                       fd}|S )Nc                 F     t                      fd            }|S )Nc            	      @   
}}|dk    rR	  | i |S # $ r;}| d|dd}t          |           t          j        |           |dz  }Y d }~nd }~ww xY w|dk    R	  | i |S # $ r1}t          j        d
 dt          |                     	r|n|d }~ww xY w)Nri  z, Retrying in r  z seconds...zSkipping after z consecutive )r  r  r  r   r  rl   )r8   r9   mtriesmdelayr  r)  ExceptionToCheckdelayr  skip_after_retriestriess         r:   f_retryz*retry.<locals>.deco_retry.<locals>.f_retry  s   "EFF1** 1d-f---'      CCfCCCCC#JJJJv&&&aKFFFFFF	  1**|q$)&)))# | | |'(V%(V(VcRSff(V(VWWbt]{]^]^z{{|s,    A1AAA' 'B,,BBr  )r  r  r  r  r  r  s   ` r:   
deco_retryzretry.<locals>.deco_retry  sQ    	q	| 	| 	| 	| 	| 	| 	| 	| 
	| r;   rB   )r  r  r  r  r  s   ```` r:   retryr    s6           " r;   c                 F   || k    sJ t          j        | | ||          }t           j                            |d          \  }}}t	          |           D ]}||k    rd||<   ||         dk    rd||<    ||                    |                              d          z  |z  S )Nre  Ffull_matricesr   ri  r  )r3   randnlinalgsvdr
  r  r  )	lrankrP  rR  AurG  vhr   s	            r:   random_square_matrix_of_rankr  (  s    19999Aqf555A|77HAq"1XX  99AaDDqTQYYAaDU%%b)))R//r;         ?gMbP?)r   sigmac                    t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        i}t          j        || |          }|                    d          }|                    d          }t           j                            |d          \  }	}
}t          j	        |dd         t          ||          fz   ||          |d|z  |z                       dd          j                            |           }|	|                    d          z  |z  S )	z
    Returns a random rectangular matrix (batch of matrices)
    with singular values sampled from a Gaussian with
    mean `mean` and standard deviation `sigma`.
    The smaller the `sigma`, the better conditioned
    the output matrix is.
    re  r  rs  Fr  NT)
descending)r3   rT  doublecfloatcdoubler  r  r  r  r  minr|  r]  r  r  )rP  rR  r   r   r  primitive_dtyper   rO  rN  r  rs   r  rG  s                r:   random_well_conditioned_matrixr(  3  s     	U[eleku|	O 	
5f555A	r

A	r

A|77HAq"	uSbSzSAYYL09OX^	_	_	_bg	gjn	n	bT	"	"6""U)) B2%%r;   c                 f   |                                  s| S | j        j        s| j        j        rt          j        }n| j        t          j        k    rd}nd}|                     | j	        dz             }||d<   | 
                                |d<   |d         }|                    | j                   |S )NTr  r  ).r   ).ri  )r  rP  is_floating_point
is_complexrX  nanr3   rq   	new_emptyr  r  requires_grad_r  )r  rZ   r  s      r:   noncontiguous_liker/  M  s    ??  	w  AG$6 	
EJ		[[4((FF6NXXZZF6NF^F
!/***Mr;   c                     |                     dt          j                  }|                     dd          }t          j        || | fz   ||d}||j        z                       d          }|S NrP  rR  r   re  r   )r  r3   r#  r  mTdiv_r  batchesr9   rP  rR  r  s         r:   random_symmetric_matrixr6  b  e    JJw--EZZ%((FgA&uVDDDA	
QTAHr;   c                     |d         |d         k    sJ t          || |          }||j        z                       d          }|S )Nrs  r  rx  r   )r&   r2  r3  )rR  rP  r  r  s       r:   make_symmetric_matricesr9  k  sK    9b	!!!!E&666A	
QTAHr;   c                     |                     dt          j                  }|                     dd          }t          j        || | fz   ||d}||j        z                       d          }|S r1  )r  r3   r#  r  mHr3  r4  s         r:   random_hermitian_matrixr<  q  r7  r;   c                     |                     dt          j                  }|                     dd          }t          j        || | fz   ||d}||j        z  S )a[  
    Returns a batch of random symmetric positive-semi-definite matrices.
    The shape of the result is batch_dims + (matrix_size, matrix_size)
    The following example creates a tensor of size 2 x 4 x 3 x 3
    >>> # xdoctest: +SKIP("undefined variables")
    >>> matrices = random_symmetric_psd_matrix(3, 2, 4, dtype=dtype, device=device)
    rP  rR  r   re  )r  r3   r#  r  r2  r4  s         r:   random_symmetric_psd_matrixr>  y  sV     JJw--EZZ%((FgA&uVDDDAqt8Or;   re  c                D    t          j        || | fz   ||d}||j        z  S )a[  
    Returns a batch of random Hermitian positive-semi-definite matrices.
    The shape of the result is batch_dims + (matrix_size, matrix_size)
    The following example creates a tensor of size 2 x 4 x 3 x 3
    >>> # xdoctest: +SKIP("undefined variables")
    >>> matrices = random_hermitian_psd_matrix(3, 2, 4, dtype=dtype, device=device)
    re  )r3   r  r;  matrix_sizerP  rR  
batch_dimsr  s        r:   random_hermitian_psd_matrixrC    s0     	jK#==eTZ[[[Aqt8Or;   c                    |                     dt          j                  }|                     dd          }t          j        || | fz   ||d}t          j        ||j                  t          j        | ||          dz  z   S )NrP  rR  r   re  h㈵>)r  r3   r#  r  matmulr2  eye)rA  rB  r9   rP  rR  r  s         r:   random_symmetric_pd_matrixrH    s    JJw--EZZ%((FjK#==	0 	0 	0A<14  
)KuV
<
<
<t
CD Dr;   c                     |d         |d         k    sJ t          || |          }t          j        |d         | |          dz  }||j        z  |z   S )Nrs  r  rx  rE  )r&   r3   rG  r2  )rR  rP  r  r  r   s        r:   make_symmetric_pd_matricesrJ    s^    9b	!!!!E&666A	%)F%8884?Aqt8a<r;   c                t    t          j        || | fz   ||d}||j        z  t          j        | ||          z   S )aU  
    Returns a batch of random Hermitian positive-definite matrices.
    The shape of the result is batch_dims + (matrix_size, matrix_size)
    The following example creates a tensor of size 2 x 4 x 3 x 3
    >>> # xdoctest: +SKIP("undefined variables")
    >>> matrices = random_hermitian_pd_matrix(3, 2, 4, dtype=dtype, device=device)
    re  )r3   r  r;  rG  r@  s        r:   random_hermitian_pd_matrixrL    sM     	jK#==	0 	0 	0Aqt8ei5HHHHHr;   )r  c                 T   t          j                    5  t          || |          }t           j                            |d          \  }}}|j        j        r|j        j        n|j        }t          |d         |d                   }	t          j	        d|	dz   ||           }
|
dd dxx         d	z  cc<   |

                                                    d
           ||
                    |j                  z  |z  }d d d            n# 1 swxY w Y   |                    |           |S )Nrx  Fr  rs  r  r   re  ri  g      r  )r3   r  r&   r  r  rP  r+  realr&  rZ  reciprocal_r  r  r.  )rR  rP  r  r  r  r  rs   r  
real_dtyperi  rG  r   s               r:   4make_fullrank_matrices_with_distinct_singular_valuesrQ    sV   	 % %fE:::<##AU#;;1b%&W%7DQV\\QW
b	59%% LAEFCCC	!$Q$3 	
R    ag"$!% % % % % % % % % % % % % % %" ]###Hs   C(DDDc                    |                     dt          j                  }|                     dd          }|                     dd          }|                     dd          }|r)t          j        j        st          j        | |||          S t          j        || |fz   ||          }|                                dk    r|S t          j        	                    |d	          \  }	}
}t          | |          }t          j        d
|d
z   z  d
|||          }|rd||d
z
  <   |dk    rd|d<   |	|                    d          z  |z  S )zReturn rectangular matrix or batches of rectangular matrices.

    Parameters:
      dtype - the data type
      device - the device kind
      singular - when True, the output will be singular
    rP  rR  r   silentFsingularre  r   r  ri  r   r  )r  r3   r#  r  rN  onesr  r;  r  r  r&  linspacer  )rowscolumnsrB  r9   rP  rR  rS  rT  r  r  rs   r  ri  rG  s                 r:   random_matrixrY    sJ    JJw--EZZ%((FZZ%((Fzz*e,,H Eeh) Ez$uVDDDDJ$0fMMMAwwyyA~~|77HAq"D'AqAE{AqfEEEA !a%q55 AaDB2%%r;   c                 p    t          || g|R i |}t          | |g|R i |}|                    |          S )zVReturn rectangular matrix or batches of rectangular matrices with
    given rank.
    )rY  rF  )r  rW  rX  rB  r9   BCs          r:   random_lowrank_matrixr]    sR     	dD8:88888AdG;j;;;F;;A88A;;r;   rW  colsnum_indicesc                 4   g }t          j        || z            }t          t          |                    }t          |           D ]7|                    fdt          j        ||          D                        8t          j        |d|                   S )z\Generate indices for a row x cols matrix, preferring at least one index per row if possible.c              3       K   | ]}|fV  	d S r2   rB   )ra   re  r  s     r:   r   z4_generate_indices_prefer_all_rows.<locals>.<genexpr>  s'      PP!1vPPPPPPr;   )ri  N)	rX  ceilr  r
  r  r  choicesr3   r  )rW  r^  r_  r  	n_per_rowr  r  s         @r:   !_generate_indices_prefer_all_rowsre    s    G	+,--IuT{{##K4[[ Q QPPPPv~kY'O'O'OPPPPPPP<-...r;   {Gz?c                    |                     dt          j                  }|                     dd          }t          t	          | |          t          | |z  |z                      }t          | ||          }t          j        |||          }|t          j        d |D             ||          	                                z  }t          j
        |                                || |f|          }	|	                                S )a4  Return rectangular random sparse matrix within given density.

    The density of the result approaches to given density as the size
    of the matrix is increased and a relatively small value of density
    is specified but higher than min(rows, columns)/(rows * columns)
    for non-singular matrices.
    rP  rR  r   re  c                 @    g | ]\  }}t          ||z
            d z   S r  rT  )ra   r   r  s      r:   rb   z(random_sparse_matrix.<locals>.<listcomp>  s-    AAAAU1q5\\1_,AAAr;   r  )r  r3   r#  rF  r&  r   re  r  r  expr  r  r  )
rW  rX  densityr9   rP  rR  nonzero_elementsr  r]  r  s
             r:   random_sparse_matrixrm    s     JJw--EZZ%((F3tW--s4'>G3K/L/LMM/g?OPPG[)vFFFF elAAAAAW]^^^bbdddF		VdG_VTTTA::<<r;   c           	          ddl }|                    dt                      d                   }|                    d|j                  }|                    dd          } fdt	                     D             }dd	}| z   z  }	t          |          |	k     rt          j        d d
z
            }
t          j        d d
z
            }|
|k    rat          j        dd|j	        z            } |j
        |          } |j        |          } || |
|||d            || |
|||d           t          |          |	k     g g g }}}t          |                                          D ]G\  \  }
}}|                    |
           |                    |           |                    |           H |j        ||g          } |j        ||  f||          S )a  Return random sparse positive-definite matrix with given density.

    The eigenvalues of the matrix are defined as::
      arange(1, matrix_size+1)/matrix_size

    Algorithm:
      A = diag(arange(1, matrix_size+1)/matrix_size)
      while <A density is smaller than required>:
          <choose random i, j in range(matrix_size), theta in [0, 2*pi]>
          R = <rotation matrix (i,j,theta)>
          A = R^T A R
    r   Nr3   rP  rR  r   c                 @    i | ]}||ft          |d z             z  S ri  ri  )ra   r   rA  s     r:   r  z+random_sparse_pd_matrix.<locals>.<dictcomp>#  s>     ) ) ) FE!a%LL;. ) ) )r;   Tc                 N   t          |          D ]}|r	||f||f}	}n||f||f}	}|                     |d          |                     |	d          }}
||
z  ||z  z   | |
z  ||z  z   }}
|
r|
| |<   n|                     |d            |r|| |	<   ~|                     |	d            d S r  )r
  r  r  )r  r_  r   r  cssnleftri  ikjkaikajks               r:   multiplyz)random_sparse_pd_matrix.<locals>.multiply'  s    q 	# 	#A (Q!QBQ!QBxxAQCCx"s(*RC#IS,@C #RT""" #RT""""	# 	#r;   ri  r   )rt  Fre  r  )rX  r  rM   r#  r
  rl  r  randintuniformpicossinsortedrd   rB  r  r  )rA  rk  r9   rX  r3   rP  rR  r  ry  
target_nnzr   r  thetarr  rs  icoordsjcoordsr]  r[  indices_tensors   `                   r:   random_sparse_pd_matrixr    s    KKKJJw		' 233EJJw--EZZ%((F) ) ) );'') ) )D# # # #" ;&4J
d))j
 
 N1kAo..N1kAo..66N1a$'k22E%B%BHT;1b"4@@@@HT;1b"5AAAA d))j
 
   "2rfWGDJJLL))  	Aqqa!U\7G"455N"5">6K;U]bkqrrrrr;   c                    |D ]{}|t           j        k    rit          j        d|||          }|                     ||j                   |                     ||j                   |                     ||j                   |d S )Nr  )rP  r  rR  )r3   float16rU  assertIsrP  r  rv  rR  )r   dtypesr  rR  rP  outs         r:   do_test_dtypesr  K  s     1 1EM!!+fE&PPPCMM%+++MM&#*---VSZ0001 1r;   c                     t          j        ddg           fd}d }t          j                    } |t          j                  |t           j        dd d            |t          j        d          |t           j        dd d           |D ]P}|j        dhD ]B} ||          }	t          j        ||||          }
 ||
|||d |           |
                                } |t          j        ||||	          |||d |            ||
                              |||d d            ||
                    |	|d
          |	||d d            |t          j	        |
          |||d d            |t          j	        |
|	||d          |	||d d           |t           j
        ur.|t           j        k    rd}t          j        |||||          }
 ||
|||||            ||
                    |dz             ||||dz   d           |
                                } |t          j        |dz   ||||	          ||||dz   |            ||
                    |dz   |	|d
          |	|||dz   d            |t          j        |
|dz             ||||dz   d            |t          j        |
|dz   |	||d          |	|||dz   d           DRd S )Nr   r  c                                         | j                                       || j                                       || j                                        | j        |           | j        r|                     || j                   |@|                               	                    |          }                     | |           d S d S r2   )
rv  r  r  rP  r  r  is_cudarR  r  rV  )	r  rP  r  rR  rZ   r  fillr   r  s	          r:   check_valuez'do_test_empty_full.<locals>.check_valueW  s    ---eV\***ffm,,,-}===> 	4f0VV]333::e$$**511DVT***** r;   c                     d                     t          |                               d          dd                   }|st          j        S  t          j        |          t                    j        S )Nr   ri  rs  )rc   rl   rI  r3   rP  r  
attrgetter)rP  modules     r:   get_int64_dtypez+do_test_empty_full.<locals>.get_int64_dtypeb  s^    #e****3//"566 	;*x"6**51177r;   rs  Fg      )rP  rR  r  r  )r  rR  r  r  )rP  rR  r  )rP  r  rR  r  ri  r   r  )r3   r  r  r  r  r  r*  r  r-  
empty_liker  r  new_full	full_like)r   r  r  rR  r  r  default_dtyperP  rgint64_dtyper[  r  fvr  s   `            @r:   do_test_empty_fullr  T  s   J1vE	+ 	+ 	+ 	+ 	+ 	+8 8 8 +--MKE""M5="dERRRK
5#&&u}b$PUVVV H H*E2 	H 	HB)/%00KEvf\^___AK5&&$;;;%%''CKEs6&`bcccvvtR9 9 9KE**E664OOOKEV[`aa#VVT5B B BK(++UFFD%PPPK(+fU[kpqqq#VVT5B B B EM))f8H.H.HJubfVcefffAuffb"===AJJub1f55uffbSTfV[\\\eeggEJub1f#fU[kmnnn!66262? ? ?AJJub1fKPVfkJll'aH H HEOArAv66vvrTUvW\]]]EOArAv2=fU[kpr r r'aH H H3	HH Hr;   c                      	 t           j                            t           j                            t          j        d                             } |                     d          r| ad S d S # t          $ r Y d S w xY w)Nr   r-  )	rK   r   r  r  r  r  endswithrunning_script_pathr   )running_files    r:   set_running_script_pathr    s~    wrw'7'7'D'DEE  '' 	/".	/ 	/   s   A#A) )
A76A7c                 *   t           d S t          j                            t          j                            t          j        | j                                      }|t           k    s+J d|                                  dt            d| d            d S )NzClass of loaded TestCase "z(" is not defined in the running script "z", but in "zE". Did you accidentally import a unittest.TestCase from another file?)	r  rK   r   r  r  r   r  r]  r   )r  test_case_class_files     r:   $check_test_defined_in_running_scriptr    s    "7??27+;+;GOIL_<`<`+a+abb#6666 9EU^UaUaUcUc 9E 9E1D9E 9EQe9E 9E 9E66666r;   c                     t                       t          j                    }|D ]9}t          s|D ]}t	          |           |j        r|                    |           :|S r2   )r  r   r  r   r  r  addTest)loaderr@  r  
test_suite
test_groupr   s         r:   
load_testsr    sy    #%%J + +
) 	;" ; ;4T:::: 	+z***r;   c                       e Zd Zd Zd ZdS )BytesIOContextc                     | S r2   rB   r   s    r:   r  zBytesIOContext.__enter__  r   r;   c                     d S r2   rB   )r   r8   s     r:   r  zBytesIOContext.__exit__  s    r;   Nr  rB   r;   r:   r  r    s2              r;   r  g-q=TEST_WITH_SLOW_GRADCHECK PYTORCH_TEST_WITH_SLOW_GRADCHECKzETests that don't use gradcheck don't need to run on slow_gradcheck CIc                     ddd}t           rd|d<   |                                D ]$\  }}|                    |d           }||n|||<   %t          j        j        | |fi |S NT)check_batched_grad	fast_modeFr  )r  rd   r  r3   autograd	gradcheck)r   inputsr9   default_valuesr  rZ   ri  s          r:   r  r    s     # N
   ,&+{#$**,, 4 4
UJJsD!!=aaes>#B99&999r;   c                     ddd}t           rd|d<   |                                D ]$\  }}|                    |d           }||n|||<   %t          j        j        | ||fi |S r  )r  rd   r  r3   r  gradgradcheck)r   r  grad_outputsr9   r  r  rZ   ri  s           r:   r  r    s     # N
   ,&+{#$**,, 4 4
UJJsD!!=aaes>'FLKKFKKKr;   c                     |                      t          ||fi |           |                      t          ||fi |           d S r2   )r  r  r  )r  apply_fnr  r9   s       r:   _assertGradAndGradgradChecksr    sV     8V>>v>>???xBB6BBCCCCCr;   r   c              #      K   t          j                    }	 t          j        |            d V  t          j        |           d S # t          j        |           w xY wr2   )rK   getcwdchdir)r   old_cwds     r:   set_cwdr    sX      ikkG

s   A ArE  g?c                 <     t                      fd            }|S )Nc                 H     | g|R i |ddi  | g|R i |ddi d S )N	coalescedTFrB   )r   r8   r9   r  s      r:   r[  zcoalescedonoff.<locals>.wrapped	  s_    	$00000040000	$1111115111111r;   r  )r  r[  s   ` r:   coalescedonoffr    s3    
1XX2 2 2 2 X2 Nr;   c                 B   |                                  }d| j        |                                 dz
  dd         z   }t          j        || j                                      d                              d          }|                                 dk    r.|                    d           ||z  	                    d          }n||z  }t          j
        ||                                d                   }|o&t          j
        ||                                          }|S )Nrp  ri  r   rs  r  )r  r  r  r3   r  rR  cumprodflip
unsqueeze_sumallcloser|  unique)rG  r  hash_coeffshash_indicesr  s        r:   is_coalesced_indicesr    s    jjllG!!3Ab!899K<AH===EEbIINNrRRL||~~###,.33A66- .|'8'8':':1'=
>
>C 
E%.|/B/B/D/DEECJr;   c               #      K   t          j                    rE	 t          j                     d V  t          j                     d S # t          j                     w xY wd V  d S r2   )r   rm  rI  rD  rB   r;   r:   
disable_gcr  #  sY      	|~~ 	JLLLEEEIKKKKKBIKKKKs   A Alib_namec                 ,   t          t          j                                                  j        }|dz  | z  }t
          j                            |          r|S t          t                                                    j        d         }|dz  dz  | z  S )Nlibr   build)	r   r3   r  resolveparentrK   r   rt  parents)r  
torch_rootr   s      r:   find_library_locationr  /  s     en%%--//6J(D	w~~d h''))1!4J%'(22r;   c                       fd}|S )z
    Similar to unittest.skip, however in the sandcastle environment it just
    "passes" the test instead to avoid creating tasks complaining about tests
    skipping continuously.
    c                 n     t           sd _         _         S t                      fd            }|S )NTc                  T    t          dj         d t          j                   d S Nz	Skipping z% on sandcastle for following reason: r  r  rf   r  r  r8   r9   r  rd  s     r:   r   z?skip_but_pass_in_sandcastle.<locals>.decorator.<locals>.wrapperE  s3    ZdmZZRXZZadakllllFr;   )rt   r*  r+  r   )r  r   rd  s   ` r:   r5  z.skip_but_pass_in_sandcastle.<locals>.decorator?  sV     	%)D")/D&K	t	 	 	 	 	 
	 r;   rB   )rd  r5  s   ` r:   skip_but_pass_in_sandcastler  9  s$    
 
 
 
 
 r;   c                 j     t                      t                      fd            }|_        |S )z}
    Returns a function that calls the real implementation of a method
    in addition to passing args to a mock object.
    c                 ,     |i |  | g|R i |S r2   rB   )r   r8   r9   r  rU  s      r:   r   zmock_wrapper.<locals>.wrapperT  s8    dfvd,T,,,V,,,r;   )r   r   rU  )r  r   rU  s   ` @r:   mock_wrapperr  M  sK    
 ;;D
6]]- - - - - ]- GLNr;   c                 r    t          d | D             d |                                D             z             S )zC Returns a set of all Tensor objects in the given args and kwargs. c                 <    g | ]}t          |t                    |S rB   r   r   rL  s     r:   rb   z$get_tensors_from.<locals>.<listcomp>]  s'    ???z#v'>'>????r;   c                 <    g | ]}t          |t                    |S rB   r  )ra   r[  s     r:   rb   z$get_tensors_from.<locals>.<listcomp>^  s'    DDDajF.C.CDDDDr;   )r  r]  r7   s     r:   get_tensors_fromr  [  sG    ??t???DD6==??DDDE F F Fr;   	byte_listrP  rR  c                 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        t           j        t          j        t           j        t          j        t           j        t          j        t           j        t          j        t           j        t          j        i}||         }t          j        |          }d }|j        rt9          |           |dz  k    sJ  ||            |                    t          j        |z  | d |                    j        }|                    t          j        |z  | |d                     j        }|d|z  z   }	nIt9          |           |k    sJ  ||            |                    t          j        |z  |            j        }	t          j         |	||          S )Nc                 2    | D ]}d|cxk    rdk    sn J d S )Nr      rB   )r  bytes     r:   check_bytesz$bytes_to_scalar.<locals>.check_bytesu  sC     	$ 	$D###########	$ 	$r;   r   y              ?rx  )!r3   rn  ctypesc_int8rh  c_uint8uint16c_uint16uint32c_uint32uint64c_uint64int16c_int16rl  c_int32rP  c_int64rq   c_boolrQ  c_floatrS  float64c_double
complex128sizeofr+  rl  from_bufferc_byterZ   r  )
r  rP  rR  dtype_to_ctypectype	num_bytesr  rN  imagr  s
             r:   bytes_to_scalarr	  b  s   
FMV^fofofoV^V^V^
FMv~v&/.N 5!Ee$$I$ $ $  9~~)a-0000I  &-)";z	z""$ % %%* 	  &-)";yzz""$ % %%* 	R$Y9~~****I!:!   	 <F%8888r;   c                     t          j        | j        | j        | j        | j        | j                  }t          j        ||           }| j	        |_	        |S )zBBased on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard))rN   argdefsclosure)
r  FunctionType__code____globals__rf   __defaults__rb  r   update_wrapper__kwdefaults__)r  gs     r:   	copy_funcr	    sS    1:q}1:#$>#$=	2 	2 	2A 	 A&&A'AHr;   c                       fd}|S )z
    Given a list of test names which are defined by a superclass of the
    class this decorates, mark them as expected failure.  This is useful
    if you are doing poor man's parameterized tests by subclassing a generic
    test class.
    c                     D ]@}t          | |t          j        t          t	          | |                                         A| S r2   )r*  r   r  r	  r   )r+  r  r@  s     r:   decoz#xfail_inherited_tests.<locals>.deco  sK     	R 	RA CH4YwsA5O5OPPQQQQ
r;   rB   )r@  r	  s   ` r:   xfail_inherited_testsr	    s#         Kr;   c                       fd}|S )z
    Similar to unittest.skipIf, however in the sandcastle environment it just
    "passes" the test instead to avoid creating tasks complaining about tests
    skipping continuously.
    c                 r     r2t           rt                      fd            }|S d _         _         S )Nc                  T    t          dj         d t          j                   d S r  r  r  s     r:   r   zBskip_but_pass_in_sandcastle_if.<locals>.decorator.<locals>.wrapper  s4    bdmbbZ`bbilisttttttr;   T)rt   r   r*  r+  )r  r   r5  rd  s   ` r:   r5  z1skip_but_pass_in_sandcastle_if.<locals>.decorator  sb     	4 4tu u u u u u)-&-3*r;   rB   )r5  rd  r5  s   `` r:   r  r    s*          r;   c                 R    t          |                               d          d         S )zC Returns the pretty name of the dtype (e.g. torch.int64 -> int64). r   ri  )rl   rI  r  s    r:   rQ  rQ    s!    u::C  ##r;   c                      dt           fdd} fdt          |           D             }t          |          }t          |d| dz
                     S )zZMeasure and return approximate number of cycles per millisecond for torch.cuda._sleep
    r\   c                  l   t           j                            d          } t           j                            d          }|                                  t           j                            d           |                                 |                                 d|                     |          z  }|S )NT)enable_timingi@B )r3   r   Eventrw  _sleepr  r*  )r  endcycles_per_mss      r:   measurez"get_cycles_per_ms.<locals>.measure  s    
  t 44jT22
'"""

%"4"4S"9"99r;   r  c                 $    g | ]}             S rB   rB   )ra   rs   r&	  s     r:   rb   z%get_cycles_per_ms.<locals>.<listcomp>  s    ***!GGII***r;   r   )rT  r
  r  r   )numvalsr&	  s     @r:   get_cycles_per_msr*	    sj    
U    $ C****uSzz***D$<<DQq[!"""r;   Tr   samplesc                     	 t          t          |                    S # t          $ r}t          j        d          |d}~ww xY w)z
    Returns the first sample from an iterable of samples, like those returned by OpInfo.
    The test will be skipped if no samples are available.
    z%Skipped! Need at least 1 sample inputN)r   r2  StopIterationr   r  )r   r,	  r  s      r:   first_sampler/	    sS    
PDMM""" P P P GHHaOPs    
A=Ac                     t          | t          j                  rt          j        |           S t          | t                    r"t          t          t          |                     S | S r2   )r   r3   r   r  r   r-  r  clone_input_helperrB  s    r:   r1	  r1	    sV    %&& "{5!!!%"" 5S+U33444Lr;   c              #      K   	 t          | ||           dV  t          | |           dS # t          | |           w xY w)zBContext manager/decorator to test ONNX export with custom operatorNr#   )opnamesymbolic_fnopset_versions      r:   	custom_opr6	    sS      =#FKGGG%fm<<<<<%fm<<<<s   + =c                     | | }t          j        |          D ]K}t          |t          j                  r/|j        r(|                                                    d           Ld t          j        |          D             }t          j        |          D ]#}t          |t          j                  rd |_        $||fS )NT)retain_graphc                 P    g | ]#}t          |t          j                  |j        $S rB   )r   r3   r   grad)ra   inps     r:   rb   z"outs_and_grads.<locals>.<listcomp>  s+    [[[#ZU\=Z=Z[SX[[[r;   )	pytreetree_leavesr   r3   r   r  r  backwardr:	  )r   
graph_inpsinpsoutsr  gradsr;	  s          r:   outs_and_gradsrC	    s    2z?D!$'' 2 2c5<(( 	2S-> 	2GGIID111[[!3D!9!9[[[E!$''  c5<(( 	CH;r;   c                     t          |||          \  }}t          |||          \  }}|                     ||           |                     ||           d S r2   )rC	  rv  )r   m1m2r@	  r1g1r2g2s           r:   compare_equal_outs_and_gradsrK	    s^    Bd++FBBd++FBRRr;   c                   B    e Zd ZdZd ZddddddZddddddZd ZdS )	TestGradientsTc                 <    t                    fd            }|S )Nc                 @     |                                  g|R i |S r2   )r  )r  r8   r9   inplace_variants      r:   r  z,TestGradients._get_safe_inplace.<locals>._fn  s+    "?17799>t>>>v>>>r;   r  )r   rP	  r  s    ` r:   _get_safe_inplacezTestGradients._get_safe_inplace  s6    				? 	? 	? 	? 
 		? 
r;   FNcheck_forward_adcheck_backward_adr  check_batched_forward_gradc                   |dv sJ |                      d                               |t          j        |          j                  s-|                      dj         dt          |                      fd}
j        o|j        }	                    ||d|t                    }|D ]j        r |
          rd }t          j                  r3t          j        j        j                                                  }n@t%          t          j        fj        j                                                            }t%          d |D                       }|D ]}|                     |j        d	           d
 fd}|dk    rA|j        }|                     t/          |||dj        j        ||d|	
  
                   *|dv rp|                     |d           dD ]T}|j        dj        j        d}|dk    rd|d<   d|d<   d|d<   d|d<   |                     t9          ||fi |           U|                     dd           d S )N)r  bwgrad_bwgradfwgrad_bwgradz!Skipped! Variant not implemented.z	Skipped! z does not support dtype c                     t          | d          r| j                                        u S |                                 u S )N__wrapped__)r   rZ	  get_inplace)variantr  s    r:   
is_inplacez/TestGradients._check_helper.<locals>.is_inplace#  sB    w.. ?*bnn.>.>>>bnn....r;   T)r  include_conjugated_inputssmall_inputs_onlyc              3   \   K   | ]'}t          |t          j                  |j        #|V  (d S r2   )r   r3   r   r  r  s     r:   r   z.TestGradients._check_helper.<locals>.<genexpr><  s<      "l"l:a;V;V"l[\[j"l1"l"l"l"l"l"lr;   zA sampled input has a gradient before running autograd. This usually means that (at least) one input tensor is reused across different SampleInputs. Please create a new tensor for each SampleInput.c                 H   t          |          rbg }|D ]Y}t          |t          j                  r(|j        r!|                    | |                    |dz   }D|                    |           Z||fS t          |t          j                  r|j        r| |         |dz   fS ||fS r  )r  r   r3   r   r  rB  )r  r;	  r   tensor_listr   s        r:   _input_recomposition_helperz@TestGradients._check_helper.<locals>._input_recomposition_helperG  s    )#.. *"$K  2 2%a66 21? 2'..vi/@AAA(1AII'..q1111&	11U\22 *s7H *!),i!m;;	>)r;   c                     g }d} 	| j         |          \  }}|                    |           j        D ]'} 	| ||          \  }}|                    |           (i }j                                        D ]\  }} 	| ||          \  }}|||<    
j        g|R i |}j                            |          S |S r  )r.  rB  r8   r9   rd   gradcheck_wrapperoutput_process_fn_grad)r  positional_argsr   r;	  r   r9   ri  r[  rn  rc	  r  sampler\	  s            r:   r   z'TestGradients._check_helper.<locals>.fnV  s   "$	!<!<VV\S\!]!]Y&&s+++ 0 0A%@%@I%V%VNC#**3//// "M//11 $ $DAq%@%@I%V%VNC #F1II--gRRRR6RR0<!88@@@r;   r  )r  check_grad_dtypes
nondet_tolr  rS	  rT	  check_undefined_gradrU	  )rW	  rX	  z-Cannot run forward AD check for gradgradcheckr^  )FT)gen_non_contig_grad_outputsr  ri	  rj	  r  rX	  check_fwd_over_revFcheck_rev_over_revr  rk	  zUnknown check requested!)r  supports_dtyper3   rR  r   rN   rl   test_conjugated_samplesr+  sample_inputsr  broadcasts_inputr  r.  r   r8   r9   r]  r-  rn  r:	  r  r  r  gradcheck_nondet_tolgradcheck_fast_moder  check_batched_gradgradr  )r   rR  rP  r  r\	  checkrS	  rT	  r  rU	  r]	  r^	  r,	  all_argsgradcheck_argsr  r   rl	  r9   rc	  rh	  s      ``              @@r:   _check_helperzTestGradients._check_helper  sg   GGGGG?MM=>>>  V(<(<(ABB 	UMMSbgSSs5zzSSTTT	/ 	/ 	/ 	/ 	/
 %'$>$S5CS!""65`y5M # O O  ]	G ]	GF& ::g+>+>  H%fl33 ^ v{FM<P<P<R<RSS V]EYEYE[E[!\!\]]""l"lh"l"l"lllN $ V V!!!&#UV V V V* * *       , ##%-)+)>&	"n=O<@575L464J;K<M?CE_!a !a !a b b b b <<<  !17f ggg3@ Q Q/7R.0.G-1&(&=%'%; F //7;347<347<349>56OOM"n$O$O$O$OPPPPQ  +EFFFF{]	G ]	Gr;   c                >    |                      ||||d||||	  	        S )Nr  rR	  )ry	  )	r   rR  rP  r  r\	  rS	  rT	  r  rU	  s	            r:   _grad_test_helperzTestGradients._grad_test_helper  s8    !!&%Wk\l4EZl=W " Y Y 	Yr;   c                     ||                     t          j        |          j                  vr|                     d           |j        s|j        s|                     d           d S d S d S )Nz4Skipped! Op doesn't support autograd for this dtype.z Skipped! autograd not supported.)supported_backward_dtypesr3   rR  r   r  supports_autogradsupports_forward_ad)r   r  rR  rP  s       r:   _skip_helperzTestGradients._skip_helper  s}    44U\&5I5I5NOOOOMMPQQQ# 	>B,B 	>MM<=====	> 	> 	> 	>r;   )rf   rg   rh   r5  rQ	  ry	  r{	  r	  rB   r;   r:   rM	  rM	    s        K   TYlp)-%pG pG pG pG pGd QVim-1eY Y Y Y Y> > > > >r;   rM	  c                 p    d }|| _         dD ]'d d}fd}t          | | ||                     (| S )Nc                 "    || _         d | _        d S r2   )_cb_value)r   cbs     r:   	lazy_initz"make_lazy_class.<locals>.lazy_init  s    r;   )addrq  rq  truedivfloordivmodrW  powlshiftrshiftandorxornegposabsinverteqneltlegtgerq   r   r   __c                       dv fd}|S )N)rq   r   c                     | j          |                                  | _        d | _         s t          | j                  |i |S  t          t                    | j        g|R i |S r2   )r	  r	  r   r  )r   r8   r9   rN   use_operators      r:   r[  z7make_lazy_class.<locals>.inner_wrapper.<locals>.wrapped  sw    8'"&((**DK#DH# Q574;55tFvFFF278T224;PPPPPPPr;   rB   )rN   r[  r	  r  s   ` @r:   inner_wrapperz&make_lazy_class.<locals>.inner_wrapper  s:    #?:LQ Q Q Q Q Q Nr;   )r   r*  )r+  r	  rN   r	  r  s       @r:   make_lazy_classr	    s}       CL 0 0
 !H   	 	 	 	 	 	T==..////Jr;   c                   :    e Zd Zd Zd Zej        d             ZdS )NestedTensorTestCasec                     d }|                      t          j        ||          t          j        ||                     d S )Nc                     t          | t          j                  r0| j        r)| j        t          j        k    r|                                 S | S r2   )r   r3   r   rN  r  jaggedrO  r   s    r:   _unbind_njtszHNestedTensorTestCase.assertEqualIgnoringNestedInts.<locals>._unbind_njts  s@    !U\** q{ qx5<?W?Wxxzz!r;   )rv  r<	  tree_map)r   rZ  r  r	  s       r:   assertEqualIgnoringNestedIntsz2NestedTensorTestCase.assertEqualIgnoringNestedInts  sH    	 	 	 	q996?<YZ;[;[\\\\\r;   c                     |                      ||           d }t          j        ||          }t          j        ||          }|                     ||           d S )Nc                 V    t          | t          j                  r| j        r| j        nd S r2   )r   r3   r   rN  r  r   s    r:   _get_njt_shapeszGNestedTensorTestCase.assertEqualNoncontigAware.<locals>._get_njt_shapes  s0     a..34;r;   )r	  r<	  r	  rv  )r   rZ  r  r	  a_shapesb_shapess         r:   assertEqualNoncontigAwarez.NestedTensorTestCase.assertEqualNoncontigAware  si     	**1a000	 	 	 ??A66??A668,,,,,r;   c              #      K   t           j        j        j        }|j                                        }|j        }	 dV  ||_        ||_        dS # ||_        ||_        w xY w)z>Context manager to branch and restore the nested tensor state.N)r3   nested	_internalnested_tensor_tensor_symint_registryr{  _tensor_id_counter)r   nested_tensor_moduleoriginal_tensor_symint_registryoriginal_tensor_id_counters       r:   branch_nested_statez(NestedTensorTestCase.branch_nested_state  s}        %|5C*>*V*[*[*]*]'%9%L"	[EEE6P 3;Z 888 7Q 3;Z 8ZZZZs   A AN)rf   rg   rh   r	  r	  r'  r   r	  rB   r;   r:   r	  r	    sY        	] 	] 	]- - -  	[ 	[ 	[ 	[ 	[r;   r	  c                       e Zd ZdS )LazyValNr  rB   r;   r:   r	  r	    s        Dr;   r	  )r  suppress_prefixr  r_  c          	         !t          j                    d|z            j        t                    t          t	          |                     }fd}t          j        d||          }t          j        dd|          }t          j        dd|          }t          j        dd	|          }t          j        t          t          j        	                                        |          }t          j        t          t          j        
                    t          j                            t          j                  d
                    d
|          }|rft          j        dd
|t
          j                  }t          j        dd
|t
          j                  }t          j        dd
|t
          j                  }|rt          j        dd
|          }t          j        dd
|t
          j                  }|S )Nri  c                     |                      d          k    rdS |                      d          dk    rdS |                      d          S )Nri  r   r   z<module>r   )r5  )rO  r  s    r:   
repl_framezmunge_exc.<locals>.repl_frame  sI    771::2 771::##2wwqzzr;   z>  File "([^"]+)", line \d+, in (.+)\n(    .+\n( +[~^]+ *\n)?)+zline \d+zline Nz.py:\d+z.py:Nzhttps:/([a-zA-Z0-9_.-]+)z
https://\1r   z\n*Set TORCH_LOGS.+)flagsz$\n*You can suppress this exception.+z\n*Set TORCHDYNAMO_VERBOSE=1.+zCannot export model.+\n\nz +$)r   r   r   r   rl   r  rq  rK   r   r  rc   r  r3   r  DOTALL	MULTILINE)r  r  r	  r  r_  rG  r	  s      `   r:   r  r    s   |}q4x(1$Ds1vvA     	PR\^_``A
{Ha((A
z7A&&A
*M1==A
t^BG$4$4T$:$:;;Q??A
~bgll27??5>+J+JBOOPPRTVWXXA NF)2q	BBBF:BSSSF4b!29MMM 8F/Q77
vr1BL111AHr;   c              #     K   fd}	 t          j                     t          j        t           j                   g }|V  t          j                     |                    t          |t           j                             t          |          }|dk    rt          j	        | d           	 ddl
}t          j	        d|  d d           |d|          D ]}|                    |gd	           n$# t          $ r t          j	        d
           Y nw xY wt          j        d           dS # t          j        d           w xY w)a  Wrap around operations you want to ensure are not leaking tensor memory.

    This code intentionally ignores other reference cycles, which can be benign and which we have plenty
    of in pytorch code.  It focuses on any reference cycles that directly or indirectly result holding a Tensor alive,
    since this is likely a more serious leak than typical python refcycles.

    limit specifies how many tensors to dump debug graphs for (default=1)
    c                 $    t          |           S r2   )r   )r  matched_types    r:   	match_objz'check_leaked_tensors.<locals>.match_obj  s    #|,,,r;   r   zH tensors were found in the garbage. Did you introduce a reference cycle?NzDumping first z objgraphs of leaked zs rendered to pngr  )	max_depthz6`pip install objgraph` to enable memory leak debugging)r   r   	set_debugDEBUG_SAVEALLr  r  garbagerl  rv  rw  objgraphshow_backrefsImportError)limitr	  r	  garbage_objsnum_garbage_objsr	  r	  s    `     r:   check_leaked_tensorsr	  
  s     - - - - -


R%&&& 

F9bj99:::|,,aM#mmm  X`U`````   &fuf- > >A**A3"*====> X X XVWWWWWX 	QQs1   B#D- .AC3 2D- 3DD- DD- -Ec                      t          j                    } t          j                            |           rDt
          r%t          j        dd| gt          j                   dS t          j
        | d           dS dS )zK
    Removes the default root folder under which extensions are built.
    rmz-rf)r  T)ignore_errorsN)r/   get_default_build_rootrK   r   rt  r  r  r  r  r  r  )default_build_roots    r:    remove_cpp_extensions_build_rootr	  5  s     '=??	w~~()) B 	B ND%);<Z_UUUUUUM,DAAAAAAB Br;   c                    t           j                            | d          }t           j                            | d          }||fD ]5}t           j                            |          rt	          j        |           6t           j                            | d          }t          j        |dd|g}t          || t           j	                  }|dk    rt          d|            d }t          j        |          D ]/\  }}	}
|	D ]&}d|v r t           j                            ||          }'0|t          d	|            |t          j        vr"t          j                            d|           d S d S )
Nr  installzsetup.pyz--root)r  r  r   z"build failed for cpp extension at z	-packagesz)installation failed for cpp extension at )rK   r   rc   rt  r  r  r  r~  r  r  r  walkinsert)extension_root	build_dirinstall_dirr  setup_py_pathr  return_codemod_install_dirrootdirectoriesrs   	directorys               r:   install_cpp_extensionr	  C  sw   ^W55I',,~y99K%  7>>! 	M! GLL<<M>=)X{
KCRZ@@@KaPPPQQQO " 4 4 @ @k1$ 	@ 	@Ii''"$',,tY"?"?	@ W~WWXXXch&&?+++++ '&r;   c                 <     t                      fd            }|S )Nc                      d } | d|i|S )Nc                  $   t           rt          j        | i |S d|vsJ t                      5 }|                    dd          rt          d| dt          j                   ||d<   t          j        | i |cd d d            S # 1 swxY w Y   d S )Nbuild_directoryr  Fz$Using temporary extension directory z...r  )r  r/   load_inliner  r  r  r  r  )r8   r9   temp_dir_names      r:   r	  z8scoped_load_inline.<locals>.wrapper.<locals>.load_inlinee  s
    B$0$A&AAA$F2222')) B]::i// fSSSSZ]Zdeeee,9()$0$A&AA	B B B B B B B B B B B B B B B B B Bs   ABB	B	r	  rB   )r8   r9   r	  r  s      r:   r   z#scoped_load_inline.<locals>.wrapperc  s1    
	B 
	B 
	B tT={=f===r;   r  rB  s   ` r:   scoped_load_inliner	  a  s3    
4[[> > > > [> Nr;   c                 R    t           j        d             }  |            |           S )Nc               3     K   t           j        j        j        j        } t           j        j        j        j        }t           j        j        j        j        }t           j        j        j        j        }t           j        j        j        j        }t           j        j        j        j        }	 d V  | t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        d S # | t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        |t           j        j        j        _        w xY wr2   )	r3   r  mkldnnconvfp32_precisionrnnrF  cudnnr   )old_mkldnn_conv_pold_mkldnn_rnn_pold_mkldnn_matmul_pold_cudnn_conv_pold_cudnn_rnn_pold_cuda_matmul_ps         r:   recoverz,recover_orig_fp32_precision.<locals>.recoveru  sP     !N16E >04C#n3:I >/4C..2A!N/6E	JEEE8IEN!&57GEN!%4:MEN!(77GEN %46EEN $38IEN&555 9JEN!&57GEN!%4:MEN!(77GEN %46EEN $38IEN&5IIIIs   &E B$G2)r'  r   )r   r	  s     r:   recover_orig_fp32_precisionr	  t  s8    J J J" 7799R==r;   c                 .     t           j         fd}|S )Nc                 B     t                      fd            }|S )Nc                 |     j         j        j                  r | g|R i |S t          j        d          )NzPython version mismatch)majorminormicror   r  )r   r8   r9   r   	predicatevis      r:   r  z<skipIfPythonVersionMismatch.<locals>.dec_fn.<locals>.wrap_fn  sR    y28RX66 Cr$0000000'(ABBBr;   r  )r   r  r
  r
  s   ` r:   r  z+skipIfPythonVersionMismatch.<locals>.dec_fn  sE    	r	C 	C 	C 	C 	C 	C 
	C
 r;   )r  r  )r
  r  r
  s   ` @r:   skipIfPythonVersionMismatchr
    s1    		B      Mr;   rI  r2   )NNNNN)NNNNNri  F)r$  )r;  r  rG   )r\  )r`  )F)r  r  F)rf  (  r  argparser'  r{  r  r  r   r   r   r   ior   r  rX  r  rK   r  r   r  r  r  r  r  r  r  r  	threadingr  r  r   rv  collections.abcr   r   r   r   r   dataclassesr   enumr	   r
   r   	itertoolsr   r   r   
statisticsr   typingr   r   r   r   r   r   r   unittest.mockr   r  r  r  r  r3   torch.backends.cudnntorch.backends.mkltorch.backends.mpstorch.backends.xnnpack
torch.cudar   torch._Cr   r   torch._dynamo.trace_rulesr   torch._utils_internalr   torch._logging.scriber   torch.nnr   r   r    r!   r"   
torch.onnxr$   r%   torch.testingr&   torch.testing._comparisonr'   r(   r)   r*   r+   r,   $torch.testing._internal.common_dtyper-   torch.utils._import_utilsr.   torch.utils._pytreer  _pytreer<	  torch.utilsr/   r%  rF  r	  
MI300_ARCHr6   r=   r  rf   r   r  disable_global_flagsFILE_SCHEMArU   rn   rq   rj   rt   r  r  rz   r   _utils_internalr{   _is_fbcode_defaultr|   r~   r   r   r[   r   r   DEFAULT_DISABLED_TESTS_FILEDEFAULT_SLOW_TESTS_FILErx  ru  r   rL   r  r  rf  "DEVICE_LIST_SUPPORT_PROFILING_TESTALLOW_XPU_PROFILING_TESTcheck_namesr  r   r   r   r   r   r   r   r  r	  r6  r8  r@  rt  r}  r  r  r  r  r  ScriptFunctionr  r  ScriptMethodr  r  r  r  r  r  r   is_running_via_run_testArgumentParserparseradd_argumentrl   BooleanOptionalActionr  r  Threadhelp_threadr  rc   parse_known_argsr8   	remainingjit_executorr  r  r  r  rerun_disabled_testsr}  import_slow_testsr   import_disabled_testsr   
log_suffixr!  run_parallelr  test_bailoutsTEST_BAILOUTS
use_pytestr{  pytest_single_testr  discover_testsry  r|  save_xmlr   repeatr  r   r   
showlocalsrz  acceptr  UNITTEST_ARGSr  r  r  r  
CI_PT_ROOTr   CI_FUNCTORCH_ROOTr  r  r-  r  r  r  r  r  r  r  r(  r;  r  rQ  r  r"  r  r  machineIS_PPCIS_X86IS_ARM64r  r  IS_AVX512_VNNI_SUPPORTEDr  r  r  getfilesystemencodingIS_FILESYSTEM_UTF8_ENCODINGr@  TEST_FAIRSEQrV  mklr  TEST_MKLr	  ops_is_mkldnn_acl_supportedr  r   r  rT  mac_verrI  r  r   r  r  r  r   r  custom_device_modTEST_PRIVATEUSE1TEST_PRIVATEUSE1_DEVICE_TYPE
TEST_NUMBATEST_TRANSFORMERS	TEST_DILLTEST_LIBROSATEST_OPT_EINSUMTEST_Z3r  
NOTEST_CPUskipIfskipIfNoDillr  r  r  r  r  r  r  r  r  r  r  r  r  TEST_CUDA_GRAPHTEST_CUDA_CUDSSTEST_CUDA_PYTHON_BINDINGSr  r  r  	num_procsr  gb_availableset_per_process_memory_fractionr  
skipUnlessrequires_cudar  	overridesTorchFunctionModer  r  r
  r  r  torch._dynamor?  rC  accumulated_recompile_limitlog_compilation_metricsissue_3_13_0_warningtorch._inductor.configr&  fallback_randomr@  r  r  r  r   r#  r/  r:  rC  rJ  rO  r[  r_  rd  ri  TEST_WITH_TVfxexperimental_configtranslation_validationrt  bool_rh  r  r  r  rn  r  rl  rP  r  rQ  r	  rS  r	  rw  r{  r~  intcrd   r-  r  r,  	complex32inline_inbuilt_nn_modulesr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r1  r6  r5  rC  rG  rK  rO  rS  rW  r\  r_  re  rh  rn  rs  ry  r~  r  r   r  r  r  r  r  r  r  r  r  r  r  register_profileHealthChecktoo_slow	Verbositynormalr  load_profilerw  ImportWarningr  r   r%  r'  r?  rU  r`  rq  r}  r  r  r  r  _AssertRaisesContextr  r  r  r  r  r  ADDRESS_IN_USECONNECT_TIMEOUTr  r  r#  r  r(  r/  r6  r9  r<  r>  rC  rH  rJ  rL  rQ  rY  r]  re  rm  r  r  r  r  r  r  r  BytesIOr  GRADCHECK_NONDET_TOLr  skipIfSlowGradcheckEnvr  r  r  r  halfdtype2prec_DONTUSEr  r  r  r  r  r  r  rP  rR  r	  r	  r	  r  rQ  	lru_cacher*	  r+	  r/	  r1	  r6	  rC	  rK	  rM	  r	  r	  r	  r  r	  r	  r	  r	  r	  r
  rB   r;   r:   <module>r
     sR+                 				   				     				    				        



          - - - - - - - - . . . . . . . .       ! ! ! ! ! !       $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $                          / . . . . . . . # # # # # #                                     + + + + + + + + 4 4 4 4 4 4 3 3 3 3 3 3 6 6 6 6 6 6                     & % % % % %              < ; ; ; ; ; ? ? ? ? ? ? : : : : : : $ $ $ $ $ $ $ $ $ % % % % % %MMMJJ   JJJ 
B B B_X _X _X _X _X _X _X _XD g!!  # # % % %<7K &&;;	 '  t    &..BB	 /  t    Z^^$677tCd C C C GE!=11 &	% 
 "**!	 +  	4    &..% /  t    $3#;#; 0 $< $ $ D     /77,]	  8         ,;+F+F/ :: ,G , , 8C=    = +     29## H%oibi0A2&F&FGGO29"B'' P)/)")4I2*N*NOOux7]7]7_7_` &< " WWWCDDDDDDD	  (+    &        38L1 3 3 3 3! ! ! !?3 ?3 ?3 ?3 ?3 ?3 ?3 ?3B1 1 1 1 1 1 1 1h& & &R, , ,^; ; ; ; ; ; ; ;(CY CY CY CY CY# CY CY CYL4X 4X 4X 4X 4X% 4X 4X 4Xn<: <: <: <: <:" <: <: <:~    D   
$ $ $ G G G C C C : : : H#,	H!*	% % %# # #
5 5 55 5 5 $2   !/  5 5 5 (778Z+L+LL 	 	 .E*ETY	Z	Z	Z   N<C  E E E   H3  5 5 5   J|  4 4 4   $&6S  A A A   JS!  4 4 4   %'8  N N N   N<  8 8 8   L#//117<F11333$  H H H   &|  < < <   Nb  9 9 9   $3  : : :   )3F]  ^ ^ ^   -CsJe  f f f   ,\  B B B   *A  > > >   N8+ISX  Y Y Y   38x38++")"*;38+NNNK))++i  ")NN	+%%",NN	(""")NN 6577N0 (0 _
 "_
, #_ {y_
wz8U++ $J!	)  $    TT)")++&&''Sibikk"")**
CTT)")++%6%6%={KKLL        D0 0 0 0" 	/ / 39/ / / /d  H H H  5 5 5' ' '  & 'u 
 
 
 
#    ('S	 'd3i ' ' ' '$ ! [! [! [! [!z <7"\W$
<8#				y	(				1	18!558(   4355    ^     ^  $ $ $ ^$ $    ^
j j j 8c799WD !!'**
##I..!!'**
>**,,> --//_EI4D4]4]4_4_>**,,chh/x/11!4::3??CDDJKK9!!##GE5))Rei.D.D.F.FR44UJ##%%	GE58#I#I#K#KTRR 3355 $xEEGG !!'**
((88   ((	##I..?x<&&|44

t
$
$/# / / / / ((3VXZ)[)[\\\
x9}i88 "' $ & & &&//$ 0       /77,  8         '//$ 0      (00% 1      '//$ 0      !*	*G M MQT T &//$ 0      '//$ 0      +33( 4   D   
 ,44) 5   T     %8!8 	M V
]T55%-*;*A*A#*F*Fqs*K!L!LMMQTT 
 d!3!cEM<N<T<TUX<Y<YZ[<\8]8]ac8c00AA 	MF33u}177<<Q?@@BF   #! ! ! ! #	 	 	 ,44% y !<EZ[[[ 5   T     v%33IBI2C8899I:**,,Q/'9L	J..uulYQT_6TXd5dgp5prs/t/tuuu##EJ$;$;$=$=OO      5?4    !0 8 8( !9 ! !    
 ,44) 5   T    .66&HH 7   t   
 (7'?'?$4 (@ ( (    
  T79EM438EM005EM- T%%%%15.. 6T1S.M M M@ @ @M M MS S Si i i   8 M"9   *   $      & & & &<^ ^ ^ ^   . (,# ##s(#$#c3h# # # #N ry/00C7 @;?EH!8 "1!9!9. ": " " $    HEJHEKIELIELIELGEJHEKHEKHEKJEMJEMJEMLEOME$ *8 8 8    3
 */bg& `_=V=\=\=^=^___      	NBJ	OR\"    	>m"<   2!P     sCx      c3h    @ @ @   N                $V       ,                   2
W 
W 
W 
W 
W 
W 
W 
W 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@
Y 
Y 
Y 
Y 
Y 
Y 
Y 
YR  :   T T T T T T T T  (  
    ? ? ?        4 4 4         (- - -   - - - 9 9 97 7 7     ./ / / / / / / /4y( y( y( y( y( y( y( y(v @ @ @ " " "L$j4 4 4 ((#-#9#B"C *1	3 	3 	34 4 4 ((#-#9#B"C *1		3 	3 	34 4 4 ((#-#9#B"C *2		4 	4 	45 5 5 $$Q929-I5#Q#Q     j j jHMY[hiiiiij     K`(+ K` K` K` K`l&6 &6 &6 &6 &6 &6 &6 &6R: : : : :
 : : :z               2
 
 
 
 
~ 
 
 
,T T T T T4 T T T6       
    l       |          9 9 9 9 98=3U 9 9 9 'T ' ' ' '       
~G ~G ~G ~G ~Gz" ~G ~G ~GB0	 	 	 	 	8 	 	 	, , , ,(    *(#'    4   2 16U 	0 	0 	0 	0 @C% & & & & &4  *         AFUZ 	 	 	 	 	D D D  
I 
I 
I _d     *& & &>  
/C 
/s 
/ 
/QVQ] 
/ 
/ 
/ 
/   *6s 6s 6s 6sr1 1 13H 3H 3Hl    E E E	 	 	    RZ     !0!9!9. ": " " $   
 )K  : : :,L L L L*D D D # (4.     k4lDj$nd,     &   3C 3D 3 3 3 3  (  F F F%9tCy %9 %9el %9 %9 %9 %9P       *$ $ $
 #5 # # # #< GCLLPx( P8A; P1 P P P P   = = =	 	 	  H> H> H> H> H>H H> H> H>T  D&[ &[ &[ &[ &[8 &[ &[ &[R 	 	 	 	 	 	 	 	 %)$TPQ     D  u| ' ' ' 'TB B B, , ,<  &  *    s+   &E- -E76E7MDAQ* Q*ARRAR