
    7h	                       S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKJrJr  SSKJrJrJrJrJrJr  SSKrSSKrSSKrSSKJr  SSKrSSK J!s  J"r#  SSKJ$r$J%r%  SSK&J'r'  SSK(J)r)J*r*  SSKJ+r+J,r,J-r-J.r.J/r/J0r0  SS	K1J2r2  SS
K3J4r4  SSK5J6r6  SSK7J8r8  SSK9J:r:J;r;J<r<J=r=J>r>  SSK?J@r@  SSKAJBrB  SSKCJDrD  SSKEJFrF  SSKGJHrHJIrIJrJJKrK  SSKLJMrMJNrN  SSKOJPrPJQrQJRrRJSrSJTrT  SSKUJUrU  SSKVJWrW  SSKXJYrY  SSKZJ[r[  SSKIJ\r\J]r]J^r^J_r_J`r`  SSKaJbrb  SSKcJdrd  SSK'JereJfrf  SSKgJhrh  SSKiJjrjJkrk  SS KlJmrmJnrnJoroJprpJqrqJrrrJsrsJtrtJuruJvrvJwrwJxrxJyryJzrz  SS!K!J{r{J|r|J}r}J~r~JrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SS"KJr  SS#KJrJrJrJr  SS$KJr  SS%KJr  SS&KJrJr  SS'KJr  SS(KJrJrJrJr  SS)KJr  \(       a  SS*KJr  \GRN                  " \5      r\GRT                  GRW                  \S+5      r\GRT                  GRW                  \S,5      r\GRT                  GRW                  \S-5      r\GRT                  GRW                  \S.5      r\'GR`                  r\" S/S09 " S1 S25      5       r\" S/S09 " S3 S45      5       r\" S/S09 " S5 S65      5       r " S7 S85      r\GRj                  S9 5       r\ " S: S;5      5       rS< r " S= S>\GRr                  GRt                  5      r " S? S@5      r\\\4   r\ " SA SB5      5       r\ " SC SD5      5       r " SE SF\5      rSGrSH r\GR                  " 5       r " SI SJ5      r " SK SL\$GR                  5      rg)Ma  
Core graph building functionality for PyTorch's Dynamo system. This module contains
the essential components for constructing and managing FX graphs during compilation:

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

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

The module supports key Dynamo features including:
- Higher-order operators through nested SubgraphTracers
- Graph deduplication for optimization
- Symbolic shape handling and propagation
- Side effect tracking and management
- Guard insertion and management
    N)	dataclassfield)AnyCallablecastOptionalTYPE_CHECKINGUnion)fxTensor)guards)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourcetracingTracingContext)
FakeTensor)signpost_event)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnvSpecialization)insert_deferred_runtime_asserts)StorageWeakRef)
OrderedSet)is_traceable_wrapper_subclass   )configexclogging	variables)
CompiledFn
CompilerFn)create_call_functioncreate_instructioncreate_load_constInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)get_interface_for_device)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplemented_v2unimplemented_v2_with_warning)apply_graph_deduplication)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffects)
AttrSourceBackwardStateSourceConstantSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceNumpyTensorSourceParamBufferSourceShapeEnvSourceSyntheticLocalSourceTensorPropertyTensorPropertySource)_extract_tensor_dictcheckpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedget_instruction_source_311get_locals_to_stealget_static_address_typeget_unique_name_wrtgraph_break_reasonsincrement_op_countistypelazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)VariableTracker)BackwardStateGraphArgGraphArgTrackedFakewrap_fx_proxy)ContextWrappingVariable)BaseListVariable)CellVariableNullVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariable)TensorWithTFOverrideVariable)InstructionTranslatorBasegraph
graph_codegraph_sizes
trace_callT)frozenc                   *    \ rS rSr% \\S'   \\S'   Srg)VariableTrackerCacheKey   vt_idsource N)__name__
__module____qualname____firstlineno__int__annotations__r   __static_attributes__rz       T/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/_dynamo/output_graph.pyrv   rv      s    J Nr   rv   c                   *    \ rS rSr% \\S'   \\S'   Srg)AliasingInfo   has_aliasingmsgrz   Nr{   r|   r}   r~   boolr   strr   rz   r   r   r   r          	Hr   r   c                   *    \ rS rSr% \\S'   \\S'   Srg)MutationInfo   has_mutationr   rz   Nr   rz   r   r   r   r      r   r   r   c                   2    \ rS rSrS rS rS rS rS rSr	g)	VariableTrackerCache   c                     0 U l         g Ncacheselfs    r   __init__VariableTrackerCache.__init__   s	    
r   c                 j    [        [        U5      U5      nX0R                  ;  a  g U R                  U   $ r   rv   idr   )r   valuery   keys       r   lookupVariableTrackerCache.lookup   s-    %bi8jj zz#r   c                 J    [        [        U5      U5      nX0R                  U'   g r   r   )r   r   ry   vtr   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   N)
r{   r|   r}   r~   r   r   r   r   r   r   rz   r   r   r   r      s    r   r   c                  6    [         R                  " [        5      $ r   )torchdynamo_loggingget_step_loggerlogrz   r   r   _step_loggerr      s    ..s33r   c                   \    \ rS rSr% Sr\\S'   \\R                     \S'   Sr
\\S'   S rSrg	)
GraphCompileReason   zOStores why a given output graph was compiled; i.e. what caused the graph break.reason
user_stackTgraph_breakc                 T    U R                   (       a  [        R                  " U 5        g g r   )r   rX   appendr   s    r   __post_init__ GraphCompileReason.__post_init__   s    &&t, r   rz   N)r{   r|   r}   r~   __doc__r   r   list	tracebackFrameSummaryr   r   r   r   rz   r   r   r   r      s,    YKY++,, K-r   r   c                    ^  U 4S jnU$ )Nc            
      P   > T V VVs/ s H  u  po " U0 UD6PM     snnn $ s  snnn f r   rz   )fnargskwargsrandom_callss      r   _gen_rand_values1_get_gen_rand_values_fn.<locals>._gen_rand_values   s*    <HIL(8&D#F#LIIIs   !rz   )r   r   s   ` r   _get_gen_rand_values_fnr      s    J r   c                      ^  \ rS rSrSrS\\\R                  R                  4   4U 4S jjr
S\4S jrS\\\R                  R                  4   4S jrSrU =r$ )	FakeRootModule   z'Trick the constructor of fx.GraphModule
nn_modulesc                 l   > [         TU ]  5         UR                  5        H  u  p#[        XU5        M     g r   )superr   itemssetattr)r   r   kv	__class__s       r   r   FakeRootModule.__init__   s-    $$&DADQ 'r   returnc                     g)NzFakeRootModule(...)rz   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   rz   )r{   r|   r}   r~   r   dictr   torchnnModuler   r   r   r   __classcell__r   s   @r   r   r      sS    1 4UXX__(<#=  
%# % c588??.B)C    r   r   c                   v    \ rS rSrS\4S jrS\R                  R                  S\	\R                     4S jrSrg)	WrapperBackend   backendc                     Xl         g r   )r   )r   r   s     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)rN   restorer   copydeepcopyr   	candidateforwardr$   verify_correctnessrP   r^   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   N)r{   r|   r}   r~   r)   r   r   r   GraphModuler   r   r   r   rz   r   r   r   r      s4    +
 +588// ellAS r   r   c                      \ rS rSr% Sr\\S'   \\S'   \\R                  R                     \S'   \\   \S'   \\\\\4   4   \S'   \\S'   \\R"                  R$                  R&                     \S	'   \\R*                     \S
'   Sr\\S'   Sr\\S'   Sr\\R2                  R4                     \S'   Sr\\\R2                  R8                        \S'   \S 5       r\S 5       r\S 5       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_deviceFexportexport_constraintsN_guards_aotautograd_guardsc                 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.guardsD  s    ||r   c                     U R                   $ r   )r	  r   s    r   aotautograd_guards)OutputGraphGuardsState.aotautograd_guardsH  s    '''r   rz   )"r{   r|   r}   r~   r   Scoper   r   r   	overridesTorchFunctionModesetr   r   r   r   r   
_functorchpyfunctorchFuncTorchInterpreterr   devicer  r   r  r  	GuardsSetr	  GuardEnvExprpropertyr  r   r  r   rz   r   r   r   r   %  s    #EOO$E$EFFF##'S#X(>#??O5++77LLMMU\\**FD$$15GXemm--.5FJ$u}}'A'A"BCJS S   ( (r   r   c                       \ rS rSr% Sr\" \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)StackLocalsMetadataiM  z^
Stores metadata for a frame's stack and locals for the purposes of building resume functions
)default_factorystack_null_idxeslocals_null_keys.stack_ctx_argsstack_ctx_idxes_origlocals_ctx_argsrz   N)r{   r|   r}   r~   r   dc_fieldr   r"  r   r   r#  r   r$  tupler   r%  r&  r   rz   r   r   r   r   M  s     #+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            
       6  ^  \ rS rSr% Sr\\S'   S\\\	4   S\
\   S\S\S\4
U 4S	 jjrS
 rS rS rS_S\4S jjrS rS rS\
