o
    Zh&                    @   sL  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mZ ddlmZmZmZmZmZmZ ddlZddlZddlZddlmZ ddlZddlm  m Z! ddlm"Z" ddl#m$Z$m%Z% ddlm&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, dd	l-m.Z. dd
l/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z?mZ@mAZA ddlBmCZCmDZD ddlEmFZFmGZGmHZHmIZImJZJ ddlKmKZK ddlLmMZM ddlNmOZO ddl>mPZPmQZQmRZRmSZSmTZT ddlUmVZV ddlWmXZX ddlYmZZZm[Z[ ddl\m]Z] ddl^m_Z_m`Z` ddlambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZo ddlmpZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZmZ ddlmZ ddlmZmZmZmZ dd lmZ dd!lmZmZ dd"lmZ dd#lmZmZmZmZ dd$lmZ erdd%lmZ eeZejed&Zejed'Zejed(Zejed)Zed*d+G d,d- d-ZG d.d/ d/Zedd0d1 ZeG d2d3 d3Zd4d5 ZG d6d7 d7ejjZG d8d9 d9Zeeef ZG d:d; d;Zd<Zd=d> Ze ZG d?d@ d@ZG dAdB dBe"jZdS )Ca  
Core graph building functionality for PyTorch's Dynamo system. This module contains
the essential components for constructing and managing FX graphs during compilation:

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

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

The module supports key Dynamo features including:
- Higher-order operators through nested SubgraphTracers
- Graph deduplication for optimization
- Symbolic shape handling and propagation
- Side effect tracking and management
- Guard insertion and management
    N)	dataclass)AnyCallablecastOptionalTYPE_CHECKINGUnion)fx)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourceTracingContext)
FakeTensor)signpost_event)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnv)insert_deferred_runtime_asserts)is_traceable_wrapper_subclass   )configexcgraph_break_hintslogging	variables)
CompiledFn
CompilerFn)create_call_functioncreate_instructioncreate_load_constInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplemented_v2unimplemented_v2_with_warning)apply_graph_deduplication)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffects)
AttrSourceBackwardStateSourceConstantSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceNumpyTensorSourceParamBufferSourceShapeEnvSourceSyntheticLocalSourceTensorPropertyTensorPropertySource)_extract_tensor_dictcheckpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedget_instruction_source_311get_locals_to_stealget_static_address_typeget_unique_name_wrtgraph_break_reasonsincrement_op_countlazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)VariableTracker)BackwardStateGraphArgGraphArgTrackedFakewrap_fx_proxy)BaseListVariable)CellVariableNullVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariable)TensorWithTFOverrideVariable)InstructionTranslatorBasegraphZ
graph_codeZgraph_sizesZ
trace_callT)frozenc                   @   s   e Zd ZU eed< eed< dS )VariableTrackerCacheKeyZvt_idsourceN)__name__
__module____qualname__int__annotations__r    ro   ro   I/var/www/auris/lib/python3.10/site-packages/torch/_dynamo/output_graph.pyrh      s   
 rh   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )VariableTrackerCachec                 C   s
   i | _ d S N)cacheselfro   ro   rp   __init__      
zVariableTrackerCache.__init__c                 C   s&   t t||}|| jvrd S | j| S rr   rh   idrs   )ru   valueri   keyro   ro   rp   lookup   s   

zVariableTrackerCache.lookupc                 C   s   t t||}|| j|< d S rr   rx   )ru   rz   ri   vtr{   ro   ro   rp   add   s   zVariableTrackerCache.addc                 C   s   t  }|j| j |S rr   )rq   rs   update)ru   Z	new_cachero   ro   rp   clone   s   zVariableTrackerCache.clonec                 C   s   | j   d S rr   )rs   clearrt   ro   ro   rp   r         zVariableTrackerCache.clearN)rj   rk   rl   rv   r|   r~   r   r   ro   ro   ro   rp   rq      s    rq   c                   C   s
   t tS rr   )torchdynamo_loggingZget_step_loggerlogro   ro   ro   rp   _step_logger      
r   c                   @   s<   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.reasonZ
user_stackTgraph_breakc                 C   s   | j r
t|  d S d S rr   )r   rP   appendrt   ro   ro   rp   __post_init__   s   z GraphCompileReason.__post_init__N)rj   rk   rl   __doc__strrn   list	tracebackFrameSummaryr   boolr   ro   ro   ro   rp   r      s   
 r   c                    s    fdd}|S )Nc                      s   dd  D S )Nc                 S   s    g | ]\}}}||i |qS ro   ro   ).0fnargskwargsro   ro   rp   
<listcomp>   s     zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>ro   ro   random_callsro   rp   _gen_rand_values   r   z1_get_gen_rand_values_fn.<locals>._gen_rand_valuesro   )r   r   ro   r   rp   _get_gen_rand_values_fn   s   r   c                       s@   e Zd ZdZdeeejjf f fddZ	defddZ
  ZS )FakeRootModulez'Trick the constructor of fx.GraphModule
nn_modulesc                    s,   t    | D ]
\}}t| || q	d S rr   )superrv   itemssetattr)ru   r   kv	__class__ro   rp   rv      s   
zFakeRootModule.__init__returnc                 C   s   dS )NzFakeRootModule(...)ro   rt   ro   ro   rp   __repr__   s   zFakeRootModule.__repr__)rj   rk   rl   r   dictr   torchnnModulerv   r   __classcell__ro   ro   r   rp   r      s    r   c                   @   s6   e Zd ZdefddZdejjdeej	 fddZ
dS )	WrapperBackendbackendc                 C   
   || _ d S rr   )r   )ru   r   ro   ro   rp   rv      rw   zWrapperBackend.__init__gmexample_inputsc                 C   s   t || _|| _t| j}| ||| _| jd u s!| j| jju r%| jjS tj	s+| jS z2z$| jjt
| }| jt
| }t||rJ| jW W |   S td|   ty]   td  w |   w )Nzincorrect results of backend zerror in verify_correctness)rF   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessrH   rU   RuntimeError	Exceptionr   	exception)ru   r   r   Zcopy_gmZcorrectresultro   ro   rp   __call__   s(   


	

zWrapperBackend.__call__N)rj   rk   rl   r"   rv   r   r	   GraphModuler   Tensorr   ro   ro   ro   rp   r      s     r   c                
       s  e Zd ZU dZeed< deeef de	e
 dededef
 fdd	Zd
