
    &Vji                       d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ 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/m0Z0 d dlm1Z1m2Z2 ddl3m4Z4  ej5        e6          Z7g dZ8 G d de          Z9 ed           G d d                      Z:da;ed             Z<	 d\deej=        ej>        ej?        j@        f         dej?        j@        deAde9deBf
dZC G d d           ZD G d! d"eDej?        j@                  ZE G d# d$eDej?        j@                  ZF G d% d&e jG                  ZH G d' d(ej?        j@                  ZI	 d]d)ed*eeH         d+eIfd,ZJd-ej        jK        d.e$d+dfd/ZLd0 ZMd1 ZNd2eAd3eAd+eAfd4ZOd5ej?        j@        d6ej?        j@        fd7ZPd8ej?        j@        d+eAfd9ZQd8ej        jR        d+ej        jS        fd:ZTd8eej        jR        eEeIf         d+ej        jS        fd;ZUd8eej        jR        eEeIf         d+ej        jS        fd<ZVd=ej?        j@        deAfd>ZW	 d]d=ej?        j@        deAd?ej?        j@        d@eeeAgej?        j@        f                  fdAZXdBeAdCeYd+eAfdDZZdEeAdBeAd+eBfdFZ[ G dG dH          Z\e G dI dJ                      Z]dKej        jK        dLeIfdMZ^	 d^dOej?        j@        dPe_eAeYf         dQeAfdRZ` G dS dT          Zad8ej        jR        dLeIdUe_eAebeA         f         fdVZcdW Zd	 d]d)ej?        j@        dXe_eAeeeA         f         dYeeeA         dZee_eYebeA         f                  fd[ZfdS )_    N)defaultdict)contextmanager)deepcopy)	dataclass)Enum)AnyCallablecastOptionalUnion)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     P/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/export/unflatten.pyr&   r&   1   s"        IFHFFFr3   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intr2   r3   r4   r7   r7   8   sE         TT))))MMM
KKKr3   r7   c               #   8   K   t           } da 	 d V  | a d S # | a w xY wNF)RUN_WITH_INTERPRETER)old_flags    r4   _disable_interpreterrF   E   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      r4   	<genexpr>z_assign_attr.<locals>.<genexpr>f   sK        Av D))     r3   )rK   z2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrr=   nnModuleupdate_modulesitemsr&   r.   
isinstance	Parameterregister_parameterr/   Tensorregister_bufferr0   r   ScriptObjectr1   )
rG   rH   rI   rJ   rK   prefixfield
to_modulestsrT   s
            @r4   _assign_attrri   R   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 0r3   c                   >    e Zd ZU ee         ed<   dee         fdZdS )_SubmoduleBase_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    r4   	type_namez_SubmoduleBase.type_name   s     xr3   N)r+   r,   r-   r   strr?   rq   r2   r3   r4   rk   rk      sE         	#8C=      r3   rk   c                        e Zd ZU dZeej        j                 ed<   	 ddej        j	        dee
         f fdZd Zd Z	 	 	 	 ddZ xZS )r!   zA module that uses torch.fx.Interpreter to execute instead of the usual
    codegen that GraphModule uses. This provides better stack trace information
    and makes it easier to debug execution.
    graph_moduleNgraphtyc                     t                                                       || _        || _        | | j        _        t
          | _        d S rO   )super__init__ru   rl   owning_modulerD   _run_with_interpreter)rp   ru   rv   	__class__s      r4   ry   zInterpreterModule.__init__   sA    
 	
