Skip to content

Support CoreML export on Linux #11172

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 1 commit into from
May 28, 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
6 changes: 5 additions & 1 deletion .ci/scripts/wheel/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
test_base.ModelTest(
model=Model.Mv3,
backend=Backend.XnnpackQuantizationDelegation,
)
),
test_base.ModelTest(
model=Model.Mv3,
backend=Backend.CoreMlExportOnly,
),
]
)
2 changes: 1 addition & 1 deletion .ci/scripts/wheel/test_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
),
test_base.ModelTest(
model=Model.Mv3,
backend=Backend.CoreMlTest,
backend=Backend.CoreMlExportAndTest,
),
]
)
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,6 @@ if(EXECUTORCH_BUILD_PTHREADPOOL AND EXECUTORCH_BUILD_CPUINFO)
endif()

if(EXECUTORCH_BUILD_PYBIND)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/pybind11)

if(NOT EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader)
endif()
Expand Down Expand Up @@ -602,7 +600,7 @@ if(EXECUTORCH_BUILD_PYBIND)
list(APPEND _dep_libs portable_ops_lib)
endif()

if(EXECUTORCH_BUILD_COREML)
if(EXECUTORCH_BUILD_COREML AND APPLE)
list(APPEND _dep_libs coremldelegate)
endif()

Expand Down Expand Up @@ -701,7 +699,7 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
list(APPEND _executor_runner_libs etdump flatccrt)
endif()

if(EXECUTORCH_BUILD_COREML)
if(EXECUTORCH_BUILD_COREML AND APPLE)
list(APPEND _executor_runner_libs coremldelegate)
endif()

Expand Down
225 changes: 112 additions & 113 deletions backends/apple/coreml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,18 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

cmake_minimum_required(VERSION 3.19)

project(executorch_coreml_backend)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

# Source root directory for executorch.
if(NOT EXECUTORCH_ROOT)
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
if(APPLE)
find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED)
find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED)
find_library(COREML_FRAMEWORK CoreML REQUIRED)
find_library(SQLITE_LIBRARY sqlite3 REQUIRED)
endif()

if(EXECUTORCH_BUILD_DEVTOOLS)
# protobuf requires frtti
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -frtti")
endif()

option(COREML_BUILD_EXECUTOR_RUNNER "Build CoreML executor runner." OFF)

# inmemoryfs sources
set(INMEMORYFS_SOURCES
runtime/inmemoryfs/inmemory_filesystem.cpp
runtime/inmemoryfs/inmemory_filesystem_utils.mm
runtime/inmemoryfs/memory_buffer.cpp
runtime/inmemoryfs/memory_stream.cpp
runtime/inmemoryfs/reversed_memory_stream.cpp
)

# kvstore sources
set(KVSTORE_SOURCES
runtime/kvstore/database.cpp runtime/kvstore/json_key_value_store.cpp
Expand Down Expand Up @@ -61,9 +44,6 @@ set(DELEGATE_SOURCES
runtime/delegate/serde_json.mm
)

# util sources
set(UTIL_SOURCES runtime/util/json_util.cpp runtime/util/objc_json_serde.mm)

# sdk sources
set(SDK_SOURCES
runtime/sdk/ETCoreMLModelAnalyzer.mm
Expand Down Expand Up @@ -116,12 +96,19 @@ set(PROTOBUF_SOURCES
runtime/sdk/format/WordTagger.pb.cc
)

find_library(FOUNDATION_FRAMEWORK Foundation)

# CoreML util

set(UTIL_SOURCES runtime/util/json_util.cpp)
if(APPLE)
list(APPEND UTIL_SOURCES runtime/util/objc_json_serde.mm)
endif()

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})
if(APPLE)
target_link_libraries(coreml_util PRIVATE ${FOUNDATION_FRAMEWORK})
endif()
target_compile_options(coreml_util PUBLIC -fPIC)

install(
TARGETS coreml_util
Expand All @@ -131,9 +118,24 @@ install(
)

# CoreML inmemoryfs

set(
INMEMORYFS_SOURCES
runtime/inmemoryfs/inmemory_filesystem.cpp
runtime/inmemoryfs/memory_buffer.cpp
runtime/inmemoryfs/memory_stream.cpp
runtime/inmemoryfs/reversed_memory_stream.cpp
)
if(APPLE)
list(APPEND INMEMORYFS_SOURCES runtime/inmemoryfs/inmemory_filesystem_utils.mm)
endif()

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})
if(APPLE)
target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util ${FOUNDATION_FRAMEWORK})
endif()
target_compile_options(coreml_inmemoryfs PUBLIC -fPIC)

install(
TARGETS coreml_inmemoryfs
Expand All @@ -142,104 +144,101 @@ install(
DESTINATION ${_common_include_directories}
)

# Define the delegate library
add_library(coremldelegate)
target_sources(coremldelegate PRIVATE ${KVSTORE_SOURCES} ${DELEGATE_SOURCES})

target_include_directories(
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
)
target_include_directories(
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/kvstore
)
target_include_directories(
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/delegate
)
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)
# executorchcoreml

