U
    9%e                     @   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graphZ
graph_codeZgraph_sizesZ
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 ee
e d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)otherrX   returnc                C   s   | j D ]}|dkr4| j|j}|d k	r|  S qn(|dkr\| j|j}|d k	r|  S qt| |}t||}||kr| | d| d|   S qd S )NrP   rT   z mismatch: z != )_fieldsrP   diffrT   getattr)selfrY   rX   krsvov rc   Y/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_dynamo/output_graph.pyr\   o   s     


zOutputGraphState.diffc                 C   s   | j jS N)rP   dynamo_guardsr^   rc   rc   rd   guards   s    zOutputGraphState.guardsN)__name__
__module____qualname__r   r   rB   __annotations__r   rD   r   r   strtorchnnModuleboolr(   intr   r\   propertyrh   rc   rc   rc   rd   rM   d   s   
rM   c                   C   s
   t tS re   )torchdynamo_loggingZget_step_loggerlogrc   rc   rc   rd   _step_logger   s    rv   c                   @   s<   e Zd ZU dZeed< eej ed< dZ	e
ed< dd ZdS )	GraphCompileReasonzOStores why a given output graph was compiled; i.e. what caused the graph break.reasonZ
user_stackTgraph_breakc                 C   s   | j rt|  d S re   )ry   r:   appendrg   rc   rc   rd   __post_init__   s    z GraphCompileReason.__post_init__N)ri   rj   rk   __doc__rm   rl   r   	tracebackFrameSummaryry   rq   r{   rc   rc   rc   rd   rw      s
   
rw   c                    s    fdd}|S )Nc                      s   dd  D S )Nc                 S   s   g | ]\}}}|||qS rc   rc   ).0fnargskwargsrc   rc   rd   
<listcomp>   s     zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>rc   rc   random_callsrc   rd   _gen_rand_values   s    z1_get_gen_rand_values_fn.<locals>._gen_rand_valuesrc   )r   r   rc   r   rd   _get_gen_rand_values_fn   s    r   c                       s:   e Zd ZdZeeejjf d fddZ	dd Z
  ZS )FakeRootModulez'Trick the constructor of fx.GraphModule)rQ   c                    s,   t    | D ]\}}t| || qd S re   )super__init__itemssetattr)r^   rQ   r_   v	__class__rc   rd   r      s    
zFakeRootModule.__init__c                 C   s   dS )NzFakeRootModule(...)rc   rg   rc   rc   rd   __repr__   s    zFakeRootModule.__repr__)ri   rj   rk   r|   r   rm   rn   ro   rp   r   r   __classcell__rc   rc   r   rd   r      s   r   c                   @   s4   e Zd ZedddZejjeej	 dddZ
dS )WrapperBackendbackendc                 C   s
   || _ d S re   r   )r^   r   rc   rc   rd   r      s    zWrapperBackend.__init__)gmexample_inputsc                 C   s   t || _|| _t| j}| ||| _| jd ksB| j| jjkrJ| jjS tj	sV| jS zvzP| jjt
| }| jt
| }t||r| jW W BS td|  | jjW W &S  tk
r   td  Y nX W 5 |   X d S )Nzincorrect results of backend zerror in verify_correctness)r2   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessr4   rA   RuntimeError	Exceptionru   	exception)r^   r   r   Zcopy_gmZcorrectresultrc   rc   rd   __call__   s&    


