o
    ߗi                    @   s0  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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& 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	l0m1Z1 d d
l2m3Z3m4Z4m5Z5 d dl6m7Z7 d dl8m9Z9 ddl:m;Z;m<Z<mZ=m>Z> ddl?m@Z@mAZA ddlBmCZCmDZDmEZEmFZFmGZG ddlHmHZH ddlImJZJ ddlKmLZL ddl<mMZMmNZNmOZOmPZPmQZQ ddlRmSZS ddlTmUZU ddlVmWZWmXZX ddlYmZZZ ddl[m\Z\m]Z] ddl^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZk ddl!mlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z| ddl}m~Z~ ddlmZmZmZmZ 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d.d/ ZeG d0d1 d1Zd2d3 ZG d4d5 d5ejjZG d6d7 d7Zeeef ZG d8d9 d9Zd:Zd;d< Ze ZG d=d> d>ZG d?d@ d@e$jZdS )A    N)	dataclass)
AnyCallablecastDictListOptionalSetTupleTYPE_CHECKINGUnion)fx)TensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourceTracingContext)signpost_event)_make_graph_module)BackwardState)free_symbolsis_symbolicShapeEnv)insert_deferred_runtime_asserts)is_traceable_wrapper_subclass   )configexc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unimplemented_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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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    rq   rq   X/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_dynamo/output_graph.pyri      s   
 ri   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selfrq   rq   rr   __init__      
zVariableTrackerCache.__init__c                 C   s&   t t||}|| jvrd S | j| S rt   ri   idru   )rw   valuerk   keyrq   rq   rr   lookup   s   

zVariableTrackerCache.lookupc                 C   s   t t||}|| j|< d S rt   rz   )rw   r|   rk   vtr}   rq   rq   rr   add   s   zVariableTrackerCache.addc                 C   s   t  }|j| j |S rt   )rs   ru   update)rw   	new_cacherq   rq   rr   clone   s   zVariableTrackerCache.clonec                 C   s   | j   d S rt   )ru   clearrv   rq   rq   rr   r         zVariableTrackerCache.clearN)rl   rm   rn   rx   r~   r   r   r   rq   rq   rq   rr   rs      s    rs   c                   C   s
   t tS rt   )torchdynamo_loggingget_step_loggerlogrq   rq   rq   rr   _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 rt   )r   rN   appendrv   rq   rq   rr   __post_init__   s   z GraphCompileReason.__post_init__N)rl   rm   rn   __doc__strrp   r   	tracebackFrameSummaryr   boolr   rq   rq   rq   rr   r      s   
 r   c                    s    fdd}|S )Nc                      s   dd  D S )Nc                 S   s    g | ]\}}}||i |qS rq   rq   ).0fnargskwargsrq   rq   rr   
<listcomp>   s     zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>rq   rq   random_callsrq   rr   _gen_rand_values   r   z1_get_gen_rand_values_fn.<locals>._gen_rand_valuesrq   )r   r   rq   r   rr   _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 rt   )superrx   itemssetattr)rw   r   kv	__class__rq   rr   rx      s   
zFakeRootModule.__init__returnc                 C   s   dS )NzFakeRootModule(...)rq   rv   rq   rq   rr   __repr__   s   zFakeRootModule.__repr__)rl   rm   rn   r   r   r   torchnnModulerx   r   __classcell__rq   rq   r   rr   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 rt   )r   )rw   r   rq   rq   rr   rx      ry   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)rE   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessrG   rS   RuntimeError	Exceptionr   	exception)rw   r   r   copy_gmcorrectresultrq   rq   rr   __call__   s(   


	

zWrapperBackend.__call__N)rl   rm   rn   r"   rx   r   r   GraphModuler   Tensorr   rq   rq   rq   rr   r      s     r   c                
       sP  e Zd ZdZ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.d/ Z#ed0d1 Z$ed2d3 Z%ed4d5 Z&ed6e'j(j)fd7d8Z*ed6eeef fd9d:Z+dd<d=Z,d>d? Z-d@dA Z.edBdC Z/dDdE Z0dFdG Z1dHdI Z2ddKdLZ3dMdN Z4e5dOdP Z6dQe7e'j8j9e'j:ef fdRdSZ;dTdU Z<	;ddWee= fdXdYZ>dZd[ Z?d\d] Z@d^d_ ZAd`efdadbZBe!j"dcdd ZCdedf ZDdgdh ZEed6eFeGjH fdidjZIed6eFeJ fdkdlZKdmeGjLd6eMfdndoZNdmeGjLd6eMfdpdqZOdrds ZPdtdu ZQd6eFe'j: fdvdwZRddxdyZSdzeFeT d6d;fd{d|ZUdd}d~ZVd6efddZWd6efddZXdddZYde	d6d;fddZZdeeGjLgd;f d6d;fddZ[de'jGjHfddZ\  Z]S )OutputGrapha  
    Wrapper class to hold outputs of InstructionTranslator.  Mainly the
    generated fx.Graph.

    OutputGraph is 1:1 with a frame being processed. Each frame is associated
    with some root InstructionTranslator. When user code calls a function,
    we construct a InliningInstructionTranslator that continues to write into
    the root InstructionTranslator's OutputGraph.
    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   rx   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defaultdictlisttracked_fakes_id_to_sourceparam_name_to_sourcer6   side_effectsrs   variable_tracker_cache	itertoolscountunique_var_iddictr   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)rw   r   r   r   r   r   r   r   r   f_coder  r   _config	fake_moder   rq   rr   rx     s   


	




