
    i3}                        % S SK r S SK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JrJr  S SKrS SKJrJr  S SKJr  S SKJrJr  S S	KJrJr  S S
KJrJr  S SKJr  S SKJ r J!r!J"r"J#r#  S SK$J%r%J&r&  S SK'J(r(J)r)J*r*  S SK+J,r,J-r-J.r.  \S 5       r/\/R`                  S\1S\,S\\   S\#4S j5       r2Sq3\4\5S'   S7S jr6\S\S   4S j5       r7S\Rp                  Rr                  S\1S\,S\\Rp                  Rt                     4S jr;S\,S\,S \Rp                  Rt                  S!\Rp                  Rt                  S"\#S#\4S$\
\1\-4   S%\
\1\.4   4S& jr<S\Rp                  Rr                  S'\S\,S#\4S\Rp                  Rr                  4
S( jr= S8S\Rp                  Rr                  S'\S\,S#\4S\Rp                  Rr                  4
S) jjr>\/R`                  S\,S*\S\,4S+ j5       r2S\Rp                  Rr                  S'\S\,S#\4S\
\1\\Rp                  Rt                     4   4
S, jr? S8S\Rp                  Rr                  S'\S\,S#\4S\
\1\\Rp                  Rt                     4   4
S- jjr@S\1S.\
\1\\Rp                  Rt                     4   S/\
\1\,4   SS4S0 jrAS1\Rp                  R                  SS4S2 jrC\ " S3 S45      5       rD\/R`                  S5\DS\
\1\,4   4S6 j5       r2g)9    N)contextmanagernullcontext)	dataclass)singledispatch)Dict	GeneratorListMapping)BackendDetailsPreprocessResult)CompileSpec)PartitionerPartitionResult)_maybe_duplicate_constant_nodesis_identical_graph)executorch_call_delegateget_lowered_module_name)get_control_flow_submodules)_unsafe_adjust_original_program&create_exported_program_from_submodulecreate_submodule_from_nodesLoweredBackendModule)get_fake_programupdate_to_real_program)	is_bufferis_lifted_tensor_constantis_param)ExportedProgram	InputSpec
OutputSpecc                     g)a  
A generic function the dispatch happens on the type of the first argument. There are currently to overloaded to_backend function:

Note: Python is dynamically-typed language and therefore cannot have proper method overloading as that requires the language to
be able to discriminate between types at compile-time. @to_backend.register will attach the function to to_backend() base on the type of the first
argument (type annotation is required). However, it can't take multiple types as arguments.

::

 def to_backend(
     backend_id: str,
     edge_graph_module: ExportedProgram,
     compile_specs: List[CompileSpec],
 ) -> LoweredBackendModule:

 def to_backend(
     edge_program: ExportedProgram,
     partitioner: Partitioner,
 ) -> ExportedProgram:
N )argss    b/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/exir/backend/backend_api.py
to_backendr%   +   s    , 	    
backend_idedge_programcompile_specsreturnc           	         [        U[        5      (       d   e[        R                  " 5        H  nXR                  :X  d  M  [
        R                  " U5      nUR                  UU5      n[        UU UR                  UUR                  S9nSUR                  0Ul        UR                  b  UR                  UR                  S'   Us  $    [        SU  S35      e)a  
Add overloaded implementations for to_backend:

::

 def to_backend(
     backend_id: str,
     edge_program: ExportedProgram,
     compile_specs: List[CompileSpec],
 ) -> LoweredBackendModule:


Requires the passed in exported program in Edge dialect to be executed in
the backend identified by backend_id. The forward method of the given
edge_graph_module will be targeted for execution.

Args:
    backend_id: The backend identifier.
    exported_program: An exported program in Edge dialect to target for
    lowering to the backend.
    compile_specs: A list of backend-specific objects with static
        metadata to configure the "compilation" process (e.g. it could be
        another dictionary itself).

Returns:
    LoweredBackendModule: A Module that has been lowered to the target backend.
    Internally, the lowered Module contains these special attributes:
    backend_id (str: backend id), __processed_module__ (str: a compiled module)
    compile_spec, original_module (original exported program)

Raises:
    NotImplementedError: The backend is not implemented (e.g. it was not found).
    This exception is derived from RuntimeError and should be caught accordingly.
    RuntimeError: The module cannot be processed by the backend.
r(   r'   processed_bytesr)   named_data_store_outputdebug_handle_map_delegate_info_metaBackend  was not found.)
isinstancer   r   __subclasses____name__copydeepcopy
preprocessr   r-   data_store_outputr/   metar0   NotImplementedError)r'   r(   r)   clscopied_edge_programpreprocess_resultlowered_modules          r$   _r@   D   s    R lO4444 ,,.%"&--"=25..#3 2)% 1 A A+(9(K(KN #$5$F$F#N !44@%99 ##$9: "!+ /, OD
EEr&   T_ENABLE_VALIDATIONc                      Sq g)zDisables validationFN)rA   r"   r&   r$   disable_validationrC      s
     r&   )NNNc               #   H   #    [         n [        5          Sv   U q g! U q f = f7f)a  
Disables checking functions (ex. if the partitioned graph is identical to
the original graph). This context manager should only be used in certain
scenarios (such as when it has been profiled that checks are taking too
long, and are not necessarily needed)
N)rA   rC   )existing_settings    r$   validation_disabledrF      s(      *.--s   " ""tagged_graph_moduletagowning_programc                    / nU R                   R                   H  nUR                  R                  SS5      U:X  d  M%  UR                  S:X  a  [        SU S35      eUR                  S:X  a  [        X$5      (       d/  [        X$5      (       d  [        X$5      (       d  [        SU S35      eUR                   H<  nUR                  R                  SS	5      nXa:w  d  M&  [        S
U SU SU SU S3	5      e   UR                  U5        M     U$ )z+
Return a list of nodes with the same tag.
delegation_tag outputzoutput node z should not be taggedplaceholderz\placeholder node for non-params, non-buffer, and non-tensor constants should not be tagged:  Nzconstant data node (z) is tagged with (z) but has user (z) which has tag ())graphnodesr:   getopRuntimeErrorr   r   r   usersappend)rG   rH   rI   	node_listnodeuser	users_tags          r$   _get_node_list_with_same_tagr\      s&    I#))//99==)2.#5ww(""\$7L#MNNww-' 66%n;;5nKK&vw{v||}~ 
 !%

