
    wiJ                       d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZmZmZmZmZ d dlZd dlmc mZ d dlmc mZ d dl m!Z! d d	l"m#Z# d d
l$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3 d dlm4Z4m5Z5 ddl6m7Z7  ej8        e9          Z:g dZ; G d de          Z<da=ed             Z>	 dUdeej?        ej@        ejA        jB        f         dejA        jB        deCde<deDf
dZE G d dejA        jB                  ZF G d dejA        jB                  ZG G d d e jH                  ZI G d! d"ejA        jB                  ZJ	 dVd#e&d$eeI         d%eJfd&ZKd'ej        jL        d(e'd%dfd)ZMd* ZNd+eCd,eCd%eCfd-ZOd.ejA        jB        d/ejA        jB        fd0ZPd1ejA        jB        d%eCfd2ZQd1ej        jR        d%ej        jS        fd3ZTd1eej        jR        eFeJf         d%ej        jS        fd4ZUd1eej        jR        eFeJf         d%ej        jS        fd5ZVd6ejA        jB        deCfd7ZW	 dVd6ejA        jB        deCd8ejA        jB        d9eeeCgejA        jB        f                  fd:ZXd;eCd<eYd%eCfd=ZZd>eCd;eCd%eDfd?Z[ G d@ dA          Z\e G dB dC                      Z]dDej        jL        dEeJfdFZ^	 dWdHejA        jB        dIeeCeYf         dJeCfdKZ_ G dL dM          Z`d1ej        jR        dEeJdNeeCeeC         f         fdOZadP Zb	 dVd#ejA        jB        dQeeCeeC         f         dReeC         dSeeeYeeC         f                  fdTZcdS )X    N)defaultdict)contextmanager)deepcopy)	dataclass)Enum)	AnyCallablecastDictListOptionalSetTuple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                       e Zd ZdZdZdZdZdS )	_AttrKind	parameterbufferconstantmoduleN)__name__
__module____qualname__	PARAMETERBUFFERCONSTANTMODULE     f/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/torch/export/unflatten.pyr*   r*   1   s"        IFHFFFr7   r*   Tc               #   8   K   t           } da 	 d V  | a d S # | a w xY w)NF)RUN_WITH_INTERPRETER)old_flags    r8   _disable_interpreterr<   ;   s?       $H ('x''''s    from_obj	to_moduletarget	attr_kind
persistentc                   	 |                     d          ^ }}|h}|D ]	t                      }|D ]w}t          |	          s-t          |	t          j                                                   |                    	fd|j        	                                D                        x|}|D ];}|t          j        k    r8t          | t          j        j                  sJ |                    ||            K|t          j        k    r5t          | t          j                  sJ |                    || |           |t          j        k    rYt          | t&                    r
J d            t          | t          j        t          j        f          sJ t          |||            |t          j        k    r2t          | t          j        j                  sJ t          |||            =d S )N.c              3   B   K   | ]\  }}t          |          |V  d S N)_is_call_name).0kt_callitems      r8   	<genexpr>z_assign_attr.<locals>.<genexpr>\   sK        Av D))     r7   )rA   z2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrtorchnnModuleupdate_modulesitemsr*   r2   
isinstance	Parameterregister_parameterr3   Tensorregister_bufferr4   r   ScriptObjectr5   )
r=   r>   r?   r@   rA   prefixfield
to_modulestsrJ   s
            @r8   _assign_attrr`   H   s6    \\#&&NVU J 
 
#&55# 	 	I9d++ <	4):):;;;II    !*!3!9!9!;!;      
 

 0 0		+++h(:;;;;;((9999)***h55555%%eX*%MMMM),,,!*  D DCD D  L&     Iuh////)***h88888Iuh///+0 0r7   c                        e Zd ZU dZeej        j                 ed<   dej        j	        f fdZ
d Zd Z	 	 	 	 d
d	Z xZS )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graphc                     t                                                       || _        | | j        _        t          | _        d S rE   )super__init__rc   owning_moduler:   _run_with_interpreter)selfrc   	__class__s     r8   rf   zInterpreterModule.__init__   s:     	
#'
 %9"""r7   c                 ~   | j         
J d            t                      sHt          j                                        s| j        s# t          | j                   j        | g|R i S rt          |          }| j	        t          |          d          }|                    fd|D                        t          |          t                    k    sJ t          |          t          | j	                  k    sJ t          |          }t          j                            | | j                  j        |ddiS )Nz&Didn't finalize this InterpreterModulec              3   0   K   | ]}|v |         V  d S rE   r6   )rG   
kwarg_namekwargss     r8   rK   z,InterpreterModule.forward.<locals>.<genexpr>   s?          "!V++ :&++++   r7   rc   enable_io_processingF)rb   r   rP   compileris_dynamo_compilingrh   typeforwardlist	arg_nameslenextendtuplefxInterpreterrc   run)ri   argsrn   arg_listkwarg_namess     `  r8   rt   zInterpreterModule.forward   sc    ,,.V,,, 	N..00	8<8R	 34)**24I$III&III '
  ::"nS]]__=        &1         ;''3v;;66668}}DN(;(;;;;;X8''DJ'??C,1  r7   c                    t           j                            | | j                  | j        d<   | j                                         g | _        | j        j        D ],}|j        dk    r| j        	                    |j
                   -d S )Nrb   placeholder)rP   rz   GraphModulerc   __dict__lintrv   nodesopappendr?   ri   nodes     r8   finalizezInterpreterModule.finalize   s     ).(<(<T4:(N(Nn%
 J$ 	3 	3Dw-''%%dk222	3 	3r7   TFc                 *    t          | d||||          S )Nr%   r   ri   print_outputinclude_strideinclude_devicecoloreds        r8   print_readablez InterpreterModule.print_readable   (     
 
 	
r7   TFFF)r/   r0   r1   __doc__r   rP   rz   r   __annotations__Graphrf   rt   r   r   __classcell__rj   s   @r8   r%   r%   {   s          
 58/0000:x~: : : : : :! ! !F3 3 3& 
 
 
 
 
 
 
 
r7   r%   c                   `     e Zd ZdZdee         dee         f fdZd Z	d Z
	 	 	 	 d
d	Z xZS )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                                                       |sJ |d         j        | _        |D ]'}t          | |t	          |d         |                     (|| _        d| _        d S Nr   )re   rf   rT   rO   getattr_call_modules
_num_calls)ri   r   r   accessorrj   s       r8   rf   z$InterpreterModuleDispatcher.__init__   sy    |$Q0 	H 	HHD(GLOX$F$FGGGG)r7   c                     | j         | j                 }| j        dz   t          | j                   z  | _        	  ||i |S # t          $ r	 d| _         w xY w)Nr"   r   )r   r   rw   	Exception)ri   r}   rn   call_modules       r8   rt   z#InterpreterModuleDispatcher.forward   sn    (9?Q.#d6H2I2II	;//// 	 	 	DO	s   A   Ac                     | j         S rE   )r   ri   s    r8   r   z(InterpreterModuleDispatcher.call_modules   s    !!r7   TFc                 `    fd| j         D             }d                    |          S )Nc                 @    g | ]}|                               S r6   )r   )rG   modr   r   r   r   s     r8   
<listcomp>z>InterpreterModuleDispatcher.print_readable.<locals>.<listcomp>   sG     
 
 
  	 
 
 
