o
    Ih&                    @   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graph
graph_codegraph_sizes
trace_callT)frozenc                   @   s   e Zd ZU eed< eed< dS )VariableTrackerCacheKeyvt_idsourceN)__name__
__module____qualname__int__annotations__r    rs   rs   N/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_dynamo/output_graph.pyrk      s   
 rk   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selfrs   rs   rt   __init__      
zVariableTrackerCache.__init__c                 C   s&   t t||}|| jvrd S | j| S rv   rk   idrw   )ry   valuerm   keyrs   rs   rt   lookup   s   

zVariableTrackerCache.lookupc                 C   s   t t||}|| j|< d S rv   r|   )ry   r~   rm   vtr   rs   rs   rt   add   s   zVariableTrackerCache.addc                 C   s   t  }|j| j |S rv   )ru   rw   update)ry   	new_cachers   rs   rt   clone   s   zVariableTrackerCache.clonec                 C   s   | j   d S rv   )rw   clearrx   rs   rs   rt   r         zVariableTrackerCache.clearN)rn   ro   rp   rz   r   r   r   r   rs   rs   rs   rt   ru      s    ru   c                   C   s
   t tS rv   )torchdynamo_loggingget_step_loggerlogrs   rs   rs   rt   _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.reason
user_stackTgraph_breakc                 C   s   | j r
t|  d S d S rv   )r   rP   appendrx   rs   rs   rt   __post_init__   s   z GraphCompileReason.__post_init__N)rn   ro   rp   __doc__strrr   list	tracebackFrameSummaryr   boolr   rs   rs   rs   rt   r      s   
 r   c                    s    fdd}|S )Nc                      s   dd  D S )Nc                 S   s    g | ]\}}}||i |qS rs   rs   ).0fnargskwargsrs   rs   rt   
<listcomp>   s     zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>rs   rs   random_callsrs   rt   _gen_rand_values   r   z1_get_gen_rand_values_fn.<locals>._gen_rand_valuesrs   )r   r   rs   r   rt   _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 rv   )superrz   itemssetattr)ry   r   kv	__class__rs   rt   rz      s   
zFakeRootModule.__init__returnc                 C   s   dS )NzFakeRootModule(...)rs   rx   rs   rs   rt   __repr__   s   zFakeRootModule.__repr__)rn   ro   rp   r   dictr   torchnnModulerz   r   __classcell__rs   rs   r   rt   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 rv   )r   )ry   r   rs   rs   rt   rz      r{   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)ry   r   r   copy_gmcorrectresultrs   rs   rt   __call__   s(   


	

zWrapperBackend.__call__N)rn   ro   rp   r"   rz   r   r	   GraphModuler   Tensorr   rs   rs   rs   rt   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_fakesallow_scalar_outputsallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards'allow_complex_guards_as_runtime_asserts	co_fieldsr   F(fake_tensor_allow_unsafe_data_ptr_accessT)	shape_envallow_non_fake_inputsr   )Vr   rz   SubgraphTracertracersinput_source_to_varr   export_constraintsframe_stateinput_source_to_sizes_stridescleanup_hooksnext_compile_id_counter
compile_idsetinstalled_globalsr   r   r   r   r1   region_trackerr   r   r   capture_scalar_outputs capture_dynamic_output_shape_opsr   r   torch._functorch.config
_functorchpatchr   _subclassesFakeTensorModer   tracing_contextr   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   tracked_fakes_id_to_sourceparam_name_to_sourcer6   r   ru   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_C_is_torch_function_enabledtorch_function_enabled_is_torch_function_mode_enabled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_fglobals%name_of_builtins_dict_key_in_fglobalsguard_on_key_order)ry   r   r   r   r   r   r   r   r   f_coder	  r   _config	fake_moder   rs   rt   rz     s   







zOutputGraph.__init__c                 C   s&   | j d }t|ts|j}| d|S )N__builtins____builtins_dict__)r   
isinstancer   __dict__install_global)ry   
f_builtinsrs   rs   rt   r    s   

