2023-04-14 02:15:40 +00:00
|
|
|
#ifndef LLMODEL_H
|
|
|
|
#define LLMODEL_H
|
2023-06-01 11:57:10 +00:00
|
|
|
|
2024-05-15 19:27:50 +00:00
|
|
|
#include <algorithm>
|
2024-06-26 19:26:27 +00:00
|
|
|
#include <cassert>
|
2024-05-31 20:34:54 +00:00
|
|
|
#include <cstddef>
|
2023-05-05 00:01:32 +00:00
|
|
|
#include <cstdint>
|
2024-03-13 22:09:24 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <optional>
|
2024-05-31 20:34:54 +00:00
|
|
|
#include <stdexcept>
|
2024-03-13 22:09:24 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2024-05-15 19:27:50 +00:00
|
|
|
#include <unordered_map>
|
2024-05-31 20:34:54 +00:00
|
|
|
#include <utility>
|
2024-03-13 22:09:24 +00:00
|
|
|
#include <vector>
|
2023-04-14 02:15:40 +00:00
|
|
|
|
2024-05-15 19:27:50 +00:00
|
|
|
using namespace std::string_literals;
|
|
|
|
|
2023-06-30 23:13:25 +00:00
|
|
|
#define LLMODEL_MAX_PROMPT_BATCH 128
|
|
|
|
|
2023-06-01 14:51:46 +00:00
|
|
|
class Dlhandle;
|
2023-04-14 02:15:40 +00:00
|
|
|
class LLModel {
|
|
|
|
public:
|
2023-06-04 12:59:24 +00:00
|
|
|
using Token = int32_t;
|
2023-12-13 17:11:09 +00:00
|
|
|
|
2024-04-25 17:16:52 +00:00
|
|
|
class BadArchError: public std::runtime_error {
|
|
|
|
public:
|
|
|
|
BadArchError(std::string arch)
|
|
|
|
: runtime_error("Unsupported model architecture: " + arch)
|
|
|
|
, m_arch(std::move(arch))
|
|
|
|
{}
|
|
|
|
|
|
|
|
const std::string &arch() const noexcept { return m_arch; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_arch;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MissingImplementationError: public std::runtime_error {
|
|
|
|
public:
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UnsupportedModelError: public std::runtime_error {
|
|
|
|
public:
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2023-12-13 17:11:09 +00:00
|
|
|
struct GPUDevice {
|
2024-05-15 19:27:50 +00:00
|
|
|
const char *backend;
|
2024-01-25 21:58:46 +00:00
|
|
|
int index;
|
|
|
|
int type;
|
|
|
|
size_t heapSize;
|
2023-12-13 17:11:09 +00:00
|
|
|
std::string name;
|
|
|
|
std::string vendor;
|
2024-01-25 21:58:46 +00:00
|
|
|
|
2024-05-15 19:27:50 +00:00
|
|
|
GPUDevice(const char *backend, int index, int type, size_t heapSize, std::string name, std::string vendor):
|
|
|
|
backend(backend), index(index), type(type), heapSize(heapSize), name(std::move(name)),
|
|
|
|
vendor(std::move(vendor)) {}
|
|
|
|
|
2024-06-26 19:26:27 +00:00
|
|
|
std::string selectionName() const
|
|
|
|
{
|
|
|
|
assert(backend == "cuda"s || backend == "kompute"s);
|
|
|
|
return backendName() + ": " + name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string backendName() const { return backendIdToName(backend); }
|
|
|
|
|
|
|
|
static std::string backendIdToName(const std::string &backend) { return s_backendNames.at(backend); }
|
2024-05-15 19:27:50 +00:00
|
|
|
|
|
|
|
static std::string updateSelectionName(const std::string &name) {
|
|
|
|
if (name == "Auto" || name == "CPU" || name == "Metal")
|
|
|
|
return name;
|
2024-06-26 19:26:27 +00:00
|
|
|
auto it = std::find_if(s_backendNames.begin(), s_backendNames.end(), [&name](const auto &entry) {
|
2024-05-15 19:27:50 +00:00
|
|
|
return name.starts_with(entry.second + ": ");
|
|
|
|
});
|
2024-06-26 19:26:27 +00:00
|
|
|
if (it != s_backendNames.end())
|
2024-05-15 19:27:50 +00:00
|
|
|
return name;
|
|
|
|
return "Vulkan: " + name; // previously, there were only Vulkan devices
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-06-26 19:26:27 +00:00
|
|
|
static inline const std::unordered_map<std::string, std::string> s_backendNames {
|
|
|
|
{"cpu", "CPU"}, {"metal", "Metal"}, {"cuda", "CUDA"}, {"kompute", "Vulkan"},
|
2024-05-15 19:27:50 +00:00
|
|
|
};
|
2023-12-13 17:11:09 +00:00
|
|
|
};
|
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
class Implementation {
|
|
|
|
public:
|
2024-02-21 20:45:32 +00:00
|
|
|
Implementation(const Implementation &) = delete;
|
|
|
|
Implementation(Implementation &&);
|
2023-07-09 15:00:20 +00:00
|
|
|
~Implementation();
|
|
|
|
|
|
|
|
std::string_view modelType() const { return m_modelType; }
|
|
|
|
std::string_view buildVariant() const { return m_buildVariant; }
|
|
|
|
|
2024-05-15 19:27:50 +00:00
|
|
|
static LLModel *construct(const std::string &modelPath, const std::string &backend = "auto", int n_ctx = 2048);
|
2024-04-04 18:52:13 +00:00
|
|
|
static std::vector<GPUDevice> availableGPUDevices(size_t memoryRequired = 0);
|
2024-01-31 19:17:44 +00:00
|
|
|
static int32_t maxContextLength(const std::string &modelPath);
|
|
|
|
static int32_t layerCount(const std::string &modelPath);
|
2024-03-13 22:09:24 +00:00
|
|
|
static bool isEmbeddingModel(const std::string &modelPath);
|
2024-02-21 20:45:32 +00:00
|
|
|
static void setImplementationsSearchPath(const std::string &path);
|
|
|
|
static const std::string &implementationsSearchPath();
|
2024-03-19 14:56:14 +00:00
|
|
|
static bool hasSupportedCPU();
|
2024-05-02 20:09:41 +00:00
|
|
|
// 0 for no, 1 for yes, -1 for non-x86_64
|
|
|
|
static int cpuSupportsAVX2();
|
2023-07-09 15:00:20 +00:00
|
|
|
|
|
|
|
private:
|
2024-03-19 14:56:14 +00:00
|
|
|
Implementation(Dlhandle &&);
|
|
|
|
|
|
|
|
static const std::vector<Implementation> &implementationList();
|
|
|
|
static const Implementation *implementation(const char *fname, const std::string &buildVariant);
|
2024-05-15 19:27:50 +00:00
|
|
|
static LLModel *constructGlobalLlama(const std::optional<std::string> &backend = std::nullopt);
|
2023-12-13 17:11:09 +00:00
|
|
|
|
2024-04-25 17:16:52 +00:00
|
|
|
char *(*m_getFileArch)(const char *fname);
|
|
|
|
bool (*m_isArchSupported)(const char *arch);
|
2023-07-09 15:00:20 +00:00
|
|
|
LLModel *(*m_construct)();
|
|
|
|
|
|
|
|
std::string_view m_modelType;
|
|
|
|
std::string_view m_buildVariant;
|
|
|
|
Dlhandle *m_dlhandle;
|
|
|
|
};
|
2023-06-04 12:59:24 +00:00
|
|
|
|
2023-04-14 02:15:40 +00:00
|
|
|
struct PromptContext {
|
2023-04-25 12:38:29 +00:00
|
|
|
std::vector<int32_t> tokens; // current tokens in the context window
|
|
|
|
int32_t n_past = 0; // number of tokens in past conversation
|
|
|
|
int32_t n_ctx = 0; // number of tokens possible in context window
|
|
|
|
int32_t n_predict = 200;
|
|
|
|
int32_t top_k = 40;
|
|
|
|
float top_p = 0.9f;
|
2024-02-24 22:51:34 +00:00
|
|
|
float min_p = 0.0f;
|
2023-04-25 12:38:29 +00:00
|
|
|
float temp = 0.9f;
|
|
|
|
int32_t n_batch = 9;
|
|
|
|
float repeat_penalty = 1.10f;
|
|
|
|
int32_t repeat_last_n = 64; // last n tokens to penalize
|
2023-12-01 21:51:15 +00:00
|
|
|
float contextErase = 0.75f; // percent of context to erase if we exceed the context window
|
|
|
|
int32_t n_last_batch_tokens = 0;
|
2023-04-14 02:15:40 +00:00
|
|
|
};
|
2023-05-31 19:37:25 +00:00
|
|
|
|
2024-02-07 14:37:59 +00:00
|
|
|
using ProgressCallback = std::function<bool(float progress)>;
|
|
|
|
|
2023-05-31 19:37:25 +00:00
|
|
|
explicit LLModel() {}
|
|
|
|
virtual ~LLModel() {}
|
|
|
|
|
2023-07-09 15:32:51 +00:00
|
|
|
virtual bool supportsEmbedding() const = 0;
|
|
|
|
virtual bool supportsCompletion() const = 0;
|
2024-01-31 19:17:44 +00:00
|
|
|
virtual bool loadModel(const std::string &modelPath, int n_ctx, int ngl) = 0;
|
2024-03-13 22:09:24 +00:00
|
|
|
virtual bool isModelBlacklisted(const std::string &modelPath) const { (void)modelPath; return false; };
|
|
|
|
virtual bool isEmbeddingModel(const std::string &modelPath) const { (void)modelPath; return false; }
|
2023-05-31 19:37:25 +00:00
|
|
|
virtual bool isModelLoaded() const = 0;
|
2024-01-31 19:17:44 +00:00
|
|
|
virtual size_t requiredMem(const std::string &modelPath, int n_ctx, int ngl) = 0;
|
2023-05-31 19:37:25 +00:00
|
|
|
virtual size_t stateSize() const { return 0; }
|
2024-02-21 20:45:32 +00:00
|
|
|
virtual size_t saveState(uint8_t *dest) const { (void)dest; return 0; }
|
|
|
|
virtual size_t restoreState(const uint8_t *src) { (void)src; return 0; }
|
2023-07-09 15:32:51 +00:00
|
|
|
|
|
|
|
// This method requires the model to return true from supportsCompletion otherwise it will throw
|
|
|
|
// an error
|
2023-04-25 12:38:29 +00:00
|
|
|
virtual void prompt(const std::string &prompt,
|
2024-02-21 20:45:32 +00:00
|
|
|
const std::string &promptTemplate,
|
2023-06-04 12:59:24 +00:00
|
|
|
std::function<bool(int32_t)> promptCallback,
|
|
|
|
std::function<bool(int32_t, const std::string&)> responseCallback,
|
|
|
|
std::function<bool(bool)> recalculateCallback,
|
2024-02-21 20:45:32 +00:00
|
|
|
PromptContext &ctx,
|
|
|
|
bool special = false,
|
|
|
|
std::string *fakeReply = nullptr);
|
2023-06-04 12:59:24 +00:00
|
|
|
|
2024-04-12 20:00:39 +00:00
|
|
|
using EmbedCancelCallback = bool(unsigned *batchSizes, unsigned nBatch, const char *backend);
|
|
|
|
|
2024-03-13 22:09:24 +00:00
|
|
|
virtual size_t embeddingSize() const {
|
|
|
|
throw std::logic_error(std::string(implementation().modelType()) + " does not support embeddings");
|
|
|
|
}
|
|
|
|
// user-specified prefix
|
|
|
|
virtual void embed(const std::vector<std::string> &texts, float *embeddings, std::optional<std::string> prefix,
|
2024-04-12 20:00:39 +00:00
|
|
|
int dimensionality = -1, size_t *tokenCount = nullptr, bool doMean = true, bool atlas = false,
|
|
|
|
EmbedCancelCallback *cancelCb = nullptr);
|
2024-03-13 22:09:24 +00:00
|
|
|
// automatic prefix
|
|
|
|
virtual void embed(const std::vector<std::string> &texts, float *embeddings, bool isRetrieval,
|
2024-03-20 15:24:02 +00:00
|
|
|
int dimensionality = -1, size_t *tokenCount = nullptr, bool doMean = true, bool atlas = false);
|
2023-07-09 15:32:51 +00:00
|
|
|
|
2024-02-21 20:45:32 +00:00
|
|
|
virtual void setThreadCount(int32_t n_threads) { (void)n_threads; }
|
2023-05-21 20:45:29 +00:00
|
|
|
virtual int32_t threadCount() const { return 1; }
|
2023-04-25 15:20:51 +00:00
|
|
|
|
2024-02-21 20:45:32 +00:00
|
|
|
const Implementation &implementation() const {
|
2023-06-01 14:51:46 +00:00
|
|
|
return *m_implementation;
|
2023-05-31 21:04:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 19:17:44 +00:00
|
|
|
virtual std::vector<GPUDevice> availableGPUDevices(size_t memoryRequired) const {
|
2024-01-25 21:58:46 +00:00
|
|
|
(void)memoryRequired;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-02-21 20:45:32 +00:00
|
|
|
virtual bool initializeGPUDevice(size_t memoryRequired, const std::string &name) const {
|
2024-01-25 21:58:46 +00:00
|
|
|
(void)memoryRequired;
|
|
|
|
(void)name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-31 19:17:44 +00:00
|
|
|
virtual bool initializeGPUDevice(int device, std::string *unavail_reason = nullptr) const {
|
2024-01-25 21:58:46 +00:00
|
|
|
(void)device;
|
2023-10-04 19:51:46 +00:00
|
|
|
if (unavail_reason) {
|
2023-10-06 15:30:55 +00:00
|
|
|
*unavail_reason = "model has no GPU support";
|
2023-10-04 19:51:46 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 21:58:46 +00:00
|
|
|
|
2024-04-18 18:52:02 +00:00
|
|
|
virtual bool usingGPUDevice() const { return false; }
|
|
|
|
virtual const char *backendName() const { return "cpu"; }
|
|
|
|
virtual const char *gpuDeviceName() const { return nullptr; }
|
2023-08-30 13:43:56 +00:00
|
|
|
|
2024-02-07 14:37:59 +00:00
|
|
|
void setProgressCallback(ProgressCallback callback) { m_progressCallback = callback; }
|
|
|
|
|
2023-04-25 15:20:51 +00:00
|
|
|
protected:
|
2023-06-04 12:59:24 +00:00
|
|
|
// These are pure virtual because subclasses need to implement as the default implementation of
|
|
|
|
// 'prompt' above calls these functions
|
2024-02-21 20:45:32 +00:00
|
|
|
virtual std::vector<Token> tokenize(PromptContext &ctx, const std::string &str, bool special = false) const = 0;
|
|
|
|
virtual std::string tokenToString(Token id) const = 0;
|
2023-06-04 12:59:24 +00:00
|
|
|
virtual Token sampleToken(PromptContext &ctx) const = 0;
|
2024-02-21 20:45:32 +00:00
|
|
|
virtual bool evalTokens(PromptContext &ctx, const std::vector<int32_t> &tokens) const = 0;
|
2023-06-04 12:59:24 +00:00
|
|
|
virtual int32_t contextLength() const = 0;
|
2024-02-21 20:45:32 +00:00
|
|
|
virtual const std::vector<Token> &endTokens() const = 0;
|
|
|
|
virtual bool shouldAddBOS() const = 0;
|
2023-06-04 12:59:24 +00:00
|
|
|
|
2024-01-31 19:17:44 +00:00
|
|
|
virtual int32_t maxContextLength(std::string const &modelPath) const
|
|
|
|
{
|
|
|
|
(void)modelPath;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int32_t layerCount(std::string const &modelPath) const
|
|
|
|
{
|
|
|
|
(void)modelPath;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-06-04 12:59:24 +00:00
|
|
|
// This is a helper function called from the default implementation of 'prompt' but it can be
|
|
|
|
// shared by all base classes so it isn't virtual
|
2023-06-02 03:15:58 +00:00
|
|
|
void recalculateContext(PromptContext &promptCtx, std::function<bool(bool)> recalculate);
|
2023-06-02 14:57:21 +00:00
|
|
|
|
2023-07-09 15:00:20 +00:00
|
|
|
const Implementation *m_implementation = nullptr;
|
2023-07-07 16:34:12 +00:00
|
|
|
|
2024-02-07 14:37:59 +00:00
|
|
|
ProgressCallback m_progressCallback;
|
|
|
|
static bool staticProgressCallback(float progress, void* ctx)
|
|
|
|
{
|
|
|
|
LLModel* model = static_cast<LLModel*>(ctx);
|
|
|
|
if (model && model->m_progressCallback)
|
|
|
|
return model->m_progressCallback(progress);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:33:46 +00:00
|
|
|
bool decodePrompt(std::function<bool(int32_t)> promptCallback,
|
2024-02-21 20:45:32 +00:00
|
|
|
std::function<bool(int32_t, const std::string&)> responseCallback,
|
|
|
|
std::function<bool(bool)> recalculateCallback,
|
|
|
|
PromptContext &promptCtx,
|
|
|
|
std::vector<Token> embd_inp);
|
|
|
|
void generateResponse(std::function<bool(int32_t, const std::string&)> responseCallback,
|
|
|
|
std::function<bool(bool)> recalculateCallback,
|
|
|
|
PromptContext &promptCtx);
|
|
|
|
|
2023-07-07 16:34:12 +00:00
|
|
|
private:
|
2023-07-08 14:04:38 +00:00
|
|
|
friend class LLMImplementation;
|
2023-04-14 02:15:40 +00:00
|
|
|
};
|
2023-07-07 16:34:12 +00:00
|
|
|
|
2023-04-18 13:46:03 +00:00
|
|
|
#endif // LLMODEL_H
|