U
    yh                     @   s  U 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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m  mZ d dlmZmZ d dlmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d d	l+m,Z,m-Z-m.Z. d d
l/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD d dlEmFZF d dlGmHZHmIZImJZJmKZK d dlLmMZMmNZN d dlOmPZP d dlmQZQ d dlRmSZS ddlTmUZU ddlVmWZWmXZXmYZYmZZZm[Z[ ddl\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZd eeefZge jhG dd dZie jhG d d! d!Zjei Zkejlejmejnejoejpejgejqerejsh	ek_ted"d# Zud$d% Zvd&d' Zwd(d) Zxd*d+ Zyd,d- Zzd.d/ Z{d0d1 Z|d2d3 Z}ej~jej~jeeef d4d5d6Ze,eaeeeejejf f dd7d8d9Zejjeadd:d;d<Zej~jejjdd4d=d>Zej~jeeef d?d@dAZeeef eQeQeee[f eeZ dBdCdDZddEdFdFdGdGdGdHeeedIf eeeef  eeeeef ee ee f  eedIf eeeeeejjdJdKdLZdMdN dFdFdOej~je,dPdQdRZej~jeeejf d?dSdTZdej~jeedIf eeeef  ee dUdVdWZeej eeejf eaeeeejejf f dXdYdZZej~jeaeeeejejf f d[d\d]Zejjdd^d_d`Zejjdd^dadbZejjeadcdddeZeXeeef dfdgdhZdaeee  edi< daeeeef  edj< dkdl Zdmdn Zeej~jdodpdqZedrds ZG dtdu duej~jZddvdwZej~jeedIf eeef eeeeef ee ee f  eedIf eeeef eQeee edxdydzZej~jeedIf eeef eeeeef ee ee f  eedIf eeeef eQeee edxd{d|ZeeWddGdEdFdFdFdFd}ej~jeedIf eeeef  eeeeef ee ee f  eeedIf eeee eeXd~ddZdS )    N)contextmanagernullcontext)AnyCallableDictListOptionalSetTupleUnion)	UserErrorUserErrorType)_fakify_script_objects_gather_constant_attrsmake_constraintsmake_fake_inputsmake_fake_params_buffers$produce_guards_and_solve_constraints)_node_metadata_hook_set_node_metadata_hook)-_AddRuntimeAssertionsForInlineConstraintsPass)CollectTracepointsPass)ConstantAttrMaplift_constants_passrewrite_script_object_meta)placeholder_naming_passplaceholder_prefixes)SpecViolationError)_wrap_submodules)aot_export_moduledetect_fake_mode)FakeScriptObject)
FakeTensorFakeTensorMode)log_export_usage)_combine_args)
OutputKind)#first_call_function_nn_module_stack)ConstraintViolationErrorfree_unbacked_symbolsGuardOnDataDependentSymNodeShapeEnv)_PyTreeCodeGen_PyTreeInfo)insert_deferred_runtime_asserts)TreeSpec)ValueRangeError   )AutogradStateOpsFailSafeguard)_disable_prexisiting_fake_modeExportedProgram	InputKindModuleCallEntryModuleCallSignature)_sig_to_specsArgumentSpecConstantArgumentCustomObjArgumentExportGraphSignatureSymIntArgumentTensorArgumentTokenArgumentc                   @   s6   e Zd ZU dZdZeed< eje	dZ
ee ed< dS )ExportDynamoConfigz:
    Manage Export-specific configurations of Dynamo.
    T	allow_rnn)default_factoryreorderable_logging_functionsN)__name__
__module____qualname____doc__rB   bool__annotations__dataclassesfieldsetrD   r	   r    rN   rN   E/var/www/html/venv/lib/python3.8/site-packages/torch/export/_trace.pyrA   U   s
   
