o
    wZh                     @   s  d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	Z	d dl
Z	d dlZ	d dlZ	d dlZ	d dlZ	d dlZd dlZd dlmZ d dlmZmZmZmZmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$Z%d dl&Z&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. d dl/m0Z0m1Z1 e	j23 Z4e4oe	j25 dkZ6e4Z7e	j23 re	j8j9se	j:; Z<e=e	j25 D ]Z>e	j2?e>d  Z@e@dk rdZ7qdd ZAdd ZBdd ZCdd ZDG dd dZEdZFG dd deZGG d d! d!ZHed"d# ZIed$d% ZJedId'd(ZKd)d* ZLd+d, ZMd-d. ZNd/d0 ZOd1d2 ZPd3d4 ZQd5d6 ZRd7d8 ZSd9d: ZTd;d< ZUd=d> ZVd?d@ ZWG dAdB dBZXdCdD ZYdEdF ZZdGdH Z[dS )J    Variable_nested_map)BroadcastingList2BroadcastingList3)OperatorExportTypesN)	FileCheck)
IS_WINDOWSfreeze_rng_state)enable_profiling_mode_for_profiling_testsProfilingModeTEST_BAILOUTSis_iterable_of_tensors)JitCommonTestCase)enable_profiling_mode)contextmanager)reduce)StringIO)defaultdict)Loader)AnyUnion      Fc                 C   s   t | || d S N)exec)codeglobloc r    P/var/www/auris/lib/python3.10/site-packages/torch/testing/_internal/jit_utils.pyexecWrapper7   s   r"   c                 C   s   t dd | |S )Nc                 S   s   t | tjS r   )
isinstancetorchTensortr    r    r!   <lambda>;       zdo_input_map.<locals>.<lambda>r   fninputr    r    r!   do_input_map:      r-   c                   C   s,   t j  t jj t jj_t jj  d S r   )	r$   _CZ_jit_clear_class_registryjit
_recursiveZConcreteTypeStoreZconcrete_type_store_stateZ_clear_class_stater    r    r    r!   clear_class_registry=   s   
r3   c                 C   s4   t | j }t|}|dkrtd| |d S )Nr   zOThis test assumes this GraphExecutor should only have one execution plan, got: r   )listexecution_plansvalueslenRuntimeError)Zgraph_executor_stater5   Z	num_plansr    r    r!   get_execution_planB   s   r9   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	&_AssertRaisesRegexWithHighlightContextz~
    A context manager that is useful for checking that error messages highlight
    the correct part of the source code.
    c                 C   s   || _ || _|| _|| _d S r   )	test_caseexception_typeregex	highlight)selfr;   	exceptionr=   r>   r    r    r!   __init__P   s   
z/_AssertRaisesRegexWithHighlightContext.__init__c                 C   s   | S r   r    r?   r    r    r!   	__enter__V   s   z0_AssertRaisesRegexWithHighlightContext.__enter__c                 C   s\   | j | j| j |r|W d    n1 sw   Y  | jr,t | jt| dS NT)	r;   assertRaisesRegexr<   r=   r>   r	   Zcheck_source_highlightedrunstr)r?   typevalue	tracebackr    r    r!   __exit__Y   s   z/_AssertRaisesRegexWithHighlightContext.__exit__N)__name__
__module____qualname____doc__rA   rC   rK   r    r    r    r!   r:   J   s
    r:   zprim::TensorExprGroupc                       s$  e Zd ZdZdZG dd deZG dd deZdd Zd	d
 Z	 fddZ
 fddZd8ddZdd Zdd Zdd Zdd Zd9ddZd:ddZd:dd Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zdddd+ejfd,d-Zd.d/ Zd0dddd+ejddfd1d2Z				d;d4d5Zd6d7 Z  Z S )<JitTestCaseTFc                   @       e Zd ZdZdd Zdd ZdS )zJitTestCase.capture_stdoutz>
        Replace sys.stdout with a temporary StringIO
        c                 C      t j| _t | _| jt _| S r   )sysstdout