z-OutputGraph.install_builtins_dict_in_fglobalshookc                 C   s6   | t | j }|| jvsJ || j|< ||  fS rv   )lenr  get_backward_state_proxy)ry   r   prefixnamers   rs   rt   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`.gb_typecontextexplanationhintsdynamo_backward_staterm   grapharg)r  r   r.   r   root_tracercreate_graph_inputtyper8   rX   nodemetanew_varr  )ry   example_valuers   rs   rt   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 rv   )guardsr   rA   
make_guardr2   	SHAPE_ENVr;   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATEr   r  r   peek_interpreter_stackFUNCTORCH_STACK_MATCH)ry   cirs   rs   rt   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 rv   )load_import_fromro   rn   rs   cgr   rs   rt   <lambda>  s    z3OutputGraph.synthetic_graph_input.<locals>.<lambda>F)r4  r)   r   add_push_nullforeachmapr    ConstantVariablecreatecall_functionr!  storer  extendget_instructionsrB   rW   buildr   getguards_contextdynamo_guardsremove_guards_with_source)ry   r   r   r5  varnamerm   r   rs   rA  rt   synthetic_graph_input  s    

z!OutputGraph.synthetic_graph_inputr   c                 C      | j | d S rv   )r   r   )ry   r   rs   rs   rt   add_cleanup_hook,     zOutputGraph.add_cleanup_hookc                 C   s$   t | jD ]}|  q| j  d S rv   )reversedr   r   )ry   r   rs   rs   rt   call_cleanup_hooks/  s   zOutputGraph.call_cleanup_hooksc                 C   
   | j d S Nr   r   rx   rs   rs   rt   r/  4  r   zOutputGraph.root_tracerc                 C   rY  Nr[  rx   rs   rs   rt   current_tracer8  r   zOutputGraph.current_tracerc                 C   s   t | jdkS )Nr   )r!  r   rx   rs   rs   rt   is_root_tracer<     zOutputGraph.is_root_tracerc                 C      | j jS rv   r^  rf   rx   rs   rs   rt   rf   @     zOutputGraph.graphc                 C   s   || j _d S rv   rb  )ry   r~   rs   rs   rt   rf   E  s   c                 C   ra  rv   )r^  input_name_to_proxyrx   rs   rs   rt   rd  I  rc  zOutputGraph.input_name_to_proxyc                 C   ra  rv   )r^  real_value_cacherx   rs   rs   rt   re  M  rc  zOutputGraph.real_value_cachec                 C   ra  rv   )r^  bound_symbolsrx   rs   rs   rt   rf  Q  rc  zOutputGraph.bound_symbolsc                 O      | j j|i |S rv   )r^  create_proxyry   r   r   rs   rs   rt   rh  [     zOutputGraph.create_proxyc                 O   rg  rv   )r^  create_noderi  rs   rs   rt   rk  ^  rj  zOutputGraph.create_nodec                 O   rg  rv   )r^  remove_noderi  rs   rs   rt   rl  a  rj  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*   rm  r^  	__enter__r   r   r   r   __exit__pop)ry   rn  prior_tracernew_scope_ctxtracerrs   rs   rt   	subtracerd  s*   
zOutputGraph.subtracerc                 C   s   | S rv   rs   rx   rs   rs   rt   output|  s   zOutputGraph.outputc                 C   ra  rv   )r   r  rx   rs   rs   rt   r    rc  zOutputGraph.fake_modec                 C   
   | j jjS rv   )r   r  r   rx   rs   rs   rt   r     r   zOutputGraph.shape_envr   c                 C   rw  rv   )r   rO  rP  rx   rs   rs   rt   r6    r   zOutputGraph.guardsc                 C   rw  rv   )r   module_contextr   rx   rs   rs   rt   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cudaautocast_enabledcpuautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r   r   r   tupler   r   r   r   global_contextglobal_stateset_torch_function_stater  r   set_grad_enabledis_grad_enabled	functoolspartialset_autocast_enabledis_autocast_enabledset_autocast_dtypeget_autocast_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)ry   outr  rs   rs   rt   r    s2   zOutputGraph.save_global_statec                 C   rT  rv   )r   r   )ry   txrs   rs   rt   push_tx  rV  zOutputGraph.push_txc                 C   s
   | j  S rv   )r   rq  rx   rs   rs   rt   pop_tx  r{   zOutputGraph.pop_txc                 C   s   | j s| jS | j d S r\  )r   r   rx   rs   rs   rt   
current_tx  s   zOutputGraph.current_txc                 C   s
   t | jS rv   )rI   rf   rx   rs   rs   rt   rI     r{   zOutputGraph.count_callsc                 C   s   t t| jjdkS rZ  )r!  r   rf   nodesrx   rs   rs   rt   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)ry   keysobjr   rs   rs   rt   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   )ry   r$  existingvarrs   rs   rt   r4    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   )ry   r$  rs   rs   rt   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 )	Nr  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)joinrF  r   rer  isalpha)namesr$  rs   rs   rt   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_attrrs   )rO   r   rh  rV   r2  )ry   r  r  	attr_nameproxyrs   rs   rt   %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 )Nrm   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 )Nguardedr  rs   r5  tensor_dict)r   r   rv  r   rN   r  r?   r3   r7  r2   ID_MATCHr<   TENSOR_MATCHr[   rh  track_object_existingr  r2  r3  rE   )
module_keyr   )optionsry   rm   r  rt  rs   rt   	wrap_name!  s,   
	z6OutputGraph.register_attr_or_module.<locals>.wrap_namec                    s   t t| fi  S rv   )r_   r1  r  r  r  rs   rt   r  G  s   c                    s   t jfi  S rv   )r    UnspecializedNNModuleVariabler  r  rs   rt   r  O  rj  c                    s$   t jd| di fdi S )Nr  rs   sym_num)ra   rH  rh  r  )r  ry   r  rs   rt   r  Z  s   c                    s*    j |   j| < t t| dS )N)source_name)rv  r  r   rW   rM  r9   r  )ry   r  rs   rt   r  e  s
   
c                    sV   j d usJ t| }  d|  }|j |< ttr)| jt| < d S d S r  )r   r@   r  r>   r  r   r  r$  )	leaf_name
new_sourcenew_name)r$  ry   rm   rs   rt   register_leaf_namev  s   


z?OutputGraph.register_attr_or_module.<locals>.register_leaf_name_parameters_buffers)r4   r   r   rW   rM  r  r   r  r@   r   r   r^  r_  r/  r   r   r3   r7  r2   	NN_MODULESymIntSymFloatr   r   r   r  rO   r   hasattrnamed_parametersnamed_buffers)
ry   r  r  r  r  r   r   r  r  r  rs   )r$  r  ry   rm   r  rt  rt   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 )Nry   r  _ref	LOAD_FASTargvalBINARY_SUBSCR
STORE_FAST)r   rN  rM   stacksymbolic_localsvaluesr   store_attr_mutationsr  rq  r  r\   r   r   mutation_typer5   rm   r:   baser>   
local_namer   	graphargs_exampler   indexr4  rK  r$   r%   )ry   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuexstolen_namevisitedoverridden_sourcesarg	list_namelist_idx
alias_name
old_sourcers   rs   rt   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 rv   )can_restore)r   blockrs   rs   rt   	<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'        	MAKE_CELLr  COPY_FREE_VARSco_freevars)r  z)export does not support pregraph_bytecodec                      s         d S rv   )add_output_instructionsr   rs   )prefix_instsry   rs   rt   append_prefix_insts  s   
z9OutputGraph.compile_subgraph.<locals>.append_prefix_insts)is_graph_breakc                 S   s   i | ]	\}}|t |qS rs   )rT   )r   r$  modrs   rs   rt   
<dictcomp>+  s    z0OutputGraph.compile_subgraph.<locals>.<dictcomp>)r     r   random_values__gen_rand_valuesr  Fc                 s   s:    | ]}t |tttf ot |to| tu  V  qd S rv   )r  rc   r`   rd   ra   python_typefloat)r   r   rs   rs   rt   r  c  s    

