
    Vh                        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	jd                  jg                         Z4e4xr e	jd                  jk                         dkD  Z6e4Z7e	jd                  jg                         r{e	jp                  jr                  see	jt                  jw                         Z< e=e	jd                  jk                               D ](  Z>e	jd                  j                  e>      d    Z@e@dk  s'dZ7* d ZAd ZBd ZCd ZD G d d      ZEdZF G d de      ZG G d d      ZHed        ZIed        ZJed2d        ZKd! ZLd" ZMd# ZNd$ ZOd% ZPd& ZQd' ZRd( ZSd) ZTd* ZUd+ ZVd, ZW G d- d.      ZXd/ ZYd0 ZZd1 Z[y)3    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                     t        | ||       y N)exec)codegloblocs      Q/home/dcms/DCMS/lib/python3.12/site-packages/torch/testing/_internal/jit_utils.pyexecWrapperr#   7   s    tS    c                 (     t        d |       |      S )Nc                 6    t        | t        j                        S r   )
isinstancetorchTensorts    r"   <lambda>zdo_input_map.<locals>.<lambda>;   s    Au||!< r$   r   )fninputs     r"   do_input_mapr/   :   s    A;<bA%HHr$   c                     t         j                  j                          t         j                  j                  j                         t         j                  j                  _        t         j                  j                  j                          y r   )	r(   _C_jit_clear_class_registryjit
_recursiveConcreteTypeStoreconcrete_type_store_state_clear_class_state r$   r"   clear_class_registryr:   =   sM    	HH&&(/4yy/C/C/U/U/WEII,	II'')r$   c                     t        | j                  j                               }t        |      }|dk7  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)graph_executor_stater=   	num_planss      r"   get_execution_planrC   B   sX    /??FFHIOO$IA~ AAJM N 	N1r$   c                   "    e Zd ZdZd Zd Zd Zy)&_AssertRaisesRegexWithHighlightContextz~
    A context manager that is useful for checking that error messages highlight
    the correct part of the source code.
    c                 <    || _         || _        || _        || _        y r   )	test_caseexception_typeregex	highlight)selfrG   	exceptionrI   rJ   s        r"   __init__z/_AssertRaisesRegexWithHighlightContext.__init__P   s    "'
"r$   c                     | S r   r9   rK   s    r"   	__enter__z0_AssertRaisesRegexWithHighlightContext.__enter__V   s    r$   c                 &   | j                   j                  | j                  | j                        5  |r|	 d d d        | j                  r;t               j                  | j                        j                  t        |             y# 1 sw Y   QxY wNT)	rG   assertRaisesRegexrH   rI   rJ   r
   check_source_highlightedrunstr)rK   typevalue	tracebacks       r"   __exit__z/_AssertRaisesRegexWithHighlightContext.__exit__Y   sq    ^^--d.A.A4::N 	 	 >>K00@DDSZP	 	s   BBN)__name__
__module____qualname____doc__rM   rP   rZ   r9   r$   r"   rE   rE   J   s    
#r$   rE   zprim::TensorExprGroupc                   ,    e Zd ZdZdZ G d de      Z G d de      Zd Zd Z	 fd	Z
 fd
Zd dZd Zd Zd Zd Zd!dZd"dZd"dZd Zd Zd Zd Zd Zddddej4                  fdZd Zdddddej4                  ddfdZ	 	 	 	 d#dZd Z xZ S )$JitTestCaseTFc                       e Zd ZdZd Zd Zy)JitTestCase.capture_stdoutz>
        Replace sys.stdout with a temporary StringIO
        c                 x    t         j                  | _        t               | _        | j                  t         _        | S r   )sysstdout
sys_stdoutr   stringiorO   s    r"   rP   z$JitTestCase.capture_stdout.__enter__m   &    !jjDO$JDMCJKr$   c                     | j                  t        | j                  j                                      | `| j                  t
        _        y r   )appendrV   rg   getvaluerf   rd   re   rK   argss     r"   rZ   z#JitTestCase.capture_stdout.__exit__s   0    KKDMM22456CJr$   Nr[   r\   r]   r^   rP   rZ   r9   r$   r"   capture_stdoutrb   i       			)r$   rp   c                       e Zd ZdZd Zd Zy)JitTestCase.capture_stderrz>
        Replace sys.stderr with a temporary StringIO
        c                 x    t         j                  | _        t               | _        | j                  t         _        | S r   )rd   stderr
