U
    ªÊ¹jŒÝ  ã                   @   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js teqd¡Zuejs teqd¡Zvejs teqd¡Zwejs teqd¡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ƒZ€G d'd(„ d(ƒZ�ee‚eƒf Z„G d)d*„ d*e ey ƒZ…G d+d,„ d,ej†ƒZ‡d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/bwave/python/venv/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   Z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j1 2¡ | _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 ©NéÿÿÿÿrË   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„}nÄtˆt	jjƒ�rZtˆt	jjƒsìt‚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 �q˜d !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_hookszŽnn.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  r0  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|| ƒ ¡ �qŠtˆj&ƒdk�rJ|ƒ  g }ˆ 'd¡ˆ_(t)dƒ}|t*ˆj&ƒƒ}ˆ +||¡ t,ˆ|	ƒ}| %| -|d¡¡ | %t.ddƒ¡ | | /ˆj0j(¡¡ ˆ 1|¡ |�rÐtdd„ |D ƒƒ�rÐtdd„ |D ƒƒ�rÐtt2|ƒƒt|ƒk�rÐˆj3 4¡ �rÐ|ƒ  ˆ 1ˆ 5ˆtt|ƒƒ|	¡tdt|ƒd�g ¡ nþˆ 'd¡}t,ˆ|	|ƒ}ˆj3 6|¡ | 7|¡ ˆj3 8|¡ t,ˆ|	|dd„ |j9 ¡ D ƒd�}ˆj3 6|¡ | 7|¡ ˆj3 8|¡ 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   r@  6  s   ÿc                 s   s   | ]}t |tƒV  qd S re   )rþ   rJ   ©r   Úxrc   rc   rd   r@  :  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_inforH  Úopnamerz   r   rD  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   rP  rà   rF  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ç   rR  rx   r>  ÚinstrI  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   )rH  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   rY  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ƒr¤d}| |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õ   r3  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_proxyrJ  rc   rc   rd   r@  ©  s     z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>ÚstatsZcalls_capturedZ__compiled_fnú%sc                      s
   ˆ  ˆ ¡S re   )rh  rc   ©r  r^   rc   rd   r  º  r  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>Zunique_graphs)"rQ  r>  rþ   r¹   rë   rˆ   rh   ÚupdaterØ   rÏ   Z
create_argr&  Úremove_unused_graphargsr5   rL   r6   rÕ   rG  r   r¡   rS  r   Úgraph_code_logrö   r<   Úgraph_tabular_logr=   Úgraph_sizes_logr>   Úcall_user_compilerr\  r    Zmake_call_generated_coder^  )
r^   rç   Úrvr`  r>  rà   Zncallsr•   Úcompiled_fnZcgrc   rk  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>)rv  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r²t|ƒ}|||  ¡ ƒ}	tƒ tjd	|› �ƒ t|	ƒsät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   rt  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ô   ru  rz   r;   rõ   rû   Z_dynamo_sourcerS   Z_param_name_to_sourcer½   Z_source_to_user_stacksr9  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totrv  r÷   Úplrù   r  r¥   rs  ÚeÚmsgrc   rc   rd   rq  Õ  sf    




ÿ
ýÿþÿ ÿþ
üýr|  c                 C   s    g }| j D ]}| |j¡ q
|S re   )rw  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ê| |	¡ qêd S )	Nr   r%  ry  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Ð   Úusersru  rÙ   r$  ÚoperatorÚgetitemr  rv  rz   rõ   rü   r„  r   Úremove)
r^   r÷   r†  r‡  Zused_symbolsZrecheck_placeholdersZbinds_symbolrù   ZfakeÚsymbolrc   rg   rd   rm    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   rF  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   rG  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   rb  rY  rh  Z_guardsr   Zclear_framer]  r   r   ÚNoderv  rw  r7   r¡   r   rq  r–   rm  r   rF  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 )NrA  rx  )Úlinenoc                     s(   t ˆˆ ƒ ¡ } dˆjj› dˆ› d| › �S )NzTRACE FX call z from re  )r8   Úrstripr÷   r  )Úline©Zcur_instÚheaderrr  Ztx_coderc   rd   Úget_trace_call_log_strÝ  s    z;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strrj  Únn_module_stack>   rz  ry  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ê   rU  rV  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Úformatr5  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^   ru  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ô   rd  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
r¾t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  rt  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‰  r7  rU  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   ra  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   r4  ZgetArtifactLoggerro  rn  rp  r¢  rM   Ú	lru_cacherv   rw   r‡   ro   rp   rˆ   r’   rm   Úobjectr�  r£   ZTracerr°   rc   rc   rc   rd   Ú<module>   sx   (,H
$
#       