
    a,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 ddlm Z m!Z! ddl"Z"ddl#Z$ddl%Z$ddl&m'Z( ddl)Z$ddl*m+c m,Z- ddl$m.Z.m/Z/ dd	l0m1Z1 dd
l2m3Z3m4Z4 ddl#m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF ddlGmHZH ddlImJZJmKZKmLZLmMZMmNZNmOZO ddlPmQZQ ddlRmSZS ddlTmUZU ddlVmWZW ddlXmYZYmZZZmZ[m\Z\ ddl]m^Z^m_Z_ ddl`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZk ddllmlZl ddlmmnZn ddlompZp ddlqmrZr dd lZmsZsmtZtmuZumvZvmwZw dd!lxmyZymzZz dd"l{m|Z| dd#l}m~Z~mZ dd$lmZ dd%l1mZmZ 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                            ed8          Ze$j                            ed9          Ze$j                            ed:          Ze$j                            ed;          Ze1j        Ze$j        j        j        Ze$j        j        j        Zd<ed=eeef         d>ed?ef         fd@Z edAB           G dC dD                      Z edAB           G dE dF                      Z	 dpdGeee$j/        edz  f                  dHee$j        j        j                 dz  d>ee$j        j        j                 fdIZej        d>efdJ            Ze G dK dL                      ZdMed>eg ee         f         fdNZ G dO dPe$j        j                  Z G dQ dR          Zeeef         Ze G dS dT                      Ze G dU dV                      Ze G dW dX                      Ze G dY dZ                      Zd[ed>eeef         fd\Z G d] d^e          Z G d_ d`e          Z G da db          ZdcZddedeedfedgedhed>dfdiZ ej                     Z e dj          Z e!dk          Z G dl dm          Z G dn doe.j                  ZdS )qa  
Core graph building functionality for PyTorch's Dynamo system. This module contains
the essential components for constructing and managing FX graphs during compilation:

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

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

The module supports key Dynamo features including:
- Higher-order operators through nested SubgraphTracers
- Graph deduplication for optimization
- Symbolic shape handling and propagation
- Side effect tracking and management
- Guard insertion and management
    N)Callable	GeneratorSequence)	dataclassfield)CodeType)AnycastOptionalTYPE_CHECKINGUnion)	ParamSpecTypeVar)fxTensor)guards)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourcetracingTracingContext)FakeScriptObject)is_opaque_type)
FakeTensor)signpost_event)_ConstraintTarget)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnvSpecializationuninteresting_files)Target)insert_deferred_runtime_asserts)
OrderedSet)is_traceable_wrapper_subclass   )configexclogging	variables)
CompiledFn
CompilerFn)create_binary_slicecreate_binary_subscrcreate_build_tuplecreate_call_functioncreate_dup_topcreate_instructioncreate_load_constcreate_rot_ncreate_swapInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)get_interface_for_device)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplementedunimplemented_with_warning)has_user_objectsindex_to_bytecode_constructor)apply_graph_deduplication)#get_backend_override_for_compile_id+get_inductor_config_override_for_compile_id)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffectsValueMutationExisting)_get_source_debug_name
AttrSourceBackwardStateSourceConstantSource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compiler_fnconfig_patchesreturn.c                 |     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   s     Z/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/torch/_dynamo/output_graph.pywrappedz+_wrap_with_inductor_config.<locals>.wrapped   s    {2~nMMMM    __name__z	<wrapped>)r	   getattrr   __wrapped__)r   r   r   s   `` r   _wrap_with_inductor_configr      si    NC N N N N N N N N N {JDDG%GNr   T)frozenc                   $    e Zd ZU eed<   eed<   dS )AliasingInfohas_aliasingmsgNr   
__module____qualname__bool__annotations__strr   r   r   r   r      %         	HHHHHr   r   c                   $    e Zd ZU eed<   eed<   dS )MutationInfohas_mutationr   Nr   r   r   r   r   r      r   r   r   tensors_with_sourcesstop_atc                 |   |t                      }t                      }g }| D ]=\  }}t          |t          j                  r|j        }||                    |           >|rY|                                }||v r||v r |                    |           |j        D ]\  }}||                    |           |Y|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    " %%.1eeG-/E) & &	fel++ 	&nG"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     s    .s333r   c                   T    e Zd ZU dZeed<   eej                 ed<   dZ	e
ed<   d	dZdS )
GraphCompileReasonzOStores why a given output graph was compiled; i.e. what caused the graph break.reason
user_stackTgraph_breakr   Nc                 @    | j         rt          j        |            d S d S r   )r   rp   r   selfs    r   __post_init__z GraphCompileReason.__post_init__$  s.     	-&t,,,,,	- 	-r   r   N)r   r   r   __doc__r   r   list	tracebackFrameSummaryr   r   r   r   r   r   r   r     s_         YYKKKY+,,,, K- - - - - -r   r   random_callsc                 8     dt           t                   f fd}|S )Nr   c                      d  D             S )Nc                 &    g | ]\  }}} ||i |S r   r   ).0fnargskwargss       r   
<listcomp>zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>+  s.    III(8D&D#F##IIIr   r   )r   s   r   _gen_rand_valuesz1_get_gen_rand_values_fn.<locals>._gen_rand_values*  s    IILIIIIr   )r   r	   )r   r   s   ` r   _get_gen_rand_values_fnr   )  s;    Jd3i J J J J J 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                     t                                                       |                                D ]\  }}t          | ||           d S r   )super__init__itemssetattr)r   r   kv	__class__s       r   r   zFakeRootModule.__init__3  sW    $$&& 	  	 DAqD!Q	  	 r   c                     dS )NzFakeRootModule(...)r   r   s    r   __repr__zFakeRootModule.__repr__8  s    $$r   c                 \    |                                 D ]\  }}t          | ||           d S r   )r   r   )r   r   r   r   s       r   add_nn_moduleszFakeRootModule.add_nn_modules;  s@    $$&& 	  	 DAqD!Q	  	 r   )r   r   r   r   dictr   r   nnModuler   r   r   __classcell__r   s   @r   r   r   0  s        11 4UX_(<#=  $            
%# % % % % c58?.B)C                  r   r   c                   \    e Zd ZdeddfdZdej        j        deej	                 de
fdZdS )WrapperBackendbackendr   Nc                     || _         d S r   )r   )r   r   s     r   r   zWrapperBackend.__init__A  s    #*r   r   r   c                 x   t          |          | _        || _        t          j        | j                  }|                     ||          | _        | j        | j        | j        j        u r| j        j        S t          j	        s| j        S 	  | j        j        t          |           } | j        t          |           }t          ||          r| j        |                                  S t          d|            # t          $ r t                              d            w xY w# |                                  w xY w)Nzincorrect results of backend zerror in verify_correctness)rd   restorer   copydeepcopyr   	candidateforwardr-   verify_correctnessrf   rv   RuntimeError	Exceptionr   	exception)r   r   r   copy_gmcorrectresults         r   __call__zWrapperBackend.__call__D  s1    ),,-((g~>>>!T^tw%F%F7?"( 	">!	%dgo|N'C'CDG#T^\.%A%ABF GV$$ &~ LLNNNN EtEEFFF 	 	 	MM7888	 LLNNNNs   
A	C: (C: :&D  D# #D9)r   r   r   r2   r   r   r   GraphModuler   r   r1   r  r   r   r   r   r   @  sn        +
 +t + + + +(&8<U\8J	     r   r   c                   T    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
dS )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_nsr   Nc                 x   t                      }i }t          j        |           D ]}t          | |j                  }|dk    rf|j                            d          }|dz  || d<   t          j        |g                               |dz             t          | |j        d           |r |j
        di | dS dS )zFlush accumulated timings to the bytecode_tracing chromium event
        and to compilation_time_metrics, then reset all counters.r   _nsg    eA_time_sbytecode_tracingN)r  )rk   dataclassesfieldsr   nameremovesuffixrg   
setdefaultr   r   try_add_event_data)r   chromium_log
event_datafns_valkeys         r   report_and_resetz'BytecodeTracingTimings.report_and_resett  s     122
#D)) 	) 	)AT16**Fzzf))%00.4sl
c???+(3C<<CCFSLQQQafa((( 	N+L+MM*MMMMM	N 	Nr   r   )r   r   r   r   r	  intr   r
  r  r  r  r   r   r   r  r  g  s         9 9 sOS-.%s...$%c%%%N N N N N Nr   r  c                   2   e Zd ZU dZeed<   eed<   eej        j	                 ed<   e
e         ed<   eeeeef         f         ed<   eed<   eej        j        j                 ed<   ej        d	z  ed
<   ej        j        j        j        ed<   ej        j        ed<   eej        j                 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        j        fd            Ze deej        j                 fd            Z#ddZ$d	S )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                 @    t          dt          |                      )Nz%shape_env shouldn't be accessed from )AssertionErrortyper   s    r   	shape_envz OutputGraphGuardsState.shape_env  s    QT$ZZQQRRRr   c                     | j         S r   )r)  r   s    r   r   zOutputGraphGuardsState.guards  s
    |r   c                     | j         S r   )r*  r   s    r   aotautograd_guardsz)OutputGraphGuardsState.aotautograd_guards  s    ''r   c                     t          | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        | j        | j        | j        | j        | j                  S )N)r   r!  r"  r#  r$  r%  r&  r'  r(  r.  r+  r-  r)  r*  r,  )r  r   r!  r"  r#  r$  r%  r&  r'  r(  r.  r+  r-  r   r5  r,  r   s    r   dump_guards_statez(OutputGraphGuardsState.dump_guards_state  sp    %(*&*&D#6*.*L!2.#6262\;#6K $ 7"4
 
 
 	
r   )r   r  )%r   r   r   r   Scoper   r   r   	overridesTorchFunctionModer   r   r   r   r	   r  
_functorchpyfunctorchFuncTorchInterpreterdevice_C_dynamor   GlobalStateGuardr)  	GuardsSetGuardEnvExprr+  r   r,  r-  r.  propertyr%   r2  r5  r7  r   r   r   r  r    s          #EO$EFFFFF####'S#X(>#????OOO5+7LMMMML4''''(/@@@@]$$$$em89999 FD#t###$$$$8<)3:<<<S8 S S S XS /    X (D)C$D ( ( ( X(
 
 
 
 
 
r   r  c                   z   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<   dS )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   rG  r  r   dc_fieldr   rJ  r   r   rK  rL  rM  tupler	   rN  rO  r   r   r   rF  rF    s2          Is#+8$ $ $L$sCx.    #+(4"@"@"@d3i@@@"*(4"@"@"@d3i@@@8@QU8V8V8VNDsE#s(O345VVV&.ht&D&D&D$s)DDD9ARV9W9W9WOT%U38_ 456WWWWWr   rF  c                   |   e Zd ZU  ee          Zeeedz  f         ed<    ee          Z	eee
eef         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<   dS )ExportMetaDatarH  Ngraph_input_idx_to_local_sourceoutput_return_typeout_specmodule_call_spec)r   r   r   rP  r   rT  r  r   r   rU  rQ  r   r	   r   utils_pytree
_LEAF_SPECrV  TreeSpecLeafSpecrW  r   r   r   rS  rS    s        
 AIA A A#T#v}*<%=    6>Xd5S5S5SS%S/12SSS 	& ek!*U[-@-II    	&&& dS%+%.1D1MMMN	P ' ' ' ' 'r   rS  r!  c                 N    | d         }t          |t                    s|j        }|S )N__builtins__)r   r   __dict__)r!  
f_builtinss     r   get_builtins_dictra    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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_sourcesr2  export_metadatatracked_fakes_id_to_sourcer   c                 t   t                                          |j        |j        |j        |j        |j        |j        |j        |j	        |j
        |j        |j        |j        |j        |j        |j                   |pi | _        |pt%                      | _        |pt)                      | _        |pi | _        d S r   )r   r   r   r!  r"  r#  r$  r%  r&  r'  r(  r)  r*  r+  r,  r-  r.  re  r%   
_shape_envrS  rf  rg  )r   rd  re  r2  rf  rg  r   s         r   r   zOutputGraphCommon.__init__  s     	%1%2%?%8%C%0%6%4%8%-%9%,%7%8%K	
 	
 	
$ -2 $1xzz.B.2B2B&," 	'''r   c                     | j         S r   )ri  r   s    r   r2  zOutputGraphCommon.shape_env@  s
    r    r   r   c                     t           r   )NotImplementedErrorr   r   r   s      r   bypass_packagez OutputGraphCommon.bypass_packageD  s
     "!r   NNNNrk  )r   r   r   r   r  r   r   r%   rS  r  r   r   r   rD  r2  r	   ro  r   r   s   @r   rc  rc    s        4 15%)15EI$
 $
#9$
 S#X-$
 d?	$

 ($.$
 %)d6l):$;d$B$
 
$
 $
 $
 $
 $
 $
L 8    X" "S " " " " " " " " " "r   rc  c                   
   e Zd ZU dZeed<   	 ddeeef         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        j1        fd6            Z2e2j3        d7ej        j1        ddfd8            Z2e)deeej         f         fd9            Z4e)deej5        ej6        f         fd:            Z7e)dee8j9        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        j5        fd?Z=d(ed=eddfd@Z>e?j@        	 ddAeAdz  dBed-         dCedz  deBdD         fdE            ZCe)ddF            ZDe)dejE        jF        fdG            ZGe)deHfdH            ZIe)dejJ        jK        fdI            ZLe)deeef         fdJ            ZMe)deejJ        jN                 fdK            ZO	 ddLeeee%d'ef         e
f         f         dz  ddfdMZPddNZQddOZRe)ddP            ZSdQedefdRZTde/fdSZUde
fdTZVde
fdUZWdVedejX        jY        ez  fdWZZddYedefdZZ[dYeddfd[Z\e]d\edefd]            Z^d^ed_edej         fd`Z_daejX        jY        ej6        z  ez  d\edbedefdcZ`d1ddeeea         eeef         f         fddZbd1ddee/deee         ecf         fdfZd	 dd1ddheedee/deec         fdiZfd1ddjegddfdkZhd1ddlee         djegdme
ddf
dnZiddoZjddhed=eddfdqZkdeeee/ez           f         fdrZldYedefdsZme?j@        defdt            ZndduZodvedw         d1dddfdxZpdvedw         d1dddfdyZqd1ddvee         dzerdeea         fd{Zse)deej5                 fd|            Zte)deeu         fd}            Zvd~ejw        dee6         dexfdZyd~ejw        dee6         dexfdZzdeeej        jw        f         fdZ{dYedej        jw        defdZ|deej6                 fdZ}ddZ~ddZddZd!eea         ddfdZdYedej        deeef         ddfdZdYed7eddfdZd!ed7edefdZd!ed7edefdZddZde%ejw        gdf         ddfdZdej        j5        defdZdejX        jY        dQeddfdZdS )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   r+  r-  frame_stater   r!  f_coder"  packager   	one_graphr   c                 	   t                               | |||
t                      i t          j        j        j        t          j        j        	                                t          j
        j        j        t          j        j        j        p't          j        j        j                                        t          j                                        g            t+          | |          g| _        i | _        g | _        || _        || _        || _        g | _        t;          t<                    | _        t                      | _         |	j!        |	j"        |	j#        d| _$        d | _%        tM                      | _'        d| _(        g | _)        tU          | j)        |ptV          j,        |ptV          j-        tV          j.        | j$                  }dd l/mc m+} |0                    d          5  t          j1        2                    |tg          | j                  | j                  }d d d            n# 1 swxY w Y   ti          |          | _5        | j5        j6        7                    |	           | j%        | j5        _%        | j5        j6        | _6        tq          j9                    | _:        | ;                                 ty          j=        t|                    | _?        i | _@        t          |           | _B        i | _C        i | _D        t                      | _E        i | _F        t          jH                    | _I        t          |          | _K        g | _L        d| _M        i | _N        d | _O        g | _P        || _Q        || _R        d | _S        || _T        i | _U        g | _V        g | _W        d| _X        i | _Y        t          j        Z                                | _[        d| _\        d| _]        t                      | _^        t          i           | __        t          i           | _`        | a                                 i | _b        g | _c        d | _d        g | _e        i | _f        i | _g        d | _h        d | _i        | j                                | _k        t          jm                    | _n        t                      | _p        | q                                | _r        i | _s        t                      | _u        t                      | _w        i | _x        d | _y        d S )	N)r#  r$  r%  r&  r'  r(  r)  r*  )	is_export)co_nameco_filenameco_firstlinenoF)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)r2  allow_non_fake_inputsr+  )zr  r   r   r   autograd
