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

 %9"r4   c                   ^ U R                   c   S5       e[        5       (       d]  [        R                  R	                  5       (       d  U R
                  (       d)  [        U R                   5      R                  " U /UQ70 TD6$ T(       a  [        U5      nU R                  [        U5      S  nUR                  U4S jU 5       5        [        U5      [        T5      :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rQ   r*   )rS   
kwarg_namekwargss     r5   rW   ,InterpreterModule.forward.<locals>.<genexpr>   s'       &1
!V+ 'F:&&1s   
r{   enable_io_processingF)rz   r   r?   compileris_dynamo_compilingr   typeforwardlist	arg_nameslenextendrB   fxInterpreterr{   run)rt   argsr   arg_listkwarg_namess     `  r5   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 r4   c                 Z   [         R                  R                  X R                  5      U R                  S'   U R                  R                  5         / U l        U R                  R                   H:  nUR                  S:X  d  M  U R                  R                  UR                  5        M<     g )Nrz   placeholder)r?   r   GraphModuler{   __dict__lintr   nodesopappendrK   rt   nodes     r5   finalizeInterpreterModule.finalize   sr     ).(<(<T::(Nn%

 JJ$$Dww-'%%dkk2 %r4   c                 "    [        U SUUUU5      $ )Nr   r   rt   print_outputinclude_strideinclude_devicecoloreds        r5   print_readable InterpreterModule.print_readable   #     
 	
r4   )r   rp   r   r{   rQ   TFFF)r+   r,   r-   r.   r>   r?   r   r   rA   Graphrw   r   r   r   r   r3   __classcell__r   s   @r5   r   r      so    
 ((&&--
 	:xx~~	: $J	: 	:!F3& 
 
r4   r   c                   b   ^  \ rS rSrSrS\\   S\\   4U 4S jjr	S r
S r    S
S jrS	rU =r$ )r       z
A module that carries a sequence of InterpreterModules corresponding to
a sequence of calls of that module. Each call to the module dispatches
to the next InterpreterModule, and wraps back around after the last.
attrscall_modulesc           	         > [         TU ]  5         U(       d   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   )r~   r   r`   r\   getattrrp   _call_modules
_num_calls)rt   r   r   accessorr   s       r5   r   $InterpreterModuleDispatcher.__init__   sd    |$Q00HDGLOX$FG ?&&)r4   c                     U R                   U R                     nU R                  S-   [        U R                   5      -  U l         U" U0 UD6$ ! [         a	    SU l        e f = f)Nr   r   )r   r   r   	Exception)rt   r   r   call_modules       r5   r   #InterpreterModuleDispatcher.forward   sd    ((9??Q.#d6H6H2II	/// 	DO	s   A A c                     U R                   $ rQ   )r   rs   s    r5   r   (InterpreterModuleDispatcher.call_modules  s    !!!r4   c           	          U R                    Vs/ s H  nUR                  UUUU5      PM     nnSR                  U5      $ s  snf )N
)r   r   join)rt   r   r   r   r   modoutputss          r5   r   *InterpreterModuleDispatcher.print_readable  s[     ))
 * 	 * 	 
 yy!!