sys_stderrr   rg   rO   s    r"   rP   z$JitTestCase.capture_stderr.__enter__|   rh   r$   c                     | j                  t        | j                  j                                      | `| j                  t
        _        y r   )rj   rV   rg   rk   rv   rd   ru   rl   s     r"   rZ   z#JitTestCase.capture_stderr.__exit__   rn   r$   Nro   r9   r$   r"   capture_stderrrs   x   rq   r$   rx   c                 l    t         j                  j                  | j                  | j                         y r   )r(   r1   _jit_set_emit_hooksemitModuleHookemitFunctionHookrO   s    r"   setHookszJitTestCase.setHooks   s"    $$T%8%8$:O:OPr$   c                 D    t         j                  j                  d d        y r   )r(   r1   rz   rO   s    r"   
clearHookszJitTestCase.clearHooks   s    $$T40r$   c                     t         |           t        j                  s3t        j
                  j                  j                          dt        _        | j                          y rR   )	supersetUpr`   _restored_warningsr(   r3   TracerWarningignore_lib_warningsr}   rK   	__class__s    r"   r   zJitTestCase.setUp   s>     --II##779-1K*r$   c                 V    t         |           | j                          t                y r   )r   tearDownr   r:   r   s    r"   r   zJitTestCase.tearDown   s      	r$   c                    fddt         dddddht        |      z  t        t              } |t         |       | j	                  t        |      dk(  d	|        t        t        |j                                     \  }}| j	                  t        |      dk(  d	|        | j	                  t        fd
|j                         D              d	|        y )Nc                     | j                         D ]i  }|j                         |k(  r||    j                  |       ,|j                         dk(  r |j                  d      ||       Y|j                         dk(  r|j	                         j                         j                         j                         dk(  sz|j	                         j                         j                         j                         dk(  s=|j	                         j                         j                         j                         dk(  r( |j                         j                         ||       K|j                         D ]  } |||        l y )Nzprim::DifferentiableGraphSubgraphprim::Ifz	aten::allprim::TypeCheckprim::RequiresGradCheck)nodeskindrj   ginputs__next__nodeblocks)blockr   accr   inner_block!get_nodes_and_parents_recursivelys        r"   r   zEJitTestCase.assertAllFused.<locals>.get_nodes_and_parents_recursively   s*    R99;$&J%%d+YY[$??5dffZ6H$PSTYY[J.DKKM4J4J4L4Q4Q4S4X4X4Z^i4i48KKM4J4J4L4Q4Q4S4X4X4Z^o4o48KKM4J4J4L4Q4Q4S4X4X4Z^w4w5dkkm6L6L6NPTVYZ'+{{} R9+tSQRRr$   zprim::Constantzprim::BailoutTemplatezprim::TupleConstructr   r   r   r   zgot c              3   B   K   | ]  }|j                         v   y wr   )r   ).0r   allowed_nodess     r"   	<genexpr>z-JitTestCase.assertAllFused.<locals>.<genexpr>   s     STDIIK=8Ss   )FUSION_GROUPsetr   r<   
assertTruer?   nextiteritemsallr   )rK   graph
except_forfusion_groupsfusion_nodesr   r   s        @@r"   assertAllFusedzJitTestCase.assertAllFused   s    	R *<9P/=NPikmpq{m|} EPPTDU)%}MM*a/4w@ $T-*=*=*?%@ AL)Q.$ug?SU[[]SSug	(r$   c                 8    t        |      }d}|D ]  }||v s y y)N)z Could not export Python functionzclosures are not exportableTF)rV   )rK   eseallowedas        r"   _isHookExceptionOkzJitTestCase._isHookExceptionOk   s0    V2 	ABw	 r$   c                 \     fd}t         j                  j                         5  	 t        |j                        dk(  r
	 d d d        y t        |t         j                  j                        r&t        |j                               dk(  r
	 d d d        y t        j                         }t         j                  j                  ||       |j                         } ||      \  }}t        j                  |      }t         j                  j!                  |      }	t        j                         }
t         j                  j                  |	|
       |
j#                  d        ||
      \  }}t%        ||      D ]  \  }} j'                  ||        t        |t         j                  j                        r9 j)                  t         j                  j+                  ||	j,                               d d d        y # t        $ r$} j                  |      s Y d }~d d d        y d }~ww xY w# 1 sw Y   y xY w)Nc                    t        j                  |       	j                  t        t	        j                                     t        j                                      t        t        d j                                     }t        d |      }fd|D        }d |D        }t        d |      }fd|D        }d |D        }||fS )Nc                 $    | j                  d      S )Nzarchive/code/)
startswithxs    r"   r,   zKJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<lambda>   s    !,,*G r$   c                 $    | j                  d      S )Nz.pyendswithr   s    r"   r,   zKJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<lambda>   s    ajj.? r$   c              3   @   K   | ]  }j                  |        y wr   openr   farchives     r"   r   zLJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>   s      IQa I   c              3      K   | ]1  }d j                  |D cg c]  }|j                          c}       3 yc c}w w) N)joindecode)r   filelines      r"   r   zLJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>   s-     b"''T"BT4;;="BCb"Bs   >9
>c                 $    | j                  d      S )Nz
.debug_pklr   )r   s    r"   r,   zKJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<lambda>   s    qzz,/G r$   c              3   @   K   | ]  }j                  |        y wr   r   r   s     r"   r   zLJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>   s     !Ka',,q/!Kr   c              3   F   K   | ]  }t        j                  |        y wr   )pickleload)r   r   s     r"   r   zLJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>   s     Fa6;;q>Fs   !)zipfileZipFileassertEqualr?   r   namelistr<   filter)
bufferfilescode_files_strcode_files_stream
code_filesdebug_files_strdebug_files_streamdebug_filesr   rK   s
           @r"   extract_filesz9JitTestCase._compared_saved_loaded.<locals>.extract_files   s    oof-GSW%5%5%7!893w?O?O?Q;RS GIYIYI[\]E#$?GN I. IbPabJ %%GOO!K?!KF3EFK{**r$   r   )r(   _jit_internal_disable_emit_hooksr?   r   r'   r1   ScriptModule_method_namesioBytesIOr3   saverk   r@   r   r   seekzipassertMultiLineEqualr   _ivalue_tags_match_c)rK   mr   r   buffer_copyr   _debug_filesr   buffer2importedsaved_module_buffer_2code_files_2_debug_files_2r   bs   `              r"   _compared_saved_loadedz"JitTestCase._compared_saved_loaded   s   	+$   446 (	Mqvv;!#	(	M (	M
 a!6!671??,-2(	M (	M 		q&) %oo/+8+@(
L jj-Gyy~~g.H %'JJL!IINN8%:;!&&q)+89N+O(L.J5 01))!Q/0 !UXX223 ; ;Ax{{ KLQ(	M (	M&   ..q1/(	M (	M&'(	M (	MsC   H"G2	A G2AG2"DH"2	H;HH"HH""H+c                 b    |j                   dk(  sd|j                   v ry | j                  |       y )Nz<lambda>zaten::)namer   )rK   funcs     r"   r|   zJitTestCase.emitFunctionHook  s*    99
"h$))&;##D)r$   c                 &    | j                  |       y r   )r   )rK   modules     r"   r{   zJitTestCase.emitModuleHook	  s    ##F+r$   Nc                    t        j                         }|j                  d        t        j                  j                  ||       |j                  d        |j                  d       t        j                  j                  ||      }|j                  d        |s|S t        j                  d      }	 |j                          |j                  |j                         t        j                  j                  |j                  |      }t        j                  |j                         |j                  d        |S # t        j                  |j                         w xY w)	Nc                 \    | j                   j                  d      r| j                         S d S )N_pack)r   _has_methodr   ss    r"   r,   z<JitTestCase.getExportImportCopyWithPacking.<locals>.<lambda>  s!    qtt'7'7'@!'') d r$   c                 \    | j                   j                  d      r| j                         S d S N_unpackr   r   r   r   s    r"   r,   z<JitTestCase.getExportImportCopyWithPacking.<locals>.<lambda>  s!    )9)9))D!))+ $ r$   r   )map_locationc                 \    | j                   j                  d      r| j                         S d S r   r   r   s    r"   r,   z<JitTestCase.getExportImportCopyWithPacking.<locals>.<lambda>  s"    0@0@0K QU r$   F)deletec                 \    | j                   j                  d      r| j                         S d S r   r   r   s    r"   r,   z<JitTestCase.getExportImportCopyWithPacking.<locals>.<lambda>$  s!    add.>.>y.Iqyy{ t r$   )r   r   applyr(   r3   r   r   r   tempfileNamedTemporaryFilecloser   osunlink)rK   r   also_test_filer   r   r   r   results           r"   getExportImportCopyWithPackingz*JitTestCase.getExportImportCopyWithPacking  s    	JK		q&!	NOA99>>&|>DUVO
 ''u5	GGIMM!&&!YY^^AFF^FFIIaffST IIaffs   8AE !E"c                     |rFt        |      }|j                        |j                  d       z
  }| j                  |dkD         y fd |      }| j                  t        |      dkD         y )Nwith r   c                     g }| j                         D ]F  }|j                         k(  r|j                  |       |j                         D ]  } | |       z  } H |S r   r   r   rj   r   r   outr   r   r   s      r"   r   z.JitTestCase.assertGraphContains.<locals>.nodes/  a    C (99;$&JJt$![[] (E5<'C((
 Jr$   )rV   countr   r?   )rK   r   r   consider_subgraphsstrgraphr  	out_nodesr   s     `    @r"   assertGraphContainszJitTestCase.assertGraphContains'  si    5zHNN4(8>>E$.+IIEOOEAI&	 %L	I*+r$   c                    	 d }|r>t        |      }|j                        |j                  d       z
  } |||||       y 	fd	 	|      } ||t        |      ||       y )Nc                 L    ||k(  ry |rdnd}t        |  d| d| d| d| 	      )N	including	excludingz
Error: graph contains  z nodes (z subgraphs) but expected )AssertionError)r   r   actualexpectedr  subgraphs         r"   perform_assertz>JitTestCase.assertGraphContainsExactly.<locals>.perform_assert<  sI    !&8{kH '1&4&
Rkltkuvx xr$   r  c                     g }| j                         D ]F  }|j                         k(  r|j                  |       |j                         D ]  } | |       z  } H |S r   r  r  s      r"   r   z5JitTestCase.assertGraphContainsExactly.<locals>.nodesJ  r  r$   )rV   r  r?   )
rK   r   r   num_kind_nodesr  r!  r  r  r  r   s
     `      @r"   assertGraphContainsExactlyz&JitTestCase.assertGraphContainsExactly;  sv    	x 5zHNN4(8>>E$.+IIE5$~-/	 %L	udC	NN)	+r$   c                     t         j                  j                  |t        j                        } | j
                  |g|i | y )N)operator_export_type)r(   onnx_optimize_tracer	   ONNXassertExpectedGraph)rK   r   rm   kwargss       r"   assertExpectedONNXGraphz#JitTestCase.assertExpectedONNXGraphW  s<    JJ&&q?R?W?W&X   4T4V4r$   c                    t        |t        j                  j                        r|}n|j	                         }t        j                  j                  |       t        j                  j                  |       t        j                  j                  |       t        j                  j                  |      }t        j                  j                  |        | j                  t        |      g|i | y r   )
