
    ɯwgR                    z   U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZmZmZmZmZmZmZmZ d dlZd dlZd dlZd dlm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( d dl)m*Z*m+Z+ d d	l,m-Z- d d
l.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD d dlEmFZF d dlGmHZH d dlImJZJmKZK d dlLmMZM d dlNmOZOmPZPmQZQ d dlRmSZS d dlTmUZU d dlVmWZW d dlXmYZYmZZZm[Z[m\Z\ d dl]m^Z^m_Z_ d dl`maZa d dlmbZb d dlcmdZd dd lemfZf dd!lgmhZhmiZimjZjmkZkmlZl dd"lmmnZnmoZo  ej                  eq      Zre j                   G d# d$             Zte j                   G d% d&             Zu e j                  d'(       G d) d*             Zv et       Zwej                  ej                  ej                  ej                  ej                  ej                  ej                  e~ej                  h	ew_        ed+        Zd, Zd- Zd.ej                  j
                  fd/Zd0 Zd1 Zd2 Zd3 Zd4ej                  j                  d5ej                  j                  d6eeef   fd7Zd.ej                  j
                  d8eod9eeej                  f   d:eeeej                  eHej                   f   f   d;ee   f
d<Zd=e/d>eod:eeeej                  eHej                   f   f   d6dfd?Zd.ej                  j
                  d>eod6dfd@Zd4ej                  j                  d5ej                  j
                  d6dfdAZdBej                  j                  d6eeef   fdCZdDeeef   dEebdFebdGeeelf   d6eek   f
dHZ	 	 ddIdJdJd'd'd'dKdLedMeedNf   dOeeeef      dPeeeeef   ee   ee   f      dQeedNf   dRedSedTedUedVed6ej                  j
                  fdWZ	 ddX dJdd'dJdYdBej                  j                  dZe/d6eufd[Zd\eKdBej                  j                  d6eeeej                  ej                  j4                  f   f   fd]Z	 ddBej                  j                  dMeedNf   dOeeeef      d6ee   fd^ZdBej                  j                  d6ee   fd_Zd`eej                     daeeej                  f   d>eod:eeeej                  eHej                   f   f   fdbZdcej                  j                  d>eod:eeeej                  eHej                   f   f   fddZdeej                  j
                  d6dfdfZdeej                  j
                  d6dfdgZd.ej                  j
                  d8eofdhZdieid6eeef   fdjZdaeee      edk<   daeeeef      edl<   dm Zdn Zdo ZdpevdqebdQeedNf   drefdsZdpevdteeef   fduZd\eKfdvZedwej                  j                  fdx       Zedy        Z G dz d{ej                  j                        Zdd|ZdBej                  j                  dMeedNf   dOeeef   dPeeeeef   ee   ee   f      dQeedNf   d}ed~eeef   debdSeded6evfdZdBej                  j                  dMeedNf   dOeeef   dPeeeeef   ee   ee   f      dQeedNf   d}ed~eeef   debdSededed6evfdZdd fdBej                  j                  dZe/d6eufdZ	 ddBej                  j                  dMeedNf   dOeeef   dPeeeeef   ee   ee   f      dQeedNf   d}ed~eeef   debdSededed6evfdZeeh	 	 dd'dIddBej                  j                  dMeedNf   dOeeeef      dPeeeeef   ee   ee   f      dedQeedNf   d6eifd              Zeeh	 	 dd'dIdJdJdJddBej                  j                  dMeedNf   dOeeeef      dPeeeeef   ee   ee   f      dedQeedNf   d}edSeded6eifd              Zy)    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)_node_metadata_hook_set_node_metadata_hook)CollectTracepointsPass)ConstantAttrMaplift_constants_passrewrite_script_object_meta)_collect_param_buffer_metadata_get_shape_env_from_gm)_populate_param_buffer_metadata_to_new_gmplaceholder_naming_passplaceholder_prefixes)SpecViolationError)_wrap_submodules)_graph_input_names_graph_output_names)create_functional_call)create_tree_flattened_fn)aot_export_module)detect_fake_mode)FakeScriptObject)