d ZddefddZdd Zdd Zdd Zdeg ef fddZdd Zedd Zedd Zdd Zed d! Zejd"d! Zed#d$ Zed%d& Zed'd( Zd)d* Z d+d, Z!d-d. Z"e#j$d/d0 Z%ed1d2 Z&ed3d4 Z'ed5d6 Z(ed7e)j*j+fd8d9Z,ed7eeef fd:d;Z-dd=d>Z.d?d@ Z/dAdB Z0edCdD Z1dEdF Z2dGdH Z3dIdJ Z4ddLdMZ5dNdO Z6e7dPdQ Z8dRedSed7e9j:fdTdUZ;dVe<e)j=j>e)j?ef fdWdXZ@dYdZ ZA	<dd\e	eB fd]d^ZCd_d` ZDdadb ZEdcdd ZFdeefdfdgZGe#j$dhdi ZHdjdk ZIdldm ZJed7eKe9jL fdndoZMed7eKeN fdpdqZOdre9jPd7eQfdsdtZRdre9jPd7eQfdudvZSdwdx ZTdydz ZUd7eKe)j? fd{d|ZVdd}d~ZWdddZXdddZYdeKeZ d7d<fddZ[dddZ\d7efddZ]d7efddZ^dddZ_ded7d<fddZ`dee9jPgd<f d7d<fddZade)j9jLfddZb  ZcS )OutputGrapha  
    Wrapper class to hold outputs of InstructionTranslator.  Mainly the
    generated fx.Graph.

    OutputGraph is 1:1 with a frame being processed. Each frame is associated
    with some root InstructionTranslator. When user code calls a function,
    we construct a InliningInstructionTranslator that continues to write into
    the root InstructionTranslator's OutputGraph.
    side_effectscode_optionscompiler_fnexportlocal_scopeglobal_scopec                    s
  t    t| |dg| _i | _|| _|| _|| _i | _g | _	t
t| _t | _|	j|	j|	jd| _t | _g | _t| jtjtjtjtj| jd}dd lm  m} |jdd tj j!|| jrbdnd| jd}W d    n1 srw   Y  t"|| _#t$% | _&| '  t()t*| _+i | _,t-| | _.t/ | _0t12 | _3t4|| _5g | _6d| _7g | _8|| _9|| _:|| _;|| _<i | _=g | _>g | _?d| _@i | _AtjBC | _DtjBE | _F|
| _Gd| _Hti | _Iti | _J| K  i | _Lg | _Md | _Ng | _Oi | _Pd | _Qd | _R| S | _Tt | _Ud S )	N)	is_export)co_nameco_filenameco_firstlineno)tracked_fakesZallow_scalar_outputsZallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards'allow_complex_guards_as_runtime_asserts	co_fieldsr   FZ(fake_tensor_allow_unsafe_data_ptr_accessT)	shape_envZallow_non_fake_inputsr   )Vr   rv   SubgraphTracertracersinput_source_to_varr   export_constraintsframe_stateZinput_source_to_sizes_stridescleanup_hooksnext_compile_id_counter
compile_idsetinstalled_globalsr   r   r   r   r1   region_trackerr   r   r   Zcapture_scalar_outputsZ capture_dynamic_output_shape_opsr   r   torch._functorch.config
_functorchpatchr   _subclassesFakeTensorModer   tracing_contextr   Zcurrent_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   Ztracked_fakes_id_to_sourceparam_name_to_sourcer6   r   rq   variable_tracker_cache	itertoolscountunique_var_idr   r   output_instructions	timestampregister_finalizer_fnsr   r   r   root_txsource_to_user_stacks_current_txcleanupsshould_exitunspec_variable_map_CZ_is_torch_function_enabledtorch_function_enabledZ_is_torch_function_mode_enabledZtorch_function_mode_enabledtorch_function_mode_stack!has_user_defined_allowed_in_graphnon_compliant_opscompliant_custom_opssave_global_state dynamo_flat_name_to_original_fqnr   random_values_varpregraph_bytecodebackward_statebackward_state_proxybackward_state_var!install_builtins_dict_in_fglobalsZ%name_of_builtins_dict_key_in_fglobalsZguard_on_key_order)ru   r   r   r   r   r   r   r   r   f_coder   r   _config	fake_moder   ro   rp   rv     s   







zOutputGraph.__init__c                 C   s&   | j d }t|ts|j}| d|S )N__builtins__Z__builtins_dict__)r   
isinstancer   __dict__install_global)ru   
f_builtinsro   ro   rp   r     s   

z-OutputGraph.install_builtins_dict_in_fglobalshookc                 C   s6   | t | j }|| jvsJ || j|< ||  fS rr   )lenr   get_backward_state_proxy)ru   r  prefixnamero   ro   rp   add_backward_state_hook  s   
z#OutputGraph.add_backward_state_hookc                 C   sb   | j d u r.| jrtdddg d t }| jjdt||t d| _ t | j j	j
d< |  | _| j S )Nz&backward_state does not support export z3Compiled autograd doesn't work with `torch.export`.Zgb_typecontextZexplanationhintsZdynamo_backward_stateri   grapharg)r   r   r.   r   root_tracercreate_graph_inputtyper8   rX   nodemetanew_varr   )ru   example_valuero   ro   rp   r
    s$   

z$OutputGraph.get_backward_state_proxyc                 C   s   | j t tj | j t tj | j t tj | j t tj	 | j t tj
 tjj }|d urN| j t tj d S d S rr   )guardsr~   rA   
make_guardr2   Z	SHAPE_ENVr;   ZDETERMINISTIC_ALGORITHMSZ	GRAD_MODEZDEFAULT_DEVICEZTORCH_FUNCTION_STATEr   r   r   Zpeek_interpreter_stackZFUNCTORCH_STACK_MATCH)ru   ciro   ro   rp   r     s   zOutputGraph.init_ambient_guardsc                    s   | }|   }t| j   fdd  ttjj|  	t
|d  | | j   t|}t| j||}t jj| |S )z]
        call fn(*args) before the graph runs and turn the result into a fake input.
        c                      s     jjS rr   )Zload_import_fromrk   rj   ro   cgr   ro   rp   <lambda>  s    z3OutputGraph.synthetic_graph_input.<locals>.<lambda>F)r  r)   r   add_push_nullZforeachmapr    ZConstantVariablecreatecall_functionr	  storer   extendget_instructionsrB   rW   buildr   getguards_contextdynamo_guardsZremove_guards_with_source)ru   r   r   r  varnameri   r   ro   r  rp   synthetic_graph_input  s    

z!OutputGraph.synthetic_graph_inputr   c                 C      | j | d S rr   )r   r   )ru   r   ro   ro   rp   add_cleanup_hook,     zOutputGraph.add_cleanup_hookc                 C   s$   t | jD ]}|  q| j  d S rr   )reversedr   r   )ru   r  ro   ro   rp   call_cleanup_hooks/  s   zOutputGraph.call_cleanup_hooksc                 C   
   | j d S Nr   r   rt   ro   ro   rp   r  4  r   zOutputGraph.root_tracerc                 C   r3  Nr5  rt   ro   ro   rp   current_tracer8  r   zOutputGraph.current_tracerc                 C   s   t | jdkS )Nr   )r	  r   rt   ro   ro   rp   is_root_tracer<     zOutputGraph.is_root_tracerc                 C      | j jS rr   r8  rf   rt   ro   ro   rp   rf   @     zOutputGraph.graphc                 C   s   || j _d S rr   r<  )ru   rz   ro   ro   rp   rf   E  s   c                 C   r;  rr   )r8  input_name_to_proxyrt   ro   ro   rp   r>  I  r=  zOutputGraph.input_name_to_proxyc                 C   r;  rr   )r8  real_value_cachert   ro   ro   rp   r?  M  r=  zOutputGraph.real_value_cachec                 C   r;  rr   )r8  bound_symbolsrt   ro   ro   rp   r@  Q  r=  zOutputGraph.bound_symbolsc                 O      | j j|i |S rr   )r8  create_proxyru   r   r   ro   ro   rp   rB  [     zOutputGraph.create_proxyc                 O   rA  rr   )r8  create_noderC  ro   ro   rp   rE  ^  rD  zOutputGraph.create_nodec                 O   rA  rr   )r8  remove_noderC  ro   ro   rp   rF  a  rD  zOutputGraph.remove_nodec                 c   s    t  }z5|r|j| ju sJ |  |r|n