r'   r(   r1   Graphr   _jit_pass_lint_jit_pass_dce_jit_pass_canonicalizeassertExpectedrV   )rK   tracerm   r+  r   s        r"   r*  zJitTestCase.assertExpectedGraph[  s    eUXX^^,EKKME&u%&//6&CJ888r$   c                    t        |t        j                  j                        r|}d}nd}|j	                         }t        j                  j                  |        t        t        j                  d|z         |      }|t        |t              s|}t        j                  j                  |       |r|j                  |       |S )NFT
_jit_pass_)	r'   r(   r1   r.  r   r/  getattrbool	set_graph)rK   r   r3  r   r8  r
  s         r"   run_passzJitTestCase.run_passh  s    eUXX^^,EIIKKME&7<$#67>j&>E&OOE"r$   c                    t        j                         }|st        d      d}||dz   k  r'|j                  }|st        d      |dz  }||dz   k  r'i }|j	                  |j
                         |j	                  |j                         |S )Nzfailed to inspect framer   r   zfailed to get frame)inspectcurrentframer@   f_backupdatef_locals	f_globals)rK   	frames_upframeidefined_varss        r"   get_frame_varszJitTestCase.get_frame_varsz  s    $$&899)a-LLE"#899FA	 )a-
 (*ENN+EOO,r$   c                     t        | |||      S r   )rE   )rK   rL   rI   rJ   s       r"   assertRaisesRegexWithHighlightz*JitTestCase.assertRaisesRegexWithHighlight  s    5dIuiXXr$   r   c
                 x   t               5  | j                  ||      5  t        |t              r8| j	                  |      }
