
    ^j(                    $   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZ d dlZd dlmc mZ d dlmc mZ d d	lmZ d d
lmZ d dlm Z  d dl!m"Z" 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/m0Z0m1Z1 d dlm2Z2m3Z3 ddl4m5Z5  ejl                  e7      Z8g dZ9 G d de      Z: ed       G d d             Z;da<e
d        Z=	 d^dedej|                  j~                  de@de:deAf
d ZB G d! d"      ZC G d# d$eCej|                  j~                        ZD G d% d&eCej|                  j~                        ZE G d' d(e j                        ZG G d) d*eCej|                  j~                        ZH	 d_d+e d,eGdz  d-eHfd.ZId/ej*                  j                  d0e%d-dfd1ZKd/ej*                  j                  d-e@dz  fd2ZLd3 ZMd4 ZNd5e@d6e@d-e@fd7ZOd8ej|                  j~                  d9ej|                  j~                  fd:ZPd;ej|                  j~                  d-e@fd<ZQd;ej*                  j                  d-ej*                  j                  fd=ZTd;ej*                  j                  eDz  eHz  d-ej*                  j                  fd>ZUd;ej*                  j                  eDz  eHz  d-ej*                  j                  fd?ZVd@ej|                  j~                  de@fdAZW	 d_d@ej|                  j~                  de@dBej|                  j~                  dCee@gej|                  j~                  f   dz  fdDZXdEe@dFeYd-e@fdGZZdHe@dEe@d-eAfdIZ[ G dJ dK      Z\e G dL dM             Z]dNej*                  j                  dOeHfdPZ^	 d`dQej|                  j~                  dRe_e@eYf   dSe@fdTZ` G dU dV      Zad;ej*                  j                  dOeHdWe_e@ebe@   f   fdXZcdY Zd	 d_d+ej|                  j~                  dZe_e@eee@   f   d[eee@   d\e_eYebe@   f   dz  fd]Zfy)a    N)defaultdict)Callable)contextmanager)deepcopy)	dataclass)Enum)Anycast)FakeScriptObject)is_opaque_value)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y)	_AttrKind	parameterbufferconstantmoduleN)__name__
__module____qualname__	PARAMETERBUFFERCONSTANTMODULE     a/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/export/unflatten.pyr%   r%   3   s    IFHFr2   r%   T)frozenc                   N    e Zd ZU dZej
                  ed<   eed<   eed<   eed<   y)	_TensorIDzJCustom tensor identifier containing storage, stride, and size information.untyped_storagestridesizestorage_offsetN)	r*   r+   r,   __doc__torchUntypedStorage__annotations__tupleintr1   r2   r3   r6   r6   :   s!    T)))M
Kr2   r6   c               #   8   K   t         } da 	 d  | a y # | a w xY wwNF)RUN_WITH_INTERPRETER)old_flags    r3   _disable_interpreterrE   G   s'      $H ('xs   	 from_obj	to_moduletarget	attr_kind
persistentc                   	 |j                  d      ^ }}|h}|D ]|  	t               }|D ]i  }t        |	      s)t        |	t        j
                  j                                |j                  	fd|j                  j                         D               k |}~ |D ]  }|t        j                  k(  rNt        | t        j
                  j                        st        dt        |              |j!                  ||        e|t        j"                  k(  rFt        | t        j$                        st        dt        |              |j'                  || |       |t        j(                  k(  rvt        | t*              rt        d      t        | t        j$                  t        j,                  f      s"t/        |       st        dt        |              t        |||        G|t        j0                  k(  s\t        | t        j
                  j                        st        dt        |              t        |||         y )	N.c              3   @   K   | ]  \  }}t        |      r|  y wN)_is_call_name).0kt_callitems      r3   	<genexpr>z_assign_attr.<locals>.<genexpr>h   s'      Av D) s   z9expected torch.nn.Parameter for PARAMETER attr_kind, got z0expected torch.Tensor for BUFFER attr_kind, got )rJ   z2FakeScriptObject should only exist during tracing.zVexpected torch.Tensor, torch.ScriptObject, or opaque type for CONSTANT attr_kind, got z3expected torch.nn.Module for MODULE attr_kind, got )splitsethasattrsetattrr<   nnModuleupdate_modulesitemsr%   r-   
isinstance	ParameterAssertionErrortyperegister_parameterr.   Tensorregister_bufferr/   r   ScriptObjectr   r0   )
rF   rG   rH   rI   rJ   prefixfield
to_modulestsrS   s
            @r3   _assign_attrrj   T   s    \\#&NVU J 
#&5# 	I9d+	4):;II !*!3!3!9!9!; 	 

   "0		+++h(:(:;$OPTU]P^O_`  ((9)***h5$FtH~FVW  %%eX*%M),,,($45$H  LL&& &h/$lmqrzm{l|}  Iuh/)***h8$I$x.IYZ  Iuh/E"0r2   c                   0    e Zd ZU edz  ed<   dedz  fdZy)_SubmoduleBaseN_tyreturnc                     | j                   S )z
        Subclass of this class - InterpreterModule, InterpreterModuleDispatcher, represents
        corresponding model in eager model. To get this type information for those modules
        in eager model we need to use this method.
        )rm   selfs    r3   	type_namez_SubmoduleBase.type_name   s     xxr2   )r*   r+   r,   strr>   rr   r1   r2   r3   rl   rl      s    	tO3: r2   rl   c                        e Zd ZU dZej
                  j                  dz  ed<   	 d
dej
                  j                  de	dz  f fdZ
d Zd Z	 	 	 	 dd	Z xZS )r    zA module that uses torch.fx.Interpreter to execute instead of the usual
    codegen that GraphModule uses. This provides better stack trace information
    and makes it easier to debug execution.
    Ngraph_modulegraphtyc                 v    t         |           || _        || _        | | j                  _        t
        | _        y rN   )super__init__rv   rm   owning_modulerC   _run_with_interpreter)rq   rv   rw   	__class__s      r3   rz   zInterpreterModule.__init__   s2    
 	
#'

 %9"r2   c                    | j                   t        d      t               sSt        j                  j                         s| j                  s) t        | j                         j                  | g|i S rt        |      }| j                  t        |      d  }|j                  fd|D               t        |      t              k7  r#t        dt        |       dt                     t        |      t        | j                        k7  r-t        dt        |       dt        | j                               t        |      }t        j                  j                  | | j                         j"                  |dd	iS )
