
    N j'                       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  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJr  S SKJrJr  S SKrS SKJs  Jr  S SKJs  Jr  S S	KJr  S S
KJr  S SKJ r   S SK!J"r"J#r#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J0r0J1r1  SSK2J3r3  \Rh                  " \55      r6/ SQr7 " S S\5      r8\" SS9 " S S5      5       r9Sq:\
S 5       r; S]S\Rx                  \Rz                  -  \R|                  R~                  -  S\R|                  R~                  S\@S\8S\A4
S jjrB " S  S!5      rC " S" S#\C\R|                  R~                  5      rD " S$ S%\C\R|                  R~                  5      rE " S& S'\ R                  5      rG " S( S)\C\R|                  R~                  5      rH S^S*\S+\GS-  S,\H4S- jjrIS.\R*                  R                  S/\#S,S4S0 jrKS.\R*                  R                  S,\@S-  4S1 jrLS2 rMS3 rNS4\@S5\@S,\@4S6 jrOS7\R|                  R~                  S8\R|                  R~                  4S9 jrPS:\R|                  R~                  S,\@4S; jrQS:\R*                  R                  S,\R*                  R                  4S< jrTS:\R*                  R                  \D-  \H-  S,\R*                  R                  4S= jrUS:\R*                  R                  \D-  \H-  S,\R*                  R                  4S> jrVS?\R|                  R~                  S\@4S@ jrW S^S?\R|                  R~                  S\@SA\R|                  R~                  SB\\@/\R|                  R~                  4   S-  4SC jjrXSD\@SE\YS,\@4SF jrZSG\@SD\@S,\A4SH jr[ " SI SJ5      r\\ " SK SL5      5       r]SM\R*                  R                  SN\H4SO jr^ S_SP\R|                  R~                  SQ\_\@\Y4   SR\@4SS jjr` " ST SU5      raS:\R*                  R                  SN\HSV\_\@\b\@   4   4SW jrcSX rd S^S*\R|                  R~                  SY\_\@\e\@   4   SZ\e\@   S[\_\Y\b\@   4   S-  4S\ jjrfg)`    N)defaultdict)Callable)contextmanager)deepcopy)	dataclass)Enum)Anycast)FakeScriptObject)ExportedProgram)reorder_kwargs)ConstantArgumentExportGraphSignature	InputKindModuleCallSignatureSymBoolArgumentSymFloatArgumentSymIntArgumentTensorArgument)is_fx_symbolic_tracing)	_get_attr_get_attr_via_attr_list_print_readable)
GetAttrKeySequenceKey   )_remove_effect_tokens)FlatArgsAdapterInterpreterModuleInterpreterModuleDispatcherUnflattenedModule	unflattenc                   $    \ rS rSrSrSrSrSrSrg)	_AttrKind2   	parameterbufferconstantmodule N)	__name__
__module____qualname____firstlineno__	PARAMETERBUFFERCONSTANTMODULE__static_attributes__r*       g/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/export/unflatten.pyr$   r$   2   s    IFHFr4   r$   T)frozenc                   V    \ rS rSr% Sr\R                  \S'   \\S'   \\S'   \	\S'   Sr
g)		_TensorID9   zJCustom tensor identifier containing storage, stride, and size information.untyped_storagestridesizestorage_offsetr*   N)r+   r,   r-   r.   __doc__torchUntypedStorage__annotations__tupleintr3   r*   r4   r5   r8   r8   9   s!    T)))M
Kr4   r8   c               #   8   #    [         n Sq  S v   U q g ! U q f = f7fNF)RUN_WITH_INTERPRETER)old_flags    r5   _disable_interpreterrH   F   s&      $H ('xs   	 from_obj	to_moduletarget	attr_kind
persistentc                   ^	 UR                  S5      Gt pVU1nU H  m	[        5       nU Hp  n[        UT	5      (       d)  [        UT	[        R
                  R                  5       5        UR                  U	4S jUR                  R                  5        5       5        Mr     UnM     U GH  nU[        R                  :X  aS  [        U [        R
                  R                  5      (       d  [        S[        U 5       35      eUR!                  X`5        Mk  U[        R"                  :X  aH  [        U [        R$                  5      (       d  [        S[        U 5       35      eUR'                  X`US9  M  U[        R(                  :X  au  [        U [*        5      (       a  [        S5      e[        U [        R$                  [        R,                  45      (       d  [        S[        U 5       35      e[        XU 5        GMP  U[        R.                  :X  d  GMg  [        U [        R
                  R                  5      (       d  [        S[        U 5       35      e[        XU 5        GM     g )	N.c              3   R   >#    U  H  u  p[        UT5      (       d  M  Uv   M     g 7fN)_is_call_name).0kt_callitems      r5   	<genexpr>_assign_attr.<locals>.<genexpr>g   s&      !;IA D) !;s   '	'z9expected torch.nn.Parameter for PARAMETER attr_kind, got z0expected torch.Tensor for BUFFER attr_kind, got )rM   z2FakeScriptObject should only exist during tracing.zHexpected torch.Tensor or torch.ScriptObject for CONSTANT attr_kind, got z3expected torch.nn.Module for MODULE attr_kind, got )splitsethasattrsetattrr?   nnModuleupdate_modulesitemsr$   r/   
isinstance	ParameterAssertionErrortyperegister_parameterr0   Tensorregister_bufferr1   r   ScriptObjectr2   )
rI   rJ   rK   rL   rM   prefixfield
to_modulestsrV   s
            @r5   _assign_attrrn   S   s    \\#&NV J#&5#I9d++	4):;II !*!3!3!9!9!;  $ 
   		+++h(:(:;;$OPTU]P^O_`  ((9)***h55$FtH~FVW  %%e*%M),,,($455$H  LL&&  %^_cdl_m^no  Ih/)***h88$I$x.IYZ  Ih/E  r4   c                   :    \ rS rSr% \S-  \S'   S\S-  4S jrSrg)_SubmoduleBase   N_tyreturnc                     U R                   $ )z
Subclass of this class - InterpreterModule, InterpreterModuleDispatcher, represents
corresponding model in eager model. To get this type information for those modules
in eager model we need to use this method.
)rr   selfs    r5   	type_name_SubmoduleBase.type_name   s     xxr4   r*   )r+   r,   r-   r.   strrA   rw   r3   r*   r4   r5   rp   rp      s    	tO3: r4   rp   c                      ^  \ rS rSr% Sr\R                  R                  S-  \S'    SS\R                  R                  S\
S-  4U 4S jjjrS rS	 r    SS
 jrSrU =r$ )r      zA module that uses torch.fx.Interpreter to execute instead of the usual
codegen that GraphModule uses. This provides better stack trace information
and makes it easier to debug execution.
Ngraph_modulegraphtyc                 p   > [         TU ]  5         Xl        X l        X R                  l        [
        U l        g rQ   )super__init__r}   rr   owning_modulerF   _run_with_interpreter)rv   r}   r~   	__class__s      r5   r   InterpreterModule.__init__   s,    
 	
#'

 %9"r4   c                 .  ^ U R                   c  [        S5      e[        5       (       d]  [        R                  R                  5       (       d  U R                  (       d)  [        U R                   5      R                  " U /UQ70 TD6$ T(       a  [        U5      nU R                  [        U5      S  nUR                  U4S jU 5       5        [        U5      [        T5      :w  a#  [        S[        U5       S[        T5       35      e[        U5      [        U R                  5      :w  a-  [        S[        U5       S[        U R                  5       35      e[        U5      n[        R                  R                  X R                   S9R"                  " USS	06$ )
Nz&Didn't finalize this InterpreterModulec              3   >   >#    U  H  nUT;   d  M  TU   v   M     g 7frQ   r*   )rS   
kwarg_namekwargss     r5   rW   ,InterpreterModule.forward.<locals>.<genexpr>   s'       &1
!V+ 'F:&&1s   
zkwarg_names length z does not match kwargs length zarg_list length z! does not match arg_names length r}   enable_io_processingF)r|   rd   r   r?   compileris_dynamo_compilingr   re   forwardlist	arg_nameslenextendrB   fxInterpreterr}   run)rv   argsr   arg_listkwarg_namess     `  r5   r   InterpreterModule.forward   sg   $ !IJJ%''NN..008R8R ))*224I$I&II
  :"nnS]_=  &1   {#s6{2(-c+.>-??]^abh^i]jk  x=C$77(*3x=/9Z[^_c_m_m[nZop  X88''JJ'?CC,1 r4   c                 Z   [         R                  R                  X R                  5      U R                  S'   U R                  R                  5         / U l        U R                  R                   H:  nUR                  S:X  d  M  U R                  R                  UR                  5        M<     g )Nr|   placeholder)r?   r   GraphModuler}   __dict__lintr   nodesopappendrK   rv   nodes     r5   finalizeInterpreterModule.finalize   sr     ).(<(<T::(Nn%

 JJ$$Dww-'%%dkk2 %r4   c                 "    [        U SUUUU5      $ )Nr   r   rv   print_outputinclude_strideinclude_devicecoloreds        r5   print_readable InterpreterModule.print_readable   #     
 	
r4   )r   rr   r   r}   rQ   TFFF)r+   r,   r-   r.   r>   r?   r   r   rA   Graphry   r   r   r   r   r3   __classcell__r   s   @r5   r   r      so    
 ((&&--
 	:xx~~	: $J	: 	:(T3& 
 
r4   r   c                   b   ^  \ rS rSrSrS\\   S\\   4U 4S jjr	S r
S r    S
S jrS	rU =r$ )r       z
A module that carries a sequence of InterpreterModules corresponding to
a sequence of calls of that module. Each call to the module dispatches
to the next InterpreterModule, and wraps back around after the last.
attrscall_modulesc           	         > [         TU ]  5         U(       d  [        S5      eUS   R                  U l        U H  n[	        X[        US   U5      5        M     US   R                  U l        X l        SU l        g )Nzcall_modules must not be emptyr   )	r   r   rd   r`   r\   getattrrr   _call_modules
_num_calls)rv   r   r   accessorr   s       r5   r   $InterpreterModuleDispatcher.__init__  sk     !ABB$Q00HDGLOX$FG ?&&)r4   c                     U R                   U R                     nU R                  S-   [        U R                   5      -  U l         U" U0 UD6$ ! [         a	    SU l        e f = f)Nr   r   )r   r   r   	Exception)rv   r   r   call_modules       r5   r   #InterpreterModuleDispatcher.forward  sd    ((9??Q.#d6H6H2II	/// 	DO	s   A A c                     U R                   $ rQ   )r   ru   s    r5   r   (InterpreterModuleDispatcher.call_modules  s    !!!r4   c           	          U R                    Vs/ s H  nUR                  UUUU5      PM     nnSR                  U5      $ s  snf )N
)r   r   join)rv   r   r   r   r   modoutputss          r5   r   *InterpreterModuleDispatcher.print_readable  s[     ))
 * 	 * 	 
 yy!!
s   A )r   r`   r   rr   r   )r+   r,   r-   r.   r>   rZ   ry   r   r   r   r   r   r   r3   r   r   s   @r5   r    r       sG    	c#h 	d;L6M 	"
 " "r4   r    c                       \ rS rSrSr\R                    SS\R                  S\R                  S\	\
   S\\\
4   S-  S\
S-  S	\	\
   4S
 jj5       rS	\	\   4S jrSrg)r   i/  zF
Adapts input arguments with ``input_spec`` to align ``target_spec``.
Ntarget_spec
input_spec
input_argsmetadataobjrs   c                     g)z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr*   )rv   r   r   r   r   r   s         r5   adaptFlatArgsAdapter.adapt4  s     	r4   c                     / $ )z>Returns a list of paths that are used to access the flat args.r*   ru   s    r5   get_flat_arg_paths"FlatArgsAdapter.get_flat_arg_paths@  s    	r4   r*   )NN)r+   r,   r-   r.   r>   abcabstractmethodpytreeTreeSpecr   r	   dictry   r   r   r3   r*   r4   r5   r   r   /  s     	 +/	__	 OO	 I		
 sCx.4'	 4Z	 
c	 	DI r4   r   c                   v   ^  \ rS rSr SS\S\S-  4U 4S jjjrS rS rS r	S	 r
S
 rS r    SS jrSrU =r$ )r!   iE  Nexport_moduleflat_args_adapterc           	      ,  >^, [         T-U ]  5         UR                  R                  b  [	        S5      eS nUR
                   Vs/ s H  oDR                  PM     nnUS   S:w  a  [        SUS   < 35      e[        UR                  5      n[        UR                  5      U l        [        R                  R                  5       U l	        X R                  l        [        UR
                  5      U l        X l        UR                  R                   U l        X R                   S'   SU l        [$        U l        [)        X`R                  5        [+        U5        [-        U5      U l        [1        5       U l        [5        X`5      u  pxU R2                  R7                  UR9                  5       5        [;        UR<                  X5        UR>                  U l        / U l         URB                  n	[E        5       n
0 nU R                  RF                   H  nX   nU" U5      U;  a>  [        RH                  RK                  URM                  5       URN                  S9X" U5      '   [Q        X" U5         U U[R        RT                  S	9  U
RW                  U5        M     [E        U R                  RX                  5      n[E        5       n0 nU R                  RZ                   H  nX;   a  SnUR\                  U   nOS
nX   nU" U5      U;  a  URM                  5       U4UU" U5      '   [Q        UU" U5         S   U U[R        R^                  US9  URW                  U5        M     U	Ra                  5        H  u  nnX;   d  X;   a  M  SnU" U5      U;   d)  [c        U[        RH                  RJ                  5      (       d  S
nU(       aB  U" U5      U;  a  US
4UU" U5      '   [Q        UU" U5         S   U U[R        R^                  S
S9  M  U" U5      U;  a
  UX" U5      '   [Q        X" U5         U U[R        RT                  S	9  M     0 nUR\                  Ra                  5        Hq  u  nnU" U5      U;  a:  [c        U[        Rd                  5      (       a  URM                  5       nUUU" U5      '   UU" U5         n[Q        UU U[R        Rf                  S	9  Ms     [i        [j        5      m,[E        5       nU,4S jn[E        5       nU R                  Rl                   GH  nURn                  [p        RT                  :X  d0  URn                  [p        R^                  :X  Ga  URr                  (       a  [u        URv                  S5      (       d!  [        S[y        URv                  5       35      e[c        URz                  [|        5      (       d!  [        S[y        URz                  5       35      eU" U" URB                  URz                     5      URv                  R~                  URz                  5        URW                  URz                  5        URW                  URz                  5        GML  URn                  [p        R^                  :X  a  URr                  (       a?  URn                  [p        R                  :X  d!  URn                  [p        R                  :X  d  GM  [u        URv                  S5      (       d.  [        SURn                   S[y        URv                  5       35      e[c        URz                  [|        5      (       d.  [        SURn                   S[y        URz                  5       35      eU" U" UR\                  URz                     5      URv                  R~                  URz                  5        URW                  URz                  5        GM     UR\                  Ra                  5        H\  u  nnUU;  d  M  U" U5      nUT,;  a  [        SU< S35      eT,U   S   u  n n!U" UU U5        URW                  WRz                  5        M^     URB                  Ra                  5        H3  u  nnUU;  d  M  U" U5      n"U"T,;  a  M  T,U"   S   u  n n!U" U"U U5        M5     0 n#T,R9                  5        H)  n$U$ V%s/ s H  n%U%S   PM
     n&n%U$ H  u  n'n!U&U#U''   M     M+     [        U U#/ 5        [        UR9                  5       5      n(U Vs/ s H  nUU(;  d  M  UPM     nnU R                  U(U5        U Vs/ s H  nSU;  d  M  UPM     nnU R                  R                   V)s/ s H  n)U)R                  S:X  d  M  U)PM     sn)U lG        S
U lH        [        U5       V*Vs0 s H	  u  n*nUU*_M     n+n*nU R                  SS9 H  u  nn!UU+;  d  M  [        U+5      U+U'   M     [        U U+5        U R                  R                  5         U R                  5         g s  snf s  sn%f s  snf s  snf s  sn)f s  snn*f )Nz%Unflattening on JointExportModule NYIc                     [        U [        R                  5      (       aD  [        U R	                  5       U R                  5       U R                  5       U R                  5       S9$ [        U 5      $ )z8Returns _TensorID dataclass for tensors, otherwise id().)r:   r;   r<   r=   )	rb   r?   rg   r8   r:   r;   r<   r=   id)r   s    r5   _id'UnflattenedModule.__init__.<locals>._idO  sU    #u||,, $'$7$7$9::<#&#5#5#7	  c7Nr4   r    z+expected first fqn to be empty string, got unflattened_moduleF)requires_grad)rL   T)rL   rM   c                 4   > TU    nUR                  X45        g rQ   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r5   add_to_consts_map5UnflattenedModule.__init__.<locals>.add_to_consts_map  s    "6*Ii56r4   namez-expected s.arg to have 'name' attribute, got z!expected s.target to be str, got z1expected s.arg to have 'name' attribute for kind z, got z%expected s.target to be str for kind z	constant z id not found in consts_mapr   @r   remove_duplicate)Or   r   graph_signaturebackward_signature
ValueErrormodule_call_graphfqnrd   r   r}   r?   r   r   r   r   r|   metaadaptedrF   r   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_root_module_typerr   _IValsivals_outline_submodulesr_   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrZ   
parametersr]   rc   cloner   rn   r$   r/   addnon_persistent_buffersbuffers	constantsr0   ra   rb   rg   r1   r   r   input_specskindr   rM   r[   argre   rK   ry   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   r   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   r   ).rv   r   r   r   entryfqn_listexport_graphseen_modules
seen_attrsr  assigned_paramsid_to_paramr   paramr	  assigned_buffersid_to_bufferrM   r'   tensor	is_bufferid_to_constr   r(   	_constantconsts_targetsr   added_params_bufferss
const_nameconstconst_idph_name_	tensor_idinputs_to_statenode_targetttargetsnredirected_call_indicesr   i	fqn_orderr   r   s.                                               @r5   r   UnflattenedModule.__init__F  s	   
 	((;;GDEE		 ,9+J+JK+J%II+JKA;" =hqk_M    3 34'(E(EFXX^^%
#'

 !)-*I*I!J!2!..33	*.		&' %9"+L:N:NOl+$\2X
#6|#J  	

,--/0 	-55tH!.!@!@*,! #--
$'E  	 ((33D$E5z,*/((*<*<KKM1D1D += +CJ' CJ'#--	 % 4 "%T%9%9%P%P!Q%(UOQ((00D-"
&006!
#)6{,.-3\\^Z,HS[)S[)!,#**%   &% 1, ',,.LD&&$*BI6{l****3 3 !	K|3 1LV-  V-a0'..# v;k1/5KF,F,'11	9 /H TV*44::<MC8}K/h55'~~/H-5CM*#CM2I#,,	 = DOtCT
#&5	7 *-%%11Avv,,,)***q||quuf--(GQUU}U  "!((C00(;DN;KL  "00:;EEJJHH
 ""188,$((2)***66Y66666Y111quuf--(KAFF8SYZ^_`_d_dZeYfg  "!((C00(?xvdSTS[S[nM]^  "//9:EEJJHH
 ""188,M 2R "/!8!8!>!>!@J/u::-(#J>1LM  (1!4
!(GZ@$((2 "A )3399;KC..K	J. '	215
!)Wc: < 13%,,.K%01[qt[G1#1%," $ /
 	T?B/"6|7J7J7L"M#+R8Cs:Q/QC8R6G#+>8Cs#~C8> "ZZ--#
-TM1ID-#
 (,$*3H*=>*=3S!V*=	>))5)AGD!9$"%i.	$ B 	D),

o Lx 2 S ?
#

 ?s6   g7g<)
h7h
h#hhh>hc                    U R                  5        Hp  u  p[        US-   5        [        US5      (       d  M&  [        UR                  [
        R                  R                  5      (       d  M[  [        UR                  5        Mr     g )N:r}   )r  printr[   rb   r}   r?   r   r   )rv   r   r   s      r5   _print_graphUnflattenedModule._print_graphS  sT    **,HC#)sG$$CIIuxx~~)N)Ncii  -r4   c                    U R                   S   R                  nX$R                  :X  a  U$ U R                  c  [	        S5      eU R                  R                  UR                  UUU R                  US9n[        U5      UR                  R                  :w  a.  [	        S[        U5       SUR                  R                   35      eU$ )Nr   zeThere is no flat args adapter specified. Are you sure you are calling this with the right arguments? )r   r   r   r   r   z<Flat args adaption failed, number of args mismatch Adatped: z 
Exported module: )	r   	signaturein_specr   	TypeErrorr   r   r   
num_leaves)rv   	flat_argsr@  inputr?  s        r5   _adapt_flat_args"UnflattenedModule._adapt_flat_argsY  s    **1-77	'''!!)O 
 ..44%--"$ 5 I 9~!2!2!=!==  #I/ 0((1(9(9(D(D'EG 
 r4   c           	         U R                   S   R                  nUnU(       a  [        X#R                  5      n[        R
                  " X45      u  pVU Vs/ s H  owS   PM	     nn[        5       (       a  U$ XcR                  :w  aR  U R                  (       d(  [        SU S3SUR                   35        [        S5        U R                  XU5      nSU l        U R                  (       a  SSKJn	  U R                  SL a  U R                  (       a  U R                  R                  5       O/ n
U
(       a;  [        U
5      [        U5      :w  a#  [!        S	[        U
5       S
[        U5       35      e[#        U5       VVs/ s H$  u  p[%        US9['        U
(       a  X   OSS94U4PM&     nnnOUnU	" U R(                  XR*                  5        U$ s  snf s  snnf )Nr   r   zGInput treespec does not match with exported module's: 
Input treespec: z. zExported module treespec: z5Adapting flat arg to match exported module's treespecT)"_check_input_constraints_for_graphzflat_arg_paths length z! does not match flat_args length )idxz<unknown location>)r   )r   r?  r   r@  r   tree_flatten_with_pathr   r   r;  rE  r  torch._export.utilsrH  r   r   r   rd   r  r   r   r  r  )rv   r   r   r?  reordered_kwargsflat_args_with_pathr@  xrC  rH  flat_arg_pathsrI  r  new_flat_args_with_paths                 r5   process_forward_inputs(UnflattenedModule.process_forward_inputst  s   **1-77	!-f6G6GH'-'D'D$(
$ $77#6aqT#6	7!##'''<<''.ir301B1B0CD
 MN--i$GIDL'' O||t# -- **==? 
 "c.&9S^&K(0^1D0EEfgjktgufvw  %.i$8+ %9 (C0&#1 &4%8%9 
 %9 ( +' +>'.'')@BXBX i 8>+s   G
:+Gc                    U R                   " U0 UD6nU R                  S   R                  n[        5       (       ab  [        R
                  R                  X R                  S9R                  " USS06n[        U[        5      (       a  [        U5      S:X  a  US   $ U$ [        R                  R                  5       (       d  U R                  (       d'  [        U R                   5      R"                  " U /UQ76 nO7[        R
                  R                  X R                  S9R                  " USS06n[$        R&                  " XdR(                  5      $ )Nr   r   r   Fr   )rQ  r   r?  r   r?   r   r   r}   r   rb   rB   r   r   r   r   re   r|   r   r   tree_unflattenout_spec)rv   r   r   rC  r?  
return_valtree_outs          r5   r   UnflattenedModule.forward  s   //@@	**1-77	!##--d**-EII16J *e,,ZA1E!!}$>>--//t7Q7QD--.66tHiHHxx++D

+CGG16H $$X/A/ABBr4   c                     [         R                  R                  X R                  5      U R                  S'   U R                  R                  5         g )Nr|   )r?   r   r   r}   r   r   ru   s    r5   r   UnflattenedModule.finalize  s1    (-(<(<T::(Nn%

r4   c                   ^ [        [        5      mU R                   H  nUR                  (       d  M  UR                  (       d  M)  UR                  n[        XR                  XD5      5      nSU;   a  UR                  S5      OUS/u  pgTU   R                  [        U5      U45        M     [        [        5      nU HF  n	SU	;   a)  U	R                  SS5      u  pX   R                  U5        M2  US   R                  U	5        MH     TR                  5        H  u  p[        U5       VVs/ s H  u  pUPM	     nnn[        U5      S:  d  M5  [!        [        U5      5       HQ  n[#        XS-   5      nXA;  d  M  UR                  S5      Gt nn[%        U U5      R&                  R)                  U5        MS     U R+                  U
[-        X   U5      5        M     U4S jnU R/                  SS9 H  u  pE[1        US	5      (       a  U" XER2                  5        M*  [1        US
5      (       d  M=  UR4                   H>  n[1        US	5      (       d  [7        S[9        U5       35      eU" UUR2                  5        M@     M     gs  snnf )zFor a module whose call signatures are preserved, replace
multiple modules corresponding to multiple calls to that module
with a single dispatcher module that tracks which module to call.
r   0rO   r   r   c                    > UR                    HQ  nUR                  S:X  d  M  UR                  R                  S5      S   nU (       a  U  SU 3OUnUT;   d  MK  X2l        MS     g )Nr   r   r   rO   )r   r   rK   rY   )rj   r}   r   r   pathcalled_moduless        r5   elide_call_indices?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  sW    77m+++++C03C06fXQse,CD~-&) $r4   Fr   r}   r   z-expected mod_ to have 'graph' attribute, got N)r   r   r   r   r?  r   getrY   r   rC   rZ   rsplitr  ra   sortedr   range
_call_namer   r`   popset_submoduler    r  r[   r}   r   rd   re   )rv   r5  r'  r  r   r   baserI  	attrs_maprK   orig_fqnr   indexed_call_modulesr.  r   r6  rj   r`  mod_r_  s                      @r5   r  #UnflattenedModule._dispatch_modules  s'    %T*++EyyyU___ ii&A&A#&KL.1SjCIIcNsCj	t$++SXsO< ,  $	$Ff}!'sA!6#''-"!!&) % /=.B.B.D*H.45I.JK.JFAC.JLK< 1$s<01A$X1u5C9(+		#/f=FFJJ4P	 2
 ""/	0C\R /E	* **E*BHCsG$$"3		2o..--D"411,KDQUJ<X  'sDJJ7 .	 C- Ls   *I<c                 "    [        U SUUUU5      $ )Nr!   r   r   s        r5   r    UnflattenedModule.print_readable
  r   r4   )r   rr   r   r  r  r   r}   r   r  r   r   r   r  rQ   r   )r+   r,   r-   r.   r   r   r   r<  rE  rQ  r   r   r  r   r3   r   r   s   @r5   r!   r!   E  sg     59K&K +T1K KZ!6>@C*;8~ 
 
r4   r!   r)   r   rs   c                     [        U 5      n [        X5      n[        R                  R	                  UR
                  SSS9Ul        U$ )a  Unflatten an ExportedProgram, producing a module with the same module
hierarchy as the original eager module. This can be useful if you are trying
to use :mod:`torch.export` with another system that expects a module
hierarchy instead of the flat graph that :mod:`torch.export` usually produces.

.. note:: The args/kwargs of unflattened modules will not necessarily match
    the eager module, so doing a module swap (e.g. :code:`self.submod =
    new_mod`) will not necessarily work. If you need to swap a module out, you
    need to set the :code:`preserve_module_call_signature` parameter of
    :func:`torch.export.export`.

Args:
    module (ExportedProgram): The ExportedProgram to unflatten.
    flat_args_adapter (Optional[FlatArgsAdapter]): Adapt flat args if input TreeSpec does not match with exported module's.

Returns:
    An instance of :class:`UnflattenedModule`, which has the same module
    hierarchy as the original eager module pre-export.
z*do not trace into preprocessing the inputsT)reason	recursive)r   r!   r?   _dynamodisablerQ  )r)   r   ms      r5   r"   r"     sL    , #6*F&4A  %}}44	  ;  5  A Hr4   r}   r   c                   ^ [        [        [        U R                  5      5      5      nUR                  S:w  d  [        UR                  5      S:w  a0  [        SUR                  < S[        UR                  5       S35      eUR                  S   nU R                   Vs0 s H!  oDR                  S:X  d  M  UR                  U_M#     nn0 nUR                  R                  5        VVs0 s H  u  pxX_M	     n	nnUR                  R                  5        VVs0 s H
  u  pxXyU   _M     nnnUR                  UR                  5        [        U5      n
USU
  H  nXkR                     nX\   nU R                  U5         U R                  S	[         R"                  R$                  R&                  R(                  X45      mUR*                  R                  5        H  u  p~UTR*                  U'   M     SSS5        UR-                  TU4S
 j5        M     [/        X:S 5      nU4Ul        gs  snf s  snnf s  snnf ! , (       d  f       NO= f)a  Transform buffer and input mutations from their functionalized form
into copy_ nodes in the graph.

Functionalization represents a buffer mutation by passing the buffer as
an input and output. For example, consider the eager code:
    def forward(self, x):
        self.buffer += x
        return x * x

This corresponds to a graph that looks like:
    def forward(self, buffer, x):
        mutated_buffer = aten.add(buffer, x)
        mul = aten.mul(x, x)
        return (mutated_buffer, mul)

We want to inplace this into something that looks like the original
eager code:
    def forward(self, buffer, x):
        mutated_buffer = aten.add(buffer, x)
        buffer.copy_(mutated_buffer)
        mul = aten.mul(x, x)
        return (mul,)

Input mutations are handled similarly.
outputr   z8expected output node with op='output' and 1 arg, got op=z with z argsr   r   Ncall_functionc                    > U TL$ rQ   r*   )rN  new_nodes    r5   <lambda>5_inplace_buffer_and_input_mutations.<locals>.<lambda>  s
    1H;Lr4   )nextiterreversedr   r   r   r   rd   r   inputs_to_buffersra   buffers_to_mutater_   user_inputs_to_mutateinserting_aftercreate_noder?   opsatencopy_defaultr   replace_all_uses_withrB   )r}   r   output_nodereturn_argsr   input_name_to_nodemutation_name_to_input_namerT   
buffer_fqnbuffer_fqn_to_input_namenum_mutationsmutation
input_name
input_nodevuser_outputsr{  s                   @r5   r   r   ?  s#   : tHU[[123K~~!S)9)9%:a%?F{~~FXX^_bcncscs_t^uuz{
 	
 ""1%K %*KK$/D77m3K		4K   #% ,;+L+L+R+R+T +T-!
+T   
 ->>DDF#FMA 	
J//F   #
  &&'L'LM34M/0?
'3
""8,((!5!5!=!=
?UH !++-#$a  . - 	&&x1LM 0" ^45L%(KI # -,s%   H&6H&(H+H1;A3H77
I	c                     U R                    HH  nSUR                  ;  a  M  UR                  S   R                  5        H  u  p#U(       a  M  Us  s  $    MJ     g )Nnn_module_stack)r   r   r   )r}   r   r^  r~   s       r5   r   r     sM    DII-		"34;;=HD4	 >	  r4   c           	         U R                    H  nSUR                  ;  a  M  UR                  S   nUR                  5        VVs/ s H!  u  p4SU;   a  UR                  S5      S   OUPM#     nnnUtpgSnU H  n	[	        Xi5      (       a	  US-  nU	nM    O   U[        U5      :  d  M  [        R                  SUU[        U5      U-
  5        [        [        UR                  5       5      S U 5      UR                  S'   M     g s  snnf )Nr  r   r   r   zPnn_module_stack fqns %s at node %s do not form a stack! dropping last %d entries)r   r   r   rY   
_is_prefixr   logwarningr   r   ra   )
r}   r   r  r   _tfqnsprev_fqn	next_fqnsnum_valid_indicescurr_fqns
             r5   r   r     s    DII-))$56 +113
3 "%CIIcN143 	 
  $!H(--!Q&!#  " s?33KKbO$'88	 ,0_**,-.@/@A,DII'(= 

s   (Dc                 `    [        U 5      [        U5      :  =(       a    US[        U 5       U :H  $ )z2Check whether `candidate` is a prefix of `target`.N)r   )	candidaterK   s     r5   r  r    s+    y>CK'QF3CS^,D	,QQr4   
parent_fqn	child_fqnc                     U S:X  a  U$ U R                  S5      nUR                  S5      nUS [        U5       U:w  a  [        SU SU  S35      eSR                  U[        U5      S  5      $ )Nr   rO   zChild module 'z(' is not a descendant of parent module 'za'.This is currently unsupported.Please try to make child module attach to parent module directly.)rY   r   RuntimeErrorr   )r  r  parent_splitchild_splits       r5   _compute_accessorr    s    R##C(L//#&K &S&'<7YK'OPZ| \P P
 	

 88KL 1 3455r4   rN  yc                    S[         R                  R                  S[        4S jn[	        U R
                  [         R                  R                  5      (       d!  [        S[        U R
                  5       35      e[	        UR
                  [         R                  R                  5      (       d!  [        S[        UR
                  5       35      eU" U R
                  5      U" UR
                  5      :H  $ )Nr}   rs   c                 h  ^
 / n0 m
S[         4U
4S jjn[        U R                  5       H  u  p4[        R                  " X$R
                  5       Vs/ s H  n[        U5      PM     nnU[        R                  " X$R                  5      R                  5        VVs/ s H  u  pxU SU 3PM     snn-  nUR                  S;   a  UR                  OSn	UR                  U SUR                   SU	 SS	R                  U5       S
35        UT
[        U5      '   M     SR                  U5      $ s  snf s  snnf )Nrs   c                    > [        U [        R                  R                  5      (       a  S[	        T[        U 5         5      -   $ [	        U 5      $ )N%)rb   r?   r   Nodery   r   )r  	nodes_idxs    r5   arg_dump>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  s:    #uxx}}--S2c7!3444s8Or4   =)ry  get_attrr   z: [z](z, )r   )ry   r  r   r   tree_mapr   r   ra   r   rK   r   r   r   )r}   retr  r6  r   r  	args_dumpkeyvaluerK   r  s             @r5   
graph_dump,_check_graph_equivalence.<locals>.graph_dump  s   $&		S 	
 !-GA-3__Xyy-QR-QcS-QIR"(//(KK"H"N"N"P"PJC %q "P I %)GG/L$LT[[RTFJJ!Btwwiq499Y3G2HJK"#Ibh . yy~ Ss   D)D.
z+expected x.graph to be torch.fx.Graph, got z+expected y.graph to be torch.fx.Graph, got )r?   r   r   ry   rb   r}   rd   re   )rN  r  r  s      r5   _check_graph_equivalencer    s    %((.. S * agguxx~~..9$qww-I
 	
 agguxx~~..9$qww-I
 	
 agg*QWW"555r4   gmc                     Sn[        U SU 35      (       a  US-  n[        U SU 35      (       a  M  SU 3n[        XU5        U$ )Nr   _spec_r   )r[   r\   )r  specr6  r   s       r5   	_add_specr    sR    	A
"qcl
#
#	Q "qcl
#
#A3<DBdKr4   c                     U R                   R                  [        R                  U45      nU R                   R                  [        R
                  US45      nU$ )Nr   )r}   ry  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r5   _generate_flattenr    sF    hh$$V%8%84'BG&&x'7'7'1FIr4   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rQ   )r  r}   r  ry  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r5   _generate_flatten_specr    sB     RD!!$'I88!!)"="=?PQQr4   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rQ   )r  r}   r  ry  r   rT  )r  r   r  r   r  s        r5   _generate_unflattenr    sB     RD!!$'I88!!&"7"7%9KLLr4   r   c                     UR                  S5      Gt p#U HA  n[        XS 5      nUc    g [        U[        R                  R
                  5      (       d    g Un MC     [        XS 5      $ )NrO   )rY   r   rb   r?   r]   r^   )r   rK   rj   rk   rV   submods         r5   _get_submoduler    s\    \\#&NVD)>&%((//22  3t$$r4   module_to_addcreate_modulec           	         UR                  S5      Gt pE[        U5       H  u  pg[        XS 5      nUcK  Ub  U" SR                  US US-    5      5      nO[        R
                  R                  5       n[        XU5        [        U[        R
                  R                  5      (       d    gUn M     U R                  XR5        g )NrO   r   F)
rY   r  r   r   r?   r]   r^   r\   rb   
add_module)	r   rK   r  r  rj   rk   r6  rV   r  s	            r5   _add_submoduler  $  s     \\#&NVV$D)>(&sxxwQ'@A*Cv&&%((//22 % NN5(r4   ri  r4  c                 &    US:X  a  U $ U  SUS-
   3$ )Nr   r   r*   )ri  r4  s     r5   rf  rf  >  s$     640$qQ00r4   	call_namec                 b    [         R                  " [         R                  " U5      S-   U 5      S L$ )Nz(@\d+)?$)rematchescape)r  ri  s     r5   rR   rR   D  s%    88BIIdOk19=TIIr4   c                   "   \ rS rSr SS\R
                  R                  S\\R
                  R                  S4   S\	\\
\
S-  \4      S\\
\4   S\R
                  R                  \-  S-  4
S	 jjrS
 rS rS rSS jrS rS rS rS rS rSrg)_ModuleFrameiI  N
flat_graphr   .module_stackr   r)   c                   ^ ^^! UT l         UT l        UT l        UT l        UT l        UT l        TT l        UT l        U	T l        U
T l	        ST l
        T R                  S   u  T l        m!n[        T R                  US-   5      T l        T   Ub,  UT l        [        US5      (       a  UR                   O0 T l        O`T R
                  R#                  T R                  [%        [&        R(                  R+                  5       T!S95      T l        TR                   T l        T R                  R,                  T l        0 T l        0 T l        S T l        TGbF  [5        TR                  T R                  5      nUU U!4S jn[7        TR                  UT R                  U5        TR,                  R9                  U5      T l        T R                  T R                     (       aA  T R                  T R                     S   nUR                  R:                  T R                  l        T R                  T R                     R=                  [?        T R                  R                  T R                  R                  T R2                  T R                  US-   T R                  S95        U
R#                  T R                  5      nUGbs  T R                  Gbd  UR@                  RB                  S	:w  a"  [E        S
UR@                  RB                   35      eUR@                  RF                  [H        La"  [E        SUR@                  RF                   35      eUR@                  RK                  5       u  nnURF                  [H        La  [E        SURF                   35      eURF                  [L        La  [E        SURF                   35      eT R,                  RO                  S 5         [Q        URB                  5       Vs/ s H!  nT R,                  RS                  SU 35      PM#     nn0 nURT                   H!  nT R,                  RS                  U5      UU'   M#     [W        T R                  [I        U5      U4UR@                  5      n[Y        URZ                  5       H  u  nnT R,                  R]                  S[^        R`                  UU4[c        U[d        5      (       d  URf                  OSU 3S9n[c        U[d        5      (       a  Mm  URf                  T R                  ;   d  M  [h        Rh                  " T R                  URf                     Rj                  5      Ul5        UT R0                  T R                  URf                     '   M     S S S 5        T R                  R,                  Rm                  T R2                  5         / nURZ                   H  n[c        U[d        5      (       a  UR=                  URn                  5        M5  URf                  T R                  ;  a  UR=                  S 5        Mb  [c        U[p        [r        [t        [v        45      (       d  [E        S[G        U5       35      eUR=                  T R                  Ry                  T R                  URf                     5      5        M     [{        T R                  R                  UUR@                  5      nT R                  R,                  R}                  [^        R`                  US45      nT R                  R,                  R}                  [^        R`                  US45      n[Q        URB                  5       Vs/ s H9  nT R                  R,                  R}                  [^        R`                  UU45      PM;     nnURT                   V s0 s H:  n U T R                  R,                  R}                  [^        R`                  UU 45      _M<     nn S S S 5        T R2                  c  [E        S5      e[I        W5      T R2                  l?        WT R2                  l@        g g g s  snf ! , (       d  f       GN= fs  snf s  sn f ! , (       d  f       Nu= f)NFr   r   r~   c                    > TR                   (       a  TR                    SU  3OU nUTR                  ;   a  TR                  U   $ [        [        R                  R                  5       TS9nUTR                  U'   U$ )NrO   r  )r   created_modulesr   r?   r   r   )r   r^  r  parentrv   r~   s      r5   r  ,_ModuleFrame.__init__.<locals>.create_module~  sj    06

&**Qse,4/////55*588>>+;C-3$$T*r4   r   )r  parent_moduleparent_call_moduler   call_idxr)      z)expected in_spec to have 2 children, got z'expected in_spec.type to be tuple, got z)expected args_spec.type to be tuple, got z*expected kwargs_spec.type to be dict, got _positional_arg_ry  
_constant_)r   rK   r   r   z_expected input to be TensorArgument, SymIntArgument, SymBoolArgument, or SymFloatArgument, got z#parent_call_module must not be None)Ar  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   rf  r  r)   r[   r   rb  r   r?   r   r   r}   node_mapnode_to_placeholderr  r  r  r   r`   r   _SubmoduleEntryr@  num_childrenrd   re   rB   childrenr   r  re  r   contextr  r  inputsr  r  r  rb   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  ry  r   r   )"rv   r  r   r  r  r  r  r  r  r  r   r)   	num_callsr   r  base_module_framer?  	args_speckwargs_specrI  	arg_nodeskwarg_nodesr   rC  r  flat_arg_nodeinput_nodesrD  inputs_node	args_nodekwargs_noder6  rT   r~   s"   `      `                         @r5   r   _ModuleFrame.__init__J  s    %
$($.("!2"&"3"3B"7"i#DHHi!m< DK)0)A)ArDJ..22!%((.."2r:DK  DJ[[&&
 =?#% 8<(T^^DH 6==(DKKO&,ll&>&>x&HD#  0$($5$5dnn$Ea$H!'8'?'?'H'H$dnn-44#{{"&++"4"4'+'>'>&];;	 &))$..9	 T[[%<  --2$?	@Q@Q@^@^?_`    %%U2$=i>O>O>T>T=UV  &/%6%6%?%?%A"I{~~U*$?	?OP  t+$@AQAQ@RS  ++D1  %Y%;%;<< JJ**-=cU+CD<   !'//D(,

(>(>t(DK% 02KK9%{3%%	
 !*)*:*: ;HC$(JJ$:$:*'//'- $.c3C#D#D  HH#-cU!3 %; 	%M "#'788 xx4??2-1YYtsxx7P7U7U-V*) 001JK! !< 2B ""33D4K4KL:<&--E!%)9::#**5;;74??:#**40)! . . / 0	    #1#MMQRW[M![#  $** KK33DOOEJJ4OP' .. 2KK&&%% !KK--;;$${A&6	 #kk//==$${A&6
 #9#9#9:: KK%%33H4D4DyRSnU:   )00	 1 t{{((66 ((;*:  1	  S M^ &&.$%JKK+0+;D##(-8D##*O &= ( 21LS MLsS   `* (`%C;`*A&`*$F aA `<aAaa%`**
`9<
a
ac                    U R                   S:X  a  [        SU S35      eUR                  U R                  La  [        S5      eU R                  R	                  S 5         U R                  R                  UR                  UR                  S9nS S S 5        [        R                  " UR                  5      Wl	        X R                  U'   g ! , (       d  f       NB= f)Nr   zCannot add placeholder z to root module6expected x.graph to be flat_graph, got different graph)	type_expr)r   rd   r}   r  r  r   r   re   r  r   r  )rv   rN  placeholder_nodes      r5   add_placeholder_ModuleFrame.add_placeholder   s    88r> #:1#_!MNN77$//) H  ZZ((.#zz55aff5O / !%		!&& 1&6  # /.s   /C


Cc                    [         R                  " [        R                  R                  U R
                  UR                  5      n[         R                  " [        R                  R                  U R
                  UR                  5      nU R                  R                  UR                  X#5      n[        R                  " UR                  5      Ul        X@R                  U'   U$ rQ   )r   tree_map_onlyr?   r   r  r  r   r   r}   ry  rK   r  r   r  )rv   rN  r   r   r   s        r5   copy_sym_call_function#_ModuleFrame.copy_sym_call_function  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar4   c                 p   UR                   U R                  La  [        S5      eXR                  ;   a  U R                  U   $ U R	                  SU S35        XR
                  ;   a  U R
                  U   $ UR                  S:X  d&  U R                  R                  U R                  5      c  U R                  U5        U R                  bm  U R                  R                   R                  U R                  5         U R                  R                  SU R                  R                  U5      5        S S S 5        U R
                  U   $ UR                  S:X  Gas  UR                   ["        R$                  R&                  R(                  R*                  ["        R$                  R&                  R,                  R.                  ["        R$                  R&                  R0                  R*                  ["        R$                  R&                  R2                  R4                  ["        R$                  R&                  R6                  R.                  ["        R$                  R&                  R8                  R.                  4;   d5  [;        UR                   S5      (       a:  UR                   R<                  S:X  a   U R?                  U5        U R                  U   $ U R                  R                  U R                  5      b  U R@                  RC                  X5      $ [E        S	UR                   S
U 35      e! , (       d  f       GN= f)Nr  zremap_input(r  r   r   ry  r,   	_operatorz(Could not run remap_input() on op type: z
 for node )#r}   r  rd   r  r;  r  r   r   rb  r   r  r  r  r  
insert_argr  rK   r?   r  r  sym_sizerC   rV   r  unbindsumdim_IntListviewdiffr[   r,   r  r   readr  )rv   rN  s     r5   r  _ModuleFrame.remap_input  sS   77$//) H  ==##

\!A&'(((++A..DDM!T%;%;%?%?%I%Q   #&&2 [[&&778O8OP++66q$++:Q:QRS:TU Q++A..TT_$HH		''++		##++		%%))		""..		##++		##++ ,//AHH4G4G;4V ''*==####''1= ::??4++:144&
1#N 1 QPs   96L&&
L5c                 n   S nU R                   R                  5        H/  nUR                  R                  S0 5      nUc  UnM&  X1:w  d  M-  0 n  O   U(       a_  U R                  c  [        S5      eXR                  R                  S'   U R                   R                  5        H  nUR                  S	 M     g g )NcustomzBparent_call_module must not be None when uplifting custom metadata)r  r   r   rb  r  rd   )rv   custom_metar   	curr_metas       r5   uplift_common_custom_metadata*_ModuleFrame.uplift_common_custom_metadataK  s    MM((*D		h3I"''  + &&.$X  6A##((2,,.IIh' / r4   c           	      h  ^ ^ T R                   R                  T R                  S 5        / nT R                  R	                  T R
                  5      nUGb   T R                  Gb  UR                   H  n[        U[        [        [        [        [        45      (       aW  UR                  T R                  ;   a*  UR!                  T R                  UR                     5        Mq  UR!                  S 5        M  [#        SU 35      e   U 4S jm[%        T R&                  [)        U4S jU 5       5      UR*                  5      n[-        T R                  R&                  T R.                  UR*                  5      nUnO/ nT R0                   Hb  nUR2                   HO  nUR                  T R                  ;  d  M  UR!                  U5        UR!                  T R0                  U   5          M`     Md     T R.                  n[5        U5      S:X  a  US   n[        U[6        [8        R:                  R<                  45      (       d  [?        S[A        U5       35      eT RB                  RE                  U5        Uc  g [        U[8        R:                  R<                  5      (       a  URF                  R	                  S5      O*U V	s/ s H  oRF                  R	                  S5      PM     sn	URF                  S'   T RI                  5         [5        U5      S:X  a   Uc  UT R                  R0                  US   '   g [K        U5       Hw  u  pUc  M
  [8        R:                  RM                  U5      U
   RN                  nURF                  R	                  S5      URF                  S'   UT R                  R0                  U'   My     g s  sn	f )Nz'Unsupported data type for output node: c                    > U c  g TR                   U R                     nUTR                  ;   a  TR                  U   $ UTR                  ;   a  TR                  U   $ [	        SU  STR
                   35      e)NzCould not find output node z	. Graph: )r  r   r  r  r  r}   )rx  	seen_noderv   s     r5   get_actual_output_node=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node~  sx    > OOFKK8	-==33$":"::33I>>&5fXYtzzlS r4   c              3   4   >#    U  H  nT" U5      v   M     g 7frQ   r*   )rS   rx  r*  s     r5   rW   0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s     P<,V44<s   r   r   z8expected graph_outputs to be list or torch.fx.Node, got val)(r  rg  r   r   rb  r  r  r   rb   r   r   r   r   r   r   r  r   r  r  r)   rB   rU  r  r  r  usersr   r   r?   r   r  rd   re   r}   rx  r   r%  r  Proxyr   )rv   orig_outputsr?  rx  tree_out_node
parent_outgraph_outputs	orig_node	user_nodeor6  orig_output	proxy_outr*  s   `            @r5   finalize_outputs_ModuleFrame.finalize_outputsc  s     40**..t~~>	 T[[%<#++&&'((	 	 {{doo5$++DOOFKK,HI$++D1&A&J ! ,( 0P<PP""M
 0F""D$;$;Y=O=O0J BOMM!]]	!*I ~~T__<$++I6%,,T]]9-EF "1 + 00J=!Q& -a 0-$)>?? J4P]K^J_`  	

-(  -77 ""5)-:;]&&**U#]; 	
 	**,|!i&74>DKK  a1"+L"9&!HHNN:6q9>>	(3(8(8(<(<U(C	u%4=$$[1 #: <s   5$N/c                     U R                  SUR                  5       5        U R                  R                  XR                  5      U R
                  U'   XR                  UR                  '   g )Ncopying)r;  format_noder}   	node_copyr  r  r  r   r   s     r5   	copy_node_ModuleFrame.copy_node  sL    

9d..01"jj2249I9IJd%)		"r4   c                    [        U R                  R                  5       H>  u  pU R                  XR                  R                  S5      UR                  5       5        M@     SnU R                  U   nUR                  S:X  a7  U R                  U5        US-  nU R                  U   nUR                  S:X  a  M7  U R                  U5        U R                  R                   H&  nUR                  S:X  d  M  U R                  U5        M(     g )Nr  r   r   r   rx  )
r  r  r   r;  r   rb  r>  r   r@  run_from)rv   r6  r   node_idxs       r5   	run_outer_ModuleFrame.run_outer  s     !6!67GAJJq))--(9:D<L<L<NO 8 zz(#gg&NN4 MH::h'D gg&
 	h OO))Dww("t$ *r4   c                 >    U R                   (       a  [        U0 UD6  g g rQ   )r  r;  )rv   r   r   s      r5   r;  _ModuleFrame.print  s    <<4"6" r4   c                 D   SnU[        U R                  5      :  Ga}  U R                  U   nUR                  S:X  a  [        SU 35      eU R	                  5         U R	                  SXR                  5       5        U R	                  U R                  5        [        U R                  5      nUR                  S:X  a  US:X  a  U$ U R                  5         U$ [        UR                  R                  S0 5      5      S:X  a  [        SU 35      eUR                  S   nSS	KJn  [        U5      S:X  a  Xe;   a  U R                  nOeUR                  S   R                  5        VV	V
s/ s H9  u  nu  pU	U	(       a  U
OS S
U;   a  [        UR                  S
5      S   5      OS4PM;     nn	nn
US U U R                  :w  aI  U R                  5         U R	                  SU R                   5        U R	                  U R"                  5        U$ Uc  [        S5      e[%        U R                  U5      (       a  Xt   nU R	                  SU5        ['        UR                  S   R)                  5       5      U   n[+        U R,                  U R                  U R.                  U R0                  U R2                  U R4                  U U R                  U/-   UR                  S
5      S   U R6                  5
      R9                  U5      nUS-  nGM  XpR                  :w  a  [        SU SU R                   35      eUR                  S:X  a2  U R2                  U R:                     R=                  UR>                  5        U RA                  U5        US-  nU[        U R                  5      :  a  GM|  g g s  sn
n	nf )Nr   r   z%unexpected placeholder node at index STEPrx  r   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYr   r  	outliningz"node_module_stack must not be NonezCreating new stack frame forzexpected node_module_stack z to equal module_stack r  )!r   r   r   rd   r;  r>  r  r:  r   rb  r  (torch._export.passes._node_metadata_hookrK  ra   rC   rY   r   r}   r  r   keysr  r  r  r  r  r  r   rC  r  r  rK   r@  )rv   rD  
module_idxr   depthr  rK  node_module_stackrT   r^  r~   next_modulenext_module_keys                r5   rC  _ModuleFrame.run_from  sK   
TZZ(::h'Dww-'$'LXJ%WXXJJLJJvx)9)9);<JJt(()))*Eww("A: $O %%'499==!2B78A="%MdV#TUU"ii(9:O
 O$).A %)$5$5! *.3D)E)K)K)M% *N:D	 "14AGGCL,-q
 *N " % !%(D,=,== %%'

;1

4::& ($%IJJ$++->?? 06

9;G #'tyy1B'C'H'H'J"KE"R'OOJJOO%%OO((%%5#))#.q1** (8$  a
 !$5$55$12C1DD[\`\m\m[no  ww*$/33DKK@NN4 MH} TZZ((F%s   "A N)r  r  r  r   r}   r   r)   r   r  r  r  r  r   r  r  r  r  r  r  rQ   )rs   N)r+   r,   r-   r.   r?   r   r   rB   r  r   ry   rC   r   r   r   r!   r   r  r  r  r%  r:  r@  rE  r;  rC  r3   r*   r4   r5   r  r  I  s     CGt9HHNNt9 UXX]]C'(t9 5cDj#!567t9  %8 89t9 $$'884?t9l7,\(0\>|*
%&#
ar4   r  c                       \ rS rSr% \\S'   \R                  R                  \S'   \R                  R                  \S'   \\S'   \\S'   \R                  R                  \S'   Srg	)
r  iB  r  r  r  r   r  r)   r*   N)r+   r,   r-   r.   ry   rA   r?   r]   r^   r   r  rC   r3   r*   r4   r5   r  r  B  s;    O88??"%	HMHHOOr4   r  
orig_graphroot_modulec                 F   0 n[        [        5      n[        [        5      n0 n[        U [	        U R
                  5      UUUUS S/SUR                   Vs0 s H-  nUR                  (       d  M  UR                  UR                  _M/     snUS9R                  5         X44$ s  snf )N)r   Nr   r   )r)   )
r   r   rZ   r  rB   r   r   r?  r   rE  )rV  rW  r  r  r  r  r  s          r5   r   r   L  s    +-J5@5FL&1#&6J24Oj	
 %66	
6 'EIIu&6	

   ik##	
s   B,Br  r7  rj   c                 .   US:X  aY  [        UR                  5       5      SS   H9  n[        X5      b  M  [        X[        R
                  R                  5       5        M;     / n[        U R                  R                  5       5       HM  u  pVUc  M
  X%-   n[        XaUR                  S5      S   S-   S9  [        X5        UR                  X   XV45        MO     UR                  [        R                  " S5      S9  U H  u  punU R!                  XV5        M     g )Nr   r   r   r   rO   )rj   )r  )r   rN  r  r  r?   r]   r^   r`   ra   r  rY   delattrr   sortr  
itemgetterregister_module)r  r7  rj   r   r  r   childr.  s           r5   r  r  e  s     |	()!"-Cf*2vEHHOO,=> . HFOO1134=mESYYs^A5F5LM56 5 MMh))!,M-"t+ #r4   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   i{  a  
Collect the intermediate values of mutations in a graph.

Example: in the following graph, suppose that buf_in and buf_out
are the input and output values of a buffer.

    buf_in = placeholder()
    ...
    ival1 = f0(buf_in, ...)  # inside self.n0(...)
    ...
    ival2 = f1(ival1, ...)  # inside self.n1(...)
    ...
    buf_out = f2(ival2, ...)  # inside self.n2(...)
    return buf_out, ...

Here ival1 and ival2 are intermediate values created inside
calls to n0 and n1 respectively, and used inside calls to
n1 and n2 respectively.
c                 ,    [        [        5      U l        g rQ   )r   rZ   node_names_by_fqnru   s    r5   r   _IVals.__init__  s    !,S!1r4   c                     [        U[        R                  R                  5      (       a  UR                  R
                  $ grE   )rb   r?   _ops
OpOverload_schema
is_mutable)rv   rK   s     r5   _is_mutable_IVals._is_mutable  s,    fejj3344>>,,,r4   c                    UR                   S:w  a  [        SUR                   < 35      eU R                  UR                  5      n[	        SUR                  U5        U(       dY  [        [        UR                  S   R                  5       5      5      u  pEU R                  U   R                  UR                  5        UR                  UR                  S   5      $ )z9
Read state corresponding to a given intermediate value.
ry  z,expected node.op to be 'call_function', got zChecking mutabilityr  r   )r   rd   rh  rK   r;  r~  r  r   r   ra  r  r   r  r   )rv   mfr   br   r.  s         r5   r  _IVals.read  s    
 77o% >twwkJ  T[[)#T[[!4 (499->#?#F#F#HIJFC""3'++DII6~~diil++r4   c           
      <   U H  nU H  nUR                   R                  nU R                  UR                     nUR                   Vs/ s H  ofR
                  U;   d  M  UPM     nnU H  nUR                  U5         UR                  S[        R                  R                  R                  R                  UR                  S   U45      n	[        R                  " UR                  5      U	l        SSS5        M     M     GM     gs  snf ! , (       d  f       M  = f)zD
Update states corresponding to intermediate values that were read.
ry  r   N)r)   r}   ra  r   r   r   r  r  r?   r  r  r  r  r   r  r   )
rv   
partitionsshared_submodulesr  r}   
node_namesr4  r   r   r{  s
             r5   r_   _IVals.update  s     ",***!33EII>
$)KKHKq66Z3GKH!D ..t4#(#4#4+!IINN0088!YYq\40$
 )-		$))(< 54 "	 + ", I 54s   DD=A4D	
D)ra  N)
r+   r,   r-   r.   r>   r   rh  r  r_   r3   r*   r4   r5   r   r   {  s    (2
,$=r4   r   r  c                     UR                  5        H9  u  p4U(       a  [        X5      OUnU H  n[        X5      n[        XVU5        M     M;     g rQ   )ra   r   r   r\   )r  rW  r  r  namesr)   r   r.  s           r5   r  r    sF    
 ',,.	6?;2[D"#CF#&  /r4   c                    0 nU  GHV  n[        U5       GHB  u  p4[        UR                  UR                  5      n[	        UR
                  U5      nSnUS U  H  n[        UR                  UR                  5      (       d  M*  UR                  n	UR                  UR                  :X  a  UR                  S5      Gt p[        X5      R                  R                  U5        [        UR                  UR                  5      n[	        UR
                  U5      nXR                  l        XU'     GM  U(       a  M  U	R                  XhR                  5        SnM     GME     GMY     U$ )NFrO   T)r  rf  r   r  r  r  r  r)   r  rY   r   r`   rg  r  rK   rh  )ro  r5  rp  r6  r  r  rK   deduplicatedseenr  rj   r   seen_child_fqnseen_targets                 r5   r  r    s(    '!"34HA"599enn=I&u'7'7CF L)"1-+DKKFF"00F xx599,
 )/S(9/?HHLLTR)3DHHdmm)L&7!,,n' ;F007=K	:)\ ,,V[[A'+= . 5 (L #"r4   r0  scopemodule_id_to_inputs_removedc           	      @  ^^ Tc  [        [        5      m[        U 5      T;   a  [        U 5      T[        U 5         0$ U R                  R	                  5        HN  u  nm[        [        ST5      UX$/-   T5      nUR	                  5        H  u  pgTU   R                  U5        M     MP     [        U SS5      nUb  [        UR                  5      S:X  a  T$ [        U[        R                  R                  5      (       d  [        S[!        U5       35      e[#        [%        S UR                  5      5      n	[        U	5      S:X  a  SOU	S   n
[%        S UR                  5      nU HW  n['        XR(                  5      mTc  M  [        T5      T;   d  M.  [+        [%        UU4S	 jUR,                  5      5      Ul        MY     0 nU	 H  nUR.                  U;  a  M  SnXR.                      HM  nUR1                  S
5      nUS[        U5       U Vs/ s H  nUR1                  S5      S   PM     sn:X  d  MK  Un  O   Uc  M{  XU'   M     [        5       nUR	                  5        H  u  p[        UR2                  5      S:  a  U[        U5      S n[5        U U5      n[        U[        R6                  [        R8                  45      (       d  [        S[!        U5       35      eUR;                  U
5         UR=                  SS
R?                  U5      5      nSSS5        URA                  WSS9  URC                  U5        URE                  UR.                  5        M     [        U [F        5      (       a  U RI                  5         [        U 5      U0$ s  snf ! , (       d  f       N= f)a  Sink params, buffers, and constants from graph inputs into get_attr nodes.

Exported modules are purely functional, so they pass their parameters and
buffers in as inputs to the graph.

To replicate eager's semantics, we need to get them from the module state
via get_attr instead.

module: GraphModule, potentially containing nested submodules.
inputs_to_state: mapping graph input names to the corresponding key in the state_dict.
scope: tracks where we are in the module hierarchy, so that we can emit the
    right `getattr(self, "foo.bar")` calls, etc.
module_id_to_inputs_removed: records inputs removed by child modules, mapping
    the module object id to the list of placeholder node names in the child module
    that were removed.
Nztorch.nn.Moduler}   r   z)expected graph to be torch.fx.Graph, got c                      U R                   S:H  $ )Nr   r   r4  s    r5   r|  _sink_params.<locals>.<lambda>'  s    144=#8r4   r  c                      U R                   S:H  $ )Nr   r~  r  s    r5   r|  r  +  s    )>r4   c                 :   > U R                   T[        T5         ;  $ rQ   )r   r   )r4  r{  	submodules    r5   r|  r  3  s    aff,G9,VVr4   rO   r   zBexpected state_attr to be torch.Tensor or torch.ScriptObject, got r  T)propagate_meta)%r   rZ   r   r`   ra   r  r
   r_   r   r   r   rb   r?   r   r   rd   re   r   filterr   rK   rB   r   r   rY   r/  r   rg   ri   r  r  r   r  
erase_noder  r   r   )r)   r0  rz  r{  r   submod_id_to_inputs_removedrT   r  r}   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitrN  inputs_removed	attr_path
state_attrr{  r  s      `                  @r5   r  r    s:   , #*&1#&6#	&z006
76
CDD "??002i&2"I.FN'	'
# 0557DA'*11!4 8 3 FGT*E}EKK(A-**eUXX^^,,HeVWW&8%++FGF [A-T6":N >L!fkk2	  R	]6Q%QVIIDI " @B99O+
!)),Bxx}H#e*%5)I5a!''#,q/5)II%
	 - )3&/ 4  #uN4::<tzz?Q"3u:<0I0CJj5<<9K9K*LMM$XY]^hYiXjk 
 &&~6 ,,Z)9LM 7 &&x&E499%! =$ &+,,vJ''U *J> 76s   N

."N
N	)TrQ   )r   )gr   r  loggingr  r  collectionsr   collections.abcr   
contextlibr   r   dataclassesr   enumr   typingr	   r
   r?   torch.fx._pytreer   _pytreer  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch.exportr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   r   r   torch.fx._symbolic_tracer   torch.fx.graph_moduler   r   r   r   r   _remove_effect_tokens_passr   	getLoggerr+   r  __all__r$   r8   rF   rH   rg   ri   r]   r^   ry   boolrn   rp   r   r    ABCr   r!   r"   r   r   r   r   r  r  r  r  r   r  r  r  r  r  r  rC   rf  rR   r  r  r   r   r  r   rZ   r  r  r   r  r*   r4   r5   <module>r     s   
    	 # $ %  !    $ $ $ $ ? ( 3	 	 	 < U U 7 = !  $     ( (" =0llU///%((//A=0xx=0 =0 	=0
 =0@	 	\
 \
~.".%((// ."bcgg ,S
 S
n JN!!0?$0F!!HH)88>>H))H) 
H)VUXX^^ d
 $NR
6# 6# 6# 6$6 6EHHOO 6B%((// C %((..  R003DDR
XX]]RM003DDM
XX]]M% % %* >B	)	)) 88??) SE588??23d:	)41S 1S 1S 1JS J J J
v vr   $EHHNN $AR $4 GI,HHOO,(,S#X,@C,,B= B=J	'	'"	' S#c(]#	'(#^ ?C	t(HHOOt(#tCy.)t( 9t( "&c3s8m!4t!;	t(r4   