
    ǄgX                        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mZmZmZmZmZmZmZ d dlZd dlmc mZ d dlmc mZ d dlmZ d dlmZ d d	lm Z m!Z!m"Z"m#Z#m$Z$m%Z% d d
l&m'Z' d dl(m)Z) d dlm*Z*m+Z+ ddl,m-Z- g dZ. G d de	      Z/da0ed        Z1	 dBdeejd                  ejf                  f   dejh                  jj                  de6de/de7f
dZ8 G d dejh                  jj                        Z9 G d de jt                        Z; G d dejh                  jj                        Z<	 dCd e!d!ee;   d"e<fd#Z=d$ej*                  j|                  d"dfd%Z?d& Z@d'e6d(e6d"e6fd)ZAd*ejh                  jj                  d+ejh                  jj                  fd,ZBd-ejh                  jj                  d"e6fd.ZCd-ejh                  jj                  d"ej*                  j                  fd/ZEd-ejh                  jj                  d"ej*                  j                  fd0ZFd1ejh                  jj                  de6fd2ZGd1ejh                  jj                  de6d3ejh                  jj                  fd4ZH G d5 d6      ZId7ej*                  j|                  d8e<fd9ZJ	 dDd:ejh                  jj                  d;ee6eKf   d<e6fd=ZLd ejh                  jj                  d>ee6ee6   f   d?ee6   fd@ZMdA ZNy)E    N)defaultdict)contextmanager)deepcopy)Enum)AnycastDictListOptionalSetTupleUnion)FakeScriptObject)reorder_kwargs)ConstantArgumentExportedProgram	InputKindModuleCallSignatureSymIntArgumentTensorArgument)is_fx_tracing_print_readable)
GetAttrKeySequenceKey   )_remove_effect_tokens)InterpreterModuleUnflattenedModule	unflattenFlatArgsAdapterc                       e Zd ZdZdZdZy)	_AttrKind	parameterbufferconstantN)__name__
__module____qualname__	PARAMETERBUFFERCONSTANT     ^/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/export/unflatten.pyr#   r#   "   s    IFHr.   r#   Tc               #   8   K   t         } da 	 d  | a y # | a w xY ww)NF)RUN_WITH_INTERPRETER)old_flags    r/   _disable_interpreterr3   +   s'      $H ('xs   	 from_obj	to_moduletarget	attr_kind
persistentc                    |j                  d      ^ }}|D ]>  }t        ||d       }|+t        j                  j	                         }t        |||       |}@ |t        j                  k(  r9t        | t        j                  j                        sJ |j                  ||        y |t        j                  k(  r1t        | t        j                        sJ |j                  || |       y |t        j                  k(  rQt        | t              rJ d       t        | t        j                  t        j                   f      sJ t        |||        y y )N.)r8   z2FakeScriptObject should only exist during tracing.)splitgetattrtorchnnModulesetattrr#   r*   
isinstance	Parameterregister_parameterr+   Tensorregister_bufferr,   r   ScriptObject)	r4   r5   r6   r7   r8   prefixfielditemts	            r/   _assign_attrrK   8   s9    \\#&NVU ItT*9!AItQ'	 I'''(EHH$6$6777$$UH5	i&&	&(ELL111!!%j!I	i((	(&
 	@?	@ 
 ""
 	
 
 		5(+ 
)r.   c                   l     e Zd ZdZdej
                  j                  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.
    graphc                 h    t         |           || _        | | j                  _        t        | _        y N)super__init__rM   owning_moduler1   _run_with_interpeter)selfrM   	__class__s     r/   rQ   zInterpreterModule.__init__b   s+     	
#'

 $8!r.   c                 n   | j                   J d       t               sSt        j                  j	                         s| j
                  s) t        | j                         j                  | g|i |S |rt        |      }| j                  t        |      d  }|D ]  }||v s|j                  ||           t        |      t        |      k(  sJ t        |      t        | j                        k(  sJ t        |      }t        j                  j                  | | j                        j                   |ddiS )Nz&Didn't finalize this InterpreterModulerM   enable_io_processingF)graph_moduler   r=   compileris_dynamo_compilingrS   typeforwardlist	arg_nameslenappendtuplefxInterpreterrM   run)rT   argskwargsarg_listkwarg_names