Nz&Didn't finalize this InterpreterModulec              3   0   K   | ]  }|v r|     y wrN   r1   )rP   
kwarg_namekwargss     r3   rT   z,InterpreterModule.forward.<locals>.<genexpr>   s&       "!V+ :& s   zkwarg_names length z does not match kwargs length zarg_list length z! does not match arg_names length rv   enable_io_processingF)ru   r`   r   r<   compileris_dynamo_compilingr|   ra   forwardlist	arg_nameslenextendr?   fxInterpreterrv   run)rq   argsr   arg_listkwarg_namess     `  r3   r   zInterpreterModule.forward   se   $ !IJJ%'NN..08R8R 34))*224I$I&II
  :"nnS]_=  &1   {#s6{2(-c+.>-??]^abh^i]jk  x=C$77(*3x=/9Z[^_c_m_m[nZop  X88''DJJ'?CC,1 r2   c                 T   t         j                  j                  | | j                        | j                  d<   | j                  j                          g | _        | j                  j                  D ]7  }|j                  dk(  s| j                  j                  |j                         9 y )Nru   placeholder)r<   r   GraphModulerv   __dict__lintr   nodesopappendrH   rq   nodes     r3   finalizezInterpreterModule.finalize   sy     ).(<(<T4::(Nn%

 JJ$$ 	3Dww-'%%dkk2	3r2   c                 "    t        | d||||      S )Nr    r   rq   print_outputinclude_strideinclude_devicecoloreds        r3   print_readablez InterpreterModule.print_readable   #     
 	
r2   rN   TFFF)r*   r+   r,   r;   r<   r   r   r>   Graphrs   rz   r   r   r   __classcell__r}   s   @r3   r    r       se    
 ((&&--
 	:xx~~	: $J	:(T3& 
r2   r    c                   T     e Zd ZdZdee   dee   f fdZd Z	d Z
	 	 	 	 ddZ xZS )	r!   z
    A module that carries a sequence of InterpreterModules corresponding to
    a sequence of calls of that module. Each call to the module dispatches
    to the next InterpreterModule, and wraps back around after the last.
    attrscall_modulesc           	          t         |           |st        d      |d   j                  | _        |D ]  }t	        | |t        |d   |              |d   j                  | _        || _        d| _        y )Nzcall_modules must not be emptyr   )	ry   rz   r`   r\   rX   getattrrm   _call_modules
_num_calls)rq   r   r   accessorr}   s       r3   rz   z$InterpreterModuleDispatcher.__init__  sv     !ABB$Q00 	HHD(GLOX$FG	H?&&)r2   c                     | j                   | j                     }| j                  dz   t        | j                         z  | _        	  ||i |S # t        $ r	 d| _         w xY w)Nr   r   )r   r   r   	Exception)rq   r   r   call_modules       r3   r   z#InterpreterModuleDispatcher.forward  sd    ((9??Q.#d6H6H2II	/// 	DO	s   A Ac                     | j                   S rN   )r   rp   s    r3   r   z(InterpreterModuleDispatcher.call_modules  s    !!!r2   c           	          | j                   D cg c]  }|j                  ||||       }}dj                  |      S c c}w )N
)r   r   join)rq   r   r   r   r   modoutputss          r3   r   z*InterpreterModuleDispatcher.print_readable  sV     ))
  	
 
 yy!!
s   =r   )r*   r+   r,   r;   rV   rs   r   r    rz   r   r   r   r   r   s   @r3   r!   r!      sB    	c#h 	d;L6M 	"
 "r2   r!   c                       e Zd ZdZej
                  	 	 ddej                  dej                  dee	   de
ee	f   dz  de	dz  dee	   fd	       Zdee   fd
Zy)r   zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    Ntarget_spec
input_spec
input_argsmetadataobjrn   c                      y)z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr1   )rq   r   r   r   r   r   s         r3   adaptzFlatArgsAdapter.adapt5  s     	r2   c                     g S )z>Returns a list of paths that are used to access the flat args.r1   rp   s    r3   get_flat_arg_pathsz"FlatArgsAdapter.get_flat_arg_pathsA  s    	r2   )NN)r*   r+   r,   r;   abcabstractmethodpytreeTreeSpecr   r	   dictrs   r   r   r1   r2   r3   r   r   0  s     	 +/	__	 OO	 I		
 sCx.4'	 4Z	 
c	 	DI r2   r   c                   f     e Zd Z	 ddededz  f fdZd Zd Zd Zd Z	d	 Z
d
 Z	 	 	 	 ddZ xZS )r"   Nexport_moduleflat_args_adapterc           	      d  , t         -|           |j                  j                  t	        d      d }|j
                  D cg c]  }|j                   }}|d   dk7  rt        d|d         t        |j                        }t        |j                        | _        t        j                  j                         | _	        | | j                  _        t        |j
                        | _        || _        |j                  j                   | _        | | j                   d<   d| _        t$        | _        t)        || j                         t+        |       t-        |      | _        t1               | _        t5        ||       \  }}| j2                  j7                  |j9                                t;        |j<                  | |       |j>                  | _        g | _         |jB                  }	tE               }
i }| j                  jF                  D ]  }|	|   } ||      |vrBt        jH                  jK                  |jM                         |jN                        | ||      <   tQ        | ||         | |tR        jT                  	       |
jW                  |        tE        | j                  jX                        }tE               }i }| j                  jZ                  D ]  }||v rd}|j\                  |   }nd
}|	|   } ||      |vr|jM                         |f| ||      <   tQ        | ||         d   | |tR        j^                  |       |jW                  |        |	ja                         D ]  \  }}||
v s||v rd} ||      |v s$tc        |t        jH                  jJ                        sd
}|rB ||      |vr|d
f| ||      <   tQ        | ||         d   | |tR        j^                  d
        ||      |vr|| ||      <   tQ        | ||         | |tR        jT                  	        i }|j\                  ja                         D ]l  \  }} ||      |vr5tc        |t        jd                        r|jM                         }|| ||      <   | ||         }tQ        || |tR        jf                  	       n ti        tj              ,tE               },fd}tE               }| j                  jl                  D ]  }|jn                  tp        jT                  k(  s)|jn                  tp        j^                  k(  r|jr                  rtu        |jv                  d      s!t        dty        |jv                               tc        |jz                  t|              s!t        dty        |jz                                | ||jB                  |jz                           |jv                  j~                  |jz                         |jW                  |jz                         |jW                  |jz                         9|jn                  tp        j^                  k(  r|jr                  r<|jn                  tp        j                  k(  s|jn                  tp        j                  k(  stu        |jv                  d      s.t        d|jn                   dty        |jv                               tc        |jz                  t|              s.t        d|jn                   dty        |jz                                | ||j\                  |jz                           |jv                  j~                  |jz                         |jW                  |jz                          |j\                  ja                         D ]U  \  }}||vs ||      }|,vrt        d|d      ,|   d   \  } }! ||| |       |jW                  jz                         W |jB                  ja                         D ],  \  }}||vs ||      }"|",vr,|"   d   \  } }! ||"| |       . i }#,j9                         D ]#  }$|$D %cg c]  }%|%d   	 }&}%|$D ]
  \  }'}!|&|#|'<    % t        | |#g        t        |j9                               }(|D cg c]	  }||(vs| }}| j                  |(|       |D cg c]	  }d|vs| }}| j                  j                  D )cg c]  })|)j                  dk(  s|) c})| _G        d
| _H        t        |      D *ci c]  \  }*}||*
 }+}*}| j                  d      D ]  \  }}!||+vst        |+      |+|<    t        | |+       | j                  j                          | j                          y c c}w c c}%w c c}w c c}w c c})w c c}}*w )Nz%Unflattening on JointExportModule NYIc                     t        | t        j                        rGt        | j	                         | j                         | j                         | j                               S t        |       S )z8Returns _TensorID dataclass for tensors, otherwise id().)r7   r8   r9   r:   )	r^   r<   rc   r6   r7   r8   r9   r:   id)r   s    r3   _idz'UnflattenedModule.__init__.<locals>._idP  sR    #u||, $'$7$7$9::<#&#5#5#7	  c7Nr2   r    z+expected first fqn to be empty string, got unflattened_moduleF)requires_grad)rI   T)rI   rJ   c                 6    |    }|j                  ||f       y rN   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r3   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_map  s     "6*Ii56r2   namez-expected s.arg to have 'name' attribute, got z!expected s.target to be str, got z1expected s.arg to have 'name' attribute for kind z, got z%expected s.target to be str for kind z	constant z id not found in consts_mapr   @r   remove_duplicate)Ory   rz   graph_signaturebackward_signature
ValueErrormodule_call_graphfqnr`   r   rv   r<   r   r   r{   r   ru   metaadaptedrC   r|   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_root_module_typerm   _IValsivals_outline_submodulesr[   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrV   
parametersrY   r_   cloner   rj   r%   r-   addnon_persistent_buffersbuffers	constantsr.   r]   r^   rc   r/   r   r   input_specskindr   rJ   rW   argra   rH   rs   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   r   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   r   ).rq   r   r   r   entryfqn_listexport_graphseen_modules
seen_attrsr   assigned_paramsid_to_paramr   paramr   assigned_buffersid_to_bufferrJ   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.                                               @r3   rz   zUnflattenedModule.__init__G  s+	   
 	((;;GDEE		 ,9+J+JK%EIIKKA;" =hqk_M    3 34'(E(EFXX^^%
#'

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

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

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

 ?s6   ff	f%f	f"f")f'>f'f,c                     | j                         D ]d  \  }}t        |dz          t        |d      s!t        |j                  t
        j                  j                        sPt        |j                         f y )N:rv   )r  printrW   r^   rv   r<   r   r   )rq   r   r   s      r3   _print_graphzUnflattenedModule._print_graphT  sU    **, 	!HC#)sG$CIIuxx~~)Ncii 	!r2   c                    | j                   d   j                  }||j                  k(  r|S | j                  t	        d      | j                  j                  |j                  ||| j                  |      }t        |      |j                  j                  k7  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 Adapted: z 