#'
 %9"""r3   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 rO   r2   )rQ   
kwarg_namekwargss     r4   rU   z,InterpreterModule.forward.<locals>.<genexpr>   s?          "!V++ :&++++   r3   ru   enable_io_processingF)rt   r   r=   compileris_dynamo_compilingr{   typeforwardlist	arg_nameslenextendr@   fxInterpreterru   run)rp   argsr   arg_listkwarg_namess     `  r4   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  r3   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appendrI   rp   nodes     r4   finalizezInterpreterModule.finalize   s     ).(<(<T4:(N(Nn%
 J$ 	3 	3Dw-''%%dk222	3 	3r3   TFc                 *    t          | d||||          S )Nr!   r   rp   print_outputinclude_strideinclude_devicecoloreds        r4   print_readablez InterpreterModule.print_readable   (     
 
 	
r3   rO   TFFF)r+   r,   r-   r<   r   r=   r   r   r?   Graphrr   ry   r   r   r   __classcell__r|   s   @r4   r!   r!      s          
 58/0000
 !	: 	:x~	: SM	: 	: 	: 	: 	: 	:! ! !F3 3 3& 
 
 
 
 
 
 
 
r3   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   )rx   ry   r]   rY   getattrrl   _call_modules
_num_calls)rp   r   r   accessorr|   s       r4   ry   z$InterpreterModuleDispatcher.__init__   s    |$Q0 	H 	HHD(GLOX$F$FGGGG?&)r3   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       r4   r   z#InterpreterModuleDispatcher.forward   sn    (9?Q.#d6H2I2II	;//// 	 	 	DO	s   A   Ac                     | j         S rO   )r   ro   s    r4   r   z(InterpreterModuleDispatcher.call_modules  s    !!r3   TFc                 `    fd| j         D             }d                    |          S )Nc                 @    g | ]}|                               S r2   )r   )rQ   modr   r   r   r   s     r4   
<listcomp>z>InterpreterModuleDispatcher.print_readable.<locals>.<listcomp>  sG     
 
 
  	 
 
 
r3   
)r   join)rp   r   r   r   r   outputss    ```` r4   r   z*InterpreterModuleDispatcher.print_readable  sV    
 
 
 
 
 
 
 )
 
 
 yy!!!r3   r   )r+   r,   r-   r<   rW   rr   r   r!   ry   r   r   r   r   r   s   @r4   r"   r"      s         c#h d;L6M        " " "
 " " " " " " " "r3   r"   c                       e Zd ZdZej        	 	 ddej        dej        dee	         de
eee	f                  de
e	         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``.Nr2   )rp   r   r   r   r   r   s         r4   adaptzFlatArgsAdapter.adapt  s	     	r3   c                     g S )z>Returns a list of paths that are used to access the flat args.r2   ro   s    r4   get_flat_arg_pathsz"FlatArgsAdapter.get_flat_arg_paths*  s    	r3   )NN)r+   r,   r-   r<   abcabstractmethodpytreeTreeSpecr   r   r   dictrr   r   r   r2   r3   r4   r    r      s          	 .2!	 	_	 O	 I		
 4S>*	 c]	 
c	 	 	 	DI      r3   r    c                   l     e Zd Z	 ddedee         f fdZd Zd Zd Z	d Z
d	 Zd
 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    sJ t          |j                  }t          |j                  | _        t          j	        
                                | _        | | j        _        t          |j                  | _        || _        |j        j        | _        | | j        d<   d| _        t           | _        t%          || j                   t'          |           t)                      | _        t-          ||           \  }}| j                            |                                           t3          |j        | |           |j        | _        g | _        |j        }t=                      }	i }
| j        j        D ]}||         } ||          |
vrDt          j         !                    |"                                |j#                  |
 ||          <   tI          |
 ||                   | |tJ          j&        	           |	'                    |           t=          | j        j(                  }t=                      }i }| j        j)        D ]}||v rd}|j*        |         }n
d
}||         } ||          |vr"|"                                |f| ||          <   tI          | ||                   d         | |tJ          j+        |           |'                    |           |,                                D ]\  }}||	v s||v rd} ||          |v st[          |t          j         j!                  sd
}|rQ ||          |vr|d
f| ||          <   tI          | ||                   d         | |tJ          j+        d
            ||          |
vr||
 ||          <   tI          |
 ||                   | |tJ          j&        	           i }|j*        ,                                D ]|\  }} ||          |vr<t[          |t          j.                  r|"                                }|| ||          <   | ||                   }tI          || |tJ          j/        	           }ta          tb                    't=                      }'fd}t=                      }| j        j2        D ]}|j3        th          j&        k    s|j3        th          j+        k    r|j5        rtm          |j7        d          sJ t[          |j8        tr                    sJ  | ||j        |j8                           |j7        j:        |j8                   |'                    |j8                   |'                    |j8                   |j3        th          j+        k    r|j5        r*|j3        th          j;        k    s|j3        th          j<        k    rtm          |j7        d          sJ t[          |j8        tr                    sJ  | ||j*        |j8                           |j7        j:        |j8                   |'                    |j8                   |j*        ,                                D ]R\  }}||vrI ||          }|'v sJ '|         d         \  }}  ||||           |'                    |j8                   S|j        ,                                D ]7\  }}||vr. ||          }!|!'vr'|!         d         \  }}  ||!||           8i }"'                                D ]}#d |#D             }$|#D ]
\  }%} |$|"|%<   t{          | |"g            t}          |                                          ((fd|D             }| ?                    (|           d |D             }d | j        j@        D             | _A        d
| _B        d t          |          D             }&| D                    d          D ]\  }} ||&vrt          |&          |&|<   t          | |&           | j        G                                 | H                                 d S )Nz%Unflattening on JointExportModule NYIc                 
   t          | t          j                  r[t          |                                 |                                 |                                 |                                           S t          |           S )z8Returns _TensorID dataclass for tensors, otherwise id().)r8   r9   r:   r;   )	r_   r=   rb   r7   r8   r9   r:   r;   id)r   s    r4   _idz'UnflattenedModule.__init__.<locals>._id9  sn    #u|,,  $'$7$7$9$9::<<#&#5#5#7#7	    c77Nr3   c                     g | ]	}|j         
S r2   )fqnrQ   entrys     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>D  s    KKK%EIKKKr3   r    unflattened_moduleF)requires_grad)rJ   T)rJ   rK   c                 F    |          }|                     ||f           d S rO   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r4   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_map  s,    "6*Ii566666r3   namec                     g | ]
}|d          S r   r2   )rQ   ts     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    111qt111r3   c                     g | ]}|v|	S r2   r2   )rQ   r   redirected_call_indicess     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s$    RRRCs:Q/Q/QC/Q/Q/Qr3   c                     g | ]}d |v|	S )@r2   )rQ   r   s     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    >>>Cs#~~C~~~r3   c                 (    g | ]}|j         d k    |S r   r   rQ   r   s     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>   s+     #
 #
 #
M1I1ID1I1I1Ir3   c                     i | ]\  }}||	S r2   r2   )rQ   ir   s      r4   
<dictcomp>z.UnflattenedModule.__init__.<locals>.<dictcomp>%  s    >>>3S!>>>r3   remove_duplicate)Irx   ry   graph_signaturebackward_signature
ValueErrormodule_call_graphr   ru   r=   r   r   rz   r   rt   metaadaptedrD   r{   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_IValsivals_outline_submodulesr\   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrW   
parametersrZ   r`   cloner   ri   r&   r.   addnon_persistent_buffersbuffers	constantsr/   r^   r_   rb   r0   r   r   input_specskindr   rK   rX   argrI   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_bufferrK   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*                                          @@r4   ry   zUnflattenedModule.__init__0  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+++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UW(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	      	 +4::<< 	 	MCs8}}K//h55 0'~~//H-5CCMM*#CCMM2I#,	     JUJ
 J

 $'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),,,
r3   c                     |                                  D ]_\  }}t          |dz              t          |d          r8t          |j        t
          j        j                  rt          |j                   `d S )N:ru   )r  printrX   r_   ru   r=   r   r   )rp   r   r   s      r4   _print_graphzUnflattenedModule._print_graph.  sw    **,, 	! 	!HC#)sG$$ !CIux~)N)N !ci   	! 	!r3   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_argsr7  inputr6  s        r4   _adapt_flat_argsz"UnflattenedModule._adapt_flat_args4  s    *1-7	i'''!)O  
 .44%-"$ 5  I 9~~!2!===G #IG G(1(9(DG G  
 r3   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   r2   rQ   xs     r4   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>Y  s    777aQqT777r3   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   )rQ   rC  r  flat_arg_pathss      r4   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>u  sp     + + + !S (C000&#1&:^C%8%8%9   
+ + +r3   )r   r6  r   r7  r   tree_flatten_with_pathr   r   r3  r<  r  torch._export.utilsrA  r   r   r   r  r  r   )rp   r   r   r6  reordered_kwargsflat_args_with_pathr7  r:  rA  new_flat_args_with_pathrE  s             @r4   process_forward_inputsz(UnflattenedModule.process_forward_inputsO  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   r3   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   )rK  r   r6  r   r=   r   r   ru   r   r_   r@   r   r   r   r{   r   rt   r   r   tree_unflattenout_spec)rp   r   r   r:  r6  
return_valtree_outs          r4   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Br3   c                     t           j                            | | j                  | j        d<   | j                                         d S )Nrt   )r=   r   r   ru   r   r   ro   s    r4   r   zUnflattenedModule.finalize  s:    (-(<(<T4:(N(Nn%
r3   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   0rM   r   r   c                     g | ]\  }}|S r2   r2   )rQ   r*  r   s      r4   r   z7UnflattenedModule._dispatch_modules.<locals>.<listcomp>  s    KKKFAsCKKKr3   c                     |j         D ]C}|j        dk    r6|j                            d          d         }| r|  d| n|}|v r||_        Dd S )Nr   r   r   rM   )r   r   rI   rV   )re   ru   r   r   pathcalled_moduless        r4   elide_call_indicesz?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  su     * *7m+++++C003C06?f,,s,,,CD~--&)* *r3   Fr   ru   r   N)r   r   r   r   r6  r   getrV   r   rA   rW   rsplitr  r^   sortedr   range
_call_namer   r]   popset_submoduler"   r  rX   ru   r   )rp   r   r#  r   r   r   baserC  	attrs_maprI   orig_fqnr   indexed_call_modulesr   r   re   rX  mod_rW  s                     @r4   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 	8r3   TFc                 *    t          | d||||          S )Nr#   r   r   s        r4   r   z UnflattenedModule.print_readable  r   r3   rO   r   )r+   r,   r-   r   r   r    ry   r4  r<  rK  r   r   r  r   r   r   s   @r4   r#   r#   /  s         8<| |&| $O4| | | | | ||! ! !  6; ; ;zC C C*  88 88 88x 
 
 
 
 
 
 
 
r3   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disablerK  )r*   r   ms      r4   r$   r$     sU    , #6**F&"344A  %}44	 ;  5    A Hr3   ru   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     r4   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>5  s/        47m3K3K	43K3K3Kr3   c                     i | ]\  }}||	S r2   r2   )rQ   rR   
buffer_fqns      r4   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>;  s+          '!Z
A     r3   c                 (    i | ]\  }}||         S r2   r2   )rQ   rR   rp  buffer_fqn_to_input_names      r4   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>>  s5     # # #Az 	
#J/# # #r3   Ncall_functionc                     | uS rO   r2   )r@  new_nodes    r4   <lambda>z5_inplace_buffer_and_input_mutations.<locals>.<lambda>T  s    1H;L r3   )nextiterreversedr   r   r   r   inputs_to_buffersr^   buffers_to_mutater\   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_noderR   vuser_outputsrr  ru  s                @@r4   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 ]}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 )Nnn_module_stackc                 V    g | ]&\  }}d |v r|                     d           d         n|'S r   r   rV   )rQ   r   _ts      r4   r   z)_fix_nn_module_stacks.<locals>.<listcomp>e  sH     
 
 
R "%CIIcNN1
 
 
r3   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           r4   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'(=   r3   c                 x    t          |           t          |          k     o|dt          |                    | k    S )z2Check whether `candidate` is a prefix of `target`.N)r   )	candidaterI   s     r4   r  r    s4    y>>CKK'QF3CS^^3C,D	,QQr3   
parent_fqn	child_fqnc                    | dk    r|S |                      d          }|                     d          }|d t          |                   |k    rt          d| d|  d          d                    |t          |          d                    S )Nr   rM   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.)rV   r   RuntimeErrorr   )r  r  parent_splitchild_splits       r4   _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5r3   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 )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    r4   arg_dumpz>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  sB    #ux}-- 5S2c77!34444s88Or3   c                 ,    g | ]}t          |          S r2   )rr   )rQ   r  s     r4   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s    RRRcSRRRr3   c                 "    g | ]\  }}| d | S )=r2   )rQ   keyvalues      r4   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s:       C       r3   )rs  get_attrr   z: [z](z, )r   )rr   r  r   r   tree_mapr   r   r^   r   rI   r   r   r   )ru   retr  r   r   	args_dumprI   r  s          @r4   
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~~r3   )r=   r   r   rr   r_   ru   )r@  r  r  s      r4   _check_graph_equivalencer    s    %(. S    ( agux~.....agux~.....:ag**QW"5"555r3   gmc                     d}t          | d|           r|dz  }t          | d|           d| }t          | ||           |S )Nr   _spec_r   )rX   rY   )r  specr   r   s       r4   	_add_specr    sd    	A
"lqll
#
# 	Q "lqll
#
# A<<DBdKr3   c                     | j                             t          j        |f          }| j                             t          j        |df          }|S r   )ru   rs  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r4   _generate_flattenr    sB    h$$V%84'BBG&&x'7'1FFIr3   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rO   )r  ru   r  rs  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r4   _generate_flatten_specr    sH     RD!!$''I8!!)"=i?PQQQr3   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rO   )r  ru   r  rs  r   rM  )r  r   r  r   r  s        r4   _generate_unflattenr    sH     RD!!$''I8!!&"7%9KLLLr3   r   c                     |                     d          ^ }}|D ]<}t          | |d           }| d S t          |t          j        j                  s d S |} =t          | |d           S )NrM   )rV   r   r_   r=   rZ   r[   )r   rI   re   rf   rT   submods         r4   _get_submoduler    sy    \\#&&NVU 	 	dD))>44&%(/22 	443t$$$r3   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 )NrM   r   F)
rV   r  r   r   r=   rZ   r[   rY   r_   
add_module)	r   rI   r  r  re   rf   r   rT   r  s	            r4   _add_submoduler    s     \\#&&NVUV$$  4dD))>(&sxxwQw'@'@AA**Cv&&&&%(/22 	55NN5-(((((r3   r`  r/  c                 &    |dk    r| n	|  d|dz
   S )Nr   r   r2   )r`  r/  s     r4   r]  r]    s(     6644$00Q000r3   	call_namec                 Z    t          j        t          j        |          dz   |           d uS )Nz(@\d+)?$)rematchescape)r  r`  s     r4   rP   rP     s&    8BIdOOk19==TIIr3   c                       e Zd Z	 ddej        j        deej        j        df         deee	e
e	         ef                  dee	ef         de
eej        j        ef                  f
dZd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )_ModuleFrameN
flat_graphr   .module_stackr   r*   c                     | _         | _        | _        | _        | _        | _         _        | _        |	 _        |
 _	        d _
         j        d         \   _        }t           j        |dz              _          |&| _        t          |d          r|j        ni  _        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"        d         }|j         j"        d         }|j#        J |j#        J  j        $                    d           5   fd
tK          |j!                  D             }i }|j#        D ]} j        &                    |          ||<    tO           j        tQ          |          |f|j                   }tS          |j*                  D ]\  }} j        +                    dtX          j-        ||ft]          |t^                    s|j0        nd|           }t]          |t^                    rd|j0         j        v rHtc          j1         j        |j0                 j2                  |_2        | j         j        |j0                 <   	 d d d            n# 1 swxY w Y    j        j        3                     j                  5  g }|j*        D ]}t]          |t^                    r|                    |j4                   2|j0         j        vr|                    d            Vt]          |tj          tl          tn          tp          f          sJ |                     j        9                     j        |j0                                      tu           j        j        ||j                   } j        j        ;                    tX          j-        |df           j        j        ;                    tX          j-        |df           fdtK          |j!                  D             } fd|j#        D             }d d d            n# 1 swxY w Y    j        J tQ          |           j        _<        | j        _=        d S d S d S )NFr   r   rv   c                     j         rj          d|  n| }|j        v rj        |         S t          t          j                                                  }|j        |<   |S )NrM   r  )r   created_modulesr!   r=   r   r   )r   rV  r  parentrp   rv   s      r4   r  z,_ModuleFrame.__init__.<locals>.create_module>  sp    06
C&*,,s,,,4////55*58>>+;+;CCC-3$T*r3   r   )r  parent_moduleparent_call_moduler   call_idxr*      c                 J    g | ]}j                             d |            S )_positional_arg_)ru   r   )rQ   rC  rp   s     r4   r   z)_ModuleFrame.__init__.<locals>.<listcomp>_  sB        J**+Cc+C+CDD  r3   rs  
_constant_)r   rI   r   r   c                 h    g | ].}j         j                            t          j        |f          /S r2   r  ru   rs  r  r  )rQ   r   	args_noderp   s     r4   r   z)_ModuleFrame.__init__.<locals>.<listcomp>  sF        K%33H4DyRSnUU  r3   c                 j    i | ]/}|j         j                            t          j        |f          0S r2   r  )rQ   rR   kwargs_noderp   s     r4   r   z)_ModuleFrame.__init__.<locals>.<dictcomp>  sO         t{(66 (;*:   r3   )>r  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   r]  r  r*   rX   r   rY  r!   r=   r   r   ru   node_mapnode_to_placeholderr  r  r  r   r]   r   _SubmoduleEntryr7  num_childrenchildren_specscontextr}  r\  r   r  r@   r  inputsr~  r  r  r_   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  rs  r   r   ) rp   r  r   r  r  r  r  r  r  r  r   r*   	num_callsr   r  base_module_framer6  	args_speckwargs_spec	arg_nodeskwarg_nodesr   r:  rC  r  flat_arg_nodeinput_nodesr;  inputs_noder  r  rv   s    `      `                     @@@r4   ry   z_ModuleFrame.__init__
  s    %
$($.("!2"&"3B"7"i#DHi!m<<V DK)0)A)AIrDJJ.22!%(.."2"2r::: DK  DJ[&
 =?#% ;?(T^DDH       6=(DKOOO&,l&>&>x&H&HD# 0 I$($5dn$Ea$H!'8'?'H$dn-44#{"&+"4'+'>&];  	 	 	 &))$.99	 T[%<$1Q6666!)8;I#+:1=K$,,,&222++D11     $Y%;<<  	 !'/ E ED(,
(>(>t(D(DK%%2K9%%{3% 	
 !*)*: ; ;  HC$(J$:$:*'/'- $.c3C#D#D4CHH!3c!3!3 %; 	% 	%M "#'788 ! x4?22-1Ytsx7P7U-V-V*) 0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#***k ! %<%<s&   5EPP	P	4E5V55V9<V9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   ru   r  r  r   r   r   r  r   r  )rp   r@  placeholder_nodes      r4   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 rO   )r   tree_map_onlyr=   r   r  r  r   r   ru   rs  rI   r  r   r  )rp   r@  r   r   r   s        r4   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_function  s}     #EHM43CQVLL%ehmT5EqxPPz''$??Iaf%%	ar3   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   rs  r,   	_operatorz(Could not run remap_input() on op type: z
 for node )"ru   r  r  r3  r  r   r   rY  r   r
  r  r  r  
insert_argr  rI   r=   r  r  sym_sizerA   rT   r  unbindsumdim_IntListviewdiffrX   r,   r  r   readr  )rp   r@  s     r4   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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                  }|}ng } j                                        D ]Q}|j        D ]G}|j         j        vr7|                    |           |                     j        |                     nHR j        }t7          |          dk    r|d         }t          |t8          t:          j        j        f          sJ  j         !                    |           |d S t          |t:          j        j                  r|j"                            d          nd |D             |j"        d<   t7          |          dk    r|| j        j        |d         <   d S tG          |          D ]c\  }	}
|
t:          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   )rm  	seen_noderp   s     r4   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node  sx    >4 OFK8	--=33$":::3I>>&SfSStzSS  r3   c              3   .   K   | ]} |          V  d S rO   r2   )rQ   rm  r  s     r4   rU   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s/      PP,,V44PPPPPPr3   r   r   valc                 B    g | ]}|j                             d           S )r  )r   rY  )rQ   os     r4   r   z1_ModuleFrame.finalize_outputs.<locals>.<listcomp>>  s&    ;;;!&**U##;;;r3   )&r  r^  r   r   rY  r  r  r   r_   r   r   r   r   r   r   r  r   r  r  r*   r@   rN  r  r  r  keysusersr   r   r=   r   r  ru   rm  r   r  Proxyr   )rp   orig_outputsr6  rm  tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outr  s   `           @r4   finalize_outputsz_ModuleFrame.finalize_outputs  sH     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
 3I"D$;Y=O3 3J HUMMM!]//11  	!*  I ~T_<<$++I666%,,T]9-EFFF	 = 0J=!!Q&& -a 0-$)>?????
-((( F -77<M""5)));;];;; 	 |!!i&74>DK a111"+L"9"9 > >;&!HNN:66q9>	(3(8(<(<U(C(C	u%4=$[11> >r3   c                     |                      d|                                           | j                            || j                  | j        |<   || j        |j        <   d S )Ncopying)r3  format_noderu   	node_copyr  r  r  r   r   s     r4   	copy_nodez_ModuleFrame.copy_nodeL  sW    

9d..00111"j2249IJJd%)	"""r3   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   rm  )
r  r  r   r3  r   rY  r.  r   r0  run_from)rp   r   r   node_idxs       r4   	run_outerz_ModuleFrame.run_outerQ  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$$$	% 	%r3   c                 0    | j         rt          |i | d S d S rO   )r  r3  )rp   r   r   s      r4   r3  z_ModuleFrame.printd  s/    < 	#4"6"""""	# 	#r3   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   STEPrm  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   )rA   rV   )rQ   rR   rV  rv   s       r4   r   z)_ModuleFrame.run_from.<locals>.<listcomp>  sj     % % % &:D"	 ",14AGGCLL,---q% % %r3   	outliningzCreating new stack frame forr   r  )r   r   r   r3  r.  r  r+  r   rY  r  (torch._export.passes._node_metadata_hookr8  r^   r   ru   r  r   r   r  r  r  r  r  r  rV   r   r2  r  r  rI   r0  )
rp   r3  
module_idxr   depthr  r8  node_module_stacknext_modulenext_module_keys
             r4   r2  z_ModuleFrame.run_fromh  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   MHq TZ((((((r3   rO   )r+   r,   r-   r=   r   r   r@   r  r   rr   r   rA   r   r   r   r   r#   ry   r
  r  r  r+  r0  r4  r3  r2  r2   r3   r4   r  r  	  s8        LPb9 b9HNb9 UX]C'(b9 5hsmS!89:b9  %8 89b9 ux35FFGHb9 b9 b9 b9H	7 	7 	7  ) ) )VX> X> X>t* * *
% % %&# # #[ [ [ [ [r3   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=   rZ   r[   r   r  rA   r2   r3   r4   r  r    s]         OOO8?"""%%%	HHHMMMHOr3   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 r2   )r6  r   r   s     r4   r   z'_outline_submodules.<locals>.<dictcomp>  s6     	
 	
 	
	
Iu	
 	
 	
r3   )r*   )r   r   rW   r  r@   r   r   r4  )rB  rC  r  r  r  r  s         r4   r   r     s    +-J5@5F5FL&1#&6&6J24Oj	
	
 	
$6	
 	
 	

     ikkk##r3   r   r  r0  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   rM   )re   )r  )r   r   r  r  r=   rZ   r[   r]   r^   r  rV   delattrr   sortr  
itemgetterregister_module)r  r0  re   r   childrenr   childr*  s           r4   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++++, ,r3   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 rO   )r   rW   node_names_by_fqnro   s    r4   ry   z_IVals.__init__  s    !,S!1!1r3   c                 \    t          |t          j        j                  r|j        j        S dS rC   )r_   r=   _ops
OpOverload_schema
is_mutable)rp   rI   s     r4   _is_mutablez_IVals._is_mutable  s)    fej344 	->,,ur3   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.
        rs  zChecking mutabilityr  r   )r   rU  rI   r3  rw  ry  r   r   rO  r  r   r  r   )rp   mfr   br   r*  s         r4   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+++r3   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 r2   rD  )rQ   r/  
