
    i.                        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J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  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 S\\!S9r" " S S\#\5      r$\ " S S5      5       r%\ " S S5      5       r&\ " S S5      5       r'\" S5      \ " S S5      5       5       r(g)    N)ABCMetaabstractmethod)	dataclass)EnumEnumMeta)CallableListOptional)EdgeProgramManagerExportedProgram)experimental)Partitioner)EdgeCompileConfigExecutorchBackendConfig)PassManagerPassType)AOBaseConfig)	Quantizer   )	StageTypec                       \ rS rSrSrSrg)RecipeTypeMeta#   z,Metaclass that combines EnumMeta and ABCMeta N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/export/recipe.pyr   r   #   s    6r!   r   c                   :    \ rS rSrSr\\S\4S j5       5       rSr	g)
RecipeType)   z
Base recipe type class that backends can extend to define their own recipe types.
Backends should create their own enum classes that inherit from RecipeType:
returnc                     g)zr
Return the backend name for this recipe type.

Returns:
    str: The backend name (e.g., "xnnpack", "qnn", etc.)
Nr   )clss    r"   get_backend_nameRecipeType.get_backend_name/   s     	r!   r   N)
r   r   r   r   r   classmethodr   strr)   r    r   r!   r"   r$   r$   )   s*    
    r!   r$   )	metaclassc                        \ rS rSrSrSrSrSrg)Mode;   z
Export mode enumeration.

Attributes:
    DEBUG: Debug mode with additional checks and information
    RELEASE: Release mode optimized for performance
debugreleaser   N)r   r   r   r   r   DEBUGRELEASEr    r   r!   r"   r/   r/   ;   s     EGr!   r/   c                   n    \ rS rSr% Sr\\S'   Sr\\	\
R                  R                  \/\4      \S'   Srg)AOQuantizationConfigH   z
Configuration for torchao quantization with optional filter function.

Attributes:
    ao_base_config: The AOBaseConfig for quantization
    filter_fn: Optional filter function to selectively apply quantization
ao_base_configN	filter_fnr   )r   r   r   r   r   r   __annotations__r9   r
   r   torchnnModuler,   boolr    r   r!   r"   r6   r6   H   s5     ! BFIx%((//3!7!=>?Fr!   r6   c                   h    \ rS rSr% SrSr\\\      \	S'   Sr
\\\      \	S'   S\\\      4S jrSrg)	QuantizationRecipeV   a]  
Configuration recipe for quantization.

This class holds the configuration parameters for quantizing a model.

Attributes:
    quantizers: Optional list of quantizers for model quantization
    ao_quantization_configs: Optional list of AOQuantizationConfig objects that pair
                             AOBaseConfig with optional filter functions
N
quantizersao_quantization_configsr&   c                     U R                   $ )zm
Get the quantizers associated with this recipe.

Returns:
    The quantizers if any are set, otherwise None
)rB   )selfs    r"   get_quantizers!QuantizationRecipe.get_quantizersf   s     r!   r   )r   r   r   r   r   rB   r
   r	   r   r:   rC   r6   rF   r    r   r!   r"   r@   r@   V   sG    	 -1Ji)0DHXd+?&@AHi 9 r!   r@   c                       \ rS rSr% SrSr\\\      \	S'   Sr
S\\\\/\\   \-  4      -  \	S'   SrS\\\/\\   \-  4      -  \	S'   Sr\\   \	S'   Srg)	LoweringRecipep   a  
Configuration recipe for lowering and partitioning.

This class holds the configuration parameters for lowering a model
to backend-specific representations.

Attributes:
    partitioners: Optional list of partitioners for model partitioning
    edge_transform_passes: Optional list of callables that take (method_name: str, exported_program: ExportedProgram)
                           and return either List[PassType] or PassManager to be applied during edge lowering.
    edge_manager_transform_passes: Optional list of callables that take EdgeProgramManager as argument
                                    and return passes to be applied. Applied sequentially after TO_EDGE stage.
    edge_compile_config: Optional edge compilation configuration
Npartitionersedge_transform_passesedge_manager_transform_passesedge_compile_configr   )r   r   r   r   r   rK   r
   r	   r   r:   rL   r   r,   r   r   r   rM   r   rN   r   r    r   r!   r"   rI   rI   p   s     15L(4,-4 	 tHc?3T(^k5QQRSS 	 "tH014>K3OOPQQ 8<"34;r!   rI   zfThis API and all of its related functionality such as ExportSession and ExportRecipe are experimental.c                   T   \ rS rSr% SrSr\\   \S'   Sr	\\
   \S'   Sr\\\\\/\4         \S'   Sr\\   \S'   Sr\\   \S'   Sr\\\      \S	'   \R,                  r\\S
'   Sr\\S'   \SS j5       r\ SS\S    S\\   SS 4S jj5       r\ SS\S    S\\   SS 4S jj5       rSrg)ExportRecipe   ab  
Configuration recipe for the export process.

This class holds the configuration parameters for exporting a model,
including compilation and transformation options.

Attributes:
    name: Optional name for the recipe
    quantization_recipe: Optional quantization recipe for model quantization
    aten_transform_passes: Optional list of functions to apply transformation passes to the program before edge lowering.
                           These callables are invoked to modify and return the transformed program.
    lowering_recipe: Optional lowering recipe for model lowering and partitioning
    executorch_backend_config: Optional backend configuration for ExecuTorch
    pipeline_stages: Optional list of stages to execute, defaults to a standard pipeline.
    mode: Export mode (debug or release)
    strict: Set the strict flag in the torch export call.
