
    i<                       d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZmZmZmZ ddlmZm Z  ddl!Z!ddl"Z#ddl$Z#ddl%m&Z' ddl(Z#ddl)m*c m+Z, ddl#m-Z-m.Z. dd	l/m0Z0 dd
l1m2Z2m3Z3 ddl"m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZL ddlMmNZN ddlOmPZP ddlQmRZR ddlSmTZT ddlUmVZVmWZWmZXmYZY ddlZm[Z[m\Z\ ddl]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZh ddlimiZi ddljmkZk ddllmmZm ddlnmoZo ddlWmpZpmqZqmrZrmsZsmtZt dd lumvZvmwZw dd!lxmyZy dd"lzm{Z{ dd#l0m|Z|m}Z} dd$l~mZ dd%lmZmZmZ dd&lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd'l*mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd(lmZ dd)lmZmZmZmZ dd*lmZ dd+lmZ dd,lmZ dd-lmZ dd.lmZmZmZ dd/lmZ dd0lmZ erdd1lmZ dd2lmZ dd3lmZ  ej                  eū      Ze#j                  j                  ed4      Ze#j                  j                  ed5      Ze#j                  j                  ed6      Ze#j                  j                  ed7      Ze0j                  Ze#j                  j                  j                  Ze#j                  j                  j                  Z ed89       G d: d;             Z ed89       G d< d=             Z ed89       G d> d?             Z G d@ dA      Zej                  dBefdC       Ze G dD dE             ZdFedBeg ee   f   fdGZ G dH dIe#j                  j                        Z G dJ dK      Zeeef   Ze G dL dM             Ze G dN dO             Ze G dP dQ             ZdRedBeeef   fdSZ G dT dUe      Z G dV dWe      Z G dX dY      ZdZZd[ed\ed]ed^ed_edBdfd`Z ej                         Z eda      Z e db      Z G dc dd      Z G de dfe-j                        Zy)ga  
Core graph building functionality for PyTorch's Dynamo system. This module contains
the essential components for constructing and managing FX graphs during compilation:

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

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

The module supports key Dynamo features including:
- Higher-order operators through nested SubgraphTracers
- Graph deduplication for optimization
- Symbolic shape handling and propagation
- Side effect tracking and management
- Guard insertion and management
    N)Callable	GeneratorSequence)	dataclassfield)CodeType)AnycastOptionalTYPE_CHECKINGUnion)	ParamSpecTypeVar)fxTensor)guards)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourcetracingTracingContext)is_opaque_type)
FakeTensor)signpost_event)_ConstraintTarget)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnvSpecializationuninteresting_files)Target)insert_deferred_runtime_asserts)
OrderedSet)is_traceable_wrapper_subclass   )configexclogging	variables)
CompiledFn
CompilerFn)create_binary_slicecreate_binary_subscrcreate_build_tuplecreate_call_functioncreate_dup_topcreate_instructioncreate_load_constcreate_rot_ncreate_swapInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)get_interface_for_device)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplementedunimplemented_with_warning)has_user_objectsindex_to_bytecode_constructor)apply_graph_deduplication)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffectsValueMutationExisting)_get_source_debug_name
AttrSourceBackwardStateSourceConstantSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceNumpyTensorSourceParamBufferSourceShapeEnvSourceSyntheticLocalSourceTensorPropertyTensorPropertySource)_extract_tensor_dictcheckpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedget_instruction_source_311get_locals_to_stealget_static_address_typeget_unique_name_wrtgraph_break_reasonsincrement_op_countistypelazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)VariableTracker)BackwardStateGraphArgGraphArgTrackedFakewrap_fx_proxy)ContextWrappingVariable)BaseListVariable)NullVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableUnspecializedPythonVariable)TensorWithTFOverrideVariable)UserDefinedDictVariable)CompilePackage)InstructionTranslatorBase)StorageWeakRefgraph
graph_codegraph_sizes
trace_callT)frozenc                   "    e Zd ZU eed<   eed<   y)VariableTrackerCacheKeyvt_idsourceN)__name__
__module____qualname__int__annotations__r        T/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/output_graph.pyr   r      s    J Nr   r   c                   "    e Zd ZU eed<   eed<   y)AliasingInfohas_aliasingmsgNr   r   r   boolr   strr   r   r   r   r          	Hr   r   c                   "    e Zd ZU eed<   eed<   y)MutationInfohas_mutationr   Nr   r   r   r   r   r      r   r   r   c                   V    e Zd ZddZdededee   fdZdedededdfdZ	dd	Z
dd
Zy)VariableTrackerCachereturnNc                     i | _         y N)cacheselfs    r   __init__zVariableTrackerCache.__init__   s	    EG
r   valuer   c                 h    t        t        |      |      }|| j                  vry | j                  |   S r   r   idr   )r   r   r   keys       r   lookupzVariableTrackerCache.lookup   s/    %bi8djj zz#r   vtc                 L    t        t        |      |      }|| j                  |<   y r   r   )r   r   r   r   r   s        r   addzVariableTrackerCache.add   s    %bi8

3r   c                 d    t               }|j                  j                  | j                         |S r   )r   r   update)r   	new_caches     r   clonezVariableTrackerCache.clone   s&    (*	tzz*r   c                 8    | j                   j                          y r   )r   clearr   s    r   r   zVariableTrackerCache.clear   s    

r   r   N)r   r   )r   r   r   r   r	   r   r   rr   r   r   r   r   r   r   r   r   r      sR    HC  H_4M  f / d r   r   r   c                  4    t        j                  t              S r   )torchdynamo_loggingget_step_loggerlogr   r   r   _step_loggerr      s    ..s33r   c                   V    e Zd ZU dZeed<   eej                     ed<   dZ	e
ed<   ddZy)	GraphCompileReasonzOStores why a given output graph was compiled; i.e. what caused the graph break.reason
user_stackTgraph_breakNc                 H    | j                   rt        j                  |        y y r   )r   rj   appendr   s    r   __post_init__z GraphCompileReason.__post_init__   s    &&t, r   r   )r   r   r   __doc__r   r   list	tracebackFrameSummaryr   r   r   r   r   r   r   r      s,    YKY++,, K-r   r   random_callsc                 .     dt         t           f fd}|S )Nr   c            
      N    D  cg c]  \  } }} | |i | c}}} S c c}}} w r   r   )fnargskwargsr   s      r   _gen_rand_valuesz1_get_gen_rand_values_fn.<locals>._gen_rand_values   s+    <HII(8D&D#F#IIIs    )r   r	   )r   r   s   ` r   _get_gen_rand_values_fnr      s    Jd3i J r   c                        e Zd ZdZdeeej                  j                  f   f fdZ	defdZ
deeej                  j                  f   ddfdZ xZS )FakeRootModulez'Trick the constructor of fx.GraphModule
nn_modulesc                 l    t         |           |j                         D ]  \  }}t        | ||        y r   )superr   itemssetattr)r   r   kv	__class__s       r   r   zFakeRootModule.__init__  s6    $$& 	 DAqD!Q	 r   r   c                      y)NzFakeRootModule(...)r   r   s    r   __repr__zFakeRootModule.__repr__  s    $r   Nc                 N    |j                         D ]  \  }}t        | ||        y r   )r   r   )r   r   r   r   s       r   add_nn_moduleszFakeRootModule.add_nn_modules  s*    $$& 	 DAqD!Q	 r   )r   r   r   r   dictr   torchnnModuler   r   r   __classcell__r   s   @r   r   r     sU    1 4UXX__(<#=  
%# % c588??.B)C    r   r   c                   r    e Zd ZdeddfdZdej                  j                  deej                     de
fdZy)WrapperBackendbackendr   Nc                     || _         y r   )r   )r   r   s     r   r   zWrapperBackend.__init__  s	    #*r   gmexample_inputsc                    t        |      | _        || _        t        j                  | j                        }| j                  ||      | _        | j                  "| j                  | j                  j                  u r| j                  j                  S t        j                  s| j                  S 	  | j                  j                  t        |       } | j                  t        |       }t        ||      r| j                  | j                          S t        d|        # t        $ r t        j                  d        w xY w# | j                          w xY w)Nzincorrect results of backend zerror in verify_correctness)r`   restorer   copydeepcopyr   	candidateforwardr,   verify_correctnessrb   rp   RuntimeError	Exceptionr   	exception)r   r   r   copy_gmcorrectresults         r   __call__zWrapperBackend.__call__  s
    ),--(g~>>>!T^^tww%F77??"((>>!	%dggoo|N'CDG#T^^\.%ABF GV$~~ LLN !>tfEFF 	MM78	 LLNs   /AD D  D??E E)r   r   r   r1   r   r   r   GraphModuler   r   r0   r   r   r   r   r   r     sE    +
 +t +((&&8<U\\8J	r   r   c                      e Zd ZU dZeed<   eed<   eej                  j                     ed<   e
e   ed<   eeeeef   f   ed<   eed<   eej                   j"                  j$                     ed<   eej(                     ed	<   ej*                  j,                  j.                  j0                  ed
<   ej2                  j4                  ed<   eej2                  j6                     ed<   dZeed<   dZeed<   dZeed<   dZ ee   ed<   e!de"fd       Z#e!dej2                  j4                  fd       Ze!deej2                  j6                     fd       Z$ddZ%y)OutputGraphGuardsStatear  
    A base class containing fields that are considered "persistent" when we
    want to save all the important state for reconstrucing guards in a different
    process. Normally we don't need to add states here, but we may have to when
    the information is needed to serialize the guards, so the fields here are
    supposed to be serializable as a requirement.
    local_scopeglobal_scopetorch_function_mode_stackguard_on_key_orderinput_source_to_sizes_strides
dual_levelfunctorch_layerscurrent_deviceglobal_state_guard_guards_aotautograd_guardsFexportskip_guards_checkexport_constraintsN%name_of_builtins_dict_key_in_fglobalsr   c                 0    t        dt        |              )Nz%shape_env shouldn't be accessed from )AssertionErrortyper   s    r   	shape_envz OutputGraphGuardsState.shape_env[  s    DT$ZLQRRr   c                     | j                   S r   )r  r   s    r   r   zOutputGraphGuardsState.guards_  s    ||r   c                     | j                   S r   )r  r   s    r   aotautograd_guardsz)OutputGraphGuardsState.aotautograd_guardsc  s    '''r   c                 b   t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S )N)r   r   r   r   r   r  r  r  r  r
  r  r	  r  r  r  )r   r   r   r   r   r   r  r  r  r  r
  r  r	  r   r  r  r   s    r   dump_guards_statez(OutputGraphGuardsState.dump_guards_stateg  s    %((**&*&D&D#66*.*L*L!22..#66262\2\;;#66KK $ 7 7"44
 	
r   )r   r   )&r   r   r   r   Scoper   r   r   	overridesTorchFunctionModesetr   r   r   r	   r   
_functorchpyfunctorchFuncTorchInterpreterr   device_C_dynamor   GlobalStateGuardr  	GuardsSetGuardEnvExprr  r   r  r	  r
  propertyr$   r  r  r  r   r   r   r   r   <  s_    #EOO$E$EFFF##'S#X(>#??O5++77LLMMU\\**((//@@@]]$$$emm8899 FD#t#$$;?)8C=?S8 S S //   (D)C)C$D ( (
r   r   c                      e Zd ZU dZdZeed<    ee      Z	ee
ef   ed<    ee      Zee   ed<    ee      Zee
   ed<    ee      Zeeeeedf   f      ed	<    ee      Zee   ed
<    ee      Zeee
eedf   f      ed<   y)StackLocalsMetadatazf
    Stores metadata for a frame's stack and locals for the purposes of building resume functions
    r   	num_stackdefault_factorylocals_namesstack_null_idxeslocals_null_keys.stack_ctx_argsstack_ctx_idxes_origlocals_ctx_argsN)r   r   r   r   r$  r   r   dc_fieldr   r'  r   r   r(  r)  r*  tupler	   r+  r,  r   r   r   r#  r#  |  s     Is#+$L$sCx.  #+4"@d3i@"*4"@d3i@8@QU8VNDsE#s(O345V&.t&D$s)D9ARV9WOT%U38_ 456Wr   r#  c                      e Zd ZU  ee      Zeeef   ed<    ee      Z	eee
eef   f   ed<   ej                  j                  j                   Zeej                  j                  j&                  ej                  j                  j(                  f   ed<    ee      Zeeeeeej                  j                  j&                  ej                  j                  j(                  f   f   f   ed<   y)ExportMetaDatar%  graph_input_idx_to_local_sourceoutput_return_typeout_specmodule_call_specN)r   r   r   r-  r   r1  r   r   r   r2  r.  r   r	   r   utils_pytree
_LEAF_SPECr3  r   TreeSpecLeafSpecr4  r   r   r   r0  r0    s    
 :BRV9W#T#v+%6W
 6>d5SS%S/12S 	&& eEKK''00%++2E2E2N2NNO  	& dS%++44ekk6I6I6R6RRSST	V 'r   r0  r   c                 H    | d   }t        |t              s|j                  }|S )N__builtins__)
isinstancer   __dict__)r   
f_builtinss     r   get_builtins_dictr?    s)     n-Jj$'((
r   c                        e Zd ZdZ	 	 	 	 ddedeeeef      dee   dee	   deee
ee   f      f
 fdZed	efd
       Zddeded	dfdZ xZS )OutputGraphCommona  
    A minimal interface for full graph capture. It is intended to be
    the target of any tracer that feeds into backends.

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

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

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

    The serializable part of this interface is OutputGraphGuardsState.
    We do not need to serialize other parts; however it will pay to
    be disciplined about what those other parts are, especially since
    we want other tracers to be able to meaningfully implement them,
    and we should generally try to cut them down when possible.
    Noutput_graph_guards_stateimport_sourcesr  export_metadatatracked_fakes_id_to_sourcec                    t         |   |j                  |j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                          |xs i | _        |xs
 t%               | _        |xs
 t)               | _        |xs i | _        y r   )r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r	  r
  rC  r$   