i }t        ||
|       |
j                  |       |
|   }n|} ||  ddd       | j                  ||      5  t        |t              r.t        j                  j                  ||      }t        ||      }n_t        j                  t        j                  |            }t        j                  j                  ||      }t        ||j                        } ||  ddd       t        |t              s?| j                  ||      5  t        j                  j!                        } ||  ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   nxY w# 1 sw Y   +xY w# 1 sw Y   yxY w)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
_frames_up)r   rS   r'   rV   rE  r#   r>  r(   r3   CompilationUnitr6  textwrapdedentr;  	getsourcer[   script)rK   rP  r   rL   rI   r   outputscapture_outputrA  	profilingrB  
the_locals	python_fncustring_frontendsourceges                    r"   checkScriptRaisesRegexz"JitTestCase.checkScriptRaisesRegex  s    78 	 ''	59 #fc* //	:E13JU
CLL, %dI &I6"# ''	59 	)fc*226i2PB&-b$&7O%__W->->v-FGF226i2PB&-b&//&BO(	) fc*++Iu=  )))4BK ;	  	 # #	) 	)   ;	  	 sT   F0AF.F0B#F+*F0%F$:F0F	F0F!	F0$F-	)F00F9c                     |j                         }t        |      }|j                  j                         }t	        d|      D ]4  }|j                  j                  |        || }| j                  ||       6 y Nr   )get_debug_staterC   r   num_bailoutsrangerequest_bailoutr   )	rK   modelr   r  stateplanr^  rC  bailout_outputss	            r"   checkBailoutszJitTestCase.checkBailouts  sm    %%'!%(yy--/q,' 	8AII%%a(#VnO_h7	8r$   r   c                    t         j                  j                  |      5  t               5  t	        d |D              }t        |t              ret         j                  j                  ||      }| j                  |      }i }t        |||       |j                  |       ||   }t        ||      }nnt        j                  t        j                  |            }| j!                  |||j"                  ||||d       t         j                  j%                  |d      }|}|rt'        d |      }n|}|r| j)                         5 } || }ddd       | j)                         5   || }ddd       | j)                         5   || }ddd       t*        s| j-                  d	   d
       | j/                  |	|
       n1 || }|s|r || } || }t0        r| j3                  |||        || }| j/                  ||	|
       | j/                  |||	|
       |cddd       cddd       S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)z|
        Checks that a given script generates the same output as the Python
        version using the given inputs.
        c              3   l   K   | ],  }t        |t        j                        xr |j                   . y wr   )r'   r(   r)   requires_grad)r   r   s     r"   r   z*JitTestCase.checkScript.<locals>.<genexpr>  s(     (i]^Au||)D)X)X(is   24rJ  rI     )optimizeinputs_requires_gradrR  rS  rA  r   c                 >    | j                         j                         S r   )detachrequires_grad_r*   s    r"   r,   z)JitTestCase.checkScript.<locals>.<lambda>  s    ahhj>W>W>Y r$   Nr   re   )subnameatolrtol)r(   r3   optimized_executionr   anyr'   rV   rL  rE  r#   r>  r6  rM  rN  r;  rO  checkScriptr[   rP  r/   rp   r   r2  r   r   re  )rK   rP  r   r   rj  rk  rR  rA  rS  rq  rr  extra_profile_runsrV  rB  rT  rU  scripted_fnrX  recording_inputsscript_stdoutscript_outputsopt_script_outputspython_outputss                          r"   ru  zJitTestCase.checkScript  s    YY**84 ?	#:< >#%((ibh(i%i"fc* 226i2PB
 !//	:E13JU