FakeTensorFakeTensorMode)log_export_usage)_check_dynamic_shapes_combine_args%_transform_shapes_for_default_dynamic)
OutputKind)#first_call_function_nn_module_stack)make_fx)ConstraintViolationErrorfree_unbacked_symbolsGuardOnDataDependentSymNodeShapeEnv)_PyTreeCodeGen_PyTreeInfo)insert_deferred_runtime_asserts)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__rI   bool__annotations__dataclassesfieldsetrK   r
   r   do_not_emit_runtime_asserts     X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/export/_trace.pyrH   rH   `   s<     It3D;3D3D4!3x=  #'rW   rH   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)rL   rM   rN   torchfxGraphModulerQ   rF   r   strr   Tensorr+   ScriptObjectrV   rW   rX   rZ   rZ   q   sM    	LL 	
	 rW   rZ   T)frozenc                   ^    e Zd ZU eed<   eed<   eed<   eeeee	j
                  f   f   ed<   y)ExportArtifactatenout_spec	fake_modemodule_call_specsN)
rL   rM   rN   rZ   rQ   r<   r-   r   ra   pytreerV   rW   rX   rf   rf      s2    
Cc6??&:!;;<<rW   rf   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)r^   backendsmkldnn	set_flagsnnpack)orig_mkldnn_flagorig_nnpack_flags     rX   _ignore_backend_decompsrt      s     ~~,,66u=~~,,66u=;'')9:'')9: 	'')9:'')9:s    AC8B' AC8'AC55C8c                     dt        |       z   S )N	L__self__)_strip_rootxs    rX   
_fixup_keyrz      s    Q''rW   c                     t        | t              r7| j                  d      r&| t        d      d  }|j                  d      r|dd  S |S | S )N_export_root.r>   )
isinstancera   
startswithlen)ry   strippeds     rX   rw   rw      sL    !Sall>:S(*+'2237x|EXEHrW   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targetr^   opshigher_order_export_tracepointr   rw   inserting_beforecreate_noder   metareplace_all_uses_with
erase_node)r[   noder   new_nodes       rX   _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 )Nr>   rV   )ry   r   count	fake_inpss     rX   lookup_fakez)_extract_fake_inputs.<locals>.lookup_fake   s    

rW   )r   r   opr   r~   r^   rb   appendr*   r-   r8   rk   tree_map_only)r[   r   r   	fake_valsr   fake_valdetected_fake_moderi   r   	fake_argsfake_kwargsr   r   s              @@rX   _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,,rW   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 N)
input_specsr   rB   	PARAMETERBUFFERr   output_specsr2   BUFFER_MUTATIONGRADIENT_TO_PARAMETER)param_buffer_tabler\   specs      rX   _replace_param_buffer_namesr      s     :99
 
 -T[[9DK:    :99&&,,
 
 -T[[9DK:rW   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        rX   _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                 *    j                  |       | S r   )r   )selfnamepartss     rX   __getattr__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getattr__   s     %T 2'+rW   c                 <    j                  t        |             | S r   )r   ra   )r   idxr   s     rX   __getitem__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getitem__$  s     %SX 6'+rW   N)rL   rM   rN   r   r   )r   s   rX   Pathr     s    ,,rW   r   Lr   r}   )evaljoin	Exception)r   r   r   s     @rX   normalize_pathz2_normalize_nn_module_stack.<locals>.normalize_path  sQ    $ ", , TC&$&)9#:;"xx.$ $#$s   37 AAr}   )resubmodulesr~   r^   r_   r`   r   r   r   r   getnextitervaluesinspectisclass
issubclassnnModulera   rM   rN   items)gm_torch_levelroot_clsrootroot_keyr[   r   add_rootr   r   tyr   keys               rX   _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		+,M)	,R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              rX   _get_param_buffer_mappingr   7  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 rW   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   r>   )r   r   r   r   r   zipr   rB   r   r   r   requires_gradr   
USER_INPUTr~   r^   rb   CONSTANT_TENSORis_leaf
CUSTOM_OBJTOKENAssertionError)r[   r\   r   r]   r   r   placeholdersir   fake_argconstants              rX   _preserve_requires_grad_passr  Z  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   rB   r   r   r   r   )r  r  r]   remap_tabler   valuer   orig_targettargetsr  r   s              rX   _remap_constantsr    s     )+K ( ;e'' 3E :K;  ++ -99%%  
 
 ++K***!ookK=AG!!*DK -H+&! -$,	&!--rW   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 )Nr>   r   )add)r   ndup_name