r7   
)r   join)ri   r   r   r   r   outputss    ```` r8   r   z*InterpreterModuleDispatcher.print_readable   sV    
 
 
 
 
 
 
 )
 
 
 yy!!!r7   r   )r/   r0   r1   r   r   strr   r%   rf   rt   r   r   r   r   s   @r8   r&   r&      s         c#h d;L6M        " " "
 " " " " " " " "r7   r&   c            
       p    e Zd ZdZej        dej        dej        dee	         dee	         fd            Z
dS )r$   zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    target_spec
input_spec
input_argsreturnc                     dS )z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr6   )ri   r   r   r   s       r8   adaptzFlatArgsAdapter.adapt  s	     	r7   N)r/   r0   r1   r   abcabstractmethodpytreeTreeSpecr   r   r   r6   r7   r8   r$   r$      st          	_ O I	
 
c     r7   r$   c                   f     e Zd Z	 ddedee         f fdZd Zd Zd Z	d Z
d	 Z	 	 	 	 ddZ xZS )r'   Nexport_moduleflat_args_adapterc                   $% t                                                       |j        j        t	          d          d |j        D             }|d         dk    sJ t          |j                  }t          |j                  | _        t          j	        
                                | _        | | j        _        t          |j                  | _        || _        d| _        t          | _        t!          || j                   t#                      | _        t'          ||           \  }}| j                            |                                |            t-          |j        | |           |j        | _        g | _        |j        }t7                      }i }	| j        j        D ]}
||
         }t;          |          |	vrHt          j                            |                                 |j!                  |	t;          |          <   tE          |	t;          |                   | |
tF          j$                   |%                    |
           t7          | j        j&                  }t7                      }i }| j        j'        D ]}
|
|v rd}|j(        |
         }n
d}||
         }t;          |          |vr&|                                 |f|t;          |          <   tE          |t;          |                   d         | |
tF          j)        |	           |%                    |
           |*                                D ]\  }
}|
|v s|
|v rd}t;          |          |v stW          |t          j        j                  sd}|r]t;          |          |vr|df|t;          |          <   tE          |t;          |                   d         | |
tF          j)        d	           t;          |          |	vr||	t;          |          <   tE          |	t;          |                   | |
tF          j$                   i }|j(        *                                D ]\  }}t;          |          |vr@tW          |t          j,                  r|                                 }||t;          |          <   |t;          |                   }tE          || |tF          j-                   t]          t^                    $t7                      }$fd
}t7                      }| j        j0        D ]}|j1        td          j$        k    s|j1        td          j)        k    r|j3        rti          |j5        d          sJ tW          |j6        tn                    sJ  |t;          |j        |j6                           |j5        j8        |j6                   |%                    |j6                   |%                    |j6                   |j1        td          j)        k    r|j3        r*|j1        td          j9        k    s|j1        td          j:        k    rti          |j5        d          sJ tW          |j6        tn                    sJ  |t;          |j(        |j6                           |j5        j8        |j6                   |%                    |j6                   |j(        *                                D ]v\  }}||vrmt;          |          $v s
J d            $t;          |                   d         \  }} |t;          |          ||           |%                    |j6                   w|j        *                                D ]S\  }}||vrJt;          |          $vr$t;          |                   d         \  }} |t;          |          ||           Ti }$                                D ]} d | D             }!| D ]
\  }"}|!||"<   tw          | |g            ty          |                                          %%fd|D             }| =                    %|           d |D             }d | j        j>        D             | _?        d| _@        d t          |          D             }#| B                    d          D ]\  }
}|
|#vrt          |#          |#|
<   t          | |#           | j        E                                 d S )Nz%Unflattening on JointExportModule NYIc                     g | ]	}|j         
S r6   )fqnrG   entrys     r8   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    KKK%EIKKKr7   r    F)requires_grad)r@   T)r@   rA   c                 F    |          }|                     ||f           d S rE   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r8   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_map  s,    "6*Ii566666r7   namez?Constants should be either aliased or appear in graph signaturec                     g | ]
}|d          S r"   r6   )rG   ts     r8   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    111qt111r7   c                     g | ]}|v|	S r6   r6   )rG   r   redirected_call_indicess     r8   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s$    RRRCs:Q/Q/QC/Q/Q/Qr7   c                     g | ]}d |v|	S )@r6   )rG   r   s     r8   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    >>>Cs#~~C~~~r7   c                 (    g | ]}|j         d k    |S r   r   rG   r   s     r8   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s+     #
 #
 #
