
    i[)                         S SK r 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J	r	J
r
Jr  S SK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\5      r\" S5       " S S\5      5       rg)    N)ABCabstractmethod)Enum)AnyDictListSet)ReplaceViewCopyWithViewPass)NamedDataStore)experimental)ExportedProgramPreprocessResult)CompileSpec)CppWrapperCpu)move_to_device_passc                       \ rS rSrSrSrg)COMPILE_SPEC_KEYS   method_name N)__name__
__module____qualname____firstlineno__METHOD_NAME__static_attributes__r       d/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/aoti/aoti_backend.pyr   r      s    Kr   r   zOThis API and all of aoti-driven backend related functionality are experimental.c                      \ rS rSrSr\\S\4S j5       5       r\\S\	\\
4   4S j5       5       r\\S\	\
\
4   4S j5       5       r\\S\\   S\	\\R                  4   4S j5       5       r\\S\\   S\\R                     4S	 j5       5       r\S\4S
 j5       r\S 5       r\\R,                  S\\   4S j5       5       r\S\S\\   S\4S j5       r\S\S\4S j5       r\S\\   S\4S j5       rSrg)AotiBackend   a  
Base mixin class for AOTInductor-based backends.

This class provides common functionality for compiling models using AOTInductor
with different device targets (CUDA, Metal, etc.).

This is a mixin class, not an actual backend object, for aoti-driven backends.
Concrete backends (e.g., CudaBackend, MetalBackend) should inherit from both
BackendDetails and AotiBackend to get the full functionality.
returnc                     g)z@Return the device name for this backend (e.g., 'cuda', 'metal').Nr   clss    r   get_device_nameAotiBackend.get_device_name-        	r   c                     g)z>Return the set of supported fallback kernels for this backend.Nr   r$   s    r   get_supported_fallback_kernels*AotiBackend.get_supported_fallback_kernels3   r(   r   c                     g)z0Return the decomposition table for this backend.Nr   r$   s    r   get_decomposition_table#AotiBackend.get_decomposition_table9   r(   r   compile_specsc                     g)z<Return the AOTInductor compilation options for this backend.Nr   r%   r/   s     r   get_aoti_compile_options$AotiBackend.get_aoti_compile_options?   s     	r   c                     g)zeReturn the list of custom passes to apply after ReplaceViewCopyWithViewPass and before decomposition.Nr   r1   s     r   get_custom_passesAotiBackend.get_custom_passesG   r(   r   c                     g)aM  
Return whether to save the named data map to an external .ptd file.

If True, the SO blob and weights blob will be saved to a separate .ptd file
(e.g., aoti_cuda_blob.ptd) that must be provided at runtime.
If False, the data will be merged into the .pte file.

