Skip to content

Commit 0cf81c0

Browse files
larryliu0820pytorchbot
authored andcommitted
Add executorch_no_prim_ops target (#2934)
Summary: Pull Request resolved: #2934 Currently `libexecutorch.a` always contain prim ops. This becomes a problem when a binary contains 2 "versions" of `libexecutorch.a`, causing a double registration of the prim ops. For example, `libA.so` depends on `libexecutorch.a` and a binary `B` depends on both `libA.so` and `libexecutorch.a`. Since both `libexecutorch.a` and `libA.so` contains prim ops, they will be registered twice. In this PR I created another library `executorch_no_prim_ops` for `libA.so` to depend on. Reviewed By: cccclai, kirklandsign Differential Revision: D55907752 fbshipit-source-id: 755a9b8d5f6f7cf44d011b83bfdc18be6da1aa05 (cherry picked from commit d309e9d)
1 parent cd2779a commit 0cf81c0

File tree

4 files changed

+73
-46
lines changed

4 files changed

+73
-46
lines changed

CMakeLists.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,23 +352,27 @@ add_subdirectory(schema)
352352
# Only contains primitive operators; does not contain portable kernels or other
353353
# full operators. Does not contain any backends.
354354
#
355-
356-
add_library(executorch ${_executorch__srcs})
357-
target_link_libraries(executorch PRIVATE program_schema)
358-
target_link_options_shared_lib(executorch)
355+
add_library(executorch_no_prim_ops ${_executorch_no_prim_ops__srcs})
356+
target_link_libraries(executorch_no_prim_ops PRIVATE program_schema)
359357
# Check if dl exists for this toolchain and only then link it.
360358
find_library(DL_LIBRARY_EXISTS NAMES dl)
361359
# Check if the library was found
362360
if(DL_LIBRARY_EXISTS)
363-
target_link_libraries(executorch PRIVATE dl) # For dladdr()
361+
target_link_libraries(executorch_no_prim_ops PRIVATE dl) # For dladdr()
364362
endif()
365-
target_include_directories(executorch PUBLIC ${_common_include_directories})
366-
target_compile_options(executorch PUBLIC ${_common_compile_options})
363+
target_include_directories(executorch_no_prim_ops PUBLIC ${_common_include_directories})
364+
target_compile_options(executorch_no_prim_ops PUBLIC ${_common_compile_options})
367365
if(MAX_KERNEL_NUM)
368-
target_compile_definitions(executorch
366+
target_compile_definitions(executorch_no_prim_ops
369367
PRIVATE MAX_KERNEL_NUM=${MAX_KERNEL_NUM})
370368
endif()
371369

370+
add_library(executorch ${_executorch__srcs})
371+
target_link_libraries(executorch PRIVATE executorch_no_prim_ops)
372+
target_include_directories(executorch PUBLIC ${_common_include_directories})
373+
target_compile_options(executorch PUBLIC ${_common_compile_options})
374+
target_link_options_shared_lib(executorch)
375+
372376
#
373377
# portable_ops_lib: A library to register core ATen ops using portable kernels,
374378
# see kernels/portable/CMakeLists.txt.
@@ -406,7 +410,7 @@ endif()
406410
# Install `executorch` library as well as `executorch-config.cmake` under
407411
# ${CMAKE_INSTALL_PREFIX}/
408412
install(
409-
TARGETS executorch
413+
TARGETS executorch executorch_no_prim_ops
410414
DESTINATION lib
411415
INCLUDES
412416
DESTINATION ${_common_include_directories})

build/cmake_deps.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ excludes = [
1919
buck_targets = [
2020
"//runtime/executor:program",
2121
]
22+
deps = [
23+
"executorch_no_prim_ops",
24+
]
25+
filters = [
26+
".cpp$",
27+
]
28+
29+
30+
[targets.executorch_no_prim_ops]
31+
buck_targets = [
32+
"//runtime/executor:program_no_prim_ops",
33+
]
2234
deps = [
2335
"program_schema",
2436
]
@@ -43,6 +55,7 @@ excludes = [
4355
]
4456
deps = [
4557
"executorch",
58+
"executorch_no_prim_ops",
4659
]
4760

4861
[targets.optimized_kernels]
@@ -59,6 +72,7 @@ excludes = [
5972
]
6073
deps = [
6174
"executorch",
75+
"executorch_no_prim_ops",
6276
"portable_kernels",
6377
]
6478

@@ -76,6 +90,7 @@ excludes = [
7690
]
7791
deps = [
7892
"executorch",
93+
"executorch_no_prim_ops",
7994
"portable_kernels",
8095
]
8196

@@ -97,6 +112,7 @@ filters = [
97112
excludes = [
98113
]
99114
deps = [
115+
"executorch_no_prim_ops",
100116
"executorch",
101117
]
102118

@@ -113,6 +129,7 @@ filters = [
113129
".cpp$",
114130
]
115131
deps = [
132+
"executorch_no_prim_ops",
116133
"executorch",
117134
]
118135

@@ -125,6 +142,7 @@ filters = [
125142
]
126143
deps = [
127144
"executorch",
145+
"executorch_no_prim_ops",
128146
"extension_data_loader",
129147
]
130148

@@ -137,6 +155,7 @@ filters = [
137155
]
138156
deps = [
139157
"executorch",
158+
"executorch_no_prim_ops",
140159
]
141160

142161
# ---------------------------------- extension end ----------------------------------
@@ -154,6 +173,7 @@ excludes = [
154173
]
155174
deps = [
156175
"executorch",
176+
"executorch_no_prim_ops",
157177
"portable_kernels",
158178
"quantized_kernels",
159179
]
@@ -169,6 +189,7 @@ excludes = [
169189
"^codegen",
170190
]
171191
deps = [
192+
"executorch_no_prim_ops",
172193
"executorch",
173194
]
174195
# ---------------------------------- binary end ----------------------------------
@@ -185,6 +206,7 @@ excludes = [
185206
]
186207
deps = [
187208
"executorch",
209+
"executorch_no_prim_ops",
188210
"portable_kernels",
189211
]
190212

@@ -197,6 +219,7 @@ filters = [
197219
]
198220
deps = [
199221
"executorch",
222+
"executorch_no_prim_ops",
200223
]
201224

202225
[targets.mps_schema]
@@ -222,6 +245,7 @@ excludes = [
222245
]
223246
deps = [
224247
"executorch",
248+
"executorch_no_prim_ops",
225249
"xnnpack_backend",
226250
"portable_kernels",
227251
]
@@ -235,6 +259,7 @@ filters = [
235259
]
236260
deps = [
237261
"executorch",
262+
"executorch_no_prim_ops",
238263
]
239264

240265
[targets.xnnpack_dynamic_quant_utils]
@@ -275,6 +300,7 @@ excludes = [
275300
]
276301
deps = [
277302
"executorch",
303+
"executorch_no_prim_ops",
278304
"optimized_kernels",
279305
"xnnpack_backend",
280306
]
@@ -292,6 +318,7 @@ excludes = [
292318
deps = [
293319
"custom_ops",
294320
"executorch",
321+
"executorch_no_prim_ops",
295322
"extension_data_loader",
296323
"extension_module",
297324
"portable_kernels",

build/executorch-config.cmake

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,20 @@
1313
cmake_minimum_required(VERSION 3.19)
1414

1515
set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
16-
add_library(executorch STATIC IMPORTED)
17-
find_library(
18-
EXECUTORCH_LIBRARY_PATH executorch
19-
HINTS "${_root}"
20-
CMAKE_FIND_ROOT_PATH_BOTH
21-
)
22-
set_target_properties(
23-
executorch PROPERTIES IMPORTED_LOCATION "${EXECUTORCH_LIBRARY_PATH}"
24-
)
25-
target_include_directories(executorch INTERFACE ${_root})
16+
set(required_lib_list executorch executorch_no_prim_ops portable_kernels)
17+
foreach(lib ${required_lib_list})
18+
set(lib_var "LIB_${lib}")
19+
add_library(${lib} STATIC IMPORTED)
20+
find_library(
21+
${lib_var} ${lib} HINTS "${_root}" CMAKE_FIND_ROOT_PATH_BOTH
22+
)
23+
set_target_properties(
24+
${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}"
25+
)
26+
target_include_directories(${lib} INTERFACE ${_root})
27+
endforeach()
2628

27-
add_library(portable_kernels STATIC IMPORTED)
28-
find_library(
29-
PORTABLE_KERNELS_PATH portable_kernels
30-
HINTS "${_root}"
31-
CMAKE_FIND_ROOT_PATH_BOTH
32-
)
33-
set_target_properties(
34-
portable_kernels PROPERTIES IMPORTED_LOCATION "${PORTABLE_KERNELS_PATH}"
35-
)
36-
target_include_directories(portable_kernels INTERFACE ${_root})
29+
target_link_libraries(executorch INTERFACE executorch_no_prim_ops)
3730

3831
if(CMAKE_BUILD_TYPE MATCHES "Debug")
3932
set(FLATCCRT_LIB flatccrt_d)

runtime/executor/targets.bzl

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,47 @@ def define_common_targets():
4444

4545
for aten_mode in (True, False):
4646
aten_suffix = "_aten" if aten_mode else ""
47-
4847
runtime.cxx_library(
4948
name = "program" + aten_suffix,
49+
exported_deps = [
50+
":program_no_prim_ops" + aten_suffix,
51+
"//executorch/kernels/prim_ops:prim_ops_registry" + aten_suffix,
52+
],
53+
visibility = [
54+
"//executorch/runtime/executor/...",
55+
"@EXECUTORCH_CLIENTS",
56+
],
57+
)
58+
59+
runtime.cxx_library(
60+
name = "program_no_prim_ops" + aten_suffix,
5061
srcs = [
5162
"method.cpp",
5263
"method_meta.cpp",
5364
"program.cpp",
5465
"tensor_parser_exec_aten.cpp",
5566
"tensor_parser{}.cpp".format(aten_suffix if aten_mode else "_portable"),
5667
],
57-
headers = [
58-
"tensor_parser.h",
59-
],
6068
exported_headers = [
6169
"method.h",
6270
"method_meta.h",
6371
"program.h",
72+
"tensor_parser.h",
6473
],
65-
deps = [
66-
"//executorch/kernels/prim_ops:prim_ops_registry" + aten_suffix,
74+
preprocessor_flags = _program_preprocessor_flags(),
75+
exported_deps = [
76+
":memory_manager",
6777
"//executorch/runtime/backend:interface",
68-
"//executorch/runtime/core/exec_aten/util:tensor_util" + aten_suffix,
6978
"//executorch/runtime/core:core",
79+
"//executorch/runtime/core:evalue" + aten_suffix,
80+
"//executorch/runtime/core:event_tracer" + aten_suffix,
81+
"//executorch/runtime/core/exec_aten:lib" + aten_suffix,
82+
"//executorch/runtime/core/exec_aten/util:tensor_util" + aten_suffix,
7083
"//executorch/runtime/kernel:kernel_runtime_context" + aten_suffix,
7184
"//executorch/runtime/kernel:operator_registry",
7285
"//executorch/runtime/platform:platform",
7386
"//executorch/schema:extended_header",
7487
"//executorch/schema:program",
75-
":memory_manager",
76-
],
77-
preprocessor_flags = _program_preprocessor_flags(),
78-
exported_deps = [
79-
"//executorch/runtime/core/exec_aten:lib" + aten_suffix,
80-
"//executorch/runtime/core:core",
81-
"//executorch/runtime/core:evalue" + aten_suffix,
82-
"//executorch/runtime/platform:platform",
83-
"//executorch/runtime/core:event_tracer" + aten_suffix,
84-
":memory_manager",
8588
],
8689
visibility = [
8790
"//executorch/runtime/executor/...",

0 commit comments

Comments
 (0)