zOutputGraph.__init__c                 C   s&   | j d }t|ts|j}| d|S )N__builtins____builtins_dict__)r   
isinstancer   __dict__install_global)rw   
f_builtinsrq   rq   rr   r    s   

z-OutputGraph.install_builtins_dict_in_fglobalshookc                 C   s6   | t | j }|| jvsJ || j|< ||  fS rt   )lenr  get_backward_state_proxy)rw   r  prefixnamerq   rq   rr   add_backward_state_hook  s   
z#OutputGraph.add_backward_state_hookc                 C   sZ   | j d u r*| jrtd t }| jjdt||t d| _ t | j j	j
d< |  | _| j S )Nz&backward_state does not support exportdynamo_backward_staterk   grapharg)r  r   r.   r   root_tracercreate_graph_inputtyper8   rV   nodemetanew_varr  )rw   example_valuerq   rq   rr   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 rt   )guardsr   r@   
make_guardr2   	SHAPE_ENVr;   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATEr   r  r   peek_interpreter_stackFUNCTORCH_STACK_MATCH)rw   cirq   rq   rr   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 rt   )load_import_fromrm   rl   rq   cgr   rq   rr   <lambda>  s    z3OutputGraph.synthetic_graph_input.<locals>.<lambda>F)r,  r)   r   add_push_nullforeachmapr    ConstantVariablecreatecall_functionr  storer  extendget_instructionsrA   rU   buildr   getguards_contextdynamo_guardsremove_guards_with_source)rw   r   r   r-  varnamerk   r   rq   r9  rr   synthetic_graph_input  s    

z!OutputGraph.synthetic_graph_inputr   c                 C      | j | d S rt   )r   r   )rw   r   rq   rq   rr   add_cleanup_hook     zOutputGraph.add_cleanup_hookc                 C   s$   t | jD ]}|  q| j  d S rt   )reversedr   r   )rw   r  rq   rq   rr   call_cleanup_hooks  s   zOutputGraph.call_cleanup_hooksc                 C   
   | j d S Nr   r   rv   rq   rq   rr   r'    r   zOutputGraph.root_tracerc                 C   rQ  NrS  rv   rq   rq   rr   current_tracer  r   zOutputGraph.current_tracerc                 C   s   t | jdkS )Nr   )r  r   rv   rq   rq   rr   is_root_tracer!     zOutputGraph.is_root_tracerc                 C      | j jS rt   rV  rd   rv   rq   rq   rr   rd   %     zOutputGraph.graphc                 C   s   || j _d S rt   rZ  )rw   r|   rq   rq   rr   rd   *  s   c                 C   rY  rt   )rV  input_name_to_proxyrv   rq   rq   rr   r\  .  r[  zOutputGraph.input_name_to_proxyc                 C   rY  rt   )rV  real_value_cacherv   rq   rq   rr   r]  2  r[  zOutputGraph.real_value_cachec                 C   rY  rt   )rV  bound_symbolsrv   rq   rq   rr   r^  6  r[  zOutputGraph.bound_symbolsc                 O      | j j|i |S rt   )rV  create_proxyrw   r   r   rq   rq   rr   r`  @     zOutputGraph.create_proxyc                 O   r_  rt   )rV  create_nodera  rq   rq   rr   rc  C  rb  zOutputGraph.create_nodec                 O   r_  rt   )rV  remove_nodera  rq   rq   rr   rd  F  rb  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*   re  rV  	__enter__r   r   r   r   __exit__pop)rw   rf  prior_tracernew_scope_ctxtracerrq   rq   rr   	subtracerI  s*   
