
    [Th&                       S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKJr  SSKJrJrJrJrJrJr  SSKrSSKrSSKrSSKJr  SSKrSSKJs  J r!  SSKJ"r"  SSK#J$r$J%r%  SSKJ&r&J'r'J(r(J)r)J*r*  SSK+J,r,  SS	K-J.r.  SS
K/J0r0  SSK1J2r2  SSK3J4r4J5r5J6r6J7r7  SSK8J9r9  SSK:J;r;  SSK<J=r=J>r>J?r?Jr@JArA  SSKBJCrCJDrD  SSKEJFrFJGrGJHrHJIrIJJrJ  SSKKJKrK  SSKLJMrM  SSKNJOrO  SSK>JPrPJQrQJRrRJSrSJTrT  SSKUJVrV  SSKWJXrX  SSKYJZrZJ[r[  SSK\J]r]  SSK^J_r_J`r`  SSKaJbrbJcrcJdrdJereJfrfJgrgJhrhJiriJjrjJkrkJlrlJmrmJnrnJoro  SSKJprpJqrqJrrrJsrsJtrtJuruJvrvJwrwJxrxJyryJzrzJ{r{J|r|J}r}J~r~JrJrJr  SSKJr  SSKJrJrJrJr  SS KJr  SS!KJrJr  SS"KJr  SS#KJrJrJrJr  SS$KJr  \(       a  SS%KJr  \GR2                  " \5      r\GR8                  GR;                  \S&5      r\GR8                  GR;                  \S'5      r\GR8                  GR;                  \S(5      r\GR8                  GR;                  \S)5      r\" S*S+9 " S, S-5      5       r " S. S/5      r\GRH                  " S5      S0 5       r\ " S1 S25      5       rS3 r " S4 S5\GRP                  GRR                  5      r " S6 S75      r\\\4   r " S8 S95      rS:rS; r\GRf                  " 5       r " S< S=5      r " S> S?\"GRl                  5      rg)@a  
Core graph building functionality for PyTorch's Dynamo system. This module contains
the essential components for constructing and managing FX graphs during compilation:

- OutputGraph: Manages the overall graph construction and compilation process. It owns
  a SubgraphTracer and handles graph compilation, execution, and state management.
  OutputGraph also manages features like graph deduplication, symbolic shape handling,
  and tracking of side effects.

- SubgraphTracer: Handles the actual FX graph construction by tracing Python code.
  It supports advanced features like higher-order operators through nested tracers,
  lifting of free variables, and handling of symbolic shapes.

The module supports key Dynamo features including:
- Higher-order operators through nested SubgraphTracers
- Graph deduplication for optimization
- Symbolic shape handling and propagation
- Side effect tracking and management
- Guard insertion and management
    N)	dataclass)AnyCallablecastOptionalTYPE_CHECKINGUnion)fx)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourceTracingContext)
FakeTensor)signpost_event)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnv)insert_deferred_runtime_asserts)is_traceable_wrapper_subclass   )configexcgraph_break_hintslogging	variables)
CompiledFn
CompilerFn)create_call_functioncreate_instructioncreate_load_constInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplemented_v2unimplemented_v2_with_warning)apply_graph_deduplication)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffects)
AttrSourceBackwardStateSourceConstantSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceNumpyTensorSourceParamBufferSourceShapeEnvSourceSyntheticLocalSourceTensorPropertyTensorPropertySource)_extract_tensor_dictcheckpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedget_instruction_source_311get_locals_to_stealget_static_address_typeget_unique_name_wrtgraph_break_reasonsincrement_op_countlazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)VariableTracker)BackwardStateGraphArgGraphArgTrackedFakewrap_fx_proxy)BaseListVariable)CellVariableNullVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariable)TensorWithTFOverrideVariable)InstructionTranslatorBasegraph
graph_codegraph_sizes
trace_callT)frozenc                   *    \ rS rSr% \\S'   \\S'   Srg)VariableTrackerCacheKey   vt_idsource N)__name__
__module____qualname____firstlineno__int__annotations__r   __static_attributes__rq       R/var/www/auris/envauris/lib/python3.13/site-packages/torch/_dynamo/output_graph.pyrm   rm      s    J Nry   rm   c                   2    \ rS rSrS rS rS rS rS rSr	g)	VariableTrackerCache   c                     0 U l         g Ncacheselfs    rz   __init__VariableTrackerCache.__init__   s	    
ry   c                 j    [        [        U5      U5      nX0R                  ;  a  g U R                  U   $ r   rm   idr   )r   valuerp   keys       rz   lookupVariableTrackerCache.lookup   s-    %bi8jj zz#ry   c                 J    [        [        U5      U5      nX0R                  U'   g r   r   )r   r   rp   vtr   s        rz   addVariableTrackerCache.add   s    %bi8

3ry   c                 d    [        5       nUR                  R                  U R                  5        U$ r   )r|   r   update)r   	new_caches     rz   cloneVariableTrackerCache.clone   s&    (*	tzz*ry   c                 8    U R                   R                  5         g r   )r   clearr   s    rz   r   VariableTrackerCache.clear   s    

ry   r   N)
rr   rs   rt   ru   r   r   r   r   r   rx   rq   ry   rz   r|   r|      s    ry   r|   c                  6    [         R                  " [        5      $ r   )torchdynamo_loggingget_step_loggerlogrq   ry   rz   _step_loggerr      s    ..s33ry   c                   \    \ rS rSr% Sr\\S'   \\R                     \S'   Sr
\\S'   S rSrg	)
GraphCompileReason   zOStores why a given output graph was compiled; i.e. what caused the graph break.reason
user_stackTgraph_breakc                 T    U R                   (       a  [        R                  " U 5        g g r   )r   rQ   appendr   s    rz   __post_init__ GraphCompileReason.__post_init__   s    &&t, ry   rq   N)rr   rs   rt   ru   __doc__strrw   list	tracebackFrameSummaryr   boolr   rx   rq   ry   rz   r   r      s,    YKY++,, K-ry   r   c                    ^  U 4S jnU$ )Nc            
      P   > T V VVs/ s H  u  po " U0 UD6PM     snnn $ s  snnn f r   rq   )fnargskwargsrandom_callss      rz   _gen_rand_values1_get_gen_rand_values_fn.<locals>._gen_rand_values   s*    <HIL(8&D#F#LIIIs   !rq   )r   r   s   ` rz   _get_gen_rand_values_fnr      s    J ry   c                   t   ^  \ rS rSrSrS\\\R                  R                  4   4U 4S jjr
S\4S jrSrU =r$ )FakeRootModule   z'Trick the constructor of fx.GraphModule
nn_modulesc                 l   > [         TU ]  5         UR                  5        H  u  p#[        XU5        M     g r   )superr   itemssetattr)r   r   kv	__class__s       rz   r   FakeRootModule.__init__   s-    $$&DADQ 'ry   returnc                     g)NzFakeRootModule(...)rq   r   s    rz   __repr__FakeRootModule.__repr__   s    $ry   rq   )rr   rs   rt   ru   r   dictr   torchnnModuler   r   rx   __classcell__r   s   @rz   r   r      s5    1 4UXX__(<#=  
%# % %ry   r   c                   v    \ rS rSrS\4S jrS\R                  R                  S\	\R                     4S jrSrg)	WrapperBackend   backendc                     Xl         g r   )r   )r   r   s     rz   r   WrapperBackend.__init__   s    #*ry   gmexample_inputsc                    [        U5      U l        Xl        [        R                  " U R                  5      nU R                  X25      U l        U R                  b#  U R                  U R                  R                  L a  U R                  R                  $ [        R                  (       d  U R                  $  U R                  R                  " [        U5      6 nU R                  " [        U5      6 n[        XE5      (       a  U R                  U R                  5         $ [        SU  35      e! [         a    [        R                  S5        e f = f! U R                  5         f = f)Nzincorrect results of backend zerror in verify_correctness)rG   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessrI   rV   RuntimeError	Exceptionr   	exception)r   r   r   copy_gmcorrectresults         rz   __call__WrapperBackend.__call__   s   (,--(g>>>!T^^tww%F77??"((>>!	ggoo|N'CDG^^\.%ABF G$$~~ LLN !>tfEFF  	MM78	 LLNs   5AD) D) )!E

E E)r   r   r   r   N)rr   rs   rt   ru   r#   r   r   r
   GraphModuler   Tensorr   rx   rq   ry   rz   r   r      s4    +
 +588// ellAS ry   r   c            
         ^  \ rS rSr% Sr\\S'   S\\\	4   S\
\   S\S\S\4
U 4S	 jjrS
 rSXS\4S jjrS rS rS rS\/ \	4   4S jrS r\S 5       r\S 5       rS r\S 5       r\R:                  S 5       r\S 5       r\S 5       r\S 5       r S r!S r"S r#\$RJ                  S 5       r&\S 5       r'\S  5       r(\S! 5       r)\S"\*RV                  RX                  4S# j5       r-\S"\\\	4   4S$ j5       r.SYS& jr/S' r0S( r1\S) 5       r2S* r3S+ r4S, r5SZS- jr6S. r7\8S/ 5       r9S0\S1\	S"\:Rv                  4S2 jr<S3\=\*R|                  R~                  \*R                  \	4   4S4 jrAS5 rB S[S6\