t| | j|| jjd}| j| |V  W |d d d  | j	  d S |d d d  | j	  w )N)parentsource_targetr   )
r*   rG  r8  	__enter__r   r   r   r   __exit__pop)ru   rH  Zprior_tracerZnew_scope_ctxtracerro   ro   rp   	subtracerd  s*   
zOutputGraph.subtracerc                 C   s   | S rr   ro   rt   ro   ro   rp   output|  s   zOutputGraph.outputc                 C   r;  rr   )r   r  rt   ro   ro   rp   r    r=  zOutputGraph.fake_modec                 C   
   | j jjS rr   )r   r  r   rt   ro   ro   rp   r     r   zOutputGraph.shape_envr   c                 C   rO  rr   )r   r*  r+  rt   ro   ro   rp   r    r   zOutputGraph.guardsc                 C   rO  rr   )r   Zmodule_contextr   rt   ro   ro   rp   r     r   zOutputGraph.nn_modulesNc                 C   s   t ttttdtf tf f |dur|n| jjj	}| j
| jf|d< tjt f|d< ttjdtdf|d< ttjdtdf|d< ttjdtdf|d	< ttjdtdf|d
< tjt f|d< dS )zc
        Saves to out if it is provided. Else saves to the tracing context's global_state.
        .Nr   grad_enabledcudaZautocast_enabledcpuZautocast_cpu_enabledZautocast_gpu_dtypeZautocast_cpu_dtypeZautocast_cache_enabled)r   r   r   tupler   r   r   r   global_contextglobal_stateset_torch_function_stater   r   Zset_grad_enabledis_grad_enabled	functoolspartialZset_autocast_enabledZis_autocast_enabledZset_autocast_dtypeZget_autocast_dtypeZset_autocast_cache_enabledZis_autocast_cache_enabled)ru   outrU  ro   ro   rp   r     s2   zOutputGraph.save_global_statec                 C   r.  rr   )r   r   )ru   txro   ro   rp   push_tx  r0  zOutputGraph.push_txc                 C   s
   | j  S rr   )r   rK  rt   ro   ro   rp   pop_tx  rw   zOutputGraph.pop_txc                 C   s   | j s| jS | j d S r6  )r   r   rt   ro   ro   rp   
current_tx  s   zOutputGraph.current_txc                 C   s
   t | jS rr   )rI   rf   rt   ro   ro   rp   rI     rw   zOutputGraph.count_callsc                 C   s   t t| jjdkS r4  )r	  r   rf   nodesrt   ro   ro   rp   is_empty_graph     zOutputGraph.is_empty_graphc                 C   s@   |sJ | j }|dD ]}t|tr|| }qt||}q|S N.)r   splitr  r   getattr)ru   keysobjr   ro   ro   rp   get_submodule  s   

zOutputGraph.get_submoduletmpc                 C   sF   t | jd }	 | dt| j }||vr"| jd  |f7  < |S q)Nco_varnamesT_)r   r   r   r   )ru   r  existingvarro   ro   rp   r    s   zOutputGraph.new_varc                 C   s*   || j d vr| j d  |f7  < dS dS )z/Ensure self.code_options.co_names contains nameco_namesN)r   )ru   r  ro   ro   rp   update_co_names  s   zOutputGraph.update_co_namesc                  G   sV   d tt| }tdd|}tdd|}tdd|}|r%|d  s)d| }|S )	Nrk  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)joinr"  r   rerp  isalpha)namesr  ro   ro   rp   module_key_name  s   zOutputGraph.module_key_nameattr_prefix
attr_valuec                 C   s6   t || j}|| j|< | d|di }t|j| |S )Nget_attrro   )rO   r   rB  rV   r  )ru   rv  rw  Z	attr_nameproxyro   ro   rp   %register_static_attr_and_return_proxy  s
   
z1OutputGraph.register_static_attr_and_return_proxytargetc           
         s  t jjrtjjfi S tdv sJ d ttr'J tt	j
rBj s7jfdd}nCtt	jjrltt	jjsRJ rdttj fdd}n!fdd}ntt	jt	jfr~fdd}nfdd}j D ]\}}|u r||  S qtj|  t jj j < tt	jjrއ fdd	}td
r̈ D ]\}}	|| qtdrވ D ]\}}	|| q| S )Nri   c                    s   j d usJ j | < jjjv rjjj S tdkr.t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jvs^J t|jjjd< |S )NZguardedrx  ro   r  Ztensor_dict)r   r   rN  r   rN   r  r?   r3   r  r2   ZID_MATCHr<   ZTENSOR_MATCHr[   rB  Ztrack_object_existingry  r  r  rE   )
module_keyr}   )optionsru   ri   r{  rL  ro   rp   	wrap_name!  s,   
	z6OutputGraph.register_attr_or_module.<locals>.wrap_namec                    s   t t| fi  S rr   )r_   r  r|  r}  r{  ro   rp   r~  G  s   c                    s   t jfi  S rr   )r    ZUnspecializedNNModuleVariabler  r  ro   rp   r~  O  rD  c                    s$   t jd| di fdi S )Nrx  ro   Zsym_num)ra   r#  rB  r  )r}  ru   r{  ro   rp   r~  Z  s   c                    s*    j |   j| < t t| dS )N)Zsource_name)rN  ro  r   rW   r(  r9   r  )ru   r{  ro   rp   r~  e  s
   
c                    sV   j d usJ t| }  d|  }|j |< ttr)| jt| < d S d S rb  )r   r@   r  r>   r   r   ru  r  )	leaf_nameZ
new_sourcenew_name)r  ru   ri   ro   rp   register_leaf_namev  s   


z?OutputGraph.register_attr_or_module.<locals>.register_leaf_name_parameters_buffers)r4   r   r   rW   r(  r^  r   r  r@   r   r   r8  r9  r  r   r   r3   r  r2   Z	NN_MODULESymIntZSymFloatr   r   r   ru  rO   r   hasattrZnamed_parametersZnamed_buffers)
ru   r{  rt  r}  r~  r   r   r  r  rk  ro   )r  r}  ru   ri   r{  rL  rp   register_attr_or_module  sH    





z#OutputGraph.register_attr_or_modulec                 C   s  | j d}t|}|sg i fS g }i }g |j|j | jj }|rt|	 }t
|tr=t
|jts7J ||j7 }q$|| jjvsIt
|jtr]t
|jtr]t
|jjtr]|jjj|v s^q$|jjj}||vrkg ||< || | |s&i }	i }
| jD ]c}t
|jtrt
|jtr|jj|v sq{|jj}|| jd v sJ || D ]<}|j|
v rq|jj}||	vr| | d}||	|< |td|dt|tdtd|dg |j}t|	| |
|< qq{||
fS )Nru   rj  Z_ref	LOAD_FASTargvalBINARY_SUBSCR
STORE_FAST)r   r)  rM   stacksymbolic_localsvaluesr   Zstore_attr_mutationsrf  rK  r  r\   r   r   Zmutation_typer5   ri   r:   baser>   
local_namer   	graphargsZ_exampler   indexr  r&  r$   r%   )ru   r[  Zmaybe_gmZstolen_list_namesalias_instsZneeds_aliasqueuexZstolen_namevisitedoverridden_sourcesargZ	list_nameZlist_idxZ
alias_nameZ
old_sourcero   ro   rp   handle_aliases_for_stolen_lists  s   












 z+OutputGraph.handle_aliases_for_stolen_listsFr   c                    s  |dusJ ddl m} |_|_d_td| tdd jD s0t	dd	d
