
    VhQ                       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Zd dlm	Z	m
Z
 d dlmZmZmZmZ d dlZd dlZd dlZd dlmc 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!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.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZA d dlBmCZCmDZD d dlEmFZFmGZG d dlHmIZI d dlJmKZK d dlLmMZM d dlNmOZO d dlPmQZQ d dlRmSZS d dlTmUZUmVZV d dlWmXZX d dlYmZZZm[Z[m\Z\m]Z] d dl^m_Z_m`Z`maZambZb d dlcmdZdmeZe d dlfmgZg d dlmhZh d d limjZj d!d"lkmlZl d!d#lmmnZnmoZompZpmqZqmrZr d!d$lsmtZtmuZu  ej                  ew      Zxe j                   G d% d&             Zze j                   G d' d(             Z{ e j                  d)*       G d+ d,             Z| ez       Z}ej                  ej                  ej                   ej                  ej                  ej                  ej                  eej
                  h	e}_        e	d-        Ze	d.        Zd/ Zd0 Zd1 Zd2ej                  j                  fd3Zd4 Zd5 Zd6 Zd7 Zd8ej&                  j(                  d9ej&                  j(                  d:eeef   fd;Zd2ej                  j                  d<eud=eeej0                  f   d>eeeej0                  eKej2                  f   f   d?ee   f
d@ZdAe+dBeud>eeeej0                  eKej2                  f   f   d:dfdCZd2ej                  j                  d:dfdDZd)dEd2ej                  j                  d:e{fdFZd2ej                  j                  dBeud:dfdGZd8ej&                  j(                  d9ej                  j                  d:dfdHZdIej&                  j(                  d:eeef   fdJZ	 ddKehdLehdMeeerf   dNeee      d:eeq   f
dOZ	 	 ddPdQdQd)d)d)dRdSedTeedUf   dVeeeef      dWeeeeef   ee   ee   f      dXeedUf   dYedZed[ed\ed]ed:ej                  j                  fd^Z	 dd_ dQdd)dQd)dQd`dIej&                  j(                  dae+dbedceddedeed:e{fdfZ	 ddIej&                  j(                  dTeedUf   dVeeeef      d:ee   fdgZdIej&                  j(                  d:ee   fdhZdieej0                     djeeej0                  f   dBeud>eeeej0                  eKej2                  f   f   d:df
dkZdlej&                  j(                  dBeud>eeeej0                  eKej2                  f   f   d:dfdmZdnej                  j                  d:dfdoZdnej                  j                  d:dfdpZd2ej                  j                  d<eud:dfdqZdreod:eeef   fdsZdaeee      edt<   daeeeef      edu<   dv Zdw ZdIej&                  j(                  d:eeef   fdxZdy Z	 ddze|dXeedUf   d{edNeee      d:eej                  j                  eeq   f   f
d|Zdze|d}eeef   fd~ZdeOfdZe	dej&                  j(                  fd       Ze	d        Z G d dej&                  j(                        ZddZdIej&                  j(                  dTeedUf   dVeeef   dWeeeeef   ee   ee   f      dXeedUf   dedeeef   dehdZedced:e|fdZdIej&                  j(                  dTeedUf   dVeeef   dWeeeeef   ee   ee   f      dXeedUf   dedeeef   dehdZedceded:e|fdZdd fdIej&                  j(                  dae+d:e{fdZd Zd2ej                  j                  ded:ej                  j                  fdZ	 ddIej&                  j(                  dTeedUf   dVeeef   dWeeeeef   ee   ee   f      dXeedUf   dedeeef   dehdZedceded:e|fdZeen	 	 dd)dPddIej&                  j(                  dTeedUf   dVeeeef      dWeeeeef   ee   ee   f      dedXeedUf   d:eofd              Zeen	 	 dd)dPdQdQdQddIej&                  j(                  dTeedUf   dVeeeef      dWeeeeef   ee   ee   f      dedXeedUf   dedZedced:eofd              Zy)    N)contextmanagernullcontext)AnyCallableOptionalUnion)enable_python_dispatcher)	UserErrorUserErrorType)exportdb_error_messageget_class_if_classified_error)_fakify_module_inputs_fakify_script_objects_gather_constant_attrs_NonStrictTorchFunctionHandlermake_constraintsmake_fake_inputs$produce_guards_and_solve_constraints)CollectTracepointsPass)_materialize_and_lift_constantsConstantAttrMap)_collect_param_buffer_metadata_compiling_state_context_fakify_params_buffers)_populate_param_buffer_metadata_to_new_gm_update_gm_meta_if_possibleapply_runtime_assertion_passplaceholder_naming_passplaceholder_prefixes)SpecViolationError)_wrap_submodules)_graph_input_names_graph_output_names)GraphSignature)get_subclass_typing_container)create_functional_call)create_tree_flattened_fnregister_buffer_assignment_hook)_detect_attribute_assignmentaot_export_module)detect_fake_mode)FakeScriptObject)dtrace_structured)FakeTensorMode)log_export_usage)!_check_input_constraints_pre_hook)_check_dynamic_shapes_combine_args)
OutputKind)get_proxy_slotmake_fxPreDispatchTorchFunctionModetrack_tensor_tree)ConstraintViolationErrorfree_unbacked_symbolsGuardOnDataDependentSymNodeShapeEnv)_PyTreeCodeGen_PyTreeInfo)	_get_attr)TreeSpec)ValueRangeError   )AutogradStateOpsFailSafeguard)_disable_prexisiting_fake_modeExportedProgram	InputKindModuleCallEntryModuleCallSignature)"_convert_to_export_graph_signatureExportGraphSignaturec                   Z    e Zd ZU dZdZeed<    ej                  e	      Z
e	e   ed<   dZy)ExportDynamoConfigz:
    Manage Export-specific configurations of Dynamo.
    T	allow_rnn)default_factoryreorderable_logging_functionsN)__name__
__module____qualname____doc__rL   bool__annotations__dataclassesfieldsetrN   r   do_not_emit_runtime_asserts     C/home/dcms/DCMS/lib/python3.12/site-packages/torch/export/_trace.pyrK   rK   j   s<     It3D;3D3D4!3x=  #'rZ   rK   c                       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<   y)ATenExportArtifactgmsig	constantsN)rO   rP   rQ   torchfxGraphModulerT   rI   dictstrr   Tensorr,   ScriptObjectrY   rZ   r[   r]   r]   {   sM    	LL 	
	 rZ   r]   T)frozenc                   h    e Zd ZU eed<   eed<   eed<   eed<   eeeee	j
                  f   f   ed<   y)ExportArtifactatenin_specout_spec	fake_modemodule_call_specsN)
rO   rP   rQ   r]   rT   r?   r.   rd   re   pytreerY   rZ   r[   rj   rj      s8    
Cc6??&:!;;<<rZ   rj   c               #     K   t         j                  j                  j                  d      } t         j                  j                  j                  d      }	 d  t        j                  j                  j                  |   t        j                  j                  j                  |  y # t        j                  j                  j                  |   t        j                  j                  j                  |  w xY wwNF)ra   backendsmkldnn	set_flagsnnpack)orig_mkldnn_flagorig_nnpack_flags     r[   _ignore_backend_decompsry      s     ~~,,66u=~~,,66u=;'')9:'')9: 	'')9:'')9:s    AC8B' AC8'AC55C8c               #   f  K   t         j                  j                  j                  } 	 dt         j                  j                  _        t         j                  j                  j                   | t         j                  j                  _        y # | t         j                  j                  _        w xY wwrr   ra   
_functorchconfigdecompose_custom_triton_ops)olds    r[   2_disable_custom_triton_op_functional_decompositionr      sw     



!
!
=
=CB>C;%%AAA>A;c;s   %B1AB - B1!B..B1c                  L    t         j                  j                  j                   S Nr{   rY   rZ   r[   (custom_triton_ops_decomposition_disabledr      s    &&BBBBrZ   c                     dt        |       z   S )N	L__self__)_strip_rootxs    r[   
_fixup_keyr      s    Q''rZ   c                     t        | t              r0| j                  d      r| t        d      d  }|j	                  d      S | S )N_export_root.)
isinstancere   
startswithlenremoveprefix)r   strippeds     r[   r   r      s?    !Sall>:S(*+$$S))HrZ   r^   c           
         | j                   j                  D ]  }|j                  t        j                  j
                  j                  k(  s6d|j                  v sEt        |j                  d         }| j                   j                  |      5  | j                   j                  dt        j                  j
                  j                  |j                  ||j                  d   d      }|j                  |_        |j                  |       | j                   j                  |       ddd        y# 1 sw Y   'xY w)z
    In-place modifiy input graph module by replacing the export tracepoint with a new node
    that has the same target and args, but with the _export_root stripped from path.
    pathcall_functionkind)r   r   )argskwargsN)graphnodestargetra   opshigher_order_export_tracepointr   r   inserting_beforecreate_noder   metareplace_all_uses_with
erase_node)r^   noder   new_nodes       r[   _rewrite_tracepoint_noder      s    
  .;;%))00CCC$"4;;v#67XX..t4 .!xx33'		..AA!YY$($(KK$7 	  4  H %)IIHM..x8HH''-. .	.. .s   BD44D>	c                    g g }| j                   j                  D ]  }|j                  dk(  rMd|j                  v r?|j                  d   }|2t	        |t
        j                        sMj                  |       _d|j                  v sn|j                  d   }|t	        |t
        j                        s|j                  |        t        |z         x}r|}nt        t               d      }dfd}t        j                  t
        j                  ||      }	t        j                  t
        j                  ||      }
|	|
|fS )z
    Given a graph module, extract fakified input tensors from the metadata of
    its placeholders, and map them to the structure of given args and kwargs.
    Also return the fake mode used to fakify those inputs.
    placeholdervalexample_valueT)	shape_envexportr   c                        }dz  |S )NrA   rY   )r   r   count	fake_inpss     r[   lookup_fakez)_extract_fake_inputs.<locals>.lookup_fake   s    

rZ   )r   r   opr   r   ra   rf   appendr+   r.   r;   rp   tree_map_only)r^   r   r   	fake_valsr   fake_valdetected_fake_modern   r   	fake_argsfake_kwargsr   r   s              @@r[   _extract_fake_inputsr      s    %'I$&I +77m#(:yy'H#
8U\\(J  *		)yy1H#
8U\\(J  *+ .i).CDDD&	"XZE	E $$U\\;EI&&u||[&IKk9,,rZ   c                 L   |j                   D ]C  }|j                  t        j                  t        j                  fv s0| |j
                     |_        E |j                  D ]C  }|j                  t        j                  t        j                  fv s0| |j
                     |_        E y r   )
input_specsr   rE   	PARAMETERBUFFERr   output_specsr3   BUFFER_MUTATIONGRADIENT_TO_PARAMETER)param_buffer_tabler_   specs      r[   _replace_param_buffer_namesr     s     :99
 
 -T[[9DK:    :99&&,,
 
 -T[[9DK:rZ   c           	          t        |       t        |      t        |      z   k(  s,J dt        |        dt        |       dt        |       d       | t        |      d  D cg c]  }||   	 }}g ||S c c}w )Nz,Total number of arg names is expected to be z	 but got z positional args, z kwargs.)r   )orig_arg_namesr   r   kw_namereordered_kwargss        r[   _convert_to_positional_argsr     s    ~#d)c&k"99 
6s>7J6K Lt9+/F}H	F9 8Fc$ik7RSGwSS		  Ts    A5c                    d}t        j                  dd|      }| j                         D ]V  }t        |t        j
                  j                        s)|j                  j                  D ]  }|j                  dv rd}|j                  j                  di       x}r|t        t        |j                                     \  }}	t        j                   |	      r0t#        |	t        j$                  j&                        r||k(  r|	|u rd}nt        |	t(              sJ |sd }
|||j*                  d	z   |j,                  z   fi|}|j/                         D 	ci c]  \  }\  }}	| |
|      |	f c}	}}|j                  d<    Y y c c}	}}w )
Nz	L['self']z[^a-zA-Z0-9]_r   outputTnn_module_stackFc                     	 g  G fdd      }t        | dd |       ii       dj                        S # t        $ r | cY S w xY w)Nc                   "    e Zd Z fdZ fdZy)@_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Pathc                 4    |dk7  rj                  |       | S )N_modules)r   )selfnamepartss     r[   __getattr__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getattr__9  s    #':#5$)LL$6'+rZ   c                 <    j                  t        |             | S r   )r   re   )r   idxr   s     r[   __getitem__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getitem__>  s     %SX 6'+rZ   N)rO   rP   rQ   r   r   )r   s   r[   Pathr   8  s    ,
,rZ   r   Lr   r   )evaljoin	Exception)r   r   r   s     @r[   normalize_pathz2_normalize_nn_module_stack.<locals>.normalize_path4  sQ    $ ", , TC&$&)9#:;"xx.$ $#$s   37 AAr   )resubmodulesr   ra   rb   rc   r   r   r   r   getnextitervaluesinspectisclass
issubclassnnModulere   rP   rQ   items)gm_torch_levelroot_clsrootroot_keyr^   r   add_rootr   r   tyr   keys               r[   _normalize_nn_module_stackr     su   DvvosD1H$$& -"ehh223HHNN *	Dww33H"&))--0A2"FFF_%;%;%= >?b ??2&:b%((//+Jt|h#(%b#...$( tX%8%83%>AVAV%VW#%# ,;+@+@+B0 0'ZdB ..330		+,O*	-T0s   Foriginal_moduletraced_modulereturnc                    i }i }| j                  d      D ]  \  }}||t        |      <    | j                  d      D ]  \  }}||t        |      <    i }|j                  d      D ]*  \  }}	||vsJ t        |	      |v s|t        |	         ||<   , |j                  d      D ]*  \  }}
||vsJ t        |
      |v s|t        |
         ||<   , |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_duplicate)named_parametersidnamed_buffers)r   r   param_lookupbuffer_lookupr   parambufferr   dynamo_namedynamo_paramdynamo_buffers              r[   _get_param_buffer_mappingr  Q  s8    $&L$&M&777O 'e"&RY''55u5M )f$(bj!) *,%2%C%C &D & M!\ "4444l|+.:2l;K.L{+M '4&A&A 'B ' O"] "4444m-.;B}<M.N{+O rZ   r_   fake_params_buffersr`   flat_fake_argsc                 ~   | j                   j                  D cg c]  }|j                  dk(  s| }}t        |j                        t        |      k(  sJ d}t        ||j                        D ]  \  }}|j                  t        j                  t        j                  fv r:|j                  J ||j                     j                  |j                  d   _        m|j                  t        j                  k(  rC||   }	t        |	t        j                         r|	j                  |j                  d   _        |dz  }|j                  t        j"                  k(  r|j                  J ||j                     }
