
    N j                    	   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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(KJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  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  \GR                  " \5      r\$GR                  GR                  \S75      r\$GR                  GR                  \S85      r\$GR                  GR                  \S95      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\" S@SA9 " SB SC5      5       r\" S@SA9 " SD SE5      5       r SoSF\\\$R^                  \S-  4      SG\\$GR                  GR                  GR                     S-  S=\\$GR                  GR                  GR                     4SH jjr\GR                  S=\4SI j5       r\ " SJ SK5      5       rSL\S=\/ \\   4   4SM jr " SN SO\$GR                  GR                  5      r " SP SQ5      r\\\4   r\ " SR SS5      5       r\ " ST SU5      5       r\ " SV SW5      5       r\ " SX SY5      5       rSZ\S=\\\4   4S[ jr " S\ S]\5      r " S^ S_\5      r " S` Sa5      rSbrSc\Sd\Se\Sf\Sg\S=S4Sh jr\GR                  " 5       r\ " Si5      r\!" Sj5      Gr  " Sk Sl5      Gr " Sm Sn\.GR                  5      Grg)pa  
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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)StorageWeakRefgraph
graph_codegraph_sizes
trace_callcompiler_fnconfig_patchesreturn.c                    ^ ^^ SSK Jm  S[        S[        S[        4U UU4S jjn[        T SS5      Ul        T Ul        U$ )	zO
Wrap a compiler function to apply inductor config patches during compilation.
r   )r-   gmexample_inputsr   c                 l   > TR                  T5         T" X5      sS S S 5        $ ! , (       d  f       g = fN)patch)r   r   r   r   inductor_configs     k/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/_dynamo/output_graph.pywrapped+_wrap_with_inductor_config.<locals>.wrapped   s%    "">2r2 322s   %
3__name__z	<wrapped>)torch._inductorr-   r	   getattrr   __wrapped__)r   r   r   r   s   `` @r   _wrap_with_inductor_configr      sG     :3C 3 3 3 3
 {JDG%GN    T)frozenc                   *    \ rS rSr% \\S'   \\S'   Srg)AliasingInfo   has_aliasingmsg 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      $ r   )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   ro   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$ )
FakeRootModulei,  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__/  s-    $$&DADQ 'r   c                     g)NzFakeRootModule(...)r   r   s    r   __repr__FakeRootModule.__repr__4  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_modules7  s!    $$&DADQ 'r   r   )r   r   r   r   r   dictr   r   nnModuler   r   r   r   __classcell__r   s   @r   r   r   ,  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__=  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)rc   restorer   copydeepcopyr   	candidateforwardr-   verify_correctnessre   ru   RuntimeError	Exceptionr   	exception)r   r   r   copy_gmcorrectresults         r   __call__WrapperBackend.__call__@  s    ),--(g>>>!T^^tww%F77??"((>>!	ggoo|N'CDG^^\.%ABF G$$~~ LLN !>tfEFF 	MM78	 LLNs   5AD) D) )!E

E E)r   r  r   r  )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)BytecodeTracingTimingsic  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)rj   dataclassesfieldsr   nameremovesuffixrf   
setdefaultr   r   try_add_event_data)r   chromium_log
event_datafns_valkeys         r   report_and_reset'BytecodeTracingTimings.report_and_resetp  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  c  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
'   \R,                  R.                  R0                  R2                  \S'   \R4                  R6                  \S'   \\R4                  R8                     \S'   Sr\\S'   Sr\\S'   Sr \\S'   Sr!\\   \S'   \"S\#4S j5       r$\"S\R4                  R6                  4S j5       r\"S\\R4                  R8                     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_layerscurrent_deviceglobal_state_guard_guards_aotautograd_guardsFexportskip_guards_checkexport_constraintsN%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   )r4  r   s    r   r   OutputGraphGuardsState.guards  s    ||r   c                     U R                   $ r   )r5  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/  r0  r1  r2  r3  r9  r6  r8  r4  r5  r7  )r*  r+  r,  r-  r.  r/  r0  r1  r2  r3  r9  r6  r8  r   rB  r7  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FuncTorchInterpreterr   device_C_dynamor   GlobalStateGuardr4  	GuardsSetGuardEnvExprr6  r   r7  r8  r9  propertyr%   r=  rB  rE  r   r   r   r   r*  r*    s_    #EOO$E$EFFF##'S#X(>#??O5++77LLMMU\\**((//@@@]]$$$emm8899 FD#t#$$;?)8C=?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   rV  r(  r   dc_fieldr   rY  r   r   rZ  r[  r\  tupler	   r]  r^  r   r   r   r   rU  rU    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   rU  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*                  4   \	S'   \" \S9r\\\\\\R                  R                   R(                  \R                  R                   R*                  4   4   4   \	S'   Srg)	ExportMetaDatai  rW  Ngraph_input_idx_to_local_sourceoutput_return_typeout_specmodule_call_specr   )r   r   r   r   r_  r   rc  r(  r   r   rd  r`  r   r	   r   utils_pytree
_LEAF_SPECre  r   TreeSpecLeafSpecrf  r   r   r   r   rb  rb    s    
 AIA#T#v}*<%=  6>d5SS%S/12S 	&& eEKK''00%++2E2E2N2NNO  	& dS%++44ekk6I6I6R6RRSST	V 'r   rb  r,  c                 R    U S   n[        U[        5      (       d  UR                  nU$ )N__builtins__)r   r   __dict__)r,  
f_builtinss     r   get_builtins_dictrp    s,     n-Jj$''((
r   c                      ^  \ rS rSrSr    SS\S\\\\4      S\\	   S\\
   S\\\\\   4      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/  r0  r1  r2  r3  r4  r5  r6  r7  r8  r9  rt  r%   
_shape_envrb  ru  rv  )r   rs  rt  r=  ru  rv  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   )rx  r   s    r   r=  OutputGraphCommon.shape_env<  s    r   r   r   c                     [         er   )NotImplementedErrorr   r   r   s      r   bypass_package OutputGraphCommon.bypass_package@  s
     "!r   )rx  ru  rt  rv  NNNN )r   r   r   r   r   r*  r   r   r   r%   rb  r(  r   r   r   rS  r=  r	   r  r   r   r   s   @r   rr  rr    s    4 48(,48HL$
#9$
 !c3h0$
 H%	$

 ".1$
 %-T#tF|2C-D$E$
 
$
 $
