
    :i                       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     I/var/www/icac/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    r8t          | t          j        j                  sJ |                    ||            K|t          j        k    r5t          | t          j                  sJ |                    || |           |t          j        k    rYt          | t&                    r
J d            t          | t          j        t          j        f          sJ t          |||            |t          j        k    r2t          | t          j        j                  sJ t          |||            =d S )N.c              3   B   K   | ]\  }}t          |          |V  d S N)_is_call_name).0kt_callitems      r2   	<genexpr>z_assign_attr.<locals>.<genexpr>g   sK        Av D))     r1   )rI   z2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrr;   nnModuleupdate_modulesitemsr$   r,   
isinstance	Parameterregister_parameterr-   Tensorregister_bufferr.   r   ScriptObjectr/   )
rE   rF   rG   rH   rI   prefixfield
to_modulestsrR   s
            @r2   _assign_attrrg   S   s-    \\#&&NVU J 
 
#&55# 	 	I9d++ <	4):):;;;II    !*!3!9!9!;!;      
 

 0 0		+++h(:;;;;;((9999)***h55555%%eX*%MMMM),,,!(,<==  D   L&     Iuh////)***h88888Iuh///+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.
        )rj   selfs    r2   	type_namez_SubmoduleBase.type_name   s     xr1   )r)   r*   r+   strr=   ro   r0   r1   r2   ri   ri      sB         	tOOO3:      r1   ri   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__rs   rj   owning_modulerB   _run_with_interpreter)rn   rs   rt   	__class__s      r2   rw   zInterpreterModule.__init__   sA    
 	
