Skip to content

Commit 71c4845

Browse files
author
zhouwg
committed
ggml-hexagon: add set_hexagon_cfg(int new_hexagon_backend, int new_hwaccel_approach) in ggml-hexagon.h for further usage
1 parent 4484a13 commit 71c4845

File tree

3 files changed

+105
-11
lines changed

3 files changed

+105
-11
lines changed

ggml/include/ggml-hexagon.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ enum HEXAGONBackend {
2121
HEXAGON_BACKEND_GGML = 4, //"fake" HEXAGON backend for compare performance between HEXAGON backend and ggml backend
2222
};
2323

24+
//0: general approach through QNN:offload ggmlop to QNN(QNNCPU, QNNGPU, QNNNPU)
25+
//1: special approach through QNN-SINGLEGRAPH:mapping entire ggml cgraph to a single QNN graph
26+
//2: general approach through Hexagon cDSP:offload ggmlop to Hexagon cDSP directly
27+
enum hwaccel_approach_type {
28+
HWACCEL_QNN = 0,
29+
HWACCEL_QNN_SINGLEGRAPH= 1,
30+
HWACCEL_CDSP = 2,
31+
};
32+
2433
GGML_BACKEND_API ggml_backend_t ggml_backend_hexagon_init(size_t dev_num, const char * qnn_lib_path);
2534

2635
GGML_BACKEND_API bool ggml_backend_is_hexagon(ggml_backend_t backend);
@@ -31,6 +40,8 @@ GGML_BACKEND_API ggml_backend_reg_t ggml_backend_hexagon_reg(void);
3140

3241
const char * ggml_backend_hexagon_get_devname(size_t dev_num);
3342

43+
void set_hexagon_cfg(int new_hexagon_backend, int new_hwaccel_approach);
44+
3445
#ifdef __cplusplus
3546
}
3647
#endif

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

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <unordered_set>
5959
#include <utility>
6060
#include <future>
61+
#include <algorithm>
6162

6263
#if defined(__ANDROID__) || defined(__linux__)
6364
#include <unistd.h>
@@ -208,15 +209,6 @@ enum qnn_profile_level {
208209
PROFILE_DETAIL = 2,
209210
};
210211

