2023-05-31 17:04:01 -04:00
|
|
|
#include "llmodel.h"
|
|
|
|
|
2024-05-31 16:34:54 -04:00
|
|
|
#include "dlhandle.h"
|
|
|
|
|
2023-05-31 15:37:25 -04:00
|
|
|
#include <cassert>
|
2023-06-01 11:41:04 -04:00
|
|
|
#include <cstdlib>
|
2024-01-31 14:17:44 -05:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2024-05-31 16:34:54 -04:00
|
|
|
#include <iterator>
|
2024-01-31 14:17:44 -05:00
|
|
|
#include <memory>
|
2024-04-25 13:16:52 -04:00
|
|
|
#include <optional>
|
2023-10-21 00:26:46 -04:00
|
|
|
#include <regex>
|
2024-01-31 14:17:44 -05:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2024-05-15 15:27:50 -04:00
|
|
|
#include <unordered_map>
|
2024-01-31 14:17:44 -05:00
|
|
|
#include <vector>
|
|
|
|
|
2024-05-16 17:39:49 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# ifndef NOMINMAX
|
|
|
|
# define NOMINMAX
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2023-07-12 11:44:08 -04:00
|
|
|
#ifdef _MSC_VER
|
2024-05-16 17:39:49 -04:00
|
|
|
# include <intrin.h>
|
2023-07-12 11:44:08 -04:00
|
|
|
#endif
|
2023-05-31 17:04:01 -04:00
|
|
|
|
2024-05-31 16:34:54 -04:00
|
|
|
#if defined(__APPLE__) && defined(__aarch64__)
|
|
|
|
# include "sysinfo.h" // for getSystemTotalRAMInBytes
|
|
|
|
#endif
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
2024-05-15 15:27:50 -04:00
|
|
|
#ifndef __APPLE__
|
|
|
|
static const std::string DEFAULT_BACKENDS[] = {"kompute", "cpu"};
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
static const std::string DEFAULT_BACKENDS[] = {"metal", "cpu"};
|
|
|
|
#else
|
|
|
|
static const std::string DEFAULT_BACKENDS[] = {"cpu"};
|
|
|
|
#endif
|
|
|
|
|
2023-06-05 11:19:02 -04:00
|
|
|
std::string s_implementations_search_path = ".";
|
2023-06-02 10:57:21 -04:00
|
|
|
|
2024-03-19 10:56:14 -04:00
|
|
|
#if !(defined(__x86_64__) || defined(_M_X64))
|
|
|
|
// irrelevant on non-x86_64
|
|
|
|
#define cpu_supports_avx() -1
|
|
|
|
#define cpu_supports_avx2() -1
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
// MSVC
|
|
|
|
static int get_cpu_info(int func_id, int reg_id) {
|
|
|
|
int info[4];
|
|
|
|
__cpuid(info, func_id);
|
|
|
|
return info[reg_id];
|
|
|
|
}
|
2023-06-04 15:28:58 -04:00
|
|
|
|
2024-03-19 10:56:14 -04:00
|
|
|
// AVX via EAX=1: Processor Info and Feature Bits, bit 28 of ECX
|
2024-05-02 16:09:41 -04:00
|
|
|
#define cpu_supports_avx() !!(get_cpu_info(1, 2) & (1 << 28))
|
2024-03-19 10:56:14 -04:00
|
|
|
// AVX2 via EAX=7, ECX=0: Extended Features, bit 5 of EBX
|
2024-05-02 16:09:41 -04:00
|
|
|
#define cpu_supports_avx2() !!(get_cpu_info(7, 1) & (1 << 5))
|
2023-05-31 21:26:18 -04:00
|
|
|
#else
|
2024-03-19 10:56:14 -04:00
|
|
|
// gcc/clang
|
2024-05-02 16:09:41 -04:00
|
|
|
#define cpu_supports_avx() !!__builtin_cpu_supports("avx")
|
|
|
|
#define cpu_supports_avx2() !!__builtin_cpu_supports("avx2")
|
2023-05-31 21:26:18 -04:00
|
|
|
#endif
|
|
|
|
|
2023-07-09 11:00:20 -04:00
|
|
|
LLModel::Implementation::Implementation(Dlhandle &&dlhandle_)
|
2023-07-08 10:04:38 -04:00
|
|
|
: m_dlhandle(new Dlhandle(std::move(dlhandle_))) {
|
|
|
|
auto get_model_type = m_dlhandle->get<const char *()>("get_model_type");
|
2023-05-31 15:37:25 -04:00
|
|
|
assert(get_model_type);
|
2023-07-08 10:04:38 -04:00
|
|
|
m_modelType = get_model_type();
|
|
|
|
auto get_build_variant = m_dlhandle->get<const char *()>("get_build_variant");
|
2023-05-31 15:37:25 -04:00
|
|
|
assert(get_build_variant);
|
2023-07-08 10:04:38 -04:00
|
|
|
m_buildVariant = get_build_variant();
|
2024-04-25 13:16:52 -04:00
|
|
|
m_getFileArch = m_dlhandle->get<char *(const char *)>("get_file_arch");
|
|
|
|
assert(m_getFileArch);
|
|
|
|
m_isArchSupported = m_dlhandle->get<bool(const char *)>("is_arch_supported");
|
|
|
|
assert(m_isArchSupported);
|
2023-07-08 10:04:38 -04:00
|
|
|
m_construct = m_dlhandle->get<LLModel *()>("construct");
|
2023-07-09 11:00:20 -04:00
|
|
|
assert(m_construct);
|
2023-05-31 15:37:25 -04:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:00:20 -04:00
|
|
|
LLModel::Implementation::Implementation(Implementation &&o)
|
2024-04-25 13:16:52 -04:00
|
|
|
: m_getFileArch(o.m_getFileArch)
|
|
|
|
, m_isArchSupported(o.m_isArchSupported)
|
2023-07-08 10:04:38 -04:00
|
|
|
, 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 11:16:33 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
LLModel::Implementation::~Implementation()
|
|
|
|
{
|
2024-03-19 10:56:14 -04:00
|
|
|
delete m_dlhandle;
|
2023-06-01 10:51:46 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
static bool isImplementation(const Dlhandle &dl)
|
|
|
|
{
|
2023-05-31 15:37:25 -04:00
|
|
|
return dl.get<bool(uint32_t)>("is_g4a_backend_model_implementation");
|
|
|
|
}
|
|
|
|
|
2024-05-16 17:39:49 -04:00
|
|
|
// Add the CUDA Toolkit to the DLL search path on Windows.
|
|
|
|
// This is necessary for chat.exe to find CUDA when started from Qt Creator.
|
2024-06-24 18:49:23 -04:00
|
|
|
static void addCudaSearchPath()
|
|
|
|
{
|
2024-05-16 17:39:49 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (const auto *cudaPath = _wgetenv(L"CUDA_PATH")) {
|
|
|
|
auto libDir = std::wstring(cudaPath) + L"\\bin";
|
|
|
|
if (!AddDllDirectory(libDir.c_str())) {
|
|
|
|
auto err = GetLastError();
|
|
|
|
std::wcerr << L"AddDllDirectory(\"" << libDir << L"\") failed with error 0x" << std::hex << err << L"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
const std::vector<LLModel::Implementation> &LLModel::Implementation::implementationList()
|
|
|
|
{
|
2024-03-19 10:56:14 -04:00
|
|
|
if (cpu_supports_avx() == 0) {
|
|
|
|
throw std::runtime_error("CPU does not support AVX");
|
|
|
|
}
|
|
|
|
|
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-07-09 11:00:20 -04:00
|
|
|
static auto* libs = new std::vector<Implementation>([] () {
|
|
|
|
std::vector<Implementation> fres;
|
2023-05-31 17:04:01 -04:00
|
|
|
|
2024-05-16 17:39:49 -04:00
|
|
|
addCudaSearchPath();
|
|
|
|
|
2024-05-15 15:27:50 -04:00
|
|
|
std::string impl_name_re = "(gptj|llamamodel-mainline)-(cpu|metal|kompute|vulkan|cuda)";
|
2024-03-19 10:56:14 -04:00
|
|
|
if (cpu_supports_avx2() == 0) {
|
2023-10-21 00:26:46 -04:00
|
|
|
impl_name_re += "-avxonly";
|
|
|
|
}
|
|
|
|
std::regex re(impl_name_re);
|
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, ';')) {
|
2024-05-31 13:12:28 -04:00
|
|
|
std::u8string u8_path(path.begin(), path.end());
|
2023-06-05 09:23:17 -04:00
|
|
|
// Iterate over all libraries
|
2024-05-31 13:12:28 -04:00
|
|
|
for (const auto &f : fs::directory_iterator(u8_path)) {
|
|
|
|
const fs::path &p = f.path();
|
2023-10-21 00:26:46 -04:00
|
|
|
|
2023-06-05 09:23:17 -04:00
|
|
|
if (p.extension() != LIB_FILE_EXT) continue;
|
2023-10-21 00:26:46 -04:00
|
|
|
if (!std::regex_search(p.stem().string(), re)) continue;
|
|
|
|
|
2023-06-05 09:23:17 -04:00
|
|
|
// Add to list if model implementation
|
2024-05-31 16:33:40 -04:00
|
|
|
Dlhandle dl;
|
2023-06-05 09:23:17 -04:00
|
|
|
try {
|
2024-05-31 16:33:40 -04:00
|
|
|
dl = Dlhandle(p);
|
|
|
|
} catch (const Dlhandle::Exception &e) {
|
|
|
|
std::cerr << "Failed to load " << p.filename().string() << ": " << e.what() << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isImplementation(dl)) {
|
|
|
|
std::cerr << "Not an implementation: " << p.filename().string() << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fres.emplace_back(Implementation(std::move(dl)));
|
2023-06-05 09:23:17 -04:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
static std::string applyCPUVariant(const std::string &buildVariant)
|
|
|
|
{
|
2024-05-15 15:27:50 -04:00
|
|
|
if (buildVariant != "metal" && cpu_supports_avx2() == 0) {
|
|
|
|
return buildVariant + "-avxonly";
|
|
|
|
}
|
|
|
|
return buildVariant;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
const LLModel::Implementation* LLModel::Implementation::implementation(const char *fname, const std::string& buildVariant)
|
|
|
|
{
|
2023-11-07 11:20:14 -05:00
|
|
|
bool buildVariantMatched = false;
|
2024-04-25 13:16:52 -04:00
|
|
|
std::optional<std::string> archName;
|
2023-06-01 10:51:46 -04:00
|
|
|
for (const auto& i : implementationList()) {
|
2023-07-08 10:04:38 -04:00
|
|
|
if (buildVariant != i.m_buildVariant) continue;
|
2023-11-07 11:20:14 -05:00
|
|
|
buildVariantMatched = true;
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
char *arch = i.m_getFileArch(fname);
|
|
|
|
if (!arch) continue;
|
|
|
|
archName = arch;
|
|
|
|
|
|
|
|
bool archSupported = i.m_isArchSupported(arch);
|
|
|
|
free(arch);
|
|
|
|
if (archSupported) return &i;
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
2023-11-07 11:20:14 -05:00
|
|
|
|
2024-03-19 10:56:14 -04:00
|
|
|
if (!buildVariantMatched)
|
2024-05-15 15:27:50 -04:00
|
|
|
return nullptr;
|
2024-04-25 13:16:52 -04:00
|
|
|
if (!archName)
|
|
|
|
throw UnsupportedModelError("Unsupported file format");
|
2024-03-19 10:56:14 -04:00
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
throw BadArchError(std::move(*archName));
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
LLModel *LLModel::Implementation::construct(const std::string &modelPath, const std::string &backend, int n_ctx)
|
|
|
|
{
|
2024-05-15 15:27:50 -04:00
|
|
|
std::vector<std::string> desiredBackends;
|
|
|
|
if (backend != "auto") {
|
|
|
|
desiredBackends.push_back(backend);
|
|
|
|
} else {
|
|
|
|
desiredBackends.insert(desiredBackends.end(), DEFAULT_BACKENDS, std::end(DEFAULT_BACKENDS));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &desiredBackend: desiredBackends) {
|
|
|
|
const auto *impl = implementation(modelPath.c_str(), applyCPUVariant(desiredBackend));
|
|
|
|
|
|
|
|
if (impl) {
|
|
|
|
// Construct llmodel implementation
|
|
|
|
auto *fres = impl->m_construct();
|
|
|
|
fres->m_implementation = impl;
|
|
|
|
|
|
|
|
#if defined(__APPLE__) && defined(__aarch64__) // FIXME: See if metal works for intel macs
|
|
|
|
/* 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. */
|
|
|
|
if (backend == "auto" && desiredBackend == "metal") {
|
2023-06-26 17:53:17 -04:00
|
|
|
// on a 16GB M2 Mac a 13B q4_0 (0.52) works for me but a 13B q4_K_M (0.55) does not
|
2024-05-15 15:27:50 -04:00
|
|
|
size_t req_mem = fres->requiredMem(modelPath, n_ctx, 100);
|
|
|
|
if (req_mem >= size_t(0.53f * getSystemTotalRAMInBytes())) {
|
|
|
|
delete fres;
|
|
|
|
continue;
|
2023-06-26 17:53:17 -04:00
|
|
|
}
|
|
|
|
}
|
2024-05-15 15:27:50 -04:00
|
|
|
#else
|
|
|
|
(void)n_ctx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return fres;
|
2023-06-09 16:48:46 -04:00
|
|
|
}
|
|
|
|
}
|
2023-07-08 10:04:38 -04:00
|
|
|
|
2024-05-15 15:27:50 -04:00
|
|
|
throw MissingImplementationError("Could not find any implementations for backend: " + backend);
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
2023-06-05 11:19:02 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
LLModel *LLModel::Implementation::constructGlobalLlama(const std::optional<std::string> &backend)
|
|
|
|
{
|
2024-05-15 15:27:50 -04:00
|
|
|
static std::unordered_map<std::string, std::unique_ptr<LLModel>> implCache;
|
|
|
|
|
|
|
|
const std::vector<Implementation> *impls;
|
|
|
|
try {
|
|
|
|
impls = &implementationList();
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
std::cerr << __func__ << ": implementationList failed: " << e.what() << "\n";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> desiredBackends;
|
|
|
|
if (backend) {
|
|
|
|
desiredBackends.push_back(backend.value());
|
|
|
|
} else {
|
|
|
|
desiredBackends.insert(desiredBackends.end(), DEFAULT_BACKENDS, std::end(DEFAULT_BACKENDS));
|
|
|
|
}
|
|
|
|
|
|
|
|
const Implementation *impl = nullptr;
|
|
|
|
|
|
|
|
for (const auto &desiredBackend: desiredBackends) {
|
|
|
|
auto cacheIt = implCache.find(desiredBackend);
|
|
|
|
if (cacheIt != implCache.end())
|
|
|
|
return cacheIt->second.get(); // cached
|
2024-03-19 10:56:14 -04:00
|
|
|
|
|
|
|
for (const auto &i: *impls) {
|
2024-05-15 15:27:50 -04:00
|
|
|
if (i.m_modelType == "LLaMA" && i.m_buildVariant == applyCPUVariant(desiredBackend)) {
|
|
|
|
impl = &i;
|
|
|
|
break;
|
|
|
|
}
|
2024-01-31 14:17:44 -05:00
|
|
|
}
|
2024-05-15 15:27:50 -04:00
|
|
|
|
|
|
|
if (impl) {
|
|
|
|
auto *fres = impl->m_construct();
|
|
|
|
fres->m_implementation = impl;
|
|
|
|
implCache[desiredBackend] = std::unique_ptr<LLModel>(fres);
|
|
|
|
return fres;
|
2024-01-31 14:17:44 -05:00
|
|
|
}
|
2024-05-15 15:27:50 -04:00
|
|
|
}
|
2024-03-19 10:56:14 -04:00
|
|
|
|
2024-05-15 15:27:50 -04:00
|
|
|
std::cerr << __func__ << ": could not find Llama implementation for backend: " << backend.value_or("default") << "\n";
|
|
|
|
return nullptr;
|
2023-12-13 12:11:09 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
std::vector<LLModel::GPUDevice> LLModel::Implementation::availableGPUDevices(size_t memoryRequired)
|
|
|
|
{
|
2024-05-15 15:27:50 -04:00
|
|
|
std::vector<LLModel::GPUDevice> devices;
|
|
|
|
#ifndef __APPLE__
|
|
|
|
static const std::string backends[] = {"kompute", "cuda"};
|
|
|
|
for (const auto &backend: backends) {
|
|
|
|
auto *llama = constructGlobalLlama(backend);
|
|
|
|
if (llama) {
|
|
|
|
auto backendDevs = llama->availableGPUDevices(memoryRequired);
|
|
|
|
devices.insert(devices.end(), backendDevs.begin(), backendDevs.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return devices;
|
2023-12-13 12:11:09 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
int32_t LLModel::Implementation::maxContextLength(const std::string &modelPath)
|
|
|
|
{
|
2024-05-15 15:27:50 -04:00
|
|
|
auto *llama = constructGlobalLlama();
|
2024-01-31 14:17:44 -05:00
|
|
|
return llama ? llama->maxContextLength(modelPath) : -1;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
int32_t LLModel::Implementation::layerCount(const std::string &modelPath)
|
|
|
|
{
|
2024-05-15 15:27:50 -04:00
|
|
|
auto *llama = constructGlobalLlama();
|
2024-01-31 14:17:44 -05:00
|
|
|
return llama ? llama->layerCount(modelPath) : -1;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
bool LLModel::Implementation::isEmbeddingModel(const std::string &modelPath)
|
|
|
|
{
|
2024-05-15 15:27:50 -04:00
|
|
|
auto *llama = constructGlobalLlama();
|
2024-03-13 18:09:24 -04:00
|
|
|
return llama && llama->isEmbeddingModel(modelPath);
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void LLModel::Implementation::setImplementationsSearchPath(const std::string& path)
|
|
|
|
{
|
2023-06-05 11:19:02 -04:00
|
|
|
s_implementations_search_path = path;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
const std::string& LLModel::Implementation::implementationsSearchPath()
|
|
|
|
{
|
2023-06-05 11:19:02 -04:00
|
|
|
return s_implementations_search_path;
|
|
|
|
}
|
2024-03-19 10:56:14 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
bool LLModel::Implementation::hasSupportedCPU()
|
|
|
|
{
|
2024-03-19 10:56:14 -04:00
|
|
|
return cpu_supports_avx() != 0;
|
|
|
|
}
|
2024-05-02 16:09:41 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
int LLModel::Implementation::cpuSupportsAVX2()
|
|
|
|
{
|
2024-05-02 16:09:41 -04:00
|
|
|
return cpu_supports_avx2();
|
|
|
|
}
|