zWrapperBackend.__call__N)ri   rj   rk   r   r   rn   r   GraphModuler   Tensorr   rc   rc   rc   rd   r      s   r   c                       s  e Zd ZdZeeef eee	e	d 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ee d"d#d$Zeeee j!j"f d"d%d&Z#d'd( Z$d)d* Z%d+d, Z&ed-d. Z'e(d"d/d0Z)e(d1d2d3Z*e+d4d5d6Z,d7d8 Z-d9d: Z.d;d< Z/dgd>d?Z0d@dA Z1e2e j!j"e j3ef dBdCdDZ4dhe5e6 dGdHdIZ7dJdK Z8dLdM Z9e j:j;< dNdO Z=ee>e?j@ d"dPdQZAee>e+ d"dRdSZBeCdTdUe?jDeEdVdWdXZFe>e j3 d"dYdZZGdFd"d[d\ZHe>eI dFd]d^d_ZJdFd"d`daZKdFd"dbdcZLedFdddedfZM  ZNS )i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rvd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)Zallow_scalar_outputsZallow_dynamic_output_shape_ops	co_fieldsTF)	shape_envZallow_non_fake_inputsr   )InstructionTranslatorBase)6r   r   SubgraphTracertracersrN   r   export_constraintsframe_staterV   r   Z"real_value_tensor_positive_aliasesr   r   r   r   rn   _subclassesZFakeTensorModer   r   Zcapture_scalar_outputsZ capture_dynamic_output_shape_opsr   tracing_contextinit_ambient_guardsrO   collectionsdefaultdictlistZtracked_fakes_id_to_sourcedictrS   r(   rT   r   output_instructionsrU   r   r   r   root_txZtorch._dynamo.symbolic_convertr   source_to_user_stacks_current_txcleanupsshould_exitrandom_values_varZunspec_variable_map_CZ_is_torch_function_enabledtorch_function_enabled!has_user_defined_allowed_in_graphsave_global_state)r^   r   r   r   r   r   r   r   r   f_code	fake_moder   r   rc   rd   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 re   )rh   addr/   
make_guardr&   Z	SHAPE_ENVr*   ZDETERMINISTIC_ALGORITHMSZ	GRAD_MODEZDEFAULT_DEVICEZTORCH_FUNCTION_STATErg   rc   rc   rd   r   P  s    zOutputGraph.init_ambient_guardsc                 C   s
   | j d S Nr   r   rg   rc   rc   rd   root_tracera  s    zOutputGraph.root_tracerc                 C   s
   | j d S Nr   rg   rc   rc   rd   current_tracere  s    zOutputGraph.current_tracerc                 C   s   t | jdkS )Nr   )lenr   rg   rc   rc   rd   is_root_traceri  s    zOutputGraph.is_root_tracerc                 C   s   | j jS re   r   rL   rg   rc   rc   rd   rL   m  s    zOutputGraph.graphc                 C   s   || j _d S re   r   )r^   valuerc   rc   rd   rL   r  s    c                 C   s   | j jS re   )r   input_name_to_proxyrg   rc   rc   rd   r   v  s    zOutputGraph.input_name_to_proxyc                 C   s   | j jS re   )r   real_value_cacherg   rc   rc   rd   r   z  s    zOutputGraph.real_value_cachec                 O   s   | j j||S re   )r   create_proxyr^   r   r   rc   rc   rd   r     s    zOutputGraph.create_proxyc                 O   s   | j j||S re   )r   create_noder   rc   rc   rd   r     s    zOutputGraph.create_nodec                 O   s   | j j||S re   )r   remove_noder   rc   rc   rd   r     s    zOutputGraph.remove_nodec                 c   sR   t  }z,|  t| | jd}| j| |V  W 5 |d d d  | j  X d S )N)parent)r!   __exit__r   pop	__enter__r   r   rz   )r^   Znew_scope_ctxtracerrc   rc   rd   new_subtracer  s    
zOutputGraph.new_subtracerc                 C   s   | S re   rc   rg   rc   rc   rd   output  s    zOutputGraph.outputc                 C   s   | j jS re   )r   r   rg   rc   rc   rd   r     s    zOutputGraph.fake_modec                 C   s
   | j jjS re   )r   r   r   rg   rc   rc   rd   r     s    zOutputGraph.shape_env)rZ   c                 C   s
   | j jjS re   )r   guards_contextrf   rg   rc   rc   rd   rh     s    zOutputGraph.guardsc                 C   s
   | j jjS re   )r   module_contextrQ   rg   rc   rc   rd   rQ     s    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_enabledZautocast_enabledZautocast_cpu_enabledZautocast_gpu_dtypeZautocast_cpu_dtypeZautocast_cache_enabled)r   global_contextrR   set_torch_function_stater   rn   Zset_grad_enabledis_grad_enabledZset_autocast_enabledZis_autocast_enabledZset_autocast_cpu_enabledZis_autocast_cpu_enabledZset_autocast_gpu_dtypeZget_autocast_gpu_dtypeZset_autocast_cpu_dtypeZget_autocast_cpu_dtypeZset_autocast_cache_enabledZis_autocast_cache_enabled)r^   rR   rc   rc   rd   r     s(    
zOutputGraph.save_global_statec                 C   s   | j | d S re   )r   rz   )r^   txrc   rc   rd   push_tx  s    zOutputGraph.push_txc                 C   s
   | j  S re   )r   r   rg   rc   rc   rd   pop_tx  s    zOutputGraph.pop_txc                 C   s   | j s| jS | j d S r   )r   r   rg   rc   rc   rd   
current_tx  s    zOutputGraph.current_txc                 C   s|   | j dk	st| 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   )rS   AssertionErrorr   r   copy_graphstater   r   rM   r   rN   r   rO   rT   clonerU   rV   )r^   Zguards_graph_statemodule_staterR   staterc   rc   rd   r     s"    zOutputGraph.copy_graphstate)r   c              
   C   s   |\	| _ | _}}}| _| _| _| _| jj| | jj	| | jj
| d}tt| jjD ]F}|jd | jkr`d|jkr|jd= | | | j|d |d7 }q`td| dS )z6Restore a checkpoint created by self.copy_graphstate()r   creation_timestampexample_valueNr   z$restore_graphstate: removed %s nodes)rN   rO   rS   rT   rU   rV   r   r   restore_graphstater   r   reversedr   rL   nodesmetar   r   r   ru   debug)r^   r   Zguards_stater   rR   Zremoved_nodesnoderc   rc   rd   r     s.    


zOutputGraph.restore_graphstateargc                    s   j r
d S  jd k	st fdd}t j D ]\}||fdd q4t 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)Zis_unspecializedfake_tensorZ	is_tensorgrapharg)
isinstancern   SymIntr   exprsympySymbolr   create_graph_inputrm   r   rC   r   )spropproxy)r   r^   rc   rd   bind_symint  s$    

z4OutputGraph.add_symbol_bindings.<locals>.bind_symintc                    s   t | tj S re   )r1   r0   ZSIZEsrcirc   rd   <lambda>)      z1OutputGraph.add_symbol_bindings.<locals>.<lambda>c                    s   t | tj S re   )r1   r0   ZSTRIDEr	  r  rc   rd   r  -  r  c                 S   s   t | tjS re   )r1   r0   ZSTORAGE_OFFSETr	  rc   rc   rd   r  1  r  )r   r   r   	enumeratesizeZstrideZstorage_offset)r^   r   r  r  rc   )r   r  r^   rd   add_symbol_bindings  s$     
 
zOutputGraph.add_symbol_bindingsc                 C   s
   t | jS re   )r5   rL   rg   rc   rc   rd   r5   4  s    zOutputGraph.count_callsc                 C   s   t t| jjdkS r   )r   r   rL   r   rg   rc   rc   rd   is_empty_graph7  s    zOutputGraph.is_empty_graphc                 C   s@   |st | j}|dD ]"}t|tr0|| }qt||}q|S N.)r   rQ   splitr   r   r]   )r^   keysobjr_   rc   rc   rd   get_submodule:  s    

zOutputGraph.get_submoduletmpc                 C   sT   t | jd }t D ]8}d| d| }||kr| jd  |f7  < |  S qd S )Nco_varnamesZ____)setr   	itertoolscount)r^   nameexistingr  varrc   rc   rd   new_varD  s    zOutputGraph.new_varc                 C   s&   || j d kr"| j d  |f7  < dS )z/Ensure self.code_options.co_names contains nameco_namesN)r   )r^   r  rc   rc   rd   update_co_namesL  s    zOutputGraph.update_co_names)targetc                    s  t rtjfS ttdg d< dks>td ttrTttt	j
rʈj stjtsd tj tdkrd tj fdd}ntt	jjrZtt	jjsttddrt	jtdt d	 tddd
r4t	jtdt  d tj fdd}n4tt	jt	jfrfdd}nfdd}j  D ] \}}|kr||  S qd!t"t#| t$%dd  t$%dd  t$%dd   r d & sd    }t'( D ]} jkrj < tt	jjr fdd}	t)dr~* D ]\}
}|	|
 qjt)dr+ D ]\}
}|	|
 q|   S | d|  qtdd S )Nrh   r   Zguardedc                    s>   j d k	stj | < tjd| t i fdi S )Nget_attrr   )rS   r   rF   r   r   tupleZ
module_key)optionsr^   r   r%  r   rc   rd   	wrap_namev  s    
z6OutputGraph.register_attr_or_module.<locals>.wrap_nameT)Z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. )Zcheck_backward_hooksZ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 S re   )rG   typer(  )r)  r%  rc   rd   r*    s    c                    s&   t jd| t i fdi S )Nr&  Zsym_num)rI   creater   r'  r(  )r)  r^   r%  rc   rd   r*    s    c                    s*    j |   j| < t t| dS )N)Zsource_name)r   r$  r   rE   r)   r(  )r^   r%  rc   rd   r*    s
    
r  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   subc                    s4   j d k	stt| }  d|  }|j |< d S r  )rS   r   r.   )	leaf_nameZ
new_sourcenew_name)r  r^   r   rc   rd   register_leaf_name  s    
z?OutputGraph.register_attr_or_module.<locals>.register_leaf_name_parameters_buffersZunreachable),r'   r   ZUnspecializedNNModuleVariabler   r  getr   r   r.   rn   r   r   r   r   r+   r   r   r&   ZTENSOR_MATCHr9   ZDATA_PTR_MATCHro   rp   r@   _loggingZwarning_onceru   r?   Z	NN_MODULEr   ZSymFloatrQ   r   joinmaprm   rer.  isalphar  r  hasattrZnamed_parametersZnamed_buffers)r^   r%  namesr)  r*  r_   r   baser  r1  r/  r  rc   )r  r)  r^   r   r%  r   rd   register_attr_or_moduleQ  sz    
  

z#OutputGraph.register_attr_or_moduleFN)rx   c                    s  |dk	st ddlm} |_|_td| tdd jD sLt	d g  t
jdkrjD ]\}|jd	kr td	|jd
 q`|jdkr tdtjd d q` t| q` fdd}tjD ]}| qֈ    tj}tj}	g }