g t
jd g tjdkrijD ].}|jdkrLtd|jd q:|jdkr`tdtjd d q:t| q:jrsjrsJ dj \} | fdd}tjD ]
}|j|jd q    tj }	|	D ]}
|
!  q" }dd j#$ D }t%|}g }i }j&$ D ]A\}}t'|j(t)r|j(j*|krqt'|t+r|j*|krqtjdkrt,-t.|rqnt,-t.|rJ ||vrg ||< || | q|/ D ]}|||  |	|gt||   qtj0dkrq|  g }1d_2|t3j0}4d|}t5| d}||6|d |t7dd ||8j9j2 :| |	rtdd |	D rtdd |	D rtt;|	t|	krшj<= rtj>dksшj?s|  :@tt|	||td t|	dg  : fd!d"t|D  dS 1d#}t5|| d}A|	| t5||d$d |jB$ D  d%}A|	| d}g }tCjDdkst|jEdkr9|@|F || t|jEdkr1||8| d}n|td& nG |  :||H   : fd'd"t|D  |rk:t5 dI|g dS dS )(zw
        Generate a subgraph to continue execution on user code.
        Automatically restore live variables.
        Nr   disableTzCOMPILING GRAPH due to %sc                 s       | ]}|  V  qd S rr   )Zcan_restore)r   blockro   ro   rp   	<genexpr>      z/OutputGraph.compile_subgraph.<locals>.<genexpr>z'Attempt to compile graph in a try blockr  z9Dynamo cannot compile traced graphs while in a try block.r        Z	MAKE_CELLr  ZCOPY_FREE_VARSco_freevars)r  z)export does not support pregraph_bytecodec                      s         d S rr   )add_output_instructionsr   ro   )prefix_instsru   ro   rp   append_prefix_insts  s   
z9OutputGraph.compile_subgraph.<locals>.append_prefix_insts)Zis_graph_breakc                 S   s   i | ]	\}}|t |qS ro   )rT   )r   r  modro   ro   rp   
<dictcomp>+  s    z0OutputGraph.compile_subgraph.<locals>.<dictcomp>)r     r   Zrandom_valuesZ__gen_rand_valuesr  Fc                 s   s:    | ]}t |tttf ot |to| tu  V  qd S rr   )r  rc   r`   rd   ra   Zpython_typefloat)r   r   ro   ro   rp   r  c  s    

c                 s   s    | ]}t |tV  qd S rr   )r  rb   r   r  ro   ro   rp   r  o      UNPACK_SEQUENCEc                       g | ]}t  d |qS r  r)   create_storer   rm  r  r[  ro   rp   r         z0OutputGraph.compile_subgraph.<locals>.<listcomp>Z	graph_outc                 S   s   i | ]\}}|d kr|dqS )r   Nro   )r   valr   ro   ro   rp   r    s    )Ztempvarsr  POP_TOPc                    r  r  r  r  r  ro   rp   r     r  )J
decoratorsr  partial_convertcompile_subgraph_reasonr   r   debugallZblock_stackr.   r   ZCAUSED_BY_EARLIER_GRAPH_BREAKsysversion_infor  opnamer   r$   r  r	  r   r   r   r   r&  r  r1  exitr   cleanup_graphZprune_dead_localsr   r  Zrealize
dedup_passr   r   r   r  r  ri   r>   r  r]   r  __instancecheck__r^   rf  r   r  r   r   r  r)   Zload_function_namer#   r  rN  r  r   r   Zis_emptydebug_localsr   compile_and_call_fx_graphcodegen_suffixZusesrI   rf   Zgraph_outputsZgraph_output_varsrun_compiler_collectiver'  Zcreate_delete)ru   r[  r  r   r  instr  r  r  stack_valuesrz   Zoutput_replacementsZnn_modules_proxiesrootZrestore_varsZval_to_namesr   r   Zrandom_calls_instructionsZrand_fnZrand_fn_namecodegenZgraph_output_varZpass1Zpass2Zstored_graph_output_varrN  ro   )r  r  ru   r[  rp   compile_subgraph  s,  	












	 
	zOutputGraph.compile_subgraphc                    s   | j   | jr*| jrJ | j D ]\}} |   | j  | q| j 	  |j
D ])\}  fdd |D ]} | qC tt|d  tdg q3 j||j d | j   d S )Nc                      s    S rr   ro   ro   r  Z	debug_varro   rp   r     s    z,OutputGraph.codegen_suffix.<locals>.<lambda>Fr  )Zvalue_from_source)r   Zcodegen_save_tempvarsr   r   r   Zappend_outputZcreate_loadr   Z
store_attrZcodegen_hooksr  r!  Zextend_outputr#   r	  r$   Zrestore_stackZcodegen_update_mutated)ru   r[  r  r  r  r  r   r  ro   r  rp   r    s    