kwarg_names         r/   r]   zInterpreterModule.forwardk   s     ,V.VV,NN..08Q8Q 34))*224I$I&II
  :"nnS]_="- <J!V+ z(:;< ;'3v;6668}DNN(;;;;X88''DJJ'?CC,1 r.   c                 T   t         j                  j                  | | j                        | j                  d<   | j                  j                          g | _        | j                  j                  D ]7  }|j                  dk(  s| j                  j                  |j                         9 y )NrY   placeholder)r=   rc   GraphModulerM   __dict__lintr_   nodesopra   r6   rT   nodes     r/   finalizezInterpreterModule.finalize   sy     ).(<(<T4::(Nn%

 JJ$$ 	3Dww-'%%dkk2	3r.   c                 "    t        | d||||      S )Nr   r   rT   print_outputinclude_strideinclude_devicecoloreds        r/   print_readablez InterpreterModule.print_readable   #     
 	
r.   TFFF)r'   r(   r)   __doc__r=   rc   GraphrQ   r]   rt   r{   __classcell__rU   s   @r/   r   r   \   s;    
9xx~~9B3& 
r.   r   c            
       z    e Zd ZdZej
                  dej                  dej                  dee	   dee	   fd       Z
y)r!   zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    target_spec
input_spec
input_argsreturnc                      y)z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr-   )rT   r   r   r   s       r/   adaptzFlatArgsAdapter.adapt   s     	r.   N)r'   r(   r)   r~   abcabstractmethodpytreeTreeSpecr
   r   r   r-   r.   r/   r!   r!      sW     	__ OO I	
 
c r.   r!   c                   N     e Zd Z	 ddedee   f fdZd Zd Z	 	 	 	 ddZ	 xZ
S )	r   export_moduleflat_args_adapterc                 @  %&' t         (|           |j                  j                  t	        d      |j
                  D cg c]  }|j                   }}|d   dk(  sJ t        |j                        }t        |j                        | _        t        j                  j                         | _        t        |j
                        | _        || _        d| _        t        | _        t!        || j                         t#        ||        |j$                  | _        g | _        |j(                  }t+               }i }| j                  j,                  D ]  }	||	   }
t/        |
      |vrEt        j0                  j3                  |
j5                         |
j6                        |t/        |
      <   t9        |t/        |
         | |	t:        j<                         |j?                  |	        t+        | j                  j@                        }t+               }i }| j                  jB                  D ]  }	|	|v rd}|jD                  |	   }nd}||	   }t/        |      |vr|j5                         |f|t/        |      <   t9        |t/        |         d   | |	t:        jF                  |       |j?                  |	        |jI                         D ]  \  }	}|	|v s|	|v rd}t/        |      |v s$tK        |t        j0                  j2                        sd}|rKt/        |      |vr|df|t/        |      <   t9        |t/        |         d   | |	t:        jF                  d       t/        |      |vr||t/        |      <   t9        |t/        |         | |	t:        j<                          i }|jD                  jI                         D ]u  \  }}t/        |      |vr8tK        |t        jL                        r|j5                         }||t/        |      <   |t/        |         }t9        || |t:        jN                         w tQ        tR              &t+               }&fd	}t+               }| j                  jT                  D ]  }|jV                  tX        j<                  k(  s)|jV                  tX        jF                  k(  r|jZ                  rt]        |j^                  d
      sJ tK        |j`                  tb              sJ  |t/        |j(                  |j`                           |j^                  jd                  |j`                         |j?                  |j`                         |j?                  |j`                         |jV                  tX        jF                  k(  r|jZ                  r<|jV                  tX        jf                  k(  s|jV                  tX        jh                  k(  sbt]        |j^                  d
      sJ tK        |j`                  tb              sJ  |t/        |jD                  |j`                           |j^                  jd                  |j`                         |j?                  |j`                          |jD                  jI                         D ]`  \  }}||vst/        |      &v sJ d       &t/        |         d   \  }} |t/        |      ||       |j?                  j`                         b |j(                  jI                         D ]?  \  }}||vst/        |      &vr&t/        |         d   \  }} |t/        |      ||       A i '&jk                         D ]#  }|D cg c]  }|d   	 } }|D ]
  \  }!}| '|!<    % tm        | 'g        %'fd% %| g        | j                  jn                  D "cg c]  }"|"jp                  dk(  s|" c}"| _9        d| _:        tw        |      D #ci c]  \  }#}||#
 }$}#}| jy                  d      D ]  \  }	}|	|$vst{        |$      |$|	<    t}        | |$       | jy                  d      D cg c]  \  }}|	 c}}tS        |$j                               k(  sJ | j                  j                          y c c}w c c}w c c}"w c c}}#w c c}}w )Nz%Unflattening on JointExportModule NYIr    F)requires_grad)r7   T)r7   r8   c                 6    |    }|j                  ||f       y rO   )ra   )obj_id	node_nametarget_name	name_list
consts_maps       r/   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_mapG  s     "6*Ii56r.   namez?Constants should be either aliased or appear in graph signaturer   c                    t        | d      r| j                  j                  D ]i  }|j                  dk(  s|j                  v s"t        fd|j                     D              sDt        |j                   d d| j                          | j                         D ]  \  }}j                  |        |       ! y )NrM   rl   c              3   ^   K   | ]$  }|j                  d       dt               k(   & yw)r:   N)r;   r`   ).0fqnscopes     r/   	<genexpr>zJUnflattenedModule.__init__.<locals>.check_module_inputs.<locals>.<genexpr>  s1        #  IIcN<SZ8EA s   *-z was not sunk into the module z which has the graph: )	hasattrrM   rp   rq   r   anyAssertionErrornamed_childrenra   )moduler   rs   r   submodcheck_module_inputsinputs_to_states    `   r/   r   z7UnflattenedModule.__init__.<locals>.check_module_inputs  s    vw'"LL.. D =0 II8  '6tyy'A  
 -#yyk)GwNdekeqeqdrs & !' 5 5 7 3fT"#FE23r.   rl   )remove_duplicate)ArP   rQ   graph_signaturebackward_signature