zOutputGraph.subtracerc                 C   s   | S rt   rq   rv   rq   rq   rr   outputa  s   zOutputGraph.outputc                 C   rY  rt   )r   r  rv   rq   rq   rr   r  e  r[  zOutputGraph.fake_modec                 C   
   | j jjS rt   )r   r  r   rv   rq   rq   rr   r   i  r   zOutputGraph.shape_envr   c                 C   ro  rt   )r   rG  rH  rv   rq   rq   rr   r.  m  r   zOutputGraph.guardsc                 C   ro  rt   )r   module_contextr   rv   rq   rq   rr   r   q  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   r
   r   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)rw   outrz  rq   rq   rr   r  u  s2   zOutputGraph.save_global_statec                 C   rL  rt   )r   r   )rw   txrq   rq   rr   push_tx  rN  zOutputGraph.push_txc                 C   s
   | j  S rt   )r   ri  rv   rq   rq   rr   pop_tx  ry   zOutputGraph.pop_txc                 C   s   | j s| jS | j d S rT  )r   r   rv   rq   rq   rr   
current_tx  s   zOutputGraph.current_txc                 C   s
   t | jS rt   )rH   rd   rv   rq   rq   rr   rH     ry   zOutputGraph.count_callsc                 C   s   t t| jjdkS rR  )r  r   rd   nodesrv   rq   rq   rr   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)rw   keysobjr   rq   rq   rr   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   )rw   r"  existingvarrq   rq   rr   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   )rw   r"  rq   rq   rr   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)joinr>  r   rer  isalpha)namesr"  rq   rq   rr   module_key_name  s   zOutputGraph.module_key_name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 D ]Q} jvr jvrj < tt	jjr fdd	}	td
rֈ D ]\}
}|	|
 qtdr D ]\}
}|	|
 q|   S | d|  qt d)Nrk   c                    s   j d usJ j | < jjjv rjjj S tdkr)ttj nt	s5ttj
 tjd| di fdi }jjj|}d|jjjvsYJ t|jjjd< |S )Nguardedget_attrrq   r-  tensor_dict)r   r   rn  r   rM   r3   r/  r2   ID_MATCHr<   TENSOR_MATCHrY   r`  track_object_existingproxyr*  r+  rD   )
module_keyr   )optionsrw   rk   r  rl  rq   rr   	wrap_name  s(   
	z6OutputGraph.register_attr_or_module.<locals>.wrap_namec                    s   t t| fi  S rt   )r]   r)  r  r  r  rq   rr   r    s   c                    s   t jfi  S rt   )r    UnspecializedNNModuleVariabler  r  rq   rr   r  %  rb  c                    s$   t jd| di fdi S )Nr  rq   sym_num)r_   r@  r`  r  )r  rw   r  rq   rr   r  0  s   c                    s*    j |   j| < t t| dS )N)source_name)rn  r  r   rU   rE  r9   r  )rw   r  rq   rr   r  ;  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"  rw   rk   rq   rr   register_leaf_nameO  s   


