Skip to content

Enable CoreML by default on macOS wheel builds #9483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backends/apple/coreml/compiler/coreml_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import coremltools as ct
import coremltools.optimize as cto
import executorchcoreml

from executorch.backends.apple.coreml import executorchcoreml
from executorch.exir.backend.backend_details import (
BackendDetails,
ExportedProgram,
Expand Down
52 changes: 0 additions & 52 deletions backends/apple/coreml/runtime/inmemoryfs/setup.py

This file was deleted.

25 changes: 0 additions & 25 deletions backends/apple/coreml/scripts/install_inmemoryfs.sh

This file was deleted.

19 changes: 6 additions & 13 deletions backends/apple/coreml/scripts/install_requirements.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ SCRIPT_DIR_PATH="$(
pwd -P
)"

# TODO(jathu): remove the need to fetch coremltools to build deps for coreml_executor_runner.
# Keep this version in sync with: pyproject.toml
COREMLTOOLS_VERSION="8.1"

red=`tput setaf 1`
green=`tput setaf 2`

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

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

STATUS=$?
Expand All @@ -43,16 +47,7 @@ fi

mkdir "$COREMLTOOLS_DIR_PATH/build"
cmake -S "$COREMLTOOLS_DIR_PATH" -B "$COREMLTOOLS_DIR_PATH/build"
cmake --build "$COREMLTOOLS_DIR_PATH/build" --parallel

echo "${green}ExecuTorch: Installing coremltools."
pip install "$COREMLTOOLS_DIR_PATH"

STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "${red}ExecuTorch: Failed to install coremltools."
exit 1
fi
cmake --build "$COREMLTOOLS_DIR_PATH/build" --parallel --target mlmodel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YifanShenSZ @cymbalrush -- any reason why we still need to build mlmodel target from source, instead of using directly from pip install coremltools?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be required any more but let me confirm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jathu

Okay, let's not block it, but create an GH issue to track this...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done #9651


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

sh "$COREML_DIR_PATH/scripts/install_inmemoryfs.sh"

echo "${green}ExecuTorch: Copying protobuf files."
mkdir -p "$COREML_DIR_PATH/runtime/sdk/format/"
cp -rf "$PROTOBUF_FILES_DIR_PATH" "$COREML_DIR_PATH/runtime/sdk/format/"
5 changes: 1 addition & 4 deletions examples/apple/coreml/scripts/extract_coreml_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

from typing import List, Optional

import executorchcoreml

from executorch.backends.apple.coreml import executorchcoreml
from executorch.backends.apple.coreml.compiler import CoreMLBackend

from executorch.exir._serialize._program import deserialize_pte_binary

from executorch.exir.schema import (
BackendDelegate,
BackendDelegateDataReference,
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ dependencies=[
"sympy",
"tabulate",
"typing-extensions",
# Keep this version in sync with: ./backends/apple/coreml/scripts/install_requirements.sh
"coremltools==8.1; platform_system == 'Darwin'",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Niiice!

]

[project.urls]
Expand Down
20 changes: 19 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def _cmake_args_defines() -> Dict[str, str]:
return result


def _is_macos() -> bool:
return sys.platform == "darwin"


class ShouldBuild:
"""Indicates whether to build various components."""

Expand Down Expand Up @@ -125,7 +129,7 @@ def pybindings(cls) -> bool:

@classmethod
def coreml(cls) -> bool:
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_COREML", default=False)
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_COREML", default=_is_macos())

@classmethod
def mps(cls) -> bool:
Expand Down Expand Up @@ -720,8 +724,14 @@ def run(self):
"-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON", # add quantized ops to pybindings.
"-DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON",
]

if ShouldBuild.training():
build_args += ["--target", "_training_lib"]

if ShouldBuild.coreml():
cmake_args += ["-DEXECUTORCH_BUILD_COREML=ON"]
build_args += ["--target", "executorchcoreml"]

build_args += ["--target", "portable_lib"]
# To link backends into the portable_lib target, callers should
# add entries like `-DEXECUTORCH_BUILD_XNNPACK=ON` to the CMAKE_ARGS
Expand Down Expand Up @@ -855,6 +865,14 @@ def get_ext_modules() -> List[Extension]:
"executorch.extension.training.pybindings._training_lib",
)
)
if ShouldBuild.coreml():
ext_modules.append(
BuiltExtension(
src="executorchcoreml.*",
src_dir="backends/apple/coreml",
modpath="executorch.backends.apple.coreml.executorchcoreml",
)
)
if ShouldBuild.llama_custom_ops():
ext_modules.append(
BuiltFile(
Expand Down
Loading