t }|r*t ||d < j D ]L\}}t |j!t"r\|j!j#|kr\q4||krpt ||< || | q4|$ D ],}|
%||  |%|gt||   qtj&dkrJ|  g }'d_(t)d}|t*j&}+|| t,|	}|%|-|d |%t.dd ||/j0j( 1| |rtdd |D rtdd |D rtt2|t|krЈj34 r|  15tt||	tdt|dg  n'd}t,|	|}j36| |7| j38| t,|	|dd |j9 D d}j36| |7| j38| g }t:j;dksnt|j<dkr|%5|= |	 t|j<dkr||/| n|td |  1||>   1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   s   | ]}|  V  qd S re   )Zcan_restore)r   blockrc   rc   rd   	<genexpr>  s     z/OutputGraph.compile_subgraph.<locals>.<genexpr>z&compile_subgraph with block_depth != 0      Z	MAKE_CELL)argvalZCOPY_FREE_VARSco_freevarsr   c                      s         d S re   )add_output_instructionsclearrc   )prefix_instsr^   rc   rd   append_prefix_insts  s    
z9OutputGraph.compile_subgraph.<locals>.append_prefix_instsr   r   Zrandom_valuesZ__gen_rand_valuesTFc                 s   s   | ]}t |ttf V  qd S re   )r   rK   rH   )r   r   rc   rc   rd   rA  6  s   c                 s   s   | ]}t |tV  qd S re   )r   rJ   r   xrc   rc   rd   rA  :  s     UNPACK_SEQUENCEZ	graph_outc                 S   s   i | ]\}}|d kr|dqS )r   Nrc   )r   valr  rc   rc   rd   
<dictcomp>P  s       z0OutputGraph.compile_subgraph.<locals>.<dictcomp>)ZtempvarsPOP_TOPc                    s   g | ]}t  |qS rc   )r    create_store)r   r!  )r   rc   rd   r   e  s     z0OutputGraph.compile_subgraph.<locals>.<listcomp>)?r   
decoratorsr?  partial_convertcompile_subgraph_reasonru   r   allZblock_stackr$   sysversion_inforI  opnamerz   r   rE  r   r   r   r   exitcleanup_graphZprune_dead_localsr   stackr   rQ   r   r   Zsymbolic_localsr   r   r   r-   Z
local_namer  extendr   r"  r   r   r   install_globalr    Zload_function_namer   rQ  r   rG  r  rT   Zis_emptycompile_and_call_fx_graphZcodegen_save_tempvarsZforeachZcodegen_update_mutatedZusesr5   rL   Zgraph_outputsZgraph_output_varsget_instructions)r^   r   rS  rx   r?  instrJ  r@  Zstack_valuesrootZrestore_varsZval_to_namesr_   r   Zrandom_calls_instructionsZrand_fn_nameZrand_fncodegenZgraph_output_varZpass1Zpass2r   rc   )rI  r^   r   rd   compile_subgraph  s    



 










 zOutputGraph.compile_subgraphc                 C   s   t | jj}t }t||dd D ]\}}|jtjjkr&t	|j