rA   c                   @   s   e Zd ZU ejjed< eed< ee	e
ejeejf f ed< dZee ed< dZee ed< dZeee	ee	ejf f  ed< dS )ExportedArtifactgmsig	constantsNout_spec	fake_modemodule_call_specs)rE   rF   rG   torchfxGraphModulerJ   r=   r   strr   Tensorr"   ScriptObjectrT   r   r0   rU   r$   rV   pytreerN   rN   rN   rO   rP   a   s   
rP   c                  c   sJ   t jjd} t jjd}z
d V  W 5 t jjj|   t jjj|  X d S NF)rW   backendsZmkldnnZ	set_flagsZnnpack)Zorig_mkldnn_flagZorig_nnpack_flagrN   rN   rO   _ignore_backend_decomps   s    
r`   c                 C   s   dt |  S )NZ	L__self__)_strip_rootxrN   rN   rO   
_fixup_key   s    rd   c                 C   sB   t | tr>| dr>| tdd  }|dr:|dd  S |S | S )N_export_root.r2   )
isinstancerZ   
startswithlen)rc   strippedrN   rN   rO   ra      s    ra   c              
   C   sb   t | dkr^d}|2 t|tjt|d t| |}W 5 Q R X W 5 Q R X |d k	sXt|j}d S )Nr   zFile "torch/_export/passes/add_runtime_assertions_for_constraints_pass.py", line 46, in _AddRuntimeAssertionsForInlineConstraintsPassstack_trace)ri   r   	functoolspartialr   r   AssertionErrorgraph_module)range_constraintsrQ   rU   rl   resrN   rN   rO   +_add_runtime_assertions_to_cond_in_subgraph   s      rs   c              
   C   s   | j jD ]}|jtjjjkrd|jkrt|jd }| j 	|L | j j
dtjjj|j||jd dd}|j|_|| | j | W 5 Q R X qd S )Npathcall_functionkind)rt   rv   )argskwargs)graphnodestargetrW   ZopsZhigher_orderZ_export_tracepointrx   ra   Zinserting_beforeZcreate_noderw   metaZreplace_all_uses_withZ
erase_node)rQ   nodert   new_noderN   rN   rO   _rewrite_node   s     
	
r   c                    s   t | }g | jjD ]@}|jdkrd|jkr|jd }|d k	rt|tjr| qt	 }rh|}nt
t dd}t|dkrt|dkrdi ||fS d  fdd}ttj||}	ttj|j|}
ttjtj|jdd	|}|	|
||fS )
NplaceholdervalT)	shape_envexportr   rN   c                    s     } d7  |S )Nr2   rN   )rc   r   countZ	fake_inpsrN   rO   convert_to_fake   s    z/_convert_input_to_fake.<locals>.convert_to_fakeZstatic_shapes)_get_params_buffersry   rz   opr|   rg   rW   r[   appendr!   r$   r,   ri   r]   Ztree_map_onlyfrom_tensorrm   rn   )rQ   rw   rx   params_buffersr}   Zfake_valZdetected_fake_moderU   r   	fake_argsfake_kwargsfake_params_buffersrN   r   rO   _convert_input_to_fake   s,    
r   c                 C   sX   |j D ]"}|jtjtjfkr| |j |_q|jD ]"}|jtjtj	fkr0| |j |_q0d S N)
input_specsrv   r6   Z	PARAMETERBUFFERr{   output_specsr'   ZBUFFER_MUTATIONZGRADIENT_TO_PARAMETER)param_buffer_tablerR   specrN   rN   rO   _replace_param_buffer_names   s    

r   c                    sf   t | t |t   ks@tdt |  dt | dt   d fdd| t |d  D }||S )Nz,Total number of arg names is expected to be z	 but got z positional args, z kwargs.c                    s   g | ]} | qS rN   rN   ).0Zkw_namerx   rN   rO   
<listcomp>   s     z/_convert_to_positional_args.<locals>.<listcomp>)ri   ro   )orig_arg_namesrw   rx   Zreordered_kwargsrN   r   rO   _convert_to_positional_args   s    "r   c           
         s   d}t dd|}|  D ]}t|tjjs.q|jjD ]}|j	dkrFq6d}|j
di  }rtt| \}}	t|	rt|	tjjr||kr|	|krd}nt|	tst|r6dd	  |||jd
 |j fi|} fdd| D |j
d< q6qd S )Nz	L['self']z[^a-zA-Z0-9]_r   outputTnn_module_stackFc                    sT   z6g  G  fddd}t | dd| ii d W S  tk
rN   |  Y S X d S )Nc                       s$   e Zd Z fddZ fddZdS )z@_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Pathc                    s     | | S r   )r   )selfnamepartsrN   rO   __getattr__  s    
zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getattr__c                    s     t| | S r   )r   rZ   )r   idxr   rN   rO   __getitem__  s    zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getitem__N)rE   rF   rG   r   r   rN   r   rN   rO   Path  s   r   Lr   rf   )evaljoin	Exception)rt   r   rN   r   rO   normalize_path  s    	z2_normalize_nn_module_stack.<locals>.normalize_pathrf   c                    s"   i | ]\}\}}| ||fqS rN   rN   )r   keyrt   tyr   rN   rO   
<dictcomp>'  s   
 z._normalize_nn_module_stack.<locals>.<dictcomp>)resubmodulesrg   rW   rX   rY   ry   rz   r   r|   getnextitervaluesinspectisclass
issubclassnnModulerZ   ro   rF   rG   items)
gm_torch_levelZroot_clsrootZroot_keyrQ   r}   Zadd_rootr   rt   r   rN   r   rO   _normalize_nn_module_stack   s2    
 
r   )original_moduletraced_modulereturnc                 C   s6  i }i }| j ddD ]\}}|t|g | q| jddD ]\}}|t|g | q@| D ]\}}|ddd ||< qh| D ]\}}|ddd ||< qi }|j ddD ]4\}	}
|	|kstt|
|kr|t|
  ||	< q|jddD ]6\}	}|	|kstt||kr|t|  ||	< q|S )z
    Returns a mapping of parameter/buffer names from the new module to the
    original model. This is to help with restoring the FQN for parameter/buffers
    of a traced module to what the original module contains.
    Fremove_duplicateN)named_parameters
setdefaultidr   named_buffersr   ro   pop)r   r   Zparam_lookupZbuffer_lookupr   parambufferZfqnsr   Zdynamo_nameZdynamo_paramZdynamo_bufferrN   rN   rO   _get_param_buffer_mapping-  s0    
r   )orig_constant_attrsgraph_signaturerS   r   c                 C   s   i }|  D ]\}}|| kr| | ||< q|jD ]`}|jtjtjfkr0|j}|dk	sXt|||g}|d |_|| }	||= |D ]}
|	||
< qq0dS )zXRewrite the graph signature and constants table to use the FQN from the original module.Nr   )	r   r   rv   r6   CONSTANT_TENSORZ
CUSTOM_OBJr{   ro   r   )r   r   rS   Zremap_tabler   valuer   Zorig_targettargetsZconstantr{   rN   rN   rO   _remap_constantsV  s"    

r   )rQ   r   r   c           
         s(  dd | j jD   fdd}ttj }ttj }i }|jD ]n}|jtjkr<|jj	