node_namess      rX   rename_constantz0_rename_constants_nodes.<locals>.rename_constant  s_    :A"&q},8;Q #'q},8;DtrW   N)r   r   r   r"   rB   r   r   r   r   argr   r   r   r   r~   r^   r_   r`   r   	recompile)r[   r  r   r  buffer_prefixconst_prefixbuffer_to_constantr   c_namemodr  s             @rX   _rename_constants_nodesr    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~   r^   rb   r   	Parameterregister_buffersetattrdelattrr   r   r   r   r  )r   r   r   r   fqnattrr   	attr_names           rX   _restore_state_dictr(    s    3?MR
 (--/ 9	c#&;;sC#84 9 (--/ 	%	c}d+}d+dELL)*T588CUCU2V))#t4M3-t$	% ##)) <77j I..0;	< rW   r  c                     | j                  d      D ci c]  \  }}|t        |      j                   c}}S c c}}w )NFr   )named_modulestyperL   )r  r   ms      rX   _get_module_hierarchyr-    sA    .1.?.?QV.?.W#*4d1g  s    :module_hierarchyin_specrh   module_call_signaturesc           	          | D cg c]  }t        ||j                  |              }}|d   j                  dk(  sJ t        g g ||      |d   _        |S c c}w )N)r%  	signaturer    )inputsoutputsr/  rh   )rC   r   r%  rD   r2  )r.  r/  rh   r0  r%  rets         rX   _make_module_call_graphr7    sr     $ 	C+A+E+Ec+JKC  q6::*2wCF Js   #ArV   F)preserve_module_call_signaturedisable_constraint_solver'allow_complex_guards_as_runtime_assertsrestore_fqn_log_export_usagesame_signaturefr   .r   dynamic_shapesr8  r9  r:  r;  r<  r=  c                   |rt        ddh       t        |t              s&t        t        j
                  dt        |             |xs i }t        | ||      }
t        |
|       t        |
|      }t        j                  j                  j                  t        j                  t                     5  	 i }t#        | ||      5  t%               5  t        j                  j'                  | |d|d|||	      |i |\  }}ddd       ddd       ddd       j4                  d<   t        | t        j6                  j8                        r|rt;        | |       |S # 1 sw Y   \xY w# 1 sw Y   `xY w# t(        t*        f$ r(}t        t        j,                  t/        |            d}~wt0        $ r-}t        t        j2                  d	t/        |       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 symbolicT)r?  tracing_moder9  +prefer_deferred_runtime_asserts_over_guardsr:  r<  r=  Nz5Consider annotating your code using torch._check*(). constrain_as_size_example)	case_namerj   )r.   r~   tupler   r   INVALID_INPUTr+  r0   r/   r1   r^   _dynamoconfigpatchrR   asdictDEFAULT_EXPORT_DYNAMO_CONFIGr$   rt   r   r5   r=   CONSTRAINT_VIOLATIONra   r7   ANTI_PATTERNr   r   r   r(  )r>  r   r   r?  r8  r9  r:  r;  r<  r=  combined_argstransformed_dynamic_shapesrj   r   r   es                   rX   rA  rA    s   $ 3<Q;RSdE"''OPTUYPZ|\
 	

 \rF!!T62M-8!F~" 
			#	#K$6$67S$T	U 	GI!13D &( %*MM$8$8#=!+.G AE<c&7#1 %9 % % %! < 0AN+,!UXX__%+A~.A   $ )/: 	HM>>AGG* 	**GAxP5 	/ sf   2G4E1E%0E>E%E1E"E%%E.	*E11G #F##G/(GGGG(c                     | S r   rV   rx   s    rX   <lambda>rX  X       rW   )	transformpre_dispatchdecomp_table_check_autograd_state_is_torch_jit_traceconstant_attrsc                   t         j                  j                         }t               }|	r|s|r
t	               }t
        d        }t         j                  j                  j                  j                  | |ddd      5  |5  t               5   |       5    |t              | |d|||      \  }}d d d        d d d        d d d        d d d        d } ||        |r		  ||       t%        j&                  ||f      }t         j(                  j*                  j,                  sbd}t/        |t1        j2                  t4        |            5  t7        |      }|r%t9        ||d	t;        |j<                         d
       d d d        |j?                          tA        |      _!        d}tE        |jF                        tE        |jH                        z   tE        |jJ                        z   }|j<                  jL                  D ]M  }|jN                  dk(  s||k\  r1|||z
     }tQ        |t         jR                        s||jT                  d<   |dz  }O tW        ||tY        |             }t[        |      }|j]                  t_        |||             |r$ddl0m1} ddl2m3}  |||      \  }} |||      \  }}|ji                         D ]  }tQ        |t         jj                  jl                        s(|j<                  jL                  D ]I  }|jN                  dv s|jT                  jo                  dd        |jT                  jo                  dd        K  tq        ||| ||||       ts        |||||       tu        |||      S # 1 sw Y   xY w# 1 sw Y   x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# 1 sw Y   mxY w)Nc               3      K   t         j                  j                  } 	 dt         j                  _        d  | t         j                  _        y # | t         j                  _        w xY wwNTr^   compiler_is_compiling_flag	old_values    rX   _compiling_state_contextz4_export_to_aten_ir.<locals>._compiling_state_contextj  A     NN55		:04ENN-09ENN-	ENN-   A'A A'A$$A'T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~   r^   r_   r`   r  r   updatelistr   r   r   r   )old_gmnew_gmold_output_nodenew_output_nodes       rX   $_maybe_fixup_gm_and_output_node_metaz@_export_to_aten_ir.<locals>._maybe_fixup_gm_and_output_node_meta  s    fehh223vv&""6;;/"6<<#5#56r:O"6<<#5#56r:O"%%1o6H6HH6TTT++,111  ''(<(<= 4rW   UFile "torch/fx/passes/runtime_assert.py", line 24, in insert_deferred_runtime_assertsstack_traceexported program: r   r   r   r   r>   )replace_autocast_with_hop_pass)replace_set_grad_with_hop_passr   r   r|  );r^   _Cis_grad_enabledr   r?   r   r   utils	stateless_reparametrize_modulert   r)   r5   r=   r   r   rR  ra   rk   tree_leavesrM  rN  rU   r   	functoolspartialr   r   r;   r3   r   r  r&   user_outputsr   
