Skip to content

Commit cb583f3

Browse files
authored
Separate CoreML util and inmemoryfs targets to be reusable (#9481)
### Summary In this diff, we separate the targets for `coreml/runtime/util` and `coreml/runtime/inmemoryfs`. #### Why? As part of #9019, we want to support CoreML export out-of-the-box. Unfortunately, part of the workflow now includes installing an executorch+coreml binding as a pip wheel: https://github.com/pytorch/executorch/blob/5fdfa511966208c7e237a9e920a7c63f513b4fb7/backends/apple/coreml/scripts/install_inmemoryfs.sh#L20 This then gets used throughout the library. i.e. https://github.com/pytorch/executorch/blob/5fdfa511966208c7e237a9e920a7c63f513b4fb7/backends/apple/coreml/compiler/coreml_preprocess.py#L19 So we want to now bake this pybinding into the main wheel. Ultimately this `executorchcoreml` module, gets built using inmemoryfs: https://github.com/pytorch/executorch/blob/5fdfa511966208c7e237a9e920a7c63f513b4fb7/backends/apple/coreml/runtime/inmemoryfs/setup.py#L17-L38 So, to enable creating a pybinding target, we must first decouple the `util` and `inmemoryfs` libraries so they can be used in `coremldelegate` and the future pybinding. ### Test plan This should really be a no-op w.r.t. builds. So, CI + ``` $ ./install_executorch.sh --pybind coreml ``` cc @larryliu0820 @lucylq
1 parent d15c0d0 commit cb583f3

File tree

5 files changed

+69
-22
lines changed

5 files changed

+69
-22
lines changed

backends/apple/coreml/CMakeLists.txt

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Copyright © 2023 Apple Inc. All rights reserved.
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
27

38
cmake_minimum_required(VERSION 3.19)
49

@@ -111,32 +116,48 @@ set(PROTOBUF_SOURCES
111116
runtime/sdk/format/WordTagger.pb.cc
112117
)
113118

119+
find_library(FOUNDATION_FRAMEWORK Foundation)
120+
121+
# CoreML util
122+
add_library(coreml_util ${UTIL_SOURCES})
123+
target_include_directories(coreml_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util)
124+
target_link_libraries(coreml_util PRIVATE ${FOUNDATION_FRAMEWORK})
125+
126+
install(
127+
TARGETS coreml_util
128+
DESTINATION lib
129+
INCLUDES
130+
DESTINATION ${_common_include_directories}
131+
)
132+
133+
# CoreML inmemoryfs
134+
add_library(coreml_inmemoryfs ${INMEMORYFS_SOURCES})
135+
target_include_directories(coreml_inmemoryfs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs)
136+
target_link_libraries(coreml_inmemoryfs PRIVATE coreml_util ${FOUNDATION_FRAMEWORK})
137+
138+
install(
139+
TARGETS coreml_inmemoryfs
140+
DESTINATION lib
141+
INCLUDES
142+
DESTINATION ${_common_include_directories}
143+
)
144+
114145
# Define the delegate library
115146
add_library(coremldelegate)
116-
target_sources(
117-
coremldelegate PRIVATE ${INMEMORYFS_SOURCES} ${KVSTORE_SOURCES}
118-
${DELEGATE_SOURCES} ${UTIL_SOURCES}
119-
)
147+
target_sources(coremldelegate PRIVATE ${KVSTORE_SOURCES} ${DELEGATE_SOURCES})
120148

121149
target_include_directories(
122150
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
123151
)
124152
target_include_directories(
125153
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/kvstore
126154
)
127-
target_include_directories(
128-
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs
129-
)
130155
target_include_directories(
131156
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/delegate
132157
)
133-
target_include_directories(
134-
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util
135-
)
136158
target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/..)
137159
target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/runtime/core/portable_type/c10)
138160
target_compile_definitions(coremldelegate PRIVATE C10_USING_CUSTOM_GENERATED_MACROS)
139-
target_link_libraries(coremldelegate PRIVATE executorch_core)
140161

141162
if(EXECUTORCH_BUILD_DEVTOOLS)
142163
target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES})
@@ -156,13 +177,17 @@ endif()
156177

157178
find_library(ACCELERATE_FRAMEWORK Accelerate)
158179
find_library(COREML_FRAMEWORK CoreML)
159-
find_library(FOUNDATION_FRAMEWORK Foundation)
160180
find_library(SQLITE_LIBRARY sqlite3)
161181