sys_stdoutr   stringiorB   r    r    r!   rC   m      z$JitTestCase.capture_stdout.__enter__c                 G   $   |  t| j  | `| jt_d S r   )appendrG   rV   getvaluerU   rS   rT   r?   argsr    r    r!   rK   s      z#JitTestCase.capture_stdout.__exit__NrL   rM   rN   rO   rC   rK   r    r    r    r!   capture_stdouti       r_   c                   @   rQ   )zJitTestCase.capture_stderrz>
        Replace sys.stderr with a temporary StringIO
        c                 C   rR   r   )rS   stderr
sys_stderrr   rV   rB   r    r    r!   rC   |   rW   z$JitTestCase.capture_stderr.__enter__c                 G   rX   r   )rY   rG   rV   rZ   rb   rS   ra   r[   r    r    r!   rK      r]   z#JitTestCase.capture_stderr.__exit__Nr^   r    r    r    r!   capture_stderrx   r`   rc   c                 C   s   t j| j| j d S r   )r$   r/   _jit_set_emit_hooksemitModuleHookemitFunctionHookrB   r    r    r!   setHooks      zJitTestCase.setHooksc                 C   s   t jd d  d S r   )r$   r/   rd   rB   r    r    r!   
clearHooks   r.   zJitTestCase.clearHooksc                    s.   t    tjstjj  dt_|   d S rD   )	supersetUprP   _restored_warningsr$   r0   ZTracerWarningZignore_lib_warningsrg   rB   	__class__r    r!   rk      s
   
zJitTestCase.setUpc                    s   t    |   t  d S r   )rj   tearDownri   r3   rB   rm   r    r!   ro      s   

zJitTestCase.tearDownr    c                    s   fdddt dddddht|B  tt}|t | | t|d	kd
|  tt| \}}| t|d	kd
|  | t	 fdd|
 D d
|  d S )Nc                    s   |   D ]b}| |kr||  | q| dkr$ |d|| q| dkrY|    dksN|    dksN|    dkrY |  || q| D ]} ||| q]qd S )Nzprim::DifferentiableGraphZSubgraphprim::Ifz	aten::allprim::TypeCheckprim::RequiresGradCheck)nodeskindrY   ginputs__next__nodeblocks)blockrt   accrx   Zinner_block)!get_nodes_and_parents_recursivelyr    r!   r|      s   $zEJitTestCase.assertAllFused.<locals>.get_nodes_and_parents_recursivelyzprim::Constantzprim::BailoutTemplatezprim::TupleConstructrp   rq   rr   r   zgot c                 3   s    | ]	}|   v V  qd S r   )rt   ).0rx   )allowed_nodesr    r!   	<genexpr>   s    z-JitTestCase.assertAllFused.<locals>.<genexpr>)FUSION_GROUPsetr   r4   
assertTruer7   nextiteritemsallrs   )r?   graphZ
except_forZfusion_groupsZfusion_nodesr    )r~   r|   r!   assertAllFused   s   zJitTestCase.assertAllFusedc                 C   s(   t |}d}|D ]	}||v r dS qdS )N)z Could not export Python functionzclosures are not exportableTF)rG   )r?   eseallowedar    r    r!   _isHookExceptionOk   s   zJitTestCase._isHookExceptionOkc                    s   fdd}t j  z?t|jdkrW W d    d S t|t jjr5t| dkr5W W d    d S t	
 }t j|| | }||\}}W n tyj } z |sY W Y d }~W d    d S d }~ww t	
|}t j|}	t	
 }
t j|	|
 |
d ||
\}}t||D ]
\}} || qt|t jjr t j||	j W d    d S W d    d S 1 sw   Y  d S )Nc                    s   t |  tt  t   ttdd   }tdd |} fdd|D }dd |D }tdd |} fdd|D }d	d |D }||fS )
Nc                 S   
   |  dS )Nzarchive/code/
startswithxr    r    r!   r(         
 zKJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<lambda>c                 S   r   )Nz.pyendswithr   r    r    r!   r(      r   c                 3       | ]}  |V  qd S r   openr}   farchiver    r!   r          zLJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>c                 s   s$    | ]}d  dd |D V  qdS ) c                 S   s   g | ]}|  qS r    )decode)r}   liner    r    r!   