zOutputGraph.codegen_suffixc                 C   s   | j sJ t| jj}|D ]	}|jdd qt }t||dd D ]@\}}|j	tj
ju rdt|j| fkrd|jsd|jd }|j	tj
ju rdt|j| fkrd|jsd|jd }| j| | j| q$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   rf   r_  r  rK  r   rW  zipr{  r   Z_set_grad_enabledrS  r   Z_erased
erase_node)ru   r_  r  rP  Znode1Znode2ro   ro   rp   r    s(   


zOutputGraph.cleanup_graphc                 C   sN   i }| j jD ]}|jdd }t|tjjr$| }dd |D ||j	< q|S )Nr  c                 S   s"   g | ]}t |tr|nt|qS ro   )r  rm   repr)r   sro   ro   rp   r     s   " z:OutputGraph.get_graph_sizes_structured.<locals>.<listcomp>)
rf   r_  r  r)  r  r   r   r   sizer  )ru   retr  r  r  ro   ro   rp   get_graph_sizes_structured  s   z&OutputGraph.get_graph_sizes_structuredr  c           	      C   s   d}|d| d7 }| j jD ]U}|jdd }t|tjjrc| }||j	 dt
| d7 }g }d}|D ]}t|trB|| q5t|tjrRd}||jj q5 n|rc||j	 d	t
| d7 }q|S )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
r  z: 
FTz (concrete): )rf   r_  r  r)  r  r   r   r   r  r  rS  rm   r   r  r  hint)	ru   r  Zgraph_sizes_strr  r  r  Zconcrete_sizeZ
has_symintszro   ro   rp   get_graph_sizes  s,   
zOutputGraph.get_graph_sizesc              
   c   s`    | j j }i }| j|d z| j j| dV  W | j jt| dS | j jt| w )zj
        Momentarily restores the global state to what it was prior to tracing the current output
        )rZ  N)r   rT  Zcopy_graphstater   Zrestore_graphstater   )ru   Zprior_global_stateZcurrent_global_statero   ro   rp   restore_global_state  s   z OutputGraph.restore_global_statec              	      s   |j   d urs jd u ru j}td j tjjddd  fddd tj	
| tj	  1 tddd	 d g|  }tj| j|d
 | _W d    n1 sWw   Y  W d    n1 sfw   Y  |j  tjd S d S )Nzcompiler_collective %sartifactc                   S   s
   dddS )Ncompiler_collectivestring)r  encodingro   ro   ro   ro   rp   r   /  s   z5OutputGraph.run_compiler_collective.<locals>.<lambda>c                      s
    j  S rr   )local_staterenderro   Zdsro   rp   r   3     
 )Zmetadata_fn
payload_fnr  T)log_pt2_compile_event)group)Zdistributed_state
all_states
compile_pgr   infor  r   _loggingtrace_structuredrQ  ZdeviceZrankZdevice_countrK   r  distZall_gather_objectZspeculation_logr   r   Z CompileCollectiveRestartAnalysis)ru   r[  r  r  ro   r  rp   r  )  s*   
	
 
	z#OutputGraph.run_compiler_collectivec                    s  t jj l ddlm} jsJ | td}t	|t
s#J t	|ts*J ddjtdd |D fi }| D ]	\}}	||	 qB|jj|| tjsj  tt|jj|jd   tj}
td d	  |
7  <    j!"  t#|j j$D ]}|  qj% _%j&'  j(d
< j) j(d< t*+dt,| dddd t j-j.dfdd fddd /  j0j1}jsddl2m3  m} |j4dd t j5j6|jd}W d   n1 sw   Y  |j0_17  8 }W d   n	1 sw   Y  ddl9m:} t	||s2t	t;|dd|rK|j<dkrKt	||r:|n|j=}|>| t	||sK|j?}||}td d  d7  < @|| tA|}|B| |C W  d   S 1 sww   Y  dS )zr
        Generate code from self.graph and return the Instruction()s to
        call that generated code.
        r   r  Z__compiled_fnrN  c                 s   r  rr   )Zas_proxyr  ro   ro   rp   r  U  r  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>)r   statsZcalls_capturedr   r   %sT)include_strideinclude_deviceZcoloredZdynamo_output_graphc                      s   d   iS )Nsizes)r  ro   rt   ro   rp   r     s    z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>c                      s    j ddddS )NFT)Zprint_outputr  r  )Zprint_readablero   )r   ro   rp   r     s    )r  r   NFr   )r   )_LazyGraphModule__self__Z_lazy_forwardZunique_graphs)Dr   _guardsr   Zclear_framer  r  r   r  r'   r  r   r   rE  r8  Z
create_argrS  r   replace_all_uses_withrN  _maybe_preserve_original_metar   Zdo_not_emit_runtime_assertsremove_unused_get_attr_nodesr   r	   r   rf   r   r   remove_unused_graphargsrI   rJ   &remove_tensorify_specialized_graphargsr?  r   r   r   r  r   r   r  r   graph_code_logr  rR   r  r  r2  r   r  r   r   r   r   r   r  call_user_compilertorch.fx._lazy_graph_moduler  re  rj   r  Zforce_recompiler   install_global_unsafer)   Zmake_call_generated_coder'  )ru   r[  rvr  Zreplaced_outputsr  r  Zoutput_nodeold_nodenew_nodeZncallsregister_finalizerZold_fake_moder  Zbackend_fake_modecompiled_fnr  Zlazy_gmr  ro   )r   ru   rp   r  A  s   









	

&z%OutputGraph.compile_and_call_fx_graphc                 C   s   | j jddS )Nplaceholderop)rf   
find_nodesrt   ro   ro   rp   placeholders  r:  zOutputGraph.placeholdersc                 C   s   dd | j D S )Nc                 S   s   g | ]}|j d  qS )r  r  )r   r  ro   ro   rp   r     s    z)OutputGraph.graphargs.<locals>.<listcomp>)r  rt   ro   ro   rp   r    s   zOutputGraph.graphargsr   c                 C   s>   t ddddd | |W  d    S 1 sw   Y  d S )NOutputGraph.call_user_compilerZbackend_compileTZ'aot_autograd_cumulative_compile_time_us)Z
phase_namer  Zdynamo_compile_column_us)rK   _call_user_compiler)ru   r   ro   ro   rp   r    s   $r
  c                 C   s  | j d usJ d}g }|jjD ]}|jdv r|d7 }|jdkr$|| qt| |D ]}|jd }|j|_q+| j	|_
| j|_t| j drI| j jnd}z/t tjd|  | j }tjr`t|}|||  }	t tjd	|  t|	syJ d
W nv ttfy     ty }
 z;| jrt| j |
t |
jd t |
| j!j"dd| dt#|
 d| j!$  d| dt#|
 ddgd W Y d }
~
n+d }
~
w t%y }
 z|
d }
~
w t&y }
 zt| j |
t |
jd d }
~
ww t'ddi | j(|t)|jjt)|d |	S )Nr   r$  call_methodcall_moduler   r  r  rj   z<unknown compiler_fn>zcalling compiler function zdone compiler function z#compiler_fn did not return callablezBackend compiler exceptionz	Backend: z
Exception:z
Traceback:
zBackend compiler `z` failed with z. Adding a graph break.z-Report an issue to the backend compiler repo.r  Zdynamor
  )Zop_countZ
node_countZinput_count)*r   rf   r_  r  r   rQ   r  ri   Z_dynamo_sourcer   Z_param_name_to_sourcer   Z_source_to_user_stacksr  rj   r   r   INFOr   r   r   r   callabler   r
   r,   r   r+   inspectcurrentframewith_traceback__traceback__r/   r   r   r   Zformat_frame_summaryr-   r   r   r   r	  )ru   r   Ztotr  r  plr  r  r   r  ero   ro   rp   r    s   










zOutputGraph._call_user_compilerc                 C   s   t jjjr	t| S t S rr   )r   _dynamor   use_graph_deduplicationr0   r   rt   ro   ro   rp   r    s   
zOutputGraph.dedup_passc                 C   s0   t || jdd}||_d|_| j||d d |S )NT)Zrequires_suffixFr  )rO   r   rj   Ztorchdynamo_force_dynamicr  )ru   r  Zsub_gmZ	next_namero   ro   rp   install_subgraph  s
   zOutputGraph.install_subgraphc                 C   s   dd | j D }|S )Nc                 S      g | ]}|j qS ro   )example)r   r  ro   ro   rp   r   )  s    z.OutputGraph.example_inputs.<locals>.<listcomp>)r  )ru   r   ro   ro   rp   r   (  s   zOutputGraph.example_inputsc                 C   s<   t | jjddddD ]}tt|jdkr| | qd S )Nrx  r  T)reverser   )sortedrf   r  r	  r   usersrF  ru   r  ro   ro   rp   r  ,  s
   