parametersbuffersinput_tokensr   r   r~   rb   r   rE   _get_non_persistent_buffersr   rs  r   3torch._export.passes.replace_autocast_with_hop_passr  3torch._export.passes.replace_set_grad_with_hop_passr  r   r_   r`   popr!   r  rZ   )r  r   r   r   r_  produce_guards_callbackrZ  r[  r\  r]  r^  r  grad_safe_guardrh  r[   r  ry  rV  r   r|  r   indextotal_non_user_inputsr   user_argexport_graph_signaturer]   r  r  _mods                                 rX   _export_to_aten_irr  P  s   " hh..0O!mO ;=O: : 
	!	!	7	7 
8 
 
 
 01
 4L3M
 ;i(9:%'
O
 
 
 
 	> )b1 	H#B' ''K(@AN==;;1 	 %	!!"5;O
 
	 /r2I/()LRXX)V(WX	
	 LLN#6r#:O  EO&&'
o%%
&	'
o**
+	, 
  77m#--)%2G*GH!(ELL9'/DIIe$QJE @8= +2.I(-C^TU	
	
 &D&&
"" &D&&
""
 

 3$ 4 45JJ$$ 	3Dww33		/6		mT2	33 
 !
"$7N 
 Q
 
 
 
 
 
 
 
R )/: 	HM>>AGG	H
	 
	sx   =N NNM8	/N7N?NN, 3O&8N=NN
NN	NN),O#;#OO#&O0ri   c                 2   i t        |j                  d            t        |j                  d            }i }i }|j                         D ]G  \  }}t	        |      |v r|t	        |         }n!| j                  |d      }||t	        |      <   |||<   I |S )NFr   Tstatic_shapes)dictr   r   r   r   from_tensor)ri   r  params_buffersfaked_params_buffersmemor   r	  fake_tensors           rX   _fakify_params_buffersr    s    
s##U#;
<
s  % 8
9N
 "$D$**, 0
Ue9r%y/K#//T/JK)DEO$/S!0  rW   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   r2  forwardbind_partial	argumentsr   r  r   _ParameterKindVAR_POSITIONALextend	enumerater   )r  r   r   r\   _argsnamesr   r	  r  r   kwargs              rX   _get_forward_arg_namesr    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 ]0  \  }}|j                  D ]  }|j                  |r| d| n|        2 |S )zO
    Returns set of non-persistent buffers in a module and its submodules.
    r}   )rT   r*  _non_persistent_buffers_setr  )r  resultr   r,  bs        rX   r  r  9  s]     UF$$& 5a.. 	5AJJ$q}!4	55 MrW   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   rB   r   r   r   )r  r  r  r]   r   r	  s         rX    _rewrite_dynamo_tensor_constantsr  D  so      ++ 	/99	(((;;***&t{{3E,, &55	).	$++&	/rW   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   rB   r   
persistentr   
get_buffer)r  r  r]   r   s       rX   0_move_non_persistent_buffers_to_tensor_constantsr  Z  sr      ++ F99	(((;;***;;i///%-%8%8%EIdkk"	FrW   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   r  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~   ra   ).0ry   s     rX   	<genexpr>z4_verify_nn_module_stack.<locals>.<genexpr>.<genexpr>  s     >q
1c 2>s   N)r~   ra   rK  r   all)r  kvs      rX   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  rt  r   r~   r^   r_   r`   r   r   r   r   r   r#   r  r   )r  r  r  r   r   s        rX   _verify_nn_module_stackr  i  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 -	rW   c           
         t        | gt        | j                               z         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'
    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  rt  r   r~   r^   r_   r`   r   r   r   r   r   ra   r#   )r  r  r  r   r|  s        rX   _verify_stack_tracer    s    \NT,2F2F2H-IIJ 3#uxx334 &&,, 	D))--t<Kww77#+z+s/K,vYtwwi 8HHS}V  55,vYtwwi 8<<G=J 	rW   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   r  r   r   r   r~   r^   r_   r`   r   r   r   r"   r   r#   )r[   r\   r   name_to_kindr  r   	node_kindprefixs           rX   _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   r>   rM   rL   r}   )op_countop_set)rT   r  r   r~   r^   r_   r`   r   r   r   r  r   r  rM   rL   )r  r  r  r,  r   s        rX   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3rW   _EXPORT_FLAGS_EXPORT_MODULE_HIERARCHYc                 B     t        j                          fd       }|S )Nc                     	 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               |t        d|t        |      t               |d }~ww xY w# d ad aw xY w)Nzexport.time)rC  metricsrD  r}   zexport.error.classified)rC  r+  messagerD  zexport.error.unclassifiedrV   )timer.   r  r  r   r+  rM   rN   r   logerrorr   ra   r  )
