|
| 1 | +# Copyright © 2025 Apple Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Please refer to the license found in the LICENSE file in the root directory of the source tree. |
| 4 | + |
| 5 | + |
| 6 | +from typing import Any, Optional, Sequence |
| 7 | + |
| 8 | +import coremltools as ct |
| 9 | + |
| 10 | +from executorch.backends.apple.coreml.compiler import CoreMLBackend |
| 11 | +from executorch.backends.apple.coreml.partition.coreml_partitioner import ( |
| 12 | + CoreMLPartitioner, |
| 13 | +) |
| 14 | +from executorch.backends.apple.coreml.recipes.coreml_recipe_types import ( |
| 15 | + COREML_BACKEND, |
| 16 | + CoreMLRecipeType, |
| 17 | +) |
| 18 | + |
| 19 | +from executorch.exir import EdgeCompileConfig |
| 20 | +from executorch.export import ( |
| 21 | + BackendRecipeProvider, |
| 22 | + ExportRecipe, |
| 23 | + LoweringRecipe, |
| 24 | + RecipeType, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class CoreMLRecipeProvider(BackendRecipeProvider): |
| 29 | + @property |
| 30 | + def backend_name(self) -> str: |
| 31 | + return COREML_BACKEND |
| 32 | + |
| 33 | + def get_supported_recipes(self) -> Sequence[RecipeType]: |
| 34 | + return list(CoreMLRecipeType) |
| 35 | + |
| 36 | + def create_recipe( |
| 37 | + self, recipe_type: RecipeType, **kwargs: Any |
| 38 | + ) -> Optional[ExportRecipe]: |
| 39 | + """Create CoreML recipe with precision and compute unit combinations""" |
| 40 | + |
| 41 | + if recipe_type not in self.get_supported_recipes(): |
| 42 | + return None |
| 43 | + |
| 44 | + if ct is None: |
| 45 | + raise ImportError( |
| 46 | + "coremltools is required for CoreML recipes. " |
| 47 | + "Install it with: pip install coremltools" |
| 48 | + ) |
| 49 | + |
| 50 | + # Validate kwargs |
| 51 | + self._validate_recipe_kwargs(recipe_type, **kwargs) |
| 52 | + |
| 53 | + # Parse recipe type to get precision and compute unit |
| 54 | + precision = None |
| 55 | + if recipe_type == CoreMLRecipeType.FP32: |
| 56 | + precision = ct.precision.FLOAT32 |
| 57 | + elif recipe_type == CoreMLRecipeType.FP16: |
| 58 | + precision = ct.precision.FLOAT16 |
| 59 | + |
| 60 | + if precision is None: |
| 61 | + raise ValueError(f"Unknown precision for recipe: {recipe_type.value}") |
| 62 | + |
| 63 | + return self._build_recipe(recipe_type, precision, **kwargs) |
| 64 | + |
| 65 | + def _validate_recipe_kwargs(self, recipe_type: RecipeType, **kwargs: Any) -> None: |
| 66 | + if not kwargs: |
| 67 | + return |
| 68 | + expected_keys = {"minimum_deployment_target", "compute_unit"} |
| 69 | + unexpected = set(kwargs.keys()) - expected_keys |
| 70 | + if unexpected: |
| 71 | + raise ValueError( |
| 72 | + f"CoreML Recipes only accept 'minimum_deployment_target' or 'compute_unit' as parameter. " |
| 73 | + f"Unexpected parameters: {list(unexpected)}" |
| 74 | + ) |
| 75 | + if "minimum_deployment_target" in kwargs: |
| 76 | + minimum_deployment_target = kwargs["minimum_deployment_target"] |
| 77 | + if not isinstance(minimum_deployment_target, ct.target): |
| 78 | + raise ValueError( |
| 79 | + f"Parameter 'minimum_deployment_target' must be an enum of type ct.target, got {type(minimum_deployment_target)}" |
| 80 | + ) |
| 81 | + if "compute_unit" in kwargs: |
| 82 | + compute_unit = kwargs["compute_unit"] |
| 83 | + if not isinstance(compute_unit, ct.ComputeUnit): |
| 84 | + raise ValueError( |
| 85 | + f"Parameter 'compute_unit' must be an enum of type ct.ComputeUnit, got {type(compute_unit)}" |
| 86 | + ) |
| 87 | + |
| 88 | + def _build_recipe( |
| 89 | + self, |
| 90 | + recipe_type: RecipeType, |
| 91 | + precision: ct.precision, |
| 92 | + **kwargs: Any, |
| 93 | + ) -> ExportRecipe: |
| 94 | + lowering_recipe = self._get_coreml_lowering_recipe( |
| 95 | + compute_precision=precision, |
| 96 | + **kwargs, |
| 97 | + ) |
| 98 | + |
| 99 | + return ExportRecipe( |
| 100 | + name=recipe_type.value, |
| 101 | + quantization_recipe=None, # TODO - add quantization recipe |
| 102 | + lowering_recipe=lowering_recipe, |
| 103 | + ) |
| 104 | + |
| 105 | + def _get_coreml_lowering_recipe( |
| 106 | + self, |
| 107 | + compute_precision: ct.precision, |
| 108 | + **kwargs: Any, |
| 109 | + ) -> LoweringRecipe: |
| 110 | + compile_specs = CoreMLBackend.generate_compile_specs( |
| 111 | + compute_precision=compute_precision, |
| 112 | + **kwargs, |
| 113 | + ) |
| 114 | + |
| 115 | + minimum_deployment_target = kwargs.get("minimum_deployment_target", None) |
| 116 | + take_over_mutable_buffer = True |
| 117 | + if minimum_deployment_target and minimum_deployment_target < ct.target.iOS18: |
| 118 | + take_over_mutable_buffer = False |
| 119 | + |
| 120 | + partitioner = CoreMLPartitioner( |
| 121 | + compile_specs=compile_specs, |
| 122 | + take_over_mutable_buffer=take_over_mutable_buffer, |
| 123 | + ) |
| 124 | + |
| 125 | + edge_compile_config = EdgeCompileConfig( |
| 126 | + _check_ir_validity=False, |
| 127 | + _skip_dim_order=False, |
| 128 | + ) |
| 129 | + |
| 130 | + return LoweringRecipe( |
| 131 | + partitioners=[partitioner], edge_compile_config=edge_compile_config |
| 132 | + ) |
0 commit comments