z(OutputGraph.remove_unused_get_attr_nodesc                    sL  j sJ dtjjfdd}dtjjfdd  fdd}d	d
lm} ttjj	D ]:}t
t|jd	kre|jdks`|jdkrF|jtju s`|jdkrX|jtju rX||jd	 s`||s`||re| q+dd }fdd}t dttjtjf fddg }jD ]}||d u}|r|js|| q|jst|jd ts|| q|jd }	t|	trqt|jd jtjr|jd j}
|jd j}tj j!"|
st#|
$ }|% D ]}t&|j'|}t()tjtjffdd| qq|	j*d ur|	j*n|	j}| q|D ]}||}|d ur"|vr|| q+| qd S )Nb_nodec                 S   sf   | du rdS t | tjsdS | jd}|d u rdS |du r dS t |tjr1|j  }d ur1|S dS )NTFr  )	r  r	   Noder  r)  r   ZSymBoolr  Zmaybe_as_bool)r   brro   ro   rp   is_static_true>  s   
z;OutputGraph.remove_unused_graphargs.<locals>.is_static_trueac                 S   sB   ddl m} t| tttfrdS t| tjrt| j	d|S dS )Nr   SymTypesTr  F)
torch.fx.experimental.sym_noder'  r  rm   r  r   r	   r!  r  r)  )r%  r'  ro   ro   rp   is_symnode_argQ  s   z;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_argc                    sp   ddl m} | jdkrdS t| jd|sdS t fdd| jD s&dS t fdd| j	 D s6dS d	S )
Nr   r&  r$  Fr  c                 3       | ]} |V  qd S rr   ro   r   r%  r)  ro   rp   r  h  r  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>c                 3   r*  rr   ro   r+  r,  ro   rp   r  j  r  T)
r(  r'  r  r  r  r)  r  r   r   r  )r  r'  r,  ro   rp   is_symnode_compute_node]  s   
zDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_noder   )is_accessor_noderx  r$  c                 S   s8   | j d }|j}t|tjrt|jjtjr|jjS d S Nr  )	r  r  r  r   r  r  exprsympySymbol)r  r  r  ro   ro   rp   placeholder_binds_symbol  s   

zEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbolc                    s<   t d| jd j  | jd=  |   j| d  d S )NzREMOVE UNUSED GRAPHARG %sr  )r   r  r  ri   r  rF  r?  rK  )r  rt   ro   rp   remove_unused  s   
z:OutputGraph.remove_unused_graphargs.<locals>.remove_unusedfakec                 S   s   | t |O } d S rr   )r   )used_symbolsr5  ro   ro   rp   update_used_symbols  r0  z@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbolsr  c                    s
    | S rr   ro   )t)r7  r6  ro   rp   r     r  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>),r   r	   r  ZArgument%torch.fx.experimental.symbolic_shapesr.  r1  r   rf   r_  r	  r  r  r{  operatorgetitemr   _checkr   rF  r   r   r  r   r  r   r  r  rX   r  ZScriptObjectZexample_strong_refZ_libraryZfake_class_registryZtracing_with_realr   Z__obj_flatten__rf  re  Zwrapped_objpytreeZtree_map_onlyfake_tensorremove)ru   r$  r-  r.  r  r3  r4  Zrecheck_placeholdersZbinds_symbolr  Zreal_script_objZfake_script_objZ	flat_dictattrZfake_attr_valr5  symbolro   )r)  ru   r7  r6  rp   r  1  s   




	









z#OutputGraph.remove_unused_graphargsc                 C   s   ddl m} | jjD ]G}|jd}t|trQ|jd urQt	|jj
jdrQtdd |jD rQ||jj
jjrQt|jD ]}|t|j | | q<| | q
d S )Nr   )TensorifyStater  r  c                 s   s    | ]}|j d kV  qdS )itemNr{  )r   uro   ro   rp   r    r  zEOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>)torch._dynamo.symbolic_convertrB  rf   r_  r  r)  r  r   Z	item_memor  r  Z_exprr  r  Zshould_specializer  r   r  r   rF  )ru   rB  r  r  rE  ro   ro   rp   r    s(   


z2OutputGraph.remove_tensorify_specialized_graphargsr  c                 C   s   | j | d| _dS )zt
        We call this on the creation of a new compiled subgraph that is inserted
        before user code.
        TN)r   r&  r   )ru   r  ro   ro   rp   r    s   
z#OutputGraph.add_output_instructionsc                 C   s6   || j vsJ | j | | jt| 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   rG   r#  r   )ru   r  rz   ro   ro   rp   r    s   z!OutputGraph.install_global_unsafec                 C   s8   | 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.
        rk  Z_c)ry   r   r   r  ru   r  rz   r  ro   ro   rp   install_global_by_id  s
   	
z OutputGraph.install_global_by_idc                 C   s   t |}| || |S )z~
        Installs a global, generating a unique name for it.

        Returns the name of the newly installed global.
        )r'   r  rG  ro   ro   rp   r    s   zOutputGraph.install_globalc                 C   s   d | _ | j  d | _| jjD ]}d|jv r|jd= q| j  | j  | j	  | j
  | j  | j  | j  | j  | j  | j  d S r/  )r   r   r   r   rf   r_  r  r?  r>  r   r   r   r   r   r   r   r   r  ro   ro   rp   cleanup  s"   










zOutputGraph.cleanupenabledc                 C   r   rr   )r   )ru   rJ  ro   ro   rp   rV  +  rw   z$OutputGraph.set_torch_function_stater  c                 C   r.  rr   )r   r   )ru   r  ro   ro   rp   add_graph_finalizer.  s   zOutputGraph.add_graph_finalizerr  c                 C   s0   |j dkr|jd jS |j dksJ | j|j S )z#Extract the non-fake example tensorr  r  rx  )r  r  r  r   r{  r  ro   ro   rp   example_value_from_input_node3  s   
z)OutputGraph.example_value_from_input_node)r  rr   )ri  FN)r   N)drj   rk   rl   r   r6   rn   r   r   r   r   r"   r   Scoperv   r   rW   r  r
  r   r-  r   r/  r2  propertyr  r8  r9  rf   setterr>  r?  r@  rB  rE  rF  
contextlibcontextmanagerrM  rN  r  r   r   r  Z	GuardsSetr  r   r   r\  r]  r^  rI   r`  rh  r  ro  staticmethodru  r	   Proxyrz  r   r   r   r   r  r  r   r  r  r  r  r  r  r  r  r   r!  r  rY   r  r   r!   r  r  r  r  r   r  r  r  r&   r  r  rH  r  rI  rV  rK  rL  r   ro   ro   r   rp   r     s   
 

	 7






	




.


	


 W
 _	
	H	

 
"


r   a  With the current config, we will graph break (and fall back to eager-mode PyTorch) on all ops that have do not have the 'pt2_compliant_tag'. Please see the following doc for how to mark this op as PT2 compliant https://pytorch.org/tutorials/advanced/custom_ops_landing_page.htmlc              
      s  |dkrd S  fdd} fdd}t |tjjr1tjj|jv r&|| d S ||d| d d S t |tjjrt|	 }t
|dkrct||d	 }tjj|jv rX|| d S ||d
| d d S tjj j||fd\}}ztjj|jg|R i |}	W n ty }
 ztddt|
g d W Y d }
~
nd }
~
ww t||	}tjj|jv r|| d S ||d| d|	 d d S d S )Nr$  c                    s   | j dv rd S  j|  d S )N>   atenZprimZprims)	namespacer   r~   rD  output_graphro   rp   encountered_compliant_opH  s   
z8check_pt2_compliant_op.<locals>.encountered_compliant_opc                    s2    j |  tjrtdd|d t g d d S d S )Nz Encountered non-PT2-compliant opr   r  )r   r~   r   Zonly_allow_pt2_compliant_opsr.   err_epilogue)r{  msgrW  ro   rp   encountered_non_compliant_opM  s   

z<check_pt2_compliant_op.<locals>.encountered_non_compliant_opz%Encountered the torch.ops.OpOverload z that is not PT2 compliant.r   r   z:Encountered the non-overloaded torch.ops.OpOverloadPacket z that is not PT2 compliant. Fz*Error when attempting to resolve op packetr  r  z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r  r   Z_opsZ
OpOverloadTagZpt2_compliant_tagtagsZOpOverloadPacketrS  	overloadsr	  re  r  utilsZget_fake_values_from_nodesr^  r   Z_jit_resolve_packetZ_qualified_op_namer   r.   r   )rX  kindr{  r   r   rY  r]  r`  r  overloadr  ro   rW  rp   check_pt2_compliant_opD  sr   



