
    ^j[#                    
   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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mZm Z m!Z! ddl"m#Z#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 m0Z1 ddl(m2Z2m3Z3 dd	l4m5Z5 dd
l6m7Z7m8Z8 ddl'm9Z9m:Z:m;Z;m<Z<m=Z=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF ddlGmHZH ddlImJZJ ddlKmLZL ddlMmNZNmOZOmPZPmQZQmRZRmSZS ddlTmUZU ddlVmWZW ddlXmYZY ddlZm[Z[ ddl\m]Z]m^Z^mZ_m`Z` ddlambZbmcZc ddldmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZo ddlpmpZp ddlqmrZr ddlsmtZt ddlumvZv dd l^mwZwmxZxmyZymzZzm{Z{ dd!l|m}Z}m~Z~ dd"lmZ dd#lmZmZ dd$lmZ dd%l5mZmZ dd&lmZ dd'lmZmZmZ dd(lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd)l/mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd*lmZmZmZmZ dd+lmZ dd,lmZmZ dd-lmZ dd.lmZ dd/lmZ dd0lmZmZmZ dd1lmZ dd2lmZ erdd3lmZ dd4lmZ dd5lmZ dd6lmZ dd7lmZ  ej                  e׫      Ze(j                  j                  ed8      Ze(j                  j                  ed9      Ze(j                  j                  ed:      Ze(j                  j                  ed;      Ze5j                  Zee(j                  z  dz  Zee   Zed<   Z G d= d>e       Z G d? d@e       Z G dA dBe      Ze(j                  j                  j                  Ze(j                  j                  j                  ZdCedDeeef   dEedFef   fdGZ edHI       G dJ dK             Z edHI       G dL dM             Z	 dwdNeee(jf                  edz  f      dOee(j                  j                  j                     dz  dEee(j                  j                  j                     fdPZej                  dEefdQ       Ze G dR dS             ZdTedEeg ee   f   fdUZ G dV dWe(j                  j                        Z G dX dY      Zeee f   Ze G dZ d[             Ze G d\ d]             Ze G d^ d_             Ze G d` da             ZdbedEeeef   fdcZ G dd dee      Z G df dge      Z G dh di      Z	djZ
dkedledmednedoedEdfdpZ ej                         Z e$dq      Z e%dr      Z G ds dt      Z G du dve2j"                        Zy)xa  
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Literal
NamedTupleOptionalTYPE_CHECKING	TypedDictUnion)NotRequired	ParamSpecTypeVar)fxTensor)guards)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourcetracingTracingContext)FakeScriptObject)is_opaque_type)
FakeTensor)signpost_event)_ConstraintTarget)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnvSpecializationuninteresting_files)Target)insert_deferred_runtime_asserts)
OrderedSet)is_traceable_wrapper_subclass   )configexclogging	variables)
CompiledFn
CompilerFn)create_binary_slicecreate_binary_subscrcreate_build_tuplecreate_call_functioncreate_dup_topcreate_instructioncreate_load_constcreate_rot_ncreate_swapInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)get_interface_for_device)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplementedunimplemented_with_warning)has_user_objectsindex_to_bytecode_constructor)apply_graph_deduplication)#get_backend_override_for_compile_id+get_inductor_config_override_for_compile_id)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffectsValueMutationExisting)_get_source_debug_name
AttrSourceBackwardStateSourceConstantSourceDictGetItemSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceNumpyTensorSourceParamBufferSourceShapeEnvSourceSyntheticLocalSourceTensorPropertyTensorPropertySource)_extract_tensor_dictcheckpoint_paramsCleanupHookclone_inputscompilation_time_metricscount_callscountersdynamo_timedget_chromium_event_loggerget_instruction_source_311get_locals_to_stealget_static_address_typeget_unique_name_wrtgraph_break_reasonsincrement_op_countistypelazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)BackwardStateGraphArgGraphArgTrackedFakewrap_fx_proxy)ContextWrappingVariable)ClosureConversionErrorVariableTracker)BaseListVariable)NullVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableUnspecializedPythonVariable)TensorWithTFOverrideVariable)UserDefinedDictVariableDynamoProfilerState)CompilePackage)InstructionTranslatorBase)_CudagraphAnnotation)StorageWeakRefgraph
graph_codegraph_sizes
trace_call)	graph_outinputconstantc                   B    e Zd ZU eed<   eed<   ee   ed<   ee   ed<   y)SizesStridesInfosizestridevalues_sizevalues_strideN)__name__
__module____qualname__TensorMetadataSequence__annotations__r        e/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/output_graph.pyr   r      s&    
  ""344566r   r   c                       e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   eed<   eedf   ed	<   eedf   ed
<   eedf   ed<   eed<   eed<   e	e   ed<   eed<   eed<   e	e   ed<   eedf   ed<   eedf   ed<   y)CodeOptionsco_argcountco_posonlyargcountco_kwonlyargcount
co_nlocalsco_stacksizeco_flagsco_code.	co_constsco_namesco_varnamesco_filenameco_nameco_qualnameco_firstlinenoco_linetableco_exceptiontableco_freevarsco_cellvarsN)
r   r   r   intr   bytestupleobjectstrr   r   r   r   r   r      s    OMNVS[!!CHosCx LS!!"5))sCx sCx r   r   c                   "    e Zd ZU eed<   eed<   y)OutputReturnInfokindvalueN)r   r   r   OutputReturnKindr   r   r   r   r   r   r      s    
Mr   r   compiler_fnconfig_patchesreturn.c                 n     dt         dt         dt         f fd}t         dd      |_         |_        |S )z
    Wrap a compiler function to apply inductor config patches during compilation.

    Passes config_patches as a keyword argument so that compile_fx can
    propagate them to backward compilation via its inner_compile wrapping.
    gmexample_inputsr   c                      | |      S )N)r   r   )r   r   r   r   s     r   wrappedz+_wrap_with_inductor_config.<locals>.wrapped  s    2~nMMr   r   z	<wrapped>)r	   getattrr   __wrapped__)r   r   r   s   `` r   _wrap_with_inductor_configr      sA    NC N N N {JDG%GNr   T)frozenc                   "    e Zd ZU eed<   eed<   y)AliasingInfohas_aliasingmsgN)r   r   r   boolr   r   r   r   r   r   r     s    	Hr   r   c                   :    e Zd ZU eed<   eed<   dZeedf   ed<   y)MutationInfohas_mutationr   r   .mutated_input_indicesN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s     	H-/5c?/r   r   tensors_with_sourcesstop_atc                 p   |
t               }t               }g }| D ]@  \  }}t        |t        j                        s!|j                  }|0|j                  |       B |rV|j                         }||v r||v r|j                  |       |j                  D ]  \  }}|	|j                  |        |rV|S )a  Collect all grad_fns reachable from tensors' autograd graphs.

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

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

    Returns:
        Set of all reachable grad_fns.
    )	set
isinstancetorchr   grad_fnappendpopaddnext_functions)	r   r   visitedstacktensor_r   nodenext_fns	            r   collect_reachable_grad_fnsr     s    " %.1eG-/E) &	fell+nnG"W%	& yy{7? 7?D-- 	&JGQ"W%	&  Nr   c                  4    t        j                  t              S N)torchdynamo_loggingget_step_loggerlogr   r   r   _step_loggerr   D  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   rt   r   selfs    r   __post_init__z GraphCompileReason.__post_init__S  s    &&t, r   r   N)r   r   r   __doc__r   r   list	tracebackFrameSummaryr   r   r   r   r   r   r   r   I  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_valuesY  s+    <HII(8D&D#F#IIIs    )r   r	   )r   r  s   ` r   _get_gen_rand_values_fnr  X  s    Jd3i J r   c                        e Zd ZdZdeeej                  j                  f   dd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_modulesr   Nc                 l    t         |           |j                         D ]  \  }}t        | ||        y r   )super__init__itemssetattr)r   r	  kv	__class__s       r   r  zFakeRootModule.__init__b  s6    $$& 	 DAqD!Q	 r   c                      y)NzFakeRootModule(...)r   r   s    r   __repr__zFakeRootModule.__repr__g  s    $r   c                 N    |j                         D ]  \  }}t        | ||        y r   )r  r  )r   r	  r  r  s       r   add_nn_moduleszFakeRootModule.add_nn_modulesj  s*    $$& 	 DAqD!Q	 r   )r   r   r   r   dictr   r   nnModuler  r  r  __classcell__r  s   @r   r  r  _  s\    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__p  s	    #*r   r   r   c                    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)rh   restorer   copydeepcopyr  	candidateforwardr1   verify_correctnessrj   rz   RuntimeError	Exceptionr   	exception)r   r   r   copy_gmcorrectresults         r   __call__zWrapperBackend.__call__s  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   r6   r  r   r   GraphModuler   r   r5   r,  r   r   r   r  r  o  sE    +
 +t +((&&8<U\\8J	r   r  c                   R    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   d	dZ
y)
BytecodeTracingTimingsaX  Accumulated wall-clock time (ns) for major components during Dynamo
    bytecode tracing that are not related to variable trackers.  Each field
    is an int accumulator that gets bumped via ``time.time_ns()`` in the
    corresponding hot-path wrapper.  To add a new timer, add a field here
    and wire up the wrapper in the relevant function.r   get_fake_value_nscreate_proxy_ns!wrap_to_fake_tensor_and_record_nsvariable_builder_call_nsNc                 |   t               }i }t        j                  |       D ]  }t        | |j                        }|dkD  s|j                  j                  d      }|dz  || d<   t        j                  |g       j                  |dz         t        | |j                  d        |r |j                  di | yy)zFlush accumulated timings to the bytecode_tracing chromium event
        and to compilation_time_metrics, then reset all counters.r   _nsg    eA_time_sN)bytecode_tracing)ro   dataclassesfieldsr   nameremovesuffixrk   
setdefaultr   r  try_add_event_data)r   chromium_log
event_datafns_valkeys         r   report_and_resetz'BytecodeTracingTimings.report_and_reset  s     12
##D) 	)AT166*Fzff))%0.4sl
cU'?+(33C<CCFSLQaffa(	) +L++M*M r   r   )r   r   r   r   r0  r   r   r1  r2  r3  rC  r   r   r   r/  r/    s:    9 sOS-.%s.$%c%N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f   ed<   eed<   eej                  j                   j"                     ed<   ej$                  d	z  ed
<   ej&                  j(                  j*                  j,                  ed<   ej.                  j0                  ed<   eej.                  j2                     ed<   dZeed<   dZeed<   dZeed<   d	Zed	z  ed<   e de!fd       Z"e dej.                  j0                  fd       Ze deej.                  j2                     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_layersNcurrent_deviceglobal_state_guard_guards_aotautograd_guardsFexportskip_guards_checkexport_constraints%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   )rO  r   s    r   r   zOutputGraphGuardsState.guards  s    ||r   c                     | j                   S r   )rP  r   s    r   aotautograd_guardsz)OutputGraphGuardsState.aotautograd_guards  s    '''r   c                 b   t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S )N)rF  rG  rH  rI  rJ  rK  rL  rM  rN  rT  rQ  rS  rO  rP  rR  )rE  rF  rG  rH  rI  rJ  rK  rL  rM  rN  rT  rQ  rS  r   r[  rR  r   s    r   dump_guards_statez(OutputGraphGuardsState.dump_guards_state  s    %((**&*&D&D#66*.*L*L!22..#66262\2\;;#66KK $ 7 7"44
 	
r   )r   rE  )%r   r   r   r   Scoper   r   r   	overridesTorchFunctionModer   r   r  r   r   
_functorchpyfunctorchFuncTorchInterpreterdevice_C_dynamor   GlobalStateGuardrO  	GuardsSetGuardEnvExprrQ  r   rR  rS  rT  r   propertyr)   rX  r[  r]  r   r   r   rE  rE    sX    #EOO$E$EFFF##'0@(@#AAO5++77LLMMLL4''((//@@@]]$$$emm8899 FD#t#$$8<)3:<S8 S S //   (D)C)C$D ( (
r   rE  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   rm  r   r   dc_fieldr  rp  r   r   rq  rr  rs  r   r   rt  ru  r   r   r   rl  rl    s     Is#+$L$sCx.  #+4"@d3i@"*4"@d3i@;C<NDsE&#+$6678  '/t&D$s)D<D=OT%U63;%7 789 r   rl  c                      e Zd ZU  ee      Zeeedz  f   ed<    ee      Z	eee
f   ed<   ej                  j                  j                  Zej                  j                  j                   ej                  j                  j"                  z  ed<    ee      Zeeeeej                  j                  j                   ej                  j                  j"                  z  f   f   ed<   y)ExportMetaDatarn  Ngraph_input_idx_to_local_sourceoutput_return_typeout_specmodule_call_spec)r   r   r   rv  r  ry  r   r   r   rz  r   r   utils_pytree
_LEAF_SPECr{  TreeSpecLeafSpecr|  r   r   r   r   rx  rx  
  s    
 AIA#T#v}*<%=  7?t6TS"223T 	&& ekk!!**U[[-@-@-I-II  	& dS%++%%..1D1D1M1MMMN	P 'r   rx  rG  c                 H    | d   }t        |t              s|j                  }|S )N__builtins__)r   r  __dict__)rG  
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f   dz  dedz  dedz  dee	e
e   f   dz  de
e
e   dz     dz  d	d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_sourcesrX  export_metadatatracked_fakes_id_to_sourceoutput_pycoder   c                    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 | _        |xs g | _        y r   )r  r  rF  rG  rH  rI  rJ  rK  rL  rM  rN  rO  rP  rQ  rR  rS  rT  r  r)   
_shape_envrx  r  r  r  )r   r  r  rX  r  r  r  r  s          r   r  zOutputGraphCommon.__init__M  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&," 	' +0br   c                     | j                   S r   )r  r   s    r   rX  zOutputGraphCommon.shape_envu  s    r   r   r  c                     t         r   )NotImplementedErrorr   r   r  s      r   bypass_packagez OutputGraphCommon.bypass_packagey  s
     "!r   )NNNNN )r   r   r   r   rE  r  r   r)   rx  r   r   r   r  rj  rX  r	   r  r  r  s   @r   r  r  5  s    4 15%)15EI7;&1#9&1 S#X-&1 d?	&1
 ($.&1 %)d6l):$;d$B&1 DI,-4&1 
