o
    h                     @   s4  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 d dlZd dlZd dlZd dlZd dlm  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(m)Z) d dl*m+Z+m,Z, d	d
l-m.Z.mZ/m0Z0 d	dl1m2Z2m3Z3 d	dl4m5Z5m6Z6m7Z7m8Z8 d	dl9m:Z: d	dl;m<Z< d	dl=m>Z>m?Z?m@Z@mAZA d	dlBmCZC d	dlDmEZE d	dlFmGZG d	dlHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQ d	dlmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZa d	dlbmcZc d	dldmeZemfZfmgZgmhZh d	dlimjZj d	dlkmlZlmmZmmnZnmoZo epeqZrejsteqdZuejsteqdZvejsteqdZwejsteqdZxG dd deZyezddd  Z{eG d!d" d"Z|d#d$ Z}G d%d& d&ej~jZG d'd( d(Zeeef ZG d)d* d*e ey ZG d+d, d,ejZdS )-    N)	dataclass)AnyDictList
NamedTupleOptionalOrderedDictSetUnion)fx)CheckpointableGuardGuardsCheckpointStateSourceTracingContext)signpost_event)free_symbolsShapeEnv)WeakIdKeyDictionaryWeakTensorKeyDictionary   )configlogging	variables)
CompiledFn
CompilerFn)create_call_functioncreate_instructionInstruction	unique_id)	PyCodegen)enter_new_scope)BackendCompilerFailed!exceptions_allowed_to_be_fallbackunimplementedunimplemented_with_warning)GuardBuilder)is_dynamic_nn_module)SideEffects)	ConstantSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceParamBufferSourceShapeEnvSourceTensorPropertyTensorPropertySource)checkpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedget_instruction_source_311get_static_address_typegraph_break_reasonsincrement_op_countlazy_format_graph_codelazy_format_graph_tabular
LazyStringnnmodule_doc_url_msgnnmodule_has_hookssame)VariableTracker)GraphArgTrackedFakeVariableBuilderwrap_fx_proxy)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariablegraph
graph_codegraph_sizes
trace_callc                   @   s   e Zd ZU eeef ed< ee ed< e	ed< e
eeejjf  ed< e
eeef  ed< e
eeef  ed< eed< eed< eed	< d
ddd dede
e fddZedd ZdS )OutputGraphStateinput_source_to_vartracked_fakesguard_state
nn_modulesglobal_stateparam_name_to_sourceside_effects	timestamptensor_weakref_to_sizes_strides )prefixotherr[   returnc                C   s   | j D ]F}|dkr| j|j}|d ur|  S q|dkr-| j|j}|d ur,|  S qt| |}t||}||krI| | d| d|   S qd S )NrS   rW   z mismatch: z != )_fieldsrS   diffrW   getattr)selfr\   r[   krsvov rf   P/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_dynamo/output_graph.pyr_   o   s"   


zOutputGraphState.diffc                 C      | j jS N)rS   dynamo_guardsra   rf   rf   rg   guards      zOutputGraphState.guardsN)__name__
__module____qualname__r   r   rB   __annotations__r   rD   r   r   strtorchnnModuleboolr(   intr   r_   propertyrl   rf   rf   rf   rg   rP   d   s   
  rP   c                   C   s
   t tS ri   )torchdynamo_loggingget_step_loggerlogrf   rf   rf   rg   _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 ri   )r   r:   appendrk   rf   rf   rg   __post_init__   s   z GraphCompileReason.__post_init__N)rn   ro   rp   __doc__rr   rq   r   	tracebackFrameSummaryr   rv   r   rf   rf   rf   rg   r~      s   
 r~   c                    s    fdd}|S )Nc                      s   dd  D S )Nc                 S   s    g | ]\}}}||i |qS rf   rf   ).0fnargskwargsrf   rf   rg   
