U
    Mh΄                     @   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/m0Z0m1Z1m2Z2m3Z3m4Z4 e	j56 Z7e7ote	j58 dkZ9e7Z:e	j56 re	j;j<se	j=> Z?e@e	j58 D ]$ZAe	j5BeAd  ZCeCdk rdZ:qdd ZDdd ZEdd ZFdd ZGG dd dZHdZIG dd deZJG d d! d!ZKed"d# ZLed$d% ZMedId'd(ZNd)d* ZOd+d, ZPd-d. ZQd/d0 ZRd1d2 ZSd3d4 ZTd5d6 ZUd7d8 ZVd9d: ZWd;d< ZXd=d> ZYd?d@ ZZG dAdB dBZ[dCdD Z\dEdF Z]dGdH 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DictListTupleUnion      Fc                 C   s   t | || d S N)exec)codeglobloc r#   S/var/www/html/venv/lib/python3.8/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:   s    r0   c                   C   s,   t j  t jj t jj_t jj  d S r   )	r'   _CZ_jit_clear_class_registryjitZ
_recursiveZConcreteTypeStoreZconcrete_type_store_stateZ_clear_class_stater#   r#   r#   r$   clear_class_registry=   s    
r4   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_stater6   Z	num_plansr#   r#   r$   get_execution_planB   s
    r:   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   sH   | j | j| j |r|W 5 Q R X | jrDt | 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__rB   rD   rL   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                   @   s    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   s   t j| _t | _| jt _| S r   )sysstdout
sys_stdoutr   stringiorC   r#   r#   r$   rD   m   s    z$JitTestCase.capture_stdout.__enter__c                 G   s$   |  t| j  | `| jt_d S r   )appendrH   rU   getvaluerT   rR   rS   r@   argsr#   r#   r$   rL   s   s    z#JitTestCase.capture_stdout.__exit__NrM   rN   rO   rP   rD   rL   r#   r#   r#   r$   capture_stdouti   s   r[   c                   @   s    e Zd ZdZdd Zdd ZdS )zJitTestCase.capture_stderrz>
        Replace sys.stderr with a temporary StringIO
        c                 C   s   t j| _t | _| jt _| S r   )rR   stderr
sys_stderrr   rU   rC   r#   r#   r$   rD   |   s    z$JitTestCase.capture_stderr.__enter__c                 G   s$   |  t| j  | `| jt_d S r   )rV   rH   rU   rW   r]   rR   r\   rX   r#   r#   r$   rL      s    z#JitTestCase.capture_stderr.__exit__NrZ   r#   r#   r#   r$   capture_stderrx   s   r^   c                 C   s   t j| j| j d S r   )r'   r1   _jit_set_emit_hooksemitModuleHookemitFunctionHookrC   r#   r#   r$   setHooks   s    zJitTestCase.setHooksc                 C   s   t jd d  d S r   )r'   r1   r_   rC   r#   r#   r$   
clearHooks   s    zJitTestCase.clearHooksc                    s.   t    tjs"tjj  dt_|   d S rE   )	supersetUprQ   _restored_warningsr'   r2   ZTracerWarningZignore_lib_warningsrb   rC   	__class__r#   r$   re      s
    
zJitTestCase.setUpc                    s   t    |   t  d S r   )rd   tearDownrc   r4   rC   rg   r#   r$   ri      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 ]}| |kr(||  | q| dkrH |d|| q| dkr|    dks|    dks|    dkr |  || q| D ]} ||| qqd S )Nzprim::DifferentiableGraphZSubgraphprim::Ifz	aten::allprim::TypeCheckprim::RequiresGradCheck)nodeskindrV   ginputs__next__nodeblocks)blockrn   accrr   Zinner_block)!get_nodes_and_parents_recursivelyr#   r$   rv      s    $zEJitTestCase.assertAllFused.<locals>.get_nodes_and_parents_recursivelyzprim::Constantzprim::BailoutTemplatezprim::TupleConstructrj   rk   rl   r   zgot c                 3   s   | ]}|   kV  qd S r   )rn   ).0rr   )allowed_nodesr#   r$   	<genexpr>   s     z-JitTestCase.assertAllFused.<locals>.<genexpr>)FUSION_GROUPsetr   r5   
assertTruer8   nextiteritemsallrm   )r@   graphZ
except_forZfusion_groupsZfusion_nodesr#   )rx   rv   r$   assertAllFused   s"       zJitTestCase.assertAllFusedc                 C   s(   t |}d}|D ]}||kr dS qdS )N)z Could not export Python functionzclosures are not exportableTF)rH   )r@   eseallowedar#   r#   r$   _isHookExceptionOk   s    zJitTestCase._isHookExceptionOkc                    sr   fdd}t j P zzt|jdkr:W W 5 Q R  d S t|t jjrht| dkrhW W 5 Q R  d S t	
 }t j|| | }||\}}W nD tk