&1P 8  "S " " "r   r  c                      e Zd ZU dZeed<   	 ddededz  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dedee   fdZdededefdZdededdfdZddZddZdefdZ	 dd ed!edeeej>                  j@                  f   fd"Z!dej>                  j@                  fd#Z"dd$Z#dee   dz  fd%Z$	 dd&e%d'ef   d(eed'f   d)eedz  d'f   dz  defd*Z&d&e%g ef   ddfd+Z'dd,Z(e)dd.       Z*e)dd/       Z+defd0Z,	 	 	 	 dd2Z-d3Z.d4e/ddfd5Z0e)dej>                  jb                  fd6       Z2e2jf                  d7ej>                  jb                  ddfd8       Z2e)de4eej@                  f   fd9       Z5e)de4ejl                  ejn                  f   fd:       Z8e)de4e9jt                  e;ej>                  j@                  d;f   f   fd<       Z<d(ed=edej>                  j@                  fd>Z=d(ed=edej>                  jl                  fd?Z>d(ed=eddfd@Z?e@j                  	 ddAeBdz  dBed-   dCedz  deCdD   fdE       ZDe)ddF       ZEe)dej                  j                  fdG       ZHe)deIfdH       ZJe)dej                  j                  fdI       ZMe)de4eef   fdJ       ZNe)deej                  j                     fdK       ZP	 ddLe4eee%d'ef   ef   f   dz  ddfdMZQddNZRddOZSe)ddP       ZTdQedefdRZUde/fdSZVdefdTZWdefdUZXdVedej                  j                  ez  fdWZ[ddXedefdYZ\dXeddfdZZ]e^d[edefd\       Z_d]ed^edej@                  fd_Z`d`ej                  j                  ejn                  z  ez  d[edaedefdbZad1ddeeeb   e4eef   f   fdcZcd1ddde/deee   edf   fdeZe	 dd1ddfefdde/deed   fdgZgd1ddhehddfdiZid1ddjee   dhehdkeddf
dlZjddmZkddfed=eddfdnZlde4eee/ez     f   fdoZmdXedefdpZne@j                  defdq       ZoddrZpdsedt   d1dddfduZqdsedt   d1dddfdvZrd1ddsee   dwesdeeeb   ee   dz  f   fdxZte)deejl                     fdy       Zue)deev   fdz       Zwd{ej                  d|ee7   deyfd}Zzd{ej                  d|ee7   deyfd~Z{de4eej>                  j                  f   fdZ|dXedej>                  j                  defdZ}deejn                     fdZ~ddZddZddZ	 dd!eeb   dee   dz  ddfdZdXedej                  de4eef   ddfdZdXed7eddfdZd!ed7edefdZd!ed7edefdZddZde%ej                  gdf   ddfdZdej>                  jl                  defdZdej                  j                  dQeddfdZy)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_effectsFcode_optionsr   Nroot_txr   rQ  rS  frame_staterF  rG  f_coderH  packager   	one_graphr   c                 
   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 | _        || _        || _        || _        g | _        t;        t<              | _        t               | _         |	jB                  |	jD                  |	jF                  d| _$        d | _%        tM               | _'        d| _(        g | _)        tU        | jR                  |xs tV        jX                  |xs tV        jZ                  tV        j\                  | jH                        }dd l/mc m+} |ja                  d      5  t        jb                  je                  |tg        | j2                        | j2                        }d d d        ti              | _5        | jj                  jl                  jo                  |	       | jJ                  | jj                  _%        | jj                  jl                  | _6        tq        jr                         | _:        | jw                          tV        jx                  'dd	l=m>}  || j~                  tV        jx                         t        j                  t              | _C        i | _D        t        |       | _F        i | _G        i | _H        t               | _I        i | _J        t        j                         | _M        t        t        t        |            | _Q        g | _R        g | _S        d| _T        i | _U        d | _V        g | _W        || _X        || _Y        d | _Z        || _[        i | _\        g | _]        g | _^        d| __        i | _`        t        j                   j                         | _b        d| _c        d| _d        t               | _e        t        i       | _f        t        i       | _g        | j                          i | _i        g | _j        d | _k        g | _l        i | _m        i | _n        d | _o        d | _p        | j                         | _r        t        j                         | _u        t               | _w        | j                         | _y        i | _z        t               | _|        t               | _~        i | _        d | _        y # 1 sw Y   xY w)
N)rI  rJ  rK  rL  rM  rN  rO  rP  )	is_export)r   r   r   F)tracked_fakesallow_scalar_outputsallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards	co_fieldsr   (fake_tensor_allow_unsafe_data_ptr_access)rX  allow_non_fake_inputsrQ  )_wire_spec_assumptions)rE  r  r   r   autograd
forward_ad_current_levelra  rb  #retrieve_all_functorch_interpretersr}  _deviceCURRENT_DEVICErf  convert_frameinitial_global_statere  r   rg  rO  rh  SubgraphTracertracersinput_source_to_varleaf_var_creation_orderrQ  rS  r  cleanup_hooksnext_compile_id_counter
compile_idinstalled_globalsr   r   r   r  cudagraph_annotationrP   region_tracker_emit_debugger_breakpointr  r)   r1   capture_scalar_outputs capture_dynamic_output_shape_opsr  torch._functorch.configpatch_subclassesFakeTensorModer   r   tracing_contexttraced_coder   r   current_compile_iddynamo_compile_idinit_ambient_guards_shapes_spectorch._dynamo.variables.builderr  rX  collectionsdefaultdictr   r  param_name_to_sourcerU   r  variable_tracker_cachemro_source_cacheguarded_mro_absent_keyssignature_cache	itertoolscountunique_var_idr
   r   r  r  output_instructionsr  	timestamp_input_mutation_streams_last_checked_input_versionsregister_finalizer_fnsr   r  profiler_stater  source_to_user_stacks_current_txcleanupsshould_exitunspec_variable_map_is_torch_function_mode_enabledtorch_function_mode_enabledtorch_function_subclass_inlined!has_user_defined_allowed_in_graphautograd_grad_consumed_grad_fnsnon_compliant_opscompliant_custom_opssave_global_state dynamo_flat_name_to_original_fqnr   random_values_varpregraph_bytecodesynthetic_source_ctor_infobackward_statebackward_state_proxybackward_state_var!install_builtins_dict_in_fglobalsrT  
contextlib	ExitStackcompiler_trace_stackr/  bytecode_tracing_timings+maybe_install_saved_tensors_hooks_subgraphs"saved_tensors_hooks_subgraph_namesr  rx  r  r.   "used_inlined_inbuilt_modules_namesattr_source_cache(_cached_replayed_side_effect_source_refs)r   r  r   r  rQ  rS  r  rF  rG  r  rH  r  r  rX  _config	fake_moder  s                    r   r  zOutputGraph.__init__  s    	''%"u*,~~00??"--99]]_ ;;..==$}}::OO  :xx&&779MM++- " 	( 	
" 'tv>? CE  ?A$"4&68#$78+.5
 ~~!--$33
 BF!02/4& 13 ,,!*!Kf.K.K+4 ,7668>8j8jnn
	& 	21]]E]J 	))88#&*4;;&7{{	 9 I	 0>i/H((//7484M4M1//;;3A3T3T3V  " *N"4>>63F3FG ##D) 	' ?A!'- FH# KM
 >AU$;=&__.)-k4;M)N68 57  KM$CG) OQ# /: ;? RT"<>+- KM  ,188+S+S+U( 05, 27. PSu, >AW ADB! 	  AC-  	 '+ 57  	' ;=;?!.2 224 	2 %/$8$8$:!(>(@% <<> 	/
 /1-/ DN</GIPT5	 	s   $A UU(populate_export_metadatar  c          	      J   |s7| j                   j                  s!| j                  t        | j                        S ddlm} g }| j                   j                         D ]  }t        |d      s|j                  }t        |t        t        f      s4t        |t              r t        |j                  |      r	|r|j                  t        d      |j                  j                   j!                         D ]  \  }}i }|j                   j!                         D ]0  \  }	}
|
j#                         ||	j$                  j#                         <   2 ddgt        |j'                               k7  r%t        dt        |j'                                      || j(                  j*                  |j$                  j#                         <    t        |t              rt        |j                  |      r|j-                  |        |D cg c]  }t/        |j0                         c}S c c}w )zKReturn Python-side effect sources that Dynamo replays outside the FX graph.r   )_ExportModuleSpecTrackerDictmutation_typezvar._base_vt must not be Nonein_specr{  z7Expected specs keys to be ['in_spec', 'out_spec'], got )r  id_to_variabler  r   torch.export._tracer  _get_modified_varshasattrr   r   rT   rV   r   r   _base_vtrV  r  as_python_constantvtkeysr  r|  r   rW   source)r   r  r  potential_side_effectsvarmut_typer  r  specsk_specvals              r   $get_replayed_side_effect_source_refsz0OutputGraph.get_replayed_side_effect_source_refs  s   
 )%%44==IEEFFD!#$$779 )	;CsO,,, 8:OP "#'>?J		#?E 4"||3&45T&U U
 !$ 2 2 8 8 :	* ! ! !
 )+3477==? !&KFC(+(>(>(@ %*&))*F*F*H$I!& %.z#:d5::<>P#P*8*abfglgqgqgsbtau(v+& %& %* !% 4 4 E E$%DD$;$;$=!"#*0 #3(?@&syy2NO.55c:S)	;V ?UUs&szz2UUUs   H basepathc                 0   |j                  d      }||d   f}|| j                  vrt        ||d         | j                  |<   | j                  |   }|dd  D ]<  }||f}|| j                  vrt        ||      | j                  |<   | j                  |   }> |S )N.r   r0   )splitr  rX   )r   r  r  partsrB  r+  parts          r   get_chained_attr_sourcez#OutputGraph.get_chained_attr_source  s    

3U1Xd,,,*4T58*DD""3''',!"I 	1D4.C$000.8.F&&s+++C0F		1
 r   rb   c                     |j                  dd      }t        |      dk(  rt        ||      S | j                  ||d         }t        ||d         S )Nr  r0   r   )rsplitlenrb   r  )r   r  r  r  intermediate_bases        r   get_chained_param_buffer_sourcez+OutputGraph.get_chained_param_buffer_source  sU     C#u:?$T400 88uQxH !2E!H==r   c                 v   | j                   j                  t        dd             t        j                  rddlm} | j                   |       | _        | j                  j                  }| j                  j                  |j                  |j                  |j                  t        j                                y y )Nr7  Tlog_pt2_compile_eventr   r   )r  enter_contextrn   r1   dynamo_profilertorch._dynamo.dynamo_profilerr   r  r  r  pushr   r   r   timetime_ns)r   r   codes      r   mark_bytecode_tracing_startz'OutputGraph.mark_bytecode_tracing_start  s    !!//"&*	
 !!I""*&9&;#<<&&D$$  ##	 "r   c                    | j                   j                          | j                  j                          t        j
                  r,| j                  ddlm} | j                  j                         }t        j                         }|||j                  z
  }||j                  z
  } ||j                  |j                  |j                   ||t#        | j$                  j&                  j(                        dd d d |j*                  d      }| j                  j-                  |       d }t/        t        j
                  t0              rt        j
                  }| j                  j3                  |       y y y )Nr   )FunctionTraceTimingr   )	func_namefilenamefirstlineno
cumtime_ns
tottime_nsbytecode_countinline_depthcaller_func_namecaller_filenamecaller_firstlinenois_primitive_call
call_stack)r  rC  r  closer1   r#  r  r$  r+  r   r&  r'  start_time_nschild_time_nsr,  r-  r.  r  r  r  r   r6  record_timingr   r   
dump_stats)r   r+  stack_entrytrace_end_nsr/  r0  timingoutput_files           r   mark_bytecode_tracing_stopz&OutputGraph.mark_bytecode_tracing_stop  s2   %%668!!'')!!d&9&9&EI--113K<<>L&)K,E,EE
'+*C*CC
,)33(11 + 7 7))#&t||':':'B'B#C!"%)$('+&1&C&C! ##11&9 K&00#6$44**;79 'F!r   c                 P    t        | j                        }| j                  d|      S )N__builtins_dict__)r  rG  install_global)r   r  s     r   r  z-OutputGraph.install_builtins_dict_in_fglobals3  s&    &t'8'89
""#6
CCr   hookprefixc                     | t        | j                         }|| j                  v rt        d| d      || j                  |<   || j                         fS )Nzbackward_state hook name 'z' already registered)r  r  rV  get_backward_state_proxy)r   rE  rF  r:  s       r   add_backward_state_hookz#OutputGraph.add_backward_state_hook7  sl     #d112344&&& ,TF2FG  %)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 exportr  z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_stater
  grapharg)r  rQ  rI   r%   root_tracercreate_graph_inputrW  rY   r|   r   metanew_varr  )r   example_values     r   rH  z$OutputGraph.get_backward_state_proxyB  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   rc   
make_guardrQ   	SHAPE_ENVr]   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICEGLOBAL_STATETORCH_FUNCTION_STATEr   re  ra  peek_interpreter_stackFUNCTORCH_STACK_MATCHrf  compiled_autogradin_compiled_autograd_regionAUTOGRAD_SAVED_TENSORS_HOOKS)r   cis     r   r  zOutputGraph.init_ambient_guardsW  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                    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k7  rt        d| d      |dk7  rt        d| d      ||gS )Nsaved_tensors_hooks_packsaved_tensors_hooks_unpacksaved_tensors_hooks_pack_0zEExpected pack_subgraph_name to be 'saved_tensors_hooks_pack_0', got ''saved_tensors_hooks_unpack_0zIExpected unpack_subgraph_name to be 'saved_tensors_hooks_unpack_0', got ')r   rf  rb  rc  ra  _aot_autogradr}  top_saved_tensors_hooks"saved_tensors_hooks_are_inlineableinstall_subgraphr   r-  r	  r   rV  )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_subgraphsu  s3   ==**FF$$2288PP	**00SS 	 & #!22&HH  '--@
  $44(HH  )//B 
 !== WXjWkklm   #AA [\p[qqrs  #$899r   r  .r  ctor_arg_sourcesc                     | }| j                         }t        | j                        j                  fd       |r#j	                  d t        ||      D               n3j	                  t        t        j                  j                  |             j                  t        |      d       j                  |       | j                  j                  j                                t!        |      }||f| j"                  |<   t%        j&                  | j                  ||      }|j)                         }t+        j,                         j.                  j0                  j3                  |       |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>  s     B'' r   c              3   h   K   | ]*  \  }}||nt         j                  j                  |       , y wr   )r4   ConstantVariablecreate).0r  srcs      r   	<genexpr>z4OutputGraph.synthetic_graph_input.<locals>.<genexpr>  s6      C I,F,F,M,Mc,RRs   02F)rV  rC   r  add_push_nullforeachzipmapr4   r~  r  call_functionr  storer  extendget_instructionsrd   r  r   buildrealizer   getguards_contextdynamo_guardsremove_guards_with_source)	r   r  r  rw  rW  varnamer
  r+  r{  s	    `      @r   synthetic_graph_inputz!OutputGraph.synthetic_graph_input  s1    D	,,.t||$
	
 JJ  #D*: ; 
 JJs955<<dCD