| fkr&|js&|j
d }|jtjjkr&t	|j
| fkr&|js&|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   rn   r   zipr%  r   Z_set_grad_enabledr'  r   Z_erased
erase_node)r^   r   r   Znode1Znode2rc   rc   rd   rZ  h  s&    

zOutputGraph.cleanup_graphc           	      C   s   d}|d| d7 }| j jD ]}|jdd }t|tjjr| }||j	 dt
| d7 }g }d}|D ]>}t|tr|| qjt|tjrd}||jj qj qqj|r||j	 d	t
| d7 }q|S )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
r   z: 
FTz (concrete): )rL   r   r   r4  r   rn   r   Z
FakeTensorr  r  r'  rr   rz   r   r   hint)	r^   r  Zgraph_sizes_strr   r   r  Zconcrete_sizeZ
has_symintszrc   rc   rd   get_graph_sizes_log_str  s*    
z#OutputGraph.get_graph_sizes_log_strc           
   	      s@  ddl m} t|tstt|ts(t|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   s   | ]}|  V  qd S re   )Zas_proxyrK  rc   rc   rd   rA    s     z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>statsZcalls_capturedZ__compiled_fn%sc                      s
     S re   )ri  rc   r  r^   rc   rd   r    r  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>Zunique_graphs)"rR  r?  r   r   r   r   rh   updater   r   Z