Exported module: )	r   	signaturein_specr   	TypeErrorr   r   r   
num_leaves)rq   	flat_argsr)  inputr(  s        r3   _adapt_flat_argsz"UnflattenedModule._adapt_flat_argsZ  s    **1-77	i'''!!)O 
 ..44%--"$ 5 I 9~!2!2!=!==  #I/ 0((1(9(9(D(D'EG 
 r2   c           	      n   | j                   d   j                  }|}|rt        ||j                        }t	        j
                  ||f      \  }}|D cg c]  }|d   	 }}t               r|S ||j                  k7  rN| j                  s(t        d| dd|j                          t        d       | j                  |||      }d| _        | j                  rddlm}	 | j                  du r| j                  r| j                  j                         ng }
|
r:t        |
      t        |      k7  r#t!        d	t        |
       d
t        |             t#        |      D cg c]&  \  }}t%        |      t'        |
r|
|   nd      f|f( }}}n|} |	| j(                  || j*                         |S c c}w c c}}w )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_graphzflat_arg_paths length z! does not match flat_args length )idxz<unknown location>)r   )r   r(  r   r)  r   tree_flatten_with_pathr   r   r%  r.  r   torch._export.utilsr0  r   r   r   r`   r  r   r   r   r   )rq   r   r   r(  reordered_kwargsflat_args_with_pathr)  xr,  r0  flat_arg_pathsr1  r   new_flat_args_with_paths                 r3   process_forward_inputsz(UnflattenedModule.process_forward_inputsu  s   **1-77	!-fi6G6GH'-'D'D#$(
$W $77aQqT7	7!#i'''<<''.ir301B1B0CD
 MN--i$GIDL'' O||t# -- **==? 
 "c.&9S^&K(0^1D0EEfgjktgufvw  %.i$8+ !S (C0&#1 &4C%8%9 
+' + +>'.'')@$BXBX i 8>+s   F,+F1c                     | j                   |i |}| j                  d   j                  }t               r_t	        j
                  j                  | | j                        j                  |ddi}t        |t              rt        |      dk(  r|d   S |S t        j                  j                         s| j                  s' t        | j                         j"                  | g| }n:t	        j
                  j                  | | j                        j                  |ddi}t%        j&                  ||j(                        S )Nr   r   r   Fr   )r9  r   r(  r   r<   r   r   rv   r   r^   r?   r   r   r   r|   ra   ru   r   r   tree_unflattenout_spec)rq   r   r   r,  r(  
return_valtree_outs          r3   r   zUnflattenedModule.forward  s   /D//@@	**1-77	!#--d$**-EII16J *e,ZA1E!!}$>>--/t7Q7Q6tD--.66tHiHHxx++D

+CGG16H $$Xy/A/ABBr2   c                     t         j                  j                  | | j                        | j                  d<   | j                  j                          y )Nru   )r<   r   r   rv   r   r   rp   s    r3   r   zUnflattenedModule.finalize  s3    (-(<(<T4::(Nn%

r2   c                    t        t              | j                  D ]  }|j                  s|j                  s|j                  }t        | |j                  ||            }d|v r|j                  d      n|dg\  }}|   j                  t        |      |f        t        t              }|D ]D  }	d|	v r*|	j                  dd      \  }
}||
   j                  |       1|d   j                  |	       F j                         D ]  \  }
}t        |      D cg c]  \  }}|	 }}}t        |      dkD  s2t!        t        |            D ]O  }t#        |
|dz         }||vs|j                  d      ^ }}t%        | |      j&                  j)                  |       Q | j+                  |
t-        ||
   |              fd}| j/                  d      D ]y  \  }}t1        |d	      r |||j2                         &t1        |d
      s3|j4                  D ]8  }t1        |d	      st7        dt9        |              |||j2                         : { yc c}}w )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   0rL   r   r   c                     |j                   D ]G  }|j                  dk(  s|j                  j                  d      d   }| r|  d| n|}|v sA||_        I y )Nr   r   r   rL   )r   r   rH   rU   )rf   rv   r   r   pathcalled_moduless        r3   elide_call_indicesz?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  s^     *77m+++++C03C06fXQse,CD~-&)*r2   Fr   rv   r   z-expected mod_ to have 'graph' attribute, got N)r   r   r   r   r(  r   getrU   r   r@   rV   rsplitr   r]   sortedr   range
_call_namer   r\   popset_submoduler!   r  rW   rv   r   r`   ra   )rq   r   r  r  r   r   baser1  	attrs_maprH   orig_fqnr   indexed_call_modulesr  r   r!  rf   rE  mod_rD  s                      @r3   r   z#UnflattenedModule._dispatch_modules  sP    %T*++ 	=EyyU__ ii&=&A&A#s&KL.1SjCIIcNsCj	ct$++SXsO<	=  $	$ 	*Ff}!'sA!6$(#''-"!!&)	* /=.B.B.D 	*H*.45I.JKFAsCKLK< 1$s<01 QA$Xq1u5C"99(+		#/f=FFJJ4P	Q
 ""/	(0C\R		* **E*B 		8HCsG$"3		2o.-- 8D"41,KDQUJ<X  'sDJJ78			8- Ls   Ic                 "    t        | d||||      S )Nr"   r   r   s        r3   r   z UnflattenedModule.print_readable  r   r2   rN   r   )r*   r+   r,   r   r   rz   r&  r.  r9  r   r   r   r   r   r   s   @r3   r"   r"   F  s\     59K&K +T1KZ!6>@C*;8~ 
r2   r"   r)   r   rn   c                     t        |       } t        | |      }t        j                  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disabler9  )r)   r   ms      r3   r#   r#     sO    , #6*F&"34A  %}}44	  ;  5  A Hr2   rv   r   c                 v   t        t        t        | j                                    }|j                  dk7  st        |j                        dk7  r/t        d|j                  dt        |j                         d      |j                  d   }| j                  D ci c]  }|j                  dk(  s|j                  |! }}i }|j                  j                         D ci c]  \  }}||
 }	}}|j                  j                         D ci c]  \  }}||	|    }}}|j                  |j                         t        |      }