M1I1ID1I1I1Ir7   c                     i | ]\  }}||	S r6   r6   )rG   ir   s      r8   
<dictcomp>z.UnflattenedModule.__init__.<locals>.<dictcomp>  s    >>>3S!>>>r7   remove_duplicate)Fre   rf   graph_signaturebackward_signature
ValueErrormodule_call_graphr   rc   rP   rz   r   rg   r   adaptedr:   rh   _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*   r2   addnon_persistent_buffersbuffers	constantsr3   rU   rV   rY   r4   r   ru   input_specskindr   rA   rN   argr?   r   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   input_placeholderscheck_input_constraints	enumeratenamed_modulesrw   _reorder_submodulesr   )'ri   r   r   fqn_listexport_graphseen_modules
seen_attrsr   assigned_paramsid_to_paramr   paramr   assigned_buffersid_to_bufferrA   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targetsn	fqn_orderr   r   rj   s'                                       @@r8   rf   zUnflattenedModule.__init__  sA	   
 	(;GDEEEKK=+JKKK{b     344'(EFFX^^%%
#'
 !)-*I!J!J!2%9"!,0DEEEXX
 $7|T#J#J j 	
,--//666 	-5tZHHH!.!@*,! #-
$'EE57(3 	& 	&Dt$E%yy++).););KKMM1D *< * *BuII& BuII&#-	    %%%%!$T%9%P!Q!Q%(UUCE(0 	' 	'D---"
&06!
#D)&zz--,2LLNNJ+GRZZ(RZZ(+#*%      &&&& ',,.. !	 !	LD&&&$2B*B*BI&zz\))*2 2) !	 vJJl22 0LF,  F,Q/'.#     f::[00.4K6

+6

+'1	     NP*4::<< 	 	MC(||;..h55 0'~~//H,4BxLL)#BxLL1I#,	     8C47H7H
#&55	7 	7 	7 	7 	7 *-%1 	- 	-Av,,,)***q|*quf-----!!(C00000!!}/9::AEJ   ""18,,,$((22229+++AL+6Y6666Y111quf-----!!(C00000!!}.qx89915:qx   ""18,,, "/!8!>!>!@!@ 	3 	3J//uII+++T ,++'5		215
!!"U))WjAAA$((222 )399;; 	< 	<KC...f::Z// '6

3A6
!!"V**gs;;; 13%,,.. 	- 	-K11[111G# - -1%,""- 	T?B///"6|7J7J7L7L"M"MRRRR8RRR6GGG>>8>>>
#
 #
!Z-#
 #
 #
 (,$>>)H*=*=>>>	))5)AA 	1 	1GD!9$$"%i..	$D),,,
r7   c                     |                                  D ]_\  }}t          |dz              t          |d          r8t          |j        t
          j        j                  rt          |j                   `d S )N:rc   )r   printrN   rV   rc   rP   rz   r   )ri   r   r   s      r8   _print_graphzUnflattenedModule._print_graph  sw    **,, 	! 	!HC#)sG$$ !CIux~)N)N !ci   	! 	!r7   c                 F   | j         d         j        }||j        k    r|S | j        t	          d          | j                            |j        ||          }t          |          |j        j        k    r,t	          dt          |           d|j        j                   |S )Nr   zeThere is no flat args adapter sepcified. Are you sure you are calling this with the right arguments? )r   r   r   z<Flat args adaption failed, number of args mismatch Adatped: z 
Exported module: )r   	signaturein_specr   	TypeErrorr   rw   
num_leaves)ri   	flat_argsr   r  s       r8   _adapt_flat_argsz"UnflattenedModule._adapt_flat_args  s    *1-7	i'''!)O  
 .44%-"$ 5  I 9~~!2!===G #IG G(1(9(DG G  
 r7   c                    | j         d         j        }t          ||j                  }t	          j        ||f          \  }}d |D             }t                      r|S ||j        k    rO| j        s+t          d| dd|j                    t          d           | 	                    ||          }d| _        | j
        r5ddlm} | j        du rd	 |D             }	n|}	 || j        |	| j                   |S )
Nr   c                     g | ]
}|d          S r   r6   rG   xs     r8   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>  s    777aQqT777r7   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_graphc                 R    g | ]$}t          d           t          d          f|f%S )r   )idxz<unknown location>)r   )r!   r    rG   r   s     r8   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>3  sJ     + + + "a(((*:N*O*O*OPRUV+ + +r7   )r   r  r   r   r   tree_flatten_with_pathr   r   r  r$  r   torch._export.utilsr)  r   r   )
ri   r}   rn   r  reordered_kwargsflat_args_with_pathr   r#  r)  new_flat_args_with_paths
             r8   process_forward_inputsz(UnflattenedModule.process_forward_inputs  sm   *1-7	)&)2CDD'-'D#$(
 (
$W 87#6777	?? 	i'''< O3'.3 3 3D1BDD  
 MNNN--iAAIDL' 	 ONNNNN|t##+ +(+ + +''
 +>'..')@$BX   r7   c                    t          j                            | j                  |i |}| j        d         j        }t                      rbt          j                            | | j	                  j
        |ddi}t          |t                    rt          |          dk    r|d         S |S t           j                                        r/| j        s(t          j                            | | j	                  | }n0t          j                            | | j	                  j
        |ddi}t%          j        ||j                  S )Nr   ro   rp   Fr"   )rP   _dynamodisabler2  r   r  r   rz   r{   rc   r|   rV   ry   rw   rq   rr   rh   r   r   tree_unflattenout_spec)ri   r}   rn   r#  r  
return_valtree_outs          r8   rt   zUnflattenedModule.forward@  s8   M))$*EFFWPVWW	*1-7	?? 	--d$*-EEI16 J *e,, %ZA1E1E!!}$>--// 	8R 	x++D$*==yIHHx++D
+CCG16 H $Xy/ABBBr7   c                    t          t                    | j        D ]}|j        r||j        ru|j        }t          | |                    ||                    }d|v r|                    d          n|dg\  }}|                             t          |          |f           t          t                    }|D ]V}	d|	v r5|	                    dd          \  }
}||
                             |           ;|d                             |	           W                                D ]\  }
}d t          |          D             }t          |          dk    rt!          |          D ]\\  }}t#          |
|dz             }||vr@|                    d          ^ }}t%          | |          j                            |           ]|                     |
t-          ||
         |                     Ȉfd}|                     d	          D ]d\  }}t1          |d
          r |||j                   't1          |d          r-|j        D ]%}t1          |d
          sJ  |||j                   &edS )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   0rC   r"   r   c                     g | ]\  }}|S r6   r6   )rG   r  r   s      r8   r   z7UnflattenedModule._dispatch_modules.<locals>.<listcomp>q  s    KKKFAsCKKKr7   c                     |j         D ]C}|j        dk    r6|j                            d          d         }| r|  d| n|}|v r||_        Dd S )Nr   r   r   rC   )r   r   r?   rL   )r\   rc   r   r   pathcalled_moduless        r8   elide_call_indicesz?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  su     * *7m+++++C003C06?f,,s,,,CD~--&)* *r7   Fr   rc   r   N)r   ru   r   r   r  r   getrL   r   intrM   rsplitr   rU   sortedrw   r   
_call_namer   rT   popset_submoduler&   r   rN   rc   r   )ri   r   r  r   r   r   baser+  	attrs_mapr?   orig_fqnr   indexed_call_modulesr   r   r   r\   r@  mod_r?  s                      @r8   r   z#UnflattenedModule._dispatch_modulesU  s    %T**+ 	= 	=Ey =U_ = i&=&A&A#s&K&KLL.1SjjCIIcNNNsCj	ct$++SXXsO<<<$$	$ 	* 	*Ff}}!'sA!6!6$(#''----"!!&)))) /=.B.B.D.D 	 	*H*KKf5I.J.JKKKL<  1$$&/&=&= Q QNA{$Xq1u55C"999(+		#/f==FJJ4PPP""/	(0C\RR  	* 	* 	* 	* 	* **E*BB 	8 	8HCsG$$ 8""3	2222o.. 8- 8 8D"411111&&sDJ7777	8 	8r7   TFc                 *    t          | d||||          S )Nr'   r   r   s        r8   r   z UnflattenedModule.print_readable  r   r7   rE   r   )r/   r0   r1   r   r   r$   rf   r  r$  r2  rt   r   r   r   r   s   @r8   r'   r'     s         8<` `&` $O4` ` ` ` ` `D! ! !  2, , ,\C C C*88 88 88x 
 
 
 
 
 
 
 
