Skip to content

Commit bc47f5a

Browse files
authored
Support CoreML export on Linux (#11172)
### Summary * Fixes #9800 * Adds support for CoreML exports on Linux * Test CoreML `.pte` export on Linux in CI ### Test plan ``` $ ./install_executorch.sh $ GITHUB_WORKSPACE=$(dirname $(pwd)) REPOSITORY=$(basename $(pwd)) python .ci/scripts/wheel/test_linux.py ```
1 parent b00a90a commit bc47f5a

File tree

12 files changed

+167
-148
lines changed

12 files changed

+167
-148
lines changed

.ci/scripts/wheel/test_linux.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
test_base.ModelTest(
1515
model=Model.Mv3,
1616
backend=Backend.XnnpackQuantizationDelegation,
17-
)
17+
),
18+
test_base.ModelTest(
19+
model=Model.Mv3,
20+
backend=Backend.CoreMlExportOnly,
21+
),
1822
]
1923
)

.ci/scripts/wheel/test_macos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
),
1818
test_base.ModelTest(
1919
model=Model.Mv3,
20-
backend=Backend.CoreMlTest,
20+
backend=Backend.CoreMlExportAndTest,
2121
),
2222
]
2323
)

CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,6 @@ if(EXECUTORCH_BUILD_PTHREADPOOL AND EXECUTORCH_BUILD_CPUINFO)
565565
endif()
566566

567567
if(EXECUTORCH_BUILD_PYBIND)
568-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/pybind11)
569-
570568
if(NOT EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)
571569
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader)
572570
endif()
@@ -602,7 +600,7 @@ if(EXECUTORCH_BUILD_PYBIND)
602600
list(APPEND _dep_libs portable_ops_lib)
603601
endif()
604602

605-
if(EXECUTORCH_BUILD_COREML)
603+
if(EXECUTORCH_BUILD_COREML AND APPLE)
606604
list(APPEND _dep_libs coremldelegate)
607605
endif()
608606

@@ -701,7 +699,7 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
701699
list(APPEND _executor_runner_libs etdump flatccrt)
702700
endif()
703701

704-
if(EXECUTORCH_BUILD_COREML)
702+
if(EXECUTORCH_BUILD_COREML AND APPLE)
705703
list(APPEND _executor_runner_libs coremldelegate)
706704
endif()
707705

backends/apple/coreml/CMakeLists.txt

Lines changed: 112 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,18 @@
55
# This source code is licensed under the BSD-style license found in the
66
# LICENSE file in the root directory of this source tree.
77

8-
cmake_minimum_required(VERSION 3.19)
9-
10-
project(executorch_coreml_backend)
11-
12-
if(NOT CMAKE_CXX_STANDARD)
13-
set(CMAKE_CXX_STANDARD 17)
14-
endif()
15-
16-
# Source root directory for executorch.
17-
if(NOT EXECUTORCH_ROOT)
18-
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
8+
if(APPLE)
9+
find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED)
10+
find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED)
11+
find_library(COREML_FRAMEWORK CoreML REQUIRED)
12+
find_library(SQLITE_LIBRARY sqlite3 REQUIRED)
1913
endif()
2014

2115
if(EXECUTORCH_BUILD_DEVTOOLS)
2216
# protobuf requires frtti
2317
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -frtti")
2418
endif()
2519