_shape_envr0  rD  rE  )r   rB  rC  r  rD  rE  r   s         r   r   zOutputGraphCommon.__init__  s     	%11%22%??%88%CC%00%66%44%88%--%99%,,%77%88%KK	
$ -2 $1xz.B.2B&," 	'r   r   c                     | j                   S r   )rG  r   s    r   r  zOutputGraphCommon.shape_env  s    r   r   r   c                     t         r   )NotImplementedErrorr   r   r   s      r   bypass_packagez OutputGraphCommon.bypass_package  s
     "!r   NNNN )r   r   r   r   r   r   r   r   r$   r0  r   r   r   r   r!  r  r	   rL  r   r   s   @r   rA  rA    s    4 48(,48HL$
#9$
 !c3h0$
 H%	$

 ".1$
 %-T#tF|2C-D$E$
L 8  "S " " "r   rA  c                   ~
   e Zd ZU dZeed<   	 d~deeef   de	e
   dddedee   d	ed
edededeej$                  j&                     de	d   deddfdZddZddZdefdZ	 ddededeeej4                  j6                  f   fdZdej4                  j6                  fdZddZde	ee      fdZde def   deedf   defd Z!de g ef   ddfd!Z"dd"Z#e$dd$       Z%e$dd%       Z&defd&Z'e$dej4                  jP                  fd'       Z)e)jT                  d(ej4                  jP                  ddfd)       Z)e$deeej6                  f   fd*       Z+e$deejX                  ejZ                  f   fd+       Z.e$dee/j`                  e1ej4                  j6                  d,f   f   fd-       Z2ded.edej4                  j6                  fd/Z3ded.edej4                  jX                  fd0Z4ded.eddfd1Z5e6jn                  	 dd2e	e8   d3d#d4e	e   de9ejt                  ddf   fd5       Z;e$dd6       Z<e$dejz                  j|                  fd7       Z?e$de@fd8       ZAe$dej                  j                  fd9       ZDe$deeef   fd:       ZEe$deej                  j                     fd;       ZG	 dd<e	eeee def   ef   f      ddfd=ZHdd?ZIdd@ZJe$ddA       ZKdeLfdBZMdefdCZNdefdDZOdEede1ej                  j                  ef   fdFZRddGedefdHZSdGeddfdIZTeUdJedefdK       ZVdLedMedej6                  fdNZWdOe1ej                  j                  ejZ                  ef   dJedPedefdQZXd>ddeeeY   eeZeZf   f   fdRZ[d>ddSeLdeee   e\f   fdTZ]	 	 dd>ddUe^dVedSeLdee\   f
dWZ_d>ddXe`ddfdYZad>ddZee   dXe`ddfd[Zbdd\ZcddUed.eddfd]Zddeeee1eLef      f   fd^ZedGedefd_Zfe6jn                  defd`       ZgddaZhd>ddbee   dceideeY   fddZje$deejX                     fde       Zke$deel   fdf       Zmdgej                  dhee-   deofdiZpdgej                  dhee-   deofdjZqdeeej4                  j                  f   fdkZrdGedlej4                  j                  defdmZsdeejZ                     fdnZtddoZuddpZvddqZwdeeY   ddfdrZxdGed(eddfdsZyded(edefdtZzded(edefduZ{ddvZ|dwe ej                  gdf   ddfdxZ}dyej4                  jX                  defdzZ~d{ej                  j                  d|eZddfd}Zy)OutputGrapha  
    Wrapper class to hold outputs of InstructionTranslator.  Mainly the
    generated fx.Graph.

    OutputGraph is 1:1 with a frame being processed. Each frame is associated
    with some root InstructionTranslator. When user code calls a function,
    we construct a InliningInstructionTranslator that continues to write into
    the root InstructionTranslator's OutputGraph.
    side_effectscode_optionscompiler_fnroot_txr   r  r	  frame_stater   r   f_coder   packager   	one_graphr   Nc                 	   t         j                  | |||
t               i t        j                  j
                  j                  t        j                  j                  j                         t        j                  j                  j                  t        j                  j                  j                  xs2 t        j                   j                  j"                  j%                         t        j&                  j)                         g        t+        | |      g| _        i | _        || _        || _        || _        g | _        t9        t:              | _        t               | _        |	j@                  |	jB                  |	jD                  d| _#        tI               | _%        g | _&        tO        | jL                  |xs tP        jR                  |xs tP        jT                  tP        jV                  | jF                        }dd l,mc m(} |j[                  d      5  t        j\                  j_                  |ta        | j0                        | j0                        }d d d        tc              | _2        | jd                  jf                  ji                  |	       | jd                  jf                  | _3        tk        jl                         | _7        | jq                          ts        jt                  tv              | _<        i | _=        t}        |       | _?        t               | _A        t        j                         | _D        t        |      | _F        g | _G        d| _H        g | _I        || _J        || _K        || _L        i | _M        g | _N        g | _O        d| _P        i | _Q        t        j                   j                         | _S        d| _T        t        i       | _U        t        i       | _V        | j                          i | _X        g | _Y        d | _Z        g | _[        i | _\        d | _]        d | _^        | j                         | _`        t        j                         | _c        | j                         | _e        i | _f        t               | _h        t               | _j        y # 1 sw Y   1xY w)	N)r   r   r  r  r  r  r  r  )	is_export)co_nameco_filenameco_firstlineno)tracked_fakesallow_scalar_outputsallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards	co_fieldsr   F(fake_tensor_allow_unsafe_data_ptr_access)r  allow_non_fake_inputsr  )kr   r   r  r   autograd
forward_ad_current_levelr  r  #retrieve_all_functorch_interpretersr5  _deviceCURRENT_DEVICEr  convert_frameinitial_global_stater  r   r  r  r  SubgraphTracertracersinput_source_to_varr  r	  rV  cleanup_hooksnext_compile_id_counter
compile_idinstalled_globalsr\  r]  r^  rc  rI   region_trackerr_  r$   r,   capture_scalar_outputs capture_dynamic_output_shape_opsrb  torch._functorch.configpatch_subclassesFakeTensorModer   r   tracing_contexttraced_coder   r   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   rE  param_name_to_sourcerN   rR  r   variable_tracker_cache	itertoolscountunique_var_idr   rS  output_instructions	timestampregister_finalizer_fnsrT  rU  rX  source_to_user_stacks_current_txcleanupsshould_exitunspec_variable_map_is_torch_function_mode_enabledtorch_function_mode_enabled!has_user_defined_allowed_in_graphnon_compliant_opscompliant_custom_opssave_global_state dynamo_flat_name_to_original_fqnr   random_values_varpregraph_bytecodebackward_statebackward_state_proxybackward_state_var!install_builtins_dict_in_fglobalsr
  
contextlib	ExitStackcompiler_trace_stack+maybe_install_saved_tensors_hooks_subgraphs"saved_tensors_hooks_subgraph_namesrC  r0  rD  r)   "used_inlined_inbuilt_modules_names)r   rS  rT  rU  r  r	  rV  r   r   rW  r   rX  rY  r  _config	fake_modes                   r   r   zOutputGraph.__init__  s    	''%"u*,~~00??"--99]]_ ;;..==$}}::OO  :xx&&779MM++- " 	( 	
" 'tv>? CE "4&68#$78+.5
 ~~!--$33
 12 13 ,,!*!Kf.K.K+4 ,7668>8j8jnn
	& 	21]]E]J 	))88#&*4;;&7{{	 9 I	 0>i/H((//7//;;--/ 	 	  " ##D) 	' BD!'- ';&<#&__.,0,>68   OQ# 2= RT"<>+- KM  ,188+S+S+U( 27. >AW ADB! 	  AC-  	 '+ 57 ;=>B!15 224 	2 %/$8$8$:! <<> 	/
 /1-/ DN</C	 	s   A Q>>Rc                 P    | j                   j                  t        dd             y )Nbytecode_tracingTlog_pt2_compile_event)r  enter_contextre   r   s    r   mark_bytecode_tracing_startz'OutputGraph.mark_bytecode_tracing_start  s$    !!//"&*	
r   c                 8    | j                   j                          y r   )r  closer   s    r   mark_bytecode_tracing_stopz&OutputGraph.mark_bytecode_tracing_stop  s    !!'')r   c                 P    t        | j                        }| j                  d|      S )N__builtins_dict__)r?  r   install_global)r   r>  s     r   r  z-OutputGraph.install_builtins_dict_in_fglobals  s&    &t'8'89
""#6
CCr   hookprefixc                     | t        | j                         }|| j                  vsJ || j                  |<   || j                         fS r   )lenr  get_backward_state_proxy)r   r  r  names       r   add_backward_state_hookz#OutputGraph.add_backward_state_hook  sV     #d112344.....$(D!T22444r   c                 f   | j                   | j                  rt        dddg        t               }| j                  j                  dt        |      |t                     | _         t               | j                   j                  j                  d<   | j                         | _        | j                   S )Nz&backward_state does not support exportrO  z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_stater   grapharg)r  r  rD   r    root_tracercreate_graph_inputr  rR   rs   nodemetanew_varr  )r   example_values     r   r  z$OutputGraph.get_backward_state_proxy  s    $$,{{D U	 *OM(,(8(8(K(K']#*,	 )L )D% ?T>UD%%**//
;&*llnD#(((r   c                    | j                   j                  t               j                  t        j
                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               t        j                  j                  j                         }|@| j                   j                  t               j                  t        j                                t        j"                  j$                  j&                  sA| j                   j                  t               j                  t        j(                               y y r   )r   r   r[   
make_guardrJ   	SHAPE_ENVrU   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICEGLOBAL_STATETORCH_FUNCTION_STATEr   r  r  peek_interpreter_stackFUNCTORCH_STACK_MATCHr  compiled_autogradin_compiled_autograd_regionAUTOGRAD_SAVED_TENSORS_HOOKS)r   cis     r   r  zOutputGraph.init_ambient_guards  sr    	(33L4J4JKL**<+P+PQ	
 	)+66|7M7MNO)+66|7R7RST)+66|7P7PQR**<+L+LM	
 XX  779>KKOO!#..|/Q/QR }}..JJKKOO!#.. == Kr   c                 b   t         j                  j                  j                  ry t         j                  j
                  j                  j                  }t         j                  j
                  j                  j                  } |       } ||      sy |\  }}| j                  dt         j                  j                  | j                  |j                              }| j                  dt         j                  j                  | j                  |j                              }|dk(  sJ |dk(  sJ ||gS )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_autogradr5  top_saved_tensors_hooks"saved_tensors_hooks_are_inlineableinstall_subgraphr   r   r   r   )r   	get_hooksare_inline_hookshookspack_gm	unpack_gmpack_subgraph_nameunpack_subgraph_names           r   r  z7OutputGraph.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"$899r   r   .r   c                 ~    | }| j                         }t        | j                        j                  fd       j	                  t        t        j                  j                  |             j                  t        |      d       j                  |       | j                  j                  j                                t        |      }t!        j"                  | j                  ||      }|j%                         }t'        j(                         j*                  j,                  j/                  |       |S )z]
        call fn(*args) before the graph runs and turn the result into a fake input.
        c                  P     j                  j                  j                        S r   )load_import_fromr   r   )cgr   s   r   <lambda>z3OutputGraph.synthetic_graph_input.<locals>.<lambda>T  s     B'' r   F)r  r>   rU  add_push_nullforeachmapr/   ConstantVariablecreatecall_functionr  storer  extendget_instructionsr\   rr   buildrealizer   getguards_contextdynamo_guardsremove_guards_with_source)r   r   r   r  varnamer   r   r  s    `     @r   synthetic_graph_inputz!OutputGraph.synthetic_graph_inputJ  s     D	,,.t||$
	
 	

3y1188$?@
TE*
%%b&9&9&;<%g. &&t||]FK!++99SS	
 r   c                 :    | j                   j                  |       y r   )rr  r   )r   r   s     r   add_cleanup_hookzOutputGraph.add_cleanup_hookf  s    !!"%r   c                 z    t        | j                        D ]	  } |         | j                  j                          y r   )reversedrr  r   )r   r  s     r   call_cleanup_hookszOutputGraph.call_cleanup_hooksi  s3    T//0 	DF	  "r   ro  c                      | j                   d   S Nr   rp  r   s    r   r  zOutputGraph.root_tracern  s    ||Ar   c                      | j                   d   S Nr  r   s    r   current_tracerzOutputGraph.current_tracerr  s    ||Br   c                 2    t        | j                        dk(  S )Nr+   )r  rp  r   s    r   is_root_tracerzOutputGraph.is_root_tracerv  s    4<< A%%r   c                 .    | j                   j                  S r   r  r   r   s    r   r   zOutputGraph.graphz  s    ""(((r   r   c                 &    || j                   _        y r   r  )r   r   s     r   r   zOutputGraph.graph  s    $)!r   c                 .    | j                   j                  S r   )r  input_name_to_proxyr   s    r   r  zOutputGraph.input_name_to_proxy  s    ""666r   c                 .    | j                   j                  S r   )r  real_value_cacher   s    r   r  zOutputGraph.real_value_cache  s    ""333r   	LazyProxyc                 .    | j                   j                  S r   )r  bound_symbolsr   s    r   r  zOutputGraph.bound_symbols  s    ""000r   r   c                 :     | j                   j                  |i |S r   )r  create_proxyr   r   r   s      r   r  zOutputGraph.create_proxy  s     /t""//@@@r   c                 :     | j                   j                  |i |S r   )r  create_noder  s      r   r  zOutputGraph.create_node       .t""..???r   c                 :     | j                   j                  |i |S r   )r  remove_noder  s      r   r  zOutputGraph.remove_node  r  r   source_targetprior_tracerdescriptionc              #     K   t               }	 |r|j                  | j                  u sJ |j                          |r|n-t	        | | j                  || j                  j
                  |      }| j                  j                  |       | |j                  d d d        | j                  j                          y # |j                  d d d        | j                  j                          w xY ww)N)parentr  r[  r  )
r?   r!  r  	__enter__ro  r[  rp  r   __exit__pop)r   r  r  r  new_scope_ctxtracers         r   	subtracerzOutputGraph.subtracer  s      ()	#**d.A.AAAA##%   #.."/"11;; +  LL'L""4t4LL ""4t4LLs   C+A=B9 .C+9/C((C+c                     | S r   r   r   s    r   outputzOutputGraph.output  s    r   c                 ^    | j                   j                  J | j                   j                  S r   )r~  r  r   s    r   r  zOutputGraph.fake_mode  s,    ##--999##---r   c                     | j                   j                  J | j                   j                  j                  J | j                   j                  j                  S r   )r~  r  r  r   s    r   r  zOutputGraph.shape_env  sP    ##--999##--77CCC##--777r   c                 B    | j                   j                  j                  S r   )r~  r  r  r   s    r   r   zOutputGraph.guards  s    ##22@@@r   c                 B    | j                   j                  j                  S r   )r~  module_contextr   r   s    r   r   zOutputGraph.nn_modules  s    ##22===r   c                 B    | j                   j                  j                  S r   )r~  r  r  r   s    r   r  zOutputGraph.aotautograd_guards  s    ##22EEEr   outc                 *   t        t        t        t        t        dt
        f   t        f   f   ||n| j                  j                  j                        }t        j                  t        j                         f|d<   t        j                  t        j                  d      t        j                   d      f|d<   t        j                  t        j                  d      t        j                   d      f|d<   t        j                  t        j"                  d      t        j$                  d      f|d<   t        j                  t        j"                  d      t        j$                  d      f|d	<   t        j&                  t        j(                         f|d
<   y)zc
        Saves to out if it is provided. Else saves to the tracing context's global_state.
        .Ngrad_enabledcudaautocast_enabledcpuautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r
   r   r   r.  r   r	   r   r~  global_contextglobal_stater   set_grad_enabledis_grad_enabled	functoolspartialset_autocast_enabledis_autocast_enabledset_autocast_dtypeget_autocast_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)r   r0  r;  s      r   r  zOutputGraph.save_global_state  s_    eHS#X.4556 ? ))88EE
 ).(>(>@U@U@W'X^$ e88&A%%f-,
'(
 e88%@%%e,0
+,
 e66?$$V,.
)*
 e66>$$U+.
)*
 ,,++-2
-.r   txc                 :    | j                   j                  |       y r   )r  r   )r   rF  s     r   push_txzOutputGraph.push_tx  s    #r   c                 6    | j                   j                         S r   )r  r$  r   s    r   pop_txzOutputGraph.pop_tx   s    ##%%r   c                 P    | j                   s| j                  S | j                   d   S r  )r  rU  r   s    r   
current_txzOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMr   c                 ,    t        | j                        S r   )rc   r   r   s    r   rc   zOutputGraph.count_calls  s    4::&&r   c                 X    t        t        | j                  j                              dk(  S r  )r  r   r   nodesr   s    r   is_empty_graphzOutputGraph.is_empty_graph
  s!    4

(()*a//r   c                     t        | j                  j                  D cg c]  }|j                  dk(  s| c}      dkD  S c c}w )Nr)  r   )r  r   rO  op)r   xs     r   has_outputszOutputGraph.has_outputs  s5    tzz//D!14483CADEIIDs
   AAkeysc                     |sJ | j                   }|j                  d      D ]$  }t        |t              r||   }t	        ||      }& |S N.)r   splitr<  r   getattr)r   rU  objr   s       r   get_submodulezOutputGraph.get_submodule  sN    tBF//C 	&A#t$!fc1o		&
 
r   r  c                     t        | j                  d         }	 | dt        | j                         }||vr| j                  dxx   |fz  cc<   |S 9)Nco_varnames_)r  rS  rs  r  )r   r  existingvars       r   r  zOutputGraph.new_var  s`    t((78F!D!3!3456C("!!-0SF:0
	 r   c                 X    || j                   d   vr| j                   dxx   |fz  cc<   yy)z/Ensure self.code_options.co_names contains nameco_namesN)rS  )r   r  s     r   update_co_nameszOutputGraph.update_co_names#  s2    t((44j)dW4) 5r   namesc                  ^   dj                  t        t        |             }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd	|      }t        j                  d
