2023-05-31 21:04:01 +00:00
|
|
|
#include "llmodel.h"
|
2023-06-01 14:51:46 +00:00
|
|
|
#include "dlhandle.h"
|
2023-06-26 21:53:17 +00:00
|
|
|
#include "sysinfo.h"
|
2023-05-31 21:04:01 +00:00
|
|
|
|
2023-05-31 19:37:25 +00:00
|
|
|
#include <cassert>
|
2023-06-01 15:41:04 +00:00
|
|
|
#include <cstdlib>
|
2024-01-31 19:17:44 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2023-10-21 04:26:46 +00:00
|
|
|
#include <regex>
|
2024-01-31 19:17:44 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-07-12 15:44:08 +00:00
|
|
|
#ifdef _MSC_VER
|
2023-08-07 22:17:41 +00:00
|
|
|
#include <intrin.h>
|
2023-07-12 15:44:08 +00:00
|
|
|
#endif
|
2023-05-31 21:04:01 +00:00
|
|
|
|
2023-06-05 15:19:02 +00:00
|
|
|
std::string s_implementations_search_path = ".";
|
2023-06-02 14:57:21 +00:00
|
|
|
|
2023-06-04 19:28:58 +00:00
|
|
|
static bool has_at_least_minimal_hardware() {
|
2023-07-13 18:44:17 +00:00
|
|
|
#if defined(__x86_64__) || defined(_M_X64)
|
2023-06-04 19:28:58 +00:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
return __builtin_cpu_supports("avx");
|
|
|
|
#else
|
2023-08-07 22:17:41 +00:00
|
|
|
int cpuInfo[4];
|
|
|
|
__cpuid(cpuInfo, 1);
|
|
|
|
return cpuInfo[2] & (1 << 28);
|
2023-06-04 19:28:58 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
return true; // Don't know how to handle non-x86_64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-01 11:57:10 +00:00
|
|
|
static bool requires_avxonly() {
|
2023-07-13 18:44:17 +00:00
|
|
|
#if defined(__x86_64__) || defined(_M_X64)
|
2023-06-01 01:26:18 +00:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
return !__builtin_cpu_supports("avx2");
|
|
|
|
#else
|
2023-08-07 22:17:41 +00:00
|
|
|
int cpuInfo[4];
|
|
|
|
__cpuidex(cpuInfo, 7, 0);
|
|
|
|
return !(cpuInfo[1] & (1 << 5));
|
2023-06-01 01:26:18 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
return false; // Don't know how to handle non-x86_64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
LLModel::Implementation::Implementation(Dlhandle &&dlhandle_)
|
2023-07-08 14:04:38 +00:00
|
|
|
: m_dlhandle(new Dlhandle(std::move(dlhandle_))) {
|
|
|
|
auto get_model_type = m_dlhandle->get<const char *()>("get_model_type");
|
2023-05-31 19:37:25 +00:00
|
|
|
assert(get_model_type);
|
2023-07-08 14:04:38 +00:00
|
|
|
m_modelType = get_model_type();
|
|
|
|
auto get_build_variant = m_dlhandle->get<const char *()>("get_build_variant");
|
2023-05-31 19:37:25 +00:00
|
|
|
assert(get_build_variant);
|
2023-07-08 14:04:38 +00:00
|
|
|
m_buildVariant = get_build_variant();
|
2023-09-21 16:41:48 +00:00
|
|
|
m_magicMatch = m_dlhandle->get<bool(const char*)>("magic_match");
|
2023-07-09 15:00:20 +00:00
|
|
|
assert(m_magicMatch);
|
2023-07-08 14:04:38 +00:00
|
|
|
m_construct = m_dlhandle->get<LLModel *()>("construct");
|
2023-07-09 15:00:20 +00:00
|
|
|
assert(m_construct);
|
2023-05-31 19:37:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
LLModel::Implementation::Implementation(Implementation &&o)
|
2023-07-08 14:04:38 +00:00
|
|
|
: m_magicMatch(o.m_magicMatch)
|
|
|
|
, m_construct(o.m_construct)
|
|
|
|
, m_modelType(o.m_modelType)
|
|
|
|
, m_buildVariant(o.m_buildVariant)
|
|
|
|
, m_dlhandle(o.m_dlhandle) {
|
|
|
|
o.m_dlhandle = nullptr;
|
2023-06-01 15:16:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
LLModel::Implementation::~Implementation() {
|
2023-07-08 14:04:38 +00:00
|
|
|
if (m_dlhandle) delete m_dlhandle;
|
2023-06-01 14:51:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
bool LLModel::Implementation::isImplementation(const Dlhandle &dl) {
|
2023-05-31 19:37:25 +00:00
|
|
|
return dl.get<bool(uint32_t)>("is_g4a_backend_model_implementation");
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
const std::vector<LLModel::Implementation> &LLModel::Implementation::implementationList() {
|
2023-06-01 11:57:10 +00: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-07-09 15:00:20 +00:00
|
|
|
static auto* libs = new std::vector<Implementation>([] () {
|
|
|
|
std::vector<Implementation> fres;
|
2023-05-31 21:04:01 +00:00
|
|
|
|
2023-12-13 17:11:09 +00:00
|
|
|
std::string impl_name_re = "(bert|gptj|llamamodel-mainline)";
|
2023-10-21 04:26:46 +00:00
|
|
|
if (requires_avxonly()) {
|
|
|
|
impl_name_re += "-avxonly";
|
|
|
|
} else {
|
|
|
|
impl_name_re += "-(default|metal)";
|
|
|
|
}
|
|
|
|
std::regex re(impl_name_re);
|
2023-06-05 13:23:17 +00: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();
|
2023-10-21 04:26:46 +00:00
|
|
|
|
2023-06-05 13:23:17 +00:00
|
|
|
if (p.extension() != LIB_FILE_EXT) continue;
|
2023-10-21 04:26:46 +00:00
|
|
|
if (!std::regex_search(p.stem().string(), re)) continue;
|
|
|
|
|
2023-06-05 13:23:17 +00:00
|
|
|
// Add to list if model implementation
|
|
|
|
try {
|
2024-01-03 16:31:06 +00:00
|
|
|
Dlhandle dl(p.string());
|
2023-07-09 15:00:20 +00:00
|
|
|
if (!Implementation::isImplementation(dl)) {
|
2023-06-05 13:23:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-07-09 15:00:20 +00:00
|
|
|
fres.emplace_back(Implementation(std::move(dl)));
|
2023-06-05 13:23:17 +00:00
|
|
|
} catch (...) {}
|
|
|
|
}
|
2023-05-31 21:04:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-05 15:19:02 +00:00
|
|
|
search_in_directory(s_implementations_search_path);
|
2023-06-05 13:23:17 +00:00
|
|
|
|
2023-05-31 21:04:01 +00:00
|
|
|
return fres;
|
|
|
|
}());
|
2023-05-31 19:37:25 +00:00
|
|
|
// Return static result
|
|
|
|
return *libs;
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:41:48 +00:00
|
|
|
const LLModel::Implementation* LLModel::Implementation::implementation(const char *fname, const std::string& buildVariant) {
|
2023-11-07 16:20:14 +00:00
|
|
|
bool buildVariantMatched = false;
|
2023-06-01 14:51:46 +00:00
|
|
|
for (const auto& i : implementationList()) {
|
2023-07-08 14:04:38 +00:00
|
|
|
if (buildVariant != i.m_buildVariant) continue;
|
2023-11-07 16:20:14 +00:00
|
|
|
buildVariantMatched = true;
|
|
|
|
|
2023-10-11 15:30:48 +00:00
|
|
|
if (!i.m_magicMatch(fname)) continue;
|
2023-05-31 19:37:25 +00:00
|
|
|
return &i;
|
2023-05-31 21:04:01 +00:00
|
|
|
}
|
2023-11-07 16:20:14 +00:00
|
|
|
|
|
|
|
if (!buildVariantMatched) {
|
|
|
|
std::cerr << "LLModel ERROR: Could not find any implementations for build variant: " << buildVariant << "\n";
|
|
|
|
}
|
2023-05-31 21:04:01 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-12-16 22:58:15 +00:00
|
|
|
LLModel *LLModel::Implementation::construct(const std::string &modelPath, std::string buildVariant, int n_ctx) {
|
2023-10-11 19:09:40 +00:00
|
|
|
if (!has_at_least_minimal_hardware()) {
|
|
|
|
std::cerr << "LLModel ERROR: CPU does not support AVX\n";
|
2023-06-04 19:28:58 +00:00
|
|
|
return nullptr;
|
2023-10-11 19:09:40 +00:00
|
|
|
}
|
2023-06-04 19:28:58 +00:00
|
|
|
|
2023-05-31 21:04:01 +00:00
|
|
|
// Get correct implementation
|
2023-07-09 15:00:20 +00:00
|
|
|
const Implementation* impl = nullptr;
|
2023-06-09 20:48:46 +00:00
|
|
|
|
|
|
|
#if defined(__APPLE__) && defined(__arm64__) // FIXME: See if metal works for intel macs
|
|
|
|
if (buildVariant == "auto") {
|
2023-06-26 21:53:17 +00:00
|
|
|
size_t total_mem = getSystemTotalRAMInBytes();
|
2023-09-26 18:38:02 +00:00
|
|
|
impl = implementation(modelPath.c_str(), "metal");
|
2023-06-26 21:53:17 +00:00
|
|
|
if(impl) {
|
2023-07-09 15:00:20 +00:00
|
|
|
LLModel* metalimpl = impl->m_construct();
|
|
|
|
metalimpl->m_implementation = impl;
|
2023-12-16 22:58:15 +00:00
|
|
|
/* TODO(cebtenzzre): after we fix requiredMem, we should change this to happen at
|
|
|
|
* load time, not construct time. right now n_ctx is incorrectly hardcoded 2048 in
|
|
|
|
* most (all?) places where this is called, causing underestimation of required
|
|
|
|
* memory. */
|
2024-01-31 19:17:44 +00:00
|
|
|
size_t req_mem = metalimpl->requiredMem(modelPath, n_ctx, 100);
|
2023-06-26 21:53:17 +00:00
|
|
|
float req_to_total = (float) req_mem / (float) total_mem;
|
|
|
|
// on a 16GB M2 Mac a 13B q4_0 (0.52) works for me but a 13B q4_K_M (0.55) does not
|
|
|
|
if (req_to_total >= 0.53) {
|
|
|
|
delete metalimpl;
|
|
|
|
impl = nullptr;
|
|
|
|
} else {
|
|
|
|
return metalimpl;
|
|
|
|
}
|
|
|
|
}
|
2023-06-09 20:48:46 +00:00
|
|
|
}
|
2023-12-16 22:58:15 +00:00
|
|
|
#else
|
|
|
|
(void)n_ctx;
|
2023-06-09 20:48:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!impl) {
|
|
|
|
//TODO: Auto-detect CUDA/OpenCL
|
|
|
|
if (buildVariant == "auto") {
|
|
|
|
if (requires_avxonly()) {
|
|
|
|
buildVariant = "avxonly";
|
|
|
|
} else {
|
|
|
|
buildVariant = "default";
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 16:41:48 +00:00
|
|
|
impl = implementation(modelPath.c_str(), buildVariant);
|
2023-06-09 20:48:46 +00:00
|
|
|
if (!impl) return nullptr;
|
|
|
|
}
|
2023-07-08 14:04:38 +00:00
|
|
|
|
2023-05-31 19:37:25 +00:00
|
|
|
// Construct and return llmodel implementation
|
2023-07-08 14:04:38 +00:00
|
|
|
auto fres = impl->m_construct();
|
|
|
|
fres->m_implementation = impl;
|
|
|
|
return fres;
|
2023-05-31 21:04:01 +00:00
|
|
|
}
|
2023-06-05 15:19:02 +00:00
|
|
|
|
2024-01-11 17:02:39 +00:00
|
|
|
LLModel *LLModel::Implementation::constructDefaultLlama() {
|
2024-01-31 19:17:44 +00:00
|
|
|
static std::unique_ptr<LLModel> llama([]() -> LLModel * {
|
|
|
|
const LLModel::Implementation *impl = nullptr;
|
|
|
|
for (const auto &i : implementationList()) {
|
|
|
|
if (i.m_buildVariant == "metal" || i.m_modelType != "LLaMA") continue;
|
|
|
|
impl = &i;
|
|
|
|
}
|
|
|
|
if (!impl) {
|
|
|
|
std::cerr << "LLModel ERROR: Could not find CPU LLaMA implementation\n";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto fres = impl->m_construct();
|
|
|
|
fres->m_implementation = impl;
|
|
|
|
return fres;
|
|
|
|
}());
|
|
|
|
return llama.get();
|
2023-12-13 17:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<LLModel::GPUDevice> LLModel::Implementation::availableGPUDevices() {
|
2024-01-31 19:17:44 +00:00
|
|
|
auto * llama = constructDefaultLlama();
|
2024-01-11 17:02:39 +00:00
|
|
|
if (llama) { return llama->availableGPUDevices(0); }
|
2023-12-13 17:11:09 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-01-31 19:17:44 +00:00
|
|
|
int32_t LLModel::Implementation::maxContextLength(const std::string &modelPath) {
|
|
|
|
auto * llama = constructDefaultLlama();
|
|
|
|
return llama ? llama->maxContextLength(modelPath) : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t LLModel::Implementation::layerCount(const std::string &modelPath) {
|
|
|
|
auto * llama = constructDefaultLlama();
|
|
|
|
return llama ? llama->layerCount(modelPath) : -1;
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
void LLModel::Implementation::setImplementationsSearchPath(const std::string& path) {
|
2023-06-05 15:19:02 +00:00
|
|
|
s_implementations_search_path = path;
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
const std::string& LLModel::Implementation::implementationsSearchPath() {
|
2023-06-05 15:19:02 +00:00
|
|
|
return s_implementations_search_path;
|
|
|
|
}
|