|s<|jj	
|r|||jj	t|d  }n|||jj	 }|||jj	< ||j_	q<|jD ] }|jj	|kr||jj	 |j_	q|  D ]F}t|tjjsq|j jD ] }	|	j	|kr||	j	  |	_	|	_q|  qdS )z\
    For strict mode, rename constants nodes that were previously annotated as buffers.
    c                 S   s   h | ]
}|j qS rN   r   )r   r}   rN   rN   rO   	<setcomp>y  s     z*_rename_constants_nodes.<locals>.<setcomp>c                    s>   |  kr0d}|  d|  } kr,|d7 }q|}   |  | S )Nr2   r   )add)r   nZdup_nameZ
node_namesrN   rO   rename_constant{  s    

z0_rename_constants_nodes.<locals>.rename_constantN)ry   rz   r   r6   r   r   r   rv   argr   rh   ri   r   r   rg   rW   rX   rY   r{   	recompile)
rQ   r   r   Zbuffer_prefixZconst_prefixZbuffer_to_constantr   Zc_namemodr}   rN   r   rO   _rename_constants_nodesq  s4    






r   c                 C   s   t | |}| D ]\}}|dd||< q| D ]\\}}t||sHq4t||}t|tjrzt|tjj	sz|
|| nt||| t|| q4|jjD ]&}|jdkr|j}||kr|| |_q|  dS )zV
    Restores the state dict of the traced module to that of the original module.
    rf   r   get_attrN)r   r   replacehasattrgetattrrg   rW   r[   r   	ParameterZregister_buffersetattrdelattrry   rz   r   r{   r   )r   r   r   r   fqnattrr}   	attr_namerN   rN   rO   _restore_state_dict  s"    



r   )r   r   c                 C   s   dd | j ddD S )Nc                 S   s   i | ]\}}|t |jqS rN   )typerE   )r   r   mrN   rN   rO   r     s     z)_get_module_hierarchy.<locals>.<dictcomp>Fr   )Znamed_modules)r   rN   rN   rO   _get_module_hierarchy  s    
r   )module_hierarchyin_specrT   module_call_signaturesr   c                    s>    fdd| D }|d j dks$ttg g ||d|d _|S )Nc                    s   g | ]}t | |d qS ))r   	signature)r7   r   )r   r   r   rN   rO   r     s   z+_make_module_call_graph.<locals>.<listcomp>r    )inputsoutputsr   rT   )r   ro   r8   r   )r   r   rT   r   retrN   r   rO   _make_module_call_graph  s    
   r   rN   FT)preserve_module_call_signaturedisable_constraint_solver(_allow_complex_guards_as_runtime_assertsrestore_fqn_log_export_usagesame_signature.)frw   rx   dynamic_shapesr   r   r   r   r   r   r   c                C   sV  |rt ddhd t|ts2ttjdt| |p8i }tjj	