|d|
 D ]  }||j                     }||   }| j                  |      5  | j                  d	t         j"                  j$                  j&                  j(                  ||f      |j*                  j                         D ]  \  }}|j*                  |<    	 ddd       |j-                  fd
        t/        ||
d       }|f|_        yc c}w c c}}w c c}}w # 1 sw Y   HxY w)a^  Transform buffer and input mutations from their functionalized form
    into copy_ nodes in the graph.

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

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

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

    Input mutations are handled similarly.
    outputr   z8expected output node with op='output' and 1 arg, got op=z with z argsr   r   Ncall_functionc                     | uS rN   r1   )r6  new_nodes    r3   <lambda>z5_inplace_buffer_and_input_mutations.<locals>.<lambda>  s    1H;L r2   )nextiterreversedr   r   r   r   r`   r   inputs_to_buffersr]   buffers_to_mutater[   user_inputs_to_mutateinserting_aftercreate_noder<   opsatencopy_defaultr   replace_all_uses_withr?   )rv   r   output_nodereturn_argsr   input_name_to_nodemutation_name_to_input_namerQ   
buffer_fqnbuffer_fqn_to_input_namenum_mutationsmutation
input_name
input_nodevuser_outputsr]  s                   @r3   r   r   @  sD   : tHU[[123K~~!S)9)9%:a%?F{~~FXX^_bcncscs_t^uuz{
 	
 ""1%K %*KK 477m3K		4  #% ,;+L+L+R+R+T '!Z
A   
 ->>DDF#Az 	
#J//# #
  &&'L'LM34M/ N0?
'
3
""8, 	%((!5!5!=!=
H?UH !++- %1#$a %	% 	&&x1LMN" ]^45L%(KI #	% 	%s%   H1H!H#H)4A3H//H8	c                     | j                   D ]>  }d|j                  vr|j                  d   j                         D ]  \  }}|r	|c c S  @ y )Nnn_module_stack)r   r   r   )rv   r   rC  rw   s       r3   r   r     sW     DII-		"34;;= 	HD"			 r2   c           	         | j                   D ]  }d|j                  vr|j                  d   }|j                         D cg c]  \  }}d|v r|j                  d      d   n|! }}}|^}}d}|D ]  }	t	        ||	      r|dz  }|	} n |t        |      k  st        j                  d||t        |      |z
         t        t        |j                               d |       |j                  d<    y c c}}w )Nry  r   r   r   zPnn_module_stack fqns %s at node %s do not form a stack! dropping last %d entries)r   r   r   rU   
_is_prefixr   logwarningr   r   r]   )
rv   r   ry  r   _tfqnsprev_fqn	next_fqnsnum_valid_indicescurr_fqns
             r3   r   r     s       DII-))$56 +113
R "%CIIcN14
 
  $9! 	H(H-!Q&!# 	 s?33KKbO$'88	 ,0_**,-.@/@A,DII'(= 

s   $C2c                 V    t        |       t        |      k  xr |dt        |        | k(  S )z2Check whether `candidate` is a prefix of `target`.N)r   )	candidaterH   s     r3   r{  r{    s+    y>CK'QF3CS^,D	,QQr2   
parent_fqn	child_fqnc                     | dk(  r|S | j                  d      }|j                  d      }|d t        |       |k7  rt        d| d|  d      dj                  |t        |      d        S )Nr   rL   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.)rU   r   RuntimeErrorr   )r  r  parent_splitchild_splits       r3   _compute_accessorr    s    R##C(L//#&K &S&'<7YK'OPZ| \P P
 	

 88KL 1 3455r2   r6  yc                    dt         j                  j                  dt        fd}t	        | j
                  t         j                  j                        s!t        dt        | j
                               t	        |j
                  t         j                  j                        s!t        dt        |j
                                || j
                         ||j
                        k(  S )Nrv   rn   c                 T  
 g }i 
dt         f
fd}t        | j                        D ]  \  }}t        j                  ||j
                        D cg c]  }t        |       }}|t        j                  ||j                        j                         D cg c]  \  }}| d|  c}}z  }|j                  dv r|j                  nd}	|j                  | d|j                   d|	 dd	j                  |       d
       |
t        |      <    dj                  |      S c c}w c c}}w )Nrn   c                     t        | t        j                  j                        rdt	        t        |                z   S t	        |       S )N%)r^   r<   r   Noders   r   )r   	nodes_idxs    r3   arg_dumpz>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  s7    #uxx}}-S2c7!3444s8Or2   =)r[  get_attrr   z: [z](z, )r   )rs   r  r   r   tree_mapr   r   r]   r   rH   r   r   r   )rv   retr  r!  r   r   	args_dumpkeyvaluerH   r  s             @r3   
graph_dumpz,_check_graph_equivalence.<locals>.graph_dump  s   $&		S 	
 !- 		$GAt-3__Xtyy-QRcSRIR"(//(DKK"H"N"N"PC %q  I %)GG/L$LT[[RTFJJ!Btwwiq499Y3G2HJK"#Ibh		$ yy~ Ss   DD$
z+expected x.graph to be torch.fx.Graph, got z+expected y.graph to be torch.fx.Graph, got )r<   r   r   rs   r^   rv   r`   ra   )r6  r  r  s      r3   _check_graph_equivalencer    s    %((.. S * agguxx~~.9$qww-I
 	
 agguxx~~.9$qww-I
 	
 agg*QWW"555r2   gmc                 v    d}t        | d|       r|dz  }t        | d|       rd| }t        | ||       |S )Nr   _spec_r   )rW   rX   )r  specr!  r   s       r3   	_add_specr    sN    	A
"qcl
#	Q "qcl
#A3<DBdKr2   c                     | j                   j                  t        j                  |f      }| j                   j                  t        j
                  |df      }|S )Nr   )rv   r[  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r3   _generate_flattenr    sF    hh$$V%8%84'BG&&x'7'7'1FIr2   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S rN   )r  rv   r  r[  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r3   _generate_flatten_specr    sF     RD!!$'I88!!)"="=i?PQQr2   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S rN   )r  rv   r  r[  r   r;  )r  r   r  r   r  s        r3   _generate_unflattenr    sF     RD!!$'I88!!&"7"7%9KLLr2   r   c                     |j                  d      ^ }}|D ];  }t        | |d       }| y t        |t        j                  j
                        s y |} = t        | |d       S )NrL   )rU   r   r^   r<   rY   rZ   )r   rH   rf   rg   rS   submods         r3   _get_submoduler    sd    \\#&NVU 	dD)>&%((//2	 3t$$r2   module_to_addcreate_modulec           	      z   |j                  d      ^ }}t        |      D ]  \  }}t        | |d       }|K| |dj                  |d |dz                }nt        j
                  j                         }t        | ||       t        |t        j
                  j                        s y|}  | j                  ||       y )NrL   r   F)