t        |
t        j                         s#|j                  d   j$                  r |
j                  |j                  d   _        \|j                  d   j                  |
j                  k(  rJ |j                  t        j&                  t        j(                  fv rt+        |j                         y c c}w )Nr   r   r   rA   )r   r   r   r   r   zipr   rE   r   r   r   requires_gradr   
USER_INPUTr   ra   rf   CONSTANT_TENSORis_leaf
CUSTOM_OBJTOKENAssertionError)r^   r_   r  r`   r  r   placeholdersir   fake_argconstants              r[   _preserve_requires_grad_passr  t  s    &(XX^^PTtww-7ODPLPs3|#4444	A,8 ,
d99
 
 ;;***-@.m IIe* YY)...%a(H(ELL1191G1G		% .FAYY)333;;*** -H(ELL199U#++5=5K5KDIIe$299U+99X=S=SSSSYY9//AA ++5, Qs
   H:H:orig_constant_attrsgraph_signaturec                 P   i }|j                         D ]  \  }}|| v s| |   ||<    |j                  D ]p  }|j                  t        j                  t        j
                  fv s0|j                  }|J |j                  ||g      }|d   |_        ||   }	||= |D ]  }
|	||
<   	 r y)zXRewrite the graph signature and constants table to use the FQN from the original module.Nr   )r   r   r   rE   r  r  r   r   )r  r  r`   remap_tabler   valuer   orig_targettargetsr  r   s              r[   _remap_constantsr    s     )+K ( ;e'' 3E :K;  ++ -99%%  
 
 ++K***!ookK=AG!!*DK -H+&! -$,	&!--rZ   c                    ddl m} ddlm} ddlm}m}  ||       x}y|j                  D ch c]1  } |||j                  |j                  f      r||j                  vr|3 }}| j                  j                  D ][  }|j                  j                  dd       |j                  j                  d      x}	= ||	d||d	
      x}
