
    i                        S SK r S SKrS SKJr  S SKJrJrJrJrJ	r	J
r
Jr  S SKrS SKJs  Jr  S SKJrJr  S SKJr  S SKJr  S SKJrJr  S SKJr  S S	KJr  S S
KJ r   S SK!J"r"J#r#  S SK$J%r%  S SK&J'r'  S SK(J)r)  S SK*J+r+  S SK,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6  S SK7J8r8J9r9J:r:J;r;J<r<J=r=   " S S\R|                  R~                  5      r@S\R                  R                  SS4S jrCS\R                  R                  S\.S\R                  R                  4S jrD S*S\.S\R                  R                  S\R                  R                  S\FS\GS\
\/\\F\\R                  \R|                  R                  4   4   \\F\\R                  \)\R                  4   4   \\F\14   \\F\54   4   4S jjrKS\R                  R                  S\.S\FS\R                  R                  S\GS\
\.\\F\14   \\F\54   4   4S jrL S*S\R                  R                  S \<S\FS!\GS\
\R                  R                  \R                  R                  4   4
S" jjrMS#\R                  R                  S\\
\F\@\R                  R                  4      4S$ jrNS#\R                  R                  S\\@   4S% jrOS\.S&\R                  R                  S'\\F\14   S(\\F\54   SS4
S) jrPg)+    N)defaultdict)AnyDictListOptionalSetTupleUnion)_PTEFile_serialize_pte_binary)NamedDataStoreOutput)CompileSpec)executorch_call_delegateget_lowered_module_name)emit_program)_get_submodule)MemoryPlanningPass)	make_specSpecPropPass)Program)Value)FakeScriptObject)
FakeTensor)
ConstantArgumentExportedProgramExportGraphSignature	InputKind	InputSpecModuleCallEntryModuleCallSignature
OutputKind
OutputSpecTensorArgument)erase_nodesfuse_as_graphmoduleinsert_subgmlegalize_graphNodeList	topo_sortc                     ^  \ rS rSr% Sr\\S'   \\S'   \\	   \S'   \
\S'   \\   \S'   \\\\4      \S'    S%S
\
S\S\S\\	   S\\   SS	4U 4S jjjrS\\\\4      SS 4S jr\S\4S j5       r\S\4S j5       r\S\\	   4S j5       r\S\
4S j5       r\S\\   4S j5       r     S&S\S\S\\   S\\   S\S\4S jjr  S'S\S\S\4S jjrS \S!\\S"4   S\4S# jrS$r U =r!$ )(LoweredBackendModule6   z
A subclass of nn.Module that is generated for modules containing
delegated functions. This is can be created by calling `to_backend`.
_backend_id_processed_bytes_compile_specs_original_exported_program_named_data_store_outputmetaNedge_program
backend_idprocessed_bytescompile_specsnamed_data_store_outputreturnc                 l   > [         TU ]  5         Xl        X l        X0l        X@l        XPl        S U l        g N)super__init__r0   r-   r.   r/   r1   r2   )selfr3   r4   r5   r6   r7   	__class__s         e/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/exir/lowered_backend_module.pyr<   LoweredBackendModule.__init__G   s5     	*6'% /+(?%	    memoc                 8   [        [        R                  " U R                  R                  5      [        R                  " U R                  R
                  5      [        R                  " U R                  R                  5      U R                  R                  [        R                  " U R                  R                  5      [        R                  " U R                  R                  5      U R                  R                  [        R                  " U R                  R                  5      /S9n[        UU R                  U R                  [        R                  " U R                  U5      U R                   S9n[        R                  " [#        U S0 5      5      Ul        U$ )Nrootgraphgraph_signature
state_dictrange_constraintsmodule_call_graph	constants	verifiers)r3   r4   r5   r6   r7   r2   )r   copydeepcopyr0   graph_modulerF   rG   rH   rI   rJ   rK   verifierr+   r-   r.   r/   r1   getattrr2   )r=   rB   copied_programress       r?   __deepcopy__!LoweredBackendModule.__deepcopy__X   s*   (t>>KKL-- ? ? E EF MM//?? 66AA"mm//AA #mm//AA 55??}}T%D%D%M%MNO
" #''' 11--(;(;TB$($A$A
 99WT6267
rA   c                     U R                   $ )z
Returns the backends name.
)r-   r=   s    r?   r4   LoweredBackendModule.backend_idu   s    
 rA   c                     U R                   $ )z;