<listcomp>   s     zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>rf   rf   random_callsrf   rg   _gen_rand_values   s   z1_get_gen_rand_values_fn.<locals>._gen_rand_valuesrf   )r   r   rf   r   rg   _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d Z
  ZS )FakeRootModulez'Trick the constructor of fx.GraphModulerT   c                    s,   t    | D ]
\}}t| || q	d S ri   )super__init__itemssetattr)ra   rT   rb   v	__class__rf   rg   r      s   
zFakeRootModule.__init__c                 C   s   dS )NzFakeRootModule(...)rf   rk   rf   rf   rg   __repr__   s   zFakeRootModule.__repr__)rn   ro   rp   r   r   rr   rs   rt   ru   r   r   __classcell__rf   rf   r   rg   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 ri   )r   )ra   r   rf   rf   rg   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)r2   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessr4   rA   RuntimeError	Exceptionr{   	exception)ra   r   r   copy_gmcorrectresultrf   rf   rg   __call__   s(   


	

zWrapperBackend.__call__N)rn   ro   rp   r   r   rs   r   GraphModuler   Tensorr   rf   rf   rf   rg   r      s     r   c                
       s  e Zd ZdZdeeef dedede	d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dd Zdd Zdd Zejdd Zed d! Zed"d# Zed$d% Zed&ee fd'd(Zed&eee j!j"f fd)d*Z#d+d, Z$d-d. Z%d/d0 Z&ed1d2 Z'd&e(fd3d4Z)d5e(fd6d7Z*d8e+fd9d:Z,d;d< Z-d=d> Z.d?d@ Z/dkdBdCZ0dDdE Z1dFe2e j!j"e j3ef fdGdHZ4	JdldKe5e6 fdLdMZ7dNdO Z8dPdQ Z9e j:j;< dRdS Z=ed&e>e?j@ fdTdUZAed&e>e+ fdVdWZBeCdXdYdZe?jDd&eEfd[d\ZFd&e>e j3 fd]d^ZGdmd_d`ZHdae>eI d&dJfdbdcZJdmdddeZKdmdfdgZLdhed&dJfdidjZM  ZNS )n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 | _t	 | _
|	j|	j|	jd| _tjjttjtj| jd| jr;dndd}
t|
| _|   g | _tt| _t | _t  | _!t|| _"g | _#d| _$|| _%|| _&|| _'|| _(ddl)m*} i | _+g | _,g | _-d| _.d | _/i | _0tj12 | _3d| _4| 5  d S )	N)export_root)co_nameco_filenameco_firstlineno)allow_scalar_outputsallow_dynamic_output_shape_ops	co_fieldsTF)	shape_envallow_non_fake_inputsr   )InstructionTranslatorBase)6r   r   SubgraphTracertracersrQ   r   export_constraintsframe_staterY   r   "real_value_tensor_positive_aliasesr   r   r   r   rs   _subclassesFakeTensorModer   r   capture_scalar_outputs capture_dynamic_output_shape_opsr   tracing_contextinit_ambient_guardsrR   collectionsdefaultdictlisttracked_fakes_id_to_sourcedictrV   r(   rW   r   output_instructionsrX   r   r   r   root_txtorch._dynamo.symbolic_convertr   source_to_user_stacks_current_txcleanupsshould_exitrandom_values_varunspec_variable_map_C_is_torch_function_enabledtorch_function_enabled!has_user_defined_allowed_in_graphsave_global_state)ra   r   r   r   r   r   r   r   r   f_code	fake_moder   r   rf   rg   r      sX   

	
zOutputGraph.__init__c                 C   sr   | j t tj | j t tj | j t tj | j t tj	 | j t tj
 d S ri   )rl   addr/   