tt zZi }
t| ||
@ t . tjj| |dd|||||	d	||\}}W 5 Q R X W 5 Q R X W nr ttfk
r } zttjt|W 5 d}~X Y n> tk
r } zttjd	t| d
dW 5 d}~X Y nX W 5 Q R X |
|jd< t| tjjrR|rRt| | |S )z
    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside and produce a torch.fx.GraphModule in torch IR.
    zexport.private_api_export_to_torch_ireventflagsAExpecting `args` to be a tuple of example positional inputs, got TZsymbolic)r   Zassume_static_by_defaultZtracing_moder   Z+prefer_deferred_runtime_asserts_over_guardsr   r   r   Nz5Consider annotating your code using torch._check*(). Zconstrain_as_size_example)Z	case_namerV   )r%   rg   tupler   r   INVALID_INPUTr   rW   Z_dynamoconfigpatchrK   asdictDEFAULT_EXPORT_DYNAMO_CONFIGr   r`   r   r)   r1   CONSTRAINT_VIOLATIONrZ   r+   ZANTI_PATTERNr|   r   r   r   )r   rw   rx   r   r   r   r   r   r   r   rV   r   r   erN   rN   rO   r     sX    
    "

r   c                 C   s   | S r   rN   rb   rN   rN   rO   <lambda>(      r  )	transformpre_dispatch_is_torch_jit_trace)r   constant_attrsc                   s2  t j }t }	|s|rt }	tdd }
t jjjj	| |ddddT |	D t
 2 |
   |t| |d||d\} W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X t| t jjrt| dr|j| j td fd	d
 jd k	}t||f}d}t jt j t j }|jjD ]H}|jdkr||krV|||  }t|t jsV||jd< |d7 }qtt j  j! j"t j# j$ j%|r jj&ni |r jj'ni |r jj(nd fddt)|jjD fddt)tt*t+t,|jjj-D  j j.d\}}t/||d}ddl0m1} ||}ddl2m3} |j4sd}t5|t6j7t8|d$ t9||j:dt;|j dd W 5 Q R X |rddl<m=} |||}|> D ]R}t|t jjsq|jjD ].}|jdkr|j?dd  |j?dd  qĐqt@|}|tA||| tB||| |||| tC|||S )Nc                  s   s*   t jj} zdt j_d V  W 5 | t j_X d S )NT)rW   compilerZ_is_compiling_flag)	old_valuerN   rN   rO   _compiling_state_context4  s
    
z4_export_to_aten_ir.<locals>._compiling_state_contextT)Ztie_weightsstrictZstack_weightsF)Ztrace_jointr  rx   r|   )r   c                    s  t |ttttd fr"td|dS d|jks:t| d|jd }| t j	k r^t
|jdS t |trtt|jdS t |tjrt|jdS t |tjrt|j|  dS t |trt|j|jdS t |tttttd frt|j|dS tdt| dd S )	Nr   )r   r   r   z8 is not a constant or a node with a 'val' metadata fieldr   )r   Z	class_fqnz*Encountered an unsupported object of type z0 while writing the metadata for exported program)rg   intrI   floatr   r;   r|   ro   ri   input_tokensr@   r   r#   r?   rW   ZSymIntr>   r\   r<   _typeZqualified_namer"   Zscript_class_namerZ   )ir}   r   )r   rN   rO   make_argument_specT  s,    


z._export_to_aten_ir.<locals>.make_argument_specr   r   r   r2   c                    s$   g | ]\}}|j d kr ||qS )r   )r   r   r  r}   r  rN   rO   r     s   
z&_export_to_aten_ir.<locals>.<listcomp>c                    s   g | ]\}} ||qS rN   rN   r  r  rN   rO   r     s   )user_inputsinputs_to_parametersinputs_to_buffersuser_outputsZbuffer_mutationsZuser_input_mutationsZgrad_paramsZgrad_user_inputsloss_outputr   r   r  output_tokens)r   r   r    )r  zUFile "torch/fx/passes/runtime_assert.py", line 24, in insert_deferred_runtime_assertsrk   zexported program: )r   )replace_set_grad_with_hop_passr   r   rl   )DrW   _Cis_grad_enabledr   r3   r   r   utilsZ	statelessZ_reparametrize_moduler`   r   rg   rX   rY   r   r|   updater:   Zbackward_signaturer]   Ztree_leavesri   
parametersbuffersr  ry   rz   r   r[   r9   rM   r  r   r!  r"  buffers_to_mutateZuser_inputs_to_mutateZgradients_to_parametersZgradients_to_user_inputsr#  	enumerater   r   reversedrw   r$  r=   torch._guardsr!   torch._dynamor  Zdo_not_emit_runtime_assertsr   rm   rn   r   r/   r   r(   Z3torch._export.passes.replace_set_grad_with_hop_passr%  r   r   r   r   r   rP   )r   r   r   r   r  r  r  r  r'  Zgrad_safe_guardr  rQ   Zis_joint	flat_argsindexZtotal_non_user_inputsr}   Zuser_argr   r   export_graph_signaturer!   rU   Z_dynamo_configrl   r%  _modrS   rN   )r   r  rO   _export_to_aten_ir!  s    


2







  

r5  c                 C   sD   i }| j ddD ]\}}|||< q| jddD ]\}}|||< q.|S )NFr   )r   r   )r   r   r   r   r   rN   rN   rO   r     s    

r   )r   rw   rx   r   c                    s   t | j}|j| j}g }| D ]D\ }|j  jt jj	kr^|
 fddt|D  q$|  q$|r|