z?OutputGraph.register_attr_or_module.<locals>.register_leaf_name_parameters_buffersr  unreachable)!r4   r   r   rU   rE  r  r   r  r?   r   r   rV  rW  r'  r   r   r3   r/  r2   	NN_MODULESymIntSymFloatr   r   r   r  r   r   r   hasattrnamed_parametersnamed_buffersAssertionError)rw   r  r  r  r  r   r   baseir  r  r  rq   )r"  r  rw   rk   r  rl  rr   register_attr_or_module  sP   





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 )Nrw   r  _ref	LOAD_FASTargvalBINARY_SUBSCR
STORE_FAST)r   rF  rL   stacksymbolic_localsvaluesr   store_attr_mutationsr  ri  r  rZ   r   r   mutation_typer5   rk   r:   r  r>   
local_namer   	graphargs_exampler   r   indexr,  rC  r$   r%   )rw   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_sourcerq   rq   rr   handle_aliases_for_stolen_listsg  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 s)t	d g t
jd	krbjD ].}|jd
krEtd
|jd q3|jdkrYtdtjd d q3t| q3jrljrlJ 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 ]@\}}t%|j&t'r|j&j(|krqt%|t)r|j(|krqt
jdkrt*+t,|rqnt*+t,|rJ ||vrg ||< || | q|- D ]}|||  |	|gt||   qtj.dkri|  g }/d_0|t1j.}2d|}t3| d}||4|d |t5dd ||6j7j0 8| |	rtdd |	D rtdd |	D rtt9|	t|	krɈj:; rtj<dksɈj=s|  8>tt|	||tdt|	dg  8 fddt|D  dS /d }t3|| d}?|	| t3||d!d |j@" D  d"}?|	| d}g }tAjBdks	t|jCdkr1|>|D || t|jCdkr)||6| d}n|td# nE |  8||F   8 fd$dt|D  |rc8t3 dG|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 rt   )can_restore)r   blockrq   rq   rr   	<genexpr>      z/OutputGraph.compile_subgraph.<locals>.<genexpr>z&compile_subgraph with block_depth != 0      	MAKE_CELLr  COPY_FREE_VARSco_freevars)r  z)export does not support pregraph_bytecodec                      s         d S rt   )add_output_instructionsr   rq   )prefix_instsrw   rq   rr   append_prefix_insts  s   
z9OutputGraph.compile_subgraph.<locals>.append_prefix_insts)is_graph_breakc                 S   s   i | ]	\}}|t |qS rq   )rR   )r   r"  modrq   rq   rr   
<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 rt   )r  ra   r^   rb   r_   python_typefloat)r   r   rq   rq   rr   r  8  s    

