Skip to content

Commit 2dc294c

Browse files
committed
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
1 parent d50803f commit 2dc294c

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

gpt4all-backend/llmodel.cpp

+26-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 std::vector<std::unique_ptr<LLModel::Implementation>> s_impl_libs;
81+
static bool s_scanned = false;
82+
83+
const std::vector<std::unique_ptr<LLModel::Implementation>> &LLModel::Implementation::implementationList() {
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";
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,34 @@ 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 {
109+
std::cerr << "attempt load: " << p << std::endl;
96110
Dlhandle dl(p.string());
97111
if (!Implementation::isImplementation(dl)) {
98112
continue;
99113
}
100-
fres.emplace_back(Implementation(std::move(dl)));
114+
s_impl_libs.emplace_back(std::make_unique<Implementation>(Implementation(std::move(dl))));
101115
} catch (...) {}
102116
}
103117
}
104118
};
105119

106120
search_in_directory(s_implementations_search_path);
107-
108-
return fres;
109-
}());
110-
// Return static result
111-
return *libs;
121+
s_scanned = true;
122+
};
123+
return s_impl_libs;
112124
}
113125

114126
const LLModel::Implementation* LLModel::Implementation::implementation(const char *fname, const std::string& buildVariant) {
115127
for (const auto& i : implementationList()) {
116-
if (buildVariant != i.m_buildVariant) continue;
117-
if (!i.m_magicMatch(fname)) continue;
118-
return &i;
128+
if (buildVariant != i->m_buildVariant) continue;
129+
if (!i->m_magicMatch(fname)) continue;
130+
return i.get();
119131
}
120132
return nullptr;
121133
}
@@ -170,6 +182,7 @@ LLModel *LLModel::Implementation::construct(const std::string &modelPath, std::s
170182

171183
void LLModel::Implementation::setImplementationsSearchPath(const std::string& path) {
172184
s_implementations_search_path = path;
185+
s_scanned = false;
173186
}
174187

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

gpt4all-backend/llmodel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LLModel {
2626
std::string_view buildVariant() const { return m_buildVariant; }
2727

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

0 commit comments

Comments
 (0)