\C   4S7 jjrDS8 rES9 rFS: rGS;\4S< jrH\$RJ                  S= 5       rIS> rJS? rK\S"\L\:R                     4S@ j5       rN\S"\L\O   4SA j5       rPSB\:R                  S"\R4SC jrSSB\:R                  S"\R4SD jrTSE rUSF rVS"\L\*R                     4SG jrWS\SH jrXS\SI jrYS\SJ jrZSK\L\[   S"S%4SL jr\S\SM jr]S"\4SN jr^S"\4SO jr_S\SP jr`SQ\S"S%4SR jraSS\\:R                  /S%4   S"S%4ST jrbSU\*Rt                  R                  4SV jrcSWrdU =re$ )]OutputGraphi  ac  
Wrapper class to hold outputs of InstructionTranslator.  Mainly the
generated fx.Graph.

OutputGraph is 1:1 with a frame being processed. Each frame is associated
with some root InstructionTranslator. When user code calls a function,
we construct a InliningInstructionTranslator that continues to write into
the root InstructionTranslator's OutputGraph.
side_effectscode_optionscompiler_fnexportlocal_scopeglobal_scopec           	        > [         TU ]  5         [        XS9/U l        0 U l        X@l        XPl        X`l        0 U l        / U l	        [        [        5      U l        [        5       U l        U	R                  U	R                   U	R"                  S.U l        ['        5       U l        / U l        [-        U R*                  [.        R0                  [.        R2                  [.        R4                  [.        R6                  U R$                  S9nSS KJs  Jn  UR=                  SS9   [>        R@                  RC                  UU R
                  (       a  SOSU R
                  S9nS S S 5        [E        W5      U l#        [H        RJ                  " 5       U l&        U RO                  5         [P        RR                  " [T        5      U l+        0 U l,        [[        U 5      U l.        [_        5       U l0        [b        Rd                  " 5       U l3        [i        U5      U l5        / U l6        SU l7        / U l8        X l9        Xl:        Xpl;        X0l<        0 U l=        / U l>        / U l?        SU l@        0 U lA        [>        R                  R                  5       U lD        [>        R                  R                  5       U lF        XlG        SU lH        [        0 5      U lI        [        0 5      U lJ        U R                  5         0 U lL        / U lM        S U lN        / U lO        0 U lP        S U lQ        S U lR        U R                  5       U lT        [        5       U lU        g ! , (       d  f       GN= f)	N)	is_export)co_nameco_filenameco_firstlineno)tracked_fakesallow_scalar_outputsallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards'allow_complex_guards_as_runtime_asserts	co_fieldsr   F(fake_tensor_allow_unsafe_data_ptr_accessT)	shape_envallow_non_fake_inputsr   )Vr   r   SubgraphTracertracersinput_source_to_varr   export_constraintsframe_stateinput_source_to_sizes_stridescleanup_hooksnext_compile_id_counter
compile_idsetinstalled_globalsr   r   r   r   r2   region_trackerr   r   r   capture_scalar_outputs capture_dynamic_output_shape_opsr   r   torch._functorch.config
_functorchpatchr   _subclassesFakeTensorModer   tracing_contextr   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   tracked_fakes_id_to_sourceparam_name_to_sourcer7   r   r|   variable_tracker_cache	itertoolscountunique_var_idr   r   output_instructions	timestampregister_finalizer_fnsr   r   r   root_txsource_to_user_stacks_current_txcleanupsshould_exitunspec_variable_map_C_is_torch_function_enabledtorch_function_enabled_is_torch_function_mode_enabledtorch_function_mode_enabledtorch_function_mode_stack!has_user_defined_allowed_in_graphnon_compliant_opscompliant_custom_opssave_global_state dynamo_flat_name_to_original_fqnr   random_values_varpregraph_bytecodebackward_statebackward_state_proxybackward_state_var!install_builtins_dict_in_fglobals%name_of_builtins_dict_key_in_fglobalsguard_on_key_order)r   r   r   r   r   r   r  r   r   f_coder+  r   _config	fake_moder   s                 rz   r   OutputGraph.__init__  s    	&t>? CE "4&KM*68#$78+.5
 ~~!--$33
 12 13 ,,!'!>!>+1+R+R8>8j8j4:4b4bnn
	$ 	21]]E]J))88#.2kkdu{{	 9 I K 0>i/H--/ 	 	  " ##D) 	' BD!'- ';&<#&__. .68   OQ# 2=(& RT"<>+- KM  ',hh&I&I&K#+088+S+S+U()B& 27. >AW ADB! 	  AC-  	 "& 57 ;=>B!15 224 	2 -0Ee KJs   =L11