\\      4S jrS rS rS\/ \	4   4S jrS r\S 5       r\S 5       rS r \S 5       r!\!RD                  S 5       r!\S 5       r#\S 5       r$\S 5       r%S  r&S! r'S" r(\)RT                  S# 5       r+\S$ 5       r,\S% 5       r-\S& 5       r.\S\/R`                  Rb                  4S' j5       r2\S\\\	4   4S( j5       r3\S) 5       r4S`S+ jr5S, r6S- r7\S. 5       r8S/ r9S0 r:S1 r;SaS2 jr<S3 r=\>S4 5       r?S5\S6\	S\@R                  4S7 jrBS8\C\/R                  R                  \/R                  \	4   4S9 jrGS: rHS; rI  SbS<S=S>\J4S? jjrKS@ rLSA rMSB rNSC\4SD jrO\)RT                  SE 5       rPSF rQSG rR\S\\@R                     4SH j5       rT\S\\U   4SI j5       rVSJ\@R                  SK\\F   S\X4SL jrYSJ\@R                  SK\\F   S\X4SM jrZSN r[SO r\S\\/R                     4SP jr]ScSQ jr^ScSR jr_ScSS jr`ST\\a   SS*4SU jrbScSV jrcS\4SW jrdS\4SX jreScSY jrfSZ\\@R                  /S*4   SS*4S[ jrgS\\/R                  R                  4S] jrhS^riU =rj$ )dOutputGraphiZ  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_fnr  r   r   c                   > [         TU ]  UUU
[        5       0 [        R                  R
                  R                  [        R                  R                  R                  5       [        R                  R                  R                  S9  [        XS9/U l        0 U l        X@l        XPl        X`l        / U l        [)        [*        5      U l        [        5       U l        U	R0                  U	R2                  U	R4                  S.U l        [9        5       U l        / U l        [?        U R<                  [@        RB                  [@        RD                  [@        RF                  [@        RH                  U R6                  S9nSS K%Js  J n  URM                  SS9   [        RN                  RQ                  UU R                   (       a  SOSU R                   S	9nS S S 5        [S        W5      U l*        U RT                  RV                  RY                  U	5        [Z        R\                  " 5       U l/        U Ra                  5         [b        Rd                  " [f        5      U l4        0 U l5        [m        U 5      U l7        [q        5       U l9        [t        Rv                  " 5       U l<        [{        U5      U l>        / U l?        SU l@        / U lA        X lB        X0lC        XlD        0 U lE        / U lF        / U lG        SU lH        0 U lI        [        R                  R                  5       U lL        SU lM        [        0 5      U lN        [        0 5      U lO        U R                  5         0 U lQ        / U lR        S U lS        / U lT        0 U lU        S U lV        S U lW        U R                  5       U lY        [        R                  " 5       U l\        U R                  5       U l^        g ! , (       d  f       GN= f)
N)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'allow_complex_guards_as_runtime_asserts	co_fieldsr   F(fake_tensor_allow_unsafe_data_ptr_accessT)r  allow_non_fake_inputsr  )_r   r   r  r   autograd
forward_ad_current_levelr  r  #retrieve_all_functorch_interpretersutils_deviceCURRENT_DEVICESubgraphTracertracersinput_source_to_varr  r  frame_statecleanup_hooksnext_compile_id_counter
compile_idinstalled_globalsr0  r1  r2  r8  r9   region_trackerr3  r   r$   capture_scalar_outputs capture_dynamic_output_shape_opsr6  r7  torch._functorch.configpatch_subclassesFakeTensorModer   tracing_contexttraced_coder   r   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   tracked_fakes_id_to_sourceparam_name_to_sourcer>   r+  r   variable_tracker_cache	itertoolscountunique_var_idr   r,  output_instructions	timestampregister_finalizer_fnsr-  root_txpackagesource_to_user_stacks_current_txcleanupsshould_exitunspec_variable_map_C_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_fglobals%name_of_builtins_dict_key_in_fglobals
contextlib	ExitStackcompiler_trace_stack+maybe_install_saved_tensors_hooks_subgraphs"saved_tensors_hooks_subgraph_names)r   r,  r-  rc  r  r  rF  r   r   f_coder   rd  r  _config	fake_moder   s                  r   r   OutputGraph.__init__g  sX    	%"u*,~~00??"--99]]_ ;;..== 	 		
 't>? CE "4&68#$78+.5
 ~~!--$33
 12 13 ,,!'!>!>+1+R+R8>8j8j4:4b4bnn
	$ 	21]]E]J))88#.2kkdu{{	 9 I K 0>i/H((//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$:! <<> 	/i KJs   =N88
Oc                 J    U R                   R                  [        SSS95        g )Nbytecode_tracingTlog_pt2_compile_event)r{  enter_contextrS   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_stop1  s    !!'')r   c                     U R                   S   n[        U[        5      (       d  UR                  nU R	                  SU5      $ )N__builtins____builtins_dict__)r   
isinstancer   __dict__install_global)r   
f_builtinss     r   rw  -OutputGraph.install_builtins_dict_in_fglobals4  s@     &&~6
*d++#,,J""#6
CCr   hookc                     U [        U R                  5       3nX0R                  ;  d   eXR                  U'   X0R                  5       4$ r   )lenrt  get_backward_state_proxy)r   r  prefixnames       r   add_backward_state_hook#OutputGraph.add_backward_state_hookG  sN    #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 export z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_statery   grapharg)ru  r  r6   r   root_tracercreate_graph_inputr  r@   ra   nodemetanew_varrv  )r   example_values     r   r  $OutputGraph.get_backward_state_proxyM  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        [        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   rI   
make_guardr:   	SHAPE_ENVrC   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATEr   rj  r  peek_interpreter_stackFUNCTORCH_STACK_MATCH_dynamocompiled_autogradin_compiled_autograd_regionAUTOGRAD_SAVED_TENSORS_HOOKS)r   cis     r   rW  OutputGraph.init_ambient_guardsb  sM    	(33L4J4JKL**<+P+PQ	
 	)+66|7M7MNO)+66|7R7RST**<+L+LM	
 XX  779>KKOO!#..|/Q/QR }}..JJKKOO!#.. == Kr   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   r  r  r  r  _aot_autogradr@  top_saved_tensors_hooks"saved_tensors_hooks_are_inlineableinstall_subgraphr   r   r   rp   )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   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                  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   s    r   dump_guards_stateOutputGraph.dump_guards_state  su    %((**&*&D&D#66*.*L*L!22..;;#66KK $ 7 7
 	
r   c                   ^^ T" U6 nU R                  5       n[        U R                  5      mTR                  UU4S j5        TR	                  [        [        R                  R                  U5      5        TR                  [        U5      S5        TR                  U5        U R                  R                  TR                  5       5        [        U5      n[         R"                  " U R                  X55      nUR%                  5       n[&        R(                  " 5       R*                  R,                  R/                  U5        U$ )zM
call fn(*args) before the graph runs and turn the result into a fake input.
c                  P   > T R                  TR                  TR                  5      $ r   )load_import_fromr|   r{   )cgr   s   r   <lambda>3OutputGraph.synthetic_graph_input.<locals>.<lambda>  s    B''r   F)r  r0   rc  add_push_nullforeachmapr'   ConstantVariablecreatecall_functionr  storers  extendget_instructionsrJ   r`   buildrealizer   getguards_contextdynamo_guardsremove_guards_with_source)r   r   r   r  varnamery   r   r  s    `     @r   synthetic_graph_input!OutputGraph.synthetic_graph_input  s     D	,,.t||$
	
 	

3y1188$?@
TE*
%%b&9&9&;<%g. &&t||]K!++99SS	
 r   r   c                 :    U R                   R                  U5        g r   )rG  r   )r   r   s     r   add_cleanup_hookOutputGraph.add_cleanup_hook  s    !!"%r   c                 ~    [        U R                  5       H
  nU" 5         M     U R                  R                  5         g r   )reversedrG  r   )r   r  s     r   call_cleanup_hooksOutputGraph.call_cleanup_hooks  s.    T//0DF 1  "r   c                      U R                   S   $ Nr   rD  r   s    r   r  OutputGraph.root_tracer  s    ||Ar   c                      U R                   S   $ Nr  r   s    r   current_tracerOutputGraph.current_tracer  s    ||Br   c                 2    [        U R                  5      S:H  $ )Nr#   )r  rD  r   s    r   is_root_tracerOutputGraph.is_root_tracer  s    4<< A%%r   c                 .    U R                   R                  $ r   r  rp   r   s    r   rp   OutputGraph.graph  s    ""(((r   c                 $    XR                   l        g r   r  )r   r   s     r   rp   r    s    $)!r   c                 .    U R                   R                  $ r   )r  input_name_to_proxyr   s    r   r  OutputGraph.input_name_to_proxy  s    ""666r   c                 .    U R                   R                  $ r   )r  real_value_cacher   s    r   r  OutputGraph.real_value_cache  s    ""333r   c                 .    U R                   R                  $ r   )r  bound_symbolsr   s    r   r  OutputGraph.bound_symbols  s    ""000r   c                 :    U R                   R                  " U0 UD6$ r   )r  create_proxyr   r   r   s      r   r
  OutputGraph.create_proxy  s    ""//@@@r   c                 :    U R                   R                  " U0 UD6$ r   )r  create_noder  s      r   r  OutputGraph.create_node      ""..???r   c                 :    U R                   R                  " U0 UD6$ r   )r  remove_noder  s      r   r  OutputGraph.remove_node  r  r   c              #     #    [        5       n U(       a  UR                  U R                  L d   eUR                  5         U(       a  UO)[	        U U R                  UU R                  R
                  S9nU R                  R                  U5        Uv   UR                  S S S 5        U R                  R                  5         g ! UR                  S S S 5        U R                  R                  5         f = f7f)N)parentsource_targetr/  )
r1   r  r  	__enter__rC  r/  rD  r   __exit__pop)r   r  prior_tracer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   C2BC  .C2 /C//C2c                     U $ r   rz   r   s    r   outputOutputGraph.output  s    r   c                 .    U R                   R                  $ r   )rS  r  r   s    r   r  OutputGraph.fake_mode  s    ##---r   c                 B    U R                   R                  R                  $ r   )rS  r  r  r   s    r   r  OutputGraph.shape_env  s    ##--777r   c                 B    U R                   R                  R                  $ r   )rS  r  r  r   s    r   r   OutputGraph.guards"  s    ##22@@@r   c                 B    U R                   R                  R                  $ r   )rS  module_contextr   r   s    r   r   OutputGraph.nn_modules&  s    ##22===r   c                 B    U R                   R                  R                  $ r   )rS  r  r  r   s    r   r  OutputGraph.aotautograd_guards*  s    ##22EEEr   Nc                 @   [        [        [        [        [        S[
        4   [        4   4   Ub  UOU R                  R                  R                  5      n[        R                  [        R                  " 5       4US'   [        R                  " [        R                  S5      [        R                   " S5      4US'   [        R                  " [        R                  S5      [        R                   " S5      4US'   [        R                  " [        R"                  S5      [        R$                  " S5      4US'   [        R                  " [        R"                  S5      [        R$                  " S5      4US	'   [        R&                  [        R(                  " 5       4US
'   g)zS
Saves to out if it is provided. Else saves to the tracing context's global_state.
.Ngrad_enabledcudaautocast_enabledcpuautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r   r   r   r(  r   r   r   rS  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   outr7  s      r   rp  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   c                 :    U R                   R                  U5        g r   )rf  r   )r   txs     r   push_txOutputGraph.push_txR  s    #r   c                 6    U R                   R                  5       $ r   )rf  r  r   s    r   pop_txOutputGraph.pop_txU  s    ##%%r   c                 Z    U R                   (       d  U R                  $ U R                   S   $ r  )rf  rc  r   s    r   
current_txOutputGraph.current_txX  s$    #'#3#3t||M9I9I"9MMr   c                 ,    [        U R                  5      $ r   )rQ   rp   r   s    r   rQ   OutputGraph.count_calls\  s    4::&&r   c                 X    [        [        U R                  R                  5      5      S:H  $ r  )r  r   rp   nodesr   s    r   is_empty_graphOutputGraph.is_empty_graph_  s!    4

(()*a//r   c                     U(       d   eU R                   nUR                  S5       H)  n[        U[        5      (       a  X#   nM  [	        X#5      nM+     U$ N.)r   splitr  r   getattr)r   keysobjr   s       r   get_submoduleOutputGraph.get_submoduleb  sH    tBF//CA#t$$fco	 !
 
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_)r  r,  rH  r_  )r   r  existingvars       r   r  OutputGraph.new_varl  s^    t((78F!D!3!3456C"!!-0SF:0
	 r   c                 Z    XR                   S   ;  a  U R                   S==   U4-  ss'   gg)z/Ensure self.code_options.co_names contains nameco_namesN)r,  )r   r  s     r   update_co_namesOutputGraph.update_co_namesu  s0    ((44j)dW4) 5r   c                     SR                  [        [        U 5      5      n[        R                  " SSU5      n[        R                  " SSU5      n[        R                  " SSU5      nU(       a  US   R                  5       (       d  SU-   nU$ )	Nr_  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)joinr  r   rerh  isalpha)namesr  s     r   module_key_nameOutputGraph.module_key_namez  sp     xxC(vv,eT:vvlIt4vvosD147??,,4<Dr   attr_prefix
attr_valuec                     [        XR                  5      nX R                  U'   U R                  SUS0 5      n[        UR                  U5        U$ )Nget_attrrz   )rW   r   r
  r_   r  )r   ro  rp  	attr_nameproxys        r   %register_static_attr_and_return_proxy1OutputGraph.register_static_attr_and_return_proxy  sI     (__E	 &0	"!!*iR@%**j1r   targetc                 \  ^ ^^^
^^ [        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      (       a8  T R                  mT R                  5       (       d  T R                  mUU UUU4S jnO[        T[        R                  R                  5      (       aj  [        T[        R                  R                  5      (       d   eT(       a0  [        TR!                  ["        R$                  5      5        UU4S jnOGUU4S jnO?[        T[        R&                  [        R(                  45      (       a	  UU U4S jnOU U4S jnT R*                  R-                  5        H  u  pVUTL d  M  U" U5      s  $    [.        R0                  " U6 m
[3        T
T R*                  T R4                  5      m
TT R*                  T
'   [        T[        R                  R                  5      (       al  U
U U4S jn[7        TS5      (       a!  TR9                  5        H  u  pU" U5        M     [7        TS	5      (       a!  TR;                  5        H  u  pU" U5        M     U" T
5      $ )
Nry   c           	      6  > TR                   c   eTTR                   U '   TTR                  R                  R                  ;   a#  TR                  R                  R                  T   $ [	        T5      S:X  a>  [        T[        5      (       d)  [        TR                  [        R                  5      5        O8[        T5      (       d(  [        TR                  [        R                  5      5        [        TR                  TR                  SU S0 5      4ST0TD6nTR                  R                  R                  R                  TU5      nSUR                   R"                  R$                  ;  d   e['        T5      UR                   R"                  R$                  S'   U$ )Nguardedrr  rz   r  tensor_dict)r[  rc  r   r+  rV   r  rG   r;   r  r:   ID_MATCHrD   TENSOR_MATCHrd   r
  track_object_existingrt  r  r  rM   )
module_keyr   optionsr   ry   rw  r  s     r   	wrap_name6OutputGraph.register_attr_or_module.<locals>.wrap_name  sV   00<<<8>))*5 T\\00===<<..;;FCC*62i?
-I I "&"3"3L4I4I"JK+F33!&"3"3L4M4M"NO"LL''
JBG #) 	 \\((55KKFTVW$BHHMM,>,>>>>4H4P""=1	r   c                 2   > [        [        T5      U T40 TD6$ r   )ri   r  r  r  rw  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           	      \   > [         R                  " TTR                  SU S0 5      4ST0TD6$ )Nrr  rz   sym_num)rk   r  r
  )r  r  r   rw  s    r   r  r    sA    &--%%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   re  r   r`   r  rA   )r  r   rw  s    r   r  r    sF    ++J706!!*-&,,"z: r   c                    > TR                   c   e[        TU 5      nT SU  3nUTR                   U'   [        T[        5      (       a1  U TR                  [
        R                  UR                  5       5      '   g g rU  )r[  rH   r  rF   rq  r*  rm  r  )	leaf_name
new_sourcenew_namer  r   ry   s      r   register_leaf_name?OutputGraph.register_attr_or_module.<locals>.register_leaf_name  s}    00<<<.vyA
"V1YK06@))(3fk22 " 99#33JOO4EF 3r   _parameters_buffers)r<   r  r`   r  rL  r   r  rH   r   r   r  r  r  r   r   r;   r  r:   	NN_MODULESymIntSymFloatr   r   r*  rm  rW   r   hasattrnamed_parametersnamed_buffers)r   rw  rl  r  r  r   r   r  r  r_  r  ry   r  s   `` `      @@@r   register_attr_or_module#OutputGraph.register_attr_or_module  s     44 #((&LGLLw-7""""f&78888fell++((F&&(( )) @ 00fehhoo6666f//0F0FGHYV u~~ >??  OO))+DAF{ |# ,
 **E2"4$:K:KL &fehhoo.." v}--$*$;$;$=LI&y1 %>vz**$*$8$8$:LI&y1 %; r   c                    U R                   R                  S5      n[        U5      nU(       d  / 0 4$ / n0 n/ UR                  QUR                  R                  5       QU R                  R                  R                  5       QnU(       GaC  UR                  5       n[        U[        5      (       a1  [        UR                  [        5      (       d   eXgR                  -  nM^  XpR                  R                  ;  d  [        UR                  [        5      (       al  [        UR                   ["        5      (       aM  [        UR                   R$                  [&        5      (       a$  UR                   R$                  R(                  U;   d  GM  UR                   R$                  R(                  nX;  a  / XX'   XX   R+                  U5        U(       a  GMC  0 n	0 n
U R,                   GH*  n[        UR.                  [        5      (       a9  [        UR                   [&        5      (       a  UR                   R(                  U;   d  M^  UR                   R(                  nXR0                  S   ;   d   eX\    H  nUR                   U
;   a  M  UR                   R2                  nX;  aO  U R5                  U S35      nXU'   UR7                  [9        SUS9[;        U5      [9        S5      [9        SUS9/5        UR                   n['        X   5      X'   M     GM-     XJ4$ )Nr   r^  _ref	LOAD_FASTargvalBINARY_SUBSCR
STORE_FAST)r   r  rU   stacksymbolic_localsvaluesr+  store_attr_mutationsrY  r  r  rf   r   r   mutation_typer=   ry   rB   baserF   
local_namer   	graphargs_exampler,  indexr  r  r+   r,   )r   rE  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuexstolen_namevisitedoverridden_sourcesarg	list_namelist_idx
alias_name
old_sources                   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 88>>*!%$+T*"J )3H%&&.{9M-h7.?.|JO	 XX
1<W=N1O".5 , "V ..r   c                    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     / n0 n	UR"                  R%                  5        GHK  u  p[        UR&                  [(        5      (       a  UR&                  R*                  U
:X  a  MA  [        U[,        5      (       a  UR*                  U
:X  a  Mh  [.        R0                  S:  a=  [2        R5                  [        U5      (       a  UR6                  R                  U
5        M  O![2        R5                  [        U5      (       a   e[        U[        5      (       a@  UR                  c  SO[        UR                  5      nUR8                  R                  X45        X;  a  / X'   X   R                  U
5        GMN     U	R;                  5        H6  nUR=                  X   5        UR=                  U/[        X   5      -  5        M8     X8U4$ )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
    - restore_vars: names of locals corresponding to the locals part of `stack_values`
    - meta: locations of NULLs and ContextWrappingVariables in the stack/locals
        (ignores the top `stack_pops` values on the stack)
rz   r#   )      )prune_dead_localsr   	enumerater  r'   LazyVariableTrackerrealize_allr  r   r  rh   r"  re   target_valuesr(  r$  r%  r  r   ry   rF   r  rg   sysversion_infor  __instancecheck__r#  r&  rY  r  )r   rE  
stack_popsstack_valuesr  ir   r  restore_varsval_to_namesr   r   s               r   _get_stack_values_to_restore(OutputGraph._get_stack_values_to_restorew  su     	"$ ""((+HA))55e<288}q J.##E*%..%%,,Q/##E*%!899--5B5ATAT;U  ##**C,=,A=+QR))003! ,& #%9; &&,,.DA !((K00QXX5H5HA5M!\**q||q/@7*)),::))003 ;
  11,BBBB!455//1BuQ__7M  $$++Q,>?$"$O""1%5 /6 ""$A0c,/&: :; % 4//r   rE  ro   r   c           
         U R                   c   e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                   H  nUR                  S:X  a%  UR                  [        SUR                  S95        M8  UR                  S:X  a1  UR                  [        S[        UR                  S   5      S	95        My  UR                  [         R                   " U5      5        M     U R#                  U5        U R$                  (       a  U R&                  (       a   S
5       eU R#                  U R$                  5        U R)                  U R                   5      u  pxU R#                  U5        [+        U R                   R,                  5       H'  n	U	R/                  U R                   UR0                  S9  M)     U R3                  5         / n
/ n/ nUn Uc   e[5        S UR,                   5       5      (       d   eU R7                  X5      u  pnU
R                  U5        UR                  U5        UR                  U5        XR                   L a  OUR8                  nM  U R:                  R=                  5        VVs0 s H  u  nnU[?        U5      _M     nnn[A        U5      nSSK!J"n  [        U RF                  5      S:  a  / nU RI                  S5      U l%        U" [M        U RF                  5      SS9nU RO                  SU5      n[Q        U R                   UUS9nURS                  URU                  US5      5        URS                  [W        SS5      5        UR                  URY                  U RJ                  5      5        U R#                  U5        SnSnU
S   nU R                   UL Ga  U(       Ga  [5        S U 5       5      (       a  [5        S U 5       5      (       a  [        [[        U5      5      [        U5      :X  a  U R\                  R_                  5       (       a  UR`                  (       d  U Rb                  (       ds  US   Rd                  (       d_  US   Rf                  (       dK  U R#                  U Ri                  U[k        [+        U5      5      U5      [        S[        U5      S	9/-   5        GOU RI                  S5      n[+        U
5       VVs/ s H  nU  H  nUPM     M     nnn[Q        U R                   UUUS9n U Rm                  UUU 5        0 n!U Rn                  R=                  5        H0  u  nn"U"S:  d  M  [q        U[r        [t        45      (       a  M+  SU!U'   M2     [Q        U R                   UUU!US9n#U Rm                  UUU#5        / n$[w        U Rx                  5      S:w  d  [        U#Rz                  5      S:w  a  U$RS                  U Ri                  UU#R}                  5       U5      5        [        U#Rz                  5      S:w  a#  U$R                  U#RY                  U5      5        SnO+U$R                  [        S5      5        OU R                  5         U R#                  U$U#R                  5       -   5        [Q        U R                   US9n%U R#                  [+        US   5       V&s/ s H  n&U%RY                  U&5      PM     sn&5        U(       a(  U(       a!  U R#                  U%R                  U5      /5        U$ s  snnf s  snnf s  sn&f )au  
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     	MAKE_CELLr  COPY_FREE_VARSco_freevars)r  z)export does not support pregraph_bytecode)is_graph_breakc              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#   disabler   random_valuesz.do not trace into Dynamo rng recovery functionr   __gen_rand_values)r  Fr  c              3      #    U  H^  n[        U[        [        [        45      (       + =(       a2    [        U[        5      =(       a    UR                  5       [        L (       + v   M`     g 7fr   )r  rm   rj   rn   rk   python_typefloat)r  r   s     r   r  r  P  sa       +A 3,4  V $A7TAMMOu<TUV +s   A&A(c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r  rl   r  r  s     r   r  r  \  s     M;LaJq.11;Ls   UNPACK_SEQUENCE	graph_out)tempvarsr  POP_TOP)Brc  r  partial_convertcompile_subgraph_reasonrh  r   debugr  r  prefix_instsopnamer   r+   r  r  r,  r   add_output_instructionsrs  r  r  r  block_stackexitr   cleanup_graphallr  r  r   r   r]   r   
decoratorsr  r   r  rr  r   r  r0   r  load_function_namer*   create_storer  r+  is_emptydebug_localsrt  r"  r#  compile_and_call_fx_graphr   codegen_suffixusesrZ   rJ   rF   rQ   rp   graph_outputsgraph_output_varsrun_compiler_collectiver  create_delete)'r   rE  r   r  r  r  instr  r  r  all_stack_valuesall_restore_varsall_stack_locals_metascur_txr  r  r  r  modnn_modules_proxiesrootr  random_calls_instructionsrand_fnrand_fn_namecodegengraph_output_varstored_graph_output_varroot_stack_valuesvalsvalstack_values_flatpass1r  r^  pass2r   local_restore_cgra  s'                                          r   compile_subgraphOutputGraph.compile_subgraph  s   , ||''' ||r!!! 	'').'-$		-v6 +-w&;;+- ''*;t{{K [[$44 ''*,#boom6T2U !''		$8 ( 	$$\2**t{{ 	
7	
; 	$$T%;%;<*.*N*NLL+
' 	$$[1 dll667EJJt||F4F4FJG 8 	 !#68%%%K8J8JKKKKK/3/P/P0,L ##L1##L1"))$/%YYF * 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  "',R0LLB!  +   M;LMMMC)*+s3D/EE!!**,,OO''*2.??*2.?? ((..X&7894 &&7SAR=STUV  $||K8 !))9 :! :dsd :  !  #5	E $5u= H#jj..0
U19VC2F1T%U%U$(HSM 1  !#5E $5u=F4::&!+s53F3F/G1/L222u7N7N7PRVW u**+q0MM%"4"45E"FG.2+MM"4Y"?@ ,,.((%2H2H2J)JK %LL-?
 	$$ $$4R$899C !--c29	
  7((!//0@AB &%A
B!hs   -\1\7\=c                   ^^ U R                   R                  T5        U R                  (       ay  U R                  (       a   eU R                  R	                  5        HH  u  pET" U5        TR                  TR                  U R                  5      5        TR                  U5        MJ     U R                   R                  T5        UR                   Hl  u  mnTR                  UU4S j5        U H  nT" U5        M     TR                  [        [        U5      S5      5        TR                  [        S5      /5        Mn     TR!                  X!R                  (       + S9  U R                   R#                  T5        g )Nc                     > T " T5      $ r   rz   )r  	debug_vars   r   r  ,OutputGraph.codegen_suffix.<locals>.<lambda>  s	    R	]r   Fr  )value_from_source)r+  codegen_save_tempvarsrt  r  r   append_outputcreate_loadrv  
store_attrcodegen_hooksr  r  extend_outputr*   r  r+   restore_stackcodegen_update_mutated)	r   rE  r  r  r  r  r   r  r  s	      `    @r   r  OutputGraph.codegen_suffix  s!    	//3{{"?!00668	3  0G0G!HId# 9 	''+  "OIt233 1#d)UCD0;<=  / 	YYG004r   c                 *   U R                   (       d   e[        U R                  R                  5      nU H  nUR                  R                  SS5        M!     [        R                  " 5       n[        XSS 5       GH  u  pEUR                  [        R                  R                  L d  M/  [        UR                  5      U(       + 4:X  d  MP  UR                  (       a  Mc  UR                  S   nUR                  [        R                  R                  L d  M  [        UR                  5      U(       + 4:X  d  M  UR                  (       a  M  UR                  S   nU R                  R                  U5        U R                  R                  U5        GM     g)z
Remove "creation_timestamp" from node meta

Remove this pattern from the graph:
    torch._C._set_grad_enabled(False)
    torch._C._set_grad_enabled(True)
creation_timestampNr#   r   )rh  r   rp   rQ  r  r  r   r9  ziprw  rj  _set_grad_enabledr(  r   _erased
erase_node)r   rQ  r  r.  node1node2s         r   r  OutputGraph.cleanup_graph  s    TZZ%%&DIIMM.5  ,,.QRy1LE : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MMM#(::a=LJJ))%0JJ))%0 2r   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  )rp   rQ  r  r  r  r   rQ  r   sizer   reprr  )r   retr  r  r/  ss         r   get_graph_sizes_structured&OutputGraph.get_graph_sizes_structured  s    JJ$$D IIMM/4@M-):):)E)EFF$))+PT!UPT1z!S'9'9!tAw"FPT!UII	 %
 
 "Vs   7,B8r  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): )rp   rQ  r  r  r  r   rQ  r   r/  r  r(  r   r   r  r  hint)	r   r  graph_sizes_strr  r  r/  concrete_size
has_symintszs	            r   get_graph_sizesOutputGraph.get_graph_sizes  s   7VD622JJ$$D IIMM/4@M-):):)E)EFF$))+ii[5;-r#BB ""
B!"c**%,,R0#B55%)
%,,RWW\\:  "z'#yyku]7K6LBO# %( r   c              #     #    U R                   R                  R                  5       n0 nU R                  US9   U R                   R                  R	                  U5        Sv   U R                   R                  R	                  [        U5      5        g! U R                   R                  R	                  [        U5      5        f = f7f)zZ
Momentarily restores the global state to what it was prior to tracing the current output
)rB  N)rS  r6  copy_graphstaterp  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 %sartifactc                      SSS.$ )Ncompiler_collectivestring)r  encodingrz   rz   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  rK  %  s    2>>#8#8#:r   )metadata_fn
payload_fnr#   z&Expect only one device type but got {}+rH  Tr  )group)rc  distributed_state
all_states
compile_pgr   inforM  r   _loggingtrace_structured_device_typesr  formatri  r2   r  r  rankacceleratordevice_countrS   r/  distall_gather_objectspeculation_logr   r%    CompileCollectiveRestartAnalysis)r   rE  rV  device_typesrU  rO  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"Vjoo&77
&&z2>>T *	 P $$&6667 9N3& PO s$   F*!<FF*
F'	#F**
F8c                   ^ ^^^^ [         R                  R                  R                  5          SSKJn  T R                  (       d   eT R                  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(                  (       dO  T R+                  5         [-        [.        R0                  " UT R2                  5      T R4                  UT R6                  S9  T R9                  5         [;        T R2                  5      n[<        S	   S
==   U-  ss'   T R?                  5         T R@                  RC                  5         [E        UT R2                  5      mT RF                  (       a)  T RF                   H  n	[I        TU	[K        X95      5        M     T RL                   H  n
U
" T5        M     UTl'        T RP                  Tl(        T RR                  RU                  5       TRV                  S'   T RX                  TRV                  S'   [Z        R]                  S[_        UTSSSS95        [         R`                  Rc                  SU 4S jU4S jS9  T Re                  5         T Rf                  Rh                  nT R6                  (       dY  SSK5J6s  Jn  URo                  SS9   [         Rp                  Rs                  UR4                  S9nSSS5        WT Rf                  l4        T Ru                  5          T Rw                  TT Ry                  5       5      mSSS5        SSK=J>n  [        TU5      (       d,  [        [K        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 R                  b  T R                  R                  UT5        U" TSS9m[<        S	   S==   S-  ss'   UR4                  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[        R                  " UR                  U/5      n[        R]                  SU5        TR                  [        R                  " U4S jU5      U45        M     [         R                  R                  SS9UUU UU4S  j5       nT R                  UU5        OT R                  UT5        T R                  c   e[        T R                  5      nUR                  U5        U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  __compiled_fnT)	with_uuidr   c              3   @   #    U  H  oR                  5       v   M     g 7fr   )as_proxyr  s     r   r  8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>P  s     5OBqjjllBr  )r  statscalls_capturedrq  rV  %s)include_strideinclude_devicecoloreddynamo_output_graphc                  (   > ST R                  5       0$ )Nsizes)r3  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$ )NFT)print_outputrn  ro  )print_readable)r   s   r   r  rt    s    2#4#4!&tD $5 $r   )rQ  r   NFr9  )r  )_LazyGraphModule__self___lazy_forwardz"do not trace Dynamo-compiled graphr  unique_graphsz:Compiling backend specialized graph with specialization=%sc                     U" X   5      $ r   rz   )idxr   check_fns      r   r  rt    s    X$(IF"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_specializationry   r~  r  r   r   rS  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'	)Yr   r  r   clear_framer  r  rh  r  r.   r  r   r   r  r  
create_argr(  
dedup_passr   _maybe_preserve_original_metar$   do_not_emit_runtime_assertsremove_unused_get_attr_nodesr   r   r   rp   r  r  remove_unused_graphargsrQ   rR   &remove_tensorify_specialized_graphargsr  r   r   r}  r   rX  rb  _backend_idr  rq  r   r  rV  graph_code_logr  r[   rX  rY  r  rS  r  rO  r  rP  rQ  rR  rC  r  r   torch.fx._lazy_graph_modulerx  r{   ry  force_recompiler   rd  add_backend_idspecializationsr  ry   r  inspect	getsourcer~  stripr   LAMBDA_GUARDr   r   r:  r;  r  install_global_unsaferc  r0   make_call_generated_coder  )r   rE  rvr  r  r  output_nodesub_gmsncallssubgraph_nameregister_finalizerold_fake_moder  backend_fake_moderx  lazy_gmr  asourcesr  source_indexcheck_fn_sourcer~  r  r  r  r   r  r  s   `                        @@@@r   r  %OutputGraph.compile_and_call_fx_graph9  s`    ]]))557+####((*_=Db$''''dN3333**$$//5OB5O0OPR	K oo'G(==bN55 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 "BN)-)E)EB&55::< GG67 ,0+A+ABGG'(  &"T$PT NN++%D ,  ##% 00::M;;99]]E]R(-(9(9(H(H"/"9"9 )I )% S 2C$$.**,"55b$:M:M:OP - E+'7887;
DACSTT((O; "+/?@@  $--  !009!+/?@@")//K||'++D+>!$HK Wo.!3."/"9"9"I"III(*%SU$-1^^<^188^<&5N#*==1F1F#GL&-&7&78O8O&P&V&V&XO%22&//() H
 IIT'
 *00%--;C !" !-	 +
 '62 &&.R&S8 8 T8. **41EF **4=<<+++4<<(B''-&&({ 87h SR -,J =G 87sJ   K=X=+(X)X=<!X&DX=)X8?EX=
X#	X=&
X5	0X==
Yc                 4    U R                   R                  SS9$ )Nplaceholderop)rp   
find_nodesr   s    r   placeholdersOutputGraph.placeholders   s    zz$$$66r   c                 ^    U R                    Vs/ s H  oR                  S   PM     sn$ s  snf Nr  )r  r  r   r  s     r   r  OutputGraph.graphargs  s)    262C2CD2C$		*%2CDDDs   *r   r   c           	      v    [        SSSSSSS9   U R                  X5      sS S S 5        $ ! , (       d  f       g = f)NOutputGraph.call_user_compilerbackend_compileTcompile_aot_autograd'aot_autograd_cumulative_compile_time_us)
phase_namer  log_waitcounterwaitcounter_name_overridedynamo_compile_column_us)rS   _call_user_compiler)r   r   r   s      r   r  OutputGraph.call_user_compiler  s>     ,("& &<%N
 ++B?
 
 
s   *
8c                 .   U R                   c   eSn/ nUR                  R                   H;  nUR                  S;   a  US-  nUR                  S:X  d  M*  UR	                  U5        M=     [        U5        U H6  n[        US5      (       a  M  UR                  S   nUR                  Ul	        M8     U R                  Ul        U R                  Ul        [        U R                   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  nUeS nAf[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  dynamor  )op_count
node_countinput_count))r-  rp   rQ  r  r   rY   r  r  ry   r  r[  _param_name_to_sourcere  _source_to_user_stacksr{   r   r&   INFOr$   r   r   callabler   r   r4   rm  r3   r  currentframewith_traceback__traceback__r7   rc  r~  r   format_frame_summaryr5   r   r   r8  r  )r   r   r   totr  r  plr  r  r-  r  es               r   r  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			
 K /0@A 	0 	55+$$a)=)=)? .1t< *##4#D6c!fX^DLLLmLmLoKpq0nSVHLcdC	 	  	 G 	8'  !W%9%9%;nQ__-48	8s3   BG L7B2J//L<J>>LALLc                 n    [         R                  R                  R                  (       a  [	        U 5      $ 0 $ r   )r   r  r$   use_graph_deduplicationr8   r   s    r   r  OutputGraph.dedup_pass`  s%    ==77,T22Ir   c                 h    [        XR                  SS9nX2l        SUl        U R	                  X#S S9  U$ )NT)requires_suffixFr  )rW   r   r{   torchdynamo_force_dynamicr  )r   r  sub_gm	next_names       r   r  OutputGraph.install_subgraphf  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_inputso  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 )Nrr  r  T)reverser   )sortedrp   r  r  r   usersr  r  s     r   r  (OutputGraph.remove_unused_get_attr_nodess  sJ    4::00J0?ND4

#$)  & Or   c                   ^ ^^^ T R                   (       d   eS[        R                  R                  4S jnS[        R                  R                  4S jmU4S jnSSKJn  [        [        T R                  R                  5      5       H  n[        [        UR                  5      5      S:X  d  M'  UR                  S:X  d  UR                  S	:X  a  UR                  [        R                  L dc  UR                  S	:X  a7  UR                  [         R"                  L a  U" UR$                  S   5      (       d  U" U5      (       d  U" U5      (       d  M  T R'                  U5        M     S
 nU 4S jn[)        5       mS[*        [         R,                  [         R.                  4   4S jm/ nT R0                   GH  nU" U5      S LnU(       a&  UR                  (       d  UR3                  U5        M9  M;  UR                  (       d,  [5        UR6                  S   [8        5      (       d
  U" U5        Mx  UR6                  S   n	[5        U	[8        5      (       a  M  [5        UR6                  S   R:                  [         R<                  5      (       a  UR6                  S   R:                  n
UR6                  S   R>                  n[         R@                  RB                  RE                  U
5      (       d  [G        U
RI                  5       5      nURK                  5        HT  n[M        URN                  U5      n[P        RR                  " [         R,                  [         R.                  4UU4S jU5        MV     GM  U	RT                  b  U	RT                  OU	R:                  nT" TU5        GM     U H1  nU" U5      nUc  M  UT;  a
  U" U5        M   TRW                  U5        M3     g )Nb_nodec                    U SL a  g[        U [        R                  5      (       d  gU R                  R	                  S5      nUc  gUSL a  g[        U[
        R                  5      (       a  UR                  R                  5       =nb  U$ g)NTFr  )	r  r   Noder  r  r   SymBoolr  maybe_as_bool)r  brs      r   is_static_true;OutputGraph.remove_unused_graphargs.<locals>.is_static_true  sw    ~fbgg..0AyDy1emm,,&&..00Q= r   r  c                     SSK Jn  [        U [        [        [
        45      (       a  g[        U [        R                  5      (       a%  [        U R                  R                  S5      U5      $ g)Nr   SymTypesTr  F)
torch.fx.experimental.sym_noder  r  r   r  r   r   r  r  r  )r  r  s     r   is_symnode_arg;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_arg  sJ    ?!c5$/00!RWW%%!!&&**_"=xHHr   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   rz   r  r  r  s     r   r  WOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s     <)Q~a(()   c              3   4   >#    U  H  nT" U5      v   M     g 7fr   rz   r  s     r   r  r    s     G2FQ~a((2Fr  T)
r  r  r  r  r  r  r  r   r   r  )r  r  r  s     r   is_symnode_compute_nodeDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node  sp    ?ww/)diimmO<hGG <$))<<<G$++2D2D2FGGGr   r   )is_accessor_noderr  r  c                 
   U R                   S   nUR                  n[        U[        R                  5      (       aI  [        UR
                  R                  [        R                  5      (       a  UR
                  R                  $ g r  )	r  r  r  r   r  r  exprsympySymbol)r  r  r  s      r   placeholder_binds_symbolEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  s^    ))J'CkkG'5<<00Z!!5<<6 6 ||(((r   c                    > [         R                  SU R                  S   R                  R	                  5       5        U R                  S	 TR                  U 5        TR                  R                  U S 5        g )NzREMOVE UNUSED GRAPHARG %sr  )r   r  r  ry   r  r  r  r  )r  r   s    r   remove_unused:OutputGraph.remove_unused_graphargs.<locals>.remove_unused  s\    II1499Z3H3O3O3T3T3VW 		*%T"!!%%dD1r   fakec                      U [        U5      -  n g r   )r   )used_symbolsr  s     r   update_used_symbols@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols  s    L..Lr   r  c                    > T" TU 5      $ r   rz   )tr  r
  s    r   r  5OutputGraph.remove_unused_graphargs.<locals>.<lambda>  s    .A,PQ.Rr   ),rh  r   r  Argument%torch.fx.experimental.symbolic_shapesr  r  r   rp   rQ  r  r  r  rw  operatorgetitemr   _checkr   r  r  r
   r  r   r  r   r  r  ra   r  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__rY  rX  wrapped_objpytreetree_map_onlyfake_tensorremove)r   r  r  r  r  r  r  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictattrfake_attr_valr  symbolr  r  r
  s   `                @@@r   r  #OutputGraph.remove_unused_graphargsx  s    	277#3#3 	&	bgg.. 		" 	KT$**"2"234D4

#$)GGz)?2t{{hFVFV7V?2 KK5<<7*499Q<88.t44'--$$T* 5		2 +.%	/E%,,:T4U 	/  "%%D3D9ELzz(//5 " zz*IIj)+@+ + "$' ))J/C!#'<== !$))J"7"?"?ASASTT*.))J*?*G*G*.))J*?*R*R$~~AASS+    )-_-L-L-N(OI(1(807$3$?$?1" !' 4 4%*\\5<<$@$R$1!"	 )9 !+.??+FCKK  (d;I &N )D-d3F!-!$' !''/ )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)rw  )r  us     r   r  EOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>%  s     ?JqF*Js   )torch._dynamo.symbolic_convertr*  rp   rQ  r  r  r  r   	item_memor  r  _exprr  r  should_specializer  r   replace_all_uses_withr   r  )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   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  rh  )r   r  s     r   r  #OutputGraph.add_output_instructions0  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)rK  r   rg  r   rO   r  r   )r   r  r   s      r   r  !OutputGraph.install_global_unsafe8  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   rJ  rK  r  r   r  r   r  s       r   install_global_by_id OutputGraph.install_global_by_idD  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_globalS  s!      ""4/r   c                    S U l         U R                  R                  5         S U l        U R                  R
                   H"  nSUR                  ;   d  M  UR                  S	 M$     U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R                   R                  5         g r  )rc  r   r   r[  rp   rQ  r  r  r  r+  r\  rb  rq  rS  rE  ri  rt  r  s     r   cleanupOutputGraph.cleanup^  s    $(!JJ$$DTYY&IIj) % 	##%  &&(!##))+##))+--335""$  &&(  &&(!!#r   r  c                 :    U R                   R                  U5        g r   )rb  r   )r   r  s     r   add_graph_finalizerOutputGraph.add_graph_finalizers  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  rr  )r  r  r  r   rw  r  s     r   example_value_from_input_node)OutputGraph.example_value_from_input_nodex  sH    77m#99Z(000ww*$$$t{{++r   ).rf  rt  ru  rv  rG  rg  r8  r,  rJ  r  r-  r{  ro  rV  rq  r  r  rF  rm  rE  rK  rx  rn  r`  rd  r[  r  rs  r   rr  rL  rb  rc  r}  rh  r+  re  ra  rl  rD  rS  r3  rZ  r_  ri  r\  )r  r   )tmp)Fr   )r   N)kr{   r|   r}   r~   r   r>   r   r   r   r   r   r)   r   r  r   r  r  rw  r`   r  r  rW  r   r|  r  r  r   r  r  r  r  r  r  rp   setterr  r  r  r
  r  r  ry  contextmanagerr  r   r  r  r   r  r  r   r   r  rp  rF  rI  rL  rQ   rR  r[  r  re  staticmethodrm  r   Proxyru  r
   r   r   r   r  r  r  r   r  r  r  r3  r<  rC  r  r  r  r  rb   r  r   r(   r  r  r  r  r   r  r  r  r-   r  r  r<  r  rA  rD  rG  r   r   r   s   @r   r*  r*  Z  sh    @
38n@
 j)@

 @
 @
 @
D
*D&5O 5)*::Xd3i=P ::
 4&8BG#4 &#
      & ) ) \\* * 7 7 4 4 1 1A@@  .   . . 8 8 A// A A >DcN > > F F"
H$& N N'05
  		,/			Jehhoou||S89JXT/lS0r a&'a& #a&F501<C 2  "7@E)N 7d277m 7 7 E4> E E@..@26v,@	@I..I26v,I	IVU\\ 2 '
T0l 'D d;.?  D  
QS 	s 	$*?"*BNN+;T+A"B?	?
,%((-- , ,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.htmlc                   ^  US:w  a  g U 4S jnU 4S jn[        U[        R                  R                  5      (       a?  [        R                  R
                  UR                  ;   a	  U" U5        g U" USU S35        g [        U[        R                  R                  5      (       Ga*  [        UR                  5       5      n[        U5      S:X  aM  [        X'S   5      n[        R                  R
                  UR                  ;   a	  U" U5        g U" USU S	35        g [        R                  R                  R                  T R                  X44S
5      u  p4 [        R                   R"                  " UR$                  /UQ70 UD6n	[        UW	5      n[        R                  R
                  UR                  ;   a	  U" U5        g U" USU SU	 S35        g g ! [&         a  n
[)        SS[+        U
5      / S9   S n
A
NsS n
A
ff = f)Nr  c                 ^   > U R                   S;   a  g TR                  R                  U 5        g )N>   atenprimprims)	namespacero  r   )rw  output_graphs    r   encountered_compliant_op8check_pt2_compliant_op.<locals>.encountered_compliant_op  s)    88))--f5r   c                    > TR                   R                  U 5        [        R                  (       a  [	        SSUS-   [
        -   / S9  g g )Nz Encountered non-PT2-compliant opr   r  )rn  r   r$   only_allow_pt2_compliant_opsr6   err_epilogue)rw  r   rT  s     r   encountered_non_compliant_op<check_pt2_compliant_op.<locals>.encountered_non_compliant_op  sB    &&**62..:#I4	 /r   z%Encountered the torch.ops.OpOverload z that is not PT2 compliant.r#   r   z:Encountered the non-overloaded torch.ops.OpOverloadPacket z that is not PT2 compliant. Fz*Error when attempting to resolve op packetr  r  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r  r   _ops
OpOverloadTagpt2_compliant_tagtagsOpOverloadPacketr(  	overloadsr  rX  r  r@  get_fake_values_from_nodesrL  rj  _jit_resolve_packet_qualified_op_namer   r6   r   )rT  kindrw  r   r   rU  r[  rc  r  overloadr  s   `          r   check_pt2_compliant_opri    s   6
 &%**//0099&&&++5$V,$3F8;VW	
 	&%**5566&**,-	 y>Q1.Byy**bgg5(,(..4X 6./ }}**EE##d^U

	xx33)),04:H VX&99&&"''1$R((=fX F33;* =%&I 70  	DF		s   .G 
G;G66G;c                        \ rS rSrS rS rSrg)	LazyProxyi  c                 4    Xl         X l        X0l        X@l        g r   )r  r   r   r   )r   r  r   r   r   s        r   r   LazyProxy.__init__  s    	r   c                 N    U R                   " U R                  0 U R                  D6$ r   )r   r   r   r   s    r   r   LazyProxy.__call__  s    ww		1T[[11r   )r   r   r   r  N)r{   r|   r}   r~   r   r   r   rz   r   r   rk  rk    s    2r   rk  c                   R  ^  \ rS rSrSrSU 4S jjrS r   SU 4S jjr SU 4S jjrS r	 SS jr
S	 rS
 rS\\\R                   R"                  4   4S jrS\\R&                  \R(                  4   S\\   4S jrS\R&                  S\\R4                     4S jrS rS rSrU =r$ )rC  i  z
Holds an FX graph that is being traced. OutputGraph owns a SubgraphTracer
and the separation of responsibilities is that SubgraphTracer is
responsible for building the graph while OutputGraph is responsible for
compiling and executing the graph.
c                   > [         TU ]  5         [        R                  " U5      U l        [
        R                  R                  5       U l        X0l	        0 U l
        0 U l        X l        X@l        0 U l        0 U l        S U l        SU l        SU l        SU l        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                  R4                  U R                  R7                  U5      U4/-   U l        [9        5       U l        / U l        [
        R>                  " 5       (       a  [A        S5      eg )NFr#   r   zSInference mode is supposed to be disabled during compilation. Please open an issue.)!r   r   weakrefrt  rT  r   r   Graphrp   r/  r  r  r  r  lifted_freevarsr  	prev_instunder_activation_checkpoint#allow_side_effects_under_checkpoint,unsafe_allow_externally_visible_side_effects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   )r   rT  r  r/  r  r   s        r   r   SubgraphTracer.__init__  sY   #MM,7XX^^%
 # 9; =? *  " TV ,1(
 490
 =B9 ,1(:@:L 2 2Q 6RS!#' $(!
 ;;#%D #';;#>#>**=9=IB $D 
 -7L 8:)**,,e  -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   rt  _COPY_META_FIELDSr  )r   rE  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   c           	      J  >^^^^^ U R                   b^  [        R                  " X445      u  p/ n
U H%  nU R                  U5      nU
R	                  U5        M'     [        R
                  " X5      u  p4[        TU ]  UTX4XVU5      mU R                  R                  n[        R                  S:  a  US;   a  UR                  mTU R                  La  TR                  bu  TR                  R                  b^  UR                   mUR#                  TR                  R                  S9mUUUU4S jn[$        R'                  S[)        U5      5        TU l        SnUR                   U R*                  La  [,        R.                  " UR                   5      R1                  SS 5      " 5       n[3        U[4        R6                  R8                  5      (       ao  S	nUR:                  R<                   Vs/ s H  nUR>                  PM     snU l         URB                  U l"        URF                  RH                  RJ                  U l&        OS U l         S U l"        S U l&        URN                  nU(       a'  URQ                  5       TRR                  R>                  S
'   US;   a>  U RT                  TRR                  RV                  T4/-   TRR                  R>                  S'   OUS:X  a  U R                   b  [Y        SSU RZ                   3S/ S9  U RT                  TRR                  RV                  []        U4S jTRR                  R>                  S
   R_                  5        5       5      4/-   TRR                  R>                  S'   U Ra                  UTRR                  5        U(       Gd+  S
TRR                  R>                  ;  a:  URN                  nU(       a'  URQ                  5       TRR                  R>                  S
'   STRR                  R>                  ;  a  US;   a>  U RT                  TRR                  RV                  T4/-   TRR                  R>                  S'   OyUS:X  as  U R                   b  [Y        SSS/ S9  U RT                  TRR                  RV                  TRR                  R>                  S
   T   S   4/-   TRR                  R>                  S'   STRR                  R>                  ;  a  / nU(       aJ  URc                  5       (       d  UR	                  URe                  5       5        [g        USS 5      nU(       a  MJ  URi                  5         [j        Rl                  Ro                  U5      Rq                  5       nSRs                  U5      TRR                  l:        [4        Rv                  Rx                  Rz                  (       d)  [4        Rv                  Rx                  R|                  (       aD  U R                  R~                  R                  U R                  R                  TRR                  5        T$ s  snf )Nr  r  )r  c                  v   > [        TT5      R                  5       n STR                  R                   ST SU  3$ )NzTRACE FX call z from r6  )rT   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   rm  Forig_graphmodulec                      g r   rz   rz   r   r   r  -SubgraphTracer.create_proxy.<locals>.<lambda>	  s    Dr   Tnn_module_stack>   r  r  r  r  z4Invoking an nn.Module inside a higher order operatorzHigher order op name: zThis is not supported.r  c              3   f   >#    U  H&  u  nu  p#UR                  S 5      S   T:X  d  M"  Uv   M(     g7f)@r   N)rW  )r  r   r_  tyrw  s       r   r  .SubgraphTracer.create_proxy.<locals>.<genexpr>	  s4      *QJAw773<?f4 *Qs   !1	1z2Invoking an nn.Module inside a HigherOrderOperatorr  r#   r  r  )Ar  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r
  rT  rL  r  r  r  ru  	positionsr  r~  get_line_of_code_headertrace_call_logr  r\   r{  r/   get_contextr  r  r   r   r   rp   rQ  r  r|  _lineno_mapr}  r   __code__r2  r~  r  r   r  r  r  r6   r  rH  r   r  is_co_filename_from_nn_modulesframe_summaryrX  r  r   StackSummary	from_listr[  ri  r  r  r$   r  track_nodes_for_deduplicationrL  
track_node)r   rg  rw  r   r   r  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsr  maybe_new_argrE  r  is_retracingorig_graphmodule_maybendr  frame_summariesmsgsr  r  r  r  r   s     `                  @@@@r   r
  SubgraphTracer.create_proxyP	  s%   V ;;"#)#6#6~#F IM  $ H H M$$]3 ! "00JLDW!&$9I

 )) w&4 4
 ,

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

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

d#  $$TYY5r   c           	         [        U[        R                  5      (       a%  U R                  R	                  UR
                  5        [        R                  SUUb  UR                  5       OSUU R                  U5        Uc  U R                  c   SU SU S35       eU R                  (       ad  U R                  cW  [        USS9(       dH  U R                  R                  R                  U/ 5      R	                  [         R"                  " 5       5        [%        XR&                  5      nU R(                  (       av  [+        [-        U R(                  5      5      nU R(                  U   R.                  nU(       a  U 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        Ou[        U[D        [F        45      (       aZ  [I        U5       HK  u  p[        U[        R                  5      (       d  M&  S nU(       a
  [K        X^SS9nU RC                  UU5        MM     [        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  rz   r  F)r  r  index_is_slice)+r  r   r   r  r   _versionr   r  r  rz  r  r/  rE   rT  re  
setdefaultr   extract_stackrW   r  r  rH  r  r  rp   inserting_beforeinserting_afterr
  r_   popitemr   compileris_compiling_lift_basic_symbolsr   r(  r  rB   r  r   r  r  r  )r   r  r  r  beforery   	prev_namer  ctxrt  r   r   is_strict_exportis_non_strict_exportr  r  e_sources                    r   r  !SubgraphTracer.create_graph_input1
  s    mU\\22--44]5K5KL		E#/FKKMX	
 >;;* 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((.
   &2  $~~#(>>#>#>#@ #,@mU\\::,,]Ce}== )- 8)!U\\::$#'!'4%+U(H 00H= !9 -66:""''< < ?D""=#5#5#:#:;{ SSs   G7O		
Oc                    U R                   c   S5       eUR                  R                  S   n[        U[        R
                  5      (       aG  UR                  R                  U R                  ;   a#  U R                  UR                  R                     $ XR                  ;   a  U R                  U   $ UR                  U R                   :w  a  U R                   R                  U5        UR                  R                  S   nU R                  UR                  R                  [        U5      U5      nX0R                  U'   U$ )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr  )r  r  r  r  r   r  r   r  rt  r  lift_tracked_freevar_to_inputr  r  r  )r   rt  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   c                 0  ^  [        U[        R                  R                  5      (       dJ  [        U[        5      (       a3  [	        U 4S jUR
                  UR                  UR                  4 5       6 $ U$ UR                  T :X  a  U$ T R                  U5      $ )z
If arg is a free variable, then lift it to be an input.
Returns the new lifted arg (if arg was a freevar), else the
original arg.
c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   )r  )r  sub_argr   s     r   r  ESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>
  s&      'FG @@II'Fs   !)
r  r   r   rM  slicestartstopstepr  r  )r   r  s   ` r   r  2SubgraphTracer.maybe_lift_tracked_freevar_to_input
  s     #uxx~~..
 #u%%(+		388SXX'F  
ZZ4J11#66r   e_proxyc                 b  ^ ^ UR                   m[        T[        5      (       d   eS[        4U 4S jjnU4S jn[        U[        R
                  5      (       Ga  [        UR                  5       5       H  u  pVU" U5      (       d  M  [        R                  SUUUTR                  5        [        TUUS[        R                  R                  R                  R                  X%40 [!        U5      S9nT R#                  Xg5        M     UR$                  [        R&                  L a  [        UR)                  5       5       H  u  pVU" U5      (       d  M  [        R                  SUUUTR                  5        [        TUUS[        R                  R                  R*                  R                  X%40 [!        U5      S9nT R#                  Xg5        M     GO;UR$                  [        R,                  L aA  T R#                  UR/                  5       U5        T R#                  UR1                  5       U5        OUR$                  [        R2                  [        R4                  1;   aA  T R#                  UR7                  5       U5        T R#                  UR9                  5       U5        OnUR$                  [        R:                  [        R<                  1;   a@  T R#                  UR?                  5       U5        T R#                  URA                  5       U5        [C        U5      (       aB  URE                  5       u  pU H)  n
[G        X5      nT R#                  U[G        X*5      5        M+     g g [        U[        RH                  5      (       a4  U" U5      (       a&  URJ                  RL                  nUTRN                  U'   g g g )Nr   c                 ^  > SSK Jn  U" U 5      =(       a    [        U R                  R                  [
        R                  5      =(       ac    U R                  R                  R                  U R                  R                  5      =(       a#    U R                  R                  TR                  ;  $ )Nr   )r   )
r  r   r  r  r   r  r  r  is_unbacked_symintr  )r2  r   r   s     r   	need_bind8SubgraphTracer.track_unbacked_symbols.<locals>.need_bind
  sq    I A :qvv{{ELL9:FF$$77D: FFKKt'9'99	r   c                 X   > TR                   " U0 UD6n[        UR                  U 5        U$ r   )r
  r_   r  )r  r   r   rt  r  s       r   _proxy_with_example_valueHSubgraphTracer.track_unbacked_symbols.<locals>._proxy_with_example_value  s+    ''88Eejj-8Lr   z>_track_unbacked_symbols %s for %s.size()[%s] at debug_level %sr  r  z@_track_unbacked_symbols %s for %s.stride()[%s] at debug_level %s)(r  r  rC  r   r   r   r  r/  r   r  rz  rk  opsrP  sym_sizer   r  track_unbacked_symbolslayoutstridedstride
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr"   __tensor_flatten__rX  r  r  r   r  )r   r  r  r  r  r  r2  
lazy_proxyattrsr  r%  inner_tr   r  s   `            @r   r  %SubgraphTracer.track_unbacked_symbols
  s     &.1111	D 		
 mU\\22!-"4"4"67Q<<IIX** "+1'		//33 "&q'	"J //>' 8* ##u}}4%m&:&:&<=DA ||		^#".. &/"5+!IINN5599$L&*1g	&
 33AB' >* %%)9)99++M,B,B,DgN++M,A,A,CWM%%%*:*:E<L<L)MM++M,F,F,H'R++M,E,E,GQ%%%*:*:E<L<L)MM++M,F,F,H'R++M,E,E,GQ,];;*==?
!D%m:G//9OP " <
 u||44''$))..-4$$T* ( 5r   r  srcc           	        ^   SS[         [        [        R                  4   S[        [
           S[        SS 4U 4S jjjn[        U[        R                  5      (       Gat  [        UR                  5       5       H+  u  pEU" UUb  [        U[        R                  U5      OS SS9  M-     UR                  [        R                  L a}  [        UR!                  5       5       H+  u  pEU" UUb  [        U[        R"                  U5      OS SS9  M-     U" UR%                  5       Ub  [        U[        R&                  5      OS SS9  GO;UR                  [        R(                  L aA  T R+                  UR-                  5       U5        T R+                  UR/                  5       U5        OUR                  [        R0                  [        R2                  1;   aA  T R+                  UR5                  5       U5        T R+                  UR7                  5       U5        OnUR                  [        R8                  [        R:                  1;   a@  T R+                  UR=                  5       U5        T R+                  UR?                  5       U5        [A        U5      (       aF  URC                  5       u  pgU H-  n[E        X5      n	T R+                  Xb  [G        X(5      OS 5        M/     g g [        U[        R                  5      (       a
  U" UU5        g g )	Nr2  ry   r  r   c           	        > [        U 5      (       d  g [        U [        R                  5      (       d   eTR	                  U 5      n[        U5      S:X  a  g TR                  b  TR                  R                  X5        U H  nTR                  R                  U   nUR                  R                  S   n[        U[        R                  5      (       d   eTR                  [        U5      [        U5      UUUS9n[        R                  SUUb  UR!                  5       OSTR"                  5        UTR$                  U'   M     g [        U5      S:X  d   SU SU  35       eUc   S	U  S
U  S35       e['        [)        U5      5      nTR                  [        U5      [        U 5      U UUS9n[        R                  SU Ub  UR!                  5       OSTR"                  5        [+        UU SS SS9UR                  R                  S'   g )Nr   r  )r  ry   z4_lift_symbols_in_symint %s from %s at debug_level %szsubgraph inputsr#   zyFor root tracer, we only expect to bind basic symbols (compound symbols should be cached before) but got unbound symbols z in zSource of 'z' is None when lifting it to input of top-level. If it's an unbacked symbol, this could be because it's not tracked with lazy_bind_unbacked_symbols. Otherwise, should provide a source when create_graph_input for `z` at root tracer.F)pass_arg_as_tensorr  	is_tensorr  )r   r  r   r  lookup_unbound_symbolsr  r  r  r  r  r  r  r   r  r   r  r  rz  rt  rH  iterrb   )	r2  ry   r  self_to_be_bounds0parent_proxyexample_valphr   s	           r   _lift_symbols_in_symintCSubgraphTracer._lift_basic_symbols.<locals>._lift_symbols_in_symintP  s   
 q>>a....#::1=#$) {{&//:*B#';;#<#<R#@L"."3"3"8"8"IK%k5<<@@@@00B[)#%% 1 B IIN)/);AR((	 :<D((6# +( +,1 HHXGYY]^_]`b1 ) !! %WWXVYYjl)
 $/01,,GG!! -  		J%+%7FKKM=N$$	 ,4', $#,Z(r   T)r  )F)$r
   r   r   r  r   r   r   r  r   r  r/  rL   rK   SIZEr  r  r  STRIDEstorage_offsetSTORAGE_OFFSETr  r  r  r  r  r  r  r  r  r  r  r  r"   r  rX  r?   )
r   r  r  r  r  r2  r  r  r%  r   s
   `         r   r  "SubgraphTracer._lift_basic_symbolsI  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   r2  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  )r2  s    r   r  7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>  s    r   )r   )r  r   r   r  r  r   r  rk  r   r   rM  r  r  )r   r2  r   to_be_boundr
  rt  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	5      (       a%  U	 Vs/ s H  obU   PM	     n
nSU
 3n[        SU5      $ [        SS5      $ s  snnnf s  snf )Nr  r  zInput mutation detected at TFr  )r  rp   rQ  r  r  r  r   r   r   r  r  r'  r  r   )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           	      2   SSK Jn  [        5       nU R                  R                   H|  nUR
                  S:X  ai  U" U/5      S   n[        U[        R                  5      (       a<  [        UR                  5       5      nXR;   a  SX%    SU 3n[        SU5      s  $ X2U'   Mz  M|    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R                  5       5      nXW;   a  S	Xu    SU	 3n[        SU5      s  $ XU'   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   )_collect_fake_inputsr  z*Input-to-input aliasing detected at nodes z and Tr   r  z,Output-to-output aliasing detected at nodes z, z+Input-to-output aliasing detected at nodes Fr  )torch._higher_order_ops.utilsr%  r   rp   rQ  r  r  r   r   r    _typed_storager   r  r  tree_leavesr   r   rY  r  ri  )r   r%  input_storagesr  r  storager   output_storages	out_nodesout_nodeintersected_storagesr2  aliasedr  os                  r   r   SubgraphTracer.has_aliasing  s   F>BfJJ$$Dww-' 4dV <Q ?mU\\::,]-I-I-KLG0 J>KbJcchimhno+D#66.27+ ;  % @DvJJ))X)6q9	**9>>!+<=Hx 4hZ @ C%mT::::mU\\::,]-I-I-KLG1 L_MeLffkltkuv+D#66/7G, >  .2247K7K7MM#$q( BVAUA"O$67AU   iiG DGDAA3eA3G DEG?yICc**E2&& !Es   2HH
)r{  r  r~  r}  r|  r  rw  r  rz  rp   r  r/  ry  rt  rT  r  ru  r  r  r  rv  rx  )NFN)NNN)NNNN)FN) r{   r|   r}   r~   r   r   r  r
  r  r  r  r  r  r
   rk  r   r   rM  r  r  r   r   r   r  r   r  r  r  r"  r   r   r   r   s   @r   rC  rC    s    QhC4 up HL(6. DHk\&P7>`5&+Iuxx~~,E&F`5F~"5<<#=>~EMfEU~D9 9ell9K 9*':*' *'r   rC  )r   rX  ry  r   r:  r  r]  r&   r  rj  r  r   rr  dataclassesr   r   r'  typingr   r   r   r   r	   r
   r  torch._guardsr   torch._loggingtorch.distributeddistributedr_  torch.nntorch.utils._pytreer@  _pytreer  r   r   torch._C._dynamor   torch._dynamo.excr   r   r   r   r   r   r   r   torch._subclasses.fake_tensorr   torch._utils_internalr   r  r   %torch.fx.experimental._backward_stater   r  r   r   r   r   r   torch.fx.passes.runtime_assertr    torch.multiprocessing.reductionsr    torch.utils._ordered_setr!   torch.utils._python_dispatchr"   r  r$   r%   r   r'   backends.registryr(   r)   bytecode_transformationr*   r+   r,   r-   r.   r/   r  r0   current_scope_idr1   device_interfacer2   r3   r4   r5   r6   r7   graph_deduplicationr8   graph_region_trackerr9   r:   r;   mutation_guardr<   r+  r=   r>   ry   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   variables.baser`   variables.builderra   rb   rc   rd   variables.ctx_managerre   variables.listsrf   variables.miscrg   rh   variables.nn_moduleri   variables.tensorrj   rk   rl   rm   variables.torch_functionrn   r/  ro   	getLoggerr{   r   rX  getArtifactLoggergraph_tabular_logr  graph_sizes_logr  RootGuardManagerrv   r   r   r   r   r   r   r   r   r   r   r   r   r   objectr  r   r   r*  rZ  ri  r^  rI  rk  TracerrC  rz   r   r   <module>rZ     s  *         	 
   4 F F       $ $  # N  5 0 : ?  K ; / F D D 5  '  - 6  ; 4 / 0 @         * ,  ; - 6 1  C H !NN44XwG 11(LI..228]K11(LI**  $   $  
 $  
 0 4 4 - - - UXX__    D 	S&[ $( $( $(N 	X 	X 	Xc,( c,N9J FR  oo' 2 2G'RYY G'r   