0015-use-std-filesystem-path-instead-of-wstring.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: jmorganca <jmorganca@gmail.com>
  3. Date: Sun, 16 Feb 2025 20:00:22 -0500
  4. Subject: [PATCH] use std::filesystem::path instead of wstring
  5. ---
  6. ggml/src/ggml-backend-reg.cpp | 199 +++++++++++++++-------------------
  7. 1 file changed, 88 insertions(+), 111 deletions(-)
  8. diff --git a/ggml/src/ggml-backend-reg.cpp b/ggml/src/ggml-backend-reg.cpp
  9. index 98d5e14d..799af5f3 100644
  10. --- a/ggml/src/ggml-backend-reg.cpp
  11. +++ b/ggml/src/ggml-backend-reg.cpp
  12. @@ -66,26 +66,6 @@
  13. #include "ggml-kompute.h"
  14. #endif
  15. -// disable C++17 deprecation warning for std::codecvt_utf8
  16. -#if defined(__clang__)
  17. -# pragma clang diagnostic push
  18. -# pragma clang diagnostic ignored "-Wdeprecated-declarations"
  19. -#endif
  20. -
  21. -static std::wstring utf8_to_utf16(const std::string & str) {
  22. - std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
  23. - return converter.from_bytes(str);
  24. -}
  25. -
  26. -static std::string utf16_to_utf8(const std::wstring & str) {
  27. - std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
  28. - return converter.to_bytes(str);
  29. -}
  30. -
  31. -#if defined(__clang__)
  32. -# pragma clang diagnostic pop
  33. -#endif
  34. -
  35. #ifdef _WIN32
  36. using dl_handle = std::remove_pointer_t<HMODULE>;
  37. @@ -96,7 +76,7 @@ struct dl_handle_deleter {
  38. }
  39. };
  40. -static dl_handle * dl_load_library(const std::wstring & path) {
  41. +static dl_handle * dl_load_library(const std::filesystem::path & path) {
  42. // suppress error dialogs for missing DLLs
  43. DWORD old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
  44. SetErrorMode(old_mode | SEM_FAILCRITICALERRORS);
  45. @@ -129,8 +109,8 @@ struct dl_handle_deleter {
  46. }
  47. };
  48. -static void * dl_load_library(const std::wstring & path) {
  49. - dl_handle * handle = dlopen(utf16_to_utf8(path).c_str(), RTLD_NOW | RTLD_LOCAL);
  50. +static void * dl_load_library(const std::filesystem::path & path) {
  51. + dl_handle * handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
  52. return handle;
  53. }
  54. @@ -141,6 +121,25 @@ static void * dl_get_sym(dl_handle * handle, const char * name) {
  55. #endif
  56. +static std::string path_to_string(const std::filesystem::path & path)
  57. +{
  58. +#ifdef _WIN32
  59. + const std::wstring wstr = path.wstring();
  60. + const int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
  61. + if (size_needed <= 0) {
  62. + return std::string();
  63. + }
  64. +
  65. + // size_needed includes the null terminator
  66. + std::string str(size_needed - 1, '\0');
  67. + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, str.data(), size_needed, nullptr, nullptr);
  68. + return str;
  69. +#else
  70. + return path.string();
  71. +#endif
  72. +}
  73. +
  74. +
  75. using dl_handle_ptr = std::unique_ptr<dl_handle, dl_handle_deleter>;
  76. struct ggml_backend_reg_entry {
  77. @@ -222,11 +221,11 @@ struct ggml_backend_registry {
  78. );
  79. }
  80. - ggml_backend_reg_t load_backend(const std::wstring & path, bool silent) {
  81. + ggml_backend_reg_t load_backend(const std::filesystem::path & path, bool silent) {
  82. dl_handle_ptr handle { dl_load_library(path) };
  83. if (!handle) {
  84. if (!silent) {
  85. - GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(path).c_str());
  86. + GGML_LOG_ERROR("%s: failed to load %s\n", __func__, path_to_string(path).c_str());
  87. }
  88. return nullptr;
  89. }
  90. @@ -234,7 +233,7 @@ struct ggml_backend_registry {
  91. auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
  92. if (score_fn && score_fn() == 0) {
  93. if (!silent) {
  94. - GGML_LOG_INFO("%s: backend %s is not supported on this system\n", __func__, utf16_to_utf8(path).c_str());
  95. + GGML_LOG_INFO("%s: backend %s is not supported on this system\n", __func__, path_to_string(path).c_str());
  96. }
  97. return nullptr;
  98. }
  99. @@ -242,7 +241,7 @@ struct ggml_backend_registry {
  100. auto backend_init_fn = (ggml_backend_init_t) dl_get_sym(handle.get(), "ggml_backend_init");
  101. if (!backend_init_fn) {
  102. if (!silent) {
  103. - GGML_LOG_ERROR("%s: failed to find ggml_backend_init in %s\n", __func__, utf16_to_utf8(path).c_str());
  104. + GGML_LOG_ERROR("%s: failed to find ggml_backend_init in %s\n", __func__, path_to_string(path).c_str());
  105. }
  106. return nullptr;
  107. }
  108. @@ -251,16 +250,16 @@ struct ggml_backend_registry {
  109. if (!reg || reg->api_version != GGML_BACKEND_API_VERSION) {
  110. if (!silent) {
  111. if (!reg) {
  112. - GGML_LOG_ERROR("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", __func__, utf16_to_utf8(path).c_str());
  113. + GGML_LOG_ERROR("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", __func__, path_to_string(path).c_str());
  114. } else {
  115. GGML_LOG_ERROR("%s: failed to initialize backend from %s: incompatible API version (backend: %d, current: %d)\n",
  116. - __func__, utf16_to_utf8(path).c_str(), reg->api_version, GGML_BACKEND_API_VERSION);
  117. + __func__, path_to_string(path).c_str(), reg->api_version, GGML_BACKEND_API_VERSION);
  118. }
  119. }
  120. return nullptr;
  121. }
  122. - GGML_LOG_INFO("%s: loaded %s backend from %s\n", __func__, ggml_backend_reg_name(reg), utf16_to_utf8(path).c_str());
  123. + GGML_LOG_INFO("%s: loaded %s backend from %s\n", __func__, ggml_backend_reg_name(reg), path_to_string(path).c_str());
  124. register_backend(reg, score_fn ? score_fn() : -1, std::move(handle));
  125. @@ -396,14 +395,14 @@ ggml_backend_t ggml_backend_init_best(void) {
  126. // Dynamic loading
  127. ggml_backend_reg_t ggml_backend_load(const char * path) {
  128. - return get_reg().load_backend(utf8_to_utf16(path), false);
  129. + return get_reg().load_backend(path, false);
  130. }
  131. void ggml_backend_unload(ggml_backend_reg_t reg) {
  132. get_reg().unload_backend(reg, true);
  133. }
  134. -static std::wstring get_executable_path() {
  135. +static std::filesystem::path get_executable_path() {
  136. #if defined(__APPLE__)
  137. // get executable path
  138. std::vector<char> path;
  139. @@ -415,15 +414,9 @@ static std::wstring get_executable_path() {
  140. }
  141. path.resize(size);
  142. }
  143. - std::string base_path(path.data(), size);
  144. - // remove executable name
  145. - auto last_slash = base_path.find_last_of('/');
  146. - if (last_slash != std::string::npos) {
  147. - base_path = base_path.substr(0, last_slash);
  148. - }
  149. - return utf8_to_utf16(base_path + "/");
  150. +
  151. + return std::filesystem::path(path.data()).parent_path();
  152. #elif defined(__linux__) || defined(__FreeBSD__)
  153. - std::string base_path = ".";
  154. std::vector<char> path(1024);
  155. while (true) {
  156. // get executable path
  157. @@ -436,76 +429,55 @@ static std::wstring get_executable_path() {
  158. break;
  159. }
  160. if (len < (ssize_t) path.size()) {
  161. - base_path = std::string(path.data(), len);
  162. - // remove executable name
  163. - auto last_slash = base_path.find_last_of('/');
  164. - if (last_slash != std::string::npos) {
  165. - base_path = base_path.substr(0, last_slash);
  166. - }
  167. - break;
  168. + return std::filesystem::path(path.data()).parent_path();
  169. }
  170. path.resize(path.size() * 2);
  171. }
  172. -
  173. - return utf8_to_utf16(base_path + "/");
  174. #elif defined(_WIN32)
  175. std::vector<wchar_t> path(MAX_PATH);
  176. DWORD len = GetModuleFileNameW(NULL, path.data(), path.size());
  177. if (len == 0) {
  178. return {};
  179. }
  180. - std::wstring base_path(path.data(), len);
  181. - // remove executable name
  182. - auto last_slash = base_path.find_last_of('\\');
  183. - if (last_slash != std::string::npos) {
  184. - base_path = base_path.substr(0, last_slash);
  185. - }
  186. - return base_path + L"\\";
  187. -#else
  188. - return {};
  189. -#endif
  190. -}
  191. -static std::wstring backend_filename_prefix() {
  192. -#ifdef _WIN32
  193. - return L"ggml-";
  194. -#else
  195. - return L"libggml-";
  196. + return std::filesystem::path(path.data()).parent_path();
  197. #endif
  198. + return {};
  199. }
  200. -static std::wstring backend_filename_suffix() {
  201. +static std::string backend_filename_prefix() {
  202. #ifdef _WIN32
  203. - return L".dll";
  204. + return "ggml-";
  205. #else
  206. - return L".so";
  207. + return "libggml-";
  208. #endif
  209. }
  210. -static std::wstring path_separator() {
  211. +static std::string backend_filename_suffix() {
  212. #ifdef _WIN32
  213. - return L"\\";
  214. + return ".dll";
  215. #else
  216. - return L"/";
  217. + return ".so";
  218. #endif
  219. }
  220. static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent, const char * user_search_path) {
  221. // enumerate all the files that match [lib]ggml-name-*.[so|dll] in the search paths
  222. // TODO: search system paths
  223. - std::wstring file_prefix = backend_filename_prefix() + utf8_to_utf16(name) + L"-";
  224. - std::vector<std::wstring> search_paths;
  225. + namespace fs = std::filesystem;
  226. + std::string file_prefix = backend_filename_prefix() + name + "-";
  227. + std::vector<fs::path> search_paths;
  228. +
  229. if (user_search_path == nullptr) {
  230. - search_paths.push_back(L"." + path_separator());
  231. + search_paths.push_back(fs::current_path());
  232. search_paths.push_back(get_executable_path());
  233. } else {
  234. - search_paths.push_back(utf8_to_utf16(user_search_path) + path_separator());
  235. + search_paths.push_back(fs::u8path(user_search_path));
  236. }
  237. int best_score = 0;
  238. - std::wstring best_path;
  239. + fs::path best_path;
  240. - namespace fs = std::filesystem;
  241. for (const auto & search_path : search_paths) {
  242. if (!fs::exists(search_path)) {
  243. continue;
  244. @@ -513,29 +485,26 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
  245. fs::directory_iterator dir_it(search_path, fs::directory_options::skip_permission_denied);
  246. for (const auto & entry : dir_it) {
  247. if (entry.is_regular_file()) {
  248. - std::wstring filename = entry.path().filename().wstring();
  249. - std::wstring ext = entry.path().extension().wstring();
  250. + std::string filename = entry.path().filename().string();
  251. + std::string ext = entry.path().extension().string();
  252. if (filename.find(file_prefix) == 0 && ext == backend_filename_suffix()) {
  253. - dl_handle_ptr handle { dl_load_library(entry.path().wstring()) };
  254. - if (!handle && !silent) {
  255. - GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  256. + dl_handle_ptr handle { dl_load_library(entry.path()) };
  257. + if (!handle) {
  258. + GGML_LOG_ERROR("%s: failed to load %s\n", __func__, path_to_string(entry.path()).c_str());
  259. + continue;
  260. }
  261. - if (handle) {
  262. - auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
  263. - if (score_fn) {
  264. - int s = score_fn();
  265. -#ifndef NDEBUG
  266. - GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str(), s);
  267. -#endif
  268. - if (s > best_score) {
  269. - best_score = s;
  270. - best_path = entry.path().wstring();
  271. - }
  272. - } else {
  273. - if (!silent) {
  274. - GGML_LOG_INFO("%s: failed to find ggml_backend_score in %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  275. - }
  276. - }
  277. +
  278. + auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
  279. + if (!score_fn) {
  280. + GGML_LOG_DEBUG("%s: failed to find ggml_backend_score in %s\n", __func__, path_to_string(entry.path()).c_str());
  281. + continue;
  282. + }
  283. +
  284. + int s = score_fn();
  285. + GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, path_to_string(entry.path()).c_str(), s);
  286. + if (s > best_score) {
  287. + best_score = s;
  288. + best_path = entry.path();
  289. }
  290. }
  291. }
  292. @@ -545,7 +514,7 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
  293. if (best_score == 0) {
  294. // try to load the base backend
  295. for (const auto & search_path : search_paths) {
  296. - std::wstring path = search_path + backend_filename_prefix() + utf8_to_utf16(name) + backend_filename_suffix();
  297. + fs::path path = fs::path(search_path) / (backend_filename_prefix() + name + backend_filename_suffix());
  298. if (fs::exists(path)) {
  299. return get_reg().load_backend(path, silent);
  300. }
  301. @@ -560,6 +529,14 @@ void ggml_backend_load_all() {
  302. ggml_backend_load_all_from_path(nullptr);
  303. }
  304. +static void ggml_backend_try_load_best(const char * name, bool silent, const char * user_search_path) {
  305. + try {
  306. + ggml_backend_load_best(name, silent, user_search_path);
  307. + } catch (const std::exception & e) {
  308. + GGML_LOG_DEBUG("%s: failed to load %s: %s\n", __func__, name, e.what());
  309. + }
  310. +}
  311. +
  312. void ggml_backend_load_all_from_path(const char * dir_path) {
  313. #ifdef NDEBUG
  314. bool silent = true;
  315. @@ -567,18 +544,18 @@ void ggml_backend_load_all_from_path(const char * dir_path) {
  316. bool silent = false;
  317. #endif
  318. - ggml_backend_load_best("blas", silent, dir_path);
  319. - ggml_backend_load_best("cann", silent, dir_path);
  320. - ggml_backend_load_best("cuda", silent, dir_path);
  321. - ggml_backend_load_best("hip", silent, dir_path);
  322. - ggml_backend_load_best("kompute", silent, dir_path);
  323. - ggml_backend_load_best("metal", silent, dir_path);
  324. - ggml_backend_load_best("rpc", silent, dir_path);
  325. - ggml_backend_load_best("sycl", silent, dir_path);
  326. - ggml_backend_load_best("vulkan", silent, dir_path);
  327. - ggml_backend_load_best("opencl", silent, dir_path);
  328. - ggml_backend_load_best("musa", silent, dir_path);
  329. - ggml_backend_load_best("cpu", silent, dir_path);
  330. + ggml_backend_try_load_best("blas", silent, dir_path);
  331. + ggml_backend_try_load_best("cann", silent, dir_path);
  332. + ggml_backend_try_load_best("cuda", silent, dir_path);
  333. + ggml_backend_try_load_best("hip", silent, dir_path);
  334. + ggml_backend_try_load_best("kompute", silent, dir_path);
  335. + ggml_backend_try_load_best("metal", silent, dir_path);
  336. + ggml_backend_try_load_best("rpc", silent, dir_path);
  337. + ggml_backend_try_load_best("sycl", silent, dir_path);
  338. + ggml_backend_try_load_best("vulkan", silent, dir_path);
  339. + ggml_backend_try_load_best("opencl", silent, dir_path);
  340. + ggml_backend_try_load_best("musa", silent, dir_path);
  341. + ggml_backend_try_load_best("cpu", silent, dir_path);
  342. // check the environment variable GGML_BACKEND_PATH to load an out-of-tree backend
  343. const char * backend_path = std::getenv("GGML_BACKEND_PATH");
  344. if (backend_path) {