c                 s   s    | ]}t |tV  qd S rv   )r  rb   r   r  rs   rs   rt   r  o      UNPACK_SEQUENCEc                       g | ]}t  d |qS r  r)   create_storer   r  r  r  rs   rt   r         z0OutputGraph.compile_subgraph.<locals>.<listcomp>	graph_outc                 S   s   i | ]\}}|d kr|dqS )r   Nrs   )r   valr   rs   rs   rt   r    s    )tempvarsr  POP_TOPc                    r  r  r	  r  r  rs   rt   r     r  )J
decoratorsr  partial_convertcompile_subgraph_reasonr  r   debugallblock_stackr.   r   CAUSED_BY_EARLIER_GRAPH_BREAKsysversion_infor  opnamer   r$   r  r!  r   r   r  r   rK  r  rW  exitr   cleanup_graphprune_dead_localsr   r  realize
dedup_passr   r   r   r  r  rm   r>   r  r]   r1  __instancecheck__r^   r  r   r4  r  r   r  r)   load_function_namer#   r
  rv  r  r   r   is_emptydebug_localsr  compile_and_call_fx_graphcodegen_suffixusesrI   rf   graph_outputsgraph_output_varsrun_compiler_collectiverL  create_delete)ry   r  r  r   r  instr  r  r  stack_valuesr~   output_replacementsnn_modules_proxiesrootrestore_varsval_to_namesr   r   random_calls_instructionsrand_fnrand_fn_namecodegengraph_output_varpass1pass2stored_graph_output_varrv  rs   )r  r  ry   r  rt   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 rv   rs   rs   rB  	debug_varrs   rt   rC    s    z,OutputGraph.codegen_suffix.<locals>.<lambda>Fr  )value_from_source)r   codegen_save_tempvarsr  r   r   append_outputcreate_loadr  