r7   r'   r.   r   r   c                 @    t          |           } t          | |          S )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     r8   r(   r(     s"    , #6**FV%6777r7   rc   r   c                 B   t          t          t          | j                                      }|j        dk    rt          |j                  dk    sJ |j        d         }|j        }|dt          |                   }d |j        	                                D             }d | j        D             }|D ]}||j
                 }	||	         }
||
         }|                     |          5  |                     dt          j        j        j        ||f          |j        	                                D ]\  }}|j        |<   	 ddd           n# 1 swxY w Y   |                    fd           t'          |t          |          d                   }|f|_        dS )	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   Nc                     i | ]\  }}||	S r6   r6   )rG   rH   vs      r8   r   z-_inplace_buffer_mutations.<locals>.<dictcomp>  s    TTT$!QATTTr7   c                 4    i | ]}|j         d k    |j        |S r   )r   r   r   s     r8   r   z-_inplace_buffer_mutations.<locals>.<dictcomp>  s/        47m3K3K	43K3K3Kr7   call_functionc                     | uS rE   r6   )r(  new_nodes    r8   <lambda>z+_inplace_buffer_mutations.<locals>.<lambda>  s    1H;L r7   )nextiterreversedr   r   rw   r}   buffers_to_mutateinputs_to_buffersrU   r   inserting_aftercreate_noderP   opsatencopy_metareplace_all_uses_withry   )rc   r   output_nodereturn_argsmutation_node_to_buffer	mutationsbuffers_to_inputsinput_name_to_nodemutationbuffer_name
input_name
input_noderH   rR  user_outputsrV  s                  @r8   r   r     s   2 tHU[112233K>X%%#k.>*?*?1*D*D*DD"1%K-?:c"9:::;ITT/*K*Q*Q*S*STTT $)K    N N-hm<&{3
'
3
""8,, 	% 	%((!5
H7M H !++-- % %1#$a  %		% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	&&x1L1L1L1LMMMM
 C/00223 L &(Ks   *AEE	E	c                 x    t          |           t          |          k     o|dt          |                    | k    S )z2Check whether `candidate` is a prefix of `target`.N)rw   )	candidater?   s     r8   
_is_prefixrq    s4    y>>CKK'QF3CS^^3C,D	,QQr7   
parent_fqn	child_fqnc                    | dk    r|S |                      d          }|                     d          }|d t          |                   |k    rt          d| d|  d          d                    |t          |          d                    S )Nr   rC   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   rw   RuntimeErrorr   )rr  rs  parent_splitchild_splits       r8   _compute_accessorrx    s    R##C((L//#&&K &S&&&'<77PY P PPZ P P P
 
 	

 88KL 1 1 3 34555r7   r(  yc                    dt           j        j        dt          fd}t	          | j        t           j        j                  sJ t	          |j        t           j        j                  sJ  || j                   ||j                  k    S )Nrc   r   c                    g }i dt           ffd}t          | j                  D ]\  }}d t          j        ||j                  D             }|d t          j        ||j                                                  D             z  }|j        dv r|j	        nd}|
                    | d|j         d| d	d
                    |           d           |t          |          <   d                    |          S )Nr   c                     t          | t          j        j                  r%dt	          t          |                              z   S t	          |           S )N%)rV   rP   rz   Noder   r   )r   	nodes_idxs    r8   arg_dumpz>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  sB    #ux}-- 5S2c77!34444s88Or7   c                 ,    g | ]}t          |          S r6   )r   r,  s     r8   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s    RRRcSRRRr7   c                 "    g | ]\  }}| d | S )=r6   )rG   keyvalues      r8   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s:       C       r7   )rT  get_attrr   z: [z](z, )r   )r   r   r   r   tree_mapr}   rn   rU   r   r?   r   r   r   )rc   retr  r   r   	args_dumpr?   r  s          @r8   
graph_dumpz,_check_graph_equivalence.<locals>.graph_dump  s1   $&		S 	 	 	 	 	 	
 !-- 	$ 	$GAtRRV_Xty-Q-QRRRI  "(/(DK"H"H"N"N"P"P   I %)G/L$L$LT[[RTFJJ!JJtwJJJJ499Y3G3GJJJKKK"#Ibhhyy~~r7   )rP   rz   r   r   rV   rc   )r(  ry  r  s      r8   _check_graph_equivalencer    s    %(. S    ( agux~.....agux~.....:ag**QW"5"555r7   gmc                     d}t          | d|           r|dz  }t          | d|           d| }t          | ||           |S )Nr   _spec_r"   )rN   rO   )r  specr   r   s       r8   	_add_specr  &  sd    	A