node_namess     r4   r   z!_IVals.update.<locals>.<listcomp>4  s%    HHHq16Z3G3G3G3G3Gr3   rs  r   N)r*   ru   rO  r   r   r}  r~  r=   r  r  r  r  r   r  r   )	rp   
partitionsshared_submodulesr   ru   r   r   ru  r[  s	           @r4   r\   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   rU  r  r\   r2   r3   r4   r   r     sZ         (2 2 2  
, , ,= = = = =r3   r   r  c                     |                                 D ]?\  }}|rt          ||          n|}|D ]#}t          | |          }t          |||           $@d S rO   )r^   r   r   rY   )r  rC  r  r  namesr*   r   r  s           r4   r   r   A  s    
 ',,.. ' '	56?P;	222[ 	' 	'D"d##CFD#&&&&	'' 'r3   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 )NFrM   T)r  r]  r   r  r  r  r  r*   r  rV   r   r]   r^  r  rI   r_  )r\  r   r]  r   r   r  rI   deduplicatedseenr  re   r   seen_child_fqnseen_targets                 r4   r  r  M  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 #"r3   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.Moduleru   r   c                     | j         dk    S )Nr   r   r/  s    r4   rv  z_sink_params.<locals>.<lambda>  s    14=#8 r3   r  c                     | j         dk    S )Nr   r   ri  s    r4   rv  z_sink_params.<locals>.<lambda>  s    )> r3   c                 <    | j         t                             vS rO   )r   r   )r/  rf  	submodules    r4   rv  z_sink_params.<locals>.<lambda>  s    af,G9,VV r3   rM   c                 D    g | ]}|                     d           d         S r  r  r?  s     r4   r   z _sink_params.<locals>.<listcomp>  s&    )I)I)Ia!''#,,q/)I)I)Ir3   r  T)propagate_meta)#r   rW   r   r]   r^   r  r
   r\   r   r   r   r_   r=   r   r   r   filterr   rI   r@   r   r   rV   r!  r   rb   rd   r}  r~  r   r  