rU   r  r   r   r<   rY   rZ   rX   r^   
add_module)	r   rH   r  r  rf   rg   r!  rS   r  s	            r3   _add_submoduler  %  s     \\#&NVUV$ 4dD)>(&sxxwQ'@A*Cv&&%((//2 NN5-(r2   rM  r  c                 $    |dk(  r| S |  d|dz
   S )Nr   r   r1   )rM  r  s     r3   rJ  rJ  ?  s$     640$qQ00r2   	call_namec                 ^    t        j                  t        j                  |      dz   |       d uS )Nz(@\d+)?$)rematchescape)r  rM  s     r3   rO   rO   E  s%    88BIIdOk19=TIIr2   c                      e Zd Z	 ddej                  j
                  deej                  j                  df   deee	e	dz  e
f      dee	ef   dej                  j                  ez  dz  f
dZd	 Zd
 Zd ZddZd Zd Zd Zd Zd Zy)_ModuleFrameN
flat_graphr   .module_stackr   r)   c                 .   ! | _         | _        | _        | _        | _        | _         _        | _        |	 _        |
 _	        d _
         j                  d   \   _        !}t         j                  |dz          _          |'| _        t        |d      r|j                   ni  _        nc j
                  j#                   j                  t%        t&        j(                  j+                         !             _        j                    _         j                  j,                   _        i  _        i  _        d  _        Ct5        j                   j                        } !fd}t7        j                  | j                  |       j,                  j9                  |       _         j                   j                     rA j                   j                     d   }|j                  j:                   j                  _         j                   j                     j=                  t?         j                  j                   j                  j                   j2                   j                  |dz    j                               |
j#                   j                        }|E j                  7|j@                  jB                  d	k7  r"tE        d
|j@                  jB                         |j@                  jF                  tH        ur"tE        d|j@                  jF                         |j@                  jK                         \  }}|jF                  tH        urtE        d|jF                         |jF                  tL        urtE        d|jF                          j,                  jO                  d       5  tQ        |jB                        D cg c]   } j,                  jS                  d|       " }}i }|jT                  D ]   } j,                  jS                  |      ||<   " tW         j                  tI        |      |f|j@                        }tY        |jZ                        D ]  \  }} j,                  j]                  dt^        j`                  ||ftc        |td              s|jf                  nd|       }tc        |td              re|jf                   j                  v s~ti        jh                   j                  |jf                     jj                        |_5        | j0                   j                  |jf                     <    	 d d d         j                  j,                  jm                   j2                        5  g }|jZ                  D ]  }tc        |td              r|j=                  |jn                         /|jf                   j                  vr|j=                  d        Ytc        |tp        tr        tt        tv        f      stE        dtG        |             |j=                   j                  jy                   j                  |jf                                   t{         j                  j                  ||j@                        } j                  j,                  j}                  t^        j`                  |df      } j                  j,                  j}                  t^        j`                  |df      }tQ        |jB                        D cg c]8  } j                  j,                  j}                  t^        j`                  ||f      : }}|jT                  D  ci c]9  } |  j                  j,                  j}                  t^        j`                  || f      ; }} d d d         j2                  tE        d      tI               j2                  _?         j2                  _@        y y y c c}w # 1 sw Y   xY wc c}w c c} w # 1 sw Y   jxY w)NFr   r   rw   c                     j                   rj                    d|  n| }|j                  v rj                  |   S t        t        j                  j                               }|j                  |<   |S )NrL   r  )r   created_modulesr    r<   r   r   )r   rC  r  parentrq   rw   s      r3   r  z,_ModuleFrame.__init__.<locals>.create_module  sj    06

&**Qse,4/////55*588>>+;C-3$$T*r2   r   )r  parent_moduleparent_call_moduler   call_idxr)      z)expected in_spec to have 2 children, got z'expected in_spec.type to be tuple, got z)expected args_spec.type to be tuple, got z*expected kwargs_spec.type to be dict, got _positional_arg_r[  
_constant_)r   rH   r   r   z_expected input to be TensorArgument, SymIntArgument, SymBoolArgument, or SymFloatArgument, got z#parent_call_module must not be None)Ar  r   
seen_nodesr  r  r  r  r  	module_idr   verboser   rJ  r  r)   rW   r   rF  r    r<   r   r   rv   node_mapnode_to_placeholderr  r  r  r   r\   r   _SubmoduleEntryr)  num_childrenr`   ra   r?   childrenr   re  rI  r   contextr  r  inputsrf  r  r  r^   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  r[  r   r   )"rq   r  r   r  r  r  r  r  r  r  r   r)   	num_callsr   r  base_module_framer(  	args_speckwargs_specr1  	arg_nodeskwarg_nodesr   r,  r   flat_arg_nodeinput_nodesr-  inputs_node	args_nodekwargs_noder!  rQ   rw   s"   `      `                         @r3   rz   z_ModuleFrame.__init__K  s    %
$($.("!2"&"3"3B"7"i#DHHi!m< DK)0)ArDJ..22!%((.."2r:DK  DJ[[&&
 =?#% 8<(T^^DH 6==(DKKO&,ll&>&>x&HD#  0$($5$5dnn$Ea$H!'8'?'?'H'H$dnn-44#{{"&++"4"4'+'>'>&];;	 &))$..9	 T[[%<  --2$?	@Q@Q@^@^?_`    %%U2$=i>O>O>T>T=UV  &/%6%6%?%?%A"I{~~U*$?	?OP  t+$@AQAQ@RS  ++D1   %Y%;%;< JJ**-=cU+CD	  !'// ED(,