#'
 %9"""r1   c                 ~   | j         
J d            t                      sHt          j                                        s| j        s# t          | j                   j        | g|R i S rt          |          }| j	        t          |          d          }|                    fd|D                        t          |          t                    k    sJ t          |          t          | j	                  k    sJ t          |          }t          j                            | | j                  j        |ddiS )Nz&Didn't finalize this InterpreterModulec              3   0   K   | ]}|v |         V  d S rM   r0   )rO   
kwarg_namekwargss     r2   rS   z,InterpreterModule.forward.<locals>.<genexpr>   s?          "!V++ :&++++   r1   rs   enable_io_processingF)rr   r   r;   compileris_dynamo_compilingry   typeforwardlist	arg_nameslenextendr>   fxInterpreterrs   run)rn   argsr~   arg_listkwarg_namess     `  r2   r   zInterpreterModule.forward   se    ,,.V,,,%'' 	N..00	8<8R	 34)**24I$III&III '
  ::"nS]]__=        &1         ;''3v;;66668}}DN(;(;;;;;X8''DJ'??C,1  r1   c                    t           j                            | | j                  | j        d<   | j                                         g | _        | j        j        D ],}|j        dk    r| j        	                    |j
                   -d S )Nrr   placeholder)r;   r   GraphModulers   __dict__lintr   nodesopappendrG   rn   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   rn   print_outputinclude_strideinclude_devicecoloreds        r2   print_readablez InterpreterModule.print_readable   (     
 
 	
r1   rM   TFFF)r)   r*   r+   r:   r;   r   r   r=   Graphrp   rw   r   r   r   __classcell__rz   s   @r2   r   r      s          
 (&----
 	: 	:x~	: $J	: 	: 	: 	: 	: 	:! ! !F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J |d         j        | _        |D ]'}t          | |t	          |d         |                     (|d         j        | _        || _        d| _        d S Nr   )rv   rw   r[   rW   getattrrj   _call_modules
_num_calls)rn   r   r   accessorrz   s       r2   rw   z$InterpreterModuleDispatcher.__init__   s    $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)rn   r   r~   call_modules       r2   r   z#InterpreterModuleDispatcher.forward   sn    (9?Q.#d6H2I2II	;//// 	 	 	DO	s   A   Ac                     | j         S rM   )r   rm   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)rn   r   r   r   r   outputss    ```` r2   r   z*InterpreterModuleDispatcher.print_readable  sV    
 
 
 
 
 
 
 )
 
 
 yy!!!r1   r   )r)   r*   r+   r:   rU   rp   r   r   rw   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objrk   c                     dS )z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr0   )rn   r   r   r   r   r   s         r2   adaptzFlatArgsAdapter.adapt  s	     	r1   c                     g S )z>Returns a list of paths that are used to access the flat args.r0   rm   s    r2   get_flat_arg_pathsz"FlatArgsAdapter.get_flat_arg_paths+  s    	r1   )NN)r)   r*   r+   r:   abcabstractmethodpytreeTreeSpecr   r	   dictrp   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                 R  '( t                                                       |j        j        t	          d          d }d |j        D             }|d         dk    sJ t          |j                  }t          |j                  | _        t          j	        
                                | _        | | j        _        t          |j                  | _        || _        |j        j        | _        | | j        d<   d| _        t           | _        t%          || j                   t'          |           t)          |          | _        t-                      | _        t1          ||           \  }}| j                            |                                           t7          |j        | |           |j        | _        g | _        |j        }tA                      }	i }
| j        j!        D ]}||         } ||          |
vrDt          j"        #                    |$                                |j%                  |
 ||          <   tM          |
 ||                   | |tN          j(        	           |	)                    |           tA          | j        j*                  }tA                      }i }| j        j+        D ]}||v rd}|j,        |         }n
d
}||         } ||          |vr"|$                                |f| ||          <   tM          | ||                   d         | |tN          j-        |           |)                    |           |.                                D ]\  }}||	v s||v rd} ||          |v st_          |t          j"        j#                  sd
}|rQ ||          |vr|d
f| ||          <   tM          | ||                   d         | |tN          j-        d
            ||          |
vr||
 ||          <   tM          |
 ||                   | |tN          j(        	           i }|j,        .                                D ]|\  }} ||          |vr<t_          |t          j0                  r|$                                }|| ||          <   | ||                   }tM          || |tN          j1        	           }te          tf                    'tA                      }'fd}tA                      }| j        j4        D ]}|j5        tl          j(        k    s|j5        tl          j-        k    r|j7        rtq          |j9        d          sJ t_          |j:        tv                    sJ  | ||j        |j:                           |j9        j<        |j:                   |)                    |j:                   |)                    |j:                   |j5        tl          j-        k    r|j7        r*|j5        tl          j=        k    s|j5        tl          j>        k    rtq          |j9        d          sJ t_          |j:        tv                    sJ  | ||j,        |j:                           |j9        j<        |j:                   |)                    |j:                   |j,        .                                D ]R\  }}||vrI ||          }|'v sJ '|         d         \  }}  ||||           |)                    |j:                   S|j        .                                D ]7\  }}||vr. ||          }!|!'vr'|!         d         \  }}  ||!||           8i }"'                                D ]}#d |#D             }$|#D ]
\  }%} |$|"|%<   t          | |"g            t          |                                          ((fd|D             }| A                    (|           d |D             }d | j        jB        D             | _C        d
| _D        d t          |          D             }&| F                    d          D ]\  }} ||&vrt          |&          |&|<   t          | |&           | j        I                                 | J                                 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;   r`   r5   r6   r7   r8   r9   id)r   s    r2   _idz'UnflattenedModule.__init__.<locals>._id:  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>E  s    KKK%EIKKKr1   r    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   namec                     g | ]
}|d          S r   r0   )rO   ts     r2   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  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>  s+     #
 #
 #
M1I1ID1I1I1Ir1   c                     i | ]\  }}||	S r0   r0   )rO   ir   s      r2   
<dictcomp>z.UnflattenedModule.__init__.<locals>.<dictcomp>#  s    >>>3S!>>>r1   remove_duplicate)Krv   rw   graph_signaturebackward_signature
ValueErrormodule_call_graphr   rs   r;   r   r   rx   r   rr   metaadaptedrB   ry   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_root_module_typerj   _IValsivals_outline_submodulesrZ   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrU   
parametersrX   r^   cloner   rg   r$   r,   addnon_persistent_buffersbuffers	constantsr-   r\   r]   r`   r.   r   r   input_specskindr   rI   rV   argrG   rp   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   r   )*rn   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   rz   s*                                          @@r2   rw   zUnflattenedModule.__init__1  s	   
 	(;GDEEE		 		 		 LK=+JKKK{b     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-----!!(C00000!!C0:;;EJH  
 ""18,,,$((2222)*** +6Y6666Y111quf-----!!(C00000!!C/9::EJH  
 ""18,,, "/!8!>!>!@!@ 	3 	3J//3u:::----'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:rs   )r  printrV   r]   rs   r;   r   r   )rn   r   r   s      r2   _print_graphzUnflattenedModule._print_graph,  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)rn   	flat_argsr6  inputr5  s        r2   _adapt_flat_argsz"UnflattenedModule._adapt_flat_args2  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 rb| j        r| j                                        ng 

r"t          
          t          |          k    sJ 
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>W  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_graphc                 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>s  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   )rn   r   r~   r5  reordered_kwargsflat_args_with_pathr6  r9  r@  new_flat_args_with_pathrD  s             @r2   process_forward_inputsz(UnflattenedModule.process_forward_inputsM  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*==??? 
 *RS-@-@C	NN-R-R-R-R+ + + + %.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   rs   r   r]   r>   r   r   r   ry   r   rr   r   r   tree_unflattenout_spec)rn   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 )Nrr   )r;   r   r   rs   r   r   rm   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 ]d\  }}t1          |d
          r |||j                   't1          |d          r-|j        D ]%}t1          |d
          sJ  |||j                   &edS )zFor a module whose call signatures are preserved, replace
        multiple modules corresponding to multiple calls to that module
        with a single dispatcher module that tracks which module to call.
        r   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   )rc   rs   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   rs   r   N)r   r   r   r   r5  r   getrT   r   r?   rU   rsplitr  r\   sortedr   range
_call_namer   r[   popset_submoduler    r  rV   rs   r   )rn   r   r"  r   r   r   baserB  	attrs_maprG   orig_fqnr   indexed_call_modulesr   r   rc   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111&&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   rw   r3  r;  rJ  r   r   r  r   r   r   s   @r2   r!   r!   0  s         59y y&y +T1y y y y y yv! ! !  6; ; ;zC C C*  88 88 88x 
 
 
 
 
 
 
 
r1   r!   r(   r   rk   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   rs   r   c                    t          t          t          | j                                      }|j        dk    rt          |j                  dk    sJ |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   r   c                 4    i | ]}|j         d k    |j        |S r   )r   r   r   s     r2   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>3  s/        47m3K3K	43K3K3Kr1   c                     i | ]\  }}||	S r0   r0   )rO   rP   
buffer_fqns      r2   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>9  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><  s5     # # #Az 	
#J/# # #r1   Ncall_functionc                     | uS rM   r0   )r?  new_nodes    r2   <lambda>z5_inplace_buffer_and_input_mutations.<locals>.<lambda>R  s    1H;L r1   )nextiterreversedr   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>   )rs   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     sY   : tHU[112233K>X%%#k.>*?*?1*D*D*D*D"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FF
	F
	c                     | j         D ]9}d|j        vr|j        d                                         D ]\  }}|s|c c S :d S )Nnn_module_stack)r   r   r   )rs   r   rU  rt   s       r2   r   r   Z  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>n  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\   )rs   r   r  fqnsprev_fqn	next_fqnsnum_valid_indicescurr_fqns           r2   r   r   e  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J t	          |j        t           j        j                  sJ  || j                   ||j                  k    S )Nrs   rk   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 )Nrk   c                     t          | t          j        j                  r%dt	          t          |                              z   S t	          |           S )N%)r]   r;   r   Noderp   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   )rp   )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   )rp   r  r   r   tree_mapr   r~   r\   r   rG   r   r   r   )rs   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   )r;   r   r   rp   r]   rs   )r?  r  r  s      r2   _check_graph_equivalencer    s    %(. S    * agux~.....agux~.....: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 r   )rs   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  rs   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  rs   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   rc   rd   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  rc   rd   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     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                  }| j        |j         j!        d	k    sJ |j         j"        tF          u sJ |j         $                                \  }}|j"        tF          u sJ |j"        tJ          u sJ  j        &                    d           5   fd
tO          |j!                  D             }i }|j(        D ]} j        )                    |          ||<    tU           j        tG          |          |f|j                   }tW          |j,                  D ]\  }} j        -                    dt\          j/        ||fta          |tb                    s|j2        nd|           }ta          |tb                    rd|j2         j        v rHtg          j3         j        |j2                 j4                  |_4        | j         j        |j2                 <   	 d d d            n# 1 swxY w Y    j        j        5                     j                  5  g }|j,        D ]}ta          |tb                    r|                    |j6                   2|j2         j        vr|                    d            Vta          |tn          tp          tr          tt          f          sJ |                     j        ;                     j        |j2                                      ty           j        j        ||j                   } j        j        =                    t\          j/        |df           j        j        =                    t\          j/        |df           fdtO          |j!                  D             } fd|j(        D             }d d d            n# 1 swxY w Y    j        J tG          |           j        _>        | j        _?        d S d S d S )NFr   r   rt   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  parentrn   rt   s      r2   r  z,_ModuleFrame.__init__.<locals>.create_moduleH  sp    06
C&*,,s,,,4////55*58>>+;+;CCC-3$T*r1   r   )r  parent_moduleparent_call_moduler   call_idxr(      c                 J    g | ]}j                             d |            S )_positional_arg_)rs   r   )rO   rB  rn   s     r2   r   z)_ModuleFrame.__init__.<locals>.<listcomp>i  sB        J**+Cc+C+CDD  r1   rr  
_constant_)r   rG   r   r   c                 h    g | ].}j         j                            t          j        |f          /S r0   r  rs   rr  r  r  )rO   r   	args_nodern   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_nodern   s     r2   r   z)_ModuleFrame.__init__.<locals>.<dictcomp>  sO         t{(66 (;*:   r1   )@r  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   r\  r  r(   rV   r   rX  r   r;   r   r   rs   node_mapnode_to_placeholderr  r  r  r   r[   r   _SubmoduleEntryr6  num_childrenr   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~   ) rn   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  rt   s    `      `                     @@@r2   rw   z_ModuleFrame.__init__  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[%<$1Q6666$)U2222%.%6%?%?%A%A"I{>U****#t++++++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	       $** K33DOEJ4OPP    2K&%  !K-;;${A&6 	 #k/==${A&6     "9#9::  	     )0	  K* * * * * * * * * * * * * * *V *666+0+;+;D#(-8D#***m ! %<%<s&   EP  P$'P$E5WWWc                 X   | j         dk    sJ d| d            |j        | j        u sJ | j                            d           5  | j                            |j        |j                  }d d d            n# 1 swxY w Y   t          j        |j                  |_        || j	        |<   d S )Nr   zCannot add placeholder z to root module)	type_expr)
r   rs   r  r  r   r   r   r  r   r  )rn   r?  placeholder_nodes      r2   add_placeholderz_ModuleFrame.add_placeholder  s    x2~~~KKKK~~~w$/))))Z((.. 	P 	P#z55af5OO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P !%	!& 1 1&6 ###s   'A77A;>A;c                 T   t          j        t          j        j        | j        |j                  }t          j        t          j        j        | j        |j                  }| j        	                    |j
        ||          }t          j        |j                  |_        || j        |<   |S rM   )r   tree_map_onlyr;   r   r  r  r   r~   rs   rr  rG   r  r   r  )rn   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 sJ || j        v r| j        |         S |                     d| d           || j        v r| j        |         S |j        dk    s| j                            | j                  | 	                    |           | j
        o| j        j                             | j
                  5  | j
                            d| j                            |                     d d d            n# 1 swxY w Y   | j        |         S |j        dk    r|j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        fv s%t9          |j        d          r2|j        j        dk    r"|                     |           | j        |         S | j                            | j                  | j                             | |          S tC          d|j         d	|           )
Nzremap_input(r  r   r   rr  r*   	_operatorz(Could not run remap_input() on op type: z
 for node )"rs   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  )rn   r?  s     r2   r  z_ModuleFrame.remap_input  s}   w$/))))=##

&!&&&'''(((+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rk   c                    d }| j                                         D ],}|j                            di           }||}"||k    ri } n-|r<| j        J || j        j        d<   | j                                         D ]}|j        d= d S d S )Ncustom)r  r   r   rX  r  )rn   custom_metar   	curr_metas       r2   uplift_common_custom_metadataz*_ModuleFrame.uplift_common_custom_metadata  s    M((** 		 		D	h33I"'K''  (  	(*6665@D#(2,,.. ( (Ih''	( 	(( (r1   c           	           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J  j                             |           |d S t          |t8          j        j                  r|j!                            d          nd |D             |j!        d<    "                                 t5          |          dk    r|| j        j        |d         <   d S tG          |          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  rs   )rl  	seen_nodern   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   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  rs   rl  r   r  r  Proxyr   )rn   orig_outputsr5  rl  tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outr   s   `           @r2   finalize_outputsz_ModuleFrame.finalize_outputs  sN     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-$)>?????
-((( 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_noders   	node_copyr  r  r  r   r   s     r2   	copy_nodez_ModuleFrame.copy_nodem  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  r2  r   r4  run_from)rn   r   r   node_idxs       r2   	run_outerz_ModuleFrame.run_outerr  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  )rn   r   r~   s      r2   r2  z_ModuleFrame.print  s/    < 	#4"6"""""	# 	#r1   c                 4   d}|t          | j                  k     r| j        |         }|j        dk    sJ |                                  |                     d||                                           |                     | j                   t          | j                  }|j        dk    r|dk    r|S |                                  |S t          |j                            di                     dk    rt          d|           |j        d         }ddl
m} t          |          dk    r||v r| j        }n)d	 |j        d                                         D             }|d |         | j        k    rK|                                  |                     d
| j                   |                     | j                   |S |J t          | j        |          r||         }|                     d|           t!          |j        d                                                   |         }	t%          | j        | j        | j        | j        | j        | j        | | j        |gz   |	                    d          d         | j        
  
                            |          }|dz  }|| j        k    sJ |j        dk    r*| j        | j                                     |j                   |                     |           |dz  }|t          | j                  k     d S d S )Nr   r   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  rt   s       r2   r   z)_ModuleFrame.run_from.<locals>.<listcomp>  sj     % % % &:D"	 ",14AGGCLL,---q% % %r1   	outliningzCreating new stack frame forr   r  )r   r   r   r2  r2  r  r/  r   rX  r  (torch._export.passes._node_metadata_hookr<  r\   r   rs   r  r   keysr  r  r  r  r  r  rT   r   r6  r  r  rG   r4  )
