Skip to content

Commit 67bb90c

Browse files
committed
ggml : add ggml_backend_load_variant to load specific backend variant
This commit adds a new function ggml_backend_load_variant to the ggml library. The motivation for this is that it is useful to be able to load a single backend variant by name. The particular use case I have is for testing where I want to be able to load a specific variant of a backend and then find out what extra buffer types it may support and then load buffer type using that backend variant.
1 parent 4b88852 commit 67bb90c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

ggml/include/ggml-backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ extern "C" {
245245
GGML_API void ggml_backend_load_all_from_path(const char * dir_path);
246246
// Load all variants for a backend and register them
247247
GGML_API void ggml_backend_load_all_variants(const char * name);
248+
GGML_API void ggml_backend_load_variant(const char * name, const char * variant);
248249

249250
//
250251
// Backend scheduler

ggml/src/ggml-backend-reg.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,35 @@ void ggml_backend_load_all_variants(const char * name) {
637637
}
638638
}
639639
}
640+
641+
void ggml_backend_load_variant(const char * name, const char * variant) {
642+
const fs::path name_path = fs::u8path(name);
643+
const fs::path variant_path = fs::u8path(variant);
644+
const fs::path file_prefix = backend_filename_prefix().native() + name_path.native() + fs::u8path("-").native();
645+
const fs::path target_filename = file_prefix.native() + variant_path.native() + backend_filename_extension().native();
646+
647+
std::vector<fs::path> search_paths;
648+
#ifdef GGML_BACKEND_DIR
649+
search_paths.push_back(fs::u8path(GGML_BACKEND_DIR));
650+
#endif
651+
// default search paths: executable directory, current directory
652+
search_paths.push_back(get_executable_path());
653+
search_paths.push_back(fs::current_path());
654+
655+
for (const auto & search_path : search_paths) {
656+
if (!fs::exists(search_path)) {
657+
GGML_LOG_DEBUG("%s: search path %s does not exist\n", __func__, path_str(search_path).c_str());
658+
continue;
659+
}
660+
661+
fs::path full_path = search_path / target_filename;
662+
if (fs::exists(full_path) && fs::is_regular_file(full_path)) {
663+
ggml_backend_reg_t backend = get_reg().load_backend(full_path, false);
664+
if (backend == nullptr) {
665+
GGML_LOG_ERROR("%s: failed to load backend variant %s\n", __func__, path_str(full_path).c_str());
666+
} else {
667+
return;
668+
}
669+
}
670+
}
671+
}

0 commit comments

Comments
 (0)