Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] Build qnn subplugin #559

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions java/android/nnstreamer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ android {
println 'Set jniLibs.srcDirs includes libraries for SNPE'
}

if (project.hasProperty('QNN_EXT_LIBRARY_PATH')) {
jniLibs.srcDirs += project.properties['QNN_EXT_LIBRARY_PATH']
println 'Set jniLibs.srcDirs includes libraries for QNN'
}

if (project.hasProperty('NNFW_EXT_LIBRARY_PATH')) {
jniLibs.srcDirs += project.properties['NNFW_EXT_LIBRARY_PATH']
println 'Set jniLibs.srcDirs includes libraries for NNFW'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public enum NNFWType {
* is a deep learning framework designed for both efficiency and flexibility.
*/
MXNET,
/**
* <a href="https://www.qualcomm.com/developer/software/qualcomm-ai-engine-direct-sdk">QNN</a>
* provides lower-level, unified APIs for Qualcomm AI development.
*/
QNN,
/**
* Unknown framework (usually error)
*/
Expand Down
63 changes: 63 additions & 0 deletions java/android/nnstreamer/src/main/jni/Android-qnn.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#------------------------------------------------------
# QNN
#------------------------------------------------------
LOCAL_PATH := $(call my-dir)

ifndef NNSTREAMER_ROOT
$(error NNSTREAMER_ROOT is not defined!)
endif

include $(NNSTREAMER_ROOT)/jni/nnstreamer.mk

QNN_DIR := $(LOCAL_PATH)/qnn

ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
QNN_LIB_PATH := $(QNN_DIR)/lib
else
$(error Target arch ABI not supported: $(TARGET_ARCH_ABI))
endif

QNN_INCLUDES := $(QNN_DIR)/include/QNN

#------------------------------------------------------
# qnn-sdk (prebuilt shared library)
#------------------------------------------------------
QNN_PREBUILT_LIBS :=

include $(CLEAR_VARS)
LOCAL_MODULE := libQnnCpu
LOCAL_SRC_FILES := $(QNN_LIB_PATH)/libQnnCpu.so
include $(PREBUILT_SHARED_LIBRARY)
QNN_PREBUILT_LIBS += libQnnCpu

include $(CLEAR_VARS)
LOCAL_MODULE := libQnnGpu
LOCAL_SRC_FILES := $(QNN_LIB_PATH)/libQnnGpu.so
include $(PREBUILT_SHARED_LIBRARY)
QNN_PREBUILT_LIBS += libQnnGpu

include $(CLEAR_VARS)
LOCAL_MODULE := libQnnDsp
LOCAL_SRC_FILES := $(QNN_LIB_PATH)/libQnnDsp.so
include $(PREBUILT_SHARED_LIBRARY)
QNN_PREBUILT_LIBS += libQnnDsp

include $(CLEAR_VARS)
LOCAL_MODULE := libQnnHtp
LOCAL_SRC_FILES := $(QNN_LIB_PATH)/libQnnHtp.so
include $(PREBUILT_SHARED_LIBRARY)
QNN_PREBUILT_LIBS += libQnnHtp

#------------------------------------------------------
# tensor-filter sub-plugin for qnn
#------------------------------------------------------
include $(CLEAR_VARS)

LOCAL_MODULE := qnn-subplugin
LOCAL_SRC_FILES := $(NNSTREAMER_FILTER_QNN_SRCS)
LOCAL_CXXFLAGS := -O3 -fPIC -frtti -fexceptions $(NNS_API_FLAGS)
LOCAL_C_INCLUDES := $(QNN_INCLUDES) $(NNSTREAMER_INCLUDES) $(GST_HEADERS_COMMON)
LOCAL_STATIC_LIBRARIES := nnstreamer
LOCAL_SHARED_LIBRARIES := $(QNN_PREBUILT_LIBS)

include $(BUILD_STATIC_LIBRARY)
10 changes: 10 additions & 0 deletions java/android/nnstreamer/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ _ENABLE_SNPE := false
endif #endif ($(filter $(TARGET_ARCH_ABI), arm64-v8a,)
endif #endif ($(ENABLE_SNPE),true)

# QNN
ENABLE_QNN := false

# PyTorch
ENABLE_PYTORCH := false

Expand Down Expand Up @@ -137,6 +140,13 @@ NNS_SUBPLUGINS += snpe-subplugin
include $(LOCAL_PATH)/Android-snpe.mk
endif

ifeq ($(ENABLE_QNN),true)
NNS_API_FLAGS += -DENABLE_QNN=1
NNS_SUBPLUGINS += qnn-subplugin

include $(LOCAL_PATH)/Android-qnn.mk
endif

ifeq ($(ENABLE_PYTORCH),true)
NNS_API_FLAGS += -DENABLE_PYTORCH=1
NNS_SUBPLUGINS += pytorch-subplugin
Expand Down
11 changes: 10 additions & 1 deletion java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ extern void init_filter_nnfw (void);
#if defined (ENABLE_SNPE)
extern void init_filter_snpe (void);
#endif
#if defined (ENABLE_QNN)
extern void init_filter_qnn (void);
#endif
#if defined (ENABLE_PYTORCH)
extern void init_filter_torch (void);
#endif
Expand Down Expand Up @@ -844,6 +847,9 @@ nns_get_nnfw_type (jint fw_type, ml_nnfw_type_e * nnfw)
case 5: /* NNFWType.MXNET */
*nnfw = ML_NNFW_TYPE_MXNET;
break;
case 6: /* NNFWType.QNN */
*nnfw = ML_NNFW_TYPE_QNN;
break;
default: /* Unknown */
_ml_logw ("Unknown NNFW type (%d).", fw_type);
return FALSE;
Expand Down Expand Up @@ -918,7 +924,7 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)
#endif /* ENABLE_FLATBUF */
#endif

#if defined (ENABLE_SNPE) || defined (ENABLE_TFLITE_QNN_DELEGATE)
#if defined (ENABLE_QNN) || defined (ENABLE_SNPE) || defined (ENABLE_TFLITE_QNN_DELEGATE)
/* some filters require additional tasks */
if (!_qc_android_set_env (env, context)) {
_ml_logw ("Failed to set environment variables for QC Android. Some features may not work properly.");
Expand All @@ -942,6 +948,9 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)
#if defined (ENABLE_SNPE)
init_filter_snpe ();
#endif
#if defined (ENABLE_QNN)
init_filter_qnn ();
#endif
#if defined (ENABLE_PYTORCH)
init_filter_torch ();
#endif
Expand Down
35 changes: 35 additions & 0 deletions java/build-nnstreamer-android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
##@@ --enable_snpe=(yes|no)
##@@ 'yes' : build with sub-plugin for SNPE
##@@ 'no' : [default]
##@@ --enable_qnn=(yes|no)
##@@ 'yes' : build with sub-plugin for QNN
##@@ 'no' : [default]
##@@ --enable_pytorch=(yes(:(1.10.1))?|no)
##@@ 'yes' : build with sub-plugin for PyTorch. You can optionally specify the version of
##@@ PyTorch to use by appending ':version' [1.10.1 is the default].
Expand Down Expand Up @@ -117,6 +120,9 @@ enable_nnfw="yes"
# Enable SNPE
enable_snpe="no"

# Enable QNN
enable_qnn="no"

# Enable PyTorch
enable_pytorch="no"

Expand Down Expand Up @@ -212,6 +218,9 @@ for arg in "$@"; do
--enable_snpe=*)
enable_snpe=${arg#*=}
;;
--enable_qnn=*)
enable_qnn=${arg#*=}
;;
--enable_pytorch=*)
IFS=':' read -ra enable_pytorch_args <<< "${arg#*=}"
is_valid_pytorch_version=0
Expand Down Expand Up @@ -284,6 +293,7 @@ elif [[ $build_type == "internal" ]]; then
enable_nnfw="yes"
enable_nnfw_ext="yes"
enable_snpe="no"
enable_qnn="no"
enable_pytorch="no"
enable_tflite="no"
enable_mxnet="no"
Expand Down Expand Up @@ -335,6 +345,13 @@ if [[ $enable_snpe == "yes" ]]; then
echo "Build with SNPE: $SNPE_DIRECTORY"
fi