(>(>t(DK%E2KK9%{3%%	
 !*)*:*: ; HC$(JJ$:$:*'//'- $.c3C#D  HH#-cU!3 %; 	%M "#'78 xx4??2-1YYtsxx7P7U7U-V*) 001JK!B ""33D4K4KL .:<&-- E!%)9:#**5;;74??:#**40)! . . / 0	  #1#MMQRW[M![#  $** KK33DOOEJJ4OP'. 2KK&&%% !KK--;;$${A&6	 #kk//==$${A&6
 #9#9#9: KK%%33H4D4DyRSnU	  )00	  t{{((66 ((;*:  S.^ &&.$%JKK+0+;D##(-8D##*O &= ( LS. .sQ   ;_4%_/8C1_4*A#_4F`=``">` `/_44_>
``c                    | j                   dk(  rt        d| d      |j                  | j                  urt        d      | j                  j	                  d       5  | j                  j                  |j                  |j                        }d d d        t        j                  |j                        _	        || j                  |<   y # 1 sw Y   =xY w)Nr   zCannot add placeholder z to root module6expected x.graph to be flat_graph, got different graph)	type_expr)r   r`   rv   r  r  r   r   ra   r  r   r  )rq   r6  placeholder_nodes      r3   add_placeholderz_ModuleFrame.add_placeholder  s    88r> #:1#_!MNN77$//) H  ZZ((. 	P#zz55aff5O	P !%		!&& 1&6  #	P 	Ps   2CCc                    t        j                  t        j                  j                  | j
                  |j                        }t        j                  t        j                  j                  | j
                  |j                        }| j                  j                  |j                  ||      }t        j                  |j                        |_        || j                  |<   |S rN   )r   tree_map_onlyr<   r   r  r  r   r   rv   r[  rH   r  r   r  )rq   r6  r   r   r   s        r3   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_function  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar2   c                 H   |j                   | j                  urt        d      || j                  v r| j                  |   S | j	                  d| d       || j
                  v r| j
                  |   S |j                  dk(  s%| j                  j                  | j                        | j                  |       | j                  m| j                  j                   j                  | j                        5  | j                  j                  d| j                  j                  |             d d d        | j
                  |   S |j                  dk(  rk|j                   t"        j$                  j&                  j(                  j*                  t"        j$                  j&                  j,                  j.                  t"        j$                  j&                  j0                  j*                  t"        j$                  j&                  j2                  j4                  t"        j$                  j&                  j6                  j.                  t"        j$                  j&                  j8                  j.                  fv s/t;        |j                   d      r9|j                   j<                  dk(  r | j?                  |       | j                  |   S | j                  j                  | j                        | j@                  jC                  | |      S tE        d	|j                   d
|       # 1 sw Y   xY w)Nr  zremap_input(r  r   r   r[  r+   	_operatorz(Could not run remap_input() on op type: z
 for node )#rv   r  r`   r  r%  r  r   r   rF  r   r  r  r  r  
insert_argr  rH   r<   rg  rh  sym_sizer@   rS   rj  unbindsumdim_IntListviewdiffrW   r+   r  r   readr  )rq   r6  s     r3   r  z_ModuleFrame.remap_input  s_   77$//) H  ==##

\!A&'(((++A..DDM!T%;%;%?%?%I%Q   #&&2 [[&&778O8OP V++66q$++:Q:QRS:TUV++A..TT_$HH		''++		##++		%%))		""..		##++		##++ ,/AHH4G4G;4V ''*==####''1= ::??4++:144&
1#N 1V Vs   36LL!c                 X   d }| j                   j                         D ],  }|j                  j                  di       }||}$||k7  s*i } n |r]| j                  t        d      || j                  j                  d<   | j                   j                         D ]  }|j                  d=  y y )NcustomzBparent_call_module must not be None when uplifting custom metadata)r  r   r   rF  r  r`   )rq   custom_metar   	curr_metas       r3   uplift_common_custom_metadataz*_ModuleFrame.uplift_common_custom_metadataL  s    MM((* 		D		h3I"'K' 		 &&.$X  6AD##((2,,. (IIh'( r2   c           	           j                   j                   j                  d        g } j                  j	                   j
                        }| j                  |j                  D ]  }t        |t        t        t        t        t        f      rS|j                   j                  v r)|j!                   j                  |j                            i|j!                  d        {t#        d|         fdt%         j&                  t)        fd|D              |j*                        }t-         j                  j&                   j.                  |j*                        }|}ng } j0                  D ]\  }|j2                  D ]K  }|j                   j                  vs|j!                  |       |j!                   j0                  |           \ ^  j.                  }t5        |      dk(  r|d   }t        |t6        t8        j:                  j<                  f      st?        dtA        |              jB                  jE                  |       |y t        |t8        j:                  j<                        r|jF                  j	                  d      n(|D 	cg c]  }	|	jF                  j	                  d       c}	|jF                  d<    jI                          t5        |      dk(  r|| j                  j0                  |d   <   y tK        |      D ]u  \  }
}|	t8        j:                  jM                  |      |
   jN                  }|jF                  j	                  d      |jF                  d<   | j                  j0                  |<   w y c c}	w )Nz'Unsupported data type for output node: c                     | y 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  rv   )rZ  	seen_noderq   s     r3   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node  sx    > OOFKK8	-==33$":"::33I>>&5fXYtzzlS r2   c              3   .   K   | ]  } |        y wrN   r1   )rP   rZ  r  s     r3   rT   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s     P,V4Ps   r   r   z8expected graph_outputs to be list or torch.fx.Node, got val)(r  rK  r   r   rF  r  r  r   r^   r   r   r   r   r   r   r  r   r  r  r)   r?   r<  r  r  r  usersr   r   r<   r   r  r`   ra   rv   rZ  r   r  r  Proxyr   )rq   orig_outputsr(  rZ  tree_out_node
