From 75d2cb718375c81fb758bb1d67bbc3d407175411 Mon Sep 17 00:00:00 2001 From: jathu Date: Fri, 21 Mar 2025 07:49:57 -0700 Subject: [PATCH 1/7] address comments --- backends/apple/coreml/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/backends/apple/coreml/CMakeLists.txt b/backends/apple/coreml/CMakeLists.txt index 06807279978..a04c6e7d2bb 100644 --- a/backends/apple/coreml/CMakeLists.txt +++ b/backends/apple/coreml/CMakeLists.txt @@ -25,8 +25,6 @@ endif() option(COREML_BUILD_EXECUTOR_RUNNER "Build CoreML executor runner." OFF) -set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15) - # inmemoryfs sources set(INMEMORYFS_SOURCES runtime/inmemoryfs/inmemory_filesystem.cpp From aa9660dd20023464d932938a71ea94b078ee64f7 Mon Sep 17 00:00:00 2001 From: jathu Date: Fri, 21 Mar 2025 08:18:37 -0700 Subject: [PATCH 2/7] address comments --- backends/apple/coreml/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backends/apple/coreml/CMakeLists.txt b/backends/apple/coreml/CMakeLists.txt index a04c6e7d2bb..06807279978 100644 --- a/backends/apple/coreml/CMakeLists.txt +++ b/backends/apple/coreml/CMakeLists.txt @@ -25,6 +25,8 @@ endif() option(COREML_BUILD_EXECUTOR_RUNNER "Build CoreML executor runner." OFF) +set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15) + # inmemoryfs sources set(INMEMORYFS_SOURCES runtime/inmemoryfs/inmemory_filesystem.cpp From 7855669474454c79b92e3282c5fef223430c1419 Mon Sep 17 00:00:00 2001 From: jathu Date: Thu, 20 Mar 2025 17:39:19 -0700 Subject: [PATCH 3/7] split targets --- backends/apple/coreml/CMakeLists.txt | 45 +++++++++++----------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/backends/apple/coreml/CMakeLists.txt b/backends/apple/coreml/CMakeLists.txt index 06807279978..f94ca5021eb 100644 --- a/backends/apple/coreml/CMakeLists.txt +++ b/backends/apple/coreml/CMakeLists.txt @@ -118,31 +118,20 @@ set(PROTOBUF_SOURCES runtime/sdk/format/WordTagger.pb.cc ) -find_library(FOUNDATION_FRAMEWORK Foundation) - # CoreML util -add_library(coreml_util ${UTIL_SOURCES}) -target_include_directories(coreml_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util) -target_link_libraries(coreml_util PRIVATE ${FOUNDATION_FRAMEWORK}) - -install( - TARGETS coreml_util - DESTINATION lib - INCLUDES - DESTINATION ${_common_include_directories} +add_library(coreml_util) +target_sources(coreml_util PRIVATE ${UTIL_SOURCES}) +target_include_directories(coreml_util + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util ) # CoreML inmemoryfs -add_library(coreml_inmemoryfs ${INMEMORYFS_SOURCES}) -target_include_directories(coreml_inmemoryfs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs) -target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util ${FOUNDATION_FRAMEWORK}) - -install( - TARGETS coreml_inmemoryfs - DESTINATION lib - INCLUDES - DESTINATION ${_common_include_directories} +add_library(coreml_inmemoryfs) +target_sources(coreml_inmemoryfs PRIVATE ${INMEMORYFS_SOURCES}) +target_include_directories(coreml_inmemoryfs + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs ) +target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util) # Define the delegate library add_library(coremldelegate) @@ -160,6 +149,9 @@ target_include_directories( target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/..) target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/runtime/core/portable_type/c10) target_compile_definitions(coremldelegate PRIVATE C10_USING_CUSTOM_GENERATED_MACROS) +target_link_libraries(coremldelegate PRIVATE coreml_util) +target_link_libraries(coremldelegate PRIVATE coreml_inmemoryfs) +target_link_libraries(coremldelegate PRIVATE executorch_core) if(EXECUTORCH_BUILD_DEVTOOLS) target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES}) @@ -179,17 +171,13 @@ endif() find_library(ACCELERATE_FRAMEWORK Accelerate) find_library(COREML_FRAMEWORK CoreML) +find_library(FOUNDATION_FRAMEWORK Foundation) find_library(SQLITE_LIBRARY sqlite3) target_link_libraries( coremldelegate - PUBLIC coreml_util - coreml_inmemoryfs - PRIVATE executorch_core - ${ACCELERATE_FRAMEWORK} - ${COREML_FRAMEWORK} - ${FOUNDATION_FRAMEWORK} - ${SQLITE_LIBRARY} + PRIVATE executorch_core ${ACCELERATE_FRAMEWORK} ${COREML_FRAMEWORK} + ${FOUNDATION_FRAMEWORK} ${SQLITE_LIBRARY} ) target_link_options_shared_lib(coremldelegate) @@ -244,5 +232,6 @@ if(EXECUTORCH_BUILD_COREML AND EXECUTORCH_BUILD_PYBIND) if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") target_compile_options(executorchcoreml PRIVATE -g) endif() - target_link_libraries(executorchcoreml PRIVATE coreml_util coreml_inmemoryfs) + target_link_libraries(executorchcoreml PRIVATE coreml_util) + target_link_libraries(executorchcoreml PRIVATE coreml_inmemoryfs) endif() From bfb46542049a07d66a93fa8a94941fa6885b7671 Mon Sep 17 00:00:00 2001 From: jathu Date: Fri, 21 Mar 2025 07:49:57 -0700 Subject: [PATCH 4/7] address comments --- backends/apple/coreml/CMakeLists.txt | 45 +++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/backends/apple/coreml/CMakeLists.txt b/backends/apple/coreml/CMakeLists.txt index f94ca5021eb..06807279978 100644 --- a/backends/apple/coreml/CMakeLists.txt +++ b/backends/apple/coreml/CMakeLists.txt @@ -118,20 +118,31 @@ set(PROTOBUF_SOURCES runtime/sdk/format/WordTagger.pb.cc ) +find_library(FOUNDATION_FRAMEWORK Foundation) + # CoreML util -add_library(coreml_util) -target_sources(coreml_util PRIVATE ${UTIL_SOURCES}) -target_include_directories(coreml_util - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util +add_library(coreml_util ${UTIL_SOURCES}) +target_include_directories(coreml_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util) +target_link_libraries(coreml_util PRIVATE ${FOUNDATION_FRAMEWORK}) + +install( + TARGETS coreml_util + DESTINATION lib + INCLUDES + DESTINATION ${_common_include_directories} ) # CoreML inmemoryfs -add_library(coreml_inmemoryfs) -target_sources(coreml_inmemoryfs PRIVATE ${INMEMORYFS_SOURCES}) -target_include_directories(coreml_inmemoryfs - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs +add_library(coreml_inmemoryfs ${INMEMORYFS_SOURCES}) +target_include_directories(coreml_inmemoryfs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs) +target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util ${FOUNDATION_FRAMEWORK}) + +install( + TARGETS coreml_inmemoryfs + DESTINATION lib + INCLUDES + DESTINATION ${_common_include_directories} ) -target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util) # Define the delegate library add_library(coremldelegate) @@ -149,9 +160,6 @@ target_include_directories( target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/..) target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/runtime/core/portable_type/c10) target_compile_definitions(coremldelegate PRIVATE C10_USING_CUSTOM_GENERATED_MACROS) -target_link_libraries(coremldelegate PRIVATE coreml_util) -target_link_libraries(coremldelegate PRIVATE coreml_inmemoryfs) -target_link_libraries(coremldelegate PRIVATE executorch_core) if(EXECUTORCH_BUILD_DEVTOOLS) target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES}) @@ -171,13 +179,17 @@ endif() find_library(ACCELERATE_FRAMEWORK Accelerate) find_library(COREML_FRAMEWORK CoreML) -find_library(FOUNDATION_FRAMEWORK Foundation) find_library(SQLITE_LIBRARY sqlite3) target_link_libraries( coremldelegate - PRIVATE executorch_core ${ACCELERATE_FRAMEWORK} ${COREML_FRAMEWORK} - ${FOUNDATION_FRAMEWORK} ${SQLITE_LIBRARY} + PUBLIC coreml_util + coreml_inmemoryfs + PRIVATE executorch_core + ${ACCELERATE_FRAMEWORK} + ${COREML_FRAMEWORK} + ${FOUNDATION_FRAMEWORK} + ${SQLITE_LIBRARY} ) target_link_options_shared_lib(coremldelegate) @@ -232,6 +244,5 @@ if(EXECUTORCH_BUILD_COREML AND EXECUTORCH_BUILD_PYBIND) if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") target_compile_options(executorchcoreml PRIVATE -g) endif() - target_link_libraries(executorchcoreml PRIVATE coreml_util) - target_link_libraries(executorchcoreml PRIVATE coreml_inmemoryfs) + target_link_libraries(executorchcoreml PRIVATE coreml_util coreml_inmemoryfs) endif() From 999065efeb165a424d0ed06147359ad139000061 Mon Sep 17 00:00:00 2001 From: jathu Date: Thu, 20 Mar 2025 19:36:32 -0700 Subject: [PATCH 5/7] build inmemoryfs when coreml or pybindings enabled --- CMakeLists.txt | 2 ++ setup.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6509d4adeef..e83e4046529 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -811,6 +811,7 @@ if(EXECUTORCH_BUILD_PYBIND) if(EXECUTORCH_BUILD_COREML) list(APPEND _dep_libs coremldelegate) + list(APPEND _dep_libs coreml_inmemoryfs_pybinding) endif() if(EXECUTORCH_BUILD_MPS) @@ -935,6 +936,7 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER) if(EXECUTORCH_BUILD_COREML) list(APPEND _executor_runner_libs coremldelegate) + list(APPEND _executor_runner_libs coreml_inmemoryfs_pybinding) endif() add_executable(executor_runner ${_executor_runner__srcs}) diff --git a/setup.py b/setup.py index 871fdf329c2..e73ed1eb4da 100644 --- a/setup.py +++ b/setup.py @@ -139,6 +139,10 @@ def mps(cls) -> bool: def xnnpack(cls) -> bool: return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_XNNPACK", default=False) + @classmethod + def coreml(cls) -> bool: + return cls._is_env_enabled("EXECUTORCH_BUILD_COREML", default=False) + @classmethod def training(cls) -> bool: return cls._is_cmake_arg_enabled( @@ -843,6 +847,15 @@ def get_ext_modules() -> List[Extension]: ] ) + if ShouldBuild.pybindings() or ShouldBuild.coreml(): + ext_modules.append( + BuiltExtension( + src="coreml_inmemoryfs_pybinding.*", + src_dir="backends/apple/coreml", + modpath="executorch.backends.apple.coreml.inmemoryfs", + ) + ) + if ShouldBuild.pybindings(): ext_modules.append( # Install the prebuilt pybindings extension wrapper for the runtime, From 51a970a12c7463c6f344c3f4d999797cb1251bcb Mon Sep 17 00:00:00 2001 From: jathu Date: Thu, 20 Mar 2025 19:39:13 -0700 Subject: [PATCH 6/7] update usage --- CMakeLists.txt | 2 -- setup.py | 13 ------------- 2 files changed, 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e83e4046529..6509d4adeef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -811,7 +811,6 @@ if(EXECUTORCH_BUILD_PYBIND) if(EXECUTORCH_BUILD_COREML) list(APPEND _dep_libs coremldelegate) - list(APPEND _dep_libs coreml_inmemoryfs_pybinding) endif() if(EXECUTORCH_BUILD_MPS) @@ -936,7 +935,6 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER) if(EXECUTORCH_BUILD_COREML) list(APPEND _executor_runner_libs coremldelegate) - list(APPEND _executor_runner_libs coreml_inmemoryfs_pybinding) endif() add_executable(executor_runner ${_executor_runner__srcs}) diff --git a/setup.py b/setup.py index e73ed1eb4da..871fdf329c2 100644 --- a/setup.py +++ b/setup.py @@ -139,10 +139,6 @@ def mps(cls) -> bool: def xnnpack(cls) -> bool: return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_XNNPACK", default=False) - @classmethod - def coreml(cls) -> bool: - return cls._is_env_enabled("EXECUTORCH_BUILD_COREML", default=False) - @classmethod def training(cls) -> bool: return cls._is_cmake_arg_enabled( @@ -847,15 +843,6 @@ def get_ext_modules() -> List[Extension]: ] ) - if ShouldBuild.pybindings() or ShouldBuild.coreml(): - ext_modules.append( - BuiltExtension( - src="coreml_inmemoryfs_pybinding.*", - src_dir="backends/apple/coreml", - modpath="executorch.backends.apple.coreml.inmemoryfs", - ) - ) - if ShouldBuild.pybindings(): ext_modules.append( # Install the prebuilt pybindings extension wrapper for the runtime, From ec4b513899e3eb4e3366a98634a8e8e37f2ede09 Mon Sep 17 00:00:00 2001 From: jathu Date: Wed, 26 Mar 2025 08:42:40 -0700 Subject: [PATCH 7/7] update docs --- backends/apple/coreml/setup.md | 12 ++---------- docs/source/backends-coreml.md | 6 ------ docs/source/using-executorch-ios.md | 13 ++++++------- examples/apple/coreml/README.md | 15 +++++---------- 4 files changed, 13 insertions(+), 33 deletions(-) diff --git a/backends/apple/coreml/setup.md b/backends/apple/coreml/setup.md index 6b7ffa4ded8..c6daae0d989 100644 --- a/backends/apple/coreml/setup.md +++ b/backends/apple/coreml/setup.md @@ -6,16 +6,8 @@ This is a tutorial for setting up the Core ML backend. 1. Follow the instructions described in [Setting Up ExecuTorch](/docs/source/getting-started-setup.md) to set up ExecuTorch environment. -2. Run `install_requirements.sh` to install dependencies required by the **Core ML** backend. -``` -cd executorch - -./backends/apple/coreml/scripts/install_requirements.sh - -``` - -3. Run the example script to validate that the **Core ML** backend is set up correctly. +2. Run the example script to validate that the **Core ML** backend is set up correctly. ``` cd executorch @@ -26,7 +18,7 @@ python3 -m examples.apple.coreml.scripts.export --model_name add ``` -4. You can now integrate the **Core ML** backend in code. +3. You can now integrate the **Core ML** backend in code. ```python # Delegate to Core ML backend diff --git a/docs/source/backends-coreml.md b/docs/source/backends-coreml.md index a06820b2d08..126727735ae 100644 --- a/docs/source/backends-coreml.md +++ b/docs/source/backends-coreml.md @@ -28,12 +28,6 @@ Before starting, make sure you install the Xcode Command Line Tools: xcode-select --install ``` -Finally you must install the CoreML backend by running the following script: -```bash -sh ./backends/apple/coreml/scripts/install_requirements.sh -``` - - ---- ## Using the CoreML Backend diff --git a/docs/source/using-executorch-ios.md b/docs/source/using-executorch-ios.md index e975cb9ef22..70c2b366fa8 100644 --- a/docs/source/using-executorch-ios.md +++ b/docs/source/using-executorch-ios.md @@ -103,19 +103,18 @@ git clone https://github.com/pytorch/executorch.git --depth 1 --recurse-submodul 3. Set up [Python](https://www.python.org/downloads/macos/) 3.10+ and activate a virtual environment: ```bash -python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip +python3 -m venv .venv && source .venv/bin/activate && ./install_requirements.sh ``` -4. Install the required dependencies, including those needed for the backends like [Core ML](backends-coreml.md) or [MPS](backends-mps.md), if you plan to build them as well: +4. Install the required dependencies, including those needed for the backends like [Core ML](backends-coreml.md) or [MPS](backends-mps.md). Choose one: ```bash -./install_executorch.sh --pybind coreml mps xnnpack +# ExecuTorch with xnnpack and CoreML backend +./install_executorch.sh --pybind xnnpack -# Optional dependencies for Core ML backend. -./backends/apple/coreml/scripts/install_requirements.sh - -# And MPS backend. +# Optional: ExecuTorch with xnnpack, CoreML, and MPS backend ./backends/apple/mps/install_requirements.sh +./install_executorch.sh --pybind xnnpack mps ``` 5. Install [CMake](https://cmake.org): diff --git a/examples/apple/coreml/README.md b/examples/apple/coreml/README.md index f4270956b2c..4dba5031358 100644 --- a/examples/apple/coreml/README.md +++ b/examples/apple/coreml/README.md @@ -18,16 +18,8 @@ We will walk through an example model to generate a Core ML delegated binary fil 1. Following the setup guide in [Setting Up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) you should be able to get the basic development environment for ExecuTorch working. -2. Run `install_requirements.sh` to install dependencies required by the **Core ML** backend. -```bash -cd executorch - -./backends/apple/coreml/scripts/install_requirements.sh - -``` - -3. Run the export script to generate a Core ML delegated binary file. +2. Run the export script to generate a Core ML delegated binary file. ```bash cd executorch @@ -39,11 +31,14 @@ python3 -m examples.portable.scripts.export -h python3 -m examples.apple.coreml.scripts.export --model_name add ``` -4. Run the binary file using the `coreml_executor_runner`. +3. Run the binary file using the `coreml_executor_runner`. ```bash cd executorch +# Install requirements needed to run the example runner +./backends/apple/coreml/scripts/install_requirements.sh + # Builds the Core ML executor runner. Generates ./coreml_executor_runner if successful. ./examples/apple/coreml/scripts/build_executor_runner.sh