sM|
|j                  d<   ] yc c}w )a  
    When we run an interpreter-based pass over a GraphModule, execution of data-dependent operators
    will produce example values with new unbacked symbols. To track that the new/old symbols are equivalent,
    we used to rely on the unbacked_renamings mapping. This led to problematic metadata where the unbacked_bindings
    keys mapped new symbols (u2) to paths containing old symbols (u0) in the example values, or worse, backed symbols
    or constants (e.g. if the original unbacked was replaced/specialized). Additionally this created problems with
    de/serialized programs, since we didn't comprehensively serialize ShapeEnv/unbacked renamings/node bindings.

    This pass attempts a simpler way of handling these for export, by throwing away the previously computed bindings, and re-running
    the pattern match used in compute_unbacked_bindings. This ensures we keep the original symbols contained in the example values,
    or delete bindings if they've been replaced/specialized.
    r   )_get_shape_env_from_gm) _free_unbacked_symbols_with_path)symbol_is_typeSymTNunbacked_bindingsr   rY   T)r   pendingsimplify)torch._export.utilsr  %torch.fx.experimental.symbolic_shapesr  torch.utils._sympy.symbolr   r!  var_to_rangeUNBACKED_INTUNBACKED_FLOATunbacked_renamingsr   r   r   popr   )r^   r  r  r   r!  r   symbolbase_unbacked_symbolsr   r   r"  s              r[   _replace_unbacked_bindingsr/    s     ;V>+B//	8  ,,&4#4#4d6I6I"JK)666 	   ?		)4099==''C4!A#-"   .?DII)*?s   6C)_prettify_placeholder_namesc        	   	      0   t        j                  ||f      }	t        | |      \  } }t        |        t	        |j
                        t	        |j                        z   t	        |j                        z   }
t        | |	|
       t        || t        |            }t        | ||      }|r$ddlm} ddlm}  || |      \  } } || |      \  } }| j!                         D ]  }t#        |t$        j&                  j(                        s(|j*                  j,                  D ]I  }|j.                  dv s|j0                  j3                  dd       |j0                  j3                  dd       K  |J |rt5        | ||||||       t7        | ||||	       t9        | ||      S )a  
    This is a helper function that is shared between export_to_aten_ir and export_to_aten_ir_make_fx
    to produce the aten artifact. (export compatible graph module + signature)

    It does:
    1. Applies runtime assertion pass
    2. Populate meta val when missing
    3. Lift constants as placeholders
    4. Replace raw autograd and autocast ops with HOPs
    5. Prettify names for placeholders
    6. Preserve requires_grad value on node meta val
    r   )replace_autocast_with_hop_pass)replace_set_grad_with_hop_passr   r   Nstack_trace)rp   tree_leavesr   r/  r   
parametersbuffersinput_tokensset_missing_meta_valsrH   _get_non_persistent_buffersr   3torch._export.passes.replace_autocast_with_hop_passr2  3torch._export.passes.replace_set_grad_with_hop_passr3  r   r   ra   rb   rc   r   r   r   r   r,  r   r  r]   )r^   modconstant_attrsr  pre_dispatchr   r   r  r0  r  total_non_user_inputsexport_graph_signaturer`   r2  r3  _modr   s                    r[   _produce_aten_artifactrC    s   4 ''K(@AN6r?KB
 r" 	O&&'
o%%
&	'
o**
+	, 
 "n.CD @8= 0
"NI 	
	
 &D&&
"" &D&&
""
 

 3$ 4 45JJ$$ 	3Dww33		/6		mT2	33 "---""	
 !
"$7N 
 rZ   c                 v  
 | j                   j                  D ch c]  }|j                   c}

fd}t        t        j
                     }t        t        j                     }i }|j                  D ]  }|j                  t        j                  k(  s!|j                  j                  j                  |      rG|j                  j                  j                  |      r, |||j                  j                  t        |      d z         }n |||j                  j                  z         }|||j                  j                  <   ||j                  _         |j                  D ]C  }|j                  j                  |v s||j                  j                     |j                  _        E | j                         D ]|  }	t        |	t        j                   j"                        s(|	j                   j                  D ],  }|j                  |v s||j                     x|_        |_        . |	j'                          ~ yc c}w )z\
    For strict mode, rename constants nodes that were previously annotated as buffers.
    c                 r    | v r d}|  d| x}v r|dz  }|  d| x}v r|} j                  |        | S )NrA   r   )add)r   ndup_name
node_namess      r[   rename_constantz0_rename_constants_nodes.<locals>.rename_constantR  s_    :A"&q},8;Q #'q},8;DtrZ   N)r   r   r   r   rE   r   r  r   r   argr   r   r   r   r   ra   rb   rc   r   	recompile)r^   r  r   rJ  buffer_prefixconst_prefixbuffer_to_constantr   c_namer=  rI  s             @r[   _rename_constants_nodesrQ  H  s    )+7$))7J ))9)9:M'	(A(ABL++ #99	111$((--:R:R;
 xx}}''6( 488==]1C1E#FF ))EF06txx}}-"DHHM#  ,, >88==...txx}}=DHHM>
 zz| #uxx334IIOO 	HDyy..*<TYY*GG	DK	H 	A 8s   H6c                 X   t        | |      }|j                         D ]  \  }}|j                  dd      ||<    |j                         D ]  \  }}t        ||      st	        ||      }t        |t        j                        r7t        |t        j                  j                        s|j                  ||       nt        |||       t        ||        |j                  j                  D ]-  }|j                  dk(  s|j                   }||v s$||   |_        / |j#                          y)zV
    Restores the state dict of the traced module to that of the original module.
    r   r   get_attrN)r  r   replacehasattrgetattrr   ra   rf   r   	Parameterregister_buffersetattrdelattrr   r   r   r   rL  )r   r   r   r   fqnattrr   	attr_names           r[   _restore_state_dictr^  y  s    3?MR
 (--/ 9	c#&;;sC#84 9 (--/ 	%	c}d+}d+dELL)*T588CUCU2V))#t4M3-t$	% ##)) <77j I..0;	< rZ   r=  c                     | j                  d      D ci c]  \  }}|t        |      j                   c}}S c c}}w NFr   )named_modulestyperO   )r=  r   ms      r[   _get_module_hierarchyrd    sA    .1.?.?QV.?.W#*4d1g  s    :rl   rm   module_call_signaturesforward_arg_namesc           	      @   t         D cg c]  }t        ||j                  |              }}|d   j                  dk(  sJ t	        g g | ||      |d   _        |j                         D cg c]  \  }}|t         vrt        ||       }}}g ||S c c}w c c}}w )N)r[  	signaturer    inputsoutputsrl   rm   rf  )_EXPORT_MODULE_HIERARCHYrF   r   r[  rG   rh  r   )rl   rm   re  rf  r[  originalrh  
additionals           r[   _make_module_call_graphrp    s     , 	C+A+E+Ec+JKH  A;??b   /+HQK 5::<C.. 	C95J 
 $X#
###s   #B-BrY   F)preserve_module_call_signaturedisable_constraint_solver'allow_complex_guards_as_runtime_assertsrestore_fqn_log_export_usagesame_signaturefr   .r   dynamic_shapesrq  rr  rs  rt  ru  rv  c                    |rt        ddh       t        |t              s&t        t        j
                  dt        |             |xs i }t        | ||      }
t        |
|       t        j                  j                  j                  t        j                  t                    5  	 i }t!               }t        | t        j"                  j$                        st'        | ||      }|5  t)               5  t        j                  j+                  | |dd|d|||		      |i |\  }}ddd       ddd       ddd       j8                  d<   t        | t        j:                  j<                        r|rt?        | |       |S # 1 sw Y   \xY w# 1 sw Y   `xY w# t,        t.        f$ r(}t        t        j0                  t3        |            d}~wt4        $ r-}t        t        j6                  d	t3        |       d
      d}~ww xY w# 1 sw Y   xY w)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 Tsymbolic)rx  assume_static_by_defaulttracing_moderr  +prefer_deferred_runtime_asserts_over_guardsrs  ru  rv  Nz5Consider annotating your code using torch._check*(). constrain_as_size_example)	case_namero   ) r/   r   tupler
   r   INVALID_INPUTrb  r2   r1   ra   _dynamor}   patchrU   asdictDEFAULT_EXPORT_DYNAMO_CONFIGr   rb   rc   r!   ry   r   r8   r@   CONSTRAINT_VIOLATIONre   r:   ANTI_PATTERNr   r   r   r^  )rw  r   r   rx  rq  rr  rs  rt  ru  rv  combined_argsro   ctxr   r   es                   r[   rz  rz    s   $ 3<Q;RSdE"''OPTUYPZ|\
 	

 \rF!!T62M-8				#	#K$6$67S$T	U  	GI-Ca!5!56&57H  -/ $)MM$8$8#1-1!+.G AE<c&7#1 %9 % % %!  D 0AN+,!UXX__%+A~.?   " )/: 	HM>>AGG* 	**GAxP5 	7   sf   &H(?F'F
21E>#F
+F>FF

F	FH%#GH(G<<HHHc                     | S r   rY   r   s    r[   <lambda>r         rZ   )	transformr?  decomp_table_check_autograd_state_is_torch_jit_tracer0  r~   r>  r  r  r0  r~   c                
   t         j                  j                         }t               }|	r|s|r