"lqll
#
# 	Q "lqll
#
# A<<DBdKr7   c                     | j                             t          j        |f          }| j                             t          j        |df          }|S r   )rc   rT  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r8   _generate_flattenr  /  sB    h$$V%84'BBG&&x'7'1FFIr7   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rE   )r  rc   r  rT  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r8   _generate_flatten_specr  5  sH     RD!!$''I8!!)"=i?PQQQr7   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rE   )r  rc   r  rT  r   r6  )r  r   r  r   r  s        r8   _generate_unflattenr  =  sH     RD!!$''I8!!&"7%9KLLLr7   r   c                     |                     d          ^ }}|D ]<}t          | |d           }| d S t          |t          j        j                  s d S |} =t          | |d           S NrC   )rL   r   rV   rP   rQ   rR   )r   r?   r\   r]   rJ   submods         r8   _get_submoduler  E  sy    \\#&&NVU 	 	dD))>44&%(/22 	443t$$$r7   module_to_addcreate_modulec           	         |                     d          ^ }}t          |          D ]\  }}t          | |d           }|[|* |d                    |d |dz                                }nt          j                                        }t          | ||           t          |t          j        j                  s dS |} | 	                    ||           d S )NrC   r"   F)
rL   r   r   r   rP   rQ   rR   rO   rV   
add_module)	r   r?   r  r  r\   r]   r   rJ   r  s	            r8   _add_submoduler  V  s     \\#&&NVUV$$  4dD))>(&sxxwQw'@'@AA**Cv&&&&%(/22 	55NN5-(((((r7   rH  r  c                 &    |dk    r| n	|  d|dz
   S )Nr"   r   r6   )rH  r  s     r8   rE  rE  p  s$     6644$1.r7   	call_namec                 Z    t          j        t          j        |          dz   |           d uS )Nz(@\d+)?$)rematchescape)r  rH  s     r8   rF   rF   v  s&    8BIdOOk19==TIIr7   c                       e Zd Z	 ddej        j        deej        j        df         deee	e
f                  dee	ef         deeej        j        ef                  f
dZd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )_ModuleFrameN
flat_graphr   .module_stackr   r.   c                     | _         | _        | _        | _        | _        | _         _        | _        |	 _        |
 _	        d _
         j        d         \   _        }t           j        |dz              _          |&| _        t          |d          r|j        ni  _        nZ j                             j        t%          t&          j                                                             _        j         _         j        j         _        i  _        i  _        d  _        t5          j         j                  } fd}t7          j        | j        |           j                            |           _         j         j                 r. j         j                 d         }|j        j         j        _         j         j                                     t?           j        j         j        j         j         j        |dz    j                             |
                     j                  }| j        |j         j!        dk    sJ |j         j"        d         }|j         j"        d         }|j#        J |j#        J  j        $                    d           5   fd	tK          |j!                  D             }i }|j#        D ]} j        &                    |          ||<    tO           j        tQ          |          |f|j                   }tS          |j*                  D ]\  }} j        +                    d
tX          j-        ||ft]          |t^                    s|j0        nd|           }t]          |t^                    rd|j0         j        v rHtc          j1         j        |j0                 j2                  |_2        | j         j        |j0                 <   	 d d d            n# 1 swxY w Y    j        j        3                     j                  5  g }|j*        D ]}t]          |t^                    r|                    |j4                   2|j0         j        vr|                    d            Vt]          |tj          tl          tn          tp          f          sJ |                     j        9                     j        |j0                                      tu           j        j        ||j                   } j        j        ;                    tX          j-        |df           j        j        ;                    tX          j-        |df           fdtK          |j!                  D             } fd|j#        D             }d d d            n# 1 swxY w Y    j        J tQ          |           j        _<        | j        _=        d S d S d S )NFr"   r   c                     j         rj          d|  n| }|j        v rj        |         S t          t          j                                                  }|j        |<   |S r  )r   created_modulesr%   rP   rz   r   )r   r>  r  parentri   s      r8   r  z,_ModuleFrame.__init__.<locals>.create_module  sk    06
C&*,,s,,,4////55*58>>+;+;<<-3$T*r7   r   )rr  parent_moduleparent_call_moduler   call_idxr.      c                 J    g | ]}j                             d |            S )_positional_arg_)rc   r   )rG   r+  ri   s     r8   r   z)_ModuleFrame.__init__.<locals>.<listcomp>  sB        J**+Cc+C+CDD  r7   rT  
_constant_)r   r?   r}   r   c                 h    g | ].}j         j                            t          j        |f          /S r6   r  rc   rT  r  r  )rG   r   	args_noderi   s     r8   r   z)_ModuleFrame.__init__.<locals>.<listcomp>  sF        K%33H4DyRSnUU  r7   c                 j    i | ]/}|j         j                            t          j        |f          0S r6   r  )rG   rH   kwargs_noderi   s     r8   r   z)_ModuleFrame.__init__.<locals>.<dictcomp>  sO         t{(66 (;*:   r7   )>r  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   rE  rs  r.   rN   r   rA  r%   rP   rz   r   rc   node_mapnode_to_placeholderr  rx  r  r   rT   r   _SubmoduleEntryr   num_childrenchildren_specscontextr]  ranger   r  ry   r   inputsr^  r  r  rV   r   r   copyrb  inserting_beforer  r   r   r   r   remap_inputr  rT  r}   rn   )ri   r  r   r  r  r  r  r  r  r  r   r.   	num_callsr   r  base_module_framer  	args_speckwargs_spec	arg_nodeskwarg_nodesr   r#  r+  r   flat_arg_nodeinput_nodesinputinputs_noder  r  s   `      `                     @@r8   rf   z_ModuleFrame.__init__|  s    %
$($.("!2"/3)#DHi!m<<V DK)0)A)AIrDJJ.22!%(.."2"233 DK  DJ[&
 =?#% ;?(T^DDH      6=(DKOOO&,l&>&>x&H&HD# 0 I$($5dn$Ea$H!'8'?'H$dn-44#{"&+"4'+'>&];  	 	 	 &))$.99	 T[%<$1Q6666!)8;I#+:1=K$,,,&222++D11 * *   $Y%;<<  	 !'/ E ED(,
(>(>t(D(DK%%2K9%%{3% 	
 !*)*: ; ; * *HC$(J$:$:*'/'- $.c3C#D#D4CHH!3c!3!3 %; 	% 	%M "#'788 ! x4?22-1Ytsx7P7U-V-V* * 0 OCH5!** * * * * * * * * * * * * * *B "33D4KLL * *=?&-  E!%)9:: #**5;77774?::#**40000)! . . / 0	        $** K33DOEJ4OPP    2K&%  !K-;;${A&6 	 #k/==${A&6     "9#9::  	     )0	  K* * * * * * * * * * * * * * *V *666+0+;+;D#(-8D#***k ! %<%<s&   0EP  PP/E5V00V47V4c                 X   | j         dk    sJ d| d            |j        | j        u sJ | j                            d           5  | j                            |j        |j                  }d d d            n# 1 swxY w Y   t          j        |j                  |_        || j	        |<   d S )Nr   zCannot add placeholder z to root module	type_expr)
r   rc   r  r  r   r   rs   r  rb  r  )ri   r(  placeholder_nodes      r8   add_placeholderz_ModuleFrame.add_placeholder   s    x2~~~KKKK~~~w$/))))Z((.. 	P 	P#z55af5OO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P !%	!& 1 1&6 ###s   'A77A;>A;c                 T   t          j        t          j        j        | j        |j                  }t          j        t          j        j        | j        |j                  }| j        	                    |j
        ||          }t          j        |j                  |_        || j        |<   |S rE   )r   tree_map_onlyrP   rz   r~  r  r}   rn   rc   rT  r?   r  rb  r  )ri   r(  r}   rn   r   s        r8   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_function+  s}     #EHM43CQVLL%ehmT5EqxPPz''$??Iaf%%	ar7   c                    |j         | j        u sJ || j        v r| j        |         S |                     d| d           || j        v r| j        |         S |j        dk    s| j                            | j                  | 	                    |           | j
        o| j        j                             | j
                  5  | j
                            d| j                            |                     d d d            n# 1 swxY w Y   | j        |         S |j        dk    r|j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        fv s%t9          |j        d          r2|j        j        dk    r"|                     |           | j        |         S | j                            | j                  &| j                             | j        | j         |          S tC          d|j         d	|           )
