Skip to content

Commit

Permalink
PoC-S26: offload simple f32 2x2 matrix addition operation to QNN CPU …
Browse files Browse the repository at this point in the history
…backend --- troubleshooting-3rd
  • Loading branch information
zhouwg committed Apr 4, 2024
1 parent f807910 commit cf10878
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 8 deletions.
Binary file added cdeosplayer/kantv/src/main/assets/libQnnSaver.so
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void initView() {
CDEAssetLoader.copyAssetFile(mContext, "libQnnGpu.so", CDEUtils.getDataPath(mContext) + "libQnnGpu.so");
CDEAssetLoader.copyAssetFile(mContext, "libQnnDsp.so", CDEUtils.getDataPath(mContext) + "libQnnDsp.so");
CDEAssetLoader.copyAssetFile(mContext, "libQnnSystem.so", CDEUtils.getDataPath(mContext) + "libQnnSystem.so");
CDEAssetLoader.copyAssetFile(mContext, "libQnnSaver.so", CDEUtils.getDataPath(mContext) + "libQnnSaver.so");
//qualcomm's prebuilt binary file
CDEAssetLoader.copyAssetFile(mContext, "raw_list.txt", CDEUtils.getDataPath() + "raw_list.txt");
CDEAssetLoader.copyAssetDir(mContext, "data", CDEUtils.getDataPath() + "data");
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions cdeosplayer/kantv/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,6 @@
<item>cpu</item>
<item>gpu</item>
<item>dsp</item>
<item>saver</item>
</string-array>
</resources>
11 changes: 10 additions & 1 deletion external/ggml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ set_target_properties(kantvffmpeg
${PREBUILT_LIB_PATH}/libkantv-ffmpeg.so)


add_library(QNNSaver
SHARED
IMPORTED)

set_target_properties(QNNSaver
PROPERTIES
IMPORTED_LOCATION
${PREBUILT_LIB_PATH}/libQnnSaver.so)

function(build_library target_name)
add_library(
${target_name}
Expand All @@ -174,7 +183,7 @@ function(build_library target_name)
if (GGML_JNI_QNN)
file(GLOB allQNNLibs "${PREBUILT_LIB_PATH}/libQnn*.so")
#target_link_libraries(${target_name} ${LOG_LIB} kantvcore kantvffmpeg android ${allQNNLibs})
target_link_libraries(${target_name} ${LOG_LIB} kantvcore kantvffmpeg android)
target_link_libraries(${target_name} ${LOG_LIB} kantvcore kantvffmpeg QNNSaver android )
else()
target_link_libraries(${target_name} ${LOG_LIB} kantvcore kantvffmpeg android)
endif()
Expand Down
3 changes: 3 additions & 0 deletions external/ggml/jni/ggml-jni-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,9 @@ void ggml_jni_bench(const char * sz_model_path, const char *sz_audio_path, int n
case 2:
qnn_backend_lib = "/data/data/com.cdeos.kantv/libQnnDsp.so";
break;
case 3:
qnn_backend_lib = "/data/data/com.cdeos.kantv/libQnnSaver.so";
break;
default:
LOGGW("backend type %d not supported\n", n_backend_type);
break;
Expand Down
3 changes: 2 additions & 1 deletion external/ggml/jni/ggml-jni.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ extern "C" {
#define BACKEND_CPU 0
#define BACKEND_GPU 1
#define BACKEND_DSP 2
#define BACKEND_MAX 2
#define BACKEND_SAVER 3
#define BACKEND_MAX 3

enum ggml_jni_op {
GGML_JNI_OP_NONE = 0,
Expand Down
3 changes: 3 additions & 0 deletions external/ggml/qnnsample/PAL/src/linux/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "PAL/FileOp.hpp"
#include "PAL/Path.hpp"

#include "libavutil/cde_log.h"

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#ifdef __QNXNTO__
Expand All @@ -57,6 +59,7 @@ static bool is_qnx_dir(const struct dirent *ep) {
bool pal::Directory::create(const std::string &path, pal::Directory::DirMode dirmode) {
struct stat st;
int status = 0;
LOGGI("path:%s\n", path.c_str());
if (stat(path.c_str(), &st) != 0) {
// Directory does not exist
status = mkdir(path.c_str(), static_cast<mode_t>(dirmode));
Expand Down
17 changes: 16 additions & 1 deletion external/ggml/qnnsample/QnnSampleApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cstring>
#include <fstream>
#include <iostream>
#include <Saver/QnnSaver.h>

#include "DataUtil.hpp"
#include "Logger.hpp"
Expand Down Expand Up @@ -40,7 +41,7 @@ sample_app::QnnSampleApp::QnnSampleApp(QnnFunctionPointers qnnFunctionPointers,
sample_app::ProfilingLevel profilingLevel,
bool dumpOutputs,
std::string cachedBinaryPath,
std::string saveBinaryName)
std::string saveBinaryName, uint32_t backendID)
: m_qnnFunctionPointers(qnnFunctionPointers),
m_outputPath(outputPath),
m_saveBinaryName(saveBinaryName),
Expand All @@ -59,6 +60,7 @@ sample_app::QnnSampleApp::QnnSampleApp(QnnFunctionPointers qnnFunctionPointers,
m_outputPath = s_defaultOutputPath;
}
m_graphConfigsInfoCount = 0;
m_backendID = backendID;

return;
}
Expand Down Expand Up @@ -176,6 +178,19 @@ int32_t sample_app::QnnSampleApp::reportError(const std::string &err) {

// Initialize a QnnBackend.
sample_app::StatusCode sample_app::QnnSampleApp::initializeBackend() {
QnnSaver_Config_t outputDirCfg;
outputDirCfg.option = QNN_SAVER_CONFIG_OPTION_OUTPUT_DIRECTORY;
outputDirCfg.outputDirectory = "/data/data/com.cdeos.kantv/qnn/"; //can be absolute e.g. "/data/local/tmp/saver/"

QnnSaver_Config_t backendIdCfg;
backendIdCfg.option = QNN_SAVER_CONFIG_OPTION_BACKEND_ID;
backendIdCfg.backendId = m_backendID;

const QnnSaver_Config_t *saverCfg[] = {&outputDirCfg, &backendIdCfg, NULL};

QnnSaver_initialize(saverCfg);


auto qnnStatus = m_qnnFunctionPointers.qnnInterface.backendCreate(
m_logHandle, (const QnnBackend_Config_t **) m_backendConfig, &m_backendHandle);
if (QNN_BACKEND_NO_ERROR != qnnStatus) {
Expand Down
4 changes: 3 additions & 1 deletion external/ggml/qnnsample/QnnSampleApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace qnn {
ProfilingLevel profilingLevel = ProfilingLevel::OFF,
bool dumpOutputs = false,
std::string cachedBinaryPath = "",
std::string saveBinaryName = "");
std::string saveBinaryName = "", uint32_t backendID = 0);

// @brief Print a message to STDERR then return a nonzero
// exit status.
Expand Down Expand Up @@ -122,6 +122,8 @@ namespace qnn {
Qnn_LogHandle_t m_logHandle = nullptr;
Qnn_BackendHandle_t m_backendHandle = nullptr;
Qnn_DeviceHandle_t m_deviceHandle = nullptr;

uint32_t m_backendID;
};
} // namespace sample_app
} // namespace tools
Expand Down
5 changes: 3 additions & 2 deletions external/ggml/qnnsample/QnnSampleMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,13 @@ namespace qnn {

QnnFunctionPointers qnnFunctionPointers;
// Load backend and model .so and validate all the required function symbols are resolved
uint32_t backendID = 0;
auto statusCode = dynamicloadutil::getQnnFunctionPointers(backEndPath,
modelPath,
&qnnFunctionPointers,
&sg_backendHandle,
!loadFromCachedBinary,
&sg_modelHandle);
&sg_modelHandle, &backendID);
if (dynamicloadutil::StatusCode::SUCCESS != statusCode) {
if (dynamicloadutil::StatusCode::FAIL_LOAD_BACKEND == statusCode) {
exitWithMessage(
Expand Down Expand Up @@ -398,7 +399,7 @@ namespace qnn {
parsedProfilingLevel,
dumpOutputs,
cachedBinaryPath,
saveBinaryName));
saveBinaryName, backendID));
return app;
}

Expand Down
4 changes: 3 additions & 1 deletion external/ggml/qnnsample/Utils/DynamicLoadUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dynamicloadutil::StatusCode dynamicloadutil::getQnnFunctionPointers(
sample_app::QnnFunctionPointers *qnnFunctionPointers,
void **backendHandleRtn,
bool loadModelLib,
void **modelHandleRtn) {
void **modelHandleRtn, uint32_t *backendID) {
void *libBackendHandle = pal::dynamicloading::dlOpen(
backendPath.c_str(), pal::dynamicloading::DL_NOW | pal::dynamicloading::DL_LOCAL);
if (nullptr == libBackendHandle) {
Expand Down Expand Up @@ -87,6 +87,8 @@ dynamicloadutil::StatusCode dynamicloadutil::getQnnFunctionPointers(
return StatusCode::FAIL_GET_INTERFACE_PROVIDERS;
}

*backendID = interfaceProviders[0]->backendId;

if (true == loadModelLib) {
QNN_INFO("Loading model shared library ([model].so)");
void *libModelHandle = pal::dynamicloading::dlOpen(
Expand Down
2 changes: 1 addition & 1 deletion external/ggml/qnnsample/Utils/DynamicLoadUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace qnn {
sample_app::QnnFunctionPointers *qnnFunctionPointers,
void **backendHandle,
bool loadModelLib,
void **modelHandleRtn);
void **modelHandleRtn, uint32_t *backendID);

StatusCode getQnnSystemFunctionPointers(std::string systemLibraryPath,
sample_app::QnnFunctionPointers *qnnFunctionPointers);
Expand Down

0 comments on commit cf10878

Please sign in to comment.