
    [Thl                    &   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JrJr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!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      r8Sq9\S 5       r: SVS\\Rv                  \Rx                  \Rz                  R|                  4   S\Rz                  R|                  S\?S\8S\@4
S jjrA " S S5      rB " S S\B\Rz                  R|                  5      rC " S S \B\Rz                  R|                  5      rD " S! S"\ R                  5      rF " S# S$\Rz                  R|                  5      rG SWS%\"S&\\F   S'\G4S( jjrHS)\R,                  R                  S*\#S'S4S+ jrJS, rKS-\?S.\?S'\?4S/ jrLS0\Rz                  R|                  S1\Rz                  R|                  4S2 jrMS3\Rz                  R|                  S'\?4S4 jrNS3\R,                  R                  S'\R,                  R                  4S5 jrQS3\\R,                  R                  \C\G4   S'\R,                  R                  4S6 jrRS3\\R,                  R                  \C\G4   S'\R,                  R                  4S7 jrSS8\Rz                  R|                  S\?4S9 jrT SWS8\Rz                  R|                  S\?S:\Rz                  R|                  S;\\\?/\Rz                  R|                  4      4S< jjrUS=\?S>\VS'\?4S? jrWS@\?S=\?S'\@4SA jrX " SB SC5      rY\ " SD SE5      5       rZSF\R,                  R                  SG\G4SH jr[ SXSI\Rz                  R|                  SJ\\\?\V4   SK\?4SL jjr] " SM SN5      r^S3\R,                  R                  SG\GSO\\\?\_\?   4   4SP jr`SQ ra SWS%\Rz                  R|                  SR\\\?\b\?   4   SS\b\?   ST\\\\V\_\?   4      4SU jjrcg)Y    N)defaultdict)contextmanager)deepcopy)	dataclass)Enum)AnyCallablecastOptionalUnion)FakeScriptObject)reorder_kwargs)	ConstantArgumentExportedProgramExportGraphSignature	InputKindModuleCallSignatureSymBoolArgumentSymFloatArgumentSymIntArgumentTensorArgument)is_fx_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)	_AttrKind1   	parameterbufferconstantmodule N)	__name__
__module____qualname____firstlineno__	PARAMETERBUFFERCONSTANTMODULE__static_attributes__r,       N/var/www/auris/envauris/lib/python3.13/site-packages/torch/export/unflatten.pyr&   r&   1   s    IFHFr6   r&   Tc               #   8   #    [         n Sq  S v   U q g ! U q f = f7f)NF)RUN_WITH_INTERPRETER)old_flags    r7   _disable_interpreterr;   ;   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  a>  [        U [        R
                  R                  5      (       d   eUR                  X`5        MV  U[        R                  :X  a3  [        U [        R                   5      (       d   eUR#                  X`US9  M  U[        R$                  :X  a\  [        U [&        5      (       a   S5       e[        U [        R                   [        R(                  45      (       d   e[        XU 5        GM  U[        R*                  :X  d  GM$  [        U [        R
                  R                  5      (       d   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      r7   	<genexpr>_assign_attr.<locals>.<genexpr>\   s&      !;IA D) !;s   '	')r@   z2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrtorchnnModuleupdate_modulesitemsr&   r1   
isinstance	Parameterregister_parameterr2   Tensorregister_bufferr3   r   ScriptObjectr4   )
r<   r=   r>   r?   r@   prefixfield
to_modulestsrI   s
            @r7   _assign_attrr`   H   s    \\#&NV J#&5#I9d++	4):;II !*!3!3!9!9!;  $ 
   		+++h(:(:;;;;((9)***h5555%%e*%M),,,!*  DCD  LL&&    Ih/)***h8888Ih/+  r6   c                   :    \ rS rSr% \\   \S'   S\\   4S jrSrg)_SubmoduleBase{   _tyreturnc                     U R                   $ rD   )rd   selfs    r7   	type_name_SubmoduleBase.type_name~   s    xxr6   r,   N)	r-   r.   r/   r0   r   str__annotations__ri   r5   r,   r6   r7   rb   rb   {   s    	#8C= r6   rb   c                      ^  \ rS rSr% Sr\\R                  R                     \	S'    SS\R                  R                  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.
graph_modulegraphtyc                 p   > [         TU ]  5         Xl        X l        X R                  l        [
        U l        g rD   )super__init__rp   rd   owning_moduler9   _run_with_interpreter)rh   rp   rq   	__class__s      r7   rt   InterpreterModule.__init__   s,    
 	
#'

 %9"r6   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      :X  d   e[        U5      [        U R                  5      :X  d   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rD   r,   )rF   
kwarg_namekwargss     r7   rJ   ,InterpreterModule.forward.<locals>.<genexpr>   s'       &1
!V+ 'F:&&1s   
rp   enable_io_processingF)ro   r   rP   compileris_dynamo_compilingrv   typeforwardlist	arg_nameslenextendtuplefxInterpreterrp   run)rh   argsr|   arg_listkwarg_namess     `  r7   r   InterpreterModule.forward   s     ,V.VV,NN..008R8R ))*224I$I&II
  :"nnS]_=  &1   ;'3v;6668}DNN(;;;;X88''JJ'?CC,1 r6   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 )Nro   placeholder)rP   r   GraphModulerp   __dict__lintr   nodesopappendr>   rh   nodes     r7   finalizeInterpreterModule.finalize   sr     ).(<(<T::(Nn%

 JJ$$Dww-'%%dkk2 %r6   c                 "    [        U SUUUU5      $ )Nr!   r   rh   print_outputinclude_strideinclude_devicecoloreds        r7   print_readable InterpreterModule.print_readable   #     
 	
r6   )rv   rd   r   rp   rD   TFFF)r-   r.   r/   r0   __doc__r   rP   r   r   rl   Graphrk   rt   r   r   r   r5   __classcell__rw   s   @r7   r!   r!      so    
 588//00
 !	:xx~~	: SM	: 	:!F3& 
 
r6   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   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 Nr   )rs   rt   rT   rO   getattrrd   _call_modules
_num_calls)rh   r   r   accessorrw   s       r7   rt   $InterpreterModuleDispatcher.__init__   sd    |$Q00HDGLOX$FG ?&&)r6   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)rh   r   r|   call_modules       r7   r   #InterpreterModuleDispatcher.forward   sd    ((9??Q.#d6H6H2II	/// 	DO	s   A A c                     U R                   $ rD   )r   rg   s    r7   r   (InterpreterModuleDispatcher.call_modules   s    !!!r6   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)rh   r   r   r   r   modoutputss          r7   r   *InterpreterModuleDispatcher.print_readable   s[     ))
 * 	 * 	 
 yy!!
s   A )r   rT   r   rd   r   )r-   r.   r/   r0   r   rM   rk   r   r!   rt   r   r   r   r5   r   r   s   @r7   r"   r"      sG    c#h d;L6M "
 " "r6   r"   c                       \ rS rSrSr\R                   SS\R                  S\R                  S\	\
   S\\\\
4      S\	\
   4
S	 jj5       rS
rg)r    i
  zF
Adapts input arguments with ``input_spec`` to align ``target_spec``.
Ntarget_spec
input_spec
input_argsmetadatare   c                     g)z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr,   )rh   r   r   r   r   s        r7   adaptFlatArgsAdapter.adapt  s     	r6   r,   rD   )r-   r.   r/   r0   r   abcabstractmethodpytreeTreeSpecr   r   r   dictrk   r   r5   r,   r6   r7   r    r    
  ss     	 .2__ OO I	
 4S>* 
