Skip to content

Commit f90a0eb

Browse files
committed
Add coreml recipes
Differential Revision: [D72410491](https://our.internmc.facebook.com/intern/diff/D72410491/) [ghstack-poisoned]
1 parent e5b996a commit f90a0eb

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

backends/apple/coreml/TARGETS

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ runtime.python_library(
5858
],
5959
)
6060

61+
runtime.python_library(
62+
name = "recipes",
63+
srcs = glob([
64+
"recipes/*.py",
65+
]),
66+
deps = [
67+
":partitioner",
68+
":quantizer",
69+
],
70+
visibility = [
71+
"@EXECUTORCH_CLIENTS",
72+
],
73+
)
74+
6175
runtime.cxx_python_extension(
6276
name = "executorchcoreml",
6377
srcs = [
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .recipes import iphone_coreml_et_recipe
2+
3+
__all__ = [
4+
"iphone_coreml_et_recipe",
5+
]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
# pyre-strict
4+
import torch
5+
6+
from executorch.exir import ExportRecipe
7+
from executorch.exir import EdgeCompileConfig, ExecutorchBackendConfig
8+
9+
def iphone_coreml_et_recipe(ios: int = 17, compute_unit: str = "CPU_ONLY") -> ExportRecipe:
10+
import coremltools as ct
11+
from coremltools.optimize.torch.quantization.quantization_config import (
12+
LinearQuantizerConfig,
13+
QuantizationScheme,
14+
)
15+
from executorch.backends.apple.coreml.compiler import CoreMLBackend
16+
from executorch.backends.apple.coreml.partition import CoreMLPartitioner
17+
from executorch.backends.apple.coreml.quantizer import CoreMLQuantizer
18+
19+
# TODO: Add compute precision, compute unit, and model type
20+
quantization_config = LinearQuantizerConfig.from_dict(
21+
{
22+
"global_config": {
23+
"quantization_scheme": QuantizationScheme.affine,
24+
"activation_dtype": torch.quint8,
25+
"weight_dtype": torch.qint8,
26+
"weight_per_channel": True,
27+
}
28+
}
29+
)
30+
minimum_deployment_target = {
31+
15: ct.target.iOS15,
32+
16: ct.target.iOS16,
33+
17: ct.target.iOS17,
34+
18: ct.target.iOS18,
35+
}[ios]
36+
compute_unit_types = ["CPU_ONLY", "CPU_AND_NE", "CPU_AND_GPU", "ALL"]
37+
assert (
38+
compute_unit in compute_unit_types
39+
), f"Invalid compute unit: {compute_unit}, should be one of {compute_unit_types}"
40+
if compute_unit == "CPU_ONLY":
41+
compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.CPU_ONLY.name.upper()]
42+
elif compute_unit == "CPU_AND_NE":
43+
compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.CPU_AND_NE.name.upper()]
44+
elif compute_unit == "CPU_AND_GPU":
45+
compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.CPU_AND_GPU.name.upper()]
46+
else: # compute_unit == "ALL"
47+
compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.ALL.name.upper()]
48+
compile_specs = CoreMLBackend.generate_compile_specs(
49+
minimum_deployment_target=minimum_deployment_target,
50+
compute_precision=ct.precision(ct.precision.FLOAT16.value),
51+
compute_unit=compute_unit_specs,
52+
model_type=CoreMLBackend.MODEL_TYPE.MODEL,
53+
)
54+
take_over_mutable_buffer = minimum_deployment_target >= ct.target.iOS18
55+
partitioner = CoreMLPartitioner(
56+
compile_specs=compile_specs,
57+
take_over_mutable_buffer=take_over_mutable_buffer,
58+
)
59+
return ExportRecipe(
60+
"iphone_coreml",
61+
quantizer=CoreMLQuantizer(quantization_config),
62+
partitioners=[partitioner],
63+
edge_compile_config=EdgeCompileConfig(
64+
_check_ir_validity=False,
65+
_skip_dim_order=True,
66+
),
67+
edge_transform_passes=[],
68+
executorch_backend_config=ExecutorchBackendConfig(),
69+
)

extension/llm/export/TARGETS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ runtime.python_library(
2828
"//caffe2:torch",
2929
"//executorch/backends/apple/coreml:backend",
3030
"//executorch/backends/apple/coreml:partitioner",
31+
"//executorch/backends/apple/coreml:recipes",
3132
"//executorch/backends/apple/mps:partitioner",
3233
"//executorch/backends/qualcomm/partition:partition",
3334
"//executorch/backends/qualcomm/quantizer:quantizer",

0 commit comments

Comments
 (0)