|
58 | 58 | #include <unordered_set> |
59 | 59 | #include <utility> |
60 | 60 | #include <future> |
| 61 | +#include <algorithm> |
61 | 62 |
|
62 | 63 | #if defined(__ANDROID__) || defined(__linux__) |
63 | 64 | #include <unistd.h> |
@@ -208,15 +209,6 @@ enum qnn_profile_level { |
208 | 209 | PROFILE_DETAIL = 2, |
209 | 210 | }; |
210 | 211 |
|
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 | | - |
220 | 212 | enum hexagon_dsp_type { |
221 | 213 | HEXAGON_ADSP = 0, |
222 | 214 | HEXAGON_MDSP = 1, |
@@ -381,7 +373,7 @@ static struct hexagon_appcfg_t g_hexagon_appcfg = { |
381 | 373 | #elif defined(_WIN32) |
382 | 374 | .qnn_runtimelib_path = "C:\\", |
383 | 375 | #endif |
384 | | - .ggml_hexagon_version = {"1.08"}, |
| 376 | + .ggml_hexagon_version = {"1.09"}, |
385 | 377 | .ggml_dsp_version = {"0.63"}, |
386 | 378 | }; |
387 | 379 |
|
@@ -1378,6 +1370,83 @@ class hexagon_appcfg { |
1378 | 1370 | value = atol(_hexagon_appcfg[section][key].c_str()); |
1379 | 1371 | } |
1380 | 1372 |
|
| 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 | + |
1381 | 1450 | private: |
1382 | 1451 | void ltrim(std::string & str) { |
1383 | 1452 | if (str.empty()) return; |
@@ -1919,6 +1988,20 @@ static void ggmlhexagon_load_cfg() { |
1919 | 1988 | initialized = true; |
1920 | 1989 | } |
1921 | 1990 |
|
| 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 | + |
1922 | 2005 | static bool ggmlhexagon_check_valid_appcfg() { |
1923 | 2006 | bool is_valid_appcfg = true; |
1924 | 2007 |
|
|
0 commit comments