Skip to content

Commit c3b1e9c

Browse files
Update on "[Executorch][target recipes] Add target based recipes for lowering models to a target device"
This diff introduces multi backend/ target based recipes to lower a model with very less code. Target recipes provide pre-configured backend recipes to use them and retarget if needed. See RFC: #13732 ## Usage ``` from executorch.export import export, ExportRecipe, IOSTargetRecipeType # CoreML + XNNPACK coreml_xnnpack_recipe = ExportRecipe.get_recipe(IOSTargetRecipeType.IOS_ARM64_COREML_FP32) session = export(model, coreml_xnnpack_recipe, example_inputs) session.save_pte_file("model.pte") ``` ## Advanced usage one can directly use `ExportRecipe.combine_recipes()` to combine specific backend recipes. ``` recipe1 = ExportRecipe.get_recipe(AndroidRecipeType.XYZ) recipe2 = ExportRecipe.get_recipe(XNNPackRecipeType.FP32) combined_recipe = ExportRecipe.combine( [recipe1, recipe2], recipe_name="multi_backend_coreml_xnnpack_fp32" ) session = export(model, combined_recipe, example_inputs) ``` Fixes: #13732 Differential Revision: [D81297451](https://our.internmc.facebook.com/intern/diff/D81297451/) [ghstack-poisoned]
1 parent 6c584c7 commit c3b1e9c

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

export/multi_backend_recipes/multi_backend_recipe_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
for optimized deployment on specific targets.
1212
"""
1313

14+
import logging
1415
from typing import Any, Optional, Sequence
1516

1617
from executorch.export import BackendRecipeProvider, ExportRecipe, RecipeType
1718

1819
from .target_recipe_types import IOSTargetRecipeType, TargetRecipeType
19-
import logging
2020

2121

2222
class MultiBackendRecipeProvider(BackendRecipeProvider):

export/multi_backend_recipes/target_recipe_types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class IOSTargetRecipeType(TargetRecipeType):
7676
# No xnnpack fallback for quantization as coreml uses torch.ao quantizer vs xnnpack uses torchao quantizer
7777
IOS_ARM64_COREML_INT8_STATIC = "ios-arm64-coreml-int8-static"
7878

79-
8079
@classmethod
8180
def get_target_platform(cls) -> str:
8281
return "ios"

export/multi_backend_recipes/tests/test_multi_backend_target_recipes.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
from executorch.runtime import Runtime
1818

19+
1920
class TestMultiBackendTargetRecipes(unittest.TestCase):
2021
"""Test multi-backend target recipes that combine CoreML and XNNPACK."""
2122

@@ -86,11 +87,15 @@ def forward(self, x, y):
8687
executorch_program.execution_plan[0].delegates[2].id,
8788
"XnnpackBackend",
8889
)
89-
90+
9091
et_runtime: Runtime = Runtime.get()
9192
backend_registry = et_runtime.backend_registry
92-
logging.info(f"backends registered: {et_runtime.backend_registry.registered_backend_names}")
93-
if backend_registry.is_available("CoreMLBackend") and backend_registry.is_available("XnnpackBackend"):
93+
logging.info(
94+
f"backends registered: {et_runtime.backend_registry.registered_backend_names}"
95+
)
96+
if backend_registry.is_available(
97+
"CoreMLBackend"
98+
) and backend_registry.is_available("XnnpackBackend"):
9499
logging.info("Running with CoreML and XNNPACK backends")
95100
et_output = session.run_method("forward", *example_inputs[0])
96101
logging.info(f"et output {et_output}")

export/recipe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ def combine(
217217
return cls._combine_recipes(recipes, recipe_name)
218218

219219
@classmethod
220-
def _combine_recipes(
220+
def _combine_recipes( # noqa: C901
221221
cls, backend_recipes: List["ExportRecipe"], recipe_name: Optional[str] = None
222-
) -> "ExportRecipe": # noqa: C901
222+
) -> "ExportRecipe":
223223
"""
224224
Util to combine multiple backend recipes into a single multi-backend recipe.
225225

0 commit comments

Comments
 (0)