t	               }|rt        nt
        }t         j                  j                  j                  j                  | |ddd      5  |5  t               5  t               5   |       5    |t              | |d|||      \  }}d d d        d d d        d d d        d d d        d d d        d } ||        |r		  ||       t'        || ||||||	      S # 1 sw Y   TxY w# 1 sw Y   XxY w# 1 sw Y   \xY w# 1 sw Y   `xY w# 1 sw Y   dxY w# t        t        f$ r(}t        t         j"                  t%        |            d }~ww xY w)NTtie_weightsstrictstack_weightsF)trace_jointr?  decompositionsr   c                    t        | t        j                  j                        rt	        | d      r%|j
                  j                  | j
                         t        | j                  j                        d   }t        |j                  j                        d   }|j                  dk(  r|j                  dk(  sJ t        |j
                        dk(  sJ |j
                  j                  |j
                         y y )Nr   r   r   )r   ra   rb   rc   rU  r   updatelistr   r   r   r   )old_gmnew_gmold_output_nodenew_output_nodes       r[   $_maybe_fixup_gm_and_output_node_metaz@_export_to_aten_ir.<locals>._maybe_fixup_gm_and_output_node_meta9  s    fehh223vv&""6;;/"6<<#5#56r:O"6<<#5#56r:O"%%1o6H6HH6TTT++,111  ''(<(<= 4rZ   )	r^   r=  r>  r  r?  r   r   r  r0  )ra   _Cis_grad_enabledr   rB   r   r   utils	stateless_reparametrize_modulery   r   r*   r8   r@   r
   r   r  re   rC  )r=  r   r   r  r>  produce_guards_callbackr  r?  r  r  r  r0  r~   r  grad_safe_guard#custom_triton_ops_decomposition_ctxr^   r  r  r  s                       r[   _export_to_aten_irr    s   & hh..0O!mO ;=O ' 	? ( 
	!	!	7	7 
8 
 
 
 01
 4L3M
 PsOt
 ;i(9:%'
O
 
 
 
 
 	> )b1 	H#B' "%'!/$?
 
Y
 
 
 
 
 
 
 
 
 
R )/: 	HM>>AGG	Hs   ?D?D3D'D	 D<D	D'D3D?3E DD	D$ D''D0,D33D<	8D??EF#E==Fc           
         t        j                  | j                        } |j                  | j                  }g }|j                         D ]}  \  }}|j                  |   j                  t         j                  j                  k(  r3|j                  t        |      D 	cg c]  \  }}	| d|  c}	}       m|j                  |        |r2|j                  |j                         D 
	cg c]  \  }
}	|
	 c}	}
       |S c c}	}w c c}	}
w )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.
    r   )r   rh  forwardbind_partial	argumentsr   r6  r   _ParameterKindVAR_POSITIONALextend	enumerater   )r=  r   r   r_   _argsnamesr   r  r  r   kwargs              r[   _get_forward_arg_namesr  b  s     

CKK
(CCd#--EE{{} e>>$$$(>(>(M(MMLLIe4DEDAqTF!A3-EFLL FLLN;qe;<L F
 <s   D+D
c                     t               }| j                  d      D ]G  \  }r%|j                  fd|j                  D               -|j                  |j                         I |S )zO
    Returns set of non-persistent buffers in a module and its submodules.
    Fr   c              3   ,   K   | ]  } d |   yw)r   NrY   ).0br   s     r[   	<genexpr>z._get_non_persistent_buffers.<locals>.<genexpr>  s     OATF!A3-Os   )rW   ra  r  _non_persistent_buffers_set)r=  resultrc  r   s      @r[   r:  r:    sa     uF$$e$< 9aMMO1N1NOOMM!778	9
 MrZ   orig_mod_bufferstraced_mod_buffersc                     |j                   D ]f  }|j                  t        j                  k(  s!|j                  J ||j                     }|| vsCt        j
                  |_        |||j                  <   h y)zt
    Dynamo erroneously marks tensor attributes on modules as buffers.
    Rewrite them to be tensor constants.
    N)r   r   rE   r   r   r  )r  r  r  r`   r   r  s         r[    _rewrite_dynamo_tensor_constantsr    so      ++ 	/99	(((;;***&t{{3E,, &55	).	$++&	/rZ   orig_modc                    |j                   D ]s  }|j                  t        j                  k(  s!|j                  r.|j
                  J |j
                  |vsJ | j                  |j
                        ||j
                  <   u y)z;
    Moves non-persistent buffers to tensor constants.
    N)r   r   rE   r   
persistentr   
get_buffer)r  r  r`   r   s       r[   0_move_non_persistent_buffers_to_tensor_constantsr    sr      ++ F99	(((;;***;;i///%-%8%8%EIdkk"	FrZ   graph_modulec           
         t        | gt        | j                               z         D ]  \  }}t        |t        j
                  j                        s,|j                  j                  D ]  }|j                  dv r|dk(  s|j                  j                  dd      x}	 t        d| d|j                   d      t        d |j                         D              rst        d| d|j                   d	|       |j                  d
v s|j                  j                  dd      st        d| d|j                   d        y)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.
    r   rS  r   r   NNode 	 of type z$ is missing nn_module_stack metadatac              3      K   | ]M  \  }}t        |t              xr4 t        |t              xr" t        |      d k(  xr t	        d |D               O yw)   c              3   <   K   | ]  }t        |t                y wr   )r   re   )r  r   s     r[   r  z4_verify_nn_module_stack.<locals>.<genexpr>.<genexpr>  s     >q