TE*
%%b&9&9&;<%g.35t=M2N''/ &&t||]FK!++99SS	
 r   c                 :    | j                   j                  |       y r   )r  r   )r   r  s     r   add_cleanup_hookzOutputGraph.add_cleanup_hook  s    !!"%r   c                 z    t        | j                        D ]	  } |         | j                  j                          y r   )reversedr  clear)r   rE  s     r   call_cleanup_hookszOutputGraph.call_cleanup_hooks  s3    T//0 	DF	  "r   r  c                      | j                   d   S Nr   r  r   s    r   rS  zOutputGraph.root_tracer  s    ||Ar   c                      | j                   d   S Nr  r   s    r   current_tracerzOutputGraph.current_tracer  s    ||Br   c                 2    t        | j                        dk(  S )Nr0   )r  r  r   s    r   is_root_tracerzOutputGraph.is_root_tracer  s    4<< A%%r   txc                    t        |d      sy|j                  j                         sy| j                  }| j                  #t        t        |j                              | _        |j                  j                         }d}|j                  j                  D ]  }|j                  dk7  r y|j                  j                  d      }t        |t        j                         sJ| j                  j                  |      }|j"                  }|C||kD  r>|| j$                  vr!t'        j(                         | j$                  |<   || j                  |<   |dz  } y)zRecord which stream index has input mutations by comparing current
        tensor versions against the versions captured at graph input creation.symbolic_stream_stateNr   placeholderrW  r0   )r  r  in_stream_contextrS  r  r  	enumerate_input_versions_at_beginningcur_stream_idr   nodesoprU  r  r   r   r   _versionr  r   extract_stack)	r   r  tracercur_stream_index	input_idxr   rW  prev_versioncur_versions	            r   &check_input_mutation_on_current_streamz2OutputGraph.check_input_mutation_on_current_stream  s6   
 r23''99;!!,,404&==>1D- 33AAC	LL&& 	Dww-' IIMM/:MmU\\:<<@@KL'00K'K,,F#4+G+GG&446 001AB @K11)<NI	r   a~  To fix this, either:
  1. Move the input mutation after the event.record() call.
  2. Record the event outside the compiled region:
       compiled_fn(x)
       event.record(stream)  # after torch.compile returns
  3. Insert a graph break before recording:
       torch._dynamo.graph_break()
       event.record(stream)
  4. Record the event on a stream that has no input mutations.stream_indexc                    || j                   vry| j                   |   }t        j                         }ddj                  |j	                                ddj                  |j	                                d| j
                  z   }t        |      )u   Error if an event is being recorded on a stream that already has
        an input mutation. Called at record time so ordering is naturally
        respected — records before mutations won't trigger this.Na  An event was recorded on a stream where a graph input was previously mutated. The input mutation is applied via copy_() in the runtime epilogue after the graph executes, so the event would not capture the mutation, leading to incorrect synchronization.

Input mutation occurred here:
r  z
Event record occurred here:

)r  r   r  joinformat_EVENT_INPUT_MUTATION_FIXr&  )r   r  mutation_stackrecord_stackr   s        r   'check_event_record_after_input_mutationz3OutputGraph.check_event_record_after_input_mutation   s     t;;;55lC%335. ww~,,./0 1,ww|**,-.b2 594R4RS 	 3r   c                 .    | j                   j                  S r   r  r   r   s    r   r   zOutputGraph.graph  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_proxy2  s     /t""//@@@r   c                 :     | j                   j                  |i |S r   )r  create_noder  s      r   r  zOutputGraph.create_node5       .t""..???r   c                 :     | j                   j                  |i |S r   )r  remove_noder  s      r   r  zOutputGraph.remove_node8  r  r   source_targetprior_tracerdescription)r  NNc              #     K   t               }	 |r#|j                  | j                  urt        d      |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)NzBprior_tracer.parent must be the current tracer to preserve lineage)parentr  r  r  )rD   r  r  rV  	__enter__r  r  r  r   __exit__r   )r   r  r  r  new_scope_ctxr  s         r   	subtracerzOutputGraph.subtracer;  s      ()	&&d.A.AA(\  ##%   #.."/"11;; +  LL'L""4t4LL ""4t4LLs   C4BC .C4/C11C4c                     | S r   r   r   s    r   outputzOutputGraph.output\  s    r   c                 p    | j                   j                  t        d      | j                   j                  S )N*tracing_context.fake_mode must not be None)r  r  rV  r   s    r   r  zOutputGraph.fake_mode`  s2    ))1 !MNN##---r   c                     | j                   j                  t        d      | j                   j                  j                  t        d      | j                   j                  j                  S )Nr  z4tracing_context.fake_mode.shape_env must not be None)r  r  rV  rX  r   s    r   rX  zOutputGraph.shape_envf  s\    ))1 !MNN))33; !WXX##--777r   c                 B    | j                   j                  j                  S r   )r  r  r  r   s    r   r   zOutputGraph.guardsn  s    ##22@@@r   c                 B    | j                   j                  j                  S r   )r  module_contextr	  r   s    r   r	  zOutputGraph.nn_modulesr  s    ##22===r   c                 B    | j                   j                  j                  S r   )r  r  r[  r   s    r   r[  zOutputGraph.aotautograd_guardsv  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                  j                         x}r|j                  nd}t!        j"                  t        j$                  |      t        j&                  |      f|d<   t!        j"                  t        j$                  d      t        j&                  d      f|d<   t!        j"                  t        j(                  |      t        j*                  |      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acceleratorcurrent_acceleratorrW  	functoolspartialset_autocast_enabledis_autocast_enabledset_autocast_dtypeget_autocast_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)r   r  r  accgpu_types        r   r  zOutputGraph.save_global_statez  s    eHS#X.4556 ? ))88EE
 ).(>(>@U@U@W'X^$ !& 1 1 E E GGGCHHf 	 e88(C%%h/,
'(
 e88%@%%e,0
+,
 e66A$$X..
)*
 e66>$$U+.
)*
 ,,++-2
-.r   c                 :    | j                   j                  |       y r   )r  r   )r   r  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  r  r   s    r   
current_txzOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMr   r
  c                 ~    |j                  | j                  j                  | j                  j                  di i       S )z
        Resolve the runtime value a Source points to using root_tx's frame.

        Useful to inspect the python object associated with the source during
        debugging. Will also be used by invoke subgraph caching later on.
        )GL)	get_valuer  	f_globalsf_locals)r   r
  s     r   resolve_source_valuez OutputGraph.resolve_source_value  s9     ,,((t||/D/DE
 	
r   c                 ,    t        | j                        S r   )rl   r   r   s    r   rl   zOutputGraph.count_calls  s    4::&&r   c                 X    t        t        | j                  j                              dk(  S r  )r  r   r   r  r   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   r  r  )r   xs     r   has_outputszOutputGraph.has_outputs  s5    tzz//D!14483CADEIIDs
   AAr	  c                     |st        d      | j                  }|j                  d      D ]$  }t        |t              r||   }t        ||      }& |S )Nzkeys must be a non-empty stringr  )rV  r	  r  r   r  r   )r   r	  objr  s       r   get_submodulezOutputGraph.get_submodule  sV     !BCC<@OO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)Nr   r   )r   r  r  r  )r   r:  existingr  s       r   rV  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 namer   N)r  )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)r  r  r   rer  isalpha)r  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  rs   r{   r   )r   r  r  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                 `   
 t         j                        r!t        j                   j                  fi S t              dvrt        d      d   t        t              rt        d       t        t        j                        r@ j                   j                         s j                  dt        dt        f fd}nUt        t        j                  j                         rt        t        j                  j                         st        dt#                     r<t%        j'                  t(        j*                               dt        dt        ffd}ndt        dt        ffd	}nt        t        j,                  t        j.                  f      rdt        dt        f fd
}net1        t#                    r> j                   j                         s j                  dt        dt        f fd}ndt        dt        f fd} j2                  j5                         D ]  \  }}|u s ||      c S  t7        j8                  | 
t;        
 j2                   j<                        
 j2                  
<   t        t        j                  j                         rhdt        dd f
 fd}t?        d      r jA                         D ]  \  }}	 ||        t?        d      r jC                         D ]  \  }}	 ||         |
      S )Nr
  z$'source' must be provided in optionsz,source must not be a ParamBufferSource, got 