26-
option(COREML_BUILD_EXECUTOR_RUNNER "Build CoreML executor runner." OFF)
27-
28-
# inmemoryfs sources
29-
set(INMEMORYFS_SOURCES
30-
runtime/inmemoryfs/inmemory_filesystem.cpp
31-
runtime/inmemoryfs/inmemory_filesystem_utils.mm
32-
runtime/inmemoryfs/memory_buffer.cpp
33-
runtime/inmemoryfs/memory_stream.cpp
34-
runtime/inmemoryfs/reversed_memory_stream.cpp
35-
)
36-
3720
# kvstore sources
3821
set(KVSTORE_SOURCES
3922
runtime/kvstore/database.cpp runtime/kvstore/json_key_value_store.cpp
@@ -61,9 +44,6 @@ set(DELEGATE_SOURCES
6144
runtime/delegate/serde_json.mm
6245
)
6346

64-
# util sources
65-
set(UTIL_SOURCES runtime/util/json_util.cpp runtime/util/objc_json_serde.mm)
66-
6747
# sdk sources
6848
set(SDK_SOURCES
6949
runtime/sdk/ETCoreMLModelAnalyzer.mm
@@ -116,12 +96,19 @@ set(PROTOBUF_SOURCES
11696
runtime/sdk/format/WordTagger.pb.cc
11797
)
11898

119-
find_library(FOUNDATION_FRAMEWORK Foundation)
120-
12199
# CoreML util
100+
101+
set(UTIL_SOURCES runtime/util/json_util.cpp)
102+
if(APPLE)
103+
list(APPEND UTIL_SOURCES runtime/util/objc_json_serde.mm)
104+
endif()
105+
122106
add_library(coreml_util ${UTIL_SOURCES})
123107
target_include_directories(coreml_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util)
124-
target_link_libraries(coreml_util PRIVATE ${FOUNDATION_FRAMEWORK})
108+
if(APPLE)
109+
target_link_libraries(coreml_util PRIVATE ${FOUNDATION_FRAMEWORK})
110+
endif()
111+
target_compile_options(coreml_util PUBLIC -fPIC)
125112

126113
install(
127114
TARGETS coreml_util
@@ -131,9 +118,24 @@ install(
131118
)
132119

133120
# CoreML inmemoryfs
121+
122+
set(
123+
INMEMORYFS_SOURCES
124+
runtime/inmemoryfs/inmemory_filesystem.cpp
125+
runtime/inmemoryfs/memory_buffer.cpp
126+
runtime/inmemoryfs/memory_stream.cpp
127+
runtime/inmemoryfs/reversed_memory_stream.cpp
128+
)
129+
if(APPLE)
130+
list(APPEND INMEMORYFS_SOURCES runtime/inmemoryfs/inmemory_filesystem_utils.mm)
131+
endif()
132+
134133
add_library(coreml_inmemoryfs ${INMEMORYFS_SOURCES})
135134
target_include_directories(coreml_inmemoryfs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs)
136-
target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util ${FOUNDATION_FRAMEWORK})
135+
if(APPLE)
136+
target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util ${FOUNDATION_FRAMEWORK})
137+
endif()
138+
target_compile_options(coreml_inmemoryfs PUBLIC -fPIC)
137139

138140
install(
139141
TARGETS coreml_inmemoryfs
@@ -142,104 +144,101 @@ install(
142144
DESTINATION ${_common_include_directories}
143145
)
144146

145-
# Define the delegate library
146-
add_library(coremldelegate)
147-
target_sources(coremldelegate PRIVATE ${KVSTORE_SOURCES} ${DELEGATE_SOURCES})
148-
149-
target_include_directories(
150-
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
151-
)
152-
target_include_directories(
153-
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/kvstore
154-
)
155-
target_include_directories(
156-
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/delegate
157-
)
158-
target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/..)
159-
target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/runtime/core/portable_type/c10)
160-
target_compile_definitions(coremldelegate PRIVATE C10_USING_CUSTOM_GENERATED_MACROS)
147+
# executorchcoreml
161148

