
    /j'                       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 d dl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  d dl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/ d dlm0Z0m1Z1 ddl2m3Z3  ej4        e5          Z6g dZ7 G d de          Z8 ed           G d d                      Z9da:e
d             Z;	 d^dej<        ej=        z  ej>        j?        z  dej>        j?        de@de8deAf
dZB G d  d!          ZC G d" d#eCej>        j?                  ZD G d$ d%eCej>        j?                  ZE G d& d'e jF                  ZG G d( d)eCej>        j?                  ZH	 d_d*ed+eGdz  d,eHfd-ZId.ej        jJ        d/e#d,dfd0ZKd.ej        jJ        d,e@dz  fd1ZLd2 ZMd3 ZNd4e@d5e@d,e@fd6ZOd7ej>        j?        d8ej>        j?        fd9ZPd:ej>        j?        d,e@fd;ZQd:ej        jR        d,ej        jS        fd<ZTd:ej        jR        eDz  eHz  d,ej        jS        fd=ZUd:ej        jR        eDz  eHz  d,ej        jS        fd>ZVd?ej>        j?        de@fd@ZW	 d_d?ej>        j?        de@dAej>        j?        dBee@gej>        j?        f         dz  fdCZXdDe@dEeYd,e@fdFZZdGe@dDe@d,eAfdHZ[ G dI dJ          Z\e G dK dL                      Z]dMej        jJ        dNeHfdOZ^	 d`dQej>        j?        dRe_e@eYf         dSe@fdTZ` G dU dV          Zad:ej        jR        dNeHdWe_e@ebe@         f         fdXZcdY Zd	 d_d*ej>        j?        dZe_e@eee@         f         d[eee@         d\e_eYebe@         f         dz  fd]ZfdS )a    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                       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     [/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/export/unflatten.pyr$   r$   2   s"        IFHFFFr1   r$   T)frozenc                   F    e Zd ZU dZej        ed<   eed<   eed<   eed<   dS )	_TensorIDzJCustom tensor identifier containing storage, stride, and size information.untyped_storagestridesizestorage_offsetN)	r)   r*   r+   __doc__torchUntypedStorage__annotations__tupleintr0   r1   r2   r5   r5   9   sE         TT))))MMM
KKKr1   r5   c               #   8   K   t           } da 	 d V  | a d S # | a w xY wNF)RUN_WITH_INTERPRETER)old_flags    r2   _disable_interpreterrD   F   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    rUt          | t          j        j                  st          dt          |                      |                    ||            h|t          j        k    rRt          | t          j                  st          dt          |                      |                    || |           |t          j        k    r|t          | t*                    rt          d          t          | t          j        t          j        f          st          dt          |                      t          |||            V|t          j        k    rOt          | t          j        j                  st          dt          |                      t          |||            d S )	N.c              3   B   K   | ]\  }}t          |          |V  d S N)_is_call_name).0kt_callitems      r2   	<genexpr>z_assign_attr.<locals>.<genexpr>g   sK        Av D))     r1   z9expected torch.nn.Parameter for PARAMETER attr_kind, got z0expected torch.Tensor for BUFFER attr_kind, got )rI   z2FakeScriptObject should only exist during tracing.zHexpected torch.Tensor or torch.ScriptObject for CONSTANT attr_kind, got z3expected torch.nn.Module for MODULE attr_kind, got )splitsethasattrsetattrr;   nnModuleupdate_modulesitemsr$   r,   
isinstance	ParameterAssertionErrortyperegister_parameterr-   Tensorregister_bufferr.   r   ScriptObjectr/   )
rE   rF   rG   rH   rI   prefixfield
to_modulestsrR   s
            @r2   _assign_attrri   S   s    \\#&&NVU J 
 
#&55# 	 	I9d++ <	4):):;;;II    !*!3!9!9!;!;      
 

 "0 "0		+++h(:;; $`PTU]P^P^``   ((9999)***h55 $WtH~~WW   %%eX*%MMMM),,,($455 $H   L&  	 %o_cdl_m_moo   Iuh////)***h88 $Z$x..ZZ   Iuh///E"0 "0r1   c                   2    e Zd ZU edz  ed<   dedz  fdZdS )_SubmoduleBaseN_tyreturnc                     | j         S )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.
        )rl   selfs    r2   	type_namez_SubmoduleBase.type_name   s     xr1   )r)   r*   r+   strr=   rq   r0   r1   r2   rk   rk      sB         	tOOO3:      r1   rk   c                        e Zd ZU dZej        j        dz  ed<   	 ddej        j        de	dz  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.
    Ngraph_modulegraphtyc                     t                                                       || _        || _        | | j        _        t
          | _        d S rM   )super__init__ru   rl   owning_modulerB   _run_with_interpreter)rp   ru   rv   	__class__s      r2   ry   zInterpreterModule.__init__   sA    
 	
#'
 %9"""r1   c                 H   | j         t          d          t                      sHt          j                                        s| j        s# t          | j                   j        | g|R i S rt          |          }| j
        t          |          d          }|                    fd|D                        t          |          t                    k    r/t          dt          |           dt                               t          |          t          | j
                  k    r4t          dt          |           dt          | j
                             t          |          }t          j                            | | j                  j        |dd	iS )
Nz&Didn't finalize this InterpreterModulec              3   0   K   | ]}|v |         V  d S rM   r0   )rO   
kwarg_namekwargss     r2   rS   z,InterpreterModule.forward.<locals>.<genexpr>   s?          "!V++ :&++++   r1   zkwarg_names length z does not match kwargs length zarg_list length z! does not match arg_names length ru   enable_io_processingF)rt   r_   r   r;   compileris_dynamo_compilingr{   r`   forwardlist	arg_nameslenextendr>   fxInterpreterru   run)rp   argsr   arg_listkwarg_namess     `  r2   r   zInterpreterModule.forward   s   $ !IJJJ%'' %	N..00%	8<8R%	 34)**24I$III&III '
  ::"nS]]__=        &1         {##s6{{22(kc+.>.>kk^abh^i^ikk   x==C$7$777(p3x==pp[^_c_m[n[npp   X8''DJ'??C,1  r1   c                    t           j                            | | j                  | j        d<   | j                                         g | _        | j        j        D ],}|j        dk    r| j        	                    |j
                   -d S )Nrt   placeholder)r;   r   GraphModuleru   __dict__lintr   nodesopappendrG   rp   nodes     r2   finalizezInterpreterModule.finalize   s     ).(<(<T4:(N(Nn%
 J$ 	3 	3Dw-''%%dk222	3 	3r1   TFc                 *    t          | d||||          S )Nr   r   rp   print_outputinclude_strideinclude_devicecoloreds        r2   print_readablez InterpreterModule.print_readable   (     
 
 	
r1   rM   TFFF)r)   r*   r+   r:   r;   r   r   r=   Graphrr   ry   r   r   r   __classcell__r|   s   @r2   r   r      s          
 (&----
 	: 	:x~	: $J	: 	: 	: 	: 	: 	:( ( (T3 3 3& 
 
 
 
 
 
 
 
r1   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t          d          |d         j        | _        |D ]'}t	          | |t          |d         |                     (|d         j        | _        || _        d| _        d S )Nzcall_modules must not be emptyr   )	rx   ry   r_   r[   rW   getattrrl   _call_modules
_num_calls)rp   r   r   accessorr|   s       r2   ry   z$InterpreterModuleDispatcher.__init__  s     	C !ABBB$Q0 	H 	HHD(GLOX$F$FGGGG?&)r1   c                     | j         | j                 }| j        dz   t          | j                   z  | _        	  ||i |S # t          $ r	 d| _         w xY w)Nr   r   )r   r   r   	Exception)rp   r   r   call_modules       r2   r   z#InterpreterModuleDispatcher.forward  sn    (9?Q.#d6H2I2II	;//// 	 	 	DO	s   A   Ac                     | j         S rM   )r   ro   s    r2   r   z(InterpreterModuleDispatcher.call_modules  s    !!r1   TFc                 `    fd| j         D             }d                    |          S )Nc                 @    g | ]}|                               S r0   )r   )rO   modr   r   r   r   s     r2   
<listcomp>z>InterpreterModuleDispatcher.print_readable.<locals>.<listcomp>#  sG     
 
 
  	 
 
 