c                 s   s    | ]}t |tV  qd S rt   )r  r`   r   r  rq   rq   rr   r  D  s    UNPACK_SEQUENCEc                       g | ]}t  d |qS r  r)   create_storer   r  r  r  rq   rr   r   T      z0OutputGraph.compile_subgraph.<locals>.<listcomp>	graph_outc                 S   s   i | ]\}}|d kr|dqS )r   Nrq   )r   valr   rq   rq   rr   r  g  s    )tempvarsr  POP_TOPc                    r  r  r  r   r  rq   rr   r     r  )H
decoratorsr  partial_convertcompile_subgraph_reasonr   r   debugallblock_stackr.   sysversion_infor  opnamer   r$   r  r  r   r   r  r   rC  r  rO  exitr   cleanup_graphprune_dead_localsr   r  realize
dedup_passr   r   r   r  r  rk   r>   r  r[   r)  __instancecheck__r\   r  r   r,  r  r   r  r)   load_function_namer#   r  rn  r  r   r   is_emptydebug_localsr  compile_and_call_fx_graphcodegen_suffixusesrH   rd   graph_outputsgraph_output_varsrun_compiler_collectiverD  create_delete)rw   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_varrn  rq   )r  r  rw   r  rr   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 rt   rq   rq   r:  	debug_varrq   rr   r;    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  r<  extend_outputr#   r  r$   restore_stackcodegen_update_mutated)rw   r  r!  r:  r"  r  r   r  rq   r0  rr   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   rd   r  r+  ri  r   r}  zipr  r  _set_grad_enabledtupler   _erased
erase_node)rw   r  r*  rq  node1node2rq   rq   rr   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 rq   )r  ro   repr)r   srq   rq   rr   r     s   " z:OutputGraph.get_graph_sizes_structured.<locals>.<listcomp>)
rd   r  r+  rF  r  r   r   
FakeTensorsizer"  )rw   retr*  r-  rF  rq   rq   rr   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): )rd   r  r+  rF  r  r   r   rE  rF  r"  r>  ro   r   r  r*  hint)	rw   r"  graph_sizes_strr*  r-  rF  concrete_size
has_symintszrq   rq   rr   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   ry  copy_graphstater  restore_graphstater   )rw   prior_global_statecurrent_global_staterq   rq   rr   restore_global_state  s   z OutputGraph.restore_global_statec                    s   |j   d ur] jd u r_ j}td j tjjddd  fddd tj	
| tj	   d g|  }tj| j|d | _W d    n1 sPw   Y  |j  tjd S d S )Nzcompiler_collective %sartifactc                   S   s
   dddS )Ncompiler_collectivestring)r"  encodingrq   rq   rq   rq   rr   r;    s   z5OutputGraph.run_compiler_collective.<locals>.<lambda>c                      s
    j  S rt   )local_staterenderrq   dsrq   rr   r;       
 )metadata_fn
payload_fn)group)distributed_state
all_states
compile_pgr   inforY  r   _loggingtrace_structuredrr  devicerankdevice_countrF  distall_gather_objectspeculation_logr   r    CompileCollectiveRestartAnalysis)rw   r  rc  rb  rq   r[  rr   r    s    

z#OutputGraph.run_compiler_collectivec                    s  t jj d 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sft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 -  j.j/}jsddl0m1  m} |j2dd t j3j4|jd}W d   n1 sw   Y  |j._/5  6 }W d   n	1 s	w   Y  ddl7m8} t	||s*t	t9|dd|rC|j:dkrCt	||r2|n|j;}|<| t	||sC|j=}||}td d  d7  < >|| t?|}|@| |A W  d   S 1 sow   Y  dS )zr
        Generate code from self.graph and return the Instruction()s to
        call that generated code.
        r   r  __compiled_fnrn  c                 s   r  rt   )as_proxyr  rq   rq   rr   r  '  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)rH  rq   rv   rq   rr   r;  Q  s    z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>c                      s    j ddddS )NFT)print_outputrs  rt  )print_readablerq   )r   rq   rr   r;  R  s    )r_  r   NFr   )r   )_LazyGraphModule__self___lazy_forwardunique_graphs)Br   _guardsr   clear_framer  r  r   r  r'   r  r   r   rc  rV  
create_argr>  r   replace_all_uses_withrn  _maybe_preserve_original_metar   do_not_emit_runtime_assertsr   r   r   rd   r   r   remove_unused_graphargsrH   rI   r]  r   r   r   r	  r  r   r+  r   graph_code_logr
  rP   re  rf  rP  r   r  r   r   r   r   r   rT  call_user_compilertorch.fx._lazy_graph_modulerz  r  rl   r{  force_recompiler   install_global_unsafer)   make_call_generated_coderD  )rw   r  rvr$  replaced_outputsr  r"  output_nodeold_nodenew_nodencallsregister_finalizerold_fake_moder  backend_fake_modecompiled_fnrz  lazy_gmr:  rq   )r   rw   rr   r    s   









	

&z%OutputGraph.compile_and_call_fx_graphc                 C   s   | j jddS )Nplaceholder)op)rd   
find_nodesrv   rq   rq   rr   placeholders  rX  zOutputGraph.placeholdersc                 C   s   dd | j D S )Nc                 S   s   g | ]}|j d  qS )r&  r+  )r   r*  rq   rq   rr   r     s    z)OutputGraph.graphargs.<locals>.<listcomp>)r  rv   rq   rq   rr   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_namelog_pt2_compile_eventdynamo_compile_column_us)rJ   _call_user_compiler)rw   r   rq   rq   rr   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|_z;t| j drJ| j jnd}t tjd|  | j }tjr`t|}|||  }	t tjd	|  t|	syJ d
W n[ ty     ty }
 z%| jrt| j |
|
jd d| j  d}t|
| jj | W Y d }
~
n(d }
~
w t!y }
 z|
d }
~
w t"y }
 zt| j |
|
jd d }
~
ww t#ddi | j$|t%|jjt%|d |	S )Nr   rA  call_methodcall_moduler   r  r&  rl    zcalling compiler function zdone compiler function z#compiler_fn did not return callablez9Backend compiler failed with a fake tensor exception at 
zAdding a graph break.dynamor  )op_count
node_countinput_count)&r   rd   r  r  r   rO   r+  rk   _dynamo_sourcer   _param_name_to_sourcer   _source_to_user_stacksr  rl   r   r   INFOr   r   r   r   callabler   r,   r  r+   with_traceback__traceback__r   format_frame_summaryr/   r  r-   r   r   r   r  )rw   r   totr  r*  plr  r"  r   r  emsgrq   rq   rr   r    s   







