
    3j                    	   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rSSKrSSKrSSKrSSKJrJrJr  SSKJrJr  SSKJr  SSKJrJrJrJrJr  SSK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.J/r/  SS	K0J1r1  SS
K2J3r3J4r4  SSK#J5r5J6r6J7r7J8r8J9r9J:r:  SSK;J<r<  SSK=J>r>  SSK?J@r@  SSKAJBrB  SSKCJDrD  SSKEJFrF  SSKGJHrH  SSKIJJrJJKrKJLrLJMrMJNrNJOrO  SSKPJQrQ  SSKRJSrS  SSKTJUrU  SSKVJWrW  SSKXJYrYJZrZJr[J\r\  SSK]J^r^J_r_  SSK`JaraJbrbJcrcJdrdJereJfrfJgrgJhrhJiriJjrjJkrk  SSKlJlrl  SSKmJnrn  SSKoJprp  SSKqJrrr  SS KZJsrsJtrtJuruJvrvJwrw  SS!KxJyryJzrz  SS"K{J|r|  SS#K}J~r~Jr  SS$KJr  SS%K1JrJr  SS&KJr  SS'KJrJrJr  SS(KJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SS)K+JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SS*KJrJrJrJr  SS+KJr  SS,KJrJr  SS-KJr  SS.KJr  SS/KJr  SS0KJrJrJr  SS1KJr  SS2KJr  \(       a  SS3KJr  SS4KJr  SS5KJr  SS6KJr  SS7KJr  \GR                  " \5      r\$GR                  GR                  \S85      r\$GR                  GR                  \S95      r\$GR                  GR                  \S:5      r\$GR                  GR                  \S;5      r\1GR                  r\$GR                  GR                  GR                  r\$GR                  GR                  GR                  rS<\S=\\\4   S>\S?\4   4S@ jr\" SASB9 " SC SD5      5       r\" SASB9 " SE SF5      5       r SpSG\\\$R^                  \S-  4      SH\\$GR                  GR                  GR                     S-  S>\\$GR                  GR                  GR                     4SI jjr\GR                  S>\4SJ j5       r\ " SK SL5      5       rSM\S>\/ \\   4   4SN jr " SO SP\$GR                  GR                  5      r " SQ SR5      r\\\4   r\ " SS ST5      5       r\ " SU SV5      5       r\ " SW SX5      5       r\ " SY SZ5      5       rS[\S>\\\4   4S\ jr " S] S^\5      r " S_ S`\5      r " Sa Sb5      rScrSd\Se\Sf\Sg\Sh\S>S4Si jr\GR                   " 5       Gr\ " Sj5      Gr\!" Sk5      Gr " Sl Sm5      Gr " Sn So\.GR
                  5      Grg)qa  
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)Callable	GeneratorSequence)	dataclassfield)CodeType)AnycastOptionalTYPE_CHECKINGUnion)	ParamSpecTypeVar)fxTensor)guards)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourcetracingTracingContext)FakeScriptObject)is_opaque_type)
FakeTensor)signpost_event)_ConstraintTarget)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnvSpecializationuninteresting_files)Target)insert_deferred_runtime_asserts)
OrderedSet)is_traceable_wrapper_subclass   )configexclogging	variables)
CompiledFn
CompilerFn)create_binary_slicecreate_binary_subscrcreate_build_tuplecreate_call_functioncreate_dup_topcreate_instructioncreate_load_constcreate_rot_ncreate_swapInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)get_interface_for_device)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplementedunimplemented_with_warning)has_user_objectsindex_to_bytecode_constructor)apply_graph_deduplication)#get_backend_override_for_compile_id+get_inductor_config_override_for_compile_id)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffectsValueMutationExisting)_get_source_debug_name
AttrSourceBackwardStateSourceConstantSourceDictGetItemSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceNumpyTensorSourceParamBufferSourceShapeEnvSourceSyntheticLocalSourceTensorPropertyTensorPropertySource)_extract_tensor_dictcheckpoint_paramsCleanupHookclone_inputscompilation_time_metricscount_callscountersdynamo_timedget_chromium_event_loggerget_instruction_source_311get_locals_to_stealget_static_address_typeget_unique_name_wrtgraph_break_reasonsincrement_op_countistypelazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)BackwardStateGraphArgGraphArgTrackedFakewrap_fx_proxy)ContextWrappingVariable)ClosureConversionErrorVariableTracker)BaseListVariable)NullVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableUnspecializedPythonVariable)TensorWithTFOverrideVariable)UserDefinedDictVariableDynamoProfilerState)CompilePackage)InstructionTranslatorBase)_CudagraphAnnotation)StorageWeakRefgraph
graph_codegraph_sizes
trace_callcompiler_fnconfig_patchesreturn.c                 r   ^ ^ S[         S[         S[         4U U4S jjn[        T SS5      Ul        T Ul        U$ )z
Wrap a compiler function to apply inductor config patches during compilation.

Passes config_patches as a keyword argument so that compile_fx can
propagate them to backward compilation via its inner_compile wrapping.
gmexample_inputsr   c                    > T" XTS9$ )N)r    )r   r   r   r   s     T/home/wildlama/miniconda3/lib/python3.13/site-packages/torch/_dynamo/output_graph.pywrapped+_wrap_with_inductor_config.<locals>.wrapped   s    2nMM    __name__z	<wrapped>)r	   getattrr   __wrapped__)r   r   r   s   `` r   _wrap_with_inductor_configr      sG    NC N N N N {JDG%GNr   T)frozenc                   *    \ rS rSr% \\S'   \\S'   Srg)AliasingInfo   has_aliasingmsgr   Nr   
__module____qualname____firstlineno__bool__annotations__str__static_attributes__r   r   r   r   r          	Hr   r   c                   *    \ rS rSr% \\S'   \\S'   Srg)MutationInfo   has_mutationr   r   Nr   r   r   r   r   r      r   r   r   tensors_with_sourcesstop_atc                    Uc
  [        5       n[        5       n/ nU  HH  u  pE[        U[        R                  5      (       d  M&  UR                  nUc  M7  UR                  U5        MJ     U(       ac  UR                  5       nXr;   a  M  Xq;   a  M%  UR                  U5        UR                   H  u  pUc  M
  UR                  U5        M     U(       a  Mc  U$ )a  Collect all grad_fns reachable from tensors' autograd graphs.

Performs a DFS traversal and collects all visited grad_fns.
Optionally stops traversal nodes in stop_at set. This signals the
autograd.grad boundary.

Args:
    tensors_with_sources: List of (tensor, source_name) tuples to start search from.
    stop_at: Optional set of grad_fns where traversal should stop (excluded from result).

Returns:
    Set of all reachable grad_fns.
)	set
isinstancetorchr   grad_fnappendpopaddnext_functions)	r   r   visitedstacktensor_r   nodenext_fns	            r   collect_reachable_grad_fnsr      s    " %.1eG-/E)	fell++nnG"W%	 * yy{? ?D--JG"W% . % Nr   c                  6    [         R                  " [        5      $ N)torchdynamo_loggingget_step_loggerlogr   r   r   _step_loggerr     s    ..s33r   c                   `    \ rS rSr% Sr\\S'   \\R                     \S'   Sr
\\S'   S
S jrS	rg)GraphCompileReasoni  zOStores why a given output graph was compiled; i.e. what caused the graph break.reason
user_stackTgraph_breakNc                 T    U R                   (       a  [        R                  " U 5        g g r   )r   rp   r   selfs    r   __post_init__ GraphCompileReason.__post_init__$  s    &&t, r   r   r   N)r   r   r   r   __doc__r   r   list	tracebackFrameSummaryr   r   r   r   r   r   r   r   r     s,    YKY++,, K-r   r   random_callsc                 2   ^  S[         [           4U 4S jjnU$ )Nr   c            
      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   r   )fnargskwargsr   s      r   _gen_rand_values1_get_gen_rand_values_fn.<locals>._gen_rand_values*  s*    <HIL(8&D#F#LIIIs   !)r   r	   )r   r   s   ` r   _get_gen_rand_values_fnr   )  s    Jd3i J r   c                      ^  \ rS rSrSrS\\\R                  R                  4   SS4U 4S jjr
S\4S jrS\\\R                  R                  4   SS4S jrS	rU =r$ )
FakeRootModulei0  z'Trick the constructor of fx.GraphModule
nn_modulesr   Nc                 l   > [         TU ]  5         UR                  5        H  u  p#[        XU5        M     g r   )super__init__itemssetattr)r   r   kv	__class__s       r   r   FakeRootModule.__init__3  s-    $$&DADQ 'r   c                     g)NzFakeRootModule(...)r   r   s    r   __repr__FakeRootModule.__repr__8  s    $r   c                 N    UR                  5        H  u  p#[        XU5        M     g r   )r   r   )r   r   r   r   s       r   add_nn_modulesFakeRootModule.add_nn_modules;  s!    $$&DADQ 'r   r   )r   r   r   r   r   dictr   r   nnModuler   r   r   r   __classcell__r   s   @r   r   r   0  sa    1 4UXX__(<#=  $  
%# % c588??.B)C      r   r   c                   ~    \ rS rSrS\SS4S jrS\R                  R                  S\	\R                     S\4S jrS	rg)
WrapperBackendi@  backendr   Nc                     Xl         g r   )r   )r   r   s     r   r   WrapperBackend.__init__A  s    #*r   r   r   c                    [        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)rd   restorer   copydeepcopyr   	candidateforwardr-   verify_correctnessrf   rv   RuntimeError	Exceptionr   	exception)r   r   r   copy_gmcorrectresults         r   __call__WrapperBackend.__call__D  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  )r   r   r   r   r2   r   r   r   GraphModuler   r   r1   r  r   r   r   r   r   r   @  sE    +
 +t +((&&8<U\\8J	r   r   c                   \    \ rS rSr% SrSr\\S'   Sr\\S'   Sr	\\S'   Sr
\\S'   SS	 jrS
rg)BytecodeTracingTimingsig  aH  Accumulated wall-clock time (ns) for major components during Dynamo
bytecode tracing that are not related to variable trackers.  Each field
is an int accumulator that gets bumped via ``time.time_ns()`` in the
corresponding hot-path wrapper.  To add a new timer, add a field here
and wire up the wrapper in the relevant function.r   get_fake_value_nscreate_proxy_ns!wrap_to_fake_tensor_and_record_nsvariable_builder_call_nsNc                    [        5       n0 n[        R                  " U 5       H  n[        XR                  5      nUS:  d  M   UR                  R                  S5      nUS-  X% S3'   [        R                  " U/ 5      R                  US-  5        [        XR                  S5        M     U(       a  UR                  " S0 UD6  gg)zzFlush accumulated timings to the bytecode_tracing chromium event
and to compilation_time_metrics, then reset all counters.r   _nsg    eA_time_sN)bytecode_tracing)rk   dataclassesfieldsr   nameremovesuffixrg   
setdefaultr   r   try_add_event_data)r   chromium_log
event_datafns_valkeys         r   report_and_reset'BytecodeTracingTimings.report_and_resett  s     12
##D)AT66*Fzff))%0.4sl
U'?+(33C<CCFSLQffa( * ++M*M r   r   r   )r   r   r   r   r   r  intr   r  r  r  r%  r   r   r   r   r  r  g  s:    9 sOS-.%s.$%c%Nr   r  c                      \ rS rSr% Sr\\S'   \\S'   \\R                  R                     \S'   \\   \S'   \\\\\4   4   \S'   \\S'   \\R"                  R$                  R&                     \S	'   \R(                  S
-  \S'   \R*                  R,                  R.                  R0                  \S'   \R2                  R4                  \S'   \\R2                  R6                     \S'   Sr\\S'   Sr\\S'   Sr\\S'   S
r \S
-  \S'   \!S\"4S j5       r#\!S\R2                  R4                  4S j5       r\!S\\R2                  R6                     4S j5       r$SS jr%Sr&g
)OutputGraphGuardsStatei  aZ  
A base class containing fields that are considered "persistent" when we
want to save all the important state for reconstrucing guards in a different
process. Normally we don't need to add states here, but we may have to when
the information is needed to serialize the guards, so the fields here are
supposed to be serializable as a requirement.
local_scopeglobal_scopetorch_function_mode_stackguard_on_key_orderinput_source_to_sizes_strides
dual_levelfunctorch_layersNcurrent_deviceglobal_state_guard_guards_aotautograd_guardsFexportskip_guards_checkexport_constraints%name_of_builtins_dict_key_in_fglobalsr   c                 0    [        S[        U 5       35      e)Nz%shape_env shouldn't be accessed from )AssertionErrortyper   s    r   	shape_env OutputGraphGuardsState.shape_env  s    DT$ZLQRRr   c                     U R                   $ r   )r3  r   s    r   r   OutputGraphGuardsState.guards  s    ||r   c                     U R                   $ r   )r4  r   s    r   aotautograd_guards)OutputGraphGuardsState.aotautograd_guards  s    '''r   c                 \   [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S9$ )N)r*  r+  r,  r-  r.  r/  r0  r1  r2  r8  r5  r7  r3  r4  r6  )r)  r*  r+  r,  r-  r.  r/  r0  r1  r2  r8  r5  r7  r   rA  r6  r   s    r   dump_guards_state(OutputGraphGuardsState.dump_guards_state  s    %((**&*&D&D#66*.*L*L!22..#66262\2\;;#66KK $ 7 7"44
 	
r   r   )r   r)  )'r   r   r   r   r   Scoper   r   r   	overridesTorchFunctionModer   r   r   r   r	   r'  
_functorchpyfunctorchFuncTorchInterpreterdevice_C_dynamor   GlobalStateGuardr3  	GuardsSetGuardEnvExprr5  r   r6  r7  r8  propertyr%   r<  rA  rD  r   r   r   r   r)  r)    s_    #EOO$E$EFFF##'S#X(>#??O5++77LLMMLL4''((//@@@]]$$$emm8899 FD#t#$$8<)3:<S8 S S //   (D)C)C$D ( (
r   r)  c                       \ rS rSr% SrSr\\S'   \" \	S9r
\	\\4   \S'   \" \S9r\\   \S'   \" \S9r\\   \S'   \" \S9r\\\\\S	4   4      \S
'   \" \S9r\\   \S'   \" \S9r\\\\\S	4   4      \S'   Srg)StackLocalsMetadatai  z^
Stores metadata for a frame's stack and locals for the purposes of building resume functions
r   	num_stackdefault_factorylocals_namesstack_null_idxeslocals_null_keys.stack_ctx_argsstack_ctx_idxes_origlocals_ctx_argsr   N)r   r   r   r   r   rU  r'  r   dc_fieldr   rX  r   r   rY  rZ  r[  tupler	   r\  r]  r   r   r   r   rT  rT    s     Is#+$L$sCx.  #+4"@d3i@"*4"@d3i@8@QU8VNDsE#s(O345V&.t&D$s)D9ARV9WOT%U38_ 456Wr   rT  c                      \ rS rSr% \" \S9r\\\S-  4   \	S'   \" \S9r
\\\\\4   4   \	S'   \R                  R                   R"                  r\R                  R                   R&                  \R                  R                   R(                  -  \	S'   \" \S9r\\\\\R                  R                   R&                  \R                  R                   R(                  -  4   4   \	S'   Srg)	ExportMetaDatai  rV  Ngraph_input_idx_to_local_sourceoutput_return_typeout_specmodule_call_specr   )r   r   r   r   r^  r   rb  r'  r   r   rc  r_  r   r	   r   utils_pytree
_LEAF_SPECrd  TreeSpecLeafSpecre  r   r   r   r   ra  ra    s    
 AIA#T#v}*<%=  6>d5SS%S/12S 	&& ekk!!**U[[-@-@-I-II  	& dS%++%%..1D1D1M1MMMN	P 'r   ra  r+  c                 R    U S   n[        U[        5      (       d  UR                  nU$ )N__builtins__)r   r   __dict__)r+  
