Skip to content

Commit 46937eb

Browse files
authored
Enable CoreML by default on macOS wheel builds (#9483)
### Summary Context: #9481 * Include the `executorchcoreml` pybinding in the builds * Remove separate installation option * Turn on CoreML by default for macOS builds * Add a dependency on coremltools for macOS ### Test plan CI ``` $ rm -rf cmake-out pip-out dist && ./install_executorch.sh $ ./examples/models/llama/install_requirements.sh $ .ci/scripts/test_llama.sh -model stories110M -build_tool cmake -dtype fp32 -mode coreml $ .ci/scripts/test_llama.sh -model stories110M -build_tool cmake -dtype fp32 -mode xnnpack+custom+quantize_kv ``` cc @larryliu0820 @lucylq
1 parent c43d5ad commit 46937eb

File tree

7 files changed

+29
-96
lines changed

7 files changed

+29
-96
lines changed

backends/apple/coreml/compiler/coreml_preprocess.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import coremltools as ct
1818
import coremltools.optimize as cto
19-
import executorchcoreml
2019

20+
from executorch.backends.apple.coreml import executorchcoreml
2121
from executorch.exir.backend.backend_details import (
2222
BackendDetails,
2323
ExportedProgram,

backends/apple/coreml/runtime/inmemoryfs/setup.py

-52
This file was deleted.

backends/apple/coreml/scripts/install_inmemoryfs.sh

-25
This file was deleted.

backends/apple/coreml/scripts/install_requirements.sh

+6-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ SCRIPT_DIR_PATH="$(
1010
pwd -P
1111
)"
1212

13+
# TODO(jathu): remove the need to fetch coremltools to build deps for coreml_executor_runner.
14+
# Keep this version in sync with: pyproject.toml
15+
COREMLTOOLS_VERSION="8.1"
16+
1317
red=`tput setaf 1`
1418
green=`tput setaf 2`
1519

@@ -24,7 +28,7 @@ rm -rf "$COREML_DIR_PATH/third-party"
2428
mkdir "$COREML_DIR_PATH/third-party"
2529

2630
echo "${green}ExecuTorch: Cloning coremltools."
27-
git clone --depth 1 --branch 8.1 "https://github.com/apple/coremltools.git" $COREMLTOOLS_DIR_PATH
31+
git clone --depth 1 --branch "${COREMLTOOLS_VERSION}" "https://github.com/apple/coremltools.git" $COREMLTOOLS_DIR_PATH
2832
cd $COREMLTOOLS_DIR_PATH
2933

3034
STATUS=$?
@@ -43,16 +47,7 @@ fi
4347

4448
mkdir "$COREMLTOOLS_DIR_PATH/build"
4549
cmake -S "$COREMLTOOLS_DIR_PATH" -B "$COREMLTOOLS_DIR_PATH/build"
46-
cmake --build "$COREMLTOOLS_DIR_PATH/build" --parallel
47-
48-
echo "${green}ExecuTorch: Installing coremltools."
49-
pip install "$COREMLTOOLS_DIR_PATH"
50-
51-
STATUS=$?
52-
if [ $STATUS -ne 0 ]; then
53-
echo "${red}ExecuTorch: Failed to install coremltools."
54-
exit 1
55-
fi
50+
cmake --build "$COREMLTOOLS_DIR_PATH/build" --parallel --target mlmodel
5651

5752
echo "${green}ExecuTorch: Cloning nlohmann."
5853
git clone https://github.com/nlohmann/json.git "$COREML_DIR_PATH/third-party/nlohmann_json"
@@ -62,8 +57,6 @@ if [ $STATUS -ne 0 ]; then
6257
exit 1
6358
fi
6459

65-
sh "$COREML_DIR_PATH/scripts/install_inmemoryfs.sh"
66-
6760
echo "${green}ExecuTorch: Copying protobuf files."
6861
mkdir -p "$COREML_DIR_PATH/runtime/sdk/format/"
6962
cp -rf "$PROTOBUF_FILES_DIR_PATH" "$COREML_DIR_PATH/runtime/sdk/format/"

examples/apple/coreml/scripts/extract_coreml_models.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
from typing import List, Optional
1212

13-
import executorchcoreml
14-
13+
from executorch.backends.apple.coreml import executorchcoreml
1514
from executorch.backends.apple.coreml.compiler import CoreMLBackend
16-
1715
from executorch.exir._serialize._program import deserialize_pte_binary
18-
1916
from executorch.exir.schema import (
2017
BackendDelegate,
2118
BackendDelegateDataReference,

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ dependencies=[
6767
"sympy",
6868
"tabulate",
6969
"typing-extensions",
70+
# Keep this version in sync with: ./backends/apple/coreml/scripts/install_requirements.sh
71+
"coremltools==8.1; platform_system == 'Darwin'",
7072
]
7173

7274
[project.urls]

setup.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def _cmake_args_defines() -> Dict[str, str]:
8484
return result
8585

8686

87+
def _is_macos() -> bool:
88+
return sys.platform == "darwin"
89+
90+
8791
class ShouldBuild:
8892
"""Indicates whether to build various components."""
8993

@@ -125,7 +129,7 @@ def pybindings(cls) -> bool:
125129

126130
@classmethod
127131
def coreml(cls) -> bool:
128-
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_COREML", default=False)
132+
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_COREML", default=_is_macos())
129133

130134
@classmethod
131135
def mps(cls) -> bool:
@@ -720,8 +724,14 @@ def run(self):
720724
"-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON", # add quantized ops to pybindings.
721725
"-DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON",
722726
]
727+
723728
if ShouldBuild.training():
724729
build_args += ["--target", "_training_lib"]
730+
731+
if ShouldBuild.coreml():
732+
cmake_args += ["-DEXECUTORCH_BUILD_COREML=ON"]
733+
build_args += ["--target", "executorchcoreml"]
734+
725735
build_args += ["--target", "portable_lib"]
726736
# To link backends into the portable_lib target, callers should
727737
# add entries like `-DEXECUTORCH_BUILD_XNNPACK=ON` to the CMAKE_ARGS
@@ -855,6 +865,14 @@ def get_ext_modules() -> List[Extension]:
855865
"executorch.extension.training.pybindings._training_lib",
856866
)
857867
)
868+
if ShouldBuild.coreml():
869+
ext_modules.append(
870+
BuiltExtension(
871+
src="executorchcoreml.*",
872+
src_dir="backends/apple/coreml",
873+
modpath="executorch.backends.apple.coreml.executorchcoreml",
874+
)
875+
)
858876
if ShouldBuild.llama_custom_ops():
859877
ext_modules.append(
860878
BuiltFile(

0 commit comments

Comments
 (0)