zOutputGraph._call_user_compilerc                 C   s   t jjjr	t| S t S rt   )r   _dynamor   use_graph_deduplicationr0   r   rv   rq   rq   rr   r    s   
zOutputGraph.dedup_passc                 C   sV   d }d}|s| d| }|| j v r|d7 }n|}|r||_d|_| j||d d |S )Nr   r  r   Fr%  )r   rl   torchdynamo_force_dynamicr  )rw   r"  sub_gm	next_namer  r   rq   rq   rr   install_subgraph  s   

zOutputGraph.install_subgraphc                 C   s   dd | j D }|S )Nc                 S      g | ]}|j qS rq   )example)r   r  rq   rq   rr   r     s    z.OutputGraph.example_inputs.<locals>.<listcomp>)r  )rw   r   rq   rq   rr   r     s   zOutputGraph.example_inputsc                    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+  rF  r   SymBoolr*  maybe_as_bool)r  brrq   rq   rr   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  ro   r  r   r   r  r+  rF  )r  r  rq   rq   rr   is_symnode_arg  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  rA  Fr-  c                 3       | ]} |V  qd S rt   rq   r   r  r  rq   rr   r  .  r  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>c                 3   r  rt   rq   r  r  rq   rr   r  0  r  T)
r  r  r  r  r+  rF  r  r   r   r  )r*  r  r  rq   rr   is_symnode_compute_node#  s   
zDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_noder   )is_accessor_noder  rA  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  rq   rq   rr   placeholder_binds_symbolE  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+  rk   r"  rd  r]  ri  )r*  rv   rq   rr   remove_unusedN  s   
z:OutputGraph.remove_unused_graphargs.<locals>.remove_unusedfakec                 S   s   | t |O } d S rt   )r   )used_symbolsr  rq   rq   rr   update_used_symbolsX  rN  z@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbolsr&  c                    s
    | S rt   rq   )t)r  r  rq   rr   r;  y  r]  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>),r   r   r*  Argument%torch.fx.experimental.symbolic_shapesr  rO  r   rd   r  r  usersr  r  operatorgetitemr   _checkr   rd  r   r   r  r   r  r   r  r+  rV   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)rw   r  r  r  r*  r  r  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictattrfake_attr_valr  symbolrq   )r  rw   r  r  rr   r    s   




	









z#OutputGraph.remove_unused_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   rC  r   )rw   r!  rq   rq   rr   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   rF   r@  r   )rw   r"  r|   rq   rq   rr   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  rw   r!  r|   r"  rq   rq   rr   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  rq   rq   rr   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   rd   r  r+  r]  r\  r   r   r   r  r   r   r  r  rw   r*  rq   rq   rr   cleanup  s"   










zOutputGraph.cleanupenabledc                 C   r   rt   )r  )rw   r  rq   rq   rr   r{    ry   z$OutputGraph.set_torch_function_stater  c                 C   rL  rt   )r   r   )rw   r  rq   rq   rr   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&  r  )r  r+  r  r   r  r  rq   rq   rr   example_value_from_input_node  s   
z)OutputGraph.example_value_from_input_node)r  rt   )r  FN)r   N)^rl   rm   rn   r   r   r   r   r   r"   r   Scoperx   r  rU   r#  r   r   rK  r   rM  rP  propertyr'  rV  rW  rd   setterr\  r]  r^  r`  rc  rd  
contextlibcontextmanagerrm  rn  r  r   r   r~  	GuardsSetr.  r   r  r  r  r  rH   r  r  r,  r  staticmethodr  r   r   r   r   r  r  r   r/  r  r  rH  rO  rT  r  r  r   r   r  r  rW   r  r   r!   r  r  r  r  r   r  r&   r  r  r  r  r  r{  r  r  r   rq   rq   r   rr   r      s    

	 7






	




,


	

 W
 X	
v	B
 


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              
      sx  |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t|
 W Y d }
~
nd }
~
ww t||	}tjj|jv r|| d S ||d| d|	 d d S d S )NrA  c                    s   | j dv rd S  j|  d S )N>   atenprimprims)	namespacer
  r   r  output_graphrq   rr   encountered_compliant_op  s   