forward_ad_current_levelr;  r<  #retrieve_all_functorch_interpretersrX  _deviceCURRENT_DEVICEr@  convert_frameinitial_global_stater?  r   rA  r)  rB  SubgraphTracertracersinput_source_to_varleaf_var_creation_orderr+  r-  rw  cleanup_hooksnext_compile_id_counter
compile_idinstalled_globalsr}  r~  r  r  cudagraph_annotationrL   region_tracker_emit_debugger_breakpointr  r%   r-   capture_scalar_outputs capture_dynamic_output_shape_opsr  torch._functorch.configpatch_subclassesFakeTensorModer   r   tracing_contexttraced_coder   r   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   rg  param_name_to_sourcerQ   rt  variable_tracker_cachemro_source_cacheguarded_mro_absent_keyssignature_cache	itertoolscountunique_var_idr   ru  output_instructions	timestamp_input_mutation_streams_last_checked_input_versionsregister_finalizer_fnsr   rv  profiler_statery  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_fglobalsr.  
contextlib	ExitStackcompiler_trace_stackr  bytecode_tracing_timings+maybe_install_saved_tensors_hooks_subgraphs"saved_tensors_hooks_subgraph_namesre  rS  rf  r*   "used_inlined_inbuilt_modules_namesattr_source_cache(_cached_replayed_side_effect_source_refs)r   ru  r   rv  r+  r-  rw  r   r!  rx  r"  ry  rz  r2  _config	fake_modes                   r   r   zOutputGraph.__init__X  s    	''%"uu*,~0?"-9]]__ ;.=$}:O  :x&7799M++-- " 	( 	
 	
 	
" 'tv>>>? CE  ?A$"4&68#$788+.55
 ~!-$3
 
 BF!022/4& 13 ,!*!Kf.K+4 ,768>8jn
 
 
	& 	211111111]]E]JJ 	 	)88#&*4;&7&7{	 9  I	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 0>i/H/H(//777484M1/;3A3T3V3V  """ #D)) 	' ?A!'-- FH# KM
 >AUU$;=&_..,0,>,>68   KM$CG) OQ# /: ;? RT"<>+- KM  ,18+S+S+U+U( 05, 27. PSuu, >AWW ADB! 	    AC-  	 '+ 57  	' ;=;?!.2 2244 	2 %/$8$:$:!(>(@(@% <<>> 	/
 /1-// DN<</GIPT555s   ":H((H,/H,populate_export_metadatar  c                   |s'| j         j        s| j        t          | j                  S ddlm} g }| j                                         D ]n}t          |d          rZ|j        }t          |t          t          f          r6t          |t                    rt          |j        |          r|r|j        J |j        j                                        D ]\  }}i }|j                                        D ]3\  }	}
|
                                ||	j                                        <   4ddgt          |                                          k    sJ || j        j        |j                                        <   t          |t                    rt          |j        |          s|                    |           pd |D             S )zKReturn Python-side effect sources that Dynamo replays outside the FX graph.Nr   )_ExportModuleSpecTrackerDictmutation_typein_specrV  c                 6    g | ]}t          |j                  S r   )rS   source)r   vars     r   r   zDOutputGraph.get_replayed_side_effect_source_refs.<locals>.<listcomp>  s#    UUUs&sz22UUUr   )rt  id_to_variabler  r   torch.export._tracer  _get_modified_varshasattrr  r   rP   rR   r   value_base_vtr   as_python_constantvtkeysrf  rW  r   )r   r  r  potential_side_effectsr  mut_typer   r   specsk_specvals              r   $get_replayed_side_effect_source_refsz0OutputGraph.get_replayed_side_effect_source_refsn  s   
 )	G%4	G =IEFFFDDDDDD!#$7799 %	; %	;CsO,, $;, 8:OP   ; "#'>?? *J	#?E E * 4 *#&<#;#;#;
 !$ 2 8 8 : :	* * ! ! !
 )+347==?? !& !&KFC(+(>(>(@(@ %*&)*F*F*H*H$I$I )2:'>$uzz||BTBT'T'T'T'T %* !% 4 E$%D$;$;$=$=!" !" #3(?@@;&sy2NOO; /55c:::UU>TUUUUr   basepathc                 *   |                     d          }||d         f}|| j        vrt          ||d                   | j        |<   | j        |         }|dd          D ]4}||f}|| j        vrt          ||          | j        |<   | j        |         }5|S )N.r   r,   )splitr  rT   )r   r  r  partsr  r  parts          r   get_chained_attr_sourcez#OutputGraph.get_chained_attr_source  s    

3U1Xd,,,*4T58*D*DD"3'',!""I 	1 	1D4.C$000.8.F.F&s++C0FFr   r^   c                     |                     dd          }t          |          dk    rt          ||          S |                     ||d                   }t          ||d                   S )Nr  r,   r   )rsplitlenr^   r  )r   r  r  r  intermediate_bases        r   get_chained_param_buffer_sourcez+OutputGraph.get_chained_param_buffer_source  se     C##u::??$T4000 88uQxHH !2E!H===r   c                 >   | j                             t          dd                     t          j        rgddlm} | j         |            | _        | j        j	        }| j        
                    |j        |j        |j        t          j                               d S d S )Nr  Tlog_pt2_compile_eventr   r   )r  enter_contextrj   r-   dynamo_profilertorch._dynamo.dynamo_profilerr   r  rv  rx  pushr}  r~  r  timetime_ns)r   r   codes      r   mark_bytecode_tracing_startz'OutputGraph.mark_bytecode_tracing_start  s    !//"&*  	
 	
 	
 ! 	IIIIII"*&9&9&;&;#<&D$$ #	    	 	r   c                 x   | j                                          | j                                         t          j        r| j        ddlm} | j        	                                }t          j                    }|u||j        z
  }||j        z
  } ||j        |j        |j        ||t#          | j        j        j                  dd d d |j        d          }| j                            |           d }t/          t          j        t0                    rt          j        }| j                            |           d S d S d S )Nr   )FunctionTraceTimingr   )	func_namefilenamefirstlineno
cumtime_ns
tottime_nsbytecode_countinline_depthcaller_func_namecaller_filenamecaller_firstlinenois_primitive_call
call_stack)r  r  r  closer-   r   r  r  r  r   r  r  start_time_nschild_time_nsr	  r
  r  r  rv  rx  co_coder  record_timingr   r   