$(IIMM2BD$I	$+"."6tf<NseScdhciiz  |E  {F  FG  !H#  !+ T"+ 0, r&   submodule_programcall_submodule_nodesubmodule_output_noder?   is_submoduletoplevel_input_specs_to_deletetoplevel_output_specs_to_deletec                    UR                   R                  n/ n	Un
UR                   H]  n[        U[        R
                  R                  5      (       a   UR                  U
R                  5       ;   a  ML  U	R                  U5        M_     S[        S[        4S jnUR                   R                  U5         [        X5      nUR                   R                  U5      nUR                   R                  [         U4[#        U	5      -   UR$                  5      nU" U5      UR&                  S'   UR                  S    Vs/ s H  nUR&                  S   PM     snUR&                  S'   UR)                  U5        UR                   R+                  U5        S S S 5        U(       a#  [-        U5      S:X  d   e[-        U5      S:X  d   eg [-        U5      S:  d  [-        U5      S:  a  [/        UWUU5        g g s  snf ! , (       d  f       Nk= f)Nepr*   c                     SnU R                   R                  R                   H(  n[        XR                  R                  SS5      5      nM*     US-   $ )z8
Generate a debug handle for the given ExportedProgram.
r   debug_handle   )graph_modulerQ   rR   maxr:   rS   )rd   rf   rY   s      r$   generate_debug_handle8_insert_lowered_submodule.<locals>.generate_debug_handle   sG     OO))//D|YY]]>1-MNL 0ar&   rf   r   val)rQ   owning_moduler#   r3   torchfxNodenamekeysrW   r   intinserting_beforer   get_attrcall_functionr   tuplekwargsr:   replace_all_uses_with
erase_nodelenr   )r]   rI   r^   r_   r?   r`   ra   rb   owning_graph_modulecall_delegate_argsinput_specs_to_deletecall_sm_inputrj   lowered_namelowered_nodecall_delegate_nodeout_args                    r$   _insert_lowered_submoduler      s    .33AA:,11}ehhmm44""&;&@&@&BB!!-0 2 /  c   
	"	"	3	34G	H./BS*0099,G066DD$Oe$677&&

 3H2W//D/I/I!/L*
/LGGLL/L*
& 	112DE!!,,-@A 
I 12a77723q888*+a/./!3'*+		
 4*
 
I	Hs   4BG39G.<G3.G33
Hpartition_resultc                 $   UR                   R                  5        GH_  u  pE[        XU5      n[        U5      S:X  a  [        R
                  " SU 35        M<  [        R
                  " SU SU 35        U(       d)  U R                  UR                  R                  5       5      O	[        5       nU   [        XU5      u  pSSS5        U R                  R                  5       n
WR                  R                  5       nU
R                  Ul        [        R
                  " SU  35        [        UUUW	U5      u  nnn[        UR                   UUR"                  5      n[%        UUU	UUUUU5        UR'                  5         GMb     U $ ! , (       d  f       N= f)zj
Partitioned and lowered the graph module based on the partition tag, this is to handle one graph module.
r   Did not find any nodes for tag For tag , found nodes NPartitioned graph module: )partition_tagsitemsr\   r{   loggingdebug_set_replace_hookgraph_signatureget_replace_hookr   r   rQ   output_noder:   r   r%   r'   r)   r   	_validate)rG   r   rI   r`   rH   delegation_specrX   replace_ctx	submodulecall_module_nodetagged_graph_module_output_noder_   r]   ra   rb   lowered_submodules                   r$   %_partition_and_lower_one_graph_moduler     s    !1 ? ? E E G 1n
	 y>QMM;C5AB^I;?@    11..??A  	 *E#+'I 
 +>*C*C*O*O*Q' ) ; ; = &E%I%I"23F2GHI 3
		
*+ '&&))
 	"!*+		
 	  "{ !H~ U [s   /F
F	c                     [        XX#5      n[        U5       H"  u  pVn[        XaUSS9nU R                  XX5        M$     U $ )z
Partitions the graph module into submodules based on tags, and then lowered the nodes with the same tag as one lowered module, including the submodule from control flow
Tr`   )r   r   _partition_and_lower
add_module)	rG   r   rI   r`   partitioned_modulerq   submod_nodepartitioned_submodules	            r$   r   r   Q  sV     ?~
  ;;MNe 4n4!
 	&&tC	  O r&   partitioner_instancec                    U R                  5          [        U 5      nU" U5      nUR                  n[        (       a1  [        UR
                  U R
                  5      (       d   SU S35       eO[        R                  " S5        UR                  c   SU S35       e[        XP5        UR                  R                  5        H  u  pg[        XV5        M     [        UR
                  UU5      nUR                   R"                   H  n	U	R$                  R'                  S	S5        M!     [)        UUR                   UR*                  UR,                  [        R                  " UR.                  5      [        R                  " UR0                  5      SUR2                  UR4                  /S
9	$ ! [         aG  n[        R                  " SU R
                   SU 35        [        R                  " U 5      n SnAGNSnAff = f)a  
Add overloaded implementations for to_backend:

::

 def to_backend(
     edge_program: ExportedProgram,
     partitioner: Partitioner,
 ) -> ExportedProgram:

Returns a semantically-equivalent program to the one given as input (represented
as a graph module in Edge dialect), but with portions of the program targeted for
delegation as determined by the partitioner.

Args:
    ExportedProgram: Program in Edge dialect.

    partitioner: An instance of the partitioner, in charge with tagging
    portions of the input program for delegation. A valid partitioner must return PartitionerResult
    including both tagged exported program and partitioner_tag: Dict[str, DelegationSpec], where each key is a tag name and
    the nodes with same tag will be fused a one subgraph and delegated to backend specififed in delegation spec.


Returns:
    ExportedProgram: The input program, with some portions targeted for delegation.
$Error in get_fake_program for graph , fallback to deepcopy: NThe partitioner # should not modify the graph module$Disabled validating the partitioner.Partitioner M needs a `partition_tags` field containing a mapping of tags to delegate specrK   	rootrQ   r   
state_dictrange_constraintsmodule_call_graphexample_inputs	constants	verifiers)r   r   	Exceptionr   warningrh   r6   r7   tagged_exported_programrA   r   r   r   r   r   r   rQ   rR   r:   popr   r   r   r   r   r   verifier)
r(   r   fake_edge_programepartitioner_resultr   rH   r@   rG   rY   s
             r$   r@   r@   i  s   > 8,\: ..?@0HH !#00%%
 
 	X 233VW	X 

 	>? 	))5z	*++xyz5 2A$3399;'(?E < /,, $))//		&- 0  !''/??*55--(?(Q(QR--(?(Q(QR)33*334
 
K  82<3L3L2MMefgehi	
 !MM,7	8s   F$ $
G5.<G00G5c                 b   0 nUR                   R                  5        GH  u  pV[        XU5      n[        U5      S:X  a  [        R
                  " SU 35        M<  [        R
                  " SU SU 35        U(       d)  U R                  UR                  R                  5       5      O	[        5       nU   [        XU5      u  pS S S 5        W	R                  R                  5       n[        R
                  " SU  35        [        U	UUW
U5      u  nnnUR                  U
R                  S'   UR                   U
R                  S'   XR                  S'   UU
R                  S	'   UU
R                  S
'   X:R                  S'   XR                  S'   UR                  U;  a  / XFR                  '   XFR                     R#                  U
R$                  5        GM     UR'                  5        Vs0 s H  o/ _M     nnUR                  5        H[  u  nnU R                  R(                   H;  nUR*                  S:X  d  M  UR$                  U;   d  M'  UU   R#                  U5        M=     M]     UR'                  5        H#  n[        UU   5      [        UU   5      :X  a  M#   e   U$ ! , (       d  f       GN= fs  snf )Nr   r   r   r   r   r'   compile_specr]   ra   rb   r`   r_   call_module)r   r   r\   r{   r   r   r   r   r   r   r   rQ   r   r   r'   r:   r)   rW   targetrr   rR   rT   )rG   r   rI   r`   backend_id_to_submodule_namerH   r   rX   r   r   r   r_   r]   ra   rb   keycreated_submodule_nodesr'   submodule_namerY   s                       r$   "_create_partitions_in_graph_moduler     s    $&  0 ? ? E E G 1n
	 y>QMM;C5AB^I;?@    11..??A  	 *E#+'I 
 !* ; ; = 	23F2GHI
 3
		
*+ />.H.Hl+0?0M0Mn-5F12* 	>? , 	?@ 1=n-9N56%%-IIGI()C)CD 	%%?%?@GG##	
y !H@ 3O2S2S2UV2U3Bw2UV&B&H&H&J"
N'--33Dww-'DKK>,I'
3::4@ 4 'K .224
*:673(4<
 
 	
 
 5
 #"o [V Ws   1J$J,
J)	c                     [        XX#5      n[        U 5       HI  u  pVn[        XaUSS9nUR                  5        H$  u  nn	X;  a  XU'   M  XH   R	                  U	5        M&     MK     U$ )NTr   )r   r   _create_partitionsr   extend)
rG   r   rI   r`   backend_id_to_call_submodulesr@   r   $nested_backend_id_to_call_submodulesr'   nested_submoduless
             r$   r   r     s     %G~%!
 44GH1/An40
, 2779
><Mj9-9@@ARS	 : I )(r&   method_to_submodules_nodesmethod_to_tagged_edge_programc                 ,   UR                  5        VVVs0 s H:  u  p4UU Vs/ s H&  n[        R                  " UR                  S   5      PM(     sn_M<     nnnnUR                  5        VVVs0 s H$  u  p4X4 Vs/ s H  oUR                  S   PM     sn_M&     nnnn[        R
                  " 5        Vs0 s H  oR                  U_M     n	nX	;  a  [        SU  S35      eX   R                  Xg5      n
U
R                  5        H  nX#   nX   nX   nXs   n[        UUU5       H  u  nnnUR                  S   n[        UU UR                  UUR                  S9nSUR                  0Ul        UR                  b(  UR                  c   eUR                  UR                  S'   UR                  S	   nUR                  S
   nUR                  S   nUR                  S   n[!        UUUUUUUU5        M     M     gs  snf s  snnnf s  snf s  snnnf s  snf )zV
Lower all submodules nodes given in the method_to_submodule_nodes map to backend_id.
r]   r   r1   r2   r,   r/   Nr0   r`   ra   rb   r_   )r   r6   r7   r:   r   r4   r5   r;   preprocess_multimethodrr   zipr   r-   r9   r/   r0   r   )r'   r   r   method_namecall_submodule_nodesrY   method_to_partitioned_programmethod_to_compile_specssubclassbackend_name_to_subclassmethod_to_preprocess_resultrI   list_of_preprocess_resultslist_of_call_submodule_nodeslist_of_compile_specsr>   r^   r   r]   r?   r`   ra   rb   r_   s                           r$   lower_all_submodules_to_backendr   1  s     2L1Q1Q1S% 2T-K 	 -
 - MM$))$789,
 	

 2T " % 2L1Q1Q1S1S-K 	<PQ<PDii/<PQQ1S   5C4Q4Q4S 4S8#4S    1!HZL"HII 	!,CC)	
   37796C%@%M"'A'N$ 7 DDG&(!E
@2L
 !4 8 89L M1.% 1 A A*(9(K(KN #$5$F$F#N !44@%**666%99 ##$9: /33NCL-@-E-E0.* /B.F.F1/+ %8$<$<=T$U!%!#%./	=E
 :1
% R
 s4   G>-G9G>.H
:HH
4H9G>H
rQ   c                    U R                    H  nUR                  R                  SS5        UR                  R                  SS5        UR                  R                  SS5        UR                  R                  SS5        UR                  R                  SS5        UR                  R                  SS5        UR                  R                  SS5        M     g)	z*
Remove the used metadata from the graph.
rK   Nr'   r]   ra   rb   r`   r_   )rR   r:   r   )rQ   rY   s     r$   remove_used_metadatar     s     		&-		lD)		)40		6=		7>		nd+		-t4 r&   c                   B    \ rS rSr% Sr\\\4   \S'   \\\	4   \S'   Sr
g)MethodProgramsPartitionerSpeci  z
Since single dispatch for to_backend requires the first argument to be a
valid class, we create the following dataclass spec to hold the dictionaries
mapping the method name to the corresponding program, partitioner
method_to_edge_programmethod_to_partitionerr"   N)r5   
__module____qualname____firstlineno____doc__r
   strr   __annotations__r   __static_attributes__r"   r&   r$   r   r     s)     $C$899"3#344r&   r    method_edge_program_partitionersc                    U R                   nU R                  n0 n0 n0 nUR                  5        GH5  u  pgXa;   d   SU S35       eX   nUR                  5          [	        U5      n	U" U	5      nUR                  nXU'   [        (       a1  [        UR                  UR                  5      (       d   SU S35       eO[        R                  " S5        UR                  c   S	U S
35       e[        X5        UR                  R                  5        H  u  p[!        X5        M     [#        UR                  UU5      nUR                  5        H  u  nnUU;  a  0 UU'   UUU   U'   M     GM8     UR                  5        H  u  nn[%        UUU5        M     UR'                  5        H  nXe;   a  XV   nUR                  5         [)        UR                  R*                  5        [-        UR                  UR                  R*                  UR.                  UR0                  [        R                  " UR2                  5      [        R                  " UR4                  5      SUR6                  UR8                  /S9	X6'   M  X   X6'   M     U$ ! [
         aG  n
[        R                  " SUR                   SU
 35        [        R                  " U5      n	 Sn
A
GNfSn
A
ff = f)a[  
Add overloaded implementations for to_backend:

::

 def to_backend(
    method_edge_program_partitioners: MethodProgramsPartitionerSpec
) -> Dict[str, ExportedProgram]:

Returns a semantically-equivalent dictionary of programs to the programs given as input (represented
as a graph module in Edge dialect), but with portions of the program targeted for
delegation as determined by the partitioner.

Args:
    method_edge_program_partitioners: contains two mappings,
    - method_to_edge_program: mapping of method names to their respective programs in Edge dialect.
    - method_to_partitioner: mapping of method names to an instance of the partitioner, in charge with tagging
    portions of the specified program for delegation. A valid partitioner must return PartitionerResult
    including both tagged exported program and partitioner_tag: Dict[str, DelegationSpec], where each key is a tag name and
    the nodes with same tag will be fused a one subgraph and delegated to backend specififed in delegation spec.


Returns:
    ExportedProgram: The input program, with some portions targeted for delegation.
zPartitioner for method z is not providedr   r   Nr   r   r   r   r   r   )r   r   r   r   r   r   r   r   rh   r6   r7   r   rA   r   r   r   r   r   r   rr   r   rQ   r   r   r   r   r   r   r   )r   r   r   )partitioned_and_lowered_exported_programs#backend_id_to_method_submodules_map!method_to_tagged_exported_programr   r   r(   r   r   r   r   rH   r@   "backend_id_to_call_submodule_nodesr'   r   method_to_submodule_nodess                      r$   r@   r@     s   : >TT<RR02-*,'(*%-B-H-H-J)1	C$[M1AB	C1-: 	< 0 > 22CD"4"L"L9P+6 %'44))  \ ""6!77Z[\ 
 OOBC --9	~.//|}	~9 	6E(77==?FC+,CI @ .@#00#.
* 0557
 !DDBD3J? % 0
; 8] .Kp 
-	2	2	4	!'%-	
 
5 .224;&G&T##--/ !8!E!E!K!KLET,99-::@@ 7 G G2=="&--+==# #'--+==#  $1;;2;;<F5B$ '3 6B- 54 54]  	<OO6|7P7P6QQijkilm !%l ;		<s   I??
K	<KK)r*   N)F)Er6   r   
contextlibr   r   dataclassesr   	functoolsr   typingr   r   r	   r
   rn   'executorch.exir.backend.backend_detailsr   r   +executorch.exir.backend.compile_spec_schemar   #executorch.exir.backend.partitionerr   r   executorch.exir.backend.utilsr   r   executorch.exir.delegater   r   executorch.exir.graph_moduler   &executorch.exir.lowered_backend_moduler   r   r   r   %executorch.exir.program._fake_programr   r   torch._export.utilsr   r   r   torch.export.exported_programr   r   r    r%   registerr   r@   rA   boolr   rC   rF   ro   GraphModulerp   r\   r   r   r   r   r   r   Graphr   r   r"   r&   r$   <module>r     s     2 ! $ 1 1  T C L
 W D  O N P P 	 	0 AFAF!AF $AF 	AF AFH   D  .Y'78 . .  -- 	  $  
%((--	 F=
&=
#=
 =
 !88==	=

 )=
 =
 %)i$8=
 &*#z/%:=
@H--H%H $H 	H
 XXH^ 	--% $ 	
 XX0 S!S%S S SlS#--S#%S# $S# 	S#
 
#tEHHMM"
"#S#t 	)--)%) $) 	)
 
#tEHHMM"
"#)6OO $S$uxx}}*=%= >O $(_(<#=O 
	Od5 54 5 5 5 5 |5&C|5	#
|5 |5r&   