c r6   r    c                   p   ^  \ rS rSr SS\S\\   4U 4S jjjrS rS r	S r
S rS	 r    SS
 jrSrU =r$ )r#   i  export_moduleflat_args_adapterc           	      2  >^) [         T*U ]  5         UR                  R                  b  [	        S5      eUR
                   Vs/ s H  o3R                  PM     nnUS   S:X  d   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        ['        XPR                  5        [)        5       U l        [-        XP5      u  pgU R*                  R/                  UR1                  5       U 5        [3        UR4                  X5        UR6                  U l        / U l        UR:                  n[=        5       n	0 n
U R                  R>                   H  nX   n[A        U5      U
;  aB  [        RB                  RE                  URG                  5       URH                  S9U
[A        U5      '   [K        U
[A        U5         U U[L        RN                  S9  U	RQ                  U5        M     [=        U R                  RR                  5      n[=        5       n0 nU R                  RT                   H  nX;   a  SnURV                  U   nOSnX   n[A        U5      U;  a  URG                  5       U4U[A        U5      '   [K        U[A        U5         S   U U[L        RX                  US	9  URQ                  U5        M     UR[                  5        H  u  nnX;   d  X;   a  M  Sn[A        U5      U;   d)  []        U[        RB                  RD                  5      (       d  SnU(       aK  [A        U5      U;  a  US4U[A        U5      '   [K        U[A        U5         S   U U[L        RX                  SS	9  M  [A        U5      U
;  a  UU
[A        U5      '   [K        U
[A        U5         U U[L        RN                  S9  M     0 nURV                  R[                  5        Hz  u  nn[A        U5      U;  a=  []        U[        R^                  5      (       a  URG                  5       nUU[A        U5      '   U[A        U5         n[K        UU U[L        R`                  S9  M|     [c        [d        5      m)[=        5       nU)4S
 jn[=        5       nU R                  Rf                   GH  nURh                  [j        RN                  :X  d/  URh                  [j        RX                  :X  a  URl                  (       a  [o        URp                  S5      (       d   e[]        URr                  [t        5      (       d   eU" [A        UR:                  URr                     5      URp                  Rv                  URr                  5        URQ                  URr                  5        URQ                  URr                  5        GM  URh                  [j        RX                  :X  a  URl                  (       a?  URh                  [j        Rx                  :X  d!  URh                  [j        Rz                  :X  d  GM~  [o        URp                  S5      (       d   e[]        URr                  [t        5      (       d   eU" [A        URV                  URr                     5      URp                  Rv                  URr                  5        URQ                  URr                  5        GM"     URV                  R[                  5        Hf  u  nnUU;  d  M  [A        U5      T);   d   S5       eT)[A        U5         S   u  nnU" [A        U5      UU5        URQ                  WRr                  5        Mh     UR:                  R[                  5        HF  u  nnUU;  d  M  [A        U5      T);  a  M  T)[A        U5         S   u  nnU" [A        U5      UU5        MH     0 n T)R1                  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R1                  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 lC        SU lD        [        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         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 NYIr    unflattened_moduleF)requires_grad)r?   T)r?   r@   c                 4   > TU    nUR                  X45        g rD   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r7   add_to_consts_map5UnflattenedModule.__init__.<locals>.add_to_consts_map  s    "6*Ii56r6   namez?Constants should be either aliased or appear in graph signaturer   @r   remove_duplicate)Jrs   rt   graph_signaturebackward_signature
ValueErrormodule_call_graphfqnr   rp   rP   r   r   ru   r   ro   metaadaptedr9   rv   _inplace_buffer_mutations_IValsivals_outline_submodulescreatevalues_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrM   
parametersidrQ   rW   cloner   r`   r&   r1   addnon_persistent_buffersbuffers	constantsr2   rU   rV   rY   r3   r   r   input_specskindr   r@   rN   argr>   rk   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   r   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   )+rh   r   r   entryfqn_listexport_graphseen_modules
seen_attrsr   assigned_paramsid_to_paramr   paramr   assigned_buffersid_to_bufferr@   r)   tensor	is_bufferid_to_constr   r*   	_constantconsts_targetsr   added_params_bufferss
const_nameconstph_name_inputs_to_statenode_targetttargetsnredirected_call_indicesr   i	fqn_orderr   rw   s+                                            @r7   rt   UnflattenedModule.__init__  s+   
 	((;;GDEE+8+J+JK+J%II+JK{b    3 34'(E(EFXX^^%
#'

 !)-*I*I!J!2!..33	*.		&' %9"!,0D0DEX
 $7|#J  	

,--/6 	-55tH!.!@!@*,! #--
$'E57((33D$E%y+).););KKM1D1D *< *BuI& BuI&#--	 % 4 "%T%9%9%P%P!Q%(UCE((00D-"
&006!
#)&z-,2LLNJ+GRZ(RZ(+#**%   &% 1, ',,.LD&&$*BI&z\)**2 2 !	vJl2 0LF,  F,Q/'..# f:[0.4K6
+6
+'11	9 /H NP*44::<MC(|;.h55'~~/H,4BxL)#BxL1I#,,	 = 8C47H
#&5	7 *-%%11Avv,,,)***q||quuf----!!((C0000!}//9:AEEJJ ""188,$((29+++ALL66Y66666Y111quuf----!!((C0000!}..qxx89155::qxx ""188,+ 20 "/!8!8!>!>!@J/uI+UTU+'5	215
!"U)WjA$((2 "A )3399;KC..f:Z/ '6
3A6
!"V*gs; < 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),

w LB 2 S ?
#

 ?s6   e:,e?<
f
f(
f	6f	f.ff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:rp   )r  printrN   rV   rp   rP   r   r   )rh   r   r   s      r7   _print_graphUnflattenedModule._print_graph  sT    **,HC#)sG$$CIIuxx~~)N)Ncii  -r6   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                  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 sepcified. Are you sure you are calling this with the right arguments? )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)rh   	flat_argsr-  r,  s       r7   _adapt_flat_args"UnflattenedModule._adapt_flat_args  s    **1-77	'''!!)O 
 ..44%--"$	 5 I 9~!2!2!=!==  #I/ 0((1(9(9(D(D'EG 
 r6   c                    U R                   S   R                  n[        X#R                  5      n[        R
                  " X45      u  pVU Vs/ s H  owS   PM	     nn[        5       (       a  U$ XcR                  :w  aQ  U R                  (       d(  [        SU S3SUR                   35        [        S5        U R                  X5      nSU l        U R                  (       aY  SSKJn	  U R                  SL a%  U V
s/ s H  n
[        SS	9[        S
S94U
4PM     nn
OUnU	" U R                  XR                   5        U$ s  snf s  s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_graph)idxz<unknown location>)r   )r   r,  r   r-  r   tree_flatten_with_pathr   r   r(  r1  r  torch._export.utilsr4  r   r   r  r   )rh   r   r|   r,  reordered_kwargsflat_args_with_pathr-  xr0  r4  r   new_flat_args_with_paths               r7   process_forward_inputs(UnflattenedModule.process_forward_inputs"  sT   **1-77	)&2C2CD'-'D'D$(
$ $77#6aqT#6	7??'''<<''.ir301B1B0CD
 MN--iAIDL'' O||t#  )+( "a(*:N*OPRUV( ( +'
 +>'.'')@BXBX I 82+s   D97D>c                    [         R                  R                  U R                  5      " 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       (       a>  U R                   (       d-  [         R                  R#                  X R                  5      " U6 nO7[         R                  R                  X R                  S9R                  " USS06n[$        R&                  " XdR(                  5      $ )Nr   r~   r   Fr   )rP   _dynamodisabler<  r   r,  r   r   r   rp   r   rV   r   r   r   r   rv   r   r   tree_unflattenout_spec)rh   r   r|   r0  r,  
return_valtree_outs          r7   r   UnflattenedModule.forwardP  s!   MM))$*E*EFWPVW	**1-77	??--d**-EII16J *e,,ZA1E!!}$>>--//8R8Rxx++D**=yIHxx++D

+CGG16H $$X/A/ABBr6   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 Hv  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   eU" UUR2                  5        M+     Mx     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   0rB   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   rB   )r   r   r>   rL   )r\   rp   r   r   pathcalled_moduless        r7   elide_call_indices?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  sW    77m+++++C03C06fXQse,CD~-&) $r6   Fr   rp   r   N)r   r   r   r   r,  r   getrL   r   intrM   rsplitr   rU   sortedr   range
_call_namer   rT   popset_submoduler"   r  rN   rp   r   )rh   r"  r  r  r   r   baser5  	attrs_mapr>   orig_fqnr   indexed_call_modulesr  r   r#  r\   rK  mod_rJ  s                      @r7   r  #UnflattenedModule._dispatch_modulese  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11&sDJJ7 .	 C- Ls   *I'c                 "    [        U SUUUU5      $ )Nr#   r   r   s        r7   r    UnflattenedModule.print_readable  r   r6   )rv   r   r  r   r   rp   r   r  r   r   r   r   rD   r   )r-   r.   r/   r0   r   r   r    rt   r)  r1  r<  r   r  r   r5   r   r   s   @r7   r#   r#     sb     8<d&d $O4d dL!4,\C*88x 
 
r6   r#   r+   r   re   c                 .    [        U 5      n [        X5      $ )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
hierachy 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.
)r   r#   )r+   r   s     r7   r$   r$     s    , #6*FV77r6   rp   r   c                   ^ [        [        [        U R                  5      5      5      nUR                  S:X  a  [        UR                  5      S:X  d   eUR                  S   nUR                  nUS[        U5       nUR                  R                  5        VVs0 s H  u  pgXv_M	     nnnU R                   V	s0 s H!  oR                  S:X  d  M  U	R                  U	_M#     n
n	U H  nXKR                     nX   nX   nU R                  U5         U R                  S[        R                  R                  R                   X45      mUR"                  R                  5        H  u  pgUTR"                  U'   M     SSS5        UR%                  TU4S j5        M     ['        U[        U5      S 5      nU4Ul        gs  snnf s  sn	f ! , (       d  f       NS= f)a  Transform buffer mutations from their functionalized form into a copy_
node in the graph.

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

Will become 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,)
outputr   r   Nr   call_functionc                    > U TL$ rD   r,   )r:  new_nodes    r7   <lambda>+_inplace_buffer_mutations.<locals>.<lambda>  s
    1H;Lr6   )nextiterreversedr   r   r   r   buffers_to_mutateinputs_to_buffersrU   r   inserting_aftercreate_noderP   opsatencopy_r   replace_all_uses_withr   )rp   r   output_nodereturn_argsmutation_node_to_buffer	mutationsrG   vbuffers_to_inputsr   input_name_to_nodemutationbuffer_name
input_name
input_nodeuser_outputsrb  s                   @r7   r   r     s   2 tHU[[123K>>X%#k.>.>*?1*DDD""1%K-??:c"9:;I*9*K*K*Q*Q*ST*S$!*ST$)KK$/D77m3K		4K   -mm<&3
'3
""8,((!5!5
7MH !++-#$a  .	 - 	&&x1LM " C/023L &(K3 U -,s   F:9G G A)G
G	c                 `    [        U 5      [        U5      :  =(       a    US[        U 5       U :H  $ )z2Check whether `candidate` is a prefix of `target`.N)r   )	candidater>   s     r7   
_is_prefixr~    s+    y>CK'QF3CS^,D	,QQr6   
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   rB   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.)rL   r   RuntimeErrorr   )r  r  parent_splitchild_splits       r7   _compute_accessorr  
  s    R##C(L//#&K &S&'<7YK'OPZ| \P P
 	

 88KL 1 3455r6   r:  yc                 j   S[         R                  R                  S[        4S jn[	        U R
                  [         R                  R                  5      (       d   e[	        UR
                  [         R                  R                  5      (       d   eU" U R
                  5      U" UR
                  5      :H  $ )Nrp   re   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 )Nre   c                    > [        U [        R                  R                  5      (       a  S[	        T[        U 5         5      -   $ [	        U 5      $ )N%)rV   rP   r   Noderk   r   )r   	nodes_idxs    r7   arg_dump>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump!  s:    #uxx}}--S2c7!3444s8Or6   =)r`  get_attrr   z: [z](z, )r   )rk   r  r   r   tree_mapr   r|   rU   r   r>   r   r   r   )rp   retr  r#  r   r   	args_dumpkeyvaluer>   r  s             @r7   
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.
)rP   r   r   rk   rV   rp   )r:  r  r  s      r7   _check_graph_equivalencer    sz    %((.. S ( agguxx~~....agguxx~~....agg*QWW"555r6   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   )rN   rO   )r  specr#  r   s       r7   	_add_specr  6  sR    	A
"qcl
#
#	Q "qcl
#
#A3<DBdKr6   c                     U R                   R                  [        R                  U45      nU R                   R                  [        R
                  US45      nU$ r   )rp   r`  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r7   _generate_flattenr  ?  sF    hh$$V%8%84'BG&&x'7'7'1FIr6   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rD   )r  rp   r  r`  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r7   _generate_flatten_specr  E  sB     RD!!$'I88!!)"="=?PQQr6   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rD   )r  rp   r  r`  r   rA  )r  r   r  r   r  s        r7   _generate_unflattenr  M  sB     RD!!$'I88!!&"7"7%9KLLr6   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      $ )NrB   )rL   r   rV   rP   rQ   rR   )r   r>   r\   r]   rI   submods         r7   _get_submoduler  U  s\    \\#&NVD)>&%((//22  3t$$r6   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 )NrB   r   F)
rL   r  r   r   rP   rQ   rR   rO   rV   
add_module)	r   r>   r  r  r\   r]   r#  rI   r  s	            r7   _add_submoduler  f  s     \\#&NVV$D)>(&sxxwQ'@A*Cv&&%((//22 % NN5(r6   rU  r!  c                 &    US:X  a  U $ U  SUS-
   3$ )Nr   r   r,   )rU  r!  s     r7   rR  rR    s$     640$qQ00r6   	call_namec                 b    [         R                  " [         R                  " U5      S-   U 5      S L$ )Nz(@\d+)?$)rematchescape)r  rU  s     r7   rE   rE     s%    88BIIdOk19=TIIr6   c                      \ rS rSr SS\R
                  R                  S\\R
                  R                  S4   S\	\\
\\
   \4      S\\
\4   S\\\R
                  R                   \4      4
S	 jjrS
 rS rS rS rS rS rS rS rSrg)_ModuleFramei  N
flat_graphr   .module_stackr   r+   c                 n  ^ ^^! 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b  T R                  Gb  UR@                  RB                  S	:X  d   eUR@                  RD                  S   nUR@                  RD                  S   nURF                  b   eURF                  c   eT R,                  RI                  S 5         [K        URB                  5       Vs/ s H!  nT R,                  RM                  S
U 35      PM#     nn0 nURF                   H!  nT R,                  RM                  U5      UU'   M#     [O        T R                  [Q        U5      U4UR@                  5      n[S        URT                  5       H  u  nnT R,                  RW                  S[X        RZ                  UU4[]        U[^        5      (       d  UR`                  OSU 3S9n[]        U[^        5      (       a  Mm  UR`                  T R                  ;   d  M  [b        Rb                  " T R                  UR`                     Rd                  5      Ul2        UT R0                  T R                  UR`                     '   M     S S S 5        T R                  R,                  Rg                  T R2                  5         / nURT                   H  n[]        U[^        5      (       a  UR=                  URh                  5        M5  UR`                  T R                  ;  a  UR=                  S 5        Mb  []        U[j        [l        [n        [p        45      (       d   eUR=                  T R                  Rs                  T R                  UR`                     5      5        M     [u        T R                  R                  UUR@                  5      nT R                  R,                  Rw                  [X        RZ                  US45      nT R                  R,                  Rw                  [X        RZ                  US45      n[K        URB                  5       Vs/ s H9  nT R                  R,                  Rw                  [X        RZ                  UU45      PM;     nnURF                   V s0 s H:  n U T R                  R,                  Rw                  [X        RZ                  UU 45      _M<     nn S S S 5        T R2                  c   e[Q        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       Nl= f)NFr   r   rq   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$ )NrB   r  )r   created_modulesr!   rP   r   r   )r   rI  r  parentrh   rq   s      r7   r  ,_ModuleFrame.__init__.<locals>.create_module  sj    06

&**Qse,4/////55*588>>+;C-3$$T*r6   r   )r  parent_moduleparent_call_moduler   call_idxr+      _positional_arg_r`  
_constant_)r   r>   r   r   )>r  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   rR  r  r+   rN   r   rM  r!   rP   r   r   rp   node_mapnode_to_placeholderr  r  r  r   rT   r   _SubmoduleEntryr-  num_childrenchildren_specscontextrj  rQ  r   r  r   r  inputsrk  r  r  rV   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  r`  r   r|   )"rh   r  r   r  r  r  r  r  r  r  r   r+   	num_callsr   r  base_module_framer,  	args_speckwargs_specr5  	arg_nodeskwarg_nodesr   r0  r   flat_arg_nodeinput_nodesinputinputs_node	args_nodekwargs_noder#  rG   rq   s"   `      `                         @r7   rt   _ModuleFrame.__init__  sy    %
$($.("!2"&"3"3B"7"i#DHHi!m< DK)0)A)ArDJ..22!%((.."2r:DK  DJ[[&&
 =?#% ;?(T^^DH 6==(DKKO&,ll&>&>x&HD#  0$($5$5dnn$Ea$H!'8'?'?'H'H$dnn-44#{{"&++"4"4'+'>'>&];;	 &))$..9	 T[[%<$$11Q666!))88;I#++::1=K$$,,,&&222++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 OOCHH5! !< 2B ""33D4K4KL=?&--E!%)9::#**5;;74??:#**40)! . . / 0	       $** 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	  K MV **666+0+;D##(-8D##*k &=  21DK MLsS   ^
(^C;^
A&^
"F^&-A ^-^&=A^!>^&^


^
^&&
^4c                    U R                   S:w  d   SU S35       eUR                  U R                  L d   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 module	type_expr)
r   rp   r  r  r   r   r   r  r   r  )rh   r:  placeholder_nodes      r7   add_placeholder_ModuleFrame.add_placeholder0  s    xx2~K!8?KK~ww$//)))ZZ((.#zz55aff5O / !%		!&& 1&6  # /.s   /B==
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$ rD   )r   tree_map_onlyrP   r   r  r  r   r|   rp   r`  r>   r  r   r  )rh   r:  r   r|   r   s        r7   copy_sym_call_function#_ModuleFrame.copy_sym_call_function;  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar6   c                    UR                   U R                  L d   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$                  R.                  R(                  [         R"                  R$                  R0                  R2                  [         R"                  R$                  R4                  R,                  [         R"                  R$                  R6                  R,                  4;   d5  [9        UR                  S5      (       a:  UR                  R:                  S:X  a   U R=                  U5        U R                  U   $ U R                  R                  U R                  5      b1  U R>                  RA                  U R                  U R                   U5      $ [C        SUR
                   S	U 35      e! , (       d  f       GN= f)
Nzremap_input(r  r   r   r`  r.   	_operatorz(Could not run remap_input() on op type: z
 for node )"rp   r  r  r(  r  r   r   rM  r   r  r  r  r  
insert_argr  r>   rP   rl  rm  sym_sizerN  rI   defaultunbindsumdim_IntListviewdiffrN   r.   r  r   readr  )rh   r:  s     r7   r  _ModuleFrame.remap_inputI  sT   ww$//)))==##

\!A&'(((++A..DDM!%%))$((3;   #&&2 [[&&778O8OP++66q$++:Q:QRS:TU Q++A..TT_$HH		''++		##++		%%))		""..		##++		##++ ,//AHH4G4G;4V ''*==####''1= ::??488TZZ;;:144&
1#N 1 QPs   06L33
Mc                 0  ^ ^ 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        Ml  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 R.                  R1                  5        Hb  nUR2                   HO  nUR                  T R                  ;  d  M  UR                  U5        UR                  T R.                  U   5          M`     Md     T R,                  n[5        U5      S:X  a  US   n[        U[6        [8        R:                  R<                  45      (       d   eT R>                  RA                  U5        Uc  g [        U[8        R:                  R<                  5      (       a  URB                  R	                  S5      O*U V	s/ s H  oRB                  R	                  S5      PM     sn	URB                  S'   [5        U5      S:X  a   Uc  UT R                  R.                  US   '   g [E        U5       Hw  u  pUc  M
  [8        R:                  RG                  U5      U
   RH                  nURB                  R	                  S5      URB                  S'   UT R                  R.                  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  rp   )r_  	seen_noderh   s     r7   get_actual_output_node=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node  sx    > OOFKK8	-==33$":"::33I>>&5fXYtzzlS r6   c              3   4   >#    U  H  nT" U5      v   M     g 7frD   r,   )rF   r_  r   s     r7   rJ   0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s     P<,V44<s   r   r   val)%r  rS  r   r   rM  r  r  r   rV   r   r   r   r   r   r  r   r  r  r+   r   rB  r  r  r  keysusersr   r   rP   r   r  rp   r_  r   r  Proxyr   )rh   orig_outputsr,  r_  tree_out_node
parent_outgraph_outputs	orig_node	user_nodeor#  orig_output	proxy_outr   s   `            @r7   finalize_outputs_ModuleFrame.finalize_outputsu  s     40**..t~~>	 T[[%<#++#^_FVW  {{doo5$++DOOFKK,HI$++D1&A&J  , 0P<PP""M
 3I""D$;$;Y=O=O3J HUMM!]]//1	!*I ~~T__<$++I6%,,T]]9-EF "1 2 00J=!Q& -a 0-$)>????

-(  -77 ""5)-:;]&&**U#]; 	 |!i&74>DKK  a1"+L"9&!HHNN:6q9>>	(3(8(8(<(<U(C	u%4=$$[1 #: <s   )$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_noderp   	node_copyr  r  r  r   r   s     r7   	copy_node_ModuleFrame.copy_node  sL    

9d..01"jj2249I9IJd%)		"r6   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 )Nnn_module_stackr   r   r   r_  )
r  r  r   r(  r   rM  r  r   r  run_from)rh   r#  r   node_idxs       r7   	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$ *r6   c                 >    U R                   (       a  [        U0 UD6  g g rD   )r  r(  )rh   r   r|   s      r7   r(  _ModuleFrame.print  s    <<4"6" r6   c                    SnU[        U R                  5      :  GaO  U R                  U   nUR                  S:w  d   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   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 R.                  U R0                  U R2                  U U R
                  U/-   UR                  S	5      S   U R4                  5
      R7                  U5      nUS-  nGM  XpR
                  :X  d   eUR                  S:X  a2  U R0                  U R8                     R;                  UR<                  5        U R?                  U5        US-  nU[        U R                  5      :  a  GMN  g g s  sn
n	nf )Nr   r   STEPr_  r   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYr   r  	outliningzCreating new stack frame forr  ) r   r   r   r(  r  r  r  r   rM  r  (torch._export.passes._node_metadata_hookr#  rU   rN  rL   r   rp   r~  r   r  r  r  r  r  r  r  r   r  r  r   r>   r  )rh   r  
module_idxr   depthr  r#  node_module_stackrG   rI  rq   next_modulenext_module_keys                r7   r  _ModuleFrame.run_from  s   
TZZ(::h'D77m+++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::&$000$++->?? 06

9;G #'tyy1B'C'H'H'J"KE"R'OOJJOO%%OO((%%5#))#.q1** (8$  a
 %(9(9999ww*$/33DKK@NN4 MHq TZZ((D%s   A M-)r  r  r  r   rp   r   r+   r   r  r  r  r  r   r  r  r  r  r  r  rD   )r-   r.   r/   r0   rP   r   r   r   r  r   rk   r   rN  r   r   r   r   r#   rt   r  r  r  r  r  r  r(  r  r5   r,   r6   r7   r  r    s     LPb9HHNNb9 UXX]]C'(b9 5hsmS!89:b9  %8 89b9 uxx335FFGHb9H	7*XR>h*
%&#[r6   r  c                       \ rS rSr% \\S'   \R                  R                  \S'   \R                  R                  \S'   \\S'   \\S'   \R                  R                  \S'   Srg	)
r  iC  r  r  r  r   r  r+   r,   N)r-   r.   r/   r0   rk   rl   rP   rQ   rR   r   r  rN  r5   r,   r6   r7   r  r  C  s;    O88??"%	HMHHOOr6   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   rM   r  r   r   r   r,  r   r  )r-  r.  r  r  r  r  r  s          r7   r   r   M  s    +-J5@5FL&1#&6J24Oj	
 %66	
6 'EIIu&6	

   ik##	
s   B,Br  r$  r\   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   rB   )r\   )r  )r   r  r  r  rP   rQ   rR   rT   rU   r  rL   delattrr   sortr  
itemgetterregister_module)r  r$  r\   r   childrenr   childr  s           r7   r  r  f  s     |	()!"-Cf*2vEHHOO,=> . HFOO1134=mESYYs^A5F5LM56 5 MMh))!,M-"t+ #r6   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 buffer mutations in a graph,
along with the module call fqns that create and use them. Later,
in each fqn associated with an intermediate value we will install
a corresponding attribute, so that it can be updated and read.

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.

Thus our analysis will produce {ival1: {n0, n1}, ival2: {n1, n2}}.
c                 :    [        [        5      U l        0 U l        g rD   )r   rM   fqnsstoragerg   s    r7   rt   _IVals.__init__  s    $	r6   c                    UR                  S5         UR                  SUR                  -   UR                  S9n[        R                  " UR
                  5      Ul        SSS5        UR                  U R                  ;  aO  UR
                  S   n[        R                  " UR                  UR                  S9U R                  UR                  '   U R                  UR                     R                  U5        W$ ! , (       d  f       N= f)z=
Read attribute corresponding to a given intermediate value.
N__ival__r  r  )dtype)r  r  r   r   r  r   r:  rP   emptyshaper>  r9  r   )rh   r   rp   r   	ival_nodefakes         r7   r  _IVals.read  s    
 ##D)zDII'=SI!YYtyy1IN * 99DLL( 99U#D&+kk$**DJJ&ODLL#		$))  % *)s   AC::
Dc                 N   U R                   UR                     R                  U5        UR                  U5         UR	                  SUR                  -   UR
                  S9n[        R                  " UR                  5      Ul        SSS5        UR                  W5         UR                  S[        R                  R                  R                  XC45      n[        R                  " UR                  5      Ul        SSS5        g! , (       d  f       N= f! , (       d  f       g= f)z?
Update attribute corresponding to a given intermediate value.
r=  r  Nr`  )r9  r   r   rj  r  r   r  r   rk  rP   rl  rm  rn  )rh   r   rp   r   rA  new_ival_nodes         r7   rS   _IVals.update  s     			$))  % ""4(zDII'=SI!YYtyy1IN ) ""9-!--!5!5	7HM "&499!5M	 .- )( .-s   AD AD
D
D$c                 F   SU4/nU H  nU H  nUR                  UR                  UR                  45        UR                  R                  nUR                   H;  nUR
                  U R                  ;   d  M  U R                  UR                  Xg5        M=     M     M     [        [        5      nU R                  R                  5        H!  u  pU
 H  nX   R                  U	5        M     M#     U H.  u  pX    H!  n	SU	 3n[        XU R                  U	   5        M#     M0     g)z
Update attributes corresponding to intermediate values that were read.
Finally, initialize attributes in all modules that read or update
corresponding intermediate values.
r   r=  N)r   r   r+   rp   r   r   r:  rS   r   r   r9  rU   rO   )rh   
partitionsr.  entriesshared_submodulesr  rp   r   r   r   r9  r   r   	ival_names                 r7   r   _IVals.create  s     $%!+*		5<<89**!KKDyyDLL0EIIu; ( + ", D!))//+JD
!!$'  ,  HC
&tf-	 T(:;	 #  r6   )r9  r:  N)
r-   r.   r/   r0   r   rt   r  rS   r   r5   r,   r6   r7   r   r   |  s    2$6 <r6   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 rD   )rU   r   r   rO   )r  r.  r  r  namesr+   r   r  s           r7   r   r     sF    
 ',,.	6?;2[D"#CF#&  /r6   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$ )NFrB   T)r  rR  r   r  r  r  r  r+   r  rL   r   rT   rS  r  r>   rT  )rH  r"  rJ  r#  r  r  r>   deduplicatedseenr  r\   r   seen_child_fqnseen_targets                 r7   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 #"r6   r  scopemodule_id_to_inputs_removedc           	        ^^ Tc  [        [        5      m[        U 5      T;   a  [        U 5      T[        U 5         0$ U R                  R	                  5        Hf  u  nm[        [        [        R                  R                  T5      UX$/-   T5      nUR	                  5        H  u  pgTU   R                  U5        M     Mh     [        U SS5      nUb  [        UR                  5      S:X  a  T$ [        U[        R                  R                   5      (       d   e[#        [%        S UR                  5      5      n	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   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.
Nrp   r   c                      U R                   S:H  $ )Nr   r   r!  s    r7   rc  _sink_params.<locals>.<lambda>B  s    144=#8r6   r  c                      U R                   S:H  $ )Nr   rX  rY  s    r7   rc  rZ  F  s    )>r6   c                 :   > U R                   T[        T5         ;  $ rD   )r   r   )r!  rU  	submodules    r7   rc  rZ  N  s    aff,G9,VVr6   rB   r   r  T)propagate_meta)%r   rM   r   rT   rU   r   r
   rP   rQ   rR   rS   r   r   r   rV   r   r   r   filterr   r>   r   r   r   rL   r  r   rY   r[   rj  rk  r   ro  
erase_noder   r!   r   )r+   r  rT  rU  r   submod_id_to_inputs_removedrG   rt  rp   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitr:  inputs_removed	attr_path
state_attrrb  r]  s      `                  @r7   r   r     s
   , #*&1#&6#	&z006
76
CDD "??002i&2),FN'	'
# 0557DA'*11!4 8 3 FGT*E}EKK(A-**eUXX^^,,,,&8%++FGFBZN >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MM &&~6 ,,Z)9LM 7 &&x&E499% = &+,,vJ''O *J8 76s   >M'
"M,,
M:	)TrD   )r   )dr   r  loggingr  r  collectionsr   
contextlibr   r   dataclassesr   enumr   typingr   r	   r
   r   r   rP   torch.fx._pytreer   _pytreer  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   r   r   r   torch.fx._symbolic_tracer   torch.fx.graph_moduler   r   r   r   r   _remove_effect_tokens_passr   	getLoggerr-   log__all__r&   r9   r;   rY   r[   rQ   rR   rk   boolr`   rb   r!   r"   ABCr    r#   r$   r   r   r~  r  r  r  r   r  r  r  r  r  r  rN  rR  rE   r  r  r   r   r  r   rM   r   r  r   r   r,   r6   r7   <module>r     sn   
    	 # %  !  7 7  $ $ $ $ ? 3
 
 
 3 U U 7 = !    ( (" 00ELL%"4"4ehhooEF00xx00 00 	00
 00f U
 U
p-".%((// -"`cgg "R
 R
l MQ88080I8848)88>>8))8) 
8)vR
6# 6# 6# 6$6 6EHHOO 64%((// C %((..  Rehh""$57HHIR
XX]]RMehh""$57HHIM
XX]]M% % %* AE	)	)) 88??) HcUEHHOO%;<=	)41S 1S 1S 1JS J J J
u up   $EHHNN $AR $4 GI,HHOO,(,S#X,@C,,]< ]<@	'	'"	' S#c(]#	'(#^ BF	p(HHOOp(#tCy.)p( 9p( "*$sCH}*=!>	p(r6   