<listcomp>   s    zWJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>.<listcomp>N)join)r}   filer    r    r!   r      s   " c                 S   r   )Nz
.debug_pklr   )r   r    r    r!   r(      r   c                 3   r   r   r   r   r   r    r!   r      r   c                 s   s    | ]}t |V  qd S r   )pickleloadr   r    r    r!   r      r   )zipfileZipFileassertEqualr7   r   namelistr4   filter)bufferfilesZcode_files_strZcode_files_stream
code_filesZdebug_files_strZdebug_files_streamZdebug_filesrB   r   r!   extract_files   s   
 z9JitTestCase._compared_saved_loaded.<locals>.extract_filesr   )r$   Z_jit_internalZ_disable_emit_hooksr7   r   r#   r/   ZScriptModuleZ_method_namesioBytesIOr0   saverZ   r8   r   r   seekzipZassertMultiLineEqualr   Z_ivalue_tags_match_c)r?   mr   r   Zbuffer_copyr   Z_debug_filesr   Zbuffer2importedZsaved_module_buffer_2Zcode_files_2Z_debug_files_2r   br    rB   r!   _compared_saved_loaded   sD   




'"z"JitTestCase._compared_saved_loadedc                 C   s&   |j dks
d|j v rd S | | d S )Nz<lambda>zaten::)namer   )r?   funcr    r    r!   rf     s   zJitTestCase.emitFunctionHookc                 C   s   |  | d S r   )r   )r?   moduler    r    r!   re   	     zJitTestCase.emitModuleHookNc              	   C   s   t  }|dd  tj|| |dd  |d tjj||d}|dd  |s1|S tj	dd}z|
  ||j tjj|j|d}W t|j nt|j w |d	d  |S )
Nc                 S      | j dr
|  S d S )N_pack)r   _has_methodr   sr    r    r!   r(         z<JitTestCase.getExportImportCopyWithPacking.<locals>.<lambda>c                 S   r   N_unpackr   r   r   r   r    r    r!   r(     r   r   )map_locationc                 S   r   r   r   r   r    r    r!   r(     r   F)deletec                 S   r   r   r   r   r    r    r!   r(   $  r   )r   r   applyr$   r0   r   r   r   tempfileNamedTemporaryFilecloser   osunlink)r?   r   Zalso_test_filer   r   r   r   resultr    r    r!   getExportImportCopyWithPacking  s"   
z*JitTestCase.getExportImportCopyWithPackingc                    sd   |rt |}| |d   }| |dk d S  fdd|}| t|dk d S )Nwith r   c                    F   g }|   D ]}|  kr|| | D ]} || 7 }qq|S r   rs   rt   rY   ry   rz   outrx   rt   rs   r    r!   rs   /     
z.JitTestCase.assertGraphContains.<locals>.nodes)rG   countr   r7   )r?   r   rt   consider_subgraphsstrgraphr   	out_nodesr    r   r!   assertGraphContains'  s   	zJitTestCase.assertGraphContainsc           	         sp   dd }|r!t |}| |d   }|| ||| d S  fdd|}|| t||| d S )Nc              
   S   s<   ||krd S |r
dnd}t |  d| d| d| d| 	)NZ	includingZ	excludingz
Error: graph contains  z nodes (z subgraphs) but expected )AssertionError)r   rt   actualexpectedr   Zsubgraphr    r    r!   perform_assert<  s   z>JitTestCase.assertGraphContainsExactly.<locals>.perform_assertr   c                    r   r   r   r   r   r    r!   rs   J  r   z5JitTestCase.assertGraphContainsExactly.<locals>.nodes)rG   r   r7   )	r?   r   rt   Znum_kind_nodesr   r   r   r   r   r    r   r!   assertGraphContainsExactly;  s   
	z&JitTestCase.assertGraphContainsExactlyc                 O   s.   t jj|tjd}| j|g|R i | d S )N)Zoperator_export_type)r$   ZonnxZ_optimize_tracer   ZONNXassertExpectedGraph)r?   ru   r\   kwargsr    r    r!   assertExpectedONNXGraphW  s   z#JitTestCase.assertExpectedONNXGraphc                 O   sx   t |tjjr