if(EXECUTORCH_BUILD_DEVTOOLS)
target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES})
target_include_directories(
coremldelegate
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
if(EXECUTORCH_BUILD_PYBIND)
pybind11_add_module(
executorchcoreml
SHARED
runtime/inmemoryfs/inmemory_filesystem_py.cpp
runtime/inmemoryfs/inmemory_filesystem_utils.cpp
)
add_subdirectory(
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
target_link_libraries(
executorchcoreml
PRIVATE
coreml_util
coreml_inmemoryfs
nlohmann_json::nlohmann_json
)

target_link_options_shared_lib(libprotobuf-lite)
target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
target_compile_options(executorchcoreml PUBLIC -fPIC)
endif()

find_library(ACCELERATE_FRAMEWORK Accelerate)
find_library(COREML_FRAMEWORK CoreML)
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}
)
# coremldelegate

target_link_options_shared_lib(coremldelegate)
if(APPLE)
add_library(coremldelegate)
target_sources(coremldelegate PRIVATE ${KVSTORE_SOURCES} ${DELEGATE_SOURCES})

if(COREML_BUILD_EXECUTOR_RUNNER)
target_link_libraries(
coremldelegate PRIVATE portable_ops_lib portable_kernels
target_include_directories(
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
)
endif()

target_compile_options(coremldelegate PRIVATE "-fobjc-arc")
target_compile_options(coremldelegate PRIVATE "-fno-exceptions")

if(EXECUTORCH_BUILD_DEVTOOLS)
target_compile_options(
executorch_core PUBLIC -DET_EVENT_TRACER_ENABLED
target_include_directories(
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/kvstore
)
target_compile_options(coremldelegate PRIVATE "-frtti")
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
else()
target_compile_options(coremldelegate PRIVATE "-fno-rtti")
endif()

set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
"-x objective-c++"
)

set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
"-Wno-null-character"
)
target_include_directories(
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/delegate
)
target_include_directories(coremldelegate PRIVATE ${PROJECT_SOURCE_DIR}/..)
target_include_directories(coremldelegate PRIVATE ${PROJECT_SOURCE_DIR}/runtime/core/portable_type/c10)
target_compile_definitions(coremldelegate PRIVATE C10_USING_CUSTOM_GENERATED_MACROS)

if(EXECUTORCH_BUILD_DEVTOOLS)
target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES})
target_include_directories(
coremldelegate
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
)
add_subdirectory(
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
)

target_link_options_shared_lib(libprotobuf-lite)
target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
endif()

set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
"-Wno-receiver-expr"
)
target_link_libraries(
coremldelegate
PUBLIC coreml_util
coreml_inmemoryfs
PRIVATE executorch_core
${ACCELERATE_FRAMEWORK}
${COREML_FRAMEWORK}
${FOUNDATION_FRAMEWORK}
${SQLITE_LIBRARY}
)

install(
TARGETS coremldelegate
DESTINATION lib
INCLUDES
DESTINATION ${_common_include_directories}
)
target_link_options_shared_lib(coremldelegate)

# We only care about building the pybinding when building for macOS wheels.
if(EXECUTORCH_BUILD_COREML AND EXECUTORCH_BUILD_PYBIND)
if(NOT TARGET pybind11::pybind11)
add_subdirectory(${EXECUTORCH_ROOT}/third-party/pybind11 ${CMAKE_CURRENT_BINARY_DIR}/pybind11)
if(EXECUTORCH_COREML_BUILD_EXECUTOR_RUNNER)
target_link_libraries(
coremldelegate PRIVATE portable_ops_lib portable_kernels
)
endif()

pybind11_add_module(executorchcoreml SHARED runtime/inmemoryfs/inmemory_filesystem_py.cpp)
target_compile_options(
coremldelegate
PRIVATE
-fobjc-arc
-fno-exceptions
-x objective-c++
-Wno-null-character
-Wno-receiver-expr
)

if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
target_compile_options(executorchcoreml PRIVATE -g)
if(EXECUTORCH_BUILD_DEVTOOLS)
target_compile_options(executorch_core PUBLIC -DET_EVENT_TRACER_ENABLED)
target_compile_options(coremldelegate PRIVATE "-frtti")
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
else()
target_compile_options(coremldelegate PRIVATE "-fno-rtti")
endif()
target_link_libraries(executorchcoreml PRIVATE coreml_util coreml_inmemoryfs)

install(
TARGETS coremldelegate
DESTINATION lib
INCLUDES
DESTINATION ${_common_include_directories}
)
endif()
7 changes: 7 additions & 0 deletions backends/apple/coreml/runtime/util/json_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
//
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.#

#include "json_util.hpp"

#include <cstdint>
#include <string>
#include <vector>

Expand Down
Loading
Loading