Returns the delegate blob created from backend.preprocess
)r.   rW   s    r?   r5   $LoweredBackendModule.processed_bytes|   s    
 $$$rA   c                     U R                   $ )zi
Returns a list of backend-specific objects with static metadata to configure the "compilation" process.
)r/   rW   s    r?   r6   "LoweredBackendModule.compile_specs   s    
 """rA   c                     U R                   $ )z"
Returns the original EXIR module
)r0   rW   s    r?   original_module$LoweredBackendModule.original_module   s    
 ...rA   c                     U R                   $ )z%
Returns the Named Data Store Output
)r1   rW   s    r?   r7   ,LoweredBackendModule.named_data_store_output   s    
 ,,,rA   extract_delegate_segmentssegment_alignmentconstant_tensor_alignmentdelegate_alignmentmemory_planningc           
      p    [        [        [        U R                  US9U R                  S9UUUUS95      nU$ )z?
Returns a buffer containing the serialized ExecuTorch binary.
)rf   )program
named_data)pte_filerb   rc   rd   re   )bytesr   r   rh   r7   )r=   rb   rc   rd   re   rf   outs          r?   bufferLoweredBackendModule.buffer   sK     !! LLLI#;; +D"3*C#5	
 
rA   emit_stacktracec                 	   SSK JnJn   [        R                  " U R
                  5      nUR                  R                   Vs/ s Hc  nUR                  S:X  d  M  UR                  UR                  R                  ;  d  M;  UR                  UR                  R                  ;  d  Ma  UPMe     nnUR                  R                  5       nUR                  R                  U5        [        UR                  R                  5       H0  nUR                  S:w  d  M  UR                  R                  U5        M2     UR                  R                   Hz  nUR                  S:X  d  M  UR                  UR                  R                  ;   d&  UR                  UR                  R                  ;   d  M_  UR                  R                  U5        M|     [!        UR"                  U 5      n	UR                  R%                  U	5      n
UR                  R'                  [(        U
/UQ75      nU R
                  R                  R                  5       R*                  S   n[-        U Vs/ s H  n[/        UR0                  S   5      PM     sn5      UR0                  S'   [-        U Vs/ s H  ofR0                  S   PM     sn5      UR0                  S'   / n[3        [5        U5      5       H\  nUR                  R'                  [6        R8                  X4S9nUR0                  S   U   UR0                  S'   UR;                  U5        M^     UR                  R=                  U5        UR"                  R?                  5         UR                  RA                  5         U Vs/ s H-  n[C        [D        RF                  [I        UR                  S9S S9PM/     nnUR                  R                  nUR                  R                  nUR                  RJ                   Vs/ s H=  nUU;  d  M  UU;  d  M  [M        [N        RP                  [I        WR                  S9S S9PM?     nn[S        UR"                  UR                  U" [U        UUS	9UR"                  5      0 URV                  URX                  S URZ                  /S
9nUc
  []        5       nU" U[_        5       U5      n[a        UUS9Rb                  nU$ s  snf s  snf s  snf s  snf s  snf )Nr   )_get_updated_graph_signature
_transformplaceholdervalspec)argsnamekindargtargetinput_specsoutput_specs)rE   rF   rG   rH   rI   rJ   example_inputsrL   )ro   )2 executorch.exir.program._programrq   rr   rM   rN   r0   rF   nodesoprx   rG   inputs_to_buffersinputs_to_parametersoutput_node
erase_nodereversedr   rO   get_attrcall_functionr   rv   tupler   r2   rangelenoperatorgetitemappendoutput	recompilelintr"   r!   USER_OUTPUTr#   user_inputsr   r   
USER_INPUTr   r   rI   rJ   rP   r   r   r   rh   )r=   ro   rf   rq   rr   lowered_exported_programnodeall_input_nodesr   lowered_namelowered_nodedelegate_nodeoriginal_output_nodesgetitem_nodesigetitem_noder   r   r   
user_inputr~   exported_programemitted_programs                          r?   rh   LoweredBackendModule.program   s   	

	< $(==1P1P#Q 
 166<<

<=(  II/??QQR	  II/??TTU < 	 

 /44@@B 	!&&11+> 5;;AABDww-'(..99$? C
 -2288Dww-'		5EEWWW99+;;PPQ )..99$? 9 /$114
 055>>|L 166DD$|&Fo&F
 ++11==?DDQG 	 &+5JK5JTYtyy'(5JK&
6" %**?@*?$YYu*?@%
5!
 s012A399GG  #' H L (5'9'9%'@'CLe$  . 3 	!&&--m< --779 &&++- !.
 !. ++"(9(9:
 !. 	 
 %44II 	 5DDVV 7FFRR	
 S
!55 "33I))"		2
 S 	 	
 +)66*008$ +, )55	 6HH6HH/889#
& "02O%&6X&o

' 	 s

d L A$
	
s<   S"S "S&S"S#S(4S-
S2S2.S2rv   kwargs.c                     [        U /UQ76 $ r:   )r   )r=   rv   r   s      r?   forwardLoweredBackendModule.forward`  s    
 (4t44rA   )r-   r/   r1   r0   r.   r2   r:   )F   NNN)FN)"__name__
__module____qualname____firstlineno____doc__str__annotations__rk   r   r   r   r   r   r   r   r<   intrT   propertyr4   r5   r6   r^   r7   boolr   rm   r   rh   r   r	   r   __static_attributes____classcell__)r>   s   @r?   r+   r+   6   s   
   !0/&  4S>
"" CG%  	
 K( "**>!? 
 "$sCx.!9 >T :  C     % % % #tK0 # # / / / -2F)G - - +0!$37,0.2#'  $,C=	
 %SM , 
< !&.2gg ,g 
	gV55 s
#5 
	5 5rA   r+   gmr8   c                    [        U R                  R                  5       GH*  nUR                  S:X  d  M  U R                  R	                  U5         [        UR                  5      S:X  d   eUR                  S   n[        U[        R                  R                  5      (       a~  UR                  R                  S5      n[        U[        5      (       aN  [        [        U5      5       Vs/ s H/  n[        R                  R                  U5      U   R                   PM1     nnS S S 5        ["        R$                  " W5      u  pVU4Ul          g    g s  snf ! , (       d  f       N8= f)Nr      r   rt   )r   rF   r   r   inserting_beforer   rv   
isinstancetorchfxNoder2   getlistr   Proxyr   pytreetree_flatten)r   r   outputsrt   r   returnsout_specs          r?   _fixup_output_noder   i  s   (77h**40499~***))A,guxx}}55!,,**51C!#t,,
 FK3s8_#ETEHHNN73A6;;_   # 1 !' 3 3G <G 
DI! )# 10s   BE+6E!EE
E#	owning_programc                   ^ [         R                  R                  5       n0 mUR                  n/ n/ n/ nU R                  R
                   H  nUR                  S:w  a  M  UR                  UR                  ;   a3  UR                  R                  SS5      U:X  a  UR                  U5        Mb  UR                  UR                  ;   a3  UR                  R                  SS5      U:X  a  UR                  U5        M  UR                  U5        M     U H  n	UR                  U	U4S j5      n
U
TU	'   M      U H  nUR                  UU4S j5      n
U
TU'   M      U H  nUR                  UU4S j5      n
U
TU'   M      U R                  R
                   H0  nUR                  S:X  a  M  UR                  UU4S j5      n
U
TU'   M2     UR                  5         U R                  R                  Ul        X0l        U $ )a  
Modifies the graph of the given graphmodule with one that contains the same nodes as the original,
but with placeholders in order of (Params + Buffers) (User Inputs)

This is used by the delegate api which disturbs the placeholder ordering when creating a submodule
from partitioned nodes

Args:
    gm: The graph module that we want arranged
    owning_program: ExportedProgram that the submodule (gm) belongs to

Returns:
    The graph module in-placed arranged
rs   delegation_tagNc                    > TU    $ r:    xnode_maps    r?   <lambda>,arrange_graph_placeholders.<locals>.<lambda>  	    Xa[rA   c                    > TU    $ r:   r   r   s    r?   r   r     s	    hqkrA   c                    > TU    $ r:   r   r   s    r?   r   r     r   rA   c                    > TU    $ r:   r   r   s    r?   r   r     s	    x{rA   )r   r   GraphrG   rF   r   r   rx   r   r2   r   r   r   	node_copyr   _codegen)r   r   tag	new_graph
graph_signparam_nodesbuffer_nodesinput_nodesr   
param_nodenew_nodebuffer_node
input_noder   s                @r?   arrange_graph_placeholdersr   }  s   "  IH//J KLK77m# II888		.5<t$II555		.5<%t$ " "
&&z3HI' " $&&{4IJ ( $ "
&&z3HI' "
 77m#&&t-BC!  NN**IHIrA   original_programcall_module_noder   is_submodulec                   ^^ U R                   n/ n/ n0 n0 n	0 m0 mU(       d1  UR                   V
s0 s H  oR                  R                  U
_M     sn
O0 n[	        [
        5      nU(       d:  UR                   H*  nXR                  R                     R                  U5        M,     UR                  R                   GH-  nUR                  S:X  Ga  UR                  U;  a;  UR                  [        [        R                  [        UR                  S9SS95        M`  XR                     n[        UR                  [        5      (       d  UR                  U5        GOiUR                   R#                  SS5      U:X  Ga  UR                  U5        UR$                  [        R                  :X  a  M  XUR                  '   UR&                  nUU R(                  ;   aC  UR$                  [        R*                  [        R,                  4;   d   eU R(                  U   TU'   OUU R.                  ;   aR  UR$                  [        R0                  [        R2                  [        R,                  4;   d   eU R.                  U   TU'   OH[5        SU S35      eUR                  [        [        R                  [        UR                  S9SS95        UR                  S:X  d  GM  [	        [
        5      nUR6                  R9                  5        H  nUR                  U;   d  M  UR                  S	:X  a  UR&                  [:        R<                  :X  d$   S
U SUR                   SUR&                   35       eUR>                  S   n[        U[@        5      (       d   S[C        U5       35       eUU   RE                  UUR                     5        M     [G        UR>                  S   5       GH  u  nnUU;   Gae  [        U[H        RJ                  RL                  5      (       d   eUU   n[O        UU4S jU 5       5      (       a  U Vs/ s HG  nUR$                  [P        RR                  :X  d  M#  UR&                  T;   d  UR&                  T;   d  ME  UPMI     snS   n[U        U5      S:X  d   SUR&                   SU  S35       eUR                  [W        [P        RR                  [        UR                  S9UR&                  S95        UU	UR                  R                  '   GM7  UR                  [W        [P        RX                  [        UR                  S9SS95        GMs  [        U[H        RJ                  RL                  5      (       d3  UR                  [W        [P        RX                  [[        SUS9SS95        GM  UR                  [W        [P        RX                  [        UR                  S9SS95        GM     GM0     []        XgS9nUTTUU	4$ s  sn
f s  snf )a  
Args:
    original_program: The original program that we are paritioning
    gm: The partitioned graph module.
    call_module_node: The node in the original program that is calling the
        partitioned graph module.
    tag: The tag being used for this partitioned submodule. This is used to
        tell if a particular parameter/buffer/constant node is being tagged,
        aka consumed by the delegate.
    is_submodule: True if we are currently partitioning inside of a
        submodule (like cond's submodule). If we are inside of a submodule,
        we do not care about consuming params/buffers.

Returns:

    new_signature (ExportGraphSignature): The new signature for the
        partitioned graph module.
    new_state_dict (Dict[str, Union[torch.Tensor, torch.nn.Parameter]]): The
        new state dict containing the consumed params/buffers.
    new_constants (Dict[str, Union[torch.Tensor, FakeScriptObject,
        torch.ScriptObject]]): The new constants table containing the
        consumed constants .
    input_specs_to_delete (Dict[str, InputSpec]): The input specs that have
        been consumed by the delegate (param/buffer input nodes) and should
        be removed from the toplevel ExportedProgram.
    output_specs_to_delete (Dict[str, InputSpec]): The output specs that have
        been consumed by the delegate (buffer mutation nodes) and should be
        removed from the toplevel ExportedProgram.
rs   rw   Nry   r   Invalid input spec 	 receivedr   r   Invalid user , node.op is  and node.target is r   Invalid getitem type: r   c              3      >#    U  HN  nUR                   [        R                  :H  =(       a%    UR                  T;   =(       d    UR                  T;   v   MP     g 7fr:   )rz   r!   BUFFER_MUTATIONr|   ).0orig_output_specnew_constantsnew_state_dicts     r?   	<genexpr>%_get_new_signature.<locals>.<genexpr>V  s\       1B, )--1K1KK ,33~E H/66-G
 1Bs   AAz	Constant a   was tagged to be consumed by the buffer, and was found to also contain a buffer mutation. However this buffer mutation node was found to also be used as other types of outputs which is currently not supported. Please file an issue on Github. 

The toplevel program: 
 )rx   valuer}   )/rG   r~   r{   rx   r   r   r   r   rF   r   r   r   r   r   r#   r   r2   r   rz   r|   rH   	PARAMETERBUFFERrK   CONSTANT_TENSOR
CUSTOM_OBJRuntimeErroruserskeysr   r   rv   r   typeextend	enumerater   r   r   anyr!   r   r   r"   r   r   r   )r   r   r   r   r   old_signaturer~   r   input_specs_to_deleteoutput_specs_to_delete
input_specinput_node_to_sigtoplevel_output_node_to_sigoutput_specr   orig_input_specinput_targetbuffer_mutation_idxsusergetitem_idxr   r   orig_output_specsr   new_signaturer   r   s                            @@r?   _get_new_signaturer    s   T %44MKLNM  <I;T;TU;TZ		j	(;TU  @K4?P(55K'(<(<=DD[Q 6 77m#yy 11""&11*		:# /		:Oo11>BB""?3/6#=""?3"''9+?+??
 4Cdii0.55#3#>#>>*//!++!((4   
 4D3N3N$4N<0 "%5%?%??*//!11!,,!((4    3C2L2L$3M,/ ')<_<MY'WXX ""&11*		:# 77h@KD@Q (..33599 ;;?2t{{hFVFV7Ve&tfM$''BVW[WbWbVcdeV"&))A,K%#S  D/[0A/BCD  )5<<3DII> 6 #,DIIaL"9;,,%k588==AAAA(<Q(?%  1B    5F,4E 0/44
8R8RR - !1 7 7> I#3#:#:m#K -4E, ,(  ##45: '(8(?(?'@ A5 6F4FbJ: %++&%/%?%?$28H8H$I'7'>'> - //?/C/C/H/HI %++&%/%;%;$28H8H$I'+ $K?? ''"!+!7!7 0b L#' !''"!+!7!7 .K4D4D E#'G #:Y n )M
 	 K 	VR,s    W=8"XX X	submodulec                    [        XU5      n [        XX2U5      u  nnnnn	[        R                  " [	        UR
                  5      0 45      S   n
[        R                  " UR                  5      S   n[        U U R                  UU[        R                  " UR                  5      [        S[        / / XS95      /UUR                  /S9UU	4$ )a  
Creates an ExportedProgram from the given submodule using the parameters and buffers
from the top-level owning program

Args:
    submodule: submodule to create and exported program from
    owning_program: exported program containing the parameters and buffers used within
        the submodule

Returns:
    The ExportedProgram created from submodule
    input_specs_to_delete (Dict[str, InputSpec]): The input specs that have
        been consumed by the delegate (param/buffer input nodes) and should
        be removed from the toplevel ExportedProgram.
    output_specs_to_delete (Dict[str, InputSpec]): The output specs that have
        been consumed by the delegate (buffer mutation nodes) and should be
        removed from the toplevel ExportedProgram.
r   r   )inputsr   in_specr   rD   )r   r  r   r   r   r   user_outputsr   rF   rM   rN   rI   r   r    rP   )r  r   r   r   r   subgraph_signaturesubgraph_state_dictsubgraph_constantstoplevel_input_specs_to_deletetoplevel_output_specs_to_deleter  r   s               r?   &create_exported_program_from_submoduler    s    4 +9cJI 	#3,	&'
 !!5);)G)G#H""MNqQG""#5#B#BCAFH 	//.*"mmN,L,LM'!2w )%../	
" 	''' rA   	node_listskip_legalize_graphc                    [        U5      nSU-   n[        XU5      u  pgn[        U5        [        XXx5      n Sn	U R                  R
                   H)  n
U
R                  S:X  d  M  U
R                  U:X  d  M'  U
n	M+     U	c  [        SU SU R                   S35      e[        U5      S:X  a  [        US   R                  S	   [        5      (       a  U R                  R                  U	5         [        R                  R!                  U	5      S   R"                  nU	R%                  U5        U	R                  S	   UR                  S	'   U	S4Ul        SSS5        OKU	R(                   H;  nUR                  R+                  S
S5        UR                  R+                  SS5        M=     [-        X5        U(       d  [/        U 5        Sn	U R                  R
                   H)  n
U
R                  S:X  d  M  U
R                  U:X  d  M'  U
n	M+     U	c  [        SU SU R                   S35      eU	c   SU SU R                   35       eXi4$ ! , (       d  f       N= f)a  
Modifies the given graph module in-place to separate out the given nodes
into a submodule. The given node_list should form a fully connected
subgraph.

Args:
    gm: The graph module that we want to partition
    node_list: A list of nodes that belong in the partition

Returns:
    The submodule that has been partitioned, the call_module node in the
    toplevel graph module calling the submodule
fused_Ncall_modulez!The submodule created with nodes z did not form             one fully contained subgraph. Check that these nodes form a             fully contained graph. Partitioned graph: .r   r   rt   nn_module_stacksource_fn_stackz(No submodule was created with the nodes z in the graph )r)   r%   r   r&   rF   r   r   r|   r   r   r   r2   r   inserting_afterr   r   r   r   replace_all_uses_withrv   r   popr$   r'   )r   r  r   r  sorted_nodessubmodule_namesub_gmorig_inputsorig_outputssubmodule_noder   	proxy_out	user_nodes                r?   create_submodule_from_nodesr0    s6   & Y'L^N(;
.)%F v	b+	<BN77m#~(E!N  /	{ ;779xxjC
 	
 <A*\!_-A-A%-H*"U"U XX%%n5~6q9>>I00;$2$7$7$>INN5!,a0IN 65 (--INN0$7NN0$7 . !
 r N77m#~(E!N  /	{ ;779xxjC
 	
 	"V	1)N288*UV" !!S 65s   *A#H??
IrO   c                    / nU R                   R                   Hd  nUR                  S:X  d  M  UR                  [        :X  d  M+  [        XS5      u  p4n[        U[        5      (       d   eUR                  X4U45        Mf     U$ )a-  
Returns a list of lowered modules that are in the given graph (does not look
into submodules). Specifically, the returned value is a list containing a
tuple of (name of the lowered module that's stored in the graph module, the
lowered module itself, and the fx node that called this lowered module).
r   r   )	rF   r   r   r|   r   r   r   r+   r   )rO   lowered_submodulesr   rx   modules        r?   get_lowered_submodulesr4  E  sx     ""((77o%$++9Q*Q!/A!FD$f&:;;;;%%tT&:;	 )
 rA   c                     / nU R                   R                   H^  nUR                  S:X  d  M  UR                  [        :X  d  M+  [        XR                  S   R                  5      nUR                  U5        M`     U$ )zL
Returns a list of exported programs which were lowered by backen delegates
r   r   )	rF   r   r   r|   r   rQ   rv   rx   r   )rO   lowered_programsr   lowered_backend_modules       r?   get_lowered_backend_modulesr8  W  sh     ""((77o%$++9Q*Q%,\99Q<;L;L%M"##$:; )
 rA   call_delegate_noder  r  c                 	   U R                   R                   Vs/ s H!  nUR                  R                  U;  d  M  UPM#     snU R                  l        U R                  R                   Vs1 s H  nUR
                  c  M  UR
                  iM      nnU R                   R                   Vs/ s H!  nUR                  R                  U;  d  M  UPM#     snU R                  l        U R                  R                   H]  nUR                  S:X  aJ  UR                  U;   a8  [        UR                  5      S:X  d   eU R                  R                  U5        M[  M]    O   UR                  5        H  nUR
                  nUc   eX;   a  M  UR                  [        R                   :X  a  U R"                  U	 MH  UR                  [        R$                  :X  aH  UR&                  (       a  U R"                  R)                  US5        M  U R*                  UR
                  	 M  UR                  [        R,                  :X  a  U R*                  UR
                  	 M  [/        SU S35      e   U R                  R1                  5       n	U	c   e[        U	R2                  5      S:X  d   SU	 SU	R2                   35       eU	R2                  S    V
s/ s H@  n
[5        U
[6        R8                  R:                  5      (       a  U
R                  U;  d  M>  U
PMB     nn
[=        U5      4U	l        / n[?        UR                  RA                  5       5      nU H  nUR                  U;   d  M  UR                  S	:X  a  UR
                  [B        RD                  :X  d$   S
U SWR                   SUR
                   35       eUR2                  S   n[5        U[F        5      (       d   S[I        U5       35       eURK                  U5        U R                  R                  U5        M     URM                  SS9  [?        UR                  RA                  5       5      nU H  nUR                  S	:X  a  UR
                  [B        RD                  :X  d   eUR2                  S   n[5        U[F        5      (       d   e[O        U5       H4  u  nnUU:  d  M  UR2                  S   U[        U5      U-
  -
  4Ul          M     M     gs  snf s  snf s  snf s  sn
f )z
Directly modify the original exported program's signature and state dict
based on the consumed params/buffers in the delegate.
Nrs   r   r   r   r   zInvalid output node: z with args r   r   r   r   r   T)reverse)(rG   r~   r{   rx   _graph_signaturer|   r   rF   r   r   r   r   r   valuesrz   r   r   _state_dictr   
persistentr'  
_constantsr   r   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   sortr  )r   r9  r  r  r  currently_used_targetsr	  r   r  toplevel_output_noder{   new_output_argsgetitem_idxs
user_nodesr  user_idxr   idxs                     r?   _unsafe_adjust_original_programrI  f  su    +::FF5FJ>>&;; 	F5%%1 +;;GG(GJ 	
G  ( ,;;HH6HK??'== 	H6%%2 !&&,,77m#yy114::!+++ &&11$7 2  - ,224
!(('''1??i111 ,,\:__	 0 00$$ ,,00tD$//
0A0AB__	 9 99 ++J,=,=>!4ZL	JKK# 5( ,11==?+++ %%&!+\	34K@T@Y@Y?Z[\+
 (,,Q//C#uxx}}--AW1W 	/  
 "'!7 9 !L(..3356J99..?*t{{h>N>N/N]tfM$'':Nt{{m\]Nyy|Hh,,W0FtH~FV.WW,)""--d3  d# (..3356Jww/)dkkX=M=M.MMM99Q<(C((((-FAs#~!YYq\8s<7H17L+MN	 .	 _5(6Vs.   S%S%+S* S**S/S/=S4S4)F)QrM   r   collectionsr   typingr   r   r   r   r   r	   r
   r   torch.utils._pytreeutils_pytreer   executorch.exir._serializer   r   ,executorch.exir._serialize._named_data_storer   +executorch.exir.backend.compile_spec_schemar   executorch.exir.delegater   r   executorch.exir.emitr   executorch.exir.graph_moduler   +executorch.exir.passes.memory_planning_passr   %executorch.exir.passes.spec_prop_passr   r   executorch.exir.schemar   executorch.exir.tracerr   "torch._library.fake_class_registryr   torch._subclassesr   torch.export.exported_programr   r   r   r   r   r   r    r!   r"   r#   !torch.fx.passes.utils.fuser_utilsr$   r%   r&   r'   r(   r)   nnModuler+   r   GraphModuler   r   r   r   r   Tensor	ParameterScriptObjectr  r  r0  r4  r8  rI  r   rA   r?   <module>rc     s     # ? ? ?  $ $ F M C V - 7 J I * ( ? (   o5588?? o5f	588// D (DD.=D
XXDZ a%aa hhmma 
	a
 a eELL%(("4"445	56eELL"2E4F4FFG	GHij	aH@xx##@#@ 
@ hhmm	@
 @ ?Di0$sJ2GGH@N !&	V"V"V" 
V" 	V"
 588./V"r((&&	%)588==8
9:$((&&	
`%``  Y/` !j1	`
 
`rA   