r1   
)r   join)rp   r   r   r   r   outputss    ```` r2   r   z*InterpreterModuleDispatcher.print_readable  sV    
 
 
 
 
 
 
 )
 
 
 yy!!!r1   r   )r)   r*   r+   r:   rU   rr   r   r   ry   r   r   r   r   r   s   @r2   r    r       s         	c#h 	d;L6M 	 	 	 	 	 	  " " "
 " " " " " " " "r1   r    c                       e Zd ZdZej        	 	 ddej        dej        dee	         de
ee	f         dz  de	dz  dee	         fd	            Zdee         fd
ZdS )r   zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    Ntarget_spec
input_spec
input_argsmetadataobjrm   c                     dS )z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr0   )rp   r   r   r   r   r   s         r2   adaptzFlatArgsAdapter.adapt4  s	     	r1   c                     g S )z>Returns a list of paths that are used to access the flat args.r0   ro   s    r2   get_flat_arg_pathsz"FlatArgsAdapter.get_flat_arg_paths@  s    	r1   )NN)r)   r*   r+   r:   abcabstractmethodpytreeTreeSpecr   r	   dictrr   r   r   r0   r1   r2   r   r   /  s          	 +/	 	_	 O	 I		
 sCx.4'	 4Z	 
c	 	 	 	DI      r1   r   c                   f     e Zd Z	 ddededz  f fdZ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 }d |j        D             }|d         dk    rt          d|d                   t          |j                  }t          |j                  | _        t          j
                                        | _        | | j        _        t          |j                  | _        || _        |j        j        | _        | | j        d<   d| _        t"          | _        t'          || j                   t)          |           t+          |          | _        t/                      | _        t3          ||           \  }}| j                            |                                           t9          |j        | |           |j        | _        g | _        |j         }tC                      }	i }
| j        j"        D ]}||         } ||          |
vrDt          j#        $                    |%                                |j&        	          |
 ||          <   tO          |
 ||                   | |tP          j)        
           |	*                    |           tC          | j        j+                  }tC                      }i }| j        j,        D ]}||v rd}|j-        |         }n
d}||         } ||          |vr"|%                                |f| ||          <   tO          | ||                   d         | |tP          j.        |           |*                    |           |/                                D ]\  }}||	v s||v rd} ||          |v sta          |t          j#        j$                  sd}|rQ ||          |vr|df| ||          <   tO          | ||                   d         | |tP          j.        d            ||          |
vr||
 ||          <   tO          |
 ||                   | |tP          j)        
           i }|j-        /                                D ]|\  }} ||          |vr<ta          |t          j1                  r|%                                }|| ||          <   | ||                   }tO          || |tP          j2        
           }tg          th                    'tC                      }'fd}tC                      }| j        j5        D ]2}|j6        tn          j)        k    s|j6        tn          j.        k    r|j8        rts          |j:        d          s$t          dtw          |j:                             ta          |j<        tz                    s$t          dtw          |j<                              | ||j         |j<                           |j:        j>        |j<                   |*                    |j<                   |*                    |j<                   |j6        tn          j.        k    r|j8        r*|j6        tn          j?        k    s|j6        tn          j@        k    rts          |j:        d          s,t          d|j6         dtw          |j:                             ta          |j<        tz                    s,t          d|j6         dtw          |j<                              | ||j-        |j<                           |j:        j>        |j<                   |*                    |j<                   4|j-        /                                D ]c\  }}||vrZ ||          }|'vrt          d|d          '|         d         \  }}  ||||           |*                    |j<                   d|j         /                                D ]7\  }}||vr. ||          }!|!'vr'|!         d         \  }}  ||!||           8i }"'                                D ]}#d |#D             }$|#D ]
\  }%} |$|"|%<   t          | |"g            t          |                                          ((fd|D             }| C                    (|           d |D             }d | j        jD        D             | _E        d| _F        d t          |          D             }&| H                    d          D ]\  }} ||&vrt          |&          |&|<   t          | |&           | j        K                                 | L                                 d S )Nz%Unflattening on JointExportModule NYIc                 
   t          | t          j                  r[t          |                                 |                                 |                                 |                                           S t          |           S )z8Returns _TensorID dataclass for tensors, otherwise id().)r6   r7   r8   r9   )	r]   r;   rb   r5   r6   r7   r8   r9   id)r   s    r2   _idz'UnflattenedModule.__init__.<locals>._idO  sn    #u|,,  $'$7$7$9$9::<<#&#5#5#7#7	    c77Nr1   c                     g | ]	}|j         