CLL, %dI")"d"3K &__W->->v-FGF$$!)-A'5"+"# % % #())"2"26a"2"HK &I''34Y[a'b$'-$!,,. H-)46F)GH,,. L-8:J-K*L,,. <)2F);<%++M!,<h+O$$^5GdY]$^ &12B%CN+/A-8:J-K*)46F)G&$**;@RS%.%7N  dQU V  1C$UY Z"}># >#?	# ?	#TH HL L< <[># ># >#?	# ?	# ?	#sl   I*DIH0I#H<)IIBI	I*0H95I<IIIII	I**I3c                    fd}||}fd}|}|rt        d |       |      }n|t        j                  j                  ||||
d      }|	r| j	                  |      }|rt        |j                          || } || }| j                  ||        | }|r(t        j                  j                   ||      |      } | }|r(t        j                  j                   ||      |      }| j                  ||       |r| j                  ||        | } ||      }|r#t        j                  j                  |d|	      }|r- |      |z  }t        j                  j                  ||      }|rt        d
 |       |      } | } ||      }|r#t        j                  j                  |d|	      }|r- |      |z  }t        j                  j                  ||      }| j                  ||       |rC| j                  ||       t              D ]  \  }}||| j                  ||dd       ! |S )Nc                 J    | d   } t        d t        |       D              S )Nc              3   z   K   | ]3  \  }}|	t        j                  |dz         |j                         z   5 y w)Nri  )mathlogsum)r   rC  vs      r"   r   z9JitTestCase.checkTrace.<locals>.allSum.<locals>.<genexpr>  s/     \TQamtxxA0\s   
;.;)r  	enumerate)vsdrops    r"   allSumz&JitTestCase.checkTrace.<locals>.allSum  s/     $Z \Yr]\\\r$   c                 8    fdt         d g             S )Nc                      t         t        j                        r  |       |S t         t              rt	         fd |       |S t	        fd |       |S )Nc                      |   |       S r   r9   )r   keyr-   r.   input_reduces     r"   r,   zVJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reduce.<locals>.<lambda>%  s    LsR,M r$   c                      ||       S r   r9   )r   valr-   r  s     r"   r,   zVJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reduce.<locals>.<lambda>'  s    Lb#,F r$   )r'   r(   r)   dictr   )r.   r-   r   r  s   `` r"   r  zDJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reduce!  sV    eU\\2ucN
 
	  t,MuVYZ 
 FsS