|}n| }tj| tj| tj| tj|}tj| | jt	|g|R i | d S r   )
r#   r$   r/   Graphr   _jit_pass_lintZ_jit_pass_dceZ_jit_pass_canonicalizeassertExpectedrG   )r?   tracer\   r   r   r    r    r!   r   [  s    zJitTestCase.assertExpectedGraphc                 C   sx   t |tjjr|}d}nd}| }tj| ttjd| |}|d ur-t |ts-|}tj| |r:|| |S )NFTZ
_jit_pass_)	r#   r$   r/   r   r   r   getattrbool	set_graph)r?   r   r   r   r   r   r    r    r!   run_passh  s   
zJitTestCase.run_passc                 C   sj   t  }|s
tdd}||d k r%|j}|std|d7 }||d k si }||j ||j |S )Nzfailed to inspect framer   r   zfailed to get frame)inspectcurrentframer8   f_backupdatef_locals	f_globals)r?   	frames_upframeiZdefined_varsr    r    r!   get_frame_varsz  s   zJitTestCase.get_frame_varsc                 C   s   t | |||S r   )r:   )r?   r@   r=   r>   r    r    r!   assertRaisesRegexWithHighlight  r   z*JitTestCase.assertRaisesRegexWithHighlightr   c
              	   C   s  t   | ||+ t|tr(| |}
i }t||
|d |
| |
| }n|}||  W d   n1 s8w   Y  | ||5 t|trWtjj	||d}t
||}ntt|}tjj	||d}t
||j}||  W d   n1 s{w   Y  t|ts| || tj|}||  W d   n1 sw   Y  W d   dS W d   dS W d   dS 1 sw   Y  dS )z
        Checks that a given function will throw the correct exception,
        when executed with normal python, the string frontend, and the
        AST frontend. Logic taken from `checkScript` (see comments there
        for details)
        r   r   NZ
_frames_up)r   rE   r#   rG   r   r"   r   r$   r0   CompilationUnitr   textwrapdedentr   	getsourcerL   script)r?   r   rv   r@   r=   r   outputscapture_outputr   	profilingr   
the_locals	python_fncuZstring_frontendsourceger    r    r!   checkScriptRaisesRegex  s<   	








"z"JitTestCase.checkScriptRaisesRegexc           	      C   sN   |  }t|}|j }td|D ]}|j| || }| || qd S Nr   )Zget_debug_stater9   r   num_bailoutsrangeZrequest_bailoutr   )	r?   modelrv   r   stateZplanr	  r   Zbailout_outputsr    r    r!   checkBailouts  s   
zJitTestCase.checkBailoutsr   c                 C   sJ  t j| t  tdd |D }t|tr?t jj||d}| |}i }t	|||d |
| || }t||}n tt|}| j|||j||||dd t jj|dd}|}|ritdd	 |}n|}|r|  }|| }W d
   n1 sw   Y  |   || }W d
   n1 sw   Y  |   || }W d
   n1 sw   Y  ts| j|d dd | j|||	|
d n|| }|s|r|| }|| }tr| ||| || }| j|||	|
d | j|||	|
d |W  d
   W  d
   S 1 sw   Y  W d
   d
S 1 sw   Y  d
S )z|
        Checks that a given script generates the same output as the Python
        version using the given inputs.
        c                 s   s"    | ]}t |tjo|jV  qd S r   )r#   r$   r%   requires_grad)r}   r   r    r    r!   r     s     z*JitTestCase.checkScript.<locals>.<genexpr>r   r      )optimizeinputs_requires_gradr   r  r   r   c                 S      |    S r   )detachrequires_grad_r&   r    r    r!   r(     r)   z)JitTestCase.checkScript.<locals>.<lambda>Nr   rT   )subnameatolrtol)r$   r0   Zoptimized_executionr   anyr#   rG   r   r   r"   r   r   r   r   r   r   checkScriptrL   r   r-   r_   r
   r   r   r   r  )r?   r   rv   r   r  r  r   r   r  r  r  Zextra_profile_runsr  r   r  r  Zscripted_fnr  recording_inputsZscript_stdoutZscript_outputsZopt_script_outputsZpython_outputsr    r    r!   r    sl   