dump_stats)r   r  stack_entrytrace_end_nsr  r  timingoutput_files           r   mark_bytecode_tracing_stopz&OutputGraph.mark_bytecode_tracing_stop  sQ   %66888!'')))! 	8d&9&EIIIIII-1133K<>>L&)K,EE
'+*CC
,,)3(1 + 7))#&t|':'B#C#C!"%)$('+&1&C!   #11&999 K&0#66 5$4**;777779	8 	8&E&Er   c                 V    t          | j                  }|                     d|          S )N__builtins_dict__)ra  r!  install_global)r   r`  s     r   r  z-OutputGraph.install_builtins_dict_in_fglobals  s)    &t'899
""#6
CCCr   hookprefixc                     | t          | j                   }|| j        vsJ || j        |<   ||                                 fS r   )r  r  get_backward_state_proxy)r   r#  r$  r  s       r   add_backward_state_hookz#OutputGraph.add_backward_state_hook  sW     4#d122444.....$(D!T224444r   c                 X   | j         | j        rt          dddg            t                      }| j                            dt          |          |t                                | _         t                      | j         j	        j
        d<   |                                 | _        | j         S )Nz&backward_state does not support exportrk  z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_stater  grapharg)r  r+  rE   r!   root_tracercreate_graph_inputr1  rU   rx   r   metanew_varr  )r   example_values     r   r&  z$OutputGraph.get_backward_state_proxy  s    $,{ D U	    *OOM(,(8(K(K']##*,,	 )L ) )D% ?T>U>UD%*/
;&*llnnD#((r   c                    | j                             t                                          t          j                             | j                             t                                          t          j                             | j                             t                                          t          j                             | j                             t                                          t          j	                             | j                             t                                          t          j
                             | j                             t                                          t          j                             t          j        j                                        }|C| j                             t                                          t          j                             t          j        j        j        sE| j                             t                                          t          j                             d S d S r   )r   r   r_   
make_guardrM   	SHAPE_ENVrY   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICEGLOBAL_STATETORCH_FUNCTION_STATEr   r?  r;  peek_interpreter_stackFUNCTORCH_STACK_MATCHr@  compiled_autogradin_compiled_autograd_regionAUTOGRAD_SAVED_TENSORS_HOOKS)r   cis     r   r  zOutputGraph.init_ambient_guards  s    	((33L4JKKLLL**<+PQQ	
 	
 	
 	)++66|7MNNOOO)++66|7RSSTTT)++66|7PQQRRR**<+LMM	
 	
 	
 X 7799>KOO!##..|/QRR   }.J 	KOO!##.. =     	 	r   c                    t           j        j        j        rd S t           j        j        j        j        }t           j        j        j        j        } |            } ||          sd S |\  }}| 	                    dt           j
                            | j        |j                            }| 	                    dt           j
                            | j        |j                            }|dk    sJ |dk    sJ ||gS )Nsaved_tensors_hooks_packsaved_tensors_hooks_unpacksaved_tensors_hooks_pack_0saved_tensors_hooks_unpack_0)r   r@  r@  rA  r;  _aot_autogradrX  top_saved_tensors_hooks"saved_tensors_hooks_are_inlineableinstall_subgraphr   r  r   r   )r   	get_hooksare_inline_hookshookspack_gm	unpack_gmpack_subgraph_nameunpack_subgraph_names           r   r  z7OutputGraph.maybe_install_saved_tensors_hooks_subgraphs1  s   =*F 	4$28P	*0S 	 	&& 	4 #!22&H  '-@@
 
  $44(H  )/BB 
  
 "%AAAAA#'EEEEE"$899r   r   .r   ctor_arg_sourcesc                     | }|                                  }t          | j                                      fd           |r.                    d t          ||          D                        n2                    t          t          j        j	        |                     
                    t          |          d                               |           | j                                                                       t!          |          }||f| j        |<   t%          j        | j        ||          }|                                }t+          j                    j        j                            |           |S )z]
        call fn(*args) before the graph runs and turn the result into a fake input.
        c                  D                          j        j                  S r   )load_import_fromr   r   )cgr   s   r   <lambda>z3OutputGraph.synthetic_graph_input.<locals>.<lambda>[  s!    B''  r   c              3   b   K   | ]*\  }}||nt           j                            |          V  +d S r   )r0   ConstantVariablecreate)r   r  srcs      r   	<genexpr>z4OutputGraph.synthetic_graph_input.<locals>.<genexpr>a  sS        C I,F,M,Mc,R,R     r   F)r4  r?   rv  add_push_nullforeachzipmapr0   r[  r\  call_functionr  storer  extendget_instructionsr`   r  r~   buildrealizer   getguards_contextdynamo_guardsremove_guards_with_source)	r   r   r   rT  r5  varnamer  r  rX  s	    `      @r   synthetic_graph_inputz!OutputGraph.synthetic_graph_inputN  s    D	,,..t|$$
    	
 	
 	
  	EJJ   #D*: ; ;      
 JJs95<dCCDDD
TE***
%%b&9&9&;&;<<<%g..35t=M2N'/ &t|]FKK!!+9SS	
 	
 	
 r   c                 :    | j                             |           d S r   )r  r   )r   r   s     r   add_cleanup_hookzOutputGraph.add_cleanup_hookt  s    !!"%%%%%r   c                 z    t          | j                  D ]} |             | j                                         d S r   )reversedr  clear)r   r#  s     r   call_cleanup_hookszOutputGraph.call_cleanup_hooksw  sD    T/00 	 	DDFFFF  """""r   r  c                     | j         d         S Nr   r  r   s    r   r1  zOutputGraph.root_tracer|  s    |Ar   c                     | j         d         S Nrw  r   s    r   current_tracerzOutputGraph.current_tracer  s    |Br   c                 2    t          | j                  dk    S )Nr,   )r  r  r   s    r   is_root_tracerzOutputGraph.is_root_tracer  s    4<  A%%r   txc                 Z   t          |d          sdS |j                                        sdS | j        }| j        &t          t          |j                            | _        |j                                        }d}|j	        j
        D ]}|j        dk    r dS |j                            d          }t          |t          j                  sE| j                            |          }|j        }|4||k    r.|| j        vrt'          j                    | j        |<   || j        |<   |dz  }dS )zRecord which stream index has input mutations by comparing current
        tensor versions against the versions captured at graph input creation.symbolic_stream_stateNr   placeholderr5  r,   )r  r  in_stream_contextr1  r  r   	enumerate_input_versions_at_beginningcur_stream_idr   nodesopr3  ri  r   r   r   _versionr  r   extract_stack)	r   r~  tracercur_stream_index	input_idxr   r5  prev_versioncur_versions	            r   &check_input_mutation_on_current_streamz2OutputGraph.check_input_mutation_on_current_stream  sS   
 r233 	F'99;; 	F!,404&=>>1 1D- 3AACC	L& 	 	Dw-'' IMM/::MmU\:: <@@KKL'0K'K,,F,F#4+GGG&466 01AB @K1)<NII	 	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dS | j         |         }t          j                    }dd                    |                                           dd                    |                                           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:
rk  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;;;F5lC%3552 ww~,,..//2 2 ww|**,,--2 2 2 594RS 	 3r   c                     | j         j        S r   r{  r   r   s    r   r   zOutputGraph.graph  s    "((r   r  c                     || j         _        d S 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6r   c                     | j         j        S r   )r{  real_value_cacher   s    r   r  zOutputGraph.real_value_cache  s    "33r   	LazyProxyc                     | j         j        S r   )r{  bound_symbolsr   s    r   r  zOutputGraph.bound_symbols  s    "00r   r   c                 &     | j         j        |i |S r   )r{  create_proxyr   r   r   s      r   r  zOutputGraph.create_proxy  s    /t"/@@@@r   c                 &     | j         j        |i |S r   )r{  create_noder  s      r   r  zOutputGraph.create_node      .t".????r   c                 &     | j         j        |i |S r   )r{  remove_noder  s      r   r  zOutputGraph.remove_node  r  r   source_targetprior_tracerdescription)r  NNc              #     K   t                      }	 |r|j        | j        u sJ |                                 |r|n"t	          | | j        || j        j        |          }| j                            |           |V  |                    d d d            | j        	                                 d S # |                    d d d            | j        	                                 w xY w)N)parentr  r|  r  )
r@   r  r{  	__enter__r  r|  r  r   __exit__r   )r   r  r  r  new_scope_ctxr  s         r   	subtracerzOutputGraph.subtracer  s      ())	 B#*d.AAAAA##%%%  #."/"1; +    L'''LLL""4t444L ""4t444Ls   A+B/ /2C!c                     | S r   r   r   s    r   outputzOutputGraph.output  s    r   c                 6    | j         j        J | j         j        S r   )r  r  r   s    r   r  zOutputGraph.fake_mode  s     #-999#--r   c                 f    | j         j        J | j         j        j        J | j         j        j        S r   )r  r  r2  r   s    r   r2  zOutputGraph.shape_env  s8    #-999#-7CCC#-77r   c                 $    | j         j        j        S r   )r  rj  rk  r   s    r   r   zOutputGraph.guards  s    #2@@r   c                 $    | j         j        j        S r   )r  module_contextr   r   s    r   r   zOutputGraph.nn_modules"  s    #2==r   c                 $    | j         j        j        S r   )r  rj  r5  r   s    r   r5  zOutputGraph.aotautograd_guards&  s    #2EEr   outc                 B   t          t          t          t          t          dt
          f         t          f         f         ||n| j        j        j	                  }t          j        t          j                    f|d<   t          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
<   dS )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   rQ  r   r	   r   r  global_contextglobal_stater   set_grad_enabledis_grad_enabledacceleratorcurrent_acceleratorr1  	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_state*  sz    eHS#X.4556 ? )8E
 
 ).(>@U@W@W'X^$ !& 1 E E G GGTCHHf 	 e8(CC%h//,
'(
 e8%@@%e,,0
+,
 e6AA$X...
)*
 e6>>$U++.
)*
 ,+--2
-...r   c                 :    | j                             |           d S r   )r  r   )r   r~  s     r   push_txzOutputGraph.push_txS  s    #####r   c                 4    | j                                         S r   )r  r   r   s    r   pop_txzOutputGraph.pop_txV  s    ##%%%r   c                 8    | j         s| j        n| j         d         S ry  )r  rv  r   s    r   
current_txzOutputGraph.current_txY  s    #'#3Mt||9I"9MMr   r  c                 ^    |                     | 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_valuerv  	f_globalsf_locals)r   r  s     r   resolve_source_valuez OutputGraph.resolve_source_value]  s7     ,(t|/DEE
 
 	
r   c                 *    t          | j                  S r   )rh   r   r   s    r   rh   zOutputGraph.count_callsj  s    4:&&&r   c                 V    t          t          | j        j                            dk    S rv  )r  r   r   r  r   s    r   is_empty_graphzOutputGraph.is_empty_graphm  s"    4
())**a//r   c                 P    t          d | j        j        D                       dk    S )Nc                 (    g | ]}|j         d k    |S )r  r  r   xs     r   r   z+OutputGraph.has_outputs.<locals>.<listcomp>q  s$    DDD!1483C3CA3C3C3Cr   r   )r  r   r  r   s    r   has_outputszOutputGraph.has_outputsp  s)    DDtz/DDDEEIIr   r  c                     |sJ | j         }|                    d          D ]0}t          |t                    r	||         } t	          ||          }1|S Nr  )r   r  r   r   r   )r   r  objr   s       r   get_submodulezOutputGraph.get_submodules  s\    t<@OC 	& 	&A#t$$ &!fc1oo
r   tmpr  c                     t          | j        d                   }	 | dt          | j                   }||vr| j        dxx         |fz  cc<   |S 6)Nco_varnamesTr   )r   ru  r  r  )r   r  existingr  s       r   r4  zOutputGraph.new_var}  sp    t(788	66D!34466C(""!-000SF:000
		r   c                 T    || j         d         vr| j         dxx         |fz  cc<   dS dS )z/Ensure self.code_options.co_names contains nameco_namesN)ru  )r   r  s     r   update_co_nameszOutputGraph.update_co_names  sD    t(444j)))dW4))))) 54r   namesc                  t   d                     t          t          |                     }t          j        dd|          }t          j        dd|          }t          j        dd|          }t          j        dd	|          }t          j        d
d|          }|r|d                                         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  rb  r   rer  isalpha)r  r  s     r   module_key_namezOutputGraph.module_key_name  s     xxC((vH&RV
 
 vA8T
 
 v,eT::vlIt44vosD11 	 47??,, 	 4<Dr   attr_prefix
attr_valuec                 Z   t          |t          j        j                  r?| j                                        D ]%\  }}||u r|                     d|di           }|c S &t          || j                  }|| j        |<   |                     d|di           }t          |j	        |           |S Nget_attrr   )
r   r   r   r   r   r   r  ro   rw   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244 ! !	c*$$ --j$BGGE LLL % (T_EE	 &0	"!!*iR@@%*j111r   targetoptionsc                    
 t           j                  rt          j         j        fi S t                    dv sJ d         t          t                    rJ t          t          j	                  r< j
                                         s j        dt          dt          f fd}nBt          t          j        j                  r{t          t          j        j                  sJ rBt!                              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}nkt-          t/                              r: j
                                         s j        dt          dt          f fd}ndt          dt          f fd	} j                                        D ]\  }}|u r ||          c S t5          j        | 
t9          
 j         j                  
 j        
<   t          t          j        j                  r{d
t          dd f
 fd}t=          d          r%                                D ]\  }}	 ||           t=          d          r%                                 D ]\  }}	 ||            |
          S )Nr  
module_keyr   c           	         j         J j         | <   j        J j        j        j        v rj        j        j                 S t	                    dk    rBt          t                    s-t                              t          j
                             n;t                    s,t                              t          j                             t          j                            d| di           fdi}j        j        j                            |          }d|                                j        j        vsJ t'                    |                                j        j        d<   |S )Nguardedr  r   r5  tensor_dict)r  rv  r  rt  rn   r   r]   rN   r7  rM   ID_MATCHrZ   TENSOR_MATCHr{   r  track_object_existingas_proxyr   r3  rc   )r	  r  r  r   r  r  r  s     r   	wrap_namez6OutputGraph.register_attr_or_module.<locals>.wrap_name  s{   0<<<8>)*5 |///T\0===<.;FCC*622i??
-I I? "&"3"3L4I"J"JKKKK+F33 P!&"3"3L4M"N"NOOO"L''
JBGG  #) 	  \(5KKFTVWW$BKKMM,>,CCCCC9Mf9U9U"'6	r   c                 <    t          t                    | fi S r   )r   r1  r	  r  r  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  s$    +DLL*fXXPWXXXr   c                 (    t          j        fi S r   )r0   UnspecializedNNModuleVariabler  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  s    $B6UUWUUUr   c           	      d    t          j        j                            d| di           fdiS )Nr  r   sym_num)r   r\  rv  r  )r	  r  r   r  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  sM    &-L%%j*b"EE  # 	  r   c                     t           j        j                            j                  }                    d| di           }t          |j        |           t          j        j	        j
        j        j        ||fi S r   )r   _libraryfake_class_registrymaybe_to_fake_objr  r  rw   r   r@  r0   script_objectTorchScriptObjectVariabler\  )r	  fake_script_objr  r  r   r  r  s      r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name%  s    "'."D"V"VNF# # ++J
BKK!%*o>>>}.<V]? .5  r   c                     j                             |            j        | <   t          j        t          |                     S )N)source_name)r  r  r!  r~   rg  rV   )r	  r   r  s    r   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name3  sO    ++J77706!*-&,"z:::  r   	leaf_namec                     j         J                     |           } d|  }|j         |<   t          t                    r)| j        t
                              |j                  <   d S d S r  r  r  r   r\   r  rs  r  r  r!  
new_sourcenew_namer  r   r  s      r   register_leaf_namez?OutputGraph.register_attr_or_module.<locals>.register_leaf_nameF  s    0<<<!AA&)TT
"00Y006@)(3fk22 " " 9#33JODD  " "r   _parameters_buffers)!rO   r+  r~   rg  r  r   r   r^   r   r   r{  r}  r1  r   r   r   rN   r7  rM   	NN_MODULESymIntSymFloatr   r1  r   r   rs  r  ro   r!  r  named_parametersnamed_buffers)r   r  r  r  r  r   r   r'  r!  r   r  r  r  s   `` `      @@@r   register_attr_or_modulez#OutputGraph.register_attr_or_module  s:     44 	M #(&LLGLLLw--7"""""f&788888fel++ t	(F&&(( * ) c  o                      D 00 B	feho66666 Vf//0FGGHHHY# Y/ Y Y Y Y Y Y Y YV# V/ V V V V V V V V u~ >?? /	c o          DLL)) 	
 (F&&(( *)c o          c o        O))++ 	$ 	$DAqF{{ y||###  *E2"4$:KLL &feho.. 	2"c "d " " " " " " " " v}-- 2$*$;$;$=$= 2 2LIq&&y1111vz** 2$*$8$8$:$: 2 2LIq&&y1111yr   c                 R   | j                             d          }t          |          }|sg i fS g }i }g |j        |j                                        | j        j                                        }|r|	                                }t          |t                    r't          |j        t                    sJ ||j        z  }R|| j        j        vst          |j        t                    rLt          |j        t"                    r2t          |j        j        t&                    r|j        j        j        |v s|j        j        j        }||vrg ||<   ||                             |           |i }	i }
| j        D ]&}t          |j        t                    r(t          |j        t&                    r|j        j        |v sF|j        j        }|| j        d         v sJ ||         D ]}|j        |
v r|j        J |j        j        }||	vrm|                     | d          }||	|<   |                    t9          d|          t;          |          t=                      t9          d|          g           |j        J |j        }t'          |	|                   |
|<   (||
fS )Nr   r  _ref	LOAD_FASTargval
STORE_FAST)r   ri  rm   r   symbolic_localsvaluesrt  store_attr_mutationsr  r   r   r   r   r   r  rP   r  rX   r  r\   
local_namer   	graphargs_exampleru  indexr4  re  r8   r9   r4   )r   r~  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuer  stolen_namer   overridden_sourcesarg	list_namelist_idx
alias_name
old_sources                   r   handle_aliases_for_stolen_listsz+OutputGraph.handle_aliases_for_stolen_lists[  s    #''///99  	r6M8:
X
&&((
 388::
  	/		A!-.. !!'400000  T.CCC!!/3LMM D qx77 D qx}k::	 D
 HM,0AAA(-2K+--+-K($++A...+  	/0 35> '	P '	PC3<..sz;77 J)[88 
-I 1- @@@@@ + P P8111 x+++8>7**!%$***" "J )3GH%&&.{9MMM-h77022.|JOOO	   x+++X
1<WX=N1O1O":..9PD ...r   
stack_popsc                 p   |                                  g }t                      }dt          ddfd}t          |j                  D ]F\  }}t          |j                  |z
  |k    }t          j                            ||           t          |t          j
                  st          j        ||           |r|                    |           t          |t                    r|j                            |           n|                    |           t          |t                    rc|j        dnt#          |j                  }	|j                            t          |          dz
  |	f           |j                            |           Ht          |          |_        t+          |                                |                                z             }
|j                                        D ]A\  }}t          j        ||           t          |j        t6                    r|j        j        |k    r
|| j        u rO||
v rTt<          j        dk    r<t@          !                    t          |          r|j"                            |           n"t@          !                    t          |          rJ t          |j#                  |j#        |<   t          |t                    r9|j        dnt#          |j                  }	|j$                            ||	f           |                    |           C||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                 n    t                               t          j        |           rt	          d          d S )NzCAttempted to reconstruct WithExitFunctionVariable outside the stack)r1  __instancecheck__r0   WithExitFunctionVariabler0  )r  s    r   ctx_exit_checkz@OutputGraph._get_stack_values_to_restore.<locals>.ctx_exit_check  s<    %%i&H#NN $Y   r   )allow_lazy_constantr   r,   )      )%prune_dead_localsrF  r~   r  r   r  r0   LazyVariableTrackerrealize_allr   rN  visitr   r   rK  r|   target_valuesrQ  rM  rN  rG  r   cellvarsfreevarsr6  r   r  r\   r9  rv  sysversion_infor1  rM  rL  rJ  rO  )r   r~  rJ  stack_valuesr3  rO  ir  rP  rW  cell_and_freevarsr   r   s                r   _get_stack_values_to_restorez(OutputGraph._get_stack_values_to_restore  s?   " 	"$$	 	D 	 	 	 	 ""(++ 	4 	4HAu"%bh--!"3z"A)55+> 6    eY%GHH =%ne<<<" ##E***%.. +%,,Q////##E***%!899 4-5BB5AT;U;U  #**C,=,=,A=+QRRR)00333\** =>> &,,.. #	# #	#DAq!.!444 18[11H'1,,$,&&%%%7** )),:: )00333
  11,BBBBB#&t'8#9#9Da !455 @/1BBuQ_7M7M  $++Q,>???""""T!!r   r   r   c                    | j         J t          j        s| j         |u sJ |                                  || _        d| _        t                              d|           g }t          j	        dk    r| j         j
        D ]v}|j        dk    rB|                    t          dt          | j         j        d                                        O|                    t!          j        |                     wg }g }|}|t#          d |j        D                       sJ |                     |||u r|nd	          \  }	}
|                    |	           |                    |
           t)          |j                  D ]}|                    ||j        
           |j        }|| j                            |           |                     |           | j        r*ddlm} |                      |                       d| _        | j        r| j        r
J d            |                     | j                   |                      | j                   \  }}|                     |           | !                                 d | j"        #                                D             }tI          |          }ddl%m&} t          | j'                  d	k    rg }| (                    d          | _)         |tU          | j'                  d          }| +                    d|          }tY          | j         ||          }|-                    |.                    |d                     |-                    t_          d	d                     |                    |0                    | j)                             |                     |           d |D             }d}d}| j         |u rX|rUt#          d |D                       r;t#          d |D                       r!t          tc          |                    t          |          k    r| j        2                                r|j3        s| j4        s|d         j5        s|d         j6        stY          | j                   }| 7                    ||           |                     g | 8                    |ts          t)          |                    |          |:                                tw          d          t          dt          |                               n| (                    d          }tY          | j         |||          }| <                    |||d           i }|j=        #                                D ],\  }}|dk    r!t}          |t~          t          f          sd||<   -tY          | j         ||||          }| <                    |||d           t          jB        j        jC        r|rt          |          dk    r|d	         }t          |t          jB        jE        jF                  r|jG        t          jB        jH        jI        u r|j#        d	         } |j#        d         }!t          | t          jB        jE        jJ                  sJ i }"|jK        L                                D ]9}#t          |#t          jB        jM        jN                  sJ |#jO        }$|#jP        |"|$<   :t          | j#                  D ]\  }%}||"v rd|"|         f| jR        jS        |%<   !|jT        @t          |jT        dd          x}&r(t          |&dd          rd |jT        f| jR        jS        |%<   h|V                                r$d!|W                                f| jR        jS        |%<   t          d"| d#|%           	 |!W                                | jR        _Y        n0# t          $ r#}'t          d$d%|! d&d'd(g|')           Y d}'~'nd}'~'ww xY wg }(t          | j]                  d	k    st          |jK                  d	k    r|(-                    | 8                    ||^                                |                     t          |jK                  d	k    r+|(                    |0                    |                     d}n7|(                    t          d*                     n| _                                 |                     |(|:                                z              |                     t          d+t          |          |d	         j`        z
            g           d	})d	}*t          |          D ]\  }+}