module_keyr   c           	      x   j                   t        d      j                   | <   j                  t        d      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 rt        d      t)              |j#                         j$                  j&                  d<   |S )	N%param_name_to_source must not be Noneroot_tx must not be Noneguardedr  r   rW  tensor_dictz/'tensor_dict' already exists in proxy node meta)r  rV  r  r  r  rr   r   ra   rR   rY  rQ   ID_MATCHr^   TENSOR_MATCHr   r  track_object_existingas_proxyr   rU  rg   )r&  r  r$  r   r
  r#  r  s     r   	wrap_namez6OutputGraph.register_attr_or_module.<locals>.wrap_name+  s~   ,,4()PQQ8>))*5 <<'()CDD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$6$6$;$;;(I  :Nf9U""''6	r   z&target must be a torch.nn.Module, got c                 2    t        t              | fi S r   )r   rW  r&  r$  r#  s    r   r0  z6OutputGraph.register_attr_or_module.<locals>.wrap_name[  s    +DL*fXPWXXr   c                 0    t        j                  fi S r   )r4   UnspecializedNNModuleVariabler2  s    r   r0  z6OutputGraph.register_attr_or_module.<locals>.wrap_named  s    $BB6UWUUr   c           	      n    t        j                  j                  j                  d| di       fdiS )Nr  r   sym_num)r   r  r  r  )r&  r$  r   r#  s    r   r0  z6OutputGraph.register_attr_or_module.<locals>.wrap_namep  sE    &--LL%%j*b"E # 	 r   c                 V   t         j                  j                  j                  j                        }j                  d| di       }t        |j                  |       t        j                  j                  j                  j                  j                  ||fdj                  iS )Nr  r   r  )r   _libraryfake_class_registrymaybe_to_fake_objr  r  r{   r   rf  r4   script_objectTorchScriptObjectVariabler  r  )r&  fake_script_objr   r$  r   r#  r  s      r   r0  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  s    "'.."D"D"V"VNNF# ++J
BK!%**o>}}..<<VV]]?/3||?F r   c                     j                   j                  |        j                  | <   t        j                  t        |             S )N)source_name)r  r  rG  r   r  rZ   )r&  r   r#  s    r   r0  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  sF    ++J706!!*-&,,"z: r   	leaf_namec                     j                   t        d      j                  |       } d|  }|j                   |<   t        t              r-| j
                  t        j                  |j                        <   y y Nr(  r  	r  rV  r  r   r`   r  r  r  r:  r@  
new_sourcenew_namer:  r   r
  s      r   register_leaf_namez?OutputGraph.register_attr_or_module.<locals>.register_leaf_name  s    ,,4()PQQ!AA&)T
"V1YK06@))(3fk2 " 99#33JOOD 3r   _parameters_buffers)"rS   rQ  r   r  r   r  rV  r   rb   r   r   r  r  rS  r   r  r  rW  rR   rY  rQ   	NN_MODULESymIntSymFloatr    r	  r  r  r  rs   rG  r  named_parametersnamed_buffers)r   r#  r  r$  r0  r  r  rG  r@  r   r:  r
  r  s   `` `      @@@r   register_attr_or_modulez#OutputGraph.register_attr_or_module  s     4 #((&LGLLw-7" !GHH"f/0 >vhG  fell+((F&&( ))%c %o % %N 0fehhoo6$<T&\NK  f//0F0FGHY# Y/ YV# V/ V u~~ >?c o   DL)
 ((F&&())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                 b   | j                   j                  d      }t        |      }|sg i fS g }i }g |j                  |j                  j                         | j                  j                  j                         }|r@|j                         }t        |t              rKt        |j                  t              s!t        dt        |j                               ||j                  z  }n|| 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 }
| j0                  D ]Y  }t        |j2                  t              r2t        |j$                  t*              r|j$                  j,                  |v sQ|j$                  j,                  }|| j4                  d   vrt        d| d      ||   D ]  }|j$                  |
v r|j$                  t        d      |j$                  j6                  }||	vrU| j9                  | d      }||	|<   |j;                  t=        d|	      t?        |      tA               t=        d
|	      g       |j$                  t        d      |j$                  }t+        |	|         |
|<    \ ||
fS )Nr   zx.items must be a list, got r   rj  z' not found in co_varnameszx.source must not be None_ref	LOAD_FASTargval
STORE_FAST)!rF  r  rq   r   symbolic_localsvaluesr  store_attr_mutationsr	  r   r   r   r  r   rV  rW  r   rT   r
  r\   r  r`   
local_namer   	graphargs_exampler  indexrV  r  r<   r=   r8   )r   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuer  stolen_namer   overridden_sourcesarg	list_namelist_idx
alias_name
old_sources                   r   handle_aliases_for_stolen_listsz+OutputGraph.handle_aliases_for_stolen_lists  s    ##''//9 r6M8:
XX
&&(
 3388:
 		A!-.!!''40(+GQWW)WXX  T..CCC!!//3LMqxx7qxx}}k:HHMM,,0AA((--22K+-+-K($++A.- 2 35>> *	PC3<<.szz;7JJ))[8 

--I 1 1- @@$q3M%NOO + P8811 88#()DEE88>>7*!%$+T*"J )3GH%&&.{9M-h702.|JO	 88#()DEEXX
1<WX=N1O":.=P*	P` ...r   
stack_popsc                 Z   |j                          g }t               }dt        ddfd}t        |j                        D ];  \  }}t        |j                        |z
  |k  }t        j                  j                  ||       t        |t        j                        st        j                  ||       |r|j                  |       t        |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         }
|j0                  j3                         D ]T  \  }}t        j                  ||       t        |j4                  t6              r(|j4                  j8                  |k(  r|| j:                  u r_||
v rdt<        j>                  dk\  r6t@        jC                  t        |      rA|jD                  j                  |       t@        jC                  t        |      rtG        d	      t        |jH                        |jH                  |<   t        |t              r@|j                   dnt#        |j                         }	|jJ                  j                  ||	f       |j                  |       W ||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   Nc                 b    t         j                  t        j                  |       rt	        d      y )NzCAttempted to reconstruct WithExitFunctionVariable outside the stack)rW  __instancecheck__r4   WithExitFunctionVariablerV  )r  s    r   ctx_exit_checkz@OutputGraph._get_stack_values_to_restore.<locals>.ctx_exit_check.  s.    %%i&H&H#N$Y  Or   )allow_lazy_constantr   r0   )      z.variable should never be NULL in Python < 3.12)&prune_dead_localsrl  r   r  r   r  r4   LazyVariableTrackerrealize_allr   rn  visitr   r   rq  r   target_valuesr   rs  rt  rm  r   cellvarsfreevarsrV  r  r
  r`   rY  r  sysversion_inforW  rm  rr  rV  rp  ru  )r   r  rj  stack_valuesrU  ro  ir   rp  rw  cell_and_freevarsr  r  s                r   _get_stack_values_to_restorez(OutputGraph._get_stack_values_to_restore  s   " 	"$	 	D 	 ""((+ 	4HAu"%bhh-!"3z"A))55+> 6  eY%G%GH%%ne<"##E*%.%%,,Q/##E*%!89--5B5ATAT;U  ##**C,=,A=+QR))003/	42 \* => &&,,. &	#DAq!!.!4 188[1HH''1,$,,&%%7* )),:))003 )),:(H  $'t'8'8#9Da !45//1BuQ__7M  $$++Q,>?"M&	#P T!!r   r   c           
      |   | j                   t        d      t        j                  ddlm}  || j                         t        j                  s| j                   |urt        d      | 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t        d      | j1                  |	|	|u r|nd      \  }
}|j#                  |
       |j#                  |       t3        |	j.                        D ]  }|j5                  |	|j6                         ! |	j8                  }	|	| j:                  j=                  |       | j?                  |       | j@                  r#ddl!m"} | j?                   |              d| _         | jF                  r| jH                  rt        d      | j?                  | jF                         | jK                  | j                         \  }}| j?                  |       | jM                          | jN                  jQ                         D ci c]  \  }}|tS        |       }}}tU        |      }ddl+m,} t'        | jZ                        dkD  rg }| j]                  d      | _/         |ta        | jZ                        d      }| jc                  d|      }te        | j                   ||      }|jg                  |ji                  |d             |jg                  tk        dd             |j#                  |jm                  | j^                               | j?                  |       |D cg c]  }|D ]  }|  }}}d}d}| j                   |u r0|r-t-        d |D              rt-        d |D              rt'        to        |            t'        |      k(  r| j:                  jq                         r|jr                  s| jt                  s|d   jv                  s|d   jx                  ste        | j                         }| j{                  ||       | j}                  |t        t3        |            |      \  }} | j?                  g ||j                         t        d      t%        dt'        |            |        n+| j]                  d      }te        | j                   |||      }!| j                  |||!d       i }"|!j                  jQ                         D ]'  \  }}#|#dkD  st        |t        t        f      r#d|"|<   ) te        | j                   |||"|       }$| j                  |||$d       t        j                  j                  j                  rC|r@t'        |      dk(  r1|d   }%t        |%t        j                  j                  j                        r|%j                  t        j                  j                  j                  u r|%jP                  d   }&|%jP                  d   }'t        |&t        j                  j                  j                        st        d!      i }(|$j                  j                         D ]b  })t        |)t        j                  j                  j                        st        d"t        |)             |)j                  }*|)j                  |(|*<   d | j                  j                  }+t        |&jP                        D ]  \  },}%|%|(v rt        d|(|%         |+|,<   |%j                  @t        |%j                  d#d      x}-r't        |-d$d      rt        d%|%j                        |+|,<   i|%j                         rt        d&|%j                               |+|,<   t        d'|% d(|,        	 |'j                         | j                  _`        g }/d}0t        | j                        dk7  st'        |$j                        dk7  r| j}                  ||$j                         |      \  }}0|/jg                  |       t'        |$j                        dk7  r#|/j#                  |$jm                  |             d}n+|/j#                  t%        d/             n| j                          | j?                  |/|0       | j?                  |$j                         |$j                                | j?                  t%        d0t'        |      |d   j                  z
        g       d}1d}2t        |      D ]  \  }3}t'        |j                        }4|3dk7  r|4|j                  z  }4|4dk(  r)| j?                  t%        d0d      gt        d             n;|2|4z  }2| j?                  t               gt        |1|2      t        d             |1|4z  }1|3t'        |      dz
  k(  ste        | j                         }5i }6| j                   j                  jQ                         D ]~  \  }7}8t        |8j                  t              s!|8j                  j                  |7k(  s;|5j                  |5j                  |7             t'        |j                        t'        |6      z   |6|7<    | j?                  |5j                         t%        d0t'        |6            t%        d1d      gz          |j                  j                  |6        | j?                  t%        d/      t%        d0t'        |            gt        |d   j                  dz                |r|r| j?                  t%        d2|3      g       t        j                  j                  j                  d4v rc| j                  d5      }9|9rOt        j                  j                  j                  d6k(  rt        j                  d7|9        |S t        d7|9       |S c c}}w c c}}w # t        $ r }.t        d)d*|' d+d,d-g|..       Y d}.~.Ed}.~.ww xY w)8a  
        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.
        Nr)  r   )_finalize_spec_wiringz7root_tx must be tx when nested_graph_breaks is disabledTzCOMPILING GRAPH due to %srq     COPY_FREE_VARSr   rd  c              3   <   K   | ]  }|j                           y wr   )can_restore)r  blocks     r   r  z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s     Kuu((*K   z3all blocks in cur_tx.block_stack must be restorable)is_graph_breakr0   )create_breakpointFz)export does not support pregraph_bytecodedisablerandom_valuesz.do not trace into Dynamo rng recovery functionr   __gen_rand_values)rc  c              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  r  s     r   r  z/OutputGraph.compile_subgraph.<locals>.<genexpr>9  s     =aAKKM=r  r     UNPACK_SEQUENCE)pycoder   )tempvarsrc  z#flat_returns must be a ListVariablez3graph output value must be a GraphOutputEntry, got r  is_inputr   r   zEncountered unrecognized type z at output z8nested function with non-constructible closure in outputz as_python_constant for out_spec zCannot return a nested function with closure from a compiled function. Dynamo failed to construct the function defined in the compiled region with closure objects.zGDefine the function at module scope instead of inside another function z0Ensure that all closure variables are constants.)rL  rM  rN  rO  from_excPOP_TOP