S r0   )fqnrO   entrys     r2   r   z.UnflattenedModule.__init__.<locals>.<listcomp>Z  s    KKK%EIKKKr1   r    z+expected first fqn to be empty string, got unflattened_moduleF)requires_grad)rH   T)rH   rI   c                 F    |          }|                     ||f           d S rM   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r2   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_map  s,    "6*Ii566666r1   namez-expected s.arg to have 'name' attribute, got z!expected s.target to be str, got z1expected s.arg to have 'name' attribute for kind z, got z%expected s.target to be str for kind z	constant z id not found in consts_mapc                     g | ]
}|d          S r   r0   )rO   ts     r2   r   z.UnflattenedModule.__init__.<locals>.<listcomp>6  s    111qt111r1   c                     g | ]}|v|	S r0   r0   )rO   r   redirected_call_indicess     r2   r   z.UnflattenedModule.__init__.<locals>.<listcomp>=  s$    RRRCs:Q/Q/QC/Q/Q/Qr1   c                     g | ]}d |v|	S )@r0   )rO   r   s     r2   r   z.UnflattenedModule.__init__.<locals>.<listcomp>@  s    >>>Cs#~~C~~~r1   c                 (    g | ]}|j         d k    |S r   r   rO   r   s     r2   r   z.UnflattenedModule.__init__.<locals>.<listcomp>E  s+     #
 #
 #
M1I1ID1I1I1Ir1   c                     i | ]\  }}||	S r0   r0   )rO   ir   s      r2   
<dictcomp>z.UnflattenedModule.__init__.<locals>.<dictcomp>J  s    >>>3S!>>>r1   remove_duplicate)Mrx   ry   graph_signaturebackward_signature
ValueErrormodule_call_graphr_   r   ru   r;   r   r   rz   r   rt   metaadaptedrB   r{   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_root_module_typerl   _IValsivals_outline_submodulesrZ   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrU   
parametersrX   r^   cloner   ri   r$   r,   addnon_persistent_buffersbuffers	constantsr-   r\   r]   rb   r.   r   r   input_specskindr   rI   rV   argr`   rG   rr   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   r   )*rp   r   r   r   fqn_listexport_graphseen_modules
seen_attrsr  assigned_paramsid_to_paramr   paramr  assigned_buffersid_to_bufferrI   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targetsn	fqn_orderr   r   r|   s*                                          @@r2   ry   zUnflattenedModule.__init__F  s
   
 	(;GDEEE		 		 		 LK=+JKKKA;" MhqkMM     344'(EFFX^^%%
#'
 !)-*I!J!J!2!.3	*.	&' %9"+L$:NOOOl+++$\22XX
#6|T#J#J j 	
,--//000 	-5tZHHH!.!@*,! #-
$'EE  	 (3 	& 	&Dt$Es5zz,,*/(*<*<KKMM1D += + +CCJJ' CCJJ'#-	    %%%%!$T%9%P!Q!Q%(UUOQ(0 	' 	'D---"
&06!
#D)s6{{,..-3\\^^Z,HSS[[)SS[[)!,#*%      &&&& ',,.. !	 !	LD&&&$2B*B*BIs6{{l****3 3* !	 CKK|33 1LV-  V-a0'.#     3v;;k11/5KF,F,'1	     TV*4::<< 	 	MCs8}}K//h55 0'~~//H-5CCMM*#CCMM2I#,	     DOtCTCT
#&55	7 	7 	7 	7 	7 *-%1 &	- &	-Av,,,)***q|*quf-- (UQUUU   "!(C00 (LDNNLL   "!C0:;;EJH  
 ""18,,,$((2222)*** +6Y6666Y111quf-- (gAFggZ^_`_dZeZegg   "!(C00 (^^^dSTS[nn^^   "!C/9::EJH  
 ""18,,, "/!8!>!>!@!@ 		3 		3J//3u:::--(MJMMM   (1!4
!!(GZ@@@$((222 )399;; 		; 		;KC...CKK	J.. '	215
!!)Wc::: 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),,,
r1   c                     |                                  D ]_\  }}t          |dz              t          |d          r8t          |j        t
          j        j                  rt          |j                   `d S )N:ru   )r  printrV   r]   ru   r;   r   r   )rp   r   r   s      r2   _print_graphzUnflattenedModule._print_graphS  sw    **,, 	! 	!HC#)sG$$ !CIux~)N)N !ci   	! 	!r1   c                 T   | j         d         j        }||j        k    r|S | j        t	          d          | j                            |j        ||| j        |          }t          |          |j        j        k    r,t	          dt          |           d|j        j                   |S )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)rp   	flat_argsr6  inputr5  s        r2   _adapt_flat_argsz"UnflattenedModule._adapt_flat_argsY  s    *1-7	i'''!)O  
 .44%-"$ 5  I 9~~!2!===G #IG G(1(9(DG G  
 r1   c                   
 | j         d         j        }|}|rt          ||j                  }t	          j        ||f          \  }}d |D             }t                      r|S ||j        k    rP| j        s+t          d| dd|j                    t          d           | 	                    |||          }d| _        | j
        rddlm} | j        du r| j        r| j                                        ng 

rOt          
          t          |          k    r/t!          d	t          
           d
t          |                     
fdt#          |          D             }	n|}	 || j        |	| j                   |S )Nr   c                     g | ]
}|d          S r   r0   rO   xs     r2   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>~  s    777aQqT777r1   zGInput treespec does not match with exported module's: 
Input treespec: z. zExported module treespec: z5Adapting flat arg to match exported module's treespecT)"_check_input_constraints_for_graphzflat_arg_paths length z! does not match flat_args length c                 n    g | ]1\  }}t          |           t          r|         nd          f|f2S ))idxz<unknown location>r   )r   r   )rO   rB  r
  flat_arg_pathss      r2   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>  sp     + + + !S (C000&#1&:^C%8%8%9   
+ + +r1   )r   r5  r   r6  r   tree_flatten_with_pathr   r   r2  r;  r  torch._export.utilsr@  r   r   r   r_   r  r  r   )rp   r   r   r5  reordered_kwargsflat_args_with_pathr6  r9  r@  new_flat_args_with_pathrD  s             @r2   process_forward_inputsz(UnflattenedModule.process_forward_inputst  s   *1-7	! 	I-fi6GHH'-'D#$(
 (
$W 87#6777	!## 	i'''< O3'.3 3 3D1BDD  
 MNNN--i$GGIDL' "	 ONNNNN|t## -D*==??? 
 " c.&9&9S^^&K&K(w^1D1Dwwgjktguguww  + + + + %.i$8$8+ + +'' +>'..')@$BX   r1   c                 @    | 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                                        s| j        s! t          | j                  j        | g|R  }n0t	          j                            | | j                  j        |ddi}t%          j        ||j                  S )Nr   r   r   Fr   )rJ  r   r5  r   r;   r   r   ru   r   r]   r>   r   r   r   r{   r`   rt   r   r   tree_unflattenout_spec)rp   r   r   r9  r5  