d|      }|r|d   j                         sd|z   }|S )Nr_  z9\._(?:modules|parameters|buffers)\[(['\"])([^'\"\]]+)\1\]z.\2z2getattr\(\s*([^,]+?)\s*,\s*(['\"])([^'\"]+)\2\s*\)z\1.\3z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)joinr  r   rerg  isalpha)re  r  s     r   module_key_namezOutputGraph.module_key_name(  s     xxC(vvH&RV
 vvA8T
 vv,eT:vvlIt4vvosD147??,4<Dr   attr_prefix
attr_valuec                 h   t        |t        j                  j                        r>| j                  j                         D ]!  \  }}||u s| j                  d|di       }|c S  t        || j                        }|| j                  |<   | j                  d|di       }t        |j                  |       |S )Nget_attrr   )
r<  r   r   r   r   r   r  ri   rq   r  )r   rl  rm  r  modproxy	attr_names          r   %register_static_attr_and_return_proxyz1OutputGraph.register_static_attr_and_return_proxy@  s    
 j%((//2!__224 !	c*$ --j$BGE L!
 (T__E	 &0	"!!*iR@%**j1r   targetoptionsc                 f   
 t         j                        r!t        j                   j                  fi S t              dv sJ d   t        t              rJ t        t        j                        r? j                   j                         s j                  dt        dt        f fd}nt        t        j                  j                        rxt        t        j                  j                        sJ r<t!        j#                  t$        j&                               dt        dt        ffd}nfdt        dt        ffd}nRt        t        j(                  t        j*                  f      rdt        dt        f fd}ndt        dt        f fd} j,                  j/                         D ]  \  }}|u s ||      c S  t1        j2                  | 
t5        
 j,                   j6                        
 j,                  
<   t        t        j                  j                        rhd	t        dd f
 fd
}t9        d      r j;                         D ]  \  }}	 ||        t9        d      r j=                         D ]  \  }}	 ||         |
      S )Nr   
module_keyr   c           	      B   j                   J j                   | <   j                  J j                  j                  j                  v r#j                  j                  j                     S t	              dk(  r9t        t              s)t        j                  t        j                               n3t              s(t        j                  t        j                               t        j                  j                  d| di       fdi}j                  j                  j                  j                  |      }d|j!                         j"                  j$                  vsJ t'              |j!                         j"                  j$                  d<   |S )Nguardedro  r   r  tensor_dict)r  rU  r)  rR  rh   r<  rY   rK   r  rJ   ID_MATCHrV   TENSOR_MATCHrv   r  track_object_existingas_proxyr  r  r_   )rw  r   ru  r   r   rt  r&  s     r   	wrap_namez6OutputGraph.register_attr_or_module.<locals>.wrap_names  se   00<<<8>))*5 ||///T\\00===<<..;;FCC*62i?
-I "&"3"3L4I4I"JK+F3!&"3"3L4M4M"NO"LL''
JBG #) 	 \\((55KKFTVW$BKKM,>,>,C,CCCC9Mf9U""''6	r   c                 2    t        t              | fi S r   )rz   r  rw  ru  rt  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  s    +DL*fXPWXXr   c                 0    t        j                  fi S r   )r/   UnspecializedNNModuleVariabler  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  s    $BB6UWUUr   c           	      Z    t        j                  j                  d| di       fdiS )Nro  r   sym_num)r|   r  r  )rw  ru  r   rt  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  sA    &--%%j*b"E # 	 r   c                     j                   j                  |        j                  | <   t        j                  t        |             S )N)source_name)r)  rd  r   rr   r  rS   )rw  r   rt  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  sF    ++J706!!*-&,,"z: r   	leaf_namec                     j                   J t        |       } d|  }|j                   |<   t        t              r-| j                  t
        j                  |j                        <   y y rW  r  rZ   r<  rX   r  rQ  rk  r  r  
new_sourcenew_namer  r   r   s      r   register_leaf_namez?OutputGraph.register_attr_or_module.<locals>.register_leaf_name  sw    00<<<.vyA
"V1YK06@))(3fk2 " 99#33JOOD 3r   _parameters_buffers)rL   r  rr   r  rL  r   r<  rZ   r   r   r  r
  r  r   r   r   rK   r  rJ   	NN_MODULESymIntSymFloatr   r   rQ  rk  ri   r   hasattrnamed_parametersnamed_buffers)r   rt  re  ru  r  r   r   r  r  r_  r  r   r&  s   `` `      @@@r   register_attr_or_modulez#OutputGraph.register_attr_or_moduleS  s`     4 #((&LGLLw-7""""f&7888fell+((F&&( )) c  o    D 0fehhoo666f//0F0FGHY# Y/ YV# V/ V u~~ >?c o  c o  OO))+ 	$DAqF{ |#	$
 **E2"4$:K:KL &fehhoo."c "d " v}-$*$;$;$= 2LIq&y12vz*$*$8$8$: 2LIq&y12 r   c                    | j                   j                  d      }t        |      }|sg i fS g }i }g |j                  |j                  j                         | j                  j                  j                         }|r |j                         }t        |t              r,t        |j                  t              sJ ||j                  z  }O|| j                  j                  vst        |j                  t              r`t        |j                   t"              rFt        |j                   j$                  t&              r"|j                   j$                  j(                  |v s|j                   j$                  j(                  }||vrg ||<   ||   j+                  |       |r i }	i }
| j,                  D ]:  }t        |j.                  t              r2t        |j                   t&              r|j                   j(                  |v sQ|j                   j(                  }|| j0                  d   v sJ ||   D ]  }|j                   |
v r|j                   J |j                   j2                  }||	vrU| j5                  | d      }||	|<   |j7                  t9        d|      t;        |      t=               t9        d|      g       |j                   J |j                   }t'        |	|         |
|<    = ||
fS )Nr   r^  _ref	LOAD_FASTargval
STORE_FAST)r   r  rg   stacksymbolic_localsvaluesrR  store_attr_mutationsrU  r$  r<  rx   r   r   mutation_typerM   r   rT   baserX   
local_namer   	graphargs_examplerS  indexr  r  r7   r8   r3   )r   rF  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuerS  stolen_namevisitedoverridden_sourcesarg	list_namelist_idx
alias_name
old_sources                   r   handle_aliases_for_stolen_listsz+OutputGraph.handle_aliases_for_stolen_lists  s    ##''//9 r6M8:
XX
&&(
 3388:
 		A!-.!!''4000  T..CCC!!//3LMqxx7qxx}}k:HHMM,,0AA((--22K+-+-K($++A.+ . 35>> '	PC3<<.szz;7JJ))[8 

--I 1 1- @@@@ + P8811 xx+++88>>7*!%$+T*"J )3GH%&&.{9M-h702.|JO	 xx+++XX
1<WX=N1O":.9P'	PZ ...r   
stack_popsc                    |j                          g }t               }t        |j                        D ]  \  }}t        j
                  j                  |       t        |j                        |z
  |k  r|j                  |       St        |t              r|j                  j                  |       n|j                  |       t        |t              s|j                  dnt        |j                        }|j                  j                  t        |      dz
  |f       |j                   j                  |       
 t        |      |_        t%        |j'                         |j)                         z         }|j*                  j-                         D ]5  \  }	}
t        |
j.                  t0              r(|
j.                  j2                  |	k(  r|| j4                  u rI|	|v rNt6        j8                  dk\  r6t:        j=                  t        |
      r8|j>                  j                  |	       t:        j=                  t        |
      rJ t        |j@                        |j@                  |	<   t        |
t              r@|
j                  dnt        |
j                        }|jB                  j                  |	|f       |j                  |
       8 ||fS )a  
        Gets the stack + locals values belonging to tx that need to be restored.

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

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

        Returns:
            - stack_values: stack and locals values that need to be restored
            - meta: locations of NULLs and ContextWrappingVariables in the stack/locals
                (ignores the top `stack_pops` values on the stack)
        r   r+   )      )"prune_dead_localsr#  	enumerater  r/   LazyVariableTrackerrealize_allr  r   r<  ry   r(  rw   target_valuesr.  r*  r+  r$  r  cellvarsfreevarsr  r   r   rX   r  rU  sysversion_infor  __instancecheck__r)  r'  r,  )r   rF  r  stack_valuesr  ir   r  cell_and_freevarsr   r   s              r   _get_stack_values_to_restorez(OutputGraph._get_stack_values_to_restore=  sY   " 	"$ ""((+ 	4HAu))55e<288}q J.##E*%.%%,,Q/##E*%!89--5B5ATAT;U  ##**C,=,A=+QR))003!	4$ \* => &&,,. !	#DAq 188[1HH''1,$,,&%%7* )),:))003  11,BBB#&t'8'8#9Da !45//1BuQ__7M  $$++Q,>?"C!	#F T!!r   r   partial_convertc                    | j                   J t        j                  s| j                   |u sJ | j                          || _        || _        d| _        t        j                  d|       g }t        j                  dk\  r| j                   j                  D ]r  }|j                  dk(  r=|j                  t        dt        | j                   j                   d                      O|j                  t#        j"                  |             t g }g }|}	|	t%        d |	j&                  D              sJ | j)                  |	|	|u r|nd	      \  }
}|j                  |
       |j                  |       t+        |	j&                        D ]  }|j-                  |	|j.                  
       ! |	j0                  }	|	| j2                  j5                  |       | j7                  |       | j8                  r| j:                  rJ d       | j7                  | j8                         | j=                  | j                         \  }}| j7                  |       | j?                          | j@                  jC                         D ci c]  \  }}|tE        |       }}}tG        |      }ddl$m%} t        | jL                        d	kD  rg }| jO                  d      | _(         |tS        | jL                        d      }| jU                  d|      }tW        | j                   ||      }|jY                  |j[                  |d             |jY                  t]        d	d             |j                  |j_                  | jP                               | j7                  |       |D cg c]  }|D ]  }|  }}}d}d}| j                   |u r)|r&t%        d |D              rt%        d |D              r t        ta        |            t        |      k(  r| j2                  jc                         r|jd                  s| jf                  s|d   jh                  s|d   jj                  stW        | j                         }| jm                  ||       | j7                  g | jo                  |tq        t+        |            |      |js                         tu        d      t        dt        |                   n| jO                  d      }tW        | j                   |||      }| jw                  |||       i }|jx                  jC                         D ]'  \  }} | dkD  st{        |t|        t~        f      r#d||<   ) tW        | j                   ||||      }!| jw                  |||!       t        j                  j                  j                  r+|r(t        |      dk(  r|d	   }"t        |"t        j                  j                  j                        r|"j                  t        j                  j                  j                  u r|"jB                  d	   }#|"jB                  d   }$t        |#t        j                  j                  j                        sJ i }%|!j                  j                         D ]M  }&t        |&t        j                  j                  j                        sJ |&j                  }'|&j                  |%|'<   O t        |#jB                        D ]  \  }(}"|"|%v rd|%|"   f| j                  j                  |(<   )|"j                  Kt        |"j                  dd      x})r2|)j                  r&d|"j                  f| j                  j                  |(<   |"j                         r*d|"j                         f| j                  j                  |(<   d|" d|( rJ  |$j                         | j                  _X        g }*t        | j                        d	k7  st        |!j                        d	k7  r|*jY                  | jo                  ||!j                         |             t        |!j                        d	k7  r#|*j                  |!j_                  |             d}n+|*j                  t        d              n| j                          | j7                  |*|!js                         z          | j7                  t        d!t        |      |d	   j                  z
        g       d	}+d	},t        |      D ]  \  }-}t        |j                        }.|-d	k7  r|.|j                  z  }.|.d	k(  r)| j7                  t        d!d	      gtu        d             n;|,|.z  },| j7                  t               gt        |+|,      tu        d             |+|.z  }+|-t        |      dz
  k(  stW        | j                         }/i }0| j                   j                  jC                         D ]~  \  }1}2t        |2j                  t~              s!|2j                  j                  |1k(  s;|/j                  |/j                  |1             t        |j                        t        |0      z   |0|1<    | j7                  |/js                         t        d!t        |0            t        d"d      gz          |j                  j                  |0        | j7                  t        d       t        d!t        |            gt        |d	   j                  dz                |r|r| j7                  t        d#|$      g       t        j                  j                  j                  d%v rd	d&lhmi}3 g }4| j2                  j                         D ]T  }5t        |5d'      s|5j                  }6t        |6t        t        f      s4t        |5t              rt        |5j                  |3      r|5jB                  jC                         D ]  \  }1}2i }7|2jB                  jC                         D ]0  \  }8}|j                         |7|8j                  j                         <   2 d(d)gtq        |7j                               k(  sJ |7| j                  j                  |1j                  j                         <    t        |5t              rt        |5j                  |3      rD|4j                  |5       W |4D 5cg c]  }5t        |5j                         }9}5|9rOt        j                  j                  j                  d*k(  rt        j                  d+|9        |S t        d+|9       |S c c}}w c c}}w c c}5w ),a  
        Compiles the current subgraph, with inputs w.r.t. self.root_tx, and codegens:
            - Call the compiled subgraph
            - Apply side effects
            - Codegen stack and locals
            - Store the locals

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

        Returns stack indices and locals keys where we dropped NULLs, and where we found inactive context manager objects.
        NTzCOMPILING GRAPH due to %sr     COPY_FREE_VARSco_freevarsr  c              3   <   K   | ]  }|j                           y wr   )can_restore).0blocks     r   	<genexpr>z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s     Kuu((*K   r   )is_graph_breakz)export does not support pregraph_bytecoder+   disablerandom_valuesz.do not trace into Dynamo rng recovery functionr   __gen_rand_values)r  Fc              3      K   | ]K  }t        |t        t        t        f       xr) t        |t              xr |j                         t        u   M y wr   )r<  r}   r{   r~   r|   python_typefloat)r  r   s     r   r  z/OutputGraph.compile_subgraph.<locals>.<genexpr>-  s`        3,4  V $A7TAMMOu<TUVs   AAc              3   <   K   | ]  }|j                           y wr   )	is_tensorr  rS  s     r   r  z/OutputGraph.compile_subgraph.<locals>.<genexpr>9  s     =aAKKM=r  r     UNPACK_SEQUENCE	graph_out)tempvarsr  r  inputconstantzEncountered unrecognized type z at output POP_TOP