BUILD_LISTLIST_EXTENDDELETE_FASTrS  )warnerrorr  r  zWhile compiling, we found certain side effects happened in the model.forward. Here are the list of potential sources you can double check: )wr  rV  r1   r  r  r  rX  nested_graph_breaksrA  compile_subgraph_reasonr  r   debugrz  r{  prefix_instsopnamer   r<   r  r  r!  allblock_stackr  r  exitr   r  r  prune_dead_object_newadd_output_instructionsr  bytecode_transformationr  r  rQ  ri  cleanup_graphr	  r  ry   r  
decoratorsr  r   rV  r  r  rD  rC   r  load_function_namer:   create_storer   is_emptydebug_localsr  rq  rr  codegen_cellscompile_and_call_fx_graphr   r  r?   codegen_suffixusesrv   rd   r`   r   rf  log_graph_in_out_metadatar   r4   NamedTupleVariable	tuple_clsfunctional_exportExportTracerOutputListVariablegraph_outputsrW  codegenGraphOutputEntryrW  variabler\  r  rz  r  r   r
  r   is_python_constantr  r{  r   rI   rl   r   graph_output_varsrun_compiler_collective
get_pycoderm  rp  r;   r7   rV  rY  append_outputcreate_loadupdater>   side_effect_replay_policyr  warningsr  r&  ):r   r  r   rj  r  r  install_stack_valuesall_stack_locals_metascur_txr|  rU  r  r  r_  rc  r:  r  nn_modules_proxiesrootr  random_calls_instructionsrand_fnrand_fn_namer  valsr  stack_values_flatstored_graph_output_vargraph_output_varcell_cginstructionsr  pass1r  r  pass2r  flat_returnsr{  vt_to_graph_out_idxr   r  rz  idxr
  er  subgraph_pycode	start_idxend_idxr}  n_valsroot_cgunmodified_locals_namesr  r  side_effect_refss:                                                             r   compile_subgraphzOutputGraph.compile_subgraph  si   * << !;<<
 *M!$..1))||2%$M 
 	'')'-$		-v6 +-w&11 	9;;"22 ''*, #DLL$=$=m$L M !''		$8	9 !#35 K8J8JKK$I  "&!B!Bfl
"L$ ##L1"))$/ "&"4"45 F

6&2D2D
EF ]]F#  ( 	//3$$\2))B(():)<=-2D*!!dkk !LMM$$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+#'#A#AD"345t$ L& ((! --/ !^	
 ''8cBS>TU  ) 	  $||K8  #5	E $5ueD H#jj..0 )
U19VC2F1T%U$(HSM)  !#5E $5udC $$>>%)*a/&q)r5==#:#:#M#MN}}66IIJ $&88A;L!xx{H%$emm&=&=&J&J --RSSFH'!&!4!4!;!;!= D)!5==#8#8#I#I  #1"UVZ[`VaUb c#  5:NN8=+H5D *.)=)=)P)P&#,\-?-?#@ R!446F +-@-D7.s3 II1+2299fd+K!K!K '
E B6F '7.s3  2246F *B,A,A,C7.s3 #1"@KPSu U# %*8@8S8S8U,,5  F"O4::&!+s53F3F/G1/L040N0N//141-o l+u**+q0MM%"4"45E"FG.2+MM"4Y"?@ ,,.(((H((&&(1A1A1C )  	$$" -.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H)-  I    ==''AAVKMMXXhWik &% 'XXhWik 
 &%e

Z OF 2 %$^&Fxj$Q! !j R# &' s$   $rrr 	r;r66r;r{  c                 $   | j                   j                  rd}|}|t        t        |j	                                     }|D ]]  }|| j
                  u r!|j                  |j                  |             2|j                  st        d       ||j                  |          _ |j                  t        t        |                   |j                  }|dz  }||j                  t        d|             y |j                  t        dd             y )Nr   zAcur_tx.post_prune_cell_and_freevars must be set for nested framesr0   r  r  )r  r   r   sortedr~  r  r  create_load_closurepost_prune_cell_and_freevarsrV  r9   r  r  r<   )r   r  r{  tx_cntr  ry  cells          r   r  zOutputGraph.codegen_cellsL	  s   ''33F79F$ !(@(@(B!CD$ FD-(()?)?)EF%BB"0 c#  6>>tDEF   !3CM!BC!% $& /&IJ/!DEr   r|  log_side_effectsc                 X  	 | j                   j                         | j                  r| j                  rt	        d      | j                  j                         D ]_  \  }} |       | j                  t	        d      j                  j                  | j                               j                  |       a t        j                  r| j                   j                         |j                  D ]h  \  	}j                  	fd       |D ]
  } |        j                  t!        t#        |      d             j                  t%        d      g       j | j'                  |       j)                  ||j                          | j                   j+                  |       y )Nz.backward_state is not supported in export modez>backward_state_var must not be None when backward_state is setc                              S r   r   )r{  	debug_vars   r   r|  z,OutputGraph.codegen_suffix.<locals>.<lambda>	  s    R	] r   Fr  )value_from_source)r  codegen_save_tempvarsr  rQ  rV  r  r  r  r  
store_attrr1   replay_side_effectscodegen_hooksr  r  extend_outputr:   r  r<   r  restore_stackcodegen_update_mutated)
r   r  r|  r{  r  r:  r  r  rd  r  s
      `     @r   r  zOutputGraph.codegen_suffixh	  su    	//3{{$%UVV!00668 $	c3**2(X    0G0G!HId#$ %%++B/  " 	>OIt23 31#d)UCD0;<=	> 	2r"
RYYG005EFr   c                    | j                   st        d      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)
        z-should_exit must be True before cleanup_graphcreation_timestampNr   )r  rV  r   r   r  rU  r   r   r  r  pairwiser#  re  _set_grad_enabledr   r  _erased
erase_node)r   r  r   r  node1node2s         r   r  zOutputGraph.cleanup_graph	  s#     !PQQ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)r  r   rf  r1   strict_precompiler2   PackageErrorr   warning_loggingtrace_structuredbypass_current_entryr  s    ``r   r  zOutputGraph.bypass_package	  s     ||==11--##00/  	3V<'' 	( 	
 	))+r   c                 T   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 )NrW  )r   r  rU  r  r   r   r  r!   shaper   reprr:  )r   retr   rW  r   ss         r   get_graph_sizes_structuredz&OutputGraph.get_graph_sizes_structured	  s    *,JJ$$ 	VD IIMM/4@M-):):)E)EF$**PT!U1z!S'9!tAw"F!UDII		V
 
 "Vs   -$B%c                 L   d}|d| dz  }| j                   j                  D ]  }|j                  j                  dd       }t	        |t
        j                  j                        sD|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 =====
rW  z: r  FTz (concrete): )r   r  rU  r  r   r   r  r!   r  r:  r   r   r   rK  r   hint)	r   r:  graph_sizes_strr   rW  r   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
        )r  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                    | j                   }|t        d      |j                  x~j                  pj                  }t
        j                  dj                         t        j                  j                  dd fd       |j                  }t        |      dk7  r)t        dj                  d	j                  |                  t        |j!                               j#                  |j%                         t        j&                  j)                         z        5  t+        d
d      5  d g|j-                         z  }t/        j0                  |j                  |       |_        d d d        d d d        |j2                  j5                          t6        j8                  y y # 1 sw Y   =xY w# 1 sw Y   AxY w)Nr)  zcompiler_collective %sr  c                      dddS )Ncompiler_collectivestringr  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   r  r0   z&Expect only one device type but got {}+r)  Tr   )group)r  rV  distributed_state
all_states
compile_pgr   infor,  r   r  r  _device_typesr  r  r  rE   r   rd  rankr  device_countrn   r   distall_gather_objectspeculation_logr  r2    CompileCollectiveRestartAnalysis)r   r  r3  device_typesr2  r.  s        @r   r  z#OutputGraph.run_compiler_collective	  s   \\: !;<<&&&B38MJHH-r~~>NN++ ; ,  &33L< A%$<CC.  ))9)9);<CCOO%(9(9(F(F(HH
+ 2$O	
+ *.1B(B
&&z2>>T *
+ 
+ $$&666C 9N3&
+ 
+ 
+ 
+s$   !F5/>F)-F5)F2	.F55F>rvr   c                    | j                   syddlm} |D ]%  }t        ||      r|j                  s|j                         j                  j                  j                  d      }t        |t        j                  j                  j                        st        dt        |             |j                  t!        |dfg      }|| j                   z  s|j"                  rt%        |j"                        n|j&                  j                  j(                  }|j*                  j,                  j/                  |       ( |j*                  j,                  r'd|j*                  _        t3        j4                  d      y)	aj  
        Validate that if torch.autograd.grad is used in the graph and outputs
        require grad, we trigger AutogradGradRestartAnalysis only if the output is connected
        to the autograd.grad computation.

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

        See Note [Tracing autograd.grad in dynamo]
        Nr0   TensorVariablerW  z/expected example_value to be a FakeTensor, got Tz3autograd.grad consumed grad_fns of returned tensorsrestart_reason)r  variables.tensorr@  r   requires_gradr/  r   rU  r  r   r  fake_tensorr!   rV  rW  r   r   r
  r   r   r:  r:  autograd_grad_leaked_tensorsr   graph_break_on_autograd_gradr2   AutogradGradRestartAnalysis)r   r=  r  r@  r  rE  reachable_grad_fnstensor_names           r   )_validate_outputs_safe_for_autograd_nodesz5OutputGraph._validate_outputs_safe_for_autograd_nodes$
  s?    334 	TCc>2#:K:K,,.--2266GKk5+<+<+H+H+S+ST$Ed;FWEXY  ""* "<k4=P<Q!R!D$H$HH14c#**oATAT""??FF{S%	T( ::>BB;11T  ;r   c                 |   ddl m} t               | j                  D ]E  }t	        ||      s|j
                  rj                  |j                         j                         G sy| j                  j                  D ]7  }|v rt        fd|j                  D              s'j                  |       9 |D ]  }t	        ||      s|j                  s|j                         j                  v s:d}|j                  rt        dd|dd	g
       Zd|j                   _        t%        j&                  d       y)a  Skip frame if a source-less requires_grad_() intermediate leaks as output.

        AOTAutograd's functionalization drops requires_grad_() on intermediates,
        so returning them (or tensors derived from them) produces wrong results.
        We detect this via FX graph reachability: find the requires_grad_() nodes
        for source-less intermediates, then check if any output is downstream.
        r0   r?  Nc              3   &   K   | ]  }|v  
 y wr   r   )r  inptainted_nodess     r   r  zHOutputGraph._check_requires_grad_intermediate_outputs.<locals>.<genexpr>i
  s     HC3-'Hs   aK  An intermediate tensor that had requires_grad_() called on it (or a tensor derived from it) is being returned from the compiled region. AOTAutograd's functionalization drops the requires_grad_() effect on graph outputs, producing wrong results. If you only need the tensor values without gradients, call .detach() before returning.z,returning intermediate with requires_grad_()z4graph output depends on source-less requires_grad_()zVIf you only need the tensor values without gradients, call .detach() before returning.zConsume the gradient inside the compiled function (call backward() and use .grad), or move requires_grad_() outside torch.compile.rK  Tz:source-less requires_grad_() intermediate leaked as outputrA  )rC  r@  r   r  r   r
  r   r/  r   r   r  anyall_input_nodesrD  r  rI   r:  graph_break_on_requires_grad_r2   RequiresGradRestartAnalysis)	r   r=  r  r@  r  r   r  r   rO  s	           @r   )_check_requires_grad_intermediate_outputsz5OutputGraph._check_requires_grad_intermediate_outputsP
  s5    	5 -0E-- 	5A!^,QXX!!!**,"3"34	5  JJ$$ 	(D}$H43G3GHH!!$'		(  	C3/%%LLN''=8Q  <<! N V$'?N	 HLB&&D99'c ;	r   r  c                    "#$%&' t         j                  j                  j                         5  ddlm}  j                  st        d       j                          t         j                        dk(  rt        |      dk(  rg dfcddd       S t        dd      }t        |t              st        d	t        |             t        |t               st        d
t        |              j#                  ||        j%                  ||        j'                  dd j(                  j+                  t-        d |D                    fi       } j/                         }|j1                  |        j(                  j3                  ||       t4        j6                  st9        d      5  t;        |      D ]L  }t=        ||      }	t        |	t>        j@                        s*tC        |	 jD                  | jF                         N  jI                          tC        t?        j@                  | j                         jD                  | jF                         ddd        jK                          t         j                        }
tL        d   dxx   |
z  cc<    jO                           jP                  jS                          tU        | j                        $ddl+m,}  |$        jZ                  r( jZ                  D ]  }t]        $|t=        ||               j^                  D ]
  } |$        ta        $jc                         d      % je                  d$jg                  ddd              jh                  |$_5         jl                  $_6         jn                  jq                         $jr                  d<    jt                  $jr                  d<   |$jr                  d<    jv                   jv                  $jr                  d<   tx        j{                  dt}        |$ddd             t         j~                  j                  d fd$fd        j                           j                  j                  }|t        d       | _D         jF                  s_ddlEmFc m} |j                  d!      5  t         j                  j                  |jD                  "      }ddd        j                  _C        $j                  j                           j                         5   j                  $ j                               #ddd       dd#lNmO} t        #|      s&t        t=        #d$d      |      rR#j                  d%k(  rCt        #|      r#n#j                  }|j                  |       t        #|      s|j                  # jF                  s|j                  j                           jh                   jh                  j                  |#        j                  r	#%%fd&}|# |#d'(      #tL        d   d)xx   dz  cc<   |jD                  t        d*      |jD                  j                  x}r5g 'i & j                  D cg c]  }|j                   }}|D ]  }|j                  |j                        }t        j                  |j                        j                         }t               }t        j                  ||j                  |gd      }t        j{                  d+|       'j                  t        j                  |fd,|      |f        t         j                  j                  d'(      d-t        d.t        d/t        f#$ &'fd0       } j                  ||       n j                  |#        j                  t        d1      t         j                        "t               r"j                  "fd2       g }t        j                         D ]<  } |"        j                         }"j                  |       |j                  |       > |D ]"  }"j                  "j                  |             $ "j                  t        t              d       "j                          t         j                        D ](  \  } }!|!j                   j                  j                  | <   * "j                  |       "j                         "j                         fcddd       S # 1 sw Y   xY w# 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)3a  
        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

        Returns a tuple of (instructions, pycode) where pycode is a string
        representation of the generated Python code if generate_pycode_prologue
        is enabled, otherwise None.
        r0   r  z2should_exit must be True before call_user_compilerr   N__compiled_fnT)	with_uuidzrv must be a list, got z#root must be a FakeRootModule, got r  c              3   <   K   | ]  }|j                           y wr   )r/  r  s     r   r  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>
  s     5Oqajjl5Or  r-   )rQ  statscalls_captured)dce_hop_extra_outputsz8Graph contains named parameters due to static addresses.Fprint_outputinclude_strideinclude_devicer   r  r  
backend_idr  %s)r^  r_  coloreddynamo_output_graphc                  (    d j                         iS )Nsizes)r  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_readabler`  s   r   r|  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    2#4#4!&tD $5 $ r   )r  zold_fake_mode must not be Noner  )rX  )_LazyGraphModule__self___lazy_forwardc                  |    t         j                  j                         5   | i |cd d d        S # 1 sw Y   y xY wr   )r   re  DisableTorchFunctionSubclass)r  r  real_compiled_fns     r   _tf_disabled_wrapperzCOutputGraph.compile_and_call_fx_graph.<locals>._tf_disabled_wrapperk  s9    >>@ A/@@A A As   2;z"do not trace Dynamo-compiled graphr  unique_graphsz(old_fake_mode.shape_env must not be Nonez: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)	rX  patch_source_specializationr
  rr  rU  r   r   r  call_user_compiler)
r  r  rr  rt  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	r)  c                  l     j                  t        j                  j                  j                  d      S )Nstore_user_object_weakrefs)rz  r   rf  graph_bytecode_inputsr   )r{  s   r   r|  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s(    B//;;DD4 r   )|r   rO  r   clear_framer  r  r  rV  r  rl   r   r  rA   r   r   rW  r  rT  rK  r  r  
create_argr   
dedup_passr  _maybe_preserve_original_metar1   do_not_emit_runtime_assertsrn   dirr   r   r-  r-   rX  rQ  remove_unused_get_attr_nodesremove_unused_graphargsrm   &remove_tensorify_specialized_graphargsr  r  r$   dce_extra_outputsr[  r  r  r  r  
parametersr  rh  r  _backend_idr  r  r!  rU  r  r  graph_code_logr  rw   r  r  r  r  r  _old_fake_moder  ra  r  r  r  lintr&  rv  r   torch.fx._lazy_graph_moduleri  r   rj  force_recompiler$  fake_tensor_converterclear_non_cpu_constantsadd_backend_idr  specializationsrZ  r
  r\  inspect	getsourcerr  stripRootGuardManagerr   LAMBDA_GUARDr   r   r  r  rf  r	   install_global_unsafer  rC   rK   r  rL   rW  rV  r  r  r  r  pop_topr  r  ry  make_call_generated_coder  r  )(r   r  r=  r  r  r:  output_nodesub_gmsattrsubgraphncallsr[  subgraph_nameregister_finalizerold_fake_moder  backend_fake_moderi  lazy_gmro  r  asourcesrt  source_indexcheck_fn_sourceunused_root_guard_managerrr  rz  tmp_varsconstructorvar_namer  rd  r{  rw  r   rn  rx  ry  s(   `                                 @@@@@@r   r  z%OutputGraph.compile_and_call_fx_graph
  s   " ]]))557 {	:+##$H  ((*4::&!+B14x{	: {	: _=Db$'$'>tBxj%IJJdN3$'J4PT:,%WXX
 ::2rB ::2rB**$$//5OB5O0OPR	K oo'G(==b+N55 ""CD  #D	 #*4#6%h?; ( $ $'+{{	 5573tTZZ8#{{	& ((* ,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 ##N((%*4PT )  $  ||'!% *.)E)EB& 55::< GG67 ,0+A+ABGG'($(BGGL!((4262K2K./  &"T$PT NN++%D ,  ##% 00::M$$%EFF"/D;;99]]E]R  ).(9(9(H(H"/"9"9 )I )%& 2C$$.HHMMO**, Q"55b$:M:M:OPQ E+'787;
DACST((O; "+/?@  $--  !009!+/?@")//K;;
 33KKM||'++D+> 33#. A 3!$HK Wo.!3.&&.$%OPP"/"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 !" !-	 +
#: &&.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 ''-&&("--/9w	{	: {	:Z v ,Q Qx =u{	: {	:sr   AeDe&5d+A6d+I e2+d8Ae !eEeeJe+d5	0e8e	=ee	
ee c                 :    | j                   j                  d      S )Nr  r  )r   
find_nodesr   s    r   placeholderszOutputGraph.placeholders  s    zz$$$66r   c                 Z    | j                   D cg c]  }|j                  d    c}S c c}w NrR  )r  rU  r   r   s     r   rZ  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)rn   _call_user_compiler)r   r   r   s      r   rv  zOutputGraph.call_user_compiler  sI     ,("& &<%N
 	@ ++B?	@ 	@ 	@s   .7c                    | j                   t        d      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                   t"        j$                        xs | j                   }t'        | j                   t"        j(                        }	|	rt+        ||	      }t        |d      r|j,                  nd	}
dd
lm}m} t"        j0                  r	 ||      }nt"        j2                  r ||      }	  t5               t6        j8                  d|
        t"        j:                  rt=        |      } |||      } t5               t6        j8                  d|
        t?        |      st        d      	 tc        ddi | jd                  |tg        |j                  j                        tg        |      d       S # t@        tB        tD        f$ r  tF        $ r}| jH                  rCtK        | j                   |tM        jN                               jQ                  |jR                        d tU        || jV                  jX                  dd|
 dt[        |       d| jV                  j]                          d|
 dt[        |       ddg       Y d }~d }~wt^        $ r  t`        $ rH}tK        | j                   |tM        jN                               jQ                  |jR                        d d }~ww xY w)Nzcompiler_fn must not be Noner   r  call_methodcall_moduler0   r  _dynamo_sourcerR  r   z<unknown compiler_fn>)inline_invoke_subgraph!inline_single_use_invoke_subgraphzcalling compiler function zdone compiler function z#compiler_fn did not return callablezBackend compiler exceptionz	Backend: z
Exception:z
Traceback:
zBackend compiler `z` failed with z. Adding a graph break.z-Report an issue to the backend compiler repo.rK  dynamor  )op_count
node_countinput_count)4r   rV  r   r  r  r   ru   r  rU  r
  r  r  _param_name_to_sourcer  _source_to_user_stacksrN   r  r1   debug_backend_overriderO   debug_inductor_config_overrider   r   5torch._higher_order_ops.passes.inline_invoke_subgraphr  r  r   r3   INFOr%  r  callabler   r   