z8check_pt2_compliant_op.<locals>.encountered_compliant_opc                    s*    j |  tjrt|d t  d S d S )N )r	  r   r   only_allow_pt2_compliant_opsr.   err_epilogue)r  r  r  rq   rr   encountered_non_compliant_op  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+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  rq   r  rr   check_pt2_compliant_op  sh   


r  c                   @   s   e Zd Zdd Zdd ZdS )	LazyProxyc                 O   s   || _ || _|| _|| _d S rt   )rl  r   r   r   )rw   rl  r   r   r   rq   rq   rr   rx   ,  s   
zLazyProxy.__init__c                 C   s   | j | ji | jS rt   )r   r   r   rv   rq   rq   rr   r   2  r  zLazyProxy.__call__N)rl   rm   rn   rx   r   rq   rq   rq   rr   r  +  s    r  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 ur5|jd nd| _d | _d | _d | _d | _| jd u rNg | _d S | jj| j||fg | _d S )NFr   r   )r   rx   weakrefr  r  r   r   Graphrd   r   r\  r]  re  lifted_freevarsr^  	prev_instunder_activation_checkpoint#allow_side_effects_under_checkpointdebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str)rw   r  re  r   rf  r   rq   rr   rx   >  s,   


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&  r'  current_instructionstarts_linerF  r   r  _COPY_META_FIELDSr+  )rw   r  r*  linenonode_idxr+  fieldrq   rq   rr   r    s.   

z,SubgraphTracer._maybe_preserve_original_metac              	      sH  | 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< n*|dkr| j d urt,d | j*j(j+t-fddj(j)d . D fg j(j)d< | /|j( |sWdj(j)vr|j&}|r|' j(j)d< dj(j)vrW|dv r2| j*j(j+fg j(j)d< n%|dkrW| j d urAt,d | j*j(j+j(j)d  d fg j(j)d< dj(j)vrg }|rx|0 so||1  t2|dd }|sc|3  t4j56|7 }d8|j(_9tj:j;j<stj:j;j=r| jj>?| jj	j( S )Nr  r  )r.  c                     s(   t   } djj d d|  S )NzTRACE FX call z from rI  )rK   rstripr*  r"  )line)cur_instheaderr  tx_coderq   rr   get_trace_call_log_str  s   z;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strrr  Forig_graphmodulec                   S   s   d S rt   rq   rq   rq   rq   rr   r;    s    z-SubgraphTracer.create_proxy.<locals>.<lambda>Tc                 S   r  rq   r  )r   ndrq   rq   rr   r     s    z/SubgraphTracer.create_proxy.<locals>.<listcomp>nn_module_stack>   r  rA  r(  r  z0Invoking an nn.Module inside HigherOrderOperatorc                 3   s.    | ]\}\}}| d d  kr|V  qdS )@r   N)r  )r   r   r  tyr  rq   rr   r    s    
z.SubgraphTracer.create_proxy.<locals>.<genexpr>r   r*  re  r  )@re  r  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r`  r  r  r  r  r+  r   	positionsr.  r  get_line_of_code_headertrace_call_logr
  rQ   r$  r(   get_contextrF  r  r   r   r   rd   r  r%  _lineno_mapr&  r   __code__r   r'  r9  r   r*  r+  r(  r"  r.   r   r   r  is_co_filename_from_nn_modulesframe_summaryr  reverser   StackSummary	from_listformatr  r*  r  r   r  track_nodes_for_deduplicationr   
track_node)rw   r  r  r   r   r"  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsr  maybe_new_argr  r6  is_retracingorig_graphmodule_mayber9  frame_summariesmsgsr   )r3  r4  r  r  r5  rr   r`    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;  )r  r  re  r  arg_tree_leavesr  r   r   r  rd   r   rc  r   r+  )
rw   r  r  r   r   r"  rM  rO  r  r*  r   rq   rr   rc  @  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 rR  )r  r  r  rd   rC  rO  r   r  r@  r\  ri  r"  )rw   r*  user_graph_nodesuserother_graph_noderq   rq   rr   rd  S  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  || jv r^t D ]}| d| }|| jvr]|} nqK| jr~tt| j}| j| j}	|rw| 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  r  rq   rM  )$r   r
  r"  r#  re  r   r=   r  r   