erase_noder  r!   r   )r*   r,  re  rf  r   submod_id_to_inputs_removedrR   r  ru   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrru  rl  s      `                 @r4   r  r  x  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		)TrO   )r   )gr   r  loggingr  r  collectionsr   
contextlibr   r   dataclassesr   enumr   typingr   r	   r
   r   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&   r7   rD   rF   rb   rd   rZ   r[   rr   boolri   rk   r!   r"   ABCr    r#   r$   r   r   r   r  r  r  r  r   r  r  r  r  r  r  rA   r]  rP   r  r  r   r   r  r   rW   r   r  r   r  r2   r3   r4   <module>r     sc   



    				 # # # # # # % % % % % %       ! ! ! ! ! !       7 7 7 7 7 7 7 7 7 7 7 7 7 7  $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ? ? ? ? ? ? ( ( ( ( ( ( 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EL%"4ehoEF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   ,~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
D MQ! !!080I!! ! ! !HE)8>E))E) 
E) E) E) E)P$ $ $NR R R
6# 6# 6# 6 6 6 6$6 6EHO 6 6 6 64%(/ C    %(.     Reh"$57HHIR
X]R R R RMeh"$57HHIM
X]M M M M% % % % % %* AE	) )	)) 8?) HcUEHO%;<=	) ) ) )41S 1S 1S 1 1 1 1JS J J J J J J
z z z z z z z zz        $EHN $AR $ $ $ $4 GI, ,HO,(,S#X,@C, , , ,,?= ?= ?= ?= ?= ?= ?= ?=D	'	'"	' S#c(]#	' 	' 	' 	'(# (# (#^ BF	p( p(HOp(#tCy.)p( 9p( "*$sCH}*=!>	p( p( p( p( p( p(r3   