$zJitTestCase.checkScripth㈵>c                    s
   fdd}|d u r|}fdd}|}|r"t dd ||}n|tjj||||
dd}|	r6| |}|r=t|j || }|| }| || | }|r\tjj	||||d	}| }|rmtjj	||||d	}| || |r~| j||||d
 | }||}|rtjj	||d|d}|r||| }tjj	|||d	}|rt dd ||}| }||}|rtjj	||d|d}|r||| }tjj	|||d	}| || |r| j||||d
 t
||D ]\}}|d u r|d u rq| j||ddd
 q|S )Nc                    s,    d ur| d    } t dd t| D S )Nc                 s   s2    | ]\}}|d urt |d |  V  qd S )Nr  )mathlogsum)r}   r   vr    r    r!   r     s   0 z9JitTestCase.checkTrace.<locals>.allSum.<locals>.<genexpr>)r  	enumerate)vs)dropr    r!   allSum  s   z&JitTestCase.checkTrace.<locals>.allSumc                    s     fdd t  dd g S )Nc                    sZ   t tjr | |S t tr t fdd| |S t fdd| |S )Nc                    s   |  | S r   r    )r{   key)r+   r,   input_reducer    r!   r(   %  s    zVJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reduce.<locals>.<lambda>c                    s   | | S r   r    )r{   val)r+   r&  r    r!   r(   '  r)   )r#   r$   r%   dictr   )r,   r+   r{   r&  r*   r!   r&  !  s   

zDJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reducec                 S   s
   | | S r   )rY   )r'   r{   r    r    r!   r(   )  r   z@JitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.<lambda>)tuple)rv   )r  r)  r!   flatten_inputs   s   z.JitTestCase.checkTrace.<locals>.flatten_inputsc                 S   r  r   )cloner  r&   r    r    r!   r(   -  r)   z(JitTestCase.checkTrace.<locals>.<lambda>F)check_tolerance_force_outplaceZcheck_trace)allow_unusedr  T)Zcreate_graphr/  c                 S   s   t | ddS )NT)r  r   r&   r    r    r!   r(   \  r)   g-C6J?)r-   r$   r0   r   ZgetExportImportCopyprintr   r   autogradgradr   )r?   r   Zreference_tensorsZinput_tensorsr#  r/  verboseZinputs_require_gradsr-  Zexport_importr.  Z	grad_atolZ	grad_rtolr$  r+  Znograd_inputsZflattened_recording_inputsr  r   Z
outputs_geZgradsZgrads_gel1l2Zgrads2Zl1_geZl2_geZ	grads2_geg2Zg2_ger    )r#  r  r!   
checkTrace  sz   


zJitTestCase.checkTracec                 C   s   t j|}t  || }W d   n1 sw   Y  t  || }W d   n1 s/w   Y  | || | || |S )zq
        Check that a nn.Module's results in Script mode match eager and that it
        can be exported
        N)r$   r0   r   r   r   ZassertExportImportModule)r?   Z	nn_moduler\   smZ	eager_outZ
script_outr    r    r!   checkModules  s   

zJitTestCase.checkModule)r    )TN)F)
NNFFTr  TFNN)!rL   rM   rN   Z_do_cuda_memory_leak_checkrl   r4   r_   rc   rg   ri   rk   ro   r   r   r   rf   re   r   r   r   r   r   r   r   r   r   Z	PROFILINGr  r  r  r7  r9  __classcell__r    r    rm   r!   rP   e   sR    

	>