BUILD_LISTLIST_EXTENDDELETE_FASTr  )warnerror)_ExportModuleSpecTrackerDictr  in_specr3  r  zWhile compiling, we found certain side effects happened in the model.forward. Here are the list of potential sources you can double check: )xrU  r,   nested_graph_breaksr  r  compile_subgraph_reasonr  r   debugr  r  prefix_instsopnamer   r7   r  rS  r   allblock_stackr  r   exitr   r!  rR  prune_dead_object_newadd_output_instructionsr  r  r  cleanup_graphr   r   ro   r   
decoratorsr  r   r  r  r   r  r>   r  load_function_namer5   create_storer  is_emptydebug_localsr  r(  r)  codegen_cellscompile_and_call_fx_graphr   r  r:   codegen_suffixusesrl   r\   rX   r   r  log_graph_in_out_metadatar<  r/   NamedTupleVariable	tuple_clsfunctional_exportExportTracerOutputListVariablegraph_outputsr  codegenGraphOutputEntryvariabler  r  rD  r2  r   rZ  is_inputis_python_constantas_python_constantr3  rc   r   graph_output_varsrun_compiler_collectiver$  r'  r6   r2   r  r  append_outputcreate_loadr   r9   side_effect_replay_policytorch.export._tracer  _get_modified_varsr  r  rM   rO   r   r   r   rU  r4  rP   warningsr  r   ):r   rF  r   r  r  r  install_stack_valuesall_stack_locals_metascur_txr  r  r  r  r  r  rp  nn_modules_proxiesrootr  random_calls_instructionsrand_fnrand_fn_namer
  valsvalstack_values_flatstored_graph_output_vargraph_output_varcell_cgpass1r  r  pass2r   flat_returnsr3  vt_to_graph_out_idxr   r  idxr   r)  	start_idxend_idxr  n_valsroot_cgunmodified_locals_namesr   r   r  potential_side_effectsra  mut_typespecsk_specside_effect_refss:                                                             r   compile_subgraphzOutputGraph.compile_subgraph  s   , ||'''))<<2%%% 	'').'-$		-v6 +-w&11 	9;;"22 ''*, #DLL$=$=m$L M !''		$8	9 !#68 K8J8JKKKK!%!B!Bfl
"L$ ##L1"))$/ "&"4"45 F

6&2D2D
EF ]]F  " 	//3$$\2**t{{ 	
7	
; 	$$T%;%;<*.*N*NLL+
'' 	$$[1 9=8M8M8O
+44D/#&&
 
 01' t  !A%(*%%)\\/%BD"'(9(9:GG  ../BGLLd7IG &,,**<> &,,-A!U-KL%,,$$T%;%;< (()BC& .>NTN#SNSNN"' LLB!  +  =+<==C)*+s3D/EE!!**,OO''*2.??*2.??  -Gr7+((33D*;!<=t
 --/ !^ ''8cBS>TU
  $||K8  #5	E $5u= H#jj..0 )
U19VC2F1T%U$(HSM)  !#5E $5u= $$>>%)*a/&q)r5==#:#:#M#MN}}66IIJ $&88A;L!xx{H%$emm&=&=&J&J   GI'!&!4!4!;!;!= D)%1F1F1W1WXXX49NN8=+H5D
 $-\-?-?#@ YR!44 + 3B 7LD00CCCH
 II1+2299fd+K!K!K & !( "		LD00CCCH  224 * " 5 5 7LD00CCCH
 &DB4{SVRW#XX#X+Y. 5=4O4O4QD((1F4::&!+s53F3F/G1/L222u7N7N7PRVW u**+q0MM%"4"45E"FG.2+MM"4Y"?@ ,,.((%2H2H2J)JK 	$$" -.1G1J1T1TT	
& 	 !78 -	BGAt**+FAv$..({,,*<Q?$Q 6!,,&(,Y@ %Q V#	 C./!33#DLL1:<' LL88>>@ DAq!!((K8QXX=P=PTU=U--g.A.A!.DE589J9J5Kc3O 6/2 ,,,,.*(c2I.J +=a@	 !!(()@A[-	Bv 	$$"9-"<S9O5PQ 4Q7AAAEF	
"  7((#M:JKL ==99=NNH%'"((;;= ?30"00H! #<>S"T &c+BC
II'CI ),		(9 	*1(*3477==? !&KFC(+(>(>(@ %*&))*F*F*H$I!& )2:'>$uzz|BT'T T'T %* !% 4 4 E E$%DD$;$;$=!"	* 's,CD *3996R S299#>5?8 ?U 7:&szz2     ==''AAVKMMXXhWik &% 'XXhWik 
 &%m

Z Or s   5uu )u&r  c                    | j                   j                  rd}|}|t        t        |j	                                     }|D ]T  }|| j
                  u r!|j                  |j                  |             2|j                  sJ  ||j                  |          V |j                  t        t        |                   |j                  }|dz  }||j                  t        d|             y |j                  t        dd             y )Nr   r+   r  r  )r  r   r.  sortedr  rU  r  create_load_closurepost_prune_cell_and_freevarsr4   r  r!  r7   )r   rF  r  tx_cntr  r  cells          r   r  zOutputGraph.codegen_cellsP  s    ''33F:<F$ !(@(@(B!CD$ FD-(()?)?)EF%BBBB6>>tDEF   !3CM!BC! $  /&IJ/!DEr   r  c                 T   | j                   j                         | j                  r| j                  rJ | j                  j	                         D ]V  \  }} |       | j
                  J j                  j                  | j
                               j                  |       X t        j                  r| j                   j                         |j                  D ]h  \  }j                  fd       |D ]
  } |        j                  t        t!        |      d             j                  t#        d      g       j | j%                  |       j'                  ||j                          t        j                  r| j                   j)                         y y )Nc                              S r   r   )r  	debug_vars   r   r  z,OutputGraph.codegen_suffix.<locals>.<lambda>  s    R	] r   Fr  )value_from_source)rR  codegen_save_tempvarsr  r  r   r  r  r  
store_attrr,   replay_side_effectscodegen_hooksr  r  extend_outputr5   r  r7   r  restore_stackcodegen_update_mutated)	r   rF  r  r  r  r"  r   r  r?  s	      `    @r   r  zOutputGraph.codegen_suffixi  sm    	//3{{"?!00668 $	c3..:::  0G0G!HId#	$
 %%++B/  " 	>OIt23 31#d)UCD0;<=	> 	2r"
RYYG%%44R8 &r   c                    | j                   sJ t        | j                  j                        }|D ]  }|j                  j                  dd         t        j                         }t        j                  |      D ]  \  }}|j                  t        j                  j                  u s-t        |j                        | fk(  sH|j                  rU|j                  d   }|j                  t        j                  j                  u st        |j                        | fk(  s|j                  r|j                  d   }| j                  j!                  |       | j                  j!                  |        y)z
        Remove "creation_timestamp" from node meta

        Remove this pattern from the graph:
            torch._C._set_grad_enabled(False)
            torch._C._set_grad_enabled(True)
        creation_timestampNr   )r  r   r   rO  r  r$  r   r=  r  pairwisert  r  _set_grad_enabledr.  r   _erased
erase_node)r   rO  r  r2  node1node2s         r   r  zOutputGraph.cleanup_graph  s    TZZ%%& 	6DIIMM.5	6 ,,.%..u5 	1LE5 : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MM#(::a=LJJ))%0JJ))%0	1r   c                 z   | j                   syt        j                  j                  j                  r*t        j                  j
                  j                  d      t        j                  d       t        j                  j                  dd fd       | j                   j                          d| _         y)zE
        Do not save this output graph to the CompilePackage
        NzDetected a package bypass: %sartifactc                      dddS )Nprecompile_cache_bypassjsonr  encodingr   r   r   r   r  z,OutputGraph.bypass_package.<locals>.<lambda>  s    1"! r   c                      di S )N_reasonr   )r   r   s   r   r  z,OutputGraph.bypass_package.<locals>.<lambda>  s    6    r   metadata_fn
payload_fn)rX  r   r  r,   strict_precompiler-   PackageErrorr   warning_loggingtrace_structuredbypass_current_entryrK  s    ``r   rL  zOutputGraph.bypass_package  s     ||==11--##00/  	3V<'' 	( 	
 	))+r   c                 \   i }| j                   j                  D ]  }|j                  j                  dd       }t	        |t
        j                  j                        sD|j                         }|D cg c]  }t	        |t              r|n
t        |      ! c}||j                  <    |S c c}w )Nr  )r   rO  r  r  r<  r   r|  r   sizer   reprr  )r   retr  r  rc  ss         r   get_graph_sizes_structuredz&OutputGraph.get_graph_sizes_structured  s    02JJ$$ 	VD IIMM/4@M-):):)E)EF$))+PT!U1z!S'9!tAw"F!UDII		V
 
 "Vs   1$B)c                 V   d}|d| dz  }| j                   j                  D ]  }|j                  j                  dd       }t	        |t
        j                  j                        sE|j                         }||j                   dt        |       dz  }g }d}|D ]g  }t	        |t              r|j                  |       %t	        |t
        j                        r(d}|j                  |j                  j                         g  |s||j                   d	t        |       dz  } |S )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
r  z: 
FTz (concrete): )r   rO  r  r  r<  r   r|  r   rc  r  r.  r   r   r  r  hint)	r   r  graph_sizes_strr  r  rc  concrete_size
has_symintszs	            r   get_graph_sizeszOutputGraph.get_graph_sizes  s   7VD622JJ$$ 	D IIMM/4@M-):):)E)EF$))+dii[5;-r#BB ""
 B!"c*%,,R0#B5%)
%,,RWW\\: "'#yyku]7K6LBO#	( r   c              #     K   | j                   j                  j                         }i }| j                  |       	 | j                   j                  j	                  |       d | j                   j                  j	                  t        |             y# | j                   j                  j	                  t        |             w xY ww)zj
        Momentarily restores the global state to what it was prior to tracing the current output
        )r0  N)r~  r:  copy_graphstater  restore_graphstater   )r   prior_global_statecurrent_global_states      r   restore_global_statez 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   9C)B %/C0CCc                 h   | j                   }|J |j                  xzj                  lj                  }t        j                  dj                         t        j                  j                  dd fd       |j                  }t        |      dk(  s%J dj                  dj                  |                   t        |j                               j!                  |j#                         t        j$                  j'                         z        5  t)        d	d
      5  d g|j+                         z  }t-        j.                  |j                  |       |_        d d d        d d d        |j0                  j3                          t4        j6                  y y # 1 sw Y   =xY w# 1 sw Y   AxY w)Nzcompiler_collective %srQ  c                      dddS )Ncompiler_collectivestringrU  r   r   r   r   r  z5OutputGraph.run_compiler_collective.<locals>.<lambda>  s    1 (% r   c                  8     j                   j                         S r   )local_staterender)dss   r   r  z5OutputGraph.run_compiler_collective.<locals>.<lambda>  s    2>>#8#8#: r   rY  r+   z&Expect only one device type but got {}+rx  Tr  )group)rU  distributed_state
all_states
compile_pgr   infor{  r   r_  r`  _device_typesr  formatrh  r@   r$  r  rankacceleratordevice_countre   rc  distall_gather_objectspeculation_logr   r-    CompileCollectiveRestartAnalysis)r   rF  r  device_typesr  r}  s        @r   r  z#OutputGraph.run_compiler_collective  s|   \\~~&&&B38MJHH-r~~>NN++ ; ,  &33L|$) 8??@VW) ))9)9);<CCOO%(9(9(F(F(HH
+ 2$O	
+ *.1B(B
&&z2>>T *
+ 
+ $$&666= 9N3 
+ 
+ 
+ 
+s$   F(">F F(F%	!F((F1rvr  c                    !"#$% t         j                  j                  j                         5  ddlm}  j                  sJ  j                          t         j                        dk(  rt        |      dk(  rg cddd       S t        dd      }t        |t              sJ t        |t              sJ  j                  dd j                   j#                  t%        d	 |D                    fi       } j'                         }|j)                  |        j                   j+                  ||       t,        j.                  st1        |      D ]L  }t3        ||      }	t        |	t4        j6                        s*t9        |	 j:                  | j<                  
       N  j?                          t9        t5        j6                  | j                         j:                  | j<                  
        jA                          t         j                        }
tB        d   dxx   |
z  cc<    jE                           jF                  jI                          tK        | j                        #ddl&m'}  |#        jP                  r( jP                  D ]  }tS        #|t3        ||               jT                  D ]
  } |#        tW        #jY                         d      H j[                  dt         j\                  j,                  j^                  #ja                  ddd              jb                  |#_2         jf                  #_3         jh                  jk                         #jl                  d<    jn                  #jl                  d<   |#jl                  d<   tp        js                  dtu        |#ddd             t         jv                  jy                  d fd#fd        j{                           j|                  j~                  }|J  j<                  s_ddl@mAc m} |j                  d      5  t         j                  j                  |j:                        }ddd        j|                  _?         j                         5   j                  # j                               "ddd       ddlHmI} t        "|      s&t        t3        "dd      |      rR"j                  dk(  rCt        "|      r"n"j                  }|j                  |       t        "|      s|j                  " jb                   jb                  j                  |"        |"d !      "tB        d   d"xx   dz  cc<   |j:                  J |j:                  j                  x}r4g %i $ j                  D cg c]  }|j                   }}|D ]  }|j                  |j                        }t        j                  |j                        j                         }t               }t        j                  ||j                  |g      }t        js                  d#|       %j                  t        j                  |fd$|      |f        t         j\                  j                  d !      d%t        d&t        d't        f"# $%fd(       } j                  ||       n j                  |"        j                  J t         j                        !t               r!j                  !fd)       g }t        j                         D ]<  } |!        j                         }!j                  |       |j                  |       > |D ]"  }!j                  !j                  |             $ !j                  t        t              d       !j                          t         j                        D ](  \  }} | j                   j                  j                  |<   * !j                  |       !j                         cddd       S # 1 sw Y   xY w# 1 sw Y   xY wc c}w # 1 sw Y   yxY w)*z
        Generate code from self.graph and return the Instruction()s to
        call that generated code.

        Code is generated w.r.t. self.root_tx.
        tx is only used for preserving GraphModule metadata
        r+   r  r   N__compiled_fnT)	with_uuidr)  c              3   <   K   | ]  }|j                           y wr   )r~  r  s     r   r  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>:  s     5Oqajjl5Or  )r  statscalls_captured)dce_hop_extra_outputszfGraph contains named parameters: either inline_inbuilt_nn_modules=False or there are static addresses.Fprint_outputinclude_strideinclude_device)inline_builtin_nn_modulesr   r  r  
backend_id%s)r  r  coloreddynamo_output_graphc                  (    d j                         iS )Nsizes)rg  r   s   r   r  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    $"A"A"CD r   c                  ,     j                  ddd      S )NFTr  )print_readable)r   s   r   r  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    2#4#4!&tD $5 $ r   )r[  rd  )r  )_LazyGraphModule__self___lazy_forwardz"do not trace Dynamo-compiled graphr  unique_graphsz:Compiling backend specialized graph with specialization=%sc                      |||          S r   r   )r+  r   check_fns      r   r  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    X$(IF" r   r   r   r   c                     	D ]  \  }} ||       s|v r |   | i |c S j                   j                  |j                  |j                        5  |j                  d<   t        |       }t        j                        5  j                  |      |<   d d d        d d d         |   | i |c S   | i |S # 1 sw Y   'xY w# 1 sw Y   +xY w)Nspecialization)	r  patch_source_specializationr   r  r  r   r   r~  call_user_compiler)
r   r   r  r  r   compiled_fnr   r   specialization_cachespecialization_guardss
        r   specialized_dispatchzCOutputGraph.compile_and_call_fx_graph.<locals>.specialized_dispatch  s	   4I Y0.#D>-1EE'K';N'K%)("-3(" !" "&!K!K . 5 5~7N7N" 	& =K(8 9?CDz%,T-A-A%B !&(,(?(?N(S %9$H!&	& $H#7#G#XQW#XX%Y& '777!& !&	& 	&s$   0CC C C	CC	c                  l     j                  t        j                  j                  j                  d      S )Nstore_user_object_weakrefs)r  r   r  graph_bytecode_inputsr   )r  s   r   r  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>	  s(    B//;;DD4 r   )qr   r  r   clear_framer  r  r  r  rc   r   r  r<   r<  r   r   r  r  
create_argr.  
dedup_passr   _maybe_preserve_original_metar,   do_not_emit_runtime_assertsdirrZ  r   r   r(   r  r  remove_unused_get_attr_nodesremove_unused_graphargsrd   &remove_tensorify_specialized_graphargsr  r   r   dce_extra_outputsr  r  r   r  rs  
parametersrL  r  inline_inbuilt_nn_modulesr  rX  _backend_idr  r  r   r  r  graph_code_logr  rm   r_  r`  r  r~  r  rz  r  r{  r|  r}  ru  r  r   torch.fx._lazy_graph_moduler  r   r  force_recompiler   add_backend_idspecializationsr  r   r  inspect	getsourcer  stripRootGuardManagerr   LAMBDA_GUARDr   r   r>  r?  r	   install_global_unsaferU  r>   rF   r  rG   r  r  r  r  r  r  pop_topr  rD  r1  make_call_generated_coder  )&r   rF  r  r  r  r  output_nodesub_gmsattrsubgraphncallsr  subgraph_nameregister_finalizerold_fake_moder  backend_fake_moder  lazy_gmr  asourcesr  source_indexcheck_fn_sourceunused_root_guard_managerr  r  tmp_varsconstructorvar_namer+  r  r  r  r   r  r  s&   `                                @@@@@r   r   z%OutputGraph.compile_and_call_fx_graph  s    ]]))557 K	)+####((*4::&!+B1K	) K	) _=Db$'''dN333**$$//5OB5O0OPR	K oo'G(==b+N55  I D&tT2H!(BNN;7$ NN #';;	 113/NN44NN;;	 ((* ,FW./69/779 !!'')#D$**5B@!"%
 66%)%L%L MMBwt]/KLM '+&A&A '""2&' BMMOT*6 ##|.3mm.B.B.\.\((%*4PT )  $  ||'!%)-)E)EB&55::< GG67 ,0+A+ABGG'($(BGGL!  &"T$PT NN++%D ,  ##% 00::M ,,,;;99]]E]R  ).(9(9(H(H"/"9"9 )I )%& 2C$$.**, Q"55b$:M:M:OPQ E+'787;
