
    9i                    Z   S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJr  S SKJrJrJrJrJr  S SKrS SKJs  Jr  S SKJs  Jr  S SKJr  S S	KJr  S S
K J!r!  S SK"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*  S SK+J,r,  S SK-J.r.J/r/J0r0  S SKJ1r1J2r2  SSK3J4r4  \Rj                  " \65      r7/ SQr8 " S S\5      r9\" SS9 " S S5      5       r:Sq;\S 5       r< S[S\\Rz                  \R|                  \R~                  R                  4   S\R~                  R                  S\AS\9S\B4
S jjrC " S S 5      rD " S! S"\D\R~                  R                  5      rE " S# S$\D\R~                  R                  5      rF " S% S&\ R                  5      rH " S' S(\R~                  R                  5      rI S\S)\S*\\H   S+\I4S, jjrJS-\R,                  R                  S.\$S+S4S/ jrLS0 rMS1 rNS2\AS3\AS+\A4S4 jrOS5\R~                  R                  S6\R~                  R                  4S7 jrPS8\R~                  R                  S+\A4S9 jrQS8\R,                  R                  S+\R,                  R                  4S: jrTS8\\R,                  R                  \E\I4   S+\R,                  R                  4S; jrUS8\\R,                  R                  \E\I4   S+\R,                  R                  4S< jrVS=\R~                  R                  S\A4S> jrW S\S=\R~                  R                  S\AS?\R~                  R                  S@\\\A/\R~                  R                  4      4SA jjrXSB\ASC\YS+\A4SD jrZSE\ASB\AS+\B4SF jr[ " SG SH5      r\\ " SI SJ5      5       r]SK\R,                  R                  SL\I4SM jr^ S]SN\R~                  R                  SO\_\A\Y4   SP\A4SQ jjr` " SR SS5      raS8\R,                  R                  SL\IST\_\A\b\A   4   4SU jrcSV rd S\S)\R~                  R                  SW\_\A\e\A   4   SX\e\A   SY\\_\Y\b\A   4      4SZ jjrfg)^    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                   $    \ rS rSrSrSrSrSrSrg)	_AttrKind1   	parameterbufferconstantmodule N)	__name__
__module____qualname____firstlineno__	PARAMETERBUFFERCONSTANTMODULE__static_attributes__r,       V/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/export/unflatten.pyr&   r&   1   s    IFHFr6   r&   T)frozenc                   V    \ rS rSr% Sr\R                  \S'   \\S'   \\S'   \	\S'   Sr
g)		_TensorID8   zJCustom tensor identifier containing storage, stride, and size information.untyped_storagestridesizestorage_offsetr,   N)r-   r.   r/   r0   __doc__torchUntypedStorage__annotations__tupleintr5   r,   r6   r7   r:   r:   8   s!    T)))M
Kr6   r:   c               #   8   #    [         n Sq  S v   U q g ! U q f = f7fNF)RUN_WITH_INTERPRETER)old_flags    r7   _disable_interpreterrJ   E   s&      $H ('xs   	 from_obj	to_moduletarget	attr_kind
persistentc                   ^	 UR                  S5      Gt pVU1nU H  m	[        5       nU Hp  n[        UT	5      (       d)  [        UT	[        R
                  R                  5       5        UR                  U	4S jUR                  R                  5        5       5        Mr     UnM     U GH[  nU[        R                  :X  a>  [        U [        R
                  R                  5      (       d   eUR                  X`5        MV  U[        R                  :X  a3  [        U [        R                   5      (       d   eUR#                  X`US9  M  U[        R$                  :X  a\  [        U [&        5      (       a   S5       e[        U [        R                   [        R(                  45      (       d   e[        XU 5        GM  U[        R*                  :X  d  GM$  [        U [        R
                  R                  5      (       d   e[        XU 5        GM^     g )N.c              3   R   >#    U  H  u  p[        UT5      (       d  M  Uv   M     g 7fN)_is_call_name).0kt_callitems      r7   	<genexpr>_assign_attr.<locals>.<genexpr>f   s&      !;IA D) !;s   '	')rO   z2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrrA   nnModuleupdate_modulesitemsr&   r1   
isinstance	Parameterregister_parameterr2   Tensorregister_bufferr3   r   ScriptObjectr4   )
rK   rL   rM   rN   rO   prefixfield
to_modulestsrX   s
            @r7   _assign_attrrn   R   s    \\#&NV J#&5#I9d++	4):;II !*!3!3!9!9!;  $ 
   		+++h(:(:;;;;((9)***h5555%%e*%M),,,!(,<== D= LL&&    Ih/)***h8888Ih/+  r6   c                   :    \ rS rSr% \\   \S'   S\\   4S jrSrg)_SubmoduleBase   _tyreturnc                     U R                   $ )z
Subclass of this class - InterpreterModule, InterpreterModuleDispatcher, represents
corresponding model in eager model. To get this type information for those modules
in eager model we need to use this method.
)rr   selfs    r7   	type_name_SubmoduleBase.type_name   s     xxr6   r,   N)	r-   r.   r/   r0   r   strrC   rw   r5   r,   r6   r7   rp   rp      s    	#8C= r6   rp   c                      ^  \ rS rSr% Sr\\R                  R                     \	S'    SS\R                  R                  S\\   4U 4S jjjrS rS r    SS	 jrS
rU =r$ )r!      zA module that uses torch.fx.Interpreter to execute instead of the usual
codegen that GraphModule uses. This provides better stack trace information
and makes it easier to debug execution.
graph_modulegraphtyc                 p   > [         TU ]  5         Xl        X l        X R                  l        [
        U l        g rS   )super__init__r}   rr   owning_modulerH   _run_with_interpreter)rv   r}   r~   	__class__s      r7   r   InterpreterModule.__init__   s,    
 	
#'

 %9"r6   c                   ^ U R                   c   S5       e[        5       (       d]  [        R                  R	                  5       (       d  U R
                  (       d)  [        U R                   5      R                  " U /UQ70 TD6$ T(       a  [        U5      nU R                  [        U5      S  nUR                  U4S jU 5       5        [        U5      [        T5      :X  d   e[        U5      [        U R                  5      :X  d   e[        U5      n[        R                  R                  X R                  S9R                   " USS06$ )Nz&Didn't finalize this InterpreterModulec              3   >   >#    U  H  nUT;   d  M  TU   v   M     g 7frS   r,   )rU   
kwarg_namekwargss     r7   rY   ,InterpreterModule.forward.<locals>.<genexpr>   s'       &1
!V+ 'F:&&1s   
r}   enable_io_processingF)r|   r   rA   compileris_dynamo_compilingr   typeforwardlist	arg_nameslenextendrD   fxInterpreterr}   run)rv   argsr   arg_listkwarg_namess     `  r7   r   InterpreterModule.forward   s     ,V.VV,%''NN..008R8R ))*224I$I&II
  :"nnS]_=  &1   ;'3v;6668}DNN(;;;;X88''JJ'?CC,1 r6   c                 Z   [         R                  R                  X R                  5      U R                  S'   U R                  R                  5         / U l        U R                  R                   H:  nUR                  S:X  d  M  U R                  R                  UR                  5        M<     g )Nr|   placeholder)rA   r   GraphModuler}   __dict__lintr   nodesopappendrM   rv   nodes     r7   finalizeInterpreterModule.finalize   sr     ).(<(<T::(Nn%

 JJ$$Dww-'%%dkk2 %r6   c                 "    [        U SUUUU5      $ )Nr!   r   rv   print_outputinclude_strideinclude_devicecoloreds        r7   print_readable InterpreterModule.print_readable   #     
 	
r6   )r   rr   r   r}   rS   TFFF)r-   r.   r/   r0   r@   r   rA   r   r   rC   Graphry   r   r   r   r   r5   __classcell__r   s   @r7   r!   r!      so    
 588//00
 !	:xx~~	: SM	: 	:!F3& 
 
r6   r!   c                   b   ^  \ rS rSrSrS\\   S\\   4U 4S jjr	S r
S r    S
S jrS	rU =r$ )r"      z
A module that carries a sequence of InterpreterModules corresponding to
a sequence of calls of that module. Each call to the module dispatches
to the next InterpreterModule, and wraps back around after the last.
attrscall_modulesc           	         > [         TU ]  5         U(       d   eUS   R                  U l        U H  n[        X[	        US   U5      5        M     US   R
                  U l        X l        SU l        g Nr   )r   r   rb   r^   getattrrr   _call_modules
_num_calls)rv   r   r   accessorr   s       r7   r   $InterpreterModuleDispatcher.__init__   sd    |$Q00HDGLOX$FG ?&&)r6   c                     U R                   U R                     nU R                  S-   [        U R                   5      -  U l         U" U0 UD6$ ! [         a	    SU l        e f = f)Nr   r   )r   r   r   	Exception)rv   r   r   call_modules       r7   r   #InterpreterModuleDispatcher.forward   sd    ((9??Q.#d6H6H2II	/// 	DO	s   A A c                     U R                   $ rS   )r   ru   s    r7   r   (InterpreterModuleDispatcher.call_modules  s    !!!r6   c           	          U R                    Vs/ s H  nUR                  UUUU5      PM     nnSR                  U5      $ s  snf )N
)r   r   join)rv   r   r   r   r   modoutputss          r7   r   *InterpreterModuleDispatcher.print_readable  s[     ))
 * 	 * 	 
 yy!!
s   A )r   rb   r   rr   r   )r-   r.   r/   r0   r@   r\   ry   r   r!   r   r   r   r   r5   r   r   s   @r7   r"   r"      sG    c#h d;L6M "
 " "r6   r"   c                       \ rS rSrSr\R                    SS\R                  S\R                  S\	\
   S\\\\
4      S\\
   S	\	\
   4S
 jj5       rS	\	\   4S jrSrg)r    i  zF
Adapts input arguments with ``input_spec`` to align ``target_spec``.
Ntarget_spec
input_spec
input_argsmetadataobjrs   c                     g)z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr,   )rv   r   r   r   r   r   s         r7   adaptFlatArgsAdapter.adapt  s     	r6   c                     / $ )z>Returns a list of paths that are used to access the flat args.r,   ru   s    r7   get_flat_arg_paths"FlatArgsAdapter.get_flat_arg_paths*  s    	r6   r,   )NN)r-   r.   r/   r0   r@   abcabstractmethodpytreeTreeSpecr   r   r   dictry   r   r   r5   r,   r6   r7   r    r      s     	 .2!	__	 OO	 I		
 4S>*	 c]	 
c	 	DI r6   r    c                   v   ^  \ rS rSr SS\S\\   4U 4S jjjrS rS r	S r
S rS	 rS
 r    SS jrSrU =r$ )r#   i/  export_moduleflat_args_adapterc           	        >^, [         T-U ]  5         UR                  R                  b  [	        S5      eS nUR
                   Vs/ s H  oDR                  PM     nnUS   S:X  d   e[        UR                  5      n[        UR                  5      U l        [        R                  R                  5       U l        X R                  l        [        UR
                  5      U l        X l        UR                  R                  U l        X R                  S'   SU l        ["        U l        ['        X`R                  5        [)        U5        [+        5       U l        [/        X`5      u  pxU R,                  R1                  UR3                  5       5        [5        UR6                  X5        UR8                  U l        / U l        UR<                  n	[?        5       n
0 nU R                  R@                   H  nX   nU" U5      U;  a>  [        RB                  RE                  URG                  5       URH                  S9X" U5      '   [K        X" U5         U U[L        RN                  S9  U
RQ                  U5        M     [?        U R                  RR                  5      n[?        5       n0 nU R                  RT                   H  nX;   a  SnURV                  U   nOS	nX   nU" U5      U;  a  URG                  5       U4UU" U5      '   [K        UU" U5         S   U U[L        RX                  US
9  URQ                  U5        M     U	R[                  5        H  u  nnX;   d  X;   a  M  SnU" U5      U;   d)  []        U[        RB                  RD                  5      (       d  S	nU(       aB  U" U5      U;  a  US	4UU" U5      '   [K        UU" U5         S   U U[L        RX                  S	S
9  M  U" U5      U;  a
  UX" U5      '   [K        X" U5         U U[L        RN                  S9  M     0 nURV                  R[                  5        Hq  u  nnU" U5      U;  a:  []        U[        R^                  5      (       a  URG                  5       nUUU" U5      '   UU" U5         n[K        UU U[L        R`                  S9  Ms     [c        [d        5      m,[?        5       nU,4S jn[?        5       nU R                  Rf                   GH  nURh                  [j        RN                  :X  d/  URh                  [j        RX                  :X  a  URl                  (       a  [o        URp                  S5      (       d   e[]        URr                  [t        5      (       d   eU" U" UR<                  URr                     5      URp                  Rv                  URr                  5        URQ                  URr                  5        URQ                  URr                  5        GM  URh                  [j        RX                  :X  a  URl                  (       a?  URh                  [j        Rx                  :X  d!  URh                  [j        Rz                  :X  d  GM{  [o        URp                  S5      (       d   e[]        URr                  [t        5      (       d   eU" U" URV                  URr                     5      URp                  Rv                  URr                  5        URQ                  URr                  5        GM     URV                  R[                  5        HN  u  nnUU;  d  M  U" U5      nUT,;   d   eT,U   S   u  n n!U" UU U5        URQ                  WRr                  5        MP     UR<                  R[                  5        H3  u  nnUU;  d  M  U" U5      n"U"T,;  a  M  T,U"   S   u  n n!U" U"U U5        M5     0 n#T,R3                  5        H)  n$U$ V%s/ s H  n%U%S   PM
     n&n%U$ H  u  n'n!U&U#U''   M     M+     [}        U U#/ 5        [        UR3                  5       5      n(U Vs/ s H  nUU(;  d  M  UPM     nnU R                  U(U5        U Vs/ s H  nSU;  d  M  UPM     nnU R                  R                   V)s/ s H  n)U)R                  S:X  d  M  U)PM     sn)U lC        S	U lD        [        U5       V*Vs0 s H	  u  n*nUU*_M     n+n*nU R                  SS9 H  u  nn!UU+;  d  M  [        U+5      U+U'   M     [        U U+5        U R                  R                  5         U R                  5         g s  snf s  sn%f s  snf s  snf s  sn)f s  snn*f )Nz%Unflattening on JointExportModule NYIc                     [        U [        R                  5      (       aD  [        U R	                  5       U R                  5       U R                  5       U R                  5       S9$ [        U 5      $ )z8Returns _TensorID dataclass for tensors, otherwise id().)r<   r=   r>   r?   )	rd   rA   rg   r:   r<   r=   r>   r?   id)r   s    r7   _id'UnflattenedModule.__init__.<locals>._id9  sU    #u||,, $'$7$7$9::<#&#5#5#7	  c7Nr6   r    unflattened_moduleF)requires_grad)rN   T)rN   rO   c                 4   > TU    nUR                  X45        g rS   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r7   add_to_consts_map5UnflattenedModule.__init__.<locals>.add_to_consts_map  s    "6*Ii56r6   namer   @r   remove_duplicate)Kr   r   graph_signaturebackward_signature
ValueErrormodule_call_graphfqnr   r}   rA   r   r   r   r   r|   metaadaptedrH   r   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_IValsivals_outline_submodulesra   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictr\   
parametersr_   re   cloner   rn   r&   r1   addnon_persistent_buffersbuffers	constantsr2   rc   rd   rg   r3   r   r   input_specskindr   rO   r]   argrM   ry   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   r   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   r   ).rv   r   r   r   entryfqn_listexport_graphseen_modules
seen_attrsr  assigned_paramsid_to_paramr   paramr
  assigned_buffersid_to_bufferrO   r)   tensor	is_bufferid_to_constr   r*   	_constantconsts_targetsr   added_params_bufferss
const_nameconstconst_idph_name_	tensor_idinputs_to_statenode_targetttargetsnredirected_call_indicesr   i	fqn_orderr   r   s.                                               @r7   r   UnflattenedModule.__init__0  sC   
 	((;;GDEE		 ,9+J+JK+J%II+JK{b    3 34'(E(EFXX^^%
#'

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

,--/0 	-55tH!.!@!@*,! #--
$'E  	 ((33D$E5z,*/((*<*<KKM1D1D += +CJ' CJ'#--	 % 4 "%T%9%9%P%P!Q%(UUW((00D-"
&006!
#)6{,.-3\\^Z,HS[)S[)!,#**%   &% 1, ',,.LD&&$*BI6{l****3 3 !	K|3 1LV-  V-a0'..# v;k1/5KF,F,'11	9 /L  	 +44::<MC8}K/h55'~~/H-5CM*#CM2I#,,	 = JUJ

 $'5	7 *-%%11Avv,,,)***q||quuf----!!((C0000!00:;EEJJHH
 ""188,$((2)***66Y66666Y111quuf----!!((C0000!//9:EEJJHH
 ""188,5 2: "/!8!8!>!>!@J/u::---'1!4
!(GZ@$((2 "A )3399;KC..K	J. '	215
!)Wc: < 13%,,.K%01[qt[G1#1%," $ /
 	T?B/"6|7J7J7L"M#+R8Cs:Q/QC8R6G#+>8Cs#~C8> "ZZ--#
-TM1ID-#
 (,$*3H*=>*=3S!V*=	>))5)AGD!9$"%i.	$ B 	D),

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

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

+CGG16H $$X/A/ABBr6   c                     [         R                  R                  X R                  5      U R                  S'   U R                  R                  5         g )Nr|   )rA   r   r   r}   r   r   ru   s    r7   r   UnflattenedModule.finalize  s1    (-(<(<T::(Nn%

r6   c                   ^ [        [        5      mU R                   H  nUR                  (       d  M  UR                  (       d  M)  UR                  n[        XR                  XD5      5      nSU;   a  UR                  S5      OUS/u  pgTU   R                  [        U5      U45        M     [        [        5      nU HF  n	SU	;   a)  U	R                  SS5      u  pX   R                  U5        M2  US   R                  U	5        MH     TR                  5        H  u  p[        U5       VVs/ s H  u  pUPM	     nnn[        U5      S:  d  M5  [!        [        U5      5       HQ  n[#        XS-   5      nXA;  d  M  UR                  S5      Gt nn[%        U U5      R&                  R)                  U5        MS     U R+                  U
[-        X   U5      5        M     U4S jnU R/                  SS9 Hv  u  pE[1        US	5      (       a  U" XER2                  5        M*  [1        US
5      (       d  M=  UR4                   H)  n[1        US	5      (       d   eU" UUR2                  5        M+     Mx     gs  snnf )zFor a module whose call signatures are preserved, replace
multiple modules corresponding to multiple calls to that module
with a single dispatcher module that tracks which module to call.
r   0rQ   r   r   c                    > UR                    HQ  nUR                  S:X  d  M  UR                  R                  S5      S   nU (       a  U  SU 3OUnUT;   d  MK  X2l        MS     g )Nr   r   r   rQ   )r   r   rM   r[   )rj   r}   r   r   pathcalled_moduless        r7   elide_call_indices?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  sW    77m+++++C03C06fXQse,CD~-&) $r6   Fr   r}   r   N)r   r   r   r   r@  r   getr[   r   rE   r\   rsplitr	  rc   sortedr   range
_call_namer   rb   popset_submoduler"   r  r]   r}   r   )rv   r6  r(  r  r   r   baserJ  	attrs_maprM   orig_fqnr   indexed_call_modulesr/  r   r7  rj   ra  mod_r`  s                      @r7   r  #UnflattenedModule._dispatch_modules  s    %T*++EyyyU___ ii&A&A#&KL.1SjCIIcNsCj	t$++SXsO< ,  $	$Ff}!'sA!6#''-"!!&) % /=.B.B.D*H.45I.JK.JFAC.JLK< 1$s<01A$X1u5C9(+		#/f=FFJJ4P	 2
 ""/	0C\R /E	* **E*BHCsG$$"3		2o..--D"41111&sDJJ7 .	 C- Ls   *I'c                 "    [        U SUUUU5      $ )Nr#   r   r   s        r7   r    UnflattenedModule.print_readable  r   r6   )r   r   r  r  r   r}   r   r  r   r   r   r  rS   r   )r-   r.   r/   r0   r   r   r    r   r=  rF  rR  r   r   r  r   r5   r   r   s   @r7   r#   r#   /  sg     8<|&| $O4| ||!6;zC*88x 
 
r6   r#   r+   r   rs   c                     [        U 5      n [        X5      n[        R                  R	                  UR
                  SSS9Ul        U$ )a  Unflatten an ExportedProgram, producing a module with the same module
hierarchy as the original eager module. This can be useful if you are trying
to use :mod:`torch.export` with another system that expects a module
hierarchy instead of the flat graph that :mod:`torch.export` usually produces.

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

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

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

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

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

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

Input mutations are handled similarly.
outputr   r   r   Ncall_functionc                    > U TL$ rS   r,   )rO  new_nodes    r7   <lambda>5_inplace_buffer_and_input_mutations.<locals>.<lambda>T  s
    1H;Lr6   )nextiterreversedr   r   r   r   r   inputs_to_buffersrc   buffers_to_mutatera   user_inputs_to_mutateinserting_aftercreate_noderA   opsatencopy_defaultr   replace_all_uses_withrD   )r}   r   output_nodereturn_argsr   input_name_to_nodemutation_name_to_input_namerV   
buffer_fqnbuffer_fqn_to_input_namenum_mutationsmutation
input_name
input_nodevuser_outputsr|  s                   @r7   r   r     s   : tHU[[123K>>X%#k.>.>*?1*DDD""1%K %*KK$/D77m3K		4K   #% ,;+L+L+R+R+T +T-!
+T   
 ->>DDF#FMA 	
J//F   #
  &&'L'LM34M/0?
'3
""8,((!5!5!=!=
?UH !++-#$a  . - 	&&x1LM 0" ^45L%(KI # -,s%   1G8G8:G=(HA3H		
H	c           	         U R                    H  nSUR                  ;  a  M  UR                  S   nUR                  5        VVs/ s H!  u  p4SU;   a  UR                  S5      S   OUPM#     nnnUtpgSnU H  n	[	        Xi5      (       a	  US-  nU	nM    O   U[        U5      :  d  M  [        R                  SUU[        U5      U-
  5        [        [        UR                  5       5      S U 5      UR                  S'   M     g s  snnf )Nnn_module_stackr   r   r   zPnn_module_stack fqns %s at node %s do not form a stack! dropping last %d entries)r   r   r  r[   
_is_prefixr   logwarningr   r   rc   )
r}   r   r  r   _tfqnsprev_fqn	next_fqnsnum_valid_indicescurr_fqns
             r7   r   r   \  s    DII-))$56 +113
3 "%CIIcN143 	 
  $!H(--!Q&!#  " s?33KKbO$'88	 ,0_**,-.@/@A,DII'(= 

s   (Dc                 `    [        U 5      [        U5      :  =(       a    US[        U 5       U :H  $ )z2Check whether `candidate` is a prefix of `target`.N)r   )	candidaterM   s     r7   r  r    s+    y>CK'QF3CS^,D	,QQr6   
parent_fqn	child_fqnc                     U S:X  a  U$ U R                  S5      nUR                  S5      nUS [        U5       U:w  a  [        SU SU  S35      eSR                  U[        U5      S  5      $ )Nr   rQ   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.)r[   r   RuntimeErrorr   )r  r  parent_splitchild_splits       r7   _compute_accessorr    s    R##C(L//#&K &S&'<7YK'OPZ| \P P
 	

 88KL 1 3455r6   rO  yc                 j   S[         R                  R                  S[        4S jn[	        U R
                  [         R                  R                  5      (       d   e[	        UR
                  [         R                  R                  5      (       d   eU" U R
                  5      U" UR
                  5      :H  $ )Nr}   rs   c                 h  ^
 / n0 m
S[         4U
4S jjn[        U R                  5       H  u  p4[        R                  " X$R
                  5       Vs/ s H  n[        U5      PM     nnU[        R                  " X$R                  5      R                  5        VVs/ s H  u  pxU SU 3PM     snn-  nUR                  S;   a  UR                  OSn	UR                  U SUR                   SU	 SS	R                  U5       S
35        UT
[        U5      '   M     SR                  U5      $ s  snf s  snnf )Nrs   c                    > [        U [        R                  R                  5      (       a  S[	        T[        U 5         5      -   $ [	        U 5      $ )N%)rd   rA   r   Nodery   r   )r  	nodes_idxs    r7   arg_dump>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  s:    #uxx}}--S2c7!3444s8Or6   =)rz  get_attrr   z: [z](z, )r   )ry   r  r   r   tree_mapr   r   rc   r   rM   r   r   r   )r}   retr  r7  r   r  	args_dumpkeyvaluerM   r  s             @r7   
graph_dump,_check_graph_equivalence.<locals>.graph_dump  s   $&		S 	
 !-GA-3__Xyy-QR-QcS-QIR"(//(KK"H"N"N"P"PJC %q "P I %)GG/L$LT[[RTFJJ!Btwwiq499Y3G2HJK"#Ibh . yy~ Ss   D)D.
)rA   r   r   ry   rd   r}   )rO  r  r  s      r7   _check_graph_equivalencer    sz    %((.. S ( agguxx~~....agguxx~~....agg*QWW"555r6   gmc                     Sn[        U SU 35      (       a  US-  n[        U SU 35      (       a  M  SU 3n[        XU5        U$ )Nr   _spec_r   )r]   r^   )r  specr7  r   s       r7   	_add_specr    sR    	A
"qcl
#
#	Q "qcl
#
#A3<DBdKr6   c                     U R                   R                  [        R                  U45      nU R                   R                  [        R
                  US45      nU$ r   )r}   rz  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r7   _generate_flattenr    sF    hh$$V%8%84'BG&&x'7'7'1FIr6   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rS   )r  r}   r  rz  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r7   _generate_flatten_specr    sB     RD!!$'I88!!)"="=?PQQr6   c                     [        X5      nU R                  R                  U5      nU R                  R                  [        R
                  X45      $ rS   )r  r}   r  rz  r   rU  )r  r   r  r   r  s        r7   _generate_unflattenr    sB     RD!!$'I88!!&"7"7%9KLLr6   r   c                     UR                  S5      Gt p#U HA  n[        XS 5      nUc    g [        U[        R                  R
                  5      (       d    g Un MC     [        XS 5      $ )NrQ   )r[   r   rd   rA   r_   r`   )r   rM   rj   rk   rX   submods         r7   _get_submoduler    s\    \\#&NVD)>&%((//22  3t$$r6   module_to_addcreate_modulec           	         UR                  S5      Gt pE[        U5       H  u  pg[        XS 5      nUcK  Ub  U" SR                  US US-    5      5      nO[        R
                  R                  5       n[        XU5        [        U[        R
                  R                  5      (       d    gUn M     U R                  XR5        g )NrQ   r   F)
r[   r  r   r   rA   r_   r`   r^   rd   
add_module)	r   rM   r  r  rj   rk   r7  rX   r  s	            r7   _add_submoduler    s     \\#&NVV$D)>(&sxxwQ'@A*Cv&&%((//22 % NN5(r6   rj  r5  c                 &    US:X  a  U $ U  SUS-
   3$ )Nr   r   r,   )rj  r5  s     r7   rg  rg    s$     640$qQ00r6   	call_namec                 b    [         R                  " [         R                  " U5      S-   U 5      S L$ )Nz(@\d+)?$)rematchescape)r  rj  s     r7   rT   rT     s%    88BIIdOk19=TIIr6   c                      \ rS rSr SS\R
                  R                  S\\R
                  R                  S4   S\	\\
\\
   \4      S\\
\4   S\\\R
                  R                   \4      4
S	 jjrS
 rS rS rS rS rS rS rS rSrg)_ModuleFramei	  N
flat_graphr   .module_stackr   r+   c                 n  ^ ^^! UT l         UT l        UT l        UT l        UT l        UT l        TT l        UT l        U	T l        U
T l	        ST l
        T R                  S   u  T l        m!n[        T R                  US-   5      T l        T   Ub,  UT l        [        US5      (       a  UR                   O0 T l        O`T R
                  R#                  T R                  [%        [&        R(                  R+                  5       T!S95      T l        TR                   T l        T R                  R,                  T l        0 T l        0 T l        S T l        TGbF  [5        TR                  T R                  5      nUU U!4S jn[7        TR                  UT R                  U5        TR,                  R9                  U5      T l        T R                  T R                     (       aA  T R                  T R                     S   nUR                  R:                  T R                  l        T R                  T R                     R=                  [?        T R                  R                  T R                  R                  T R2                  T R                  US-   T R                  S95        U
R#                  T R                  5      nUGb  T R                  Gb  UR@                  RB                  S	:X  d   eUR@                  RD                  S   nUR@                  RD                  S   nURF                  b   eURF                  c   eT R,                  RI                  S 5         [K        URB                  5       Vs/ s H!  nT R,                  RM                  S
U 35      PM#     nn0 nURF                   H!  nT R,                  RM                  U5      UU'   M#     [O        T R                  [Q        U5      U4UR@                  5      n[S        URT                  5       H  u  nnT R,                  RW                  S[X        RZ                  UU4[]        U[^        5      (       d  UR`                  OSU 3S9n[]        U[^        5      (       a  Mm  UR`                  T R                  ;   d  M  [b        Rb                  " T R                  UR`                     Rd                  5      Ul2        UT R0                  T R                  UR`                     '   M     S S S 5        T R                  R,                  Rg                  T R2                  5         / nURT                   H  n[]        U[^        5      (       a  UR=                  URh                  5        M5  UR`                  T R                  ;  a  UR=                  S 5        Mb  []        U[j        [l        [n        [p        45      (       d   eUR=                  T R                  Rs                  T R                  UR`                     5      5        M     [u        T R                  R                  UUR@                  5      nT R                  R,                  Rw                  [X        RZ                  US45      nT R                  R,                  Rw                  [X        RZ                  US45      n[K        URB                  5       Vs/ s H9  nT R                  R,                  Rw                  [X        RZ                  UU45      PM;     nnURF                   V s0 s H:  n U T R                  R,                  Rw                  [X        RZ                  UU 45      _M<     nn S S S 5        T R2                  c   e[Q        W5      T R2                  l<        WT R2                  l=        g g g s  snf ! , (       d  f       GN= fs  snf s  sn f ! , (       d  f       Nl= f)NFr   r   r~   c                    > TR                   (       a  TR                    SU  3OU nUTR                  ;   a  TR                  U   $ [        [        R                  R                  5       TS9nUTR                  U'   U$ )NrQ   r  )r   created_modulesr!   rA   r   r   )r   r_  r  parentrv   r~   s      r7   r  ,_ModuleFrame.__init__.<locals>.create_module>  sj    06

&**Qse,4/////55*588>>+;C-3$$T*r6   r   )r  parent_moduleparent_call_moduler   call_idxr+      _positional_arg_rz  
_constant_)r   rM   r   r   )>r  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   rg  r  r+   r]   r   rc  r!   rA   r   r   r}   node_mapnode_to_placeholderr  r  r  r   rb   r   _SubmoduleEntryrA  num_childrenchildren_specscontextr  rf  r   r  rD   r  inputsr  r  r  rd   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  rz  r   r   )"rv   r  r   r  r  r  r  r  r  r  r   r+   	num_callsr   r  base_module_framer@  	args_speckwargs_specrJ  	arg_nodeskwarg_nodesr   rD  r  flat_arg_nodeinput_nodesrE  inputs_node	args_nodekwargs_noder7  rV   r~   s"   `      `                         @r7   r   _ModuleFrame.__init__
  st    %
$($.("!2"&"3"3B"7"i#DHHi!m< DK)0)A)ArDJ..22!%((.."2r:DK  DJ[[&&
 =?#% ;?(T^^DH 6==(DKKO&,ll&>&>x&HD#  0$($5$5dnn$Ea$H!'8'?'?'H'H$dnn-44#{{"&++"4"4'+'>'>&];;	 &))$..9	 T[[%<$$11Q666!))88;I#++::1=K$$,,,&&222++D1  %Y%;%;<< JJ**-=cU+CD<   !'//D(,

(>(>t(DK% 02KK9%{3%%	
 !*)*:*: ;HC$(JJ$:$:*'//'- $.c3C#D#D  HH#-cU!3 %; 	%M "#'788 xx4??2-1YYtsxx7P7U7U-V*) 001JK! !< 2B ""33D4K4KL=?&--E!%)9::#**5;;74??:#**40)! . . / 0	       $** KK33DOOEJJ4OP .& 2KK&&%% !KK--;;$${A&6	 #kk//==$${A&6
 #9#9#9:: KK%%33H4D4DyRSnU:   )00	 1 t{{((66 ((;*:  1	  K MV **666+0+;D##(-8D##*k &=  21DK MLsS   ^
(^C;^
A&^
"F^&-A ^-^&=A^!>^&^


^
^&&
^4c                    U R                   S:w  d   SU S35       eUR                  U R                  L d   eU R                  R                  S 5         U R                  R	                  UR
                  UR                  S9nS S S 5        [        R                  " UR                  5      Wl        X R                  U'   g ! , (       d  f       NB= f)Nr   zCannot add placeholder z to root module)	type_expr)
r   r}   r  r  r   r   r   r  r   r  )rv   rO  placeholder_nodes      r7   add_placeholder_ModuleFrame.add_placeholder  s    xx2~K!8?KK~ww$//)))ZZ((.#zz55aff5O / !%		!&& 1&6  # /.s   /B==
Cc                    [         R                  " [        R                  R                  U R
                  UR                  5      n[         R                  " [        R                  R                  U R
                  UR                  5      nU R                  R                  UR                  X#5      n[        R                  " UR                  5      Ul        X@R                  U'   U$ rS   )r   tree_map_onlyrA   r   r  r  r   r   r}   rz  rM   r  r   r  )rv   rO  r   r   r   s        r7   copy_sym_call_function#_ModuleFrame.copy_sym_call_function  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar6   c                 ^   UR                   U R                  L d   eXR                  ;   a  U R                  U   $ U R                  SU S35        XR                  ;   a  U R                  U   $ UR
                  S:X  d&  U R                  R                  U R                  5      c  U R                  U5        U R                  bm  U R                  R                   R                  U R                  5         U R                  R                  SU R                  R                  U5      5        S S S 5        U R                  U   $ UR
                  S:X  Gas  UR                  [         R"                  R$                  R&                  R(                  [         R"                  R$                  R*                  R,                  [         R"                  R$                  R.                  R(                  [         R"                  R$                  R0                  R2                  [         R"                  R$                  R4                  R,                  [         R"                  R$                  R6                  R,                  4;   d5  [9        UR                  S5      (       a:  UR                  R:                  S:X  a   U R=                  U5        U R                  U   $ U R                  R                  U R                  5      b  U R>                  RA                  X5      $ [C        SUR
                   S	U 35      e! , (       d  f       GN= f)
Nzremap_input(r  r   r   rz  r.   	_operatorz(Could not run remap_input() on op type: z
 for node )"r}   r  r  r<  r  r   r   rc  r   r  r  r  r  
insert_argr  rM   rA   r  r  sym_sizerE   rX   r  unbindsumdim_IntListviewdiffr]   r.   r  r   readr  )rv   rO  s     r7   r  _ModuleFrame.remap_input  sG   ww$//)))==##

\!A&'(((++A..DDM!T%;%;%?%?%I%Q   #&&2 [[&&778O8OP++66q$++:Q:QRS:TU Q++A..TT_$HH		''++		##++		%%))		""..		##++		##++ ,//AHH4G4G;4V ''*==####''1= ::??4++:144&
1#N 1 QPs   06L
L,c           	      :  ^ ^ T R                   R                  T R                  S 5        / nT R                  R	                  T R
                  5      nUGb   T R                  Gb  UR                   H  n[        U[        [        [        [        [        45      (       aW  UR                  T R                  ;   a*  UR!                  T R                  UR                     5        Mq  UR!                  S 5        M  [#        SU 35      e   U 4S jm[%        T R&                  [)        U4S jU 5       5      UR*                  5      n[-        T R                  R&                  T R.                  UR*                  5      nUnO/ nT R0                  R3                  5        Hb  nUR4                   HO  nUR                  T R                  ;  d  M  UR!                  U5        UR!                  T R0                  U   5          M`     Md     T R.                  n[7        U5      S:X  a  US   n[        U[8        [:        R<                  R>                  45      (       d   eT R@                  RC                  U5        Uc  g [        U[:        R<                  R>                  5      (       a  URD                  R	                  S5      O*U V	s/ s H  oRD                  R	                  S5      PM     sn	URD                  S'   [7        U5      S:X  a   Uc  UT R                  R0                  US   '   g [G        U5       Hw  u  pUc  M
  [:        R<                  RI                  U5      U
   RJ                  nURD                  R	                  S5      URD                  S'   UT R                  R0                  U'   My     g s  sn	f )Nz'Unsupported data type for output node: c                    > U c  g TR                   U R                     nUTR                  ;   a  TR                  U   $ UTR                  ;   a  TR                  U   $ [	        SU  STR
                   35      e)NzCould not find output node z	. Graph: )r  r   r  r  r  r}   )ry  	seen_noderv   s     r7   get_actual_output_node=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node  sx    > OOFKK8	-==33$":"::33I>>&5fXYtzzlS r6   c              3   4   >#    U  H  nT" U5      v   M     g 7frS   r,   )rU   ry  r#  s     r7   rY   0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s     P<,V44<s   r   r   val)&r  rh  r   r   rc  r  r  r   rd   r   r   r   r   r   r   r  r   r  r  r+   rD   rV  r  r  r  keysusersr   r   rA   r   r  r}   ry  r   r  Proxyr   )rv   orig_outputsr@  ry  tree_out_node
parent_outgraph_outputs	orig_node	user_nodeor7  orig_output	proxy_outr#  s   `            @r7   finalize_outputs_ModuleFrame.finalize_outputs  s     40**..t~~>	 T[[%<#++&&'((	 	 {{doo5$++DOOFKK,HI$++D1&A&J ! ,( 0P<PP""M
 3I""D$;$;Y=O=O3J HUMM!]]//1	!*I ~~T__<$++I6%,,T]]9-EF "1 2 00J=!Q& -a 0-$)>????

-(  -77 ""5)-:;]&&**U#]; 	 |!i&74>DKK  a1"+L"9&!HHNN:6q9>>	(3(8(8(<(<U(C	u%4=$$[1 #: <s   .$Nc                     U R                  SUR                  5       5        U R                  R                  XR                  5      U R
                  U'   XR                  UR                  '   g )Ncopying)r<  format_noder}   	node_copyr  r  r  r   r   s     r7   	copy_node_ModuleFrame.copy_nodeL  sL    

9d..01"jj2249I9IJd%)		"r6   c                    [        U R                  R                  5       H>  u  pU R                  XR                  R                  S5      UR                  5       5        M@     SnU R                  U   nUR                  S:X  a7  U R                  U5        US-  nU R                  U   nUR                  S:X  a  M7  U R                  U5        U R                  R                   H&  nUR                  S:X  d  M  U R                  U5        M(     g )Nr  r   r   r   ry  )
r  r  r   r<  r   rc  r8  r   r:  run_from)rv   r7  r   node_idxs       r7   	run_outer_ModuleFrame.run_outerQ  s     !6!67GAJJq))--(9:D<L<L<NO 8 zz(#gg&NN4 MH::h'D gg&
 	h OO))Dww("t$ *r6   c                 >    U R                   (       a  [        U0 UD6  g g rS   )r  r<  )rv   r   r   s      r7   r<  _ModuleFrame.printd  s    <<4"6" r6   c                    SnU[        U R                  5      :  GaO  U R                  U   nUR                  S:w  d   eU R                  5         U R                  SXR	                  5       5        U R                  U R
                  5        [        U R
                  5      nUR                  S:X  a  US:X  a  U$ U R                  5         U$ [        UR                  R                  S0 5      5      S:X  a  [        SU 35      eUR                  S   nSSK
Jn  [        U5      S:X  a  Xe;   a  U R
                  nOeUR                  S   R                  5        VV	V
s/ s H9  u  nu  pU	U	(       a  U
OS S	U;   a  [        UR                  S	5      S
   5      OS4PM;     nn	nn
US U U R
                  :w  aI  U R                  5         U R                  SU R                  5        U R                  U R                   5        U$ Uc   e[#        U R
                  U5      (       a  Xt   nU R                  SU5        [%        UR                  S   R'                  5       5      U   n[)        U R*                  U R                  U R,                  U R.                  U R0                  U R2                  U U R
                  U/-   UR                  S	5      S   U R4                  5
      R7                  U5      nUS-  nGM  XpR
                  :X  d   eUR                  S:X  a2  U R0                  U R8                     R;                  UR<                  5        U R?                  U5        US-  nU[        U R                  5      :  a  GMN  g g s  sn
n	nf )Nr   r   STEPry  r   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYr   r  	outliningzCreating new stack frame forr  ) r   r   r   r<  r8  r  r4  r   rc  r  (torch._export.passes._node_metadata_hookrE  rc   rE   r[   r   r}   r  r   r(  r  r  r  r  r  r  r   r=  r  r	  rM   r:  )rv   r>  
module_idxr   depthr  rE  node_module_stackrV   r_  r~   next_modulenext_module_keys                r7   r=  _ModuleFrame.run_fromh  s   
TZZ(::h'D77m+++JJLJJvx)9)9);<JJt(()))*Eww("A: $O %%'499==!2B78A="%MdV#TUU"ii(9:O
 O$).A %)$5$5! *.3D)E)K)K)M% *N:D	 "14AGGCL,-q
 *N " % !%(D,=,== %%'

;1

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

9;G #'tyy1B'C'H'H'J"KE"R'OOJJOO%%OO((%%5#))#.q1** (8$  a
 %(9(9999ww*$/33DKK@NN4 MHq TZZ((D%s   A M-)r  r  r  r   r}   r   r+   r   r  r  r  r  r   r  r  r  r  r  r  rS   )r-   r.   r/   r0   rA   r   r   rD   r  r   ry   r   rE   r   r   r   r   r#   r   r  r  r  r4  r:  r?  r<  r=  r5   r,   r6   r7   r  r  	  s     LPb9HHNNb9 UXX]]C'(b9 5hsmS!89:b9  %8 89b9 uxx335FFGHb9H	7)VX>t*
%&#[r6   r  c                       \ rS rSr% \\S'   \R                  R                  \S'   \R                  R                  \S'   \\S'   \\S'   \R                  R                  \S'   Srg	)
r  i  r  r  r  r   r  r+   r,   N)r-   r.   r/   r0   ry   rC   rA   r_   r`   r   r  rE   r5   r,   r6   r7   r  r    s;    O88??"%	HMHHOOr6   r  
orig_graphroot_modulec                 F   0 n[        [        5      n[        [        5      n0 n[        U [	        U R
                  5      UUUUS S/SUR                   Vs0 s H-  nUR                  (       d  M  UR                  UR                  _M/     snUS9R                  5         X44$ s  snf )N)r   Nr   r   )r+   )
r   r   r\   r  rD   r   r   r@  r   r?  )rO  rP  r  r  r  r  r  s          r7   r   r     s    +-J5@5FL&1#&6J24Oj	
 %66	
6 'EIIu&6	

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

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

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

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

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

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

module: GraphModule, potentially containing nested submodules.
inputs_to_state: mapping graph input names to the corresponding key in the state_dict.
scope: tracks where we are in the module hierarchy, so that we can emit the
    right `getattr(self, "foo.bar")` calls, etc.
module_id_to_inputs_removed: records inputs removed by child modules, mapping
    the module object id to the list of placeholder node names in the child module
    that were removed.
Nztorch.nn.Moduler}   r   c                      U R                   S:H  $ )Nr   r   r5  s    r7   r}  _sink_params.<locals>.<lambda>  s    144=#8r6   r  c                      U R                   S:H  $ )Nr   rx  ry  s    r7   r}  rz    s    )>r6   c                 :   > U R                   T[        T5         ;  $ rS   )r   r   )r5  ru  	submodules    r7   r}  rz    s    aff,G9,VVr6   rQ   r   r  T)propagate_meta)#r   r\   r   rb   rc   r  r
   ra   r   r   r   rd   rA   r   r   r   filterr   rM   rD   r   r   r[   r)  r   rg   ri   r  r  r   r  
erase_noder	  r!   r   )r+   r1  rt  ru  r   submod_id_to_inputs_removedrV   r  r}   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitrO  inputs_removed	attr_path
state_attrr|  r}  s      `                  @r7   r  r  x  s   , #*&1#&6#	&z006
76
CDD "??002i&2"I.FN'	'
# 0557DA'*11!4 8 3 FGT*E}EKK(A-**eUXX^^,,,,&8%++FGF [A-T6":N >L!fkk2	  R	]6Q%QVIIDI " @B99O+
!)),Bxx}H#e*%5)I5a!''#,q/5)II%
	 - )3&/ 4  #uN4::<tzz?Q"3u:<0I0CJj5<<9K9K*LMMMM &&~6 ,,Z)9LM 7 &&x&E499% = &+,,vJ''O *J8 76s   7M 
"M%%
M3	)TrS   )r   )gr   r  loggingr  r  collectionsr   
contextlibr   r   dataclassesr   enumr   typingr   r	   r
   r   r   rA   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&   r:   rH   rJ   rg   ri   r_   r`   ry   boolrn   rp   r!   r"   ABCr    r#   r$   r   r   r   r  r  r  r  r   r  r  r  r  r  r  rE   rg  rT   r  r  r   r   r  r   r\   r  r  r   r  r,   r6   r7   <module>r     s   
    	 # %  !  7 7  $ $ $ $ ? ( 3	 	 	 < U U 7 = !  $     ( (" 00ELL%"4"4ehhooEF00xx00 00 	00
 00f	 	U
 U
p-".%((// -"`cgg ,~
 ~
D MQ!!080I!!HE)88>>E))E) 
E)P$NR
6# 6# 6# 6$6 6EHHOO 64%((// C %((..  Rehh""$57HHIR
XX]]RMehh""$57HHIM
XX]]M% % %* AE	)	)) 88??) HcUEHHOO%;<=	)41S 1S 1S 1JS J J J
z zz   $EHHNN $AR $4 GI,HHOO,(,S#X,@C,,?= ?=D	'	'"	' S#c(]#	'(#^ BF	p(HHOOp(#tCy.)p( 9p( "*$sCH}*=!>	p(r6   