r   r   startr  endrV  t
error_typerJ  fns
            rX   wrapperz$_log_export_wrapper.<locals>.wrapper  s
    	,IIKET$V$B))+C #e# r"	6 !M'+$	1  	QA+ann<J5a8I$		0;< 3#F'	 G !5#F'	 G'	* !M'+$s%   AA 	C5"BC00C55C8 8C>)r  wraps)r  r  s   ` rX   _log_export_wrapperr    s&    __R$ $L NrW   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~   rK  rt  r  r^   rb   )example_inputsexample_kwarg_inputss     rX   $_process_jit_trace_inputs_for_exportr    sj    nudD&9:(*	ND	)~. 	>ELL$#78 ((*#!///rW   c                 N   | j                  d      }t        |t              s&t        t        j
                  dt        |             ||ni }t        j                  ||f      \  }}t        |t        j                  j                        r|j                  | ||      }|||||fS )NT)	keep_varsrE  )
state_dictr~   rK  r   r   rL  r+  rk   tree_flattenr^   r   ShapesCollectionr?  )r  r   r   r?  original_state_dictr   original_in_specs          rX   _process_export_inputsr    s    ..4.8dE"''OPTUYPZ|\
 	
 )VrF --tVn=A.%,,"?"?@'66sD&I)+>NNrW   export_artifactr  strict_mode_exportc                    | j                   j                  }| j                   j                  }| j                  }| j                  }i }|j                         D ]%  \  }	}
|st        |	      n|	}t        dg g d|
||<   ' t        |      dkD  r/|st        |        t        ||      |      }|J |j                  }t        J t        t        |||      }||fS )z
    In-place modify the graph module in export_artifact, remove _export_tracepoint nodes and
    return module_call_graph.
    )r4  r5  r   rV   )rg   r[   r\   rj   rh   r   rw   rD   r   r   r   r  r  r7  )r  r  r8  r  r[   r  rj   rh   r0  r%  specsmod_fqnresmodule_call_graphs                 rX   _get_module_call_graphr  &  s     /3366B3B3G3G3K3K 	))  )11H  '--/ 

U*<+c"#*= +
r+
%*+
w'
 )*Q.!$R(T$%;=STUWX#//// 	    rW   rT  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   rB   r   )r  r  ss      rX   r  z)_get_range_constraints.<locals>.<genexpr>W  s.      	
1vv--- 	
s   ,.)	rg   r[   r\   ri   r   r  r   r   r   )r  rT  r?  r[   r  ri   
num_liftedrange_constraintss           rX   _get_range_constraintsr   P  s      /3366B3B3G3G3K3K / 9 9I	
!"8"D"DE	

 	"../J )
 rW   c                     | j                   J | j                   j                  j                         D ci c]  \  }}t        |      r|| c}}S c c}}w r   )r   var_to_ranger   r6   )ri   r  r  s      rX   _get_inline_constraintsr  h  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      rX   patch_forwardr
  q  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rm   )r^   r  _jit_texpr_fuser_enabled_jit_set_texpr_fuser_enabled)original_states    rX   _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__r>  )r   r>  r  s     rX   r  z_WrapperModule.__init__  s    rW   c                 &     | j                   |i |S r   )r>  )r   r   r   s      rX   r  z_WrapperModule.forward  s    tvvt&v&&rW   rL   rM   rN   r  r  __classcell__)r  s   @rX   r  r    s    'rW   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)rm  r^  )r  torch.jit._tracer  r  r~   r^   r  ScriptModule_exportmoduleScriptMethodownerr   r   r
  r  )traced_callabler   r   r  export_argsexport_kwargss         rX   "_convert_ts_to_export_experimentalr#    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_specr^  c
                 d    t        j                  t        |      }
t        | |||||||||	|
      S )N)r[  )r  r   r   r?  r8  r[  r  r$  r:  r^  lower_to_aten_callback)r  r  r  _strict_export_lower_to_aten_ir)r  r   r   r?  r8  r[  r  r$  r:  r^  lower_to_atens              rX   _strict_exportr)    sE     %%&8|TM*%'E!/!0W/, rW   r&  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<    |j                  x}}|J |J |j                  t         t"        fvrt%        j&                  t"        d |g      }|j                  j(                  j*                  j,                  }t/        t1        ||j2                  |            |j                  _        |j5                          t7        |t        |              t9        |      }t;        |       }t=        | |      }t?        |       }|jA                         D ci c]  \  }}||
 }}}|D ch c]  }||v r||    c}|_!        |5   |
