
    ȅi                       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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	K/J0r0  SS
K1J2r2J3r3  SSK"J4r4J5r5J6r6J7r7J8r8J9r9  SSK:J;r;  SSK<J=r=  SSK>J?r?  SSK@JArA  SSKBJCrC  SSKDJErE  SSKFJGrGJHrHJIrIJJrJJKrKJLrL  SSKMJNrN  SSKOJPrP  SSKQJRrR  SSKSJTrT  SSKUJVrVJWrWJrXJYrY  SSKZJ[r[J\r\  SSK]J^r^J_r_J`r`JaraJbrbJcrcJdrdJereJfrfJgrgJhrh  SSKiJiri  SSKjJkrk  SSKlJmrm  SSKnJoro  SSKWJprpJqrqJrrrJsrsJtrt  SS KuJvrvJwrw  SS!KxJyry  SS"KzJ{r{  SS#K0J|r|J}r}  SS$K~Jr  SS%KJrJrJr  SS&KJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SS'K*JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SS(KJrJrJrJr  SS)KJr  SS*KJrJr  SS+KJr  SS,KJr  SS-KJr  SS.KJrJrJr  SS/KJr  SS0KJr  \(       a  SS1KJr  SS2KJr  SS3KJr  \GR                  " \5      r\#GR                  GR                  \S45      r\#GR                  GR                  \S55      r\#GR                  GR                  \S65      r\#GR                  GR                  \S75      r\0GR                  r\#GR                  GR                  GR                  r\#GR                  GR                  GR                  r\" S8S99 " S: S;5      5       r\" S8S99 " S< S=5      5       r\" S8S99 " S> S?5      5       r " S@ SA5      r\GR                  SB\4SC j5       r\ " SD SE5      5       rSF\SB\/ \\   4   4SG jr " SH SI\#GR                  GR                  5      r " SJ SK5      r\\\4   r\ " SL SM5      5       r\ " SN SO5      5       r\ " SP SQ5      5       rSR\SB\\\4   4SS jr " ST SU\5      r " SV SW\5      r " SX SY5      rSZrS[\S\\S]\S^\S_\SBS4S` jr\GR                  " 5       r\" Sa5      r\ " Sb5      r " Sc Sd5      r " Se Sf\-GR                  5      rg)ga  
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)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)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count_callscountersdynamo_timedget_instruction_source_311get_locals_to_stealget_static_address_typeget_unique_name_wrtgraph_break_reasonsincrement_op_count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)CompilePackage)InstructionTranslatorBase)StorageWeakRefgraph
graph_codegraph_sizes
trace_callT)frozenc                   *    \ rS rSr% \\S'   \\S'   Srg)VariableTrackerCacheKey   vt_idsource N)__name__
__module____qualname____firstlineno__int__annotations__r   __static_attributes__r       T/home/james-whalen/.local/lib/python3.13/site-packages/torch/_dynamo/output_graph.pyr   r      s    J Nr   r   c                   *    \ rS rSr% \\S'   \\S'   Srg)AliasingInfo   has_aliasingmsgr   Nr   r   r   r   boolr   strr   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   c                   h    \ rS rSrSS jrS\S\S\\   4S jr	S\S\S\SS4S	 jr
SS
 jrSS jrSrg)VariableTrackerCache   returnNc                     0 U l         g Ncacheselfs    r   __init__VariableTrackerCache.__init__   s	    EG