create_argr'  remove_unused_graphargsr5   rL   r6   r   rH  r   r   rT  r   graph_code_logr   r<   graph_tabular_logr=   graph_sizes_logr>   call_user_compilerr]  r    Zmake_call_generated_coder_  )
r^   r   rvra  r?  r   Zncallsr   compiled_fnZcgrc   rl  rd   r^    s>    

 

z%OutputGraph.compile_and_call_fx_graphc                 C   s0   g }| j jD ]}|jdkr&|| q q,q|S )Nplaceholder)rL   r   oprz   )r^   r`   r   rc   rc   rd   placeholders  s    

zOutputGraph.placeholdersc                 C   s   dd | j D S )Nc                 S   s   g | ]}|j d  qS )r   )r   )r   r   rc   rc   rd   r     s     z)OutputGraph.graphargs.<locals>.<listcomp>)rw  rg   rc   rc   rd   	graphargs  s    zOutputGraph.graphargsZbackend_compile)Z
phase_name)r   rZ   c              
   C   s  d}g }|j jD ]*}|jdkr&|d7 }|jdkr|| qt| |D ]}|jd }|j|_qH| j|_	| j
|_zvt| jdr| jjnd}t tjd|  | j}tjrt|}|||  }	t tjd	|  t|	std
W n tk
rJ }
 zD| jrt| j|
|
jd d| j  d}t|
| jj | W 5 d }
~
X Y n: t!k
r }
 zt| j|
|
jd W 5 d }
~
X Y nX t"dd| j#|t$|j jt$|d |	S )Nr   call_functioncall_methodcall_moduler   ru  r   ri   rW   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.ZdynamoOutputGraph.call_user_compiler)Zop_countZ
node_countZinput_count)%rL   r   rv  rz   r;   r   r   Z_dynamo_sourcerS   Z_param_name_to_sourcer   Z_source_to_user_stacksr:  r   ri   rv   r   INFOr   r   r   r   callabler   r#   r   r"   with_traceback__traceback__r   Zformat_frame_summaryr%   r   r   r   r   r   )r^   r   Ztotrw  r   plr   r  r   rt  emsgrc   rc   rd   rr    sf    





 
r}  c                 C   s    g }| j D ]}||j q
|S re   )rx  rz   example)r^   r   r   rc   rc   rd   r     s    
zOutputGraph.example_inputsc           
         s"  t t jjD ]L}tt|jdkr|jdkr< | q|jdkr|jt	j
kr | qdd } fdd}t }g } jD ]b}||d k	}|r|js|| q|js|| q|jd }|jd k	r|jn|j}|t|O }q|D ]2}||}	|	d k	r|	|kr|| q||	 qd S )	Nr   r&  rz  c                 S   s8   | j d }|j}t|tjr4t|jjtjr4|jjS d S Nr   )	r   r  r   rn   r   r   r  r  r  )r   r   r  rc   rc   rd   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   )ru   r   r   r   r  r   r   r   )r   rg   rc   rd   remove_unused)  s    
z:OutputGraph.remove_unused_graphargs.<locals>.remove_unusedr   )r   r   rL   r   r   usersrv  r   r%  operatorgetitemr  rw  rz   r   r   r  r   remove)
r^   r   r  r  Zused_symbolsZrecheck_placeholdersZbinds_symbolr   Zfakesymbolrc   rg   rd   rn    s6    
	




z#OutputGraph.remove_unused_graphargs)rX   rZ   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   )r^   rX   rc   rc   rd   rG  N  s    z#OutputGraph.add_output_instructionsc                 C   s   | j t| j|| d S re   )r   rz   r3   r-  r   )r^   r  r   rc   rc   rd   r]  V  s    zOutputGraph.install_globalc                 C   sX   d | _ | j  d | _| jjD ]}d|jkr|jd= q| j  | j  | j	  d S r  )
r   rQ   rH  rS   rL   r   r   r   r   rT   )r^   r   rc   rc   rd   cleanupY  s    




zOutputGraph.cleanup)enabledrZ   c                 C   s
   || _ d S re   )r   )r^   r  rc   rc   rd   r   h  s    z$OutputGraph.set_torch_function_state)r  )FN)Ori   rj   rk   r|   r   rm   r   r   rq   Scoper   r   rs   r   r   r   rL   setterr   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r	   r   rh   rn   ro   rp   rQ   r   r   r   r   rM   r   r   rC   r  r5   r  r  r"  r$  r
   r   r=  r   rw   rc  rZ  ri  Z_guardsr   Zclear_framer^  r   r   Noderw  rx  r7   r   r   rr  r   rn  r   rG  r]  r  r   r   rc   rc   r   rd   r      s   
p





	




.

     

-	;7r   c                       s`   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 | _|r2|d ks2t	|| _
t | _i | _|| _t | _d | _d S re   )r   r   weakrefr  output_graphrn   r   ZGraphrL   r   r   r   r   r   r   r   lifted_freevars	prev_inst)r^   r  r   r   r   rc   rd   r   t  s    