162182
target_link_libraries(
163183
coremldelegate
164-
PRIVATE executorch_core ${ACCELERATE_FRAMEWORK} ${COREML_FRAMEWORK}
165-
${FOUNDATION_FRAMEWORK} ${SQLITE_LIBRARY}
184+
PUBLIC coreml_util
185+
coreml_inmemoryfs
186+
PRIVATE executorch_core
187+
${ACCELERATE_FRAMEWORK}
188+
${COREML_FRAMEWORK}
189+
${FOUNDATION_FRAMEWORK}
190+
${SQLITE_LIBRARY}
166191
)
167192

168193
target_link_options_shared_lib(coremldelegate)

examples/apple/coreml/executor_runner/coreml_executor_runner.xcodeproj/project.pbxproj

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/* Begin PBXBuildFile section */
1010
38626BB42B225A560059413D /* libflatccrt.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 38626BB32B225A560059413D /* libflatccrt.a */; };
1111
38626BB52B225A890059413D /* libetdump.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 38626BAF2B21C98F0059413D /* libetdump.a */; };
12+
879121DA2D91DDBA001E6C66 /* libcoreml_inmemoryfs.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 879121D82D91DDBA001E6C66 /* libcoreml_inmemoryfs.a */; };
13+
879121DB2D91DDBA001E6C66 /* libcoreml_util.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 879121D92D91DDBA001E6C66 /* libcoreml_util.a */; };
1214
C94D51592ACF4BFC00AF47FD /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = C94D51582ACF4BFC00AF47FD /* main.mm */; };
1315
C94D515E2ACFCBA000AF47FD /* libexecutorch.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C94D515C2ACFCBA000AF47FD /* libexecutorch.a */; };
1416
C94D51622ACFCBBA00AF47FD /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = C94D51612ACFCBBA00AF47FD /* libsqlite3.tbd */; };
@@ -36,6 +38,8 @@
3638
/* Begin PBXFileReference section */
3739
38626BAF2B21C98F0059413D /* libetdump.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libetdump.a; path = libraries/libetdump.a; sourceTree = "<group>"; };
3840
38626BB32B225A560059413D /* libflatccrt.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libflatccrt.a; path = "../../../../third-party/flatcc/lib/libflatccrt.a"; sourceTree = "<group>"; };
41+
879121D82D91DDBA001E6C66 /* libcoreml_inmemoryfs.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libcoreml_inmemoryfs.a; path = "libraries/libcoreml_inmemoryfs.a"; sourceTree = "<group>"; };
42+
879121D92D91DDBA001E6C66 /* libcoreml_util.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libcoreml_util.a; path = "libraries/libcoreml_util.a"; sourceTree = "<group>"; };
3943
C94D514E2ACF4B9300AF47FD /* coreml_executor_runner */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = coreml_executor_runner; sourceTree = BUILT_PRODUCTS_DIR; };
4044
C94D51582ACF4BFC00AF47FD /* main.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = main.mm; sourceTree = "<group>"; };
4145
C94D515C2ACFCBA000AF47FD /* libexecutorch.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libexecutorch.a; path = libraries/libexecutorch.a; sourceTree = "<group>"; };
@@ -54,6 +58,8 @@
5458
isa = PBXFrameworksBuildPhase;
5559
buildActionMask = 2147483647;
5660
files = (
61+
879121DA2D91DDBA001E6C66 /* libcoreml_inmemoryfs.a in Frameworks */,
62+
879121DB2D91DDBA001E6C66 /* libcoreml_util.a in Frameworks */,
5763
38626BB52B225A890059413D /* libetdump.a in Frameworks */,
5864
F24817E72BC65B2000E80D98 /* libexecutorch_core.a in Frameworks */,
5965
38626BB42B225A560059413D /* libflatccrt.a in Frameworks */,
@@ -91,6 +97,8 @@
9197
C94D51602ACFCBBA00AF47FD /* Frameworks */ = {
9298
isa = PBXGroup;
9399
children = (
100+
879121D82D91DDBA001E6C66 /* libcoreml_inmemoryfs.a */,
101+
879121D92D91DDBA001E6C66 /* libcoreml_util.a */,
94102
C988D69C2B998CD700979CF6 /* libprotobuf-lite.a */,
95103
38626BB32B225A560059413D /* libflatccrt.a */,
96104
38626BAF2B21C98F0059413D /* libetdump.a */,
@@ -214,7 +222,7 @@
214222
GCC_OPTIMIZATION_LEVEL = 0;
215223
GCC_PREPROCESSOR_DEFINITIONS = (
216224
"DEBUG=1",
217-
"C10_USING_CUSTOM_GENERATED_MACROS",
225+
C10_USING_CUSTOM_GENERATED_MACROS,
218226
"$(inherited)",
219227
);
220228
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -273,7 +281,7 @@
273281
GCC_C_LANGUAGE_STANDARD = gnu17;
274282
GCC_NO_COMMON_BLOCKS = YES;
275283
GCC_PREPROCESSOR_DEFINITIONS = (
276-
"C10_USING_CUSTOM_GENERATED_MACROS",
284+
C10_USING_CUSTOM_GENERATED_MACROS,
277285
"$(inherited)",
278286
);
279287
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -297,9 +305,9 @@
297305
DEVELOPMENT_TEAM = "";
298306
ENABLE_HARDENED_RUNTIME = YES;
299307
HEADER_SEARCH_PATHS = (
300-
"$(SRCROOT)/include",
301-
"$(SRCROOT)/include/executorch/runtime/core/portable_type/c10",
302-
);
308+
"$(SRCROOT)/include",
309+
"$(SRCROOT)/include/executorch/runtime/core/portable_type/c10",
310+
);
303311
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
304312
LIBRARY_SEARCH_PATHS = (
305313
"$(SRCROOT)/libraries",
@@ -319,9 +327,9 @@
319327
DEVELOPMENT_TEAM = "";
320328
ENABLE_HARDENED_RUNTIME = YES;
321329
HEADER_SEARCH_PATHS = (
322-
"$(SRCROOT)/include",
323-
"$(SRCROOT)/include/executorch/runtime/core/portable_type/c10",
324-
);
330+
"$(SRCROOT)/include",
331+
"$(SRCROOT)/include/executorch/runtime/core/portable_type/c10",
332+
);
325333
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
326334
LIBRARY_SEARCH_PATHS = (
327335
"$(SRCROOT)/libraries",

examples/apple/coreml/scripts/build_executor_runner.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ find "$CMAKE_BUILD_DIR_PATH/" -name 'libexecutorch_core.a' -exec cp -f "{}" "$LI
6666
find "$CMAKE_BUILD_DIR_PATH/" -name 'libprotobuf-lite.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libprotobuf-lite.a" \;
6767
find "$CMAKE_BUILD_DIR_PATH/" -name 'libprotobuf-lited.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libprotobuf-lite.a" \;
6868
find "$CMAKE_BUILD_DIR_PATH/" -name 'libetdump.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libetdump.a" \;
69+
find "$CMAKE_BUILD_DIR_PATH/" -name 'libcoreml_util.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libcoreml_util.a" \;
70+
find "$CMAKE_BUILD_DIR_PATH/" -name 'libcoreml_inmemoryfs.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libcoreml_inmemoryfs.a" \;
6971
find "$CMAKE_BUILD_DIR_PATH/" -name 'libcoremldelegate.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libcoremldelegate.a" \;
7072
find "$CMAKE_BUILD_DIR_PATH/" -name 'libportable_ops_lib.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libportable_ops_lib.a" \;
7173
find "$CMAKE_BUILD_DIR_PATH/" -name 'libportable_kernels.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libportable_kernels.a" \;

scripts/build_apple_frameworks.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ libextension_tensor.a,\
3535
:$HEADERS_PATH"
3636

3737
FRAMEWORK_BACKEND_COREML="backend_coreml:\
38+
libcoreml_util.a,\
39+
libcoreml_inmemoryfs.a,\
3840
libcoremldelegate.a,\
3941
:"
4042

tools/cmake/executorch-config.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ set(lib_list
6767
bundled_program
6868
extension_data_loader
6969
${FLATCCRT_LIB}
70+
coreml_util
71+
coreml_inmemoryfs
7072
coremldelegate
7173
mpsdelegate
7274
neuron_backend
@@ -143,6 +145,14 @@ if(TARGET optimized_kernels)
143145
"executorch_core;cpublas;extension_threadpool"
144146
)
145147
endif()
148+
149+
if(TARGET coremldelegate)
150+
set_target_properties(
151+
coremldelegate PROPERTIES INTERFACE_LINK_LIBRARIES
152+
"coreml_inmemoryfs;coreml_util"
153+
)
154+
endif()
155+
146156
if(TARGET optimized_native_cpu_ops_lib)
147157
if(TARGET optimized_portable_kernels)
148158
set(_maybe_optimized_portable_kernels_lib optimized_portable_kernels)

0 commit comments

Comments
 (0)