DACST((O; "+/?@  $--  !009!+/?@")//K||'++D+>!$HK Wo.!3. **666"/"9"9"I"III(*%SU$-1^^<188<<&5 N#*==1F1F#GL&-&7&78O8O&P&V&V&XO0@0B-%221&//() H IIT'
 *00%--;C !" !-	 +
!8 &&.R&S8 8s 8s 8 8 T8. **41EF **4=<<+++4<<(B!    #@#G#G#I .KO  HHX&OOH-. !) ?H$$R^^H%=>?   %B!CUK

%dnn5 WSLOJJ$$DDSIW ''-&&(WK	) K	)d *Q QL =[K	) K	)sX   AaC0a6J<a2+`3)a!a 'Da)a<I-a3`=	8a a
	aac                 :    | j                   j                  d      S )NplaceholderrR  )r   
find_nodesr   s    r   placeholderszOutputGraph.placeholders6	  s    zz$$$66r   c                 Z    | j                   D cg c]  }|j                  d    c}S c c}w Nr  )r  r  r   r  s     r   r  zOutputGraph.graphargs:	  s%    262C2CD$		*%DDDs   (r   r   c                 t    t        dddddd      5  | j                  ||      cd d d        S # 1 sw Y   y xY w)NOutputGraph.call_user_compilerbackend_compileTcompile_aot_autograd'aot_autograd_cumulative_compile_time_us)
phase_namer  log_waitcounterwaitcounter_name_overridedynamo_compile_column_us)re   _call_user_compiler)r   r   r   s      r   r  zOutputGraph.call_user_compiler>	  sI     ,("& &<%N
 	@ ++B?	@ 	@ 	@s   .7c                    | j                   J d}g }|j                  j                  D ]6  }|j                  dv r|dz  }|j                  dk(  s&|j	                  |       8 t        |       |D ]/  }t        |d      r|j                  d   }|j                  |_	        1 | j                  |_        | j                  |_        t        | j                   d      r| j                   j                  nd}	  t               t         j"                  d	|        | j                   }	t$        j&                  rt)        |	      }	 |	||      }
 t               t         j"                  d
|        t+        |
      sJ d       	 tM        ddi | jN                  |tQ        |j                  j                        tQ        |      d       
S # t,        t.        f$ r  t0        $ r}| j2                  rCt5        | j                   |t7        j8                               j;                  |j<                        d t?        || j@                  jB                  dd| dtE        |       d| j@                  jG                          d| dtE        |       ddg       Y d }~d }~wtH        $ r}|d }~wtJ        $ rH}t5        | j                   |t7        j8                               j;                  |j<                        d d }~ww xY w)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))rT  r   rO  rR  r   rk   r  r  r   r  r  _param_name_to_sourcer  _source_to_user_stacksr   r   r.   INFOr,   r   r   callabler   r   rB   r  rA   r  currentframewith_traceback__traceback__rE   rU  rW  r   format_frame_summaryrC   r   r   rc  r  )r   r   r   totr  r  plr  r  rT  r  es               r   r  zOutputGraph._call_user_compilerK	  s    +++HHNN 	*DwwIIqww-'##D)		*
 	3 	/B2/0ggj) %(JJ!	/ $(#<#< $($>$>! t''4 %%( 	
 	8LN7<<+EdV)LM**K((,[9%b.9KLN7<<+B4&)IJK(O*OO(6 	,..!"((..1"<0			
 M /0@A 	0 	55+$$a)=)=)? .1t< '##4#D6c!fX^DLLLmLmLoKpq0nSVHLcdC	 	  	 G 	8'  !W%9%9%;nQ__-48	8s3   2BG   K/B/JK/JK/'AK**K/c                 d    t         j                  j                  j                  rt	        |       S i S r   )r   r  r,   use_graph_deduplicationrH   r   s    r   r  zOutputGraph.dedup_pass	  s%    ==77,T22Ir   sub_gmc                 z    t        || j                  d      }||_        d|_        | j	                  ||d        |S )NT)requires_suffixFr  )ri   r   r   torchdynamo_force_dynamicr  )r   r  r  	next_names       r   r  zOutputGraph.install_subgraph	  s@    'dootT	#+0( 	$$VYt$Dr   c                 V    | j                   D cg c]  }|j                   }}|S c c}w r   )r  example)r   r  r   s      r   r   zOutputGraph.example_inputs	  s'    )-8##++88 9s   &c                     t        | j                  j                  d      d      D ]5  }t        t	        |j
                              dk(  s%| j                  |       7 y )Nro  r  T)reverser   )r8  r   r  r  r   usersr  r  s     r   r  z(OutputGraph.remove_unused_get_attr_nodes	  sN    4::00J0?N 	'D4

#$)  &	'r   c                 &     j                   sJ dt        j                  j                  dt        fd}dt        j                  j                  dt        fddt        j
                  dt        ffd}dd	lm} t        t         j                  j                              D ]  }t        t        |j                              dk(  s%|j                  d
k(  s||j                  dk(  r|j                  t         j"                  u sQ|j                  dk(  r1|j                  t$        j&                  u r ||j(                  d         s ||      s	 ||      s j+                  |        dt        j
                  dt,        t.        j0                     fd}dt        j
                  dd f fd}t3               dt2        t.        j0                     dt4        t$        j6                  t$        j8                  f   dd fdg } j:                  D ]  } ||      d u}|r|j                  r|j=                  |       /|j                  s&t?        |j@                  d   tB              s	 ||       a|j@                  d   }	t?        |	tB              rt?        |j@                  d   jD                  t$        jF                        r|j@                  d   jD                  }