Nzremap_input(r  r   r   rT  r0   	_operatorz(Could not run remap_input() on op type: z
 for node )"rc   r  r  r  r  r   r   rA  r   r  r  r  r  
insert_argr  r?   rP   r_  r`  sym_sizerB  rJ   defaultunbindsumdim_IntListviewdiffrN   r0   r  r   readru  )ri   r(  s     r8   r  z_ModuleFrame.remap_input9  s   w$/))))=##

&!&&&'''(((+A..DM!!%))$(33;   ###&2 [&778OPP V V+66q$+:Q:QRS:T:TUUUV V V V V V V V V V V V V V V+A..T_$$H	'+	#+	%)	".	#+	#+  ,// 56H4G;4V4V ''***=###''11= :??48TZ;;;N14NN1NN  s    4D  DDc                      j                              j        d            g } j                             j                  }| j        |j        D ]}t          |t          t          t          t          f          rJ|j         j        v r&|                     j        |j                            ^|                    d            tt!          d|            fdt#           j        t'          fd|D                       |j                  }t+           j        j         j        |j                  }|}ng } j                                        D ]Q}|j        D ]G}|j         j        vr7|                    |           |                     j        |                     nHR j        }t5          |          dk    r|d         }t          |t6          t8          j        j        f          sJ  j                             |           |d S t          |t8          j        j                  r|j!                            d          nd |D             |j!        d<   t5          |          dk    r|| j        j        |d         <   d S tE          |          D ]c\  }	}
|
t8          j        #                    |          |	         j$        }|
j!                            d          |j!        d<   | j        j        |
<   dd S )Nz'Unsupported data type for output node: c                     | d S j         | j                 }|j        v rj        |         S |j        v rj        |         S t	          d|  dj                   )NzCould not find output node z	. Graph: )r  r   r  r  ru  rc   )rP  	seen_noderi   s     r8   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_nodez  sx    >4 OFK8	--=33$":::3I>>&SfSStzSS  r7   c              3   .   K   | ]} |          V  d S rE   r6   )rG   rP  r  s     r8   rK   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s/      PP,,V44PPPPPPr7   r"   r   valc                 B    g | ]}|j                             d           S )r  )rb  rA  )rG   os     r8   r   z1_ModuleFrame.finalize_outputs.<locals>.<listcomp>  s&    ;;;!&**U##;;;r7   )%r  rF  r   r   rA  rs  r  r   rV   r   r   r   r   r   r  r   ru  r  r.   ry   r7  r  r  r  keysusersrw   ru   rP   rz   r~  rc   rP  rb  r   Proxyr   )ri   orig_outputsr  rP  tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outr  s   `           @r8   finalize_outputsz_ModuleFrame.finalize_outputse  sA     4000*..t~>>	 T[%<#+  #^_FVW   {do55$++DOFK,HIIII$++D1111&J&JJ       0PPPP<PPPPP" M
 3I"D$;Y=O3 3J HUMMM!]//11  	!*  I ~T_<<$++I666%,,T]9-EFFF	 = 0J=!!Q&& -a 0-$)>?????
-((( F -77<M""5)));;];;; 	 |!!i&74>DK a111"+L"9"9 > >;&!HNN:66q9>	(3(8(<(<U(C(C	u%4=$[11> >r7   c                     |                      d|                                           | j                            || j                  | j        |<   || j        |j        <   d S )Ncopying)r  format_noderc   	node_copyr  r  r  r   r   s     r8   	copy_nodez_ModuleFrame.copy_node  sW    

9d..00111"j2249IJJd%)	"""r7   c                    d}| j         j        D ]H}|                     ||j                            d          |                                           |dz  }Id}| j        |         }|j        dk    r2|                     |           |dz  }| j        |         }|j        dk    2|                     |           | j         j        D ]"}|j        dk    r|                     |           #d S )Nr   nn_module_stackr"   r   rP  )	r  r   r  rb  rA  r  r   r  run_from)ri   r   r   node_idxs       r8   	run_outerz_ModuleFrame.run_outer  s   O) 	 	DJJq$)--(9::D<L<L<N<NOOOFAA z(#g&&NN4   MH:h'D g&&
 	h O) 	% 	%Dw(""t$$$	% 	%r7   c                 0    | j         rt          |i | d S d S rE   )r  r  )ri   r}   rn   s      r8   r  z_ModuleFrame.print  s/    < 	#4"6"""""	# 	#r7   c                 4   d}|t          | j                  k     r| j        |         }|j        dk    sJ |                                  |                     d||                                           |                     | j                   t          | j                  }|j        dk    r|dk    r|S |                                  |S t          |j                            di                     dk    rt          d|           |j        d         }ddl