s   A )r   r`   r   rp   r   )r+   r,   r-   r.   r>   rZ   rw   r   r   r   r   r   r   r3   r   r   s   @r5   r    r       sG    c#h d;L6M "
 " "r4   r    c                       \ rS rSrSr\R                    SS\R                  S\R                  S\	\
   S\\\
4   S-  S\
S-  S	\	\
   4S
 jj5       rS	\	\   4S jrSrg)r   i  zF
Adapts input arguments with ``input_spec`` to align ``target_spec``.
Ntarget_spec
input_spec
input_argsmetadataobjrq   c                     g)z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr*   )rt   r   r   r   r   r   s         r5   adaptFlatArgsAdapter.adapt  s     	r4   c                     / $ )z>Returns a list of paths that are used to access the flat args.r*   rs   s    r5   get_flat_arg_paths"FlatArgsAdapter.get_flat_arg_paths+  s    	r4   r*   )NN)r+   r,   r-   r.   r>   abcabstractmethodpytreeTreeSpecr   r	   dictrw   r   r   r3   r*   r4   r5   r   r     s     	 +/	__	 OO	 I		
 sCx.4'	 4Z	 
c	 	DI r4   r   c                   v   ^  \ rS rSr SS\S\S-  4U 4S jjjrS rS rS r	S	 r
S
 rS r    SS jrSrU =r$ )r!   i0  Nexport_moduleflat_args_adapterc           	        >^, [         T-U ]  5         UR                  R                  b  [	        S5      eS nUR
                   Vs/ s H  oDR                  PM     nnUS   S: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        ['        X`R                  5        [)        U5        [+        U5      U l        [/        5       U l        [3        X`5      u  pxU R0                  R5                  UR7                  5       5        [9        UR:                  X5        UR<                  U l        / U l        UR@                  n	[C        5       n
0 nU R                  RD                   H  nX   nU" U5      U;  a>  [        RF                  RI                  URK                  5       URL                  S9X" U5      '   [O        X" U5         U U[P        RR                  S9  U
RU                  U5        M     [C        U R                  RV                  5      n[C        5       n0 nU R                  RX                   H  nX;   a  SnURZ                  U   nOS	nX   nU" U5      U;  a  URK                  5       U4UU" U5      '   [O        UU" U5         S   U U[P        R\                  US
9  URU                  U5        M     U	R_                  5        H  u  nnX;   d  X;   a  M  SnU" U5      U;   d)  [a        U[        RF                  RH                  5      (       d  S	nU(       aB  U" U5      U;  a  US	4UU" U5      '   [O        UU" U5         S   U U[P        R\                  S	S
9  M  U" U5      U;  a
  UX" U5      '   [O        X" U5         U U[P        RR                  S9  M     0 nURZ                  R_                  5        Hq  u  nnU" U5      U;  a:  [a        U[        Rb                  5      (       a  URK                  5       nUUU" U5      '   UU" U5         n[O        UU U[P        Rd                  S9  Ms     [g        [h        5      m,[C        5       nU,4S jn[C        5       nU R                  Rj                   GH  nURl                  [n        RR                  :X  d/  URl                  [n        R\                  :X  a  URp                  (       a  [s        URt                  S5      (       d   e[a        URv                  [x        5      (       d   eU" U" UR@                  URv                     5      URt                  Rz                  URv                  5        URU                  URv                  5        URU                  URv                  5        GM  URl                  [n        R\                  :X  a  URp                  (       a?  URl                  [n        R|                  :X  d!  URl                  [n        R~                  :X  d  GM{  [s        URt                  S5      (       d   e[a        URv                  [x        5      (       d   eU" U" URZ                  URv                     5      URt                  Rz                  URv                  5        URU                  URv                  5        GM     URZ                  R_                  5        HN  u  nnUU;  d  M  U" U5      nUT,;   d   eT,U   S   u  n n!U" UU U5        URU                  WRv                  5        MP     UR@                  R_                  5        H3  u  nnUU;  d  M  U" U5      n"U"T,;  a  M  T,U"   S   u  n n!U" U"U U5        M5     0 n#T,R7                  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R7                  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 lE        S	U lF        [        U5       V*Vs0 s H	  u  n*nUU*_M     n+n*nU R                  SS9 H  u  nn!UU+;  d  M  [        U+5      U+U'   M     [        U U+5        U R                  R                  5         U R                  5         g s  snf s  sn%f s  snf s  snf s  sn)f s  snn*f )Nz%Unflattening on JointExportModule NYIc                     [        U [        R                  5      (       aD  [        U R	                  5       U R                  5       U R                  5       U R                  5       S9$ [        U 5      $ )z8Returns _TensorID dataclass for tensors, otherwise id().)r:   r;   r<   r=   )	rb   r?   re   r8   r:   r;   r<   r=   id)r   s    r5   _id'UnflattenedModule.__init__.<locals>._id:  sU    #u||,, $'$7$7$9::<#&#5#5#7	  c7Nr4   r    unflattened_moduleF)requires_grad)rL   T)rL   rM   c                 4   > TU    nUR                  X45        g rQ   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r5   add_to_consts_map5UnflattenedModule.__init__.<locals>.add_to_consts_map  s    "6*Ii56r4   namer   @r   remove_duplicate)Mr~   r   graph_signaturebackward_signature
ValueErrormodule_call_graphfqnr   r{   r?   r   r   r   r   rz   metaadaptedrF   r   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_root_module_typerp   _IValsivals_outline_submodulesr_   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrZ   
parametersr]   rc   cloner   rl   r$   r/   addnon_persistent_buffersbuffers	constantsr0   ra   rb   re   r1   r   r   input_specskindr   rM   r[   argrK   rw   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   r   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   r   ).rt   r   r   r   entryfqn_listexport_graphseen_modules
seen_attrsr  assigned_paramsid_to_paramr   paramr	  assigned_buffersid_to_bufferrM   r'   tensor	is_bufferid_to_constr   r(   	_constantconsts_targetsr   added_params_bufferss
const_nameconstconst_idph_name_	tensor_idinputs_to_statenode_targetttargetsnredirected_call_indicesr   i	fqn_orderr   r   s.                                               @r5   r   UnflattenedModule.__init__1  sF   
 	((;;GDEE		 ,9+J+JK+J%II+JK{b    3 34'(E(EFXX^^%
#'

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

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

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

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

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

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

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

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

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

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

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

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

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

 88KL 1 3455r4   rN  yc                 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  $ )Nr{   rq   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 )Nrq   c                    > [        U [        R                  R                  5      (       a  S[	        T[        U 5         5      -   $ [	        U 5      $ )N%)rb   r?   r   Noderw   r   )r  	nodes_idxs    r5   arg_dump>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  s:    #uxx}}--S2c7!3444s8Or4   =)ry  get_attrr   z: [z](z, )r   )rw   r  r   r   tree_mapr   r   ra   r   rK   r   r   r   )r{   retr  r6  r   r  	args_dumpkeyvaluerK   r  s             @r5   
graph_dump,_check_graph_equivalence.<locals>.graph_dump  s   $&		S 	
 !-GA-3__Xyy-QR-QcS-QIR"(//(KK"H"N"N"P"PJC %q "P I %)GG/L$LT[[RTFJJ!Btwwiq499Y3G2HJK"#Ibh . yy~ Ss   D)D.
)r?   r   r   rw   rb   r{   )rN  r  r  s      r5   _check_graph_equivalencer    sz    %((.. S * agguxx~~....agguxx~~....agg*QWW"555r4   gmc                     Sn[        U SU 35      (       a  US-  n[        U SU 35      (       a  M  SU 3n[        XU5        U$ )Nr   _spec_r   )r[   r\   )r  specr6  r   s       r5   	_add_specr    sR    	A
"qcl
#
#	Q "qcl
#
#A3<DBdKr4   c                     U R                   R                  [        R                  U45      nU R                   R                  [        R
                  US45      nU$ r   )r{   ry  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r5   _generate_flattenr    sF    hh$$V%8%84'BG&&x'7'7'1FIr4   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rQ   )r  r{   r  ry  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r5   _generate_flatten_specr    sB     RD!!$'I88!!)"="=?PQQr4   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rQ   )r  r{   r  ry  r   rT  )r  r   r  r   r  s        r5   _generate_unflattenr    sB     RD!!$'I88!!&"7"7%9KLLr4   r   c                     UR                  S5      Gt p#U HA  n[        XS 5      nUc    g [        U[        R                  R
                  5      (       d    g Un MC     [        XS 5      $ )NrO   )rY   r   rb   r?   r]   r^   )r   rK   rh   ri   rV   submods         r5   _get_submoduler    s\    \\#&NVD)>&%((//22  3t$$r4   module_to_addcreate_modulec           	         UR                  S5      Gt pE[        U5       H  u  pg[        XS 5      nUcK  Ub  U" SR                  US US-    5      5      nO[        R
                  R                  5       n[        XU5        [        U[        R
                  R                  5      (       d    gUn M     U R                  XR5        g )NrO   r   F)
rY   r  r   r   r?   r]   r^   r\   rb   
add_module)	r   rK   r  r  rh   ri   r6  rV   r  s	            r5   _add_submoduler    s     \\#&NVV$D)>(&sxxwQ'@A*Cv&&%((//22 % NN5(r4   ri  r4  c                 &    US:X  a  U $ U  SUS-
   3$ )Nr   r   r*   )ri  r4  s     r5   rf  rf    s$     640$qQ00r4   	call_namec                 b    [         R                  " [         R                  " U5      S-   U 5      S L$ )Nz(@\d+)?$)rematchescape)r  ri  s     r5   rR   rR     s%    88BIIdOk19=TIIr4   c                   "   \ rS rSr SS\R
                  R                  S\\R
                  R                  S4   S\	\\
\
S-  \4      S\\
\4   S\R
                  R                  \-  S-  4
S	 jjrS
 rS rS rSS jrS rS rS rS rS rSrg)_ModuleFramei  N
flat_graphr   .module_stackr   r)   c                   ^ ^^! UT l         UT l        UT l        UT l        UT l        UT l        TT l        UT l        U	T l        U
T l	        ST l
        T R                  S   u  T l        m!n[        T R                  US-   5      T l        T   Ub,  UT l        [        US5      (       a  UR                   O0 T l        O`T R
                  R#                  T R                  [%        [&        R(                  R+                  5       T!S95      T l        TR                   T l        T R                  R,                  T l        0 T l        0 T l        S T l        TGbF  [5        TR                  T R                  5      nUU U!4S jn[7        TR                  UT R                  U5        TR,                  R9                  U5      T l        T R                  T R                     (       aA  T R                  T R                     S   nUR                  R:                  T R                  l        T R                  T R                     R=                  [?        T R                  R                  T R                  R                  T R2                  T R                  US-   T R                  S95        U
R#                  T R                  5      nUGb  T R                  Gb  UR@                  RB                  S	:X  d   eUR@                  RD                  [F        L d   eUR@                  RI                  5       u  nnURD                  [F        L d   eURD                  [J        L d   eT R,                  RM                  S 5         [O        URB                  5       Vs/ s H!  nT R,                  RQ                  S
U 35      PM#     nn0 nURR                   H!  nT R,                  RQ                  U5      UU'   M#     [U        T R                  [G        U5      U4UR@                  5      n[W        URX                  5       H  u  nnT R,                  R[                  S[\        R^                  UU4[a        U[b        5      (       d  URd                  OSU 3S9n[a        U[b        5      (       a  Mm  URd                  T R                  ;   d  M  [f        Rf                  " T R                  URd                     Rh                  5      Ul4        UT R0                  T R                  URd                     '   M     S S S 5        T R                  R,                  Rk                  T R2                  5         / nURX                   H  n[a        U[b        5      (       a  UR=                  URl                  5        M5  URd                  T R                  ;  a  UR=                  S 5        Mb  [a        U[n        [p        [r        [t        45      (       d   eUR=                  T R                  Rw                  T R                  URd                     5      5        M     [y        T R                  R                  UUR@                  5      nT R                  R,                  R{                  [\        R^                  US45      nT R                  R,                  R{                  [\        R^                  US45      n[O        URB                  5       Vs/ s H9  nT R                  R,                  R{                  [\        R^                  UU45      PM;     nnURR                   V s0 s H:  n U T R                  R,                  R{                  [\        R^                  UU 45      _M<     nn S S S 5        T R2                  c   e[G        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   r|   c                    > TR                   (       a  TR                    SU  3OU nUTR                  ;   a  TR                  U   $ [        [        R                  R                  5       TS9nUTR                  U'   U$ )NrO   r  )r   created_modulesr   r?   r   r   )r   r^  r  parentrt   r|   s      r5   r  ,_ModuleFrame.__init__.<locals>.create_moduleH  sj    06

&**Qse,4/////55*588>>+;C-3$$T*r4   r   )r  parent_moduleparent_call_moduler   call_idxr)      _positional_arg_ry  
_constant_)r   rK   r   r   )@r  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   rf  r  r)   r[   r   rb  r   r?   r   r   r{   node_mapnode_to_placeholderr  r  r  r   r`   r   _SubmoduleEntryr@  num_childrenr   rB   childrenr   r  re  r   contextr  r  inputsr  r  r  rb   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  ry  r   r   )"rt   r  r   r  r  r  r  r  r  r  r   r)   	num_callsr   r  base_module_framer?  	args_speckwargs_specrI  	arg_nodeskwarg_nodesr   rC  r  flat_arg_nodeinput_nodesrD  inputs_node	args_nodekwargs_noder6  rT   r|   s"   `      `                         @r5   r   _ModuleFrame.__init__  s}    %
$($.("!2"&"3"3B"7"i#DHHi!m< DK)0)A)ArDJ..22!%((.."2r:DK  DJ[[&&
 =?#% 8<(T^^DH 6==(DKKO&,ll&>&>x&HD#  0$($5$5dnn$Ea$H!'8'?'?'H'H$dnn-44#{{"&++"4"4'+'>'>&];;	 &))$..9	 T[[%<$$11Q666$$))U222%.%6%6%?%?%A"I{>>U***##t+++++D1  %Y%;%;<< JJ**-=cU+CD<   !'//D(,

(>(>t(DK% 02KK9%{3%%	
 !*)*:*: ;HC$(JJ$:$:*'//'- $.c3C#D#D  HH#-cU!3 %; 	%M "#'788 xx4??2-1YYtsxx7P7U7U-V*) 001JK! !< 2B ""33D4K4KL:<&--E!%)9::#**5;;74??:#**40)! . . / 0	       $** 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##*m &=  21DK MLsS   ^ 4(^C;^ A&^ 8F^<A ^2^<A^7^<^  
^/2
^<<
_
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   r{   r  r  r   r   r   r  r   r  )rt   rN  placeholder_nodes      r5   add_placeholder_ModuleFrame.add_placeholder  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$ rQ   )r   tree_map_onlyr?   r   r  r  r   r   r{   ry  rK   r  r   r  )rt   rN  r   r   r   s        r5   copy_sym_call_function#_ModuleFrame.copy_sym_call_function  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar4   c                 ^   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      b  U R>                  RA                  X5      $ [C        SUR
                   S	U 35      e! , (       d  f       GN= f)
Nzremap_input(r  r   r   ry  r,   	_operatorz(Could not run remap_input() on op type: z
 for node )"r{   r  r  r;  r  r   r   rb  r   r  r  r  r  
insert_argr  rK   r?   r  r  sym_sizerC   rV   r  unbindsumdim_IntListviewdiffr[   r,   r  r   readr  )rt   rN  s     r5   r  _ModuleFrame.remap_input  sG   ww$//)))==##

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

-(  -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_noder{   	node_copyr  r  r  r   r   s     r5   	copy_node_ModuleFrame.copy_nodem  sL    

9d..01"jj2249I9IJd%)		"r4   c                    [        U R                  R                  5       H>  u  pU R                  XR                  R                  S5      UR                  5       5        M@     SnU R                  U   nUR                  S:X  a7  U R                  U5        US-  nU R                  U   nUR                  S:X  a  M7  U R                  U5        U R                  R                   H&  nUR                  S:X  d  M  U R                  U5        M(     g )Nr  r   r   r   rx  )
r  r  r   r;  r   rb  r=  r   r?  run_from)rt   r6  r   node_idxs       r5   	run_outer_ModuleFrame.run_outerr  s     !6!67GAJJq))--(9:D<L<L<NO 8 zz(#gg&NN4 MH::h'D gg&
 	h OO))Dww("t$ *r4   c                 >    U R                   (       a  [        U0 UD6  g g rQ   )r  r;  )rt   r   r   s      r5   r;  _ModuleFrame.print  s    <<4"6" r4   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   STEPrx  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  r9  r   rb  r  (torch._export.passes._node_metadata_hookrJ  ra   rC   rY   r   r{   r  r   keysr  r  r  r  r  r  r   rB  r  r  rK   r?  )rt   rC  
module_idxr   depthr  rJ  node_module_stackrT   r^  r|   next_modulenext_module_keys                r5   rB  _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 MHs TZZ((D%s   A M-)r  r  r  r   r{   r   r)   r   r  r  r  r  r   r  r  r  r  r  r  rQ   )rq   N)r+   r,   r-   r.   r?   r   r   rB   r  r   rw   rC   r   r   r   r!   r   r  r  r  r$  r9  r?  rD  r;  rB  r3   r*   r4   r5   r  r    s     CGc9HHNNc9 UXX]]C'(c9 5cDj#!567c9  %8 89c9 $$'884?c9J	7)V(*Y>v*
%&#
\r4   r  c                       \ rS rSr% \\S'   \R                  R                  \S'   \R                  R                  \S'   \\S'   \\S'   \R                  R                  \S'   Srg	)
r  i  r  r  r  r   r  r)   r*   N)r+   r,   r-   r.   rw   rA   r?   r]   r^   r   r  rC   r3   r*   r4   r5   r  r    s;    O88??"%	HMHHOOr4   r  
orig_graphroot_modulec                 F   0 n[        [        5      n[        [        5      n0 n[        U [	        U R
                  5      UUUUS S/SUR                   Vs0 s H-  nUR                  (       d  M  UR                  UR                  _M/     snUS9R                  5         X44$ s  snf )N)r   Nr   r   )r)   )
r   r   rZ   r  rB   r   r   r?  r   rD  )rU  rV  r  r  r  r  r  s          r5   r   r     s    +-J5@5FL&1#&6J24Oj	
 %66	
6 'EIIu&6	

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

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

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

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

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

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

module: GraphModule, potentially containing nested submodules.
inputs_to_state: mapping graph input names to the corresponding key in the state_dict.
scope: tracks where we are in the module hierarchy, so that we can emit the
    right `getattr(self, "foo.bar")` calls, etc.
module_id_to_inputs_removed: records inputs removed by child modules, mapping
    the module object id to the list of placeholder node names in the child module
    that were removed.
Nztorch.nn.Moduler{   r   c                      U R                   S:H  $ )Nr   r   r4  s    r5   r|  _sink_params.<locals>.<lambda>  s    144=#8r4   r  c                      U R                   S:H  $ )Nr   r}  r~  s    r5   r|  r    s    )>r4   c                 :   > U R                   T[        T5         ;  $ rQ   )r   r   )r4  rz  	submodules    r5   r|  r    s    aff,G9,VVr4   rO   r   r  T)propagate_meta)#r   rZ   r   r`   ra   r  r
   r_   r   r   r   rb   r?   r   r   r   filterr   rK   rB   r   r   rY   r.  r   re   rg   r  r  r   r  
erase_noder  r   r   )r)   r0  ry  rz  r   submod_id_to_inputs_removedrT   r  r{   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitrN  inputs_removed	attr_path
state_attrr{  r  s      `                  @r5   r  r    s   , #*&1#&6#	&z006
76
CDD "??002i&2"I.FN'	'
# 0557DA'*11!4 8 3 FGT*E}EKK(A-**eUXX^^,,,,&8%++FGF [A-T6":N >L!fkk2	  R	]6Q%QVIIDI " @B99O+
!)),Bxx}H#e*%5)I5a!''#,q/5)II%
	 - )3&/ 4  #uN4::<tzz?Q"3u:<0I0CJj5<<9K9K*LMMMM &&~6 ,,Z)9LM 7 &&x&E499% = &+,,vJ''O *J8 76s   7M 
"M%%
M3	)TrQ   )r   )gr   r  loggingr  r  collectionsr   collections.abcr   
contextlibr   r   dataclassesr   enumr   typingr	   r
   r?   torch.fx._pytreer   _pytreer  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch.exportr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   r   r   torch.fx._symbolic_tracer   torch.fx.graph_moduler   r   r   r   r   _remove_effect_tokens_passr   	getLoggerr+   r  __all__r$   r8   rF   rH   re   rg   r]   r^   rw   boolrl   rn   r   r    ABCr   r!   r"   r   r   r   r   r  r  r  r  r   r  r  r  r  r  r  rC   rf  rR   r  r  r   r   r  r   rZ   r  r  r   r  r*   r4   r5   <module>r     s   
    	 # $ %  !    $ $ $ $ ? ( 3	 	 	 < U U 7 = !  $     ( (" 00llU///%((//A00xx00 00 	00
 00f	 	U
 U
p-".%((// -"`cgg ,{
 {
~ JN!!0?$0F!!HE)88>>E))E) 
E)PUXX^^ d
 $NR
6# 6# 6# 6$6 6EHHOO 66%((// C %((..  R003DDR
XX]]RM003DDM
XX]]M% % %* >B	)	)) 88??) SE588??23d:	)41S 1S 1S 1JS J J J
S Sl   $EHHNN $AR $4 GI,HHOO,(,S#X,@C,,?= ?=D	'	'"	' S#c(]#	'(#^ ?C	p(HHOOp(#tCy.)p( 9p( "&c3s8m!4t!;	p(r4   