f_builtinss     r   get_builtins_dictro    s,     n-Jj$''((
r   c                      ^  \ rS rSrSr    SS\S\\\4   S-  S\S-  S\	S-  S\\
\\   4   S-  S	S4U 4S
 jjjr\S	\4S j5       rSS\S\S	S4S jjrSrU =r$ )OutputGraphCommoni  a  
A minimal interface for full graph capture. It is intended to be
the target of any tracer that feeds into backends.

Currently dynamo's OutputGraph is the only known implementation
of this interface, used by (aot) precompile and (strict) export.
Importantly, that implementation also contains many other fields
that are using during tracing but not included in this interface
because they are not used once tracing is complete.

It should be safe to assume that (caching) precompile also uses
this interface.

In the future, we want make_fx, used by (non-strict) export, to
also implement this interface.

The serializable part of this interface is OutputGraphGuardsState.
We do not need to serialize other parts; however it will pay to
be disciplined about what those other parts are, especially since
we want other tracers to be able to meaningfully implement them,
and we should generally try to cut them down when possible.
Noutput_graph_guards_stateimport_sourcesr<  export_metadatatracked_fakes_id_to_sourcer   c                   > [         TU ]  UR                  UR                  UR                  UR
                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                   5        U=(       d    0 U l        U=(       d
    [%        5       U l        U=(       d
    [)        5       U l        U=(       d    0 U l        g r   )r   r   r*  r+  r,  r-  r.  r/  r0  r1  r2  r3  r4  r5  r6  r7  r8  rs  r%   
_shape_envra  rt  ru  )r   rr  rs  r<  rt  ru  r   s         r   r   OutputGraphCommon.__init__  s     	%11%22%??%88%CC%00%66%44%88%--%99%,,%77%88%KK	
$ -2 $1xz.B.2B&," 	'r   c                     U R                   $ r   )rw  r   s    r   r<  OutputGraphCommon.shape_env@  s    r   r   r   c                     [         er   )NotImplementedErrorr   r   r   s      r   bypass_package OutputGraphCommon.bypass_packageD  s
     "!r   )rw  rt  rs  ru  NNNN )r   r   r   r   r   r)  r   r   r%   ra  r'  r   r   r   rR  r<  r	   r~  r   r   r   s   @r   rq  rq    s    4 15%)15EI$
#9$
 S#X-$
 d?	$

 ($.$
 %)d6l):$;d$B$
 
$
 $
L 8  "S " " " "r   rq  c                      \ rS rSr% Sr\\S'    SS\\\	4   S\
S-  SS	S
\S\\   S\	S\S\S\S\\R$                  R&                     S\S   S\SS4S jjrSS.S\S\\   4S jjrS\S\S\4S jrS\S\SS4S jrSS jrSS jrS\4S  jr SS!\S"\S\\\R@                  RB                  4   4S# jjr"S\R@                  RB                  4S$ jr#SS% jr$S\\   S-  4S& jr% SS'\&S(\	4   S)\\	S(4   S*\\S-  S(4   S-  S\4S+ jjr'S'\&/ \	4   SS4S, jr(SS- jr)\*SS/ j5       r+\*SS0 j5       r,S\4S1 jr-    SS3 jr.S4r/S5\0SS4S6 jr1\*S\R@                  Rd                  4S7 j5       r3\3Rh                  S8\R@                  Rd                  SS4S9 j5       r3\*S\\\ RB                  4   4S: j5       r5\*S\\ Rl                  \Rn                  4   4S; j5       r8\*S\\9Rt                  \;\R@                  RB                  S<4   4   4S= j5       r<S)\	S>\	S\R@                  RB                  4S? jr=S)\	S>\	S\R@                  Rl                  4S@ jr>S)\	S>\	SS4SA jr?\@R                   SSB\BS-  SC\S.   SD\S-  S\CSE   4SF jj5       rD\*SSG j5       rE\*S\R                  R                  4SH j5       rH\*S\I4SI j5       rJ\*S\R                  R                  4SJ j5       rM\*S\\\	4   4SK j5       rN\*S\\R                  R                     4SL j5       rP SSM\\\\&S(\	4   \4   4   S-  SS4SN jjrQSSO jrRSSP jrS\*SSQ j5       rTSR\S\	4SS jrUS\04ST jrVS\4SU jrWS\4SV jrXSW\S\R                  R                  \	-  4SX jr[SSY\S\4SZ jjr\SY\SS4S[ jr]\^S\\	S\4S] j5       r_S^\S_\	S\ RB                  4S` jr`Sa\R                  R                  \Rn                  -  \	-  S\\	Sb\	S\4Sc jraS2S	S\\\b   \\\4   4   4Sd jrcS2S	Se\0S\\\   \d4   4Sf jre SS2S	Sg\fSe\0S\\d   4Sh jjrgS2S	Si\hSS4Sj jriS2S	Sk\\   Si\hSl\SS4
Sm jrjSSn jrkSSg\S>\	SS4So jjrlS\\\\0\-     4   4Sp jrmSY\S\4Sq jrn\@R                  S\	4Sr j5       roSSs jrpSt\Su   S2S	SS4Sv jrqSt\Su   S2S	SS4Sw jrrS2S	St\\   Sx\sS\\b   4Sy jrt\*S\\ Rl                     4Sz j5       ru\*S\\v   4S{ j5       rwS|\ R                  S}\\7   S\y4S~ jrzS|\ R                  S}\\7   S\y4S jr{S\\\R@                  R                  4   4S jr|SY\S\R@                  R                  S\4S jr}S\\Rn                     4S jr~SS jrSS jrSS jrS"\\b   SS4S jrSY\S\R                  S\\\	4   SS4S jrSY\S8\	SS4S jrS"\S8\	S\4S jrS"\S8\	S\4S jrSS jrS\&\ R                  /S4   SS4S jrS\R@                  Rl                  S\	4S jrS\R                  R                  SR\SS4S jrSrg)OutputGraphiK  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_effectsFcode_optionsr   Nroot_txr   r5  r7  frame_stater*  r+  f_coder,  packager   	one_graphr   c                 B
   [         R                  U UUU
[        5       0 [        R                  R
                  R                  [        R                  R                  R                  5       [        R                  R                  R                  [        R                  R                  R                  =(       d2    [        R                   R                  R"                  R%                  5       [        R&                  R)                  5       / S9  [+        XS9/U l        0 U l        / U l        X@l        XPl        X`l        / U l        [;        [<        5      U l        [        5       U l         U	RB                  U	RD                  U	RF                  S.U l$        S U l%        [M        5       U l'        SU l(        / U l)        [U        U RR                  U=(       d    [V        RX                  U=(       d    [V        RZ                  [V        R\                  U RH                  S9nSS K/Js  J+n  URa                  SS9   [        Rb                  Re                  U[g        U R2                  5      U R2                  S9nS S S 5        [i        W5      U l5        U Rj                  Rl                  Ro                  U	5        U RJ                  U Rj                  l%        U Rj                  Rl                  U l6        [p        Rr                  " 5       U l:        U Rw                  5         [x        Rz                  " [|        5      U l?        0 U l@        [        U 5      U lB        0 U lC        0 U lD        [        5       U lE        0 U lF        [        R                  " 5       U lI        [        U5      U lK        / U lL        SU lM        0 U lN        S U lO        / U lP        X lQ        X0lR        S U lS        XlT        0 U lU        / U lV        / U lW        SU lX        0 U lY        [        R                   R                  5       U l[        SU l\        SU l]        [        5       U l^        [        0 5      U l_        [        0 5      U l`        U R                  5         0 U lb        / U lc        S U ld        / U le        0 U lf        0 U lg        S U lh        S U li        U R                  5       U lk        [        R                  " 5       U ln        [        5       U lp        U R                  5       U lr        0 U ls        [        5       U lu        [        5       U lw        0 U lx        S U ly        g ! , (       d  f       GN= f)	N)r-  r.  r/  r0  r1  r2  r3  r4  )	is_export)co_nameco_filenameco_firstlinenoF)tracked_fakesallow_scalar_outputsallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards	co_fieldsr   (fake_tensor_allow_unsafe_data_ptr_access)r<  allow_non_fake_inputsr5  )zr)  r   r   r   autograd
forward_ad_current_levelrI  rJ  #retrieve_all_functorch_interpretersrf  _deviceCURRENT_DEVICErN  convert_frameinitial_global_staterM  r   rO  r3  rP  SubgraphTracertracersinput_source_to_varleaf_var_creation_orderr5  r7  r  cleanup_hooksnext_compile_id_counter
compile_idinstalled_globalsr  r  r  r  cudagraph_annotationrL   region_tracker_emit_debugger_breakpointr  r%   r-   capture_scalar_outputs capture_dynamic_output_shape_opsr  torch._functorch.configpatch_subclassesFakeTensorModer   r   tracing_contexttraced_coder   r   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   ru  param_name_to_sourcerQ   r  variable_tracker_cachemro_source_cacheguarded_mro_absent_keyssignature_cache	itertoolscountunique_var_idr   r  output_instructions	timestamp_input_mutation_streams_last_checked_input_versionsregister_finalizer_fnsr   r  profiler_stater  source_to_user_stacks_current_txcleanupsshould_exitunspec_variable_map_is_torch_function_mode_enabledtorch_function_mode_enabledtorch_function_subclass_inlined!has_user_defined_allowed_in_graphautograd_grad_consumed_grad_fnsnon_compliant_opscompliant_custom_opssave_global_state dynamo_flat_name_to_original_fqnr   random_values_varpregraph_bytecodesynthetic_source_ctor_infobackward_statebackward_state_proxybackward_state_var!install_builtins_dict_in_fglobalsr8  
contextlib	ExitStackcompiler_trace_stackr  bytecode_tracing_timings+maybe_install_saved_tensors_hooks_subgraphs"saved_tensors_hooks_subgraph_namesrs  ra  rt  r*   "used_inlined_inbuilt_modules_namesattr_source_cache(_cached_replayed_side_effect_source_refs)r   r  r   r  r5  r7  r  r*  r+  r  r,  r  r  r<  _config	fake_modes                   r   r   OutputGraph.__init__X  s    	''%"u*,~~00??"--99]]_ ;;..==$}}::OO  :xx&&779MM++- " 	( 	
" 't>? CE  ?A$"4&68#$78+.5
 ~~!--$33
 BF!02/4& 13 ,,!*!Kf.K.K+4 ,7668>8j8jnn
	& 	21]]E]J))88#&*4;;&7{{	 9 I K 0>i/H((//7484M4M1//;;3A3T3T3V  " ##D) 	' ?A!'- FH# KM
 >AU$;=&__.,0,>68   KM$CG) OQ# /: ;? RT"<>+- KM  ,188+S+S+U( 05, 27. PSu, >AW ADB! 	  AC-  	 '+ 57  	' ;=;?!.2 224 	2 %/$8$8$:!(>(@% <<> 	/
 /1-/ DN</GIPT5o KJs   #=T
Tpopulate_export_metadatar  c                b   U(       d=  U R                   R                  (       d"  U R                  b  [        U R                  5      $ SSKJn  / nU R                   R                  5        GH  n[        US5      (       d  M  UR                  n[        U[        [        45      (       d  M@  [        U[        5      (       Ga   [        UR                  U5      (       a  U(       a  UR                  c   eUR                  R                  R                  5        H  u  nn0 nUR                  R                  5        H/  u  pU
R!                  5       XR"                  R!                  5       '   M1     SS/[        UR%                  5       5      :X  d   eUU R&                  R(                  UR"                  R!                  5       '   M     [        U[        5      (       a  [        UR                  U5      (       a  GM  UR+                  U5        GM     U Vs/ s H  n[-        UR.                  5      PM     sn$ s  snf )zKReturn Python-side effect sources that Dynamo replays outside the FX graph.r   )_ExportModuleSpecTrackerDictmutation_typein_specrd  )r  id_to_variabler  r   torch.export._tracer  _get_modified_varshasattrr  r   rP   rR   r   value_base_vtr   as_python_constantvtkeysrt  re  r   rS   source)r   r  r  potential_side_effectsvarmut_typer   r   specsk_specvals              r   $get_replayed_side_effect_source_refs0OutputGraph.get_replayed_side_effect_source_refsn  s   
 )%%44==IEEFFD!#$$779CsO,,,, 8:OP  "#'>??J		#?E E 4#&<<#;;#;
 !$ 2 2 8 8 :	! ! !
 )+3477==?KF(+(>(>(@ %*))*F*F*H$I 4C )2:'>$uzz|BT'T T'T %* !% 4 4 E E$%DD$;$;$=!" !;" #3(?@@&syy2NOO.55c:K :N ?UU>Ts&szz2>TUUUs   
H,basepathc                 0   UR                  S5      nXS   4nX@R                  ;  a  [        XS   5      U R                  U'   U R                  U   nUSS   H<  nXV4nX@R                  ;  a  [        XV5      U R                  U'   U R                  U   nM>     U$ )N.r   r,   )splitr  rT   )r   r  r  partsr$  r  parts          r   get_chained_attr_source#OutputGraph.get_chained_attr_source  s    

31X,,,*4T8*DD""3''',!"ID.C000.8.F&&s+++C0F	 
 r   r^   c                     UR                  SS5      n[        U5      S:X  a  [        X5      $ U R                  XS   5      n[        XCS   5      $ )Nr  r,   r   )rsplitlenr^   r  )r   r  r  r  intermediate_bases        r   get_chained_param_buffer_source+OutputGraph.get_chained_param_buffer_source  sO     C#u:?$T00 88QxH !2!H==r   c                 ~   U R                   R                  [        SSS95        [        R                  (       a  SSKJn  U R                  c  U" 5       U l        U R                  R                  nU R                  R                  UR                  UR                  UR                  [        R                  " 5       5        g g )Nr  Tlog_pt2_compile_eventr   r   )r  enter_contextrj   r-   dynamo_profilertorch._dynamo.dynamo_profilerr   r  r  r  pushr  r  r  timetime_ns)r   r   codes      r   mark_bytecode_tracing_start'OutputGraph.mark_bytecode_tracing_start  s    !!//"&*	
 !!I""*&9&;#<<&&D$$  ##	 "r   c                    U R                   R                  5         U R                  R                  5         [        R
                  (       Ga/  U R                  Gb   SSKJn  U R                  R                  5       n[        R                  " 5       nUb  X2R                  -
  nXBR                  -
  nU" UR                  UR                  UR                   UU[#        U R$                  R&                  R(                  5      SS S S UR*                  SS9nU R                  R-                  U5        S n[/        [        R
                  [0        5      (       a  [        R
                  nU R                  R3                  U5        g g g )Nr   )FunctionTraceTimingr   )	func_namefilenamefirstlineno
cumtime_ns
tottime_nsbytecode_countinline_depthcaller_func_namecaller_filenamecaller_firstlinenois_primitive_call
call_stack)r  r%  r  closer-   r  r  r  r  r   r  r  start_time_nschild_time_nsr  r  r   r  r  r  co_coder(  record_timingr   r   
dump_stats)r   r  stack_entrytrace_end_nsr!  r"  timingoutput_files           r   mark_bytecode_tracing_stop&OutputGraph.mark_bytecode_tracing_stop  s4   %%668!!'')!!!d&9&9&EI--113K<<>L&),E,EE
'*C*CC
,)33(11 + 7 7))#&t||':':'B'B#C!"%)$('+&1&C&C! ##11&9 K&00#66$44**;79 'F!r   c                 P    [        U R                  5      nU R                  SU5      $ )N__builtins_dict__)ro  r+  install_global)r   rn  s     r   r  -OutputGraph.install_builtins_dict_in_fglobals  s&    &t'8'89
""#6
CCr   hookprefixc                     U [        U R                  5       3nX0R                  ;  d   eXR                  U'   X0R                  5       4$ r   )r  r  get_backward_state_proxy)r   r:  r;  r  s       r   add_backward_state_hook#OutputGraph.add_backward_state_hook  sP     #d11234.....$(D!22444r   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 exportr  z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_stater  grapharg)r  r5  rE   r!   root_tracercreate_graph_inputr;  rU   rx   r   metanew_varr  )r   example_values     r   r=  $OutputGraph.get_backward_state_proxy  s    $$,{{D U	 *OM(,(8(8(K(K']#*,	 )L )D% ?T>UD%%**//
;&*llnD#(((r   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        U R                   R                  [        5       R                  [        R                  5      5        [        R                  R                  R                  5       nUb@  U R                   R                  [        5       R                  [        R                   5      5        [        R"                  R$                  R&                  (       dA  U R                   R                  [        5       R                  [        R(                  5      5        g g r   )r   r   r_   
make_guardrM   	SHAPE_ENVrY   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICEGLOBAL_STATETORCH_FUNCTION_STATEr   rM  rI  peek_interpreter_stackFUNCTORCH_STACK_MATCHrN  compiled_autogradin_compiled_autograd_regionAUTOGRAD_SAVED_TENSORS_HOOKS)r   cis     r   r  OutputGraph.init_ambient_guards  sr    	(33L4J4JKL**<+P+PQ	
 	)+66|7M7MNO)+66|7R7RST)+66|7P7PQR**<+L+LM	
 XX  779>KKOO!#..|/Q/QR }}..JJKKOO!#.. == Kr   c                 v   [         R                  R                  R                  (       a  g [         R                  R
                  R                  R                  n[         R                  R
                  R                  R                  nU" 5       nU" U5      (       d  g Uu  pEU R                  S[         R                  R                  U R                  UR                  5      5      nU R                  S[         R                  R                  U R                  UR                  5      5      nUS:X  d   eUS:X  d   eXg/$ )Nsaved_tensors_hooks_packsaved_tensors_hooks_unpacksaved_tensors_hooks_pack_0saved_tensors_hooks_unpack_0)r   rN  rY  rZ  rI  _aot_autogradrf  top_saved_tensors_hooks"saved_tensors_hooks_are_inlineableinstall_subgraphr   r  r   r   )r   	get_hooksare_inline_hookshookspack_gm	unpack_gmpack_subgraph_nameunpack_subgraph_names           r   r  7OutputGraph.maybe_install_saved_tensors_hooks_subgraphs1  s   ==**FF$$2288PP	**00SS 	 && #!22&HH  '--@
  $44(HH  )//B 
 "%AAAA#'EEEE"99r   r   .r   ctor_arg_sourcesc                   ^^ T" U6 nU R                  5       n[        U R                  5      mTR                  UU4S j5        U(       a"  TR	                  S [        X#5       5       5        O3T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TX#4U R"                  U'   [$        R&                  " U R                  XF5      nUR)                  5       n[*        R,                  " 5       R.                  R0                  R3                  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_fromr   r   )cgr   s   r   <lambda>3OutputGraph.synthetic_graph_input.<locals>.<lambda>[  s    B''r   c              3   n   #    U  H+  u  pUb  UO[         R                  R                  U5      v   M-     g 7fr   )r0   ConstantVariablecreate).0r  srcs      r   	<genexpr>4OutputGraph.synthetic_graph_input.<locals>.<genexpr>a  s3       ;HC I,F,F,M,Mc,RR ;s   35F)rL  r?   r  add_push_nullforeachzipmapr0   rw  rx  call_functionr  storer  extendget_instructionsr`   r  r~   buildrealizer   getguards_contextdynamo_guardsremove_guards_with_source)	r   r   r   ro  rM  varnamer  r  rs  s	    `      @r   synthetic_graph_input!OutputGraph.synthetic_graph_inputN  s)    D	,,.t||$
	
 JJ  #D ; 
 JJs955<<dCD
TE*
%%b&9&9&;<%g.35t2N''/ &&t||]K!++99SS	
 r   c                 :    U R                   R                  U5        g r   )r  r   )r   r   s     r   add_cleanup_hookOutputGraph.add_cleanup_hookt  s    !!"%r   c                 ~    [        U R                  5       H
  nU" 5         M     U R                  R                  5         g r   )reversedr  clear)r   r:  s     r   call_cleanup_hooksOutputGraph.call_cleanup_hooksw  s.    T//0DF 1  "r   r  c                      U R                   S   $ Nr   r  r   s    r   rI  OutputGraph.root_tracer|  s    ||Ar   c                      U R                   S   $ Nr  r   s    r   current_tracerOutputGraph.current_tracer  s    ||Br   c                 2    [        U R                  5      S:H  $ )Nr,   )r  r  r   s    r   is_root_tracerOutputGraph.is_root_tracer  s    4<< A%%r   txc                    [        US5      (       d  gUR                  R                  5       (       d  gU R                  nU R                  c#  [        [        UR                  5      5      U l        UR                  R                  5       nSnUR                  R                   H  nUR                  S:w  a    gUR                  R                  S5      n[        U[        R                   5      (       d  MQ  U R                  R                  U5      nUR"                  nUbD  X:  a?  X0R$                  ;  a"  [&        R(                  " 5       U R$                  U'   XR                  U'   US-  nM     g)zRecord which stream index has input mutations by comparing current
tensor versions against the versions captured at graph input creation.symbolic_stream_stateNr   placeholderrM  r,   )r  r  in_stream_contextrI  r  r   	enumerate_input_versions_at_beginningcur_stream_idr   nodesoprK  r  r   r   r   _versionr  r   extract_stack)	r   r  tracercur_stream_index	input_idxr   rM  prev_versioncur_versions	            r   &check_input_mutation_on_current_stream2OutputGraph.check_input_mutation_on_current_stream  s4   
 r233''99;;!!,,404&==>1D- 33AAC	LL&&Dww-' IIMM/:MmU\\::<<@@KL'00K'K,F#+G+GG&446 001AB @K11)<NI 'r   a~  To fix this, either:
  1. Move the input mutation after the event.record() call.
  2. Record the event outside the compiled region:
       compiled_fn(x)
       event.record(stream)  # after torch.compile returns
  3. Insert a graph break before recording:
       torch._dynamo.graph_break()
       event.record(stream)
  4. Record the event on a stream that has no input mutations.stream_indexc                     XR                   ;  a  gU R                   U   n[        R                  " 5       nSSR                  UR	                  5       5       SSR                  UR	                  5       5       S3U R
                  -   n[        U5      e)u   Error if an event is being recorded on a stream that already has
an input mutation. Called at record time so ordering is naturally
respected — records before mutations won't trigger this.Na  An event was recorded on a stream where a graph input was previously mutated. The input mutation is applied via copy_() in the runtime epilogue after the graph executes, so the event would not capture the mutation, leading to incorrect synchronization.

Input mutation occurred here:
r  z
Event record occurred here:

)r  r   r  joinformat_EVENT_INPUT_MUTATION_FIXr  )r   r  mutation_stackrecord_stackr   s        r   'check_event_record_after_input_mutation3OutputGraph.check_event_record_after_input_mutation  s     ;;;55lC%335. ww~,,./0 1,ww|**,-.b2 594R4RS 	 3r   c                 .    U R                   R                  $ r   r  r   r   s    r   r   OutputGraph.graph  s    ""(((r   r  c                 $    XR                   l        g r   r  )r   r  s     r   r   r    s    $)!r   c                 .    U R                   R                  $ r   )r  input_name_to_proxyr   s    r   r  OutputGraph.input_name_to_proxy  s    ""666r   c                 .    U R                   R                  $ r   )r  real_value_cacher   s    r   r  OutputGraph.real_value_cache  s    ""333r   	LazyProxyc                 .    U R                   R                  $ r   )r  bound_symbolsr   s    r   r  OutputGraph.bound_symbols  s    ""000r   r   c                 :    U R                   R                  " U0 UD6$ r   )r  create_proxyr   r   r   s      r   r  OutputGraph.create_proxy  s    ""//@@@r   c                 :    U R                   R                  " U0 UD6$ r   )r  create_noder  s      r   r  OutputGraph.create_node      ""..???r   c                 :    U R                   R                  " U0 UD6$ r   )r  remove_noder  s      r   r  OutputGraph.remove_node  r  r   source_targetprior_tracerdescription)r  NNc              #     #    [        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
                  U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)parentr  r  r  )
r@   r  r  	__enter__r  r  r  r   __exit__r   )r   r  r  r  new_scope_ctxr  s         r   	subtracerOutputGraph.subtracer  s      ()	#**d.A.AAAA##%   #.."/"11;; +  LL'L""4t4LL ""4t4LLs   C3BC .C3/C00C3c                     U $ r   r   r   s    r   outputOutputGraph.output  s    r   c                 `    U R                   R                  c   eU R                   R                  $ r   )r  r  r   s    r   r  OutputGraph.fake_mode  s,    ##--999##---r   c                     U R                   R                  c   eU R                   R                  R                  c   eU R                   R                  R                  $ r   )r  r  r<  r   s    r   r<  OutputGraph.shape_env  sP    ##--999##--77CCC##--777r   c                 B    U R                   R                  R                  $ r   )r  r  r  r   s    r   r   OutputGraph.guards  s    ##22@@@r   c                 B    U R                   R                  R                  $ r   )r  module_contextr   r   s    r   r   OutputGraph.nn_modules"  s    ##22===r   c                 B    U R                   R                  R                  $ r   )r  r  rA  r   s    r   rA  OutputGraph.aotautograd_guards&  s    ##22EEEr   outc                    [        [        [        [        [        S[
        4   [        4   4   Ub  UOU R                  R                  R                  5      n[        R                  [        R                  " 5       4US'   [        R                  R                  5       =n(       a  UR                  OSn[         R"                  " [        R$                  U5      [        R&                  " U5      4US'   [         R"                  " [        R$                  S5      [        R&                  " S5      4US'   [         R"                  " [        R(                  U5      [        R*                  " U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.
.Ngrad_enabledcudaautocast_enabledcpuautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r
   r   r   r_  r   r	   r   r  global_contextglobal_stater   set_grad_enabledis_grad_enabledacceleratorcurrent_acceleratorr;  	functoolspartialset_autocast_enabledis_autocast_enabledset_autocast_dtypeget_autocast_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)r   r  r  accgpu_types        r   r  OutputGraph.save_global_state*  s    eHS#X.4556 ? ))88EE
 ).(>(>@U@U@W'X^$ !& 1 1 E E GGGCHHf 	 e88(C%%h/,
'(
 e88%@%%e,0
+,
 e66A$$X..
)*
 e66>$$U+.
)*
 ,,++-2
-.r   c                 :    U R                   R                  U5        g r   )r  r   )r   r  s     r   push_txOutputGraph.push_txS  s    #r   c                 6    U R                   R                  5       $ r   )r  r   r   s    r   pop_txOutputGraph.pop_txV  s    ##%%r   c                 Z    U R                   (       d  U R                  $ U R                   S   $ r  )r  r  r   s    r   
current_txOutputGraph.current_txY  s$    #'#3#3t||M9I9I"9MMr   r  c                 ~    UR                  U R                  R                  U R                  R                  S.0 0 5      $ )z
Resolve the runtime value a Source points to using root_tx's frame.

Useful to inspect the python object associated with the source during
debugging. Will also be used by invoke subgraph caching later on.
)GL)	get_valuer  	f_globalsf_locals)r   r  s     r   resolve_source_value OutputGraph.resolve_source_value]  s9     ,,((t||/D/DE
 	
r   c                 ,    [        U R                  5      $ r   )rh   r   r   s    r   rh   OutputGraph.count_callsj  s    4::&&r   c                 X    [        [        U R                  R                  5      5      S:H  $ r  )r  r   r   r  r   s    r   is_empty_graphOutputGraph.is_empty_graphm  s!    4

(()*a//r   c                     [        U R                  R                   Vs/ s H  oR                  S:X  d  M  UPM     sn5      S:  $ s  snf )Nr  r   )r  r   r  r  )r   xs     r   has_outputsOutputGraph.has_outputsp  s9    tzz//D/!4483CA/DEIIDs
   AAr  c                     U(       d   eU R                   nUR                  S5       H)  n[        U[        5      (       a  X#   nM  [	        X#5      nM+     U$ Nr  )r   r  r   r   r   )r   r  objr   s       r   get_submoduleOutputGraph.get_submodules  sH    t<@OOCA#t$$fco	 !
 
r   r  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_varnamesr   )r   r  r  r  )r   r  existingr  s       r   rL  OutputGraph.new_var}  s^    t((78F!D!3!3456C"!!-0SF:0
	 r   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   r  s     r   update_co_namesOutputGraph.update_co_names  s0    ((44j)dW4) 5r   namesc                  |   SR                  [        [        U 5      5      n[        R                  " SSU5      n[        R                  " SSU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   z9\._(?:modules|parameters|buffers)\[(['\"])([^'\"\]]+)\1\]z.\2z2getattr\(\s*([^,]+?)\s*,\s*(['\"])([^'\"]+)\2\s*\)z\1.\3z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)r  r  r   rer4  isalpha)r2  r  s     r   module_key_nameOutputGraph.module_key_name  s     xxC(vvH&RV
 vvA8T
 vv,eT:vvlIt4vvosD147??,,4<Dr   attr_prefix
attr_valuec                 p   [        U[        R                  R                  5      (       a?  U R                  R                  5        H!  u  p4XBL d  M  U R                  SUS0 5      nUs  $    [        XR                  5      nX R                  U'   U R                  SUS0 5      n[        UR                  U5        U$ Nget_attrr   )
r   r   r   r   r   r   r  ro   rw   r   )r   r9  r:  r  modproxy	attr_names          r   %register_static_attr_and_return_proxy1OutputGraph.register_static_attr_and_return_proxy  s    
 j%((//22!__224	$ --j$BGE L 5
 (__E	 &0	"!!*iR@%**j1r   targetoptionsc                   ^ ^^^
^^ [        TT 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      (       aG  T R                  mT R                  5       (       d  T R                  mS[        S[        4UU UUU4S jjnGOh[        T[        R                  R                  5      (       a  [        T[        R                  R                  5      (       d   eT(       a>  [!        TR#                  [$        R&                  5      5        S[        S[        4UU4S jjnOS[        S[        4UU4S jjnO[        T[        R(                  [        R*                  45      (       a  S[        S[        4UU U4S jjnOs[-        [/        T5      5      (       aE  T R                  mT R                  5       (       d  T R                  mS[        S[        4UU UU4S jjnOS[        S[        4U U4S	 jjnT R0                  R3                  5        H  u  pVUTL d  M  U" U5      s  $    [4        R6                  " U6 m
[9        T
T R0                  T R:                  5      m
TT R0                  T
'   [        T[        R                  R                  5      (       av  S
[        SS 4U
U U4S jjn[=        TS5      (       a!  TR?                  5        H  u  pU" U5        M     [=        TS5      (       a!  TRA                  5        H  u  pU" U5        M     U" T
5      $ )Nr  
module_keyr   c           	      d  > TR                   c   eTTR                   U '   TR                  c   e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!                  5       R"                  R$                  ;  d   e['        T5      UR!                  5       R"                  R$                  S'   U$ )Nguardedr=  r   rM  tensor_dict)r  r  r  r  rn   r   r]   rN   rP  rM   ID_MATCHrZ   TENSOR_MATCHr{   r  track_object_existingas_proxyr   rK  rc   )rF  r  rD  r   r  rC  r  s     r   	wrap_name6OutputGraph.register_attr_or_module.<locals>.wrap_name  sn   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$BKKM,>,>,C,CCCC9Mf9U""''6	r   c                 2   > [        [        T5      U T40 TD6$ r   )r   r;  rF  rD  rC  s    r   rN  rO    s    +DL*fXPWXXr   c                 2   > [         R                  " T40 TD6$ r   )r0   UnspecializedNNModuleVariablerQ  s    r   rN  rO    s    $BB6UWUUr   c           	      p   > [         R                  " TR                  TR                  SU S0 5      4ST0TD6$ )Nr=  r   sym_num)r   rx  r  r  )rF  rD  r   rC  s    r   rN  rO    sE    &--LL%%j*b"E # 	 r   c                 >  > [         R                  R                  R                  TR                  T5      nTR                  SU S0 5      n[        UR                  U5        [         R                  R                  R                  R                  R                  " X!40 TD6$ r<  )r   _libraryfake_class_registrymaybe_to_fake_objr  r  rw   r   rN  r0   script_objectTorchScriptObjectVariablerx  )rF  fake_script_objr?  rD  r   rC  r  s      r   rN  rO  %  s    "'.."D"D"V"VNNF# ++J
BK!%**o>}}..<<VV]].5 r   c                    > TR                   R                  U 5        TTR                  U '   [        R                  " TT[        U S95      $ )N)source_name)r  r0  r+  r~   r  rV   )rF  r   rC  s    r   rN  rO  3  sF    ++J706!!*-&,,"z: r   	leaf_namec                    > TR                   c   eTR                  TU 5      nT SU  3nUTR                   U'   [        T[        5      (       a-  U TR                  [
        R                  UR                  5      '   g g r&  r  r  r   r\   r  r  r7  r  r_  
new_sourcenew_namer  r   r  s      r   register_leaf_name?OutputGraph.register_attr_or_module.<locals>.register_leaf_nameF  s    00<<<!AA&)T
"V1YK06@))(3fk22 " 99#33JOOD 3r   _parameters_buffers)!rO   r5  r~   r  r  r   r   r^   r   r   r  r  rI  r   r   r   rN   rP  rM   	NN_MODULESymIntSymFloatr   r;  r   r   r  r7  ro   r+  r  named_parametersnamed_buffers)r   rC  r2  rD  rN  r   r   re  r_  r   r  r  r  s   `` `      @@@r   register_attr_or_module#OutputGraph.register_attr_or_module  s     44 #((&LGLLw-7""""f&78888fell++((F&&(( )) c  o    D 00fehhoo6666f//0F0FGHY# Y/ Y YV# V/ V V u~~ >??c o   DL))
 ((F&&(())c o  c o   OO))+DAF{ |# ,
 **E2"4$:K:KL &fehhoo.."c "d " " v}--$*$;$;$=LI&y1 %>vz**$*$8$8$:LI&y1 %; r   c                 X   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HG  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                   c   eUR                   R2                  nX;  aN  U R5                  U S35      nXU'   UR7                  [9        SUS9[;        U5      [=        5       [9        SUS9/5        UR                   c   eUR                   n['        X   5      X'   M     GMJ     XJ4$ )Nr   r+  _ref	LOAD_FASTargval
STORE_FAST)r*  r  rm   r   symbolic_localsvaluesr  store_attr_mutationsr  r   r   r   r   r   r  rP   r  rX   r  r\   
local_namer   	graphargs_exampler  indexrL  r  r8   r9   r4   )r   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuer"  stolen_namer   overridden_sourcesarg	list_namelist_idx
alias_name
old_sources                   r   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.+ e0 35>>C3<<..szz;77JJ))[8 

--I 1 1- @@@@ +8811 xx+++88>>*!%$+T*"J )3H%&&.{9M-h702.|JO	 xx+++XX
1<W=N1O".9 , "Z ..r   
stack_popsc                    UR                  5         / n[        5       nS[        SS4S jn[        UR                  5       GHN  u  pg[        UR                  5      U-
  U:*  n[        R                  R                  XxS9  [        U[        R                  5      (       d  [        R                  " XW5        U(       a  UR                  U5        M  [        U[        5      (       a  UR                  R                  U5        OUR                  U5        [        U[        5      (       d  M  UR                   c  SO[#        UR                   5      n	UR$                  R                  [        U5      S-
  U	45        UR&                  R                  U5        GMQ     [        U5      Ul        [+        UR-                  5       UR/                  5       -   5      n
UR0                  R3                  5        GHf  u  p[        R                  " X\5        [        UR4                  [6        5      (       a*  UR4                  R8                  U:X  a  XR:                  L a  Me  X;   a  Ml  [<        R>                  S:  a=  [@        RC                  [        U5      (       a  URD                  R                  U5        M  O![@        RC                  [        U5      (       a   e[        URF                  5      URF                  U'   [        U[        5      (       a@  UR                   c  SO[#        UR                   5      n	URH                  R                  X45        UR                  U5        GMi     X44$ )	a[  
Gets the stack + locals values belonging to tx that need to be restored.

Also prunes dead tx locals and realizes all VTs in the tx's stack.

NullVariables in stack/locals will NOT be restored, unless they are the top `stack_pops`
elements of the stack - it is expected that the next instruction to run will pop the top
`stack_pops` elements of the stack, so we should codegen NULLs.

Returns:
    - stack_values: stack and locals values that need to be restored
    - meta: locations of NULLs and ContextWrappingVariables in the stack/locals
        (ignores the top `stack_pops` values on the stack)
r  r   Nc                 l    [         R                  [        R                  U 5      (       a  [	        S5      eg )NzCAttempted to reconstruct WithExitFunctionVariable outside the stack)r;  __instancecheck__r0   WithExitFunctionVariabler:  )r  s    r   ctx_exit_check@OutputGraph._get_stack_values_to_restore.<locals>.ctx_exit_check  s1    %%i&H&H#NN$Y  Or   )allow_lazy_constantr   r,   )      )%prune_dead_localsrT  r~   r  r   r  r0   LazyVariableTrackerrealize_allr   r  visitr   r   rY  r|   target_valuesr_  r[  r\  rU  r   cellvarsfreevarsrv  r   r  r\   ry  r  sysversion_infor;  r  rZ  rX  r]  )r   r  r  stack_valuesrK  r  ir  r  r  cell_and_freevarsr   r   s                r   _get_stack_values_to_restore(OutputGraph._get_stack_values_to_restore  s   " 	"$	 	D 	 ""((+HA"%bhh-!"3z"A))55 6  eY%G%GHH%%n<"##E*%..%%,,Q/##E*%!899--5B5ATAT;U  ##**C,=,A=+QR))003/ ,2 \* => &&,,.DA!!.4 188[11HH''1,,,&%7* )),::))003 ;
  11,BBBB#&t'8'8#9Da !455//1BuQ__7M  $$++Q,>?"G /J !!r   r   c                    U R                   c   e[        R                  (       d  U R                   UL d   eU R                  5         X l        SU l        [        R                  SU5        / n[        R                  S:  a  U R                   R                   Hs  nUR                  S:X  a;  UR                  [        S[        U R                   R                  S   5      S95        MN  UR                  [         R                   " U5      5        Mu     / n/ nUnUb  [#        S UR$                   5       5      (       d   eU R'                  XUL a  UOS	5      u  pUR                  U	5        UR                  U
5        [)        UR$                  5       H  nUR+                  XR,                  S
9  M     UR.                  nUb  M  U R0                  R3                  U5        U R5                  U5        U R6                  (       a#  SSKJn  U R5                  U" 5       5        SU l        U R<                  (       a  U R>                  (       a   S5       eU R5                  U R<                  5        U RA                  U R                   5      u  pU R5                  U5        U RC                  5         U RD                  RG                  5        VVs0 s H  u  nnU[I        U5      _M     nnn[K        U5      nSSK&J'n  [        U RP                  5      S	:  a  / nU RS                  S5      U l*        U" [W        U RP                  5      SS9nU RY                  SU5      n[[        U R                   UUS9nUR]                  UR_                  US5      5        UR]                  [a        S	S5      5        UR                  URc                  U RT                  5      5        U R5                  U5        U VVs/ s H  nU  H  nUPM     M     nnnSnSnU R                   UL GaO  U(       GaG  [#        S U 5       5      (       Ga/  [#        S U 5       5      (       Ga  [        [e        U5      5      [        U5      :X  a  U R0                  Rg                  5       (       a  URh                  (       d  U Rj                  (       d  US   Rl                  (       d  US   Rn                  (       d  [[        U R                   5      nU Rq                  UU5        U R5                  / U Rs                  U[u        [)        U5      5      U5      QURw                  5       Q[y        S5      Q[        S[        U5      S9P5        GO.U RS                  S5      n[[        U R                   UUUS9nU R{                  UUUS5        0 nUR|                  RG                  5        H0  u  nn U S:  d  M  [        U[        [        45      (       a  M+  SUU'   M2     [[        U R                   UUUUS9n!U R{                  UUU!S5        [        R                  R                  R                  (       Gaa  U(       GaY  [        U5      S:X  GaI  US	   n"[        U"[        R                  R                  R                  5      (       Ga  U"R                  [        R                  R                  R                  L Ga  U"RF                  S	   n#U"RF                  S   n$[        U#[        R                  R                  R                  5      (       d   e0 n%U!R                  R                  5        HS  n&[        U&[        R                  R                  R                  5      (       d   eU&R                  n'U&R                  U%U''   MU     [        U#RF                  5       H  u  n(n"U"U%;   a   SU%U"   4U R                  R                  U('   M,  U"R                  bW  [        U"R                  SS5      =n)(       a9  [        U)SS5      (       a'  SU"R                  4U R                  R                  U('   M  U"R                  5       (       a+  SU"R                  5       4U R                  R                  U('   M  [        S U" S!U( 35      e    U$R                  5       U R                  lZ        / n+[        U R                  5      S	:w  d  [        U!R                  5      S	:w  a  U+R]                  U Rs                  UU!R                  5       U5      5        [        U!R                  5      S	:w  a#  U+R                  U!Rc                  U5      5        SnO+U+R                  [        S(5      5        OU R                  5         U R5                  U+U!Rw                  5       -   5        U R5                  [        S)[        U5      US	   R                  -
  S9/5        S	n,S	n-[        U5       GH  u  n.n
[        U
R                  5      n/U.S	:w  a  U/U
R                  -  n/U/S	:X  a&  U R5                  [        S)S	S9/[y        S5      Q5        O;U-U/-  n-U R5                  [        5       /[        U,U-5      Q[y        S5      Q5        U,U/-  n,U.[        U5      S-
  :X  d  M  [[        U R                   5      n00 n1U R                   R                  RG                  5        H  u  n2n3[        U3R                  [        5      (       d  M'  U3R                  R                  U2:X  d  MC  U0R                  U0R                  U25      5        [        U
R                  5      [        U15      -   U1U2'   M     U R5                  U0Rw                  5       [        S)[        U15      S9[        S*SS9/-   5        U
R                  R                  U15        GM     U R5                  [        S(5      [        S)[        U5      S9/[        US	   R                  S-   5      Q5        U(       a!  U(       a  U R5                  [        S+US,9/5        [        R                  R                  R                  S-;   ag  U R                  SS.9n4U4(       aQ  [        R                  R                  R                  S/:X  a  [        R                  " S0U4 35        U$ [        S0U4 35      eU$ s  snnf s  snnf ! [         a  n*[        S"S#U$ 3S$S%S&/U*S'9   Sn*A*GN-Sn*A*ff = f)1au  
Compiles the current subgraph, with inputs w.r.t. self.root_tx, and codegens:
    - Call the compiled subgraph
    - Apply side effects
    - Codegen stack and locals
    - Store the locals

Python does not allow NULL to be an arg to a function, so we do not codegen NULLs on the stack,
unless the value is one of the top `stack_pops` values on the stack (these values are expected to be
popped immediately after this generated code. The prologue of the resume function is expected to restore
any dropped NULLs.

Returns stack indices and locals keys where we dropped NULLs, and where we found inactive context manager objects.
NTzCOMPILING GRAPH due to %sr     COPY_FREE_VARSco_freevarsr  c              3   @   #    U  H  oR                  5       v   M     g 7fr   )can_restore)ry  blocks     r   r{  /OutputGraph.compile_subgraph.<locals>.<genexpr>Z  s     K8Ju((**8J   r   )is_graph_breakr,   )create_breakpointFz)export does not support pregraph_bytecodedisablerandom_valuesz.do not trace into Dynamo rng recovery functionr   __gen_rand_values)r  c              3      #    U  H^  n[        U[        [        [        45      (       + =(       a2    [        U[        5      =(       a    UR                  5       [        L (       + v   M`     g 7fr   )r   r   r   r   r   python_typefloat)ry  r   s     r   r{  r    sa       +A 3,4  V $A7TAMMOu<TUV +s   A&A(c              3   @   #    U  H  oR                  5       v   M     g 7fr   )	is_tensorry  r"  s     r   r{  r    s     =+<aKKMM+<r  r     UNPACK_SEQUENCE	graph_out)tempvarsr  r  is_inputinputconstantzEncountered unrecognized type z at output z8nested function with non-constructible closure in outputz as_python_constant for out_spec zCannot return a nested function with closure from a compiled function. Dynamo failed to construct the function defined in the compiled region with closure objects.zGDefine the function at module scope instead of inside another function z0Ensure that all closure variables are constants.)rB  rC  rD  rE  from_excPOP_TOP
BUILD_LISTLIST_EXTENDDELETE_FASTrs  )warnerrorr  r  zWhile compiling, we found certain side effects happened in the model.forward. Here are the list of potential sources you can double check: )pr  r-   nested_graph_breaksr4  compile_subgraph_reasonr  r   debugr  r  prefix_instsopnamer   r8   r  r  r  allblock_stackr  r  exitr   r  r  prune_dead_object_newadd_output_instructionsr  bytecode_transformationr  r  r5  r  cleanup_graphr   r   ru   r   
decoratorsr  r   rL  r  r   r8  r?   r  load_function_namer6   create_storer   is_emptydebug_localsr  rY  rZ  codegen_cellscompile_and_call_fx_graphr   r  r;   codegen_suffixusesrr   r`   r\   r   rN  log_graph_in_out_metadatar   r0   NamedTupleVariable	tuple_clsfunctional_exportExportTracerOutputListVariablegraph_outputsrw  codegenGraphOutputEntryvariabler|  r  rt  rc  r  r   is_python_constantr  r:  rd  r}   rE   rh   r   graph_output_varsrun_compiler_collectiverU  rX  r7   r3   rv  ry  append_outputcreate_loadupdater:   side_effect_replay_policyr  warningsr  r  )5r   r  r   r  r  install_stack_valuesall_stack_locals_metascur_txr  rK  r  r  r  r  r  r>  nn_modules_proxiesrootr  random_calls_instructionsrand_fnrand_fn_namer  valsr  stack_values_flatstored_graph_output_vargraph_output_varcell_cgpass1r  r  pass2r  flat_returnsrd  vt_to_graph_out_idxr  r  idxr  er  	start_idxend_idxr  n_valsroot_cgunmodified_locals_namesr   r   side_effect_refss5                                                        r   compile_subgraphOutputGraph.compile_subgraph   s   * ||'''))<<2%%% 	'')'-$		-v6 +-w&11;;"22 ''*, #DLL$=$=m$L M !''		$8 2 !#35 K8J8JKKKKK!%!B!Bl
"L ##L1"))$/ "&"4"45

62D2D
E 6 ]]F  " 	//3$$\2))B(():)<=-2D***t{{ 	
7	
; 	$$T%;%;<*.*N*NLL+
' 	$$[1 9=8M8M8O
8O94D/#&&8O 	 
 01' t  !A%(*%%)\\/%BD"'(9(9:GG  ../BGLLd7IG &,,**<> &,,-A!U-KL%,,$$T%;%;< (()BC& .>N-=T#SS-=N"' LLB!  +   =+<===C)*+s3D/EE!!**,,OO''*2.??*2.??  -Gr7+((33D*;!<=t
 --/ !^ ''8cBS>TU
  $||K8  #5	E $5ueD H#jj..0
U19VC2F1T%U%U$(HSM 1  !#5E $5udC $$>>>%)*a/&q)r5==#:#:#M#MNN}}66IIJ $&88A;L!xx{H%$emm&=&=&J&J    GI'!&!4!4!;!;!=)%1F1F1W1WXXXX49NN8=+H5 ">
 $-\-?-?#@R!44 + 3B 7LD00CCCH
 II1+2299fd+K!K!K '
E B B !( "		LD00CCCH  2244 * " 5 5 7LD00CCCH
 #1"@KPSu U# + $A08@8S8S8U,,5  F4::&!+s53F3F/G1/L222u7N7N7PRVW u**+q0MM%"4"45E"FG.2+MM"4Y"?@ ,,.((%2H2H2J)JK 	$$" -.1G1J1T1TT	
& 	 !78GAt**+FAv$..({,,*<Q?$Q 6!,,&(,Y@ %Q V#	 C./!33#DLL1:<' LL88>>@DAq!!((K88QXX=P=PTU=U--g.A.A!.DE589J9J5Kc3O 6/2 A ,,,,.*(c2I.J +=a@	 !!(()@A[ 9v 	$$"9-"<S9O5PQ 4Q7AAAEF	
"  7((#M:JKL ==99=NN#HH)-  I    ==''AAVKMMXXhWik &% 'XXhWik 
 &%O

Z Oz 2 %$^&Fxj$Q! !j R# &' s$   9rr2r 
s&r>>srs  c                 &   U R                   R                  (       a  SnUnUb  [        [        UR	                  5       5      5      nU H[  nX@R
                  L a"  UR                  UR                  U5      5        M3  UR                  (       d   eU" UR                  U   5        M]     UR                  [        [        U5      5      5        UR                  nUS-  nUb  M  UR                  [        SUS95        g UR                  [        SSS95        g )Nr   r,   r  r  )r  r   r_  sortedr  r  r  create_load_closurepost_prune_cell_and_freevarsr5   r  r  r8   )r   r  rs  tx_cntr  r  cells          r   r  OutputGraph.codegen_cells  s    ''33F79F$ !(@(@(B!CD$D-(()?)?)EF%BBBB6>>tDE %   !3CM!BC! $  /&IJ/!DEr   r  log_side_effectsc                 `  ^^	 U R                   R                  T5        U R                  (       a  U R                  (       a   eU R                  R	                  5        HW  u  pVT" U5        U R
                  c   eTR                  TR                  U R
                  5      5        TR                  U5        MY     [        R                  (       a  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     U R%                  UT5        TR'                  X!R                  (       + S9  U R                   R)                  TU5        g )Nc                     > T " T5      $ r   r   )rs  	debug_vars   r   rt  ,OutputGraph.codegen_suffix.<locals>.<lambda>  s	    R	]r   Fr  )value_from_source)r  codegen_save_tempvarsr  r5  r   r  r  r  
store_attrr-   replay_side_effectscodegen_hooksr  r}  extend_outputr6   r  r8   r  restore_stackcodegen_update_mutated)
r   r  r  rs  r  r  r  r   r  r  s
      `     @r   r  OutputGraph.codegen_suffix  sO    	//3{{"?!00668	3..:::  0G0G!HId#	 9
 %%++B/  "OIt233 1#d)UCD0;<=  / 	2r"
YYG005EFr   c                 :   U R                   (       d   e[        U R                  R                  5      nU H  nUR                  R                  SS5        M!     [        R                  " 5       n[        R                  " U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   r  rK  r   r   r  r  pairwiserC  rM  _set_grad_enabledr_  r   _erased
erase_node)r   r  r   r  node1node2s         r   r  OutputGraph.cleanup_graph	  s    TZZ%%&DIIMM.5  ,,.%..u5LE : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MMM#(::a=LJJ))%0JJ))%0 6r   c                   ^^ U R                   (       d  g[        R                  R                  R                  (       a*  [        R                  R
                  R                  ST5      e[        R                  ST5        [        R                  R                  SS UU4S jS9  U R                   R                  5         SU l         g)z5
Do not save this output graph to the CompilePackage
NzDetected a package bypass: %sartifactc                      SSS.$ )Nprecompile_cache_bypassjsonr  encodingr   r   r   r   rt  ,OutputGraph.bypass_package.<locals>.<lambda>6	  s    1"!r   c                     > ST0T E$ )N_reasonr   )r   r   s   r   rt  r1  :	  s    6   r   metadata_fn
payload_fn)r  r   rN  r-   strict_precompiler.   PackageErrorr   warning_loggingtrace_structuredbypass_current_entryr}  s    ``r   r~  OutputGraph.bypass_package)	  s     ||==11--##00/  	3V<'' 	( 	
 	))+r   c                 r   0 nU R                   R                   H  nUR                  R                  SS 5      n[	        U[
        R                  R                  5      (       d  MJ  UR                  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 )NrM  )r   r  rK  r  r   r   r  r   shaper'  reprr  )r   retr   rM  sizess         r   get_graph_sizes_structured&OutputGraph.get_graph_sizes_structuredC	  s    *,JJ$$D IIMM/4@M-):):)E)EFF$**PT!UPT1z!S'9'9!tAw"FPT!UII	 %
 
 "Vs   3,B4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                  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 =====
rM  z: r  FTz (concrete): )r   r  rK  r  r   r   r  r   r?  r  r_  r'  r   rj  r   hint)	r   r  graph_sizes_strr   rM  rB  concrete_size
has_symintszs	            r   get_graph_sizesOutputGraph.get_graph_sizesL	  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# %( r   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      r   restore_global_state OutputGraph.restore_global_statee	  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                 v  ^ U R                   nUc   eUR                  =mGbu  TR                  Gcf  TR                  n[        R                  STR                  5        [        R                  R                  SS U4S jS9  UR                  n[        U5      S:X  d%   SR                  SR                  U5      5      5       e[        UR                  5       5      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R0                  R3                  5         [4        R6                  eg g ! , (       d  f       NB= f! , (       d  f       NK= f)Nzcompiler_collective %sr+  c                      SSS.$ )Ncompiler_collectivestringr/  r   r   r   r   rt  5OutputGraph.run_compiler_collective.<locals>.<lambda>	  s    1 (%r   c                  8   > T R                   R                  5       $ r   )local_staterender)dss   r   rt  rY  	  s    2>>#8#8#:r   r4  r,   z&Expect only one device type but got {}+rW  Tr  )group)r  distributed_state
all_states
compile_pgr   infor[  r   r:  r;  _device_typesr  r  r  rA   r   rL  rankr  device_countrj   rB  distall_gather_objectspeculation_logr  r.    CompileCollectiveRestartAnalysis)r   r  rb  device_typesra  r]  s        @r   r  #OutputGraph.run_compiler_collectivew	  sp   \\~~&&&B38MJHH-r~~>NN++ ; ,  &33L|$) 8??@VW) ))9)9);<CCOO%(9(9(F(F(HH 2$O)-1B(B
&&z2>>T * P $$&666= 9N3( PO s$   F*!<FF*
F'	#F**
F8rvr~   c                    U R                   (       d  gSSKJn  U GH,  n[        XC5      (       a  UR                  (       d  M'  UR                  5       R                  R                  R                  S5      n[        U[        R                  R                  R                  5      (       d   eUR                  c  M  [        US4/5      nX`R                   -  (       d  M  UR                  (       a  [!        UR                  5      OUR"                  R                  R$                  nUR&                  R(                  R+                  U5        GM/     UR&                  R(                  (       a%  SUR&                  l        [.        R0                  " SS9eg)a:  
Validate that if torch.autograd.grad is used in the graph and outputs
require grad, we trigger AutogradGradRestartAnalysis only if the output is connected
to the autograd.grad computation.

rv here refers to list of variables that are being returned from dynamo graph.

See Note [Tracing autograd.grad in dynamo]
Nr,   TensorVariablerM  Tz3autograd.grad consumed grad_fns of returned tensorsrestart_reason)r  variables.tensorrp  r   requires_gradrM  r   rK  r  r   r  fake_tensorr   r   r   r  r   r?  r  ri  autograd_grad_leaked_tensorsr   graph_break_on_autograd_gradr.   AutogradGradRestartAnalysis)r   rm  r  rp  r  ru  reachable_grad_fnstensor_names           r   )_validate_outputs_safe_for_autograd_nodes5OutputGraph._validate_outputs_safe_for_autograd_nodes	  s$    334Cc22#:K:K,,.--2266GKk5+<+<+H+H+S+STTTT""* "<k4=P<Q!R!$H$HHH14c#**oATAT""??FF{S " ::>BB;11T  ;r   c                   ^ SSK Jn  [        5       mU R                   HQ  n[	        XC5      (       d  M  UR
                  (       a  M(  TR                  UR                  5       R                  5        MS     T(       d  gU R                  R                   HB  nUT;   a  M  [        U4S jUR                   5       5      (       d  M1  TR                  U5        MD     U H  n[	        Xc5      (       d  M  UR                  (       d  M(  UR                  5       R                  T;   d  MH  SnUR                  (       a  [        SSUSS	/S
9  Mk  SUR                   l        [$        R&                  " SS9e   g)ao  Skip frame if a source-less requires_grad_() intermediate leaks as output.

AOTAutograd's functionalization drops requires_grad_() on intermediates,
so returning them (or tensors derived from them) produces wrong results.
We detect this via FX graph reachability: find the requires_grad_() nodes
for source-less intermediates, then check if any output is downstream.
r,   ro  Nc              3   ,   >#    U  H	  oT;   v   M     g 7fr   r   )ry  inptainted_nodess     r   r{  HOutputGraph._check_requires_grad_intermediate_outputs.<locals>.<genexpr>	  s     H3GC-'3Gs   aK  An intermediate tensor that had requires_grad_() called on it (or a tensor derived from it) is being returned from the compiled region. AOTAutograd's functionalization drops the requires_grad_() effect on graph outputs, producing wrong results. If you only need the tensor values without gradients, call .detach() before returning.z,returning intermediate with requires_grad_()z4graph output depends on source-less requires_grad_()zVIf you only need the tensor values without gradients, call .detach() before returning.zConsume the gradient inside the compiled function (call backward() and use .grad), or move requires_grad_() outside torch.compile.rA  Tz:source-less requires_grad_() intermediate leaked as outputrq  )rs  rp  r   r  r   r  r   rM  r   r   r  anyall_input_nodesrt  r  rE   ri  graph_break_on_requires_grad_r.   RequiresGradRestartAnalysis)	r   rm  r  rp  r   r   r  r   r  s	           @r   )_check_requires_grad_intermediate_outputs5OutputGraph._check_requires_grad_intermediate_outputs	  s0    	5 -0E--A!,,QXXX!!!**,"3"34 .  JJ$$D}$H43G3GHHH!!$'	 % C3//%%%LLN''=8Q  <<! N V$'?N	 HLB&&D99'c ; r   r  c                 >  ^ ^"^#^$^%^&^' [         R                  R                  R                  5          SSKJn  T R                  (       d   eT R                  5         [        T R                  5      S:X  a  [        U5      S:X  a  / sSSS5        $ [        SSS9n[        U[        5      (       d   e[        U[        5      (       d   eT R                  X!5        T R!                  X!5        T R#                  SST R$                  R'                  [)        S	 U 5       5      5      40 5      nT R+                  5       nUR-                  U5        T R$                  R/                  X5        [0        R2                  (       d  [5        S
5         [7        U5       HO  n[9        X85      n	[        U	[:        R<                  5      (       d  M/  [?        U	T R@                  UT RB                  S9  MQ     T RE                  5         [?        [:        R<                  " UT R                  5      T R@                  UT RB                  S9  SSS5        T RG                  5         [        T R                  5      n
[H        S   S==   U
-  ss'   T RK                  5         T RL                  RO                  5         [Q        UT R                  5      m$SSK)J*n  U" T$5        T RV                  (       a)  T RV                   H  n[Y        T$U[9        X<5      5        M     T RZ                   H  nU" T$5        M     []        T$R_                  5       S5      b  T Ra                  ST$Rc                  SSSS9S9  T Rd                  b  UT$l3        T Rh                  T$l4        T Rj                  Rm                  5       T$Rn                  S'   T Rp                  T$Rn                  S'   UT$Rn                  S'   T Rr                  b  T Rr                  T$Rn                  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                  R                  nUc   eUT lB        T RB                  (       dY  SSKCJDs  Jn  UR                  SS9   [         R                  R                  UR@                  S9nSSS5        WT R                  lA        T R                  5          T R                  T$T R                  5       5      m#SSS5        SSKKJLn  [        T#U5      (       d,  [        [9        T#S S5      U5      (       a]  T#R                  S!:X  aM  [        T#U5      (       a  T#OT#R                  nUR                  U5        [        T#U5      (       d  UR                  m#T Rd                  b  T Rd                  R                  UT#5        T R                  (       a
  T#m%U%4S" jnUm#U" T#S#S$9m#[H        S   S%==   S-  ss'   UR@                  c   eUR@                  R                  =n(       Ga=  / m'0 m&T R                   Vs/ s H  nUR                  PM     nnU H  nUR                  UR                  5      n[        R                  " UR                  5      R                  5       n[        5       n[        R                  " UUR                  U/S5      n[        Rw                  S&U5        T'R                  [        R                  " U4S' jU5      U45        M     [         R                  R                  S#S$9S([        S)[        S*[        4U#U$U U&U'4S+ jj5       nT R                  UU5        OT R                  UT#5        T R                  c   e[        T R                  5      m"[        5       (       a  T"R                  U"4S, j5        / n[        R                  " 5        H=  nU" T"5        T R                  5       nT"R                  U5        UR                  U5        M?     U H#  nT"R                  T"R                  U5      5        M%     T"R                  [        [        5      S5        T"R                  5         [        T R                  5       H)  u  n n!U!R                  T R                  R                  U '   M+     T"R                  U5        T"R                  5       sSSS5        $ ! , (       d  f       GN= f! , (       d  f       GN:= f! , (       d  f       GN= fs  snf ! , (       d  f       g= f)-z
Generate code from self.graph and return the Instruction()s to
call that generated code.

Code is generated w.r.t. self.root_tx.
tx is only used for preserving GraphModule metadata
r,   r  r   N__compiled_fnT)	with_uuidr  c              3   @   #    U  H  oR                  5       v   M     g 7fr   )rM  r  s     r   r{  8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>*
  s     5OBqjjllBr  r)   )r5  statscalls_captured)dce_hop_extra_outputsz8Graph contains named parameters due to static addresses.Fprint_outputinclude_strideinclude_devicer   r  r  
backend_idr  %s)r  r  coloreddynamo_output_graphc                  (   > ST R                  5       0$ )Nsizes)rD  r   s   r   rt  7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>
  s    $"A"A"CDr   c                  &   > T R                  SSSS9$ )NFTr  )print_readabler  s   r   rt  r  
  s    2#4#4!&tD $5 $r   )r6  r  )r<  )_LazyGraphModule__self___lazy_forwardc                     > [         R                  R                  5          T" U 0 UD6sS S S 5        $ ! , (       d  f       g = fr   )r   rM  DisableTorchFunctionSubclass)r   r   real_compiled_fns     r   _tf_disabled_wrapperCOutputGraph.compile_and_call_fx_graph.<locals>._tf_disabled_wrapper
  s-    >>@/@@ A@@s	   2
A z"do not trace Dynamo-compiled graphr  unique_graphsz:Compiling backend specialized graph with specialization=%sc                     U" X   5      $ r   r   )r  r   check_fns      r   rt  r  
  s    X$(IF"r   r   r   r   c                    > T	 H  u  p#U" U 5      (       d  M  UT;   a  TU   " U 0 UD6s  $ TR                   R                  UR                  UR                  5         UTR                  S'   [        U 5      n[        TR                  5         TR                  TU5      TU'   S S S 5        S S S 5        TU   " U 0 UD6s  $    T" U 0 UD6$ ! , (       d  f       N-= f! , (       d  f       N6= f)Nspecialization)	r<  patch_source_specializationr  r  rK  r   r   r  call_user_compiler)
r   r   r  r  r   compiled_fnr   r   specialization_cachespecialization_guardss
        r   specialized_dispatchCOutputGraph.compile_and_call_fx_graph.<locals>.specialized_dispatch
  s    4I0#D>>-1EE';N'K%)("-3(" !" "&!K!K . 5 5~7N7N" =K(8 9?CDz%,T-A-A%B(,(?(?N(S %9$H &C" $8#G#XQW#XX% 5J& '777 &C%B" "s$   0CC!C
CC
C'	c                  l   > T R                  [        R                  R                  R                  S5      $ )Nstore_user_object_weakrefs)rr  r   rN  graph_bytecode_inputsr   )rs  s   r   rt  r     s&    B//;;DD4r   )vr   r3  r   clear_framer  r  r  r  rh   r   r  r=   r   r   r   r  r{  r  r  
create_argr_  
dedup_passr   _maybe_preserve_original_metar-   do_not_emit_runtime_assertsrj   dirr   r   r  r)   r<  r5  remove_unused_get_attr_nodesremove_unused_graphargsri   &remove_tensorify_specialized_graphargsr  r  r    dce_extra_outputsr  r  r   r  r  
parametersr~  r  r  _backend_idr  r  r  rK  r  r  graph_code_logr  rs   r:  r;  r  r  r  _old_fake_moder  rI  r  r  r  rS  r  r   torch.fx._lazy_graph_moduler  r   r  force_recompiler  add_backend_idr  specializationsrz  r  r|  inspect	getsourcer  stripRootGuardManagerr   LAMBDA_GUARDr   r   r   r  rN  r	   install_global_unsafer  r?   rG   r}  rH   rw  rL  r  r  r  r  pop_topr  rt  rb  make_call_generated_coder  )(r   r  rm  r  r  r  output_nodesub_gmsattrsubgraphncallsr  subgraph_nameregister_finalizerold_fake_moder  backend_fake_moder  lazy_gmr  r  asourcesr  source_indexcheck_fn_sourceunused_root_guard_managerr  r  tmp_varsconstructorvar_namer  r  rs  r  r   r  r  r  s(   `                                 @@@@@@r   r  %OutputGraph.compile_and_call_fx_graph
  s    ]]))557+####((*4::&!+B1 87 _=Db$''''dN3333
 ::2B ::2B**$$//5OB5O0OPR	K oo'G(==bN55 ""CD #D	#*4#6%h??; ( $ $'+{{	 !* 5573tTZZ8#{{	 E& ((* ,FW./69/779 !!'')#D$**5B@!"%
 66%)%L%LMBwt/KL &M '+&A&A""2& 'B BMMOT*6 ##N((%*4PT )  $  ||'!% *.)E)EB& 55::< GG67 ,0+A+ABGG'($(BGGL!((4262K2K./  &"T$PT NN++%D ,  ##% 00::M ,,,"/D;;99]]E]R ).(9(9(H(H"/"9"9 )I )% S& 2C$$.**,"55b$:M:M:OP - E+'7887;
DACSTT((O; "+/?@@  $--  !009!+/?@@")//K||'++D+> 33#. A 3!$HK Wo.!3. **666"/"9"9"I"III(*%SU$-1^^<^188^<&5N#*==1F1F#GL&-&7&78O8O&P&V&V&XO0@0B-%221&//()	 H IIT'
 *00%--;C !" !-	 +
# '6: &&.R&S8 8s 8s 8 8 T8. **41EF **4=<<+++4<<(B!!    #@#G#G#IKO  HHX&OOH- $J !)H$$R^^H%=> !)   %B!CUK

%dnn5SLOJJ$$DDSI 6 ''-&&(U	 87P EDt SR* -,h =W 87sq   AdC4d7c<A3c/Id
(c%2)d!c7<D6d2d	Jd
c"	d%
c4	/d7
d	d
dc                 4    U R                   R                  SS9$ )Nr  r  )r   
find_nodesr   s    r   placeholdersOutputGraph.placeholders<  s    zz$$$66r   c                 ^    U R                    Vs/ s H  oR                  S   PM     sn$ s  snf NrH  )r  rK  r   r   s     r   rz  OutputGraph.graphargs@  s)    262C2CD2C$		*%2CDDDs   *r   r   c           	      v    [        SSSSSSS9   U R                  X5      sS S S 5        $ ! , (       d  f       g = f)NOutputGraph.call_user_compilerbackend_compileTcompile_aot_autograd'aot_autograd_cumulative_compile_time_us)
phase_namer  log_waitcounterwaitcounter_name_overridedynamo_compile_column_us)rj   _call_user_compiler)r   r   r   s      r   r  OutputGraph.call_user_compilerD  s>     ,("& &<%N
 ++B?
 
 
s   *
8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 H6  n[        US5      (       a  M  UR                  S   nUR                  Ul	        M8     U R                  Ul        U R                  Ul        [        U R                  [         R"                  5      =(       d    U R                   n[%        U R                  [         R&                  5      n	U	(       a  [)        X5      n[        US5      (       a  UR*                  OSn
[         R,                  (       a  SS	KJn  U" U5      n [1        5       " [2        R4                  S
U
 35        [         R6                  (       a  [9        U5      nU" X5      n[1        5       " [2        R4                  SU
 35        [;        U5      (       d   S5       e []        SS0 U R^                  EU[a        UR                  R                  5      [a        U5      S.E5        W$ ! [<        [>        4 a    e [@         a  nU RB                  (       aD  [E        U R                   U[F        RH                  " 5       5      RK                  URL                  5      S e[O        UU RP                  RR                  SSU
 S[U        U5       SU RP                  RW                  5        3SU
 S[U        U5       S3S/S9   S nAGNS nAf[X         a    e [Z         aI  n[E        U R                   U[F        RH                  " 5       5      RK                  URL                  5      S eS nAff = f)Nr   r  call_methodcall_moduler,   r  _dynamo_sourcerH  r   z<unknown compiler_fn>)inline_invoke_subgraphz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.rA  dynamor  )op_count
node_countinput_count)1r   r   r  r  r   rq   r  rK  r  r  r  _param_name_to_sourcer  _source_to_user_stacksrJ   r  r-   debug_backend_overriderK   debug_inductor_config_overrider   r   r  5torch._higher_order_ops.passes.inline_invoke_subgraphr   r/   INFOr  r   callabler   r   rC   r  rB   r  currentframewith_traceback__traceback__rF   r  r  r   format_frame_summaryrD   r  r   r  r  )r   r   r   totr  r   plr  r   inductor_config_overrider  r  r  r  s                 r   r  OutputGraph._call_user_compilerQ  s#    +++HHNNDwwIIqww-'##D)	 #
 	3B2/00ggj) %(JJ!  $(#<#< $($>$>! 0&&(E(E   	 	 $O""F$I$I$
  $4K {J//   ( 	
 (( (+B	8N7<<+EdV)LM((,[9%b9KN7<<+B4&)IJK((O*OO(6 	,..!"((..1"<0			
 M /0@A 	0 	55+$$a)=)=)? .1t< '##4#D6c!fX^DLLLmLmLoKpq0nSVHLcdC	 	  	  	8'  !W%9%9%;nQ__-48	8s'   BI M?'B2LM?6AM::M?c                 n    [         R                  R                  R                  (       a  [	        U 5      $ 0 $ r   )r   rN  r-   use_graph_deduplicationrI   r   s    r   r  OutputGraph.dedup_pass  s%    ==77,T22Ir   sub_gmc                 h    [        XR                  SS9nX2l        SUl        U R	                  X#S S9  U$ )NT)requires_suffixFrG  )ro   r   r   torchdynamo_force_dynamicrn  )r   r  r  	next_names       r   rf  OutputGraph.install_subgraph  s:    'ootT	#+0( 	$$Vt$Dr   c                 Z    U R                    Vs/ s H  oR                  PM     nnU$ s  snf r   )rz  example)r   r  r  s      r   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   )r  r   r  r  r   usersr  r  s     r   r  (OutputGraph.remove_unused_get_attr_nodes  sJ    4::00J0?ND4

#$)  & Or   c                   ^ ^^^ T R                   (       d   eS[        R                  R                  S[        4S jnS[        R                  R                  S[        4S jmS[        R
                  S[        4U4S j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[        R
                  S[,        R.                  S -  4S jnS[        R
                  SS 4U 4S jjn[1        5       mS[0        [,        R.                     S[$        R2                  [$        R4                  -  SS 4S jm/ nT R6                   GH  nU" U5      S LnU(       a&  UR                  (       d  UR9                  U5        M9  M;  UR                  (       d,  [;        UR<                  S   [>        5      (       d
  U" U5        Mx  UR<                  S   n	[;        U	[>        5      (       a  M  [;        UR<                  S   R@                  [$        RB                  5      (       a  UR<                  S   R@                  n
UR<                  S   RD                  n[$        RF                  RH                  RK                  U
5      (       ds  [M        U
RO                  5       5      nU HT  n[Q        URR                  U5      n[T        RV                  " [$        R2                  [$        R4                  4UU4S jU5        MV     GM  [Y        [[        UR<                  S   R@                  5      5      (       a  GM  U	R\                  b  U	R\                  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R_                  U5        M3     g )Nb_noder   c                    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)NTFrM  )	r   r   NoderK  r  r   SymBoolr   maybe_as_bool)r   brs      r   is_static_true;OutputGraph.remove_unused_graphargs.<locals>.is_static_true  sw    ~fbgg..0AyDy1emm,,&&..00Q= r   r  c                     SSK Jn  [        U [        [        [
        45      (       a  g[        U [        R                  5      (       a%  [        U R                  R                  S5      U5      $ g)Nr   SymTypesTrM  F)
torch.fx.experimental.sym_noder+  r   r'  r  r   r   r"  rK  r  )r  r+  s     r   is_symnode_arg;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_arg  sJ    ?!c5$/00!RWW%%!!&&**_"=xHHr   r   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   r*  r  FrM  c              3   4   >#    U  H  nT" U5      v   M     g 7fr   r   ry  r  r-  s     r   r{  WOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s     <)Q~a(()   c              3   4   >#    U  H  nT" U5      v   M     g 7fr   r   r1  s     r   r{  r2    s     G2FQ~a((2Fr3  T)
r,  r+  r  r   rK  r  r  r   r   rw  )r   r+  r-  s     r   is_symnode_compute_nodeDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node  sp    ?ww/)diimmO<hGG <$))<<<G$++2D2D2FGGGr   r   )is_accessor_noder=  r  c                 
   U R                   S   nUR                  n[        U[        R                  5      (       aI  [        UR
                  R                  [        R                  5      (       a  UR
                  R                  $ g r  )	rK  r  r   r   rj  r   exprsympySymbol)r   r  r  s      r   placeholder_binds_symbolEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  s^    ))J'CkkG'5<<00Z!!5<<6 6 ||(((r   c                    > [         R                  SU R                  S   R                  R                  5        U R                  S	 TR                  U 5        TR                  R                  U S 5        g )NzREMOVE UNUSED GRAPHARG %srH  )r   r  rK  r  r  r  r  r   )r   r   s    r   remove_unused:OutputGraph.remove_unused_graphargs.<locals>.remove_unused%  sY    II1499Z3H3O3O3T3TU 		*%T"!!%%dD1r   used_symbolsfakec                      U [        U5      -  n g r   )r"   )rA  rB  s     r   update_used_symbols@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols/  s     L..Lr   rH  c                    > T" TU 5      $ r   r   )trD  rA  s    r   rt  5OutputGraph.remove_unused_graphargs.<locals>.<lambda>R  s    .A,PQ.Rr   )0r  r   r   Argumentr   r"  %torch.fx.experimental.symbolic_shapesr7  r  r   r   r  r  r  r  rC  operatorgetitemr   _checkr   r  r:  r;  r   rj  r   r  r   r   rK  rx   r  ScriptObjectexample_strong_refrW  rX  tracing_with_realr   __obj_flatten__r   wrapped_objpytreetree_map_onlyr   r;  ru  remove)r   r'  r5  r7  r   r<  r?  recheck_placeholdersbinds_symbolr  real_script_objr\  	flat_dictr  fake_attr_valrB  symbolr-  rD  rA  s   `                @@@r   r  #OutputGraph.remove_unused_graphargs  sq    	277#3#3 	 	&	bgg.. 	4 		"'' 	d 	" 	KT$**"2"234D4

#$)GGz)?2t{{hFVFV7V?2 KK5<<7*499Q<88.t44'--$$T* 5	277 	u||d7J 		2 	2D 	2 +.%	/ell+	/38<<%,,3N	/	/
  "%%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07$3$?$?1" !' 4 4%*\\5<<$@$R$1!"	 )2 !%d499Z+@+H+H&IJJ +.??+FCKK  (d;M &R )D-d3F!-!$' !''/ )r   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   )TensorifyStaterM  r  c              3   >   #    U  H  oR                   S :H  v   M     g7f)itemN)rC  )ry  us     r   r{  EOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>~  s     ?JqF*Js   )torch._dynamo.symbolic_convertr^  r   r  rK  r  r   r   	item_memor  r   _exprr  r  should_specializer  r   replace_all_uses_withr#   r  )r   r^  r   rM  ra  s        r   r  2OutputGraph.remove_tensorify_specialized_graphargsg  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' *   & %r   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  r  r  )r   r;  s     r   r  #OutputGraph.add_output_instructions  s     
 	  ''/r   r  r  c                   ^^^ TR                   (       aF  S[        [        R                  S4   S[        R                  4UUU4S jjnU R                  TU5        gU R                  T[        R                  " TTT5      5        g)a1  Install a resume function as a global.

When the code has freevars, installs a factory that creates the
function with correct globals and closure (since MAKE_FUNCTION
inherits the current frame's globals, which is wrong for resume
functions from inlined frames). Otherwise installs the function
directly.
closure.r   c                 8   > [         R                  " TTTS U 5      $ r   )typesFunctionType)rl  r  r  r  s    r   _make_fn<OutputGraph.install_resume_function_global.<locals>._make_fn  s     ))$	4wOOr   N)r  r_  rn  CellTypero  r  )r   r  r  r  rp  s    ``` r   install_resume_function_global*OutputGraph.install_resume_function_global  su     Pu~~s23P##P P
 &&tX6&&""4D9r   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   re   rx  r+  )r   r  r  s      r   r  !OutputGraph.install_global_unsafe  sO     11111""4([//0A0A4OPr   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)idr  r  r  r   r;  r  r  s       r   install_global_by_id OutputGraph.install_global_by_id  sG     2e9+R'89)))K""4/r   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  rz  s       r   r8  OutputGraph.install_global  s!      ""4/r   c                    S U l         U R                  R                  5         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                  5       5      U l        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         U R.                  R                  5         U R0                  R                  5         g r  )r  r   r  r  r  r   r  rK  r  r  r_  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  s     r   cleanupOutputGraph.cleanup  sz    //557$(!JJ$$DTYY&IIj) % 	##%  &&(8=5579
5 	!##))+##%$$**,""$##))+--335""$  &&($$**,  &&(!!#r   r  c                 :    U R                   R                  U5        g r   )r  r   )r   r  s     r   add_graph_finalizerOutputGraph.add_graph_finalizer  s     	##**+=>r   r   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  rH  r=  )r  rK  r  r   rC  r  s     r   example_value_from_input_node)OutputGraph.example_value_from_input_node  sH    77m#99Z(000ww*$$$t{{++r   inlined_modulec                   ^ ^^ [         R                  TR                  5      m[        TT R                  T R
                  5      mT R                  R                  T5        S[        SS 4UU U4S jjn[        US5      (       aX  [        UR                  5      (       a>  UR                  R                  [        L a!  UR                  5        H  u  pEU" U5        M     [        US5      (       a[  [        UR                  5      (       a@  UR                  R                  [        L a"  UR                  5        H  u  pEU" U5        M     g g g g )Nr_  r   c                    > TR                   c   eTR                  TU 5      nT SU  3nUTR                   U'   [        T[        5      (       a-  U TR                  [
        R                  UR                  5      '   g g r&  ra  rb  s      r   re  HOutputGraph.add_fqn_info_for_inlined_modules.<locals>.register_leaf_name  s    ,,888==fiPJq,H2<D%%h/&+..  55//
@ /r   rg  rh  )r  r7  r  ro   r  r+  r   r   r  r  rl  __func__!og_module_named_parameters_fn_ptrrm  og_module_named_buffers_fn_ptr)r   r  r  re  r_  r   r  s   ` `   @r    add_fqn_info_for_inlined_modules,OutputGraph.add_fqn_info_for_inlined_modules  s    **6;;7"$994;L;L
 	//33D9	# 	$ 	 	 >=118899"33<<45 %3$C$C$ELI&y1 %F>:..5566"009912 %3$@$@$BLI&y1 %C2 7 /r   )Ar  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r5  r7  rt  r  r  r  rs  r  r  r  r  r8  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  ru  r  r  r  r  Fr   )r:  r   )r   r  )r  r   r   N)r   r  )r   r   )tmp)r   r  )r   r   r   r   r   rQ   r   r   r   r	   r2   r   r   r   rF  r   r   r   rG  rH  r   r   r  r   rT   r  r  r  r4  r  r~   r_  r   Proxyr>  r=  r  r  r   r  r  r  rR  rI  r  r  r  r  r'  r  Graphr   setterr  r"  r   r  r:  r;  r   r  r  r  r  r  contextmanagerr(   r   r  r  r  r  r  r%   r<  r3  rP  r   r   rQ  rA  r  r  r  r  r  rh   r  r#  r   r   r(  rL  r0  staticmethodr7  rA  rn  r<   r  rT  r  r   r
  r?   r  r  r  r~  rD  rL  rS  r  r{  r  r   r  r  ry   rz  r  r1   r  r  r  rf  r   r  r  r  r  rn  rs  r  r{  r8  r  r  r  r  r   r   r   r   r  r  K  st
      TU38nTU  $&TU -	TU
 TU %%67TU TU TU TU TU $((I(I#JTU *+TU TU 
TUn 385V+/5V	c5VnF # * >>"%>	>* 8DD3 D
 4:5#5-05	sEHHNN"	#5)%((.. )*<:T#Y=M :B >B	$S#X$ CHo$  s 23d:	$
 
$L&8BG#4 & &#
      & & - 	 F	I  C  D  . )uxx~~ ) ) \\*588>> *d * * 7T#rxx-%8 7 7 4$rww'<"= 4 4 1tELL%8S2T$TU 1 1A# A A A@ @ @ @@ @ @ @ 
 #'	} /0 4Z	
 
/	0 :   .5,,;; . . 88 8 8
 A// A A >DcN > > FD)C)C$D F F HL'
U8CH#5t#;<<=D'
	'
R$& N N
6 
c 
'S '0 0JT J# %((//C*? C C 5C 5D 5
    .,/	&c%,,.4c c 	c
 
cJY/-Y/	tK $vv~"66	7Y/vh"-h";>h"	tO$&99	:h"\ 	l&'l& #l& 	l&
 
!	"l&\F ; F Ft F2"G'"G ?+"G 	"G
 "G 
"GH1<S   4Dd39o1E,F C C 2 c  "!7F'()'/J'	'R>()>/J>	>@w)'w) !w) 	w)
 
k	w)r	 7d277m 7 7 E4> E E@..@26v,@	@a..a26v,a	aFDehh&:&:!:; S %((2F2F 3 U\\ 2 
'
X0t 'D d;.?  D   nn S>	
 
8
Q# 
Qc 
Qd 
Q3 s s 	S 	 	 	$:?"*BNN+;T+A"B?	?
,%((-- ,C ,$2#hhoo$27=$2	$2r   r  c                       \ rS rSr% \\S'   \\S'   \S-  \S'   \S-  \S'   \\S4   S-  \S'   \	\
\4   \S	'    SS
SS\S-  SS4S jjrSS jrSrg)DynamoTracerOutputi#  error_on_graph_breakis_tracing_resume_prologueNoutput_graphoutput_graph_for_cleanup.rl  r  r  r   r  r   c                     UR                   U l         UR                  U l        UR                  U l        UR                  U l        UR                  U l        U(       a  S U l        g UR                  U l        g r   )r  r  rl  r  r  r  r  )r   r  r  s      r   r   DynamoTracerOutput.__init__-  s[     %+$?$?!*0*K*K'~~))(.% $D &Dr   c                 T   U R                   nU(       a  UR                   H  nUR                  R                  5         M     UR                  R
                  (       aL  UR                  R
                  R                  (       a&  S UR                  R
                  R                  l        g g g g r   )r  r  r   _clear_nodesr  r  r<  r  )r   r  r  s      r   _cleanup_output_graph(DynamoTracerOutput._cleanup_output_graph:  s    44&..))+ / ,,66 00::DDQU,,66@@N E 7 r   )rl  r  r  r  r  r  r   r   )r   r   r   r   r   r   r  r_  r	   r   r   r   r  r   r   r   r   r  r  #  su     $$$$ *D0038_t##CH~ HL.1.:=*.	.
Vr   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.htmlr  kindrC  r   r   c                   ^  US:w  a  g S[         R                  R                  SS 4U 4S jjnS[         R                  R                  S[        SS 4U 4S j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)Nr  rC  r   c                 ^   > U R                   S;   a  g TR                  R                  U 5        g )N>   atenprimprims)	namespacer  r   )rC  r  s    r   encountered_compliant_op8check_pt2_compliant_op.<locals>.encountered_compliant_opV  s)    88))--f5r   r   c                    > TR                   R                  U 5        [        R                  (       a  [	        SSUS-   [
        -   / S9  g g )Nz Encountered non-PT2-compliant opr   rA  )r  r   r-   only_allow_pt2_compliant_opsrE   err_epilogue)rC  r   r  s     r   encountered_non_compliant_op<check_pt2_compliant_op.<locals>.encountered_non_compliant_op[  sB    &&**62..:#I4	 /r   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 packetr  rA  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r   _ops
OpOverloadr   r   Tagpt2_compliant_tagtagsOpOverloadPacketr_  	overloadsr  r   rN  rf  get_fake_values_from_nodesr  rM  _jit_resolve_packet_qualified_op_namer  rE   )r  r  rC  r   r   r  r  r  r  overloadr  s   `          r   check_pt2_compliant_opr  P  s    6)>)> 64 6
UZZ-B-B  QU  &%**//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   .H 
H=H88H=PRc            
       r    \ rS rSrSSS\\\4   S\R                  S\R                  SS4
S	 jr	S\
4S
 jrSrg)r  i  r  r  r   r   r   r   Nc                 4    Xl         X l        X0l        X@l        g r   )r  r   r   r   )r   r  r   r   r   s        r   r   LazyProxy.__init__  s     	r   c                 N    U R                   " U R                  0 U R                  D6$ r   )r   r   r   r   s    r   r  LazyProxy.__call__  s    ww		1T[[11r   )r   r   r   r  )r   r   r   r   r   r  r  r   r   r   r	   r  r   r   r   r   r  r    sS      QTN vv	
 (( 
2# 2r   r  c                     ^  \ rS rSrSr    S.SSS\S    S\S\S-  S	\S-  S
S4U 4S jjjr	S\
S
S4S jrSSS\R                  S
S4S jr   S/S\S\S\S\S\S-  S\S-  S\\R                  /\R"                  4   S-  S
\R"                  4S jjr   S/S\S\S\S\S\S-  S\S-  S\\R                  /\R"                  4   S-  S
\R"                  4U 4S jjjr    S0S\S\S\S\S\S-  S\S-  S
\R                  4U 4S jjjrS\R                  S
S4S jr  S1S\S\S\S\S\S-  S
\R"                  4S  jjrS!\R"                  S
\\R"                  -  4S" jrS#\S
\4S$ jrS\S%\\R                  R"                  -  S
S4S& jrS\R:                  \R<                  -  S'\S-  S
S4S( jrS)\R:                  S
\ \!RD                     4S* jr#S
\$4S+ jr%S
\&4S, jr'S-r(U =r)$ )2r  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.
Nr  r  r  r  r  r  r   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        XPl        0 U l        0 U l        0 U l        S U l        SU l        SU l        SU l        S U l        [/        5       U l        SU l        Ub  UR4                  S-   OSU l        S U l        S U l        S U l        S U l        U R                  c  / U l        O:U R                  R>                  U R                  RA                  U5      U4/-   U l        [/        5       U l!        / U l"        [
        RF                  " 5       (       a  [I        S5      e[/        5       U l%        g )NFr,   r   zSInference mode is supposed to be disabled during compilation. Please open an issue.)&r   r   weakrefr?  r  r   r   r  r   r  r  r  r  r  r  lifted_freevarsr  dynamic_scalar_nodes	prev_inst,unsafe_allow_externally_visible_side_effects+traced_with_externally_visible_side_effectsallow_side_effects_in_hopside_effect_stackr*   traced_sourcesis_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str_used_namesr  is_inference_mode_enabledr  tracked_proxyable_vt)r   r  r  r  r  r  r   s         r   r   SubgraphTracer.__init__  s    	#MM,7XX^^%
 # 9; =? *& :< NP >@!
 =B9;@8 */& AE 3=, ,1(:@:L 2 2Q 6RS/3AE 04!
 ;;.0D #';;#>#>**=9=IB $D 
 -7L 8:)**,,e  BL!r   r  c                 :    U R                   R                  U5        g r   )r  r   )r   r  s     r   record_proxyable_vt"SubgraphTracer.record_proxyable_vt"  s    !!%%b)r   r  r   r   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_FIELDSrK  )r   r  r   linenonode_idxrK  r   s          r   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, ) $ * ) r   r  rC  r   r   r  	type_exprproxy_factory_fnc           	      \   [         R                  " 5       n U R                  XX4XVU5      U R                  R                  =R
                  [         R                  " 5       U-
  -  sl        $ ! U R                  R                  =R
                  [         R                  " 5       U-
  -  sl        f = fr   )r  r  _create_proxyr  r  r  )	r   r  rC  r   r   r  r  r  _t0s	            r   r  SubgraphTracer.create_proxy<  s     lln	%%dD=M 66FF$FD66FF$Fs   A* *AB+c           	        >^^^^^ 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UUUUU5      mU R                  R                  n[        R                  S:  a  US;   a  UR                  mTU R                  La  TR                  b}  TR                  R                  bf  UR                   mUR#                  TR                  R                  S9mS[$        4UUUU4S jjn[&        R)                  S[+        U5      5        TU l        SnUR                   U R,                  La  [.        R0                  " UR                   5      R3                  SS	 5      " 5       n[5        U[6        R8                  R:                  5      (       ao  S
nUR<                  R>                   Vs/ s H  nUR@                  PM     snU l!        URD                  U l#        URH                  RJ                  RL                  U l'        OS U l!        S U l#        S U l'        URP                  nU(       a'  URS                  5       TRT                  R@                  S'   US;   a  TRT                  RV                  T4nU(       a  [Y        TRT                  R@                  S   R[                  5       5      S   S   nUR\                  R_                  S5      (       a8  UR\                  R_                  S5      (       d  TRT                  RV                  U4nU R`                  U/-   TRT                  R@                  S'   OUS:X  a  U R                   b  [c        SSU Rd                   3S/ S9  U R`                  TRT                  RV                  [g        U4S jTRT                  R@                  S   Ri                  5        5       5      4/-   TRT                  R@                  S'   U Rk                  UTRT                  5        U(       Gd+  STRT                  R@                  ;  a:  URP                  nU(       a'  URS                  5       TRT                  R@                  S'   STRT                  R@                  ;  a  US;   a>  U R`                  TRT                  RV                  T4/-   TRT                  R@                  S'   OyUS:X  as  U R                   b  [c        SSS/ S9  U R`                  TRT                  RV                  TRT                  R@                  S   T   S   4/-   TRT                  R@                  S'   STRT                  R@                  ;  a  / nU(       aJ  URm                  5       (       d  UR	                  URo                  5       5        [q        USS 5      nU(       a  MJ  U Vs/ s H  nURr                  [u        5       ;  d  M  UPM!     nnURw                  5         [x        Rz                  R}                  U5      R                  5       nSR                  U5      TRT                  lA        [6        R                  R                  R                  (       d)  [6        R                  R                  R                  (       aD  U R                  R                  R                  U R                  R                  TRT                  5        T$ s  snf s  snf )Nr  r  )r  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  )rl   rstripr   r  )linecur_instheaderrm  tx_codes    r   get_trace_call_log_str<SubgraphTracer._create_proxy.<locals>.get_trace_call_log_str  s9    5gxHOOQD+BGGLL>xr$PPr   r  Forig_graphmodulec                      g r   r   r   r   r   rt  .SubgraphTracer._create_proxy.<locals>.<lambda>  s    Dr   Tnn_module_stack>   r  r  r  r,   )ztorch.nn.modulesz	torch.ao.ztorch.nn.modules.containerr  r  z4Invoking an nn.Module inside a higher order operatorzHigher order op name: zThis is not supported.rA  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  )ry  r   r   tyrC  s       r   r{  /SubgraphTracer._create_proxy.<locals>.<genexpr>  s4      *QJAw773<?f4 *Qs   !1	1z2Invoking an nn.Module inside a HigherOrderOperatorr  r  r  )Hr  rS  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r  r  r  r  r  r  r  	positionsr  r  get_line_of_code_headerr   trace_call_logr  rt   r  r>   get_contextr  r   r   r   r  r   r  rK  r  _lineno_mapr  r  __code__r  r  r  r  r   r  r   rw  r   
startswithr  rE   r  r  r   r  is_co_filename_from_nn_modulesframe_summaryr   r  r'   r  r   StackSummary	from_listr  r  r  rN  r-   r  track_nodes_for_deduplicationr  
track_node)r   r  rC  r   r   r  r  r  	flat_args	tree_specnew_flat_argsr  maybe_new_argr  r  is_retracingorig_graphmodule_maybendr  r   current_nn_moduleframe_summariesframefiltered_frame_summariesmsgsr  r  rm  r  r   s     `                      @@@@r   r  SubgraphTracer._create_proxyP  s   V ;;"#)#6#6~#F IM  $ H H M$$]3 ! "00JLDW!
 )) w&4 4
 ,

 --H.&&2&&--9))338;M;M;T;T3UQ 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WW\\6*E %)6G)H)O)O)Q$RSU$V%! %//::5 +66AA0   WW\\+<=E.2.B.BeW.LBGGLL*+]"{{&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 " -(,E>>)<)>> , % ( %,,. ))334LMTTVD"$''$-BGG MM  88}}##AA,,77!!,,bgg 	[&r(s   0[-[20[2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 R                  R                  U	R                   5        U	$ )Nz2create_node using arg not from this SubgraphTracerr"  )r  r  r  rS  arg_tree_leavesr   r   r   r"  r   r   r  r  rK  r  r   r  )r   r  rC  r   r   r  r  r  r  r   r   s             r   r  SubgraphTracer.create_node  s     	t00$M;;"..??I !#uxx}}55yyDJJ. H. ! w"4tO*.*;*;*E*E		&'TYY'r   c                    [        UR                  5      S:  a  / nUR                   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  )r  r  r   r  r  r   r  r&  r  r   r  )r   r   user_graph_nodesuserother_graph_nodes        r   r  SubgraphTracer.remove_node8  s    tzz?Q46

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

d#  $$TYY5r   rM  beforer  c           	         [        U[        R                  5      (       a%  U R                  R	                  UR
                  5        [        R                  SUUb  UR                  OSUU R                  U5        Uc  U R                  c   SU SU S35       eU R                  (       ai  U R                  c\  Uc   e[        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 R0                  R3                  U5      nO7U R0                  R5                  U5      nOU R0                  R3                  S 5      nU   U R7                  SUS	0 US
9n	[9        U	R.                  U5        U R(                  (       a@  U(       a9  U R(                  R;                  5       u  pXR(                  U'   XR(                  U
'   OXR(                  U'   U R&                  R=                  U5        U R                  n[        R>                  RA                  5       nU(       d  U(       d  [        U[        R                  5      (       a  U RC                  X55        Ov[        U[D        [F        45      (       a[  [I        U5       HL  u  p[        U[        R                  5      (       d  M&  S nU(       a  [K        UUSS9nU RC                  UU5        MN     [        U[        RL                  5      (       aU  [        UR.                  RN                  [P        RR                  5      (       a"  XRT                  UR.                  RN                  '   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  r   r  F)r  r|  index_is_slice)+r   r   r   r  r   r  r   r  r  r  r  r  r[   r  r  r  r   r  ro   r  r  r  r  r   r   inserting_beforeinserting_afterr  rw   popitemr   compileris_compiling_lift_basic_symbolsr   r_  r  rX   rj  r9  r:  r;  r  )r   r  r  rM  r$  r  	prev_namer   ctxr?  r   r   is_strict_exportis_non_strict_exportr  r  e_sources                    r   rJ  !SubgraphTracer.create_graph_inputN  s    mU\\22--44]5K5KL		E!-FKK8	
 >;;* B4&VcUddwx* >>dkk1%%%'F!!77BB62NUU"002 #4)9)9:##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((.
   &@  $~~#(>>#>#>#@ #,@mU\\::,,]Ce}== )- 8)!U\\::$#'!'4%+&'/4(H 00H= !9 -66:""''< < ?D""=#5#5#:#:;M SSs   	G8O
Or?  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[        5      (       a  [        UR                  5      O
[        U5      nU R                  UR                  R                  X25      nX@R                  U'   U$ )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerrM  )r  r   rK  r   r   rj  r9  r  r  r  lift_tracked_freevar_to_inputr   r;  real_objrJ  r  )r   r?  rM  r  	new_proxys        r   r6  ,SubgraphTracer.lift_tracked_freevar_to_input  s.    {{& 	
W	
& 

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

8 -)9:: ''(m$ 	
 ++EJJOOYV	&/U#r   r  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   )ry  sub_argr   s     r   r{  ESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>  s&      'FG @@II'Fs   !)
r   r   r   r  slicestartstopstepr  r6  )r   r  s   ` r   r   2SubgraphTracer.maybe_lift_tracked_freevar_to_input  s     #uxx~~..
 #u%%(+		388SXX'F  
ZZ4J11#66r   e_proxyc                   ^ ^^ TR                   m[        T[        5      (       d   eS[        S[        4U 4S jjnS[        S[        S[        S[
        R                  4UU4S jjn[        U[        R                  5      (       Ga  [        UR                  5       5       H  u  pVU" U5      (       d  M  [        R                  SUTUTR                  5        [        TUUS	[        R                  R                   R"                  R$                  TU40 ['        U5      S
9nT R)                  Xg5        M     UR+                  5       nU" U5      (       ao  [        R                  SUTTR                  5        [        TUUS	[        R                  R                   R,                  T40 ['        U5      S
9nT R)                  X5        UR.                  [        R0                  L a  [        UR3                  5       5       H  u  pVU" U5      (       d  M  [        R                  SUTUTR                  5        [        TUUS	[        R                  R                   R4                  R$                  TU40 ['        U5      S
9nT R)                  Xg5        M     GO;UR.                  [        R6                  L aA  T R)                  UR9                  5       T5        T R)                  UR;                  5       T5        OUR.                  [        R<                  [        R>                  1;   aA  T R)                  URA                  5       T5        T R)                  URC                  5       T5        OnUR.                  [        RD                  [        RF                  1;   a@  T R)                  URI                  5       T5        T R)                  URK                  5       T5        [M        U5      (       aC  URO                  5       u  pU	 H*  n[Q        X5      nT R)                  U[Q        TU5      5        M,     g g [        U[        RR                  5      (       a4  U" U5      (       a&  URT                  RV                  nTTRX                  U'   g g g )NrC  r   c                    > SSK Jn  U" U 5      =(       aX    [        U R                  R                  [
        R                  5      =(       a#    U R                  R                  TR                  ;  $ )Nr   )r$   )rJ  r$   r   r   r9  r:  r;  r  )rC  r$   r   s     r   	need_bind8SubgraphTracer.track_produced_symints.<locals>.need_bind)  sJ    I A :qvv{{ELL9:FFKKt'9'99r   rM  r   r   c                 j  > [        T[        5      (       a  T" 5       OTm[        T[        R                  R                  5      (       d   eTR
                  R                  TR                  5         TR                  " U0 UD6n[        UR                  U 5        UsS S S 5        $ ! , (       d  f       g = fr   )
r   r  r   r   r  r   r*  r   r  rw   )rM  r   r   r?  rC  r  s       r   _proxy_with_example_valueHSubgraphTracer.track_produced_symints.<locals>._proxy_with_example_value2  s}    
 $.gy#A#AgiwGguxx~~6666--gll;++T<V<!%**m< <;;s   0*B$$
B2z=track_produced_symints %s for %s.size()[%s] at debug_level %sr  r'  zCtrack_produced_symints %s for %s.storage_offset() at debug_level %sz?track_produced_symints %s for %s.stride()[%s] at debug_level %s)-r  r   r  r	   r   r   r  r   r   r  rB  r   r  r  r  opsr  sym_sizer'  r;  track_produced_symintsstorage_offsetsym_storage_offset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   rj  r   r9  r  )r   rM  rC  rF  rI  r  rC  
lazy_proxyrN  attrsr0  r  inner_tr9  r  s   ` `           @r   rM  %SubgraphTracer.track_produced_symints  s     &.1111	 	 	
	
	'*
	69
	XX
	 
	 mU\\22!-"4"4"67Q<<IIW** "+1'		//33 !"&q'	"J //>' 8* +99;N((		Y"&&	 '-"#IINN55J">2	
 ++NG##u}}4%m&:&:&<=DA ||		]#".. &/"5+!IINN5599$aL&*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* ( 5r   rz  c           	        ^   SS[         [        R                  -  S[        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                  [        R,                  [        R.                  1;   aA  T R'                  UR1                  5       U5        T R'                  UR3                  5       U5        OnUR                  [        R4                  [        R6                  1;   a@  T R'                  UR9                  5       U5        T R'                  UR;                  5       U5        [=        U5      (       aF  UR?                  5       u  pgU H-  n[A        X5      n	T R'                  Xb  [C        X(5      OS 5        M/     g g [        U[        R                  5      (       a
  U" UU5        g g )	NrC  r  r$  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                   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                   OSTR"                  5        [+        UU SS SS9UR                  R                  S'   g )Nr   rM  )r$  r  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_tensorru  r  rH  )r$   r   r   rj  lookup_unbound_symbolsr  r  r.  r  r   rK  rJ  r   r;  r   r  r  r  r  r  iterry   )	rC  r  r$  self_to_be_bounds0parent_proxyexample_valphr   s	           r   _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'-'9?P((	 :<D((6# +( +,1 HHXGYY]^_]`b1 ) !! %WWXVYYjl)
 $/01,,GG!! -  		J#)#5FKK;L$$	 ,4', $#,Z(r   T)r$  r  )"r'  r   rj  r   r   r   r   r  rB  rb   ra   SIZErP  rQ  rR  STRIDErN  STORAGE_OFFSETrT  r.  rU  rV  rW  rX  rY  rZ  r[  r\  r]  r^  r+   r_  r   rT   )
r   rM  rz  rn  r  rC  ra  r0  r  rb  s
   `         r   r.  "SubgraphTracer._lift_basic_symbols  s    !B	U\\!B	TM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# 5r   rC  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   )r  )rC  s    r   rt  7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>'  s    r   )r$  )r   r9  r"   r  r  r   r   r  r   r   r  r  r  )r   rC  r"   to_be_boundrj  r?  s         r   rg  %SubgraphTracer.lookup_unbound_symbols  s    vv{{//|!IB+++""2&&&r*E%++).""2&eUXX^^449M &rd*LMM  k'788r   c                 (   U R                   n/ n/ nU R                  R                   Hq  nUR                  S:X  a^  UR                  S   n[        U[        R                  5      (       a.  UR                  UR                  5        UR                  U5        Mo  Mq    O   [        [        X5      5       VVVs/ s H  u  nu  pxXx:w  d  M  UPM     n	nnnU	(       a%  U	 Vs/ s H  obU   PM	     n
nSU
 3n[        SU5      $ [        SS5      $ s  snnnf s  snf )Nr  rM  zInput mutation detected at TFr  )r  r   r  r  rK  r   r   r   r   r  r  r  r   )r   input_versions_at_beginninginput_nodesinput_versions_at_endr   rM  r  v1v2mutated_inputsmutated_nodesr   s               r   has_input_mutation!SubgraphTracer.has_input_mutation)  s   &*&G&G# "JJ$$Dww-' $		/ :mU\\::)001G1GH&&t, ;  %  )/G 
 8B x	   	 
 5CD^^^MD/?Cc**E2&&
 Es   4DDDc           	         SSK Jn  SSKJn  [	        5       nU R
                  R                   Ht  nUR                  S:X  aa  U" U/5      S   n[        U[        R                  5      (       a4  U" U5       H&  nXc;   a  SX6    SU 3n[        SU5      s  s  $ XCU'   M(     Mr  Mt    O   [	        5       nU R
                  R                  SS	9S   n	[        R                  " U	R                  S   5       H  n
U
(       d  M  U" U
/5      S   n[        U[         5      (       a   e[        U[        R                  5      (       d  MP  U" U5       H&  nXh;   a  S
X    SU
 3n[        SU5      s  s  $ XU'   M(     M     UR#                  5       UR#                  5       -  n[%        U5      S:  aT  U Vs/ s H  oU   X   4PM     nnSR'                  U VVs/ s H  u  pU SU 3PM     snn5      nSU 3n[        SU5      $ [        SS5      $ s  snf s  snnf )Nr   )get_tensor_storages)_collect_fake_inputsr  z*Input-to-input aliasing detected at nodes z and Tr  r  z,Output-to-output aliasing detected at nodes z, z+Input-to-output aliasing detected at nodes Fr  )(torch._dynamo.variables.higher_order_opsr  torch._higher_order_ops.utilsr  r   r   r  r  r   r   r   r   r  rS  tree_leavesr   r   r  r  r  )r   r  r  input_storagesr   rM  storager   output_storages	out_nodesout_nodeintersected_storagesrC  aliasedr  os                   r   r   SubgraphTracer.has_aliasingF  s   PF>BfJJ$$Dww-' 4dV <Q ?mU\\::#6}#E"4$N~OfNgglmqlr"sC#/c#::26w/ $F ;  % @DvJJ))X)6q9	**9>>!+<=Hx 4hZ @ C%mT::::mU\\::#6}#E"5$PQ`QiPjjopxoy"zC#/c#::3;0 $F >  .2247K7K7MM#$q( BVAUA"O$67AU   iiG DGDAA3eA3G DEG?yICc**E2&& !Es   (HH	
)r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )NFNN)NNNr  )FN)*r   r   r   r   r   r   r   r(   r   r   r~   r  r   r"  r  r	   r   r  r  r  r  r  r   rJ  r  r6  r   r   rM  rj  r   r.  r   r:  r;  rg  r   r  r   r   r   r   r   s   @r   r  r    sq    .2'+"&eN#eN )*eN 	eN
 }eN 4ZeN 
eN eNN*o *$ *C-C57WWC	C8   $AE  	
  Dj : #BGG9bhh#67$> 
4   $AELL L 	L
 L DjL :L #BGG9bhh#67$>L 
L Ld  $  	
  Dj : 
 46 6D 66  $zz z 	z
 z z 
zz)288 )	BHH@T )V7s 7s 7@y5 y5+4uxx~~+Ey5	y5x~"\\ELL8~?E}~	~D9 9ell9K 9*'L ':+'l +' +'r   r  r   (  r   r  r  r  r  r   r  r  r/   rK  r5  r  r  r   rn  r  r  collections.abcr   r   r   r   r   r^  r   typingr	   r
   r   r   r   typing_extensionsr   r   r:  torch._guardsr   torch._loggingtorch.distributeddistributedrg  torch.nntorch.utils._pytreerf  rg  rS  r   r   torch._C._dynamor   torch._dynamo.excr   r   r   r   r   r   r   r   "torch._library.fake_class_registryr   torch._library.opaque_objectr   torch._subclasses.fake_tensorr   torch._utils_internalr   torch.export.dynamic_shapesr   r  r    %torch.fx.experimental._backward_stater!   rJ  r"   r#   r$   r%   r&   r'   torch.fx.noder(   torch.fx.passes.runtime_assertr)   torch.utils._ordered_setr*   torch.utils._python_dispatchr+   r  r-   r.   r   r0   backends.registryr1   r2   r  r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r  r?   current_scope_idr@   device_interfacerA   rB   rC   rD   rE   rF   r  rG   rH   graph_deduplicationrI   graph_id_filterrJ   rK   graph_region_trackerrL   rM   rN   mutation_guardrO   r  rP   rQ   rR   r  rS   rT   rU   rV   rW   rX   rY   rZ   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.builderrx   ry   rz   r{   variables.ctx_managerr|   variables.functionsr}   r~   variables.listsr   variables.miscr   variables.nn_moduler   rs  r   r   r   variables.torch_functionr   variables.user_definedr   r  r   torch._dynamo.packager   rc  r   torch._inductorr    torch.multiprocessing.reductionsr   	getLoggerr   r   r:  getArtifactLoggergraph_tabular_logr  graph_sizes_logr  r  r   r   rm  r  rl  r  r   r   r   r   r   r   r_  r   r  r   r"  r   cacher   r   r   r   r   objectrF  r  r)  rT  ra  ro  rq  r  r  r  r  r  r  r  r  r  Tracerr  r   r   r   <module>r     s  *          	 
      9 9 4  < < 0       $ $  # N  @ 7 4 0 9 : ?  ! J / F D D 5    '  - 6  S : 5 / 0 W W    $     .  ; H - ( 1 
 C ; A4H4?!NN44XwG 11(LI..228]K11(LI**  "'!>!> $)HHOO$D$D !&*38nc3h& $  
 $   6:)uU\\3:%=>?)%%**+d2) 				"	"#)X 4c 4 4 - - -# (2tCy=2I  UXX__   ! !H 	S&[ N N N: <
 <
 <
~ X X X" ' ' ',E d38n (F". F"RU+2# U+2pV!V !VJJ HH%(H25H=@HJMH	HV  oo' cNCL2 2$~'RYY ~'r   