162-
if(EXECUTORCH_BUILD_DEVTOOLS)
163-
target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES})
164-
target_include_directories(
165-
coremldelegate
166-
PRIVATE
167-
${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
168-
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
149+
if(EXECUTORCH_BUILD_PYBIND)
150+
pybind11_add_module(
151+
executorchcoreml
152+
SHARED
153+
runtime/inmemoryfs/inmemory_filesystem_py.cpp
154+
runtime/inmemoryfs/inmemory_filesystem_utils.cpp
169155
)
170-
add_subdirectory(
171-
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
156+
target_link_libraries(
157+
executorchcoreml
158+
PRIVATE
159+
coreml_util
160+
coreml_inmemoryfs
161+
nlohmann_json::nlohmann_json
172162
)
173-
174-
target_link_options_shared_lib(libprotobuf-lite)
175-
target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
163+
target_compile_options(executorchcoreml PUBLIC -fPIC)
176164
endif()
177165

178-
find_library(ACCELERATE_FRAMEWORK Accelerate)
179-
find_library(COREML_FRAMEWORK CoreML)
180-
find_library(SQLITE_LIBRARY sqlite3)
181-
182-
target_link_libraries(
183-
coremldelegate
184-
PUBLIC coreml_util
185-
coreml_inmemoryfs
186-
PRIVATE executorch_core
187-
${ACCELERATE_FRAMEWORK}
188-
${COREML_FRAMEWORK}
189-
${FOUNDATION_FRAMEWORK}
190-
${SQLITE_LIBRARY}
191-
)
166+
# coremldelegate
192167

193-
target_link_options_shared_lib(coremldelegate)
168+
if(APPLE)
169+
add_library(coremldelegate)
170+
target_sources(coremldelegate PRIVATE ${KVSTORE_SOURCES} ${DELEGATE_SOURCES})
194171

195-
if(COREML_BUILD_EXECUTOR_RUNNER)
196-
target_link_libraries(
197-
coremldelegate PRIVATE portable_ops_lib portable_kernels
172+
target_include_directories(
173+
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
198174
)
199-
endif()
200-
201-
target_compile_options(coremldelegate PRIVATE "-fobjc-arc")
202-
target_compile_options(coremldelegate PRIVATE "-fno-exceptions")
203-
204-
if(EXECUTORCH_BUILD_DEVTOOLS)
205-
target_compile_options(
206-
executorch_core PUBLIC -DET_EVENT_TRACER_ENABLED
175+
target_include_directories(
176+
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/kvstore
207177
)
208-
target_compile_options(coremldelegate PRIVATE "-frtti")
209-
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
210-
else()
211-
target_compile_options(coremldelegate PRIVATE "-fno-rtti")
212-
endif()
213-
214-
set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
215-
"-x objective-c++"
216-
)
217-
218-
set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
219-
"-Wno-null-character"
220-
)
178+
target_include_directories(
179+
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/delegate
180+
)
181+
target_include_directories(coremldelegate PRIVATE ${PROJECT_SOURCE_DIR}/..)
182+
target_include_directories(coremldelegate PRIVATE ${PROJECT_SOURCE_DIR}/runtime/core/portable_type/c10)
183+
target_compile_definitions(coremldelegate PRIVATE C10_USING_CUSTOM_GENERATED_MACROS)
184+
185+
if(EXECUTORCH_BUILD_DEVTOOLS)
186+
target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES})
187+
target_include_directories(
188+
coremldelegate
189+
PRIVATE
190+
${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
191+
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
192+
)
193+
add_subdirectory(
194+
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
195+
)
196+
197+
target_link_options_shared_lib(libprotobuf-lite)
198+
target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
199+
endif()
221200

222-
set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
223-
"-Wno-receiver-expr"
224-
)
201+
target_link_libraries(
202+
coremldelegate
203+
PUBLIC coreml_util
204+
coreml_inmemoryfs
205+
PRIVATE executorch_core
206+
${ACCELERATE_FRAMEWORK}
207+
${COREML_FRAMEWORK}
208+
${FOUNDATION_FRAMEWORK}
209+
${SQLITE_LIBRARY}
210+
)
225211

226-
install(
227-
TARGETS coremldelegate
228-
DESTINATION lib
229-
INCLUDES
230-
DESTINATION ${_common_include_directories}
231-
)
212+
target_link_options_shared_lib(coremldelegate)
232213

233-
# We only care about building the pybinding when building for macOS wheels.
234-
if(EXECUTORCH_BUILD_COREML AND EXECUTORCH_BUILD_PYBIND)
235-
if(NOT TARGET pybind11::pybind11)
236-
add_subdirectory(${EXECUTORCH_ROOT}/third-party/pybind11 ${CMAKE_CURRENT_BINARY_DIR}/pybind11)
214+
if(EXECUTORCH_COREML_BUILD_EXECUTOR_RUNNER)
215+
target_link_libraries(
216+
coremldelegate PRIVATE portable_ops_lib portable_kernels
217+
)
237218
endif()
238219

239-
pybind11_add_module(executorchcoreml SHARED runtime/inmemoryfs/inmemory_filesystem_py.cpp)
220+
target_compile_options(
221+
coremldelegate
222+
PRIVATE
223+
-fobjc-arc
224+
-fno-exceptions
225+
-x objective-c++
226+
-Wno-null-character
227+
-Wno-receiver-expr
228+
)
240229

241-
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
242-
target_compile_options(executorchcoreml PRIVATE -g)
230+
if(EXECUTORCH_BUILD_DEVTOOLS)
231+
target_compile_options(executorch_core PUBLIC -DET_EVENT_TRACER_ENABLED)
232+
target_compile_options(coremldelegate PRIVATE "-frtti")
233+
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
234+
else()
235+
target_compile_options(coremldelegate PRIVATE "-fno-rtti")
243236
endif()
244-
target_link_libraries(executorchcoreml PRIVATE coreml_util coreml_inmemoryfs)
237+
238+
install(
239+
TARGETS coremldelegate
240+
DESTINATION lib
241+
INCLUDES
242+
DESTINATION ${_common_include_directories}
243+
)
245244
endif()

backends/apple/coreml/runtime/util/json_util.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
// Copyright © 2024 Apple Inc. All rights reserved.
66
//
77
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
8+
//
9+
// Copyright (c) Meta Platforms, Inc. and affiliates.
10+
// All rights reserved.
11+
//
12+
// This source code is licensed under the BSD-style license found in the
13+
// LICENSE file in the root directory of this source tree.#
814

915
#include "json_util.hpp"
1016

17+
#include <cstdint>
1118
#include <string>
1219
#include <vector>
1320

0 commit comments

Comments
 (0)