store_attrcodegen_hooksr$  rD  extend_outputr#   r!  r$   restore_stackcodegen_update_mutated)ry   r  r-  rB  r$  r  r   r  rs   r<  rt   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  r3  rq  r   r  zipr  r  _set_grad_enabledr  r   _erased
erase_node)ry   r  r2  ry  node1node2rs   rs   rt   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 )Nr5  c                 S   s"   g | ]}t |tr|nt|qS rs   )r  rq   repr)r   srs   rs   rt   r     s   " z:OutputGraph.get_graph_sizes_structured.<locals>.<listcomp>)
rf   r  r3  rN  r  r   r   r   sizer$  )ry   retr2  r5  rP  rs   rs   rt   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 =====
r5  z: 
FTz (concrete): )rf   r  r3  rN  r  r   r   r   rP  r$  r  rq   r   r  r2  hint)	ry   r$  graph_sizes_strr2  r5  rP  concrete_size
has_symintszrs   rs   rt   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
        )r  N)r   r  copy_graphstater  restore_graphstater   )ry   prior_global_statecurrent_global_staters   rs   rt   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$  encodingrs   rs   rs   rs   rt   rC  /  s   z5OutputGraph.run_compiler_collective.<locals>.<lambda>c                      s
    j  S rv   )local_staterenderrs   dsrs   rt   rC  3     
 )metadata_fn