|tE        |||      i ||      }d d d        jF                  }|jH                  }|jJ                  } tM        |||       tO        tQ        | jS                               tU        |jW                               ||        tY        ||       t[        | ||        t]        |||        t_        ||       ta        ||||j                  d	   
      S c c}}w c c}w # 1 sw Y   xY w)NF)r8  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  r]   rj   rg   rh   ri   rj   )1rA  r   r  r   r   r   r   r   r   r~   r^   r   r   r  	_out_specr+  rt  rK  rk   r<   _codegenpytree_info	orig_argsr9   r:   _in_specr  r   r   r   r   r  r   r  r   r[   r\   r]   r    r  rT   r  r  r   r   r  r  r  rf   )!r  r   r   r?  r8  r[  r  r$  r:  r^  r&  r   r   r   dynamo_fake_moder   r   r&  rh   orig_out_specr   params_buffers_to_node_metar_  r   non_persistent_bufferstracedorigreverse_name_lookupr   aten_export_artifactr[   r  r]   s!                                    rX   r'  r'    s'    )'E0W	N  	^T6:	 11A>R $$** 
77j U$))%;>4;;7DdEHHOO4$0xwx0#3#?#? $@ $		% 
  .777H} $$$ }}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   K!+K'K,,K5c                     | S r   rV   rx   s    rX   rX  rX  m  rY  rW   c           
      n   t         d        }d }t        j                  j                  j                  j                  | |ddd      5  t               5   |       5  t        t        | j                  d                  }	t        t        | j                  d                  }
|	|
z   }  ||      | |d|      \  }}t        | t        j                  j                        r1t        | d      r%|j                  j!                  | j                         d d d        d d d        d d d        t#        j$                  ||f      }d	}j&                  j(                  D ]M  }|j*                  d
k(  s|k\  r1|||z
     }t        |t        j,                        s||j                  d<   |dz  }O t/        |t1        |             }|r		  ||       t?        |      }t        j@                  jB                  jD                  s_d}tG        |tI        jJ                  tL        |            5  tO        ||jP                  dtS        |j&                         d       d d d        |jU                         D ]  }t        |t        j                  j                        s(|j&                  j(                  D ]I  }|j*                  dv s|j                  jW                  dd        |j                  jW                  dd        K  tY        |      }|j!                  t[        |||             t]        |||||       t_        ||| ||||       ta        |||      S # 1 sw Y   8xY w# 1 sw Y   =xY w# 1 sw Y   BxY w# t2        t4        f$ r(}t7        t8        j:                  t=        |            d }~ww xY w# 1 sw Y   [xY w)Nc               3      K   t         j                  j                  } 	 dt         j                  _        d  | t         j                  _        y # | t         j                  _        w xY wwrb  rc  rf  s    rX   rh  z<_export_to_aten_ir_make_fx.<locals>._compiling_state_contexto  ri  rj  c                    ddl m} |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        |dd      | }|j                   j#                          d d d        t%              }t'        |      } |t)        |      t)        |      ||d  |t        t+        |d|
 |            t        t+        ||
|
|z    |            i i ||d g g 	      }||fS # 1 sw Y   |xY w)
Nr   )GraphSignatureFr   T)store_orig_modc                       t         |        S r   )rK  )r   flat_fns    rX   
wrapped_fnzG_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>.wrapped_fn  s    $((rW   )record_module_stackr[  )r  r  user_inputsr  inputs_to_parametersinputs_to_buffersbuffers_to_mutateuser_inputs_to_mutater/  rh   backward_signaturer  output_tokens)&torch._functorch._aot_autograd.schemasr=  r  r   r   rk   r  rK  r   r'   r  r(   r  r  r   r4   r   eliminate_dead_coder%   r&   rt  r   )r  r   r   rD  r=  r   r   params_and_buffersparams_and_buffers_flatparams_spec	param_len
buffer_len
params_lenfunctional_callparams_buffers_argsrh   	flat_argsr/  rA  r[   input_namesoutput_namesr\   r@  s                          @rX   _make_fx_helperz3_export_to_aten_ir_make_fx.<locals>._make_fx_helperx  s    I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		!	) 
"	) &' 	+$(! 	B
 HH((*	+ ),*2.,-'#JK0%!%c+a	*BDT&U!V"K	I
,BC]S !"$#
" 3w9	+ 	+s   ,F99GTrk  Fr   )ro  r   r   r   r   r   r>   rz  r{  r}  r~  r   r   r|  )1r   r^   r   r  r  r  rt   r   r  r   r   r~   r_   r`   r  r   rs  rk   r  r   r   r   rb   rE   r  r5   r=   r   r   rR  ra   r*   rM  rN  rU   r   r  r  r   r;   r   r3   r   r  r   r   r  r!   rZ   )r  r   r   r   r_  r  rZ  rh  rW  rO  rP  rQ  r[   r  rT  r  r   r  r  rV  ri   r|  r  r]   s                           rX   _export_to_aten_ir_make_fxrX  f  s    : :=D 
	!	!	7	7 