IndexErrorrG   r  rF   r  currentframewith_traceback__traceback__rJ   r  r  r   format_frame_summaryrH   r'  r"   r  r  )r   r   r   totr  r   plrd  r   inductor_config_overrider:  r  r  rw  r  s                  r   r  zOutputGraph._call_user_compiler  sM    # !?@@HHNN 	*DwwIIqww-'##D)		*
 	3 	/B2/0ggj) %(JJ!	/ $(#<#< $($>$>! 0&&(E(E   	 	 $O""F$I$I$
  $45K {J/   ( 	
	

 (('+B55226B"	8LN7<<+EdV)LM((,[9%b.9KLN7<<+B4&)IJK($%JKK )< 	,..!"((..1"<0			
 Q /0@*M 	 0 	55+$$a)=)=)? .1t< '##4#D6c!fX^DLLLmLmLoKpq0nSVHLcdC	 	  	  	8'  !W%9%9%;nQ__-48	8s'   A>I
 
M9'B/LM91AM44M9c                 d    t         j                  j                  j                  rt	        |       S i S r   )r   rf  r1   use_graph_deduplicationrM   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_suffixFrQ  )rs   r	  r   torchdynamo_force_dynamicrO  )r   r:  r  	next_names       r   ro  zOutputGraph.install_subgraphc  s@    'dootT	#+0( 	$$VYt$Dr   c                 V    | j                   D cg c]  }|j                   }}|S c c}w r   )rZ  example)r   rd  r+  s      r   r   zOutputGraph.example_inputsl  s'    )-8##++88 9s   &c                     t        | j                  j                  d      d      D ]5  }t        t	        |j
                              dk(  s%| j                  |       7 y )Nr  r  T)reverser   )r  r   r  r  r   usersr  r  s     r   r  z(OutputGraph.remove_unused_get_attr_nodesq  sN    4::00J0?N 	'D4

#$)  &	'r   c                 $     j                   st        d      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.        j0                  d z  fd}dt        j                  dd f fd}t3               dt2        t.        j0                     dt&        j4                  t&        j6                  z  dd fdg } j8                  D ]  } ||      d u}|r|j                  r|j;                  |       /|j                  s&t=        |j>                  d   t@              s	 ||       a|j>                  d   }	t=        |	t@              rt=        |j>                  d   jB                  t&        jD                        r|j>                  d   jB                  }
|j>                  d   jF                  }t&        jH                  jJ                  jM                  |
      sotO        |
jQ                               }|D ]Q  }tS        |jT                  |      }tW        jX                  t&        j4                  t&        j6                  ffd|       S ~t[        t]        |j>                  d   jB                              r|	j^                  |	j^                  n|	jB                  } |        |D ]+  } ||      }||vr	 ||       ja                  |       - y )Nz(should_exit must be True before finalize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)NTFrW  )	r   r   NoderU  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   SymTypesTrW  F)
torch.fx.experimental.sym_noder  r   r   r  r   r   r  rU  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  FrW  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  r  r   rU  r  r  r  r  rW  )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_noder  r  c                     | j                   d   }|j                  }t        |t        j                        rDt        |j
                  j                  t        j                        r|j
                  j                  S y r  )	rU  r  r   r   rK  r   exprsympySymbol)r   rd  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 %srR  )r   r  rU  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   rR  c                      |       S r   r   )tr  r   s    r   r|  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>  s    .A,PQ.R r   )1r  rV  r   r   Argumentr   r  %torch.fx.experimental.symbolic_shapesr  r  r   r   r  r  r  r  r#  operatorgetitemr   _checkr  r  r  r  r   rK  r   r  r   r   rU  r|   r  ScriptObjectexample_strong_refr8  r9  tracing_with_realr  __obj_flatten__r   wrapped_objpytreetree_map_onlyr    rW  rE  remove)r   r  r  r  r   r  r  recheck_placeholdersbinds_symbolrd  real_script_objr=  	flat_dictr  fake_attr_valr  symbolr  r  r   s   `                @@@r   r  z#OutputGraph.remove_unused_graphargsv  sh     !KLL	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 	u||d7J 		2 	2D 	2 +.%	/ell+	/38<<%,,3N	/	/
  "%% &	<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   )TensorifyStaterW  r:  c              3   :   K   | ]  }|j                   d k(    yw)itemN)r#  )r  us     r   r  zEOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>(  s     ?qF*?s   )torch._dynamo.symbolic_convertr  r   r  rU  r  r   r!   	item_memor  r   _exprr  r  should_specializer:  r   replace_all_uses_withr'   r  )r   r  r   rW  r  s        r   r  z2OutputGraph.remove_tensorify_specialized_graphargs  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   r  c                     | j                   j                  |       t        |t        t	        d      f      st        d      | j                  j                  |       d| _        y)a>  
        We call this on the creation of a new compiled subgraph that is inserted
        before user code.
        An optional pycode argument can be passed to add python codegen along with
        the bytecode. The generated python code should match the bytecode behavior
        from the `prefix` argument.
        Nz,pycode must be a list of python code or NoneT)	r  r  r   r   rW  rV  r  r   r  )r   rF  r  s      r   r  z#OutputGraph.add_output_instructions3  sS     	  ''/&4d"45 !OPP!!&)r   r(  r  c                     j                   rDdt        t        j                  df   dt        j                  ffd}| j                  |       y| j                  t        j                               y)aa  Install a resume function as a global.

        When the code has freevars, installs a factory that creates the
        function with correct globals and closure (since MAKE_FUNCTION
        inherits the current frame's globals, which is wrong for resume
        functions from inlined frames). Otherwise installs the function
        directly.
        closure.r   c                 6    t        j                  d |       S r   )typesFunctionType)r%  r(  r  r:  s    r   _make_fnz<OutputGraph.install_resume_function_global.<locals>._make_fnS  s     ))$	4wOOr   N)r   r   r'  CellTyper(  r  )r   r:  r(  r  r)  s    ``` r   install_resume_function_globalz*OutputGraph.install_resume_function_globalC  so     Pu~~s23P##P
 &&tX6&&""4D9r   c                     || j                   v rt        d| d      | 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.
        zglobal 'z' is already installedN)r  rV  r   r  r   ri   r  rG  )r   r:  r   s      r   r  z!OutputGraph.install_global_unsafe_  sb     4))) 8D61G!HII""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)idr  r  r  r   rF  r   r:  s       r   install_global_by_idz OutputGraph.install_global_by_idl  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.
        )rA   r  r0  s       r   rD  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                          t        | j                               | _        | j                  j                          | j                  j                          | j                  j                          | j                   j                          | j"                  j                          | j$                  j                          | j&                  j                          | j(                  j                          | j*                  j                          | j,                  j                          | j.                  j                          | j0                  j                          y r  )r  r	  r  r  r  r   r  rU  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  s     r   cleanupzOutputGraph.cleanup  s    //557$(!JJ$$ 	*DTYY&IIj)	* 	##%  &&(8=5579