dd | D  |S )a  
    Gets the argument names to forward that are used, for restoring the
    original signature when unlifting the exported program module.
    - Positional args: retain the original argument names, and enumerate
        *args as args_0, args_1, ...
    - Keyword args: retain the original kwarg names in the order specified
        by the user. This order seems to matter for the current state of
        export lifted modules.
    c                    s   g | ]\}}  d | qS )r   rN   )r   r  r   r   rN   rO   r     s     z*_get_forward_arg_names.<locals>.<listcomp>c                 S   s   g | ]\}}|qS rN   rN   )r   kwargr   rN   rN   rO   r     s     )r   r   forwardbind_partial	argumentsr   r*  rv   _ParameterKindVAR_POSITIONALextendr-  r   )r   rw   rx   rR   _argsnamesr   rN   r   rO   _get_forward_arg_names  s    r?  orig_mod_bufferstraced_mod_buffersr   rS   c                 C   sN   |j D ]B}|jtjkr|jdk	s$t||j }|| krtj|_|||j< qdS )zrDynamo erroneously marks tensor attributes on modules as a buffers.

    Rewrite them to be tensor constants.
    N)r   rv   r6   r   r{   ro   r   )rA  rB  r   rS   r   r   rN   rN   rO    _rewrite_dynamo_tensor_constants  s    


rC  )orig_modr   rS   c                 C   sb   |   }|jD ]N}|jtjkr|jdk	s,t|j|kr|j|ksDtd|_| |j||j< qdS )zDynamo erroneously drops the persistent flag on buffers.

    Rewrite non-persistent buffers to reflect the original module.
    NF)	
state_dictr   rv   r6   r   r{   ro   
persistentZ
get_buffer)rD  r   rS   rE  r   rN   rN   rO   _rewrite_non_persistent_buffers  s    	

rG  )rp   r   c              	   C   s   t | gt|   D ]\}}t|tjjs.q|jjD ]}|j	dkr|dkr|j
dd }dkrztd| d|j	 dtdd	 | D std| d|j	 d
| q6|j	dkr6|j
ddr6td| d|j	 dq6qdS )a  
    Perform nn_module_stack checks on the graph.
    Current constraints:
        For the top level graph:
        - populated for 'call_function', 'get_attr'
        - None for 'placeholder', 'output'
        For submodule graphs:
        - None for 'placeholder', output'

    TODO(pianpwk): make this a consistent node-level check once nn_module_stack is populated for cond submodules.
    ru   r   r   r   NNode 	 of type z$ is missing nn_module_stack metadatac                 s   sD   | ]<\}}t |to:t |to:t|d ko:tdd |D V  qdS )   c                 s   s   | ]}t |tV  qd S r   )rg   rZ   )r   rc   rN   rN   rO   	<genexpr>H  s     z4_verify_nn_module_stack.<locals>.<genexpr>.<genexpr>N)rg   rZ   r  ri   allr   kvrN   rN   rO   rL  D  s   

z*_verify_nn_module_stack.<locals>.<genexpr>z[ has incorrect nn_module_stack metadata formatexpected Dict[str, Tuple[str, str]], but got r   z7 contains nn_module_stack metadata, this should be None)r-  listr   rg   rW   rX   rY   ry   rz   r   r|   r   r   rM  r   )rp   r  r   r}   r   rN   rN   rO   _verify_nn_module_stack+  s.    

rR  c              	   C   s   t | gt|   D ]\}}t|tjjs.q| jjD ]v}|j	
dd}|jdkr|dkst|tstd| d|j d| q6|jdkr6|r6td| d|j d| q6qdS )	z
    Perform stack trace checks on the graph.
    Constraints:
        - None or non-empty str for 'call_function', 'get_attr'
        - None for 'placeholder', 'output'
    rl   NrH  rI  rJ  zP has invalid stack_trace metadata, expected a string or None but instead found: r   zA contains stack_trace metadata, expected None but instead found: )r-  rQ  r   rg   rW   rX   rY   ry   rz   r|   r   r   rZ   r   )rp   r  r   r}   rl   rN   rN   rO   _verify_stack_traceV  s    

rS  )rQ   rR   c              	   C   s   dd |j D }|  D ]r}t|tjjs,q|jjD ]T}|jdkr4|j	|krNq4||j	 }t
| }|j	|s4td|j	 d| d| q4qdS )a  
    Performs a sanity check on the placeholder node names.
    - User input nodes: no restrictions, should match the original forward() signature
    - Params/buffers/constants/custom_obj/token nodes: should start with prefixes defined in <placeholder_prefixes>
    c                 S   s   i | ]}|j j|jqS rN   )r   r   rv   )r   r   rN   rN   rO   r   v  s      z-_verify_placeholder_names.<locals>.<dictcomp>r   zPlaceholder node name z does not follow spec for z, name should have prefix: N)r   r   rg   rW   rX   rY   ry   rz   r   r   r   rh   r   )rQ   rR   Zname_to_kindr   r}   Z	node_kindprefixrN   rN   rO   _verify_placeholder_namesp  s    


rU  )epr   c                 C   s   d}t  }| j D ]r}t|tjjs(q|jjD ]T}|j	dkr@q0|d7 }t
|jdsXtt
|jdsht||jj d|jj  q0q||dS )Nr   ru   r2   rF   rE   rf   )op_countop_set)rM   rp   r   rg   rW   rX   rY   ry   rz   r   r   r{   ro   r   rF   rE   )rV  rW  rX  r   r}   rN   rN   rO   get_ep_stats  s    
 rY  _EXPORT_FLAGS_EXPORT_MODULE_HIERARCHYc                    s   t   fdd}|S )Nc               
      s   zz<t } | |}t }tf d|| t dt| W nR tk