1c 2>s   N)r   re   r  r   all)r  kvs      r[   r  z*_verify_nn_module_stack.<locals>.<genexpr>  s^      
 !Aq	 #1c* ?&q%0?FaK?  >A>>?s   AA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  r  r   r   ra   rb   rc   r   r   r   r   r   r    r  r   )r  r  r=  r   r   s        r[   _verify_nn_module_stackr    sS    \NT,2F2F2H-IIJ 3#uxx334IIOO 	Dww776+/99==9JD+QQ 1#D6477);_`   
 %4$9$9$;  1#D6477) <LL[K\^  5599==!2D9,vYtwwi7no -	rZ   c           
         | g| j                         D ]  }t        |t        j                  j                        s(| j
                  j                  D ]  }|j                  j                  dd      }|j                  dv r2|0t        |t              rAt        d| d|j                   d|       |j                  dv sn|sqt        d| d|j                   d|         y)	z
    Perform stack trace checks on the graph.
    Constraints:
        - None or non-empty str for 'call_function', 'get_attr'
        - None for 'placeholder', 'output'
    r4  Nr  r  r  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   r   ra   rb   rc   r   r   r   r   r   re   r    )r  r=  r   r4  s       r[   _verify_stack_tracer    s     6|3356 #uxx334 &&,, 	D))--t<Kww77#+z+s/K,vYtwwi 8HHS}V  55,vYtwwi 8<<G=J 	rZ   c           
         |j                   D ci c]#  }|j                  j                  |j                  % }}| j	                         D ]  }t        |t        j                  j                        s(|j                  j                  D ]r  }|j                  dk(  s|j                  |vr"||j                     }t        |   }|j                  j                  |      rVt        d|j                   d| d|         yc c}w )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>
    r   zPlaceholder node name z does not follow spec for z, name should have prefix: N)r   rK  r   r   r   r   ra   rb   rc   r   r   r   r   r   r    )r^   r_   r   name_to_kindr=  r   	node_kindprefixs           r[   _verify_placeholder_namesr    s     :=IDHHMM499,ILIzz| #uxx334IIOO 		Dww-'99L0(3	-i8yy++F3,0;UV_U``{  }C  |D  E 		 Js   (C?epc                    d}t               }| j                  j                         D ]  }t        |t        j
                  j                        s(|j                  j                  D ]  }|j                  dk7  r|dz  }t        |j                  d      sJ t        |j                  d      sJ |j                  |j                  j                   d|j                  j                            ||dS )Nr   r   rA   rP   rO   r   )op_countop_set)rW   r  r   r   ra   rb   rc   r   r   r   rU  r   rF  rP   rO   )r  r  r  rc  r   s        r[   get_ep_statsr    s    HUF__$$& 	K!UXX112GGMM 	KDww/)MH4;;5554;;
333JJ$++0014;;3G3G2HIJ	K	K !F33rZ   _EXPORT_FLAGSrm  c                 B     t        j                          fd       }|S )Nc                  b   	 t        j                          } 	| i |}t        j                          }t        d	d||z
  t        dt        |       	 d ad a|S # t        $ r}t        |      }|j                  dz   |j                  z   }t        |      }|;t        j                  t        |             t        d|t        |      t               nt        d|t        |      t               t        |d      r%t        |j                  t         j"                         |d }~ww xY w# d ad aw xY w)
Nzexport.time)r|  metricsr}  r   zexport.error.classified)r|  rb  messager}  zexport.error.unclassifiedpartial_fx_graph)filerY   )timer/   r  r  r   rb  rP   rQ   r   logerrorr   re   rU  printr  sysstderrrm  )
r   r   startr  endr  t
error_typer  fns
            r[   wrapperz$_log_export_wrapper.<locals>.wrapper%  s)   '	,IIKET$V$B))+C #e# r"	D !M'+$	?  	QA+ann<J5a8I$		0;< 3#F'	 !5#F'	 q,-&&
 G5	8 !M'+$s%   AA 	D%"B>D  D%%D( (D.)	functoolswraps)r  r  s   ` r[   _log_export_wrapperr  $  s&    __R+ +Z NrZ   c                     t        | t        t        t        f      s| f} nAt        | t              rt        |       } n%t        | t        j
                  t        f      r|| f} |i }| |fS r   )r   r  r  rd   ra   rf   )example_inputsexample_kwarg_inputss     r[   $_process_jit_trace_inputs_for_exportr  V  sj    nudD&9:(*	ND	)~. 	>ELL$#78 ((*#!///rZ   c                     t        | j                  d            }t        | j                  d            }||z  }t        |       }|D ]  }|j	                  |d         |S r`  )rd   r   r   r:  r,  )r=  r   r   original_state_dictnon_persistent_buffersr  s         r[   _get_original_state_dictr  h  sr     C00%0HI**E*BCM*]:8=# )4() rZ   c                 (   t        |t              s&t        t        j                  dt        |             ||ni }t        j                  ||f      \  }}t        |t        j                  j                        r|j                  | ||      }||||fS )Nr~  )r   r  r
   r   r  rb  rp   tree_flattenra   r   ShapesCollectionrx  )r=  r   r   rx  r   original_in_specs         r[   _process_export_inputsr  v  s    dE"''OPTUYPZ|\
 	
 )VrF --tVn=A.%,,"?"?@'66sD&I)>99rZ   export_artifactstrict_mode_exportc                    | j                   j                  }| j                   j                  }| j                  }| j                  }| j
                  }i }	|j                         D ]-  \  }
}|st        |
      n|
}t        g g |d   |d   d      |	|<   / t        |      dkD  r/|st        |        t        |	|      |      }|J |j                  }t        J t        |||	|      }||fS )z
    In-place modify the graph module in export_artifact, remove _export_tracepoint nodes and
    return module_call_graph.
    rl   rm   Nrj  r   )rk   r^   r_   ro   rl   rm   r   r   rG   r   r   r   r  rm  rp  )r  rq  r  rf  r^   rA  ro   rl   rm   re  r[  specsmod_fqnresmodule_call_graphs                  r[   _get_module_call_graphr    s#     /3366B3B3G3G3K3K 	))  (//G(11H >@'--/ 

U*<+c"#*=)$:&"+
w'
 )*Q.!$R(T$%;=STUWX#////	    rZ   r  c                    | j                   j                  }| j                   j                  }| j                  }t	        d t        |j                        D        t        |j                              }t        |||||      }|S )Nc              3   `   K   | ]&  \  }}|j                   t        j                  k(  r| ( y wr   )r   rE   r
  )r  r  ss      r[   r  z)_get_range_constraints.<locals>.<genexpr>  s.      	
1vv--- 	
s   ,.)	rk   r^   r_   rn   r   r  r   r   r   )r  r  rx  r^   rA  rn   
num_liftedrange_constraintss           r[   _get_range_constraintsr    s      /3366B3B3G3G3K3K / 9 9I	
!"8"D"DE	

 	"../J )
 rZ   rn   c                     | j                   J | j                   j                  j                         D ci c]  \  }}t        |      r|| c}}S c c}}w r   )r   r(  r   r9   )rn   r  r  s      r[   _get_inline_constraintsr    s[    *** ''44::<Aq # 	
1  s   Aobjc              #      K   | j                   }|j                  | | j                        | _         	 d || _         y# || _         w xY ww)znHelper method to make it easier to cleanly torch.export() a method on a
    module that is not `forward`.
    N)r  __get__	__class__)r  
new_methodoriginal_methods      r[   patch_forwardr    sC      kkO $$S#--8CK& &os   .A	= A		AA	c               #     K   t         j                  j                         } t         j                  j                  d       	 d  t         j                  j                  |        y # t         j                  j                  |        w xY wwrr   )ra   r  _jit_texpr_fuser_enabled_jit_set_texpr_fuser_enabled)original_states    r[   _temp_disable_texpr_fuserr"    sV     XX668N	HH))%0>--n=--n=s   >B	A%  B	%!BB	c                   $     e Zd Z fdZd Z xZS )_WrapperModulec                 0    t         |           || _        y r   )super__init__rw  )r   rw  r  s     r[   r'  z_WrapperModule.__init__  s    rZ   c                 &     | j                   |i |S r   )rw  )r   r   r   s      r[   r  z_WrapperModule.forward  s    tvvt&v&&rZ   rO   rP   rQ   r'  r  __classcell__)r  s   @r[   r$  r$    s    'rZ   r$  c           	         t               5  ddlm} t        ||      \  }}t	        | |t
        j                  j                  f      r't        | ||dd      j                         cd d d        S t	        | t
        j                        rt	        | j                         t
        j                  j                  t
        j                  j                  f      rYt        | j                         |       5  t        | j                         ||dd      j                         cd d d        cd d d        S t        t        |       ||dd      j                         cd d d        S # 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY w)Nr   )TopLevelTracedModuleFT)r  r  )r"  torch.jit._tracer,  r  r   ra   r  ScriptModule_exportmoduleScriptMethodownerr   r   r  r$  )traced_callabler   r   r,  export_argsexport_kwargss         r[   "_convert_ts_to_export_experimentalr6    sN   	"	$ !9%I$PV%W"]o(<ehh>S>S'TU$( fh! ! ););<!!#ehh&;&;UXX__%MB
 446H #))+! (, &( #! !6 /$( fhC! !"  #! ! !s1   AE3.B E3.,E	E3-&E3E&	"E33E<r?  r  orig_in_specc
                 d    t        j                  t        |      }