rd  c                   @   s   e Zd Zdd Zdd ZdS )	LazyProxyc                 O   s   || _ || _|| _|| _d S rr   )rL  r   r   r   )ru   rL  r   r   r   ro   ro   rp   rv     s   
zLazyProxy.__init__c                 C   s   | j | ji | jS rr   )r   r   r   rt   ro   ro   rp   r     ra  zLazyProxy.__call__N)rj   rk   rl   rv   r   ro   ro   ro   rp   re    s    re  c                       s   e Zd ZdZd fdd	Zdd Z			d  fdd		Z	d! fd
d	Zdd Z	d"ddZ	dd Z
dd Zdeeejjf fddZdeejejf dee fddZdejdeej fddZ  ZS )#r   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.
    NFc                    s   t    t|| _tj | _|| _	i | _
i | _|| _|| _i | _i | _d | _d| _d| _d| _|d ur;|jd nd| _d | _d | _d | _d | _| jd u rTg | _d S | jj| j||fg | _d S )NFr   r   )r   rv   weakrefry  rX  r   r	   ZGraphrf   r   r>  r?  rG  rH  lifted_freevarsr@  	prev_instZunder_activation_checkpointZ#allow_side_effects_under_checkpointZis_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stackZ_target_to_str)ru   rX  rG  r   rH  r   ro   rp   rv     s0   


zSubgraphTracer.__init__c                 C   s   | j rE| jrG| jrI|jj}d }|d ur| j|| j d }|d urK| j | }tjjD ]}||v r7|| |j	|< q*d|v rM|d |j	d< d S d S d S d S d S d S )Nstack_trace)
rk  rl  rm  current_instructionstarts_liner)  r	   ry  Z_COPY_META_FIELDSr  )ru   r[  r  linenoZnode_idxr  fieldro   ro   rp   r    s.   

z,SubgraphTracer._maybe_preserve_original_metac              	      sb  | j d ur't||f\}}	g }
|D ]}| |}|
| qt|
|	\}}t ||||||| jj	}t
jdkrr|dv rr|j  | jurr jd urr jjd urr|j|j jjd fdd}tdt|  | _d}|j| jurt|jdd	d
  }t|tjjrd}dd |jjD | _|j | _!|j"j#j$| _%n	d | _d | _!d | _%|j&}|r|' j(j)d< |dv r| j*j(j+fg j(j)d< n3|dkr| j d urt,dd| j- dg d | j*j(j+t.fddj(j)d / D fg j(j)d< | 0|j( |sddj(j)vr |j&}|r |' j(j)d< dj(j)vrd|dv r;| j*j(j+fg j(j)d< n)|dkrd| j d urNt,dddg d | j*j(j+j(j)d  d fg j(j)d< dj(j)vrg }|r|1 s|||2  t3|dd }|sp|4  t5j67|8 }d9|j(_:tj;j<j=stj;j<j>r| jj?@| jj	j( S )Nr  r  )rr  c                     s(   t   } djj d d|  S )NzTRACE FX call z from r  )rL   rstripr  r  )line)cur_instheaderr  tx_codero   rp   get_trace_call_log_strF  s   z;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strr  FZorig_graphmodulec                   S   s   d S rr   ro   ro   ro   ro   rp   r   Q  s    z-SubgraphTracer.create_proxy.<locals>.<lambda>Tc                 S   r  ro   r	  )r   ndro   ro   rp   r   U  s    z/SubgraphTracer.create_proxy.<locals>.<listcomp>nn_module_stack>   r  r$  rn  r  z4Invoking an nn.Module inside a higher order operatorzHigher order op name: zThis is not supported.r  c                 3   s.    | ]\}\}}| d d  kr|V  qdS )@r   N)rd  )r   r   rk  tyrD  ro   rp   r  u  s    
z.SubgraphTracer.create_proxy.<locals>.<genexpr>z2Invoking an nn.Module inside a HigherOrderOperatorr  r   ro  rG  )ArG  r=  Ztree_flatten#maybe_lift_tracked_freevar_to_inputr   Ztree_unflattenr   rB  rX  r^  r  r  rp  rh  Z	positionsrr  r   Zget_line_of_code_headertrace_call_logr  rS   rj  r(   Zget_contextr)  r  r   r	   r   rf   r_  rk  Z_lineno_maprl  r   __code__r   rm  r{  r   r  r  rn  r  r.   rH  r   r   r  Zis_co_filename_from_nn_modulesframe_summaryre  r  r   StackSummary	from_listformatrq  ro  r  r   r  Ztrack_nodes_for_deduplicationr   Z
track_node)ru   rb  r{  r   r   r  	type_exprZproxy_factory_fn	flat_argsZ	tree_specZnew_flat_argsr  Zmaybe_new_argr[  ry  Zis_retracingZorig_graphmodule_mayber{  Zframe_summariesZmsgsr   )rv  rw  r  r{  rx  rp   rB    s   
+











zSubgraphTracer.create_proxyc           
         s   t | j|||| | jd ur-tj|i |}|D ]}t|tjjs"q|j	| j	ks,J dqt
 ||||||}	| jj|	jd< |	S )Nz2create_node using arg not from this SubgraphTracerr  )rd  rX  rG  r=  Zarg_tree_leavesr  r   r	   r!  rf   r   rE  r   r  )