r$   c                 $    |j                  |       S r   )rj   )r+   r   s     r"   r,   z@JitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.<lambda>)  s    szzRS} r$   )tuple)r   r  rx  s    @r"   flatten_inputsz.JitTestCase.checkTrace.<locals>.flatten_inputs   s!     &68TVXYZZr$   c                 >    | j                         j                         S r   )clonern  r*   s    r"   r,   z(JitTestCase.checkTrace.<locals>.<lambda>-  s    aggi6N6N6P r$   F)check_tolerance_force_outplacecheck_trace)allow_unusedrp  T)create_graphr  c                     t        | d      S )NT)rh  r   r*   s    r"   r,   z(JitTestCase.checkTrace.<locals>.<lambda>\  s    hqPT6U r$   g-C6J?)r/   r(   r3   r3  getExportImportCopyprintr   r   autogradgradr   )rK   r   reference_tensorsinput_tensorsr  r  verboseinputs_require_gradsr  export_importr  	grad_atol	grad_rtolr  r  nograd_inputsflattened_recording_inputsrY  rQ  
outputs_gegradsgrads_gel1l2grads2l1_gel2_ge	grads2_geg2g2_gerx  s       `                         @r"   
checkTracezJitTestCase.checkTrace  s   	]  -M		[ *+,PRcd)78H)I&0
 YY__T=/-<%  Q ))"-B"((O &'
*- ()NN''w9S5A ( CE )*
~~**6*+=?Y8D + FH*-UH99M ()G_NN'',FUY5A ( CE-"$B^^((-GVb(cF+,UWhi)78H)I&)*
z"~~**1S_ + aH  H%-E++E3M\h+iI*-UH99M 3 B	E:%-  UD AB
 	r$   c                    t         j                  j                  |      }t               5   || }ddd       t               5   || }ddd       | j	                         | j                  ||       |S # 1 sw Y   GxY w# 1 sw Y   ;xY w)zq
        Check that a nn.Module's results in Script mode match eager and that it
        can be exported
        N)r(   r3   rP  r   r   assertExportImportModule)rK   	nn_modulerm   sm	eager_out
script_outs         r"   checkModulezJitTestCase.checkModules  s    
 YYi( 	)!4(I	)  	#TJ	# 	J/%%b$/		) 	)	# 	#s   A6B6A?B)r9   )TN)F)
NNFFTgh㈵>TFNN)!r[   r\   r]   _do_cuda_memory_leak_checkr   r<   rp   rx   r}   r   r   r   r   r   r   r|   r{   r  r  r$  r,  r*  r9  rE  rG  r   	PROFILINGrZ  re  ru  r  r  __classcell__)r   s   @r"   r`   r`   e   s    !%) )) )Q1(<;M|*,4,(+859$Y %)$u)*m6M6M( T8  !).#(+55N#` AE:?RVDHbHr$   r`   c                       e Zd Zd Zd Zy)NoTracerWarnContextManagerc                     t         j                  j                         | _        t         j                  j	                  d       y NF)r(   r1   _jit_get_tracer_state_warnprev_jit_set_tracer_state_warnrO   s    r"   rP   z$NoTracerWarnContextManager.__enter__  s(    HH779	++E2r$   c                 V    t         j                  j                  | j                         y r   )r(   r1   r  r  rl   s     r"   rZ   z#NoTracerWarnContextManager.__exit__  s    ++DII6r$   N)r[   r\   r]   rP   rZ   r9   r$   r"   r  r    s    37r$   r  c              #     K   t         j                  j                         }t         j                  j                  |        	 d  t         j                  j                  |       y # t         j                  j                  |       w xY wwr   )r(   r1   _jit_get_inline_everything_mode_jit_set_inline_everything_mode)should_inlineolds     r"   inline_everything_moder    sV     
((
2
2
4C	HH,,];6005005   >B	A%  B	%!BB	c              #     K   t         j                  j                         }t         j                  j                  |        	 d  t         j                  j                  |       y # t         j                  j                  |       w xY wwr   )r(   r1    _debug_get_fusion_group_inlining _debug_set_fusion_group_inlining)inliningr  s     r"   set_fusion_group_inliningr    sV     
((
3
3
5C	HH--h7711#611#6r  c              #      K   t         j                  j                  |         	 d  t         j                  j                  d       y # t         j                  j                  d       w xY wwrR   )r(   r1   %_debug_set_autodiff_subgraph_inlining)enableds    r"   "disable_autodiff_subgraph_inliningr    sF     	HH22w;?=66t<66t<s   !A,A  A,!A))A,c                 B     t        j                          fd       }|S )Nc                  V    t        d      5   | i | d d d        y # 1 sw Y   y xY wrR   r  rm   r+  r-   s     r"   wrapperz#_inline_everything.<locals>.wrapper  s-    #D) 	 	  	  	    	(	functoolswrapsr-   r  s   ` r"   _inline_everythingr    %    __R    Nr$   c                 B     t        j                          fd       }|S )Nc                  V    t        d      5   | i | d d d        y # 1 sw Y   y xY wr  r  r  s     r"   r  z5_tmp_donotuse_dont_inline_everything.<locals>.wrapper  s-    #E* 	 	  	  	 r  r  r  s   ` r"   $_tmp_donotuse_dont_inline_everythingr    r  r$   c                        fd}|S )Nc                 F    t        j                  j                  | fi S r   )r(   r3   r3  )r   rm   r+  s    r"   r  z_trace.<locals>.wrapper  s    yytT4V44r$   r9   )rm   r+  r  s   `` r"   _tracer    s    5Nr$   c                       fd}|S )Nc                  P   t         j                  j                  d       t         j                  j                  d       t         j                  j	                  d       	  | i | t         j                  j                  d       t         j                  j                  d       t         j                  j	                  d       y # t         j                  j                  d       t         j                  j                  d       t         j                  j	                  d       w xY wNTF)r(   r1   $_jit_override_can_fuse_on_cpu_legacy_jit_override_can_fuse_on_cpu_jit_set_te_must_use_llvm_cpur  s     r"   r  z!enable_cpu_fuser.<locals>.wrapper  s    55d;..t4..u5	9HH99%@HH2259HH2248 HH99%@HH2259HH2248s    C AD%r9   r  s   ` r"   enable_cpu_fuserr    s    	9 Nr$   c                     | rt         S d }|S )Nc                       fd}|S )Nc                       | i |S r   r9   r  s     r"   r  z8enable_cpu_fuser_if.<locals>.noop_fuser.<locals>.wrapper  s    4*6**r$   r9   r  s   ` r"   
noop_fuserz'enable_cpu_fuser_if.<locals>.noop_fuser  s    +Nr$   )r  )condr  s     r"   enable_cpu_fuser_ifr    s    	 r$   c                 $    | j                  d      S Nforward)_get_methodcs    r"   get_forwardr    s    ==##r$   c                 8    | j                  d      j                  S r  )r  r   r  s    r"   get_forward_graphr    s    ==#)))r$   c                 V    | j                   j                  |      j                  |      S r   )r   r6  r  )r   r   methods      r"   get_module_methodr     s     44<<++F33r$   c                     | j                   j                  j                         D cg c]  \  }}|j                  |      r| c}}S c c}}w r   )_modulesr   r   r   )r   prefixr   _s       r"   attrs_with_prefixr    sD     //,,224 %$!Q||F#  % % %s   A	c                     d}g }t        |      D ]T  }t        |      dkD  r2t        j                  j                  | g| }|j                  |       C| j                  d       V |S )N   r   T)retain_graph)r_  r?   r(   r  r  rj   backward)r   rm   profiling_countresultsr  rs         r"   warmup_backwardr    se    OG?# *t9q=##A--ANN1JJDJ)* Nr$   c                  v    | D ]4  }t        t        j                  |j                     |j                  |       6 y r   )setattrrd   modulesr\   r[   )rm   args     r"   make_globalr    s0     @CNN+S\\3?@r$   c                    t        j                         5 }t        j                  j	                  |d      }t        |d      5 }|j                  |        d d d        t        j                  j                  ||      }t        j                  j                  |      }|j                  }t        |t              sJ |j                  |       t        ||      }|cd d d        S # 1 sw Y   xY w# 1 sw Y   y xY w)Nz	script.pyw)r  TemporaryDirectoryr  pathr   r   write	importlibutilspec_from_file_locationmodule_from_specloaderr'   r   exec_moduler6  )	r   fn_nametmp_dirscript_pathr   specr   r  r-   s	            r"   _get_py3_coder"    s    		$	$	& 
'ggll7K8+s# 	qGGDM	~~55g{K006&&)))6"VW%
 
	 	
 