payload_fnr`  T)log_pt2_compile_event)group)distributed_state
all_states
compile_pgr   inforc  r   _loggingtrace_structuredrz  devicerankdevice_countrK   rP  distall_gather_objectspeculation_logr   r    CompileCollectiveRestartAnalysis)ry   r  rn  rm  rs   re  rt   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  __compiled_fnrv  c                 s   r  rv   )as_proxyr  rs   rs   rt   r  U  r  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>)r   statscalls_capturedr  r   %sT)include_strideinclude_devicecoloreddynamo_output_graphc                      s   d   iS )Nsizes)rR  rs   rx   rs   rt   rC    s    z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>c                      s    j ddddS )NFT)print_outputr~  r  )print_readablers   )r   rs   rt   rC    s    )ri  r   NFr   )r   )_LazyGraphModule__self___lazy_forwardunique_graphs)Dr   _guardsr   clear_framer  r  r  r*  r'   r  r   r   rk  r^  
create_argr  r   replace_all_uses_withrv  _maybe_preserve_original_metar   do_not_emit_runtime_assertsremove_unused_get_attr_nodesr   r	   r   rf   r   r   remove_unused_graphargsrI   rJ   &remove_tensorify_specialized_graphargsre  r   r   r   r  r  r   r3  r   graph_code_logr  rR   rp  rq  rX  r   r  r   r   r   r   r   r^  call_user_compilertorch.fx._lazy_graph_moduler  r  rn   r  force_recompiler   install_global_unsafer)   make_call_generated_coderL  )ry   r  rvr0  replaced_outputsr  r$  output_nodeold_nodenew_nodencallsregister_finalizerold_fake_moder  backend_fake_modecompiled_fnr  lazy_gmrB  rs   )r   ry   rt   r%  A  s   









	

&z%OutputGraph.compile_and_call_fx_graphc                 C   s   | j jddS )Nplaceholderop)rf   
find_nodesrx   rs   rs   rt   placeholders  r`  zOutputGraph.placeholdersc                 C   s   dd | j D S )Nc                 S   s   g | ]}|j d  qS )r.  r3  )r   r2  rs   rs   rt   r     s    z)OutputGraph.graphargs.<locals>.<listcomp>)r  rx   rs   rs   rt   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_compilerbackend_compileT'aot_autograd_cumulative_compile_time_us)
phase_namerj  dynamo_compile_column_us)rK   _call_user_compiler)ry   r   rs   rs   rt   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   rI  call_methodcall_moduler   r  r.  rn   z<unknown compiler_fn>zcalling compiler function zdone compiler function z#compiler_fn did not return callablezBackend compiler exceptionz	Backend: z
Exception:z
Traceback:
zBackend compiler `z` failed with z. Adding a graph break.z-Report an issue to the backend compiler repo.r'  dynamor  )op_count
node_countinput_count)*r   rf   r  r  r   rQ   r3  rm   _dynamo_sourcer   _param_name_to_sourcer   _source_to_user_stacksr  rn   r   r   INFOr   r   r   r   callabler   r
   r,   r
  r+   inspectcurrentframewith_traceback__traceback__r/   r   r  r   format_frame_summaryr-   r   r   r   r!  )ry   r   totr  r2  plr  r$  r   r  ers   rs   rt   r    s   










zOutputGraph._call_user_compilerc                 C   s   t jjjr	t| S t S rv   )r   _dynamor   use_graph_deduplicationr0   r   rx   rs   rs   rt   r     s   
zOutputGraph.dedup_passc                 C   s0   t || jdd}||_d|_| j||d d |S )NT)requires_suffixFr-  )rO   r   rn   torchdynamo_force_dynamicr  )ry   r$  sub_gm	next_namers   rs   rt   install_subgraph  s
   zOutputGraph.install_subgraphc                 C   s   dd | j D }|S )Nc                 S      g | ]}|j qS rs   )example)r   r  rs   rs   rt   r   )  s    z.OutputGraph.example_inputs.<locals>.<listcomp>)r  )ry   r   rs   rs   rt   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 )Nr  r  T)reverser   )sortedrf   r  r!  r   usersrl  ry   r2  rs   rs   rt   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 )NTFr5  )	r  r	   Noder3  rN  r   SymBoolr2  maybe_as_bool)r  brrs   rs   rt   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   SymTypesTr5  F)