r   valuer   c                 j    [        [        U5      U5      nX0R                  ;  a  g U R                  U   $ r   r   idr   )r   r   r   keys       r   lookupVariableTrackerCache.lookup   s-    %bi8jj zz#r   vtc                 J    [        [        U5      U5      nX0R                  U'   g r   r   )r   r   r   r   r   s        r   addVariableTrackerCache.add   s    %bi8

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

r   r   r   N)r   r   )r   r   r   r   r   r	   r   r   rx   r   r   r   r   r   r   r   r   r   r      sR    HC  H_4M  f / d r   r   r   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)GraphCompileReason   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   rj   appendr   s    r   __post_init__ GraphCompileReason.__post_init__   s    &&t, r   r   r   )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   )superr   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__  s    $r   c                 N    UR                  5        H  u  p#[        XU5        M     g r   )r   r   )r   r   r   r   s       r   add_nn_modulesFakeRootModule.add_nn_modules  s!    $$&DADQ 'r   r   )r   r   r   r   r   dictr   torch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   gmexample_inputsc                    [        U5      U l        Xl        [        R                  " U R                  5      nU R                  X25      U l        U R                  b#  U R                  U R                  R                  L a  U R                  R                  $ [        R                  (       d  U R                  $  U R                  R                  " [        U5      6 nU R                  " [        U5      6 n[        XE5      (       a  U R                  U R                  5         $ [        SU  35      e! [         a    [        R                  S5        e f = f! U R                  5         f = f)Nzincorrect results of backend zerror in verify_correctness)r`   restorer   copydeepcopyr   	candidateforwardr,   verify_correctnessrb   rp   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   r1   r   r   r   GraphModuler   r   r0   r  r   r   r   r   r   r     sE    +
 +t +((&&8<U\\8J	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   )r  r   s    r   r   OutputGraphGuardsState.guards_  s    ||r   c                     U R                   $ r   )r  r   s    r   aotautograd_guards)OutputGraphGuardsState.aotautograd_guardsc  s    '''r   c                 \   [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S9$ )N)r  r  r  r  r  r  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   dump_guards_state(OutputGraphGuardsState.dump_guards_stateg  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TorchFunctionModesetr   r   r   r	   r   
_functorchpyfunctorchFuncTorchInterpreterr   device_C_dynamor   GlobalStateGuardr  	GuardsSetGuardEnvExprr  r   r  r  r  propertyr$   r#  r(  r+  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   r=  r   r   dc_fieldr   r@  r   r   rA  rB  rC  tupler	   rD  rE  r   r   r   r   r<  r<  |  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   r<  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  r>  Ngraph_input_idx_to_local_sourceoutput_return_typeout_specmodule_call_specr   )r   r   r   r   rF  r   rJ  r   r   r   rK  rG  r   r	   r   utils_pytree
_LEAF_SPECrL  r   TreeSpecLeafSpecrM  r   r   r   r   rI  rI    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   rI  r  c                 R    U S   n[        U[        5      (       d  UR                  nU$ )N__builtins__)
isinstancer   __dict__)r  
f_builtinss     r   get_builtins_dictrX    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  r  r  r  r  r  r  r  r  r  r  r\  r$   
_shape_envrI  r]  r^  )r   r[  r\  r#  r]  r^  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   )r`  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   )r`  r]  r\  r^  NNNN )r   r   r   r   r   r  r   r   r   r$   rI  r   r   r   r   r:  r#  r	   rg  r   r   r   s   @r   rZ  rZ    s    4 48(,48HL$
#9$
 !c3h0$
 H%	$

 ".1$
 %-T#tF|2C-D$E$
 
$
 $
L 8  "S " " " "r   rZ  c                      \ 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SS jrSS jrS\4S jr SS\S\S\\\R6                  R8                  4   4S jjrS\R6                  R8                  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\R6                  RR                  4S( j5       r*\*RV                  S)\R6                  RR                  SS4S* j5       r*\%S\\\R8                  4   4S+ j5       r,\%S\\RZ                  \R\                  4   4S, j5       r/\%S\\0Rb                  \2\R6                  R8                  S-4   4   4S. j5       r3S \	S/\	S\R6                  R8                  4S0 jr4S \	S/\	S\R6                  RZ                  4S1 jr5S \	S/\	SS4S2 jr6\7Rp                   SS3\
\9   S4\
S$   S5\
\   S\:S6   4S7 jj5       r;\%SS8 j5       r<\%S\Rz                  R|                  4S9 j5       r?\%S\@4S: j5       rA\%S\R                  R                  4S; j5       rD\%S\\\	4   4S< j5       rE\%S\\R                  R                     4S= j5       rG SS>\
\\\\!S\	4   \4   4      SS4S? jjrHSSA jrISSB jrJ\%SSC j5       rKS\L4SD jrMS\4SE jrNS\4SF jrOSG\S\2\R                  R                  \	4   4SH jrRSSI\S\4SJ jjrSSI\SS4SK jrT\USL\	S\4SM j5       rVSN\SO\	S\R8                  4SP jrWSQ\2\R                  R                  \R\                  \	4   SL\	SR\	S\4SS jrXS@SS\\\Y   \\Z\Z4   4   4ST jr[S@SSU\LS\\\   \\4   4SV jr]  SS@SSW\^SX\SU\LS\\\   4
SY jjr_S@SSZ\`SS4S[ jraS@SS\\\   SZ\`S]\SS4
S^ jrbSS_ jrcSSW\S/\	SS4S` jjrdS\\\\2\L\4      4   4Sa jreSI\S\4Sb jrf\7Rp                  S\	4Sc j5       rgSSd jrhS@SSe\\   Sf\iS\\Y   4Sg jrj\%S\\RZ                     4Sh j5       rk\%S\\l   4Si j5       rmSj\R                  Sk\\.   S\o4Sl jrpSj\R                  Sk\\.   S\o4Sm jrqS\\\R6                  R                  4   4Sn jrrSI\So\R6                  R                  S\4Sp jrsS\\R\                     4Sq jrtSSr jruSSs jrvSSt jrwS\\Y   SS4Su jrxSI\S)\	SS4Sv jryS\S)\	S\4Sw jrzS\S)\	S\4Sx jr{SSy jr|Sz\!\R                  /S4   SS4S{ jr}S|\R6                  RZ                  S\	4S} jr~S~\R                  R                  S\ZSS4S jrSrg)OutputGraphi  ac  
Wrapper class to hold outputs of InstructionTranslator.  Mainly the
generated fx.Graph.

OutputGraph is 1:1 with a frame being processed. Each frame is associated
with some root InstructionTranslator. When user code calls a function,
we construct a InliningInstructionTranslator that continues to write into
the root InstructionTranslator's OutputGraph.
side_effectscode_optionscompiler_fnroot_txr   r  r  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        X@l        XPl        X`l        / U l        [9        [:        5      U l        [        5       U l        U	R@                  U	RB                  U	RD                  S.U l#        [I        5       U l%        / U l&        [O        U RL                  U=(       d    [P        RR                  U=(       d    [P        RT                  [P        RV                  U RF                  S9nSS K,Js  J(n  UR[                  SS9   [        R\                  R_                  U[a        U R0                  5      U R0                  S9nS S S 5        [c        W5      U l2        U Rd                  Rf                  Ri                  U	5        U Rd                  Rf                  U l3        [j        Rl                  " 5       U l7        U Rq                  5         [r        Rt                  " [v        5      U l<        0 U l=        [}        U 5      U l?        [        5       U lA        [        R                  " 5       U lD        [        U5      U lF        / U lG        SU lH        / U lI        X lJ        X0lK        XlL        0 U lM        / U lN        / U lO        SU lP        0 U lQ        [        R                   R                  5       U lS        SU lT        [        0 5      U lU        [        0 5      U lV        U R                  5         0 U lX        / U lY        S U lZ        / U l[        0 U l\        S U l]        S U l^        U R                  5       U l`        [        R                  " 5       U lc        U R                  5       U le        0 U lf        [        5       U lh        [        5       U lj        g ! , (       d  f       GN7= f)	N)r  r  r  r  r  r  r  r  )	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_inputsr  )kr  r   r0  r   autograd
forward_ad_current_levelr1  r2  #retrieve_all_functorch_interpretersrN  _deviceCURRENT_DEVICEr6  convert_frameinitial_global_stater5  r   r7  r  r8  SubgraphTracertracersinput_source_to_varr  r  rr  cleanup_hooksnext_compile_id_counter
compile_idinstalled_globalsrx  ry  rz  r  rI   region_trackerr{  r$   r,   capture_scalar_outputs capture_dynamic_output_shape_opsr~  torch._functorch.configpatch_subclassesFakeTensorModer   r   tracing_contexttraced_coder   r   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   r^  param_name_to_sourcerN   rn  r   variable_tracker_cache	itertoolscountunique_var_idr   ro  output_instructions	timestampregister_finalizer_fnsrp  rq  rt  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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_fglobalsr  
contextlib	ExitStackcompiler_trace_stack+maybe_install_saved_tensors_hooks_subgraphs"saved_tensors_hooks_subgraph_namesr\  rI  r]  r)   "used_inlined_inbuilt_modules_names)r   ro  rp  rq  r  r  rr  r  r  rs  r  rt  ru  r#  _config	fake_modes                   r   r   OutputGraph.__init__  s    	''%"u*,~~00??"--99]]_ ;;..==$}}::OO  :xx&&779MM++- " 	( 	
" 't>? CE "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!'- ';&<#&__.,0,>68   OQ# 2= RT"<>+- KM  ,188+S+S+U( 27. >AW ADB! 	  AC-  	 '+ 57 ;=>B!15 224 	2 %/$8$8$:! <<> 	/
 /1-/ DN</C KJs   =Q;;
R
c                 J    U R                   R                  [        SSS95        g )Nbytecode_tracingTlog_pt2_compile_event)r  enter_contextre   r   s    r   mark_bytecode_tracing_start'OutputGraph.mark_bytecode_tracing_start  s$    !!//"&*	
r   c                 8    U R                   R                  5         g r   )r  closer   s    r   mark_bytecode_tracing_stop&OutputGraph.mark_bytecode_tracing_stop  s    !!'')r   c                 P    [        U R                  5      nU R                  SU5      $ )N__builtins_dict__)rX  r  install_global)r   rW  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   )lenr  get_backward_state_proxy)r   r  r  names       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 exportrk  z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_stater   grapharg)r  r  rD   r    root_tracercreate_graph_inputr"  rR   rr   node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_guardrJ   	SHAPE_ENVrU   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICEGLOBAL_STATETORCH_FUNCTION_STATEr   r5  r1  peek_interpreter_stackFUNCTORCH_STACK_MATCHr6  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   r6  r  r  r1  _aot_autogradrN  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>V  s    B''r   F)r  r>   rq  add_push_nullforeachmapr/   ConstantVariablecreatecall_functionr  storer  extendget_instructionsr\   rx   buildrealizer   getguards_contextdynamo_guardsremove_guards_with_source)r   r   r   r  varnamer   r  r  s    `     @r   synthetic_graph_input!OutputGraph.synthetic_graph_inputL  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_hookh  s    !!"%r   c                 ~    [        U R                  5       H
  nU" 5         M     U R                  R                  5         g r   )reversedr  r   )r   r  s     r   call_cleanup_hooksOutputGraph.call_cleanup_hooksk  s.    T//0DF 1  "r   r  c                      U R                   S   $ Nr   r  r   s    r   r  OutputGraph.root_tracerp  s    ||Ar   c                      U R                   S   $ Nr,  r   s    r   current_tracerOutputGraph.current_tracert  s    ||Br   c                 2    [        U R                  5      S:H  $ )Nr+   )r  r  r   s    r   is_root_tracerOutputGraph.is_root_tracerx  s    4<< A%%r   c                 .    U R                   R                  $ r   r1  r   r   s    r   r   OutputGraph.graph|  s    ""(((r   r   c                 $    XR                   l        g r   r7  )r   r   s     r   r   r8    s    $)!r   c                 .    U R                   R                  $ r   )r1  input_name_to_proxyr   s    r   r;  OutputGraph.input_name_to_proxy  s    ""666r   c                 .    U R                   R                  $ r   )r1  real_value_cacher   s    r   r>  OutputGraph.real_value_cache  s    ""333r   	LazyProxyc                 .    U R                   R                  $ r   )r1  bound_symbolsr   s    r   rB  OutputGraph.bound_symbols  s    ""000r   r   c                 :    U R                   R                  " U0 UD6$ r   )r1  create_proxyr   r   r   s      r   rE  OutputGraph.create_proxy  s    ""//@@@r   c                 :    U R                   R                  " U0 UD6$ r   )r1  create_noderF  s      r   rI  OutputGraph.create_node      ""..???r   c                 :    U R                   R                  " U0 UD6$ r   )r1  remove_noderF  s      r   rM  OutputGraph.remove_node  rK  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)parentrO  rw  rQ  )
r?   rS  r1  	__enter__r  rw  r  r   __exit__pop)r   rO  rP  rQ  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.output  s    r   c                 `    U R                   R                  c   eU R                   R                  $ r   )r  r  r   s    r   r  OutputGraph.fake_mode  s,    ##--999##---r   c                     U R                   R                  c   eU R                   R                  R                  c   eU R                   R                  R                  $ r   )r  r  r#  r   s    r   r#  OutputGraph.shape_env  sP    ##--999##--77CCC##--777r   c                 B    U R                   R                  R                  $ r   )r  r  r  r   s    r   r   OutputGraph.guards  s    ##22@@@r   c                 B    U R                   R                  R                  $ r   )r  module_contextr   r   s    r   r   OutputGraph.nn_modules  s    ##22===r   c                 B    U R                   R                  R                  $ r   )r  r  r(  r   s    r   r(  OutputGraph.aotautograd_guards  s    ##22EEEr   outc                 @   [        [        [        [        [        S[
        4   [        4   4   Ub  UOU R                  R                  R                  5      n[        R                  [        R                  " 5       4US'   [        R                  " [        R                  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   rG  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   ri  rt  s      r   r  OutputGraph.save_global_state  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  rV  r   s    r   pop_txOutputGraph.pop_tx  s    ##%%r   c                 Z    U R                   (       d  U R                  $ U R                   S   $ r/  )r  rq  r   s    r   
current_txOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMr   c                 ,    [        U R                  5      $ r   )rc   r   r   s    r   rc   OutputGraph.count_calls	  s    4::&&r   c                 X    [        [        U R                  R                  5      5      S:H  $ r+  )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$ N.)r   splitrU  r   getattr)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_varnames_)r0  ro  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)ro  )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)joinr  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   )
rU  r   r   r   r   r   rE  ri   rq   r  )r   r  r  r  modproxy	attr_names          r   %register_static_attr_and_return_proxy1OutputGraph.register_static_attr_and_return_proxyB  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  rq  r\  rn  rh   rU  rY   rK   r  rJ   ID_MATCHrV   TENSOR_MATCHru   rE  track_object_existingas_proxyr  r  r_   )r  r   r  r   r   r  rX  s     r   	wrap_name6OutputGraph.register_attr_or_module.<locals>.wrap_nameu  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    s    +DL*fXPWXXr   c                 2   > [         R                  " T40 TD6$ r   )r/   UnspecializedNNModuleVariabler  s    r   r  r    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}   r  rq  rE  )r  r  r   r  s    r   r  r    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  rx   r  rS   )r  r   r  s    r   r  r    sF    ++J706!!*-&,,"z: r   	leaf_namec                    > TR                   c   e[        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  rZ   rU  rX   r  rm  r  r  r  
new_sourcenew_namer  r   r   s      r   register_leaf_name?OutputGraph.register_attr_or_module.<locals>.register_leaf_name  sz    00<<<.vyA
"V1YK06@))(3fk22 " 99#33JOOD 3r   _parameters_buffers)rL   r  rx   r  r  r   rU  rZ   r   r   r1  r4  r  r   r   r   rK   r  rJ   	NN_MODULESymIntSymFloatr   r   rm  r  ri   r  hasattrnamed_parametersnamed_buffers)r   r  r  r  r  r   r   r  r  r  r  r   rX  s   `` `      @@@r   register_attr_or_module#OutputGraph.register_attr_or_moduleU  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  rg   stacksymbolic_localsvaluesrn  store_attr_mutationsr  rV  rU  ry   r   r   mutation_typerM   r   rT   baserX   
local_namer   	graphargs_examplero  indexr  r  r7   r8   r3   )r   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuer  stolen_namevisited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.+ e. 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[        UR                  5       GH  u  pV[        R
                  R                  U5        [        UR                  5      U-
  U::  a  UR                  U5        MT  [        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M     [        U5      Ul        [%        UR'                  5       UR)                  5       -   5      nUR*                  R-                  5        GHP  u  p[        U
R.                  [0        5      (       a*  U
R.                  R2                  U	:X  a  XR4                  L a  MO  X;   a  MV  [6        R8                  S:  a=  [:        R=                  [        U
5      (       a  UR>                  R                  U	5        M  O![:        R=                  [        U
5      (       a   e[        UR@                  5      UR@                  U	'   [        U
[        5      (       a@  U
R                  c  SO[        U
R                  5      nURB                  R                  X45        UR                  U
5        GMS     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+   )      )"prune_dead_localsr<  	enumerater  r/   LazyVariableTrackerrealize_allr  r   rU  rz   rA  rv   target_valuesrG  rC  rD  r=  r0  cellvarsfreevarsr  r   r   rX   r  rq  sysversion_infor"  __instancecheck__rB  r@  rE  )r   r  r  stack_valuesr  ir   r  cell_and_freevarsr   r   s              r   _get_stack_values_to_restore(OutputGraph._get_stack_values_to_restore?  sY   " 	"$ ""((+HA))55e<288}q J.##E*%..%%,,Q/##E*%!899--5B5ATAT;U  ##**C,=,A=+QR))003! ,$ \* => &&,,.DA 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,>?"C /F !!r   r   partial_convertc                    U R                   c   e[        R                  (       d  U R                   UL d   eU R                  5         X0l        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	R0                  n	U	b  M  U R2                  R5                  U5        U R7                  U5        U R8                  (       a  U R:                  (       a   S5       eU R7                  U R8                  5        U R=                  U R                   5      u  pU R7                  U5        U R?                  5         U R@                  RC                  5        VVs0 s H  u  nnU[E        U5      _M     nnn[G        U5      nSSK$J%n  [        U RL                  5      S	:  a  / nU RO                  S5      U l(        U" [S        U RL                  5      SS9nU RU                  SU5      n[W        U R                   UUS9nURY                  UR[                  US5      5        URY                  []        S	S5      5        UR                  UR_                  U RP                  5      5        U R7                  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  [        [a        U5      5      [        U5      :X  a  U R2                  Rc                  5       (       a  URd                  (       d  U Rf                  (       d  US   Rh                  (       d  US   Rj                  (       d  [W        U R                   5      nU Rm                  UU5        U R7                  / U Ro                  U[q        [+        U5      5      U5      QURs                  5       Q[u        S5      Q[        S[        U5      S9P5        GO-U RO                  S5      n[W        U R                   UUUS9nU Rw                  UUUS5        0 nURx                  RC                  5        H0  u  nn U S:  d  M  [{        U[|        [~        45      (       a  M+  SUU'   M2     [W        U R                   UUUUS9n!U Rw                  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"RB                  S	   n#U"RB                  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#RB                  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                  lY        / n+[        U R                  5      S	:w  d  [        U!R                  5      S	:w  a  U+RY                  U Ro                  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 R7                  U+U!Rs                  5       -   5        U R7                  [        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 R7                  [        S'S	S9/[u        S5      Q5        O;U-U/-  n-U R7                  [        5       /[        U,U-5      Q[u        S5      Q5        U,U/-  n,U.[        U5      S-
  :X  d  M  [W        U R                   5      n00 n1U R                   R                  RC                  5        H  u  n2n3[        U3R                  [~        5      (       d  M'  U3R                  R                  U2:X  d  MC  U0R                  U0R                  U25      5        [        UR                  5      [        U15      -   U1U2'   M     U R7                  U0Rs                  5       [        S'[        U15      S9[        S(SS9/-   5        UR                  R                  U15        GM     U R7                  [        S&5      [        S'[        U5      S9/[        US	   R                  S-   5      Q5        U(       a!  U(       a  U R7                  [        S)US*9/5        [        R                  R                  R                  S+;   Ga  S	S,KkJln4  / n5U R2                  R                  5        GH{  n6[        U6S-5      (       d  M  U6R                  n7[        U7[        [        45      (       d  M@  [        U6[        5      (       a  [        U6R                  U45      (       a  U6RB                  RC                  5        H  u  n2n30 n8U3RB                  RC                  5        H1  u  n9nUR                  5       U8U9R                  R                  5       '   M3     S.S//[q        U8R                  5       5      :X  d   eU8U R                  R                  U2R                  R                  5       '   M     [        U6[        5      (       a  [        U6R                  U45      (       a  GMj  U5R                  U65        GM~     U5 V6s/ s H  n6[        U6R                  5      PM     n:n6U:(       aQ  [        R                  R                  R                  S0:X  a  [        R                  " S1U: 35        U$ [        S1U: 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n6f )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r     COPY_FREE_VARSco_freevarsr  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)r  Fc              3      #    U  H^  n[        U[        [        [        45      (       + =(       a2    [        U[        5      =(       a    UR                  5       [        L (       + v   M`     g 7fr   )rU  r~   r|   r   r}   python_typefloat)r  r   s     r   r  r  /  sa       +A 3,4  V $A7TAMMOu<TUV +s   A&A(c              3   @   #    U  H  oR                  5       v   M     g 7fr   )	is_tensorr  r  s     r   r  r  ;  s     =+<aKKMM+<r  r0     UNPACK_SEQUENCE	graph_out)tempvarsr  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_specrL  r7  zWhile compiling, we found certain side effects happened in the model.forward. Here are the list of potential sources you can double check: ){rq  r,   nested_graph_breaksr  r  compile_subgraph_reasonr  r   debugr
  r  prefix_instsopnamer   r7   r  ro  r  allblock_stackr  r'  exitr   rS  rn  prune_dead_object_newadd_output_instructionsr  r  r  cleanup_graphr   r   ro   r   
decoratorsr"  r   r  r  r   r  r>   r  load_function_namer5   create_storer0  is_emptydebug_localsr  rA  rB  codegen_cellscompile_and_call_fx_graphr   r  r:   codegen_suffixusesrl   r\   rX   r   r6  log_graph_in_out_metadatarU  r/   NamedTupleVariable	tuple_clsfunctional_exportExportTracerOutputListVariablegraph_outputsr  codegenGraphOutputEntryvariabler  r  r]  rK  r   r  is_inputis_python_constantas_python_constantr!  rL  rw   rD   rc   r   graph_output_varsrun_compiler_collectiver=  r@  r6   r2   r  r  append_outputcreate_loadr   r9   side_effect_replay_policytorch.export._tracer9  _get_modified_varsr  r  rM   rO   r   r   r   r  rM  rP   warningsr7  r  );r   r  r   r  r  r>  install_stack_valuesall_stack_locals_metascur_txr  r  r  r  r  r  r  nn_modules_proxiesrootr"  random_calls_instructionsrand_fnrand_fn_namerV  valsvalstack_values_flatstored_graph_output_vargraph_output_varcell_cgpass1r/  r  pass2r   flat_returnsrL  vt_to_graph_out_idxr   rX  idxr   er\  	start_idxend_idxr  n_valsroot_cgunmodified_locals_namesr   r   r9  potential_side_effectsr  mut_typespecsk_specside_effect_refss;                                                              r   compile_subgraphOutputGraph.compile_subgraph  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
8O94D/#&&8O 	 
 01' t  !A%(*%%)\\/%BD"'(9(9:GG  ../BGLLd7IG &,,**<> &,,-A!U-KL%,,$$T%;%;< (()BC& .>N-=T#SS-=N"' LLB!  +   =+<===C)*+s3D/EE!!**,,OO''*2.??*2.??  -Gr7+((33D*;!<=t
 --/ !^ ''8cBS>TU
  $||K8  #5	E $5ueD H#jj..0
U19VC2F1T%U%U$(HSM 1  !#5E $5udC $$>>>%)*a/&q)r5==#:#:#M#MNN}}66IIJ $&88A;L!xx{H%$emm&=&=&J&J    GI'!&!4!4!;!;!=)%1F1F1W1WXXXX49NN8=+H5 ">
 $-\-?-?#@R!44 + 3B 7LD00CCCH
 II1+2299fd+K!K!K & !( "		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#>9 >< ?U >Ts&szz2>T     ==''AAVKMMXXhWik &% 'XXhWik 
 &%Q
Z Oz 2 %$^&Fxj$Q! !j R# &' \ s*   x&x x& y&
y0yyr  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+   r4  r  )r<  r   rG  sortedr  rq  r^  create_load_closurepost_prune_cell_and_freevarsr4   r  rS  r7   )r   r  r  tx_cntrg  r	  cells          r   rK  OutputGraph.codegen_cellsd  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   r  log_side_effectsc                 `  ^^	 U R                   R                  T5        U R                  (       a  U R                  (       a   eU R                  R	                  5        HW  u  pVT" U5        U R
                  c   eTR                  TR                  U R
                  5      5        TR                  U5        MY     [        R                  (       a  U R                   R                  T5        UR                   Hl  u  m	nTR                  UU	4S j5        U H  nT" U5        M     TR                  [        [!        U5      S5      5        TR                  [#        S5      /5        Mn     U R%                  UT5        TR'                  X!R                  (       + S9  U R                   R)                  TU5        g )Nc                     > T " T5      $ r   r   )r  	debug_vars   r   r  ,OutputGraph.codegen_suffix.<locals>.<lambda>  s	    R	]r   Fr3  )value_from_source)rn  codegen_save_tempvarsr  r  r   r  r^  r_  
store_attrr,   replay_side_effectscodegen_hooksrJ  r  extend_outputr5   r  r7   rK  restore_stackcodegen_update_mutated)
r   r  r  r  r  r  rn  r   r  r  s
      `     @r   rM  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  rV  r   rv  r  pairwiser  r5  _set_grad_enabledrG  r   _erased
erase_node)r   r  r  rk  node1node2s         r   rE  OutputGraph.cleanup_graph  s    TZZ%%&DIIMM.5  ,,.%..u5LE : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MMM#(::a=LJJ))%0JJ))%0 6r   c                   ^^ U R                   (       d  g[        R                  R                  R                  (       a*  [        R                  R
                  R                  ST5      e[        R                  ST5        [        R                  R                  SS UU4S jS9  U R                   R                  5         SU l         g)z5
Do not save this output graph to the CompilePackage
NzDetected a package bypass: %sartifactc                      SSS.$ )Nprecompile_cache_bypassjsonr  encodingr   r   r   r   r  ,OutputGraph.bypass_package.<locals>.<lambda>  s    1"!r   c                     > ST0T E$ )N_reasonr   )r   r   s   r   r  r    s    6   r   metadata_fn
payload_fn)rt  r   r6  r,   strict_precompiler-   PackageErrorr   warning_loggingtrace_structuredbypass_current_entryrf  s    ``r   rg  OutputGraph.bypass_package  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  rU  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  rU  r   r  r   r  r  rG  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
)ri  N)r  rs  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   r  5OutputGraph.run_compiler_collective.<locals>.<lambda>  s    1 (%r   c                  8   > T R                   R                  5       $ r   )local_staterender)dss   r   r  r    s    2>>#8#8#:r   r  r+   z&Expect only one device type but got {}+r  Tr  )group)rq  distributed_state
all_states
compile_pgr   infor  r   r  r  _device_typesr  formatr  r@   rV  r4  rankacceleratordevice_countre   r  distall_gather_objectspeculation_logr   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rvri  c                 b  ^ ^!^"^#^$^% [         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                  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        [,        R.                  (       d  [1        U5       HO  n[3        X85      n	[        U	[4        R6                  5      (       d  M/  [9        U	T R:                  UT R<                  S
9  MQ     T R?                  5         [9        [4        R6                  " UT R                  5      T R:                  UT R<                  S
9  T RA                  5         [        T R                  5      n
[B        S   S==   U
-  ss'   T RE                  5         T RF                  RI                  5         [K        UT R                  5      m#SSK&J'n  U" T#5        T RP                  (       a)  T RP                   H  n[S        T#U[3        X<5      5        M     T RT                   H  nU" T#5        M     [W        T#RY                  5       S5      bB  T R[                  S[         R\                  R,                  R^                  T#Ra                  SSSS9S9  T Rb                  b  UT#l2        T Rf                  T#l3        T Rh                  Rk                  5       T#Rl                  S'   T Rn                  T#Rl                  S'   UT#Rl                  S'   [p        Rs                  S[u        UT#SSSS95        [         Rv                  Ry                  SU 4S jU#4S jS9  T R{                  5         T R|                  R~                  nUc   eT R<                  (       dY  SSK@JAs  Jn  UR                  SS9   [         R                  R                  UR:                  S9nSSS5        WT R|                  l?        T R                  5          T R                  T#T R                  5       5      m"SSS5        SSKHJIn  [        T"U5      (       d,  [        [3        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 Rb                  b  T Rb                  R                  UT"5        U" T"S S!9m"[B        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[        Rs                  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s  snf ! , (       d  f       g= f)*z
Generate code from self.graph and return the Instruction()s to
call that generated code.

Code is generated w.r.t. self.root_tx.
tx is only used for preserving GraphModule metadata
r+   r!  r   N__compiled_fnT)	with_uuidr\  c              3   @   #    U  H  oR                  5       v   M     g 7fr   )r  r+  s     r   r  8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>N  s     5OBqjjllBr  )r  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)r  r  coloreddynamo_output_graphc                  (   > ST R                  5       0$ )Nsizes)r  r   s   r   r  7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    $"A"A"CDr   c                  &   > T R                  SSSS9$ )NFTr  )print_readable)r   s   r   r  r    s    2#4#4!&tD $5 $r   )r  r  )r#  )_LazyGraphModule__self___lazy_forwardz"do not trace Dynamo-compiled graphr$  unique_graphsz:Compiling backend specialized graph with specialization=%sc                     U" X   5      $ r   r   )rw  r   check_fns      r   r  r    s    X$(IF"r   r   r   r   c                    > T	 H  u  p#U" U 5      (       d  M  UT;   a  TU   " U 0 UD6s  $ TR                   R                  UR                  UR                  5         UTR                  S'   [        U 5      n[        TR                  5         TR                  TU5      TU'   S S S 5        S S S 5        TU   " U 0 UD6s  $    T" U 0 UD6$ ! , (       d  f       N-= f! , (       d  f       N6= f)Nspecialization)	r#  patch_source_specializationr   r  r  r   r   r  call_user_compiler)
r   r   r  r  r   compiled_fnr   r   specialization_cachespecialization_guardss
        r   specialized_dispatchCOutputGraph.compile_and_call_fx_graph.<locals>.specialized_dispatch	  s    4I0#D>>-1EE';N'K%)("-3(" !" "&!K!K . 5 5~7N7N" =K(8 9?CDz%,T-A-A%B(,(?(?N(S %9$H &C" $8#G#XQW#XX% 5J& '777 &C%B" "s$   0CC!C
CC
C'	c                  l   > T R                  [        R                  R                  R                  S5      $ )Nstore_user_object_weakrefs)r  r   r6  graph_bytecode_inputsr   )r  s   r   r  r  /	  s&    B//;;DD4r   )qr   r  r   clear_framerF  r"  r  r]  rc   r   r  r<   rU  r   r   rI  r1  
create_argrG  
dedup_passr   _maybe_preserve_original_metar,   do_not_emit_runtime_assertsdirr  r   r  r(   r#  r  remove_unused_get_attr_nodesremove_unused_graphargsrd   &remove_tensorify_specialized_graphargsr>  r   r   dce_extra_outputsr  r  r   r  r  
parametersrg  r6  inline_inbuilt_nn_modulesr  rt  _backend_idr<  r  r  r  r  graph_code_logr=  rm   r  r  r(  r  r  r  r1  r  r  r  r  r  r   torch.fx._lazy_graph_moduler  r   r   force_recompiler  add_backend_idspecializationsr  r   r  inspect	getsourcer  stripRootGuardManagerr   LAMBDA_GUARDr   r   rw  rx  r	   install_global_unsaferq  r>   rF   r  rG   r  r  r  r^  r_  r  pop_topr  r]  rJ  make_call_generated_coder  )&r   r  r  ri  r"  r  output_nodesub_gmsattrsubgraphncallsr  subgraph_nameregister_finalizerold_fake_moder  backend_fake_moder  lazy_gmr"  asourcesr  source_indexcheck_fn_sourceunused_root_guard_managerr  r  tmp_varsconstructorvar_namerw  r  r  r	  r   r
  r  s&   `                                @@@@@r   rL  %OutputGraph.compile_and_call_fx_graph0  sp    ]]))557+####((*4::&!+B1 87 _=Db$''''dN3333**$$//5OB5O0OPR	K oo'G(==bN55  ID&t2H!(BNN;;7$ NN #';;	 & 113/NN44NN;;	 ((* ,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 ,,,;;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 ''-&&(Y 87d SR* -,L =[ 87sX   Ab C=b J<b 	(a71)b !b	;Db b,Jb 7
b	b 	
b	b  
b.c                 4    U R                   R                  SS9$ )Nplaceholderr  )r   
find_nodesr   s    r   placeholdersOutputGraph.placeholdersK	  s    zz$$$66r   c                 ^    U R                    Vs/ s H  oR                  S   PM     sn$ s  snf Nr  )rB  r  r   r  s     r   r  OutputGraph.graphargsO	  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)re   _call_user_compiler)r   r   r   s      r   r  OutputGraph.call_user_compilerS	  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                   S5      (       a  U R                   R                  OSn [        5       " [         R"                  S	U 35        U R                   n	[$        R&                  (       a  [)        U	5      n	U	" X5      n
[        5       " [         R"                  S
U 35        [+        U
5      (       d   S5       e [M        SS0 U RN                  EU[Q        UR                  R                  5      [Q        U5      S.E5        W
$ ! [,        [.        4 a    e [0         a  nU R2                  (       aD  [5        U R                   U[6        R8                  " 5       5      R;                  UR<                  5      S e[?        UU R@                  RB                  SSU S[E        U5       SU R@                  RG                  5        3SU S[E        U5       S3S/S9   S nAGNS nAf[H         a    e [J         aI  n[5        U R                   U[6        R8                  " 5       5      R;                  UR<                  5      S eS nAff = f)Nr   r  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  dynamorI  )op_count
node_countinput_count))rp  r   r  r  r   rk   r  r  r   rW  r  _param_name_to_sourcer  _source_to_user_stacksr   r   r.   INFOr,   r  r   callabler   r   rB   r  rA   r#  currentframewith_traceback__traceback__rE   rq  rs  r   format_frame_summaryrC   r  r   r  r  )r   r   r   totrB  r  plr  r  rp  r	  rx  s               r   rQ  OutputGraph._call_user_compiler`	  s    +++HHNNDwwIIqww-'##D)	 #
 	3B2/00ggj) %(JJ!  $(#<#< $($>$>! t''44 %%( 	
 	8N7<<+EdV)LM**K((,[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G L7B2J//LAL

Lc                 n    [         R                  R                  R                  (       a  [	        U 5      $ 0 $ r   )r   r6  r,   use_graph_deduplicationrH   r   s    r   r  OutputGraph.dedup_pass	  s%    ==77,T22Ir   sub_gmc                 h    [        XR                  SS9nX2l        SUl        U R	                  X#S S9  U$ )NT)requires_suffixFr  )ri   r   r   torchdynamo_force_dynamicr  )r   r  rj  	next_names       r   r  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   r  r  s      r   r   OutputGraph.example_inputs	  s&    )-8#++8 9s   (c                     [        U R                  R                  SS9SS9 H8  n[        [	        UR
                  5      5      S:X  d  M'  U R                  U5        M:     g )Nr  r@  T)reverser   )r  r   rA  r  r   usersrM  rF  s     r   r  (OutputGraph.remove_unused_get_attr_nodes	  sJ    4::00J0?ND4

#$)  & Or   c                   ^ ^^^ T R                   (       d   eS[        R                  R                  S[        4S jnS[        R                  R                  S[        4S jmS[        R
                  S[        4U4S jjnSS	KJn  [        [        T R                  R                  5      5       H  n[        [        UR                  5      5      S:X  d  M'  UR                  S
:X  d  UR                  S:X  a  UR                  [         R"                  L dc  UR                  S:X  a7  UR                  [$        R&                  L a  U" UR(                  S   5      (       d  U" U5      (       d  U" U5      (       d  M  T R+                  U5        M     S[        R
                  S[,        [.        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  )	rU  r   Noder  r  r   SymBoolr  maybe_as_bool)rx  brs      r   is_static_true;OutputGraph.remove_unused_graphargs.<locals>.is_static_true	  sw    ~fbgg..0AyDy1emm,,&&..00Q= r   r5  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  rU  r   r(  r   r   rz  r  r  )r5  r  s     r   is_symnode_arg;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_arg	  sJ    ?!c5$/00!RWW%%!!&&**_"=xHHr   r  c                 :  > SSK Jn  U R                  S:w  a  g[        U R                  R                  S5      U5      (       d  g[        U4S jU R                   5       5      (       d  g[        U4S jU R                  R                  5        5       5      (       d  gg)	Nr   r  r  Fr  c              3   4   >#    U  H  nT" U5      v   M     g 7fr   r   r  r5  r  s     r   r  WOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>	  s     <)Q~a(()   c              3   4   >#    U  H  nT" U5      v   M     g 7fr   r   r  s     r   r  r  	  s     G2FQ~a((2Fr  T)
r  r  r  rU  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  r  c                 
   U R                   S   nUR                  n[        U[        R                  5      (       aI  [        UR
                  R                  [        R                  5      (       a  UR
                  R                  $ g rE  )	r  rq  rU  r   r  r  exprsympySymbol)r  r  rq  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  rM  r>  rV  )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   r  5OutputGraph.remove_unused_graphargs.<locals>.<lambda>J
  s    .A,PQ.Rr   )2r  r   r  Argumentr   rz  %torch.fx.experimental.symbolic_shapesr  r'  r   r   r  r  ru  r  r  operatorgetitemr   _checkr   rM  r   r  r  r0  r   r  r   rB  r   rU  r  rr   rq  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__r  wrapped_objpytreetree_map_onlyr   r"  fake_tensorremove)r   r  r  r  r  r  r  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictr-  fake_attr_valr  symbolr  r  r  s   `                @@@r   r  #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   r  EOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>v
  s     ?JqF*Js   )torch._dynamo.symbolic_convertr  r   r  r  r  rU  r   	item_memor  r  _exprr@  ru  should_specializer  r   replace_all_uses_withr"   rM  )r   r  r  r  r  s        r   r  2OutputGraph.remove_tensorify_specialized_graphargs_
  s     	BJJ$$D IIMM/:M=*55!++7M3388>>GG?DJJ???"44!++0066;; 
 djj)A++L9P9P,QR$$Q' *   & %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   rD  #OutputGraph.add_output_instructions
  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   ra   r  r  )r   r  r   s      r   r(  !OutputGraph.install_global_unsafe
  sO     11111""4([//0A0A4OPr   c                     U S[        U5       SU R                   3nX0R                  ;   a  U$ U R                  X25        U$ )z
Installs a global if it hasn't been installed already.
This is determined by (prefix, id(value)) pair.

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

Returns the name of the newly installed global.
)r<   r(  r  s       r   r  OutputGraph.install_global
  s!      ""4/r   c                    S U l         U R                  R                  5         U R                  R                  5         S U l        U R
                  R                   H"  nSUR                  ;   d  M  UR                  S	 M$     U R                  R                  5         U R                  R                  5         U R                  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 rE  )rq  r   r   r  r  r   r  r  r>  r;  rn  r  r  r  r  r  r  r  rF  s     r   cleanupOutputGraph.cleanup
  s    //557$(!JJ$$DTYY&IIj) % 	##%  &&(!##))+##))+--335""$  &&(  &&(!!#r   r1  c                 :    U R                   R                  U5        g r   )r  r   )r   r1  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  rq  r   r  rF  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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
  sz    ,,888*69=Jq,H2<D%%h/&+..  55//
@ /r   r  r  )rm  r  r  ri   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   )2r  r  r  r  r  r  r  ro  r  r<  rp  r  r  r  r  r  r  r]  rr  r  r\  r  r  r  r  r  rt  r  r  r  r   r  r  r  rq  r  r  rn  r  r  r  r  r  r  r{  r^  r  r  r  r  Fr   )r  )r   r  r   )r   rm  )r  r   r   N)r   r   )tmp)Fr   rj  )r   r   r   r   r   rN   r   r   r   r	   r   r1   r   r   r   r-  r   r   r   r.  r/  r   r  r  r  rx   rG  r   Proxyr  r  r  r  r   r!  r$  r(  r:  r  r1  r4  Graphr   setterr;  rz  r   r>  r  r  r   rB  rE  rI  rM  r  contextmanagerr'   r   rY  r\  r  r  r  r$   r#  r  r8  r   r   r9  r(  r  r  r  r  r   rc   r  r  r   r   r  r  r  staticmethodr  r  r  r;   r   r  r<  r  r   r  r>   rK  rM  rE  rg  r  r  r  r]  r   rL  rB  rs   r  r  r0   r  rQ  r  r  r   r  r  r  rD  r(  r  r  r  r  r  r  r   r   r   r   rm  rm    sF	      TP38nTP j)TP -	TP
 TP %%67TP TP TP TP TP $((I(I#JTP *+TP TP 
TPl
*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`X/-X/	tK $vv~"66	7X/tY"-Y";>Y"	tO$&99	:Y"~ !&H&'H& #H& 	H&
 H& 
!	"H&T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Y)'Y) !Y) 	Y)
 
k	Y)v 7d277m 7 7 E4> E E@..@26v,@	@J..J26v,J	JXDehh&:&:!:; S %((2F2F 3 U\\ 2 
'
X0t 'D d;.?  D  
Q# 
Qc 
Qd 
Q3 s s 	S 	 	 	$,?"*BNN+;T+A"B?	?
,%((-- ,C ,$2#hhoo$27=$2	$2r   rm  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_globalsNrX  r   r8  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  r  r  r\  r  r  )r   rX  r8  s      r   r   DynamoTracerOutput.__init__  s[     %+$?$?!*0*K*K'~~))(.% $D &Dr   c                 T   U R                   nU(       a  UR                   H  nUR                  R                  5         M     UR                  R
                  (       aL  UR                  R
                  R                  (       a&  S UR                  R
                  R                  l        g g g g r   )r  r  r   _clear_nodesr  r  r#  r{  )r   r  rX  s      r   _cleanup_output_graph(DynamoTracerOutput._cleanup_output_graph  s    44&..))+ / ,,66 00::DDQU,,66@@N E 7 r   )r  r  r  r  r  r  r   r   )r   r   r   r   r   r   r   rm  rG  r	   r   r   r   r  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.htmlr  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)Nr  r  r   c                 ^   > U R                   S;   a  g TR                  R                  U 5        g )N>   atenprimprims)	namespacer  r   )r  r  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 oprk   r  )r  r   r,   only_allow_pt2_compliant_opsrD   err_epilogue)r  r   r  s     r   encountered_non_compliant_op<check_pt2_compliant_op.<locals>.encountered_non_compliant_op0  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 packetrk  r  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r   _ops
OpOverloadr   rU  Tagpt2_compliant_tagtagsOpOverloadPacketrG  	overloadsr  r  r6  rN  get_fake_values_from_nodesr  r5  _jit_resolve_packet_qualified_op_namer  rD   )r  r  r  r   r   r  r  r  r  overloadrx  s   `          r   check_pt2_compliant_opr  %  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@  ix  rX  r  r   r   r   r   Nc                 4    Xl         X l        X0l        X@l        g r   )rX  r   r   r   )r   rX  r   r   r   s        r   r   LazyProxy.__init__y  s     	r   c                 N    U R                   " U R                  0 U R                  D6$ r   )r   r   r   r   s    r   r  LazyProxy.__call__  s    ww		1T[[11r   )r   r   r   rX  )r   r   r   r   r   r  r  r   r   r   r	   r  r   r   r   r   r@  r@  x  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U 4S jjjr    S/S\S\S\S\S\\   S\\   S
\R                  4U 4S jjjrS\R                  S
S4S jr  S0S\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
\ \!RD                     4S) jr#S
\$4S* jr%S
\&4S+ jr'S,r(U =r)$ )1r  i  z
Holds an FX graph that is being traced. OutputGraph owns a SubgraphTracer
and the separation of responsibilities is that SubgraphTracer is
responsible for building the graph while OutputGraph is responsible for
compiling and executing the graph.
Nr  rm  rS  rw  rO  rQ  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  r  r   r   r  r   rw  r;  r>  rS  rO  rQ  lifted_freevarsrB  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   r  rS  rw  rO  rQ  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)
r%  r&  r'  current_instructionstarts_liner  r   r  _COPY_META_FIELDSr  )r   r  r  linenonode_idxr  r   s          r   r  ,SubgraphTracer._maybe_preserve_original_meta  s     (())++77FH!3377666 #))(3XX77E}+/;		%( 8 !D(/3M/BDIIm, ) $ * ) r   r  r  r   r   r  	type_exprproxy_factory_fnc           	        >^^^^^ U R                   b^  [        R                  " X445      u  p/ n
U H%  nU R                  U5      nU
R	                  U5        M'     [        R
                  " X5      u  p4[        TU ]  UTUUUUU5      mU R                  R                  n[        R                  S:  a  US;   a  UR                  mTU R                  La  TR                  b}  TR                  R                  bf  UR                   mUR#                  TR                  R                  S9mS[$        4UUUU4S jjn[&        R)                  S[+        U5      5        TU l        SnUR                   U R,                  La  [.        R0                  " UR                   5      R3                  SS	 5      " 5       n[5        U[6        R8                  R:                  5      (       ao  S
nUR<                  R>                   Vs/ s H  nUR@                  PM     snU l!        URD                  U l#        URH                  RJ                  RL                  U l'        OS U l!        S U l#        S U l'        URP                  nU(       a'  URS                  5       TRT                  R@                  S'   US;   a  TRT                  RV                  T4nU(       a  [Y        TRT                  R@                  S   R[                  5       5      S   S   nUR\                  R_                  S5      (       a8  UR\                  R_                  S5      (       d  TRT                  RV                  U4nU R`                  U/-   TRT                  R@                  S'   OUS:X  a  U R                   b  [c        SSU Rd                   3S/ S9  U R`                  TRT                  RV                  [g        U4S jTRT                  R@                  S   Ri                  5        5       5      4/-   TRT                  R@                  S'   U Rk                  UTRT                  5        U(       Gd+  STRT                  R@                  ;  a:  URP                  nU(       a'  URS                  5       TRT                  R@                  S'   STRT                  R@                  ;  a  US;   a>  U R`                  TRT                  RV                  T4/-   TRT                  R@                  S'   OyUS:X  as  U R                   b  [c        SSS/ S9  U R`                  TRT                  RV                  TRT                  R@                  S   T   S   4/-   TRT                  R@                  S'   STRT                  R@                  ;  a  / nU(       aJ  URm                  5       (       d  UR	                  URo                  5       5        [q        USS 5      nU(       a  MJ  U Vs/ s H  nURr                  [u        5       ;  d  M  UPM!     nnURw                  5         [x        Rz                  R}                  U5      R                  5       nSR                  U5      TRT                  lA        [6        R                  R                  R                  (       d)  [6        R                  R                  R                  (       aD  U R                  R                  R                  U R                  R                  TRT                  5        T$ s  snf s  snf )Nr  rT  )r7  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  )rf   rstripr  r  )linecur_instheaderr  tx_codes    r   get_trace_call_log_str;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_str[  s9    5gxHOOQD+BGGLL>xr$PPr   r  Forig_graphmodulec                      g r   r   r   r   r   r  -SubgraphTracer.create_proxy.<locals>.<lambda>f  s    Dr   Tnn_module_stack>   rU  r  r0  r+   )ztorch.nn.modulesz	torch.ao.ztorch.nn.modules.containerr(  rV  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   r  .SubgraphTracer.create_proxy.<locals>.<genexpr>  s4      *QJAw773<?f4 *Qs   !1	1z2Invoking an nn.Module inside a HigherOrderOperatorrk  r3  rS  )HrS  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   rE  r  r  r
  r  r4  r  	positionsr7  rs  get_line_of_code_headerr   trace_call_logr=  rn   r$  r=   get_contextr  rU  r   r   r  r   r  r  r%  _lineno_mapr&  r  __code__rz  r'  rH  r  r  r  r   r  r   
startswithr(  rD   rO  r  r   r  is_co_filename_from_nn_modulesframe_summaryr  filenamer&   rt  r   StackSummary	from_listr  r  r3  r6  r,   rh  track_nodes_for_deduplicationr  
track_node)r   r  r  r   r   r  r:  r;  	flat_args	tree_specnew_flat_argsr  maybe_new_argr  rC  is_retracingorig_graphmodule_maybendrH  r  current_nn_moduleframe_summariesframefiltered_frame_summariesmsgsr@  rA  r  rB  r   s     `                      @@@@r   rE  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  )r  r  rS  r  arg_tree_leavesrU  r   r   rz  r   r   rI  r  r  r*  r   r  )r   r  r  r   r   r  r:  r^  r  r  r   s             r   rI  SubgraphTracer.create_node  s     	t00$M;;"..??I !#uxx}}55yyDJJ. H. ! w"4tO*.*;*;*E*E		&'TYY'r   c                    [        UR                  5      S:  a  / nUR                   HV  nUR                  U R                  :w  d  M  UR                  [	        [        UR                  R                  5      5      5        MX     U H  nUR                  R                  U5        M      U R                  R                  U5        U R                  R                  UR                  S 5        g r+  )r  ru  r   r  r'  r   r  r  r;  rV  r  )r   r  user_graph_nodesuserother_graph_nodes        r   rM  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)+rU  r   r   r+  r   _versionr   r=  r  r#  rS  rw  rW   r  r  
setdefaultr   extract_stackri   r*  r;  r  r'  r  r   inserting_beforeinserting_afterrE  rq   popitemr   compileris_compiling_lift_basic_symbolsr   rG  r  rT   r  r  r  r  rB  )r   r  r:  r  rs  r   	prev_namer  ctxr  r   r   is_strict_exportis_non_strict_exportr  rx  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 R                  UR                  R                  [        U5      U5      nX0R                  U'   U$ )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr  )rS  r  r  rU  r   r  r  rB  r  rX  lift_tracked_freevar_to_inputr  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++JJOOT-0-
	 '0U#r   r  c                 0  ^  [        U[        R                  R                  5      (       dJ  [        U[        5      (       a3  [	        U 4S jUR
                  UR                  UR                  4 5       6 $ U$ UR                  T :X  a  U$ T R                  U5      $ )z
If arg is a free variable, then lift it to be an input.
Returns the new lifted arg (if arg was a freevar), else the
original arg.
c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   )rN  )r  sub_argr   s     r   r  ESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>  s&      'FG @@II'Fs   !)
rU  r   r   r  slicestartstopsteprX  r  )r   r  s   ` r   rN  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#   rU  r  r  r  r  rB  )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   )
rU  r@  r   r   r  r   r|  r  rE  rq   )r  r   r   r  r  rX  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 %sr  rv  zCtrack_produced_symints %s for %s.storage_offset() at debug_level %sz?track_produced_symints %s for %s.stride()[%s] at debug_level %s)-rX  rU  r  r	   r   r   r  r   r   r  r  r   r=  r#  r@  opsr  sym_sizer   r"  track_produced_symintsstorage_offsetsym_storage_offsetlayoutstridedstride
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr*   __tensor_flatten__r  r  r  r  rB  )r   r  r  r  r  r  r  
lazy_proxyr  attrsr  r-  inner_tr  rX  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   rs  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  )rs  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_tensorr  r*  r  )r#   rU  r   r  lookup_unbound_symbolsr  rS  r  rB  r  r  r  r   r"  r   r=  r  r#  r  r  iterrs   )	r  r   rs  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)rs  r  )$r   r   r   r  r   r   r   rU  r   r  r  r^   r]   SIZEr  r  r  STRIDEr  STORAGE_OFFSETr  r  r  r  r  r  r  r  r  r  r  r  r*   r  r  rQ   )
r   r  r  r  r  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   r  7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>  s    r   )r   )r  r  r!   r  rB  r   rU  r@  r   r   r  rX  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 TFrk  )r+  r   r  r  r  rU  r   r   r   rx  r  zipr   )r   input_versions_at_beginninginput_nodesinput_versions_at_endr  r  r  v1v2mutated_inputsmutated_nodesr   s               r   has_input_mutation!SubgraphTracer.has_input_mutation  s   &*&G&G# "JJ$$Dww-' $		/ :mU\\::)001G1GH&&t, ;  %  )/G 
 8B x	   	 
 5CD^^^MD/?Cc**E2&&
 Es   4DDDc           	         SSK Jn  SSKJn  [	        5       nU R
                  R                   Ht  nUR                  S:X  aa  U" U/5      S   n[        U[        R                  5      (       a4  U" U5       H&  nXc;   a  SX6    SU 3n[        SU5      s  s  $ XCU'   M(     Mr  Mt    O   [	        5       nU R
                  R                  SS	9S   n	[        R                  " U	R                  S   5       H  n
U
(       d  M  U" U
/5      S   n[        U[         5      (       a   e[        U[        R                  5      (       d  MP  U" U5       H&  nXh;   a  S
X    SU
 3n[        SU5      s  s  $ XU'   M(     M     UR#                  5       UR#                  5       -  n[%        U5      S:  aT  U Vs/ s H  oU   X   4PM     nnSR'                  U VVs/ s H  u  pU SU 3PM     snn5      nSU 3n[        SU5      $ [        SS5      $ s  snf s  snnf )Nr   )get_tensor_storages)_collect_fake_inputsr?  z*Input-to-input aliasing detected at nodes z and Tr\  r@  z,Output-to-output aliasing detected at nodes z, z+Input-to-output aliasing detected at nodes Frk  )(torch._dynamo.variables.higher_order_opsr  torch._higher_order_ops.utilsr  r   r   r  r  rU  r   r   r   rA  r  tree_leavesr   r   r  r  r  )r   r  r  input_storagesr  r  storager   output_storages	out_nodesout_nodeintersected_storagesr  aliasedr  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	
)r$  r+  r'  r&  r%  r*  r!  rB  r#  rQ  r  r   r;  rw  r"  r  r  rS  r  r>  r(  rO  r   r-  r  )NFNN)NNNri  )FN)*r   r   r   r   r   r   r   r'   r   r   rx   r0  r   rz  r  r	   r   r  rE  rI  rM  r   r  r   r@  r  rN  r   r  r  r   r  r   r  r  r  r   r  r   r   r   r   r   s   @r   r  r    s    .2*.%)]U#]U )*]U 	]U
  ']U c]]U 
]U ]U~1_ 1 1C-C57WWC	C8 ##'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"(("	#(T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  rw  r#  r  r.   r  r  r
  r   rc  r  collections.abcr   r   r   dataclassesr   r   rF  typesr   typingr	   r
   r   r   r   typing_extensionsr   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedr  torch.nntorch.utils._pytreerN  rO  r  r   r   torch._C._dynamor   torch._dynamo.excr   r   r   r   r   r   r   r   torch._library.opaque_objectr   torch._subclasses.fake_tensorr   torch._utils_internalr   torch.export.dynamic_shapesr   r  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*   rk  r,   r-   r   r/   backends.registryr0   r1   bytecode_transformationr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   rV  r>   current_scope_idr?   device_interfacer@   rA   rB   rC   rD   rE   r  rF   rG   graph_deduplicationrH   graph_region_trackerrI   rJ   rK   mutation_guardrL   rn  rM   rN   rO   r   rP   rQ   rR   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   variables.builderrr   rs   rt   ru   variables.ctx_managerrv   variables.functionsrw   rx   variables.listsry   variables.miscrz   variables.nn_moduler{   variables.tensorr|   r}   r~   variables.torch_functionr   variables.user_definedr   torch._dynamo.packager   r  r    torch.multiprocessing.reductionsr   	getLoggerr   r   r  getArtifactLoggergraph_tabular_logr  graph_sizes_logrR  r&  r   r   r  r  r  r  r   r   r   r   r   r   r   r   r   r   r   r   r   objectr-  r  r<  rI  rX  rZ  rm  r  r  r  r  r  r  r  r@  Tracerr  r   r   r   <module>r     s  *         	 
    9 9 4  < < 0       $ $  # N  8 4 0 9 : ?  ! J / F D D 5    '  - 6  S : 4 / 0 W W    "     *  ; H - ( 1 
 C ; 4H?!NN44XwG 11(LI..228]K11(LI**  "'!>!> $)HHOO$D$D ! $   $  
 $  
 0 4c 4 4 - - -# (2tCy=2I  UXX__   ! !H 	S&[ <
 <
 <
~ X X X" ' ' ',E d38n (F". F"Rs#2# s#2lG!V !VJJ JJ%(J25J=@JJMJ	JZ  oo' cNCL2 2$c'RYY c'r   