L 8  "S " " " "r   rr  c                   V   \ rS rSr% Sr\\S'    SS\\\	4   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\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>                  R@                  4   4S jjr!S\R>                  R@                  4S  jr"SS! jr#S\
\\      4S" jr$S#\%S$\	4   S%\\	S$4   S\4S& jr&S#\%/ \	4   SS4S' jr'SS( jr(\)SS* j5       r*\)SS+ j5       r+S\4S, jr,\)S\R>                  RZ                  4S- j5       r.\.R^                  S.\R>                  RZ                  SS4S/ j5       r.\)S\\\R@                  4   4S0 j5       r0\)S\\Rb                  \Rd                  4   4S1 j5       r3\)S\\4Rj                  \6\R>                  R@                  S24   4   4S3 j5       r7S%\	S4\	S\R>                  R@                  4S5 jr8S%\	S4\	S\R>                  Rb                  4S6 jr9S%\	S4\	SS4S7 jr:\;Rx                   SS8\
\=   S9\
S)   S:\
\   S\>S;   4S< jj5       r?\)SS= j5       r@\)S\R                  R                  4S> j5       rC\)S\D4S? j5       rE\)S\R                  R                  4S@ j5       rH\)S\\\	4   4SA j5       rI\)S\\R                  R                     4SB j5       rK SSC\
\\\\%S$\	4   \4   4      SS4SD jjrLSSF jrMSSG jrN\)SSH j5       rOS\P4SI jrQS\4SJ jrRS\4SK jrSSL\S\6\R                  R                  \	4   4SM jrVSSN\S\4SO jjrWSN\SS4SP jrX\YSQ\	S\4SR j5       rZSS\ST\	S\R@                  4SU jr[SV\6\R                  R                  \Rd                  \	4   SQ\	SW\	S\4SX jr\SESS\\\]   \\\4   4   4SY jr^SESSZ\PS\\\   \_4   4S[ jr` SSESS\\aSZ\PS\\_   4S] jjrbSESS^\cSS4S_ jrdSESS`\\   S^\cSa\SS4
Sb jreSSc jrfSS\\S4\	SS4Sd jjrgS\\\\6\P\4      4   4Se jrhSN\S\4Sf jri\;Rx                  S\	4Sg j5       rjSSh jrkSi\Sj   SESSS4Sk jrlSESSi\\   Sl\mS\\]   4Sm jrn\)S\\Rb                     4Sn j5       ro\)S\\p   4So j5       rqSp\R                  Sq\\2   S\s4Sr jrtSp\R                  Sq\\2   S\s4Ss jruS\\\R>                  R                  4   4St jrvSN\Su\R>                  R                  S\4Sv jrwS\\Rd                     4Sw jrxSSx jrySSy jrzSSz jr{S\\]   SS4S{ jr|SN\S.\	SS4S| jr}S\S.\	S\4S} jr~S\S.\	S\4S~ jrSS jrS\%\R                  /S4   SS4S jrS\R>                  Rb                  S\	4S jrS\R                  R                  S\SS4S jrSrg)OutputGraphiG  ac  
Wrapper class to hold outputs of InstructionTranslator.  Mainly the
generated fx.Graph.

OutputGraph is 1:1 with a frame being processed. Each frame is associated
with some root InstructionTranslator. When user code calls a function,
we construct a InliningInstructionTranslator that continues to write into
the root InstructionTranslator's OutputGraph.
side_effectscode_optionsr   root_txr   r6  r8  frame_stater+  r,  f_coder-  packager   	one_graphr   Nc                 ~	   [         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$        [K        5       U l&        / U l'        [Q        U RN                  U=(       d    [R        RT                  U=(       d    [R        RV                  [R        RX                  U RH                  S9nSS K-Js  J)n  UR]                  SS9   [        R^                  Ra                  U[c        U R2                  5      U R2                  S9nS S S 5        [e        W5      U l3        U Rf                  Rh                  Rk                  U	5        U Rf                  Rh                  U l4        [l        Rn                  " 5       U l8        U Rs                  5         [t        Rv                  " [x        5      U l=        0 U l>        [        U 5      U l@        0 U lA        0 U lB        [        R                  " 5       U lE        [        U5      U lG        / U lH        SU lI        / U lJ        X lK        X0lL        S U lM        XlN        0 U lO        / U lP        / U lQ        SU lR        0 U lS        [        R                   R                  5       U lU        SU lV        [        5       U lW        [        0 5      U lX        [        0 5      U lY        U R                  5         0 U l[        / U l\        S U l]        / U l^        0 U l_        S U l`        S U la        U R                  5       U lc        [        R                  " 5       U lf        [        5       U lh        U R                  5       U lj        0 U lk        [        5       U lm        [        5       U lo        0 U lp        g ! , (       d  f       GNb= f)	N)r.  r/  r0  r1  r2  r3  r4  r5  )	is_export)co_nameco_filenameco_firstlineno)tracked_fakesallow_scalar_outputsallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards	co_fieldsr   F(fake_tensor_allow_unsafe_data_ptr_access)r=  allow_non_fake_inputsr6  )qr*  r   r   r   autograd
forward_ad_current_levelrJ  rK  #retrieve_all_functorch_interpretersrg  _deviceCURRENT_DEVICErO  convert_frameinitial_global_staterN  r   rP  r4  rQ  SubgraphTracertracersinput_source_to_varleaf_var_creation_orderr6  r8  r  cleanup_hooksnext_compile_id_counter
compile_idinstalled_globalsr  r  r  r  rL   region_trackerr  r%   r-   capture_scalar_outputs capture_dynamic_output_shape_opsr  torch._functorch.configr   _subclassesFakeTensorModer   r   tracing_contexttraced_coder   r   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   rv  param_name_to_sourcerQ   r  variable_tracker_cachesignature_cache	itertoolscountunique_var_idr   r  output_instructions	timestamp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!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backward_statebackward_state_proxybackward_state_var!install_builtins_dict_in_fglobalsr9  
contextlib	ExitStackcompiler_trace_stackr  bytecode_tracing_timings+maybe_install_saved_tensors_hooks_subgraphs"saved_tensors_hooks_subgraph_namesrt  rb  ru  r*   "used_inlined_inbuilt_modules_namesattr_source_cache)r   r  r   r  r6  r8  r  r+  r,  r  r-  r  r  r=  _config	fake_modes                   r   r   OutputGraph.__init__T  s    	''%"u*,~~00??"--99]]_ ;;..==$}}::OO  :xx&&779MM++- " 	( 	
" 't>? CE  ?A$"4&68#$78+.5
 ~~!--$33
 12 13 ,,!*!Kf.K.K+4 ,7668>8j8jnn
	& 	21]]E]J))88#&*4;;&7{{	 9 I K 0>i/H((//7//;;--/ 	 	  " ##D) 	' BD!'- FH#;=&__.,0,>68   OQ# 2= >B RT"<>+- KM  ,188+S+S+U( 27. PSu, >AW ADB! 	  AC-  	 '+ 57 ;=>B!15 224 	2 %/$8$8$:!(>(@% <<> 	/
 /1-/ DN</GIc KJs   =R--
R<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_sourceK  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_contextri   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_startT  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_stopi  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__)rp  r,  install_global)r   ro  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sourcegrapharg)r  r6  rE   r!   root_tracercreate_graph_inputr<  rU   rw   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_ENVrX   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICEGLOBAL_STATETORCH_FUNCTION_STATEr   rN  rJ  peek_interpreter_stackFUNCTORCH_STACK_MATCHrO  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   rO  r9  r:  rJ  _aot_autogradrg  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_subgraphs  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   c                   ^^ T" U6 nU R                  5       n[        U R                  5      mTR                  UU4S j5        TR	                  [        [        R                  R                  U5      5        TR                  [        U5      S5        TR                  U5        U R                  R                  TR                  5       5        [        U5      n[         R"                  " U R                  X55      nUR%                  5       n[&        R(                  " 5       R*                  R,                  R/                  U5        U$ )zM
call fn(*args) before the graph runs and turn the result into a fake input.
c                  P   > T R                  TR                  TR                  5      $ r   )load_import_fromr   r   )cgr   s   r   <lambda>3OutputGraph.synthetic_graph_input.<locals>.<lambda>  s    B''r   F)r,  r?   r  add_push_nullforeachmapr0   ConstantVariablecreatecall_functionr  storer  extendget_instructionsr_   r}   buildrealizer   getguards_contextdynamo_guardsremove_guards_with_source)r   r   r   r-  varnamer'  r  rR  s    `     @r   synthetic_graph_input!OutputGraph.synthetic_graph_input  s     D	,,.t||$
	
 	