|j@                  d   jH                  }t$        jJ                  jL                  jO                  |
      sotQ        |
jS                               }|D ]Q  }tU        |jV                  |      }tY        jZ                  t$        j6                  t$        j8                  ffd|       S ~t]        t_        |j@                  d   jD                              r|	j`                  |	j`                  n|	jD                  } |        |D ]+  } ||      }||vr	 ||       jc                  |       - y )Nb_noder   c                     | du ryt        | t        j                        sy| j                  j	                  d      }|y|du ryt        |t
        j                        r|j                  j                         x}|S y)NTFr  )	r<  r   Noder  r  r   SymBoolr  maybe_as_bool)r  brs      r   is_static_truez;OutputGraph.remove_unused_graphargs.<locals>.is_static_true	  sq    ~fbgg.0AyDy1emm,&&..00Q= r   r  c                     ddl m} t        | t        t        t
        f      ryt        | t        j                        r%t        | j                  j                  d      |      S y)Nr   SymTypesTr  F)
torch.fx.experimental.sym_noder  r<  r   r  r   r   r  r  r  )r  r  s     r   is_symnode_argz;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_arg	  sD    ?!c5$/0!RWW%!!&&**_"=xHHr   r  c                    ddl m} | j                  dk7  ryt        | j                  j                  d      |      syt        fd| j                  D              syt        fd| j                  j                         D              syy)	Nr   r  r  Fr  c              3   .   K   | ]  } |        y wr   r   r  r  r  s     r   r  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>	  s     <Q~a(<   c              3   .   K   | ]  } |        y wr   r   r"  s     r   r  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>	  s     GQ~a(Gr#  T)
r  r  rR  r<  r  r  r  r   r   r  )r  r  r  s     r   is_symnode_compute_nodezDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node	  sg    ?ww/)diimmO<hG <$))<<G$++2D2D2FGGr   r   )is_accessor_nodero  r  c                     | j                   d   }|j                  }t        |t        j                        rDt        |j
                  j                  t        j                        r|j
                  j                  S y r  )	r  r  r<  r   r  r  exprsympySymbol)r  r  r  s      r   placeholder_binds_symbolzEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol	  sV    ))J'CkkG'5<<0Z!!5<<6 ||(((r   c                     t         j                  d| j                  d   j                  j                         | j                  d= j                  |        j                  j                  | d        y )NzREMOVE UNUSED GRAPHARG %sr  )r   r  r  r   r  r  r  r$  )r  r   s    r   remove_unusedz:OutputGraph.remove_unused_graphargs.<locals>.remove_unused
  sY    II1499Z3H3O3O3T3TU 		*%T"!!%%dD1r   used_symbolsfakec                      | t        |      z  } y r   )r!   )r.  r/  s     r   update_used_symbolsz@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols
  s     L..Lr   r  c                      |       S r   r   )tr1  r.  s    r   r  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>4
  s    .A,PQ.R r   )2r  r   r  Argumentr   r  %torch.fx.experimental.symbolic_shapesr&  r   r   r   rO  r  r  rR  rt  operatorgetitemr   _checkr   r  r   r)  r*  r  r   r  r   r  r   r<  r  rs   r  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__rZ  wrapped_objpytreetree_map_onlyr   r  fake_tensorremove)r   r  r%  r&  r  r+  r-  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictr  fake_attr_valr/  symbolr  r1  r.  s   `                @@@r   r  z#OutputGraph.remove_unused_graphargs	  sg    	277#3#3 	 	&	bgg.. 	4 		"'' 	d 	" 	KT$**"2"234 	+D4

#$)GGz)?2t{{hFVFV7V?2 KK5<<7*499Q<8.t4'-$$T*	+	277 	x7M 		2 	2D 	2 +.%	/ell+	/38u||9S3T	/	/
  "%% &	<D3D9ELzz(//5zz*IIj)+@+ "$' ))J/C!#'<= !$))J"7"?"?ASAST*.))J*?*G*G*.))J*?*R*R$~~AASS+  )-_-L-L-N(OI(1 "07$3$?$?1" !' 4 4%*\\5<<$@$R$1!"	" !%d499Z+@+H+H&IJ +.??+FCKK  (d;M&	<R ) 	0D-d3F!-!$' !''/	0r   c                    ddl m} | j                  j                  D ]  }|j                  j                  d      }t        |t              s0|j                  =t        |j                  j                  j                  d      sht        d |j                  D              s|j                  |j                  j                  j                  j                        st!        |j                        D ]7  }|j#                  t%        |j                               | j'                  |       9 | j'                  |       ! y )Nr   )TensorifyStater  r  c              3   :   K   | ]  }|j                   d k(    yw)itemN)rt  )r  us     r   r  zEOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>`
  s     ?qF*?s   )torch._dynamo.symbolic_convertrL  r   rO  r  r  r<  r   	item_memor  r  _exprr  r  should_specializer  r   replace_all_uses_withr"   r  )r   rL  r  r  rO  s        r   r  z2OutputGraph.remove_tensorify_specialized_graphargsI
  s     	BJJ$$ 	'D IIMM/:M=*5!++7M3388>>G?DJJ??"44!++0066;;
 djj) (A++L9P9P,QR$$Q'(   &	'r   c                 H    | j                   j                  |       d| _        y)zt
        We call this on the creation of a new compiled subgraph that is inserted
        before user code.
        TN)r  r  r  )r   r  s     r   r  z#OutputGraph.add_output_instructionsk
  s     
 	  ''/r   c                     || j                   vsJ | j                   j                  |       | j                  j                  t	        j
                  | j                  ||             y)a`  
        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)rv  r   r  r   ra   r  r   )r   r  r   s      r   r  z!OutputGraph.install_global_unsafes
  sS     411111""4([//0A0A4OPr   c                     | dt        |       d| j                   }|| j                  v r|S | j                  ||       |S )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   ru  rv  r  r   r  r   r  s       r   install_global_by_idz OutputGraph.install_global_by_id
  sK     2e9+R'894)))K""4/r   c                 @    t        |      }| j                  ||       |S )z~
        Installs a global, generating a unique name for it.

        Returns the name of the newly installed global.
        )r<   r  rY  s       r   r  zOutputGraph.install_global
  s#      ""4/r   c                    d | _         | j                  j                          | j                  j                          d | _        | j
                  j                  D ]  }d|j                  v s|j                  d=   | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                   j                          | j"                  j                          y r  )rU  r   r   r  r  r   rO  r  r  r  rR  r  r  r  r~  rq  r  r  r  s     r   cleanupzOutputGraph.cleanup
  s    //557$(!JJ$$ 	*DTYY&IIj)	* 	##%  &&(!##))+##))+--335""$  &&(  &&(!!#r   r  c                 :    | j                   j                  |       y r   )r  r   )r   r  s     r   add_graph_finalizerzOutputGraph.add_graph_finalizer
  s     	##**+=>r   r  c                     |j                   dk(  r|j                  d   j                  S |j                   dk(  sJ | j                  |j                     S )z#Extract the non-fake example tensorr  r  ro  )rR  r  r  r   rt  r  s     r   example_value_from_input_nodez)OutputGraph.example_value_from_input_node
  sH    77m#99Z(000ww*$$$t{{++r   inlined_moduler   c                 Z    t         j                  j                        t         j                   j
                         j                  j                         dt        dd f fd}t        |d      rQt        |j                        r<|j                  j                  t        u r |j                         D ]  \  }} ||        t        |d      rTt        |j                        r>|j                  j                  t        u r!|j                         D ]  \  }} ||        y y y y )Nr  r   c                     j                   J t        |       } d|  }|j                   |<   t        t              r-| j                  t
        j                  |j                        <   y y rW  r  r  s      r   r  zHOutputGraph.add_fqn_info_for_inlined_modules.<locals>.register_leaf_name
  sw    ,,888*69=Jq,H2<D%%h/&+.  55//
@ /r   r  r  )rQ  rk  r  ri   r  r   r   r   r  r  r  __func__!og_module_named_parameters_fn_ptrr  og_module_named_buffers_fn_ptr)r   rb  r   r  r  r_  r  s   ` `   @r    add_fqn_info_for_inlined_modulesz,OutputGraph.add_fqn_info_for_inlined_modules
  s    **6;;7"$994;L;L
 	//33D9	# 	$ 	 >=1889"33<<45 %3$C$C$E 2LIq&y12>:.556"009912 %3$@$@$B 2LIq&y122 7 /r   Fr   )r  )r   ro  r   )r   rQ  )rF  r   r   N)r   r   )tmp)Fr   rN  )r   r   r   r   rN   r   r   r   r	   r   r1   r   r   r   r  r   r   r   r  r  r   r  r  r  rr   r.  r   Proxyr  r  r  r  r   r  r  r  r!  r  r  r
  Graphr   setterr  r  r   r  r)  r*  r   r  r  r  r  r  contextmanagerr'   r   Tracerr'  r)  r|  r}  r  r$   r  r  r  r   r   r   r  r  rH  rJ  rL  r   rc   rP  rT  r   r   r\  r  rd  staticmethodrk  rs  r  r;   r   r  r#  r  r   r6  r>   r  r  r  rL  rg  ro  ru  r  r   r   r  rt   r  r   r0   r  r  r  r  r   r  r  r  r  r  rZ  r  r]  r_  ra  rh  r   r   r   rQ  rQ     s:	      TP38nTP j)TP -	TP
 TP %%67TP TP TP TP TP $((I(I#JTP *+TP TP 
TPl
*D3 D
 4:5#5-05	sEHHNN"	#5)%((.. )*<:Xd3i=P ::38$,1#s(O	8&8BG#4 & &#
      & & )uxx~~ ) ) \\*588>> *d * * 7T#rxx-%8 7 7 4$rww'<"= 4 4 1tELL%8S2T$TU 1 1A# A A A@ @ @ @@ @ @ @ 
 &*	' ' c]	
 
299dD(	) :   .5,,;; . . 88 8 8
 A// A A >DcN > > FD)C)C$D F F KO$
DeHS#X,>,D&E!EFG$
	$
L$& N N'S '0 0JT J# %0D*E C C 5C 5D 5
    .,/	&Nehhoou||S89N N 	N
 
N`X/-X/	tK $vv~"66	7X/tY"-Y";>Y"	tO$&99	:Y"~ !&v&'v& #v& 	v&
 v& 
!	"v&pF ; F Ft F2"9'"9 ?+"9 	"9
 
"9H1<S   4Dd5c?6K1K,L C C 2 c  "!7FX)'X) !X) 	X)
 
k	X)t 7d277m 7 7 E4> E E@..@26v,@	@J..J26v,J	JXDehh&:&:!:; S %((2F2F 3 U\\ 2 '
X0t 'D d;.?  D  
Q# 
Qc 
Qd 
Q3 s s 	S 	 	 	$,?"*BNN+;T+A"B?	?
,%((-- ,C ,$2#hhoo$27=$2	$2r   rQ  c                       e Zd ZU eed<   eed<   ee   ed<   ee   ed<   eeedf      ed<   e	e
ef   ed<   	 dd	d
dee   ddfdZddZy)DynamoTracerOutputerror_on_graph_breakis_tracing_resume_prologueoutput_graphoutput_graph_for_cleanup.closure	f_globalsNr&  r   r  r   c                     |j                   | _         |j                  | _        |j                  | _        |j                  | _        |j                  | _        |rd | _        y |j                  | _        y r   )rs  rt  rw  rx  r)  rv  ru  )r   r&  r  s      r   r   zDynamoTracerOutput.__init__
  s[     %+$?$?!*0*K*K'~~))(.% $D &Dr   c                 2   | j                   }|r|j                  D ]  }|j                  j                           |j                  j
                  rG|j                  j
                  j                  r&d |j                  j
                  j                  _        y y y y r   )rv  rp  r   _clear_nodesr~  r  r  r_  )r   ru  r&  s      r   _cleanup_output_graphz(DynamoTracerOutput._cleanup_output_graph
  s    44&.. ,))+, ,,66 00::DDQU,,66@@N E 7 r   r   r   )r   r   r   r   r   r   rQ  r.  r	   r   r   r   r|  r   r   r   rr  rr  
  su     $$;'' '{33eCHo&&CH~ KO.1.:B3-.	.
Vr   rr  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.htmlru  kindrt  r   r   c                 \    |dk7  ry dt         j                  j                  dd f fd}dt         j                  j                  dt        dd f fd}t	        |t         j                  j                        r=t         j
                  j                  |j                  v r	 ||       y  ||d| d       y t	        |t         j                  j                        r't        |j                               }t        |      d	k(  rLt        ||d
         }t         j
                  j                  |j                  v r	 ||       y  ||d| d       y t         j                  j                  j                   j                   ||fd      \  }}	 t        j"                  j$                  |j&                  g|i |}	t        |	      }t         j
                  j                  |j                  v r	 ||       y  ||d| d|	 d       y y # t(        $ r"}
t+        ddt        |
      g        Y d }