s$   -C-C!BC-!C*	&C--C6c                       e Zd ZddZd Zy)TensorExprTestOptionsNc                 (   t         j                  j                  d      | _        t         j                  j	                  d      | _        t         j                  j                         | _        t         j                  j                         | _	        t         j                  j                  d       t         j                  j                  d       t         j                  j                         | _        t         j                  j                  d       t         j                  j                         | _        t         j                  j#                  d       t         j                  j%                         | _        t         j                  j)                  d       y r  )r(   r1   _jit_set_profiling_executorold_profiling_executor_get_graph_executor_optimizeold_profiling_mode_jit_can_fuse_on_cpuold_cpu_fuser_state_jit_can_fuse_on_gpuold_gpu_fuser_stater  _jit_override_can_fuse_on_gpu_jit_texpr_fuser_enabledtexpr_fuser_state_jit_set_texpr_fuser_enabledr  old_fusion_inliningr  _jit_get_te_must_use_llvm_cpuold_te_must_use_llvm_cpur  rO   s    r"   rM   zTensorExprTestOptions.__init__  s    &+hh&J&J4&P#"'(("G"G"M#(88#@#@#B #(88#@#@#B ..t4..t4!&!B!B!D--d3#(88#L#L#N 11%8(-(N(N(P%..u5r$   c                 B   t         j                  j                  | j                         t         j                  j	                  | j
                         t         j                  j                  | j                         t         j                  j                  | j                         t         j                  j                  | j                         t         j                  j                  | j                         t         j                  j                  | j                         y r   )r(   r1   r&  r'  r(  r)  r1  r0  r.  r-  r  r+  r  r2  r  r4  rO   s    r"   restorezTensorExprTestOptions.restore  s    ,,T-H-HI--d.E.EF--d.D.DE..t/G/GH..t/G/GH11$2J2JK..t/L/LMr$   )returnN)r[   r\   r]   rM   r6  r9   r$   r"   r$  r$    s    6Nr$   r$  c                 b   g }| D ]  }t        |t        j                        r.|j                  |j	                         j                                Kt        |      r<|j                  |D cg c]   }|j	                         j                         " c}       |j                  |        |S c c}w r   )r'   r(   r)   rj   rm  r  r   )rm   r   r  r+   s       r"   clone_inputsr9    s    <>F c5<<(MM#**,,,./#C(MMs;!188:++-;<MM# M	 <s   *%B,