*
P
drP   c                   @   s   e Zd Zdd Zdd ZdS )NoTracerWarnContextManagerc                 C   s   t j | _t jd d S NF)r$   r/   Z_jit_get_tracer_state_warnprev_jit_set_tracer_state_warnrB   r    r    r!   rC     s   z$NoTracerWarnContextManager.__enter__c                 G   s   t j| j d S r   )r$   r/   r>  r=  r[   r    r    r!   rK     r.   z#NoTracerWarnContextManager.__exit__N)rL   rM   rN   rC   rK   r    r    r    r!   r;    s    r;  c              	   c   @    t j }t j|  zd V  W t j| d S t j| w r   )r$   r/   Z_jit_get_inline_everything_modeZ_jit_set_inline_everything_mode)Zshould_inlineoldr    r    r!   inline_everything_mode     
rA  c              	   c   r?  r   )r$   r/    _debug_get_fusion_group_inlining _debug_set_fusion_group_inlining)Zinliningr@  r    r    r!   set_fusion_group_inlining  rB  rE  Tc              	   c   s8    t j|   zd V  W t jd d S t jd w rD   )r$   r/   Z%_debug_set_autodiff_subgraph_inlining)enabledr    r    r!   "disable_autodiff_subgraph_inlining  s
   rG  c                       t   fdd}|S )Nc                     <   t d  | i | W d    d S 1 sw   Y  d S rD   rA  r\   r   r+   r    r!   wrapper     
"z#_inline_everything.<locals>.wrapper	functoolswrapsr+   rM  r    rL  r!   _inline_everything     rS  c                    rH  )Nc                     rI  r<  rJ  rK  rL  r    r!   rM    rN  z5_tmp_donotuse_dont_inline_everything.<locals>.wrapperrO  rR  r    rL  r!   $_tmp_donotuse_dont_inline_everything  rT  rU  c                     s    fdd}|S )Nc                    s   t jj|  fi S r   )r$   r0   r   )r   rK  r    r!   rM    rh   z_trace.<locals>.wrapperr    )r\   r   rM  r    rK  r!   _trace  s   rV  c                        fdd}|S )Nc               	      s   t jd t jd t jd z | i | W t jd t jd t jd d S t jd t jd t jd w NTF)r$   r/   Z$_jit_override_can_fuse_on_cpu_legacy_jit_override_can_fuse_on_cpu_jit_set_te_must_use_llvm_cpurK  rL  r    r!   rM    s   z!enable_cpu_fuser.<locals>.wrapperr    rR  r    rL  r!   enable_cpu_fuser  s   
r[  c                 C   s   | rt S dd }|S )Nc                    rW  )Nc                     s    | i |S r   r    rK  rL  r    r!   rM    r   z8enable_cpu_fuser_if.<locals>.noop_fuser.<locals>.wrapperr    rR  r    rL  r!   
noop_fuser  s   z'enable_cpu_fuser_if.<locals>.noop_fuser)r[  )Zcondr\  r    r    r!   enable_cpu_fuser_if  s   r]  c                 C   r   Nforward)_get_methodcr    r    r!   get_forward  s   
rc  c                 C   s   |  djS r^  )r`  r   ra  r    r    r!   get_forward_graph  s   rd  c                 C   s   | j ||S r   )r   r   r`  )r   r   methodr    r    r!   get_module_method  r.   rf  c                    s    fdd| j j D S )Nc                    s   g | ]\}}|  r|qS r    r   )r}   r   _prefixr    r!   r     s    
z%attrs_with_prefix.<locals>.<listcomp>)Z_modulesr   r   )r   ri  r    rh  r!   attrs_with_prefix  s   rj  c                 G   sR   d}g }t |D ]}t|dkr tjj| g|R  }|| q| jdd q|S )N   r   T)Zretain_graph)r
  r7   r$   r1  r2  rY   Zbackward)r   r\   Zprofiling_countresultsrg  rr    r    r!   warmup_backward  s   rn  c                  G   s$   | D ]}t tj|j |j| qd S r   )setattrrS   modulesrM   rL   )r\   argr    r    r!   make_global  s   rr  c           	   	   C   s   t  L}tj|d}t|d}||  W d    n1 s!w   Y  tj	||}tj
|}|j}t|ts=J || t||}|W  d    S 1 sSw   Y  d S )Nz	script.pyw)r   TemporaryDirectoryr   pathr   r   write	importlibutilspec_from_file_locationmodule_from_specloaderr#   r   exec_moduler   )	r   fn_nameZtmp_dirZscript_pathr   specr   r{  r+   r    r    r!   _get_py3_code  s   