t        | |||||||||	|
      S )N)r?  )r=  r   r   rx  rq  r?  r  r7  rs  r  lower_to_aten_callback)r  partialr  _strict_export_lower_to_aten_ir)r=  r   r   rx  rq  r?  r  r7  rs  r  lower_to_atens              r[   _strict_exportr=    sE     %%&8|TM*%'E!/!0W/, rZ   r9  c           
         t        | ||||d|d      }t        |||      \  }}}t        ||      }|j                  j                  D ]  }|j
                  dk(  sd|j                  vs"t        ||j                        }t        |t        j                  j                        r]|J d       |j                  |d      |j                  d<    t        |j                  j                  t        j                  j                  j                         sJ |j                  j                  j"                  j$                  x}}|J |J |j&                  t(        t*        fvrt-        j.                  t*        d |g      }|j                  j                  j"                  j0                  }t!        t3        ||j4                  |            |j                  _        |j7                          t9        |t'        |              t;        |      }t=        |       }t?        | |      }tA        |       }|jC                         D ci c]  \  }}||
 }}}|D ch c]  }||v r||    c}|_"        |5   |
|tG        |||      i ||      }d d d        jH                  }|jJ                  }|jL                  } tO        |||       tQ        tS        | jU                               tW        |jY                               ||        t[        ||       t]        | ||        t_        |||        ta        ||       tc        |||||j                  d	   
      S c c}}w c c}w # 1 sw Y   xY w)NF)rq  rt  rs  ru  rS  r   zbCannot find dynamo_fake_mode. This could be due to the exported graph module have no placeholders.Tstatic_shapes)r  r  r  r`   ro   rk   rl   rm   rn   ro   )2rz  r   r   r   r   r   r   rV  r   r   ra   r   r   from_tensor_codegenrb   r<   pytree_inform   rb  r  r  rp   r?   	orig_argsr=   _in_specrL  r   r   r   r  r:  r   r  r   r^   r_   r`   r   r  rW   r7  rd   r   r   r  r  rQ  rj   )!r=  r   r   rx  rq  r?  r  r7  rs  r  r9  r   r   r   dynamo_fake_moder  r   r\  rm   orig_out_specr   params_buffers_to_node_metar>  r   r   tracedorigreverse_name_lookupr   aten_export_artifactr^   rA  r`   s!                                    r[   r;  r;  ;  sh    )'E0W	N  	^T6:	 11A>R $$** 
77j U$))%;>4;;7DdEHHOO4$0xwx0#3#?#? $@ $		% 
 n**33UXX^^5R5RSSS
  .33<<HHQQQH} $$$ }}T5M)??5$
;#))22>>HHN$2##	
%N! ~tCy9"@"P ,C0N)B3)W
 9=<N<T<T<VWLFD4<WW +2&& 	D!2N.
 
 
5'	;O 

 
	 	 B155$..I-#R)? %S[[]+ < < >?.	   24JK 5#Y
 ^%;YG B 67!"(--.AB a X2

 
s   9MM
'MMc                     | S r   rY   r   s    r[   r  r    r  rZ   c           
      f   d }t         j                  j                  j                  j	                  | |ddd      5  t               5  t               5    ||      | |d|      \  }}	|j                  j                  D ]I  }
|
j                  dk(  st        |
j                        dk(  s,d|
j                  vs;d |
j                  d<   K t        | t         j                  j                        r1t!        | d	      r%|j                  j#                  | j                         d d d        d d d        d d d        |r		  |       t1        | |	d|||
      S # 1 sw Y   7xY w# 1 sw Y   ;xY w# 1 sw Y   ?xY w# t$        t&        f$ r(}t)        t*        j,                  t/        |            d }~ww xY w)Nc                   $% |xs i }t        | j                  d            }t        | j                  d            }i ||}t        j                  |      \  }}t        |      }t        |      }	t        |      }
t        |      }t        | ||d      }g }|j                  |       |j                  |       t        |||      \  %}t        j                  ||f      \  }}t        j                  %      %fd       }t               5  t               }t        | dd       }|t        |      }i }t!        ||      }d $t"        $fd       }|5   ||      5   t%        |dd	      | }d d d        d d d        |t'              }t)        |j*                        D ci c]  \  }}|||	|z       }}}t-        |j.                  j0                        d
   }j3                         D ]  \  }}t5        |||         }t5        ||      }|j.                  j7                  |      5  |j.                  j9                  dt:        j<                  j>                  j@                  jB                  ||f      } |jD                  | _"        d d d         jG                          d }!j.                  jI                  |!       d d d        t'              }tK        |      }"tM        t-        |      t-        |      ||d  |"t        tO        |d|	 |            t        tO        ||	|	|
z    |            i i ||d g g       }#||#fS # 1 sw Y   xY w# 1 sw Y   xY wc c}}w # 1 sw Y   zxY w# 1 sw Y   xY w)NFr   T)store_orig_modc                       t         |        S r   )r  )r   flat_fns    r[   
wrapped_fnzG_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>.wrapped_fn  s    $((rZ   r   c                    || |      }||v rt         j                  j                         rt        |t         j                        rt         j
                  j                         }|D ]  }t        |t              s|j                  }t        | |      j                  }|j                  dt         j                  j                  j                  j                  ||fi       }	t!        ||	d|        |S )a7  
                The idea here is that we override subclass getattr methods to proxy
                inner tensors and metadata. Because of infinite loop shenanigans, we have
                to manually construct the getattr proxy nodes without relying on torch function
                system.
                r   N)r  tracer)ra   r  _is_torch_function_mode_enabledr   rf   	overrides _get_current_function_mode_stackr6   rV  r4   proxycreate_proxyr   r   access_subclass_inner_tensordefaultr7   )
r   r\  original_getattrattrs_to_proxyouttorch_function_mode_stackmoderV  rZ  inner_proxys
             r[   custom_getattributezP_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>.custom_getattribute  s     'tT2>)xx??A%c5<<8 !& P P R 6 )B &#-d4P#Q-1[[F,:4,H,N,NE282E2E(7(-		(8(8(U(U(](]).(*	3&K %6(+[4PV%&& 
rZ   c              3   $  K   i }| D ]|  }t        |      }|D ]j  }||vst        ||         dkD  sJ ||   d   }t        t        |            t        t        t        j
                              z
  }|j                  |f||<   l ~ 	 |j                         D ]'  \  }\  }}t        j                  ||      }	|	|_        ) d |j                         D ]  \  }\  }}
||_         y# |j                         D ]  \  }\  }}
||_         w xY ww)z
                Context manager that temporarily monkey patches
                tensor.__getattribute__ so that we can intercept it at
                torch_function layer.
                r   )r^  r_  N)
r%   r   rW   dirra   rf   __getattribute__r   r  partialmethod)r   tensor_type_to_old_getattributerK  subclass_types_to_instancessubclass_typeinstancer_  r  old_getattrcustomr   rd  s              r[   $override_getattribute_for_subclassesza_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>.override_getattribute_for_subclasses+  s]      0   C 6c: 0 *E 	(0OO#&'B='Q#RUV#VV#V'B='QRS'TH-0X-?#c%,,FWBX-XN - > > .N;MJ		9 9>>@	4  #&!*!8!8/-8+9"
 .4*	4 /N/T/T/V 9++K-8*9/N/T/T/V 9++K-8*9s#   DA'D>C) #D)$DD)record_module_stackr?  r  r   )r   c                    | j                   dk(  r| j                  t        j                  j                  j
                  j                  t        j                  j                  j                  j                  t        j                  j                  j                  j                  t        j                  j                  j                  j                  t        j                  j                  j                  j                  fv ryy)Nr   FT)r   r   ra   r   profiler_record_function_enterr]  _record_function_enter_new_record_function_exit_RecordFunctionrk   detachr   r\  )r   s    r[   
_is_impurezG_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>._is_impures  s    77o-$++ II&&==EEII&&AAIIII&&<<LL IINN))11II$$AAIIB 3 !rZ   r   )r6  r7  user_inputsuser_outputsinputs_to_parametersinputs_to_buffersbuffers_to_mutateuser_inputs_to_mutaterl   rm   backward_signaturer8  output_tokens)(rd   r   r   rp   r  r  r   r&   r  r'   r  r  r	   r   rV  r)   r(   r   r5   r"   r  _buffersr  r   r   r   
_find_noder   r   ra   r   rk   copy_r]  r   removeeliminate_dead_coder#   r$   r  )&r=  r   r   r}  r   r   params_and_buffersparams_and_buffers_flatparams_spec	param_len
buffer_len
params_lenfunctional_callparams_buffers_argsrm   	flat_argsrl   rT  r  non_strict_rootassigned_buffershookro  r^   input_namesr  bufbuffer_input_namesoutput_noder   buf_node	name_noder   rx  output_namesr_   rd  rS  s&                                       @@r[   _make_fx_helperz3_export_to_aten_ir_make_fx.<locals>._make_fx_helper  s   2 4 4e 4 LMS...FGB 0BMB/5/B/BCU/V,"'(?"@()	'
+,
0j
 *,""#:;""4(40&
 $002Ev1NO	7		!	) 
