2023-04-13 22:15:40 -04:00
|
|
|
#ifndef LLMODEL_H
|
|
|
|
#define LLMODEL_H
|
2023-06-01 07:57:10 -04:00
|
|
|
|
2024-05-15 15:27:50 -04:00
|
|
|
#include <algorithm>
|
2024-06-26 15:26:27 -04:00
|
|
|
#include <cassert>
|
2024-05-31 16:34:54 -04:00
|
|
|
#include <cstddef>
|
2023-05-04 20:01:32 -04:00
|
|
|
#include <cstdint>
|
2024-03-13 18:09:24 -04:00
|
|
|
#include <functional>
|
|
|
|
#include <optional>
|
2024-05-31 16:34:54 -04:00
|
|
|
#include <stdexcept>
|
2024-03-13 18:09:24 -04:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2024-05-15 15:27:50 -04:00
|
|
|
#include <unordered_map>
|
2024-05-31 16:34:54 -04:00
|
|
|
#include <utility>
|
2024-03-13 18:09:24 -04:00
|
|
|
#include <vector>
|
2023-04-13 22:15:40 -04:00
|
|
|
|
2024-05-15 15:27:50 -04:00
|
|
|
using namespace std::string_literals;
|
|
|
|
|
2023-06-30 19:13:25 -04:00
|
|
|
#define LLMODEL_MAX_PROMPT_BATCH 128
|
|
|
|
|
2023-06-01 10:51:46 -04:00
|
|
|
class Dlhandle;
|
2023-04-13 22:15:40 -04:00
|
|
|
class LLModel {
|
|
|
|
public:
|
2023-06-04 08:59:24 -04:00
|
|
|
using Token = int32_t;
|
2023-12-13 12:11:09 -05:00
|
|
|
|
2024-04-25 13:16:52 -04: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 12:11:09 -05:00
|
|
|
struct GPUDevice {
|
2024-05-15 15:27:50 -04:00
|
|
|
const char *backend;
|
2024-01-25 16:58:46 -05:00
|
|
|
int index;
|
|
|
|
int type;
|
|
|
|
size_t heapSize;
|
2023-12-13 12:11:09 -05:00
|
|
|
std::string name;
|
|
|
|
std::string vendor;
|
2024-01-25 16:58:46 -05:00
|
|
|
|
2024-05-15 15:27:50 -04: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 15:26:27 -04: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 15:27:50 -04:00
|
|
|
|
|
|
|
static std::string updateSelectionName(const std::string &name) {
|
|
|
|
if (name == "Auto" || name == "CPU" || name == "Metal")
|
|
|
|
return name;
|
2024-06-26 15:26:27 -04:00
|
|
|
auto it = std::find_if(s_backendNames.begin(), s_backendNames.end(), [&name](const auto &entry) {
|
2024-05-15 15:27:50 -04:00
|
|
|
return name.starts_with(entry.second + ": ");
|
|
|
|
});
|
2024-06-26 15:26:27 -04:00
|
|
|
if (it != s_backendNames.end())
|
2024-05-15 15:27:50 -04:00
|
|
|
return name;
|
|
|
|
return "Vulkan: " + name; // previously, there were only Vulkan devices
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-06-26 15:26:27 -04:00
|
|
|
static inline const std::unordered_map<std::string, std::string> s_backendNames {
|
|
|
|
{"cpu", "CPU"}, {"metal", "Metal"}, {"cuda", "CUDA"}, {"kompute", "Vulkan"},
|
2024-05-15 15:27:50 -04:00
|
|
|
};
|
2023-12-13 12:11:09 -05:00
|
|
|
};
|
|
|
|
|
2023-07-09 11:00:20 -04:00
|
|
|
class Implementation {
|
|
|
|
public:
|
2024-02-21 15:45:32 -05:00
|
|
|
Implementation(const Implementation &) = delete;
|
|
|
|
Implementation(Implementation &&);
|
2023-07-09 11:00:20 -04:00
|
|
|
~Implementation();
|
|
|
|
|
|
|
|
std::string_view modelType() const { return m_modelType; }
|
|
|
|
std::string_view buildVariant() const { return m_buildVariant; }
|
|
|
|
|
2024-05-15 15:27:50 -04:00
|
|
|
static LLModel *construct(const std::string &modelPath, const std::string &backend = "auto", int n_ctx = 2048);
|
2024-04-04 14:52:13 -04:00
|
|
|
static std::vector<GPUDevice> availableGPUDevices(size_t memoryRequired = 0);
|
2024-01-31 14:17:44 -05:00
|
|
|
static int32_t maxContextLength(const std::string &modelPath);
|
|
|
|
static int32_t layerCount(const std::string &modelPath);
|
2024-03-13 18:09:24 -04:00
|
|
|
static bool isEmbeddingModel(const std::string &modelPath);
|
2024-02-21 15:45:32 -05:00
|
|
|
static void setImplementationsSearchPath(const std::string &path);
|
|
|
|
static const std::string &implementationsSearchPath();
|
2024-03-19 10:56:14 -04:00
|
|
|
static bool hasSupportedCPU();
|
2024-05-02 16:09:41 -04:00
|
|
|
// 0 for no, 1 for yes, -1 for non-x86_64
|
|
|
|
static int cpuSupportsAVX2();
|
2023-07-09 11:00:20 -04:00
|
|
|
|
|
|
|
private:
|
2024-03-19 10:56:14 -04:00
|
|
|
Implementation(Dlhandle &&);
|
|
|
|
|
|
|
|
static const std::vector<Implementation> &implementationList();
|
|
|
|
static const Implementation *implementation(const char *fname, const std::string &buildVariant);
|
2024-05-15 15:27:50 -04:00
|
|
|
static LLModel *constructGlobalLlama(const std::optional<std::string> &backend = std::nullopt);
|
2023-12-13 12:11:09 -05:00
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
char *(*m_getFileArch)(const char *fname);
|
|
|
|
bool (*m_isArchSupported)(const char *arch);
|
2023-07-09 11:00:20 -04:00
|
|
|
LLModel *(*m_construct)();
|
|
|
|
|
|
|
|
std::string_view m_modelType;
|
|
|
|
std::string_view m_buildVariant;
|
|
|
|
Dlhandle *m_dlhandle;
|
|
|
|
};
|
2023-06-04 08:59:24 -04:00
|
|
|
|
2023-04-13 22:15:40 -04:00
|
|
|
struct PromptContext {
|
2023-04-25 08:38:29 -04:00
|
|
|
std::vector<float> logits; // logits of current context
|
|
|
|
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 17:51:34 -05:00
|
|
|
float min_p = 0.0f;
|
2023-04-25 08:38:29 -04: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 16:51:15 -05: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-13 22:15:40 -04:00
|
|
|
};
|
2023-05-31 15:37:25 -04:00
|
|
|
|
2024-02-07 09:37:59 -05:00
|
|
|
using ProgressCallback = std::function<bool(float progress)>;
|
|
|
|
|
2023-05-31 15:37:25 -04:00
|
|
|
explicit LLModel() {}
|
|
|
|
virtual ~LLModel() {}
|
|
|
|
|
2023-07-09 11:32:51 -04:00
|
|
|
virtual bool supportsEmbedding() const = 0;
|
|
|
|
virtual bool supportsCompletion() const = 0;
|
2024-01-31 14:17:44 -05:00
|
|
|
virtual bool loadModel(const std::string &modelPath, int n_ctx, int ngl) = 0;
|
2024-03-13 18:09:24 -04: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 15:37:25 -04:00
|
|
|
virtual bool isModelLoaded() const = 0;
|
2024-01-31 14:17:44 -05:00
|
|
|
virtual size_t requiredMem(const std::string &modelPath, int n_ctx, int ngl) = 0;
|
2023-05-31 15:37:25 -04:00
|
|
|
virtual size_t stateSize() const { return 0; }
|
2024-02-21 15:45:32 -05: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 11:32:51 -04:00
|
|
|
|
|
|
|
// This method requires the model to return true from supportsCompletion otherwise it will throw
|
|
|
|
// an error
|
2023-04-25 08:38:29 -04:00
|
|
|
virtual void prompt(const std::string &prompt,
|
2024-02-21 15:45:32 -05:00
|
|
|
const std::string &promptTemplate,
|
2023-06-04 08:59:24 -04:00
|
|
|
std::function<bool(int32_t)> promptCallback,
|
|
|
|
std::function<bool(int32_t, const std::string&)> responseCallback,
|
|
|
|
std::function<bool(bool)> recalculateCallback,
|
2024-02-21 15:45:32 -05:00
|
|
|
PromptContext &ctx,
|
|
|
|
bool special = false,
|
|
|
|
std::string *fakeReply = nullptr);
|
2023-06-04 08:59:24 -04:00
|
|
|
|
2024-04-12 16:00:39 -04:00
|
|
|
using EmbedCancelCallback = bool(unsigned *batchSizes, unsigned nBatch, const char *backend);
|
|
|
|
|
2024-03-13 18:09:24 -04: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 16:00:39 -04:00
|
|
|
int dimensionality = -1, size_t *tokenCount = nullptr, bool doMean = true, bool atlas = false,
|
|
|
|
EmbedCancelCallback *cancelCb = nullptr);
|
2024-03-13 18:09:24 -04:00
|
|
|
// automatic prefix
|
|
|
|
virtual void embed(const std::vector<std::string> &texts, float *embeddings, bool isRetrieval,
|
2024-03-20 11:24:02 -04:00
|
|
|
int dimensionality = -1, size_t *tokenCount = nullptr, bool doMean = true, bool atlas = false);
|
2023-07-09 11:32:51 -04:00
|
|
|
|
2024-02-21 15:45:32 -05:00
|
|
|
virtual void setThreadCount(int32_t n_threads) { (void)n_threads; }
|
2023-05-21 16:45:29 -04:00
|
|
|
virtual int32_t threadCount() const { return 1; }
|
2023-04-25 11:20:51 -04:00
|
|
|
|
2024-02-21 15:45:32 -05:00
|
|
|
const Implementation &implementation() const {
|
2023-06-01 10:51:46 -04:00
|
|
|
return *m_implementation;
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
|
|
|
|
2024-01-31 14:17:44 -05:00
|
|
|
virtual std::vector<GPUDevice> availableGPUDevices(size_t memoryRequired) const {
|
2024-01-25 16:58:46 -05:00
|
|
|
(void)memoryRequired;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-02-21 15:45:32 -05:00
|
|
|
virtual bool initializeGPUDevice(size_t memoryRequired, const std::string &name) const {
|
2024-01-25 16:58:46 -05:00
|
|
|
(void)memoryRequired;
|
|
|
|
(void)name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:17:44 -05:00
|
|
|
virtual bool initializeGPUDevice(int device, std::string *unavail_reason = nullptr) const {
|
2024-01-25 16:58:46 -05:00
|
|
|
(void)device;
|
2023-10-04 15:51:46 -04:00
|
|
|
if (unavail_reason) {
|
2023-10-06 11:30:55 -04:00
|
|
|
*unavail_reason = "model has no GPU support";
|
2023-10-04 15:51:46 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 16:58:46 -05:00
|
|
|
|
2024-04-18 14:52:02 -04:00
|
|
|
virtual bool usingGPUDevice() const { return false; }
|
|
|
|
virtual const char *backendName() const { return "cpu"; }
|
|
|
|
virtual const char *gpuDeviceName() const { return nullptr; }
|
2023-08-30 09:43:56 -04:00
|
|
|
|
2024-02-07 09:37:59 -05:00
|
|
|
void setProgressCallback(ProgressCallback callback) { m_progressCallback = callback; }
|
|
|
|
|
2023-04-25 11:20:51 -04:00
|
|
|
protected:
|
2023-06-04 08:59:24 -04:00
|
|
|
// These are pure virtual because subclasses need to implement as the default implementation of
|
|
|
|
// 'prompt' above calls these functions
|
2024-02-21 15:45:32 -05: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 08:59:24 -04:00
|
|
|
virtual Token sampleToken(PromptContext &ctx) const = 0;
|
2024-02-21 15:45:32 -05:00
|
|
|
virtual bool evalTokens(PromptContext &ctx, const std::vector<int32_t> &tokens) const = 0;
|
2023-06-04 08:59:24 -04:00
|
|
|
virtual int32_t contextLength() const = 0;
|
2024-02-21 15:45:32 -05:00
|
|
|
virtual const std::vector<Token> &endTokens() const = 0;
|
|
|
|
virtual bool shouldAddBOS() const = 0;
|
2023-06-04 08:59:24 -04:00
|
|
|
|
2024-01-31 14:17:44 -05: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 08:59:24 -04: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-01 23:15:58 -04:00
|
|
|
void recalculateContext(PromptContext &promptCtx, std::function<bool(bool)> recalculate);
|
2023-06-02 10:57:21 -04:00
|
|
|
|
2023-07-09 11:00:20 -04:00
|
|
|
const Implementation *m_implementation = nullptr;
|
2023-07-07 12:34:12 -04:00
|
|
|
|
2024-02-07 09:37:59 -05: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 11:33:46 -04:00
|
|
|
bool decodePrompt(std::function<bool(int32_t)> promptCallback,
|
2024-02-21 15:45:32 -05: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 12:34:12 -04:00
|
|
|
private:
|
2023-07-08 10:04:38 -04:00
|
|
|
friend class LLMImplementation;
|
2023-04-13 22:15:40 -04:00
|
|
|
};
|
2023-07-07 12:34:12 -04:00
|
|
|
|
2023-04-18 09:46:03 -04:00
|
|
|
#endif // LLMODEL_H
|