r } z4t|}|jd |j }td|t	|t d |W 5 d }~X Y nX W 5 d a d aX |S )Nzexport.time)r  Zmetricsr  rf   zexport.error)r  r   messager  )
rZ  r[  timer%   rY  r   r   rF   rG   rZ   )rw   rx   startrV  endr  tZ
error_typefnrN   rO   wrapper  s0    

z$_log_export_wrapper.<locals>.wrapper)rm   wraps)rb  rc  rN   ra  rO   _log_export_wrapper  s    re  c                 C   s^   t | tttfs| f} n2t | tr,t| } nt | tjtfrJ|d krJ| f} |d krVi }| |fS r   )rg   r  rQ  dictrW   r[   )example_inputsZexample_kwarg_inputsrN   rN   rO   $_process_jit_trace_inputs_for_export  s    

rh  )objc                 c   s.   | j }|| | j| _ z
dV  W 5 || _ X dS )znHelper method to make it easier to cleanly torch.export() a method on a
    module that is not `forward`.
    N)r7  __get__	__class__)ri  Z
new_methodZoriginal_methodrN   rN   rO   patch_forward  s
    
rl  c               	   c   s4   t j } t jd z
d V  W 5 t j|  X d S r^   )rW   r&  Z_jit_texpr_fuser_enabledZ_jit_set_texpr_fuser_enabled)Zoriginal_staterN   rN   rO   _temp_disable_texpr_fuser  s
    

rm  c                       s$   e Zd Z fddZdd Z  ZS )_WrapperModulec                    s   t    || _d S r   )super__init__r   )r   r   rk  rN   rO   rp    s    
z_WrapperModule.__init__c                 O   s   | j ||S r   )r   )r   rw   rx   rN   rN   rO   r7    s    z_WrapperModule.forwardrE   rF   rG   rp  r7  __classcell__rN   rN   rq  rO   rn    s   rn  c                 C   s   t   ddlm} t||\}}t| |tjjfrVt| ||ddd	 W  5 Q R  S t| tj
rt|  tjjtjjfrt|  | 4 t|  ||ddd	 W  5 Q R  W  5 Q R  S Q R X n&tt| ||ddd	 W  5 Q R  S W 5 Q R X d S )Nr   )TopLevelTracedModuleFT)r  r  )rm  Ztorch.jit._tracert  rh  rg   rW   r&  ZScriptModule_exportmoduleZScriptMethodownerr   r   rl  rn  )Ztraced_callablerw   rx   rt  Zexport_argsZexport_kwargsrN   rN   rO   "_convert_ts_to_export_experimental  s<     *	rx  )r   rw   rx   r   r   r  original_state_dictorig_in_specr   _disable_forced_specializationsr  c           (   
   C   s  t | ||||d|dd}t|||\}}}}|jjD ]V}|jdkr4d|jkr4t||j}t|t	j
js4|d k	svtd|j|dd|jd< q4i }|jjD ]}|j}|j}|jdkr"t||}t|t	j
jr"|jddd	D ]\}}|||d
 | < q|jddd	D ]\}}|||d
 | < q|jdkrPt||}t|t	jjsP|||< |jdkrt|jt	jjs|jD ]@}|jdkrpt	jjjD ]"}||kr|| ||j |< qqpq|j }}|d k	st|jttfkrttd |g}|jjjj}t t!||j"||j_|#  t$|t|  t%| }|" t&|t'|||i |||d}W 5 Q R X |j(}|j)} |j*}!|+ D ]}"|",dd  |",dd  qr|jjD ]}|jdkr|j| j-kr| j-|j }#|#|kr||# . D ]\}$}%|%|j|$< q|j| j/kr| j/|j }&|&|kr||& . D ]\}$}%|%|j|$< q qt0t1| 2 t3| | |!d t4| |}'t5|'|  t6| | |! t7|| |! t8||  ||_9||_:|jd |_;|S )NF)r   r   r   r   r   r   zbCannot find dynamo_fake_mode. This could be due to the exported graph module have no placeholders.Tr   Zcall_module)recurser   rf   ru   )r  r   rl   r   r@  rV   )<r   r   ry   rz   r   r|   r   r{   rg   rW   r   r   ro   r   r   r   rX   rY   Z_opsZHigherOrderOperatorZ_input_nodesproxyZ_COPY_META_FIELDSZ	_out_specr   rQ  r  r]   r0   Z_codegenZpytree_info	orig_argsr-   r.   Z_in_specr   r   r   r5  r   rQ   rR   rS   r   r   r   r   r!  rC  rM   r+  rf  r   r   rG  r   r   rT   rU   rV   )(r   rw   rx   r   r   r  ry  rz  r   r{  r  r   r   r   r   Zdynamo_fake_moder}   r   Zparams_buffers_to_node_metar{   r|   	submoduler   r   r   entryrT   Zorig_out_specr   r  aten_export_artifactrQ   r3  rS   metadata
