-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
justlm.cpp
66 lines (61 loc) · 2.15 KB
/
justlm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "justlm.hpp"
#include "dlhandle.hpp"
#include <string>
#include <vector>
#include <fstream>
#include <filesystem>
static
Dlhandle get_implementation(std::ifstream& input_f) {
Dlhandle matching;
Dlhandle fallback;
// Iterate over all libraries
for (const auto& f : std::filesystem::directory_iterator(".")) {
// Get path
const auto& p = f.path();
// Check extension
if (p.extension() != LIB_FILE_EXT) continue;
// Load library
try {
Dlhandle dl(p);
// Get implementation info getter
auto implementation_getter = dl.get<const LM::Implementation *()>("get_justlm_implementation");
if (!implementation_getter) continue;
// Get implementation info
const auto *implementation_info = implementation_getter();
// Set if fallback
if (implementation_info->is_fallback) {
fallback = std::move(dl);
continue;
}
// Set if matching magic
input_f.seekg(0);
auto magic_match = dl.get<bool(std::ifstream&)>("magic_match");
if (magic_match && magic_match(input_f)) {
matching = std::move(dl);
continue;
}
} catch (...) {}
}
// Return matching if any, fallback otherwise
if (matching) return matching;
return fallback;
}
LM::Inference *LM::Inference::construct(const std::string &weights_path, const Params &p) {
static std::vector<Dlhandle> dls;
// Read magic
std::ifstream f(weights_path, std::ios::binary);
if (!f) {
throw Exception("Failed to open weights file for reading at "+weights_path);
}
// Get correct implementation
auto impl = get_implementation(f);
if (!impl) return nullptr;
// Get inference constructor
auto constructor = impl.get<LM::Inference *(const std::string &, std::ifstream&, const LM::Inference::Params &)>("construct");
if (!constructor) return nullptr;
// Back up Dlhandle
dls.push_back(std::move(impl));
// Construct inference
f.seekg(0);
return constructor(weights_path, f, p);
}