setdefaultr   r   extract_stackr\  r   r   r   rO  r*  rd   inserting_beforeinserting_afterr`  rT   popitemr   compileris_compilingr  r   _lift_basic_symbolsr  r  r  r  r^  )rw   r"  rM  r-  beforerk   r  candidate_name	prev_namer*  ctxr  r   r   is_strict_exportis_non_strict_exportrq   rq   rr   r(  i  sl   







$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-  )re  r*  r+  r  r   r  r  r^  r  rl  lift_tracked_freevar_to_inputr(  r"  r)  )rw   r  r-  	new_proxyrq   rq   rr   rk    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 rt   )r=  )r   sub_argrv   rq   rr   r   	  s
    
zESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>)
r  r   r   Proxyslicestartstopsteprl  rk  )rw   r  rq   rv   rr   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   )
r  r   r  r*  r  r  r  r   is_unbacked_symintr^  )rD  r   rv   rq   rr   	need_bind%	  s   z8SubgraphTracer.track_unbacked_symbols.<locals>.need_bindc                    s     j |i |}t|j|  |S rt   )r`  rT   r*  )r-  r   r   r  )rl  rq   rr   _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 %srA  r\  z@_track_unbacked_symbols %s for %s.stride()[%s] at debug_level %s)(rl  r  r   r   r   r   	enumeraterF  r   r
  r#  r  opsr  sym_sizero   r)  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  r*  r  r^  )rw   r-  rs  ru  rv  r  rD  
lazy_proxyattrsrh  r  inner_tr  rq   )rw   rl  rr   rz  	  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 )NFrD  rk   re  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-  )re  rk   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  re  rd  r^  r*  r+  r(  r   r)  r   r
  r"  r#  r  r   iterrW   )rD  rk   re  self_to_be_bounds0parent_proxyexample_valphrv   rq   rr   _lift_symbols_in_symint|	  sx   


zCSubgraphTracer._lift_basic_symbols.<locals>._lift_symbols_in_symintT)re  )F)$r   ro   r   r  r   r   r   r  r   rw  rF  rC   rB   SIZEr{  r|  r}  STRIDEstorage_offsetSTORAGE_OFFSETr  rd  r  r  r  r  r  r  r  r  r  r  r   r  r  r7   )
rw   r-  r  r  r  rD  r  rh  r  r  rq   rv   rr   rd  u	  sv   
D			
z"SubgraphTracer._lift_basic_symbolsrD  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 rt   )r"  )rD  rq   rq   rr   r;  

  s    z7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>)r}   )r*  r  r   r  r^  r   r  r  r   r   rn  rl  sorted)rw   rD  r   to_be_boundr  r  rq   rq   rr   r  	  s$   






z%SubgraphTracer.lookup_unbound_symbols)NFN)NNN)NNNNr  )rl   rm   rn   r   rx   r  r`  rc  rd  r(  rk  r=  r   r  r   r   rn  rz  r  r   r   r   rd  r   r  r  r  r   rq   rq   r   rr   r   6  s4    A /
b(
c
 "r   )r   r  r   r~  r   r   r  r  r  r   r  dataclassesr   typingr   r   r   r   r   r   r	   r
   r   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedrj  torch.nntorch.utils._pytreer  _pytreer  r   torch._dynamo.excr   r   r   r   r   r   torch._utils_internalr   r  r   %torch.fx.experimental._backward_stater   r  r   r   r   torch.fx.passes.runtime_assertr   torch.utils._python_dispatchr   r  r   r   r   r    backends.registryr!   r"   bytecode_transformationr#   r$   r%   r&   r'   r(   r*  r)   current_scope_idr*   r+   r,   r-   r.   r/   graph_deduplicationr0   graph_region_trackerr1   r.  r2   r3   mutation_guardr4   r   r5   r6   rk   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   variables.baserU   variables.builderrV   rW   rX   rY   variables.listsrZ   variables.miscr[   r\   variables.nn_moduler]   variables.tensorr^   r_   r`   ra   variables.torch_functionrb   torch._dynamo.symbolic_convertrc   	getLoggerrl   r   re  getArtifactLoggergraph_tabular_logr  graph_sizes_logrA  ri   rs   	lru_cacher   r   r   r   r   r   r   r   objectr  r   r  r  r   r   r  Tracerr   rq   rq   rq   rr   <module>   s   0<L

#           s	@