parent_outgraph_outputs	orig_node	user_nodeor!  orig_output	proxy_outr  s   `            @r3   finalize_outputsz_ModuleFrame.finalize_outputsd  s     40**..t~~>	 T[[%<#++ &&'((	 {{doo5$++DOOFKK,HI$++D1&A&J !( 0P<PP""M
 0F""D$;$;Y=O=O0J BOMM!]] 	!* I ~~T__<$++I6%,,T]]9-EF 00J=!Q& -a 0-$)>? J4P]K^J_`  	

-(  -7 ""5)-:;!&&**U#; 	
 	**,|!i&74>DKK  a1"+L"9 >;&!HHNN:6q9>>	(3(8(8(<(<U(C	u%4=$$[1> <s   "Nc                     | j                  d|j                                | j                  j                  || j                        | j
                  |<   || j                  |j                  <   y )Ncopying)r%  format_noderv   	node_copyr  r  r  r   r   s     r3   	copy_nodez_ModuleFrame.copy_node  sP    

9d..01"jj2249I9IJd%)		"r2   c                 
   t        | j                  j                        D ]?  \  }}| j                  ||j                  j                  d      |j                                A d}| j                  |   }|j                  dk(  r5| j                  |       |dz  }| j                  |   }|j                  dk(  r5| j                  |       | j                  j                  D ]#  }|j                  dk(  s| j                  |       % y )Nry  r   r   r   rZ  )
r  r  r   r%  r   rF  r  r   r  run_from)rq   r!  r   node_idxs       r3   	run_outerz_ModuleFrame.run_outer  s     !6!67 	PGAtJJq$))--(9:D<L<L<NO	P zz(#gg&NN4 MH::h'D gg&
 	h OO)) 	%Dww("t$	%r2   c                 4    | j                   rt        |i | y y rN   )r  r%  )rq   r   r   s      r3   r%  z_ModuleFrame.print  s    <<4"6" r2   c                    d}|t        | j                        k  re| j                  |   }|j                  dk(  rt        d|       | j	                          | j	                  d||j                                | j	                  | j                         t        | j                        }|j                  dk(  r|dk(  r|S | j                          |S t        |j                  j                  di             dk(  rt        d|       |j                  d   }dd	lm} t        |      dk(  r||v r| j                  }n\|j                  d   j                         D 	
cg c]2  \  }\  }	}
|	|	r|
nd d
|v rt        |j                  d
      d         ndf4 }}	}}
|d | | j                  k7  rI| j                          | j	                  d| j                          | j	                  | j"                         |S |t        d      t%        | j                  |      r||   }| j	                  d|       t'        |j                  d   j)                               |   }t+        | j,                  | j                  | j.                  | j0                  | j2                  | j4                  | | j                  |gz   |j                  d
      d   | j6                  
      j9                  |      }|dz  }|| j                  k7  rt        d| d| j                         |j                  dk(  r2| j2                  | j:                     j=                  |j>                         | jA                  |       |dz  }|t        | j                        k  rdy y c c}
}	}w )Nr   r   z%unexpected placeholder node at index STEPrZ  r   ry  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYr   r  	outliningz"node_module_stack must not be NonezCreating new stack frame forzexpected node_module_stack z to equal module_stack r  )!r   r   r   r`   r%  r  r  r  r   rF  r  (torch._export.passes._node_metadata_hookr  r]   r@   rU   r   rv   r{  r   keysr  r  r  r  r  r  r   r  r  r   rH   r  )rq   r  
module_idxr   depthry  r  node_module_stackrQ   rC  rw   next_modulenext_module_keys                r3   r  z_ModuleFrame.run_from  sN   
TZZ(::h'Dww-'$'LXJ%WXX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% % &:D"	 "14AGGCL,-q%! % !%(D,=,== %%'

;1

4::& ($%IJJ$++->? 06

9;G #'tyy1B'C'H'H'J"KE"R'OOJJOO%%OO((%%5#))#.q1** (8$  a
 !D$5$55$12C1DD[\`\m\m[no  ww*$/33DKK@NN4 MH} TZZ(F%s   7NrN   )rn   N)r*   r+   r,   r<   r   r   r?   r  r   rs   r@   r   r   r   r"   rz   r  r  r  r  r  r  r  r%  r  r1   r2   r3   r  r  J  s     CGt9HHNNt9 UXX]]C'(t9 5cDj#!567t9  %8 89t9 $$'884?t9l7,\(0\>|*
%&#
ar2   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<   y)r  r  r  r  r   r  r)   N)r*   r+   r,   rs   r>   r<   rY   rZ   r   r  r@   r1   r2   r3   r  r  C  s;    O88??"%	HMHHOOr2   r  
orig_graphroot_modulec                 :   i }t        t              }t        t              }i }t        | t	        | j
                        ||||d dgd|j                  D ci c]%  }|j                  r|j                  |j                  ' c}|      j                          ||fS c c}w )N)r   Nr   r   )r)   )
r   r   rV   r  r?   r   r   r(  r   r  )r(  r)  r  r  r  r  r  s          r3   r   r   M  s    +-J5@5FL&1#&6J24Oj	
 %66	
 IIu&	

   ik##	
s   *Br  r"  rf   c                    |dk(  rWt        |j                               dd  D ]8  }t        | |      t        | |t        j
                  j                                : g }t        | j                  j                               D ]  \  }}|	||z   }t        |||j                  d      d   dz          t        | |       |j                  d      d   }|j                  |j                  ||j                  |t        |                  ||f        |j                  t!        j"                  d             |D ]  \  }}}| j%                  ||        y )Nr   r   r   r   rL   )rf   )r  )r   r!  r  r  r<   rY   rZ   r\   r]   r  rU   delattrr   rF  r   sortr  
itemgetterregister_module)	r  r"  rf   r   r  r   childbase_fqnr  s	            r3   r  r  f  s9    |	()!"- 	?Cfc*2vsEHHOO,=>	? HFOO1134 	
e=tmE9SYYs^A5F5LM99S>!$]]3	hI GH$PUV	
	
 MMh))!,M-" ,4tU+,r2   c                   (    e Zd ZdZd Zd Zd Zd Zy)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                 ,    t        t              | _        y rN   )r   rV   node_names_by_fqnrp   s    r3   rz   z_IVals.__init__  s    !,S!1r2   c                 x    t        |t        j                  j                        r|j                  j
                  S yrB   )r^   r<   _ops
OpOverload_schema
is_mutable)rq   rH   s     r3   _is_mutablez_IVals._is_mutable  s)    fejj334>>,,,r2   c                    |j                   dk7  rt        d|j                         | j                  |j                        }t	        d|j                  |       |sZt        t        |j                  d   j                                     \  }}| j                  |   j                  |j                         |j                  |j                  d         S )zI
        Read state corresponding to a given intermediate value.
        r[  z,expected node.op to be 'call_function', got zChecking mutabilityry  r   )r   r`   r:  rH   r%  r_  ra  r   r   r4  r   r   r  r   )rq   mfr   br   r  s         r3   r  z_IVals.read  s    
 77o% >twwkJ  T[[)#T[[!4 (499->#?#F#F#HIJFC""3'++DII6~~diil++r2   c           
         |D ]  }|D ]  }|j                   j                  }| j                  |j                     }|j                  D cg c]  }|j
                  |v s| }}|D ]  }|j                  |      5  |j                  dt        j                  j                  j                  j                  |j                  d   |f      }	t        j                  |j                        |	_        ddd          yc c}w # 1 sw Y   xY w)zT
        Update states corresponding to intermediate values that were read.
        r[  r   N)r)   rv   r4  r   r   r   re  rf  r<   rg  rh  ri  rj  r   r  r   )
