Skip to content

Commit 9017295

Browse files
authored
Ship the Qualcomm delegate as a linkable component of the wheel (pytorch#21989)
The wheel already carried the Qualcomm delegate, but a C++ application could not link it. The package config published no target for it, so the only way to use it from C++ was to build ExecuTorch from source. Publish it as `executorch::backend_qnn`, the same shape as the XNNPACK, OpenVINO and CUDA delegates. Two things had to change for that to need no special case. The shared build now names the output `libexecutorch_backend_qnn.so` and installs it into `lib/` beside the other delegates. Every other build keeps the original name and location, because the device tooling, the signing step and the exporter's push list all expect them there. Without the rename the finder would have needed a per-component search directory, and the component would have been the one delegate that did not look like the others. The name was previously left alone on the belief that the exporter opens the library from a copy shipped by the vendor SDK. That is not so: the signing script signs this repository's own build output into the SDK tree, so the file under the SDK is ours. Both the name and the location are therefore ours to choose, and the non-shared builds that keep the old spelling are unaffected. The ownership check gains the delegate, required on Linux x86_64 rather than on Linux generally, since the SDK it builds against is published only for that architecture and the aarch64 wheels ship no such library. Verified against an installed wheel with the shipped config replaced and the library staged as the rename produces it: a consumer asking for the component configures and builds; a consumer asking only for the runtime and kernels is unaffected; and with the library removed the component is not defined and the request fails while configuring rather than at link time. The installed files were backed up first and restored after.
1 parent ac9857b commit 9017295

4 files changed

Lines changed: 56 additions & 9 deletions

File tree

‎.ci/scripts/wheel/test_shared_libraries.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import importlib.util
3030
import json
3131
import os
32+
import platform
3233
import re
3334
import shutil
3435
import subprocess
@@ -143,6 +144,7 @@
143144
_BUNDLED_THREADPOOL_SYMBOLS = ("pthreadpool_create", "cpuinfo_initialize")
144145
# The delegate's own entry points. A second definer means the delegate is compiled
145146
# into the Python extension as well, which would register it twice in one process.
147+
_QNN_BACKEND_SYMBOLS = ("QnnExecuTorchBackendRegister",)
146148
_OPENVINO_BACKEND_SYMBOLS = ("executorch::backends::openvino::OpenvinoBackend",)
147149

148150
_BUNDLED_XNNPACK_SYMBOLS = ("xnn_create_runtime_v4",)
@@ -859,6 +861,11 @@ def _wheel_cuda_train() -> str:
859861

860862
# Marker for a row whose owner every Linux wheel carries and no macOS wheel does.
861863
_REQUIRED_ON_LINUX = "linux-only"
864+
# Narrower than the marker above on purpose. The Qualcomm SDK the delegate builds
865+
# against is published for Linux x86_64 only, so the aarch64 Linux wheels ship no such
866+
# library and demanding it there would fail a wheel that is correct. Reusing the
867+
# Linux-wide marker would do exactly that.
868+
_REQUIRED_ON_LINUX_X86 = "linux-x86-only"
862869

863870

864871
def _resolve_required(required):
@@ -867,6 +874,8 @@ def _resolve_required(required):
867874
return bool(_wheel_cuda_train())
868875
if required == _REQUIRED_ON_LINUX:
869876
return sys.platform == "linux"
877+
if required == _REQUIRED_ON_LINUX_X86:
878+
return sys.platform == "linux" and platform.machine() in ("x86_64", "amd64")
870879
return required
871880

872881

@@ -994,6 +1003,16 @@ def _resolve_required(required):
9941003
_library_file_name("libexecutorch_backend_openvino"),
9951004
_REQUIRED_ON_LINUX,
9961005
),
1006+
# Required on Linux x86_64 only, where the Qualcomm SDK the delegate builds against
1007+
# is published. A fixed False would pass a wheel that compiled the delegate back
1008+
# into the extension, since no library ships, the row skips, and one definer inside
1009+
# the extension is the monolithic layout a definer count cannot distinguish.
1010+
(
1011+
"Qualcomm delegate",
1012+
_QNN_BACKEND_SYMBOLS,
1013+
_library_file_name("libexecutorch_backend_qnn"),
1014+
_REQUIRED_ON_LINUX_X86,
1015+
),
9971016
)
9981017

9991018
# The one component that legitimately exists twice. The code generator compiles the quantized kernel
@@ -2280,6 +2299,7 @@ def test_shipped_library_names_are_expected() -> None:
22802299
"libextension_cuda",
22812300
"libexecutorch_backend_xnnpack",
22822301
"libexecutorch_backend_openvino",
2302+
"libexecutorch_backend_qnn",
22832303
"libexecutorch_threadpool",
22842304
"libexecutorch_etdump",
22852305
)