Nnamequantization_recipeaten_transform_passeslowering_recipeexecutorch_backend_configpipeline_stagesmodeTstrictr&   c                 @   SSK J n  [        U[        5      (       d  [        SU 35      eUR	                  5       nUR
                  " X40 UD6nUcI  UR                  U5      n[        SUR                   SU SU Vs/ s H  owR                  PM     sn 35      eU$ s  snf )a  
Get an export recipe from backend. Backend is automatically determined based on the
passed recipe type.

Args:
    recipe: The type of recipe to create
    **kwargs: Recipe-specific parameters

Returns:
    ExportRecipe configured for the specified recipe type
r   )recipe_registryzInvalid recipe type: zRecipe 'z' not supported by 'z'. Supported: )r[   
isinstancer$   
ValueErrorr)   create_recipeget_supported_recipesvalue)r(   recipekwargsr[   backendexport_recipe	supportedrs           r"   
get_recipeExportRecipe.get_recipe   s     	5&*--4VH=>>))+'55fPP '==gFI6<<.(<WI F09:	1ww	:;=   ;s   ;Brecipesrecipe_namec                 p    U(       d  [        S5      e[        U5      S:X  a  US   $ U R                  X5      $ )a  
Combine multiple ExportRecipe objects into a single recipe.

Args:
    recipes: List of ExportRecipe objects to combine
    recipe_name: Optional name for the combined recipe

Returns:
    A new ExportRecipe that combines all input recipes

Example:
    recipe1 = ExportRecipe.get_recipe(CoreMLRecipeType.FP32)
    recipe2 = ExportRecipe.get_recipe(XNNPackRecipeType.FP32)
    combined_recipe = ExportRecipe.combine(
        [recipe1, recipe2],
        recipe_name="multi_backend_coreml_xnnpack_fp32"
    )
zRecipes cannot be emptyr   r   )r]   len_combine_recipes)r(   ri   rj   s      r"   combineExportRecipe.combine   s:    , 677w<11:##G99r!   backend_recipesc                 0   / n/ n/ n/ n/ nSnU GHw  n	U	R                   (       a  UR                  U	R                   5        U	R                  (       a@  U	R                  R                  (       a%  UR                  U	R                  R                  5        U	R                  (       a@  U	R                  R                  (       a%  UR                  U	R                  R                  5        U	R
                  =n
(       aX  U
R                  (       a  UR                  U
R                  5        U
R                  (       a  UR                  U
R                  5        Ub  GMC  U	R                  (       d  GMW  [        R                  " U	R                  5      nGMz     SnU(       d  U(       a  [        U(       a  UOSU(       a  UOSS9nSnU(       d  U(       a  SnU HI  n	U	R                  (       d  M  U	R                  R                  (       d  M3  U	R                  R                  n  O   [        U(       a  UOSU(       a  UOSU=(       d
    [        5       S9nU=(       d:    SR                  U Vs/ s H  oR                   c  M  UR                   PM     sn5      nU " UUUUUS9$ s  snf )a
  
Util to combine multiple backend recipes into a single multi-backend recipe.

Args:
    backend_recipes: List of ExportRecipe objects to combine
    recipe_name: Optional name for the combined recipe

Returns:
    Combined ExportRecipe for multi-backend deployment
N)rB   rC   )rK   rL   rN   _)rR   rS   rT   rU   rV   )rT   extendrU   rK   rL   rS   rB   rC   rV   copydeepcopyr@   rN   rI   r   joinrR   )r(   rp   rj   all_partitionersall_quantizersall_ao_quantization_configsall_pre_edge_passesall_transform_passescombined_backend_configra   rS   combined_quantization_recipecombined_lowering_reciperN   rf   s                  r"   rm   ExportRecipe._combine_recipes   s1    &(# !"&%F++#**6+G+GH %%&*@*@*M*M ''(>(>(K(KL %%&*@*@*V*V$++**@@
 '-&@&@@"@&11"))*=*H*HI '>>/66+CC
 '.63S3S3S*.--44+'; &D (,$8+=-;>3N/TX,( $( 3"&)***..BBB*0*@*@*T*T' * (61A-t,@(d$7$N;L;N($ " 
SXX,C_VQVV_C&
  <"54&=
 	
 Ds   J2Jr   )ra   r$   r&   rP   )N)r   r   r   r   r   rR   r
   r,   r:   rS   r@   rT   r	   r   r   rU   rI   rV   r   rW   r   r/   r4   rX   rY   r>   r+   rg   rn   rm   r    r   r!   r"   rP   rP      s)   
$ D(3-8<"45< 	 8XsO,o=>?  15OXn-4CGx(?@G15OXd9o.5D$FD 6 IM:>*:9A#:	: :: QU]
">2]
AI#]
	]
 ]
r!   rP   ))rt   abcr   r   dataclassesr   enumr   r   typingr   r	   r
   r;   executorch.exirr   r   executorch.exir._warningsr   #executorch.exir.backend.partitionerr   executorch.exir.capturer   r   executorch.exir.pass_managerr   r   torchao.core.configr   #torchao.quantization.pt2e.quantizerr   typesr   r   r$   r,   r/   r6   r@   rI   rP   r   r!   r"   <module>r      s     ' !  + +  ? 2 ; N > , 9 	Xw 	 $
3 
 
G 
G 
G   2 < < <8 l w
 w
 w
r!   