ru   r  r{  r   r   r  r  r  r  r  r   ro   rp   rE    s   
zSubgraphTracer.create_nodec                 C   sz   t |jdkr-g }|j D ]}|j| jkr!|tt|jj q|D ]}|j| q$| j| | j	
|jd  d S r4  )r	  r  rf  rf   r&  r1  r   r_  r  r>  rK  r  )ru   r  Zuser_graph_nodesuserZother_graph_nodero   ro   rp   rF    s   zSubgraphTracer.remove_nodec                 C   s  t d||d ur| nd|| j| |d u r'| jd us'J d| d| d| jrB| jd u rBt|ddsB| jj	|g 
t  t|| j}| jrhtt| j}| j| j}|ra| j|}n| j|}n| jd }|c | jd|d	i |d
}	t|	j| | jr|r| j \}
}|	| j|< || j|
< n|	| j|< | j}tj }|s|st|tjr| || t|tjrt|jjt j!r|	| j"|jj< |	W  d    S 1 sw   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)Zonly_allow_inputr  ro   r  )#r   r  r  ri  rG  r   r=   rX  r   
setdefaultr   r   extract_stackrO   r>  r   r1  r  rf   Zinserting_beforeZinserting_afterrB  rV   popitemr   compilerZis_compilingr  r   _lift_basic_symbolsr  r0  r1  r2  r@  )ru   r  r  r  beforeri   Z	prev_namer  ctxry  r   r   Zis_strict_exportZis_non_strict_exportro   ro   rp   r    s`   





$z!SubgraphTracer.create_graph_inputc                 C   s   | j d us	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r9| j 
| |jjd }| |jjt||}|| j|< |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr  )rG  r  r  r  r   r  r0  r@  rg  rL  lift_tracked_freevar_to_inputr  r  r  )ru   ry  r  Z	new_proxyro   ro   rp   r  7	  s$   
	


z,SubgraphTracer.lift_tracked_freevar_to_inputc                    sV   t |tjjst |trt fd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   s    | ]}  |V  qd S rr   )r~  )r   Zsub_argrt   ro   rp   r  l	  s
    
zESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>)
r  r   r	   rT  slicestartstopsteprL  r  )ru   r  ro   rt   rp   r~  _	  s   



z2SubgraphTracer.maybe_lift_tracked_freevar_to_inpute_proxyc                    s$  |j tts
J dtf fdd}fdd}t|tjrt| D ],\}}||rQt	d|||j
 t||dtjjjj||fi t|d} || q%|jtju rt| D ],\}}||rt	d	|||j
 t||dtjjjj||fi t|d} || q^nJ|jtju r | |  | | n3|jtjtjhv r | |  | | n|jtjtjhv rֈ | |  |  | t!|r|" \}}	|D ]}
t#||
} |t#||
 qd S d S t|tj$r||r|j%j&}|j'|< d S d S d S )
Nr   c                    sD   ddl m} || o!t| jjtjo!| jj| jjo!| jj j	vS )Nr   )r   )
r9  r   r  r  r0  r1  r2  r   Zis_unbacked_symintr@  )r  r   rt   ro   rp   	need_bind	  s   z8SubgraphTracer.track_unbacked_symbols.<locals>.need_bindc                    s     j |i |}t|j|  |S rr   )rB  rV   r  )r  r   r   ry  )rL  ro   rp   _proxy_with_example_value	  s   zHSubgraphTracer.track_unbacked_symbols.<locals>._proxy_with_example_valuez>_track_unbacked_symbols %s for %s.size()[%s] at debug_level %sr$  r  z@_track_unbacked_symbols %s for %s.stride()[%s] at debug_level %s)(rL  r  r   r   r   r   	enumerater  r   r  ri  re  opsrU  Zsym_sizerm   r  track_unbacked_symbolslayoutstridedstrideZ
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr   __tensor_flatten__re  r  r  r0  r@  )ru   r  r  r  r  ir  Z
lazy_proxyattrsr  r@  inner_tr0  ro   )ru   rL  rp   r  ~	  s   






z%SubgraphTracer.track_unbacked_symbolsr  srcc           
   	      s  	d
dt ttjf dtt dtdd f fdd}t|tjrt	|
 D ]\}}|||d ur5t|tj|nd dd	 q$|jtju rrt	| D ]\}}|||d urXt|tj|nd dd	 qG|| |d urlt|tjnd dd	 nJ|jtju r | |  | | n3|jtjtjhv r | |  | | n|jtjtjhv r | |  | | t |r|! \}}|D ]}t"||}	 |	|d urt#||nd  qd S d S t|tjr||| d S d S )NFr  ri   r  r   c                    sr  t | sd S t| tjsJ  | }t|dkrd S  jd urf j| | |D ]:} jj| }|j	j
d }t|tjs?J  jt|t||||d}td||d urY| nd j | j|< q)d S t|dksvJ d| d|  |d usJ d	|  d
|  dtt|} jt|t| | ||d}td| |d ur| nd j t|| dd dd|j	j
d< d S )Nr   r  )r  ri   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)Zpass_arg_as_tensorr>  Z	is_tensorr  )r   r  r   r  lookup_unbound_symbolsr	  rG  r  r@  r  r  r  r   r  r   r  r  ri  rg  r   iterrY   )r  ri   r  Zself_to_be_bounds0Zparent_proxyZexample_valphrt   ro   rp   _lift_symbols_in_symint	  sx   


zCSubgraphTracer._lift_basic_symbols.<locals>._lift_symbols_in_symintT)r  )F)$r   rm   r   r  r   r   r   r  r   r  r  rD   rC   ZSIZEr  r  r  ZSTRIDEZstorage_offsetZSTORAGE_OFFSETr  r  r  r  r  r  r  r  r  r  r  r  r   r  re  r7   )
ru   r  r  r  r  r  r  r  r@  r  ro   rt   rp   r  	  sv   
D			
z"SubgraphTracer._lift_basic_symbolsr  r   c                 C   s   |j jj}t|dkrg S g }|D ]3}|| jvr|| q| j| }t|tr0| }|| j|< t|tj	j
r<|j| u sDJ d| dqt|dd dS )Nr   zThe proxy of symbol z" doesn't belong to current tracer.c                 S   s   | j S rr   )r  )r  ro   ro   rp   r   v
  s    z7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>)r{   )r  r0  r   r	  r@  r   r  re  r   r	   rT  rL  r  )ru   r  r   Zto_be_boundr  ry  ro   ro   rp   r  c
  s    






z%SubgraphTracer.lookup_unbound_symbols)NFN)NNN)NNNNrM  )rj   rk   rl   r   rv   r  rB  rE  rF  r  r  r~  r   re  r   r	   rT  r  r  r   r   r   r  r   r1  r2  r  r   ro   ro   r   rp   r     s4    E 9
[(
c
 "r   )r   r   rQ  r   rX  r  r   r   r:  rr  r  r   rf  dataclassesr   typingr   r   r   r   r   r   r1  Ztorch._guardsr   Ztorch._loggingZtorch.distributeddistributedr  Ztorch.nnZtorch.utils._pytreera  Z_pytreer=  r	   Ztorch._dynamo.excr
   r   r   r   r   r   r   Ztorch._subclasses.fake_tensorr   Ztorch._utils_internalr   r  r   Z%torch.fx.experimental._backward_stater   r9  r   r   r   r   Ztorch.fx.passes.runtime_assertr   Ztorch.utils._python_dispatchr   r  r   r   r   r   r    Zbackends.registryr!   r"   Zbytecode_transformationr#   r$   r%   r&   r'   r(   r  r)   Zcurrent_scope_idr*   r+   r,   r-   r.   r/   Zgraph_deduplicationr0   Zgraph_region_trackerr1   r  r2   r3   Zmutation_guardr4   r   r5   r6   ri   r7   r8   r9   r:   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   Zvariables.baserW   Zvariables.builderrX   rY   rZ   r[   Zvariables.listsr\   Zvariables.miscr]   r^   Zvariables.nn_moduler_   Zvariables.tensorr`   ra   rb   rc   Zvariables.torch_functionrd   rF  re   	getLoggerrj   r   r  ZgetArtifactLoggerZgraph_tabular_logr  Zgraph_sizes_logr  rh   rq   	lru_cacher   r   r   r   r   r   r   r   r   objectrN  r   r[  rd  r   r   re  ZTracerr   ro   ro   ro   rp   <module>   s    @P

#            <	I