zSubgraphTracer.__init__c              	      s  | j d k	rNt||f\}}	g }
|D ]}| |}|
| q$t|
|	\}}t |||||||| jj	}t
jdkr|dkr|j  | jk	rڈ jjd k	r|j|j jjd fdd}tdt|  | _|j}|r| jjd< |dkrjj|fjjd	< nB|d
krV| j d k	r2td jjjjd | d fjjd	< g }|r~||  t|dd }qZ|  tj |! }d"|j_#S )NrB  ry  )linenoc                     s(   t   } djj d d|  S )NzTRACE FX call z from rf  )r8   rstripr   r  )lineZcur_instheaderrs  Ztx_coderc   rd   get_trace_call_log_str  s    z;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strrk  nn_module_stack>   rz  r{  Z	source_fnr|  z0Invoking an nn.Module inside HigherOrderOperatorr   r   rW   )$r   pytreetree_flatten#maybe_lift_tracked_freevar_to_inputrz   Ztree_unflattenr   r   r  r   rV  rW  Zcurrent_instructionr  Z	positionsr  r   Zget_line_of_code_headertrace_call_logr   r>   r  r   r   r   r  r$   frame_summaryr]   reverser}   StackSummary	from_listformatr6  Zstack_trace)r^   kindr%  r   r   r  	type_exprZproxy_factory_fn	flat_argsZ	tree_specZnew_flat_argsr   Zmaybe_new_argr   r  r  Zframe_summariesZmsgsr   r  rd   r     sX    +

      