return_valtree_outs          r2   r   zUnflattenedModule.forward  s2   /D/@@@	*1-7	!## 	--d$*-EEI16 J *e,, %ZA1E1E!!}$>--// 	t7Q 	6tD-..6tHiHHHHHx++D
+CCG16 H $Xy/ABBBr1   c                     t           j                            | | j                  | j        d<   | j                                         d S )Nrt   )r;   r   r   ru   r   r   ro   s    r2   r   zUnflattenedModule.finalize  s:    (-(<(<T4:(N(Nn%
r1   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!          t          |                    D ]Y}t#          |
|dz             }||vr@|                    d          ^ }}t%          | |          j                            |           Z|                     |
t-          ||
         |                     ҈fd}|                     d	          D ]\  }}t1          |d
          r |||j                   't1          |d          rJ|j        D ]B}t1          |d
          st7          dt9          |                      |||j                   Cd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   0rK   r   r   c                     g | ]\  }}|S r0   r0   )rO   r)  r   s      r2   r   z7UnflattenedModule._dispatch_modules.<locals>.<listcomp>  s    KKKFAsCKKKr1   c                     |j         D ]C}|j        dk    r6|j                            d          d         }| r|  d| n|}|v r||_        Dd S )Nr   r   r   rK   )r   r   rG   rT   )re   ru   r   r   pathcalled_moduless        r2   elide_call_indicesz?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  su     * *7m+++++C003C06?f,,s,,,CD~--&)* *r1   Fr   ru   r   z-expected mod_ to have 'graph' attribute, got N)r   r   r   r   r5  r   getrT   r   r?   rU   rsplitr  r\   sortedr   range
_call_namer   r[   popset_submoduler    r  rV   ru   r   r_   r`   )rp   r   r"  r   r   r   baserB  	attrs_maprG   orig_fqnr   indexed_call_modulesr   r   re   rW  mod_rV  s                     @r2   r  z#UnflattenedModule._dispatch_modules  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$$s<0011 Q QA$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 ,XDQUJJXX   '&sDJ7777		8 		8r1   TFc                 *    t          | d||||          S )Nr!   r   r   s        r2   r   z UnflattenedModule.print_readable
  r   r1   rM   r   )r)   r*   r+   r   r   ry   r3  r;  rJ  r   r   r  r   r   r   s   @r2   r!   r!   E  s         59K K&K +T1K K K K K KZ! ! !  6> > >@C C C*  ;8 ;8 ;8~ 
 
 
 
 
 
 
 
r1   r!   r(   r   rm   c                     t          |           } t          | |          }t          j                            |j        dd          |_        |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
    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disablerJ  )r(   r   ms      r2   r"   r"     sU    , #6**F&"344A  %}44	 ;  5    A Hr1   ru   r   c                    t          t          t          | j                                      }|j        dk    st          |j                  dk    r-t          d|j        dt          |j                   d          |j        d         }d | j        D             }i }d |j        	                                D             fd	|j
        	                                D             }|                    |j                   t          |          }|d
|         D ]}||j                 }||         }	|                     |          5  |                     dt           j        j        j        j        |	|f          |j        	                                D ]\  }
}|j        |
<   	 d
d
d
           n# 1 swxY w Y   |                    fd           t/          ||d
                   }|f|_        d
S )a^  Transform buffer and input mutations from their functionalized form
    into copy_ nodes in the graph.

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

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

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

    Input mutations are handled similarly.
    outputr   z8expected output node with op='output' and 1 arg, got op=z with z argsr   c                 4    i | ]}|j         d k    |j        |S r   )r   r   r   s     r2   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>c  s/        47m3K3K	43K3K3Kr1   c                     i | ]\  }}||	S r0   r0   )rO   rP   
buffer_fqns      r2   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>i  s+          '!Z
A     r1   c                 (    i | ]\  }}||         S r0   r0   )rO   rP   ro  buffer_fqn_to_input_names      r2   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>l  s5     # # #Az 	
#J/# # #r1   Ncall_functionc                     | uS rM   r0   )r?  new_nodes    r2   <lambda>z5_inplace_buffer_and_input_mutations.<locals>.<lambda>  s    1H;L r1   )nextiterreversedr   r   r   r   r_   inputs_to_buffersr\   buffers_to_mutaterZ   user_inputs_to_mutater   inserting_aftercreate_noder;   opsatencopy_defaultr   replace_all_uses_withr>   )ru   r   output_nodereturn_argsinput_name_to_nodemutation_name_to_input_namenum_mutationsmutation
input_name
input_noderP   vuser_outputsrq  rt  s                @@r2   r   r   ?  s   : tHU[112233K~!!S)9%:%:a%?%?{{~{{_bcncs_t_t{{{
 
 	
 "1%K $)K   #%   +:+L+R+R+T+T     # # # #,>DDFF# # #
  &&'LMMM344M/ N N0?
'
3
""8,, 	% 	%((!5!=
H?U H !++-- % %1#$a  %	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	&&x1L1L1L1LMMMM ]^^455L%(Ks   AF11F5	8F5	c                     | j         D ]9}d|j        vr|j        d                                         D ]\  }}|s|c c S :d S )Nnn_module_stack)r   r   r   )ru   r   rU  rv   s       r2   r   r     sp      DI--	"34;;== 	 	HD" 						 4r1   c           	         | j         D ]}d|j        vr|j        d         }d |                                D             }|^}}d}|D ]}t          ||          r|dz  }|} |t	          |          k     rkt
                              d||t	          |          |z
             t          t          |	                                          d |                   |j        d<   d S )Nr  c                 V    g | ]&\  }}d |v r|                     d           d         n|'S r   r   rT   )rO   r   _ts      r2   r   z)_fix_nn_module_stacks.<locals>.<listcomp>  sH     
 
 
R "%CIIcNN1
 
 
r1   r   zPnn_module_stack fqns %s at node %s do not form a stack! dropping last %d entries)
r   r   r   
_is_prefixr   logwarningr   r   r\   )ru   r   r  fqnsprev_fqn	next_fqnsnum_valid_indicescurr_fqns           r2   r   r     s2        DI--)$56
 
*1133
 
 
  $9! 	 	H(H-- !Q&!#  s?3333KKbO$$'88	   ,0_**,,--.@/@.@A, ,DI'(=   r1   c                 x    t          |           t          |          k     o|dt          |                    | k    S )z2Check whether `candidate` is a prefix of `target`.N)r   )	candidaterG   s     r2   r  r    s4    y>>CKK'QF3CS^^3C,D	,QQr1   
parent_fqn	child_fqnc                    | dk    r|S |                      d          }|                     d          }|d t          |                   |k    rt          d| d|  d          d                    |t          |          d                    S )Nr   rK   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.)rT   r   RuntimeErrorr   )r  r  parent_splitchild_splits       r2   _compute_accessorr    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5r1   r?  yc                    dt           j        j        dt          fd}t	          | j        t           j        j                  s$t          dt          | j                             t	          |j        t           j        j                  s$t          dt          |j                              || j                   ||j                  k    S )Nru   rm   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 )Nrm   c                     t          | t          j        j                  r%dt	          t          |                              z   S t	          |           S )N%)r]   r;   r   Noderr   r   )r
  	nodes_idxs    r2   arg_dumpz>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  sB    #ux}-- 5S2c77!34444s88Or1   c                 ,    g | ]}t          |          S r0   )rr   )rO   r
  s     r2   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s    RRRcSRRRr1   c                 "    g | ]\  }}| d | S )=r0   )rO   keyvalues      r2   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s:       C       r1   )rr  get_attrr   z: [z](z, )r   )rr   r  r   r   tree_mapr   r   r\   r   rG   r   r   r   )ru   retr  r   r   	args_dumprG   r  s          @r2   
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~~r1   z+expected x.graph to be torch.fx.Graph, got z+expected y.graph to be torch.fx.Graph, got )r;   r   r   rr   r]   ru   r_   r`   )r?  r  r  s      r2   _check_graph_equivalencer    s    %(. S    * agux~.. 
I$qw--II
 
 	
 agux~.. 
I$qw--II
 
 	
 :ag**QW"5"555r1   gmc                     d}t          | d|           r|dz  }t          | d|           d| }t          | ||           |S )Nr   _spec_r   )rV   rW   )r  specr   r   s       r2   	_add_specr    sd    	A