t          |
ja                  },|+d	k    r
|,|
j`        z  },|,d	k    r5|                     t          d+d	          gtw          d                     nK|*|,z  }*|                     t                      gt          |)|*          tw          d                     |)|,z  })|+t          |          dz
  k    r$tY          | j                   }-i }.| j         jd        #                                D ]~\  }/}0t          |0jT        t                    r_|0jT        je        |/k    rO|-f                    |-g                    |/                     t          |
ja                  t          |.          z   |.|/<   |                     |-:                                t          d+t          |.                    t          d,d          gz              |
ja        h                    |.           |                     t          d*          t          d+t          |                    gt          |d	         j`        dz                        |r'|r%|                     t          d-|.          g           t          jB        j        jj        d/v r\| k                    d0          }1|1rDt          jB        j        jj        d1k    rt          jm        d2|1            nt          d2|1           |S )3a  
        Compiles the current subgraph, with inputs w.r.t. self.root_tx, and codegens:
            - Call the compiled subgraph
            - Apply side effects
            - Codegen stack and locals
            - Store the locals

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

        Returns stack indices and locals keys where we dropped NULLs, and where we found inactive context manager objects.
        NTzCOMPILING GRAPH due to %srQ     COPY_FREE_VARSco_freevarsrD  c              3   >   K   | ]}|                                 V  d S r   )can_restore)r   blocks     r   r^  z/OutputGraph.compile_subgraph.<locals>.<genexpr>Z  s.      KKuu((**KKKKKKr   r   )is_graph_breakr,   )create_breakpointFz)export does not support pregraph_bytecodec                 4    i | ]\  }}|t          |          S r   )ru   )r   r  r  s      r   
<dictcomp>z0OutputGraph.compile_subgraph.<locals>.<dictcomp>  s3     
 
 
+44D/#&&
 
 
r   disablerandom_valuesz.do not trace into Dynamo rng recovery functionr   __gen_rand_values)rC  c                     g | ]	}|D ]}|
S r   r   )r   valsr  s      r   r   z0OutputGraph.compile_subgraph.<locals>.<listcomp>  s%    NNNTNN#SNNNNr   c              3      K   | ]X}t          |t          t          t          f           o0t          |t                    o|                                t          u  V  Yd S 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7TAMMOOu<TU     r   c              3   >   K   | ]}|                                 V  d S r   )	is_tensorr  s     r   r^  z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s*      ==aAKKMM======r   rz     UNPACK_SEQUENCE	graph_out)tempvarsrC  r  is_inputinputconstantzEncountered unrecognized type z at output z8nested function with non-constructible closure in outputz as_python_constant for out_spec zCannot return a nested function with closure from a compiled function. Dynamo failed to construct the function defined in the compiled region with closure objects.zGDefine the function at module scope instead of inside another function z0Ensure that all closure variables are constants.)r*  r+  r,  r-  from_excPOP_TOP
BUILD_LISTLIST_EXTENDDELETE_FASTr3  )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: )orv  r-   nested_graph_breaksr  compile_subgraph_reasonr  r   debugrZ  r[  prefix_instsopnamer   r8   r  ru  r   allblock_stackr_  rr  exitr   r  rt  prune_dead_object_newadd_output_instructionsr  bytecode_transformationrj  r  r+  rI  cleanup_graphr   r   r   
decoratorsrn  r   r4  r  r   r"  r?   re  load_function_namer6   create_storer   is_emptydebug_localsr  rK  rL  codegen_cellscompile_and_call_fx_graphr   rf  r;   codegen_suffixusesrr   r`   r\   r   r@  log_graph_in_out_metadatar   r0   NamedTupleVariable	tuple_clsfunctional_exportExportTracerOutputListVariablegraph_outputsr7  codegenGraphOutputEntryvariabler<  r  rf  rU  r  r   is_python_constantr  r0  rV  r}   rE   rh   r   graph_output_varsrun_compiler_collectiverG  rJ  r7   r3   r6  r9  append_outputcreate_loadupdater:   side_effect_replay_policyr  warningsr  r   )2r   r~  r   rJ  r  install_stack_valuesall_stack_locals_metascur_txr\  r3  rh  rj  r?  rC  nn_modules_proxiesrootrn  random_calls_instructionsrand_fnrand_fn_namer  stack_values_flatstored_graph_output_vargraph_output_varcell_cgpass1r|  r  r  pass2r  flat_returnsrV  vt_to_graph_out_idxr  r  idxr  er  	start_idxend_idxr]  n_valsroot_cgunmodified_locals_namesr   r   side_effect_refss2                                                     r   compile_subgraphzOutputGraph.compile_subgraph   s   * |''') 	&<2%%%% 	'')))'-$		-v666 +-w&&1 	9 	9;"222 ''*, #DL$=m$L M M      !''	$8888
 !#35 KK8JKKKKKKKK!%!B!Bfll

" "L$ ##L111"))$/// "&"455 F F

6&2D
EEEE]F  " 	//333$$\222) 	3BBBBBB(():):)<)<===-2D** 	
t{ 	
 	
7	
 	
; 	$$T%;<<<*.*N*NL+
 +
'' 	$$[111
 
8<8M8M8O8O
 
 
 011'''''' t !!A%%(*%%)\\/%B%BD"g'(9::G  G  ../BGLLLd7I  G &,,**<>>   &,,-A!U-K-KLLL%,,$$T%;<<   (()BCCC& ON-=NNN"' LB!    +      ==+<=====  C)**++s3D/E/EEE!**,, FO F ' F +2.?	 F
 +2.? F  --Gr7+++((33D*;!<!<==t 
 --// !^^ ''8cBS>T>TUUU
 
 
 
  $||K88  #5	  E $5ueDDD H#j..00 ) )
U199VC2F1T%U%U9$(HSM !#5  E $5udCCC $>>%> )**a//&q)r5=#:#MNN8}6IJ J $&8A;L!x{H%$em&=&J     GI'!&!4!;!;!=!= D D)%1F1WXXXXX49N8=+H55#,\-?#@#@  R!444 + 3B 7LD0CCHH
 I1+229fd+K+K!K 2 '
E B B 2
 !( "	LD0CCHH  2244  * " 5 5 7 7LD0CCHH
 #1 U U UPS U U# # 8@8S8S8U8U,551   %$^$Qx$Q$Q! !j R# &'         F4:&&!++s53F/G/G1/L/L222u7N7N7P7PRVWW   u*++q00MM%"4"45E"F"FGGG.2++MM"4Y"?"?@@@@ ,,...((%2H2H2J2J)JKKK 	$$" -..1G1J1TT  	
 	
 	
& 	 !788 -	B -	BGAt*++FAvv$.({{,,*<Q???$Q    6!,,&((,Y@@ %Q   V#	 C.//!333#DL11:<' L8>>@@  DAq!!(K88 QX=PTU=U=U--g.A.A!.D.DEEE589J5K5Kc3O O 6/2 ,,,,..*(c2I.J.J   +=a@@@	 	 	 !(()@AAA 	$$"9--"<S9O5P5PQQQ 4Q7AAEFF	
 	
 	
"  	 7 	((#M:JKKKL   =9=NNN#HH)-  I       
='AVKKMkXhk k   
 'kXhk k  
 &%s   _% %
`/``rX  c                 @   | j         j        rd}|}|t          t          |                                                    }|D ]S}|| j        u r)|                    |                    |                     4|j        sJ  ||j        |                    T|                    t          t          |                               |j        }|dz  }||                    t          d|                     d S |                    t          dd                     d S )Nr   r,   r  re  )r  r   rQ  sortedr^  rv  r  create_load_closurepost_prune_cell_and_freevarsr5   r  r  r8   )r   r~  rX  tx_cntr  rY  cells          r   r  zOutputGraph.codegen_cells  s>   '3 	FF79F$ !(@(@(B(B!C!CDD$ F FD--(()?)?)E)EFFFF%BBBB6>tDEEEE  !3CMM!B!BCCC! $  /&IIIJJJJJ/!DDDEEEEEr   r\  log_side_effectsc                 B  	 | j                                        | j        r~| j        rJ | j                                        D ][\  }} |           | j        J                                         | j                                                 |           \t          j
        r| j                                        |j        D ]\  	}                    	fd           |D ]} |                               t          t!          |          d                                         t#          d          g           |                     |                               ||j                    | j                             |           d S )Nc                                  S r   r   )rX  	debug_vars   r   rY  z,OutputGraph.codegen_suffix.<locals>.<lambda>  s    RR	]] r   Fr  )value_from_source)rt  codegen_save_tempvarsr  r+  r   r  r  r  
store_attrr-   replay_side_effectscodegen_hooksr  r_  extend_outputr6   r  r8   r  restore_stackcodegen_update_mutated)
r   r~  r\  rX  r  r  r  r   rD  r  s
      `     @r   r  zOutputGraph.codegen_suffix  s    	//333 	${""?!06688 $ $	c3.:::  0G!H!HIIId####% 	0++B///  " 	> 	>OIt22222333  31#d))UCCDDD0;;<==== 	2r"""
RYGGG005EFFFFFr   c                 d   | j         sJ t          | j        j                  }|D ]}|j                            dd           t          j                    }t          j	        |          D ]\  }}|j
        t          j        j        u rt          |j                  | fk    r|j        s|j        d         }|j
        t          j        j        u rbt          |j                  | fk    rH|j        sA|j        d         }| j                            |           | j                            |           dS )z
        Remove "creation_timestamp" from node meta

        Remove this pattern from the graph:
            torch._C._set_grad_enabled(False)
            torch._C._set_grad_enabled(True)
        creation_timestampNr   )r  r   r   r  r3  r   r   r  r  pairwiser  r?  _set_grad_enabledrQ  r   _erased
erase_node)r   r  r   r  node1node2s         r   r  zOutputGraph.cleanup_graph	  s:    TZ%&& 	6 	6DIMM.5555,..%.u55 	1 	1LE5 :::%*%%l*:)<<< =  %z!}LEH$>>>ej)),.>-@@@!M A $):a=LJ))%000J))%000	1 	1r   rk  c                 V   | j         sdS t          j        j        j        r%t          j        j                            d          t                              d           t          j	        
                    dd fd           | j                                          d| _         dS )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   rY  z,OutputGraph.bypass_package.<locals>.<lambda>6	  s    1"! ! r   c                      di S )N_reasonr   )r   r   s   r   rY  z,OutputGraph.bypass_package.<locals>.<lambda>:	  s    6    r   metadata_fn
payload_fn)ry  r   r@  r-   strict_precompiler.   PackageErrorr   warning_loggingtrace_structuredbypass_current_entryrn  s    ``r   ro  zOutputGraph.bypass_package)	  s     | 	F=1 	-#00/   	3V<<<''      	( 	
 	
 	
 	))+++r   c                     i }| j         j        D ]W}|j                            dd           }t	          |t
          j        j                  r|j        }d |D             ||j	        <   X|S )Nr5  c                 Z    g | ](}t          |t                    r|nt          |          )S r   )r   r  repr)r   ss     r   r   z:OutputGraph.get_graph_sizes_structured.<locals>.<listcomp>I	  s1    !U!U!U1z!S'9'9"F!!tAww!U!U!Ur   )
r   r  r3  ri  r   r   r  r   shaper  )r   retr   r5  sizes        r   get_graph_sizes_structuredz&OutputGraph.get_graph_sizes_structuredC	  ss    *,J$ 	V 	VD IMM/4@@M-):)EFF V$*!U!UPT!U!U!UDI
r   c                    d}|d| dz  }| j         j        D ]}|j                            dd           }t	          |t
          j        j                  r|j        }||j	         dt          |           dz  }g }d}|D ]j}t	          |t                    r|                    |           -t	          |t
          j                  r"d}|                    |j        j                   i n|r||j	         d	t          |           dz  }|S )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
r5  z: r  FTz (concrete): )r   r  r3  ri  r   r   r  r   r  r  rQ  r  r   r+  r   hint)	r   r  graph_sizes_strr   r5  r  concrete_size
has_symintszs	            r   get_graph_sizeszOutputGraph.get_graph_sizesL	  s=   72D2222J$ 	 	D IMM/4@@M-):)EFF $*di#B#B5;;#B#B#BB ""
  B!"c** %,,R0000#B55 %)
%,,RW\::::! '#yOOu]7K7KOOO r   c              #   x  K   | j         j                                        }i }|                     |           	 | j         j                            |           dV  | j         j                            t          |                     dS # | j         j                            t          |                     w xY 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_statee	  s      
 "1@PPRR<>#7888	 /BBCUVVVEEE  /BB,-ABB    D /BB,-ABB   s   #B .B9c                    | j         }|J |j        xj        j        }t                              dj                   t          j        	                    dd fd           |j
        }t          |          dk    s0J d                    d                    |                                t          |                                                              |                                t          j                                        z            5  t)          d	d
          5  d g|                                z  }t-          j        |j        |           |_        d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |j                                         t4          j        d S d S )Nzcompiler_collective %sr  c                      dddS )Ncompiler_collectivestringr  r   r   r   r   rY  z5OutputGraph.run_compiler_collective.<locals>.<lambda>	  s    1 (% % r   c                  6     j                                         S r   )local_staterender)dss   r   rY  z5OutputGraph.run_compiler_collective.<locals>.<lambda>	  s    2>#8#8#:#: r   r  r,   z&Expect only one device type but got {}+r  Tr  )group)rv  distributed_state