m} t          |          dk    r||v r| j        }n)d	 |j        d                                         D             }|d |         | j        k    rK|                                  |                     d
| j                   |                     | j                   |S |J t          | j        |          r||         }|                     d|           t!          |j        d                                                   |         }	t%          | j        | j        | j        | j        | j        | j        | | j        |gz   |	                    d          d         | j        
  
                            |          }|dz  }|| j        k    sJ |j        dk    r*| j        | j                                     |j                   |                     |           |dz  }|t          | j                  k     d S d S )Nr   r   STEPrP  r"   r
  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYc                 z    g | ]8\  }\  }}|d |v r(t          |                    d           d                   ndf9S )r   r  r   )rB  rL   )rG   rH   r>  tys       r8   r   z)_ModuleFrame.run_from.<locals>.<listcomp>  sY     % % %%:D" C1HH3qwws||B/000!D% % %r7   	outliningzCreating new stack frame forr   r  )rw   r   r   r  r  r  r  rb  rA  ru  (torch._export.passes._node_metadata_hookr  rU   r   rc   rq  ru   r  r  r  r  r  r  r  rL   r   r  rs  r   r?   r  )
ri   r  
module_idxr   depthr
  r  node_module_stacknext_modulenext_module_keys
             r8   r  z_ModuleFrame.run_from  sK   
TZ((:h'D7m++++JJLLLJJvx)9)9););<<<JJt())))**Ew(""A:: $O %%'''49==!2B7788A=="#Td#T#TUUU"i(9:O     
 O$$))./AA %)$5!!% %)-3D)E)K)K)M)M% % %!
 !%(D,=== %%'''

;111

4:&&&$000$+->??  06

9;GGG #'ty1B'C'H'H'J'J"K"KE"R'OJO%O(%5#))#..q1*  (8$$  a
 %(99999w*$$/33DK@@@NN4   MHi TZ((((((r7   rE   )r/   r0   r1   rP   rz   r   r   r~  r   r   rB  r   r   r   r   r   r'   rf   r  r  r  r  r  r  r  r  r6   r7   r8   r  r  {  s0        LPb9 b9HNb9 UX]C'(b9 5c?+b9  %8 89b9 ux35FFGHb9 b9 b9 b9H	7 	7 	7  * * *XR> R> R>h* * *
% % %*# # #W W W W Wr7   r  c                       e Zd ZU eed<   ej        j        ed<   ej        j	        ed<   eed<   e
ed<   ej        j        ed<   dS )r  rr  r  r  r   r  r.   N)r/   r0   r1   r   r   rP   rQ   rR   rz   r~  rB  r6   r7   r8   r  r  1  s]         OOO8?"""%%%	HHHMMMHOr7   r  
orig_graphroot_modulec                     i }t          t                    }t          t                    }i }t          | t	          | j                  ||||d dgdd |j        D             |                                           ||fS )N)r   r   r   c                 6    i | ]}|j         	|j        |j         S r6   )r  r   r   s     r8   r   z'_outline_submodules.<locals>.<dictcomp>J  s6     	
 	
 	
	
Iu	
 	
 	
r7   )r.   )r   ru   rM   r  ry   r   r   r  )r  r  r  r  r  r  s         r8   r   r   ;  s    +-J5@5F5FL&1#&6&6J24Oj		
	
 	
$6	
 	
 	

     ikkk##r7   r   r  r  r\   c                 d   |dk    rit          |                                          dd          D ]?}t          | |          -t          | |t          j                                                   @g }t          | j                                                  D ]P\  }}|||z   }t          |||dz              t          | |           |                    ||         ||f           Q|                    t          j        d                     |D ]\  }}}|                     ||           d S )Nr   r"   rC   )r\   r   )r  )ru   r  r  r  rP   rQ   rR   rT   rU   r   delattrr   sortr  
itemgetterregister_module)r  r  r\   r   childrenr   childr  s           r8   r   r   T  sE    ||	(())!""- 	? 	?Cfc**2vsEHOO,=,=>>>HFO113344 7 7e=tmE9S3Y????3u56666MMh)!,,M---" , ,4tU++++, ,r7   c                   *    e Zd ZdZd Zd Zd Zd ZdS )r   av  
    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                 F    t          t                    | _        i | _        d S rE   )r   rM   fqnsstorager   s    r8   rf   z_IVals.__init__  s    $$	r7   c                    |                     d          5  |                    d|j        z   |j                  }t	          j        |j                  |_        ddd           n# 1 swxY w Y   |j        | j        vr:|j        d         }t          j        |j	        |j
                  | j        |j        <   | j        |j                                     |           |S )zM
        Read attribute corresponding to a given intermediate value.
        N__ival__r  r  )dtype)r  r  r   rs   r  rb  r*  rP   emptyshaper-  r)  r   )ri   r   rc   r   	ival_nodefakes         r8   r  z_IVals.read  s   
 ##D)) 	2 	2zDI'=SSI!Yty11IN	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 9DL(( 9U#D&+k$*DJ&O&O&ODL#	$)  %%%s   AA%%A),A)c                     | j         |j                                     |           |                    |          5  |                    d|j        z   |j                  }t          j        |j                  |_        ddd           n# 1 swxY w Y   |                    |          5  |                    dt          j
        j        j        ||f          }t          j        |j                  |_        ddd           dS # 1 swxY w Y   dS )zO
        Update attribute corresponding to a given intermediate value.
        r,  r  NrT  )r)  r   r   r]  r  rs   r  rb  r^  rP   r_  r`  ra  )ri   r   rc   r   r0  new_ival_nodes         r8   rS   z_IVals.update  su    		$)  %%% ""4(( 	2 	2zDI'=SSI!Yty11IN	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 ""9-- 	6 	6!--!5	47H M "&49!5!5M		6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s%   AB

BB*ADD
Dc                    d|fg}|D ]h}|D ]c}|                     |j        |j        f           |j        j        }|j        D ],}|j        | j        v r|                     |j        ||           -dit          t                    }| j
                                        D ]%\  }	}
|
D ]}||                              |	           &|D ]1\  }}||         D ]#}	d|	 }t          ||| j        |	                    $2dS )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.   rc   r   r   r*  rS   r   ru   r)  rU   rO   )ri   
partitionsr  entriesshared_submodulesr   rc   r   r   r   r)  r   r   	ival_names                 r8   r   z_IVals.create  sW    $%!+ 	< 	<* < <	5<8999*!K < <DyDL00EIud;;;<< D!!)//++ 	( 	(JD$ ( (c
!!$''''(   	< 	<HCc
 < <-t--	 YT(:;;;;	<	< 	<r7   N)r/   r0   r1   r   rf   r  rS   r   r6   r7   r8   r   r   j  sZ         2    $6 6 6 < < < < <r7   r   r  c                     |                                 D ]?\  }}|rt          ||          n|}|D ]#}t          | |          }t          |||           $@d S rE   )rU   r   r   rO   )r  r  r  rs  namesr.   r   r  s           r8   r   r     s    
 ',,.. ' '	56?P;	222[ 	' 	'D"d##CFD#&&&&	'' 'r7   c                 f   i }| D ]*}t          |          D ]\  }}t          |j        |j                  }t	          |j        |          }d}|d |         D ]}t          |j        |j                  r|j        }	|j        |j        k    r|	                    d          ^ }
}t          |	|
          j                            |           t          |j        |j                  }t	          |j        |          }||j        _        |||<    n |s|	                    ||j                   d}Ր,|S )NFrC   T)r   rE  r   r  rx  rr  r  r.   r  rL   r   rT   rF  r  r?   rG  )r5  r   r7  r   r   rs  r?   deduplicatedseenr  r\   r   seen_child_fqnseen_targets                 r8   r   r     s]    ' $, $,!"344 #	, #	,HAu"59en==I&u'7CCF L)"1"- , ,+DKFF ,"0F x59,,
 )/S(9(9/??HLLTRRR)3DHdm)L)L&7!,n' ' ;F07=K/	:) , ,,VT[AAA'+G#	,J #"r7   r  scopemodule_id_to_inputs_removedc                    t          t                    t          |           v r$t          |           t          |                    iS | j                                        D ]m\  }t          t          t          j        j	                  |||gz             }|                                D ] \  }}|         
                    |           !nt          | dd          }|t          |j                  dk    rS t          |t          j        j                  sJ t#          t%          d |j                            }	|	d         }