"lqll
#
# 	Q "lqll
#
# A<<DBdKr1   c                     | j                             t          j        |f          }| j                             t          j        |df          }|S )Nr   )ru   rr  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r2   _generate_flattenr    sB    h$$V%84'BBG&&x'7'1FFIr1   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rM   )r  ru   r  rr  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r2   _generate_flatten_specr    sH     RD!!$''I8!!)"=i?PQQQr1   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rM   )r  ru   r  rr  r   rL  )r  r   r  r   r  s        r2   _generate_unflattenr    sH     RD!!$''I8!!&"7%9KLLLr1   r   c                     |                     d          ^ }}|D ]<}t          | |d           }| d S t          |t          j        j                  s d S |} =t          | |d           S )NrK   )rT   r   r]   r;   rX   rY   )r   rG   re   rf   rR   submods         r2   _get_submoduler    sy    \\#&&NVU 	 	dD))>44&%(/22 	443t$$$r1   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 )NrK   r   F)
rT   r  r   r   r;   rX   rY   rW   r]   
add_module)	r   rG   r  r  re   rf   r   rR   r  s	            r2   _add_submoduler  $  s     \\#&&NVUV$$  4dD))>(&sxxwQw'@'@AA**Cv&&&&%(/22 	55NN5-(((((r1   r_  r.  c                 &    |dk    r| n	|  d|dz
   S )Nr   r   r0   )r_  r.  s     r2   r\  r\  >  s(     6644$00Q000r1   	call_namec                 Z    t          j        t          j        |          dz   |           d uS )Nz(@\d+)?$)rematchescape)r  r_  s     r2   rN   rN   D  s&    8BIdOOk19==TIIr1   c                       e Zd Z	 ddej        j        deej        j        df         deee	e	dz  e
f                  dee	ef         dej        j        ez  dz  f
dZd	 Zd
 Zd Zd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  _        n\ 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                  }|H j        B|j         j!        d	k    rtE          d
