Skip to content

Commit ef97148

Browse files
Hao-Wei Hsushewu-quicchunit-quicchiwwangharshs-qti
authored andcommitted
Qualcomm AI Engine Direct Backend (#490)
Summary: Hi there, This commit adds QNN backend. Please check setup.md and README under `backends/qnn` for more details. We add pybind dependency at the top level, because we guess you might need it in the near future. If it's not the case, please feel free to tell us. We'll move it under `backends/qnn`. Below is some SoC model mapping: - `SM8550` -> `Snapdragon 8 Gen 2` - `SM8475` -> `Snapdragon 8 Gen 1+` - `SM8450` -> `Snapdragon 8 Gen 1` Please feel free to contact us if you find any problem. --------- Pull Request resolved: #490 Reviewed By: cccclai Differential Revision: D49686980 Pulled By: kimishpatel fbshipit-source-id: 5398137bd015a056de80f55a921f4d718df08a24 Co-authored-by: shewu-quic <[email protected]> Co-authored-by: chunit-quic <[email protected]> Co-authored-by: chiwwang <[email protected]> Co-authored-by: harshs-qti <[email protected]>
1 parent de3c024 commit ef97148

File tree

135 files changed

+14548
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+14548
-5
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
[submodule "third-party/googletest"]
1414
path = third-party/googletest
1515
url = https://github.com/google/googletest.git
16+
[submodule "third-party/pybind11"]
17+
path = third-party/pybind11
18+
url = https://github.com/pybind/pybind11.git
1619
[submodule "backends/xnnpack/third-party/pthreadpool"]
1720
path = backends/xnnpack/third-party/pthreadpool
1821
url = https://github.com/Maratyszcza/pthreadpool.git

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ if(EXECUTORCH_BUILD_XNNPACK)
307307
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/xnnpack)
308308
endif()
309309

310+
option(EXECUTORCH_BUILD_QNN "Build the backends/qualcomm directory" OFF)
311+
if(EXECUTORCH_BUILD_QNN)
312+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/qualcomm)
313+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/qualcomm)
314+
endif()
315+
310316
# Add selective build subdirectory
311317
if(BUILD_SELECTIVE_BUILD_TEST)
312318
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/selective_build)

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ For "ExecuTorch" software
44

55
Copyright (c) Meta Platforms, Inc. and affiliates.
66
Copyright 2023 Arm Limited and/or its affiliates.
7+
Copyright (c) Qualcomm Innovation Center, Inc.
78

89
Redistribution and use in source and binary forms, with or without modification,
910
are permitted provided that the following conditions are met:

backends/qualcomm/CMakeLists.txt

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
# Copyright (c) Qualcomm Innovation Center, Inc.
2+
# All rights reserved
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
10+
11+
#
12+
# get path
13+
#
14+
get_filename_component(EXECUTORCH_SOURCE_DIR
15+
"${CMAKE_CURRENT_LIST_DIR}/../.."
16+
ABSOLUTE)
17+
get_filename_component(QNN_EXECUTORCH_ROOT_DIR
18+
${CMAKE_CURRENT_LIST_DIR}
19+
ABSOLUTE)
20+
# Let files say "include <executorch/path/to/header.h>".
21+
get_filename_component(_common_include_directories
22+
"${EXECUTORCH_SOURCE_DIR}/.."
23+
ABSOLUTE)
24+
25+
if(NOT DEFINED QNN_SDK_ROOT)
26+
message(FATAL_ERROR
27+
"Please define QNN_SDK_ROOT, e.g. cmake <..> -DQNN_SDK_ROOT=<...>")
28+
elseif(CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\.toolchain\.cmake$")
29+
message(FATAL_ERROR
30+
"ios is not supported by Qualcomm AI Engine Direct")
31+
endif()
32+
33+
message(STATUS "Using qnn sdk root ${QNN_SDK_ROOT}")
34+
message(STATUS "Using EXECUTORCH_SOURCE_DIR ${EXECUTORCH_SOURCE_DIR}")
35+
36+
if(${ANDROID})
37+
find_library(android_log log)
38+
endif()
39+
40+
add_compile_options("-Wall" "-Werror" "-Wno-sign-compare")
41+
42+
# GNU emit wanring for ignored attributes
43+
# Unfortunately, we use [[maybe_unused]] which can be ignored by GNU.
44+
# So we make it a warning, not an error in GNU.
45+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
46+
add_compile_options("-Wno-error=attributes")
47+
endif()
48+
49+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
50+
# strip symbols
51+
add_link_options("-s")
52+
# hide dynamic symbols
53+
set(CMAKE_C_VISIBILITY_PRESET hidden)
54+
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
55+
56+
# --gc-sections is added by torch.
57+
add_compile_definitions("-O3" "-ffunction-sections" "-fdata-sections")
58+
endif()
59+
60+
61+
include_directories(
62+
BEFORE
63+
${_common_include_directories}
64+
)
65+
include_directories(
66+
BEFORE
67+
${QNN_SDK_ROOT}/include/QNN
68+
)
69+
70+
#
71+
# declare targets
72+
#
73+
add_library(executorch_backend INTERFACE)
74+
add_library(qnn_executorch_backend STATIC)
75+
add_library(qnn_executorch_header INTERFACE)
76+
add_library(qnn_manager STATIC)
77+
add_library(qnn_function_interface INTERFACE)
78+
add_library(qnn_implementation STATIC)
79+
add_library(qnn_sys_function_interface INTERFACE)
80+
add_library(qnn_sys_implementation STATIC)
81+
add_library(qnn_logger STATIC)
82+
add_library(qnn_device STATIC)
83+
add_library(qnn_context STATIC)
84+
add_library(qnn_backend_cache STATIC)
85+
add_library(qnn_graph STATIC)
86+
add_library(qnn_backend STATIC)
87+
add_library(qnn_factory STATIC)
88+
add_library(qnn_header INTERFACE)
89+
add_library(qnn_logging STATIC)
90+
add_library(wrappers STATIC)
91+
add_library(utils STATIC)
92+
93+
#
94+
# declare dependency
95+
#
96+
target_link_libraries(wrappers
97+
PRIVATE
98+
qnn_header
99+
)
100+
target_link_libraries(qnn_function_interface
101+
INTERFACE
102+
qnn_header
103+
)
104+
target_link_libraries(qnn_implementation
105+
PRIVATE
106+
qnn_function_interface
107+
qnn_header
108+
qnn_logging
109+
${CMAKE_DL_LIBS}
110+
)
111+
target_link_libraries(qnn_sys_function_interface
112+
INTERFACE
113+
qnn_header
114+
)
115+
target_link_libraries(qnn_sys_implementation
116+
PRIVATE
117+
qnn_sys_function_interface
118+
qnn_header
119+
qnn_logging
120+
${CMAKE_DL_LIBS}
121+
)
122+
target_link_libraries(qnn_logger
123+
PRIVATE
124+
qnn_implementation
125+
${android_log}
126+
)
127+
target_link_libraries(qnn_backend
128+
PRIVATE
129+
qnn_implementation
130+
qnn_logger
131+
)
132+
target_link_libraries(qnn_device
133+
PRIVATE
134+
qnn_implementation
135+
qnn_logger
136+
utils
137+
)
138+
target_link_libraries(qnn_backend_cache
139+
PRIVATE
140+
qnn_sys_implementation
141+
)
142+
target_link_libraries(qnn_context
143+
PRIVATE
144+
qnn_implementation
145+
qnn_logger
146+
qnn_backend
147+
qnn_device
148+
qnn_backend_cache
149+
)
150+
target_link_libraries(qnn_graph
151+
PRIVATE
152+
qnn_implementation
153+
qnn_context
154+
)
155+
target_link_libraries(qnn_factory
156+
PUBLIC
157+
qnn_header
158+
PRIVATE
159+
qnn_backend
160+
qnn_device
161+
qnn_context
162+
qnn_graph
163+
)
164+
target_link_libraries(qnn_manager
165+
PRIVATE
166+
qnn_factory
167+
wrappers
168+
)
169+
target_link_libraries(qnn_executorch_backend
170+
PRIVATE
171+
qnn_executorch_header
172+
qnn_manager
173+
executorch
174+
)
175+
176+
#
177+
# add linker option
178+
#
179+
target_link_options_shared_lib(qnn_executorch_backend)
180+
181+
#
182+
# add sources
183+
#
184+
add_subdirectory(
185+
${QNN_EXECUTORCH_ROOT_DIR}/runtime
186+
${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch
187+
)
188+
add_subdirectory(
189+
${QNN_EXECUTORCH_ROOT_DIR}/runtime/backends
190+
${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/backends
191+
)
192+
add_subdirectory(
193+
${QNN_EXECUTORCH_ROOT_DIR}/aot/wrappers
194+
${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/wrappers
195+
)
196+
197+
# QNN pybind
198+
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
199+
add_subdirectory(${EXECUTORCH_SOURCE_DIR}/third-party/pybind11
200+
${CMAKE_CURRENT_BINARY_DIR}/pybind11)
201+
add_library(PyQnnManagerAdaptor MODULE)
202+
add_library(PyQnnWrapperAdaptor MODULE)
203+
204+
target_link_libraries(PyQnnManagerAdaptor
205+
PRIVATE
206+
pybind11::module
207+
pybind11::lto
208+
qnn_manager
209+
qnn_executorch_header
210+
executorch
211+
)
212+
target_link_libraries(PyQnnWrapperAdaptor
213+
PRIVATE
214+
pybind11::module
215+
pybind11::lto
216+
wrappers
217+
qnn_logging
218+
qnn_executorch_header
219+
)
220+
221+
pybind11_extension(PyQnnManagerAdaptor)
222+
pybind11_extension(PyQnnWrapperAdaptor)
223+
if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
224+
# Strip unnecessary sections of the binary
225+
pybind11_strip(PyQnnManagerAdaptor)
226+
pybind11_strip(PyQnnWrapperAdaptor)
227+
endif()
228+
229+
add_subdirectory(
230+
${QNN_EXECUTORCH_ROOT_DIR}/aot/python
231+
${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/python
232+
)
233+
endif()

backends/qualcomm/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Qualcomm AI Engine Direct Backend
2+
3+
Disclaimer: At present, we do not offer any backward compatibility guarantees
4+
for any APIs. We are currently in a pre-alpha development phase, and as such,
5+
we reserve the right to modify interfaces and implementations.
6+
7+
This backend is implemented on the top of
8+
[Qualcomm AI Engine Direct SDK](https://developer.qualcomm.com/software/qualcomm-ai-engine-direct-sdk).
9+
Please follow [setup](setup.md) to setup environment, build, and run executorch models by this backend.
10+
11+
## Delegate Options
12+
13+
Please check `generate_qnn_executorch_compiler_spec()` in
14+
[utils.py](./utils/utils.py) for supported SoC and inference type.
15+
16+
### Supported Chipset
17+
- Snapdragon 8 Gen 1
18+
- Snapdragon 8 Gen 1+
19+
- Snapdragon 8 Gen 2
20+
21+
### Supported Inference Type
22+
- Quantized
23+
- FP16
24+
25+
## Directory Structure
26+
27+
```
28+
backends/qualcomm
29+
├── aot # Codes for generating QNN context binary (AoT Part).
30+
| ├── wrappers # Wrapper of QNN data structures for ease of use.
31+
| └── python # Python interface for using QNN libraries.
32+
├── builders # Codes for lowering each operators (AoT Part).
33+
├── partition # QNN Partitioner (AoT Part).
34+
├── passes # Various passes helping lower models to QNN backend (AoT Part).
35+
├── python # Places to put pybind artifacts for accessing QNN APIs, structures, etc (AoT Part).
36+
├── runtime # Here is QNN runtime responsbile for compiling a model on x64.
37+
| | # Meanwhile, this is also the runtime responsbile for executing compiled
38+
| | # models on a device.
39+
| └── backends # Backends supported by QNN.
40+
| └── htpbackend
41+
| ├── aarch64 # Configuration required to run on device. (Device Part).
42+
| └── x86_64 # Configuration required to compile graph on host. (AoT Part).
43+
├── scripts # Misc supporting scripts, not related to core functionality.
44+
├── tests # Unit tests and model tests go here.
45+
└── utils # Miscellaneous utilities.
46+
47+
examples/backend
48+
└── qualcomm # Examples to run QNN backends.
49+
```
50+
51+
## Examples
52+
53+
Please see this [README.md](../../examples/backend/qualcomm/README.md).
54+
55+
Further, an example build script is provided as [build.sh](scripts/build.sh).
56+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) Qualcomm Innovation Center, Inc.
2+
# All rights reserved
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
8+
# PyQnnManagerAdaptor
9+
target_sources(PyQnnManagerAdaptor
10+
PUBLIC
11+
${CMAKE_CURRENT_LIST_DIR}/PyQnnManagerAdaptor.cpp
12+
)
13+
14+
# PyQnnWrapperAdaptor
15+
target_sources(PyQnnWrapperAdaptor
16+
PUBLIC
17+
${CMAKE_CURRENT_LIST_DIR}/PyQnnWrapperAdaptor.cpp
18+
${CMAKE_CURRENT_LIST_DIR}/PyQnnWrapperAdaptor.h
19+
)
20+

0 commit comments

Comments
 (0)