make_guardr&   	SHAPE_ENVr*   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATErk   rf   rf   rg   r   P  s   zOutputGraph.init_ambient_guardsc                 C   
   | j d S Nr   r   rk   rf   rf   rg   root_tracera  r}   zOutputGraph.root_tracerc                 C   r   Nr   rk   rf   rf   rg   current_tracere  r}   zOutputGraph.current_tracerc                 C   s   t | jdkS )Nr   )lenr   rk   rf   rf   rg   is_root_traceri  s   zOutputGraph.is_root_tracerc                 C   rh   ri   r   rL   rk   rf   rf   rg   rL   m  rm   zOutputGraph.graphc                 C   s   || j _d S ri   r   )ra   valuerf   rf   rg   rL   r  s   c                 C   rh   ri   )r   input_name_to_proxyrk   rf   rf   rg   r   v  rm   zOutputGraph.input_name_to_proxyc                 C   rh   ri   )r   real_value_cacherk   rf   rf   rg   r   z  rm   zOutputGraph.real_value_cachec                 O      | j j|i |S ri   )r   create_proxyra   r   r   rf   rf   rg   r        zOutputGraph.create_proxyc                 O   r   ri   )r   create_noder   rf   rf   rg   r     r   zOutputGraph.create_nodec                 O   r   ri   )r   remove_noder   rf   rf   rg   r     r   zOutputGraph.remove_nodec                 c   sj    t  }z#|  t| | jd}| j| |V  W |d d d  | j  d S |d d d  | j  w )N)parent)r!   	__enter__r   r   r   r   __exit__pop)ra   new_scope_ctxtracerrf   rf   rg   new_subtracer  s   zOutputGraph.new_subtracerc                 C   s   | S ri   rf   rk   rf   rf   rg   output  s   zOutputGraph.outputc                 C   rh   ri   )r   r   rk   rf   rf   rg   r     rm   zOutputGraph.fake_modec                 C   
   | j jjS ri   )r   r   r   rk   rf   rf   rg   r     r}   zOutputGraph.shape_envr]   c                 C   r  ri   )r   guards_contextrj   rk   rf   rf   rg   rl     r}   zOutputGraph.guardsc                 C   r  ri   )r   module_contextrT   rk   rf   rf   rg   rT     r}   zOutputGraph.nn_modulesc                 C   s   | j jj}| j| jf|d< tjt f|d< tjt	 f|d< tj
t f|d< tjt f|d< tjt f|d< tjt f|d< d S )Nr   grad_enabledautocast_enabledautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r   global_contextrU   set_torch_function_stater   rs   set_grad_enabledis_grad_enabledset_autocast_enabledis_autocast_enabledset_autocast_cpu_enabledis_autocast_cpu_enabledset_autocast_gpu_dtypeget_autocast_gpu_dtypeset_autocast_cpu_dtypeget_autocast_cpu_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)ra   rU   rf   rf   rg   r     s(   
zOutputGraph.save_global_statec                 C   s   | j | d S ri   )r   r   )ra   txrf   rf   rg   push_tx  s   zOutputGraph.push_txc                 C   s
   | j  S ri   )r   r   rk   rf   rf   rg   pop_tx  r   zOutputGraph.pop_txc                 C   s   | j s| jS | j d S r   )r   r   rk   rf   rf   rg   
current_tx  s   zOutputGraph.current_txc                 C   s|   | j dusJ | jj }| jj }| jj }tt| jt	| j
|||t| j | j | jt| j	}|  jd7  _|S )z>Create a checkpoint of the current state by copying everythingNr   )rV   r   r  copy_graphstater  r
  rP   r   rQ   r   rR   rW   clonerX   rY   )ra   guards_graph_statemodule_staterU   staterf   rf   rg   r    s"   zOutputGraph.copy_graphstater   c              
   C   s   |\	| _ | _}}}| _| _| _| _| jj| | jj	| | jj
| d}tt| jjD ]#}|jd | jkrSd|jv rC|jd= | | | j|d |d7 }q0td| dS )z6Restore a checkpoint created by self.copy_graphstate()r   creation_timestampexample_valueNr   z$restore_graphstate: removed %s nodes)rQ   rR   rV   rW   rX   rY   r   r  restore_graphstater  r
  reversedr   rL   nodesmetar   r   r   r{   debug)ra   r   guards_stater  rU   removed_nodesnoderf   rf   rg   r#    s0   