rq   
partitionsshared_submodulesr  rv   
node_namesr  r   r   r]  s
             r3   r[   z_IVals.update  s     ", 	=* =**!33EII>
$)KKHq166Z3GHH! 	=D ..t4 =#(#4#4+!IINN0088!YYq\40$
 )-		$))(<= =	=	=	= I= =s   C:C:8A3C?	?DN)r*   r+   r,   r;   rz   r:  r  r[   r1   r2   r3   r   r     s    (2
,$=r2   r   r  c                     |j                         D ]5  \  }}|rt        ||      n|}|D ]  }t        | |      }t        |||        7 y rN   )r]   r   r   rX   )r  r)  r  r  namesr)   r   r  s           r3   r   r     sX    
 ',,. '	56?;	2[ 	'D"d#CFD#&	''r2   c                    i }| D ]I  }t        |      D ]7  \  }}t        |j                  |j                        }t	        |j
                  |      }d}|d | D ]  }t        |j                  |j                        s$|j                  }	|j                  |j                  k(  r|j                  d      ^ }
}t        |	|
      j                  j                  |       t        |j                  |j                        }t	        |j
                  |      }||j                  _        |||<    |r|	j                  ||j                         d} : L |S )NFrL   T)r  rJ  r   r  r  r  r  r)   r  rU   r   r\   rK  r  rH   rL  )r?  r   r@  r!  r  r  rH   deduplicatedseenr  rf   r   seen_child_fqnseen_targets                 r3   r   r     s?    ' $,!"34 #	,HAu"599enn=I&u'7'7CF L)"1- ,+DKKF"00F xx599,
 )/S(9/?HHLLTR)3DHHdmm)L&7!,,n' ;F007=K/	:) ,,VT[[A'+=,#	,$,L #"r2   r  scopemodule_id_to_inputs_removedc           	         t        t              t        |       v rt        |       t        |          iS | j                  j	                         D ]M  \  }t        t        d      |||gz         }|j	                         D ]  \  }}|   j                  |        O t        | dd      }|t        |j                        dk(  rS t        |t        j                  j                        st        dt!        |             t#        t%        d |j                              }	t        |	      dk(  rdn|	d   }
t%        d |j                        }|D ]Q  }t'        | |j(                        t              v s*t+        t%        fd	|j,                              |_        S i }|	D ]u  }|j.                  |vrd}||j.                     D ]H  }|j1                  d
      }|dt        |       |D cg c]  }|j1                  d      d    c}k(  sF|} n |q|||<   w t               }|j	                         D ]  \  }}t        |j2                        dkD  r|t        |      d }t5        | |      }t        |t        j6                  t        j8                  f      st        dt!        |             |j;                  |
      5  |j=                  dd
j?                  |            }ddd       |jA                  d       |jC                  |       |jE                  |j.                          t        | tF              r| jI                          t        |       |iS c c}w # 1 sw Y   |xY w)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.Modulerv   r   z)expected graph to be torch.fx.Graph, got c                      | j                   dk(  S )Nr   r   r  s    r3   r^  z_sink_params.<locals>.<lambda>+  s    144=#8 r2   r  c                      | j                   dk(  S )Nr   rM  rN  s    r3   r^  z_sink_params.<locals>.<lambda>/  s    )> r2   c                 8    | j                   t                 vS rN   )r   r   )r  rJ  	submodules    r3   r^  z_sink_params.<locals>.<lambda>7  s    aff,G9,VV r2   rL   r   zBexpected state_attr to be torch.Tensor or torch.ScriptObject, got r  T)propagate_meta)%r   rV   r   r\   r]   r   r
   r[   r   r   r   r^   r<   r   r   r`   ra   r   filterr   rH   r?   r   r   rU   r  r   rc   re   re  rf  r   rk  
erase_noder   r    r   )r)   r  rI  rJ  r   submod_id_to_inputs_removedrQ   rv  rv   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitr6  inputs_removed	attr_path
state_attrr]  rQ  s      `                  @r3   r   r     sc   , #*&1#&6#	&z006
76
CDD "??002 5i&2"I.TFN'	'
# 0557 	5DAq'*11!4	55 FGT*E}EKK(A-**eUXX^^,HeVWW&8%++FGF [A-T6":N >L! 
fdkk2	  R	]6Q%QVIIDI
 @B 499O+
!$)), 	Bxx}H#e*%5)Ia!''#,q/)II%
		 )3 &/44  #uN4::< &jtzz?Q"3u:<0I0CJj5<<9K9K*LM$XY]^hYiXjk 
 &&~6 N ,,Z)9LMN &&x&E499%!&$ &+,vJ''U *J>N Ns   6M"

"M''M0	)TrN   )r   )gr   r  loggingr  r  collectionsr   collections.abcr   
contextlibr   r   dataclassesr   enumr   typingr	   r
   r<   torch.fx._pytreer   _pytreer  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch._library.opaque_objectr   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%   r6   rC   rE   rY   rZ   rs   boolrj   rl   r    r!   ABCr   r"   r#   r   r   r   r   r{  r  r  r  r   r  r  r  r  r  r  r@   rJ  rO   r  r  r   r   r  r   rV   r   r   r   r   r1   r2   r3   <module>rv     s   
    	 # $ %  !    $ $ $ $ ? 8 ( 3	 	 	 < U U 7 = g!  $     ( (" =0=0xx=0 =0 	=0
 =0@	 	\
 \
~.".%((// ."bcgg ,S
 S
n JN!!0?$0F!!HH)88>>H))H) 
H)VUXX^^ d
 $NR
6# 6# 6# 6$6 6EHHOO 6B%((// C %((..  R003DDR
XX]]RM003DDM
XX]]M% % %* >B	)	)) 88??) SE588??23d:	)41S 1S 1S 1JS J J J
v vr   $EHHNN $AR $4 GI,HHOO,(,S#X,@C,2B= B=J	'	'"	' S#c(]#	'(#^ ?C	t(HHOOt(#tCy.)t( 9t( "&c3s8m!4t!;	t(r2   