param_namerO  rP  Zbuffer_namer   rN   rN   rO   _strict_export  s    
 
  
 











r  c                    s$  d i fdd}t | ||||
|d\}}}}}t|t| }|\ t| |||B\}}}} t||||||||
d} fdd|j D |_W 5 Q R X W 5 Q R X zt||j||||	|
d W n6 t	t
fk
r } zttjt|W 5 d }~X Y nX t| |j|j |_||_|_|S )Nc                    s   d fdd	}|S )Nc              	      s  |pi }G fdddt jj}|| }dd D }t||,  ||fd|i|\}}td| W 5 Q R X tt|j	|_	tt|j
|_
tt|j|_tt|j|_tt|j|_|jjD ]8}	d|	jkr|	jd }
dd	 tt|
 D |	jd< q||fS )
Nc                       s(   e Zd Z fddZfddZ  ZS )z]_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapperc                    s   t    || _d S r   )ro  rp  re   )r   r   rq  rN   rO   rp    s    
zf_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.__init__c              	      sb   t | jtjjr@tjj  tj| jj||}W 5 Q R X n| j||}t	
|\} t|S r   )rg   re   rW   rX   rY   	tracebackZpreserve_node_metaZInterpreterrunr]   tree_flattenr  )r   rw   rx   Ztree_outZ	flat_outsrT   rN   rO   r7    s     ze_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.forwardrr  rN   r  rq  rO   Wrapper  s   r  c                 S   s   g | ]}d | qS )z_export_root.rN   )r   r  rN   rN   rO   r     s    z`_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.<listcomp>rx   z%Exported program from AOTAutograd:
%sr   c                 S   s   i | ]\}}t ||qS rN   )rd   )r   r   r   rN   rN   rO   r     s    z`_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.<dictcomp>)rW   r   r   r   logdebugr]   Ztree_mapra   r*  r+  r!  r   r,  ry   rz   r|   r   )r   rw   rx   r  r  Zwrapped_modZnew_preserved_call_signaturesrQ   rR   r}   r   )
aot_exportrV   rT   r   rN   rO   _aot_export_non_strict  s>       

 zL_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict)NrN   )r  r  )rV   rT   r   )r  rO   _tuplify_outputs  s    3z,_non_strict_export.<locals>._tuplify_outputs)r  r   )r  r  r  c                    s(   i | ] \}}|t |tr  | n|qS rN   )rg   r"   )r   r   ri  )map_fake_to_realrN   rO   r   0  s    z&_non_strict_export.<locals>.<dictcomp>)r{  r  )r   r   r   r   r5  rS   r   r   rQ   r)   r1   r   r   r  rZ   rG  rR   rT   rU   rV   )r   rw   rx   r   r   r  ry  rz  r   r{  r  r  rU   r   r   Zequalities_inputsZoriginal_signaturer   Zpatched_modZnew_fake_argsZnew_fake_kwargsZnew_fake_constant_attrsr  r  rN   )r  rV   rT   r   rO   _non_strict_export  sv    <


	"  r  )r  r   r  r   r{  r  )r   rw   rx   r   r  r   r  r   r{  r  r   c                 C   sn  t |ts ttjdt| |r4|r4ttjdt| at }