3y1188$?@
TE*
%%b&9&9&;<%g. &&t||]K!++99SS	
 r   c                 :    U R                   R                  U5        g r   )r  r   )r   r   s     r   add_cleanup_hookOutputGraph.add_cleanup_hook  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_hooks  s.    T//0DF 1  "r   r  c                      U R                   S   $ Nr   r  r   s    r   r)  OutputGraph.root_tracer  s    ||Ar   c                      U R                   S   $ Nrq  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   c                 .    U R                   R                  $ r   rv  r   r   s    r   r   OutputGraph.graph  s    ""(((r   valuec                 $    XR                   l        g r   r|  )r   r~  s     r   r   r}    s    $)!r   c                 .    U R                   R                  $ r   )rv  input_name_to_proxyr   s    r   r  OutputGraph.input_name_to_proxy   s    ""666r   c                 .    U R                   R                  $ r   )rv  real_value_cacher   s    r   r  OutputGraph.real_value_cache$  s    ""333r   	LazyProxyc                 .    U R                   R                  $ r   )rv  bound_symbolsr   s    r   r  OutputGraph.bound_symbols(  s    ""000r   r   c                 :    U R                   R                  " U0 UD6$ r   )rv  create_proxyr   r   r   s      r   r  OutputGraph.create_proxy2  s    ""//@@@r   c                 :    U R                   R                  " U0 UD6$ r   )rv  create_noder  s      r   r  OutputGraph.create_node5      ""..???r   c                 :    U R                   R                  " U0 UD6$ r   )rv  remove_noder  s      r   r  OutputGraph.remove_node8  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  rv  	__enter__r  r  r  r   __exit__r   )r   r  r  r  new_scope_ctxtracers         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.outputY  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_envb  sP    ##--999##--77CCC##--777r   c                 B    U R                   R                  R                  $ r   )r  ra  rb  r   s    r   r   OutputGraph.guardsh  s    ##22@@@r   c                 B    U R                   R                  R                  $ r   )r  module_contextr   r   s    r   r   OutputGraph.nn_modulesl  s    ##22===r   c                 B    U R                   R                  R                  $ r   )r  ra  rB  r   s    r   rB  OutputGraph.aotautograd_guardsp  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                  S5      [        R                   " S5      4US'   [        R                  " [        R                  S5      [        R                   " S5      4US'   [        R                  " [        R"                  S5      [        R$                  " S5      4US'   [        R                  " [        R"                  S5      [        R$                  " S5      4US	'   [        R&                  [        R(                  " 5       4US
'   g)zS
Saves to out if it is provided. Else saves to the tracing context's global_state.
.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	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  s      r   r  OutputGraph.save_global_statet  s_    eHS#X.4556 ? ))88EE
 ).(>(>@U@U@W'X^$ e88&A%%f-,
'(
 e88%@%%e,0
+,
 e66?$$V,.
)*
 e66>$$U+.
)*
 ,,++-2
-.r   txc                 :    U R                   R                  U5        g r   )r  r   )r   r  s     r   push_txOutputGraph.push_tx  s    #r   c                 6    U R                   R                  5       $ r   )r  r   r   s    r   pop_txOutputGraph.pop_tx  s    ##%%r   c                 Z    U R                   (       d  U R                  $ U R                   S   $ rt  )r  r  r   s    r   
current_txOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMr   c                 ,    [        U R                  5      $ r   )rg   r   r   s    r   rg   OutputGraph.count_calls  s    4::&&r   c                 X    [        [        U R                  R                  5      5      S:H  $ rp  )r  r   r   nodesr   s    r   is_empty_graphOutputGraph.is_empty_graph  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  op)r   xs     r   has_outputsOutputGraph.has_outputs  s9    tzz//D/!4483CA/DEIIDs
   AAkeysc                     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_submodule  sH    tBF//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  existingvars       r   r,  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)joinrW  r   rer  isalpha)r  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  rn   rv   r   )r   r  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O
