Skip to content

Commit 5c02d40

Browse files
apage43cebtenzzre
andcommitted
llmodel: dont load libs in static initialization
this fixes some issues that were being seen on installed windows builds of 2.5.0 setImplementationsSearchPath did not work if llmodel was initialized before it was called * do not actually load impl dlls in the static initializer, wait until we're actually trying to load a model for the first time * rescan on the next model load attempt if the search path has been changed * only load dlls that actually might be model impl dlls, otherwise we pull all sorts of random junk into the process before it might expect to be Co-authored-by: cebtenzzre <[email protected]> Signed-off-by: Aaron Miller <[email protected]>
1 parent d50803f commit 5c02d40

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

gpt4all-backend/llmodel.cpp

+25-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <cassert>
1111
#include <cstdlib>
1212
#include <sstream>
13+
#include <regex>
14+
#include <iterator>
1315
#ifdef _MSC_VER
1416
#include <intrin.h>
1517
#endif
@@ -75,12 +77,20 @@ bool LLModel::Implementation::isImplementation(const Dlhandle &dl) {
7577
return dl.get<bool(uint32_t)>("is_g4a_backend_model_implementation");
7678
}
7779

78-
const std::vector<LLModel::Implementation> &LLModel::Implementation::implementationList() {
80+
static bool s_scanned = false;
81+
82+
const std::vector<std::unique_ptr<LLModel::Implementation>> &LLModel::Implementation::implementationList() {
83+
static std::vector<std::unique_ptr<LLModel::Implementation>> s_impl_libs;
7984
// NOTE: allocated on heap so we leak intentionally on exit so we have a chance to clean up the
8085
// individual models without the cleanup of the static list interfering
81-
static auto* libs = new std::vector<Implementation>([] () {
82-
std::vector<Implementation> fres;
83-
86+
if(!s_scanned) {
87+
std::string impl_name_re = "(bert|llama|gptj|llamamodel-mainline)";
88+
if (requires_avxonly()) {
89+
impl_name_re += "-avxonly";
90+
} else {
91+
impl_name_re += "-(default|metal)";
92+
}
93+
std::regex re(impl_name_re);
8494
auto search_in_directory = [&](const std::string& paths) {
8595
std::stringstream ss(paths);
8696
std::string path;
@@ -90,32 +100,33 @@ const std::vector<LLModel::Implementation> &LLModel::Implementation::implementat
90100
// Iterate over all libraries
91101
for (const auto& f : std::filesystem::directory_iterator(fs_path)) {
92102
const std::filesystem::path& p = f.path();
103+
93104
if (p.extension() != LIB_FILE_EXT) continue;
105+
if (!std::regex_search(p.stem().string(), re)) continue;
106+
94107
// Add to list if model implementation
95108
try {
96109
Dlhandle dl(p.string());
97110
if (!Implementation::isImplementation(dl)) {
98111
continue;
99112
}
100-
fres.emplace_back(Implementation(std::move(dl)));
113+
s_impl_libs.emplace_back(std::make_unique<Implementation>(std::move(dl)));
101114
} catch (...) {}
102115
}
103116
}
104117
};
105118

106119
search_in_directory(s_implementations_search_path);
107-
108-
return fres;
109-
}());
110-
// Return static result
111-
return *libs;
120+
s_scanned = true;
121+
};
122+
return s_impl_libs;
112123
}
113124

114125
const LLModel::Implementation* LLModel::Implementation::implementation(const char *fname, const std::string& buildVariant) {
115126
for (const auto& i : implementationList()) {
116-
if (buildVariant != i.m_buildVariant) continue;
117-
if (!i.m_magicMatch(fname)) continue;
118-
return &i;
127+
if (buildVariant != i->m_buildVariant) continue;
128+
if (!i->m_magicMatch(fname)) continue;
129+
return i.get();
119130
}
120131
return nullptr;
121132
}
@@ -170,6 +181,7 @@ LLModel *LLModel::Implementation::construct(const std::string &modelPath, std::s
170181

171182
void LLModel::Implementation::setImplementationsSearchPath(const std::string& path) {
172183
s_implementations_search_path = path;
184+
s_scanned = false;
173185
}
174186

175187
const std::string& LLModel::Implementation::implementationsSearchPath() {

gpt4all-backend/llmodel.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <fstream>
99
#include <cstdint>
1010
#include <limits>
11+
#include <memory>
1112

1213
#define LLMODEL_MAX_PROMPT_BATCH 128
1314

@@ -26,7 +27,7 @@ class LLModel {
2627
std::string_view buildVariant() const { return m_buildVariant; }
2728

2829
static bool isImplementation(const Dlhandle&);
29-
static const std::vector<Implementation>& implementationList();
30+
static const std::vector<std::unique_ptr<Implementation>>& implementationList();
3031
static const Implementation *implementation(const char *fname, const std::string& buildVariant);
3132
static LLModel *construct(const std::string &modelPath, std::string buildVariant = "auto");
3233
static void setImplementationsSearchPath(const std::string& path);

0 commit comments

Comments
 (0)