zOutputGraph.restore_graphstateargc                    s   j rd S  jd usJ  fdd}t j D ]\}||fdd qt j D ]\}||fdd q/| j dd  d S )Nc                    sh   t | tjrt | jjtjsd S jjt	| jjtjd| j
d}t| j
| dd dd|jjd< d S )NT)beforesourceF)is_unspecializedfake_tensor	is_tensorgrapharg)
isinstancers   SymIntr*  exprsympySymbolr   create_graph_inputrr   r-  rC   r&  )spropproxy)r+  ra   rf   rg   bind_symint  s$   

z4OutputGraph.add_symbol_bindings.<locals>.bind_symintc                       t | tj S ri   )r1   r0   SIZEsrcirf   rg   <lambda>)      z1OutputGraph.add_symbol_bindings.<locals>.<lambda>c                    r<  ri   )r1   r0   STRIDEr>  r@  rf   rg   rB  -  rC  c                 S   s   t | tjS ri   )r1   r0   STORAGE_OFFSETr>  rf   rf   rg   rB  1  s    )r   r/  	enumeratesizestridestorage_offset)ra   r+  r;  r8  rf   )r+  rA  ra   rg   add_symbol_bindings  s    zOutputGraph.add_symbol_bindingsc                 C   s
   t | jS ri   )r5   rL   rk   rf   rf   rg   r5   4  r   zOutputGraph.count_callsc                 C   s   t t| jjdkS r   )r   r   rL   r%  rk   rf   rf   rg   is_empty_graph7  s   zOutputGraph.is_empty_graphc                 C   s@   |sJ | j }|dD ]}t|tr|| }qt||}q|S N.)rT   splitr2  r   r`   )ra   keysobjrb   rf   rf   rg   get_submodule:  s   

zOutputGraph.get_submoduletmpc                 C   sT   t | jd }t D ]}d| d| }||vr'| jd  |f7  < |  S qd S )Nco_varnames____)setr   	itertoolscount)ra   nameexistingrA  varrf   rf   rg   new_varD  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   )ra   rY  rf   rf   rg   update_co_namesL  s   zOutputGraph.update_co_namestargetc                    s  t rtjfi S ttdg d< dv s!J d ttr,J ttj	rgj
 s<jtsKd tj tdkr\d tj fdd}n^ttjjrttjjswJ tddrtjtdt d	 tddd
rtjtdt  d tj fdd}nttjtjfrfdd}nfdd}j D ]\}}|u r||  S qd t!t"| t#$dd  t#$dd  t#$dd   r d % sd    }t&' D ]S} jvrTj < ttjjrN fdd}	t(dr:) D ]	\}
}|	|
 q0t(drN* D ]	\}
}|	|
 qD|   S | d|  q	t+d)Nrl   r-  guardedc                    s>   j d usJ j | < tjd| t i fdi S )Nget_attrr"  )rV   rF   r   r   tuple
module_key)optionsra   r-  r_  r   rf   rg   	wrap_namev  s   
z6OutputGraph.register_attr_or_module.<locals>.wrap_nameT)check_forward_hooksaI  nn.Module forward/_pre hooks are only partially supported, and were detected in your model. In particular, if you do not change/remove hooks after calling .compile(), you can disregard this warning, and otherwise you may need to set torch._dynamo.config.skip_nnmodule_hook_guards=False to ensure recompiling after changing hooks. )check_backward_hookscheck_state_dict_hooksznn.Module state_dict and backward hooks are not yet supported by torch.compile, but were detected in your model and will be silently ignored. c                    s   t t| fi  S ri   )rG   typerc  )re  r_  rf   rg   rf    s   c                    s&   t jd| t i fdi S )Nra  sym_num)rI   creater   rb  rc  )re  ra   r_  rf   rg   rf    s   c                    s*    j |   j| < t t| dS )N)source_name)r   r^  r   rE   r)   rc  )ra   r_  rf   rg   rf    s
   
rU  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   subc                    s4   j d usJ t| }  d|  }|j |< d S rL  )rV   r.   )	leaf_name
new_sourcenew_name)rY  ra   r-  rf   rg   register_leaf_name  s   
z?OutputGraph.register_attr_or_module.<locals>.register_leaf_name_parameters_buffersunreachable),r'   r   UnspecializedNNModuleVariabler   rV  getr2  r.   rs   r   r   r   r   r+   r   r   r&   TENSOR_MATCHr9   DATA_PTR_MATCHrt   ru   r@   _loggingwarning_oncer{   r?   	NN_MODULEr3  SymFloatrT   r   joinmaprr   rero  isalpharW  rX  hasattrnamed_parametersnamed_buffersAssertionError)ra   r_  namesre  rf  rb   r   baserA  rs  rp  rU  rf   )rY  re  ra   r-  r_  r   rg   register_attr_or_moduleQ  s   
z#OutputGraph.register_attr_or_moduleFNr   c                    s  |dusJ ddl m} |_|_td| tdd jD s&td g  t	j
dkr_jD ].}|jd	krB td	|jd
 q0|jdkrV tdtjd d q0 t| q0 fdd}tjD ]}| qk    tj}tj}	g }
t }|rt ||d < j D ]!\}}t|j t!r|j j"|krq||vrt ||< || | q|# D ]}|
$||  |$|gt||   qtj%dkr|  g }&d_'t(d}|t)j%}*|| t+|	}|$|,|d |$t-dd ||.j/j' 0| |ratdd |D ratdd |D ratt1|t|kraj23 ra|  04tt||	tdt|dg  n&d}t+|	|}j25| |6| j27| t+|	|dd |j8 D d}j25| |6| j27| g }t9j:dkst|j;dkr|$4|< |	 t|j;dkr||.| n|td |  0||=   0fddt|
D  dS ) zw
        Generate a subgraph to continue execution on user code.
        Automatically restore live variables.
        Nr   disablezCOMPILING GRAPH due to %sc                 s       | ]}|  V  qd S ri   )can_restore)r   blockrf   rf   rg   	<genexpr>      z/OutputGraph.compile_subgraph.<locals>.<genexpr>z&compile_subgraph with block_depth != 0      	MAKE_CELL)argvalCOPY_FREE_VARSco_freevars)r+  c                      s         d S ri   )add_output_instructionsclearrf   )prefix_instsra   rf   rg   append_prefix_insts  s   
z9OutputGraph.compile_subgraph.<locals>.append_prefix_instsr   r   random_values__gen_rand_valuesTFc                 s   s     | ]}t |ttf V  qd S ri   )r2  rK   rH   )r   r   rf   rf   rg   r  6  s
    
c                 s   s    | ]}t |tV  qd S ri   )r2  rJ   r   xrf   rf   rg   r  :  s    UNPACK_SEQUENCE	graph_outc                 S   s   i | ]\}}|d kr|dqS )r   Nrf   )r   valrX  rf   rf   rg   
<dictcomp>P  s    z0OutputGraph.compile_subgraph.<locals>.<dictcomp>)tempvarsPOP_TOPc                    s   g | ]	}t  |qS rf   )r    create_store)r   r[  )r  rf   rg   r   e  s    z0OutputGraph.compile_subgraph.<locals>.<listcomp>)>
decoratorsr  partial_convertcompile_subgraph_reasonr{   r'  allblock_stackr$   sysversion_infor  opnamer   r   r  r   r   r   r$  exitcleanup_graphprune_dead_localsr   stackr   rT   r   r   symbolic_localsr   r2  r-  r-   
local_namerO  extendr   r\  r   r   r   install_globalr    load_function_namer   r  r   r  rV  rW   is_emptycompile_and_call_fx_graphcodegen_save_tempvarsforeachcodegen_update_mutatedusesr5   rL   graph_outputsgraph_output_varsget_instructions)ra   r  r  r   r  instr  r  stack_valuesrootrestore_varsval_to_namesrb   r   random_calls_instructionsrand_fn_namerand_fncodegengraph_output_varpass1pass2r   rf   )r  ra   r  rg   compile_subgraph  s   












 zOutputGraph.compile_subgraphc                 C   s   t | jj}t }t||dd D ]@\}}|jtjju rSt	|j
| fkrS|jsS|j
d }|jtjju rSt	|j
| fkrS|jsS|j
d }| j| | j| qdS )z
        Remove this pattern from the graph:
            torch._C._set_grad_enabled(False)
            torch._C._set_grad_enabled(True)
        r   Nr   )r   rL   r%  rs   r  zipr_  r   _set_grad_enabledrb  r   _erased
erase_node)ra   r%  r  node1node2rf   rf   rg   r  h  s"   

zOutputGraph.cleanup_graphc           	      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): )rL   r%  r&  rx  r2  rs   r   
FakeTensorrG  rY  rb  rw   r   r3  r*  hint)	ra   rY  graph_sizes_strr*  r"  rG  concrete_size
has_symintszrf   rf   rg   get_graph_sizes_log_str  s,   
z#OutputGraph.get_graph_sizes_log_strc           
   	      s@  ddl m} t|tsJ t|tsJ |D ]	}j|j qddj	t
dd |D fi    tj}td d  |7  < j  t|j}j|_td td	t | td	t | td	t fd
d |}||}td d  d7  <  | t|}	|	  |	  S )zr
        Generate code from self.graph and return the Instruction()s to
        call that generated code.
        r   r  r   c                 s   r  ri   )as_proxyr  rf   rf   rg   r    r  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>statscalls_captured__compiled_fn%sc                      s
     S ri   )r  rf   rY  ra   rf   rg   rB    s   
 z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>unique_graphs)!r  r  r2  r   r   rl   updater   r   
create_argrb  remove_unused_graphargsr5   rL   r6   r   r  r   r   r  r   graph_code_logr'  r<   graph_tabular_logr=   graph_sizes_logr>   call_user_compilerr  r    make_call_generated_coder  )
ra   r  rvr  r  r   ncallsr   compiled_fncgrf   r  rg   r    s<   



z%OutputGraph.compile_and_call_fx_graphc                 C   s0   g }| j jD ]}|jdkr|| q |S |S )Nplaceholder)rL   r%  opr   )ra   rc   r*  rf   rf   rg   placeholders  s   

zOutputGraph.placeholdersc                 C   s   dd | j D S )Nc                 S   s   g | ]}|j d  qS )r1  )r&  )r   r*  rf   rf   rg   r     s    z)OutputGraph.graphargs.<locals>.<listcomp>)r  rk   rf   rf   rg   	graphargs  s   zOutputGraph.graphargsbackend_compile)
phase_namer   c              
   C   s  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rC| jjnd}t tjd|  | j}tjrYt|}|||  }	t tjd	|  t|	srJ d
W nG ty }
 z%| jrt| j|
|
jd d| j  d}t|
| jj| W Y d }
~
nd }
~
w t y }
 zt| j|
|
jd d }
~
ww t!ddi | j"|t#|j jt#|d |	S )Nr   call_functioncall_methodcall_moduler   r  r1  rn   rZ   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.dynamoOutputGraph.call_user_compiler)op_count
node_countinput_count)$rL   r%  r  r   r;   r&  r-  _dynamo_sourcerV   _param_name_to_sourcer   _source_to_user_stacksr  r   rn   r|   r   INFOr   r   r   r   callabler#   r   r"   with_traceback__traceback__r   format_frame_summaryr%   r   r   r   r   r   )ra   r   totr  r*  plr+  rY  r   r  emsgrf   rf   rg   r    sv   







r  c                 C   s    g }| j D ]}||j q|S ri   )r  r   example)ra   r   r+  rf   rf   rg   r     s   
zOutputGraph.example_inputsc           
         s   t t jjD ]&}tt|jdkr.|jdkr | q|jdkr.|jt	j
u r. | qdd } fdd}t }g } jD ]1}||d u}|rT|jsS|| qA|js\|| qA|jd }|jd uri|jn|j}|t|O }qA|D ]}||}	|	d ur|	|vr|| qu||	 qud S )	Nr   ra  r  c                 S   s8   | j d }|j}t|tjrt|jjtjr|jjS d S Nr1  )	r&  r
  r2  rs   r3  r*  r4  r5  r6  )r*  r+  r
  rf   rf   rg   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 %sr1  )r{   r'  r&  r-  rY  r   r   r   )r*  rk   rf   rg   remove_unused)  s   
z:OutputGraph.remove_unused_graphargs.<locals>.remove_unusedr1  )r$  r   rL   r%  r   usersr  r   r_  operatorgetitemrV  r  r   r&  r/  r
  r   remove)
ra   r*  r  r  used_symbolsrecheck_placeholdersbinds_symbolr+  fakesymbolrf   rk   rg   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   r  r   )ra   r[   rf   rf   rg   r  N  s   
z#OutputGraph.add_output_instructionsc                 C   s   | j t| j|| d S ri   )r   r   r3   rm  r   )ra   rY  r   rf   rf   rg   r  V  s   zOutputGraph.install_globalc                 C   sX   d | _ | j  d | _| jjD ]}d|jv r|jd= q| j  | j  | j	  d S r  )
r   rT   r  rV   rL   r%  r&  r   r   rW   )ra   r*  rf   rf   rg   cleanupY  s   



zOutputGraph.cleanupenabledc                 C   r   ri   )r   )ra   r  rf   rf   rg   r  h  r   z$OutputGraph.set_torch_function_state)rR  )FN)r]   N)Orn   ro   rp   r   r   rr   r   r   rv   Scoper   r   rx   r   r   r   rL   setterr   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r	   r   rl   rs   rt   ru   rT   r   r  r  r  rP   r  r#  rC   rJ  r5   rK  rQ  r\  r^  r
   r   r  r   r~   r  r  r  _guardsr   clear_framer  r   r   Noder  r  r7   r   r   r  r   r  r   r  r  r  r  r   rf   rf   r   rg   r      s    

	p





	




.


 
 

-	;
7

r   c                       sh   e Zd ZdZd f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
  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                    sb   t    t|| _tj | _|r|d u sJ || _	t
 | _i | _|| _t
 | _d | _d S ri   )r   r   weakrefr:  output_graphrs   r   GraphrL   r   r   r   r   r   r   lifted_freevars	prev_inst)ra   r!  r   r   r   rf   rg   r   t  s   



zSubgraphTracer.__init__c              	      s  | j d ur't||f\}}	g }
|D ]}| |}|
| qt|
|	\}}t |||||||| jj	}t
jdkrm|dv rm|j  | jurm jjd urm|j|j jjd fdd}tdt|  | _|j}|rz| jjd< |dv rjj|fjjd	< n|d
kr| j d urtd jjjjd | d fjjd	< g }|r||  t|dd }|s|  tj |! }d"|j_#S )Nr  r  )linenoc                     s(   t   } djj d d|  S )NzTRACE FX call z from r  )r8   rstripr*  rY  )linecur_instheaderr  tx_coderf   rg   get_trace_call_log_str  s   z;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strr  nn_module_stack>   r  r  	source_fnr  z0Invoking an nn.Module inside HigherOrderOperatorr   r   rZ   )$r   pytreetree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r   r!  r  r  r  current_instructionr$  	positionsr%  r   get_line_of_code_headertrace_call_logr'  r>   r-  r   r*  r&  rY  r$   frame_summaryr`   reverser   StackSummary	from_listformatr  stack_trace)ra   kindr_  r   r   rY  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsr+  maybe_new_argr  r,  r-  frame_summariesmsgsr   r(  rg   r     sN   
+

zSubgraphTracer.create_proxyc                    s~   | j d ur+t||f\}}|D ]}	t|	tjjsqd|	jv r q|	j| jks*J dqt	 
||||||}
| jj|
jd< |
S )Nsaved_tensor_markedz2create_node using arg not from this SubgraphTracerr!  )r   r/  r0  r2  rs   r   r  r&  rL   r   r   r!  rX   )ra   r  r_  r   r   rY  r>  r@  rU  r+  r*  r   rf   rg   r      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 r   )r   r  rO  rL   r  r$  r   r%  r  r   r   rY  )ra   r*  user_graph_nodesuserother_graph_noderf   rf   rg   r     s   zSubgraphTracer.remove_nodec                 C   sB  |d u r| j d usJ d| jr#t|dds#| jj|g t  || j	v r?t
 D ]}| d| }|| j	vr>|} nq,| j	r_tt| j	}| j	| j}|rX| j|}	n| j|}	n| jd }	|	0 | jd|di |d}
