Skip to content

Commit 86008ef

Browse files
author
zhouwg
committed
ggml-hexagon: remove reduament code and make debug log more clear
1 parent f51b53d commit 86008ef

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

ggml/src/ggml-hexagon/ggml-hexagon.cpp

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ struct hexagon_appcfg_t {
317317
int enable_rpc_ion_mempool; // enable/disable rpc ion memory pool
318318
int enable_rpc_dma_mempool; // enable/disable rpc dma memory pool
319319
const char * cfgfilename;
320-
const char * runtimelib_path;
320+
const char * runtime_libpath;
321321
char ggml_hexagon_version[GGMLHEXAGON_TMPBUF_LEN];
322322
};
323323

@@ -338,7 +338,7 @@ static struct hexagon_appcfg_t g_hexagon_appcfg = {
338338
.cfgfilename = "ggml-hexagon.cfg",
339339
#if defined(__ANDROID__)
340340
//Android command line program
341-
.runtimelib_path = "/data/local/tmp/",
341+
.runtime_libpath = "/data/local/tmp/",
342342
#elif defined(__linux__)
343343
.qnn_runtimelib_path = "/tmp/",
344344
#elif defined(_WIN32)
@@ -989,7 +989,7 @@ class hexagon_appcfg {
989989
if (pos != len) str.erase(pos);
990990
}
991991

992-
void trim(std::string& str) {
992+
void trim(std::string & str) {
993993
ltrim(str);
994994
rtrim(str);
995995
}
@@ -1392,16 +1392,16 @@ static void ggmlhexagon_set_runtime_path(size_t device, const std::string & path
13921392
if ((HEXAGON_BACKEND_QNNNPU == device) || (HWACCEL_CDSP == g_hexagon_appcfg.hwaccel_approach)) {
13931393
std::string lib_runtime_path = path + ":/vendor/dsp/cdsp:/vendor/lib64:/vendor/dsp/dsp:/vendor/dsp/images";
13941394
if (0 == setenv("LD_LIBRARY_PATH", lib_runtime_path.c_str(), 1)) {
1395-
GGMLHEXAGON_LOG_DEBUG("HEXAGON_BACKEND_QNNNPU / HEXAGON_BACKEND_CDSP setenv %s successfully", lib_runtime_path.c_str());
1395+
GGMLHEXAGON_LOG_DEBUG("setenv LD_LIBRARY_PATH %s successfully", lib_runtime_path.c_str());
13961396
} else {
1397-
GGMLHEXAGON_LOG_ERROR("HEXAGON_BACKEND_QNNNPU / HEXAGON_BACKEND_CDSP setenv %s failure", lib_runtime_path.c_str());
1397+
GGMLHEXAGON_LOG_ERROR("setenv LD_LIBRARY_PATH %s failure", lib_runtime_path.c_str());
13981398
}
13991399

14001400
std::string adsp_runtime_path = path + ";/vendor/dsp/cdsp;/vendor/lib/rfsa/adsp;/system/lib/rfsa/adsp;/vendor/dsp/dsp;/vendor/dsp/images;/dsp";
14011401
if (0 == setenv("ADSP_LIBRARY_PATH", adsp_runtime_path.c_str(), 1)) {
1402-
GGMLHEXAGON_LOG_DEBUG("HEXAGON_BACKEND_QNNNPU / HEXAGON_BACKEND_CDSP setenv %s successfully", adsp_runtime_path.c_str());
1402+
GGMLHEXAGON_LOG_DEBUG("setenv ADSP_LIBRARY_PATH %s successfully", adsp_runtime_path.c_str());
14031403
} else {
1404-
GGMLHEXAGON_LOG_ERROR("HEXAGON_BACKEND_QNNNPU / HEXAGON_BACKEND_CDSP setenv %s failure", adsp_runtime_path.c_str());
1404+
GGMLHEXAGON_LOG_ERROR("setenv ADSP_LIBRARY_PATH %s failure", adsp_runtime_path.c_str());
14051405
}
14061406
} else {
14071407
if (0 == setenv("LD_LIBRARY_PATH",
@@ -1429,7 +1429,7 @@ static void ggmlhexagon_load_cfg() {
14291429
memset(time_string, 0, GGMLHEXAGON_TMPBUF_LEN);
14301430
ggmlhexagon_get_timestring(time_string);
14311431
GGMLHEXAGON_LOG_DEBUG("program running start time:%s", time_string);
1432-
std::string cfg_filename = std::string(g_hexagon_appcfg.runtimelib_path) + std::string(g_hexagon_appcfg.cfgfilename);
1432+
std::string cfg_filename = std::string(g_hexagon_appcfg.runtime_libpath) + std::string(g_hexagon_appcfg.cfgfilename);
14331433
GGMLHEXAGON_LOG_INFO("load hexagon appcfg from %s", cfg_filename.c_str());
14341434
hexagon_appcfg qnncfg_instance;
14351435
qnncfg_instance.load(cfg_filename);
@@ -1454,29 +1454,36 @@ static void ggmlhexagon_load_cfg() {
14541454
qnncfg_instance.get_stringvalue("qnn", "precision_mode", precision_mode, "fp32");
14551455
qnncfg_instance.get_intvalue("cdsp", "enable_rpc_ion_mempool", g_hexagon_appcfg.enable_rpc_ion_mempool, 1);
14561456
qnncfg_instance.get_intvalue("cdsp", "enable_rpc_dma_mempool", g_hexagon_appcfg.enable_rpc_dma_mempool, 0);
1457-
GGMLHEXAGON_LOG_INFO("ggml_hexagon_version=%s", ggml_hexagon_version.c_str());
1458-
memcpy(g_hexagon_appcfg.ggml_hexagon_version, ggml_hexagon_version.c_str(), strlen(ggml_hexagon_version.c_str()));
1457+
GGMLHEXAGON_LOG_INFO("internal ggml_hexagon_version=%s", g_hexagon_appcfg.ggml_hexagon_version);
1458+
GGMLHEXAGON_LOG_INFO("external ggml_hexagon_version=%s", ggml_hexagon_version.c_str());
14591459
GGMLHEXAGON_LOG_INFO("hwaccel_approach=%d(%s)", g_hexagon_appcfg.hwaccel_approach,
14601460
ggmlhexagon_get_hwaccel_approach_name(g_hexagon_appcfg.hwaccel_approach));
14611461
GGMLHEXAGON_LOG_INFO("hexagon_backend=%d(%s)", g_hexagon_appcfg.hexagon_backend,
14621462
ggml_backend_hexagon_get_devname(g_hexagon_appcfg.hexagon_backend));
1463-
GGMLHEXAGON_LOG_INFO("qnn runtime lib path=%s", g_hexagon_appcfg.runtimelib_path);
1463+
GGMLHEXAGON_LOG_INFO("runtime libpath=%s", g_hexagon_appcfg.runtime_libpath);
1464+
14641465
if (precision_mode.find("fp16") != std::string::npos) {
14651466
g_hexagon_appcfg.precision_mode = 1;
14661467
} else {
14671468
g_hexagon_appcfg.precision_mode = 0;
14681469
}
14691470

1470-
if (HWACCEL_CDSP == g_hexagon_appcfg.hwaccel_approach) {
1471-
ggmlhexagon_set_runtime_path(HEXAGON_BACKEND_CDSP, g_hexagon_appcfg.runtimelib_path);
1472-
}
1471+
ggmlhexagon_set_runtime_path(HEXAGON_BACKEND_CDSP, g_hexagon_appcfg.runtime_libpath);
14731472

14741473
initialized = true;
14751474
}
14761475

14771476
static bool ggmlhexagon_check_valid_appcfg() {
14781477
bool is_valid_appcfg = true;
14791478

1479+
GGMLHEXAGON_LOG_DEBUG("user's specified hwaccel approach=%d(%s)", g_hexagon_appcfg.hwaccel_approach,
1480+
ggmlhexagon_get_hwaccel_approach_name(g_hexagon_appcfg.hwaccel_approach));
1481+
GGMLHEXAGON_LOG_DEBUG("user's specified hexagon_backend=%d", g_hexagon_appcfg.hexagon_backend);
1482+
if (g_hexagon_appcfg.hexagon_backend >= GGML_HEXAGON_MAX_DEVICES) {
1483+
GGMLHEXAGON_LOG_INFO("using default ggml backend");
1484+
is_valid_appcfg = false;
1485+
}
1486+
14801487
if (HWACCEL_QNN_SINGLEGRAPH == g_hexagon_appcfg.hwaccel_approach) {
14811488
GGMLHEXAGON_LOG_INFO("HWACCEL_QNN_SINGLEGRAPH not supported");
14821489
is_valid_appcfg = false;
@@ -2599,7 +2606,7 @@ int qnn_instance::load_system() {
25992606
if (nullptr == _system_lib_handle) {
26002607
GGMLHEXAGON_LOG_WARN("can not open QNN library %s, error: %s\n", system_lib_path.c_str(), dlerror());
26012608
//re-try with default path of QNN binary runtime lib
2602-
_lib_path = std::string(g_hexagon_appcfg.runtimelib_path);
2609+
_lib_path = std::string(g_hexagon_appcfg.runtime_libpath);
26032610
#if !defined(__ANDROID__) && !defined(__linux__)
26042611
system_lib_path = _lib_path + "QnnSystem.dll";
26052612
#else
@@ -2881,7 +2888,7 @@ int qnn_instance::qnn_init(const QnnSaver_Config_t ** saver_config) {
28812888
}
28822889

28832890
#if defined(__ANDROID__) || defined(__linux__)
2884-
std::filesystem::path full_path(std::string(g_hexagon_appcfg.runtimelib_path) + "libcdsprpc.so");
2891+
std::filesystem::path full_path(std::string(g_hexagon_appcfg.runtime_libpath) + "libcdsprpc.so");
28852892
full_path /= std::filesystem::path("libcdsprpc.so").filename();
28862893
_rpc_lib_handle = dlopen(full_path.string().c_str(), RTLD_NOW | RTLD_LOCAL);
28872894
if (nullptr == _rpc_lib_handle) {
@@ -5657,11 +5664,8 @@ static ggml_backend_t ggml_backend_hexagon_device_init_backend(ggml_backend_dev_
56575664
GGMLHEXAGON_LOG_DEBUG("enter %s\n", __func__);
56585665
size_t dev_index = 0;
56595666

5660-
//case-1: test-backend-ops or other similar scenairo: calling ggml_backend_dev_init(dev, reinterpret_cast<const char *>(i)) directly in user's code
5667+
//case-1: test-backend-ops or other similar scenario: calling ggml_backend_dev_init(dev, reinterpret_cast<const char *>(i)) directly in user's code
56615668
ggmlhexagon_load_cfg();
5662-
GGMLHEXAGON_LOG_DEBUG("user's specified hexagon_backend in cfgfile = %d", g_hexagon_appcfg.hexagon_backend);
5663-
GGMLHEXAGON_LOG_DEBUG("user's sepcified qnn runtime lib path in cfgfile = %s", g_hexagon_appcfg.runtimelib_path);
5664-
56655669
if (!ggmlhexagon_check_valid_appcfg()) {
56665670
return nullptr;
56675671
}
@@ -5681,7 +5685,7 @@ static ggml_backend_t ggml_backend_hexagon_device_init_backend(ggml_backend_dev_
56815685
GGMLHEXAGON_LOG_INFO("program specified dev_index %d\n", dev_index);
56825686
}
56835687
GGMLHEXAGON_LOG_DEBUG("hexagon_backend=%d", dev_index);
5684-
ggml_backend_t hexagon_backend = ggml_backend_hexagon_init(dev_index, g_hexagon_appcfg.runtimelib_path);
5688+
ggml_backend_t hexagon_backend = ggml_backend_hexagon_init(dev_index, g_hexagon_appcfg.runtime_libpath);
56855689
GGMLHEXAGON_LOG_DEBUG("leave %s\n", __func__);
56865690

56875691
return hexagon_backend;
@@ -5872,8 +5876,11 @@ static const char * ggml_backend_hexagon_reg_get_name(ggml_backend_reg_t reg) {
58725876

58735877
static size_t ggml_backend_hexagon_reg_get_device_count(ggml_backend_reg_t reg) {
58745878
GGML_UNUSED(reg);
5875-
if (g_hexagon_appcfg.hwaccel_approach == HWACCEL_CDSP) {
5879+
if (HWACCEL_CDSP == g_hexagon_appcfg.hwaccel_approach) {
58765880
GGML_ASSERT(g_hexagon_appcfg.hexagon_backend == HEXAGON_BACKEND_CDSP);
5881+
//here is the trick:
5882+
//there only 1 backend_device when g_hexagon_appcfg.hwaccel_approach == HWACCEL_CDSP
5883+
//so return 1
58775884
return 1;
58785885
} else {
58795886
return GGML_HEXAGON_MAX_DEVICES;
@@ -5925,16 +5932,6 @@ ggml_backend_reg_t ggml_backend_hexagon_reg() {
59255932

59265933
//case-2: normal scenario, such as llama-cli or UI applicaton
59275934
ggmlhexagon_load_cfg();
5928-
GGMLHEXAGON_LOG_DEBUG("hwaccel approach=%d(%s)", g_hexagon_appcfg.hwaccel_approach,
5929-
ggmlhexagon_get_hwaccel_approach_name(g_hexagon_appcfg.hwaccel_approach));
5930-
GGMLHEXAGON_LOG_DEBUG("user's specified hexagon_backend=%d", g_hexagon_appcfg.hexagon_backend);
5931-
GGMLHEXAGON_LOG_DEBUG("user's specified runtime lib path=%s", g_hexagon_appcfg.runtimelib_path);
5932-
if (g_hexagon_appcfg.hexagon_backend >= GGML_HEXAGON_MAX_DEVICES) {
5933-
GGMLHEXAGON_LOG_INFO("using default ggml backend");
5934-
GGMLHEXAGON_LOG_DEBUG("leave ggml_backend_hexagon_reg");
5935-
return nullptr;
5936-
}
5937-
59385935
if (!ggmlhexagon_check_valid_appcfg()) {
59395936
return nullptr;
59405937
}
@@ -6050,28 +6047,32 @@ static qnn_instance * ggmlqnn_init_qnn_instance(size_t device, const char * qnn_
60506047
/**
60516048
*
60526049
* @param device 0: HEXAGON_BACKEND_QNNCPU 1: HEXAGON_BACKEND_QNNGPU 2: HEXAGON_BACKEND_QNNNPU/HEXAGON_BACKEND_CDSP
6053-
* @param qnn_lib_path QNN binrary runtime library path, such as "/data/local/tmp/" on Android or specified in JNI layer
6050+
* @param runtime_libpath binary runtime library path, such as "/data/local/tmp/" on Android or specified in user's code
60546051
* @return
60556052
*/
6056-
ggml_backend_t ggml_backend_hexagon_init(size_t device, const char * qnn_lib_path) {
6053+
ggml_backend_t ggml_backend_hexagon_init(size_t device, const char * runtime_libpath) {
60576054
GGMLHEXAGON_LOG_DEBUG("enter %s", __func__);
6058-
//case-3: calling ggml_backend_hexagon_init() directly in user's code
6059-
ggmlhexagon_load_cfg();
6060-
6061-
if (nullptr == qnn_lib_path)
6055+
if (nullptr == runtime_libpath)
60626056
return nullptr;
60636057

6058+
//case-3: calling ggml_backend_hexagon_init() directly in user's code
6059+
ggmlhexagon_load_cfg();
60646060
if (!ggmlhexagon_check_valid_appcfg()) {
60656061
return nullptr;
60666062
}
60676063

60686064
GGMLHEXAGON_LOG_DEBUG("device %d", device);
6069-
GGMLHEXAGON_LOG_DEBUG("qnn_lib_path %s", qnn_lib_path);
6065+
GGMLHEXAGON_LOG_DEBUG("runtime libpath %s", runtime_libpath);
60706066
if (device >= GGML_HEXAGON_MAX_DEVICES) {
60716067
GGMLHEXAGON_LOG_ERROR("invalid device %d", device);
60726068
return nullptr;
60736069
}
60746070

6071+
if (0 != memcmp(runtime_libpath, g_hexagon_appcfg.runtime_libpath, strlen(g_hexagon_appcfg.runtime_libpath))) {
6072+
//re-setting runtime libpath
6073+
ggmlhexagon_set_runtime_path(device, runtime_libpath);
6074+
}
6075+
60756076
if (nullptr != g_hexagon_mgr[device].backend) {
60766077
GGMLHEXAGON_LOG_DEBUG("backend %d(%s) already loaded", device,
60776078
ggml_backend_hexagon_get_devname(device));
@@ -6081,7 +6082,7 @@ ggml_backend_t ggml_backend_hexagon_init(size_t device, const char * qnn_lib_pat
60816082

60826083
//don't initialize QNN when hwaccel approach is offload ggml op to Hexagon cDSP directly
60836084
if (HWACCEL_CDSP != g_hexagon_appcfg.hwaccel_approach) {
6084-
qnn_instance * instance = ggmlqnn_init_qnn_instance(device, qnn_lib_path);
6085+
qnn_instance * instance = ggmlqnn_init_qnn_instance(device, runtime_libpath);
60856086
if (nullptr == instance)
60866087
return nullptr;
60876088
}

0 commit comments

Comments
 (0)