all_states
compile_pgr   infor  r   r  r  _device_typesr  r  r  rA   r   r>  rankr  device_countrj   r  distall_gather_objectspeculation_logrs  r.    CompileCollectiveRestartAnalysis)r   r~  r  device_typesr  r  s        @r   r  z#OutputGraph.run_compiler_collectivew	  s<   \~~~&&B38MJHH-r~>>>N++  ;::: ,    &3L|$$)))8??@V@VWW *)) ))9)9););<<CCOO%%(9(F(F(H(HH 
+ 
+ 2$OOO	
+ 
+ *.1B1B(B
&z2>TTTT *
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ $$&&&66= 438M8Ms6   F-<E5)F5E9	9F<E9	=FFFrvr~   c                 D   | j         sdS ddlm} |D ]}t          ||          r|j        s|                                j        j                            d          }t          |t          j
        j        j                  sJ |j        yt          |dfg          }|| j         z  rK|j        rt!          |j                  n|j        j        j        }|j        j                            |           |j        j        r!d|j        _        t/          j        d          dS )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]
        Nr,   TensorVariabler5  Tz3autograd.grad consumed grad_fns of returned tensorsrestart_reason)r  variables.tensorr"  r   requires_gradr  r   r3  ri  r   r  fake_tensorr   r   r   r  r   r  r  r  autograd_grad_leaked_tensorsr   graph_break_on_autograd_gradr.   AutogradGradRestartAnalysis)r   r  r~  r"  r  r'  reachable_grad_fnstensor_names           r   )_validate_outputs_safe_for_autograd_nodesz5OutputGraph._validate_outputs_safe_for_autograd_nodes	  sE    3 	F444444 	T 	TCc>22 #:K ,,..-266GGKk5+<+H+STTTTT"* "<k4=P<Q!R!R!D$HH T14Tc#*oooAT"?FF{SSS: 	>BB;1T   	 	r   c                 R   ddl m} t                      | j        D ]E}t	          ||          r3|j        s,                    |                                j                   FsdS | j	        j
        D ]<}|v rt          fd|j        D                       r                    |           =|D ]t}t	          ||          rb|j        r[|                                j        v r@d}|j        rt          dd|dd	g
           Sd|j        _        t%          j        d          udS )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.
        r,   r!  Nc              3       K   | ]}|v V  	d S r   r   )r   inptainted_nodess     r   r^  zHOutputGraph._check_requires_grad_intermediate_outputs.<locals>.<genexpr>	  s(      HHC3-'HHHHHHr   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.r)  Tz:source-less requires_grad_() intermediate leaked as outputr#  )r%  r"  r   r  r   r  r   r  r   r   r  anyall_input_nodesr&  rz  rE   r  graph_break_on_requires_grad_r.   RequiresGradRestartAnalysis)	r   r  r~  r"  r   r   r  r   r1  s	           @r   )_check_requires_grad_intermediate_outputsz5OutputGraph._check_requires_grad_intermediate_outputs	  s    	544444 -0EE- 	5 	5A!^,, 5QX 5!!!**,,"3444 	F J$ 	( 	(D}$$HHHH43GHHHHH (!!$'''
  	 	C3//% LLNN'=88Q  < ! N V$'?N	     HLB&D9'c   ;	 	r   r  c                    !"#$%& t           j        j                                        5  ddlm}  j        sJ                                   t           j	                  dk    r!t          |          dk    rg cddd           S t          dd          }t          |t                    sJ t          |t                    sJ                      ||                                ||                                dd j                            t)          d	 |D                                 fi           }                                 }|                    |            j                            ||           t0          j        st5          d
          5  t7          |          D ]I}t9          ||          }	t          |	t:          j                  rt?          |	 j         | j!                   J "                                 t?          t;          j        | j	                   j         | j!                   ddd           n# 1 swxY w Y    #                                 t           j	                  }
tH          d         dxx         |
z  cc<    %                                  j&        '                                 tQ          | j	                  #ddl)m*}  |#            j+        r) j+        D ]!}tY          #|t9          ||                     " j-        D ]} |#           t]          #/                                d          - 0                    d#1                    ddd                      j2        |#_3         j4        #_4         j5        6                                #j7        d<    j8        #j7        d<   |#j7        d<    j9         j9        #j7        d<   tt          ;                    dty          |#ddd                     t           j=        >                    d fd#fd            ?                                  j@        jA        }|J | _B         j!        shddlCmDc m} |E                    d          5  t           jF        G                    |j                   }ddd           n# 1 swxY w Y   | j@        _A         H                                5   I                    # J                                          "ddd           n# 1 swxY w Y   ddlKmL} t          "|          s*t          t9          "d d          |          rP"jM        d!k    rEt          "|          r"n"jN        }|O                    |           t          "|          s|jP        " j2         j2        Q                    |"            jR        r	"$$fd"}|" |"d#$          "tH          d         d%xx         dz  cc<   |j         J |j         jS        x}r.g &i %d&  jT        D             }|D ]}|U                    |jV                  }t          jX        |jY                  Z                                }t                      }t          j]        ||jY        |gd          }t          ;                    d'|           &_                    t          ja        |fd(|          |f           t           jb                            d#$          d)t          d*t          d+t          f"# %&fd,            } d                    ||           n d                    |"            je        J t           je                  !t                      r!h                    !fd-           g }t          jj                    D ]K} |!            k                                }!l                    |           |_                    |           L|D ]*}!m                    !n                    |                     +!o                    t          t                    d           !p                                 t           jT                  D ]\  }} | jV         jr        js        |<   !t                    |           !u                                cddd           S # 1 swxY w Y   dS ).z
        Generate code from self.graph and return the Instruction()s to
        call that generated code.

        Code is generated w.r.t. self.root_tx.
        tx is only used for preserving GraphModule metadata
        r,   rm  r   N__compiled_fnT)	with_uuidr  c              3   >   K   | ]}|                                 V  d S r   )r  r  s     r   r^  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>*
  s*      5O5Oqajjll5O5O5O5O5O5Or   r)   )r+  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@  rA  coloreddynamo_output_graphc                  0    d                                  iS )Nsizes)r  r   s   r   rY  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>
  s    $"A"A"C"CD r   c                  4                          ddd          S )NFTr>  )print_readablerB  s   r   rY  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>
  s#    2#4#4!&tD $5 $ $ r   )r  r  )r2  )_LazyGraphModule__self___lazy_forwardc                      t           j                                        5   | i |cd d d            S # 1 swxY w Y   d S r   )r   r?  DisableTorchFunctionSubclass)r   r   real_compiled_fns     r   _tf_disabled_wrapperzCOutputGraph.compile_and_call_fx_graph.<locals>._tf_disabled_wrapper
  s    >>@@ A A//@@@A A A A A A A A A A A A A A A A A As   599z"do not trace Dynamo-compiled graphrp  unique_graphsc                     g | ]	}|j         
S r   r/  )r   as     r   r   z9OutputGraph.compile_and_call_fx_graph.<locals>.<listcomp>
  s    <<<18<<<r   z:Compiling backend specialized graph with specialization=%sc                 $     |||                    S r   r   )r  r   check_fns      r   rY  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>
  s    XX$(IF" F" r   r   r   r   c                     	D ]\  }} ||           r|v r |         | i |c S j                             |j        |j                  5  |j        d<   t          |           }t          j                  5                      |          |<   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y    |         | i |c S  | i |S )Nspecialization)	r2  patch_source_specializationr  rV  r3  r   r   r  call_user_compiler)
r   r   rV  rX  r   compiled_fnr   r   specialization_cachespecialization_guardss
        r   specialized_dispatchzCOutputGraph.compile_and_call_fx_graph.<locals>.specialized_dispatch
  s   4I Y Y0.#8D>> Y-1EEE'K';N'K%)("-3(" (" !" !" !" "&!K!K . 5~7N" " 	& 	& =K(8 9?CDzz%,T-A%B%B !& !&(,(?(?N(S(S %9$H!& !& !& !& !& !& !& !& !& !& !& !& !& !& !&	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& $H#7#G#XQW#X#XXXX#Y$ ';7777s6   .B8;B!B8!B%%B8(B%)B88B<	?B<	c                  X                          t          j        j        j        d          S )Nstore_user_object_weakrefs)rW  r   r@  graph_bytecode_inputsr   )rX  s   r   rY  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>   s%    B//;D4  r   )vr   r)  r   clear_framer  rn  r  r  rh   r   r  r=   r   r   r   r6  r-  r  r{  
