0015-try-catch-backend-load.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Michael Yang <mxyng@pm.me>
  3. Date: Tue, 11 Feb 2025 14:06:36 -0800
  4. Subject: [PATCH] try/catch backend load
  5. ---
  6. ggml/src/ggml-backend-reg.cpp | 45 ++++++++++++++++++-----------------
  7. 1 file changed, 23 insertions(+), 22 deletions(-)
  8. diff --git a/ggml/src/ggml-backend-reg.cpp b/ggml/src/ggml-backend-reg.cpp
  9. index 98d5e14d..1c19129a 100644
  10. --- a/ggml/src/ggml-backend-reg.cpp
  11. +++ b/ggml/src/ggml-backend-reg.cpp
  12. @@ -512,32 +512,33 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
  13. }
  14. fs::directory_iterator dir_it(search_path, fs::directory_options::skip_permission_denied);
  15. for (const auto & entry : dir_it) {
  16. - if (entry.is_regular_file()) {
  17. - std::wstring filename = entry.path().filename().wstring();
  18. - std::wstring ext = entry.path().extension().wstring();
  19. - if (filename.find(file_prefix) == 0 && ext == backend_filename_suffix()) {
  20. - dl_handle_ptr handle { dl_load_library(entry.path().wstring()) };
  21. - if (!handle && !silent) {
  22. - GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  23. - }
  24. - if (handle) {
  25. + try {
  26. + if (entry.is_regular_file()) {
  27. + std::wstring filename = entry.path().filename().wstring();
  28. + std::wstring ext = entry.path().extension().wstring();
  29. + if (filename.find(file_prefix) == 0 && ext == backend_filename_suffix()) {
  30. + dl_handle_ptr handle { dl_load_library(entry.path().wstring()) };
  31. + if (!handle) {
  32. + GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  33. + continue;
  34. + }
  35. +
  36. auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
  37. - if (score_fn) {
  38. - int s = score_fn();
  39. -#ifndef NDEBUG
  40. - GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str(), s);
  41. -#endif
  42. - if (s > best_score) {
  43. - best_score = s;
  44. - best_path = entry.path().wstring();
  45. - }
  46. - } else {
  47. - if (!silent) {
  48. - GGML_LOG_INFO("%s: failed to find ggml_backend_score in %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  49. - }
  50. + if (!score_fn) {
  51. + GGML_LOG_DEBUG("%s: failed to find ggml_backend_score in %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  52. + continue;
  53. + }
  54. +
  55. + int s = score_fn();
  56. + GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str(), s);
  57. + if (s > best_score) {
  58. + best_score = s;
  59. + best_path = entry.path().wstring();
  60. }
  61. }
  62. }
  63. + } catch (const std::exception & e) {
  64. + GGML_LOG_ERROR("%s: failed to load %s: %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str(), e.what());
  65. }
  66. }
  67. }