8 
 %  % #;"<% S1151IJK	c///GHI
+
8i8	
O c588//0WS&5IGGNN388$'% % %* ""I{#;<IE 77m#
"$UZ%78!(ELL9'/DIIe$QJE @8=
 	H#B' !+I==;;1 	 %	!!"5;O
 	 ,##$%H%R$ST		 

 3$ 4 45JJ$$ 	3Dww33		/6		mT2	33 +2.I(-C^TU 
"$7I
 
 
 m% % % % % %N )/: 	HM>>AGG	H	 	sa   M#MB8M	MM#M0 !0N*	MMM 	M##M-0N'?#N""N'*N4dispatch_tracing_modec                 :  	 |
dv sJ di fd}t        | |||	|      \  }}t        |       }	fd}5  t               5  t        j                  j
                  j                  d      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 t%        |      S c c}}w # 1 sw Y   [xY w# 1 sw Y   _xY w# 1 sw Y   cxY w# 1 sw Y   gx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.
    )r4   
aot_exportNc                      d fd	}|S )Nc                    |xs i } G fddt         j                  j                        } ||       }D cg c]  }d|z   	 }}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     rX   r  zf_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.__init__2  s    G$&(+D%rW   c                    t        | j                  t        j                  j                        rjt        j                  j
                  j                         5  t        j                  j                  | j                        j                  |i |}d d d        n | j                  |i |}t        j                        \  }t        |      S # 1 sw Y   ,xY wr   )r~   r|   r^   r_   r`   	tracebackpreserve_node_metaInterpreterrunrk   r  rK  )r   r   r   tree_out	flat_outsrh   s        rX   r  ze_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.forward6  s    !$"3"3UXX5I5IJ"XX//BBD ',xx';';D<M<M'N'R'R!%()/(H 
 $54#4#4d#Ef#E*0*=*=h*G'Ix ++ s   9CCr  )r  rh   s   @rX   Wrapperr_  1  s    ,
, 
,rW   rh  z_export_root.r   z%Exported program from AOTAutograd:
%sr   )r^   r   r   r$   r  debugrk   tree_maprw   r  r  rE  rD  rF  r   r   r   r   rz   )r  r   r   rD  rh  wrapped_modr  new_preserved_call_signaturesr[   r\   r   r   r   r   r[  rj   rh   r8  s                 rX   _aot_export_non_strictzL_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.  s   \rF,%((// ," "#,K .L-()!#-) - ":<M 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 s7N7-H H"4s   G&G#1G0#G-r   rV   )r[  rm  rj   rh   r8  s   ` rX   _tuplify_outputsz,_non_strict_export.<locals>._tuplify_outputs-  s    2	 2	h &%rW   )r^  r:  c                 &    t        |       S )N)ri   r[   r?  equalities_inputsoriginal_signaturer^  )r   )r[   r^  rp  ri   rq  rU  s    rX   _produce_guards_callbackz4_non_strict_export.<locals>._produce_guards_callbackv  s"    35/1 3
 	
rW   F)assume_static_by_defaultr4   )r[  r^  )r  rZ  r+  )r   r  r   r^   rM  rN  rO  r   rX  r  r  r  r]   r   r~   r+   r  r\   rf   )r  r   r   r?  r8  r[  r  r$  r:  r^  rY  rn  r   r   r   rr  patched_modnew_fake_argsnew_fake_kwargsnew_fake_constant_attrsmap_fake_to_real_to_aten_funcr8  r%  r  rp  ri   rj   rq  rh   rU  s       `    `               @@@@@@rX   _non_strict_exportrz    s   " !$====#'H?A5&| 	/0W	" 1C@
 
 
  24  emm6J6J6P6P!& 7Q 7   $CKK 	 P
# )I5 +&&&!-(;  $1#'(@*$  !5 > > D D F.C j>N.O%c*UXX. *5	     D 5!%%';'E'E !+	 .5	 	           sg   	F+F?E9AE-	("E'
	E-	E9F#F'E-	-E62E99F>FF	
FF)rm  r8  rm  c                   t        |       a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#        |	|||      \  }}||j                  d<   t%        |       t'        |       t)        ||
       d	d
