+
    &jc(                       ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RI	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ RIHtHt ^ RIt^ RIHu Ht ^ RIHu Ht ^ R	IHt ^ R
IHt ^ RIH t  ^ RI!H"t"H#t#H$t$H%t%H&t&H't'H(t(H)t) ^ RI*H+t+ ^ RI,H-t-H.t.H/t/ ^ RIH0t0H1t1 ^RI2H3t3 ]Ph                  ! ]54      t6. ROOt7 ! R R]4      t8]! RR7       ! R R4      4       t9Rs:]
R 4       t;RPR R llt< ! R R4      t= ! R  R]=]P|                  P~                  4      t@ ! R! R]=]P|                  P~                  4      tA ! R" R] P                  4      tC ! R# R]=]P|                  P~                  4      tDRQR$ R% lltER& R' ltFR( R) ltGR* tHR+ tIR, R- ltJR. R/ ltKR0 R1 ltLR2 R3 ltMR4 R5 ltNR6 R7 ltOR8 R9 ltPRQR: R; lltQR< R= ltRR> R? ltS ! R@ RA4      tT] ! RB RC4      4       tURD RE ltVRRRF RG lltW ! RH RI4      tXRJ RK ltYRL tZRQRM RN llt[R# )S    N)defaultdict)Callable)contextmanager)deepcopy)	dataclass)Enum)Anycast)FakeScriptObject)ExportedProgram)reorder_kwargs)ConstantArgumentExportGraphSignature	InputKindModuleCallSignatureSymBoolArgumentSymFloatArgumentSymIntArgumentTensorArgument)is_fx_symbolic_tracing)	_get_attr_get_attr_via_attr_list_print_readable)
GetAttrKeySequenceKey)_remove_effect_tokensFlatArgsAdapterInterpreterModuleInterpreterModuleDispatcherUnflattenedModulec                   &    ] tR t^2tRtRtRtRtRtR# )	_AttrKind	parameterbufferconstantmodule N)	__name__
__module____qualname____firstlineno__	PARAMETERBUFFERCONSTANTMODULE__static_attributes__r'       n/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/torch/export/unflatten.pyr"   r"   2   s    IFHFr1   r"   T)frozenc                   0   a  ] tR t^9t o RtV 3R ltRtV tR# )	_TensorIDzJCustom tensor identifier containing storage, stride, and size information.c                ^   < V ^8  d   Qh/ S[ P                  ;R&   S[;R&   S[;R&   S[;R&   # )   untyped_storagestridesizestorage_offset)torchUntypedStoragetupleint)format__classdict__s   "r2   __annotate___TensorID.__annotate__9   s=      )))	 
 M  K   r1   r'   N)r(   r)   r*   r+   __doc____annotate_func__r0   __classdictcell__rA   s   @r2   r5   r5   9   s     T  r1   r5   c               #   :   "   \         p R s  Rx  V s R#   T s i ; i5i)FN)RUN_WITH_INTERPRETER)old_flags    r2   _disable_interpreterrK   F   s&      $H ('xs   	 c          
          V ^8  d   QhR\         P                  \         P                  ,          \         P                  P                  ,          R\         P                  P                  R\
        R\        R\        /# )r7   from_obj	to_moduletarget	attr_kind
persistent)r<   TensorScriptObjectnnModulestrr"   bool)r@   s   "r2   rB   rB   S   s^     =0 =0llU///%((//A=0xx=0 =0 	=0
 =0r1   c                   a	 VP                  R 4      Ev rVV0pV F  o	\        4       pV Fq  p\        VS	4      '       g*   \        VS	\        P
                  P                  4       4       VP                  V	3R lVP                  P                  4        4       4       Ks  	  TpK  	  V EF  pV\        P                  8X  dU   \        V \        P
                  P                  4      '       g   \        R\        V 4       24      hVP!                  W`4       Km  V\        P"                  8X  dM   \        V \        P$                  4      '       g   \        R\        V 4       24      hVP'                  W`VR7       K  V\        P(                  8X  dx   \        V \*        4      '       d   \        R4      h\        V \        P$                  \        P,                  34      '       g   \        R\        V 4       24      h\        WV 4       EKZ  V\        P.                  8X  g   EKr  \        V \        P
                  P                  4      '       g   \        R\        V 4       24      h\        WV 4       EK  	  R# )	.c              3   T   <"   T F  w  r\        VS4      '       g   K  Vx  K  	  R # 5iN)_is_call_name).0kt_callitems   &  r2   	<genexpr>_assign_attr.<locals>.<genexpr>g   s&      !;IA D) !;s   (
(z9expected torch.nn.Parameter for PARAMETER attr_kind, got z0expected torch.Tensor for BUFFER attr_kind, got )rQ   z2FakeScriptObject should only exist during tracing.zHexpected torch.Tensor or torch.ScriptObject for CONSTANT attr_kind, got z3expected torch.nn.Module for MODULE attr_kind, got N)splitsethasattrsetattrr<   rT   rU   update_modulesitemsr"   r,   
isinstance	ParameterAssertionErrortyperegister_parameterr-   rR   register_bufferr.   r   rS   r/   )
rM   rN   rO   rP   rQ   prefixfield
to_modulestsr`   s
   &&&&&    @r2   _assign_attrrt   S   s    \\#&NV J#&5#I9d++	4):;II !*!3!3!9!9!;  $ 
   		+++h(:(:;;$OPTU]P^O_`  ((9)***h55$FtH~FVW  %%e*%M),,,($455$H  LL&&  %^_cdl_m^no  Ih/)***h88$I$x.IYZ  Ih/E  r1   c                   >   a  ] tR t^t o V 3R lR ltV 3R ltRtV tR# )_SubmoduleBasec                .   < V ^8  d   QhRS[ R,          /# r7   returnNrV   )r@   rA   s   "r2   rB   _SubmoduleBase.__annotate__   s      3: r1   c                    V P                   # )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.
)_tyselfs   &r2   	type_name_SubmoduleBase.type_name   s     xxr1   c                4   < V ^8  d   Qh/ S[ R,          ;R&   # )r7   Nr}   rz   )r@   rA   s   "r2   rB   r{      s     	tO r1   r'   N)r(   r)   r*   r+   r   rE   r0   rF   rG   s   @r2   rv   rv      s        r1   rv   c                   h   a a ] tR t^t oRtR	V3R lV 3R llltR tR tR
R ltV3R lt	Rt
VtV ;t# )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.
c                \   < V ^8  d   QhRS[ P                  P                  RS[R,          /# )r7   graphtyNr<   fxGraphrV   )r@   rA   s   "r2   rB   InterpreterModule.__annotate__   s+     	: 	:xx~~	: $J	:r1   c                r   < \         SV `  4        Wn        W n        W P                  n        \
        V n        R # r[   )super__init__r   r}   owning_modulerI   _run_with_interpreter)r   r   r   	__class__s   &&&r2   r   InterpreterModule.__init__   s,    
 	
#'

 %9"r1   c                B  a V P                   f   \        R4      h\        4       '       g`   \        P                  P                  4       '       g   V P                  '       g*   \        V P                   4      P                  ! V .VO5/ SB # S'       d   \        V4      pV P                  \        V4      R  pVP                  V3R lV 4       4       \        V4      \        S4      8w  d$   \        R\        V4       R\        S4       24      h\        V4      \        V P                  4      8w  d.   \        R\        V4       R\        V P                  4       24      h\        V4      p\        P                  P                  W P                   R7      P"                  ! VRR	/ # )
Nz&Didn't finalize this InterpreterModulec              3   H   <"   T F  pVS9   g   K  SV,          x  K  	  R # 5ir[   r'   )r]   
kwarg_namekwargss   & r2   ra   ,InterpreterModule.forward.<locals>.<genexpr>   s*       &1
!V+ 'F:&&&1s   	""zkwarg_names length z does not match kwargs length zarg_list length z! does not match arg_names length r   enable_io_processingF)graph_modulerl   r   r<   compileris_dynamo_compilingr   rm   forwardlist	arg_nameslenextendr>   r   Interpreterr   run)r   argsr   arg_listkwarg_namess   &*l  r2   r   InterpreterModule.forward   sj   $ !IJJ%''NN..008R8R8R ))*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''JJ'?CC,1 r1   c                ^   \         P                  P                  W P                  4      V P                  R &   V P                  P                  4        . V n        V P                  P                   F;  pVP                  R8X  g   K  V P                  P                  VP                  4       K=  	  R# )r   placeholderN)r<   r   GraphModuler   __dict__lintr   nodesopappendrO   r   nodes   & r2   finalizeInterpreterModule.finalize   sr     ).(<(<T::(Nn%

 JJ$$Dww-'%%dkk2 %r1   c                "    \        V R VVVV4      # )r   r   r   print_outputinclude_strideinclude_devicecoloreds   &&&&&r2   print_readable InterpreterModule.print_readable   #     
 	
r1   c                \   < V ^8  d   Qh/ S[ P                  P                  R,          ;R&   # )r7   Nr   )r<   r   r   )r@   rA   s   "r2   rB   r      s!      ((&&-- r1   )r   r}   r   r   r[   TFFF)r(   r)   r*   r+   rD   r   r   r   r   rE   r0   rF   __classcell__r   rA   s   @@r2   r   r      s,     	: 	:(T3"
]  r1   c                   X   a a ] tR t^t oRtV3R lV 3R lltR tR tRR ltRt	Vt
V ;t# )	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.
c                F   < V ^8  d   QhRS[ S[,          RS[S[,          /# )r7   attrscall_modules)rd   rV   r   r   )r@   rA   s   "r2   rB   (InterpreterModuleDispatcher.__annotate__  s%     	 	c#h 	d;L6M 	r1   c           	       < \         SV `  4        V'       g   \        R 4      hV^ ,          P                  V n        V F   p\	        W\        V^ ,          V4      4       K"  	  V^ ,          P                  V n        W n        ^ V n        R# )zcall_modules must not be emptyN)	r   r   rl   rh   rf   getattrr}   _call_modules
_num_calls)r   r   r   accessorr   s   &&& r2   r   $InterpreterModuleDispatcher.__init__  sk     !ABB$Q00HDGLOX$FG ?&&)r1   c                    V P                   V P                  ,          pV P                  ^,           \        V P                   4      ,          V n         V! V/ VB #   \         d
    ^ T n        h i ; i)   )r   r   r   	Exception)r   r   r   call_modules   &*, r2   r   #InterpreterModuleDispatcher.forward  sd    ((9??Q.#d6H6H2II	/// 	DO	s   A A-c                    V P                   # r[   )r   r~   s   &r2   r   (InterpreterModuleDispatcher.call_modules  s    !!!r1   c           	         V P                    Uu. uF  pVP                  VVVV4      NK  	  ppR P                  V4      # u upi )
)r   r   join)r   r   r   r   r   modoutputss   &&&&&  r2   r   *InterpreterModuleDispatcher.print_readable  s[     ))
 * 	 * 	 
 yy!!