zSubgraphTracer.create_proxyc                    s~   | j d k	rVt||f\}}|D ]4}	t|	tjjs4q d|	jkr@q |	j| jks t	dq t
 ||||||}
| jj|
jd< |
S )Nsaved_tensor_markedz2create_node using arg not from this SubgraphTracerr   )r   r  r  r   rn   r   r  r   rL   r   r   r   r  rU   )r^   rv  r%  r   r   r  r  r  r  r   r   r   rc   rd   r      s    


zSubgraphTracer.create_nodec                 C   sz   t |jdkrZg }|j D ]&}|j| jkr|tt|jj q|D ]}|j| qH| j| | j	
|jd  d S r   )r   r  r  rL   r\  r   r   r   re  r   r   r  )r^   r   Zuser_graph_nodesuserZother_graph_noderc   rc   rd   r     s    zSubgraphTracer.remove_nodec              
   C   s:  |d kr| j d k	std| jrFt|ddsF| jj|g t	  || j
kr~t D ]$}| d| }|| j
krX|} q~qX| j
rtt| j
}| j
| j}|r| j|}	q| j|}	n| jd }	|	b | jd|di |d}
| j
r|r| j
 \}}|
| j
|< || j
|< n
|
| j
|< |
W  5 Q R  S Q R X d S )NzByou are required to provide a source for inputs on the root tracerF)Zallow_cell_or_freevarr  ru  rc   )r  )r   r   r   r,   r  r   
setdefaultrz   r   extract_stackr   r  r  nextr   r   rL   Zinserting_beforeZinserting_afterr   popitem)r^   r  r  r   r   r  Zcandidate_nameZ	prev_namer   ctxr  r_   r   rc   rc   rd   r  +  s>    



z!SubgraphTracer.create_graph_inputc                 C   sx   | j d k	std|| jkr&| j| S | |jj}|jjd |jjd< || j|< | j d k	rt|j| j krt| j | |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr   )	r   r   r  r  r   r  r   r   lift_tracked_freevar_to_input)r^   r  Z	new_proxyrc   rc   rd   r  [  s    


z,SubgraphTracer.lift_tracked_freevar_to_inputc                 C   s:   t |tjjs|S |j| kr |S d|jjkr0|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.
        r  )r   rn   r   ZProxyr   r   r   r  )r^   r   rc   rc   rd   r  m  s    
z2SubgraphTracer.maybe_lift_tracked_freevar_to_input)NF)NNN)NNNN)NFN)ri   rj   rk   r|   r   r   r   r   r  r  r  r   rc   rc   r   rd   r   l  s   )   j       
0r   )r   r  r   	functoolsr  r   r  r8  rV  r}   r  dataclassesr   typingr   r   r   r   r   r   r	   r
   r  Ztorch._guardsrn   Ztorch._loggingZtorch.nnZtorch.utils._pytreeutilsZ_pytreer  r   r   r   r   r   r   Ztorch._utils_internalr   Z%torch.fx.experimental.symbolic_shapesr   r   Ztorch.utils.weakr   r   rW   r   rt   r   Zbackends.registryr   r   Zbytecode_transformationr   r   r   r   rb  r    Zcurrent_scope_idr!   excr"   r#   r$   r%   rh   r&   Zmutation_guardr'   rT   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   Zvariables.baserB   Zvariables.builderrC   rD   rE   rF   Zvariables.nn_modulerG   Zvariables.tensorrH   rI   rJ   rK   	getLoggerri   ru   r5  ZgetArtifactLoggerrp  ro  rq  r  rM   	lru_cacherv   rw   r   ro   rp   r   r   rm   objectr  r   ZTracerr   rc   rc   rc   rd   <module>   sx   (,H
$
#       