ValueErrormodule_call_graphr   r   rM   r=   rc   r   r   adaptedr1   rS   _inplace_buffer_mutations_outline_submodulesrange_constraintsequality_constraints
state_dictset
parametersidr>   rB   cloner   rK   r#   r*   addnon_persistent_buffersbuffers	constantsr+   itemsrA   rD   r,   r   r^   input_specskindr   r8   r   argr6   strr   CONSTANT_TENSOR
CUSTOM_OBJvalues_sink_paramsrp   rq   input_placeholderscheck_input_constraints	enumeratenamed_modulesr`   _reorder_submoduleskeysro   ))rT   r   r   entryfqn_listexport_graphr   assigned_paramsid_to_paramr   paramr   assigned_buffersid_to_bufferr8   r%   tensor	is_bufferid_to_constr   r&   	_constantconsts_targetsr   added_params_bufferss
const_nameconstph_name_node_targetrJ   targetsnrs   i	fqn_orderr   r   r   rU   s)                                        @@@r/   rQ   zUnflattenedModule.__init__   s   
 	((;;GDEE+8+J+JK%EIIKK{b    3 34'(E(EFXX^^%
!)-*I*I!J!2$8!!,0D0DEL$/!.!@!@*,! #--
$'E57((33 	&Dt$E%y+).););KKM1D1D *< *BuI& BuI&#--	 %	& "%T%9%9%P%P!Q%(U  	 ((00 	'D--"
&006!
#D)&z-,2LLNJ+GRZ(RZ(+#**%   &%	', ',,. !	LD&&$2B*BI&z\)**2 !	vJl2 0LF,  F,Q/'..# f:[0.4K6
+6
+'11	9!	H NP*44::< 	MC(|;.h5'~~/H,4BxL)#BxL1I#,,		 8C47H
#&5	7 *-%%11 	-Avv,,,)***q||quuf---!!((C000!}//9:AEEJJ ""188,$((29+++ALL66Y66666Y111quuf---!!((C000!}..qxx89155::qxx ""188,+	-0 "/!8!8!>!>!@ 	3J/uI+UTU+'5	215
!"U)WjA$((2	3 )3399; 	<KC..f:Z/ '6
3A6
!"V*gs;	< 13%,,. 	-K%01qt1G1# -1%,"-	-
 	T?B/	34 	D"% "ZZ--#
M1ID#
 (,$*3H*=>3S!V>	>))5)A 	1GD!9$"%i.	$	1 	D),"&"4"4e"4"LMQMQUNNR
 
 	
 
 	

O Lf 2L#

 ? Ns$   bb
*b?b b>bc                     | j                         D ]d  \  }}t        |dz          t        |d      s!t        |j                  t
        j                  j                        sPt        |j                         f y )N:rM   )r   printr   rA   rM   r=   rc   r   )rT   r   mods      r/   _print_graphzUnflattenedModule._print_graph  sU    **, 	!HC#)sG$CIIuxx~~)Ncii 	!r.   c                    | j                   d   j                  }t        ||j                        }t	        j
                  ||f      \  }}|D cg c]  }|d   	 }}t               r_t        j                  j                  | | j                        j                  |ddi}	t        |	t              rt        |	      dk(  r|	d   S |	S ||j                  k7  r| j                  st!        d| dd|j                          | j"                  t%        d	      | j                  st!        d
       | j"                  j'                  |j                  ||      }d| _        t        |      |j                  j(                  k7  r.t%        dt        |       d|j                  j(                         | j*                  r\ddlm}
 | j                  du r(|D cg c]  }t1        d      t3        d      f|f }}n|} |
| j4                  || j6                         t        j8                  j;                         r9| j<                  s-t        j                  j?                  | | j                        | }n:t        j                  j                  | | j                        j                  |ddi}t	        j@                  ||jB                        S c c}w c c}w )Nr   r   rW   rX   FzGInput treespec does not match with exported module's: 
Input treespec: z. zExported module treespec: zeThere is no flat args adapter sepcified. Are you sure you are calling this with the right arguments? z5Adapting flat arg to match exported module's treespec)r   r   r   Tz<Flat args adaption failed, number of args mismatch Adatped: z 
Exported module: )"_check_input_constraints_for_graph)idxz<unknown location>)r   )"r   	signaturer   in_specr   tree_flatten_with_pathr   r=   rc   rd   rM   re   rA   rb   r`   r   r   r   	TypeErrorr   
num_leavesr   torch._export.utilsr   r   r   r   r   rZ   r[   _run_with_interpreterrm   tree_unflattenout_spec)rT   rf   rg   r   reordered_kwargsflat_args_with_pathr   x	flat_args
return_valr   r   new_flat_args_with_pathtree_outs                 r/   r]   zUnflattenedModule.forward  s   **1-77	)&)2C2CD'-'D'D#$(
$W $77aQqT7	7?--d$**-EII16J *e,ZA1E!!}$i'''<<''.ir301B1B0CD
 %%-S 
 ||QR 2288 ) 1 1&( 9 	
  $y>Y%6%6%A%AA#$$'	N#3 4,,5,=,=,H,H+IK  '' O||t#  )+ "a(*:N*OPRUV+' +
 +>'.'')@$BXBX >>--/8R8Rxx++D$**=yIHxx++D

+CGG16H $$Xy/A/ABB 8^+s   K!Kc                 "    t        | d||||      S )Nr   r   rv   s        r/   r{   z UnflattenedModule.print_readable   r|   r.   rO   r}   )r'   r(   r)   r   r   r!   rQ   r   r]   r{   r   r   s   @r/   r   r      sH     8<p&p $O4pd!GCV 
r.   r   r   r   r   c                 0    t        |       } t        | |      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
    hierachy 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.
    )r   r   )r   r   s     r/   r    r      s    , #6*FV%677r.   rM   c                    t        t        t        | j                                    }|j                  dk(  rt        |j                        dk(  sJ |j                  d   }|j                  }|dt        |       }|j                  j                         D ci c]  \  }}||
 }}}| j                  D 	ci c]  }	|	j                  dk(  s|	j                  |	! }
}	|D ]  }||j                     }||   }|
|   }| j                  |      5  | j                  dt        j                  j                  j                   ||f      |j"                  j                         D ]  \  }}|j"                  |<    	 ddd       |j%                  fd        t'        |t        |      d       }|f|_        yc c}}w c c}	w # 1 sw Y   KxY w)a  Transform buffer mutations from their functionalized form into a copy_
    node in the graph.

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

    Will become 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,)
    outputr   r   Nrl   call_functionc                     | uS rO   r-   )r   new_nodes    r/   <lambda>z+_inplace_buffer_mutations.<locals>.<lambda>X  s    1H;L r.   )nextiterreversedrp   rq   r`   rf   buffers_to_mutateinputs_to_buffersr   r   inserting_aftercreate_noder=   opsatencopy_metareplace_all_uses_withrb   )rM   r   output_nodereturn_argsmutation_node_to_buffer	mutationskvbuffers_to_inputsrs   input_name_to_nodemutationbuffer_name
input_name
input_nodeuser_outputsr  s                   @r/   r   r   +  s   , tHU[[123K>>X%#k.>.>*?1*DDD""1%K-??:c"9:;I*9*K*K*Q*Q*ST$!QATT$)KK 477m3K		4   N-hmm<&{3
'
3
""8, 	%((!5!5
H7MH !++- %1#$a %		% 	&&x1LMN" C/023L &(K3 U	% 	%s   F46F:F:A)F??G	c                 V    t        |       t        |      k  xr |dt        |        | k(  S )z2Check whether `candidate` is a prefix of `target`.N)r`   )	candidater6   s     r/   
_is_prefixr  c  s+    y>CK'QF3CS^,D	,QQr.   
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   r:   zChild module 'z(' is not a descendant of parent mldule 'za'.This is currently unsupported.Please try to make child module attach to parent module direclty.)r;   r`   RuntimeErrorjoin)r  r   parent_splitchild_splits       r/   _compute_accessorr&  h  s    R##C(L//#&K &S&'<7YK'OPZ| \P P
 	

 88KL 1 3455r.   r   yc                     dt         j                  j                  dt        fd} || j                         ||j                        k(  sJ y )NrM   r   c                 V  
 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k(  r|j                  nd}	|j                  | d|j                   d|	 dd	j                  |       d
       |
t        |      <    dj                  |      S c c}w c c}}w )Nr   c                     t        | t        j                  j                        rdt	        t        |                z   S t	        |       S )N%)rA   r=   rc   Noder   r   )r   	nodes_idxs    r/   arg_dumpz?_verify_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  s7    #uxx}}-S2c7!3444s8Or.   =r   r   z: [z](z, )
)r   r   rp   r   tree_maprf   rg   r   rq   r6   ra   r#  r   )rM   retr.  r   rs   r   	args_dumpkeyvaluer6   r-  s             @r/   
graph_dumpz-_verify_graph_equivalence.<locals>.graph_dump{  s   $&		S 	
 !- 	$GAt-3__Xtyy-QRcSRIR"(//(DKK"H"N"N"PC %q  I %)GG$>T[[BFJJ!Btwwiq499Y3G2HJK"#Ibh	$ yy~ Ss   D D%
)r=   rc   r   r   rM   )r   r'  r8  s      r/   _verify_graph_equivalencer9  z  s?    %((.. S ( agg*QWW"5555r.   gmc                 v    d}t        | d|       r|dz  }t        | d|       rd| }t        | ||       |S )Nr   _spec_r   )r   r@   )r:  specr   r   s       r/   	_add_specr>    sN    	A
"qcl
#	Q "qcl
#A3<DBdKr.   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S rO   )r>  rM   get_attrr   	fx_pytreetree_flatten_spec)r:  rs   r=  r   	spec_nodes        r/   _generate_flattenrD    sD    RD!!$'I88!!)"="=i?PQQr.   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S rO   )r>  rM   r@  r   r   r   )r:  rp   r=  r   rC  s        r/   _generate_unflattenrF    sD    RD!!$'I88!!&"7"7%9KLLr.   r   c                     |j                  d      ^ }}|D ];  }t        | |d       }| y t        |t        j                  j
                        s y |} = t        | |d       S )Nr:   )r;   r<   rA   r=   r>   r?   )r   r6   rG   rH   rI   r   s         r/   _get_submodulerH    sd    \\#&NVU 	dD)>&%((//2	 3t$$r.   module_to_addc                 "   |j                  d      ^ }}|D ]d  }t        | |d       }|+t        j                  j	                         }t        | ||       t        |t        j                  j                        s y|} f | j                  ||       y )Nr:   F)r;   r<   r=   r>   r?   r@   rA   
add_module)r   r6   rI  rG   rH   rI   r   s          r/   _add_submodulerL    s|    \\#&NVU 
dD)>XX__&FCv&&%((//2
 NN5-(r.   c                       e Zd Z	 ddej                  j
                  deej                  j                  df   dee	   de
e	ef   deej                  j                     f
dZd	 Zd
 Zd Zd Zd Zd Zd Zd Zy)_ModuleFrameN
flat_graphrp   .module_stackr   r   c
                    || _         || _        || _        || _        || _        || _        || _        || _        d| _        | j
                  d   | _	        |	|	| _
        n,t        t        j                  j                               | _
        | j                  | j                  v r| j                  | j                     | _        n*d | _        | j                  | j                  | j                  <   | j                  j                   | _        i | _        i | _        d | _        |yt)        |j                  | j                        }
t+        |j                  |
| j                  | j                  n| j                         |j                   j-                  |
      | _        |j/                  | j                        }|| j                  |j0                  j2                  dk(  sJ |j0                  j4                  d   }|j0                  j4                  d   }|j6                  J |j6                  J | j                   j9                  d       5  g }t;        |j2                        D ]/  }|j=                  | j                   j?                  d|              1 i }|j6                  D ]   }| j                   j?                  |      ||<   " tA        | j                  tC        |      |f|j0                        }tE        |jF                        D ]  \  }}| j                   jI                  dtJ        jL                  ||ftO        |tP              s|jR                  nd| 	      }tO        |tP              re|jR                  | j                  v s~tU        jT                  | j                  |jR                     jV                        |_+        || j$                  | j                  |jR                     <    	 d d d        | j                  j                   jY                  | j&                        5  g }|jF                  D ]  }tO        |tP              r|jZ                  |j=                  d        1|jR                  | j                  vr|j=                  d        [tO        |t\        t^        f      sJ |j=                  | j                  ja                  | j                  |jR                                   tc        | j                  j                  ||j0                        }| j                  j                   je                  tJ        jL                  |df      }| j                  j                   je                  tJ        jL                  |df      }t;        |j2                        D cg c]8  }| j                  j                   je                  tJ        jL                  ||f      : }}|j6                  D ci c]9  }|| j                  j                   je                  tJ        jL                  ||f      ; }}d d d        | j&                  J tC              | j&                  _3        | j&                  _4        y y y # 1 sw Y   }xY wc c}w c c}w # 1 sw Y   \xY w)
NF   r   r   _positional_arg_r   
_constant_)rq   r6   rf   r   )5rO  rp   
seen_nodesseen_modulesparentrP  	module_idr   verboser   r   r   r=   rc   r   cached_graph_modulerM   node_mapnode_to_placeholderparent_call_moduler&  rL  call_modulegetr   num_childrenchildren_specscontextr  rangera   rl   rD  rb   r   inputsr	  operatorgetitemrA   r   r   copyr  inserting_beforer7  r   r   remap_inputrF  r   rf   rg   )rT   rO  rp   rV  rW  rX  rP  rY  r   r   accessorr   	args_speckwargs_spec	arg_nodesr   kwarg_nodesr   r   r   flat_arg_nodeinput_nodesinputinputs_node	args_nodekwargs_noder   r  s                               r/   rQ   z_ModuleFrame.__init__  s    %
$(("!2$$R( DK+EHHNN,<=DK>>T...'+'8'8'HD$'+D$04Ddnn-[[&&
 =?#% ;?(TXX>H //7 KK11 '-ll&>&>x&HD#%))$((3	 T[[%<$$11Q666!))88;I#++::1=K$$,,,&&222++D1 *	 !7!78 WC$$TZZ%;%;>Nse<T%UVW '// ED(,

(>(>t(DK%E-KK9%{3%%	
 !*)*:*: ; *HC$(JJ$:$:*'//'- $.c3C#D  HH#-cU!3 %; 	%M "#'78 xx4??2-1YYtsxx7P7U7U-V* * 00 OOCHH5!**@ ""33D4K4KL "=?&-- 	E!%)9:u{{?R#**404??:#**40)%..1QRRR#** KK33DOOEJJ4OP	 2KK&&%% !KK--;;$${A&6	 #kk//==$${A&6
 #9#9#9: KK%%33H4D4DyRSnU	  )00	  t{{((66 ((;*:  ;"F **666+0+;D##(-8D##*Y &= * *r;" "s?   D9Y?A#YE3Y5=Y+
Y5>Y0Y5Y(+
Y55Y>c                    | j                   dk7  sJ d| d       |j                  | j                  u sJ | 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 module)	type_expr)
r   rM   rO  ri  rl   r   r\   rh  r  r]  )rT   r   placeholder_nodes      r/   add_placeholderz_ModuleFrame.add_placeholderP  s    xx2~K!8?KK~ww$//)))ZZ((. 	P#zz55aff5O	P !%		!&& 1&6  #	P 	Ps   2B>>Cc                    t        j                  t        j                  j                  | j
                  |j                        }t        j                  t        j                  j                  | j
                  |j                        }| j                  j                  |j                  ||      }t        j                  |j                        |_        || j                  |<   |S rO   )r   tree_map_onlyr=   rc   r,  rj  rf   rg   rM   r   r6   rh  r  r\  )rT   r   rf   rg   rs   s        r/   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_function[  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar.   c                    |j                   | j                  u sJ || 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$                  j.                  j(                  t         j"                  j$                  j0                  j2                  t         j"                  j$                  j4                  j,                  t         j"                  j$                  j6                  j,                  fv s/t9        |j                  d      r9|j                  j:                  dk(  r | j=                  |       | j                  |   S t?        d|j
                   d	|       # 1 sw Y   xY w)
Nzremap_input(r1  rl   r   r   r(   	_operatorz(Could not run remap_input() on op type: z
 for node ) rM   rO  r\  r   r]  rq   r   r`  r   ry  r^  rX  ri  
insert_argrj  r6   r=   r
  r  sym_sizeintrI   defaultunbindsumdim_IntListviewdiffr   r(   r|  r"  )rT   r   s     r/   rj  z_ModuleFrame.remap_inputi  s"   ww$//)))==##

\!A&'(((++A..DDM!%%))$((3;   #&&2 [[&&778O8OP V++66q$++:Q:QRS:TUV++A..TT_$HH		''++		##++		%%))		""..		##++		##++ ,/AHH4G4G;4V ''*==##:144&
1#N )V Vs   *6KKc                     g } j                   j                   j                        }| j                  |j                  D ]x  }t        |t        t        f      rS|j                   j                  v r)|j                   j                  |j                            Z|j                  d        lt        d|         fdt         j                  t        fd|D              |j                        }t!         j                  j                   j"                  |j                        }|}ng } j$                  j'                         D ]\  }|j(                  D ]K  }|j                   j                  vs|j                  |       |j                   j$                  |           \ ^  j"                  }t+        |      dk(  r|d   }t        |t,        t.        j0                  j2                  f      sJ  j4                  j7                  |       |y t        |t.        j0                  j2                        r|j8                  j                  d      n(|D 	cg c]  }	|	j8                  j                  d       c}	|j8                  d<   t+        |      dk(  r|| j                  j$                  |d   <   nt;        |      D ]u  \  }
}|	t.        j0                  j=                  |      |
   j>                  }|j8                  j                  d      |j8                  d<   | j                  j$                  |<   w  j@                  !tC         j@                   j                         y 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: )rV  r   r\  r]  r"  rM   )r   	seen_noderT   s     r/   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node  sx    > OOFKK8	-==33$":"::33I>>&5fXYtzzlS r.   c              3   .   K   | ]  } |        y wrO   r-   )r   r   r  s     r/   r   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s     P,V4Ps   r   r   val)"r   r`  r   rX  outputsrA   r   r   r   rV  ra   r"  rF  r   rb   r   rD  r^  r\  r   usersr`   r^   r=   rc   r,  rM   r   r  r   Proxyrs   r[  r9  )rT   orig_outputsr   r   tree_out_node
parent_outgraph_outputs	orig_node	user_nodeor   orig_output	proxy_outr  s   `            @r/   finalize_outputsz_ModuleFrame.finalize_outputs  s   **..txx8	 T[[%<#++ 	f~~&FG{{doo5$++DOOFKK,HI$++D1&A&J 	 0P<PP""M
 3D""D$;$;Y=O=O3J HUMM!]]//1 	!* I ~~T__<$++I6%,,T]]9-EF 00J=!Q& -a 0-$)>???

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

9d..01"jj2249I9IJd%)		"r.   c                     d}| j                   j                  D ]A  }| j                  ||j                  j	                  d      |j                                |dz  }C 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 )Nr   nn_module_stackr   rl   r   )	rO  rp   r   r  r`  r  rq   r  run_from)rT   r   rs   node_idxs       r/   	run_outerz_ModuleFrame.run_outer  s    OO)) 	DJJq$))--(9:D<L<L<NOFA	
 zz(#gg&NN4 MH::h'D gg&
 	h OO)) 	%Dww("t$	%r.   c                 4    | j                   rt        |i | y y rO   )rZ  r   )rT   rf   rg   s      r/   r   z_ModuleFrame.print  s    <<4"6" r.   c                    d}|t        | j                        k  r| j                  |   }|j                  dk7  sJ | j                          | j                  d||j	                                | j                  | j
                         |j                  dk(  r,t        | j
                        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]  \  }}|	 }}}|d t        | j
                         | j
                  k7  rI| j                          | j                  d	| j                         | j                  | j                         |S |J t        | j
                  |      r|t        | j
                           }	| j                  d
|	       t!        | j"                  | j                  | j$                  | j&                  | | j
                  |	gz   t)        |j                  d   j+                               t        | j
                           | j,                        j/                  |      }|dz  }|| j
                  k(  sJ | j1                  |       |dz  }|t        | j                        k  ry y c c}}w )Nr   rl   STEPr   r   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEY	outliningzCreating new stack frame for)r`   rp   rq   r   r  rP  r  r  r`  r"  (torch._export.passes._node_metadata_hookr  r   r   rM   r  rN  rO  rV  rW  r^   r   r   r  r  )
rT   r  
module_idxrs   r  r  node_module_stackpathtynext_modules
             r/   r  z_ModuleFrame.run_from  s   
TZZ(::h'D77m+++JJLJJvx)9)9);<JJt(()ww("t(()Q. $O %%'499==!2B78A="%MdV#TUU"ii(9:O
 O$)./A %)$5$5! *.3D)E)L)L)N%%T2D%! % !!93t'8'8#9:d>O>OO %%'

;1

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

9;G (OOJJOO%%%%5#45::<=c$BSBS>TU**	 (8$  a
 %(9(9999NN4 MHU TZZ(B%s   K3rO   )r'   r(   r)   r=   rc   r   r   r,  r
   r   r	   r   r   r>   r?   rQ   ry  r|  rj  r  r  r  r   r  r-   r.   r/   rN  rN    s     -1C9HHNNC9 UXX]]C'(C9 3iC9  %8 89C9 )C9J	7&PPMd*
%*#Mr.   rN  
orig_graphroot_modulec                     i }i }t        | t        | j                        ||d dgd|j                  D ci c]%  }|j                  r|j
                  |j                  ' c}|	      j                          y c c}w )Nr   )r   )rN  rb   rp   r   r   r   r  )r  r  rV  rW  r   s        r/   r   r   Q  s{    +-J/1Lj	
 %66	
 IIu&	

  ik	
s   *A5rX  r   rG   c                    |dk(  rWt        |j                               dd  D ]8  }t        | |      t        | |t        j
                  j                                : g }t        | j                  j                               D ]A  \  }}|	||z   }t        |||dz          t        | |       |j                  ||   ||f       C |j                  t        j                  d             |D ]  \  }}}| j                  ||        y )Nr   r   r:   )rG   r   )r6  )r^   r   rH  rL  r=   r>   r?   _modulesr   r   delattrra   sortrf  
itemgetterregister_module)rX  r   rG   r   childrenr   childr   s           r/   r   r   e  s     |	()!"- 	?Cfc*2vsEHHOO,=>	? HFOO1134 7e=tmE9S3Y?3u567 MMh))!,M-" ,4tU+,r.   r   r   c                    t        t              | j                  j                         D ]d  \  }t	        t        t        j                  j                        |||gz         }|j                         D ]  \  }}|   j                  |        f t        | d      sS | j                  }t        t        d |j                              }|d   }	t        d |j                        }
|
D ]`  }t        | |j                  j!                  d            +t#              v s9t%        t        fd|j&                              |_        b i }|D ]U  }|j(                  |vrd}||j(                     D ](  }|j!                  d      }|dt+        |       |k(  s&|} n |Q|||<   W g }|j                         D ]  \  }}t+        |j,                        dkD  r|t+        |      d }t        | |      }t/        |t        j0                  t        j2                  f      sJ |j5                  |	      5  |j7                  d	dj9                  |            }ddd       |j;                  d
       |j=                  |       |j?                  |j(                          t/        | t@              r| jC                          t#        |       |iS # 1 sw Y   wxY w)aT  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 containining 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.
    rM   c                      | j                   dk(  S )Nrl   rq   r   s    r/   r  z_sink_params.<locals>.<lambda>  s    144=#8 r.   rR  c                      | j                   dk(  S )Nr_  r  r  s    r/   r  z_sink_params.<locals>.<lambda>  s    )> r.   r:   Nc                 8    | j                   t                 vS rO   )r   r   )r   module_id_to_inputs_removed	submodules    r/   r  z_sink_params.<locals>.<lambda>  s    aff,G9,VV r.   r   r@  T)propagate_meta)"r   r^   r  r   r   r   r=   r>   r?   extendr   rM   filterrp   _recursive_getattrr6   r;   r   rb   rf   r   r`   r  rA   rD   rF   r  r	  r#  r  
erase_nodera   r   rt   )r   r   r   r   submod_id_to_inputs_removedr  r  rM   re  the_last_inputcall_module_nodesrs   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrr  r  r  s                       @@r/   r   r   {  s   * 9DD8I "??002 5i&2),ouv~'
# 0557 	5DAq'*11!4	5	5 67#**LLE&8%++FGFBZN >L! 
&vt{{/@/@/EF	  R	]6Q%QVIIDI
 @B 499O+
!$)), 	Bxx}H#e*%.%
		 )3 &/44 !#N4::< )jtzz?Q"3u:<0I+FI>Jj5<<9K9K*LMMM &&~6 N ,,Z)9LMN &&x&Edii() &+,vJ''N Ns   1"K		K	c                 H    |D ]  }t        | |      s y t        | |      }  | S rO   )r   r<   )objr  attrs      r/   r  r    s1     !sD!c4 !
 Jr.   )TrO   )r   )Or   rh  rf  collectionsr   
contextlibr   r   enumr   typingr   r   r	   r
   r   r   r   r   r=   torch.fx._pytreerc   _pytreerA  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   torch.fx._symbolic_tracer   torch.fx.graph_moduler   r   r   _remove_effect_tokens_passr   __all__r#   r1   r3   rD   rF   r>   r?   r   boolrK   r   ABCr!   r   r    r   r   r  r&  r9  r>  r,  rD  rF  rH  rL  rN  r   r  r   r   r  r-   r.   r/   <module>r     s   
   # %   E E E  $ $ $ $ ? 3  3 1 7 = U    ( (" !,ELL%"4"445!,xx!, !, 	!,
 !,HO
 O
dcgg  P
 P
h
 MQ88080I8845)UXX^^ 5) 5)pR
6# 6# 6# 6$6 6UXX__ 60%((// C R%((// R%((-- RMEHHOO MUXX]] M% % %") ) )UXX__ )$D DNEHHNN AR * GI,HHOO,(,S#X,@C,,f(HHOOf(#tCy.)f( 9f(Rr.   