r } z& |s nW Y W 5 Q R  d S W 5 d }~X Y nX t	
|}t j|}	t	
 }
t j|	|
 |
d ||
\}}t||D ]\}} || q&t|t jjrd t j||	j W 5 Q R X 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   s
   |  dS )Nzarchive/code/
startswithxr#   r#   r$   r+      r,   zKJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<lambda>c                 S   s
   |  dS )Nz.pyendswithr   r#   r#   r$   r+      r,   c                 3   s   | ]}  |V  qd S r   openrw   farchiver#   r$   ry      s     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)rw   liner#   r#   r$   
<listcomp>   s     zWJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>.<listcomp>N)join)rw   filer#   r#   r$   ry      s     c                 S   s
   |  dS )Nz
.debug_pklr   )r   r#   r#   r$   r+      r,   c                 3   s   | ]}  |V  qd S r   r   r   r   r#   r$   ry      s     c                 s   s   | ]}t |V  qd S r   )pickleloadr   r#   r#   r$   ry      s     )zipfileZipFileassertEqualr8   r{   namelistr5   filter)bufferfilesZcode_files_strZcode_files_stream
code_filesZdebug_files_strZdebug_files_streamdebug_filesrC   r   r$   extract_files   s    
 z9JitTestCase._compared_saved_loaded.<locals>.extract_filesr   )r'   Z_jit_internalZ_disable_emit_hooksr8   r    r&   r1   ZScriptModuleZ_method_namesioBytesIOr2   saverW   r9   r   r   seekzipZassertMultiLineEqualr|   Z_ivalue_tags_match_c)r@   mr   r   Zbuffer_copyr   r   r   Zbuffer2importedZsaved_module_buffer_2Zcode_files_2Zdebug_files_2r   br#   rC   r$   _compared_saved_loaded   s4    
&