211-
//0: general approach through QNN:offload ggmlop to QNN
212-
//1: special approach through QNN-SINGLEGRAPH:mapping entire ggml cgraph to a single QNN graph
213-
//2: general approach through Hexagon cDSP:offload ggmlop to Hexagon cDSP directly
214-
enum hwaccel_approach_type {
215-
HWACCEL_QNN = 0,
216-
HWACCEL_QNN_SINGLEGRAPH = 1,
217-
HWACCEL_CDSP = 2,
218-
};
219-
220212
enum hexagon_dsp_type {
221213
HEXAGON_ADSP = 0,
222214
HEXAGON_MDSP = 1,
@@ -381,7 +373,7 @@ static struct hexagon_appcfg_t g_hexagon_appcfg = {
381373
#elif defined(_WIN32)
382374
.qnn_runtimelib_path = "C:\\",
383375
#endif
384-
.ggml_hexagon_version = {"1.08"},
376+
.ggml_hexagon_version = {"1.09"},
385377
.ggml_dsp_version = {"0.63"},
386378
};
387379

@@ -1378,6 +1370,83 @@ class hexagon_appcfg {
13781370
value = atol(_hexagon_appcfg[section][key].c_str());
13791371
}
13801372

1373+
bool modify_hexagon_config(std::string & cfg_filename, int new_hexagon_backend, int new_hwaccel_approach) {
1374+
std::ifstream inputfile(cfg_filename);
1375+
if (!inputfile.is_open()) {
1376+
GGMLHEXAGON_LOG_WARN("can't open file %s", cfg_filename.c_str());
1377+
return false;
1378+
}
1379+
1380+
std::string filedata = "";
1381+
1382+
std::string line;
1383+
std::string backupline;
1384+
bool is_rewrite = false;
1385+
bool is_founded = false;
1386+
bool is_key = true;
1387+
std::string key;
1388+
std::string value;
1389+
std::string newvalue;
1390+
while (std::getline(inputfile, line)) {
1391+
is_founded = false;
1392+
backupline = line;
1393+
trim(line);
1394+
if (0 == line.rfind("#", 0)) {
1395+
filedata += backupline;
1396+
filedata += "\n";
1397+
continue;
1398+
}
1399+
1400+
newvalue = "";
1401+
if (line.rfind("hexagon_backend", 0) != std::string::npos) {
1402+
is_founded = true;
1403+
is_rewrite = true;
1404+
newvalue = std::to_string(new_hexagon_backend);
1405+
}
1406+
1407+
if (line.rfind("hwaccel_approach", 0) != std::string::npos) {
1408+
is_founded = true;
1409+
is_rewrite = true;
1410+
newvalue = std::to_string(new_hwaccel_approach);
1411+
}
1412+
1413+
if (is_founded) {
1414+
is_key = true;
1415+
key = "";
1416+
value = "";
1417+
1418+
for (size_t i = 0; i < line.size(); ++i) {
1419+
if (line[i] == '=') {
1420+
is_key = false;
1421+
continue;
1422+
}
1423+
if (is_key) {
1424+
key += line[i];
1425+
} else {
1426+
value += line[i];
1427+
}
1428+
}
1429+
trim(key);
1430+
trim(value);
1431+
GGMLHEXAGON_LOG_INFO("key %s value %s\n", key.c_str(), value.c_str());
1432+
GGMLHEXAGON_LOG_INFO("key %s new value %s\n", key.c_str(), newvalue.c_str());
1433+
backupline = key + " = " + newvalue;
1434+
}
1435+
filedata += backupline;
1436+
filedata += "\n";
1437+
}
1438+
inputfile.close();
1439+
1440+
if (is_rewrite) {
1441+
std::ofstream outputfile;
1442+
outputfile.open(cfg_filename);
1443+
outputfile.flush();
1444+
outputfile << filedata;
1445+
outputfile.close();
1446+
}
1447+
return true;
1448+
}
1449+
13811450
private:
13821451
void ltrim(std::string & str) {
13831452
if (str.empty()) return;
@@ -1919,6 +1988,20 @@ static void ggmlhexagon_load_cfg() {
19191988
initialized = true;
19201989
}
19211990

1991+
void set_hexagon_cfg(int new_hexagon_backend, int new_hwaccel_approach) {
1992+
std::string cfg_filename = std::string(g_hexagon_appcfg.runtime_libpath) + std::string(g_hexagon_appcfg.cfgfilename);
1993+
GGMLHEXAGON_LOG_INFO("load hexagon appcfg from %s", cfg_filename.c_str());
1994+
hexagon_appcfg hexagoncfg_instance;
1995+
GGMLHEXAGON_LOG_INFO("set_hexagon_cfg with new_hexagon_backend %d, new_hwaccel_approach %d", new_hexagon_backend, new_hwaccel_approach);
1996+
hexagoncfg_instance.modify_hexagon_config(cfg_filename, new_hexagon_backend, new_hwaccel_approach);
1997+
hexagoncfg_instance.load(cfg_filename);
1998+
hexagoncfg_instance.dump([](const std::string & section, const std::string & key, const std::string value) {
1999+
std::ostringstream tmposs;
2000+
tmposs << "section[" << std::setw(10) << std::left << section << "],[" << std::setw(25) << std::left << key << "] = [" << value << "]";
2001+
GGMLHEXAGON_LOG_INFO("%s", tmposs.str().c_str());
2002+
});
2003+
}
2004+
19222005
static bool ggmlhexagon_check_valid_appcfg() {
19232006
bool is_valid_appcfg = true;
19242007

scripts/ggml-hexagon.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[general]
22
#version of ggml-hexagon.cpp on ARM-AP side
3-
version = "1.08"
3+
version = "1.09"
44
#version of ggml-dsp.c on cDSP side
55
ggmldsp_version = "0.63"
66

0 commit comments

Comments
 (0)