t%          d |j                  }|D ]U}t'          | |j                  <t                    v r+t+          t%          fd|j                            |_        Vi }|	D ]d}|j        |vrd}||j                 D ]@}|                    d          }|dt          |                   d	 |D             k    r|} nA|_|||<   et                      }|                                D ]	\  }}t          |j                  dk    r|t          |          d         }t5          | |          }t          |t          j        t          j        f          sJ |                    |
          5  |                    d
d                    |                    }ddd           n# 1 swxY w Y   |                     |d           |!                    |           |"                    |j                   t          | tF                    r| $                                 t          |           |iS )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.
    Nrc   r   c                     | j         dk    S )Nr   r   r  s    r8   rW  z_sink_params.<locals>.<lambda>0  s    14=#8 r7   r  c                     | j         dk    S )Nr   r   rD  s    r8   rW  z_sink_params.<locals>.<lambda>4  s    )> r7   c                 <    | j         t                             vS rE   )r   r   )r  rA  	submodules    r8   rW  z_sink_params.<locals>.<lambda><  s    af,G9,VV r7   rC   c                 D    g | ]}|                     d           d         S )r   r   )rL   r'  s     r8   r   z _sink_params.<locals>.<listcomp>J  s&    )I)I)Ia!''#,,q/)I)I)Ir7   r  T)propagate_meta)%r   rM   r   rT   rU   r   r
   rP   rQ   rR   rS   r   rw   r   rV   rz   r   ru   filterr   r?   ry   r}   r   rL   r  r   rY   r[   r]  r^  r   rc  
erase_noder   r%   r   )r.   r  r@  rA  r   submod_id_to_inputs_removedrH   rR  rc   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrrV  rG  s      `                 @r8   r   r     s   , #*&1#&6&6#	&zz0006

76

CDD "?0022 5 5i&2),,TFN'	'
 '
# 05577 	5 	5DAq'*11!4444	5 FGT**E}EK((A--**eUX^,,,,,&88%+FFGGFBZN >>LL! 
 
fdk22	  R	]]6Q%Q%QVVVVVI  DI @B 4 49O++
!$), 	 	Bxx}}H#e**%)I)I5)I)I)III%
 J )3 &&  #uuN4::<< & &jtz??Q"3u::<<0I0CCJj5<9K*LMMMMM &&~66 N N ,,Z)9L9LMMN N N N N N N N N N N N N N N &&x&EEE49%%%%&+,, vJJ''s   	*K??L	L	)TrE   )r   )dr   r  loggingr  r  collectionsr   
contextlibr   r   dataclassesr   enumr   typingr   r	   r
   r   r   r   r   r   r   rP   torch.fx._pytreerz   _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*   r:   r<   rY   r[   rQ   rR   r   boolr`   r%   r&   ABCr$   r'   r(   r   r   rq  rx  r  r  r   r~  r  r  r  r  r  rB  rE  rF   r  r  r   r   r   r   r   r   r6   r7   r8   <module>rk     s   



    				 # # # # # # % % % % % %       ! ! ! ! ! !       O O O O O O O O O O O O O O O O O O O O O O  $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ? ? ? ? ? ? 3 3 3 3 3 3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 3 2 2 2 2 2 U U U U U U U U U U 7 7 7 7 7 7 7 7 = = = = = = g!!            ( ( (" 00 00EL%"4ehoEF00x00 00 	00
 00 00 00 00fS
 S
 S
 S
 S
 S
 S
 S
l," ," ," ," ,"%(/ ," ," ,"^    cg    M
 M
 M
 M
 M
 M
 M
 M
b MQ8 88080I88 8 8 848)8>8))8) 
8) 8) 8) 8)vR R R
6# 6# 6# 6 6 6 6$6 6EHO 6 6 6 64%(/ C    %(.     Reh"$57HHIR
X]R R R RMeh"$57HHIM
X]M M M M% % % % % %* AE	) )	)) 8?) HcUEHO%;<=	) ) ) )4/S /S /S / / / /JS J J J J J J
s s s s s s s sl        $EHN $AR $ $ $ $4 GI, ,HO,(,S#X,@C, , , ,,]< ]< ]< ]< ]< ]< ]< ]<@	'	'"	' S#c(]#	' 	' 	' 	'(# (# (#^ BF	p( p(HOp(#tCy.)p( 9p( "*$sCH}*=!>	p( p( p( p( p( p(r7   