Skip to content

Commit eb80d2b

Browse files
committed
ggml: search for dynamic backend libs in the same directory as the visioncpp library
1 parent c93122b commit eb80d2b

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/visp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ target_sources(visioncpp PRIVATE
88
ml.cpp
99
mobile-sam.cpp
1010
nn.cpp
11+
platform.cpp
1112
vision.cpp
1213
)
1314
target_compile_features(visioncpp PUBLIC cxx_std_20)

src/visp/ml.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "visp/ml.hpp"
2+
#include "visp/platform.hpp"
23
#include "util/string.hpp"
34

45
#include <algorithm>
@@ -27,6 +28,12 @@ bool load_ggml_backends() {
2728
return true; // already loaded
2829
}
2930
ggml_backend_load_all();
31+
if (ggml_backend_reg_count() == 0) {
32+
if (path dir = current_library_path(); !dir.empty()) {
33+
auto str = dir.parent_path().u8string();
34+
ggml_backend_load_all_from_path((char const*)str.c_str());
35+
}
36+
}
3037
return true;
3138
}();
3239
return loaded;

src/visp/platform.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "visp/platform.hpp"
2+
3+
#ifdef _WIN32
4+
# define WIN32_LEAN_AND_MEAN
5+
# include <windows.h>
6+
#else
7+
# include <dlfcn.h>
8+
#endif
9+
10+
namespace visp {
11+
using path = std::filesystem::path;
12+
13+
path current_library_path() {
14+
15+
#ifdef _WIN32
16+
HMODULE module = nullptr;
17+
DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
18+
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
19+
if (GetModuleHandleExW(flags, (LPCWSTR)&current_library_path, &module)) {
20+
wchar_t buffer[MAX_PATH];
21+
if (GetModuleFileNameW(module, buffer, MAX_PATH) > 0) {
22+
return std::filesystem::path(buffer);
23+
}
24+
}
25+
#else
26+
Dl_info info;
27+
if (dladdr(reinterpret_cast<void*>(&current_library_path), &info)) {
28+
return std::filesystem::path(info.dli_fname);
29+
}
30+
#endif
31+
return std::filesystem::path();
32+
}
33+
34+
} // namespace visp

src/visp/platform.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <filesystem>
4+
5+
namespace visp {
6+
using path = std::filesystem::path;
7+
8+
path current_library_path();
9+
10+
} // namespace visp

0 commit comments

Comments
 (0)