
    Ph2                        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	jj                  jm                         Z7e7xr e	jj                  jq                         dkD  Z9e7Z:e	jj                  jm                         r{e	jv                  jx                  see	jz                  j}                         Z? e@e	jj                  jq                               D ](  ZAe	jj                  j                  eA      d    ZCeCdk  s'dZ:* d ZDd ZEd ZFd ZG G d d      ZHdZI G d de      ZJ G d d      ZKed        ZLed        ZMed2d        ZNd! ZOd" ZPd# ZQd$ ZRd% ZSd& ZTd' ZUd( ZVd) ZWd* ZXd+ ZYd, ZZ G d- d.      Z[d/ Z\d0 Z]d1 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DictListTupleUnion      Fc                     t        | ||       y N)exec)codegloblocs      lC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/testing/_internal/jit_utils.pyexecWrapperr&   5   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>9   s    Au||!<r'   r   )fninputs     r%   do_input_mapr2   8   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_planrF   @   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)selfrJ   	exceptionrL   rM   s        r%   __init__z/_AssertRaisesRegexWithHighlightContext.__init__N   s    "'
"r'   c                     | S r    r<   rN   s    r%   	__enter__z0_AssertRaisesRegexWithHighlightContext.__enter__T   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)	rJ   assertRaisesRegexrK   rL   rM   r
   check_source_highlightedrunstr)rN   typevalue	tracebacks       r%   __exit__z/_AssertRaisesRegexWithHighlightContext.__exit__W   sl    ^^--d.A.A4::N  O >>K00@DDSZP ONs   BBN)__name__
__module____qualname____doc__rP   rS   r]   r<   r'   r%   rH   rH   H   s    
#r'   rH   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   stringiorR   s    r%   rS   z$JitTestCase.capture_stdout.__enter__k   &    !jjDO$JDMCJKr'   c                     | j                  t        | j                  j                                      | `| j                  t
        _        y r    )appendrY   rj   getvalueri   rg   rh   rN   argss     r%   r]   z#JitTestCase.capture_stdout.__exit__q   0    KKDMM22456CJr'   Nr^   r_   r`   ra   rS   r]   r<   r'   r%   capture_stdoutre   g       			)r'   rs   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    )rg   stderr
sys_stderrr   rj   rR   s    r%   rS   z$JitTestCase.capture_stderr.__enter__z   rk   r'   c                     | j                  t        | j                  j                                      | `| j                  t
        _        y r    )rm   rY   rj   rn   ry   rg   rx   ro   s     r%   r]   z#JitTestCase.capture_stderr.__exit__   rq   r'   Nrr   r<   r'   r%   capture_stderrrv   v   rt   r'   r{   c                 l    t         j                  j                  | j                  | j                         y r    )r+   r4   _jit_set_emit_hooksemitModuleHookemitFunctionHookrR   s    r%   setHookszJitTestCase.setHooks   s"    $$T%8%8$:O:OPr'   c                 D    t         j                  j                  d d        y r    )r+   r4   r}   rR   s    r%   