‎backends/qualcomm/CMakeLists.txt‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,15 @@ target_link_libraries(
262262
if(EXECUTORCH_BUILD_SHARED)
263263
target_link_libraries(qnn_executorch_backend PRIVATE executorch_shared)
264264
executorch_target_link_shared_runtime(qnn_executorch_backend)
265+
# Named like the other shipped delegates, so a consumer of the wheel finds it
266+
# where it finds them and the package config needs no special case. Only the
267+
# shared build renames: the device and direct-mode builds keep the original
268+
# name, which their own tooling and the signing step expect.
269+
set_target_properties(
270+
qnn_executorch_backend PROPERTIES OUTPUT_NAME executorch_backend_qnn
271+
)
265272
executorch_target_shared_runtime_path(
266-
qnn_executorch_backend "backends/qualcomm"
267-
"${CMAKE_INSTALL_LIBDIR}/executorch/backends/qualcomm"
273+
qnn_executorch_backend "lib" "${CMAKE_INSTALL_LIBDIR}"
268274
)
269275
else()
270276
target_link_libraries(qnn_executorch_backend PRIVATE executorch_core)
@@ -336,12 +342,20 @@ add_subdirectory(
336342
${QNN_EXECUTORCH_ROOT_DIR}/aot/wrappers
337343
${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/wrappers
338344
)
345+
# The shared build installs beside the other delegates; every other build keeps
346+
# the original location, which the device tooling and the exporter's push list
347+
# expect.
348+
if(EXECUTORCH_BUILD_SHARED)
349+
set(_qnn_install_dir ${CMAKE_INSTALL_LIBDIR})
350+
else()
351+
set(_qnn_install_dir ${CMAKE_INSTALL_LIBDIR}/executorch/backends/qualcomm)
352+
endif()
339353
install(
340354
TARGETS qnn_executorch_backend
341355
EXPORT ExecuTorchTargets
342-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/executorch/backends/qualcomm
343-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/executorch/backends/qualcomm
344-
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/executorch/backends/qualcomm
356+
LIBRARY DESTINATION ${_qnn_install_dir}
357+
ARCHIVE DESTINATION ${_qnn_install_dir}
358+
RUNTIME DESTINATION ${_qnn_install_dir}
345359
)
346360

347361
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES Hexagon)

‎setup.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,12 +2394,19 @@ def run(self): # noqa C901
23942394
),
23952395
# The stream helper this library needs ships in lib/ from here on,
23962396
# alongside the other components a C++ consumer links.
2397+
# The Qualcomm delegate, beside the others so a C++ consumer links it
2398+
# the same way. Only the adapter ships here: the Qualcomm runtime is
2399+
# resolved by name at run time and comes from the vendor SDK, which a
2400+
# user installs separately. The shared build renames the output to
2401+
# match its siblings, so this looks for that name.
23972402
BuiltFile(
23982403
src_dir="%CMAKE_CACHE_DIR%/backends/qualcomm/%BUILD_TYPE%/",
2399-
src_name="qnn_executorch_backend",
2400-
dst="executorch/backends/qualcomm/",
2401-
is_dynamic_lib=True,
2402-
dependent_cmake_flags=["EXECUTORCH_BUILD_QNN"],
2404+
src_name="*executorch_backend_qnn" + _dynamic_lib_suffix(),
2405+
dst="executorch/lib/",
2406+
dependent_cmake_flags=[
2407+
"EXECUTORCH_BUILD_SHARED",
2408+
"EXECUTORCH_BUILD_QNN",
2409+
],
24032410
),
24042411
BuiltExtension(
24052412
src_dir="backends/qualcomm/%BUILD_TYPE%/",

‎tools/cmake/executorch-wheel-config.cmake‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ _executorch_define_component(backend_openvino executorch_backend_openvino)
650650
# while configuring.
651651
_executorch_define_component(backend_cuda executorch_backend_cuda)
652652
_executorch_define_component(extension_cuda executorch_extension_cuda)
653+
# The Qualcomm delegate, present only in a wheel whose build found the QNN SDK,
654+
# which today means Linux x86_64. Like the OpenVINO delegate it carries no
655+
# undefined vendor symbols, so it links without the SDK present and resolves the
656+
# SDK by name at run time. A consumer still needs the SDK installed to run a
657+
# model through it.
658+
_executorch_define_component(backend_qnn executorch_backend_qnn)
653659

654660
# Find prebuilt _C.<EXT_SUFFIX>.so, the extension a custom-op project builds
655661
# against. Reported for that purpose only, not published as a target: see the

0 commit comments

Comments
 (0)