c           	         g }j                  | |      }j                         }j                         }||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}	|r|S |D ]  }
|j                         D ]  }|t        |      rt        fd|	      }|D ]w  }|d   D ]m  }t        |
j                        }t        |
j                        |kD  r.t        |
j                  |   t              rt        ||         ||<   t!        |      |
_        o y |j#                  ||
f         |S )N)functionr  )resize_
resize_as_r  div_floor_roundingr   )r   arg_idxdiv_no_rounding_modediv_trunc_rounding
index_fillri  	full_likemulnew_fullr   c                 (    j                   | d   k(  S )Nr   )formatted_name)r   ops    r"   r,   z1get_traced_sample_variant_pairs.<locals>.<lambda>n  s    B,=,=6,J r$   r?  )sample_inputsget_op
get_methodr   r6  r(   r)   r>   	is_lambdar   r<   rm   r?   r'   r7  intr  rj   )devicedtyperH  rQ  samplesr   r  variantshas_fake_functionops_with_unsupported_bool_argssamplevariantmatching_opsop_dataidxrm   s     `             r"   get_traced_sample_variant_pairsrY  +  s   %'Gvu-G 99;D]]_F FH #<<gellBGG<= )s	

 +s	

 )s	

 !s	

  s	

 s	

 s	
3&"@  .( 	.G!!"JLjkL' ."9- .C,D6;;'#-*V[[=Mt2T$'S	NS	"'+FK	.. NNGV,-	..$ Nr$   c                 h    d }t        | t        |            xr | j                  |j                  k(  S )Nc                       yr\  r9   r9   r$   r"   r,   zis_lambda.<locals>.<lambda>|  s    r$   )r'   rW   r[   )lambLAMBDAs     r"   rL  rL  {  s*    FdDL)Ndmmv.NNr$   )T)\torch.autogradr   torch.autograd.functionr   torch.jit.annotationsr   r   
torch.onnxr	   r(   
torch.cuda	torch.jittorch.jit._loggingtorch.jit.frontendtorch.jit.quantizedr   r  torch.testingr
   $torch.testing._internal.common_utilsr   r   r   r   r   r   "torch.testing._internal.common_jitr   r   
contextlibr   r   r   r   collectionsr   importlib.utilr  r;  r  r  r   rd   r  rM  importlib.abcr   typingr   r   cudais_availableRUN_CUDAdevice_countRUN_CUDA_MULTI_GPURUN_CUDA_HALFversionhipr1   _cuda_getCompiledVersionCUDA_VERSIONr_  dget_device_capabilitymajorr#   r/   r:   rC   rE   r   r`   r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r"  r$  r9  rY  rL  r9   r$   r"   <module>r|     s   $ / F *         $  A F &   #   	  	  
     ::""$?%**"9"9";a"? ::U]]%6%688446L5::**,- "

003A6AI!M"
I*
 2 '^# ^@7 7 6 6 7 7 = =$*4%
@N N4M`Or$   