[        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OqS[        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OS[        S[        4U U4S jjnT R,                  R/                  5        H  u  pVUTL d  M  U" U5      s  $    [0        R2                  " U6 m
[5        T
T R,                  T R6                  5      m
TT R,                  T
'   [        T[        R                  R                  5      (       av  S	[        SS 4U
U U4S
 jjn[9        TS5      (       a!  TR;                  5        H  u  pU" U5        M     [9        TS5      (       a!  TR=                  5        H  u  pU" U5        M     U" T
5      $ )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   r-  tensor_dict)r  r  r  r  rm   r   r\   rN   r0  rM   ID_MATCHrY   TENSOR_MATCHrz   r  track_object_existingas_proxyr   r+  rb   )r  vtr  r   r'  r  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<  r  r  r  s    r   r  r  8  s    +DL*fXPWXXr   c                 2   > [         R                  " T40 TD6$ r   )r0   UnspecializedNNModuleVariabler	  s    r   r  r  A  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   rY  r  r  )r  r  r   r  s    r   r  r  M  sE    &--LL%%j*b"E # 	 r   c                    > TR                   R                  U 5        TTR                  U '   [        R                  " TT[        U S95      $ )N)source_name)r  r  r,  r}   r^  rV   )r  r   r  s    r   r  r  X  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  r  r  r  
new_sourcenew_namer  r   r'  s      r   register_leaf_name?OutputGraph.register_attr_or_module.<locals>.register_leaf_namek  s    00<<<!AA&)T
"V1YK06@))(3fk22 " 99#33JOOD 3r   _parameters_buffers)rO   r6  r}   r^  r  r   r   r]   r   r   rv  ry  r)  r   r   r   rN   r0  rM   	NN_MODULESymIntSymFloatr   r   r  r  rn   r,  hasattrnamed_parametersnamed_buffers)r   r  r  r  r  r   r   r  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  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`  rl   r   symbolic_localsvaluesr  store_attr_mutationsr  r   r   r~   r   r   mutation_typerP   r'  rW   r  r[   
local_namer   	graphargs_exampler  indexr,  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_localsrU  r}   	enumerater   r  r0   LazyVariableTrackerrealize_allr   rB  visitr   r   rZ  r{   target_valuesr`  r\  r]  rV  r   cellvarsfreevarsr(  r   r'  r[   r,  r  sysversion_infor<  rA  r[  rY  r^  )r   r  r>  stack_valuesr+  rC  ir~  rE  rM  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  U R8                  (       a   S5       eU R5                  U R6                  5        U R;                  U R                   5      u  pU R5                  U5        U R=                  5         U R>                  RA                  5        VVs0 s H  u  pU[C        U5      _M     nnn[E        U5      nSSK#J$n  [        U RJ                  5      S	:  a  / nU RM                  S5      U l'        U" [Q        U RJ                  5      SS9nU RS                  SU5      n[U        U R                   UUS9nURW                  URY                  US5      5        URW                  [[        S	S5      5        UR                  UR]                  U RN                  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  [        [_        U5      5      [        U5      :X  a  U R0                  Ra                  5       (       a  URb                  (       d  U Rd                  (       d  US   Rf                  (       d  US   Rh                  (       d  [U        U R                   5      nU Rk                  UU5        U R5                  / U Rm                  U[o        [)        U5      5      U5      QURq                  5       Q[s        S5      Q[        S[        U5      S9P5        GO-U RM                  S5      n[U        U R                   UUUS9nU Ru                  UUUS5        0 nURv                  RA                  5        H0  u  nnUS:  d  M  [y        U[z        [|        45      (       a  M+  SUU'   M2     [U        U R                   UUUUS9n U Ru                  UUU S5        [~        R                  R                  R                  (       Ga`  U(       GaX  [        U5      S:X  GaH  US	   n![        U![~        R                  R                  R                  5      (       Ga  U!R                  [~        R                  R                  R                  L Ga  U!R@                  S	   n"U!R@                  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"R@                  5       H  u  n'n!U!U$;   a   SU$U!   4U R                  R                  U''   M,  U!R                  bV  [        U!R                  SS5      =n((       a8  U(R                  (       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                  lX        / n*[        U R                  5      S	:w  d  [        U R                  5      S	:w  a  U*RW                  U Rm                  UU R                  5       U5      5        [        U R                  5      S	:w  a#  U*R                  U R]                  U5      5        SnO+U*R                  [        S&5      5        OU R                  5         U R5                  U*U Rq                  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/[s        S5      Q5        O;U,U.-  n,U R5                  [        5       /[        U+U,5      Q[s        S5      Q5        U+U.-  n+U-[        U5      S-
  :X  d  M  [U        U R                   5      n/0 n0U R                   R                  RA                  5        H  u  n1n2[        U2R                  [|        5      (       d  M'  U2R                  R                  U1:X  d  MC  U/R                  U/R                  U15      5        [        U
R                  5      [        U05      -   U0U1'   M     U R5                  U/Rq                  5       [        S'[        U05      S9[        S(SS9/-   5        U
R                  R                  U05        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+;   Ga  S	S,KjJkn3  / n4U R0                  R                  5        GH{  n5[        U5S-5      (       d  M  U5R                  n6[        U6[        [        45      (       d  M@  [        U5[        5      (       a  [        U5R                  U35      (       a  U5R@                  RA                  5        H  u  n1n20 n7U2R@                  RA                  5        H1  u  n8nUR                  5       U7U8R                  R                  5       '   M3     S.S//[o        U7R                  5       5      :X  d   eU7U R                  R                  U1R                  R                  5       '   M     [        U5[        5      (       a  [        U5R                  U35      (       a  GMj  U4R                  U55        GM~     U4 V5s/ s H  n5[        U5R                  5      PM     n9n5U9(       aQ  [~        R                  R                  R                  S0:X  a  [        R                  " S1U9 35        U$ [        S1U9 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NSn)A)ff = fs  sn5f )2au  
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rF     COPY_FREE_VARSco_freevarsr7  c              3   @   #    U  H  oR                  5       v   M     g 7fr   )can_restore).0blocks     r   	<genexpr>/OutputGraph.compile_subgraph.<locals>.<genexpr>~  s     K8Ju((**8J   r   )is_graph_breakz)export does not support pregraph_bytecoder,   disablerandom_valuesz.do not trace into Dynamo rng recovery functionr   __gen_rand_values)r6  Fc              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)r_  r   s     r   ra  rb    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r_  r  s     r   ra  rb    s     =+<aKKMM+<rc  ru     UNPACK_SEQUENCE	graph_out)tempvarsr6  r  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.)r!  r"  r#  r$  from_excPOP_TOP
BUILD_LISTLIST_EXTENDDELETE_FASTr%  )warnerror)_ExportModuleSpecTrackerDictr+  in_specre  r{  zWhile compiling, we found certain side effects happened in the model.forward. Here are the list of potential sources you can double check: )zr  r-   nested_graph_breaksr  compile_subgraph_reasonr  r   debugrP  rQ  prefix_instsopnamer   r8   r  r  r  allblock_stackrU  rk  exitr   r  r  prune_dead_object_newadd_output_instructionsr  r6  r<  cleanup_graphr   r   rt   r   
decoratorsrf  r   r,  r  r   r  r?   r\  load_function_namer6   create_storer   is_emptydebug_localsr  rZ  r[  codegen_cellscompile_and_call_fx_graphr   r]  r;   codegen_suffixusesrq   r_   r[   r   rO  log_graph_in_out_metadatar   r0   NamedTupleVariable	tuple_clsfunctional_exportExportTracerOutputListVariablegraph_outputsr)  codegenGraphOutputEntryvariabler/  rI  ru  rd  r'  r   is_inputis_python_constantas_python_constantr;  re  r|   rE   rg   r   graph_output_varsrun_compiler_collectiverV  rY  r7   r3   r(  r,  append_outputcreate_loadupdater:   side_effect_replay_policytorch.export._tracer}  _get_modified_varsr  r+  rP   rR   r   r~  r  r  rf  rS   warningsr{  r  ):r   r  r   r>  r  install_stack_valuesall_stack_locals_metascur_txrR  r+  r`  r2  r6  r  r  nn_modules_proxiesrootrf  random_calls_instructionsrand_fnrand_fn_namer  valsvalstack_values_flatstored_graph_output_vargraph_output_varcell_cgpass1rs  r  pass2r  flat_returnsre  vt_to_graph_out_idxr~  r  idxr'  er  	start_idxend_idxrS  n_valsroot_cgunmodified_locals_namesr   r   r}  potential_side_effectsr  mut_typespecsk_specside_effect_refss:                                                             r   compile_subgraphOutputGraph.compile_subgraphE  s   * ||'''))<<2%%% 	'')'-$		-v6 +-w&11;;"22 ''*, #DLL$=$=m$L M !''		$8 2 !#68 K8J8JKKKKK!%!B!Bl
"L ##L1"))$/ "&"4"45

62D2D
E 6 ]]F  " 	//3$$\2**t{{ 	
7	
; 	$$T%;%;<*.*N*NLL+
' 	$$[1 9=8M8M8O
8O94D/#&&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 & !( "		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%'"((;;=300"00H! #<>S"T  &c+BCC
II'CI I ),		(91(*3477==?KFC(+(>(>(@ %*&))*F*F*H$I 4C )2:'>$uzz|BT'T T'T %* !% 4 4 E E$%DD$;$;$=!" ):  's,CDD *3996R S S299#>; >> ?U >Ts&szz2>T     ==''AAVKMMXXhWik &% 'XXhWik 
 &%W
Z O~ 2 %$^&Fxj$Q! !j R# &' ^ s*   xx<x y	
y)yyrR  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,   rx  r\  )r  r   r`  sortedrT  r  r  create_load_closurepost_prune_cell_and_freevarsr5   r  r  r8   )r   r  rR  tx_cntr  rO  cells          r   r  OutputGraph.codegen_cells  s    ''33F:<F$ !(@(@(B!CD$D-(()?)?)EF%BBBB6>>tDE %   !3CM!BC! $  /&IJ/!DEr   rR  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   )rR  	debug_vars   r   rS  ,OutputGraph.codegen_suffix.<locals>.<lambda>A  s	    R	]r   Frw  )value_from_source)r  codegen_save_tempvarsr  r6  r   r  r  r  
store_attrr-   replay_side_effectscodegen_hooksr  rU  extend_outputr6   r  r8   r  restore_stackcodegen_update_mutated)
r   r  rR  rR  r  r  r  r   r7  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  r+  r   r   r  r  pairwiser  rN  _set_grad_enabledr`  r   _erased
erase_node)r   r  r   r  node1node2s         r   r  OutputGraph.cleanup_graphM  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   rS  ,OutputGraph.bypass_package.<locals>.<lambda>x  s    1"!r   c                     > ST0T E$ )N_reasonr   )r   r   s   r   rS  r  |  s    6   r   metadata_fn
payload_fn)r  r   rO  r-   strict_precompiler.   PackageErrorr   warning_loggingtrace_structuredbypass_current_entryr~  s    ``r   r  OutputGraph.bypass_packagek  s     ||==11--##00/  	3V<'' 	( 	
 	))+r   c                 z   0 nU R                   R                   H  nUR                  R                  SS 5      n[	        U[
        R                  R                  5      (       d  MJ  UR                  5       nU Vs/ s H%  n[	        U[        5      (       a  UO
[        U5      PM'     snXR                  '   M     U$ s  snf )Nr-  )r   r  r+  r`  r   r   r  r   sizer(  reprr  )r   retr   r-  r  ss         r   get_graph_sizes_structured&OutputGraph.get_graph_sizes_structured  s    02JJ$$D IIMM/4@M-):):)E)EFF$))+PT!UPT1z!S'9'9!tAw"FPT!UII	 %
 
 "Vs   7,B8c                    SnUSU S3-  nU R                   R                   GH  nUR                  R                  SS 5      n[	        U[
        R                  R                  5      (       d  MK  UR                  5       nX#R                   S[        U5       S3-  n/ nSnU Ht  n[	        U[        5      (       a  UR                  U5        M+  [	        U[
        R                  5      (       a)  SnUR                  UR                  R                  5        Ms    M     U(       d  M  UUR                   S	[        U5       S3-  nGM      U$ )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
r-  z: 
FTz (concrete): )r   r  r+  r`  r   r   r  r   r  r  r`  r(  r   r  r   hint)	r   r  graph_sizes_strr   r-  r  concrete_size
has_symintszs	            r   get_graph_sizesOutputGraph.get_graph_sizes  s   7VD622JJ$$D IIMM/4@M-):):)E)EFF$))+ii[5;-r#BB ""
B!"c**%,,R0#B55%)
%,,RWW\\:  "z'#yyku]7K6LBO# %( 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_state  s     
 "11@@PPR<>#78	  //BBCUV   //BB,-ABD  //BB,-ABs   6C)B "/C0CCc                 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   rS  5OutputGraph.run_compiler_collective.<locals>.<lambda>  s    1 (%r   c                  8   > T R                   R                  5       $ r   )local_staterender)dss   r   rS  r    s    2>>#8#8#:r   r  r,   z&Expect only one device type but got {}+r  Tr  )group)r  distributed_state
all_states
compile_pgr   infor  r   r  r  _device_typesr  formatr  rA   r   rM  rankacceleratordevice_countri   r  distall_gather_objectspeculation_logrl  r.    CompileCollectiveRestartAnalysis)r   r  r   device_typesr  r  s        @r   r  #OutputGraph.run_compiler_collective  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 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  SUR                  l        ["        R$                  " 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,   )TensorVariabler-  Tz3autograd.grad consumed grad_fns of returned tensors)restart_reason)r  variables.tensorr/  r   requires_gradr  r   r+  r`  r   r  fake_tensorr   r   r   r)  graph_break_on_autograd_gradr.   AutogradGradRestartAnalysis)r   r-  r  r/  r  r3  reachable_grad_fnss          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HBF""?55#X  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!                  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        [.        R0                  (       d  [3        S
5         [5        U5       HO  n[7        X85      n	[        U	[8        R:                  5      (       d  M/  [=        U	T R>                  UT R@                  S9  MQ     T RC                  5         [=        [8        R:                  " UT R                  5      T R>                  UT R@                  S9  SSS5        T RE                  5         [        T R                  5      n
[F        S   S==   U
-  ss'   T RI                  5         T RJ                  RM                  5         [O        UT R                  5      m#SSK(J)n  U" T#5        T RT                  (       a)  T RT                   H  n[W        T#U[7        X<5      5        M     T RX                   H  nU" T#5        M     [[        T#R]                  5       S5      bB  T R_                  S[         R`                  R.                  Rb                  T#Re                  SSSS9S9  T Rf                  b  UT#l4        T Rj                  T#l5        T Rl                  Ro                  5       T#Rp                  S'   T Rr                  T#Rp                  S'   UT#Rp                  S'   [t        Rw                  S[y        UT#SSSS95        [         Rz                  R}                  SU 4S jU#4S jS9  T R                  5         T R                  R                  nUc   eUT lB        T R@                  (       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,  [        [7        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 Rf                  b  T Rf                  R                  UT"5        U" T"S!S"9m"[F        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,   re  r   N__compiled_fnT)	with_uuidr  c              3   @   #    U  H  oR                  5       v   M     g 7fr   )r  ro  s     r   ra  8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>"	  s     5OBqjjllBrc  r)   )r6  statscalls_captured)dce_hop_extra_outputszfGraph contains named parameters: either inline_inbuilt_nn_modules=False or there are static addresses.Fprint_outputinclude_strideinclude_device)inline_builtin_nn_modulesr   r  r  
backend_id%s)rC  rD  coloreddynamo_output_graphc                  (   > ST R                  5       0$ )Nsizes)r  r   s   r   rS  7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>y	  s    $"A"A"CDr   c                  &   > T R                  SSSS9$ )NFTrA  )print_readable)r   s   r   rS  rL  z	  s    2#4#4!&tD $5 $r   )r  r  )r=  )_LazyGraphModule__self___lazy_forwardz"do not trace Dynamo-compiled graphrh  unique_graphsz:Compiling backend specialized graph with specialization=%sc                     U" X   5      $ r   r   )r  r   check_fns      r   rS  rL  	  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'  rT  r+  r   r   r  call_user_compiler)
r   r   rT  rV  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)rQ  r   rO  graph_bytecode_inputsr   )rR  s   r   rS  rL  
  s&    B//;;DD4r   )tr   r4  r   clear_framer  rf  r  r  rg   r   r  r=   r   r   r   r7  r  rv  
create_argr`  
dedup_passr   _maybe_preserve_original_metar-   do_not_emit_runtime_assertsri   dirr   r   r  r)   r=  r6  remove_unused_get_attr_nodesremove_unused_graphargsrh   &remove_tensorify_specialized_graphargsr  rl  r    dce_extra_outputsr@  r  r   r  r  
parametersr  rO  inline_inbuilt_nn_modulesrN  r  _backend_idr  r  r  r+  r  graph_code_logr  rr   r  r  rm  r  r  _old_fake_moder  rJ  r   r  r  r  rX  r   torch.fx._lazy_graph_modulerO  r   rP  force_recompiler  add_backend_idspecializationsr-  r'  r/  inspect	getsourcerT  stripRootGuardManagerr   LAMBDA_GUARDr   r   r  r  r	   install_global_unsafer  r?   rG   rU  rH   r)  r,  r[  r  r  rZ  pop_toprI  ru  rc  make_call_generated_coder]  )&r   r  r-  r  rf  r  output_nodesub_gmsattrsubgraphncallsr@  subgraph_nameregister_finalizerold_fake_moder  backend_fake_moderO  lazy_gmrs  asourcesrV  source_indexcheck_fn_sourceunused_root_guard_managerrT  r\  tmp_varsconstructorvar_namer  r7  rR  rY  r   rZ  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**$$//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 ##|.3mm.B.B.\.\((%*4PT )  $  ||'!%)-)E)EB&55::< GG67 ,0+A+ABGG'($(BGGL!  &"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+>!$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 ''-&&(g 87F EDl SR* -,L =i 87sq   AcC#c/7b#*A3b#Ic5(b5)c!c'DccJc#
b2	-c5
c	?c
c	c
c,c                 4    U R                   R                  SS9$ )Nplaceholder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 Nr(  )r  r+  r   r   s     r   r-  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)ri   _call_user_compiler)r   r   r   s      r   rX  OutputGraph.call_user_compiler*
  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
 [-        5       " [.        R0                  S	U
 35        [         R2                  (       a  [5        U5      nU" X5      n[-        5       " [.        R0                  S
U
 35        [7        U5      (       d   S5       e [Y        SS0 U RZ                  EU[]        UR                  R                  5      []        U5      S.E5        W$ ! [8        [:        4 a    e [<         a  nU R>                  (       aD  [A        U R                   U[B        RD                  " 5       5      RG                  URH                  5      S e[K        UU RL                  RN                  SSU
 S[Q        U5       SU RL                  RS                  5        3SU
 S[Q        U5       S3S/S9   S nAGNS nAf[T         a    e [V         aI  n[A        U R                   U[B        RD                  " 5       5      RG                  URH                  5      S eS nAff = f)Nr   rZ  call_methodcall_moduler,   r  _dynamo_sourcer(  r   z<unknown compiler_fn>zcalling compiler function zdone compiler function z#compiler_fn did not return callablezBackend compiler exceptionz	Backend: z
Exception:z
Traceback:
zBackend compiler `z` failed with z. Adding a graph break.z-Report an issue to the backend compiler repo.r   dynamor  )op_count
node_countinput_count)/r   r   r  r  r   rp   r  r+  r'  r  r  _param_name_to_sourcer  _source_to_user_stacksrJ   r  r-   debug_backend_overriderK   debug_inductor_config_overrider   r   r   r/   INFOr  r   callabler   r   rC   r  rB   rt  currentframewith_traceback__traceback__rF   r  r  r   format_frame_summaryrD   r	  r   r  r  )r   r   r   totr  r   plr7  r   inductor_config_overrider  rY  r  s                r   r  OutputGraph._call_user_compiler7
  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//   ( 	
	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H* *MB2K<<MAMMc                 n    [         R                  R                  R                  (       a  [	        U 5      $ 0 $ r   )r   rO  r-   use_graph_deduplicationrI   r   s    r   rc  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_suffixFr&  )rn   r   r   torchdynamo_force_dynamicr   )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   )r-  example)r   r7  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   rg  (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[,        [.        R0                     4S jnS[        R
                  SS 4U 4S jjn[3        5       mS[2        [.        R0                     S[4        [$        R6                  [$        R8                  4   SS 4S jm/ nT R:                   GH  nU" U5      S LnU(       a&  UR                  (       d  UR=                  U5        M9  M;  UR                  (       d,  [?        UR@                  S   [B        5      (       d
  U" U5        Mx  UR@                  S   n	[?        U	[B        5      (       a  M  [?        UR@                  S   RD                  [$        RF                  5      (       a  UR@                  S   RD                  n
UR@                  S   RH                  n[$        RJ                  RL                  RO                  U
5      (       ds  [Q        U
RS                  5       5      nU HT  n[U        URV                  U5      n[X        RZ                  " [$        R6                  [$        R8                  4UU4S jU5        MV     GM  []        [_        UR@                  S   RD                  5      5      (       a  GM  U	R`                  b  U	R`                  OU	RD                  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Rc                  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)NTFr-  )	r   r   Noder+  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   SymTypesTr-  F)
torch.fx.experimental.sym_noder  r   r(  rl  r   r   r  r+  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  rZ  Fr-  c              3   4   >#    U  H  nT" U5      v   M     g 7fr   r   r_  r  r  s     r   ra  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   r  s     r   ra  r  
  s     G2FQ~a((2Fr  T)
r  r  r  r   r+  r`  r  r   r   r)  )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  rZ  c                 
   U R                   S   nUR                  n[        U[        R                  5      (       aI  [        UR
                  R                  [        R                  5      (       a  UR
                  R                  $ g r  )	r+  r  r   r   r  r   exprsympySymbol)r   r7  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 %sr(  )r   r  r+  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"   )r  r  s     r   update_used_symbols@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols  s     L..Lr   r(  c                    > T" TU 5      $ r   r   )tr  r  s    r   rS  5OutputGraph.remove_unused_graphargs.<locals>.<lambda>1  s    .A,PQ.Rr   )2r  r   r   Argumentr   r  %torch.fx.experimental.symbolic_shapesr  rk  r   r   r  r  r  r  r  operatorgetitemr   _checkr   r  r   r  r  r   r   r  r   r  r   r   r+  rw   r  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__r   wrapped_objpytreetree_map_onlyr   r<  r3  remove)r   r  r  r  r   r  r  recheck_placeholdersbinds_symbolr7  real_script_objfake_script_obj	flat_dictr~  fake_attr_valr  symbolr  r  r  s   `                @@@r   rh  #OutputGraph.remove_unused_graphargs
  sv    	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 	x7M 		2 	2D 	2 +.%	/ell+	/38u||9S3T	/	/
  "%%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   )TensorifyStater-  r  c              3   >   #    U  H  oR                   S :H  v   M     g7f)itemN)r  )r_  us     r   ra  EOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>]  s     ?JqF*Js   )torch._dynamo.symbolic_convertr  r   r  r+  r`  r   r   	item_memor  r   _exprr  r  should_specializer  r   replace_all_uses_withr#   r  )r   r  r   r-  r  s        r   ri  2OutputGraph.remove_tensorify_specialized_graphargsF  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_instructionsh  s     
 	  ''/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   rd   rY  r,  )r   r  r~  s      r   ry  !OutputGraph.install_global_unsafep  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  ry  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=   ry  r  s       r   r  OutputGraph.install_global  s!      ""4/r   c                 p   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                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                   R                  5         U R"                  R                  5         U R$                  R                  5         U R&                  R                  5         g r  )r  r   rl  r  r  r   r  r+  r  r  r  r  r  r  r  r  r  r  r  r  r  s     r   cleanupOutputGraph.cleanup  s;    //557$(!JJ$$DTYY&IIj) % 	##%  &&(!##))+""$##))+--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  r(  r  )r  r+  r  r   r  r  s     r   example_value_from_input_node)OutputGraph.example_value_from_input_node  sH    77m#99Z(000ww*$$$t{{++r   inlined_moduler'  c                   ^ ^^ [         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  r  r  s      r   r  HOutputGraph.add_fqn_info_for_inlined_modules.<locals>.register_leaf_name  s    ,,888==fiPJq,H2<D%%h/&+..  55//
@ /r   r  r  )r  r  r  rn   r  r,  r   r   r  r  r  __func__!og_module_named_parameters_fn_ptrr  og_module_named_buffers_fn_ptr)r   r-  r'  r  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   )8r  ro  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r6  r8  ru  r  r  rt  r  r  r  r9  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rv  r  r  r  r  Fr   )r  )r   r  r   )r   r  )r  r   r   N)r   r   )tmp)r   r  )r   r   r   r   r   rQ   r   r   r   r	   r   r2   r   r   r   rG  r   r   r   rH  rI  r   r   rT   r  r  r  r  r  r}   r`  r   Proxyr  r  r  r  r   re  rh  rm  rS  r)  rv  ry  Graphr   setterr  r  r   r  r  r  r   r  r  r  r  r  contextmanagerr(   r   r  r  r  r  r  r%   r=  r4  rQ  r   r   rR  rB  r  r  r  r  r(  rg   r  r  r   r   r  r,  r  staticmethodr  r  r   r<   r<  rU  rU  r   r  r?   r  r  r  r  r  r
  r  r  r7  r   r  r  rx   r-  r  r1   rX  r  rc  rF  r   rg  rh  ri  r  ry  r   r  r%  r(  r+  r4  r   r   r   r   r  r  G  s	      hJ38nhJ j)hJ -	hJ
 hJ %%67hJ hJ hJ hJ hJ $((I(I#JhJ *+hJ hJ 
hJTF # * >>"%>	>* 8DD3 D
 4:5#5-05	sEHHNN"	#5)%((.. )*<:Xd3i=P ::38$,1#s(O	8&8BG#4 & &#
      & & )uxx~~ ) ) \\*588>> *d * * 7T#rxx-%8 7 7 4$rww'<"= 4 4 1tELL%8S2T$TU 1 1A# A A A@ @ @ @@ @ @ @ 
 &*	' /0 c]	
 
/	0 :   .5,,;; . . 88 8 8
 A// A A >DcN > > FD)C)C$D F F KO$
DeHS#X,>,D&E!EFG$
	$
L$& N N'S '0 0JT J# %0D*E C C 5C 5D 5
    .,/	&Nehhoou||S89N N 	N
 
N`Y/-Y/	tK $vv~"66	7Y/vh"-h";>h"	tO$&99	:h"\ 	I&'I& #I& 	I&
 
!	"I&VF ; F Ft F2"G'"G ?+"G 	"G
 "G 
"GH1<S   4Dd5c?6K1K,L C C 2 c  "!7F"()"/J"	"H`)'`) !`) 	`)
 
k	`)D	 7d277m 7 7 E4> E E@..@26v,@	@Z..Z26v,Z	ZxDehh&:&:!:; S %((2F2F 3 U\\ 2 
'
X0t 'D d;.?  D  
Q# 
Qc 
Qd 
Q3 s s 	S 	 	 	$0?"*BNN+;T+A"B?	?
,%((-- ,C ,$2#hhoo$27=$2	$2r   r  c                       \ rS rSr% \\S'   \\S'   \\   \S'   \\   \S'   \\\	S4      \S'   \
\\	4   \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_prologueoutput_graphoutput_graph_for_cleanup.closure	f_globalsNr  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@  rC  rD  r  rB  rA  )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   )rB  r  r   _clear_nodesr  r  r=  r  )r   rA  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   )rC  r?  rD  r@  rA  rB  r   r   )r   r   r   r   r   r   r   r  r`  r	   r   r   r   rI  r   r   r   r   r>  r>    su     $$;'' '{33eCHo&&CH~ KO.1.:B3-.	.
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.htmlrA  kindr  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)NrZ  r  r   c                 ^   > U R                   S;   a  g TR                  R                  U 5        g )N>   atenprimprims)	namespacer  r   )r  rA  s    r   encountered_compliant_op8check_pt2_compliant_op.<locals>.encountered_compliant_op  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   r   )r  r   r-   only_allow_pt2_compliant_opsrE   err_epilogue)r  r   rA  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  r   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   rO  rg  get_fake_values_from_nodesr  rN  _jit_resolve_packet_qualified_op_namer  rE   )rA  rK  r  r   r   rR  rX  r`  r  overloadr  s   `          r   check_pt2_compliant_opre    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;* =%&K 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  ia  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__b  s     	r   c                 N    U R                   " U R                  0 U R                  D6$ r   )r   r   r   r   s    r   r  LazyProxy.__call__o  s    ww		1T[[11r   )r   r   r   r  )r   r   r   r   r   rf  rg  r   r   r   r	   r  r   r   r   r   r  r  a  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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\\\R                  /\R"                  4      S
\R"                  4S jjr   S/S\S\S\S\S\\   S\\   S\\\R                  /\R"                  4      S
\R"                  4U 4S jjjr    S0S\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
\R"                  4S  jjrS!\R"                  S
\\\R"                  4   4S" jrS#\S
\4S$ jrS\S%\\\R                  R"                  4   S
S4S& jrS\\R<                  \R>                  4   S'\\   S
S4S( jr S)\R<                  S
\!\"RF                     4S* jr$S
\%4S+ jr&S
\'4S, jr(S-r)U =r*$ )2r  is  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.
NrA  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        Ub  UR.                  S-   OSU l        S U l        S U l        S U l        S U l        U R                  c  / U l        O:U R                  R8                  U R                  R;                  U5      U4/-   U l        [=        5       U l        / U l         [
        RB                  " 5       (       a  [E        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  rA  r   r   r9  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is_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_strr*   _used_names_input_versions_at_beginningis_inference_mode_enabledr  tracked_tensor_or_symint_vt)r   rA  r  r  r  r  r   s         r   r   SubgraphTracer.__init__{  ss    	#MM,7XX^^%
 # 9; =? *& :< TV >@!
 =B9;@8 */& ,1(:@:L 2 2Q 6RS26GK 37!
 ;;.0D #';;#>#>**=9=IB $D 
 -7L 8:)**,,e  IS(r   r  c                 :    U R                   R                  U5        g r   )r  r   )r   r  s     r   record_tensor_or_symint_vt)SubgraphTracer.record_tensor_or_symint_vt  s    ((,,R0r   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)
ry  rz  r{  current_instructionstarts_liner`  r   r  _COPY_META_FIELDSr+  )r   r  r   linenonode_idxr+  r   s          r   rd  ,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   rK  r  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_proxyrA  r  r  )	r   rK  r  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 )NrX  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  )rk   rstripr   r  )linecur_instheaderr-  tx_codes    r   get_trace_call_log_str<SubgraphTracer._create_proxy.<locals>.get_trace_call_log_strX  s9    5gxHOOQD+BGGLL>xr$PPr   rG  Forig_graphmodulec                      g r   r   r   r   r   rS  .SubgraphTracer._create_proxy.<locals>.<lambda>c  s    Dr   Tnn_module_stack>   r  rZ  ru  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.r   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  )r_  r   r   tyr  s       r   ra  /SubgraphTracer._create_proxy.<locals>.<genexpr>  s4      *QJAw773<?f4 *Qs   !1	1z2Invoking an nn.Module inside a HigherOrderOperatorr  r  r  )Hr  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r  rA  r  rP  rQ  r  rr  	positionsr  r  get_line_of_code_headerr   trace_call_logr  rs   rx  r>   get_contextr`  r   r   r   r  r   r  r+  ry  _lineno_maprz  r  __code__r  r{  r  r  r   r  r   r)  r   
startswithr|  rE   r  r  r   rd  is_co_filename_from_nn_modulesframe_summaryr   r  r'   r  r   StackSummary	from_listr#  r  r  rO  r-   r  track_nodes_for_deduplicationr  
track_node)r   rK  r  r   r   r  r  r  	flat_args	tree_specnew_flat_argsr7  maybe_new_argr  r  is_retracingorig_graphmodule_maybendr  r   current_nn_moduleframe_summariesframefiltered_frame_summariesmsgsr  r  r-  r  r   s     `                      @@@@r   r  SubgraphTracer._create_proxy  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 	_&v(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  )re  rA  r  r  arg_tree_leavesr   r   r   r  r   r   r  r  r+  r~  r   r  )r   rK  r  r   r   r  r  r  r7  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 rp  )r  r  r   r\  rk  r   r  r  r  r   r  )r   r   user_graph_nodesuserother_graph_nodes        r   r  SubgraphTracer.remove_node  s    tzz?Q46

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

d#  $$TYY5r   r-  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   _versionr   r  r  rw  r  r  rZ   rA  r  r  r   extract_stackrn   r~  r  r  rk  r   r   inserting_beforeinserting_afterr  rv   popitemr   compileris_compiling_lift_basic_symbolsr   r`  rI  rW   r  r  r  r  r  )r   r  r  r-  r  r'  	prev_namer   ctxr  r   r   is_strict_exportis_non_strict_exportrS  r  e_sources                    r   r*  !SubgraphTracer.create_graph_input  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 SubgraphTracerr-  )r  r   r+  r   r   r  r  r  rp  r  lift_tracked_freevar_to_inputr   r<  real_objr*  r  )r   r  r-  r  	new_proxys        r   r  ,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   r7  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  )r_  sub_argr   s     r   ra  ESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>  s&      'FG @@II'Fs   !)
r   r   r   r8  slicestartstopstepr  r  )r   r7  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 )Nr   r   c                    > SSK Jn  U" U 5      =(       aX    [        U R                  R                  [
        R                  5      =(       a#    U R                  R                  TR                  ;  $ )Nr   )r$   )r  r$   r   r   r  r  r  r  )r   r$   r   s     r   	need_bind8SubgraphTracer.track_produced_symints.<locals>.need_bind  sJ    I A :qvv{{ELL9:FFKKt'9'99r   r-  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   r8  r   r  r   r  rv   )r-  r   r   r  r  r  s       r   _proxy_with_example_valueHSubgraphTracer.track_produced_symints.<locals>._proxy_with_example_value  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 %srZ  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   r8  r   r   rI  r  r   r  rw  r  opsrN  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   r  r   r  r  )r   r-  r  r  r  rS  r   
lazy_proxyr  attrsr  r~  inner_tr  r  s   ` `           @r   r  %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   srcc           	        ^   SS[         [        [        R                  4   S[        [
           S[        SS 4U 4S jjjn[        U[        R                  5      (       Gat  [        UR                  5       5       H+  u  pEU" UUb  [        U[        R                  U5      OS SS9  M-     UR                  [        R                  L a}  [        UR!                  5       5       H+  u  pEU" UUb  [        U[        R"                  U5      OS SS9  M-     U" UR%                  5       Ub  [        U[        R&                  5      OS SS9  GO;UR                  [        R(                  L aA  T R+                  UR-                  5       U5        T R+                  UR/                  5       U5        OUR                  [        R0                  [        R2                  1;   aA  T R+                  UR5                  5       U5        T R+                  UR7                  5       U5        OnUR                  [        R8                  [        R:                  1;   a@  T R+                  UR=                  5       U5        T R+                  UR?                  5       U5        [A        U5      (       aF  URC                  5       u  pgU H-  n[E        X5      n	T R+                  Xb  [G        X(5      OS 5        M/     g g [        U[        R                  5      (       a
  U" UU5        g g )	Nr   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   r-  )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_tensorr3  rn  r(  )r$   r   r   r  lookup_unbound_symbolsr  r  r  r  r   r+  r*  r   r<  r   r  r  rw  rp  r  iterrx   )	r   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_symintU  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  r6  )$r   r(  r   r  r   r   r   r   r   rI  r  ra   r`   SIZEr  r  r  STRIDEr  STORAGE_OFFSETr  r  r  r  r  r   r  r  r  r  r  r  r+   r  r   rT   )
r   r-  r  r  rS  r   r	  r  r~  r
  s
   `         r   r  "SubgraphTracer._lift_basic_symbolsN  s    !B	S%,,&'B	V$B	 B	 	B	 B	H mU\\22!-"4"4"67' ? -S.2E2EqI! 8 ##u}}4%m&:&:&<=DA+  # 1n6K6KQO!%# > (!002 ? -S.2O2OP! %%)9)99(()?)?)A3G(()>)>)@#F%%%*:*:E<L<L)MM(()C)C)EsK(()B)B)DcJ%%%*:*:E<L<L)MM(()C)C)EsK(()B)B)DcJ,];;*==?
!D%m:G,,/C!6t " < u||44# 5r   r   c                    UR                   R                  R                  n[        U5      S:X  a  / $ / nU H  nX@R                  ;  a  UR                  U5        M%  U R                  U   n[        U[        5      (       a  U" 5       nXPR                  U'   [        U[        R                  R                  5      (       a  UR                  U L a  M   SU S35       e   [        US S9$ )Nr   zThe proxy of symbol z" doesn't belong to current tracer.c                     U R                   $ r   )r  )r   s    r   rS  7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>  s    r   )r%  )r   r  r"   r  r  r   r   r  r   r   r8  r  r  )r   r   r"   to_be_boundr  r  s         r   r  %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  r-  zInput mutation detected at TFr  )r  r   r  r  r+  r   r   r   r   r  rI  zipr   )r   input_versions_at_beginninginput_nodesinput_versions_at_endr   r-  rS  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  r  tree_leavesr   r   r  r  r  )r   r.  r/  input_storagesr   r-  storager   output_storages	out_nodesout_nodeintersected_storagesr   aliasedrS  os                   r   r   SubgraphTracer.has_aliasing  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	
)rx  r  r{  rz  ry  r~  ru  r  rw  r  rq  r   r  r  rv  rp  rA  r  rr  r  r|  r  rt  r  rs  )NFNN)NNNr  )FN)+r   r   r   r   r   r   r   r(   r   r   r}   r  r   r  rd  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   r   r   s   @r   r  r  s  s    .2*.%)]U#]U )*]U 	]U
  ']U c]]U 
]U ]U~1_ 1 1C-C57WWC	C8 ##'DH  	
  sm C= #8RWWIrxx,?#@A 
4 ##'DHNN N 	N
 N smN C=N #8RWWIrxx,?#@AN 
N Nh "#'  	
  sm C= 
 46 6D 66 #'zz z 	z
 z  z 
zz+XX+	y"(("	#+Z7s 7s 7@y5 y5+0EHHNN1J+Ky5	y5x~"5<<#=>~EMfEU~	~D9 9ell9K 9*'L ':+'l +' +'r   r  r   (  r   r  r  r  r  r  rt  r  r/   r  r  rP  r  r   r  ro  collections.abcr   r   r   r   r   r_  typesr   typingr	   r
   r   r   r   typing_extensionsr   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedr'  torch.nntorch.utils._pytreerg  rh  r  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   rp  r    %torch.fx.experimental._backward_stater!   r  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   bytecode_transformationr3   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   variables.builderrw   rx   ry   rz   variables.ctx_managerr{   variables.functionsr|   r}   variables.listsr~   variables.miscr   variables.nn_moduler   r1  r   r   r   variables.torch_functionr   variables.user_definedr   r  r   torch._dynamo.packager   r  r    torch.multiprocessing.reductionsr   	getLoggerr   r   r  getArtifactLoggergraph_tabular_logrn  graph_sizes_logr  rw  r   r   r  r3  r  r2  r   r   r   r   r   r   r`  r   r  r   r  r   cacher   r   r   r   r   objectrG  r  r*  rU  rb  rp  rr  r  r>  rW  re  r  r  rf  rg  r  Tracerr  r   r   r   <module>rk     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?!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W&2# W&2tL!V !VJJ JJ%(J25J=@JJMJ	JZ  oo' cNCL2 2$z'RYY z'r   