~
sd }
~
ww xY w)Nr  rt  r   c                 Z    | j                   dv ry j                  j                  |        y )N>   atenprimprims)	namespacer  r   )rt  ru  s    r   encountered_compliant_opz8check_pt2_compliant_op.<locals>.encountered_compliant_op  s)    88))--f5r   r   c                     j                   j                  |        t        j                  rt	        dd|dz   t
        z   g        y y )Nz Encountered non-PT2-compliant oprO   r  )r  r   r,   only_allow_pt2_compliant_opsrD   err_epilogue)rt  r   ru  s     r   encountered_non_compliant_opz<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 packetrO  r  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r   _ops
OpOverloadr   r<  Tagpt2_compliant_tagtagsOpOverloadPacketr.  	overloadsr  rZ  r  r5  get_fake_values_from_nodesrL  r  _jit_resolve_packet_qualified_op_namer   rD   )ru  r}  rt  r   r   r  r  r  rR  overloadr  s   `          r   check_pt2_compliant_opr    s    6)>)> 64 6
UZZ-B-B  QU  &%**//099&&&++5$V,$3F8;VW	
 	&%**556&**,-	 y>Q1.Byy**bgg5(,(..4X 6./ }}**EE##dF^U
f
	xx33)),04:H VX&99&&"''1$R((=fX F33;* =%&K 70  	DF	 	s   -H   	H+	H&&H+PRc            
       f    e Zd Zdddeeef   dej                  dej                  ddf
dZde	fd	Z
y)
r  r&  ro  r   r   r   r   Nc                 <    || _         || _        || _        || _        y r   )r&  r   r   r   )r   r&  r   r   r   s        r   r   zLazyProxy.__init__c  s      	r   c                 N     | j                   | j                  i | j                  S r   )r   r   r   r   s    r   r   zLazyProxy.__call__p  s     tww		1T[[11r   )r   r   r   r   r  r  r   r   r   r	   r   r   r   r   r  r  b  sS      QTN vv	
 (( 
2# 2r   r  c                   
    e Zd ZdZ	 	 	 	 d,ddded    dedee   dee   d	df fd
Zde	fdZ
dddej                  d	dfdZ	 	 	 d-dededededee   dee   deeej                  gej                   f      d	ej                   f fdZ	 	 	 	 d.dededededee   dee   d	ej                  f fdZdej                  d	dfdZ	 	 d/dededededee   d	ej                   fdZd ej                   d	eeej                   f   fd!Zd"ed	efd#Zded$eeej                  j                   f   d	dfd%Zdeej8                  ej:                  f   d&ee   d	dfd'Zd(ej8                  d	ee jB                     fd)Z"d	e#fd*Z$d	e%fd+Z& xZ'S )0ro  a  
    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.
    Nru  rQ  r!  r[  r  r  r   c                    t         |           t        j                  |      | _        t
        j                  j                         | _        || _	        i | _
        i | _        || _        || _        || _        i | _        i | _        i | _        d | _        d| _        d| _        d| _        d| _        ||j.                  dz   nd| _        d | _        d | _        d | _        d | _        | j                  g | _        n:| j                  j8                  | j                  j;                  |      |fgz   | _        t=               | _        g | _         t        jB                         rtE        d      t=               | _#        y )NFr+   r   zSInference mode is supposed to be disabled during compilation. Please open an issue.)$r   r   weakrefrq  ru  r   r   rl  r   r[  r  r  r!  r  r  lifted_freevarsr  dynamic_scalar_nodes	prev_inst,unsafe_allow_externally_visible_side_effects+traced_with_externally_visible_side_effectsallow_side_effects_in_hopis_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_strr)   _used_names_input_versions_at_beginningis_inference_mode_enabledr   tracked_tensor_or_symint_vt)r   ru  r!  r[  r  r  r   s         r   r   zSubgraphTracer.__init__|  sx    	#MM,7XX^^%
 # 9; =? *& :< TV >@!
 =B9;@8 */& ,1(:@:L 2 2Q 6RS26GK 37!
 ;;.0D #';;#>#>**=9=IB $D 
 -7L 8:)**,e  IS(r   r   c                 :    | j                   j                  |       y r   )r  r   )r   r   s     r   record_tensor_or_symint_vtz)SubgraphTracer.record_tensor_or_symint_vt  s    ((,,R0r   rF  r   r  c                    | j                   r| j                  r| j                  r|j                  j                  }d }|)| j                  j                  || j                  z
  d       }|]| j                   |   }t        j                  j                  D ]  }||v s||   |j                  |<    d|v r|d   |j                  d<   y y y y y y )Nstack_trace)
r  r  r  current_instructionstarts_liner  r   rq  _COPY_META_FIELDSr  )r   rF  r  linenonode_idxr  r   s          r   r  z,SubgraphTracer._maybe_preserve_original_meta  s     (())++77FH!3377T666 #))(3XX77 7E}+/;		%(7 !D(/3M/BDIIm, ) $ * ) r   r}  rt  r   r   r  	type_exprproxy_factory_fnc           	      >   | j                   ^t        j                  ||f      \  }}	g }
|D ]$  }| j                  |      }|
j	                  |       & t        j
                  |
|	      \  }}t        |   ||||||      | j                  j                  }t        j                  dk\  r|dv r|j                  | j                  urj                  }j                  j                  g|j                   |j#                  j                  j                        dt$        ffd}t&        j)                  dt+        |             | _        d}|j                   | j,                  ur t/        j0                  |j                         j3                  dd	              }t5        |t6        j8                  j:                        rld
}|j<                  j>                  D cg c]  }|j@                   c}| _!        |jD                  | _#        |jH                  jJ                  jL                  | _'        nd | _!        d | _#        d | _'        |jP                  }|r'|jS                         jT                  j@                  d<   |dv rƉjT                  jV                  f}|rtY        jT                  j@                  d   j[                               d   d   }|j\                  j_                  d      r3|j\                  j_                  d      sjT                  jV                  |f}| j`                  |gz   jT                  j@                  d<   n|dk(  r| j                   tc        dd| jd                   dg        | j`                  jT                  jV                  tg        fdjT                  j@                  d   ji                         D              fgz   jT                  j@                  d<   | jk                  |jT                         |s!djT                  j@                  vr5|jP                  }|r'|jS                         jT                  j@                  d<   djT                  j@                  vr|dv r>| j`                  jT                  jV                  fgz   jT                  j@                  d<   nz|dk(  ru| j                   tc        dddg        | j`                  jT                  jV                  jT                  j@                  d      d   fgz   jT                  j@                  d<   djT                  j@                  vrg }|r?|jm                         s|j	                  |jo                                tq        |dd       }|r?|D cg c]  }|jr                  tu               vr| }}|jw                          tx        jz                  j}                  |      j                         }dj                  |      jT                  _A        t6        j                  j                  j                  s$t6        j                  j                  j                  rD| j                  j                  j                  | j                  j                  jT                         S c c}w c c}w )Nr  r  )r  r   c                  v    t              j                         } dj                  j                   d d|  S )NzTRACE FX call z from ri  )rf   rstripr  r  )linecur_instheaderr  tx_codes    r   get_trace_call_log_strz;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strE  s9    5gxHOOQD+BGGLL>xr$PPr   r  Forig_graphmodulec                       y r   r   r   r   r   r  z-SubgraphTracer.create_proxy.<locals>.<lambda>P  s    r   Tnn_module_stack>   r  r  r  r+   )ztorch.nn.modulesz	torch.ao.ztorch.nn.modules.containerr  r  z4Invoking an nn.Module inside a higher order operatorzHigher order op name: zThis is not supported.r  c              3   \   K   | ]#  \  }\  }}|j                  d       d   k(  r| % yw)@r   N)rY  )r  r   r_  tyrt  s       r   r  z.SubgraphTracer.create_proxy.<locals>.<genexpr>  s5      &Aw2773<?f4 s   ),z2Invoking an nn.Module inside a HigherOrderOperatorrO  r  r!  )Hr!  r@  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r  ru  rL  r  r  r  r  	positionsr  rW  get_line_of_code_headerr   trace_call_logr  rn   r  r=   get_contextr  r<  r   r   r   r   rO  r  r  _lineno_mapr  r   __code__r^  r  r  r   r  r  r   r  r   
startswithr  rD   r  rs  r   r  is_co_filename_from_nn_modulesframe_summaryrZ  filenamer&   r  r   StackSummary	from_listr  rh  r  r  r,   r  track_nodes_for_deduplicationrw  
track_node)r   r}  rt  r   r   r  r  r  	flat_args	tree_specnew_flat_argsr  maybe_new_argrF  r  is_retracingorig_graphmodule_maybendr  r  current_nn_moduleframe_summariesframefiltered_frame_summariesmsgsr  r  r  r  r   s     `                      @@@@r   r  zSubgraphTracer.create_proxy  s   V ;;"#)#6#6f~#F IyM  4 $ H H M$$]34 "00	JLD&W!
 )) w&4 4
 ,

 --H.&&2&&--9))338;M;M;T;T3UQ Q Q $$T:6L+MN!) 99DNN*&\%=%=bii%H%L%L"L& &" 0%((2F2FG#&<&B&B&H&H& "BGG&" ,B+M+M(*22;;JJ ) &*"+/(,0),,.=.B.B.DBGGLL*+33WW\\6*E %)6G)H)O)O)Q$RSU$V%! %//::5+66AA0  WW\\+<=E.2.B.BeW.LBGGLL*+]"{{&R4T5G5G4HI 8	 /3.B.BGGLL *,'',,7H*I*O*O*Q 	F 	/BGGLL*+ 	**2rww7 4"$"4"4"6E6J6J6LBGGLL!23 4;;6:6J6Jv.N 7BGGLL!23 ]*{{.%$X$&(@"$	 7;6J6JGGLLGGLL):;FCAFN 7BGGLL!23 ,<>O 88:#**2+;+;+=>R40  -(>>)<)>> ($ ( %,,. ))334LMTTVD"$''$-BGG MM  88}}##AA,,77!!,,bgg 	_&v(s   #Z	ZrR  c                    t        | j                  ||||       | j                  at        j                  |i |}|D ]G  }t        |t        j                  j                        s(|j                  | j                  k(  rBJ d        t        
| -  ||||||      }	| j                  j                  |	j                  d<   | j                  j                  |	j                          |	S )Nz2create_node using arg not from this SubgraphTracerrI  )r  ru  r!  r@  arg_tree_leavesr<  r   r   r  r   r   r  r  r  r  r   r  )r   rR  rt  r   r   r  r  r  r  r  r   s             r   r  zSubgraphTracer.create_node  s     	t00"fdFK;;"..??I  !#uxx}}5yyDJJ. H. w"2vtVT9M*.*;*;*E*E		&'TYY'r   c                    t        |j                        dkD  rg }|j                  D ]S  }|j                  | j                  k7  s|j                  t	        t        |j                  j                                     U |D ]  }|j                  j                  |        | j                  j                  |       | j                  j                  |j                  d        y r  )r  r  r   r  r   r   rO  rM  r  r$  r  )r   r  user_graph_nodesuserother_graph_nodes        r   r  zSubgraphTracer.remove_node  s    tzz?Q46

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

d#  $$TYY5r   r  beforer   c                 (   t        |t        j                        r%| j                  j	                  |j
                         t        j                  d|||j                  nd|| j                  |       || j                  J d| d| d       | j                  rd| j                  X|J t        |d      sG| j                  j                  j                  |g       j	                  t!        j"                                t%        || j&                        }| j(                  rqt+        t-        | j(                              }| j(                  |   j.                  }|r| j0                  j3                  |      }n7| j0                  j5                  |      }n| j0                  j3                  d       }|5  | j7                  d|d	i |
      }	t9        |	j.                  |       | j(                  r>|r<| j(                  j;                         \  }
}|	| j(                  |<   || j(                  |
<   n|	| j(                  |<   | j&                  j=                  |       | j                  }t        j>                  jA                         }|s|st        |t        j                        r| jC                  ||       nht        |tD        tF        f      rRtI        |      D ]D  \  }}t        |t        j                        s!d }|rtK        ||d      }| jC                  ||       F t        |t        jL                        rQt        |j.                  jN                  tP        jR                        r#|	| jT                  |j.                  jN                  <   |	cd d d        S # 1 sw Y   y xY w)Nz7create_graph_input %s %s %s at debug_level %s before=%sz(none)z0you are required to provide a source for inputs z example_val z on the root tracerT)only_allow_inputr  r   r  F)r  r  index_is_slice)+r<  r   r   r  r   _versionr   r  r  r  r!  r[  rW   ru  r  
setdefaultr   extract_stackri   r  r  rs  r   r  r   inserting_beforeinserting_afterr  rq   popitemr   compileris_compiling_lift_basic_symbolsr   r.  r  rT   r  r(  r)  r*  r  )r   r  r  r  r  r   	prev_namer  ctxrq  r   r   is_strict_exportis_non_strict_exportr  r  e_sources                    r   r  z!SubgraphTracer.create_graph_input  s    mU\\2--44]5K5KL		E!-FKK8	
 >;;* B4&VcUddwx* >>dkk1%%%'F!!77BB62NUU"002 #4)9)9:##Xd&>&>?@I++I6;;Djj11$7jj006**--d3C D	%%mT2rY%WEejj-8''F//779116((../((+16((.
   &@  $~~#(>>#>#>#@ #,@mU\\:,,]FCe}= )- 8 
>1)!U\\:$#'!'4%+1U(H 00H=
> -6:""''< ?D""=#5#5#:#:;ID	 D	 D	s   .GNNrq  c                 n   | j                   J d       |j                  j                  d   }t        |t        j
                        rE|j                  j                  | j                  v r#| j                  |j                  j                     S || j                  v r| j                  |   S |j                  | j                   k7  r| j                   j                  |       |j                  j                  d   }| j                  |j                  j                  t        |      |      }|| j                  |<   |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr  )r!  r  r  r<  r   r  r(  r  r  r&  lift_tracked_freevar_to_inputr  r  r  )r   rq  r  	new_proxys       r   r   z,SubgraphTracer.lift_tracked_freevar_to_inputp  s   
 {{& 	
W	
& 

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

8++JJOOT-0-
	 '0U#r   r  c                     t        |t        j                  j                        sDt        |t              r2t	         fd|j
                  |j                  |j                  fD         S |S |j                   k(  r|S  j                  |      S )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   @   K   | ]  }j                  |        y wr   )r  )r  sub_argr   s     r   r  zESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>  s$      # @@Is   )