Default is False (merge with .pte file). Subclasses can override this.
Fr   r$   s    r   save_data_externally AotiBackend.save_data_externallyM   s     r   c                 ,    [         R                  " 5       $ )zmReturn extra context manager to apply during aoti_compile stage. By default returns an empty context manager.)
contextlibnullcontextr$   s    r   &get_extra_aoti_compile_context_manager2AotiBackend.get_extra_aoti_compile_context_managerZ   s     %%''r   missing_fallback_kernelsc              #     ^^^^#    U R                  5       m[        R                  m[        R                  mS[        S[
        S[
        SS4UUU4S jjnS[
        S[
        S[
        SS4UUU4S jjnU[        l        U[        l         Sv   T[        l        T[        l        g! T[        l        T[        l        f = f7f)	z
Context manager to collect unsupported fallback kernels during compilation.
Monitors both extern kernel calls and runtime lookup.
kernelargskwargsr"   Nc                 J   > UT;  a  TR                  U5        T" X/UQ70 UD6$ )N)add)selfrA   rB   rC   r?   +original_generate_c_shim_extern_kernel_callsupported_kernelss       r   Bgenerate_c_shim_extern_kernel_call_and_collect_unsupported_kernels|AotiBackend.collect_unsupported_fallback_kernels.<locals>.generate_c_shim_extern_kernel_call_and_collect_unsupported_kernelso   s<     ..(,,V4>#'- r   op_overloadc                 v   > [        US[        U5      5      nUT;  a  TR                  U5        T" X/UQ70 UD6$ )N_name)getattrstrrE   )rF   rK   rB   rC   kernel_namer?   9original_generate_fallback_kernel_with_runtime_lookup_aotrH   s        r   Pgenerate_fallback_kernel_with_runtime_lookup_aot_and_collect_unsupported_kernelsڊAotiBackend.collect_unsupported_fallback_kernels.<locals>.generate_fallback_kernel_with_runtime_lookup_aot_and_collect_unsupported_kernelsy   sO     "+wK8HIK"33(,,[9L$(,2 r   )r*   r   "generate_c_shim_extern_kernel_call0generate_fallback_kernel_with_runtime_lookup_aotrO   r   )r%   r?   rI   rR   rG   rQ   rH   s    `  @@@r   $collect_unsupported_fallback_kernels0AotiBackend.collect_unsupported_fallback_kernels_   s       >>@ << 	4 JJ 	B		&)	58		 			"		+.		:=				 		 O 	8 JZF	 < < J J < < J Js   BC 
B% C %B==C edge_programc                    U R                  5       nU R                  5       nU R                  U5      n[        XS:w  a  UOS5      n[	        5       " UR
                  5        U R                  U5      nU H1  n[        USS5      (       a
  U" U5        M  U" UR
                  5        M3     U(       a  UR                  U5      nUR                  5       n	UR                  R                  n
/ nUR                  R                   HE  nUR                  S:X  d  M  UR                  U
;   d  M'  UR!                  UR"                  S   5        MG     [%        5       nU R'                  U5         [(        R*                  " 5          U R-                  5          [(        R.                  R1                  U	[3        U5      US9n[5        U5      S:  aI  S	R7                  [9        U5      5      nU R;                  U5      n[=        S
U S[5        U5       SU S35      e SSS5        SSS5        SSS5        SnSn[?        W[@        5      (       a>  U H7  nURC                  S5      (       a  UnM  URC                  S5      (       d  M5  UnM9     OUnUb  Uc  [=        SU 35      e[E        US5       nURG                  5       nSSS5        [E        US5       nURG                  5       nSSS5        [I        5       nU R;                  U5      nURK                  US-   WSS5        U RM                  5       (       a  SU S3OSnURK                  US-   WSU5        [N        RP                  " U5        [N        RP                  " U5        [S        S0 URU                  5       S9$ ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f)zn
Preprocess the edge program and compile it using AOTInductor.
Weights are always separated from the SO file.
metalmpsrequires_exported_programFplaceholderval)optionsr   z
  - zMethod z missing fallback kernels (z total):
  - z%
Please add them to the AOTI backend.Nz.wrapper.soz.wrapper_weights.blobz5Could not find required files in compiled paths, got rb_so_blob   aoti__blob_weights_blobr   )processed_bytesdebug_handle_mapdata_store_output)+r&   r-   r2   r   r
   graph_moduler5   rN   run_decompositionsmodulegraph_signatureuser_inputsgraphnodesopnameappendmetasetrV   torchno_gradr=   	_inductoraot_compiletuplelenjoinsortedmethod_name_from_compile_specsRuntimeError
isinstancelistendswithopenreadr   add_named_datar8   osremover   get_named_data_store_output)r%   rX   r/   device_namedecomposition_tabler_   device_edge_programcustom_passescustom_passedge_program_moduleuser_input_namesuser_input_placeholdersnoder?   pathsformatted_kernelsr   so_path	blob_pathpathfso_data	blob_datanamed_data_storeexternal_tags                            r   
preprocessAotiBackend.preprocess   s    ))+!99;..}= 2)?+U

 	$%&9&F&FG --m<(K{$?GG/0/<<=	 ) "5"H"H## 288: />>JJ"$'--33Dww-'DII9I,I'..tyy/?@ 4
 .1U  55$
==?CFFHOO//#U+B%CW 0 E +,q0$,MM&9Q2R$S!!@@O"k]*EcJbFcEddr  tE  sF F; ;  1 I?
  	eT""==//"G]]#:;; $I	  G?i/GwO 
 '4 AffhG ! )T"aI # *+88G''j(@'1dS +.*B*B*D*DeK=&$ 	 	''/)9a	

 			'
		).JJL
 	
u IH??
 
D !  #"sU   !N(7NBN
NN(N: O
NN
N%	 N((
N7:
O	
Or   c                 h    [        [        R                  R                  UR	                  S5      5      $ )z3
Generate a CompileSpec for the given method name.
utf-8)r   r   r   valueencode)r%   r   s     r   !generate_method_name_compile_spec-AotiBackend.generate_method_name_compile_spec  s.     ))//w'
 	
r   c                     U HH  nUR                   [        R                  R                  :X  d  M-  UR                  R	                  S5      s  $    [        SU 35      e)z1
Extract the method name from the compile specs.
r   z-Could not find method name in compile specs: )keyr   r   r   decoder~   )r%   r/   specs      r   r}   *AotiBackend.method_name_from_compile_specs  sU     "Dxx,88>>>zz((11 " ;M?K
 	
r   r   N)r   r   r   r   __doc__classmethodr   rO   r&   r   r   r*   r-   r   r   typingr2   r5   boolr8   r=   r;   contextmanagerr	   rV   r   r   r   r   r}   r   r   r   r   r    r       s   	     tCH~    S#X     -	c6::o	   d;.? DDT    
T 
 
 ( ( 0CPSH 0  0d p
%p
 K(p
 
	p
 p
d 



 


 

 
K(
 

 
r   r    )r;   r   r   abcr   r   enumr   r   r   r   r	   ru   ;executorch.backends.aoti.passes.replace_view_copy_with_viewr
   ,executorch.exir._serialize._named_data_storer   executorch.exir._warningsr   'executorch.exir.backend.backend_detailsr   r   +executorch.exir.backend.compile_spec_schemar   'torch._inductor.codegen.cpp_wrapper_cpur   torch.export.passesr   r   r    r   r   r   <module>r      sk     	  #  ' '  H 2 U C A 3    U
# 

r   