"	) &' L	5-C%c>4@O*2?C 46 6#%5"H )9 )9V  :9E W(,!% 	  *04 #,O,D,D"E&3 Y]33&" & #288>>226!1!7!7!9 	7IC)".@.EFH *2t 4I22;? 7#%88#7#7+!IINN0088"*I!6 $8 $
 )27 7	7 & HH((4YL	5^ ),*2.,-'#JK0%!%c+a	*BDT&U!V"K	I
,BC]S !"$#
" 3wK   &7 7_L	5 L	5sc   AM)	M	L<.M	6.M)$M7A1M)(AM8M)<MM		M	M)M&!M))M2Tr  F)r  r   r   r   r   r   )r^   r=  r>  r  r?  r   r   r  )ra   r   r  r  r  ry   r   r   r   r   r   usersr   r   rb   rc   rU  r  r8   r@   r
   r   r  re   rC  )r=  r   r   r  r>  r  r  r  r^   r  r   r  s               r[   _export_to_aten_ir_make_fxr    s   AL 
	!	!	7	7 
8 
 %  % #;"<% 9i8	
O HHNN 	(D?*

Oq(*#'		% 	( c588//0WS&5IGGNN388$=% % %B 	H#B' "%'/	 	O% % % % % %H )/: 	HM>>AGG	Hsf   E-E!>EE*E9A&EE!'E-:E9 EE!!E*	&E--E69F0#F++F0c                    d}t        |      }| j                  j                  D ]  }|j                  dk(  r;||k\  r1|||z
     }t	        |t
        j                        s||j                  d<   |dz  }|j                  dk(  s]t        | |j                        }t	        |t
        j                        sd|j                  vsJ d|j                   d       |j                  |d	      |j                  d<    y )
Nr   r   r   rA   rS  zFound attribute z that has already been fakified but not yet lifted as an input. This should be impossible because (1) we should have already fakified AND lifted params/buffers (2) we should have NOT yet fakified OR lifted tensor constants. Tr?  )r+   r   r   r   r   ra   rf   r   r>   r   rB  )r^   r  num_params_buffersindexrn   r   user_argr   s           r[   r9  r9    s     E +I R77m#**$U-?%?@!(ELL9'/DIIe$QJE77j B,C#u||,DII- &t{{m 4W W- $-#8#8D#8#Q		% !RrZ   r   c                 f    t        t        fd| j                  j                  D                    S )Nc              3   B   K   | ]  }|j                   k(  s|  y wr   )r   )r  r   r   s     r[   r  z_find_node.<locals>.<genexpr>  s     Jd		T8ITJs   )r   r   r   r   )r^   r   s    `r[   r  r    s!    JbhhnnJJKKrZ   dispatch_tracing_modec                   	 |
dv sJ ddi fd}t        | ||	|      \  }}t        |       }	fd}5  t               5  t        | ||      5 \  }}}}}t	        ||      5  |
dk(  rt
        nt        j                  t        |	      } ||||||||      }|j                  j                         D ci c]  \  }}|t        |t              r||   n| c}}|_	        ddd       ddd       ddd       ddd       t        | j                  |j                         J J t        |	      S c c}}w # 1 sw Y   `xY w# 1 sw Y   dxY w# 1 sw Y   hxY w# 1 sw Y   lxY w)
u   
    ``dispatch_tracing_mode`` can be either "make_fx” or “aot_export”, corresponding to
    _export_to_aten_ir_make_fx and _export_to_aten_ir, respectively.
    )r5   
aot_exportNc                      d fd	}|S )Nc                 N   |xs i } G fddt         j                  j                        } ||       }D cg c]  }d|z   	 }}t               }t	        | t         j
                  j                        st        ||      }|5   ||fd|i|\  }	}
t        j                  d|	       d d d        t        j                  t        
j                        |
_        t        j                  t        |
j                        |
_        t        j                  t        |
j                        |
_        t        j                  t        |
j                         |
_        t        j                  t        |
j"                        |
_        	j$                  j&                  D ]r  }d|j(                  v s|j(                  d   }t        j                  t        |      j+                         D ci c]  \  }}t-        |      | c}}|j(                  d<   t |	|
fS c c}w # 1 sw Y   kxY wc c}}w )Nc                   ,     e Zd Z fdZfdZ xZS )]_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapperc                 0    t         |           || _        y r   )r&  r'  r   )r   r=  r  s     r[   r'  zf_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.__init__  s    G$&(+D%rZ   c                 D   | j                   }t        j                  ||f      \  }t        |t        j
                  j                        rt        |j                  j                         v rt        |||       t        j
                  j                  j                         5  g ||j                         }t	        j
                  j                  |      j                  | }d d d        n ||i |}t        j                        \  }t        |      S # 1 sw Y   ,xY wr   )r   rp   r  r   ra   rb   rc   r0   _forward_pre_hooksr   	tracebackpreserve_node_metaInterpreterrunr  )	r   r   r   r=  r   tree_out	flat_outsrl   rm   s	          r[   r  ze_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.forward  s     ++C!'!4!4dF^!DJAw!#uxx';';< >"55<<>? >c4P"XX//BBD L#<T#<FMMO#<D',xx';';C'@'D'Dd'KHL L $'#7#7*0*=*=h*G'Ix ++L Ls   !ADDr)  )r  rl   rm   s   @r[   Wrapperr    s    ,, ,rZ   r  z_export_root.r   z%Exported program from AOTAutograd:
%sr   )ra   r   r   r   r   rb   rc   r!   r  debugrp   tree_mapr   r6  r7  r|  r{  r}  r   r   r   r   r   )r=  r   r   r}  r  wrapped_modr  new_preserved_call_signaturesr  r^   r_   r   r   r   r   r  rl   ro   rm   rq  s                  r[   _aot_export_non_strictzL_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict  s   \rF,%((// ,: "#,K .L-()!#-) - -Cc588#7#78&!>@Q  H$[$OvOOC		BBGH $__[#..ICN //+s{{CCK$*OOKAVAV$WC!'-S55(C$ %+OOKAVAV$WC! $		1&*ii0A&BO )/')%'	4$C #3,4DII/0 s7N=-H H4s   H&H"H!Hr   rY   )r  r  rl   ro   rm   rq  s   ` r[   _tuplify_outputsz,_non_strict_export.<locals>._tuplify_outputs  s    A	 A	F &%rZ   )r  rs  c                 &    t        |       S )N)rn   r^   rx  equalities_inputsoriginal_signaturer  )r   )r^   r  rx  r  rn   r  s    r[   _produce_guards_callbackz4_non_strict_export.<locals>._produce_guards_callbackc  s"    3)/1 3
 	
rZ   r5   )r?  r  )r  r  rA  )r   r   r   r   r   r  r  r:  r  r`   r   r   r,   r  r_   rj   )r=  r   r   rx  rq  r?  r  r7  rs  r  r  r  r   r   r  r  patched_modnew_fake_argsnew_fake_kwargsnew_fake_constant_attrsmap_fake_to_real_to_aten_funcrM  r[  r  r  rn   rl   ro   r  rm   s      ``    `               @@@@@@r[   _non_strict_exportr    s   " !$====#'H"&G?AD&Z 	/0W	 1C@
 
 
 24 #CKK 	 P
# KC	 )I5 +&&&!-(;  $1#'(@*$  !5 > > D D F.C j>N.O%c*UXX. *5	 	 @ 5!%%';'E'E !+ .5	 	 	 	   sg   E=E1'E%;AE	"E1	E	:E%E1
E=E	E"E%%E.*E11E:	6E==Fr  rq  r  c                   t        |       at        | |||      \  }}}}t        |       }|rt	        j
                  t        t              nt	        j
                  t        d      } || ||||d||dd
      }	|	j                  j                  }