rn   r7  
module_idxr   depthr  r<  node_module_stacknext_modulenext_module_keys
             r2   r6  z_ModuleFrame.run_from  sM   
TZ((:h'D7m++++JJLLLJJvx)9)9););<<<JJt())))**Ew(""A:: $O %%'''49==!2B7788A=="#Td#T#TUUU"i(9:O     
 O$$))./AA %)$5!!% % *.3D)E)K)K)M)M% % %! !%(D,=== %%'''

;111

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

9;GGG #'ty1B'C'H'H'J'J"K"KE"R'OJO%O(%5#))#..q1*  (8$$  a
 %(99999w*$$/33DK@@@NN4   MHs TZ((((((r1   rM   )rk   N)r)   r*   r+   r;   r   r   r>   r  r   rp   r?   r   r   r   r!   rw   r
  r  r  r  r/  r4  r8  r2  r6  r0   r1   r2   r  r    sG        CGc9 c9HNc9 UX]C'(c9 5cDj#!567c9  %8 89c9 $'884?c9 c9 c9 c9J	7 	7 	7  ) ) )V( ( ( (*Y> Y> Y>v* * *
% % %&# # #
\ \ \ \ \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+   rp   r=   r;   rX   rY   r   r  r?   r0   r1   r2   r  r    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   r8  )rG  rH  r  r  r  r  s         r2   r   r     s    +-J5@5F5FL&1#&6&6J24Oj	
	
 	
$6	
 	
 	

     ikkk##r1   r   r  r/  rc   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   )rc   )r  )r   r@  r  r  r;   rX   rY   r[   r\   r  rT   delattrr   sortr  
itemgetterregister_module)r  r/  rc   r   r  r   childr)  s           r2   r  r    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_fqnrm   s    r2   rw   z_IVals.__init__7  s    !,S!1!1r1   c                 \    t          |t          j        j                  r|j        j        S dS rA   )r]   r;   _ops
OpOverload_schema
is_mutable)rn   rG   s     r2   _is_mutablez_IVals._is_mutable;  s)    fej344 	->,,ur1   c                    |j         dk    s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Checking mutabilityr  r   )r   rY  rG   r2  rv  rx  r   r   rS  r  r   r  r   )rn   mfr   br   r)  s         r2   r  z_IVals.read@  s    
 w/))))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>W  s%    HHHq16Z3G3G3G3G3Gr1   rr  r   N)r(   rs   rS  r   r   r|  r}  r;   r~  r  r  r  r   r  r   )	rn   
partitionsshared_submodulesr   rs   r   r   rt  r_  s	           @r2   rZ   z_IVals.updateO  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:   rw   rY  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  rH  r  r  namesr(   r   r"  s           r2   r   r   d  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^  )r`  r   ra  r   r   r  rG   deduplicatedseenr  rc   r   seen_child_fqnseen_targets                 r2   r  r  p  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J 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         }t1          | |          }t          |t          j        t          j        f          sJ |                    |
          5  |                    dd	                    |                    }ddd           n# 1 swxY w Y   |                    |d           |                    |           |                     |j                   t          | tB                    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.Modulers   r   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   rm  s    r2   ru  z_sink_params.<locals>.<lambda>  s    )> r1   c                 <    | j         t                             vS rM   )r   r   )r.  rj  	submodules    r2   ru  z_sink_params.<locals>.<lambda>  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>  s&    )I)I)Ia!''#,,q/)I)I)Ir1   r  T)propagate_meta)#r   rU   r   r[   r\   r  r
   rZ   r   r   r   r]   r;   r   r   r   filterr   rG   r>   r   r   rT   r%  r   r`   rb   r|  r}  r   r  
erase_noder  r   r   )r(   r+  ri  rj  r   submod_id_to_inputs_removedrP   r  rs   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrrt  rp  s      `                 @r2   r  r    s$   , #*&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^,,,,,&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MMM &&~66 N N ,,Z)9L9LMMN N N N N N N N N N N N N N N &&x&EEE49%%%%&+,, vJJ''s   *LL		L		)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   r`   rb   rX   rY   rp   boolrg   ri   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 00lU//%(/A00x00 00 	00
 00 00 00 00f	 	 	 	 	 	 	 	U
 U
 U
 U
 U
 U
 U
 U
p-" -" -" -" -".%(/ -" -" -"`    cg   ,{
 {
 {
 {
 {
 {
 {
 {
~ JN! !!0?$0F!! ! ! !HE)8>E))E) 
E) E) E) E)PUX^ d
    $ $ $NR R R
6# 6# 6# 6 6 6 6$6 6EHO 6 6 6 66%(/ 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
S S S S S S S Sl        $EHN $AR $ $ $ $4 GI, ,HO,(,S#X,@C, , , ,,?= ?= ?= ?= ?= ?= ?= ?=D	'	'"	' S#c(]#	' 	' 	' 	'(# (# (#^ ?C	p( p(HOp(#tCy.)p( 9p( "&c3s8m!4t!;	p( p( p( p( p( p(r1   