create_argrQ  
dedup_passr   _maybe_preserve_original_metar-   do_not_emit_runtime_assertsrj   dirr   r   r  r)   r2  r+  remove_unused_get_attr_nodesremove_unused_graphargsri   &remove_tensorify_specialized_graphargsr  rs  r    dce_extra_outputsr=  r  r   r  r  
parametersro  rJ  ry  _backend_idr  r  r   r3  r  r  graph_code_logr  rs   r  r  rt  r  r  _old_fake_moder  r;  r  r  r  r  rZ  r   torch.fx._lazy_graph_modulerK  r   rL  force_recompiler   add_backend_idr  specializationsr:  r<  r  inspect	getsourcerV  stripRootGuardManagerr   LAMBDA_GUARDr   r   r  r  r@  r	   install_global_unsaferv  r?   rG   r_  rH   r7  r4  rd  r  r  rc  pop_topr  rf  rT  make_call_generated_coderf  )'r   r~  r  r  rn  r  output_nodesub_gmsattrsubgraphncallsr=  subgraph_nameregister_finalizerold_fake_moder  backend_fake_moderK  lazy_gmrQ  rs  sourcesrX  source_indexcheck_fn_sourceunused_root_guard_managerrV  r^  tmp_varsconstructorvar_namer  rD  rX  r[  r   rP  r\  r]  s'   `                                @@@@@@r   r  z%OutputGraph.compile_and_call_fx_graph
  s    ])5577 j	) j	)++++++####((***4:&&!++B1j	) j	) j	) j	) j	) j	) j	) j	) _===Db$'''''dN33333
 ::2rBBB ::2rBBB**$//5O5OB5O5O5O0O0OPPR	 K oo''G(((==b+NNN5  ""CDD   #D		  #*4#6#6%h?? ; ( $ $'+{	    557773tTZ88#{	                 & ((*** ,,FW.///69///77999 !'')))#D$*55B@@@@@@!!"%%%
 6 M%)%L M MMBwt]/K/KLLLL&*&A ' '"""2&&&&BMMOOT**6 ##N((%*4PT )   $    |'!% *.)EB& 5::<< G67 ,0+ABG'($(BGL!(4262K./  &"T$PT     N++%DDDD    ,    ##%%% 0:M ,,,"/D; C999999999]]E]RR   ).(9(H(H"/"9 )I ) )%              & 2C$.**,, Q Q"55b$:M:M:O:OPPQ Q Q Q Q Q Q Q Q Q Q Q Q Q Q EDDDDD+'788 27;
DAACSTT2(O;; "+/?@@.KK$-  !00999!+/?@@ 2")/K|'++D+>>> 3 3#. A A A A A 3!'$H  K Wo...!3... *666"/"9"II <>(*%SU$<<T^<<<&5  N#*==1F#G#GL&-&78O&P&P&V&V&X&XO0@0B0B-%21&/()	   H IIT'  
 *00%-;C !" !" !" !-	  +
 
 
 
 &&.R&SS8 8s 8s 8 8 8 8 8 8 8 8 8 TS8. **41EFFFF **4===<+++4<((B!!          #@#G#I#I . .KKOOO  HHX&&&OOH---- ( ? ?H$$R^^H%=%=>>>>  %B!C!CUKKK

%dn55 W WSLOJ$DSII''---&&((U	j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	) j	)s   AcDcB#I	=c	I	cI	H=c&S 4c S	cS	#c+)T c T$	$c'T$	(N!cccc                 8    | j                             d          S )Nr  r  )r   
find_nodesr   s    r   placeholderszOutputGraph.placeholders<  s    z$$$666r   c                 $    d | j         D             S )Nc                 (    g | ]}|j         d          S )r0  r3  )r   r   s     r   r   z)OutputGraph.graphargs.<locals>.<listcomp>B  s    DDD$	*%DDDr   )r  r   s    r   r:  zOutputGraph.graphargs@  s    DD$2CDDDDr   r   r   c                     t          dddddd          5  |                     ||          cd d d            S # 1 swxY w Y   d S )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)rj   _call_user_compiler)r   r   r   s      r   rZ  zOutputGraph.call_user_compilerD  s     ,("& &<%N
 
 
 	@ 	@ ++B??	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   9= =c                 J   | j         J d}g }|j        j        D ]0}|j        dv r|dz  }|j        dk    r|                    |           1t          |           |D ]+}t          |d          s|j        d         }|j        |_	        ,| j
        |_        | j        |_        t          | j        t           j                  p| j         }t%          | j        t           j                  }	|	rt)          ||	          }t          |d          r|j        nd}
t           j        rdd	lm}  ||          }	  t1                      t2          j        d
|
            t           j        rt9          |          } |||          } t1                      t2          j        d|
            t;          |          s
J d            n'# t<          t>          f$ r  t@          $ r}| j!        r@tE          | j         |tG          j$                              %                    |j&                  d tO          || j(        j)        dd|
 dtU          |           d| j(        +                                 d|
 dtU          |           ddg           Y d }~n_d }~wtX          $ r  tZ          $ rE}tE          | j         |tG          j$                              %                    |j&                  d d }~ww xY wt]          ddi | j/        |ta          |j        j                  ta          |          d           |S )Nr   rc  call_methodcall_moduler,   r  _dynamo_sourcer0  r   z<unknown compiler_fn>)inline_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.r)  dynamor  )op_count
node_countinput_count)1r   r   r  r  r   rq   r  r3  r  r  r  _param_name_to_sourcer  _source_to_user_stacksrJ   r  r-   debug_backend_overriderK   debug_inductor_config_overrider   r   r  5torch._higher_order_ops.passes.inline_invoke_subgraphr   r/   INFOr   r   callabler   r   rC   r  rB   rt  currentframewith_traceback__traceback__rF   rv  rx  r   format_frame_summaryrD   r   r   r  r  )r   r   r   totr  r   plrD  r   inductor_config_overrider  r  r[  r  s                 r   r  zOutputGraph._call_user_compilerQ  s    +++HN 	* 	*DwIIIqw-''##D)))3 	/ 	/B2/00 /gj) %(J! $(#< $($>! 0&(E    	 	 $O"F$I$
 $
  $ 	45 K {J//)K  ( 	
 ( 	,      ('++B	8LNN7<)Ld)L)LMMM( :,[99%+b.99KLNN7<)I4)I)IJJJK((OO*OOO(O.0@A 	 	 	0 	 	 	5 <+$a)=)?)?  .11t< '#4qDqqc!ffqqDLLmLmLoLoqqdddSVVdddC	 	 	 	 	 	 	 	 	  	 	 	  	8 	8 	8' !W%9%;%; nQ_--48	8
 	,.!"(.11"<00	  		
 		
 		
 s'   .B
F: :KB(JKA KKc                 P    t           j        j        j        rt	          |           S i S r   )r   r@  r-   use_graph_deduplicationrI   r   s    r   rd  zOutputGraph.dedup_pass  s%    =7 	,T222Ir   sub_gmc                     t          || j        d          }||_        d|_        |                     ||d            |S )NT)requires_suffixFr/  )ro   r   r   torchdynamo_force_dynamicr/  )r   r  r  	next_names       r   rL  zOutputGraph.install_subgraph  sJ    'dotTTT	#+0( 	$$VYt$DDDr   c                 (    d | j         D             }|S )Nc                     g | ]	}|j         
S r   )example)r   rD  s     r   r   z.OutputGraph.example_inputs.<locals>.<listcomp>  s    888##+888r   )r:  )r   r  s     r   r   zOutputGraph.example_inputs  s    88888r   c                     t          | j                            d          d          D ]<}t          t	          |j                            dk    r|                     |           =d S )Nr  r  T)reverser   )r  r   r  r  r   usersr  r   r   s     r   rh  z(OutputGraph.remove_unused_get_attr_nodes  sm    4:00J0??NNN 	' 	'D4
##$$))  &&&	' 	'r   c                 R     j         sJ dt          j        j        dt          fd}dt          j        j        dt          fddt          j        dt          ffd}dd	lm} t          t           j
        j                            D ]}t          t          |j                            dk    r|j        d
k    sh|j        dk    r|j        t           j        u sJ|j        dk    r)|j        t$          j        u r ||j        d                   s ||          s ||          r                     |           dt          j        dt,          j        d z  fd}dt          j        dd f fd}t1                      dt0          t,          j                 dt$          j        t$          j        z  dd fdg } j        D ]} ||          d u}|r|j        s|                    |           /|j        s,t;          |j        d         t>                    s ||           b|j        d         }	t;          |	t>                    rt;          |j        d         j         t$          j!                  r|j        d         j         }
|j        d         j"        }t$          j#        j$        %                    |
          sktM          |
'                                          }|D ]G}tQ          |j)        |          }tU          j+        t$          j        t$          j        ffd|           HdtY          t[          |j        d         j                             r|	j.        |	j.        n|	j         } |           |D ]4} ||          }|%|vr ||           /                    |           5d S )Nb_noder   c                     | du rdS t          | t          j                  sdS | j                            d          }|dS |du rdS t          |t
          j                  r|j                                        x}|S dS )NTFr5  )	r   r   Noder3  ri  r   SymBoolr   maybe_as_bool)r  brs      r   is_static_truez;OutputGraph.remove_unused_graphargs.<locals>.is_static_true  s    ~~tfbg.. u00AyuDyyt1em,,&..000Q= 5r   rT  c                     ddl m} t          | t          t          t
          f          rdS t          | t          j                  r(t          | j        	                    d          |          S dS )Nr   SymTypesTr5  F)
torch.fx.experimental.sym_noder  r   r  rv  r   r   r  r3  ri  )rT  r  s     r   is_symnode_argz;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_arg  sj    ??????!c5$/00 t!RW%% I!!&**_"="=xHHH5r   r   c                 .   ddl m} | j        dk    rdS t          | j                            d          |          sdS t          fd| j        D                       sdS t          fd| j        	                                D                       sdS dS )	Nr   r  rc  Fr5  c              3   .   K   | ]} |          V  d S r   r   r   rT  r  s     r   r^  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s-      <<Q~~a((<<<<<<r   c              3   .   K   | ]} |          V  d S r   r   r  s     r   r^  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s-      GGQ~~a((GGGGGGr   T)
r  r  r  r   r3  ri  r  r   r   r7  )r   r  r  s     r   is_symnode_compute_nodezDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node  s    ??????w/))udimmO<<hGG u <<<<$)<<<<< uGGGG$+2D2D2F2FGGGGG u4r   r   )is_accessor_noder  rc  c                     | j         d         }|j        }t          |t          j                  r0t          |j        j        t          j                  r|j        j        S d S Nr0  )	r3  r  r   r   r+  r   exprsympySymbol)r   rD  r  s      r   placeholder_binds_symbolzEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  sX    )J'CkG'5<00 )Z!5<6 6 ) |((4r   c                     t                               d| j        d         j        j                   | j        d=                     |            j                            | d            d S )NzREMOVE UNUSED GRAPHARG %sr0  )r   r  r3  r  r  r  r  r   )r   r   s    r   remove_unusedz:OutputGraph.remove_unused_graphargs.<locals>.remove_unused%  sd    II149Z3H3O3TUUU 	*%T"""!%%dD11111r   used_symbolsfakec                 *    | t          |          z  } d S r   )r"   )r  r  s     r   update_used_symbolsz@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols/  s     L...LLLr   r0  c                      |           S r   r   )tr  r  s    r   rY  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>R  s    .A.A,PQ.R.R r   )0r  r   r   Argumentr   r  %torch.fx.experimental.symbolic_shapesr  rr  r   r   r  r  r  r  r  operatorgetitemr   _checkr   r  r  r  r   r+  r   r  r   r   r3  rx   r  ScriptObjectexample_strong_refr  r  tracing_with_realr   __obj_flatten__r   wrapped_objpytreetree_map_onlyr   r1  r'  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   ri  z#OutputGraph.remove_unused_graphargs  su    	27#3 	 	 	 	 	&	bg. 	4 	 	 	 		"' 	d 	 	 	 	 	 	" 	KJJJJJT$*"23344 	+ 	+D4
##$$))Gz))?22t{hFV7V7V?22 K5<77*N49Q<88 8 /.t44 8 ('--	 8 $$T***	27 	u|d7J 	 	 	 		2 	2D 	2 	2 	2 	2 	2 	2 +.%%	/el+	/38<%,3N	/	/ 	/ 	/ 	/
  "% &	< &	<D33D99EL #<z 6(//555z <*Ij)+@+ + < "M$'''' )J/C!#'<== ! !$)J"7"?ASTT !*.)J*?*G*.)J*?*R$~ASS+    " )-_-L-L-N-N(O(OI(1 " "07$3$?1" 1" !' 4%*\5<$@$R$R$R$R$R$1!" !" !" !"
 !%d49Z+@+H&I&IJJ ! +.?+FCK  ('d;;;; ) 	0 	0D--d33F!--!M$'''' !''///	0 	0r   c                 <   ddl m} | j        j        D ]}|j                            d          }t          |t                    r|j        t          |j        j
        j        d          rt          d |j        D                       r|                    |j        j
        j        j                  rht!          |j                  D ]>}|                    t%          |j                             |                     |           ?|                     |           	d S )Nr   )TensorifyStater5  r  c              3   ,   K   | ]}|j         d k    V  dS )itemN)r  )r   us     r   r^  zEOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>~  s)      ??qF*??????r   )torch._dynamo.symbolic_convertr  r   r  r3  ri  r   r   	item_memor  r   _exprr  r  should_specializer  r   replace_all_uses_withr#   r  )r   r  r   r5  r  s        r   rj  z2OutputGraph.remove_tensorify_specialized_graphargsg  s.    	BAAAAAJ$ 	' 	'D IMM/::M=*55'!+7M38>GG 8??DJ????? 8 #44!+06;  8 dj)) ( (A++L9P,Q,QRRR$$Q''''  &&&	' 	'r   c                 H    | j                             |           d| _        dS )zt
        We call this on the creation of a new compiled subgraph that is inserted
        before user code.
        TN)r  re  r  )r   r$  s     r   r  z#OutputGraph.add_output_instructions  s)    
 	 ''///r   r  r  c                     j         rEdt          t          j        df         dt          j        ffd}|                     |           dS |                     t          j                             dS )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                 4    t          j        d |           S r   )typesFunctionType)r  r  r  r  s    r   _make_fnz<OutputGraph.install_resume_function_global.<locals>._make_fn  s     )$	4wOOOr   N)rd  rQ  r  CellTyper  ry  )r   r  r  r  r  s    ``` r   install_resume_function_globalz*OutputGraph.install_resume_function_global  s      	Pu~s23P#P P P P P P P P
 &&tX66666&&"4D99    r   c                     || j         vsJ | j                             |           | j                            t	          j        | j        ||                     dS )a`  
        WARNING: prefer the safer `install_global_by_id/install_global`.
        torch.compile instances should be independent of each other;
        one footgun is to have one instance depend on the existence of
        a global installed by another instance. This can happen if we mangle
        a global the same way across both instances.
        N)r  r   r  r   re   r\  r!  )r   r  r  s      r   ry  z!OutputGraph.install_global_unsafe  s]     411111""4((([/0A4OOPPPPPr   c                     | dt          |           d| j         }|| j        v r|S |                     ||           |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  ry  r   r$  r  r  s       r   install_global_by_idz OutputGraph.install_global_by_id  sU     992e9999994)))K""4///r   c                 P    t          |          }|                     ||           |S )z~
        Installs a global, generating a unique name for it.

        Returns the name of the newly installed global.
        )r=   ry  r  s       r   r"  zOutputGraph.install_global  s,       ""4///r   c                    d | _         | j                                         | j                                         d | _        | j        j        D ]}d|j        v r|j        d= | j                                         | j	                                         t          |                                           | _        | j                                         | j                                         | j                                         | j                                         | j                                         | j                                         | j                                         | j                                         | j                                         | j                                         | j                                         | j                                         d S r  )rv  r   rs  r  r  r   r  r3  r  r  rQ  r  r  rt  r  r  r  r  r  r  r  r  r  r  r  r  s     r   cleanupzOutputGraph.cleanup  s    /55777$(!J$ 	* 	*DTY&&Ij)##%%% &&(((8=55779
 9
5 	!!!#))+++##%%%$**,,,""$$$#))+++-33555""$$$ &&((($**,,, &&(((!!#####r   r  c                 :    | j                             |           d S r   )r  r   )r   r  s     r   add_graph_finalizerzOutputGraph.add_graph_finalizer  s"     	#**+=>>>>>r   r   c                 z    |j         dk    r|j        d         j        S |j         dk    sJ | j        |j                 S )z#Extract the non-fake example tensorr  r0  r  )r  r3  r  r   r  r  s     r   example_value_from_input_nodez)OutputGraph.example_value_from_input_node  sA    7m##9Z(00w*$$$$t{++r   inlined_modulec                 R    t                               j                  t           j         j                   j                                       dt          dd f fd}t          |d          rLt          |j
                  r8|j
        j        t          u r%|
                                D ]\  }} ||           t          |d          rLt          |j                  r:|j        j        t          u r)|                                D ]\  }} ||           d S d S d S d S )Nr!  r   c                     j         J                     |           } d|  }|j         |<   t          t                    r)| j        t
                              |j                  <   d S d S r  r#  r$  s      r   r'  zHOutputGraph.add_fqn_info_for_inlined_modules.<locals>.register_leaf_name  s    ,888==fiPPJ,,,,H2<D%h/&+..   5//
@@   r   r(  r)  )rs  r  r  ro   r  r!  r   r   r  r  r-  __func__!og_module_named_parameters_fn_ptrr.  og_module_named_buffers_fn_ptr)r   r  r  r'  r!  r   r  s   ` `   @r    add_fqn_info_for_inlined_modulesz,OutputGraph.add_fqn_info_for_inlined_modules  s    **6;77"$94;L
 
 	/33D999	# 	$ 	 	 	 	 	 	 	 	 >=11 	28992"3<45 5 %3$C$C$E$E 2 2LIq&&y1111>:.. 	25662"0912 2 %3$@$@$B$B 2 2LIq&&y1111	2 	22 22 22 2r   Fr   )r#  r   )r   r  )r~  r   r   N)r   rs  )r   r   )r  )r   rq  )r   r   r   r   rQ   r   r   r   r	   r2   r   r   r   r8  r   r   r   r9  r:  r   r   r  r   rT   r  r  r  r  r  r~   rQ  r   Proxyr'  r&  r  r  r   rn  rp  rt  rD  r1  r{  r}  r  r  r  r  Graphr   setterr  r  r   r  r  r  r   r  r  r  r  r  contextmanagerr(   r   r  r  r  r  r  r%   r2  r)  rB  r   r   rC  r5  r  r  r  r  r  rh   r  r  r   r   r  r4  r  staticmethodr  r  r/  r<   rI  rF  r_  r   r  r?   r  r  r  ro  r  r  r  r  r-  r6  r   r  r  ry   r:  r  r1   rZ  r  rd  rL  r   rh  ri  rj  r  r  r
  ry  r  r"  r  r  r  r  r   r   r   rs  rs  K  s(            TU TU38nTU  $&TU -	TU
 TU %%67TU TU TU TU TU $((I#JTU *+TU TU 
TU TU TU TUn 385V 5V 5V+/5V	c5V 5V 5V 5VnF # *    >>"%>	> > > >   * 8  8  8  8DD3 D D D D
 4:5 5#5-05	sEHN"	#5 5 5 5)%(. ) ) ) )*   <:T#Y=M : : : :B >B	$ $S#X$ CHo$  s 23d:	$
 
$ $ $ $L&8BG#4 & & & & &# # # #
    X       X & & & & & - 	       F	I  C  D        . )ux~ ) ) ) X) \*58> *d * * * \* 7T#rx-%8 7 7 7 X7 4$rw'<"= 4 4 4 X4 1tEL%8S2T$TU 1 1 1 X1A# A A A A A A@ @ @ @ @ @ @@ @ @ @ @ @ @ 
 #'	 } /0 4Z	
 
/	0   :    X .5,; . . . X. 88 8 8 8 X8
 A/ A A A XA >DcN > > > X> FD)C$D F F F XF HL'
 '
U8CH#5t#;<<=D'
	'
 '
 '
 '
R$ $ $ $& & & & N N N XN
6 
c 
 
 
 
'S ' ' ' '0 0 0 0 0JT J J J J# %(/C*?     C C    5C 5D 5 5 5 5
      \.,/	   &c%,.4c c 	c
 
c c c cJY/-Y/	tK $vv~"66	7Y/ Y/ Y/ Y/vh"-h";>h"	tO$&99	:h" h" h" h"\ 	l& l&'l& #l& 	l&
 
!	"l& l& l& l&\F ; F Ft F F F F2"G'"G ?+"G 	"G
 "G 
"G "G "G "GH1 1 1 1< S      4Dd39o1E,F    C C    2 c    "!7 !7 !7 !7F'()'/J'	' ' ' 'R>()>/J>	> > > >@w)'w) !w) 	w)
 
k	w) w) w) w)r	 7d27m 7 7 7 X7 E4> E E E XE@.@26v,@	@ @ @ @a.a26v,a	a a a aFDeh&:!:;    S %(2F 3    U\ 2    
' ' ' '
X0 X0 X0 X0t '  '  '  'D d;.?  D         n S>	
 
   8
Q# 
Qc 
Qd 
Q 
Q 
Q 
Q3 s s    	S 	 	 	 	 	 	$ $ $ $:?"*BN+;T+A"B?	? ? ? ?
,%(- ,C , , , ,$2#ho$27=$2	$2 $2 $2 $2 $2 $2r   rs  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dS )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 | _        d S |j        | _        d S r   )r'  r(  r  r  r  r*  r)  )r   r  r  s      r   r   zDynamoTracerOutput.__init__-  s]     %+$?!*0*K'~)(.% 	. $D &Dr   c                     | j         }|rX|j        D ]}|j                                         |j        j        r+|j        j        j        rd |j        j        j        _        d S d S d S d S r   )r*  r  r   _clear_nodesr  r  r2  r  )r   r)  r  s      r   _cleanup_output_graphz(DynamoTracerOutput._cleanup_output_graph:  s    4 	V&. , ,))++++ ,6V 0:DV RV,6@NNN	V 	VV V V Vr   r   r   )r   r   r   r   r   rs  rQ  r	   r   r   r   r.  r   r   r   r&  r&  #  s          $$$$$$$$ *D000038_t####CH~ HL. .1.:=*.	. . . .
V 
V 
V 
V 
V 
Vr   r&  a  With the current config, we will graph break (and fall back to eager-mode PyTorch) on all ops that have do not have the 'pt2_compliant_tag'. Please see the following doc for how to mark this op as PT2 compliant https://pytorch.org/tutorials/advanced/custom_ops_landing_page.htmlr)  kindr  r   r   c                     |dk    rd S dt           j        j        dd f fd}dt           j        j        dt          dd f fd}t	          |t           j        j                  r7t           j        j        |j        v r ||           d S  ||d| d           d S t	          |t           j        j                  rSt          |
                                          }t          |          d	k    rMt          ||d
                   }t           j        j        |j        v r ||           d S  ||d| d           d S t           j        j                             j        ||fd          \  }}	 t          j        j        |j        g|R i |}	n7# t(          $ r*}