5 	!##))+##%$$**,""$##))+--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k7  rt        d|j                    d      | j                  |j
                     S )z#Extract the non-fake example tensorr  rR  r  z(expected node.op to be 'get_attr', got 'rj  )r  rU  r  rV  r	  r#  r  s     r   example_value_from_input_nodez)OutputGraph.example_value_from_input_node  s\    77m#99Z(00077j  #KDGG9TU!VWWt{{++r   inlined_modulec                 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                   t        d      j                  |       } d|  }|j                   |<   t        t              r-| j
                  t        j                  |j                        <   y y rB  rC  rD  s      r   rG  zHOutputGraph.add_fqn_info_for_inlined_modules.<locals>.register_leaf_name  s    ((0$%LMM==fiPJq,H2<D%%h/&+.  55//
@ /r   rH  rI  )r  r  r:  rs   r  rG  r   r   r  r  rM  __func__!og_module_named_parameters_fn_ptrrN  og_module_named_buffers_fn_ptr)r   r9  r
  rG  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   )rE  r   )r   r  )r  r   r   N)r   r  )r   r   )tmp)r   r  )r   r   r   r   rU   r   r   r6   r   r   r#   r	   r^  r   r   r   r_  r`  r   r  r   r  r   rX   r  r  r)  rA  r  r   r   r   ProxyrI  rH  r  r  r   r  r  r  rj  rS  r  r  r  r  r   r  Graphr   setterr  r  r  r   r  r  r  r   r  r  r  r  r  contextmanagerr,   r   r  r  r  r  r  r)   rX  rO  rh  r   r	  ri  r[  r  r  r  r   r  rl   r
  r  r  r  r  rV  r  staticmethodr  r"  rO  r@   ri  rl  r  r   r  rC   r  r  r  r  r  r   r&  r  rK  rT  r  r  r  r}   rZ  r-  r5   rv  r  r  ro  r   r  r  r  r  r'  r+  r  r1  rD  r4  r6  r8  r?  r   r   r   r  r    s
      \U!\U  $&\U -	\U
 \U %%67\U \U \U \U \U $((I(I#J\U *+\U \U 
\U~ 389V+/9V	c9VvF # * >>"%>	>* 8DD3 D
 4:	5#	5-0	5	sEHHNN"	#	5)%((.. )*<!:T#Y=M !:N >B	$S#X$ CHo$  s 23d:	$
 
$L&8BG#4 & &#
      & & - 	 F	I  C  D  . )uxx~~ ) ) \\*588>> *d * * 7T#rxx-%8 7 7 4$rww'<"= 4 4 1tELL%8S2T$TU 1 1A# A A A@ @ @ @@ @ @ @ 
 #'	} /0 4Z	
 
/	0 @   .5,,;; . .
 88 8 8 A// A A >DcN > > FD)C)C$D F F HL'
U8CH#5t#;<<=D'
	'
R$& N N
6 
c 
'S '0 0JT J	# 	%((//C*? 	C C 5C 5D 5
    .,/	&p%,,.4p p 	p
 
pd]/-]/	tK $vv~"66	7]/~k"-k";>k"	tO$&99	:k"b 	E&'E& #E& 	E&
 
!	"E&NF ; F Ft F8&G'&G ?+&G 	&G
 &G 
&GP1>S   4Dd39o1E,F C C 2 c  "%7N*()*/J*	*X>()>/J>	>@L:'L: !L: 	L:
 
tK $s)d"22	3L:\
 7d277m 7 7 E4> E E@..@26v,@	@h..h26v,h	hTDehh&:&:!:; S %((2F2F 3 U\\ 2 
'
Y0v 'F EI ;' 15cT1A 	   nn S>	
 
8Q# Qc Qd Q3 s s 	S 	 	 	$:?"*BNN+;T+A"B?	?
,%((-- ,C ,%2#hhoo%27=%2	%2r   r  c                       e Zd ZU eed<   eed<   edz  ed<   edz  ed<   eedf   dz  ed<   ee	ef   ed<   	 dd	d
dedz  ddfdZ
ddZy)DynamoTracerOutputerror_on_graph_breakis_tracing_resume_prologueNoutput_graphoutput_graph_for_cleanup.r%  r  r  r   r  r   c                     |j                   | _         |j                  | _        |j                  | _        |j                  | _        |j                  | _        |rd | _        y |j                  | _        y r   )rI  rJ  r%  r  r  rL  rK  )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   )rL  r  r   _clear_nodesr  r  rX  r  )r   rK  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  r   r	   r  r   r  rP  r   r   r   rH  rH    su     $$$$ *D0038_t##CH~ HL.1.:=*.	.
Vr   rH  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.htmlrK  r   r#  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  r#  r   c                 Z    | j                   dv ry j                  j                  |        y )N>   atenprimprims)	namespacer  r   )r#  rK  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 opr   rK  )r  r   r1   only_allow_pt2_compliant_opsrI   err_epilogue)r#  r   rK  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.r0   r   z:Encountered the non-overloaded torch.ops.OpOverloadPacket z that is not PT2 compliant. Fz*Error when attempting to resolve op packetr  rK  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r   _ops
OpOverloadr   r   Tagpt2_compliant_tagtagsOpOverloadPacketr   	overloadsr  r   rf  r}  get_fake_values_from_nodesr   re  _jit_resolve_packet_qualified_op_namer&  rI   )rK  r   r#  r  r  rW  r\  rc  r  overloadr  s   `          r   check_pt2_compliant_oprh    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;* =%&I 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  r  r  r  r  r   Nc                 <    || _         || _        || _        || _        y r   )r  r  r  r  )r   r  r  r  r  s        r   r  zLazyProxy.__init__W  s      	r   c                 N     | j                   | j                  i | j                  S r   )r  r  r  r   s    r   r,  zLazyProxy.__call__d  s     tww		1T[[11r   )r   r   r   r   ri  rj  r  r  r  r	   r,  r   r   r   r  r  V  sS      QTN vv	
 (( 
2# 2r   r  c                       e Zd ZdZ	 	 	 	 d,ddded    dededz  dedz  d	df fd
Zde	d	dfdZ
dddej                  d	dfdZ	 	 	 d-dedededededz  dedz  deej                  gej                   f   dz  d	ej                   fdZ	 	 	 d-dedededededz  dedz  deej                  gej                   f   dz  d	ej                   f fdZ	 	 	 	 d.dedededededz  dedz  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dz  d	ej                   fdZd ej                   d	eej                   z  fd!Zd"ed	efd#Zded$eej                  j                   z  d	dfd%Zdej8                  ej:                  z  d&edz  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 )0r  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.
    NrK  r  r  r  r  r  r   c                 Z   t         |           t        j                  |      | _        t
        j                  j                         | _        || _	        i | _
        i | _        || _        || _        || _        i | _        i | _        i | _        d | _        d| _        d| _        d| _        d | _        t/               | _        d| _        ||j4                  dz   nd| _        d | _        d | _        d | _        d | _        | j                  g | _        n[|| j                  j>                  d d  | _        n:| j                  j>                  | j                  jA                  |      |fgz   | _        t/               | _!        g | _"        t        jF                         rtI        d      t/               | _%        y )NFr0   r   zSInference mode is supposed to be disabled during compilation. Please open an issue.)&r  r  weakrefr   rK  r   r   rC  r   r  r  r  r  r  r  lifted_freevarsr  dynamic_scalar_nodes	prev_inst,unsafe_allow_externally_visible_side_effects+traced_with_externally_visible_side_effectsallow_side_effects_in_hopside_effect_stackr.   traced_sourcesis_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str_used_namesr  is_inference_mode_enabledr&  tracked_proxyable_vt)r   rK  r  r  r  r  r  s         r   r  zSubgraphTracer.__init__p  s    	#MM,7XX^^%
 # 9; =? *& :< NP >@!
 =B9;@8 */& AE 3=, ,1(:@:L 2 2Q 6RS/3AE 04!
 ;;.0D "#';;#>#>q#AD #';;#>#>**=9=IB $D 
 -7L 8:)**,e  BL!r   r  c                 :    | j                   j                  |       y r   )r  r   )r   r  s     r   record_proxyable_vtz"SubgraphTracer.record_proxyable_vt  s    !!%%b)r   r  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   r   _COPY_META_FIELDSrU  )r   r  r   linenonode_idxrU  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   r#  r  r  r:  	type_exprproxy_factory_fnc           	      \   t        j                         }	 | j                  |||||||      | j                  j                  xj
                  t        j                         |z
  z  c_        S # | j                  j                  xj
                  t        j                         |z
  z  c_        w xY wr   )r&  r'  _create_proxyrK  r  r1  )	r   r   r#  r  r  r:  r  r  _t0s	            r   r  zSubgraphTracer.create_proxy  s     lln	%%fdFD)=M 66FF$FD66FF$Fs   A+ +A B+c           	      >   | 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 r  )rp   rstripr   r:  )linecur_instheaderr=  tx_codes    r   get_trace_call_log_strz<SubgraphTracer._create_proxy.<locals>.get_trace_call_log_strW  s9    5gxHOOQD+BGGLL>xr$PPr   rb  Forig_graphmodulec                       y r   r   r   r   r   r|  z.SubgraphTracer._create_proxy.<locals>.<lambda>b  s    r   Tnn_module_stack>   r  r  r  r0   )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.rK  c              3   \   K   | ]#  \  }\  }}|j                  d       d   k(  r| % yw)@r   N)r  )r  r  r   tyr#  s       r   r  z/SubgraphTracer._create_proxy.<locals>.<genexpr>  s5      &Aw2773<?f4 s   ),z2Invoking an nn.Module inside a HigherOrderOperatorr  r  r  )Hr  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr  r  rK  r   rz  r{  r  rs  	positionsr  r  get_line_of_code_headerr   trace_call_logr  rx   r{  rB   get_contextr  r   r   r   r-  r   r  rU  r|  _lineno_mapr}  r$  __code__r   r~  r  r!  r   r:  r   rW  r   
startswithr  rI   r  r  r  r  is_co_filename_from_nn_modulesframe_summaryr   r-  r+   r  r   StackSummary	from_listr  r  r  rf  r1   r  track_nodes_for_deduplicationr  
track_node)r   r   r#  r  r  r:  r  r  	flat_args	tree_specnew_flat_argsrd  maybe_new_argr  r  is_retracingorig_graphmodule_maybendr  r   current_nn_moduleframe_summariesframefiltered_frame_summariesmsgsr  r  r=  r  r  s     `                      @@@@r   r  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 	[&r(s   #Z	Zc                    t        | j                  ||||       | j                  et        j                  |i |}|D ]K  }t        |t        j                  j                        s(|j                  | j                  k7  sBt        d       t        
| 1  ||||||      }	| j                  j                  |	j                  d<   | j                  j!                  |	j"                         |	S )Nz2create_node using arg not from this SubgraphTracerr  )rh  rK  r  r  arg_tree_leavesr   r   r   r  r   rV  r  r  r  rU  r  r   r:  )r   r   r#  r  r  r:  r  r  rd  r   r  s             r   r  zSubgraphTracer.create_node  s     	t00$fM;;"..??I  !#uxx}}599

