2023-05-31 17:04:01 -04:00
|
|
|
#include "llmodel.h"
|
2023-06-01 10:51:46 -04:00
|
|
|
#include "dlhandle.h"
|
2023-05-31 17:04:01 -04:00
|
|
|
|
2023-06-01 23:15:58 -04:00
|
|
|
#include <iostream>
|
2023-05-31 17:04:01 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
|
|
|
#include <filesystem>
|
2023-05-31 15:37:25 -04:00
|
|
|
#include <cassert>
|
2023-06-01 11:41:04 -04:00
|
|
|
#include <cstdlib>
|
2023-06-05 09:23:17 -04:00
|
|
|
#include <sstream>
|
2023-05-31 17:04:01 -04:00
|
|
|
|
2023-06-05 11:19:02 -04:00
|
|
|
std::string s_implementations_search_path = ".";
|
2023-06-02 10:57:21 -04:00
|
|
|
|
2023-06-04 15:28:58 -04:00
|
|
|
static bool has_at_least_minimal_hardware() {
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
return __builtin_cpu_supports("avx");
|
|
|
|
#else
|
|
|
|
int cpuInfo[4];
|
|
|
|
__cpuid(cpuInfo, 1);
|
|
|
|
return cpuInfo[2] & (1 << 28);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
return true; // Don't know how to handle non-x86_64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-01 07:57:10 -04:00
|
|
|
static bool requires_avxonly() {
|
2023-05-31 21:26:18 -04:00
|
|
|
#ifdef __x86_64__
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
return !__builtin_cpu_supports("avx2");
|
|
|
|
#else
|
|
|
|
int cpuInfo[4];
|
|
|
|
__cpuidex(cpuInfo, 7, 0);
|
|
|
|
return !(cpuInfo[1] & (1 << 5));
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
return false; // Don't know how to handle non-x86_64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-01 10:51:46 -04:00
|
|
|
LLModel::Implementation::Implementation(Dlhandle &&dlhandle_) : dlhandle(new Dlhandle(std::move(dlhandle_))) {
|
|
|
|
auto get_model_type = dlhandle->get<const char *()>("get_model_type");
|
2023-05-31 15:37:25 -04:00
|
|
|
assert(get_model_type);
|
|
|
|
modelType = get_model_type();
|
2023-06-01 10:51:46 -04:00
|
|
|
auto get_build_variant = dlhandle->get<const char *()>("get_build_variant");
|
2023-05-31 15:37:25 -04:00
|
|
|
assert(get_build_variant);
|
|
|
|
buildVariant = get_build_variant();
|
2023-06-01 10:51:46 -04:00
|
|
|
magicMatch = dlhandle->get<bool(std::ifstream&)>("magic_match");
|
2023-05-31 15:37:25 -04:00
|
|
|
assert(magicMatch);
|
2023-06-01 10:51:46 -04:00
|
|
|
construct_ = dlhandle->get<LLModel *()>("construct");
|
2023-05-31 15:37:25 -04:00
|
|
|
assert(construct_);
|
|
|
|
}
|
|
|
|
|
2023-06-01 11:16:33 -04:00
|
|
|
LLModel::Implementation::Implementation(Implementation &&o)
|
|
|
|
: construct_(o.construct_)
|
|
|
|
, modelType(o.modelType)
|
|
|
|
, buildVariant(o.buildVariant)
|
|
|
|
, magicMatch(o.magicMatch)
|
|
|
|
, dlhandle(o.dlhandle) {
|
|
|
|
o.dlhandle = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-06-01 10:51:46 -04:00
|
|
|
LLModel::Implementation::~Implementation() {
|
2023-06-01 11:16:33 -04:00
|
|
|
if (dlhandle) delete dlhandle;
|
2023-06-01 10:51:46 -04:00
|
|
|
}
|
|
|
|
|
2023-05-31 15:37:25 -04:00
|
|
|
bool LLModel::Implementation::isImplementation(const Dlhandle &dl) {
|
|
|
|
return dl.get<bool(uint32_t)>("is_g4a_backend_model_implementation");
|
|
|
|
}
|
|
|
|
|
2023-06-01 10:51:46 -04:00
|
|
|
const std::vector<LLModel::Implementation> &LLModel::implementationList() {
|
2023-06-01 07:57:10 -04:00
|
|
|
// NOTE: allocated on heap so we leak intentionally on exit so we have a chance to clean up the
|
|
|
|
// individual models without the cleanup of the static list interfering
|
2023-05-31 15:37:25 -04:00
|
|
|
static auto* libs = new std::vector<LLModel::Implementation>([] () {
|
|
|
|
std::vector<LLModel::Implementation> fres;
|
2023-05-31 17:04:01 -04:00
|
|
|
|
2023-06-05 09:23:17 -04:00
|
|
|
auto search_in_directory = [&](const std::string& paths) {
|
|
|
|
std::stringstream ss(paths);
|
|
|
|
std::string path;
|
|
|
|
// Split the paths string by the delimiter and process each path.
|
|
|
|
while (std::getline(ss, path, ';')) {
|
|
|
|
std::filesystem::path fs_path(path);
|
|
|
|
// Iterate over all libraries
|
|
|
|
for (const auto& f : std::filesystem::directory_iterator(fs_path)) {
|
|
|
|
const std::filesystem::path& p = f.path();
|
|
|
|
if (p.extension() != LIB_FILE_EXT) continue;
|
|
|
|
// Add to list if model implementation
|
|
|
|
try {
|
|
|
|
Dlhandle dl(p.string());
|
|
|
|
if (!Implementation::isImplementation(dl)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fres.emplace_back(Implementation(std::move(dl)));
|
|
|
|
} catch (...) {}
|
|
|
|
}
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-05 11:19:02 -04:00
|
|
|
search_in_directory(s_implementations_search_path);
|
2023-06-05 09:23:17 -04:00
|
|
|
|
2023-05-31 17:04:01 -04:00
|
|
|
return fres;
|
|
|
|
}());
|
2023-05-31 15:37:25 -04:00
|
|
|
// Return static result
|
|
|
|
return *libs;
|
|
|
|
}
|
|
|
|
|
2023-06-01 10:51:46 -04:00
|
|
|
const LLModel::Implementation* LLModel::implementation(std::ifstream& f, const std::string& buildVariant) {
|
|
|
|
for (const auto& i : implementationList()) {
|
2023-05-31 17:04:01 -04:00
|
|
|
f.seekg(0);
|
2023-06-01 10:51:46 -04:00
|
|
|
if (!i.magicMatch(f)) continue;
|
|
|
|
if (buildVariant != i.buildVariant) continue;
|
2023-05-31 15:37:25 -04:00
|
|
|
return &i;
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLModel *LLModel::construct(const std::string &modelPath, std::string buildVariant) {
|
2023-06-04 15:28:58 -04:00
|
|
|
|
|
|
|
if (!has_at_least_minimal_hardware())
|
|
|
|
return nullptr;
|
|
|
|
|
2023-05-31 17:04:01 -04:00
|
|
|
// Read magic
|
|
|
|
std::ifstream f(modelPath, std::ios::binary);
|
|
|
|
if (!f) return nullptr;
|
|
|
|
// Get correct implementation
|
2023-06-09 16:48:46 -04:00
|
|
|
const LLModel::Implementation* impl = nullptr;
|
|
|
|
|
|
|
|
#if defined(__APPLE__) && defined(__arm64__) // FIXME: See if metal works for intel macs
|
|
|
|
if (buildVariant == "auto") {
|
|
|
|
impl = implementation(f, "metal");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!impl) {
|
|
|
|
//TODO: Auto-detect CUDA/OpenCL
|
|
|
|
if (buildVariant == "auto") {
|
|
|
|
if (requires_avxonly()) {
|
|
|
|
buildVariant = "avxonly";
|
|
|
|
} else {
|
|
|
|
buildVariant = "default";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl = implementation(f, buildVariant);
|
|
|
|
if (!impl) return nullptr;
|
|
|
|
}
|
2023-05-31 17:04:01 -04:00
|
|
|
f.close();
|
2023-05-31 15:37:25 -04:00
|
|
|
// Construct and return llmodel implementation
|
|
|
|
return impl->construct();
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
2023-06-05 11:19:02 -04:00
|
|
|
|
|
|
|
void LLModel::setImplementationsSearchPath(const std::string& path) {
|
|
|
|
s_implementations_search_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& LLModel::implementationsSearchPath() {
|
|
|
|
return s_implementations_search_path;
|
|
|
|
}
|