M c                     U R                   S   n[        U[        5      (       d  UR                  nU R	                  SU5      $ )N__builtins____builtins_dict__)r   
isinstancer   __dict__install_global)r   
f_builtinss     rz   r6  -OutputGraph.install_builtins_dict_in_fglobals  s@     &&~6
*d++#,,J""#6
CCry   hookc                     U [        U R                  5       3nX0R                  ;  d   eXR                  U'   X0R                  5       4$ r   )lenr3  get_backward_state_proxy)r   rE  prefixnames       rz   add_backward_state_hook#OutputGraph.add_backward_state_hook  sN    #d11234.....$(D!22444ry   c                 f   U R                   c  U R                  (       a  [        SSS/ S9  [        5       nU R                  R                  S[        U5      U[        5       S9U l         [        5       U R                   R                  R                  S'   U R                  5       U l        U R                   $ )Nz&backward_state does not support export z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_staterp   grapharg)r4  r   r/   r   root_tracercreate_graph_inputtyper9   rY   nodemetanew_varr5  )r   example_values     rz   rH  $OutputGraph.get_backward_state_proxy  s    $$,{{ D U	 *OM(,(8(8(K(K']#*,	 )L )D% ?T>UD%%**//
;&*llnD#(((ry   c                 \   U R                   R                  [        5       R                  [        R
                  5      5        U R                   R                  [        5       R                  [        R                  5      5        U R                   R                  [        5       R                  [        R                  5      5        U R                   R                  [        5       R                  [        R                  5      5        U R                   R                  [        5       R                  [        R                  5      5        [        R                  R                  R                  5       nUbA  U R                   R                  [        5       R                  [        R                  5      5        g g r   )guardsr   rB   
make_guardr3   	SHAPE_ENVr<   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATEr   r&  r  peek_interpreter_stackFUNCTORCH_STACK_MATCH)r   cis     rz   r  OutputGraph.init_ambient_guards  s    	(33L4J4JKL**<+P+PQ	
 	)+66|7M7MNO)+66|7R7RST**<+L+LM	
 XX  779>KKOO!#..|/Q/QR ry   c                 b  ^^ T" U6 nU R                  5       n[        U R                  5      mTR                  UU4S j5        TR	                  [        [        R                  R                  U5      5        TR                  [        U5      S5        TR                  U5        U R                  R                  TR                  5       5        [        U5      n[         R"                  " U R                  X55      n[$        R&                  " 5       R(                  R*                  R-                  U5        U$ )zM
call fn(*args) before the graph runs and turn the result into a fake input.
c                  P   > T R                  TR                  TR                  5      $ r   )load_import_fromrs   rr   )cgr   s   rz   <lambda>3OutputGraph.synthetic_graph_input.<locals>.<lambda>  s    B''ry   F)r\  r*   r   add_push_nullforeachmapr!   ConstantVariablecreatecall_functionrG  storer2  extendget_instructionsrC   rX   buildr   getguards_contextdynamo_guardsremove_guards_with_source)r   r   r   r]  varnamerp   r   rn  s    `     @rz   synthetic_graph_input!OutputGraph.synthetic_graph_input  s     D	,,.t||$
	
 	

3y1188$?@
TE*
%%b&9&9&;<%g. &&t||]K++99SS	
 ry   r   c                 :    U R                   R                  U5        g r   )r  r   )r   r   s     rz   add_cleanup_hookOutputGraph.add_cleanup_hook,  s    !!"%ry   c                 ~    [        U R                  5       H
  nU" 5         M     U R                  R                  5         g r   )reversedr  r   )r   rE  s     rz   call_cleanup_hooksOutputGraph.call_cleanup_hooks/  s.    T//0DF 1  "ry   c                      U R                   S   $ Nr   r   r   s    rz   rW  OutputGraph.root_tracer4  s    ||Ary   c                      U R                   S   $ Nr  r   s    rz   current_tracerOutputGraph.current_tracer8  s    ||Bry   c                 2    [        U R                  5      S:H  $ )Nr   )rG  r   r   s    rz   is_root_tracerOutputGraph.is_root_tracer<  s    4<< A%%ry   c                 .    U R                   R                  $ r   r  rg   r   s    rz   rg   OutputGraph.graph@  s    ""(((ry   c                 $    XR                   l        g r   r  )r   r   s     rz   rg   r  E  s    $)!ry   c                 .    U R                   R                  $ r   )r  input_name_to_proxyr   s    rz   r  OutputGraph.input_name_to_proxyI  s    ""666ry   c                 .    U R                   R                  $ r   )r  real_value_cacher   s    rz   r  OutputGraph.real_value_cacheM  s    ""333ry   c                 .    U R                   R                  $ r   )r  bound_symbolsr   s    rz   r  OutputGraph.bound_symbolsQ  s    ""000ry   c                 :    U R                   R                  " U0 UD6$ r   )r  create_proxyr   r   r   s      rz   r  OutputGraph.create_proxy[  s    ""//@@@ry   c                 :    U R                   R                  " U0 UD6$ r   )r  create_noder  s      rz   r  OutputGraph.create_node^      ""..???ry   c                 :    U R                   R                  " U0 UD6$ r   )r  remove_noder  s      rz   r  OutputGraph.remove_nodea  r  ry   c              #     #    [        5       n U(       a  UR                  U R                  L d   eUR                  5         U(       a  UO)[	        U U R                  UU R                  R
                  S9nU R                  R                  U5        Uv   UR                  S S S 5        U R                  R                  5         g ! UR                  S S S 5        U R                  R                  5         f = f7f)N)parentsource_targetr   )
r+   r  r  	__enter__r   r   r   r   __exit__pop)r   r  prior_tracernew_scope_ctxtracers        rz   	subtracerOutputGraph.subtracerd  s     ')	#**d.A.AAAA##%   #.."/"11;;	  LL'L""4t4LL ""4t4LLs   C2BC  .C2 /C//C2c                     U $ r   rq   r   s    rz   outputOutputGraph.output|  s    ry   c                 .    U R                   R                  $ r   )r  r;  r   s    rz   r;  OutputGraph.fake_mode  s    ##---ry   c                 B    U R                   R                  R                  $ r   )r  r;  r   r   s    rz   r   OutputGraph.shape_env  s    ##--777ry   r   c                 B    U R                   R                  R                  $ r   )r  r|  r}  r   s    rz   r`  OutputGraph.guards  s    ##22@@@ry   c                 B    U R                   R                  R                  $ r   )r  module_contextr   r   s    rz   r   OutputGraph.nn_modules  s    ##22===ry   Nc                 v   [        [        [        [        [        S[
        4   [        4   4   Ub  UOU R                  R                  R                  5      nU R                  U R                  4US'   [        R                  [        R                  " 5       4US'   [        R                   " [        R"                  S5      [        R$                  " S5      4US'   [        R                   " [        R"                  S5      [        R$                  " S5      4US'   [        R                   " [        R&                  S5      [        R(                  " S5      4US	'   [        R                   " [        R&                  S5      [        R(                  " S5      4US
'   [        R*                  [        R,                  " 5       4US'   g)zS
Saves to out if it is provided. Else saves to the tracing context's global_state.
.Nr(  grad_enabledcudaautocast_enabledcpuautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r   r   r   tupler   r   r   r  global_contextglobal_stateset_torch_function_stater(  r   set_grad_enabledis_grad_enabled	functoolspartialset_autocast_enabledis_autocast_enabledset_autocast_dtypeget_autocast_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)r   outr  s      rz   r/  OutputGraph.save_global_state  s    eHS#X.4556 ? ))88EE
  ))''2
-. ).(>(>@U@U@W'X^$ e88&A%%f-,
'(
 e88%@%%e,0
+,
 e66?$$V,.
)*
 e66>$$U+.
)*
 ,,++-2
-.ry   c                 :    U R                   R                  U5        g r   )r"  r   )r   txs     rz   push_txOutputGraph.push_tx  s    #ry   c                 6    U R                   R                  5       $ r   )r"  r  r   s    rz   pop_txOutputGraph.pop_tx  s    ##%%ry   c                 Z    U R                   (       d  U R                  $ U R                   S   $ r  )r"  r   r   s    rz   
current_txOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMry   c                 ,    [        U R                  5      $ r   )rJ   rg   r   s    rz   rJ   OutputGraph.count_calls  s    4::&&ry   c                 X    [        [        U R                  R                  5      5      S:H  $ r  )rG  r   rg   nodesr   s    rz   is_empty_graphOutputGraph.is_empty_graph  s!    4

(()*a//ry   c                     U(       d   eU R                   nUR                  S5       H)  n[        U[        5      (       a  X#   nM  [	        X#5      nM+     U$ N.)r   splitr@  r   getattr)r   keysobjr   s       rz   get_submoduleOutputGraph.get_submodule  sH    tBF//CA#t$$fco	 !
 
ry   c                     [        U R                  S   5      n U S[        U R                  5       3nX2;  a  U R                  S==   U4-  ss'   U$ M;  )Nco_varnames_)r  r   r  r  )r   rJ  existingvars       rz   r\  OutputGraph.new_var  s^    t((78F!D!3!3456C"!!-0SF:0
	 ry   c                 Z    XR                   S   ;  a  U R                   S==   U4-  ss'   gg)z/Ensure self.code_options.co_names contains nameco_namesN)r   )r   rJ  s     rz   update_co_namesOutputGraph.update_co_names  s0    ((44j)dW4) 5ry   c                     SR                  [        [        U 5      5      n[        R                  " SSU5      n[        R                  " SSU5      n[        R                  " SSU5      nU(       a  US   R                  5       (       d  SU-   nU$ )	Nr  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)joinrs  r   rer  isalpha)namesrJ  s     rz   module_key_nameOutputGraph.module_key_name  sp     xxC(vv,eT:vvlIt4vvosD147??,,4<Dry   attr_prefix
attr_valuec                     [        XR                  5      nX R                  U'   U R                  SUS0 5      n[        UR                  U5        U$ )Nget_attrrq   )rP   r   r  rW   rZ  )r   r  r	  	attr_nameproxys        rz   %register_static_attr_and_return_proxy1OutputGraph.register_static_attr_and_return_proxy  sI     (__E	 &0	"!!*iR@%**j1ry   targetc                 p  ^ ^^^
^^ [        TT R                  R                  5      (       a"  [        R                  " T R
                  T40 TD6$ [        T5      mST;   d   eTS   m[        T[        5      (       a   e[        T[        R                  5      (       a8  T R                  mT R                  5       (       d  T R                  mUU UUU4S jnO[        T[        R                  R                  5      (       aj  [        T[        R                  R                  5      (       d   eT(       a0  [!        TR#                  [$        R&                  5      5        UU4S jnOGUU4S jnO?[        T[        R(                  [        R*                  45      (       a	  UU U4S jnOU U4S jnT R,                  R/                  5        H  u  pVUTL d  M  U" U5      s  $    [0        R2                  " U6 m
[5        T
T R,                  T R6                  5      m
TT R,                  T
'   [        T[        R                  R                  5      (       al  U
U U4S jn[9        TS5      (       a!  TR;                  5        H  u  pU" U5        M     [9        TS	5      (       a!  TR=                  5        H  u  pU" U5        M     U" T
5      $ )
Nrp   c           	      6  > TR                   c   eTTR                   U '   TTR                  R                  R                  ;   a#  TR                  R                  R                  T   $ [	        T5      S:X  a>  [        T[        5      (       d)  [        TR                  [        R                  5      5        O8[        T5      (       d(  [        TR                  [        R                  5      5        [        TR                  TR                  SU S0 5      4ST0TD6nTR                  R                  R                  R                  TU5      nSUR                   R"                  R$                  ;  d   e['        T5      UR                   R"                  R$                  S'   U$ )Nguardedr  rq   r]  tensor_dict)r  r   r  r   rO   r@  r@   r4   ra  r3   ID_MATCHr=   TENSOR_MATCHr\   r  track_object_existingr  rZ  r[  rF   )
module_keyr   optionsr   rp   r  r  s     rz   	wrap_name6OutputGraph.register_attr_or_module.<locals>.wrap_name!  sV   00<<<8>))*5 T\\00===<<..;;FCC*62i?
-I I "&"3"3L4I4I"JK+F33!&"3"3L4M4M"NO"LL''
JBG #) 	 \\((55KKFTVW$BHHMM,>,>>>>4H4P""=1	ry   c                 2   > [        [        T5      U T40 TD6$ r   )r`   rY  r  r  r  s    rz   r  r  G  s    +DL*fXPWXXry   c                 2   > [         R                  " T40 TD6$ r   )r!   UnspecializedNNModuleVariabler  s    rz   r  r  O  s    $BB6UWUUry   c           	      \   > [         R                  " TTR                  SU S0 5      4ST0TD6$ )Nr  rq   sym_num)rb   ru  r  )r  r  r   r  s    rz   r  r  Z  sA    &--%%j*b"E # 	 ry   c                    > TR                   R                  U 5        TTR                  U '   [        R                  " TT[        U S95      $ )N)source_name)r  r  r   rX   rz  r:   )r  r   r  s    rz   r  r  e  sD    ++J706!!*-&,,&.Z"H ry   c                    > TR                   c   e[        TU 5      nT SU  3nUTR                   U'   [        T[        5      (       a1  U TR                  [
        R                  UR                  5       5      '   g g r  )r  rA   r@  r?   r0  r   r  rJ  )	leaf_name
new_sourcenew_namerJ  r   rp   s      rz   register_leaf_name?OutputGraph.register_attr_or_module.<locals>.register_leaf_namev  s}    00<<<.vyA
"V1YK06@))(3fk22 " 99#33JOO4EF 3ry   _parameters_buffers)r5   r   r   rX   rz  r  r   r@  rA   r   r   r  r  rW  r   r   r4   ra  r3   	NN_MODULESymIntSymFloatr   r   r   r  rP   r   hasattrnamed_parametersnamed_buffers)r   r  r  r  r  r   r   r(  r%  r  rJ  rp   r  s   `` `      @@@rz   register_attr_or_module#OutputGraph.register_attr_or_module  s     (;(;<< #((&LGLLw-7""""f&78888fell++((F&&(( )) @ 00fehhoo6666f//0F0FGHYV u~~ >??  OO))+DAF{ |# ,
 **E2"4$:K:KL &fehhoo.." v}--$*$;$;$=LI&y1 %>vz**$*$8$8$:LI&y1 %; ry   c                    U R                   R                  S5      n[        U5      nU(       d  / 0 4$ / n0 n/ UR                  QUR                  R                  5       QU R                  R                  R                  5       QnU(       GaC  UR                  5       n[        U[        5      (       a1  [        UR                  [        5      (       d   eXgR                  -  nM^  XpR                  R                  ;  d  [        UR                  [        5      (       al  [        UR                   ["        5      (       aM  [        UR                   R$                  [&        5      (       a$  UR                   R$                  R(                  U;   d  GM  UR                   R$                  R(                  nX;  a  / XX'   XX   R+                  U5        U(       a  GMC  0 n	0 n
U R,                   GH*  n[        UR.                  [        5      (       a9  [        UR                   [&        5      (       a  UR                   R(                  U;   d  M^  UR                   R(                  nXR0                  S   ;   d   eX\    H  nUR                   U
;   a  M  UR                   R2                  nX;  aO  U R5                  U S35      nXU'   UR7                  [9        SUS9[;        U5      [9        S5      [9        SUS9/5        UR                   n['        X   5      X'   M     GM-     XJ4$ )Nr   r  _ref	LOAD_FASTargvalBINARY_SUBSCR
STORE_FAST)r   r{  rN   stacksymbolic_localsvaluesr   store_attr_mutationsr  r  r@  r]   r   r   mutation_typer6   rp   r;   baser?   
local_namer   	graphargs_exampler   indexr\  rx  r%   r&   )r   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuexstolen_namevisitedoverridden_sourcesarg	list_namelist_idx
alias_name
old_sources                   rz   handle_aliases_for_stolen_lists+OutputGraph.handle_aliases_for_stolen_lists  s   ##''//9 r6M8:
XX
&&(
 3388:
 		A!-..!!''40000  ..CCC!!//3LMMqxx77qxx}}k::HHMM,,0AA((--22K-+-($++A.+ e. 35>>C3<<..szz;77JJ))[8 

--I 1 1- @@@@ +8811 88>>*!%$+T*"J )3H%&&.{9M-h7.?.|JO	 XX
1<W=N1O".5 , "V ..ry   r   c                   ^ ^! Uc   eSSK Jn  UT l        UT l        ST l        [
        R                  SU5        [        S UR                   5       5      (       d  [        SSS	/ [        R                  QS
9  / m![        R                  S:  a  UR                   H  nUR                  S:X  a%  T!R!                  [#        SUR$                  S95        M8  UR                  S:X  a1  T!R!                  [#        S['        UR(                  S   5      S95        My  T!R!                  [*        R*                  " U5      5        M     T R,                  (       a  T R.                  (       a   S5       eT!R1                  T R,                  5        T R3                  U5      u  pgT!R1                  U5        U!U 4S jn[5        UR                  5       H  n	U	R7                  XR8                  S9  M     T R;                  5         UR=                  5         [?        UR@                  5      n
U
 H  nURC                  5         M     T RE                  5       nT RF                  RI                  5        VVs0 s H  u  pU[K        U5      _M     nnn[M        U5      n/ n0 nURN                  RI                  5        H  u  nn[Q        URR                  [T        5      (       a  URR                  RV                  U:X  a  MA  [Q        U[X        5      (       a  URV                  U:X  a  Mh  [        R                  S:  a"  [Z        R]                  [^        U5      (       a  M  O![Z        R]                  [^        U5      (       a   eUU;  a  / UU'   UU   R!                  U5        M     URa                  5        H8  nUR1                  UU   5        U
R1                  U/['        UU   5      -  5        M:     ['        T Rb                  5      S:  a  U" 5         / nT Re                  S5      T l3        U" [i        T Rb                  5      5      nT Rk                  SU5      n[m        UUUS9nUR1                  URo                  US5      5        UR1                  [q        SS5      5        UR!                  URs                  URt                  Rf                  5      5        T Rw                  U5        U
(       Ga,  [        S U
 5       5      (       Ga  [        S U
 5       5      (       a  ['        [y        U
5      5      ['        U
5      :X  a  T Rz                  R}                  5       (       a  ['        UR~                  5      S:w  d  T R                  (       d  U" 5         T Rw                  T R                  U[?        [5        U
5      5      UU5      [#        S['        U
5      S9/-   5        T Rw                  [5        U5       Vs/ s H  n[m        XS9Rs                  U5      PM     sn5        gT Re                  S5      n[m        UUUUS9nT R                  XU5        [m        UUUUR                  RI                  5        VVs0 s H  u  nnUS:  d  M  US_M     snnUS9nT R                  XU5        Sn/ n [        T R                  5      S:w  d  ['        UR                  5      S:w  a  U R1                  T R                  UUR                  5       UU5      5        ['        UR                  5      S:w  a#  U R!                  URs                  U5      5        SnO,U R!                  [#        S5      5        OT R                  U5        U" 5         T Rw                  U UR                  5       -   5        T Rw                  [5        U5       Vs/ s H  n[m        XS9Rs                  U5      PM     sn5        U(       a)  T Rw                  [m        XS9R                  U5      /5        ggs  snnf s  snf s  snnf s  snf ) z_
Generate a subgraph to continue execution on user code.
Automatically restore live variables.
Nr   disableTzCOMPILING GRAPH due to %sc              3   @   #    U  H  oR                  5       v   M     g 7fr   )can_restore).0blocks     rz   	<genexpr>/OutputGraph.compile_subgraph.<locals>.<genexpr>  s     CN5$$&&N   z'Attempt to compile graph in a try blockrN  z9Dynamo cannot compile traced graphs while in a try block.rO        	MAKE_CELLr7  COPY_FREE_VARSco_freevars)rN  z)export does not support pregraph_bytecodec                  H   > TR                  T 5        T R                  5         g r   )add_output_instructionsr   )prefix_instsr   s   rz   append_prefix_insts9OutputGraph.compile_subgraph.<locals>.append_prefix_insts  s    ((6 ry   )is_graph_break)r`     r   random_values__gen_rand_values)rM  Fc              3      #    U  H^  n[        U[        [        [        45      (       + =(       a2    [        U[        5      =(       a    UR                  5       [        L (       + v   M`     g 7fr   )r@  rd   ra   re   rb   python_typefloat)rZ  r   s     rz   r\  r]  c  sa       &A 3,4  V $A7TAMMOu<TUV &s   A&A(c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r@  rc   rZ  rJ  s     rz   r\  r]  o  s     H<aJq.11<s   UNPACK_SEQUENCE	graph_out)tempvarsrM  POP_TOP)K
decoratorsrW  partial_convertcompile_subgraph_reasonr$  r   debugallblock_stackr/   r   CAUSED_BY_EARLIER_GRAPH_BREAKsysversion_inforg  opnamer   r%   r8  rG  r   r   r2  r   rx  rS  r  exitr   cleanup_graphprune_dead_localsr   r;  realize
dedup_passr   r   rU   r   r<  r@  rp   r?   rA  r^   rY  __instancecheck__r_   r  r   r\  r1  r   rB  r*   load_function_namer$   create_storer  rf  r  r   is_emptydebug_localsr3  compile_and_call_fx_graphcodegen_suffixusesrJ   rg   graph_outputsgraph_output_varsrun_compiler_collectivery  create_delete)"r   r  rx  r   rW  instrG  rM  rh  r[  stack_valuesr   output_replacementsrJ  modnn_modules_proxiesrootrestore_varsval_to_namesr   r   random_calls_instructionsrand_fnrand_fn_namecodegenr  graph_output_varpass1valr  pass2stored_graph_output_varr  rg  s"   `                                @rz   compile_subgraphOutputGraph.compile_subgraph  s     !!!'.'-$		-v6CBNNCCCAW&DD	 +-w&;;+- ''*;t{{K [[$44 ''*,#boom6T2U !''		$8 ( **t{{ 	
7	
; 	D223*.*N*Nr*R'K(	! bnn-EJJr*<*<J= . 	
BHH~ "EMMO " #oo/ 9=8M8M8O
8O94D/#&&8O 	 
 01"$9; &&,,.DAq !((K00QXX5H5HA5M!\**q||q/@7*)),:: ;  11,BBBB$"$QO""1%) /* ""$AQ0c,q/&: :; %
 t  !A%!(*%%)\\/%BD"5d6G6GHIG../BGLLD=OPG%,,**<> &,,-A!U-KL%,,$$RYY%@%@A (()BC   &   H<HHHC%&#l*;;!!**,,(A-''!((..Xl34d<O &&7S=NOPQ ((
  (5	  6 bHUU  6	  $||K8D*?QE %8  6;jj6F6F6HV6H
UETUI)#t)6HV#5E %8&+#F4::&!+s53F3F/G1/L22E335t=P u**+q0MM%"4"45E"FG.2+MM"4Y"?@ ,,R0!((%2H2H2J)JK ((
  (5	  6 bHUU  6	 ',,!'-(89 'W
h& W8s   _"_%_*_*"_0c                   ^^ U R                   R                  T5        U R                  (       ay  U R                  (       a   eU R                  R	                  5        HH  u  pET" U5        TR                  TR                  U R                  5      5        TR                  U5        MJ     U R                   R                  T5        UR                   Hl  u  mnTR                  UU4S j5        U H  nT" U5        M     TR                  [        [        U5      S5      5        TR                  [        S5      /5        Mn     TR!                  X!R                  (       + S9  U R                   R#                  T5        g )Nc                     > T " T5      $ r   rq   )rn  	debug_vars   rz   ro  ,OutputGraph.codegen_suffix.<locals>.<lambda>  s	    R	]ry   Frv  )value_from_source)r   codegen_save_tempvarsr3  r   r   append_outputcreate_loadr5  
store_attrcodegen_hooksr  rq  extend_outputr$   rG  r%   restore_stackcodegen_update_mutated)	r   r  r  rn  rJ  r  r   rN  r  s	      `    @rz   r  OutputGraph.codegen_suffix  s!    	//3{{"?!00668	3  0G0G!HId# 9 	''+  "OIt233 1#d)UCD0;<=  / 	YYG004ry   c                 *   U R                   (       d   e[        U R                  R                  5      nU H  nUR                  R                  SS5        M!     [        R                  " 5       n[        XSS 5       GH  u  pEUR                  [        R                  R                  L d  M/  [        UR                  5      U(       + 4:X  d  MP  UR                  (       a  Mc  UR                  S   nUR                  [        R                  R                  L d  M  [        UR                  5      U(       + 4:X  d  M  UR                  (       a  M  UR                  S   nU R                  R                  U5        U R                  R                  U5        GM     g)z
Remove "creation_timestamp" from node meta

Remove this pattern from the graph:
    torch._C._set_grad_enabled(False)
    torch._C._set_grad_enabled(True)
creation_timestampNr   r   )r$  r   rg   r  r[  r  r   r  zipr  r&  _set_grad_enabledr  r   _erased
erase_node)r   r  rZ  r  node1node2s         rz   r  OutputGraph.cleanup_graph  s    TZZ%%&DIIMM.5  ,,.QRy1LE : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MMM#(::a=LJJ))%0JJ))%0 2ry   c                 z   0 nU R                   R                   H  nUR                  R                  SS 5      n[	        U[
        R                  R                  5      (       d  MJ  UR                  5       nU Vs/ s H%  n[	        U[        5      (       a  UO
[        U5      PM'     snXR                  '   M     U$ s  snf )Nr]  )rg   r  r[  r{  r@  r   r  r   sizerv   reprrJ  )r   retrZ  r]  r  ss         rz   get_graph_sizes_structured&OutputGraph.get_graph_sizes_structured  s    JJ$$D IIMM/4@M-):):)E)EFF$))+PT!UPT1z!S'9'9!tAw"FPT!UII	 %
 
 "Vs   7,B8rJ  c                    SnUSU S3-  nU R                   R                   GH  nUR                  R                  SS 5      n[	        U[
        R                  R                  5      (       d  MK  UR                  5       nX#R                   S[        U5       S3-  n/ nSnU Ht  n[	        U[        5      (       a  UR                  U5        M+  [	        U[
        R                  5      (       a)  SnUR                  UR                  R                  5        Ms    M     U(       d  M  UUR                   S	[        U5       S3-  nGM      U$ )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
r]  z: 
FTz (concrete): )rg   r  r[  r{  r@  r   r  r   r  rJ  r  rv   r   r-  rZ  hint)	r   rJ  graph_sizes_strrZ  r]  r  concrete_size
has_symintszs	            rz   get_graph_sizesOutputGraph.get_graph_sizes  s   7VD622JJ$$D IIMM/4@M-):):)E)EFF$))+ii[5;-r#BB ""
B!"c**%,,R0#B55%)
%,,RWW\\:  "z'#yyku]7K6LBO# %( ry   c              #     #    U R                   R                  R                  5       n0 nU R                  US9   U R                   R                  R	                  U5        Sv   U R                   R                  R	                  [        U5      5        g! U R                   R                  R	                  [        U5      5        f = f7f)zZ
Momentarily restores the global state to what it was prior to tracing the current output
)r  N)r  r  copy_graphstater/  restore_graphstater   )r   prior_global_statecurrent_global_states      rz   restore_global_state OutputGraph.restore_global_state  s     
 "11@@PPR<>#78	  //BBCUV   //BB,-ABD  //BB,-ABs   6C)B "/C0CCc                   ^ UR                   =mGb,  TR                  Gc  TR                  n[        R	                  STR
                  5        [        R                  R                  SS U4S jS9  [        R                  R                  UR                  5       [        R                  R                  5       -  5         [        SSS9   S /UR                  5       -  n[        R                   " UTR
                  US	9  UTl        S S S 5        S S S 5        UR"                  R%                  5         [&        R(                  eg g ! , (       d  f       NB= f! , (       d  f       NK= f)
Nzcompiler_collective %sartifactc                      SSS.$ )Ncompiler_collectivestring)rJ  encodingrq   rq   ry   rz   ro  5OutputGraph.run_compiler_collective.<locals>.<lambda>/  s    1 (%ry   c                  8   > T R                   R                  5       $ r   )local_staterender)dss   rz   ro  r  3  s    2>>#8#8#:ry   )metadata_fn
payload_fnr  T)log_pt2_compile_event)group)distributed_state
all_states
compile_pgr   infor  r   _loggingtrace_structuredr  devicerankdevice_countrL   r  distall_gather_objectspeculation_logr   r    CompileCollectiveRestartAnalysis)r   r  r  r  r  s       @rz   r  #OutputGraph.run_compiler_collective)  s   &&&B38MJHH-r~~>NN++ ; ,  

!!*//"3ejj6M6M6O"OP2$O"Vjoo&77
&&z2>>T *	 P Q $$&666+ 9N3 PO QPs$   <E<D?E?
E		E
Ec                 	  ^ ^ [         R                  R                  R                  5          SSKJn  T R                  (       d   eT R                  U5        [        S5      n[        U[        5      (       d   e[        U[        5      (       d   eT R                  SST R                  R                  [        S U 5       5      5      40 5      nUR!                  5        H  u  pUR#                  U	5        M     UR$                  R                  R'                  X5        [(        R*                  (       dO  T R-                  5         [/        [0        R2                  " UT R4                  5      T R6                  UT R8                  S9  T R;                  5         [=        T R4                  5      n
[>        S   S==   U
-  ss'   T RA                  5         T RB                  RE                  5         [G        UT R4                  5      mT RH                   H  nU" T5        M     T RJ                  Tl%        T RL                  RO                  5       TRP                  S	'   T RR                  TRP                  S
'   [T        RW                  S[Y        UTSSSS95        [         RZ                  R]                  SU 4S jU4S jS9  T R_                  5         T R`                  Rb                  nT R8                  (       dY  SSK2J3s  Jn  URi                  SS9   [         Rj                  Rm                  UR6                  S9nSSS5        WT R`                  l1        T Ro                  5          T Rq                  T5      nSSS5        SSK9J:n  [        WU5      (       d,  [        [w        USS5      U5      (       a]  URx                  S:X  aM  [        UU5      (       a  UOURz                  nUR}                  U5        [        UU5      (       d  UR~                  nU" U5      n[>        S   S==   S-  ss'   T R                  Xo5        [        U5      nUR                  U5        UR                  5       sSSS5        $ ! , (       d  f       GN<= f! , (       d  f       GN= f! , (       d  f       g= f)zZ
Generate code from self.graph and return the Instruction()s to
call that generated code.
r   rV  __compiled_fnr  c              3   @   #    U  H  oR                  5       v   M     g 7fr   )as_proxyrr  s     rz   r\  8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>U  s     5OBqjjllBr^  )r   statscalls_capturedr0  r  %sT)include_strideinclude_devicecoloreddynamo_output_graphc                  (   > ST R                  5       0$ )Nsizes)r  r   s   rz   ro  7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    $"A"A"CDry   c                  &   > T R                  SSSS9$ )NFT)print_outputr  r  )print_readable)r   s   rz   ro  r    s    2#4#4!&tD $5 $ry   )r  r   NFr   )r   )_LazyGraphModule__self___lazy_forwardunique_graphs)Dr   _guardsr   clear_framerw  rW  r$  r  r(   r@  r   r   r  r  
create_argr  r   replace_all_uses_withr  _maybe_preserve_original_metar   do_not_emit_runtime_assertsremove_unused_get_attr_nodesr   r
   r   rg   r   r   remove_unused_graphargsrJ   rK   &remove_tensorify_specialized_graphargsr  r   r   r  ry  r0  r   r[  r  graph_code_logrz  rS   r  r  r  r  r;  r  r  r  r  r  r  call_user_compilertorch.fx._lazy_graph_moduler  r  rr   r  force_recompiler   install_global_unsafer*   make_call_generated_codery  )r   r  rvr  replaced_outputsrW  rJ  output_nodeold_nodenew_nodencallsregister_finalizerold_fake_moder:  backend_fake_modecompiled_fnr  lazy_gmrn  r   s   `                  @rz   r  %OutputGraph.compile_and_call_fx_graphA  s   
 ]]))557+####((,_-Db$''''dN3333**$$//5OB5O0OPR	K '7&<&<&>"..x8 '? II$$BB2S55 113/NN44NN;;	 ((* ,FW./69/779 !!'')#D$**5B&*&A&A""2& 'B *.)E)EB&55::< GG67 ,0+A+ABGG'(  &"T$PT NN++%D ,  ##% 00::M;;99]]E]R(-(9(9(H(H"/"9"9 )I )% S 2C$$.**,"55b9 - E+'7887;
DACSTT((O; "+/?@@  $--  !009!+/?@@")//K!+.KWo.!3.&&t92B''-&&(q 87X SR -,m 87s=   KQ=<(Q$)Q=Q+C0Q=
Q(	#Q=+
Q:	5Q==
Rc                 4    U R                   R                  SS9$ )Nplaceholderop)rg   
find_nodesr   s    rz   placeholdersOutputGraph.placeholders  s    zz$$$66ry   c                 ^    U R                    Vs/ s H  oR                  S   PM     sn$ s  snf NrV  )r&  r[  r   rZ  s     rz   rB  OutputGraph.graphargs  s)    262C2CD2C$		*%2CDDDs   *r   c                 r    [        SSSSS9   U R                  U5      sS S S 5        $ ! , (       d  f       g = f)NOutputGraph.call_user_compilerbackend_compileT'aot_autograd_cumulative_compile_time_us)
phase_namer  dynamo_compile_column_us)rL   _call_user_compiler)r   r   s     rz   r  OutputGraph.call_user_compiler  s6    ,("&%N	
 ++B/
 
 
s   (
6c                 $   U R                   c   eSn/ nUR                  R                   H;  nUR                  S;   a  US-  nUR                  S:X  d  M*  UR	                  U5        M=     [        U5        U H#  nUR                  S   nUR                  Ul        M%     U R                  Ul
        U R                  Ul        [        U R                   S5      (       a  U R                   R                  OSn [        5       " [         R"                  SU 35        U R                   n[$        R&                  (       a  [)        U5      nU" XR+                  5       5      n	[        5       " [         R"                  S	U 35        [-        U	5      (       d   S
5       e [O        SS0 U RP                  EU[S        UR                  R                  5      [S        U5      S.E5        W	$ ! [.        [0        4 a    e [2         a  n
U R4                  (       aD  [7        U R                   U
[8        R:                  " 5       5      R=                  U
R>                  5      S e[A        U
U RB                  RD                  SSU S[G        U
5       SU RB                  RI                  5        3SU S[G        U
5       S3S/S9   S n
A
GNS n
A
f[J         a  n
U
eS n
A
f[L         aI  n
[7        U R                   U
[8        R:                  " 5       5      R=                  U
R>                  5      S eS n
A
ff = f)Nr   rv  call_methodcall_moduler   r"  rV  rr   z<unknown compiler_fn>zcalling compiler function zdone compiler function z#compiler_fn did not return callablezBackend compiler exceptionz	Backend: z
Exception:z
Traceback:
zBackend compiler `z` failed with z. Adding a graph break.z-Report an issue to the backend compiler repo.rO  dynamor-  )op_count
node_countinput_count)*r   rg   r  r$  r   rR   r[  rp   _dynamo_sourcer  _param_name_to_sourcer!  _source_to_user_stacksr/  rr   r   r    INFOr   r   r   r   callabler   r   r-   r,  r,   inspectcurrentframewith_traceback__traceback__r0   r   r9  r   format_frame_summaryr.   r   r   r   rG  )r   r   totr&  rZ  plrN  rJ  r   r  es              rz   r2  OutputGraph._call_user_compiler  s   +++HHNNDwwIIqww-'##D)	 #
 	3B''*%C !$

B	  $(#<#< $($>$>! t''44 %%( 	
 	8N7<<+EdV)LM**K((,[9%b*=*=*?@KN7<<+B4&)IJK((O*OO(6 	,..!"((..1"<0			
 K /0@A 	0 	55+$$a)=)=)? .1t< *##4#D6c!fX^DLLLmLmLoKpq0nSVHLcdC	 	  	 G 	8'  !W%9%9%;nQ__-48	8s3   3BG L2B2J**L7J99LAL

Lc                 ~    [         R                  R                  R                  (       a  [	        U 5      $ [        5       $ r   )r   _dynamor   use_graph_deduplicationr1   r   r   s    rz   r  OutputGraph.dedup_pass  s'    ==77,T226Mry   c                 h    [        XR                  SS9nX2l        SUl        U R	                  X#S S9  U$ )NT)requires_suffixFrU  )rP   r   rr   torchdynamo_force_dynamicr2  )r   rJ  sub_gm	next_names       rz   install_subgraphOutputGraph.install_subgraph  s:    'ootT	#+0( 	$$Vt$Dry   c                 Z    U R                    Vs/ s H  oR                  PM     nnU$ s  snf r   )rB  example)r   rN  r   s      rz   r   OutputGraph.example_inputs(  s&    )-8#++8 9s   (c                     [        U R                  R                  SS9SS9 H8  n[        [	        UR
                  5      5      S:X  d  M'  U R                  U5        M:     g )Nr  r#  T)reverser   )sortedrg   r%  rG  r   usersr  r*  s     rz   r  (OutputGraph.remove_unused_get_attr_nodes,  sJ    4::00J0?ND4

#$)  & Ory   c                   ^ ^^^ T R                   (       d   eS[        R                  R                  4S jnS[        R                  R                  4S jmU4S jnSSKJn  [        [        T R                  R                  5      5       H  n[        [        UR                  5      5      S:X  d  M'  UR                  S:X  d  UR                  S	:X  a  UR                  [        R                  L dc  UR                  S	:X  a7  UR                  [         R"                  L a  U" UR$                  S   5      (       d  U" U5      (       d  U" U5      (       d  M  T R'                  U5        M     S
 nU 4S jn[)        5       mS[*        [         R,                  [         R.                  4   4S jm/ nT R0                   GH  nU" U5      S LnU(       a&  UR                  (       d  UR3                  U5        M9  M;  UR                  (       d,  [5        UR6                  S   [8        5      (       d
  U" U5        Mx  UR6                  S   n	[5        U	[8        5      (       a  M  [5        UR6                  S   R:                  [         R<                  5      (       a  UR6                  S   R:                  n
UR6                  S   R>                  n[         R@                  RB                  RE                  U
5      (       d  [G        U
RI                  5       5      nURK                  5        HT  n[M        URN                  U5      n[P        RR                  " [         R,                  [         R.                  4UU4S jU5        MV     GM  U	RT                  b  U	RT                  OU	R:                  nT" TU5        GM     U H1  nU" U5      nUc  M  UT;  a
  U" U5        M   TRW                  U5        M3     g )Nb_nodec                    U SL a  g[        U [        R                  5      (       d  gU R                  R	                  S5      nUc  gUSL a  g[        U[
        R                  5      (       a  UR                  R                  5       =nb  U$ g)NTFr]  )	r@  r
   Noder[  r{  r   SymBoolrZ  maybe_as_bool)r^  brs      rz   is_static_true;OutputGraph.remove_unused_graphargs.<locals>.is_static_true>  sw    ~fbgg..0AyDy1emm,,&&..00Q= ry   ac                     SSK Jn  [        U [        [        [
        45      (       a  g[        U [        R                  5      (       a%  [        U R                  R                  S5      U5      $ g)Nr   SymTypesTr]  F)
torch.fx.experimental.sym_noderj  r@  rv   rp  r   r
   r`  r[  r{  )rg  rj  s     rz   is_symnode_arg;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_argQ  sJ    ?!c5$/00!RWW%%!!&&**_"=xHHry   c                 :  > SSK Jn  U R                  S:w  a  g[        U R                  R                  S5      U5      (       d  g[        U4S jU R                   5       5      (       d  g[        U4S jU R                  R                  5        5       5      (       d  gg)	Nr   ri  rv  Fr]  c              3   4   >#    U  H  nT" U5      v   M     g 7fr   rq   rZ  rg  rl  s     rz   r\  WOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>h  s     <)Q~a(()   c              3   4   >#    U  H  nT" U5      v   M     g 7fr   rq   rp  s     rz   r\  rq  j  s     G2FQ~a((2Frr  T)
rk  rj  r$  r@  r[  r{  r{  r   r   r=  )rZ  rj  rl  s     rz   is_symnode_compute_nodeDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node]  sp    ?ww/)diimmO<hGG <$))<<<G$++2D2D2FGGGry   r   )is_accessor_noder  rv  c                 
   U R                   S   nUR                  n[        U[        R                  5      (       aI  [        UR
                  R                  [        R                  5      (       a  UR
                  R                  $ g r)  )	r[  rV  r@  r   r-  rZ  exprsympySymbol)rZ  rN  rV  s      rz   placeholder_binds_symbolEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  s^    ))J'CkkG'5<<00Z!!5<<6 6 ||(((ry   c                    > [         R                  SU R                  S   R                  R	                  5       5        U R                  S	 TR                  U 5        TR                  R                  U S 5        g )NzREMOVE UNUSED GRAPHARG %srV  )r   rz  r[  rp   rJ  r  r  r  )rZ  r   s    rz   remove_unused:OutputGraph.remove_unused_graphargs.<locals>.remove_unused  s\    II1499Z3H3O3O3T3T3VW 		*%T"!!%%dD1ry   fakec                      U [        U5      -  n g r   )r   )used_symbolsr  s     rz   update_used_symbols@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols  s    L..Lry   rV  c                    > T" TU 5      $ r   rq   )tr  r  s    rz   ro  5OutputGraph.remove_unused_graphargs.<locals>.<lambda>  s    .A,PQ.Rry   ),r$  r
   rZ  Argument%torch.fx.experimental.symbolic_shapesrv  r  r   rg   r  rG  r[  r$  r  operatorgetitemr   _checkr   r  r  r	   r-  r   r&  r   r@  r[  rY   rV  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__r  r  wrapped_objpytreetree_map_onlyfake_tensorremove)r   re  rt  rv  rZ  r{  r~  recheck_placeholdersbinds_symbolrN  real_script_objfake_script_obj	flat_dictattrfake_attr_valr  symbolrl  r  r  s   `                @@@rz   r  #OutputGraph.remove_unused_graphargs1  s    	277#3#3 	&	bgg.. 		" 	KT$**"2"234D4

#$)GGz)?2t{{hFVFV7V?2 KK5<<7*499Q<88.t44'--$$T* 5		2 +.%	/E%,,:T4U 	/  "%%D3D9ELzz(//5 " zz*IIj)+@+ + "$' ))J/C!#'<== !$))J"7"?"?ASASTT*.))J*?*G*G*.))J*?*R*R$~~AASS+    )-_-L-L-N(OI(1(807$3$?$?1" !' 4 4%*\\5<<$@$R$1!"	 )9 !+.??+FCKK  (d;I &N )D-d3F!-!$' !''/ )ry   c                    SSK Jn  U R                  R                   GH;  nUR                  R                  S5      n[        U[        5      (       d  M6  UR                  c  ME  [        UR                  R                  R                  S5      (       d  Mv  [        S UR                   5       5      (       d  M  UR                  UR                  R                  R                  R                  5      (       d  M  [!        UR                  5       H8  nUR#                  [%        UR                  5      5        U R'                  U5        M:     U R'                  U5        GM>     g )Nr   )TensorifyStater]  rJ  c              3   >   #    U  H  oR                   S :H  v   M     g7f)itemN)r  )rZ  us     rz   r\  EOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>  s     ?JqF*Js   )torch._dynamo.symbolic_convertr  rg   r  r[  r{  r@  r   	item_memor/  rZ  _exprr{  r[  should_specializerJ  r   r	  r   r  )r   r  rZ  r]  r  s        rz   r  2OutputGraph.remove_tensorify_specialized_graphargs  s     	BJJ$$D IIMM/:M=*55!++7M3388>>GG?DJJ???"44!++0066;; 
 djj)A++L9P9P,QR$$Q' *   & %ry   rI  c                 H    U R                   R                  U5        SU l        g)z\
We call this on the creation of a new compiled subgraph that is inserted
before user code.
TN)r  rx  r$  )r   rI  s     rz   rf  #OutputGraph.add_output_instructions  s     
 	  ''/ry   c                     XR                   ;  d   eU R                   R                  U5        U R                  R                  [        R
                  " U R                  X5      5        g)a0  
WARNING: prefer the safer `install_global_by_id/install_global`.
torch.compile instances should be independent of each other;
one footgun is to have one instance depend on the existence of
a global installed by another instance. This can happen if we mangle
a global the same way across both instances.
N)r  r   r#  r   rH   ru  r   )r   rJ  r   s      rz   r  !OutputGraph.install_global_unsafe  sO     11111""4([//0A0A4OPry   c                     U S[        U5       SU R                   3nX0R                  ;   a  U$ U R                  X25        U$ )z
Installs a global if it hasn't been installed already.
This is determined by (prefix, id(value)) pair.

Returns the name of the newly installed global.
r  _c)r   r  r  r  r   rI  r   rJ  s       rz   install_global_by_id OutputGraph.install_global_by_id  sG     2e9+R'89)))K""4/ry   c                 >    [        U5      nU R                  X25        U$ )zf
Installs a global, generating a unique name for it.

Returns the name of the newly installed global.
)r(   r  r  s       rz   rB  OutputGraph.install_global  s!      ""4/ry   c                    S U l         U R                  R                  5         S U l        U R                  R
                   H"  nSUR                  ;   d  M  UR                  S	 M$     U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                   R                  5         g r)  )r   r   r   r  rg   r  r[  r  r  r   r  r  r0  r  r   r%  r3  r*  s     rz   cleanupOutputGraph.cleanup  s    $(!JJ$$DTYY&IIj) % 	##%  &&(!##))+##))+--335""$  &&(  &&(!!#ry   enabledc                     Xl         g r   )r(  )r   r  s     rz   r  $OutputGraph.set_torch_function_state+  s    &-#ry   r  c                 :    U R                   R                  U5        g r   )r  r   )r   r  s     rz   add_graph_finalizerOutputGraph.add_graph_finalizer.  s     	##**+=>ry   rZ  c                     UR                   S:X  a  UR                  S   R                  $ UR                   S:X  d   eU R                  UR                     $ )z#Extract the non-fake example tensorr"  rV  r  )r$  r[  rV  r   r  r*  s     rz   example_value_from_input_node)OutputGraph.example_value_from_input_node3  sH    77m#99Z(000ww*$$$t{{++ry   )1r"  r3  r4  r5  r  r#  r   r   r  ry  r   r.  r  r0  r   r   r  r   r8  r,  r  r   r  r   r7  r-  r  r  rx  r2  r   r1  r	  r  r   r$  r   r!  r  r(  r*  r+  r   r  r   r  r  r%  r  )rE  r   )tmpFN)r   N)frr   rs   rt   ru   r   r7   rw   r   r   r   r   r#   r   Scoper   r6  rX   rK  rH  r  r  r   r  r  propertyrW  r  r  rg   setterr  r  r  r  r  r  
contextlibcontextmanagerr  r  r;  r   r   r  	GuardsSetr`  r   r/  r  r  r  rJ   r  r  r\  r  staticmethodr  r
   Proxyr  r	   r   r   r   r2  rS  r   r  r  r  r  r  r  r  r  r   r`  r&  rZ   rB  r   r"   r  r2  r  rS  r   r  r  r  r'   rf  r  r  rB  r  r  r  r  rx   r   r   s   @rz   r   r     s    t238nt2 j)t2
 t2 t2 t2lD&5O 5)*.0&8BG#4 &#
      & ) ) \\* * 7 7 4 4 1 1A@@  .   . . 8 8 A// A A >DcN > >,
\$& N N'05
  		,/			Hehhoou||S89HTT/n QU\19:L1M\|501<C 2  "70})~ 7d277m 7 7 E4> E E0R^^ 0
 0Fbnn F FPU\\ 2 '
S0j 'D d;.?  D  
QS 	s 	$*. . .?"*BNN+;T+A"B?	?
,%((-- , ,ry   r   a  With the current config, we will graph break (and fall back to eager-mode PyTorch) on all ops that have do not have the 'pt2_compliant_tag'. Please see the following doc for how to mark this op as PT2 compliant https://pytorch.org/tutorials/advanced/custom_ops_landing_page.htmlc                   ^  US:w  a  g U 4S jnU 4S jn[        U[        R                  R                  5      (       a?  [        R                  R
                  UR                  ;   a	  U" U5        g U" USU S35        g [        U[        R                  R                  5      (       Ga*  [        UR                  5       5      n[        U5      S:X  aM  [        X'S   5      n[        R                  R
                  UR                  ;   a	  U" U5        g U" USU S	35        g [        R                  R                  R                  T R                  X44S
5      u  p4 [        R                   R"                  " UR$                  /UQ70 UD6n	[        UW	5      n[        R                  R
                  UR                  ;   a	  U" U5        g U" USU SU	 S35        g g ! [&         a  n
[)        SS[+        U
5      / S9   S n
A
NsS n
A
ff = f)Nrv  c                 ^   > U R                   S;   a  g TR                  R                  U 5        g )N>   atenprimprims)	namespacer.  r   )r  output_graphs    rz   encountered_compliant_op8check_pt2_compliant_op.<locals>.encountered_compliant_opH  s)    88))--f5ry   c                    > TR                   R                  U 5        [        R                  (       a  [	        SSUS-   [
        -   / S9  g g )Nz Encountered non-PT2-compliant oprN   rO  )r-  r   r   only_allow_pt2_compliant_opsr/   err_epilogue)r  msgr  s     rz   encountered_non_compliant_op<check_pt2_compliant_op.<locals>.encountered_non_compliant_opM  sB    &&**62..:#I4	 /ry   z%Encountered the torch.ops.OpOverload z that is not PT2 compliant.r   r   z:Encountered the non-overloaded torch.ops.OpOverloadPacket z that is not PT2 compliant. Fz*Error when attempting to resolve op packetrN  rO  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r@  r   _ops
OpOverloadTagpt2_compliant_tagtagsOpOverloadPacketr  	overloadsrG  r  rK  utilsget_fake_values_from_nodesr  r&  _jit_resolve_packet_qualified_op_namer   r/   r   )r  kindr  r   r   r  r  r  r$  overloadrH  s   `          rz   check_pt2_compliant_opr  D  s   6
 &%**//0099&&&++5$V,$3F8;VW	
 	&%**5566&**,-	 y>Q1.Byy**bgg5(,(..4X 6./ }}**EE##d^U

	xx33)),04:H VX&99&&"''1$R((=fX F33;* =%&I 70  	DF		s   .G 
G;G66G;c                        \ rS rSrS rS rSrg)	LazyProxyi  c                 4    Xl         X l        X0l        X@l        g r   )r  r   r   r   )r   r  r   r   r   s        rz   r   LazyProxy.__init__  s    	ry   c                 N    U R                   " U R                  0 U R                  D6$ r   )r   r   r   r   s    rz   r   LazyProxy.__call__  s    ww		1T[[11ry   )r   r   r   r  N)rr   rs   rt   ru   r   r   rx   rq   ry   rz   r  r    s    2ry   r  c                   F  ^  \ rS rSrSrSU 4S jjrS r   SU 4S jjr SU 4S jjrS r	 SS jr
S	 rS
 rS\\\R                   R"                  4   4S jrS\\R&                  \R(                  4   S\\   4S jrS\R&                  S\\R4                     4S jrSrU =r$ )r   i  z
Holds an FX graph that is being traced. OutputGraph owns a SubgraphTracer
and the separation of responsibilities is that SubgraphTracer is
responsible for building the graph while OutputGraph is responsible for
compiling and executing the graph.
c                 :  > [         TU ]  5         [        R                  " U5      U l        [
        R                  R                  5       U l        X0l	        0 U l
        0 U l        X l        X@l        0 U l        0 U l        S U l        SU l        SU l        SU l        Ub  UR(                  S-   OSU l        S U l        S U l        S U l        S U l        U R                  c  / U l        g U R                  R2                  U R                  R5                  U5      U4/-   U l        g )NFr   r   )r   r   weakrefr  r  r   r
   Graphrg   r   r  r  r  r  lifted_freevarsr  	prev_instunder_activation_checkpoint#allow_side_effects_under_checkpointis_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str)r   r  r  r   r  r   s        rz   r   SubgraphTracer.__init__  s   #MM,7XX^^%
 # 9; =? *  " TV ,1(
 490 ,1(:@:L 2 2Q 6RS!#' $(!
 ;;#%D #';;#>#>**=9=IB $D ry   c                    U R                   (       a  U R                  (       a  U R                  (       a  UR                  R                  nS nUb(  U R                  R                  X0R                  -
  S 5      nUbb  U R                   U   n[        R                  R                   H  nXe;   d  M
  XV   UR                  U'   M     SU;   a  US   UR                  S'   g g g g g g )Nstack_trace)
r  r  r  current_instructionstarts_liner{  r
   r  _COPY_META_FIELDSr[  )r   r  rZ  linenonode_idxr[  fields          rz   r
  ,SubgraphTracer._maybe_preserve_original_meta  s    (())++77FH!3377666 #))(3XX77E}+/;		%( 8 !D(/3M/BDIIm, ) $ * ) ry   c           	      J  >^^^^^ U R                   b^  [        R                  " X445      u  p/ n
U H%  nU R                  U5      nU
R	                  U5        M'     [        R
                  " X5      u  p4[        TU ]  UTX4XVU5      mU R                  R                  n[        R                  S:  a  US;   a  UR                  mTU R                  La  TR                  bu  TR                  R                  b^  UR                   mUR#                  TR                  R                  S9mUUUU4S jn[$        R'                  S[)        U5      5        TU l        SnUR                   U R*                  La  [,        R.                  " UR                   5      R1                  SS 5      " 5       n[3        U[4        R6                  R8                  5      (       ao  S	nUR:                  R<                   Vs/ s H  nUR>                  PM     snU l         URB                  U l"        URF                  RH                  RJ                  U l&        OS U l         S U l"        S U l&        URN                  nU(       a'  URQ                  5       TRR                  R>                  S
'   US;   a>  U RT                  TRR                  RV                  T4/-   TRR                  R>                  S'   OUS:X  a  U R                   b  [Y        SSU RZ                   3S/ S9  U RT                  TRR                  RV                  []        U4S jTRR                  R>                  S
   R_                  5        5       5      4/-   TRR                  R>                  S'   U Ra                  UTRR                  5        U(       Gd+  S
TRR                  R>                  ;  a:  URN                  nU(       a'  URQ                  5       TRR                  R>                  S
'   STRR                  R>                  ;  a  US;   a>  U RT                  TRR                  RV                  T4/-   TRR                  R>                  S'   OyUS:X  as  U R                   b  [Y        SSS/ S9  U RT                  TRR                  RV                  TRR                  R>                  S
   T   S   4/-   TRR                  R>                  S'   STRR                  R>                  ;  a  / nU(       aJ  URc                  5       (       d  UR	                  URe                  5       5        [g        USS 5      nU(       a  MJ  URi                  5         [j        Rl                  Ro                  U5      Rq                  5       nSRs                  U5      TRR                  l:        [4        Rv                  Rx                  Rz                  (       d)  [4        Rv                  Rx                  R|                  (       aD  U R                  R~                  R                  U R                  R                  TRR                  5        T$ s  snf )Nr_  r5  )r  c                  v   > [        TT5      R                  5       n STR                  R                   ST SU  3$ )NzTRACE FX call z from r  )rM   rstriprZ  rJ  )linecur_instheaderr  tx_codes    rz   get_trace_call_log_str;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strF  s9    5gxHOOQD+BGGLL>xr$PPry   r  Forig_graphmodulec                      g r   rq   rq   ry   rz   ro  -SubgraphTracer.create_proxy.<locals>.<lambda>Q  s    Dry   Tnn_module_stack>   r6  rv  r  r7  z4Invoking an nn.Module inside a higher order operatorzHigher order op name: zThis is not supported.rO  c              3   f   >#    U  H&  u  nu  p#UR                  S 5      S   T:X  d  M"  Uv   M(     g7f)@r   N)r  )rZ  r   r  tyr  s       rz   r\  .SubgraphTracer.create_proxy.<locals>.<genexpr>u  s4      *QJAw773<?f4 *Qs   !1	1z2Invoking an nn.Module inside a HigherOrderOperatorrN  r   r  r  )Ar  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r  r  r  r~  r  r  r  	positionsr  r9  get_line_of_code_headertrace_call_logrz  rT   r  r)   get_contextr{  r@  r   r
   r   rg   r  r[  r  _lineno_mapr  r   __code__r   r  r  r   rZ  r  rJ  r/   r  r  r   r
  is_co_filename_from_nn_modulesframe_summaryr  rY  r   StackSummary	from_listformatr  r  rK  r   rL  track_nodes_for_deduplicationr	  
track_node)r   r  r  r   r   rJ  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsrN  maybe_new_argr  r  is_retracingorig_graphmodule_maybendr  frame_summariesmsgsr  r  r  r  r   s     `                  @@@@rz   r  SubgraphTracer.create_proxy  s%   V ;;"#)#6#6~#F IM  $ H H M$$]3 ! "00JLDW!&$9I

 )) w&4 4
 ,

 --H.&&2&&--9))338;M;M;T;T3UQ Q $$T:6L+MN!) 99DNN*%1%=%=bii%H%L%L"L& &" 0%((2F2FGG#&<&B&B&H&H&&HBGG&H&" ,B+M+M(*22;;JJ ) &*"+/(,0),,.=.B.B.DBGGLL*+33.2.B.Bv&F /BGGLL*+ ]"{{& R4T5G5G4HI 8	 /3.B.BGGLL *,'',,7H*I*O*O*Q 	F 	/BGGLL*+ 	**2rww7 4"$"4"4"6E6J6J6LBGGLL!23 4;;6:6J6Jv.N 7BGGLL!23 ]*{{.($X$&(@"$	 7;6J6JGGLLGGLL):;FCAFN 7BGGLL!23 ,<>O 88::#**2+;+;+=>R40 " ##% ))33ODKKMD"$''$-BGG MM  88}}##AA,,77!!,,bgg 	y&s   &X c                   > [        U R                  XX45        U R                  bk  [        R                  " U0 UD6nU HO  n[        U[        R                  R                  5      (       d  M.  UR                  U R                  :X  a  MJ   S5       e   [        T
U ]-  XX4XV5      n	U R                  R                  U	R                  S'   U	$ )Nz2create_node using arg not from this SubgraphTracerr  )r  r  r  r  arg_tree_leavesr@  r   r
   r`  rg   r   r  r  r[  )r   r$  r  r   r   rJ  r,  r.  rN  rZ  r   s             rz   r  SubgraphTracer.create_node  s     	t00"dK;;"..??I !#uxx}}55yyDJJ. H. ! w"2tTM*.*;*;*E*E		&'ry   c                    [        UR                  5      S:  a  / nUR                  R                  5        HV  nUR                  U R                  :w  d  M  UR	                  [        [        UR                  R                  5      5      5        MX     U H  nUR                  R                  U5        M      U R                  R                  U5        U R                  R                  UR                  S 5        g r  )rG  r[  r  rg   rx  r  r   r  r  r  r  rJ  )r   rZ  user_graph_nodesuserother_graph_nodes        rz   r  SubgraphTracer.remove_node  s    tzz?Q46

) ::+ %++HT$**:J:J5K,LM * %5  &&112BC %5

d#  $$TYY5ry   c           	      
   [         R                  SUUb  UR                  5       OSUU R                  U5        Uc  U R                  c   SU SU S35       eU R
                  (       ad  U R                  cW  [        USS9(       dH  U R                  R                  R                  U/ 5      R                  [        R                  " 5       5        [        XR                  5      nU R                  (       av  [        [!        U R                  5      5      nU R                  U   R"                  nU(       a  U R$                  R'                  U5      nO7U R$                  R)                  U5      nOU R$                  R'                  S 5      nU   U R+                  SUS	0 US
9n	[-        U	R"                  U5        U R                  (       a@  U(       a9  U R                  R/                  5       u  pXR                  U'   XR                  U
'   OXR                  U'   U R
                  n[0        R2                  R5                  5       nU(       d7  U(       d0  [7        U[0        R8                  5      (       a  U R;                  X55        [7        U[0        R<                  5      (       aU  [7        UR"                  R>                  [@        RB                  5      (       a"  XRD                  UR"                  R>                  '   U	sS S S 5        $ ! , (       d  f       g = f)Nz7create_graph_input %s %s %s at debug_level %s before=%sz(none)z0you are required to provide a source for inputs z example_val z on the root tracerT)only_allow_inputr"  rq   r,  )#r   rz  rJ  r  r  r   r>   r  r!  
setdefaultr   r   extract_stackrP   r  r  r  rZ  rg   inserting_beforeinserting_afterr  rW   popitemr   compileris_compilingr@  r   _lift_basic_symbolsr-  rx  ry  rz  r  )r   rJ  r,  r]  beforerp   	prev_namerZ  ctxr  r   r   is_strict_exportis_non_strict_exports                 rz   rX  !SubgraphTracer.create_graph_input  sV    			E#/FKKMX	
 >;;* B4&VcUddwx* >>dkk1'F!!77BB62NUU"002 #4)A)AB##Xd&>&>?@I++I6;;Djj11$7jj006**--d3C%%mT2rY%WEejj-8''F//77916((../((+16((.2  $~~#(>>#>#>#@ $,}ell;;((? -66:""''< < ?D""=#5#5#:#:;_ SSs   E&K44
Lc                    U R                   c   S5       eUR                  R                  S   n[        U[        R
                  5      (       aG  UR                  R                  U R                  ;   a#  U R                  UR                  R                     $ XR                  ;   a  U R                  U   $ UR                  U R                   :w  a  U R                   R                  U5        UR                  R                  S   nU R                  UR                  R                  [        U5      U5      nX0R                  U'   U$ )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr]  )r  rZ  r[  r@  r   r-  rx  r  r  r  lift_tracked_freevar_to_inputrX  rJ  rY  )r   r  r]  	new_proxys       rz   rR  ,SubgraphTracer.lift_tracked_freevar_to_input7	  s    {{& 	
W	
& 

8 }ell33""''4+=+==%%m&8&8&=&=>>
 (((''..
 <<4;;&KK55e<

8++JJOOT-0-
	 '0U#ry   c                 0  ^  [        U[        R                  R                  5      (       dJ  [        U[        5      (       a3  [	        U 4S jUR
                  UR                  UR                  4 5       6 $ U$ UR                  T :X  a  U$ T R                  U5      $ )z
If arg is a free variable, then lift it to be an input.
Returns the new lifted arg (if arg was a freevar), else the
original arg.
c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   )r  )rZ  sub_argr   s     rz   r\  ESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>l	  s&      'FG @@II'Fs   !)
r@  r   r
   r  slicestartstopstepr  rR  )r   rN  s   ` rz   r  2SubgraphTracer.maybe_lift_tracked_freevar_to_input_	  s     #uxx~~..
 #u%%(+		388SXX'F  
ZZ4J11#66ry   e_proxyc                 b  ^ ^ UR                   m[        T[        5      (       d   eS[        4U 4S jjnU4S jn[        U[        R
                  5      (       Ga  [        UR                  5       5       H  u  pVU" U5      (       d  M  [        R                  SUUUTR                  5        [        TUUS[        R                  R                  R                  R                  X%40 [!        U5      S9nT R#                  Xg5        M     UR$                  [        R&                  L a  [        UR)                  5       5       H  u  pVU" U5      (       d  M  [        R                  SUUUTR                  5        [        TUUS[        R                  R                  R*                  R                  X%40 [!        U5      S9nT R#                  Xg5        M     GO;UR$                  [        R,                  L aA  T R#                  UR/                  5       U5        T R#                  UR1                  5       U5        OUR$                  [        R2                  [        R4                  1;   aA  T R#                  UR7                  5       U5        T R#                  UR9                  5       U5        OnUR$                  [        R:                  [        R<                  1;   a@  T R#                  UR?                  5       U5        T R#                  URA                  5       U5        [C        U5      (       aB  URE                  5       u  pU H)  n
[G        X5      nT R#                  U[G        X*5      5        M+     g g [        U[        RH                  5      (       a4  U" U5      (       a&  URJ                  RL                  nUTRN                  U'   g g g )Nr   c                 ^  > SSK Jn  U" U 5      =(       a    [        U R                  R                  [
        R                  5      =(       ac    U R                  R                  R                  U R                  R                  5      =(       a#    U R                  R                  TR                  ;  $ )Nr   )r   )
r  r   r@  rZ  rx  ry  rz  r   is_unbacked_symintr  )r  r   r   s     rz   	need_bind8SubgraphTracer.track_unbacked_symbols.<locals>.need_bind	  sq    I A :qvv{{ELL9:FF$$77D: FFKKt'9'99	ry   c                 X   > TR                   " U0 UD6n[        UR                  U 5        U$ r   )r  rW   rZ  )r]  r   r   r  r  s       rz   _proxy_with_example_valueHSubgraphTracer.track_unbacked_symbols.<locals>._proxy_with_example_value	  s+    ''88Eejj-8Lry   z>_track_unbacked_symbols %s for %s.size()[%s] at debug_level %srv  rB  z@_track_unbacked_symbols %s for %s.stride()[%s] at debug_level %s)(r  r@  r   r   r   r   	enumerater  r   rz  r  r  opsr  sym_sizerv   rY  track_unbacked_symbolslayoutstridedstride
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr   __tensor_flatten__r  r-  rZ  rx  r  )r   r]  r^  rb  re  ir  
lazy_proxyattrsrM  r  inner_trx  r  s   `            @rz   rj  %SubgraphTracer.track_unbacked_symbols~	  s     &.1111	D 		
 mU\\22!-"4"4"67Q<<IIX** "+1'		//33 "&q'	"J //>' 8* ##u}}4%m&:&:&<=DA ||		^#".. &/"5+!IINN5599$L&*1g	&
 33AB' >* %%)9)99++M,B,B,DgN++M,A,A,CWM%%%*:*:E<L<L)MM++M,F,F,H'R++M,E,E,GQ%%%*:*:E<L<L)MM++M,F,F,H'R++M,E,E,GQ,];;*==?
!D%m:G//9OP " <
 u||44''$))..-4$$T* ( 5ry   r]  srcc           	        ^   SS[         [        [        R                  4   S[        [
           S[        SS 4U 4S jjjn[        U[        R                  5      (       Gat  [        UR                  5       5       H+  u  pEU" UUb  [        U[        R                  U5      OS SS9  M-     UR                  [        R                  L a}  [        UR!                  5       5       H+  u  pEU" UUb  [        U[        R"                  U5      OS SS9  M-     U" UR%                  5       Ub  [        U[        R&                  5      OS SS9  GO;UR                  [        R(                  L aA  T R+                  UR-                  5       U5        T R+                  UR/                  5       U5        OUR                  [        R0                  [        R2                  1;   aA  T R+                  UR5                  5       U5        T R+                  UR7                  5       U5        OnUR                  [        R8                  [        R:                  1;   a@  T R+                  UR=                  5       U5        T R+                  UR?                  5       U5        [A        U5      (       aF  URC                  5       u  pgU H-  n[E        X5      n	T R+                  Xb  [G        X(5      OS 5        M/     g g [        U[        R                  5      (       a
  U" UU5        g g )	Nr  rp   rK  r   c           	        > [        U 5      (       d  g [        U [        R                  5      (       d   eTR	                  U 5      n[        U5      S:X  a  g TR                  b  TR                  R                  X5        U H  nTR                  R                  U   nUR                  R                  S   n[        U[        R                  5      (       d   eTR                  [        U5      [        U5      UUUS9n[        R                  SUUb  UR!                  5       OSTR"                  5        UTR$                  U'   M     g [        U5      S:X  d   SU SU  35       eUc   S	U  S
U  S35       e['        [)        U5      5      nTR                  [        U5      [        U 5      U UUS9n[        R                  SU Ub  UR!                  5       OSTR"                  5        [+        UU SS SS9UR                  R                  S'   g )Nr   r]  )rK  rp   z4_lift_symbols_in_symint %s from %s at debug_level %szsubgraph inputsr   zyFor root tracer, we only expect to bind basic symbols (compound symbols should be cached before) but got unbound symbols z in zSource of 'z' is None when lifting it to input of top-level. If it's an unbacked symbol, this could be because it's not tracked with lazy_bind_unbacked_symbols. Otherwise, should provide a source when create_graph_input for `z` at root tracer.F)pass_arg_as_tensorr  	is_tensorrV  )r   r@  r   r-  lookup_unbound_symbolsrG  r  rJ  r  rZ  r[  rX  r   rY  r   rz  rJ  r  r  r  iterrZ   )	r  rp   rK  self_to_be_bounds0parent_proxyexample_valphr   s	           rz   _lift_symbols_in_symintCSubgraphTracer._lift_basic_symbols.<locals>._lift_symbols_in_symint	  s   
 q>>a....#::1=#$) {{&//:*B#';;#<#<R#@L"."3"3"8"8"IK%k5<<@@@@00B[)#%% 1 B IIN)/);AR((	 :<D((6# +( +,1 HHXGYY]^_]`b1 ) !! %WWXVYYjl)
 $/01,,GG!! -  		J%+%7FKKM=N$$	 ,4', $#,Z(ry   T)rK  )F)$r	   rv   r   r-  r   r   r   r@  r   rg  r  rE   rD   SIZErk  rl  rm  STRIDEstorage_offsetSTORAGE_OFFSETro  rJ  rp  rq  rr  rs  rt  ru  rv  rw  rx  ry  r   rz  r  r8   )
r   r]  r  r  r{  r  r}  rM  r  r~  s
   `         rz   rJ  "SubgraphTracer._lift_basic_symbols	  s    !B	S%,,&'B	V$B	 B	 	B	 B	H mU\\22!-"4"4"67' ? -S.2E2EqI! 8 ##u}}4%m&:&:&<=DA+  # 1n6K6KQO!%# > (!002 ? -S.2O2OP! %%)9)99(()?)?)A3G(()>)>)@#F%%%*:*:E<L<L)MM(()C)C)EsK(()B)B)DcJ%%%*:*:E<L<L)MM(()C)C)EsK(()B)B)DcJ,];;*==?
!D%m:G,,/C!6t " < u||44# 5ry   r  r   c                    UR                   R                  R                  n[        U5      S:X  a  / $ / nU H  nX@R                  ;  a  UR                  U5        M%  U R                  U   n[        U[        5      (       a  U" 5       nXPR                  U'   [        U[        R                  R                  5      (       a  UR                  U L a  M   SU S35       e   [        US S9$ )Nr   zThe proxy of symbol z" doesn't belong to current tracer.c                     U R                   $ r   )rJ  )r  s    rz   ro  7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>v
  s    ry   )r   )rZ  rx  r   rG  r  r   r@  r  r   r
   r  r  rZ  )r   r  r   to_be_boundr  r  s         rz   r  %SubgraphTracer.lookup_unbound_symbolsc
  s    vv{{//|!IB+++""2&&&r*E%++).""2&eUXX^^449M &rd*LMM  k'788ry   )r  r  r  r  r  r  r  rg   r  r   r  r  r  r  r  r  r  r  r  )NFN)NNN)NNNNr  )rr   rs   rt   ru   r   r   r
  r  r  r  rX  rR  r  r	   r  r   r
   r  rj  r-  r   r   r   rJ  r   ry  rz  r  rx   r   r   s   @rz   r   r     s    BJC4 up HL&6. DHXv&P7>`5&+Iuxx~~,E&F`5F~"5<<#=>~EMfEU~D9 9ell9K 9 9ry   r   )r   r  r  r   r  rA  r  r    r  r  r~  r   r  dataclassesr   typingr   r   r   r   r   r	   ry  torch._guardsr   torch._loggingtorch.distributeddistributedr  torch.nntorch.utils._pytreer  _pytreer  r
   torch._dynamo.excr   r   r   r   r   r   r   torch._subclasses.fake_tensorr   torch._utils_internalr   r  r   %torch.fx.experimental._backward_stater   r  r   r   r   r   torch.fx.passes.runtime_assertr   torch.utils._python_dispatchr   rN  r   r   r   r   r!   backends.registryr"   r#   bytecode_transformationr$   r%   r&   r'   r(   r)   r  r*   current_scope_idr+   r,   r-   r.   r/   r0   graph_deduplicationr1   graph_region_trackerr2   r`  r3   r4   mutation_guardr5   r   r6   r7   rp   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   variables.baserX   variables.builderrY   rZ   r[   r\   variables.listsr]   variables.miscr^   r_   variables.nn_moduler`   variables.tensorra   rb   rc   rd   variables.torch_functionre   r  rf   	getLoggerrr   r   r  getArtifactLoggergraph_tabular_logr  graph_sizes_logr!  rm   r|   	lru_cacher   r   r   r   r   r   r   r   r   objectr  r   r  r  r  r  r  Tracerr   rq   ry   rz   <module>r     s`  *         	 
   ! F F       $ $  N  5 0 : ?  K F W W 5  '  -  ; 4 / 0 @        ( ,  . 6 1  C H !NN44XwG 11(LI..228]K11(LI $   0 T4 4 - - -	%UXX__ 	%   F 	S&[l, l,`1J FR  oo' 2 2[9RYY [9ry   