torch.fx.experimental.sym_noder  r  rq   r  r   r	   r  r3  rN  )r  r  rs   rs   rt   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  rI  Fr5  c                 3       | ]} |V  qd S rv   rs   r   r  r  rs   rt   r  h  r  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>c                 3   r  rv   rs   r  r  rs   rt   r  j  r  T)
r  r  r  r  r3  rN  r  r   r   r  )r2  r  r  rs   rt   is_symnode_compute_node]  s   
zDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_noder   )is_accessor_noder  rI  c                 S   s8   | j d }|j}t|tjrt|jjtjr|jjS d S Nr.  )	r3  r  r  r   r  r2  exprsympySymbol)r2  r  r  rs   rs   rt   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  r3  rm   r$  rl  re  rq  )r2  rx   rs   rt   remove_unused  s   
z:OutputGraph.remove_unused_graphargs.<locals>.remove_unusedfakec                 S   s   | t |O } d S rv   )r   )used_symbolsr  rs   rs   rt   update_used_symbols  rV  z@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbolsr.  c                    s
    | S rv   rs   )t)r  r  rs   rt   rC    rg  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>),r  r	   r2  Argument%torch.fx.experimental.symbolic_shapesr  rW  r   rf   r  r!  r  r  r  operatorgetitemr   _checkr   rl  r   r   r  r   r  r   r  r3  rX   r  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__r  r  wrapped_objpytreetree_map_onlyfake_tensorremove)ry   r  r  r  r2  r  r  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictattrfake_attr_valr  symbolrs   )r  ry   r  r  rt   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   )TensorifyStater5  r$  c                 s   s    | ]}|j d kV  qdS )itemNr  )r   urs   rs   rt   r    r  zEOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>)torch._dynamo.symbolic_convertr  rf   r  r3  rN  r  r   	item_memor  r2  _exprr  r  should_specializer$  r   r  r   rl  )ry   r  r2  r5  r  rs   rs   rt   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   rK  r  )ry   r#  rs   rs   rt   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   rH  r   )ry   r$  r~   rs   rs   rt   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.
        r  _c)r}   r   r   r  ry   r#  r~   r$  rs   rs   rt   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  r  rs   rs   rt   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  r3  re  rd  r   r   r   r  r   r   r  r  r  rs   rs   rt   cleanup  s"   










zOutputGraph.cleanupenabledc                 C   r   rv   )r  )ry   r  rs   rs   rt   r  +  r{   z$OutputGraph.set_torch_function_stater  c                 C   rT  rv   )r   r   )ry   r  rs   rs   rt   add_graph_finalizer.  s   zOutputGraph.add_graph_finalizerr2  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.  r  )r  r3  r  r   r  r  rs   rs   rt   example_value_from_input_node3  s   
z)OutputGraph.example_value_from_input_node)r   rv   )r  FN)r   N)drn   ro   rp   r   r6   rr   r   r   r   r   r"   r   Scoperz   r  rW   r%  r"  r   rS  r   rU  rX  propertyr/  r^  r_  rf   setterrd  re  rf  rh  rk  rl  
contextlibcontextmanagerru  rv  r  r   r   r  	GuardsSetr6  r   r  r  r  r  rI   r  r  r4  r  staticmethodr  r	   Proxyr  r   r   r   r   r  r  r   r;  r&  r  rR  rY  r^  r*  r%  r   r  r  rY   r  r   r!   r  r  r   r  r   r  r  r  r&   r  r  r  r  r  r  r  r  r   rs   rs   r   rt   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 )NrI  c                    s   | j dv rd S  j|  d S )N>   atenprimprims)	namespacer  r   r  output_graphrs   rt   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   only_allow_pt2_compliant_opsr.   err_epilogue)r  msgr  rs   rt   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   _ops
OpOverloadTagpt2_compliant_tagtagsOpOverloadPacketr  	overloadsr!  r  r  utilsget_fake_values_from_nodesr  r  _jit_resolve_packet_qualified_op_namer   r.   r   )r   kindr  r   r   r!  r&  r-  r  overloadr  rs   r  rt   check_pt2_compliant_opD  sr   