clearHookszJitTestCase.clearHooks   s    $$T40r'   c                     t         |           t        j                  s3t        j
                  j                  j                          dt        _        | j                          y rU   )	supersetUprc   _restored_warningsr+   r6   TracerWarningignore_lib_warningsr   rN   	__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kindrm   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   99;$&J%%d+YY[$??5dffZ6H$PSTYY[J.DKKM4J4J4L4Q4Q4S4X4X4Z^i4i48KKM4J4J4L4Q4Q4S4X4X4Z^o4o48KKM4J4J4L4Q4Q4S4X4X4Z^w4w5dkkm6L6L6NPTVYZ'+{{}9+tSQ (5 &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     S]TDIIK=8]s   )FUSION_GROUPsetr   r?   
assertTruerB   nextiteritemsallr   )rN   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)rY   )rN   eseallowedas        r%   _isHookExceptionOkzJitTestCase._isHookExceptionOk   s+    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    !,,*Gr'   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      I.Qa.   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>   s4     bPa"''T"BTT4;;=T"BCPa"Bs   >9
>c                 $    | j                  d      S )Nz
.debug_pklr   )r   s    r%   r/   zKJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<lambda>   s    qzz,/Gr'   c              3   @   K   | ]  }j                  |        y wr    r   r   s     r%   r   zLJitTestCase._compared_saved_loaded.<locals>.extract_files.<locals>.<genexpr>   s     !K?a',,q/?r   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     F3Ea6;;q>3Es   !)zipfileZipFileassertEqualrB   r   namelistr?   filter)
bufferfilescode_files_strcode_files_stream
code_filesdebug_files_strdebug_files_streamdebug_filesr   rN   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_hooksrB   r"   r*   r4   ScriptModule_method_namesioBytesIOr6   savern   rC   r   r   seekzipassertMultiLineEqualr   _ivalue_tags_match_c)rN   mr   r   buffer_copyr   r   r   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qvv;!#	 76
 a!6!671??,-2 76 		q&) %oo/*7*?'
K jj-Gyy~~g.H %'JJL!IINN8%:;!&&q)*78M*N'L-J51))!Q/ 6 !UXX223 ; ;Ax{{ KLQ 76&   ..q1/ 76&' 76sC   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   )rN   funcs     r%   r   zJitTestCase.emitFunctionHook  s*    99
"h$))&;##D)r'   c                 &    | j                  |       y r    )r   )rN   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'@!'')JdJr'   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!))+N$Nr'   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!UQU!Ur'   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{StSr'   )r   r   applyr+   r6   r   r   r   tempfileNamedTemporaryFilecloser   osunlink)rN   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   rm   r   r   outr   r   r   s      r%   r   z.JitTestCase.assertGraphContains.<locals>.nodes-  W    C99;$&JJt$![[]E5<'C + &
 Jr'   )rY   countr   rB   )rN   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>.nodesH  r  r'   )rY   r  rB   )
rN   r   r   num_kind_nodesr  r#  r  r  r  r   s
     `      @r%   assertGraphContainsExactlyz&JitTestCase.assertGraphContainsExactly9  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)rN   r   rp   kwargss       r%   assertExpectedONNXGraphz#JitTestCase.assertExpectedONNXGraphU  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+   r4   Graphr   _jit_pass_lint_jit_pass_dce_jit_pass_canonicalizeassertExpectedrY   )rN   tracerp   r-  r   s        r%   r,  zJitTestCase.assertExpectedGraphY  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+   r4   r0  r   r1  getattrbool	set_graph)rN   r   r5  r   r:  r  s         r%   run_passzJitTestCase.run_passf  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currentframerC   f_backupdatef_locals	f_globals)rN   	frames_upframeidefined_varss        r%   get_frame_varszJitTestCase.get_frame_varsx  s    $$&899)a-LLE"#899FA	 )a-
 (*ENN+EOO,r'   c                     t        | |||      S r    )rH   )rN   rO   rL   rM   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   rV   r*   rY   rG  r&   r@  r+   r6   CompilationUnitr8  textwrapdedentr=  	getsourcer^   script)rN   rR  r   rO   rL   r   outputscapture_outputrC  	profilingrD  
the_locals	python_fncustring_frontendsourceges                    r%   checkScriptRaisesRegexz"JitTestCase.checkScriptRaisesRegex  sb    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 >; 9899 :9 >=; 98sT   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_staterF   r"   num_bailoutsrangerequest_bailoutr   )	rN   modelr   r!  stateplanr`  rE  bailout_outputss	            r%   checkBailoutszJitTestCase.checkBailouts  sh    %%'!%(yy--/q,'AII%%a(#VnO_h7 (r'   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+     (ibh]^Au||)D)X)Xbhs   24rL  rK     )optimizeinputs_requires_gradrT  rU  rC  r   c                 >    | j                         j                         S r    )detachrequires_grad_r-   s    r%   r/   z)JitTestCase.checkScript.<locals>.<lambda>  s    ahhj>W>W>Yr'   Nr   rh   )subnameatolrtol)r+   r6   optimized_executionr   anyr*   rY   rN  rG  r&   r@  r8  rO  rP  r=  rQ  checkScriptr^   rR  r2   rs   r   r4  r   r   rg  )rN   rR  r   r   rl  rm  rT  rC  rU  rs  rt  extra_profile_runsrX  rD  rV  rW  scripted_fnrZ  recording_inputsscript_stdoutscript_outputsopt_script_stdoutopt_script_outputs_python_stdoutpython_outputss                            r%   rw  zJitTestCase.checkScript  sl    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$'-$!,,.-)46F)G /,,.2C-8:J-K* /,,..)2F); /%++M!,<h+O$$^5GdY]$^ &12B%CN+/A-8:J-K*)46F)G&$**;@RS%.%7N  dQU V  1C$UY Z"} =< 54T /.....[ =<< 544sl   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)Nrk  )mathlogsum)r   rE  vs      r%   r   z9JitTestCase.checkTrace.<locals>.allSum.<locals>.<genexpr>  s0     \]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    r<   )r   keyr0   r1   input_reduces     r%   r/   zVJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reduce.<locals>.<lambda>#  s    LsR,Mr'   c                      ||       S r    r<   )r   valr0   r  s     r%   r/   zVJitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.input_reduce.<locals>.<lambda>%  s    Lb#,Fr'   )r*   r+   r,   dictr   )r1   r0   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    )rm   )r.   r   s     r%   r/   z@JitTestCase.checkTrace.<locals>.flatten_inputs.<locals>.<lambda>'  s    szzRS}r'   )tuple)r   r  rz  s    @r%   flatten_inputsz.JitTestCase.checkTrace.<locals>.flatten_inputs  s!     &68TVXYZZr'   c                 >    | j                         j                         S r    )clonerp  r-   s    r%   r/   z(JitTestCase.checkTrace.<locals>.<lambda>+  s    aggi6N6N6Pr'   F)check_tolerance_force_outplacecheck_trace)allow_unusedrr  T)create_graphr  c                     t        | d      S )NT)rj  r   r-   s    r%   r/   z(JitTestCase.checkTrace.<locals>.<lambda>Z  s    hqPT6Ur'   g-C6J?)r2   r+   r6   r5  getExportImportCopyprintr   r   autogradgradr   )rN   r   reference_tensorsinput_tensorsr  r  verboseinputs_require_gradsr  export_importr  	grad_atol	grad_rtolr  r  nograd_inputsflattened_recording_inputsr[  rS  
outputs_gegradsgrads_gel1l2grads2l1_gel2_ge	grads2_geg2g2_gerz  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	E:%-  UD A 4
 	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+   r6   rR  r   r   assertExportImportModule)rN   	nn_modulerp   sm	eager_out
script_outs         r%   checkModulezJitTestCase.checkModuleq  s|    
 YYi(!4(I   TJ   	J/%%b$/	    s   A6B6A?B)r<   )TN)F)
NNFFTgh㈵>TFNN)!r^   r_   r`   _do_cuda_memory_leak_checkr   r?   rs   r{   r   r   r   r   r   r   r   r   r~   r  r  r&  r.  r,  r;  rG  rI  r   	PROFILINGr\  rg  rw  r  r  __classcell__)r   s   @r%   rc   rc   c   s    !%) )) )Q1(<;M|*,4,(+859$Y %)$u)*m6M6M( T8  !).#(+55N#` AE:?RVDHbHr'   rc   c                       e Zd Zd Zd Zy)NoTracerWarnContextManagerc                     t         j                  j                         | _        t         j                  j	                  d       y NF)r+   r4   _jit_get_tracer_state_warnprev_jit_set_tracer_state_warnrR   s    r%   rS   z$NoTracerWarnContextManager.__enter__  s(    HH779	++E2r'   c                 V    t         j                  j                  | j                         y r    )r+   r4   r  r  ro   s     r%   r]   z#NoTracerWarnContextManager.__exit__  s    ++DII6r'   N)r^   r_   r`   rS   r]   r<   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+   r4   _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+   r4    _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rU   )r+   r4   %_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rU   r  rp   r-  r0   s     r%   wrapperz#_inline_everything.<locals>.wrapper  s$    #D) *))   	(	functoolswrapsr0   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+   r6   r5  )r   rp   r-  s    r%   r  z_trace.<locals>.wrapper  s    yytT4V44r'   r<   )rp   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+   r4   $_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%r<   r  s   ` r%   enable_cpu_fuserr    s    	9 Nr'   c                     | rt         S d }|S )Nc                       fd}|S )Nc                       | i |S r    r<   r  s     r%   r  z8enable_cpu_fuser_if.<locals>.noop_fuser.<locals>.wrapper  s    4*6**r'   r<   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   r8  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	    sJ     //,,224 %4$!Q||F# 4 % % %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)ra  rB   r+   r  r  rm   backward)r   rp   profiling_countresultsrE  rs         r%   warmup_backwardr    s`    OG?#t9q=##A--ANN1JJDJ) $ Nr'   c                  v    | D ]4  }t        t        j                  |j                     |j                  |       6 y r    )setattrrg   modulesr_   r^   )rp   args     r%   make_globalr    s)    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_moduler8  )	r"   fn_nametmp_dirscript_pathr   specr   r   r0   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 Zd Zy)TensorExprTestOptionsc                 (   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+   r4   _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  rR   s    r%   rP   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+   r4   r*  r+  r,  r-  r5  r4  r2  r1  r  r/  r  r6  r  r8  rR   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'   N)r^   r_   r`   rP   r:  r<   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,   rm   ro  r  r   )rp   r   r  r.   s       r%   clone_inputsr<    s    <>Fc5<<(MM#**,,,./#C(MMs;s!188:++-s;<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_fillrk  	full_likemulnew_fullr   c                 (    j                   | d   k(  S )Nr   )formatted_name)r   ops    r%   r/   z1get_traced_sample_variant_pairs.<locals>.<lambda>l  s    B,=,=6,Jr'   rB  )sample_inputsget_op
get_methodr   r8  r+   r,   rA   	is_lambdar   r?   rp   rB   r*   r9  intr  rm   )devicedtyperK  rS  samplesr   r  variantshas_fake_functionops_with_unsupported_bool_argssamplevariantmatching_opsop_dataidxrp   s     `             r%   get_traced_sample_variant_pairsr\  )  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^  r<   r<   r'   r%   r/   zis_lambda.<locals>.<lambda>z  s    Qr'   )r*   rZ   r^   )lambLAMBDAs     r%   rO  rO  y  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   rg   r  rO  importlib.abcr   typingr   r   r   r   r   cudais_availableRUN_CUDAdevice_countRUN_CUDA_MULTI_GPURUN_CUDA_HALFversionhipr4   _cuda_getCompiledVersionCUDA_VERSIONra  dget_device_capabilitymajorr&   r2   r=   rF   rH   r   rc   r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r&  r(  r<  r\  rO  r<   r'   r%   <module>r     s   # / F *         $  A F &   #   	  	  
     0 0::""$?%**"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'   