
    IЦi1                       % S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKJrJ	r	  S SK
JrJrJrJrJrJrJrJr  S SKrS SKrS SKrS SKJs  Jr  S SKJr  S SKJrJr  S SKJ r J!r!  S SK"J#r#J$r$J%r%J&r&J'r'J(r(  S SK)J*r*  S S	K+J,r,J-r-J.r.  S S
K/J0r0J1r1J2r2J3r3J4r4J5r5  S SK6J7r7  S SK8J9r9  S SK:J;r;J<r<  S SK=J>r>  S SK?J@r@JArA  S SKBJCrCJDrD  S SKEJFrF  S SKGJHrH  S SKIJJrJJKrK  S SKLJMrM  S SKNJOrO  S SKPJQrQJRrR  S SKSJTrT  S SKUJVrV  S SKWJXrXJYrYJZrZJ[r[  S SK\J]r]J^r^  S SK_J`r`  S SKJara  S SKbJcrc  SSKdJere  SS KfJgrgJhrhJiriJjrjJkrk  SS!KlJmrmJnrn  \R                  " \p5      rq\ R                   " S" S#5      5       rs\ R                   " S$ S%5      5       rt\ R                  " S&S'9 " S( S)5      5       ru\s" 5       rv\R                  \R                  \R                  \R                  \R                  \R                  \R                  \}\R                  1	\vl        \S* 5       rS+ rS, rS-\GR                  GR                  4S. jrS/ rS0 rS1 rS2 rS3\GR                  GR                  S4\GR                  GR                  S5\\\4   4S6 jrS-\GR                  GR                  S7\nS8\\\GR                  4   S9\\\\GR                  \H\GR                  4   4   S:\\   4
S; jrS<\,S=\nS9\\\\GR                  \H\GR                  4   4   S5S4S> jrS-\GR                  GR                  S5\t4S? jrS-\GR                  GR                  S=\nS5S4S@ jrS3\GR                  GR                  S4\GR                  GR                  S5S4SA jrSB\GR                  GR                  S5\\\4   4SC jr SSD\aSE\aSF\\\k4   SG\\\      S5\\j   4
SH jjr  SSISJSJS&S&S&SK.SL\SM\\SN4   SO\\\\4      SP\\\\\4   \\   \\   4      SQ\\SN4   SR\SS\ST\SU\SV\S5\GR                  GR                  4SW jjjr SSX SJSS&SJSY.SB\GR                  GR                  SZ\,S[\S\\S5\t4
S] jjjrS^\KSB\GR                  GR                  S5\\\\GR                  \GR                  GR4                  4   4   4S_ jr SSB\GR                  GR                  SM\\SN4   SO\\\\4      S5\\   4S` jjrSB\GR                  GR                  S5\\   4Sa jrSb\\GR                     Sc\\\GR                  4   S=\nS9\\\\GR                  \H\GR                  4   4   S5S4
Sd jrSe\GR                  GR                  S=\nS9\\\\GR                  \H\GR                  4   4   S5S4Sf jrSg\GR                  GR                  S5S4Sh jrSg\GR                  GR                  S5S4Si jrS-\GR                  GR                  S7\nS5S4Sj jrSk\hS5\\\4   4Sl jrSq\\\      \Sm'   Sq\\\\4      \Sn'   So rSp rSq r SSr\uSs\aSQ\\SN4   St\SG\\\      S5\\GR                  GR                  \\j   4   4Su jjrSr\uSv\\\4   4Sw jrS^\K4Sx jr\Sy\GR                  GR                  4Sz j5       r\S{ 5       r " S| S}\GR                  GR                  5      rSS~ jrSB\GR                  GR                  SM\\SN4   SO\\\4   SP\\\\\4   \\   \\   4      SQ\\SN4   S\S\\\4   S\aSS\S\\S5\u4S jrSB\GR                  GR                  SM\\SN4   SO\\\4   SP\\\\\4   \\   \\   4      SQ\\SN4   S\S\\\4   S\aSS\S\\S\S5\u4S jrSS 4SB\GR                  GR                  SZ\,S5\t4S jjrS rS-\GR                  GR                  S\S5\GR                  GRj                  4S jr SSB\GR                  GR                  SM\\SN4   SO\\\4   SP\\\\\4   \\   \\   4      SQ\\SN4   S\S\\\4   S\aSS\S\\S\S5\u4S jjr\\g  SS&SIS.SB\GR                  GR                  SM\\SN4   SO\\\\4      SP\\\\\4   \\   \\   4      S\SQ\\SN4   S5\h4S jjj5       5       r\\g  SS&SISJSJSJS.SB\GR                  GR                  SM\\SN4   SO\\\\4      SP\\\\\4   \\   \\   4      S\SQ\\SN4   S\SS\S\\S5\h4S jjj5       5       rg)    N)contextmanagernullcontext)AnyCallableDictListOptionalSetTupleUnion)enable_python_dispatcher)	UserErrorUserErrorType)exportdb_error_messageget_class_if_classified_error)_fakify_script_objects_gather_constant_attrs_NonStrictTorchFunctionHandlermake_constraintsmake_fake_inputs$produce_guards_and_solve_constraints)CollectTracepointsPass)ConstantAttrMaplift_constants_passrewrite_script_object_meta)_collect_param_buffer_metadata)_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)create_functional_call)create_tree_flattened_fnregister_buffer_assignment_hook)_detect_attribute_assignmentaot_export_module)detect_fake_mode)FakeScriptObject)
FakeTensorFakeTensorMode)log_export_usage)!_check_input_constraints_pre_hook)_check_dynamic_shapes_combine_args)
OutputKind)make_fx)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                   \    \ rS rSr% SrSr\\S'   \R                  " \
S9r\\   \S'   SrSrg)	ExportDynamoConfig_   z2
Manage Export-specific configurations of Dynamo.
T	allow_rnn)default_factoryreorderable_logging_functions N)__name__
__module____qualname____firstlineno____doc__rJ   bool__annotations__dataclassesfieldsetrL   r
   r   do_not_emit_runtime_asserts__static_attributes__rM       R/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/export/_trace.pyrH   rH   _   s:     It3>3D3D4!3x=  #'rZ   rH   c                       \ rS rSr% \R
                  R                  \S'   \\S'   \	\
\\R                  \\R                  4   4   \S'   Srg)ATenExportArtifactp   gmsig	constantsrM   N)rN   rO   rP   rQ   torchfxGraphModulerT   rF   r   strr   Tensorr,   ScriptObjectrY   rM   rZ   r[   r]   r]   p   sM    	LL 	
	 rZ   r]   T)frozenc                   f    \ rS rSr% \\S'   \\S'   \\S'   \\	\\	\
R                  4   4   \S'   Srg)ExportArtifact~   atenout_spec	fake_modemodule_call_specsrM   N)rN   rO   rP   rQ   r]   rT   r<   r.   r   re   pytreerY   rM   rZ   r[   rj   rj   ~   s2    
Cc6??&:!;;<<rZ   rj   c               #     #    [         R                  R                  R                  S5      n [         R                  R                  R                  S5      n S v   [         R                  R                  R                  " U 6   [         R                  R                  R                  " U6   g ! [         R                  R                  R                  " U 6   [         R                  R                  R                  " U6   f = f7fNF)rb   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C<B) AC<)AC99C<c                     S[        U 5      -   $ )N	L__self__)_strip_rootxs    r[   
_fixup_keyr      s    Q''rZ   c                     [        U [        5      (       aA  U R                  S5      (       a+  U [        S5      S  nUR                  S5      (       a  USS  $ U$ U $ )N_export_root.r>   )
isinstancere   
startswithlen)r~   strippeds     r[   r|   r|      sU    !Sall>::S(*+'22377x|EXEHrZ   r_   c           
         U R                   R                   GH  nUR                  [        R                  R
                  R                  :X  d  M8  SUR                  ;   d  MJ  [        UR                  S   5      nU R                   R                  U5         U R                   R                  S[        R                  R
                  R                  UR                  UUR                  S   S.S9nUR                  Ul        UR                  U5        U R                   R                  U5        SSS5        GM     g! , (       d  f       GM1  = f)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targetrb   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''- 54	  54s   BD88
E	c                   ^^ / m/ nU R                   R                   H  nUR                  S:X  aX  SUR                  ;   aH  UR                  S   nUb4  [	        U[
        R                  5      (       a  TR                  U5        Mg  Mi  Mk  SUR                  ;   d  M}  UR                  S   nUc  M  [	        U[
        R                  5      (       d  M  UR                  U5        M     [        TU-   5      =n(       a  UnO[        [        5       SS9nSmUU4S jn[        R                  " [
        R                  X5      n	[        R                  " [
        R                  X5      n
XU4$ )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                    > TT   nTS-  mU$ )Nr>   rM   )r~   r   count	fake_inpss     r[   lookup_fake)_extract_fake_inputs.<locals>.lookup_fake   s    

rZ   )r   r   opr   r   rb   rf   appendr+   r.   r8   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(J  * )K#		)yy1H#
8U\\(J(J  *  .i).CDDD&	"XZE	E $$U\\;EI&&u||[IK9,,rZ   c                 \   UR                    HF  nUR                  [        R                  [        R                  4;   d  M3  XR
                     Ul        MH     UR                   HF  nUR                  [        R                  [        R                  4;   d  M3  XR
                     Ul        MH     g N)
input_specsr   rB   	PARAMETERBUFFERr   output_specsr3   BUFFER_MUTATIONGRADIENT_TO_PARAMETER)param_buffer_tabler`   specs      r[   _replace_param_buffer_namesr      s    99
 
 -[[9DK     99&&,,
 
 -[[9DK !rZ   c           	          [        U 5      [        U5      [        U5      -   :X  d,   S[        U 5       S[        U5       S[        U5       S35       eU [        U5      S   Vs/ s H  o2U   PM	     nn/ UQUQ7$ s  snf )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S7RGw7RS		  Ts   !A8c                 h   Sn[         R                  " SSU5      nU R                  5        GH|  n[        U[        R
                  R                  5      (       d  M/  UR                  R                   GH2  nUR                  S;   a  M  SnUR                  R                  S0 5      =n(       a  [        [        UR                  5       5      5      u  p[        R                   " U	5      (       a5  [#        U	[        R$                  R&                  5      (       a  X:X  a  XL a  SnO[        U	[(        5      (       d   eU(       d  M  S n
X2UR*                  S	-   UR,                  -   40UEnUR/                  5        VVV	s0 s H  u  nu  pX" U5      U	4_M     sn	nnUR                  S'   GM5     GM     g s  sn	nnf )
Nz	L['self']z[^a-zA-Z0-9]_r   outputTnn_module_stackFc                    ^  / m " U4S jS5      n[        U SSU" 5       005        SR                  T5      $ ! [         a    U s $ f = f)Nc                   .   > \ rS rSrU 4S jrU 4S jrSrg)@_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Pathi  c                 *   > TR                  U5        U $ r   )r   )selfnamepartss     r[   __getattr__L_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getattr__  s     %T 2'+rZ   c                 <   > TR                  [        U5      5        U $ r   )r   re   )r   idxr   s     r[   __getitem__L_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getitem__#  s     %SX 6'+rZ   rM   N)rN   rO   rP   rQ   r   r   rY   )r   s   r[   Pathr     s    ,, ,rZ   r   Lr   r   )evaljoin	Exception)r   r   r   s     @r[   normalize_path2_normalize_nn_module_stack.<locals>.normalize_path  sQ    $ ", , TC&$&)9#:;"xx.$ $#$s   48 AAr   )resubmodulesr   rb   rc   rd   r   r   r   r   getnextitervaluesinspectisclass
issubclassnnModulere   rO   rP   items)gm_torch_levelroot_clsrootroot_keyr_   r   add_rootr   r   tyr   keys               r[   _normalize_nn_module_stackr     sp   DvvosD1H$$&"ehh2233HHNNDww33H"&))--0A2"FFF_%;%;%= >? ??2&&:b%((//+J+J|#(%b#....x$& X%8%83%>AVAV%VW#%# ,;+@+@+B0+BZd ..33+B0		+,M # 'R0s   7F-original_moduletraced_modulereturnc                    0 n0 nU R                  SS9 H  u  pEXB[        U5      '   M     U R                  SS9 H  u  pFXC[        U5      '   M     0 nUR                  SS9 H-  u  pX;  d   e[        U	5      U;   d  M  U[        U	5         Xx'   M/     UR                  SS9 H-  u  pX;  d   e[        U
5      U;   d  M  U[        U
5         Xx'   M/     U$ )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   6  s    $&L$&M&777O"&RY P'55u5M$(bj! N *,%2%C%C &D &! 444l|+.:2l;K.L+& '4&A&A 'B '" 444m-.;B}<M.N+' rZ   r`   fake_params_buffersra   flat_fake_argsc                    U R                   R                   Vs/ s H  oUR                  S:X  d  M  UPM     nn[        UR                  5      [        U5      :X  d   eSn[        XaR                  5       GH  u  pXUR                  [        R                  [        R                  4;   a<  UR                  c   eUUR                     R                  UR                  S   l        Mp  UR                  [        R                  :X  aH  XG   n	[        U	[        R                   5      (       a  U	R                  UR                  S   l        US-  nM  UR                  [        R"                  :X  a  UR                  c   eX8R                     n
[        U
[        R                   5      (       ak  UR                  S   R$                  (       a!  U
R                  UR                  S   l        GMo  UR                  S   R                  U
R                  :X  d   eGM  GM  UR                  [        R&                  [        R(                  4;   a  GM  [+        UR                  5      e   g s  snf )Nr   r   r   r>   )r   r   r   r   r   zipr   rB   r   r   r   requires_gradr   
USER_INPUTr   rb   rf   CONSTANT_TENSORis_leaf
CUSTOM_OBJTOKENAssertionError)r_   r`   r   ra   r   r   placeholdersir   fake_argconstants              r[   _preserve_requires_grad_passr  Y  s    &(XX^^P^Tww-7OD^LPs3|#4444	A,8
99
 
 ;;***-@.m IIe* YY)...%(H(ELL11191G1G		% .FAYY)333;;*** -H(ELL1199U#++5=5K5KDIIe$299U+99X=S=SSSS 2 YY9//AA ++5 9 Qs
   IIorig_constant_attrsgraph_signaturec                 Z   0 nUR                  5        H  u  pEXP;   d  M  X   X4'   M     UR                   Hs  nUR                  [        R                  [        R
                  4;   d  M3  UR                  nUc   eUR                  Xw/5      nUS   Ul        X'   n	X'	 U H  n
XU
'   M	     Mu     g)zXRewrite the graph signature and constants table to use the FQN from the original module.Nr   )r   r   r   rB   r  r  r   r   )r  r  ra   remap_tabler   valuer   orig_targettargetsr  r   s              r[   _remap_constantsr    s     )+K (' 3 :K )  ++99%%  
 
 ++K***!ook=AG!!*DK -H&!$,&! " ,rZ   c           	      Z   [         R                  " XV45      n[        X5      u  p[        UR                  5      [        UR
                  5      -   [        UR                  5      -   n	[        XU	5        [        X0[        U5      5      n
[        U 5      nUR                  [        X
U5      5        U(       a   SSKJn  SSKJn  U" X
5      u  p
U" X
5      u  p
U R#                  5        H  n[%        U[&        R(                  R*                  5      (       d  M.  UR,                  R.                   HM  nUR0                  S;   d  M  UR2                  R5                  SS5        UR2                  R5                  SS5        MO     M     U
c   e[7        U U
UUUUU5        [9        X
X{U5        [;        U U
U5      $ )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   
parametersbuffersinput_tokensset_missing_meta_valsrE   _get_non_persistent_buffersr   updater   3torch._export.passes.replace_autocast_with_hop_passr  3torch._export.passes.replace_set_grad_with_hop_passr  r   r   rb   rc   rd   r   r   r   r   popr    r  r]   )r_   modconstant_attrsr  pre_dispatchr   r   r   r   total_non_user_inputsexport_graph_signaturera   r  r  _modr   s                   r[   _produce_aten_artifactr*    s   2 ''(@AN6rKB 	O&&'
o%%
&	'
o**
+	, 
 ".CD @8= +2.I(^TU	
	
 &D&
" &D&
"
 

$ 4 455JJ$$Dww33		/6		mT2 %  "---
 !
$7N 
 rZ   c                   ^
 U R                   R                   Vs1 s H  o"R                  iM     snm
U
4S jn[        [        R
                     n[        [        R                     n0 nUR                   H  nUR                  [        R                  :X  d  M#  UR                  R                  R                  U5      (       a  MO  UR                  R                  R                  U5      (       a+  U" XWR                  R                  [        U5      S -   5      nOU" XWR                  R                  -   5      nXUR                  R                  '   XR                  l        M     UR                   HF  nUR                  R                  U;   d  M  XgR                  R                     UR                  l        MH     U R                  5        H  n	[        U	[        R                   R"                  5      (       d  M.  U	R                   R                   H/  nUR                  U;   d  M  XbR                     =Ul        Ul        M1     U	R'                  5         M     gs  snf )zT
For strict mode, rename constants nodes that were previously annotated as buffers.
c                    > U T;   a%  SnU  SU 3=nT;   a  US-  nU  SU 3=nT;   a  M  Un TR                  U 5        U $ )Nr>   r   )add)r   ndup_name
node_namess      r[   rename_constant0_rename_constants_nodes.<locals>.rename_constant  s_    :A"&q},8;Q #'q},8;DtrZ   N)r   r   r   r!   rB   r   r  r   r   argr   r   r   r   r   rb   rc   rd   r   	recompile)r_   r  r   r1  buffer_prefixconst_prefixbuffer_to_constantr   c_namer$  r0  s             @r[   _rename_constants_nodesr9    s    )+7))7J ))9)9:M'	(A(ABL++99	111$((--:R:R;
 ;
 xx}}''66( 88==]1C1E#FF ))EF06txx}}-"HHM ,  ,,88==...xx}}=DHHM -
 zz|#uxx3344IIOODyy..*<YY*GG	DK $ 	 A 8s   Ic                 x   [        X5      nUR                  5        H  u  p4UR                  SS5      X#'   M     UR                  5        H  u  p4[        X5      (       d  M  [	        X5      n[        U[        R                  5      (       a;  [        U[        R                  R                  5      (       d  UR                  XE5        O[        XU5        [        X5        M     UR                  R                   H1  nUR                  S:X  d  M  UR                   nXr;   d  M(  X'   Ul        M3     UR#                  5         g)zN
Restores the state dict of the traced module to that of the original module.
r   r   get_attrN)r   r   replacehasattrgetattrr   rb   rf   r   	Parameterregister_buffersetattrdelattrr   r   r   r   r4  )r   r   r   r   fqnattrr   	attr_names           r[   _restore_state_dictrF  .  s     3?R
 (--/	#&;;sC#8  0 (--/	}++}+dELL))*T588CUCU2V2V))#4M-$ 0 ##))77j I.0;	 * rZ   r$  c                 ~    U R                  SS9 VVs0 s H  u  pU[        U5      R                  _M     snn$ s  snnf )NFr   )named_modulestyperN   )r$  r   ms      r[   _get_module_hierarchyrK  R  sD    .1.?.?QV.?.W.W74d1g.W  s   "9in_specrm   module_call_signaturesforward_arg_namesc           	      >   [          Vs/ s H  n[        XBR                  U5      S9PM     nnUS   R                  S:X  d   e[	        / / U UUS9US   l        UR                  5        VVs/ s H  u  pFU[         ;  d  M  [        XFS9PM     nnn/ UQUQ$ s  snf s  snnf )N)rC  	signaturer    inputsoutputsrL  rm   rN  )_EXPORT_MODULE_HIERARCHYrC   r   rC  rD   rP  r   )rL  rm   rM  rN  rC  originalrP  
additionals           r[   _make_module_call_graphrX  X  s     ,+C 	C+E+Ec+JK+   A;??b   /+HQK 5::<<NC.. 	6C5<  
 $X#
###s   "B*B>BrM   F)preserve_module_call_signaturedisable_constraint_solver'allow_complex_guards_as_runtime_assertsrestore_fqn_log_export_usagesame_signaturefr   .r   dynamic_shapesrY  rZ  r[  r\  r]  r^  c                   U(       a  [        SS1S9  [        U[        5      (       d&  [        [        R
                  S[        U5       35      eU=(       d    0 n[        XU5      n
[        X5        [        R                  R                  R                  [        R                  " [        5      5          0 n[!        XU5         [#        5          [        R                  R%                  U USSUSUUU	S9	" U0 UD6u  pSSS5        SSS5        SSS5        WWR2                  S'   [        U [        R4                  R6                  5      (       a  U(       a  [9        X5        U$ ! , (       d  f       Nj= f! , (       d  f       Ns= f! [&        [(        4 a(  n[        [        R*                  [-        U5      5      eSnAf[.         a*  n[        [        R0                  S	[-        U5       3S
S9eSnAff = f! , (       d  f       N= f)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)r`  assume_static_by_defaulttracing_moderZ  +prefer_deferred_runtime_asserts_over_guardsr[  r]  r^  Nz5Consider annotating your code using torch._check*(). constrain_as_size_example)	case_namero   )r/   r   tupler   r   INVALID_INPUTrI  r2   r1   rb   _dynamoconfigpatchrU   asdictDEFAULT_EXPORT_DYNAMO_CONFIGr#   ry   r   r5   r=   CONSTRAINT_VIOLATIONre   r7   ANTI_PATTERNr   r   r   rF  )r_  r   r   r`  rY  rZ  r[  r\  r]  r^  combined_argsro   r   r   es                  r[   rb  rb  r  s   $ 3<Q;RSdE""''OPTUYPZ|\
 	

 \rF!!62M-8				#	#K$6$67S$T	U	GI!3D&($)MM$8$8#1-1!+.G AE<c&7#1 %9 % % %! ) 
V> 0AN+,!UXX__%%+A.? )( & )/: 	HM>>AGG* 	**GAxP5 	1 
V	Usr   1G-3F E/.E:E/F 
E,(E//
E=	9F <G-=F  G*#F33G* %G%%G**G--
G;c                     U $ r   rM   r}   s    r[   <lambda>ry        rZ   )	transformr&  decomp_table_check_autograd_state_is_torch_jit_tracer%  r}  r~  c                   [         R                  R                  5       n[        5       nU	(       a  U(       d  U(       a
  [	        5       n[
        S 5       n[         R                  R                  R                  R                  U USSSS9   U   [        5          U" 5          U" [        5      " U USUUUS9u  pS S S 5        S S S 5        S S S 5        S S S 5        S nU" U W5        U(       a	   U" U5        [%        UU UWUUUUS9$ ! , (       d  f       NR= f! , (       d  f       N[= f! , (       d  f       Nd= f! , (       d  f       Nm= f! [        [        4 a(  n[        [        R                   [#        U5      5      eS nAff = f)Nc               3      #    [         R                  R                  n  S[         R                  l        S v   U [         R                  l        g ! U [         R                  l        f = f7fNTrb   compiler_is_compiling_flag	old_values    r[   _compiling_state_context4_export_to_aten_ir.<locals>._compiling_state_context  @     NN55		:04ENN-09ENN-	ENN-   A'A A'A$$A'Ttie_weightsstrictstack_weightsF)trace_jointr&  decompositionsr   c                    [        U [        R                  R                  5      (       a  [	        U S5      (       a%  UR
                  R                  U R
                  5        [        U R                  R                  5      S   n[        UR                  R                  5      S   nUR                  S:X  a  UR                  S:X  d   e[        UR
                  5      S:X  d   eUR
                  R                  UR
                  5        g g )Nr   r   r   )r   rb   rc   rd   r=  r   r   listr   r   r   r   )old_gmnew_gmold_output_nodenew_output_nodes       r[   $_maybe_fixup_gm_and_output_node_meta@_export_to_aten_ir.<locals>._maybe_fixup_gm_and_output_node_meta  s    fehh223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   )rb   _Cis_grad_enabledr   r?   r   r   utils	stateless_reparametrize_modulery   r*   r5   r=   r   r   rt  re   r*  )r$  r   r   r   r%  produce_guards_callbackr{  r&  r|  r}  r~  r  grad_safe_guardr  r_   r  r  rw  s                     r[   _export_to_aten_irr    sV   " hh..0O!mO ;=O: : 
	!	!	7	7 
8 
 /13K3M'(9:%'
 4N1
 	> )b1 	H#B' "%'!/	 	M 4N3M11
 
R )/: 	HM>>AGG	Hsl   	D6D%DD	7D?D%D6+E 
DD
D"D%%
D3	/D66
EE?#E::E?rn   c                 "   0 [        UR                  SS95      E[        UR                  SS95      En0 n0 nUR                  5        HD  u  pV[	        U5      U;   a  U[	        U5         nOU R                  USS9nXt[	        U5      '   XsU'   MF     U$ )NFr   Tstatic_shapes)dictr   r   r   r   from_tensor)rn   r$  params_buffersfaked_params_buffersmemor   r  fake_tensors           r[   _fakify_params_buffersr    s    
s##U#;
<
s  % 8
9N
 "$D$**,
e9r%y/K#//T/JK)EO$/S! -  rZ   c           
      4   [         R                  " U R                  5      nUR                  " U6 R                  n/ nUR                  5        H  u  pgUR                  U   R                  [         R                  R                  :X  a6  UR                  [        U5       VV	s/ s H  u  pU SU 3PM     sn	n5        Mp  UR                  U5        M     U(       a4  UR                  UR                  5        V
V	s/ s H  u  pU
PM	     sn	n
5        U$ s  sn	nf s  sn	n
f )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   rP  forwardbind_partial	argumentsr   r  r   _ParameterKindVAR_POSITIONALextend	enumerater   )r$  r   r   r`   _argsnamesr   r  r
  r   kwargs              r[   _get_forward_arg_namesr  *  s     

CKK
(Cd#--EE{{}>>$$$(>(>(M(MMLLIe4DE4DDATF!A3-4DEFLL % FLLN;NeN;<L F
 <s   D6D
c                     [        5       nU R                  SS9 H7  u  p#UR                   H"  nUR                  U(       a  U SU 3OU5        M$     M9     U$ )zG
Returns set of non-persistent buffers in a module and its submodules.
Fr   r   )rW   rH  _non_persistent_buffers_setr-  )r$  resultr   rJ  bs        r[   r  r  I  sV     UF$$e$<..AJJ$q}!4 / = MrZ   orig_mod_bufferstraced_mod_buffersc                     UR                    Hj  nUR                  [        R                  :X  d  M#  UR                  c   eXR                     nXP;  d  MG  [        R
                  Ul        XSUR                  '   Ml     g)zh
Dynamo erroneously marks tensor attributes on modules as buffers.
Rewrite them to be tensor constants.
N)r   r   rB   r   r   r  )r  r  r  ra   r   r  s         r[    _rewrite_dynamo_tensor_constantsr  T  sc      ++99	(((;;***&{{3E, &55	).$++& ,rZ   orig_modc                     UR                    H~  nUR                  [        R                  :X  d  M#  UR                  (       a  M6  UR
                  c   eUR
                  U;  d   eU R                  UR
                  5      X#R
                  '   M     g)z3
Moves non-persistent buffers to tensor constants.
N)r   r   rB   r   
persistentr   
get_buffer)r  r  ra   r   s       r[   0_move_non_persistent_buffers_to_tensor_constantsr  j  sk      ++99	(((;;***;;i///%-%8%8%EIkk"	 ,rZ   graph_modulec           
         [        U /[        U R                  5       5      -   5       GH6  u  p[        U[        R
                  R                  5      (       d  M1  UR                  R                   H  nUR                  S;   a  US:X  a  UR                  R                  SS5      =n c  [        SU SUR                   S35      e[        S UR                  5        5       5      (       d  [        SU SUR                   S	U 35      eM  M  UR                  S
;   d  M  UR                  R                  SS5      (       d  M  [        SU SUR                   S35      e   GM9     g)af  
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   r;  r   r   NNode 	 of type z$ is missing nn_module_stack metadatac              3      #    U  H\  u  p[        U[        5      =(       a>    [        U[        5      =(       a'    [        U5      S :H  =(       a    [	        S U 5       5      v   M^     g7f)   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   re   ).0r~   s     r[   	<genexpr>4_verify_nn_module_stack.<locals>.<genexpr>.<genexpr>  s     >Aq
1c 2 2As   N)r   re   rm  r   all)r  kvs      r[   r  *_verify_nn_module_stack.<locals>.<genexpr>  s]      
 %<DA	 #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   rb   rc   rd   r   r   r   r   r   r"   r  r   )r  r
  r$  r   r   s        r[   _verify_nn_module_stackr  y  s]    \NT,2F2F2H-IIJ#uxx3344IIOOD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9,vYtwwi7no - $ KrZ   c           
         U /U R                  5       Q H  n[        U[        R                  R                  5      (       d  M.  U R
                  R                   H  nUR                  R                  SS5      nUR                  S;   a:  Ub5  [        U[        5      (       d  [        SU SUR                   SU 35      eMg  Mi  UR                  S;   d  M{  U(       d  M  [        SU SUR                   SU 35      e   M     g)	z
Perform stack trace checks on the graph.
Constraints:
    - None or non-empty str for 'call_function', 'get_attr'
    - None for 'placeholder', 'output'
r  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   rb   rc   rd   r   r   r   r   r   re   r"   )r  r$  r   r  s       r[   _verify_stack_tracer    s     6|3356#uxx3344 &&,,D))--t<Kww77#+z+s/K/K,vYtwwi 8HHS}V  0L+
 55;,vYtwwi 8<<G=J  - 7rZ   c           
      2   UR                    Vs0 s H#  o"R                  R                  UR                  _M%     nnU R	                  5        H  n[        U[        R                  R                  5      (       d  M.  UR                  R                   H|  nUR                  S:X  d  M  UR                  U;  a  M'  X5R                     n[        U   nUR                  R                  U5      (       a  M`  [        SUR                   SU SU 35      e   M     gs  snf )z
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   r3  r   r   r   r   rb   rc   rd   r   r   r   r!   r   r"   )r_   r`   r   name_to_kindr$  r   	node_kindprefixs           r[   _verify_placeholder_namesr    s     :=IHHMM499,LIzz|#uxx3344IIOODww-'99L0(3	-i8yy++F33,0;UV_U``{  }C  |D  E  $  Js   *Depc                    Sn[        5       nU R                  R                  5        H  n[        U[        R
                  R                  5      (       d  M.  UR                  R                   H  nUR                  S:w  a  M  US-  n[        UR                  S5      (       d   e[        UR                  S5      (       d   eUR                  UR                  R                   SUR                  R                   35        M     M     XS.$ )Nr   r   r>   rO   rN   r   )op_countop_set)rW   r  r   r   rb   rc   rd   r   r   r   r=  r   r-  rO   rN   )r  r  r  rJ  r   s        r[   get_ep_statsr    s    HUF__$$&!UXX1122GGMMDww/)MH4;;55554;;
3333JJ$++0014;;3G3G2HIJ " ' !33rZ   _EXPORT_FLAGSrU  c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                    >  [         R                   " 5       nT	" U 0 UD6n[         R                   " 5       n[        SSXB-
  [        S.[        U5      D6   S qS qU$ ! [         a  n[        U5      nUR                  S-   UR                  -   n[        U5      nUb9  [        R                  [        U5      5        [        SU[        U5      [        S9  Ue[        SU[        U5      [        S9  UeS nAff = f! S qS qf = f)Nzexport.time)rd  metricsre  r   zexport.error.classified)rd  rI  messagere  zexport.error.unclassifiedrM   )timer/   r  r  r   rI  rO   rP   r   logerrorr   re   rU  )
r   r   startr  endrw  t
error_typerl  fns
            r[   wrapper$_log_export_wrapper.<locals>.wrapper  s    	,IIKET$V$B))+C ## r"	6 !M'+$	1  	QA+ann<J5a8I$		0;< 3#F'	 G !5#F'	 G'	* !M'+$s%   AA 
C2$B	C--C22C5 5C;)	functoolswraps)r  r  s   ` r[   _log_export_wrapperr    s&    __R$ $L NrZ   c                     [        U [        [        [        45      (       d  U 4n OL[        U [        5      (       a  [        U 5      n O+[        U [        R
                  [        45      (       a  Uc  U 4n Uc  0 nX4$ r   )r   rm  r  r  rb   rf   )example_inputsexample_kwarg_inputss     r[   $_process_jit_trace_inputs_for_exportr    sp    nudD&9::(*	ND	)	)~. 	>ELL$#788 ((*#!//rZ   c                    [        U R                  SS95      n[        U R                  SS95      nXE-  n[        U 5      nU H  nUR	                  US 5        M     [        U[        5      (       d&  [        [        R                  S[        U5       35      eUb  UO0 n[        R                  " X45      u  p[        U[        R                  R                  5      (       a  UR!                  XU5      nXXU4$ )NFr   rf  )r  r   r   r  r#  r   rm  r   r   rn  rI  rp   tree_flattenrb   r   ShapesCollectionr`  )r$  r   r   r`  r   r   original_state_dictnon_persistent_buffersr  r   original_in_specs              r[   _process_export_inputsr  '  s     C00%0HI**E*BCM*:8=#4( $ dE""''OPTUYPZ|\
 	
 )VrF --tn=A.%,,"?"?@@'66s&I)NNrZ   export_artifactr  strict_mode_exportc           	         U R                   R                  nU R                   R                  nU R                  nU R                  n0 n	UR                  5        H.  u  pU(       d  [        U
5      OU
n[        / / US   US   SS9X'   M0     [        U5      S:  a4  U(       d  [        U5        [        X5      " U5      nUc   eUR                  n[        c   e[        UUU	U5      nX^4$ )zt
In-place modify the graph module in export_artifact, remove _export_tracepoint nodes and
return module_call_graph.
rL  rm   NrR  r   )rl   r_   r`   ro   rm   r   r|   rD   r   r   r   r  rU  rX  )r  r  rY  r   rN  r_   r(  ro   rm   rM  rC  specsmod_fqnresmodule_call_graphs                  r[   _get_module_call_graphr  @  s     /3366B3B3G3G3K3K 	))  )11H >@'--/
*<+c"#*=)$:&"+
' 0 )*Q.!$R($%;TUWX#////	   rZ   rv  c                    U R                   R                  nU R                   R                  nU R                  n[	        S [        UR                  5       5       [        UR                  5      5      n[        UUUUU5      nU$ )Nc              3   j   #    U  H)  u  pUR                   [        R                  :X  d  M%  Uv   M+     g 7fr   )r   rB   r  )r  r
  ss      r[   r  )_get_range_constraints.<locals>.<genexpr>v  s-      	
Evv--- AEs   $3	3)	rl   r_   r`   rn   r   r  r   r   r   )r  rv  r`  r_   r(  rn   
num_liftedrange_constraintss           r[   _get_range_constraintsr  o  s      /3366B3B3G3G3K3K / 9 9I	
!"8"D"DE	

 	"../J )
 rZ   c                     U R                   c   eU R                   R                  R                  5        VVs0 s H  u  p[        U5      (       d  M  X_M     snn$ s  snnf r   )r   var_to_ranger   r6   )rn   r  r  s      r[   _get_inline_constraintsr    s^    *** ''44::<<DA # 	<  s   AAobjc              #      #    U R                   nUR                  X R                  5      U l          Sv   X l         g! X l         f = f7f)zfHelper 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    s<      kkO $$S--8CK& &os   -A; AAAc               #     #    [         R                  R                  5       n [         R                  R                  S5         S v   [         R                  R                  U 5        g ! [         R                  R                  U 5        f = f7frr   )rb   r  _jit_texpr_fuser_enabled_jit_set_texpr_fuser_enabled)original_states    r[   _temp_disable_texpr_fuserr    sU     XX668N	HH))%0>--n=--n=s   >B	A%  B	%!BB	c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )_WrapperModulei  c                 .   > [         TU ]  5         Xl        g r   )super__init__r_  )r   r_  r  s     r[   r!  _WrapperModule.__init__  s    rZ   c                 &    U R                   " U0 UD6$ r   r_  )r   r   r   s      r[   r  _WrapperModule.forward  s    vvt&v&&rZ   r$  rN   rO   rP   rQ   r!  r  rY   __classcell__)r  s   @r[   r  r    s    ' 'rZ   r  c           
         [        5          SSKJn  [        X5      u  pE[	        X[
        R                  R                  45      (       a$  [        U UUSSS9R                  5       sS S S 5        $ [	        U [
        R                  5      (       a  [	        U R                  5       [
        R                  R                  [
        R                  R                  45      (       aV  [        U R                  5       U 5         [        U R                  5       UUSSS9R                  5       sS S S 5        sS S S 5        $ [        [        U 5      UUSSS9R                  5       sS S S 5        $ ! , (       d  f       O= f S S S 5        g ! , (       d  f       g = f)Nr   )TopLevelTracedModuleFT)r  r~  )r  torch.jit._tracer)  r  r   rb   r  ScriptModule_exportmoduleScriptMethodownerr   r   r  r  )traced_callabler   r   r)  export_argsexport_kwargss         r[   "_convert_ts_to_export_experimentalr3    s:   	"	$9%I$%W"oehh>S>S'TUU$( fh 
%	$ ););<<!!#ehh&;&;UXX__%MB
 B
 446H#))+! (, &( IH# 
%	$6 /$( fhC 
%	$" IHH# 
%	$	$s1   AE;-B
E;7)E  	E;3#E; 
E.	*E;;
F	r&  r  orig_in_specc
                 Z    [         R                  " [        US9n
[        U UUUUUUUUU	U
S9$ )N)r&  )r$  r   r   r`  rY  r&  r  r4  r[  r~  lower_to_aten_callback)r  partialr  _strict_export_lower_to_aten_ir)r$  r   r   r`  rY  r&  r  r4  r[  r~  lower_to_atens              r[   _strict_exportr:    sE     %%&8|TM*%'E!/!0W/, rZ   r6  c                    [        U UUUUSUSS9n[        XU5      u  nnn[        X5      nUR                  R                   H  nUR
                  S:X  d  M  SUR                  ;  d  M'  [        UUR                  5      n[        U[        R                  R                  5      (       a  Mh  Uc   S5       eUR                  USS9UR                  S'   M     [        UR                  R                  [        R                  R                  R                   5      (       d   eUR                  R                  R"                  R$                  =nnUc   eUc   eUR&                  [(        [*        4;  a  [,        R.                  " [*        S U/5      nUR                  R                  R"                  R0                  n[!        [3        UUR4                  U5      5      UR                  l        UR7                  5         [9        U['        U 5      5        [;        U5      n[=        U 5      n[?        X5      n[A        U 5      nURC                  5        VVs0 s H	  u  nnUU_M     nnnU Vs1 s H  nUU;   d  M  UU   iM     snUl"        U   U
" U[G        UX5      0 UU5      nS S S 5        WRH                  nURJ                  nURL                  n [O        UUU5        [Q        [S        U RU                  5       5      [W        URY                  5       5      UU S9  [[        UU5        []        U UU 5        [_        UUU 5        [a        UU5        [c        UUUUR                  S	   S
9$ s  snnf s  snf ! , (       d  f       N= f)NF)rY  r\  r[  r]  r;  r   zbCannot find dynamo_fake_mode. This could be due to the exported graph module have no placeholders.Tr  )r  r  r  ra   ro   rl   rm   rn   ro   )2rb  r   r  r   r   r   r   r>  r   r   rb   r   r   r  _codegenrc   r9   pytree_inform   rI  r  rm  rp   r<   	orig_argsr:   _in_specr4  r   r   r   r   r  r   r  r   r_   r`   ra   r   r  rW   r  r  r   r   r  r  r9  rj   )!r$  r   r   r`  rY  r&  r  r4  r[  r~  r6  r   r   r   dynamo_fake_moder   r   rD  rm   orig_out_specr   params_buffers_to_node_metar%  r   r  tracedorigreverse_name_lookupr   aten_export_artifactr_   r(  ra   s!                                    r[   r8  r8    sb    )'E0W	N  	^6:	 11AR $$**77j U$))%;>4;;7DdEHHOO44$0xwx0#3#?#? $@ $		%  + n**33UXX^^5R5RS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W<VLFD4<<VW +2*D&& 	"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   M
M-	M M  
M.c                     U $ r   rM   r}   s    r[   ry  ry    rz  rZ   c                 @   [         S 5       nS n[        R                  R                  R                  R                  U USSSS9   [        5          U" 5          [        [        U R                  SS95      5      n	[        [        U R                  SS95      5      n
X-   nU" U5      " U USUS9u  pUR                  R                   HQ  nUR                  S:X  d  M  [        UR                  5      S	:X  d  M0  S
UR                  ;  d  MB  S UR                  S
'   MS     [!        U [        R"                  R$                  5      (       a6  ['        U S5      (       a%  UR                  R)                  U R                  5        S S S 5        S S S 5        S S S 5        U(       a	   U" W5        [7        WU UWSUUUS9$ ! , (       d  f       N>= f! , (       d  f       NG= f! , (       d  f       NP= f! [*        [,        4 a(  n[/        [0        R2                  [5        U5      5      eS nAff = f)Nc               3      #    [         R                  R                  n  S[         R                  l        S v   U [         R                  l        g ! U [         R                  l        f = f7fr  r  r  s    r[   r  <_export_to_aten_ir_make_fx.<locals>._compiling_state_context  r  r  c                   ^$ SSK Jn  U=(       d    0 n[        U R                  SS95      n[        U R	                  SS95      n0 UEUEn[
        R                  " U5      u  p[        U5      n[        U5      n
[        U5      n[        U5      n[        X	USS9n/ nUR                  U5        UR                  U5        [        XU5      u  m$n[
        R                  " X45      u  nn[        R                  " T$5      U$4S j5       n[        5          [        5       n[!        U SS 5      nUb  [#        U5      n0 n[%        UU5      nU   ['        USSS	9" U6 nS S S 5        UGb!  [)        W5      n[+        UR,                  5       VVs0 s H  u  nnUUU
U-      _M     nnn[/        UR0                  R2                  5      S
   nWR5                  5        H  u  nn[7        UUU   5      n[7        UU5      nUR0                  R9                  U5         UR0                  R;                  S[<        R>                  R@                  RB                  RD                  UU4S9n URF                  U l#        S S S 5        M     WRI                  5         S n!WR0                  RK                  U!5        S S S 5        [)        W5      n[M        U5      n"U" [/        U5      [/        U5      UUS  U"[        [O        USU
 U5      5      [        [O        UXU-    U5      5      0 0 UUS / / S9n#UU#4$ ! , (       d  f       GN= fs  snnf ! , (       d  f       GMp  = f! , (       d  f       N= f)Nr   )GraphSignatureFr   T)store_orig_modc                      > [        T" U 6 5      $ r   )rm  )r   flat_fns    r[   
wrapped_fnG_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>.wrapped_fn  s    $((rZ   r   )record_module_stackr&  r  r   )r   c                 T   U R                   S:X  a  U R                  [        R                  R                  R
                  R                  [        R                  R                  R                  R                  [        R                  R                  R                  R                  4;   a  gg)Nr   FT)
r   r   rb   r   profiler_record_function_enterdefault_record_function_enter_new_record_function_exit_RecordFunction)r   s    r[   
_is_impureG_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>._is_impure  sr    77o-$++II&&==EEII&&AAIIII&&<<LLB 3
 !rZ   )r  r  user_inputsuser_outputsinputs_to_parametersinputs_to_buffersbuffers_to_mutateuser_inputs_to_mutaterL  rm   backward_signaturer  output_tokens)(&torch._functorch._aot_autograd.schemasrM  r  r   r   rp   r  rm  r   r&   r  r'   r  r  r   r   r>  r)   r(   r4   r$   r  _buffersr  r   r   r   
_find_noder   r   rb   r   rl   copy_rW  r   removeeliminate_dead_coder%   r  )%r$  r   r   re  rM  r   r   params_and_buffersparams_and_buffers_flatparams_spec	param_len
buffer_len
params_lenfunctional_callparams_buffers_argsrm   	flat_argsrL  rQ  ctxnon_strict_rootassigned_buffershookr_   input_namesr
  bufbuffer_input_namesoutput_noder   buf_node	name_noder   r[  output_namesr`   rP  s%                                       @r[   _make_fx_helper3_export_to_aten_ir_make_fx.<locals>._make_fx_helper  s^   I2 4 4e 4 LMS...FGB 0BMB/5/B/BCU/V,"'(?"@()	'
+,
0j
 *,""#:;""4(4&
 $002E1NO	7		!	) 
"	) &'-C%c>4@O*2?C 46 6#%5 (,!% 	  *04 #,O,D,D"E&"E3 Y]33"E # & #288>>226!1!7!7!9IC)".@.EFH *2t 4I22;?#%88#7#7+!IINN0088"*I!6 $8 $
 )2 @? ":  HH((4o (t ),*2.,-'#JK0%!%c+a	*BDT&U!V"K	
,BC]S !"$#
" 3wA & @?? ('sJ   6M L/M L'A1M 	AL-$:M 
L$	M -
L=7	M  
MTr  Fr   )r  r   r   r   r   r   r  )r   rb   r   r  r  r  ry   r   r  r   r   r   r   r   usersr   r   rc   rd   r=  r   r5   r=   r   r   rt  re   r*  )r$  r   r   r   r%  r  r{  r  r  rn  ro  rp  r_   r  r   rw  s                   r[   _export_to_aten_ir_make_fxr    s    : :nf 
	!	!	7	7 
8 
  ":"<S1151IJK	c///GHI
+
'8	
 HHNND?*

Oq(*#'		%  # c588//00WS&5I5IGGNN388$9 #= 
J 	H#B' "%'/	 	K #="<  
 
P )/: 	HM>>AGG	Hsh   GGA?F2F26F2A2F2:GGG% 2
G <G
G	G
G"%H5#HHc                    Sn[        U5      nU R                  R                   H  nUR                  S:X  a>  X2:  a4  XU-
     n[	        U[
        R                  5      (       d  XeR                  S'   US-  nUR                  S:X  d  Mc  [        XR                  5      n[	        U[
        R                  5      (       d  M  SUR                  ;  d   SUR                   S35       eUR                  USS	9UR                  S'   M     g )
Nr   r   r   r>   r;  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   rb   rf   r   r;   r   r  )r_   rs  num_params_buffersindexrn   r   user_argr   s           r[   r  r  H  s     E +I77m#*$-?%?@!(ELL99'/IIe$QJE77j B,C#u||,,DII- &t{{m 4W W- $-#8#8D#8#Q		% ! rZ   r   c                 h   ^ [        [        U4S jU R                  R                   5       5      5      $ )Nc              3   J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fr   )r   )r  r   r   s     r[   r  _find_node.<locals>.<genexpr>e  s     Jnd		T8ITTns   #	#)r   r   r   r   )r_   r   s    `r[   rg  rg  d  s!    JbhhnnJJKKrZ   dispatch_tracing_modec                   ^^^	^^^^^ U
S;   d   eSm0 mUUU4S jn[        U UUTT	US9u  mnnmmm[        TU 5      nU	UUUU4S jnT   [        5          [        XUT5       u  nnnnnU
S:X  a  [        O[
        R                  " [        UT	S9nU" UUUUUUUS9nUR                  R                  5        VVs0 s H#  u  nnU[        U[        5      (       a  UU   OU_M%     snnUl        SSS5        SSS5        SSS5        [        U WR                  UR                  5        Tc   e[        UTTTS	9$ s  snnf ! , (       d  f       NV= f! , (       d  f       N_= f! , (       d  f       Nh= f)
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.
)r4   
aot_exportNc                     >^  SU UUU4S jjnU$ )Nc                   > U=(       d    0 n " U4S jS[         R                  R                  5      nU" U 5      nT Vs/ s H  nSU-   PM
     nn[        XWT5         T" XQ4SU0UD6u  p[        R                  SU5        S S S 5        [        R                  " [        W	R                  5      U	l	        [        R                  " [        U	R                  5      U	l
        [        R                  " [        U	R                  5      U	l        [        R                  " [        U	R                  5      U	l        [        R                  " [        U	R                  5      U	l        WR                  R                   Hy  n
SU
R                   ;   d  M  U
R                   S   n[        R                  " [        U5      R#                  5        VVs0 s H  u  p[%        U5      U_M     snnU
R                   S'   M{     X4$ s  snf ! , (       d  f       GN|= fs  snnf )Nc                   6   >^  \ rS rSrU 4S jrU4S jrSrU =r$ )]_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapperi  c                 .   > [         TU ]  5         Xl        g r   )r   r!  r   )r   r$  r  s     r[   r!  f_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.__init__  s    G$&(+%rZ   c                 *  > U R                   n[        U[        R                  R                  5      (       a  [
        UR                  R                  5       ;   a  [        X1U5        [        R                  R                  R                  5          / UQUR                  5       Q7n[        R                  R                  U5      R                  " U6 nS S S 5        OU" U0 UD6n[        R                  " W5      u  nm[        U5      $ ! , (       d  f       N2= fr   )r   r   rb   rc   rd   r0   _forward_pre_hooksr   	tracebackpreserve_node_metaInterpreterrunrp   r  rm  )r   r   r   r$  tree_out	flat_outsrm   s         r[   r  e_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.forward  s    ++C!#uxx';';<< >"55<<>? >cP"XX//BBD#<T#<FMMO#<D',xx';';C'@'D'Dd'KH ED $'#7#7*0*=*=h*G'Ix ++ EDs   AD
D)r   r&  )r  rm   s   @r[   Wrapperr    s    ,, ,rZ   r  z_export_root.r   z%Exported program from AOTAutograd:
%sr   )rb   r   r   r#   r  debugrp   tree_mapr|   r  r  r`  r_  ra  r   r   r   r   r   )r$  r   r   re  r  wrapped_modr
  new_preserved_call_signaturesr_   r`   r   r   r   r   r  ro   rm   rY  s                 r[   _aot_export_non_strictL_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict  s   \rF,%((// ,6 "#,K .L--K!#-K * - "<M %[OvOO		BBG	 $__[#..ICN //+s{{CCK$*OOKAVAV$WC!'-S55(C$ %+OOKAVAV$WC!$		1&*ii0A&BO )/')%')"4)"HC #3,)"4DII/0 ' 7N7- "4s   G0$G5 H5
Hr   rM   )r  r  ro   rm   rY  s   ` r[   _tuplify_outputs,_non_strict_export.<locals>._tuplify_outputs~  s    <	 <	| &%rZ   )r~  r[  c           	          > [        TU TTTTS9$ )N)rn   r_   r`  equalities_inputsoriginal_signaturer~  )r   )r_   r~  r`  r  rn   r  s    r[   _produce_guards_callback4_non_strict_export.<locals>._produce_guards_callback  s"    3)/1 3
 	
rZ   r4   )r&  r~  )r  r{  r<  )r   r  r   r   r  r  r7  r  ra   r   r   r,   r  r`   rj   )r$  r   r   r`  rY  r&  r  r4  r[  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_funcrG  rC  r  r  rn   ro   r  rm   s      ``    `               @@@@@r[   _non_strict_exportr  h  s   " !$====#'H?A?&P 	/0W	 1C@
 
 
24#CKK P
# )I5 +&&&!-(;  $1#'(@*$  !5 > > D D F. FHC j>N.O.O%c*UXX F. *5 L 5@ 5!%%';'E'E !+	 .5 LK 54sO   
EE#AD<9*D6#	D<,E4E6D<<
E
E
E	E
E,r  rY  r  c                   [        U 5      q[        XX#5      u  nnnnnU(       a  [        R                  " [
        [        S9O[        R                  " [        SS9nU" U UUUUSUUSSS9
n	U	R                  R                  n
[        XU5      n[        U	R                  5      nXR                  R                  R                  S'   [        U	[!        XUSS9U5      n[#        U	UUUU5      u  p[%        U5        ['        U5        [)        X5        [+        X5        SS	KJn  [1        UUR2                  U
UUUX4U	R                  R4                  U/S
9	nU$ )N)r6  r4   )r  F)
r$  r   r   r`  rY  r&  r  r4  r[  r~  inline_constraintsr~  r   )TrainingIRVerifier	r   r   r  
state_dictr  r  r  ra   	verifiers)rK  rU  r  r  r7  r8  r  r  rl   r`   r  r  rn   r_   r   r  r2   r  r  r  r  r   torch._export.verifierr  rA   r   ra   )r$  r   r   r`  r  rY  r4  r  export_funcr  r(  rN  r  r  r_   r  r  exported_programs                     r[   _export_for_trainingr  	  s     5S9 	s&A 	 	+#=	

 "+
  "%'E/!05!O -1155.s&A01J1JK :L  !56.cUC 3&B Bb9(9&hh.&++~!&&00%&
 rZ   )r  rY  r&  r[  r~  c                   SSK Jn	  [        U 5      q[	        5       n
U
R                  U(       a  SOS5        U
R                  U(       a  SOS5        U
q[        S[        S9  U(       a  U	" 5       (       a  [        U UUUUUS	9$ [        XX#5      u  nnnnnU(       a  [        O[        nU" U UUUUUUUUU5
      nUR                  R                  nU(       d  [        XU5      OS
n[        UR                   5      nUUR                  R"                  R$                  S'   ['        U[)        XX(S9U5      n[+        UUUUU5      u  nn[-        U5        [/        U5        U(       d  [1        UU5        [2        R4                  R6                  R9                  USS9  SSKJn  [?        UU 5        [A        UURB                  UUUUX4UR                  RD                  U/S9	nU$ )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.enterrc  r  Nr  r  T)in_place)Verifierr  )#torch._utils_internalr  rK  rU  rW   r-  r  r/   r  r  r:  r  rl   r`   r  r  rn   r_   r   r  r2   r  r  r  r  rb   r,  r  remove_proxy_from_state_dictr  r  r   rA   r   ra   )r$  r   r   r`  r  rY  r&  r[  r~  r  re  r  r  r  r  r(  rN  r  r  r_   r  r  r  s                          r[   r,  r,  d  s   t G  5S9EE	II&hl3	IIn.AM>? 8::#+I
 	
 	s&A %+.0BK!&/O 4C3G3G3K3K :Ms&1RV  11J1JK :LO  !56.cQ
 3&B B!"&<= 
MM445HSW4X/C(&hh.&++~!&&00*
 rZ   r   )NN)r  )rU   r  r   loggingr   r  warnings
contextlibr   r   typingr   r   r   r   r	   r
   r   r   rb   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   -torch._export.passes.collect_tracepoints_passr   (torch._export.passes.lift_constants_passr   r   r   torch._export.utilsr   r   r   r   r    r!   r  r"   torch._export.wrappersr#   4torch._functorch._aot_autograd.input_output_analysisr$   r%   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._subclasses.fake_tensorr-   r.   r  r/   torch.export._unliftr0   torch.export.dynamic_shapesr1   r2   torch.export.exported_programr3   "torch.fx.experimental.proxy_tensorr4   %torch.fx.experimental.symbolic_shapesr5   r6   r7   r8   torch.fx.graphr9   r:   torch.fx.graph_moduler;   r<   torch.utils._sympy.value_rangesr=   
_safeguardr?   r  r@   rA   rB   rC   rD   r  rE   rF   	getLoggerrN   r  	dataclassrH   r]   rj   rs  criticalr  r  	exceptioninfowarningprintwarnrL   ry   r   r|   rc   rd   r   r   r   r   r   r   r   re   r   rf   rg   r  r  r*  r9  rF  rK  rX  rS   rb  r  r?  r  r  r  r  r  r  r  r  r  r  rT   rU  r  r  r  r  r  r  r  r  r  r3  r:  r8  r  r  Noderg  r  r  r,  rM   rZ   r[   <module>r     s       	   2 I I I    $ $ ; 6  Q 
  6 3 + ? D 2 B L 4 6  7 + ( ; 5  V ! ' ' '  
 
 
 d#= = $=  23 MMMMLLKKOO	MM
>  : ; ;(.!5!5 .0#-L:	0f 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__ _D..). 
.b!XX__!5:XX5I5I!	!Huxx 4S>  .2	$$$ !&9!9:$  S	*	$
 
/$: (,MQ	B 79&+49"BB
S/B T#s(^$B U4S>5:tCy#HIJ	B %*#s(OB  $B .2B B B B XXBV ![ "& %[	[
 $[  [ [ [|  	  
#uU\\588#5#556
67 0 (,	
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(V0$O< .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  8S	S
S/S cNS U4S>5:tCy#HIJ	S
 %*#s(OS S c3hS S .2S S %S Sx !y	y
 $y yxR8L588'' Ls Luxx}} L ".^	^
S/^ cN^ U4S>5:tCy#HIJ	^
 %*#s(O^ ^ c3h^ ^ .2^ ^ ^ ^B  (,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	[ 6849 %[	[
S/[ T#s(^$[ U4S>5:tCy#HIJ	[ [ %*#s(O[ [ .2[ [ [   [rZ   