t+          ddt          |
          g            Y d }
~
nd }
~
ww xY wt          ||	          }t           j        j        |j        v r ||           d S  ||d| d|	 d           d S d S )Nrc  r  r   c                 R    | j         dv rd S j                            |            d S )N>   atenprimprims)	namespacer  r   )r  r)  s    r   encountered_compliant_opz8check_pt2_compliant_op.<locals>.encountered_compliant_opV  s4    888F)--f55555r   r   c                     j                             |            t          j        r t	          dd|dz   t
          z   g            d S d S )Nz Encountered non-PT2-compliant oprk   r)  )r  r   r-   only_allow_pt2_compliant_opsrE   err_epilogue)r  r   r)  s     r   encountered_non_compliant_opz<check_pt2_compliant_op.<locals>.encountered_non_compliant_op[  se    &**6222. 	:#I4	     	 	r   z%Encountered the torch.ops.OpOverload z that is not PT2 compliant.r,   r   z:Encountered the non-overloaded torch.ops.OpOverloadPacket z that is not PT2 compliant. Fz*Error when attempting to resolve op packetrk  r)  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r   _ops
OpOverloadr   r   Tagpt2_compliant_tagtagsOpOverloadPacketrQ  	overloadsr  r   r@  rX  get_fake_values_from_nodesr  r?  _jit_resolve_packet_qualified_op_namer   rE   )r)  r/  r  r   r   r6  r;  rB  r  overloadr  s   `          r   check_pt2_compliant_oprG  P  s    6)> 64 6 6 6 6 6 6
UZ-B  QU       &%*/00 9&&+55$$V,,,F$$WFWWW	
 	
 	
 	&%*566 )&**,,--	 y>>Q1..By*bg55((,,,((/.4/ / /   F}*EE#dF^U
 
f
	x3),0  4: HH  	 	 	DFF	        	 VX&&9&"'11$$R(((((((&f & &3;& & &    I) )s   +F 
F? F::F?PRc            
       Z    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
dS )
r  r  r  r   r   r   r   Nc                 >    || _         || _        || _        || _        d S r   )r  r   r   r   )r   r  r   r   r   s        r   r   zLazyProxy.__init__  s$     	r   c                 0     | j         | j        i | j        S r   )r   r   r   r   s    r   r  zLazyProxy.__call__  s    tw	1T[111r   )r   r   r   r   rH  rI  r   r   r   r	   r  r   r   r   r  r    s          QTN v	
 ( 
   2# 2 2 2 2 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	 	 d0d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j        ej        z  d'edz  d
dfd(Zd)ej        d
ee j!                 fd*Z"d
e#fd+Z$d
e%fd,Z& xZ'S )1r  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.
    NFr)  rs  r  r|  r  r  r   c                 0   t                                                       t          j        |          | _        t
          j                                        | _        || _	        i | _
        i | _        || _        || _        || _        i | _        i | _        i | _        d | _        d| _        d| _        d| _        d | _        t/                      | _        d| _        |
|j        dz   nd| _        d | _        d | _        d | _        d | _        | j        g | _        n/| j        j        | j                             |          |fgz   | _        t/                      | _!        g | _"        t          j#                    rtI          d          t/                      | _%        d S )NFr,   r   zSInference mode is supposed to be disabled during compilation. Please open an issue.)&r   r   weakrefr  r)  r   r   r!  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   r)  r  r|  r  r  r   s         r   r   zSubgraphTracer.__init__  s    	#M,77X^^%%
 # 9; =? *& :< NP >@!
 =B9;@8 */& AE 3=,, ,1(:@:L 2Q 6 6RS/3AE 04!
 ;.0D  #';#>**=99=IB $D 
 -7LL 8:)*,, 	e   BL!!!r   r  c                 :    | j                             |           d S r   )rb  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        z
  d           }|Q| j         |         }t          j        j        D ]}||v r||         |j	        |<   d|v r|d         |j	        d<   d S d S d S d S d S d S )Nstack_trace)
r[  r\  r]  current_instructionstarts_lineri  r   r  _COPY_META_FIELDSr3  )r   r~  r   linenonode_idxr3  r   s          r   re  z,SubgraphTracer._maybe_preserve_original_meta&  s     	C(	C )	C
 +7FH!377T66  #)(3X7 7 7E}}+/;	%( D((/3M/BDIm,,,#	C 	C 	C 	C 	C 	C $#
 )(r   r/  r  r   r   r  	type_exprproxy_factory_fnc           	      "   t          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_proxyr)  r  r
  )	r   r/  r  r   r   r  rl  rm  _t0s	            r   r  zSubgraphTracer.create_proxy<  s     lnn	%%fdFD)=M  6FF$FFFD6FF$FFFFFFs   A 0Bc           	      
   | j         bt          j        ||f          \  }}	g }
|D ],}|                     |          }|
                    |           -t          j        |
|	          \  }}t                                          ||||||          | j        j	        }t          j        dk    r|dv r|j        | j        uryj        rj        j        f|j        |                    j        j                  dt$          ffd}t&                              dt+          |                     | _        d}|j        | j        ur t/          j        |j                                      dd	                       }t5          |t6          j        j                  r@d
}d |j        j        D             | _         |j!        | _"        |j#        j$        j%        | _&        nd | _         d | _"        d | _&        |j'        }|r!|(                                j)        j*        d<   |dv rj)        j+        f}|rtY          j)        j*        d         -                                          d         d         }|j.        /                    d          r(|j.        /                    d          sj)        j+        |f}| j0        |gz   j)        j*        d<   n|dk    r| j         tc          dd| j2         dg            | j0        j)        j+        tg          fdj)        j*        d         4                                D                       fgz   j)        j*        d<   | 5                    |j)                   |sdj)        j*        vr*|j'        }|r!|(                                j)        j*        d<   dj)        j*        vr|dv r%| j0        j)        j+        fgz   j)        j*        d<   n`|dk    rZ| j         tc          dddg            | j0        j)        j+        j)        j*        d                  d         fgz   j)        j*        d<   dj)        j*        vrg }|rN|6                                s'|                    |7                                           tq          |dd           }|Nd |D             }|9                                 tt          j;        <                    |          =                                }d>                    |          j)        _?        t6          j@        jA        jB        st6          j@        jA        jC        r/| j        jD        E                    | j        j	        j)                   S )Nra  r  )rj  r   c                  r    t                                                    } dj        j         d d|  S )NzTRACE FX call z from r  )rl   rstripr   r  )linecur_instheaderr  tx_codes    r   get_trace_call_log_strz<SubgraphTracer._create_proxy.<locals>.get_trace_call_log_str  s@    5gxHHOOQQDPBGLPPPP$PPPr   rD  Forig_graphmodulec                      d S r   r   r   r   r   rY  z.SubgraphTracer._create_proxy.<locals>.<lambda>  s    D r   Tc                     g | ]	}|j         
S r   r  )r   nds     r   r   z0SubgraphTracer._create_proxy.<locals>.<listcomp>  s'     & & & "BG& & &r   nn_module_stack>   r  rc  rz  r,   )ztorch.nn.modulesz	torch.ao.ztorch.nn.modules.containerr^  r  z4Invoking an nn.Module inside a higher order operatorzHigher order op name: zThis is not supported.r)  c              3   f   K   | ]+\  }\  }}|                     d           d         k    '|V  ,dS )@r   N)r  )r   r   r   tyr  s       r   r^  z/SubgraphTracer._create_proxy.<locals>.<genexpr>  sP        &Aw2773<<?f44 4444 r   z2Invoking an nn.Module inside a HigherOrderOperatorrk  rf  r  c                 <    g | ]}|j         t                      v|S r   )r
  r'   )r   frames     r   r   z0SubgraphTracer._create_proxy.<locals>.<listcomp>  s6     ( ( (>)<)>)>>> >>>r   )Fr  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r  r)  r  rZ  r[  rg  rR  	positionsrj  rx  get_line_of_code_headerr   trace_call_logr  rt   rZ  r>   get_contextri  r   r   r   r  r   r  r[  _lineno_mapr\  r   __code__r  r]  r}  r   r   r3  r  r   r7  r   
startswithr^  rE   r  r  r   re  is_co_filename_from_nn_modulesframe_summaryr   r  r   StackSummary	from_listr  r  rf  r@  r-   r  track_nodes_for_deduplicationr  
track_node)r   r/  r  r   r   r  rl  rm  	flat_args	tree_specnew_flat_argsrD  maybe_new_argr~  rx  is_retracingorig_graphmodule_mayber}  r   current_nn_moduleframe_summariesfiltered_frame_summariesmsgsru  rv  r  rw  r   s     `                    @@@@r   ro  zSubgraphTracer._create_proxyP  sU   V ;"#)#6f~#F#F IyM  4 4 $ H H M M$$]3333!0	JJLD&WW!!
 
 ) w&&4 4
 ,
 ,

 -H..&2&-9)338;M;T3UUQ Q Q Q Q Q Q Q Q Q $$T:6L+M+MNNN!) 9DN**&\%=bi%H%H%L%L"LL& & & &" 0%(2FGG 1#& &&<&B&H& & &" ,B+M(*2;J )) &*"+/(,0), 	E.=.B.B.D.DBGL*+333W\6*E > %)6G)H)O)O)Q)Q$R$RSU$V%! %/::5  >+6AA0 >
  W\+<=E.2.BeW.LBGL*++]""{&RIT5GII 8	    /3.BGL    *,',7H*I*O*O*Q*Q    	F 	/BGL*+ 	**2rw777 	 44"$"4" M6E6J6J6L6LBGL!23 44;;;6:6Jv.N 7BGL!233 ]**{.%$X$&(@"$	    7;6JGLGL):;FCAFN 7BGL!23 ,,<>O 1 88:: ?#**2+;+;+=+=>>>R400  1( (,( ( ($ %,,... )334LMMTTVVD"$''$--BG M 8	}#A	 ,77!,bg   	r   c                    t          | j        ||||           | j        Pt          j        |i |}|D ]<}t          |t          j        j                  s"|j	        | j	        k    s
J d            =t                                          ||||||          }	| j        j        |	j        d<   | j                            |	j                   |	S )Nz2create_node using arg not from this SubgraphTracerr  )rG  r)  r  r  arg_tree_leavesr   r   r   r  r   r   r  r  r3  r`  r   r  )r   r/  r  r   r   r  rl  r  rD  r   r   s             r   r  zSubgraphTracer.create_node  s     	t0$fMMM;".???I   !#ux}55 yDJ...H /... ww""4vtYOO*.*;*E	&'TY'''r   c                    t          |j                  dk    rtg }|j        D ]K}|j        | j        k    r9|                    t	          t          |j        j                                       L|D ]}|j                            |           | j                            |           | j        	                    |j
        d            d S rv  )r  r  r   re  rr  r   r  r  r  r   r  )r   r   user_graph_nodesuserother_graph_nodes        r   r  zSubgraphTracer.remove_node8  s    tz??Q46
 N N :++ %++HT$*:J5K5K,L,LMMM$4 D D  &112BCCCC
d### $$TY55555r   r5  beforer  c                    t          |t          j                  r| j                            |j                   t                              d|||j        nd|| j	        |           || j
        J d| d| d            | j        r`| j
        Y|J t          |d          sD| j        j                            |g                               t!          j                               t%          || j                  }| j        rkt+          t-          | j                            }| j        |         j        }|r| j                            |          }n5| j                            |          }n| j                            d           }|5  |                     d|d	i |
          }	t9          |	j        |           | j        r3|r1| j                                        \  }
}|	| j        |<   || j        |
<   n
|	| j        |<   | j                            |           | j        }t          j                                         }|s|st          |t          j                  r| !                    ||           nxt          |tD          tF          f          r\tI          |          D ]L\  }}t          |t          j                  s d }|rtK          ||d          }| !                    ||           Mt          |t          j&                  r8t          |j        j'        tP          j)                  r|	| j*        |j        j'        <   |	cd d d            S # 1 swxY w Y   d S )Nz7create_graph_input %s %s %s at debug_level %s before=%sz(none)z0you are required to provide a source for inputs z example_val z on the root tracerT)only_allow_inputr  r   rl  F)r  r<  index_is_slice)+r   r   r   r  r   r  r   r  r  rY  r  r|  r[   r)  r  r  r   r  ro   r`  r  r  rr  r   r   inserting_beforeinserting_afterr  rw   popitemr   compileris_compiling_lift_basic_symbolsr   rQ  r  rX   r+  r  r  r  r  )r   r  rl  r5  r  r  	prev_namer   ctxr  r   r   is_strict_exportis_non_strict_exportr]  r  e_sources                    r   r2  z!SubgraphTracer.create_graph_inputN  s    mU\22 	M-44]5KLLL		E!-FKK8	
 	
 	
 >;**x4xxVcxxx +** > 	dk1%%%'FFF !7BB62NNUU"022   #4)9::# 	4Xd&>??@@I+I6;D 7j11$77j0066*--d33C F	 F	%%mT2rY%WWEej-888' 7F 7/7799116(../(++16(.
   &&&@  $~#(>#>#>#@#@ # >,@ >mU\:: >,,]FCCCCe}== > )- 8 8 > >1)!U\:: %$#'! '4%+&'/4( ( (H 00H==== -66 D:"'< < D ?D"=#5#:;MF	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	 F	s   F3MMMr  c                 >   | j         
J d            |j        j        d         }t          |t          j                  r*|j        j        | j        v r| j        |j        j                 S || j        v r| j        |         S |j	        | j         k    r| j         
                    |           |j        j        d         }t          |t                    rt          |j                  nt          |          }|                     |j        j        ||          }|| j        |<   |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr5  )r  r   r3  r   r   r+  r  r  rP  r  lift_tracked_freevar_to_inputr   r1  real_objr2  r  )r   r  r5  rl  	new_proxys        r   r  z,SubgraphTracer.lift_tracked_freevar_to_input  s#    {&&W '&& 