$r  c                   @   s   e Zd ZdddZdd ZdS )TensorExprTestOptionsreturnNc                 C   s   t jd| _t jd| _t j | _t j | _	t j
d t jd t j | _t jd t j | _t jd t j | _t jd d S rX  )r$   r/   _jit_set_profiling_executorold_profiling_executor_get_graph_executor_optimizeold_profiling_modeZ_jit_can_fuse_on_cpuold_cpu_fuser_stateZ_jit_can_fuse_on_gpuold_gpu_fuser_staterY  _jit_override_can_fuse_on_gpuZ_jit_texpr_fuser_enabledtexpr_fuser_state_jit_set_texpr_fuser_enabledrC  old_fusion_inliningrD  Z_jit_get_te_must_use_llvm_cpuold_te_must_use_llvm_cpurZ  rB   r    r    r!   rA     s   zTensorExprTestOptions.__init__c                 C   sf   t j| j t j| j t j| j t j| j	 t j
| j t j| j t j| j d S r   )r$   r/   r  r  r  r  r  r  r  r  rY  r  rD  r  rZ  r  rB   r    r    r!   restore  s   zTensorExprTestOptions.restore)r  N)rL   rM   rN   rA   r  r    r    r    r!   r    s    
r  c                 C   sZ   g }| D ]&}t |tjr||   qt|r%|dd |D  q|| q|S )Nc                 S   s   g | ]}|   qS r    )r  r,  )r}   r'   r    r    r!   r   %  r   z clone_inputs.<locals>.<listcomp>)r#   r$   r%   rY   r  r,  r   )r\   rv   rq  r    r    r!   clone_inputs  s   r  c              	      sF  g }  | |}  }  }||d} jdv }|r%dttj ji}ddgdddgdddgdd	d
gdddgdddgdddgdg}	|rN|S |D ]P}
| D ]I}|d u r]qVt|rbqVt	 fdd|	}|D ]*}|d D ]#}t
|
j}t|
j|krt|
j| trt|| ||< t||
_qsqm|||
f qVqP|S )N)functionre  )Zresize_Z
resize_as_re  Zdiv_floor_roundingr   )r   arg_idxZdiv_no_rounding_modeZdiv_trunc_roundingZ
index_fillr  Z	full_likemulZnew_fullr   c                    s    j | d kS )Nr   )Zformatted_namer   opr    r!   r(   n  s    z1get_traced_sample_variant_pairs.<locals>.<lambda>r  )Zsample_inputsZget_op
get_methodr   r   r$   r%   r6   	is_lambdar   r4   r\   r7   r#   r   intr*  rY   )ZdeviceZdtyper  r   Zsamplesr   re  variantsZhas_fake_functionZops_with_unsupported_bool_argssamplevariantZmatching_opsZop_dataidxr\   r    r  r!   get_traced_sample_variant_pairs+  sd   
 
r  c                 C   s"   dd }t | t|o| j|jkS )Nc                   S   s   dS r  r    r    r    r    r!   r(   |  s    zis_lambda.<locals>.<lambda>)r#   rH   rL   )ZlambZLAMBDAr    r    r!   r  {  s   r  )T)\Ztorch.autogradr   Ztorch.autograd.functionr   Ztorch.jit.annotationsr   r   Z
torch.onnxr   r$   Z
torch.cudaZ	torch.jitZtorch.jit._loggingZtorch.jit.frontendZtorch.jit.quantizedr   rP  Ztorch.testingr	   Z$torch.testing._internal.common_utilsr
   r   r   r   r   r   Z"torch.testing._internal.common_jitr   r   
contextlibr   r   r   r   collectionsr   importlib.utilrw  r   r  r   r   rS   r   r   importlib.abcr   typingr   r   cudaZis_availableZRUN_CUDAZdevice_countZRUN_CUDA_MULTI_GPUZRUN_CUDA_HALFversionZhipr/   Z_cuda_getCompiledVersionZCUDA_VERSIONr
  dZget_device_capabilitymajorr"   r-   r3   r9   r:   r   rP   r;  rA  rE  rG  rS  rU  rV  r[  r]  rc  rd  rf  rj  rn  rr  r  r  r  r  r  r    r    r    r!   <module>   s    

    $

		
P