|j         j!                   |j         j#        tH          urtE          d|j         j#                   |j         %                                \  }}|j#        tH          urtE          d|j#                   |j#        tL          urtE          d|j#                    j        '                    d           5   fdtQ          |j!                  D             }i }|j)        D ]} j        *                    |          ||<    tW           j        tI          |          |f|j                   }tY          |j-                  D ]\  }} j        .                    dt^          j0        ||ftc          |td                    s|j3        nd|           }tc          |td                    rd|j3         j        v rHti          j4         j        |j3                 j5                  |_5        | j         j        |j3                 <   	 d d d            n# 1 swxY w Y    j        j        6                     j                  5  g }|j-        D ]}tc          |td                    r|                    |j7                   2|j3         j        vr|                    d            Vtc          |tp          tr          tt          tv          f          stE          dtG          |                     |                     j        <                     j        |j3                                      t{           j        j        ||j                   } j        j        >                    t^          j0        |df           j        j        >                    t^          j0        |df           fdtQ          |j!                  D             } fd|j)        D             }d d d            n# 1 swxY w Y    j        tE          d          tI          |           j        _?        | j        _@        d S d S d S )NFr   r   rv   c                     j         rj          d|  n| }|j        v rj        |         S t          t          j                                                  }|j        |<   |S )NrK   r  )r   created_modulesr   r;   r   r   )r   rU  r  parentrp   rv   s      r2   r  z,_ModuleFrame.__init__.<locals>.create_module~  sp    06
C&*,,s,,,4////55*58>>+;+;CCC-3$T*r1   r   )r  parent_moduleparent_call_moduler   call_idxr(      z)expected in_spec to have 2 children, got z'expected in_spec.type to be tuple, got z)expected args_spec.type to be tuple, got z*expected kwargs_spec.type to be dict, got c                 J    g | ]}j                             d |            S )_positional_arg_)ru   r   )rO   rB  rp   s     r2   r   z)_ModuleFrame.__init__.<locals>.<listcomp>  sB        J**+Cc+C+CDD  r1   rr  
_constant_)r   rG   r   r   z_expected input to be TensorArgument, SymIntArgument, SymBoolArgument, or SymFloatArgument, got c                 h    g | ].}j         j                            t          j        |f          /S r0   r  ru   rr  r  r  )rO   r   	args_noderp   s     r2   r   z)_ModuleFrame.__init__.<locals>.<listcomp>  sF        K%33H4DyRSnUU  r1   c                 j    i | ]/}|j         j                            t          j        |f          0S r0   r  )rO   rP   kwargs_noderp   s     r2   r   z)_ModuleFrame.__init__.<locals>.<dictcomp>  sO         t{(66 (;*:   r1   z#parent_call_module must not be None)Ar  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   r\  r  r(   rV   r   rX  r   r;   r   r   ru   node_mapnode_to_placeholderr  r  r  r   r[   r   _SubmoduleEntryr6  num_childrenr_   r`   r>   childrenr   r|  r[  contextr   r  r  inputsr}  r  r  r]   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  rr  r   r   ) rp   r  r   r  r  r  r  r  r  r  r   r(   	num_callsr   r  base_module_framer5  	args_speckwargs_spec	arg_nodeskwarg_nodesr   r9  rB  r
  flat_arg_nodeinput_nodesr:  inputs_noder  r  rv   s    `      `                     @@@r2   ry   z_ModuleFrame.__init__J  s    %
$($.("!2"&"3B"7"i#DHi!m<<Q DK)0)A)AIrDJJ.22!%(.."2"2r::: DK  DJ[&
 =?#% 8<(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[%< -22$`	@Q@^``    %U22$Vi>O>TVV   &/%6%?%?%A%A"I{~U**$P	PP   t++$SAQSS   ++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1JK!              B "33D4KLL . .:<&-  E!%)9:: #**5;77774?::#**40000)! . . / 0	     #1![MQRW[[![ ![# #  $** K33DOEJ4OPP    2K&%  !K-;;${A&6 	 #k/==${A&6     "9#9::  	     )0	  S. . . . . . . . . . . . . . .^ &.$%JKKK+0+;+;D#(-8D#***O ! %<%<s&   .EQ>>RR-FYYYc                 |   | j         dk    rt          d| d          |j        | j        urt          d          | 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 module6expected x.graph to be flat_graph, got different graph)	type_expr)r   r_   ru   r  r  r   r   r`   r  r   r  )rp   r?  placeholder_nodes      r2   add_placeholderz_ModuleFrame.add_placeholder   s   8r>> !M1!M!M!MNNN7$/)) H   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   'B		BBc                 T   t          j        t          j        j        | j        |j                  }t          j        t          j        j        | j        |j                  }| j        	                    |j
        ||          }t          j        |j                  |_        || j        |<   |S rM   )r   tree_map_onlyr;   r   r  r  r   r   ru   rr  rG   r  r   r  )rp   r?  r   r   r   s        r2   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_function  s}     #EHM43CQVLL%ehmT5EqxPPz''$??Iaf%%	ar1   c                    |j         | j        urt          d          || 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%t;          |j        d          r2|j        j        dk    r"|                     |           | j        |         S | j                            | j	                  | j         !                    | |          S tE          d	|j         d
|           )Nr  zremap_input(r  r   r   rr  r*   	_operatorz(Could not run remap_input() on op type: z
 for node )#ru   r  r_   r  r2  r  r   r   rX  r   r  r  r  r  
insert_argr  rG   r;   r~  r  sym_sizer?   rR   r  unbindsumdim_IntListviewdiffrV   r*   r  r   readr  )rp   r?  s     r2   r  z_ModuleFrame.remap_input  s   7$/)) H   =##