*(L 	 w"4vtYO*.*;*;*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   r  r  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   rW  beforer
  c                 B   t        |t        j                        r%| j                  j	                  |j
                         t        j                  d|||j                  nd|| j                  |       || j                  t        d| d| d      | j                  rm| j                  a|t        d      t        |d      sG| j                  j                  j!                  |g       j	                  t#        j$                                t'        || j(                        }| j*                  rqt-        t/        | j*                              }| j*                  |   j0                  }|r| j2                  j5                  |      }n7| j2                  j7                  |      }n| j2                  j5                  d       }|5  | j9                  d	|d
i |      }	t;        |	j0                  |       | j*                  r>|r<| j*                  j=                         \  }
}|	| j*                  |<   || j*                  |
<   n|	| j*                  |<   | j(                  j?                  |       | j                  }t        j@                  jC                         }|s|st        |t        j                        r| jE                  ||       nht        |tF        tH        f      rRtK        |      D ]D  \  }}t        |t        j                        s!d }|rtM        ||d      }| jE                  ||       F t        |t        jN                        rQt        |j0                  jP                  tR        jT                        r#|	| jV                  |j0                  jP                  <   |	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 tracerz5source must not be None for export root tracer inputsT)only_allow_inputr  r   r  F)r  r\  index_is_slice),r   r   r   r  r   r  r   r  r:  rz  r  rV  r  r_   rK  r  r<  r   r  rs   r  r  r  r  r   r   inserting_beforeinserting_afterr  r{   popitemr   compileris_exporting_lift_basic_symbolsr   r   r  r\   rK  r  r  r  r  )r   r:  r  rW  r  r
  	prev_namer   ctxr   r  r  is_strict_exportis_non_strict_exportr}  r  e_sources                    r   rT  z!SubgraphTracer.create_graph_input  s"    mU\\2--44]5K5KL		E!-FKK8	
 >{{"$FtfMZgYhh{|  >>dkk1~$K  (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 I	%%mT2rY%WEejj-8''F//779116((../((+16((.
   &@  $~~ $)>>#>#>#@ #,@mU\\:,,]FCe}= )- 8 >1)!U\\:$#'!'4%+&'/4(H 00H=> -6:""''< ?D""=#5#5#:#:;SI	 I	 I	s   ;GNNr   c                 v   | j                   t        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                  |       t	        |t
        j                        r?t        |      r4|j                  | j                   u r| j                   j                  ||       |j                  j                  d   }t	        |t              rt!        |j"                        n
t!        |      }| j%                  |j                  j&                  ||      }|| j                  |<   |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerrW  )r  rV  r   rU  r   r   rK  r  r  rq  r  lift_tracked_freevar_to_inputr   r/   track_produced_symintsr   rW  real_objrT  r:  )r   r   rW  r  	new_proxys        r   r  z,SubgraphTracer.lift_tracked_freevar_to_input  sj    ;; [  

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

8 -)9: ''(m$ 	
 ++EJJOOYV	&/U#r   rd  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   rB  slicestartstopstepr  r  )r   rd  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t        dt	                     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+                  ||       |j0                  t        j2                  u rt        |j5                               D ]  \  }} ||      st        j                  d||j                         t!        ||d
t        j"                  j$                  j6                  j(                  |fi t	        |            } j+                  ||        n6|j0                  t        j8                  u rA j+                  |j;                                 j+                  |j=                                n|j0                  t        j>                  t        j@                  hv rA j+                  |jC                                 j+                  |jE                                nl|j0                  t        jF                  t        jH                  hv r@ j+                  |jK                                 j+                  |jM                                tO        |      rC|jQ                         \  }	}
|	D ]*  }tS        ||      } j+                  |tS        |             , y y t        |t        jT                        r/ ||      r&|jV                  jX                  }jZ                  |<   y y y )Nz%tracer must be a SubgraphTracer, got r  r   c                     ddl m}  ||       xrR t        | j                  j                  t
        j                        xr" | j                  j                  j                  vS )Nr   )r(   )r  r(   r   r   r  r  r  r  )r  r(   r   s     r   	need_bindz8SubgraphTracer.track_produced_symints.<locals>.need_bind  sJ    I A :qvv{{ELL9:FFKKt'9'99r   rW  r  r  c                 v   t        t              r        nt        t        j                  j                        st        dt                     j                  j                  j                        5   j                  |i |}t        |j                  |        |cd d d        S # 1 sw Y   y xY w)Nz!e_proxy must be an fx.Proxy, got )r   r  r   r   rB  rV  rW  r   r  r   r  r{   )rW  r  r  r   r  r  s       r   _proxy_with_example_valuezHSubgraphTracer.track_produced_symints.<locals>._proxy_with_example_value  s    
 $.gy#AgiwGguxx~~6$7WG  --gll; +++T<V<!%**m<  s   ;*B//B8z=track_produced_symints %s for %s.size()[%s] at debug_level %sr  r  zCtrack_produced_symints %s for %s.storage_offset() at debug_level %sz?track_produced_symints %s for %s.stride()[%s] at debug_level %s).r  r   r  rV  rW  r	   r   r   rB  r   r   r  r   r   r  rz  r  opsrS  sym_sizer   r  storage_offsetsym_storage_offsetlayoutstridedr   
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr/   __tensor_flatten__r   rK  r   r  r  )r   rW  r  r  r  r}  r  
lazy_proxyr  attrsr  r  inner_tr  r  s   ` `           @r   r  z%SubgraphTracer.track_produced_symints  s     &.1 #Hf!WXX	 	 			'*	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   r  c           	          	 ddt         t        j                  z  dt        d z  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        j,                  t        j.                  hv rA j'                  |j1                         |        j'                  |j3                         |       nl|j                  t        j4                  t        j6                  hv r@ j'                  |j9                         |        j'                  |j;                         |       t=        |      rG|j?                         \  }}|D ].  }tA        ||      }	 j'                  |	|tC        ||      nd        0 y y t        |t        j                        r
 |||       y y )	Nr  r
  r  r   c                 @   t        |       sy t        | t        j                        st	        dt        |              j                  |       }t        |      dk(  ry j                  j                  j                  | |       |D ]  }j                  j                  |   }|j                  j                  d   }t        |t        j                        st	        dt        |             j                  t        |      t        |      |||      }t        j!                  d|||j"                  ndj$                         |j&                  |<    y t        |      dk7  rt	        d	| d
|        |t	        d|  d|  d      t)        t+        |            }j                  t        |      t        |       | ||      }t        j!                  d| ||j"                  ndj$                         t-        || dd d      |j                  j                  d<   y )Nzs must be a SymInt, got r   rW  z"example_val must be a SymInt, got )r  r
  z4_lift_symbols_in_symint %s from %s at debug_level %szsubgraph inputsr0   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_tensorrE  r  rR  )r(   r   r   rK  rV  rW  lookup_unbound_symbolsr  r  r  r  r   rU  rT  r   r   r  r:  rz  rq  r  iterr}   )	r  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_symintg  sA   
 q>a.$'?Qy%IJJ#::1=#$) {{&//6:* <B#';;#<#<R#@L"."3"3"8"8"IK%k5<<@,@kAR@ST  00B[)#%% 1 B IIN'-'9?P((	 :<D((6)<. '(A-(LL\K]]abcadf  >(%aS )[[\Z]]np 
 $/01,,GG!! -  		J#)#5FKK;L$$	 ,4', $#,Z(r   T)r  r@  )"r   r   rK  r   r   r   r   r  r   rf   re   SIZEr  r  r   STRIDEr  STORAGE_OFFSETr  r  r  r  r  r  r  r  r  r  r  r  r/   r  r   rX   )
r   rW  r  r  r}  r  r  r  r  r  s
   `         r   r  z"SubgraphTracer._lift_basic_symbols`  s    !H	U\\!H	TMH	 H	 	H	T 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   r  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st        d| d       t        |d       S )Nr   zThe proxy of symbol z" doesn't belong to current tracer.c                     | j                   S r   )r:  )r  s    r   r|  z7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>  s
     r   )rB  )r   r  r&   r  r  r   r   r  r   r   rB  r  rV  r  )r   r  r&   to_be_boundr   r   s         r   r  z%SubgraphTracer.lookup_unbound_symbols  s    vv{{//|!I 	B+++""2&&&r*E%+).""2&eUXX^^4D8P$*2$.PQ 	 k'788r   c                 n   | j                   }g }g g }t        | j                  j                        D ]}  \  }}|j                  dk(  rh|j
                  d   }t        |t        j                        s?|j                  |j                         |j                  |       j                  |       } n t        t        ||            D 	cg c]  \  }\  }}	||	k7  r| }
}}}	|
r8|
D cg c]  }||   	 }}t        fd|
D              }d| }t        d||      S t        ddd      S c c}	}}w c c}w )	Nr  rW  c              3   (   K   | ]	  }|     y wr   r   )r  r}  tensor_placeholder_indicess     r   r  z4SubgraphTracer.has_input_mutation.<locals>.<genexpr>  s      *23*1-*s   zInput mutation detected at TFr  r   )r  r  r   r  r  rU  r   r   r   r   r  r  r   r   )r   input_versions_at_beginninginput_nodesinput_versions_at_endplaceholder_idxr   rW  r}  v1v2mutated_inputsmutated_nodesr   r   r  s                 @r   has_input_mutationz!SubgraphTracer.has_input_mutation  sP   &*&G&G#%'" "%.tzz/?/?%@ 	!OTww-' $		/ :mU\\:)001G1GH&&t,.55oF	  )/1FG 
 
8B Rx	 
 
 5CD[^DMD$) *7E* %! 0?Cc+@AAE2r**!
 Es   D+*D2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 ]x  }
|
s ||
g      d   }t        |t               rt#        d
      t        |t        j                        sH ||      D ]&  }||v rd||    d|
 }t        d|      c c S |
||<   ( z |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 example_value must not be a listz,Output-to-output aliasing detected at nodes z, z+Input-to-output aliasing detected at nodes Fr  )(torch._dynamo.variables.higher_order_opsr  torch._higher_order_ops.utilsr  r  r   r  r  r   r   r   r   r  r  tree_leavesr  r   rV  r	  r  r  )r   r  r  input_storagesr   rW  storager   output_storages	out_nodesout_nodeintersected_storagesr  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2()KLL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-7G2
)NFNN)NNN)NNNN)FN)(r   r   r   r   r   r   r,   r   r  r   r  r   r  r  r	   r   rB  r  r  r  r  r   rT  r  r  r  r   r  rK  r   r  r   r  r  r  r   r  r   r   r  r  s   @r   r  r  h  s[    .2'+"&gN#gN )*gN 	gN
 }gN 4ZgN 
gNR*o *$ *C-C57WWC	C8   $AE  	
  Dj : #BGG9bhh#67$> 
4   $AELL L 	L
 L DjL :L #BGG9bhh#67$>L 
Ld  $  	
  Dj : 
66 6D 66  $AA A 	A
 A A 
AH4288 4	BHH@T 4l7s 7s 7@}5 }5+4uxx~~+E}5	}5@D"\\ELL8D?E}D	DP9 9ell9K 9, +L  +D,'l ,'r   r  r   (  r   r  r  r!  r8  r  r  r  r3   r  r  rz  r&  r   r'  r  rp  collections.abcr   r   r   r   r   rv  r   typingr	   r
   r   r   r   r   r   r   typing_extensionsr   r   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedr8  torch.nntorch.utils._pytreer}  r~  r  r   r   torch._C._dynamor   torch._dynamo.excr   r   r   r   r   r   r   r   "torch._library.fake_class_registryr   torch._library.opaque_objectr    torch._subclasses.fake_tensorr!   torch._utils_internalr"   torch.export.dynamic_shapesr#   r  r$   %torch.fx.experimental._backward_stater%   r  r&   r'   r(   r)   r*   r+   torch.fx.noder,   torch.fx.passes.runtime_assertr-   torch.utils._ordered_setr.   torch.utils._python_dispatchr/   r  r1   r2   r   r4   backends.registryr5   r6   r  r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   r  rC   current_scope_idrD   device_interfacerE   rF   rG   rH   rI   rJ   r}  rK   rL   graph_deduplicationrM   graph_id_filterrN   rO   graph_region_trackerrP   rQ   rR   mutation_guardrS   r  rT   rU   rV   r
  rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   variables.builderr|   r}   r~   r   variables.ctx_managerr   variables.functionsr   r   variables.listsr   variables.miscr   variables.nn_moduler   rC  r   r   r   variables.torch_functionr   variables.user_definedr   r$  r   torch._dynamo.packager   r  r   torch._inductorr    torch.multiprocessing.reductionsr   	getLoggerr   r   r  getArtifactLoggergraph_tabular_logr  graph_sizes_logr  r  r   rK  TensorMetadataDimr   r   r   r   r   r  r  rN  r>  rM  r=  r  r   r   r   r   r   r   r   r  r   r  r   cacher   r   r  r  r  r   r^  r/  rE  rl  rx  r  r  r  rH  r[  rh  r  r  ri  rj  r  Tracerr  r   r   r   <module>rS     s	  *          	 
      9 9 4 	 	 	 > =       $ $  # N  @ 7 4 0 9 : ?  ! J / F D D 5    '  - 6  S : 5 / 0 W W    $     .  ; H - ( 1 
 C ; A4H4?g!NN44XwG 11(LI..228]K11(LI** %,,&- !"34 ;< 7y 7!) !*z  "'!>!> $)HHOO$D$D !&*38nc3h& $  
 $0 0 0 6:)uU\\3:%=>?)%%**+d2) 				"	"#)X 4c 4 4 - - -# (2tCy=2I  UXX__   ! !H 	S&[ N N N: <
 <
 <
~   * ' ' ',E d38n (H". H"VU-2# U-2pZ!V !VJJ HH%(H25H=@HJMH	HV &ioo' cNCL2 2$d'RYY d'r   