8 }el33	?"'4+===%m&8&=>>
 D((('..
 <4;&&K55e<<<
8 -)9::%D'(((m$$ 	
 ++EJOYVV	&/U#r   rD  c                     t          |t          j        j                  s>t          |t                    r't	           fd|j        |j        |j        fD              S |S |j         k    r|S  	                    |          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   B   K   | ]}                     |          V  d S r   )r  )r   sub_argr   s     r   r^  zESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>  sE        # @@II     r   )
r   r   r   r   slicestartstopstepr  r  )r   rD  s   ` r   r  z2SubgraphTracer.maybe_lift_tracked_freevar_to_input  s     #ux~.. 	
 #u%%    (+	38SX'F    
Z4J11#666r   e_proxyc                     j         t          t                    sJ dt          dt          f fd}dt          dt          dt          dt
          j        ffd}t          |t          j                  rt          |
                                          D ]\  }} ||          rxt                              d||j                   t          ||d	t          j        j        j        j        |fi t'          |          
          }                     ||           |                                } ||          rqt                              d|j                   t          ||d	t          j        j        j        fi t'          |          
          }                     ||           |j        t          j        u rt          |                                          D ]\  }} ||          rxt                              d||j                   t          ||d	t          j        j        j        j        |fi t'          |          
          }                     ||           nC|j        t          j        u rQ                     |                                                                |                                           n|j        t          j        t          j        hv rQ                     |                                                                 |!                                           no|j        t          j"        t          j#        hv rP                     |$                                                                |%                                           tM          |          rP|'                                \  }	}
|	D ]8}tQ          ||          }                     |tQ          |                     7d S d S t          |t          j)                  r# ||          r|j*        j+        }j,        |<   d S d S d S )Nr  r   c                     ddl m}  ||           o6t          | j        j        t
          j                  o| 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)  sS    IIIIII A :qv{EL99:FKt'99r   r5  r   r   c                 B   t          t                    r
             nt          t          j        j                  sJ j                            j                  5   j        |i |}t          |j        |            |cd d d            S # 1 swxY w Y   d S r   )
r   r  r   r   r   r   r  r   r  rw   )r5  r   r   r  r  r  s       r   _proxy_with_example_valuezHSubgraphTracer.track_produced_symints.<locals>._proxy_with_example_value2  s    
 $.gy#A#ANggiiiwGgux~66666--gl;;  ++T<V<<!%*m<<<                 s   #$BBBz=track_produced_symints %s for %s.size()[%s] at debug_level %src  r  zCtrack_produced_symints %s for %s.storage_offset() at debug_level %sz?track_produced_symints %s for %s.stride()[%s] at debug_level %s)-r  r   r  r	   r   r   r   r   r   r  r  r   r  rY  r  opsr2  sym_sizer  r1  track_produced_symintsstorage_offsetsym_storage_offsetlayoutstridedstride
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr+   __tensor_flatten__r   r+  r   r  r  )r   r5  r  r  r  r]  r  
lazy_proxyr  attrsr  r~  inner_tr  r  s   ` `           @r   r  z%SubgraphTracer.track_produced_symints  s     &.11111	 	 	 	 	 	 	 	
	
	'*
	69
	X
	 
	 
	 
	 
	 
	 
	 mU\22 Q	5!-"4"4"6"677 ? ?19Q<< ?IIW*   "+1'	/3 !"&q''	" 	" 	"J //:>>>*99;;Ny(( H		Y"&	   '-"#IN5J">22	 	 	
 ++NJGGG#u}44%m&:&:&<&<== C CDAq y|| C		]#".   &/"5+!IN59$aL&*1gg	& 	& 	&
 33AzBBB'C* %)999++M,B,B,D,DgNNN++M,A,A,C,CWMMMM%%*:E<L)MMM++M,F,F,H,H'RRR++M,E,E,G,GQQQQ%%*:E<L)MMM++M,F,F,H,H'RRR++M,E,E,G,GQQQ,];; Q*==??
s! Q QD%mT::G//$9O9OPPPP	Q QQ Q u|44 	5y'' 5$).-4$T***	5 	55 5r   r]  c           	      *    	 d	dt           t          j        z  dt          d z  dt          dd f fd}t          |t          j                  rt          |                                          D ]0\  }} |||t          |t          j        |          nd d           1|j        t          j        u rt          |                                          D ]0\  }} |||t          |t          j        |          nd d           1 ||                                |t          |t          j                  nd d           nC|j        t          j        u rQ                     |                                |                                |                                |           n|j        t          j        t          j        hv rQ                     |                                |                                |                                |           no|j        t          j        t          j        hv rP                     |                                |                                |                                |           t=          |          rT|                                \  }}|D ]<}tA          ||          }	                     |	|tC          ||          nd            ;d S d S t          |t          j                  r |||           d S d S )
NFr  r  r  r   c                    t          |           sd S t          | t          j                  sJ                     |           }t          |          dk    rd S j        ˉj                            | |           |D ]}j        j        |         }|j	        j
        d         }t          |t          j                  sJ                     t          |          t          |          |||          }t                              d|||j        ndj                   |j        |<   d S t          |          dk    sJ d| d|              |J d	|  d
|  d            t'          t)          |                    }                    t          |          t          |           | ||          }t                              d| ||j        ndj                   t+          || dd d          |j	        j
        d<   d S )Nr   r5  )r  r  z4_lift_symbols_in_symint %s from %s at debug_level %szsubgraph inputsr,   zyFor root tracer, we only expect to bind basic symbols (compound symbols should be cached before) but got unbound symbols z in zSource of 'z' is None when lifting it to input of top-level. If it's an unbacked symbol, this could be because it's not tracked with lazy_bind_unbacked_symbols. Otherwise, should provide a source when create_graph_input for `z` at root tracer.F)pass_arg_as_tensorr'  rx  r0  )r$   r   r   r+  lookup_unbound_symbolsr  r  r  r  r   r3  r2  r   r1  r   r  r  rY  rP  r  iterry   )	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_symint  s   
 q>> a.....#::1==#$$)) {&//6:::* < <B#';#<R#@L"."3"8"IK%k5<@@@@@00B[))#%% 1  B IIN'-'9?P(	   :<D(66#< <( +,,111bHXb b^_b b 211 ))l! l lWXl l l *))
 $/0011,,GGGG!! -   		J#)#5FKK;L$	   ,4', $#, , ,Z(((r   T)r  r  )"r  r   r+  r   r   r   r   r  r  rb   ra   SIZEr  r  r  STRIDEr  STORAGE_OFFSETr  r  r  r  r  r  r  r  r  r  r  r  r+   r  r   rT   )
r   r5  r]  r  r]  r  r  r  r~  r  s
   `         r   r  z"SubgraphTracer._lift_basic_symbols  s    !B	 B	U\!B	TMB	 B	 	B	 B	 B	 B	 B	 B	H mU\22 3	!-"4"4"6"677 	 	1'' ? -S.2EqIII!     #u}44%m&:&:&<&<== 	 	DAq++  # 1n6KQOOO!%#     ('!0022 ? -S.2OPPP!     %)999(()?)?)A)A3GGG(()>)>)@)@#FFFF%%*:E<L)MMM(()C)C)E)EsKKK(()B)B)D)DcJJJJ%%*:E<L)MMM(()C)C)E)EsKKK(()B)B)D)DcJJJ,];; *==??
s!  D%mT::G,,#/C!6!6!6t   	  
 u|44 	##    	 	r   r  c                    |j         j        j        }t          |          dk    rg S g }|D ]}|| j        vr|                    |           !| j        |         }t          |t                    r |            }|| j        |<   t          |t          j	        j
                  r	|j        | u sJ 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   rY  z7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>'  s     r   )r  )r   r  r"   r  r  r   r   r  r   r   r   r  r  )r   r  r"   to_be_boundr  r  s         r   r  z%SubgraphTracer.lookup_unbound_symbols  s    v{/|!!I 	 	B+++""2&&&&r*E%++ /)."2&eUX^44 9M9M9MMrMMM :N9MM9M k'7'78888r   c                    | j         }g g }| j        j        D ]d}|j        dk    rW|j        d         }t          |t          j                  r/|                    |j	                                       |           d d t          t          ||                    D             }|r#fd|D             }d| }t          d|          S t          dd          S )	Nr  r5  c                 *    g | ]\  }\  }}||k    |S r   r   )r   r]  v1v2s       r   r   z5SubgraphTracer.has_input_mutation.<locals>.<listcomp>7  s5     
 
 
8B Rxx	  xxr   c                      g | ]
}|         S r   r   )r   r]  input_nodess     r   r   z5SubgraphTracer.has_input_mutation.<locals>.<listcomp>@  s    DDD[^DDDr   zInput mutation detected at TFrk  )r  r   r  r  r3  r   r   r   r   r  r  ra  r   )	r   input_versions_at_beginninginput_versions_at_endr   r5  mutated_inputsmutated_nodesr   r  s	           @r   has_input_mutationz!SubgraphTracer.has_input_mutation)  s   &*&G# "J$ 	 	Dw-'' $	/ :mU\:: -)001GHHH&&t,,,
 
(/1FGG   
 
 
  	+DDDD^DDDM???Cc***E2&&&r   c                    ddl m} ddlm} t	                      | j        j        D ]s}|j        dk    rf ||g          d         }t          |t          j
                  r9 ||          D ]-}|v r"d|          d| }t          d|          c c S ||<   .s t	                      | j                            d	          d         }t          j        |j        d                   D ]}|r| ||g          d         }t          |t                     rJ t          |t          j
                  r9 ||          D ]-}|v r"d
|          d| }t          d|          c c S ||<   .                                                                z  }	t%          |	          dk    rCfd|	D             }
d                    d |
D                       }
d|
 }t          d|          S t          dd          S )Nr   )get_tensor_storages)_collect_fake_inputsr  z*Input-to-input aliasing detected at nodes  and Tr  r  z,Output-to-output aliasing detected at nodes c                 0    g | ]}|         |         fS r   r   )r   r  input_storagesoutput_storagess     r   r   z/SubgraphTracer.has_aliasing.<locals>.<listcomp>j  s5       <="OA$67  r   z, c                 "    g | ]\  }}| d | S )r  r   )r   r]  os      r   r   z/SubgraphTracer.has_aliasing.<locals>.<listcomp>m  s&     D D DDAqAA D D Dr   z+Input-to-output aliasing detected at nodes Frk  )(torch._dynamo.variables.higher_order_opsr  torch._higher_order_ops.utilsr  r   r   r  r  r   r   r   r   r  r  tree_leavesr   r   r  r  r  )r   r  r  r   r5  storager   	out_nodesout_nodeintersected_storagesaliasedr  r  s              @@r   r   zSubgraphTracer.has_aliasingF  s   PPPPPPFFFFFF>BffJ$ 	 	Dw-'' 4 4dV < <Q ?mU\:: 7#6#6}#E#E 7 7"n44"s~^eOf"s"smq"s"sC#/c#:#::::::26w//?CvvJ))X)66q9	*9>!+<== 
	< 
	<H 	< 4 4hZ @ @ C%mT:::::mU\:: <#6#6}#E#E < <"o55"zQ`ahQi"z"zpx"z"zC#/c#:#::::::3;00-22447K7K7M7MM#$$q((    AU  G ii D DG D D DEEGIIICc***E2&&&r   )NFNN)NNNrp  )FN)(r   r   r   r   r   r   r(   r   r   r~   rd  r   r  re  r	   r   r   r  ro  r  r  r   r2  r  r  r  r   r  r+  r   r  r   r  r  r  r   r  r   r   r   r   s   @r   r  r    s         .2'+"&eN eN#eN )*eN 	eN
 }eN 4ZeN 
eN eN eN eN eN eNN*o *$ * * * *C-C57WC	C C C C8   $AE   	
  Dj : #BG9bh#67$> 
   4   $AEL LL L 	L
 L DjL :L #BG9bh#67$>L 
L L L L L Ld  $   	
  Dj : 
     46 6D 6 6 6 66  $z zz z 	z
 z z 
z z z zz)28 )	BH@T ) ) ) )V7s 7s 7 7 7 7@y5 y5+4ux~+Ey5	y5 y5 y5 y5x~"\EL8~?E}~	~ ~ ~ ~D9 9el9K 9 9 9 9*'L ' ' ' ':+'l +' +' +' +' +' +' +' +'r   r  r   (  r   r  r  r   r  r  rt  r  r/   r  r  rZ  r  r   r  r  rO  collections.abcr   r   r   r   r   rP  r   typingr	   r
   r   r   r   typing_extensionsr   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedr  torch.nntorch.utils._pytreerX  rY  r  r   r   torch._C._dynamor   torch._dynamo.excr   r   r   r   r   r   r   r   "torch._library.fake_class_registryr   torch._library.opaque_objectr   torch._subclasses.fake_tensorr   torch._utils_internalr   torch.export.dynamic_shapesr   rp  r    %torch.fx.experimental._backward_stater!   r  r"   r#   r$   r%   r&   r'   torch.fx.noder(   torch.fx.passes.runtime_assertr)   torch.utils._ordered_setr*   torch.utils._python_dispatchr+   rk  r-   r.   r   r0   backends.registryr1   r2   r  r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r  r?   current_scope_idr@   device_interfacerA   rB   rC   rD   rE   rF   ra  rG   rH   graph_deduplicationrI   graph_id_filterrJ   rK   graph_region_trackerrL   rM   rN   mutation_guardrO   rt  rP   rQ   rR   r  rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   variables.builderrx   ry   rz   r{   variables.ctx_managerr|   variables.functionsr}   r~   variables.listsr   variables.miscr   variables.nn_moduler   r%  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_logrn  graph_sizes_logr  rw  r   r   r.  r  r-  r  r   r   r   r   r   r   rQ  r   r  r   r  r   cacher   r   r   r   r   objectr8  r  r  rF  rS  ra  rc  rs  r&  r:  rG  r  r  rH  rI  r  Tracerr  r   r   r   <module>r7     s~   *                         				 



         9 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4 4 4       < < < < < < < < < < < < < < 0 0 0 0 0 0 0 0                       $ $ $ $ $ $ $ $ $         # # # # # # N N N N N N N N                @ ? ? ? ? ? 7 7 7 7 7 7 4 4 4 4 4 4 0 0 0 0 0 0 9 9 9 9 9 9 : : : : : : ? ? ? ? ? ?                !           J J J J J J / / / / / / F F F F F F D D D D D D D D D D D D 5 5 5 5 5 5 5 5                          ' & & & & &       - - - - - - 6 6 6 6 6 6              S R R R R R R R : : : : : :        5 4 4 4 4 4 / / / / / / / / 0 0 0 0 0 0 W W W W W W W W W W                                   $                                             .            ; : : : : : H H H H H H H H - - - - - - ( ( ( ( ( ( 1 1 1 1 1 1         
 C B B B B B ; ; ; ; ; ;  @AAAAAA444444HHHHHH444444??????g!!N44XwGG 11(LII.228]KK11(LII*  "'!> $)HO$D !&*38nc3h   & $       
 $        6:) )uU\3:%=>?)%*+d2) 			"#) ) ) )X 4c 4 4 4 4 - - - - - - - -# (2tCy=2I             UX_       ! ! ! ! ! ! ! !H 	S&[ N N N N N N N N: <
 <
 <
 <
 <
 <
 <
 <
~ X X X X X X X X" ' ' ' ' ' ' ' ',E d38n    (F" F" F" F" F". F" F" F"RU+2 U+2 U+2 U+2 U+2# U+2 U+2 U+2pV!V !V !V !V !V !V !V !VJJ HH%(H25H=@HJMH	H H H HV &io'' IcNNGCLL2 2 2 2 2 2 2 2$~' ~' ~' ~' ~'RY ~' ~' ~' ~' ~'r   