&!&&&'''(((+A..DM!!T%;%?%?%I%I%Q   ###&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= :??4+++N14NN1NN  s   4DDDrm   c                 0   d }| j                                         D ],}|j                            di           }||}"||k    ri } n-|rI| j        t          d          || j        j        d<   | j                                         D ]}|j        d= d S d S )NcustomzBparent_call_module must not be None when uplifting custom metadata)r  r   r   rX  r  r_   )rp   custom_metar   	curr_metas       r2   uplift_common_custom_metadataz*_ModuleFrame.uplift_common_custom_metadataK  s    M((** 		 		D	h33I"'K''  (  	(&.$X   6AD#(2,,.. ( (Ih''	( 	(( (r1   c           	      F     j                              j        d            g } j                             j                  }| j        |j        D ]}t          |t          t          t          t          t          f          rJ|j         j        v r&|                     j        |j                            d|                    d            zt#          d|            fdt%           j        t)          fd|D                       |j                  }t-           j        j         j        |j                  }|}n}g } 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t?          dtA          |                      j!        "                    |           |d S t          |t8          j        j                  r|j#                            d          nd |D             |j#        d<    $                                 t5          |          dk    r|| j        j        |d         <   d S tK          |          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  r  ru   )rl  	seen_noderp   s     r2   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node~  sx    >4 OFK8	--=33$":::3I>>&SfSStzSS  r1   c              3   .   K   | ]} |          V  d S rM   r0   )rO   rl  r!  s     r2   rS   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s/      PP,,V44PPPPPPr1   r   r   z8expected graph_outputs to be list or torch.fx.Node, got valc                 B    g | ]}|j                             d           S )r#  )r   rX  )rO   os     r2   r   z1_ModuleFrame.finalize_outputs.<locals>.<listcomp>  s&    ;;;!&**U##;;;r1   )(r  r]  r   r   rX  r  r  r   r]   r   r   r   r   r   r   r  r   r  r  r(   r>   rM  r  r  r  usersr   r   r;   r   r  r_   r`   ru   rl  r   r  r  Proxyr   )rp   orig_outputsr5  rl  tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outr!  s   `           @r2   finalize_outputsz_ModuleFrame.finalize_outputsc  sr     4000*..t~>>	 T[%<#+  &&'((	 	  {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
 0F"D$;Y=O0 0J BOMMM!]  	!*  I ~T_<<$++I666%,,T]9-EFFF	 = 0J=!!Q&& -a 0-$)>?? 	 `4P]K^K^``   	
-((( 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> >r1   c                     |                      d|                                           | j                            || j                  | j        |<   || j        |j        <   d S )Ncopying)r2  format_noderu   	node_copyr  r  r  r   r   s     r2   	copy_nodez_ModuleFrame.copy_node  sW    

9d..00111"j2249IJJd%)	"""r1   c                    t          | j        j                  D ]F\  }}|                     ||j                            d          |                                           Gd}| j        |         }|j        dk    r2|                     |           |dz  }| j        |         }|j        dk    2| 	                    |           | j        j        D ]"}|j        dk    r|                     |           #d S )Nr  r   r   r   rl  )
r  r  r   r2  r   rX  r3  r   r5  run_from)rp   r   r   node_idxs       r2   	run_outerz_ModuleFrame.run_outer  s    !677 	P 	PGAtJJq$)--(9::D<L<L<N<NOOOO z(#g&&NN4   MH:h'D g&&
 	h O) 	% 	%Dw(""t$$$	% 	%r1   c                 0    | j         rt          |i | d S d S rM   )r  r2  )rp   r   r   s      r2   r2  z_ModuleFrame.print  s/    < 	#4"6"""""	# 	#r1   c                    d}|t          | j                  k     r1| j        |         }|j        dk    rt          d|           |                                  |                     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 |t          d          t!          | j        |          r||         }|                     d|           t#          |j        d                                                   |         }	t'          | j        | j        | j        | j        | j        | j        | | j        |gz   |	                    d          d         | j        
  
                            |          }|dz  }|| j        k    rt          d| d| j                   |j        dk    r*| j        | j                                     |j                   |                     |           |dz  }|t          | j                  k     /d S d S )Nr   r   z%unexpected placeholder node at index STEPrl  r   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYc           	          g | ]=\  }\  }}||r|nd d|v r(t          |                    d          d                   ndf>S )Nr   r  r   )r?   rT   )rO   rP   rU  rv   s       r2   r   z)_ModuleFrame.run_from.<locals>.<listcomp>  sj     % % % &:D"	 ",14AGGCLL,---q% % %r1   	outliningz"node_module_stack must not be NonezCreating new stack frame forr   zexpected node_module_stack z to equal module_stack r  ) r   r   r   r_   r2  r3  r  r0  r   rX  r  (torch._export.passes._node_metadata_hookr=  r\   r   ru   r  r   keysr  r  r  r  r  r  rT   r   r7  r  r  rG   r5  )
rp   r8  
module_idxr   depthr  r=  node_module_stacknext_modulenext_module_keys
             r2   r7  z_ModuleFrame.run_from  s   
TZ((:h'Dw-''$%WX%W%WXXX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:&&& ($%IJJJ$+->??  06

9;GGG #'ty1B'C'H'H'J'J"K"KE"R'OJO%O(%5#))#..q1*  (8$$  a
 !D$555$o2Coo\`\moo   w*$$/33DK@@@NN4   MH} TZ((((((r1   rM   )rm   N)r)   r*   r+   r;   r   r   r>   r  r   rr   r?   r   r   r   r!   ry   r  r  r  r  r0  r5  r9  r2  r7  r0   r1   r2   r  r  I  sG        CGt9 t9HNt9 UX]C'(t9 5cDj#!567t9  %8 89t9 $'884?t9 t9 t9 t9l7 7 7  , , ,\( ( ( (0\> \> \>|* * *
% % %&# # #
a a a a ar1   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  r  r  r  r   r  r(   N)r)   r*   r+   rr   r=   r;   rX   rY   r   r  r?   r0   r1   r2   r  r  B  s]         OOO8?"""%%%	HHHMMMHOr1   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   Nr   r   c                 6    i | ]}|j         	|j        |j         S r0   )r5  r   r   s     r2   r   z'_outline_submodules.<locals>.<dictcomp>[  s6     	
 	
 	
	
Iu	
 	
 	
r1   )r(   )r   r   rU   r  r>   r   r   r9  )rH  rI  r  r  r  r  s         r2   r   r   L  s    +-J5@5F5FL&1#&6&6J24Oj	
	
 	
$6	
 	
 	

     ikkk##r1   r   r  r/  re   c                    |dk    rit          |                                          dd          D ]?}t          | |          -t          | |t          j                                                   @g }t          | j                                                  D ]i\  }}|||z   }t          |||
                    d          d         dz              t          | |           |                    ||         ||f           j|                    t          j        d                     |D ]\  }}}|                     ||           d S )Nr   r   r   r   rK   )re   )r  )r   rA  r  r  r;   rX   rY   r[   r\   r  rT   delattrr   sortr  
itemgetterregister_module)r  r/  re   r   r  r   childr)  s           r2   r  r  e  sU    ||	(())!""- 	? 	?Cfc**2vsEHOO,=,=>>>HFO113344 7 7e=tmE9SYYs^^A5F5LMMMM3u56666MMh)!,,M---" , ,4tU++++, ,r1   c                   *    e Zd ZdZd Zd Zd Zd ZdS )r   aY  
    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                 8    t          t                    | _        d S rM   )r   rU   node_names_by_fqnro   s    r2   ry   z_IVals.__init__  s    !,S!1!1r1   c                 \    t          |t          j        j                  r|j        j        S dS rA   )r]   r;   _ops
OpOverload_schema
is_mutable)rp   rG   s     r2   _is_mutablez_IVals._is_mutable  s)    fej344 	->,,ur1   c                    |j         dk    rt          d|j                   |                     |j                  }t	          d|j        |           |sat          t          |j        d                                                             \  }}| j	        |         
                    |j                   |                    |j        d                   S )zI
        Read state corresponding to a given intermediate value.
        rr  z,expected node.op to be 'call_function', got zChecking mutabilityr  r   )r   r_   rZ  rG   r2  rv  rx  r   r   rT  r  r   r  r   )rp   mfr   br   r)  s         r2   r  z_IVals.read  s    
 7o%% JtwJJ   T[))#T[!444 	7 (49->#?#F#F#H#HIIJJFC"3'++DI666~~dil+++r1   c           
         |D ]}|D ]}|j         j        }| j        |j                 fd|j        D             }|D ]}|                    |          5  |                    dt          j        j	        j
        j        |j        d         |f          }t          j        |j                  |_        ddd           n# 1 swxY w Y   dS )zT
        Update states corresponding to intermediate values that were read.
        c                 &    g | ]}|j         v |S r0   rC  )rO   r.  
node_namess     r2   r   z!_IVals.update.<locals>.<listcomp>  s%    HHHq16Z3G3G3G3G3Gr1   rr  r   N)r(   ru   rT  r   r   r|  r}  r;   r~  r  r  r  r   r  r   )	rp   
partitionsshared_submodulesr   ru   r   r   rt  r`  s	           @r2   rZ   z_IVals.update  s/    ", 	= 	=* = =*!3EI>
HHHHEKHHH! 	= 	=D ..t44 = =#(#4#4+!IN08!Yq\40$ $
 )-	$)(<(<= = = = = = = = = = = = = = =	=	=	= 	=s   AB<	<C C N)r)   r*   r+   r:   ry   rZ  r  rZ   r0   r1   r2   r   r   {  sZ         (2 2 2  
, , ,$= = = = =r1   r   r  c                     |                                 D ]?\  }}|rt          ||          n|}|D ]#}t          | |          }t          |||           $@d S rM   )r\   r   r   rW   )r  rI  r  r  namesr(   r   r#  s           r2   r   r     s    
 ',,.. ' '	56?P;	222[ 	' 	'D"d##CFD#&&&&	'' 'r1   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 )NFrK   T)r  r\  r   r  r  r  r  r(   r  rT   r   r[   r]  r  rG   r^  )ra  r   rb  r   r   r  rG   deduplicatedseenr  re   r   seen_child_fqnseen_targets                 r2   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 #"r1   r+  scopemodule_id_to_inputs_removedc                    t          t                    t          |           v r$t          |           t          |                    iS | j                                        D ]^\  }t          t          d          |||gz             }|                                D ] \  }}|                             |           !_t          | dd          }|t          |j
                  dk    rS t          |t          j        j                  st          dt!          |                     t#          t%          d |j
                            }	t          |	          dk    rdn|	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t          dt!          |                     |                    |
          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.
    Nztorch.nn.Moduleru   r   z)expected graph to be torch.fx.Graph, got c                     | j         dk    S )Nr   r   r.  s    r2   ru  z_sink_params.<locals>.<lambda>'  s    14=#8 r1   r  c                     | j         dk    S )Nr   r   rn  s    r2   ru  z_sink_params.<locals>.<lambda>+  s    )> r1   c                 <    | j         t                             vS rM   )r   r   )r.  rk  	submodules    r2   ru  z_sink_params.<locals>.<lambda>3  s    af,G9,VV r1   rK   c                 D    g | ]}|                     d           d         S r  r  r>  s     r2   r   z _sink_params.<locals>.<listcomp>A  s&    )I)I)Ia!''#,,q/)I)I)Ir1   zBexpected state_attr to be torch.Tensor or torch.ScriptObject, got r  T)propagate_meta)%r   rU   r   r[   r\   r  r
   rZ   r   r   r   r]   r;   r   r   r_   r`   r   filterr   rG   r>   r   r   rT   r&  r   rb   rd   r|  r}  r   r  
erase_noder  r   r   )r(   r+  rj  rk  r   submod_id_to_inputs_removedrP   r  ru   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrrt  rq  s      `                 @r2   r  r    sa   , #*&1#&6&6#	&zz0006

76

CDD "?0022 5 5i&2"I..TFN'	'
 '
# 05577 	5 	5DAq'*11!4444	5 FGT**E}EK((A--**eUX^,, XVeVVWWW&88%+FFGGF [[A--TT6":N >>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 $kY]^hYiYikk  
 &&~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   	*L??M	M	)TrM   )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$   r5   rB   rD   rb   rd   rX   rY   rr   boolri   rk   r   r    ABCr   r!   r"   r   r   r   r   r  r  r  r  r   r  r  r  r  r  r  r?   r\  rN   r  r  r   r   r  r   rU   r   r  r   r  r0   r1   r2   <module>r     s   



    				 # # # # # # $ $ $ $ $ $ % % % % % %       ! ! ! ! ! !                $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ? ? ? ? ? ? ( ( ( ( ( ( 3 3 3 3 3 3	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 < ; ; ; ; ; U U U U U U U U U U 7 7 7 7 7 7 7 7 = = = = = = g!!          $          ( ( (" =0 =0lU//%(/A=0x=0 =0 	=0
 =0 =0 =0 =0@	 	 	 	 	 	 	 	\
 \
 \
 \
 \
 \
 \
 \
~." ." ." ." .".%(/ ." ." ."b    cg   ,S
 S
 S
 S
 S
 S
 S
 S
n JN! !!0?$0F!! ! ! !HH)8>H))H) 
H) H) H) H)VUX^ d
    $ $ $NR R R
6# 6# 6# 6 6 6 6$6 6EHO 6 6 6 6B%(/ C    %(.     R003DDR
X]R R R RM003DDM
X]M M M M% % % % % %* >B	) )	)) 8?) SE58?23d:	) ) ) )41S 1S 1S 1 1 1 1JS J J J J J J
v v v v v v v vr        $EHN $AR $ $ $ $4 GI, ,HO,(,S#X,@C, , , ,,B= B= B= B= B= B= B= B=J	'	'"	' S#c(]#	' 	' 	' 	'(# (# (#^ ?C	t( t(HOt(#tCy.)t( 9t( "&c3s8m!4t!;	t( t( t( t( t( t(r1   