|
	|rNdnd |
	|r`dnd t
d|
d |
a|p|i }t |tjjr|| ||}t||f\}}| jd	d
}|	st| ||}nd}|rtnt}|| ||||||||||	}|j}|j}|j}|j}|j}|j}||jd< dd |jj D |jd< t dd t!|j"D t#|j"}t$| |||	d}t%|||||}|rt&||| i }| D ]4\}}|st'|n|}t(f g g d|||< qt#|dkr|st)| t*|||}|dk	st+|j,}|dk	s t+t-| t.| |	s@t/|| t0||j1|||t2t|||||f|jd}|S )a  
    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside and produce a ExportedProgram.

    Args:
        f: the `nn.Module` to trace.

        args: example positional inputs.

        kwargs: optional example keyword inputs.

        dynamic_shapes:
         An optional argument where the type should either be:
         1) a dict from argument names of ``f`` to their dynamic shape specifications,
         2) a tuple that specifies dynamic shape specifications for each input in original order.
         If you are specifying dynamism on keyword args, you will need to pass them in the order that
         is defined in the original function signature.

         The dynamic shape of a tensor argument can be specified as either
         (1) a dict from dynamic dimension indices to :func:`Dim` types, where it is
         not required to include static dimension indices in this dict, but when they are,
         they should be mapped to None; or (2) a tuple / list of :func:`Dim` types or None,
         where the :func:`Dim` types correspond to dynamic dimensions, and static dimensions
         are denoted by None. Arguments that are dicts or tuples / lists of tensors are
         recursively specified by using mappings or sequences of contained specifications.

        preserve_module_call_signature: A list of submodule paths for which the original
            calling conventions are preserved as metadata.

        _allow_complex_guards_as_runtime_asserts:
         With the current dynamic shapes language for dims and derived dims, we can run into constraints
         that are not expressible with the language. For example, flattening a matrix and adding to a vector,
         both fully dynamic (i.e. x.reshape([-1]) + y) emits a guard s0 * s1 = s2, which is not expressible.
         By default, we either raise a constraint violation error or specialize to static values.
         If this flag is set to True, we avoid erroring out and instead allow complex constraints to exist as runtime
         assertions in the graph. The sympy interpreter (torch/utils/_sympy/interp.py) will produce the math ops
         required to compute and assert the value of the guard (e.g. sym_size_int, eq, _assert_scalar).
         Additionally, if TORCH_DYNAMO_DO_NOT_EMIT_RUNTIME_ASSERTS=1 is specified, we will allow complex constraints
         while not emitting runtime asserts, returning a cleaner graph with lesser guarantees around dynamic shapes.

        _disable_forced_specializations:
         Similar to _allow_complex_guards_as_runtime_asserts, but only avoids specializing to static values if set to True.
         For complex guards that don't specialize, this flag doesn't have any effect. Ideally this would be subsumed by
         _allow_complex_guards_as_runtime_asserts, but this handles one additional case: single-variable equalities where
         the symbol is solvable for a concrete value (e.g. Eq(s0 // 4, 400) -> s0 = 1600). If set to True, this flag will
         avoid specializations. Direct equalities (e.g. s0 = 4), will still specialize.

    Returns:
        An ExportedProgram containing the traced method.
    r  zL_disable_forced_specializations can be only be specified in non-strict mode.r  Z
non_strictr  Zaot_dispatchzexport.enterr  T)Z	keep_varsNforward_arg_namesc                 S   s   i | ]\}}t |r||qS rN   )r*   rN  rN   rN   rO   r     s    z_export.<locals>.<dictcomp>Zinline_constraintsc                 s   s"   | ]\}}|j tjkr|V  qd S r   )rv   r6   Z
USER_INPUT)r   r  srN   rN   rO   rL    s   z_export.<locals>.<genexpr>)r  )r   r   r   )r   ry   r   rE  rq   Zmodule_call_graphrg  rS   )3rg   r  r   r   r  r   r   r[  rM   r   r%   rZ  rW   r   ZShapesCollectionr   r]   r  rE  r?  r  r  rQ   rR   rT   rS   rU   rV   r|   r   Zvar_to_ranger   r   r-  r   ri   r&   r   rs   ra   r8   r   r   ro   rp   rR  rS  rU  r5   ry   r   ) r   rw   rx   r   r  r   r  r   r{  r  r  r1  rz  ry  r  Zexport_funcr  rQ   r3  rT   rS   rU   rV   Z
num_liftedZcombined_argsrq   r   r   specsZmod_fqnrr   exported_programrN   rN   rO   ru  L  s    A


    
ru  )NN)N)N)NN)rK   rm   r   loggingr   r]  warnings
contextlibr   r   typingr   r   r   r   r   r	   r
   r   rW   r0  Ztorch.fxZtorch.utils._pytreer(  Z_pytreer]   Ztorch._dynamo.excr   r   Ztorch._export.non_strict_utilsr   r   r   r   r   r   Z(torch._export.passes._node_metadata_hookr   r   Z@torch._export.passes.add_runtime_assertions_for_constraints_passr   Z-torch._export.passes.collect_tracepoints_passr   Z(torch._export.passes.lift_constants_passr   r   r   Ztorch._export.utilsr   r   Ztorch._export.verifierr   Ztorch._export.wrappersr   Ztorch._functorch.aot_autogradr   r/  r!   Z"torch._library.fake_class_registryr"   Ztorch._subclasses.fake_tensorr#   r$   Ztorch._utils_internalr%   Ztorch.export.dynamic_shapesr&   Ztorch.export.exported_programr'   Ztorch.fx._utilsr(   Z%torch.fx.experimental.symbolic_shapesr)   r*   r+   r,   Ztorch.fx.graphr-   r.   Ztorch.fx.passes.runtime_assertr/   r0   Ztorch.utils._sympy.value_rangesr1   Z
_safeguardr3   r  r4   r5   r6   r7   r8   r   r9   r:   r;   r<   r=   r>   r?   r@   	getLoggerrE   r  	dataclassrA   rP   r
  criticalr  error	exceptioninfowarningprintwarnrD   r`   rd   ra   rs   r   r   r   r   r   r   r   rZ   r   r[   r\   r   rX   rY   r   r   r   r   rI   r   r5  r   r?  rC  rG  rR  rS  rU  rY  rZ  rJ   r[  re  rh  rl  rm  rn  rx  r  r  ru  rN   rN   rN   rO   <module>   s   ( (


$4
*2 $

  
 
K 9 
 +!
		
&

 

 0

 

   
 