r4  c                   @   s   e Zd Zdd Zdd ZdS )	LazyProxyc                 O   s   || _ || _|| _|| _d S rv   )rt  r   r   r   )ry   rt  r   r   r   rs   rs   rt   rz     s   
zLazyProxy.__init__c                 C   s   | j | ji | jS rv   )r   r   r   rx   rs   rs   rt   r     r  zLazyProxy.__call__N)rn   ro   rp   rz   r   rs   rs   rs   rt   r5    s    r5  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   rz   weakrefr  r   r   r	   Graphrf   r   rd  re  rm  rn  lifted_freevarsrf  	prev_instunder_activation_checkpoint#allow_side_effects_under_checkpointis_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str)ry   r   rm  r   rn  r   rs   rt   rz     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)
r?  r@  rA  current_instructionstarts_linerN  r	   r  _COPY_META_FIELDSr3  )ry   r  r2  linenonode_idxr3  fieldrs   rs   rt   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  )rH  c                     s(   t   } djj d d|  S )NzTRACE FX call z from rS  )rL   rstripr2  r$  )line)cur_instheaderr  tx_coders   rt   get_trace_call_log_strF  s   z;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strr}  Forig_graphmodulec                   S   s   d S rv   rs   rs   rs   rs   rt   rC  Q  s    z-SubgraphTracer.create_proxy.<locals>.<lambda>Tc                 S   r  rs   r  )r   ndrs   rs   rt   r   U  s    z/SubgraphTracer.create_proxy.<locals>.<listcomp>nn_module_stack>   r  rI  rB  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)r  )r   r   r  tyr  rs   rt   r  u  s    
z.SubgraphTracer.create_proxy.<locals>.<genexpr>z2Invoking an nn.Module inside a HigherOrderOperatorr&  r   rD  rm  )Arm  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   rh  r   r  r  r  rE  r9  	positionsrH  r  get_line_of_code_headertrace_call_logr  rS   r>  r(   get_contextrN  r  r   r	   r   rf   r  r?  _lineno_mapr@  r   __code__r   rA  rS  r   r2  r3  rB  r$  r.   rn  r   r   r  is_co_filename_from_nn_modulesframe_summaryr  r  r   StackSummary	from_listformatr  rD  r  r   r  track_nodes_for_deduplicationr   
track_node)ry   r2  r  r   r   r$  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsr  maybe_new_argr  rP  is_retracingorig_graphmodule_mayberS  frame_summariesmsgsr   )rM  rN  r  r  rO  rt   rh    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 SubgraphTracerrG  )r4  r   rm  r  arg_tree_leavesr  r   r	   r  rf   r   rk  r   r3  )
ry   r  r  r   r   r$  rf  rh  r  r2  r   rs   rt   rk    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 rZ  )r!  r  r  rf   rK  rW  r   r  rK  rd  rq  r$  )ry   r2  user_graph_nodesuserother_graph_noders   rs   rt   rl    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)only_allow_inputr  rs   rf  )#r   r  r$  r=  rm  r   r=   r   r   
setdefaultr   r   extract_stackrO   rd  r   rW  r2  rf   inserting_beforeinserting_afterrh  rV   popitemr   compileris_compilingr  r   _lift_basic_symbolsr  r  r  r  rf  )ry   r$  rf  r5  beforerm   	prev_namer2  ctxr  r   r   is_strict_exportis_non_strict_exportrs   rs   rt   r0    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 SubgraphTracerr5  )rm  r2  r3  r  r   r  r  rf  r8  rt  lift_tracked_freevar_to_inputr0  r$  r1  )ry   r  r5  	new_proxyrs   rs   rt   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 rv   )rW  )r   sub_argrx   rs   rt   r  l	  s
    
zESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>)
r  r   r	   r  slicestartstopsteprt  r  )ry   r  rs   rx   rt   rW  _	  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   )
r  r   r  r2  r  r  r  r   is_unbacked_symintrf  )rO  r   rx   rs   rt   	need_bind	  s   z8SubgraphTracer.track_unbacked_symbols.<locals>.need_bindc                    s     j |i |}t|j|  |S rv   )rh  rV   r2  )r5  r   r   r  )rt  rs   rt   _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 %srI  ru  z@_track_unbacked_symbols %s for %s.stride()[%s] at debug_level %s)(rt  r  r   r   r   r   	enumeraterP  r   r  r=  r5  opsr  sym_sizerq   r1  track_unbacked_symbolslayoutstridedstride
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr   __tensor_flatten__r  r  r2  r  rf  )ry   r5  r  r  r  irO  
lazy_proxyattrsr  r   inner_tr  rs   )ry   rt  rt   r  ~	  s   






z%SubgraphTracer.track_unbacked_symbolsr5  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 )NFrO  rm   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   r5  )r~  rm   z4_lift_symbols_in_symint %s from %s at debug_level %szsubgraph inputsr   zyFor root tracer, we only expect to bind basic symbols (compound symbols should be cached before) but got unbound symbols z in zSource of 'z' is None when lifting it to input of top-level. If it's an unbacked symbol, this could be because it's not tracked with lazy_bind_unbacked_symbols. Otherwise, should provide a source when create_graph_input for `z` at root tracer.F)pass_arg_as_tensorr  	is_tensorr.  )r   r  r   r  lookup_unbound_symbolsr!  rm  r}  rf  r2  r3  r0  r   r1  r   r  r$  r=  r8  r   iterrY   )rO  rm   r~  self_to_be_bounds0parent_proxyexample_valphrx   rs   rt   _lift_symbols_in_symint	  sx   


zCSubgraphTracer._lift_basic_symbols.<locals>._lift_symbols_in_symintT)r~  )F)$r   rq   r   r  r   r   r   r  r   r  rP  rD   rC   SIZEr  r  r  STRIDEstorage_offsetSTORAGE_OFFSETr  r}  r  r  r  r  r  r  r  r  r  r  r   r  r  r7   )
ry   r5  r  r  r  rO  r  r  r   r  rs   rx   rt   r}  	  sv   
D			
z"SubgraphTracer._lift_basic_symbolsrO  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 rv   )r$  )rO  rs   rs   rt   rC  v
  s    z7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>)r   )r2  r  r   r!  rf  r   r  r5  r   r	   r  rt  r  )ry   rO  r   to_be_boundr  r  rs   rs   rt   r  c
  s    






z%SubgraphTracer.lookup_unbound_symbols)NFN)NNN)NNNNr  )rn   ro   rp   r   rz   r  rh  rk  rl  r0  r  rW  r   r5  r   r	   r  r  r  r   r   r   r}  r   r  r  r  r   rs   rs   r   rt   r     s4    E 9
[(
c
 "r   )r   r   r  r   r  r  r   r   r  r  r  r   r6  dataclassesr   typingr   r   r   r   r   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedru  torch.nntorch.utils._pytreer.  _pytreer  r	   torch._dynamo.excr
   r   r   r   r   r   r   torch._subclasses.fake_tensorr   torch._utils_internalr   r  r   %torch.fx.experimental._backward_stater   r  r   r   r   r   torch.fx.passes.runtime_assertr   torch.utils._python_dispatchr   r&  r   r   r   r   r    backends.registryr!   r"   bytecode_transformationr#   r$   r%   r&   r'   r(   r6  r)   current_scope_idr*   r+   r,   r-   r.   r/   graph_deduplicationr0   graph_region_trackerr1   r6  r2   r3   mutation_guardr4   r   r5   r6   rm   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   variables.baserW   variables.builderrX   rY   rZ   r[   variables.listsr\   variables.miscr]   r^   variables.nn_moduler_   variables.tensorr`   ra   rb   rc   variables.torch_functionrd   r  re   	getLoggerrn   r   rp  getArtifactLoggergraph_tabular_logr  graph_sizes_logr[  rk   ru   	lru_cacher   r   r   r   r   r   r   r   r   objectr  r   r$  r4  r   r   r5  Tracerr   rs   rs   rs   rt   <module>   s    @P

#            <	I