z"JitTestCase._compared_saved_loadedc                 C   s&   |j dksd|j krd S | | d S )Nz<lambda>zaten::)namer   )r@   funcr#   r#   r$   ra     s    zJitTestCase.emitFunctionHookc                 C   s   |  | d S r   )r   )r@   moduler#   r#   r$   r`   	  s    zJitTestCase.emitModuleHookNc              	   C   s   t  }|dd  tj|| |dd  |d tjj||d}|dd  |sb|S tj	dd}z*|  ||j tjj|j|d}W 5 t
|j X |d	d  |S )
Nc                 S   s   | j dr|  S d S )N_pack)r   _has_methodr   sr#   r#   r$   r+     r,   z<JitTestCase.getExportImportCopyWithPacking.<locals>.<lambda>c                 S   s   | j dr|  S d S N_unpackr   r   r   r   r#   r#   r$   r+     r,   r   )map_locationc                 S   s   | j dr|  S d S r   r   r   r#   r#   r$   r+     r,   F)deletec                 S   s   | j dr|  S d S r   r   r   r#   r#   r$   r+   $  r,   )r   r   applyr'   r2   r   r   r   tempfileNamedTemporaryFileosunlinkr   close)r@   r   Zalso_test_filer   r   r   r   resultr#   r#   r$   getExportImportCopyWithPacking  s"    
z*JitTestCase.getExportImportCopyWithPackingc                    sd   |r8t |}| |d   }| |dk d S  fdd|}| t|dk d S )Nwith r   c                    sF   g }|   D ]4}|  kr&|| | D ]} || 7 }q.q|S r   rm   rn   rV   rs   rt   outrr   rn   rm   r#   r$   rm   /  s    
z.JitTestCase.assertGraphContains.<locals>.nodes)rH   countr|   r8   )r@   r   rn   consider_subgraphsstrgraphr   	out_nodesr#   r   r$   assertGraphContains'  s    	zJitTestCase.assertGraphContainsc           	         sp   dd }|rBt |}| |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| 	d S )NZ	includingZ	excludingz
Error: graph contains  z nodes (z subgraphs) but expected )AssertionError)r   rn   actualexpectedr   Zsubgraphr#   r#   r$   perform_assert<  s    z>JitTestCase.assertGraphContainsExactly.<locals>.perform_assertr   c                    sF   g }|   D ]4}|  kr&|| | D ]} || 7 }q.q|S r   r   r   r   r#   r$   rm   J  s    
z5JitTestCase.assertGraphContainsExactly.<locals>.nodes)rH   r   r8   )	r@   r   rn   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|f|| d S )N)Zoperator_export_type)r'   ZonnxZ_optimize_tracer   ZONNXassertExpectedGraph)r@   ro   rY   kwargsr#   r#   r$   assertExpectedONNXGraphW  s    z#JitTestCase.assertExpectedONNXGraphc                 O   sr   t |tjjr|}n| }tj| tj| tj| tj|}tj| | jt	|f|| d S r   )
r&   r'   r1   Graphr   _jit_pass_lintZ_jit_pass_dceZ_jit_pass_canonicalizeassertExpectedrH   )r@   tracerY   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 k	rZt |tsZ|}tj| |rt|| |S )NFTZ
_jit_pass_)	r&   r'   r1   r   r   r   getattrbool	set_graph)r@   r   r   r   r   r   r#   r#   r$   run_passh  s    
zJitTestCase.run_passc                 C   s`   t  }|stdd}||d k r@|j}|s6td|d7 }qi }||j ||j |S )Nzfailed to inspect framer   r   zfailed to get frame)inspectcurrentframer9   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@   rA   r>   r?   r#   r#   r$   assertRaisesRegexWithHighlight  s    z*JitTestCase.assertRaisesRegexWithHighlightr   c
              
   C   s   t   | ||L t|trR| |}
i }t||
|d |
| |
| }n|}||  W 5 Q R X | ||` t|trtjj	||d}t
||}n,tt|}tjj	||d}t
||j}||  W 5 Q R X t|ts| || tj|}||  W 5 Q R X W 5 Q R X 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"   Z
_frames_upN)r   rF   r&   rH   r   r%   r   r'   r2   CompilationUnitr   textwrapdedentr   	getsourcerM   script)r@   r   rp   rA   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_stater:   r    num_bailoutsrangeZrequest_bailoutr   )	r@   modelrp   r   stateZplanr   r   Zbailout_outputsr#   r#   r$   checkBailouts  s    
zJitTestCase.checkBailoutsr   c                 C   s  t j| t  tdd |D }t|trt jj||d}| |}i }t	|||d |
| || }t||}n@tt|}| j|||j||||dd t jj|dd}|}|rtdd	 |}n|}|r^|  }|| }W 5 Q R X |  }|| }W 5 Q R X |  }|| }W 5 Q R X tsJ| j|d
 dd | j|||	|
d n@|| }|sr|rz|| }|| }tr| ||| || }| j|||	|
d | j|||	|
d |W  5 Q R  W  5 Q R  S Q R X W 5 Q R X 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)rw   r   r#   r#   r$   ry     s     z*JitTestCase.checkScript.<locals>.<genexpr>r   r      )optimizeinputs_requires_gradr   r   r   r   c                 S   s   |    S r   )detachrequires_grad_r)   r#   r#   r$   r+     r,   z)JitTestCase.checkScript.<locals>.<lambda>r   rS   )subnameatolrtolN)r'   r2   Zoptimized_executionr   anyr&   rH   r   r   r%   r   r   r   r   r   r   checkScriptrM   r   r0   r[   r
   r   r   r   r   )r@   r   rp   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_stdoutZopt_script_outputsZ_python_stdoutZpython_outputsr#   r#   r$   r    s^    






zJitTestCase.checkScripth㈵>c                    s   fdd}|d kr|}fdd}|}|rDt dd ||}n|tjj||||
dd}|	rl| |}|rzt|j || }|| }| || | }|rtjj	||||d	}| }|rtjj	||||d	}| || |r| j||||d
 | }||}|r&tjj	||d|d}|rJ||| }tjj	|||d	}|rft dd ||}| }||}|rtjj	||d|d}|r||| }tjj	|||d	}| || |r| j||||d
 t
||D ]4\}}|d kr|d krq| j||ddd
 q|S )Nc                    s,    d k	r| d    } t dd t| D S )Nc                 s   s0   | ](\}}|d k	rt |d |  V  qd S )Nr  )mathlogsum)rw   r   vr#   r#   r$   ry     s      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                    sV   t tjr | n:t tr<t fdd| nt fdd| |S )Nc                    s   |  | S r   r#   )ru   key)r.   r/   input_reducer#   r$   r+   %  r,   zVJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reduce.<locals>.<lambda>c                    s   | | S r   r#   )ru   val)r.   r  r#   r$   r+   '  r,   )r&   r'   r(   dictr   )r/   r.   ru   r  r-   r$   r  !  s    
zDJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reducec                 S   s
   | | S r   )rV   )r*   ru   r#   r#   r$   r+   )  r,   z@JitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.<lambda>)tuple)rp   )r  r  r$   flatten_inputs   s    z.JitTestCase.checkTrace.<locals>.flatten_inputsc                 S   s   |    S 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?)r0   r'   r2   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  s    
 

   zJitTestCase.checkTracec              	   C   s\   t j|}t  || }W 5 Q R X t  || }W 5 Q R X | || | || |S )zq
        Check that a nn.Module's results in Script mode match eager and that it
        can be exported
        )r'   r2   r   r   r   ZassertExportImportModule)r@   Z	nn_modulerY   smZ	eager_outZ
script_outr#   r#   r$   checkModules  s    zJitTestCase.checkModule)r#   )TN)F)F)
NNFFTr  TFNN)!rM   rN   rO   Z_do_cuda_memory_leak_checkrf   r5   r[   r^   rb   rc   re   ri   r   r   r   ra   r`   r   r   r   r   r   r   r   r   r   Z	PROFILINGr   r   r  r(  r*  __classcell__r#   r#   rg   r$   rQ   e   sb   

	>


   
*
P                
drQ   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'   r1   Z_jit_get_tracer_state_warnprev_jit_set_tracer_state_warnrC   r#   r#   r$   rD     s    z$NoTracerWarnContextManager.__enter__c                 G   s   t j| j d S r   )r'   r1   r/  r.  rX   r#   r#   r$   rL     s    z#NoTracerWarnContextManager.__exit__N)rM   rN   rO   rD   rL   r#   r#   r#   r$   r,    s   r,  c              	   c   s4   t j }t j|  z
d V  W 5 t j| X d S r   )r'   r1   Z_jit_get_inline_everything_modeZ_jit_set_inline_everything_mode)Zshould_inlineoldr#   r#   r$   inline_everything_mode  s
    

r1  c              	   c   s4   t j }t j|  z
d V  W 5 t j| X d S r   )r'   r1    _debug_get_fusion_group_inlining _debug_set_fusion_group_inlining)Zinliningr0  r#   r#   r$   set_fusion_group_inlining  s
    

r4  Tc              	   c   s,   t j|   z
d V  W 5 t jd X d S rE   )r'   r1   Z%_debug_set_autodiff_subgraph_inlining)Zenabledr#   r#   r$   "disable_autodiff_subgraph_inlining  s    
r5  c                    s   t   fdd}|S )Nc               	      s"   t d  | | W 5 Q R X d S rE   r1  rY   r   r.   r#   r$   wrapper  s    
z#_inline_everything.<locals>.wrapper	functoolswrapsr.   r9  r#   r8  r$   _inline_everything  s    r>  c                    s   t   fdd}|S )Nc               	      s"   t d  | | W 5 Q R X d S r-  r6  r7  r8  r#   r$   r9    s    
z5_tmp_donotuse_dont_inline_everything.<locals>.wrapperr:  r=  r#   r8  r$   $_tmp_donotuse_dont_inline_everything  s    r?  c                     s    fdd}|S )Nc                    s   t jj|  fS r   )r'   r2   r   )r   r7  r#   r$   r9    s    z_trace.<locals>.wrapperr#   )rY   r   r9  r#   r7  r$   _trace  s    r@  c                    s    fdd}|S )Nc               	      s^   t jd t jd t jd z | | W 5 t jd t jd t jd X d S NTF)r'   r1   Z$_jit_override_can_fuse_on_cpu_legacy_jit_override_can_fuse_on_cpu_jit_set_te_must_use_llvm_cpur7  r8  r#   r$   r9    s    z!enable_cpu_fuser.<locals>.wrapperr#   r=  r#   r8  r$   enable_cpu_fuser  s    
rD  c                 C   s   | rt S dd }|S d S )Nc                    s    fdd}|S )Nc                     s
    | |S r   r#   r7  r8  r#   r$   r9    s    z8enable_cpu_fuser_if.<locals>.noop_fuser.<locals>.wrapperr#   r=  r#   r8  r$   
noop_fuser  s    z'enable_cpu_fuser_if.<locals>.noop_fuser)rD  )ZcondrE  r#   r#   r$   enable_cpu_fuser_if  s    rF  c                 C   s
   |  dS Nforward)_get_methodcr#   r#   r$   get_forward  s    rL  c                 C   s   |  djS rG  )rI  r   rJ  r#   r#   r$   get_forward_graph  s    rM  c                 C   s   | j ||S r   )r   r   rI  )r   r   methodr#   r#   r$   get_module_method  s    rO  c                    s    fdd| j j D S )Nc                    s   g | ]\}}|  r|qS r#   r   )rw   r   _prefixr#   r$   r     s    
z%attrs_with_prefix.<locals>.<listcomp>)Z_modulesr   r   )r   rR  r#   rQ  r$   attrs_with_prefix  s    rS  c                 G   sP   d}g }t |D ]:}t|dkr>tjj| f| }|| q| jdd q|S )N   r   T)Zretain_graph)r   r8   r'   r"  r#  rV   Zbackward)r   rY   Zprofiling_countresultsr   rr#   r#   r$   warmup_backward  s    rW  c                  G   s$   | D ]}t tj|j |j| qd S r   )setattrrR   modulesrN   rM   )rY   argr#   r#   r$   make_global  s    r[  c           	   
   C   s   t  }tj|d}t|d}||  W 5 Q R X tj	||}tj
|}|j}t|tsft|| t||}|W  5 Q R  S Q R X d S )Nz	script.pyw)r   TemporaryDirectoryr   pathr   r   write	importlibutilspec_from_file_locationmodule_from_specloaderr&   r   r   exec_moduler   )	r    fn_nameZtmp_dirZscript_pathr   specr   rd  r.   r#   r#   r$   _get_py3_code  s    


rh  c                   @   s   e Zd Zdd Zdd ZdS )TensorExprTestOptionsc                 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 rA  )r'   r1   _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_staterB  _jit_override_can_fuse_on_gpuZ_jit_texpr_fuser_enabledtexpr_fuser_state_jit_set_texpr_fuser_enabledr2  old_fusion_inliningr3  Z_jit_get_te_must_use_llvm_cpuold_te_must_use_llvm_cpurC  rC   r#   r#   r$   rB     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'   r1   rj  rk  rl  rm  rr  rq  rp  ro  rB  rn  r3  rs  rC  rt  rC   r#   r#   r$   restore  s    zTensorExprTestOptions.restoreN)rM   rN   rO   rB   ru  r#   r#   r#   r$   ri    s   ri  c                 C   sZ   g }| D ]L}t |tjr,||   qt|rJ|dd |D  q|| q|S )Nc                 S   s   g | ]}|   qS r#   )r  r  )rw   r*   r#   r#   r$   r   %  s     z clone_inputs.<locals>.<listcomp>)r&   r'   r(   rV   r  r  r   )rY   rp   rZ  r#   r#   r$   clone_inputs  s    rv  c              	      sJ  g }  | |}  }  }||d} jdk}|rJd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}	|r|S |D ]}
| D ]}|d krqt|rqt	 fdd|	}|D ]X}|d D ]J}t
|
j}t|
j|kr&t|
j| tr&t|| ||< t||
_qq|||
f qq|S )N)functionrN  )Zresize_Z
resize_as_rN  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  r,   z1get_traced_sample_variant_pairs.<locals>.<lambda>rx  )Zsample_inputsZget_op
get_methodr   r   r'   r(   r7   	is_lambdar   r5   rY   r8   r&   r   intr  rV   )ZdeviceZdtyper{  r   Zsamplesr   rN  variantsZhas_fake_functionZops_with_unsupported_bool_argssamplevariantZmatching_opsZop_dataidxrY   r#   rz  r$   get_traced_sample_variant_pairs+  sb     
 
"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+   |  r,   zis_lambda.<locals>.<lambda>)r&   rI   rM   )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   r;  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.utilr`  r   r  r   r   rR   r   r   importlib.abcr   typingr   r   r   r   r   cudaZis_availableZRUN_CUDAZdevice_countZRUN_CUDA_MULTI_GPUZRUN_CUDA_HALFversionZhipr1   Z_cuda_getCompiledVersionZCUDA_VERSIONr   dZget_device_capabilitymajorr%   r0   r4   r:   r;   rz   rQ   r,  r1  r4  r5  r>  r?  r@  rD  rF  rL  rM  rO  rS  rW  r[  rh  ri  rv  r  r}  r#   r#   r#   r$   <module>   s    


    $

		
P