| j	r|r| j	 \}}|
| j	|< || j	|< n|
| j	|< |
W  d    S 1 sw   Y  d S )NzByou are required to provide a source for inputs on the root tracerF)allow_cell_or_freevarrU  r  rf   )r>  )r   r   r,   r!  r   
setdefaultr   r   extract_stackr   rW  rX  nextr$  r*  rL   inserting_beforeinserting_afterr   popitem)ra   rY  r>  r,  r-  rA  candidate_name	prev_namer*  ctxr:  rb   r   rf   rf   rg   r7  +  s@   




$z!SubgraphTracer.create_graph_inputc                 C   sx   | j d us	J d|| jv r| j| S | |jj}|jjd |jjd< || j|< | j d ur:|j| j kr:| j | |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr"  )r   r#  r7  r*  rY  r&  r   lift_tracked_freevar_to_input)ra   r:  	new_proxyrf   rf   rg   rT  [  s   


z,SubgraphTracer.lift_tracked_freevar_to_inputc                 C   s:   t |tjjs	|S |j| kr|S d|jjv 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.
        rF  )r2  rs   r   Proxyr   r*  r&  rT  )ra   r+  rf   rf   rg   r1  m  s   

z2SubgraphTracer.maybe_lift_tracked_freevar_to_input)NF)NNN)NNNN)NFN)rn   ro   rp   r   r   r   r   r   r7  rT  r1  r   rf   rf   r   rg   r   l  s    )j
0r   )r   r  r   	functoolsrW  r   r  r  r  r   r   dataclassesr   typingr   r   r   r   r   r   r	   r
   r5  torch._guardsrs   torch._loggingtorch.nntorch.utils._pytreeutils_pytreer/  r   r   r   r   r   r   torch._utils_internalr   %torch.fx.experimental.symbolic_shapesr   r   torch.utils.weakr   r   rZ   r   ry   r   backends.registryr   r   bytecode_transformationr   r   r   r   r  r    current_scope_idr!   excr"   r#   r$   r%   rl   r&   mutation_guardr'   rW   r(   r-  r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   variables.baserB   variables.builderrC   rD   rE   rF   variables.nn_modulerG   variables.tensorrH   rI   rJ   rK   	getLoggerrn   r{   r{  getArtifactLoggerr  r  r  r6  rP   	lru_cacher|   r~   r   rt   ru   r   r   rr   objectr  r   Tracerr   rf   rf   rf   rg   <module>   sz    (,H
$
#       