if [[ $enable_qnn == "yes" ]]; then
[ -z "$QNN_DIRECTORY" ] && echo "Need to set QNN_DIRECTORY, to build sub-plugin for QNN." && exit 1
[ $target_abi != "arm64-v8a" ] && echo "Set target ABI arm64-v8a to build sub-plugin for QNN." && exit 1

echo "Build with QNN: $QNN_DIRECTORY"
fi

if [[ $enable_pytorch == "yes" ]]; then
echo "Build with PyTorch $pytorch_ver"
fi
Expand Down Expand Up @@ -579,6 +596,24 @@ if [[ $enable_snpe == "yes" ]]; then
mv nnstreamer/src/main/jni/snpe/lib/ext/arm64-v8a/libSNPE.so nnstreamer/src/main/jni/snpe/lib
fi

# Update QNN option
if [[ $enable_qnn == "yes" ]]; then
sed -i "s|ENABLE_QNN := false|ENABLE_QNN := true|" nnstreamer/src/main/jni/Android.mk
sed -i "$ a QNN_EXT_LIBRARY_PATH=src/main/jni/qnn/lib/ext" gradle.properties

mkdir -p nnstreamer/src/main/jni/qnn/lib/ext/arm64-v8a
cp -r $QNN_DIRECTORY/include nnstreamer/src/main/jni/qnn

# Copy libs to build QNN
cp $QNN_DIRECTORY/lib/aarch64-android/libQnnCpu.so nnstreamer/src/main/jni/qnn/lib/
cp $QNN_DIRECTORY/lib/aarch64-android/libQnnGpu.so nnstreamer/src/main/jni/qnn/lib/
cp $QNN_DIRECTORY/lib/aarch64-android/libQnnDsp.so nnstreamer/src/main/jni/qnn/lib/
cp $QNN_DIRECTORY/lib/aarch64-android/libQnnHtp.so nnstreamer/src/main/jni/qnn/lib/

# Copy external so files for QNN
cp $QNN_DIRECTORY/lib/hexagon-v*/unsigned/libQnn*Skel.so nnstreamer/src/main/jni/qnn/lib/ext/arm64-v8a
fi

# Update PyTorch option
if [[ $enable_pytorch == "yes" ]]; then
sed -i "s|ENABLE_PYTORCH := false|ENABLE_PYTORCH := true|" nnstreamer/src/main/jni/Android.mk
Expand Down
Loading