s   ?)r   rh   r   r}   r   )r(   r)   r*   r+   rD   r   r   r   r   r0   rF   r   r   s   @@r2   r   r      s(     	 	"" "r1   c                   j   a  ] tR tRt o Rt]P                  R	V 3R lR ll4       tV 3R lR ltRt	V t
R# )
r   i/  zF
Adapts input arguments with ``input_spec`` to align ``target_spec``.
Nc                   < V ^8  d   QhRS[ P                  RS[ P                  RS[S[,          RS[S[S[3,          R,          RS[R,          RS[S[,          /# )r7   target_spec
input_spec
input_argsmetadataNobjry   )pytreeTreeSpecr   r	   dictrV   )r@   rA   s   "r2   rB   FlatArgsAdapter.__annotate__5  sh     	 	__	 OO	 I		
 sCx.4'	 4Z	 
c	r1   c                    R# )z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr'   )r   r   r   r   r   r   s   &&&&&&r2   adaptFlatArgsAdapter.adapt4  s     	r1   c                0   < V ^8  d   QhRS[ S[,          /# r7   ry   )r   rV   )r@   rA   s   "r2   rB   r   @  s      DI r1   c                    . # )z>Returns a list of paths that are used to access the flat args.r'   r~   s   &r2   get_flat_arg_paths"FlatArgsAdapter.get_flat_arg_paths@  s    	r1   r'   )NN)r(   r)   r*   r+   rD   abcabstractmethodr   r   r0   rF   rG   s   @r2   r   r   /  s3      		 	 	 r1   c                   p   a a ] tR tRt oRV3R lV 3R llltR tR tR tR tR t	R	 t
RR
 ltRtVtV ;t# )r    iE  c                4   < V ^8  d   QhRS[ RS[R,          /# )r7   export_moduleflat_args_adapterN)r   r   )r@   rA   s   "r2   rB   UnflattenedModule.__annotate__F  s(     K K&K +T1Kr1   c           	     (  <a, \         S-V `  4        VP                  P                  e   \	        R4      hR pVP
                   Uu. uF  qDP                  NK  	  ppV^ ,          R8w  d   \        RV^ ,          : 24      h\        VP                  4      p\        VP                  4      V n        \        P                  P                  4       V n	        W P                  n        \        VP
                  4      V n        W n        VP                  P                   V n        W P                   R&   RV n        \$        V n        \)        W`P                  4       \+        V4       \-        V4      V n        \1        4       V n        \5        W`4      w  rxV P2                  P7                  VP9                  4       4       \;        VP<                  W4       VP>                  V n        . V n         VPB                  p	\E        4       p
/ pV P                  PF                   F  pW,          pV! V4      V9  dB   \        PH                  PK                  VPM                  4       VPN                  R7      W! V4      &   \Q        W! V4      ,          V V\R        PT                  R7       V
PW                  V4       K  	  \E        V P                  PX                  4      p\E        4       p/ pV P                  PZ                   F  pW9   d   RpVP\                  V,          pM
R	pW,          pV! V4      V9  d   VPM                  4       V3VV! V4      &   \Q        VV! V4      ,          ^ ,          V V\R        P^                  VR
7       VPW                  V4       K  	  V	Pa                  4        F  w  ppW9   g   W9   d   K  RpV! V4      V9   g+   \c        V\        PH                  PJ                  4      '       g   R	pV'       dO   V! V4      V9  d   VR	3VV! V4      &   \Q        VV! V4      ,          ^ ,          V V\R        P^                  R	R
7       K  V! V4      V9  d   VW! V4      &   \Q        W! V4      ,          V V\R        PT                  R7       K  	  / pVP\                  Pa                  4        Fz  w  ppV! V4      V9  d<   \c        V\        Pd                  4      '       d   VPM                  4       pVVV! V4      &   VV! V4      ,          p\Q        VV V\R        Pf                  R7       K|  	  \i        \j        4      o,\E        4       pV,3R lp\E        4       pV P                  Pl                   EF  pVPn                  \p        PT                  8X  g4   VPn                  \p        P^                  8X  Ed   VPr                  '       Ed   \u        VPv                  R4      '       g"   \        R\y        VPv                  4       24      h\c        VPz                  \|        4      '       g"   \        R\y        VPz                  4       24      hV! V! VPB                  VPz                  ,          4      VPv                  P~                  VPz                  4       VPW                  VPz                  4       VPW                  VPz                  4       EKV  VPn                  \p        P^                  8X  d   VPr                  '       dB   VPn                  \p        P                  8X  g#   VPn                  \p        P                  8X  g   EK  \u        VPv                  R4      '       g/   \        RVPn                   R\y        VPv                  4       24      h\c        VPz                  \|        4      '       g/   \        RVPn                   R\y        VPz                  4       24      hV! V! VP\                  VPz                  ,          4      VPv                  P~                  VPz                  4       VPW                  VPz                  4       EK  	  VP\                  Pa                  4        Ff  w  ppVV9  g   K  V! V4      pVS,9  d   \        RV: R24      hS,V,          ^ ,          w  p p!V! VV V4       VPW                  XPz                  4       Kh  	  VPB                  Pa                  4        F=  w  ppVV9  g   K  V! V4      p"V"S,9  d   K   S,V",          ^ ,          w  p p!V! V"V V4       K?  	  / p#S,P9                  4        F,  p$V$ U%u. uF  p%V%^,          NK  	  p&p%V$ F  w  p'p!V&V#V'&   K  	  K.  	  \        V V#. 4       \        VP9                  4       4      p(V Uu. uF  pVV(9  g   K  VNK  	  ppV P                  V(V4       V Uu. uF  pRV9  g   K  VNK  	  ppV P                  P                   U)u. uF  p)V)P                  R8X  g   K  V)NK  	  up)V nG        R	V nH        \        V4       U*Uu/ uF	  w  p*pVV*bK  	  p+p*pV P                  RR7       F  w  pp!VV+9  g   K  \        V+4      V+V&   K  	  \        V V+4       V P                  P                  4        V P                  4        R # u upi u up%i u upi u upi u up)i u upp*i )Nz%Unflattening on JointExportModule NYIc                    \        V \        P                  4      '       dH   \        V P	                  4       V P                  4       V P                  4       V P                  4       R7      # \        V 4      # )z8Returns _TensorID dataclass for tensors, otherwise id().)r8   r9   r:   r;   )	rj   r<   rR   r5   r8   r9   r:   r;   id)r   s   &r2   _id'UnflattenedModule.__init__.<locals>._idO  sU    #u||,, $'$7$7$9::<#&#5#5#7	  c7Nr1    z+expected first fqn to be empty string, got unflattened_moduleF)requires_grad)rP   T)rP   rQ   c                 >   < SV ,          pVP                  W34       R # r[   )r   )obj_id	node_nametarget_name	name_list
consts_maps   &&& r2   add_to_consts_map5UnflattenedModule.__init__.<locals>.add_to_consts_map  s    "6*Ii56r1   namez-expected s.arg to have 'name' attribute, got z!expected s.target to be str, got z1expected s.arg to have 'name' attribute for kind z, got z%expected s.target to be str for kind z	constant z id not found in consts_map@r   remove_duplicate)Or   r   graph_signaturebackward_signature
ValueErrormodule_call_graphfqnrl   r   r   r<   r   r   r   r   r   metaadaptedrI   r   #_inplace_buffer_and_input_mutations_fix_nn_module_stacks_root_module_typer}   _IValsivals_outline_submodulesrg   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrd   
parametersrT   rk   cloner   rt   r"   r,   addnon_persistent_buffersbuffers	constantsr-   ri   rj   rR   r.   r   r   input_specskindr   rQ   re   argrm   rO   rV   r  CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   r   input_placeholderscheck_input_constraints	enumeratenamed_modulesr   _reorder_submodulesr   r   ).r   r   r   r   entryfqn_listexport_graphseen_modules
seen_attrsr  assigned_paramsid_to_paramr  paramr  assigned_buffersid_to_bufferrQ   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.   &&&                                         @r2   r   UnflattenedModule.__init__F  s	   
 	((;;GDEE		 ,9+J+JK+J%II+J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|#J  	

,--/0 	-55tH!.!@!@*,! #--
$'E  	 ((33D$E5z,*/((*<*<KKM1D1D += +CJ' CJ'#--	 % 4 "%T%9%9%P%P!Q%(UOQ((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 /H TV*44::<MC8}K/h55'~~/H-5CM*#CM2I#,,	 = DOtCT
#&5	7 *-%%11Avv,,,)***q|||quuf--(GQUU}U  "!((C0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0(?xvdSTS[S[nM]^  "//9:EEJJHH
 ""188,M 2R "/!8!8!>!>!@J/u::-(#J>1LM  (1!4
!(GZ@$((2 "A )3399;KC..K	J. '	215
!)Wc: < 13%,,.K%01[qtt[G1#1%," $ /
 	T?B/"6|7J7J7L"M#+R8Cs:Q/QCC8R6G#+>8Cs#~CC8> "ZZ--#
-TM1IDD-#
 (,$*3H*=>*=3S!V*=	>))5)AGD!9$"%i.	$ B 	D),

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

 ?s6   i5i:#	i?1i?	jj=j	j	8jc                   V P                  4        Fv  w  r\        VR ,           4       \        VR4      '       g   K+  \        VP                  \
        P                  P                  4      '       g   Ka  \        VP                  4       Kx  	  R# ):r   N)r+  printre   rj   r   r<   r   r   )r   r  r   s   &  r2   _print_graphUnflattenedModule._print_graphS  sT    **,HC#)sG$$CIIuxx~~)N)Ncii  -r1   c                   V P                   ^ ,          P                  pW$P                  8X  d   V# V P                  f   \	        R4      hV P                  P                  VP                  VVV P                  VR7      p\        V4      VP                  P                  8w  d/   \	        R\        V4       RVP                  P                   24      hV# )r   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)r   	flat_argsrT  inputrS  s   &&&& r2   _adapt_flat_args"UnflattenedModule._adapt_flat_argsY  s    **1-77	'''!!)O 
 ..44%--"$ 5 I 9~!2!2!=!==  #I/ 0((1(9(9(D(D'EG 
 r1   c           	        V P                   ^ ,          P                  pTpV'       d   \        W#P                  4      p\        P
                  ! W34      w  rVV Uu. uF  qw^,          NK  	  pp\        4       '       d   V# WcP                  8w  dT   V P                  '       g)   \        RV R2RVP                   24       \        R4       V P                  WV4      pRV n        V P                  '       d   ^ RIHp	 V P                  RJ d   V P                  '       d   V P                  P                  4       M. p
V
'       d=   \        V
4      \        V4      8w  d$   \!        R\        V
4       R\        V4       24      h\#        V4       UUu. uF/  w  r\%        VR	7      \'        V
'       d	   W,          MR
R7      3V3NK1  	  pppMTpV	! V P(                  WP*                  4       V# u upi u uppi )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
  rS  r   rT  r   tree_flatten_with_pathr   r  rO  rY  r)  torch._export.utilsr\  r   r   r   rl   r*  r   r   r(  r  )r   r   r   rS  reordered_kwargsflat_args_with_pathrT  xrW  r\  flat_arg_pathsr]  r"  new_flat_args_with_paths   &*,           r2   process_forward_inputs(UnflattenedModule.process_forward_inputst  s   **1-77	!-f6G6GH'-'D'D$(
$ $77#6aqTT#6	7!##'''<<<''.ir301B1B0CD
 MN--i$GIDL''' O||t# --- **==? 
 "c.&9S^&K(0^1D0EEfgjktgufvw  %.i$8+ %9 (C0&#1 &4%8%9 
 %9 ( +' +>'.'')@BXBX i 8>+s   G$
5G)c                   V P                   ! V/ VB pV P                  ^ ,          P                  p\        4       '       dl   \        P
                  P                  W P                  R7      P                  ! VRR/ p\        V\        4      '       d   \        V4      ^8X  d
   V^ ,          # V# \        P                  P                  4       '       g   V P                  '       g)   \        V P                   4      P"                  ! V .VO5!  pM:\        P
                  P                  W P                  R7      P                  ! VRR/ p\$        P&                  ! WdP(                  4      # )r   r   r   F)re  r
  rS  r   r<   r   r   r   r   rj   r>   r   r   r   r   rm   r   r   r   tree_unflattenout_spec)r   r   r   rW  rS  
return_valtree_outs   &*,    r2   r   UnflattenedModule.forward  s   //@@	**1-77	!##--d**-EII16J *e,,ZA1E!!}$>>--//t7Q7Q7QD--.66tHiHHxx++D

+CGG16H $$X/A/ABBr1   c                    \         P                  P                  W P                  4      V P                  R &   V P                  P                  4        R# )r   N)r<   r   r   r   r   r   r~   s   &r2   r   UnflattenedModule.finalize  s1    (-(<(<T::(Nn%

r1   c                D  a \        \        4      oV P                   F  pVP                  '       g   K  VP                  '       g   K+  VP                  p\        WP                  WD4      4      pRV9   d   VP                  R4      MVR.w  rgSV,          P                  \        V4      V34       K  	  \        \        4      pV FO  p	RV	9   d.   V	P                  R^4      w  rW,          P                  V4       K7  VR,          P                  V	4       KQ  	  SP                  4        F  w  r\        V4       UUu. uF  w  rVNK	  	  ppp\        V4      ^8  g   K5  \!        \        V4      4       FV  p\#        W^,           4      pWA9  g   K  VP                  R4      Ev pp\%        V V4      P&                  P)                  V4       KX  	  V P+                  V
\-        W,          V4      4       K  	  V3R lpV P/                  RR7       F  w  rE\1        VR4      '       d   V! WEP2                  4       K+  \1        VR	4      '       g   K?  VP4                   F?  p\1        VR4      '       g   \7        R
\9        V4       24      hV! VVP2                  4       KA  	  K  	  R# u uppi )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  0rY   r   c                    < VP                    FX  pVP                  R 8X  g   K  VP                  P                  R4      ^ ,          pV '       d   V  RV 2MTpVS9   g   KR  W2n        KZ  	  R# )r   r  rY   N)r   r   rO   rc   )rp   r   r   r  pathcalled_moduless   &&   r2   elide_call_indices?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  sW    77m+++++C03C06fXQse,CD~-&) $r1   Fr  r   r   z-expected mod_ to have 'graph' attribute, got N)r   r   r
  r  rS  r   getrc   r   r?   rd   rsplitr  ri   sortedr   range
_call_namer   rh   popset_submoduler   r+  re   r   r   rl   rm   )r   rI  r;  r-  r  r   baser]  	attrs_maprO   orig_fqnr  indexed_call_modulesrB  r   rJ  rp   rt  mod_rs  s   &&&                @r2   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,KDQUJ<X  'sDJJ7 .	 C- Ls   :Jc                "    \        V R VVVV4      # )r    r   r   s   &&&&&r2   r    UnflattenedModule.print_readable
  r   r1   )r   r}   r  r)  r  r   r   r  r(  r  r  r
  r  r[   r   )r(   r)   r*   r+   r   rP  rY  re  r   r   r'  r   r0   rF   r   r   s   @@r2   r    r    E  s=     K KZ!6>@C*;8z
 
r1   c                J    V ^8  d   QhR\         R\        R,          R\        /# )r7   r&   r   Nry   )r   r   r    )r@   s   "r2   rB   rB     s)     ! !!0?$0F!!r1   c                    \        V 4      p \        W4      p\        P                  P	                  VP
                  RRR7      Vn        V# )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disablere  )r&   r   ms   && r2   	unflattenr    sL    , #6*F&4A  %}}44	  ;  5  A Hr1   c                \    V ^8  d   QhR\         P                  P                  R\        RR/# )r7   r   r  ry   N)r<   r   r   r   )r@   s   "r2   rB   rB   ?  s3     H) H)88>>H))H) 
H)r1   c           	       a \        \        \        V P                  4      4      4      pVP                  R8w  g   \        VP                  4      ^8w  d1   \        RVP                  : R\        VP                  4       R24      hVP                  ^ ,          pV P                   Uu/ uF"  qDP                  R8X  g   K  VP                  VbK$  	  pp/ pVP                  P                  4        UUu/ uF  w  rxWbK	  	  p	ppVP                  P                  4        UUu/ uF  w  rxWyV,          bK  	  pppVP                  VP                  4       \        V4      p
VRV
  F  pWkP                  ,          pW\,          pV P                  V4      ;_uu_ 4        V P                  R\         P"                  P$                  P&                  P(                  W34      oVP*                  P                  4        F  w  r~VSP*                  V&   K  	  RRR4       VP-                  SV3R l4       K  	  \/        W:R 4      pV3Vn        R# u upi u uppi u uppi   + '       g   i     LR; i)	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.
outputz8expected output node with op='output' and 1 arg, got op=z with z argsr   Ncall_functionc                    < V SJ# r[   r'   )rb  new_nodes   &r2   <lambda>5_inplace_buffer_and_input_mutations.<locals>.<lambda>  s
    1H;Lr1   )nextiterreversedr   r   r   r   rl   r  inputs_to_buffersri   buffers_to_mutaterg   user_inputs_to_mutateinserting_aftercreate_noder<   opsatencopy_defaultr  replace_all_uses_withr>   )r   r  output_nodereturn_argsr   input_name_to_nodemutation_name_to_input_namer^   
buffer_fqnbuffer_fqn_to_input_namenum_mutationsmutation
input_name
input_nodevuser_outputsr  s   &&              @r2   r  r  ?  s&   : tHU[[123K~~!S)9)9%:a%?F{~~FXX^_bcncscs_t^uuz{
 	
 ""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%   %H?<H?.II
A3II c                f    V ^8  d   QhR\         P                  P                  R\        R,          /# )r7   r   ry   Nr   )r@   s   "r2   rB   rB     s&      UXX^^ d
 r1   c                     V P                    FN  pR VP                  9  d   K  VP                  R ,          P                  4        F  w  r#V'       d   K  Vu u # 	  KP  	  R# )nn_module_stackN)r   r  r  )r   r   rr  r   s   &   r2   r  r    sM    DII-		"34;;=HD4	 >	  r1   c           	      <   V P                    EF  pR VP                  9  d   K  VP                  R ,          pVP                  4        UUu. uF&  w  r4RV9   d   VP                  R4      ^ ,          MTNK(  	  pppVvrg^pV F!  p	\	        Wi4      '       d   V^,          pT	pK!   M	  V\        V4      8  g   K  \        P                  RVV\        V4      V,
          4       \        \        VP                  4       4      RV 4      VP                  R &   EK  	  R# u uppi )r  r  zPnn_module_stack fqns %s at node %s do not form a stack! dropping last %d entriesN)r   r  r  rc   
_is_prefixr   logwarningr   r   ri   )
r   r   r  r  _tfqnsprev_fqn	next_fqnsnum_valid_indicescurr_fqns
   &         r2   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                b    \        V 4      \        V4      8  ;'       d    VR\        V 4       V 8H  # )z2Check whether `candidate` is a prefix of `target`.N)r   )	candidaterO   s   &&r2   r  r    s.    y>CK'QQF3CS^,D	,QQr1   c                <    V ^8  d   QhR\         R\         R\         /# )r7   
parent_fqn	child_fqnry   rz   )r@   s   "r2   rB   rB     s!     6 6# 6# 6# 6r1   c                     V R 8X  d   V# V P                  R4      pVP                  R4      pVR\        V4       V8w  d   \        RV RV  R24      hRP                  V\        V4      R 4      # )r   rY   Nz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.)rc   r   RuntimeErrorr   )r  r  parent_splitchild_splits   &&  r2   _compute_accessorr    s    R##C(L//#&K &S&'<7YK'OPZ| \P P
 	

 88KL 1 3455r1   c                    V ^8  d   QhR\         P                  P                  R\         P                  P                  /# )r7   rb  y)r<   rT   rU   )r@   s   "r2   rB   rB     s*     6 6 6EHHOO 6r1   c                    R  R lp\        V P                  \        P                  P                  4      '       g"   \        R\        V P                  4       24      h\        VP                  \        P                  P                  4      '       g"   \        R\        VP                  4       24      hV! V P                  4      V! VP                  4      8H  # )c                X    V ^8  d   QhR\         P                  P                  R\        /# )r7   r   ry   r   )r@   s   "r2   rB   ._check_graph_equivalence.<locals>.__annotate__  s"      %((.. S r1   c                 d  a
 . p/ o
R  V
3R llp\        V P                  4       F  w  r4\        P                  ! W$P                  4       Uu. uF  p\        V4      NK  	  ppT\        P                  ! W$P                  4      P                  4        UUu. uF  w  rxV RV 2NK  	  upp,          pVP                  R
9   d   VP                  MRp	VP                  V RVP                   RV	 RRP                  V4       R24       VS
\        V4      &   K  	  R	P                  V4      # u upi u uppi )c                $    V ^8  d   QhR\         /# r   rz   )r@   s   "r2   rB   B_check_graph_equivalence.<locals>.graph_dump.<locals>.__annotate__  s     	 	S 	r1   c                    < \        V \        P                  P                  4      '       d#   R \	        S\        V 4      ,          4      ,           # \	        V 4      # )%)rj   r<   r   NoderV   r   )r"  	nodes_idxs   &r2   arg_dump>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  s:    #uxx}}--S2c7!3444s8Or1   =r   z: [z](z, )r   )r  get_attr)r*  r   r   tree_mapr   rV   r   ri   r   rO   r   r   r   )r   retr  rJ  r   r"  	args_dumpkeyvaluerO   r  s   &         @r2   
graph_dump,_check_graph_equivalence.<locals>.graph_dump  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,
z+expected x.graph to be torch.fx.Graph, got z+expected y.graph to be torch.fx.Graph, got )rj   r   r<   r   r   rl   rm   )rb  r  r  s   && r2   _check_graph_equivalencer    s    * agguxx~~..9$qww-I
 	
 agguxx~~..9$qww-I
 	
 agg*QWW"555r1   c                X    V ^8  d   QhR\         P                  P                  R\        /# r7   gmry   r<   rT   rU   rV   )r@   s   "r2   rB   rB     s"      %((// C r1   c                 l    ^ p\        V RV 24      '       d   V^,          pK   RV 2p\        WV4       V# )r   _spec_)re   rf   )r  specrJ  r  s   &&  r2   	_add_specr    s=    	A
"qcl
#
#	QA3<DBdKr1   c                    V ^8  d   QhR\         P                  P                  R\         P                  P                  /# r  )r<   r   r   r  )r@   s   "r2   rB   rB     s,      %((..  r1   c                     V P                   P                  \        P                  V34      pV P                   P                  \        P
                  V^ 34      pV# )r   )r   r  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s   &&  r2   _generate_flattenr    sF    hh$$V%8%84'BG&&x'7'7'1FIr1   c                    V ^8  d   QhR\         P                  P                  \        ,          \        ,          R\         P                  P
                  /# r  r<   r   r   r   r    r  )r@   s   "r2   rB   rB     s>     R R003DDR
XX]]Rr1   c                     \        W4      pV P                  P                  V4      pV P                  P                  \        P
                  W34      # r[   )r  r   r  r  	fx_pytreetree_flatten_spec)r  r   r  r  	spec_nodes   &&&  r2   _generate_flatten_specr    sB     RD!!$'I88!!)"="=?PQQr1   c                    V ^8  d   QhR\         P                  P                  \        ,          \        ,          R\         P                  P
                  /# r  r  )r@   s   "r2   rB   rB     s>     M M003DDM
XX]]Mr1   c                     \        W4      pV P                  P                  V4      pV P                  P                  \        P
                  W34      # r[   )r  r   r  r  r   rh  )r  r   r  r  r  s   &&&  r2   _generate_unflattenr    sB     RD!!$'I88!!&"7"7%9KLLr1   c                X    V ^8  d   QhR\         P                  P                  R\        /# )r7   r   rO   r  )r@   s   "r2   rB   rB     s"     % % % %r1   c                     VP                  R 4      Ev r#V FE  p\        WR4      pVf    R# \        V\        P                  P
                  4      '       g    R# Tp KG  	  \        WR4      # )rY   N)rc   r   rj   r<   rT   rU   )r   rO   rp   rq   r`   submods   &&    r2   _get_submoduler    s\    \\#&NVD)>&%((//22  3t$$r1   c          
          V ^8  d   QhR\         P                  P                  R\        R\         P                  P                  R\        \        .\         P                  P                  3,          R,          /# )r7   r   rO   module_to_addcreate_moduleN)r<   rT   rU   rV   r   )r@   s   "r2   rB   rB   $  sY     ) )	)) 88??) SE588??23d:	)r1   c           	         VP                  R 4      Ev rE\        V4       F  w  rg\        WR4      pVfQ   Ve#   V! R P                  VRV^,            4      4      pM\        P
                  P                  4       p\        WV4       \        V\        P
                  P                  4      '       g    R# Tp K  	  V P                  WR4       R# )rY   NF)
rc   r*  r   r   r<   rT   rU   rf   rj   
add_module)	r   rO   r  r  rp   rq   rJ  r`   r  s	   &&&&     r2   _add_submoduler  $  s     \\#&NVV$D)>(&sxxwQ'@A*Cv&&%((//22 % NN5(r1   c                <    V ^8  d   QhR\         R\        R\         /# )r7   r}  rH  ry   )rV   r?   )r@   s   "r2   rB   rB   >  s!     1 1S 1S 1S 1r1   c                 0    V^8X  d   V # V  RV^,
           2# )r   r  r'   )r}  rH  s   &&r2   rz  rz  >  s$     640$qQ00r1   c                <    V ^8  d   QhR\         R\         R\        /# )r7   	call_namer}  ry   )rV   rW   )r@   s   "r2   rB   rB   D  s&     J JS J J Jr1   c                 j    \         P                  ! \         P                  ! V4      R ,           V 4      RJ# )z(@\d+)?$N)rematchescape)r  r}  s   &&r2   r\   r\   D  s%    88BIIdOk19=TIIr1   c                   x   a  ] tR tRt o RV 3R lR lltR tR tR tV 3R lR	 ltR
 t	R t
R tR tR tRtV tR# )_ModuleFrameiI  Nc                0  < V ^8  d   QhRS[ P                  P                  RS[S[ P                  P                  R3,          RS[S[S[S[R,          S[3,          ,          RS[S[S[	3,          RS[ P                  P                  S[,          R,          /# )r7   
flat_graphr   .module_stackNr
  r&   )r<   r   r   r>   r  r   rV   r?   r   r   r   r    )r@   rA   s   "r2   rB   _ModuleFrame.__annotate__J  s     t9 t9HHNNt9 UXX]]C'(t9 5cDj#!567t9  %8 89t9 $$'884?t9r1   c                Z  a aa! VS n         VS n        VS n        VS n        VS n        VS n        SS n        VS n        V	S n        V
S n	        R S n
        S P                  R,          w  S n        o!p\        S P                  V^,           4      S n        S  Ve.   VS n        \        VR4      '       d   VP                   M/ S n        McS P
                  P#                  S P                  \%        \&        P(                  P+                  4       S!R7      4      S n        SP                   S n        S P                  P,                  S n        / S n        / S n        RS n        SEe_   \5        SP                  S P                  4      pVV V!3R lp\7        SP                  VS P                  V4       SP,                  P9                  V4      S n        S P                  S P                  ,          '       dJ   S P                  S P                  ,          ^ ,          pVP                  P:                  S P                  n        S P                  S P                  ,          P=                  \?        S P                  P                  S P                  P                  S P2                  S P                  V^,           S P                  R7      4       V
P#                  S P                  4      pVEe   S P                  Ee   VP@                  PB                  ^8w  d#   \E        RVP@                  PB                   24      hVP@                  PF                  \H        Jd#   \E        RVP@                  PF                   24      hVP@                  PK                  4       w  ppVPF                  \H        Jd   \E        RVPF                   24      hVPF                  \L        Jd   \E        R	VPF                   24      hS P,                  PO                  R4      ;_uu_ 4        \Q        VPB                  4       Uu. uF!  pS P,                  PS                  R
V 24      NK#  	  pp/ pVPT                   F!  pS P,                  PS                  V4      VV&   K#  	  \W        S P                  \I        V4      V3VP@                  4      p\Y        VPZ                  4       F  w  ppS P,                  P]                  R\^        P`                  VV3\c        V\d        4      '       g   VPf                  MRV 2R7      p\c        V\d        4      '       d   Kr  VPf                  S P                  9   g   K  \h        Ph                  ! S P                  VPf                  ,          Pj                  4      Vn5        VS P0                  S P                  VPf                  ,          &   K  	  RRR4       S P                  P,                  Pm                  S P2                  4      ;_uu_ 4        . pVPZ                   F  p\c        V\d        4      '       d   VP=                  VPn                  4       K6  VPf                  S P                  9  d   VP=                  R4       Kd  \c        V\p        \r        \t        \v        34      '       g   \E        R\G        V4       24      hVP=                  S P                  Py                  S P                  VPf                  ,          4      4       K  	  \{        S P                  P                  VVP@                  4      pS P                  P,                  P}                  \^        P`                  V^ 34      pS P                  P,                  P}                  \^        P`                  V^34      p\Q        VPB                  4       Uu. uF9  pS P                  P,                  P}                  \^        P`                  VV34      NK;  	  ppVPT                   U u/ uF:  p V S P                  P,                  P}                  \^        P`                  VV 34      bK<  	  pp RRR4       S P2                  f   \E        R4      h\I        X4      S P2                  n?        XS P2                  n@        R# R# R# u upi   + '       g   i     EL; iu upi u up i   + '       g   i     L}; i)FNr  r   c                 
  < SP                   '       d   SP                    R V  2MT pVSP                  9   d   SP                  V,          # \        \        P                  P                  4       SR7      pVSP                  V&   V# )rY   r  )r  created_modulesr   r<   r   r   )r  rr  r  parentr   r   s   &  r2   r  ,_ModuleFrame.__init__.<locals>.create_module~  sl    06


&**Qse,4/////55*588>>+;C-3$$T*r1   )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   rO   r   r  z_expected input to be TensorArgument, SymIntArgument, SymBoolArgument, or SymFloatArgument, got z#parent_call_module must not be None)Ar  r   
seen_nodesr0  r1  r  r  r  	module_idr
  verboser  rz  r  r&   re   r  rv  r   r<   r   r   r   node_mapnode_to_placeholderr  r  r  r   rh   r   _SubmoduleEntryrT  num_childrenrl   rm   r>   childrenr   r  ry  r   contextr  r*  inputsr  r  r  rj   r   r  copyr  inserting_beforer  r   r   r   r   remap_inputr  r  r   r   )"r   r  r   r  r0  r1  r  r  r  r  r
  r&   	num_callsr   r  base_module_framerS  	args_speckwargs_specr]  	arg_nodeskwarg_nodesr  rW  r"  flat_arg_nodeinput_nodesrX  inputs_node	args_nodekwargs_noderJ  r^   r   s"   f&&&&&&f&&&&                     @r2   r   _ModuleFrame.__init__J  s    %
$($.("!2"&"3"3B"7"i#DHHi!m< DK)0)A)ArDJ..22!%((.."2r:DK  DJ[[&&
 =?#% 8<(T^^DH 6==(DKKO&,ll&>&>x&HD#  0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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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	 1 t{{((66 ((;*:  1	  S M^ &&.$%JKK+0+;D##(-8D##*O &= ( 211LS MLsR   <a<'a7;D a< A.a<-F'b?bb#A b#b7a<<b	
bb*	c                   V P                   R 8X  d   \        RV R24      hVP                  V P                  Jd   \        R4      hV P                  P	                  R4      ;_uu_ 4        V P                  P                  VP                  VP                  R7      pRRR4       \        P                  ! VP                  4      Xn	        W P                  V&   R#   + '       g   i     LE; i)r   zCannot add placeholder z to root module6expected x.graph to be flat_graph, got different graphN)	type_expr)r  rl   r   r  r(  r   r  rm   r'  r  r!  )r   rb  placeholder_nodes   && r2   add_placeholder_ModuleFrame.add_placeholder   s    88r> #:1#_!MNN77$//) H  ZZ((..#zz55aff5O / !%		!&& 1&6  # /.s   )2CC(	c                   \         P                  ! \        P                  P                  V P
                  VP                  4      p\         P                  ! \        P                  P                  V P
                  VP                  4      pV P                  P                  VP                  W#4      p\        P                  ! VP                  4      Vn        W@P                  V&   V# r[   )r   tree_map_onlyr<   r   r  r)  r   r   r   r  rO   r'  r  r   )r   rb  r   r   r   s   &&   r2   copy_sym_call_function#_ModuleFrame.copy_sym_call_function  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar1   c                   VP                   V P                  Jd   \        R 4      hWP                  9   d   V P                  V,          # V P	                  RV R24       WP
                  9   d   V P
                  V,          # VP                  R8X  g(   V P                  P                  V P                  4      f   V P                  V4       V P                  ev   V P                  P                   P                  V P                  4      ;_uu_ 4        V P                  P                  ^ V P                  P                  V4      4       RRR4       V P
                  V,          # VP                  R8X  Ed{   VP                   \"        P$                  P&                  P(                  P*                  \"        P$                  P&                  P,                  P.                  \"        P$                  P&                  P0                  P*                  \"        P$                  P&                  P2                  P4                  \"        P$                  P&                  P6                  P.                  \"        P$                  P&                  P8                  P.                  39   g8   \;        VP                   R4      '       d@   VP                   P<                  R8X  d%   V P?                  V4       V P                  V,          # V P                  P                  V P                  4      e   V P@                  PC                  W4      # \E        RVP                   R	V 24      h  + '       g   i     EL; i)
r7  zremap_input(r  r   Nr  r)   	_operatorz(Could not run remap_input() on op type: z
 for node )#r   r  rl   r   rO  r!  r   r
  rv  r  r:  r  r  r(  
insert_argr)  rO   r<   r  r  sym_sizer?   r`   r  unbindsumdim_IntListviewdiffre   r)   r>  r  readr  )r   rb  s   &&r2   r)  _ModuleFrame.remap_input  sY   77$//) H  ==##

\!A&'(((++A..DDM!T%;%;%?%?%I%Q   #&&2 [[&&778O8OPP++66q$++:Q:QRS:TU Q++A..TT_$HH		''++		##++		%%))		""..		##++		##++ ,//AHH4G4G;4V ''*==####''1= ::??4++:144&
1#N 1 QPPs   6M		M	c                   < V ^8  d   QhRR/# rx   r'   )r@   rA   s   "r2   rB   r  K  s     ( (t (r1   c                z   R pV P                   P                  4        F1  pVP                  P                  R/ 4      pVf   TpK'  W18w  g   K/  / p M	  V'       db   V P                  f   \        R4      hWP                  P                  R&   V P                   P                  4        F  pVP                  R K  	  R # R # )NcustomzBparent_call_module must not be None when uplifting custom metadata)r   r  r  rv  r  rl   )r   custom_metar   	curr_metas   &   r2   uplift_common_custom_metadata*_ModuleFrame.uplift_common_custom_metadataK  s    MM((*D		h3I"''  + &&.$X  6A##((2,,.IIh' / r1   c           	       a a S P                   P                  S P                  R 4       . pS P                  P	                  S P
                  4      pVEeE   S P                  Ee6   VP                   F  p\        V\        \        \        \        \        34      '       d]   VP                  S P                  9   d/   VP!                  S P                  VP                  ,          4       Kw  VP!                  R 4       K  \#        RV 24      h	  V 3R lo\%        S P&                  \(        ;QJ d    . V3R lV 4       F  NK  	  5M! V3R lV 4       4      VP*                  4      p\-        S P                  P&                  S P.                  VP*                  4      pTpM. pS P0                   Fg  pVP2                   FT  pVP                  S P                  9  g   K   VP!                  V4       VP!                  S P0                  V,          4        Ke  	  Ki  	  S P.                  p\5        V4      ^8X  d
   V^ ,          p\        V\6        \8        P:                  P<                  34      '       g   \?        R\A        V4       24      hS PB                  PE                  V4       Vf   R # \        V\8        P:                  P<                  4      '       d   VPF                  P	                  R4      M)V U	u. uF  qPF                  P	                  R4      NK  	  up	VPF                  R&   S PI                  4        \5        V4      ^8X  d'   Vf#   VS P                  P0                  V^ ,          &   R # \K        V4       F|  w  rVf   K  \8        P:                  PM                  V4      V
,          PN                  pVPF                  P	                  R4      VPF                  R&   VS P                  P0                  V&   K~  	  R # u up	i )Nz'Unsupported data type for output node: c                   < V f   R # SP                   V P                  ,          pVSP                  9   d   SP                  V,          # VSP                  9   d   SP                  V,          # \	        RV  RSP
                   24      h)NzCould not find output node z	. Graph: )r  r  r   r!  r  r   )r  	seen_noder   s   & r2   get_actual_output_node=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node~  sx    > OOFKK8	-==33$":"::33I>>&5fXYtzzlS r1   c              3   4   <"   T F  pS! V4      x  K  	  R # 5ir[   r'   )r]   r  rU  s   & r2   ra   0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s     P<,V44<s   z8expected graph_outputs to be list or torch.fx.Node, got val)(r  r{  r  r
  rv  r  r  r   rj   r   r   r   r   r   r  r  r   r  r  r&   r>   ri  r  r  r   usersr   r   r<   r   r  rl   rm   r   r  r  rP  r*  Proxyr   )r   orig_outputsrS  r  tree_out_node
parent_outgraph_outputs	orig_node	user_nodeorJ  orig_output	proxy_outrU  s   f            @r2   finalize_outputs_ModuleFrame.finalize_outputsc  s     40**..t~~>	 T[[%<#++&&'((	 	 {{doo5$++DOOFKK,HI$++D1&A&J ! ,( 0P<PP<PP""M
 0F""D$;$;Y=O=O0J BOMM!]]	!*I ~~T__<$++I6%,,T]]9-EF "1 + 00J=!Q& -a 0-$)>?? J4P]K^J_`  	

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

9d..01"jj2249I9IJd%)		"r1   c                   \        V P                  P                  4       F>  w  rV P                  WP                  P                  R 4      VP                  4       4       K@  	  ^ pV P                  V,          pVP                  R8X  d0   V P                  V4       V^,          pV P                  V,          pK@  V P                  V4       V P                  P                   F'  pVP                  R8X  g   K  V P                  V4       K)  	  R# )r  r   r  N)
r*  r  r   rO  r  rv  ri  r   rk  run_from)r   rJ  r   node_idxs   &   r2   	run_outer_ModuleFrame.run_outer  s     !6!67GAJJq))--(9:D<L<L<NO 8 zz(#gg&NN4 MH::h'Dh OO))Dww("t$ *r1   c                D    V P                   '       d   \        V/ VB  R # R # r[   )r  rO  )r   r   r   s   &*,r2   rO  _ModuleFrame.print  s    <<<4"6" r1   c                   ^ pV\        V P                  4      8  Ed   V P                  V,          pVP                  R8X  d   \        RV 24      hV P	                  4        V P	                  RWP                  4       4       V P	                  V P                  4       \        V P                  4      pVP                  R8X  d   V^8X  d   V# V P                  4        V# \        VP                  P                  R/ 4      4      ^ 8X  d   \        RV 24      hTP                  R,          p^ RIHp \        V4      ^8X  d   We9   d   V P                  pMnVP                  R,          P                  4        UU	U
u. uF?  w  pw  rT	V	'       d   T
MRR	V9   d"   \        VP                  R	4      R,          4      M^ 3NKA  	  pp	pp
VRV V P                  8w  dJ   V P                  4        V P	                  R
V P                   4       V P	                  V P"                  4       V# Vf   \        R4      h\%        V P                  V4      '       d   Wt,          pV P	                  RV4       \'        VP                  R,          P)                  4       4      V,          p\+        V P,                  V P                  V P.                  V P0                  V P2                  V P4                  V V P                  V.,           VP                  R	4      ^ ,          V P6                  4
      P9                  V4      pV^,          pEK+  WpP                  8w  d   \        RV RV P                   24      hVP                  R8X  d7   V P2                  V P:                  ,          P=                  VP>                  4       V PA                  V4       V^,          pEK  R# u up
p	pi )r   r   z%unexpected placeholder node at index STEPr  r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYNr  	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   rl   rO  ri  r  re  r  rv  r  (torch._export.passes._node_metadata_hookrv  ri   r?   rc   r  r   r  r   keysr  r  r  r0  r1  r  r
  rn  r  r  rO   rk  )r   ro  
module_idxr   depthr  rv  node_module_stackr^   rr  r   next_modulenext_module_keys   &&           r2   rn  _ModuleFrame.run_from  s;   
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% *N:D	 "14AGGCL,-q
 *N " % !%(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
 !$5$55$12C1DD[\`\m\m[no  ww*$/33DKK@NN4 MH} )F%s   5AN?)r  r  r  r  r   r  r&   r
  r  r  r   r!  r   r  r  r1  r0  r  r  r[   )r(   r)   r*   r+   r   r:  r>  r)  rP  re  rk  rp  rO  rn  r0   rF   rG   s   @r2   r  r  I  sN     t9 t9l7,\( (0\>|*
%&#
a ar1   r  c                   ,   a  ] tR tRt o V 3R ltRtV tR# )r"  iB  c                   < V ^8  d   Qh/ S[ ;R&   S[P                  P                  ;R&   S[P                  P
                  ;R&   S[ ;R&   S[;R&   S[P                  P                  ;R&   # )r7   r  r  r  r  r  r&   )rV   r<   rT   rU   r   r  r?   )r@   rA   s   "r2   rB   _SubmoduleEntry.__annotate__B  sf     O  88??"  %	 
 
H  M  HHOO r1   r'   N)r(   r)   r*   r+   rE   r0   rF   rG   s   @r2   r"  r"  B  s      r1   r"  c                X    V ^8  d   QhR\         P                  P                  R\        /# )r7   
orig_graphroot_module)r<   r   r   r    )r@   s   "r2   rB   rB   L  s#     $ $EHHNN $AR $r1   c                 L   / p\        \        4      p\        \        4      p/ p\        T \	        V P
                  4      TTTTR R.RVP                   Uu/ uF.  pVP                  '       g   K  VP                  VP                  bK0  	  upVR7      P                  4        W43# u upi )Nr   )r&   )r   Nr   )
r   r   rd   r  r>   r   r
  rS  r  rp  )r  r  r  r0  r1  r  r-  s   &&     r2   r  r  L  s    +-J5@5FL&1#&6J24Oj	
 %66	
6 'EIIu&6	

   ik##	
s   B!,B!c                    V ^8  d   QhR\         P                  P                  R\        \        \
        3,          R\        /# )r7   r  rK  rp   )r<   rT   rU   r   rV   r?   )r@   s   "r2   rB   rB   e  s4     , ,HHOO,(,S#X,@C,r1   c                    VR 8X  d_   \        VP                  4       4      R,           F:  p\        W4      e   K  \        W\        P
                  P                  4       4       K<  	  . p\        V P                  P                  4       4       F  w  rVVf   K  W%,           p\        WaVP                  R4      ^ ,          R,           R7       \        W4       VP                  R4      ^ ,          pVP                  VP                  W1P                  V\        V4      4      4      WV34       K  	  VP                  \         P"                  ! ^ 4      R7       V F  w  rpV P%                  WV4       K  	  R# )r   :r   NNNr  rY   )rp   )r  )r   ry  r  r  r<   rT   rU   rh   ri   r,  rc   delattrr   rv  r   sortr  
itemgetterregister_module)	r  rK  rp   r  r$  r  childbase_fqnrB  s	   &&&      r2   r,  r,  e  s    |	()"--Cf*2vEHHOO,=> . HFOO1134=mESYYs^A5F5LM99S>!$]]3hI GH$V	
 5 MMh))!,M-"t+ #r1   c                   <   a  ] tR tRt o RtR tR tR tR tRt	V t
R# )	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                .    \        \        4      V n        R # r[   )r   rd   node_names_by_fqnr~   s   &r2   r   _IVals.__init__  s    !,S!1r1   c                    \        V\        P                  P                  4      '       d   VP                  P
                  # R # )F)rj   r<   _ops
OpOverload_schema
is_mutable)r   rO   s   &&r2   _is_mutable_IVals._is_mutable  s,    fejj3344>>,,,r1   c                   VP                   R8w  d   \        RVP                   : 24      hV P                  VP                  4      p\	        RVP                  V4       V'       gb   \        \        VP                  R,          P                  4       4      4      w  rEV P                  V,          P                  VP                  4       VP                  VP                  ^ ,          4      # )z9
Read state corresponding to a given intermediate value.
r  z,expected node.op to be 'call_function', got zChecking mutabilityr  )r   rl   r  rO   rO  r  r  r  r  r  r  r  r)  r   )r   mfr   br  rB  s   &&&   r2   rI  _IVals.read  s    
 77o% >twwkJ  T[[)#T[[!4 (499->#?#F#F#HIJFC""3'++DII6~~diil++r1   c                h   V EF  pV EF  pVP                   P                  pV P                  VP                  ,          pVP                   Uu. uF  qfP
                  V9   g   K  VNK  	  ppV F  pVP                  V4      ;_uu_ 4        VP                  R\        P                  P                  P                  P                  VP                  ^ ,          V34      p	\        P                  ! VP                  4      V	n        RRR4       K  	  EK	  	  EK  	  R# u upi   + '       g   i     K  ; i)zD
Update states corresponding to intermediate values that were read.
r  N)r&   r   r  r  r   r  r  r  r<   r  r  r  r  r   r'  r  )
r   
partitionsshared_submodulesr-  r   
node_namesrH  r   r   r  s
   &&        r2   rg   _IVals.update  s     ",***!33EII>
$)KKHKq66Z3GKH!D ..t44#(#4#4+!IINN0088!YYq\40$
 )-		$))(< 54 "	 + ", I 544s   D%DA8D  D1)r  N)r(   r)   r*   r+   rD   r   r  rI  rg   r0   rF   rG   s   @r2   r  r  ~  s#     (2
,$= =r1   r  c          	          V ^8  d   QhR\         P                  P                  R\        R\        \
        \        \
        ,          3,          /# )r7   r  r  r1  )r<   r   r   r    r   rV   rd   )r@   s   "r2   rB   rB     s=     	' 	'	'"	' S#c(]#	'r1   c                     VP                  4        F:  w  r4V'       d   \        W4      MTpV F  p\        W4      p\        WVV4       K  	  K<  	  R # r[   )ri   r   r   rf   )r  r  r1  r  namesr&   r  rY  s   &&&     r2   r  r    sF    
 ',,.	6?;2[D"#CF#&  /r1   c                    / pV  EFZ  p\        V4       EFF  w  r4\        VP                  VP                  4      p\	        VP
                  V4      pR pVRV  F  p\        VP                  VP                  4      '       g   K+  VP                  p	VP                  VP                  8X  d   VP                  R4      Ev r\        W4      P                  P                  V4       \        VP                  VP                  4      p\	        VP
                  V4      pWP                  n        WV&    EK  V'       d   K  V	P                  WhP                  4       RpEK  	  EKI  	  EK]  	  V# )FNrY   T)r*  rz  r  r  r  r  r  r&   r  rc   r   rh   r{  r  rO   r|  )r  rI  r  rJ  r-  r  rO   deduplicatedseenr  rp   r  seen_child_fqnseen_targets   &             r2   r&  r&    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 #"r1   c                   V ^8  d   QhR\         P                  P                  R\        \        \
        \        ,          3,          R\
        \        ,          R\        \        \        \        ,          3,          R,          /# )r7   r&   rD  scopemodule_id_to_inputs_removedN)r<   rT   rU   r   rV   r   r?   rd   )r@   s   "r2   rB   rB     sa     t( t(HHOOt(#tCy.)t( 9t( "&c3s8m!4t!;	t(r1   c           	       aa Sf   \        \        4      o\        V 4      S9   d   \        V 4      S\        V 4      ,          /# V P                  P	                  4        FV  w  po\        \        RS4      VW$.,           S4      pVP	                  4        F  w  rgSV,          P                  V4       K  	  KX  	  \        V RR4      pVe   \        VP                  4      ^ 8X  d   S# \        V\        P                  P                  4      '       g   \        R\!        V4       24      h\#        \%        R VP                  4      4      p	\        V	4      ^ 8X  d   RMV	R,          p
\%        R VP                  4      pV FY  p\'        WP(                  4      oSf   K  \        S4      S9   g   K0  \+        \%        VV3R lVP,                  4      4      Vn        K[  	  / pV	 F  pVP.                  V9  d   K  RpWP.                  ,           FQ  pVP1                  R4      pVR\        V4       V Uu. uF  pVP1                  R	4      ^ ,          NK  	  up8X  g   KO  Tp M	  Vf   K  WV&   K  	  \        4       pVP	                  4        EF  w  r\        VP2                  4      ^ 8  d   V\        V4      R p\5        V V4      p\        V\        P6                  \        P8                  34      '       g   \        R
\!        V4       24      hVP;                  V
4      ;_uu_ 4        VP=                  RRP?                  V4      4      pRRR4       VPA                  XRR7       VPC                  V4       VPE                  VP.                  4       EK  	  \        V \F        4      '       d   V PI                  4        \        V 4      V/# u upi   + '       g   i     L; i)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   z)expected graph to be torch.fx.Graph, got c                      V P                   R 8H  # )r   r   rH  s   &r2   r  _sink_params.<locals>.<lambda>*  s    144=#8r1   c                      V P                   R 8H  # )r   r  r  s   &r2   r  r  .  s    )>r1   c                 B   < V P                   S\        S4      ,          9  # r[   )r  r   )rH  r  	submodules   &r2   r  r  6  s    aff,G9,VVr1   rY   r  zBexpected state_attr to be torch.Tensor or torch.ScriptObject, got r  T)propagate_metar  )%r   rd   r   rh   ri   r%  r
   rg   r   r   r   rj   r<   r   r   rl   rm   r   filterr   rO   r>   r   r  rc   rZ  r   rR   rS   r  r  r   r  
erase_noder  r   r   )r&   rD  r  r  r  submod_id_to_inputs_removedr^   r  r   r&  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitrb  inputs_removed	attr_path
state_attrr  r  s   &&&f                  @r2   r%  r%    sB   , #*&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^^,,HeVWW&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$XY]^hYiXjk 
 &&~66 ,,Z)9LM 7 &&x&E499%! =$ &+,,vJ''U *J> 76s   )!N<
"OO)r   r   r   r    r  )Tr[   )r   )\r   r'  loggingr  r  collectionsr   collections.abcr   
contextlibr   r   dataclassesr   enumr   typingr	   r
   r<   torch.fx._pytreer   _pytreer  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch.exportr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   r   r   torch.fx._symbolic_tracer   torch.fx.graph_moduler   r   r   r   r   _remove_effect_tokens_passr   	getLoggerr(   r  __all__r"   r5   rI   rK   rt   rv   rT   rU   r   r   ABCr   r    r  r  r  r  r  r  r  r  r  r  r  r  r  rz  r\   r  r"  r  r,  r  r  r&  r%  r'   r1   r2   <module>r     s   
    	 # $ %  !    $ $ $ $ ? ( 3	 	 	 < U U 7 = !  $     ( (=0@	 	\
 \
~.".%((// ."bcgg ,S
 S
l!HH)V$NR
6$6BRM%")41J
v vr   $2,2B= B=J	'(#Vt( t(r1   