r<  r   r   rk  slicestartstopstepr&  r   )r   r  s   ` r   r  z2SubgraphTracer.maybe_lift_tracked_freevar_to_input  sy     #uxx~~.
 #u%(+		388SXX'F  
ZZ4J11#66r   e_proxyc                     j                   t        t              sJ dt        dt        f fd}dt        dt        dt        dt
        j                  ffd}t        |t        j                        rt        |j                               D ]  \  }} ||      st        j                  d||j                         t        ||d	t        j                  j                   j"                  j$                  |fi t'        |      
      } j)                  ||        |j+                         } ||      rst        j                  d|j                         t        ||d	t        j                  j                   j,                  fi t'        |      
      } j)                  ||       |j.                  t        j0                  u rt        |j3                               D ]  \  }} ||      st        j                  d||j                         t        ||d	t        j                  j                   j4                  j$                  |fi t'        |      
      } j)                  ||        n6|j.                  t        j6                  u rA j)                  |j9                                 j)                  |j;                                n|j.                  t        j<                  t        j>                  hv rA j)                  |jA                                 j)                  |jC                                nl|j.                  t        jD                  t        jF                  hv r@ j)                  |jI                                 j)                  |jK                                tM        |      rC|jO                         \  }	}
|	D ]*  }tQ        ||      } j)                  |tQ        |             , y y t        |t        jR                        r/ ||      r&|jT                  jV                  }jX                  |<   y y y )Nrf  r   c                     ddl m}  ||       xrR t        | j                  j                  t
        j                        xr" | j                  j                  j                  vS )Nr   )r#   )r5  r#   r<  r  r(  r)  r*  r  )rf  r#   r   s     r   	need_bindz8SubgraphTracer.track_produced_symints.<locals>.need_bind  sJ    I A :qvv{{ELL9:FFKKt'9'99r   r  r   r   c                 L   t        t              r        nt        t        j                  j                        sJ j
                  j                  j                        5   j                  |i |}t        |j                  |        |cd d d        S # 1 sw Y   y xY wr   )
r<  r  r   r   rk  r   r  r  r  rq   )r  r   r   rq  r	  r&  s       r   _proxy_with_example_valuezHSubgraphTracer.track_produced_symints.<locals>._proxy_with_example_value  s    
 $.gy#AgiwGguxx~~666--gll; +++T<V<!%**m<  s   &*BB#z=track_produced_symints %s for %s.size()[%s] at debug_level %sr  r  zCtrack_produced_symints %s for %s.storage_offset() at debug_level %sz?track_produced_symints %s for %s.stride()[%s] at debug_level %s)-r&  r<  ro  r	   r   r   rk  r   r   r  rc  r   r  r  r  opsr  sym_sizer   r  track_produced_symintsstorage_offsetsym_storage_offsetlayoutstridedstride
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr*   __tensor_flatten__rZ  r  r  r(  r  )r   r  r	  r  r  r  rf  
lazy_proxyr  attrsr  r  inner_tr(  r&  s   ` `           @r   r  z%SubgraphTracer.track_produced_symints  s     &.111	 	 	
	
	'*
	69
	XX
	 mU\\2!-"4"4"67 ?1Q<IIW** "+1'		//33 !"&q'	"J //:>'?* +99;N(		Y"&&	 '-"#IINN55J">2	
 ++NJG##u}}4%m&:&:&<= CDAq |		]#".. &/"5+!IINN5599$aL&*1g	&
 33AzB'C* %%)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,];*==?
s! QD%mT:G//$9OPQ <
 u||4'$))..-4$$T* ( 5r   srcc           	          	 ddt         t        t        j                  f   dt        t
           dt        dd f fd}t        |t        j                        rnt        |j                               D ]-  \  }} |||t        |t        j                  |      nd d       / |j                  t        j                  u rt        |j!                               D ]-  \  }} |||t        |t        j"                  |      nd d       /  ||j%                         |t        |t        j&                        nd d       n6|j                  t        j(                  u rA j+                  |j-                         |        j+                  |j/                         |       n|j                  t        j0                  t        j2                  hv rA j+                  |j5                         |        j+                  |j7                         |       nl|j                  t        j8                  t        j:                  hv r@ j+                  |j=                         |        j+                  |j?                         |       tA        |      rG|jC                         \  }}|D ].  }tE        ||      }	 j+                  |	|tG        ||      nd        0 y y t        |t        j                        r
 |||       y y )	Nrf  r   r  r   c                    t        |       sy t        | t        j                        sJ j	                  |       }t        |      dk(  ry j                  ىj                  j                  | |       |D ]  }j                  j                  |   }|j                  j                  d   }t        |t        j                        sJ j                  t        |      t        |      |||      }t        j                  d|||j                   ndj"                         |j$                  |<    y t        |      dk(  sJ d| d|         |J d	|  d
|  d       t'        t)        |            }j                  t        |      t        |       | ||      }t        j                  d| ||j                   ndj"                         t+        || dd d      |j                  j                  d<   y )Nr   r  )r  r   z4_lift_symbols_in_symint %s from %s at debug_level %szsubgraph inputsr+   zyFor root tracer, we only expect to bind basic symbols (compound symbols should be cached before) but got unbound symbols z in zSource of 'z' is None when lifting it to input of top-level. If it's an unbacked symbol, this could be because it's not tracked with lazy_bind_unbacked_symbols. Otherwise, should provide a source when create_graph_input for `z` at root tracer.F)pass_arg_as_tensorrB  r  r  )r#   r<  r   r  lookup_unbound_symbolsr  r!  r  r  r  r  r  r   r  r   r  r  r  r  rs  iterrt   )	rf  r   r  self_to_be_bounds0parent_proxyexample_valphr   s	           r   _lift_symbols_in_symintzCSubgraphTracer._lift_basic_symbols.<locals>._lift_symbols_in_symint=  s   
 q>a...#::1=#$) {{&//6:* <B#';;#<#<R#@L"."3"3"8"8"IK%k5<<@@@00B[)#%% 1 B IIN'-'9?P((	 :<D((6#<( +,1 HHXGYY]^_]`b1 ) !! %WWXVYYjl)
 $/01,,GG!! -  		J#)#5FKK;L$$	 ,4', $#,Z(r   T)r  ri  )$r   r   r   r  r   r   r   r<  r   r  rc  r^   r]   SIZEr  r  r  STRIDEr  STORAGE_OFFSETr  r  r  r  r  r  r  r  r  r   r!  r"  r*   r#  rZ  rQ   )
r   r  r'  r2  r  rf  r%  r  r  r&  s
   `         r   r  z"SubgraphTracer._lift_basic_symbols6  s    !B	S%,,&'B	V$B	 B	 	B	H mU\\2!-"4"4"67 	1' ? -S.2E2EqI!	 ##u}}4%m&:&:&<= 	DAq+  # 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,];*==?
s! D%mT:G,,#/C!6t < u||4# 5r   rf  c                    |j                   j                  j                  }t        |      dk(  rg S g }|D ]  }|| j                  vr|j                  |       #| j                  |   }t        |t              r |       }|| j                  |<   t        |t        j                  j                        r|j                  | u rJ d| d        t        |d       S )Nr   zThe proxy of symbol z" doesn't belong to current tracer.c                     | j                   S r   )r  )rf  s    r   r  z7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>  s
     r   )r   )r  r(  r!   r  r  r   r<  r  r   r   rk  r&  r8  )r   rf  r!   to_be_boundr.  rq  s         r   r+  z%SubgraphTracer.lookup_unbound_symbols  s    vv{{//|!I 	B+++""2&&&r*E%+).""2&eUXX^^49M &rd*LMM	 k'788r   c                    | j                   }g }g }| j                  j                  D ]i  }|j                  dk(  rW|j                  d   }t        |t        j                        s<|j                  |j                         |j                  |       i n t        t        ||            D cg c]  \  }\  }}||k7  r| }	}}}|	r#|	D cg c]  }||   	 }
}d|
 }t        d|      S t        dd      S c c}}}w c c}w )Nr  r  zInput mutation detected at TFrO  )r  r   rO  rR  r  r<  r   r   r   r  r  zipr   )r   input_versions_at_beginninginput_nodesinput_versions_at_endr  r  r  v1v2mutated_inputsmutated_nodesr   s               r   has_input_mutationz!SubgraphTracer.has_input_mutation  s   &*&G&G# "JJ$$ 	Dww-' $		/ :mU\\:)001G1GH&&t,	  )/1FG 
 
8B Rx	 
 
 5CD[^DMD/?Cc**E2&&
 Es   ,C5
C<c           	         ddl m} ddlm} t	               }| j
                  j                  D ]k  }|j                  dk(  rY ||g      d   }t        |t        j                        s9 ||      D ]&  }||v rd||    d| }t        d|      c c S |||<   ( k n t	               }| j
                  j                  d	      d   }	t        j                  |	j                  d         D ]o  }
|
s ||
g      d   }t        |t               rJ t        |t        j                        s? ||      D ]&  }||v rd
||    d|
 }t        d|      c c S |
||<   ( q |j#                         |j#                         z  }t%        |      dkD  rQ|D cg c]  }||   ||   f }}dj'                  |D cg c]  \  }}| d|  c}}      }d| }t        d|      S t        dd      S c c}w c c}}w )Nr   )get_tensor_storages)_collect_fake_inputsr  z*Input-to-input aliasing detected at nodes z and Tr)  r  z,Output-to-output aliasing detected at nodes z, z+Input-to-output aliasing detected at nodes FrO  )(torch._dynamo.variables.higher_order_opsrD  torch._higher_order_ops.utilsrE  r   r   rO  rR  r<  r   r   r   r  r@  tree_leavesr   r   rU  r  rh  )r   rD  rE  input_storagesr  r  storager   output_storages	out_nodesout_nodeintersected_storagesrf  aliasedr  os                   r   r   zSubgraphTracer.has_aliasing  s'   PF>BfJJ$$ 	Dww-' 4dV <Q ?mU\\:#6}#E 7"n4$N~^eOfNgglmqlr"sC#/c#::26w/7 	 @DvJJ))X)6q9	**9>>!+<= 
	<H 4hZ @ C%mT:::mU\\:#6}#E <"o5$PQ`ahQiPjjopxoy"zC#/c#::3;0<
	<  .2247K7K7MM#$q( BV<="OA$67G  iiG DDAqA3eA3 DEG?yICc**E2&& !Es   G$.G)
)NFNN)NNNrM  )FN)(r   r   r   r   r   r   r'   r   r   rr   r  r   r  r  r	   r   rk  r  r  r  r   r  r   r  r   r  r   r  r  r   r  r   r)  r*  r+  r   rB  r   r   r   r   s   @r   ro  ro  t  s    .2*.%)]U#]U )*]U 	]U
  ']U c]]U 
]U~1_ 1C-C57WWC	C8 ##'DHNN N 	N
 N smN C=N #8RWWIrxx,?#@AN 
Nh "#'  	
  sm C= 
46 6D 66 #'xx x 	x
 x  x 
xv(XX(	y"(("	#(T7s 7s 7@y5 y5+0EHHNN1J+Ky5	y5x~"5<<#=>~EMfEU~	~D9 9ell9K 9*'L ':+'l +'r   ro  )r   r  r  r   r>  r  r  r.   r6  ri  r  r   r  r  collections.abcr   r   r   dataclassesr   r   r-  typesr   typingr	   r
   r   r   r   typing_extensionsr   r   r)  torch._guardsr   torch._loggingtorch.distributeddistributedr  torch.nntorch.utils._pytreer5  r6  r@  r   r   torch._C._dynamor   torch._dynamo.excr   r   r   r   r   r   r   r   torch._library.opaque_objectr   torch._subclasses.fake_tensorr   torch._utils_internalr   torch.export.dynamic_shapesr   r  r   %torch.fx.experimental._backward_stater    r5  r!   r"   r#   r$   r%   r&   torch.fx.noder'   torch.fx.passes.runtime_assertr(   torch.utils._ordered_setr)   torch.utils._python_dispatchr*   rO  r,   r-   r   r/   backends.registryr0   r1   bytecode_transformationr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r
  r>   current_scope_idr?   device_interfacer@   rA   rB   rC   rD   rE   r  rF   rG   graph_deduplicationrH   graph_region_trackerrI   rJ   rK   mutation_guardrL   rR  rM   rN   rO   r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   variables.baserr   variables.builderrs   rt   ru   rv   variables.ctx_managerrw   variables.listsrx   variables.miscry   variables.nn_modulerz   variables.tensorr{   r|   r}   variables.torch_functionr~   variables.user_definedr   torch._dynamo.packager   rP  r    torch.multiprocessing.reductionsr   	getLoggerr   r   r_  getArtifactLoggergraph_tabular_logr  graph_sizes_logr  r  r   r   r  rg  r  rf  r   r   r   r   r   r   r   r   r   r   r   r   r   objectr  r   r#  r0  r?  rA  rQ  rr  r  r  r  rt  r  r  r  ro  ro  r   r   r   <module>r~     s	  *         	 
    9 9 4  < < 0       $ $  # N  8 4 0 9 : ?  ! J / F D D 5    '  - 6  S : 4 / 0 W W    "     * ,  ; - ( 1 
 C ; 4H?g!NN44XwG 11(LI..228]K11(LI**  "'!>!> $)HHOO$D$D ! $   $  
 $  
 0 4c 4 4 - - -# (2tCy=2I  UXX__   ! !H 	S&[ <
 <
 <
~ X X X" ' ' '(E d38n (F". F"R_#2# _#2DG!V !VJJ JJ%(J25J=@JJMJ	JZ &ioo' cNCL2 2$a'RYY a'r   