t        | ||      }t        |	j                        }||	j                  j                  j                  d<   t!        |	t#        | ||d      |      }t%        |	|||      \  }}t'        |       t)        |       t+        ||
       t-        ||        dd	lm} t3        ||j4                  |
|||||f|	j                  j6                  |g
	      }|S )N)r9  r5   )r  F)
r=  r   r   rx  rq  r?  r  r7  rs  r  inline_constraintsr  r   )TrainingIRVerifier	r   r   r  
state_dictr  r  r  r`   	verifiers)rd  rm  r  r  r  r:  r;  r  r  rk   r_   r  r  rn   r^   r   r  r2   r  r  r  r  r   torch._export.verifierr  rD   r   r`   )r=  r   r   rx  r  rq  r7  r  export_funcr  rA  rf  r  r  r^   r  r  exported_programs                     r[   _export_for_trainingr    s     5S9 	sD&.A 337 	 	+#=	

 "+
  "%'E/!05!O -1155.sD&A01J1JK :LO  !56.c4UC 3&	B Bb"89C(9&hh.&++f~!&&00%&
 rZ   )r  rq  r?  rs  r  c                   ddl m}	 t        |       at	               }
|
j                  |rdnd       |
j                  |rdnd       |
at        dt               t        d	d
        |r* |	       r#t        | |||||      t        dfd       S t        | |||      \  }}}}t        |       }|rt        nt        } || |||||||||
      }|j                  j                  }|st!        | ||      nd}t#        |j$                        }||j                  j&                  j(                  d<   t+        |t-        | |||      |      }t/        ||||      \  }}t1        |       t3        |       |st5        ||       t6        j8                  j:                  j=                  |d       ddlm } tC        ||        tE        ||jF                  ||||||f|j                  jH                  |g	      t        df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.

    Returns:
        An ExportedProgram containing the traced method.
    r   ) export_training_ir_rollout_checkr  
non_strictr?  aot_dispatchzexport.enterr{  r   c                       y)Nzstart!rY   rY   rZ   r[   r  z_export.<locals>.<lambda>>  s    rZ   )
payload_fnr  r  c                      t               S r   re   )r  s   r[   r  z_export.<locals>.<lambda>P  s    R rZ   Nr  r  T)in_place)Verifierr  c                      t               S r   r  )r  s   r[   r  z_export.<locals>.<lambda>  s    SAQ=R rZ   )%torch._utils_internalr  rd  rm  rW   rF  r  r/   r-   r  r  r  r=  r  rk   r_   r  r  rn   r^   r   r  r2   r  r  r  r  ra   r/  r  remove_proxy_from_state_dictr  r  r   rD   r   r`   )r=  r   r   rx  r  rq  r?  rs  r  r  r}  r  r  r  r  rA  rf  r  r  r^   r  r  r  r  s                         @@r[   r/  r/    s1   t G  5S9EE	II&hl3	IIn.AM>?h+;< 8:!+I
 	,I	 	sD&.A 337 %+.0BK!&/O 4C3G3G3K3K :MsD&1RV  11J1JK :LO  !56.c4=PQ
 3&	B B!"&<= 
MM445HSW4X/C(&hh.&++f~!&&00*
 (5RSrZ   r   )NN)r  )rU   r  r   loggingr   r  r  warnings
contextlibr   r   typingr   r   r   r   ra   torch._dynamotorch.fxtorch.utils._pytreer  _pytreerp   torch._dispatch.pythonr	   torch._dynamo.excr
   r   torch._export.db.loggingr   r   torch._export.non_strict_utilsr   r   r   r   r   r   r   -torch._export.passes.collect_tracepoints_passr   (torch._export.passes.lift_constants_passr   r   r%  r   r   r   r   r   r   r   r   r  r    torch._export.wrappersr!   4torch._functorch._aot_autograd.input_output_analysisr"   r#   &torch._functorch._aot_autograd.schemasr$   -torch._functorch._aot_autograd.subclass_utilsr%   9torch._functorch._aot_autograd.traced_function_transformsr&   $torch._functorch._aot_autograd.utilsr'   r(   torch._functorch.aot_autogradr)   r*   torch._guardsr+   "torch._library.fake_class_registryr,   torch._loggingr-   torch._subclasses.fake_tensorr.   r  r/   torch.export._unliftr0   torch.export.dynamic_shapesr1   r2   torch.export.exported_programr3   "torch.fx.experimental.proxy_tensorr4   r5   r6   r7   r&  r8   r9   r:   r;   torch.fx.graphr<   r=   torch.fx.graph_moduler>   r?   torch.utils._sympy.value_rangesr@   
_safeguardrB   r  rC   rD   rE   rF   rG   r  rH   rI   	getLoggerrO   r  	dataclassrK   r]   rj   r  criticalr  r  	exceptioninfowarningr  warnrN   ry   r   r   r   r   rb   rc   r   r   r   r   r   r   r   rd   re   r  rf   rg   r  r  r  r/  rC  rQ  r^  rd  rp  r  rS   rz  r  r  rW   r:  r  r  r  r  r  r  r  rT   rm  r  r  r  r  r  r  r  r  r"  r$  r6  r=  r;  r  r9  Noder  r  r  r/  rY   rZ   r[   <module>r     s       	 
   2 1 1    $ $ ; 6   Q	 	 	 6 3 B W + ? , 8 2 B L 4   7 + ( ; 5  V g! ' ' '  
 
 
 d#= = $=  23 MMMMLLKKOO	MM
>  : ; ; B BC(.!5!5 .0#-L:	1h XX__ 88??  
#s(^ F$,$,	$, c5<</0$, Cu||-=u?Q?QQRRS	$,
 I$,N-(-)- Cu||-=u?Q?QQRRS- 
	-6%?588#7#7 %?D %?d !%gg gT..). 
.b!XX__!5:XX5I5I!	!Huxx 4S>  .2	$$$ !&9!9:$  S	*	$
 
/$: (,MQ	E 79&+49"EE
S/E T#s(^$E U4S>5:tCy#HIJ	E %*#s(OE  $E .2E E E E XXE\ !Z "& %(,(-Z	Z
 $Z  Z Z "&Z "&Z Z@ (,	
S/ T#s(^$ 
#Y	>
UXX__ 
S 
/%,,'/S%,,.// */ Cu||-=u?Q?QQRRS	/
 
/,FhhooF)F Cu||-=u?Q?QQRRSF 
	F(%((*>*> (4 (Vehh&:&: t 4#7	04_ 4c3h 4  %)xC! (59 (4S>2 9/d0$%((// d38n :& .2	,!#,!$)#s(O,! ,!  S	*	,!
 588o!667,!^#48cN0~  &uxx & &" > >'UXX__ '"J	
S/ cN U4S>5:tCy#HIJ	
 %*#s(O  c3h  .2  8T	T
S/T cNT U4S>5:tCy#HIJ	T
 %*#s(OT T c3hT T .2T T %T Tz !	
 $ DR8L588'' Ls Luxx}} L ".f	f
S/f cNf U4S>5:tCy#HIJ	f
 %*#s(Of f c3hf f .2f f f fR  (,MQ	V 68V	V
S/V T#s(^$V U4S>5:tCy#HIJ	V V %*#s(OV V   Vr  (,MQ	a 6849 %a	a
S/a T#s(^$a U4S>5:tCy#HIJ	a a %*#s(Oa a .2a a a   arZ   