lm} t/        ||j0                  |
|||||f|	j                  j2                  |g	      }|S )N)r&  r4   )rY  F)
r  r   r   r?  r8  r[  r  r$  r:  r^  inline_constraintsr^  forward_arg_namesr   )TrainingIRVerifier	r   r   r  r  r  r  r  r]   	verifiers)r-  r  r  r  r  r'  rX  rz  rg   r\   r  r  ri   r[   r   r   r0   r  r  r  r  torch._export.verifierr  rA   r   r]   )r  r   r   r?  rm  r8  r$  r  export_funcr  r  r~  r|  r  r[   r  r  exported_programs                     rX   _export_for_trainingr    s     5S9 	sD&.A 	 	+#=	

 "+
  "%'E/!05!O -1155.sD&A01J1JK :LO  !56.c4UC 3'EvB
 $5BGG Bb"899&hh.&++f~!&&00%&
 rW   )rm  r8  r[  r:  r^  c                &   t        |       at               }	|	j                  |rdnd       |	j                  |rdnd       |	at        dt               t        | |||      \  }}}
}}|rt        nt        } || |||||||
||
      }|j                  j                  }|st        | ||      nd}t        |j                        }||j                  j                  j                  d<   t!        |t#        | |||	      |      }t%        ||
||      \  }}||j                  d
<   t'        |       t)        |       |st+        ||       t,        j.                  j0                  j3                  |d       ddlm} t9        | t,        j:                  j<                        rDt?        | d      r8d| j                  v r*|j                  jA                  d| j                  d   i       tC        ||jD                  ||||||f|j                  jF                  |g	      }|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.
    rm  
non_strictr[  aot_dispatchzexport.enterrB  Nr|  r}  r~  T)in_placer   )Verifierr   customr  )$r-  r  rT   r  r  r.   r  r)  rz  rg   r\   r  r  ri   r[   r   r   r0   r  r  r  r  r^   r  r  remove_proxy_from_state_dictr  r  r~   r_   r`   r  rs  rA   r   r]   )r  r   r   r?  rm  r8  r[  r:  r^  rD  r  r  r  r  r  r~  r|  r  r[   r  r  r  s                         rX   r  r  	  s   v  5S9EE	II&hl3	IIn.AM>? 	sD&.A %+.0BK!&/O 4C3G3G3K3K :MsD&1RV  11J1JK :LO  !56.c4=PQ
 3)+I6B
 $5BGG B!"&<= 
MM445HSW4X/ 	3,,-C  
#((8"456&hh.&++f~!&&00*
 rW   )NNr   )r[  )rR   r  r   loggingr   r  warnings
contextlibr   r   typingr   r   r   r   r	   r
   r   r   r^   torch._dynamotorch.fxtorch.utils._pytreer  _pytreerk   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._node_metadata_hookr   r   -torch._export.passes.collect_tracepoints_passr   (torch._export.passes.lift_constants_passr   r   r   torch._export.utilsr   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(   torch._functorch.aot_autogradr)   torch._guardsr*   "torch._library.fake_class_registryr+   torch._subclasses.fake_tensorr,   r-   torch._utils_internalr.   torch.export.dynamic_shapesr/   r0   r1   torch.export.exported_programr2   torch.fx._utilsr3   "torch.fx.experimental.proxy_tensorr4   %torch.fx.experimental.symbolic_shapesr5   r6   r7   r8   torch.fx.graphr9   r:   torch.fx.passes.runtime_assertr;   r<   torch.utils._sympy.value_rangesr=   
_safeguardr?   r  r@   rA   rB   rC   rD   r  rE   rF   	getLoggerrL   r  	dataclassrH   rZ   rf   rQ  criticalri  r  	exceptioninfowarningprintwarnrK   rt   rz   rw   r_   r`   r   r   r   r   r   r   r   ra   r   rb   rc   r  r  r  r(  r-  r7  rP   rA  r  r!  r  r  r  r  r  r  r  r  r  r  rQ   r  r  r  r  r  r   r  r
  r  r  r#  r)  r'  rX  rz  r  r  rV   rW   rX   <module>r     s       	   2 I I I    $ $ ; 6  Q 
  6 3 J ; * ? D 2 
 5 ? 6  7 J ( ; 5  V g! ' ' '  
 
 
 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..). 
.b!XX__!5:XX5I5I!	!Huxx 4S> 38n  !&9!9:	
 
/( (,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\ !r r	r
 $r rj  	  
#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(%((*>*> (4 (Vehh&:&: t 4%(("6"6 =Q ,4_ 4c3h 4  %)xC! (59 (4S>2 9(V0$O"'!#'!'! %*#s(O'! 	'!T#48cN0~  &uxx & &" > >'UXX__ '"J	
S/ cN U4S>5:tCy#HIJ	
 %*#s(O  c3h  .2  8L	L
S/L cNL U4S>5:tCy#HIJ	L
 %*#s(OL L c3hL L .2L L %L Lj !n	n
 $n nx ".V	V
S/V cNV U4S>5:tCy#HIJ	V
 %*#s(OV V c3hV V .2V V V Vt  (,MQ	S 68S	S
S/S T#s(^$S U4S>5:tCy#HIJ	S S %*#s(OS S   Sl  (,MQ	L 6849 %L	L
S/L T#s(^$L U4S>5:tCy#HIJ	L L %*#s(OL L .2L L L   LrW   