2023-05-31 21:04:01 +00:00
|
|
|
#define LLAMAMODEL_H_I_KNOW_WHAT_I_AM_DOING_WHEN_INCLUDING_THIS_FILE
|
|
|
|
#include "llamamodel_impl.h"
|
2023-04-15 19:57:32 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
2023-05-16 15:35:33 +00:00
|
|
|
#if defined(_WIN32) && defined(_MSC_VER)
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
|
|
|
#include <windows.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2023-04-15 19:57:32 +00:00
|
|
|
#include <random>
|
|
|
|
#include <thread>
|
2023-05-03 15:58:26 +00:00
|
|
|
#include <unordered_set>
|
2023-04-15 19:57:32 +00:00
|
|
|
|
2023-05-31 21:04:01 +00:00
|
|
|
#include <llama.h>
|
|
|
|
#include <ggml.h>
|
|
|
|
|
2023-08-30 13:43:56 +00:00
|
|
|
#ifdef GGML_USE_KOMPUTE
|
|
|
|
#include "ggml-vulkan.h"
|
|
|
|
#endif
|
2023-05-31 21:04:01 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const char *modelType_ = "LLaMA";
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gpt_params {
|
|
|
|
int32_t seed = -1; // RNG seed
|
|
|
|
int32_t n_keep = 0; // number of tokens to keep from initial prompt
|
|
|
|
#if LLAMA_DATE <= 230511
|
|
|
|
int32_t n_parts = -1; // amount of model parts (-1 = determine from model dimensions)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if LLAMA_DATE >= 230519
|
|
|
|
// sampling parameters
|
|
|
|
float tfs_z = 1.0f; // 1.0 = disabled
|
|
|
|
float typical_p = 1.0f; // 1.0 = disabled
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string prompt = "";
|
|
|
|
|
|
|
|
bool memory_f16 = true; // use f16 instead of f32 for memory kv
|
|
|
|
|
|
|
|
bool use_mmap = true; // use mmap for faster loads
|
|
|
|
bool use_mlock = false; // use mlock to keep model in memory
|
|
|
|
};
|
|
|
|
|
|
|
|
#if LLAMA_DATE >= 230519
|
|
|
|
static int llama_sample_top_p_top_k(
|
|
|
|
llama_context *ctx,
|
|
|
|
const llama_token *last_n_tokens_data,
|
|
|
|
int last_n_tokens_size,
|
|
|
|
int top_k,
|
|
|
|
float top_p,
|
|
|
|
float temp,
|
|
|
|
float repeat_penalty) {
|
|
|
|
auto logits = llama_get_logits(ctx);
|
|
|
|
auto n_vocab = llama_n_vocab(ctx);
|
|
|
|
// Populate initial list of all candidates
|
|
|
|
std::vector<llama_token_data> candidates;
|
|
|
|
candidates.reserve(n_vocab);
|
|
|
|
for (int token_id = 0; token_id < n_vocab; token_id++) {
|
|
|
|
candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
|
|
|
|
}
|
|
|
|
llama_token_data_array candidates_p = {candidates.data(), candidates.size(), false};
|
|
|
|
// Sample repeat penalty
|
|
|
|
llama_sample_repetition_penalty(nullptr, &candidates_p, last_n_tokens_data, last_n_tokens_size, repeat_penalty);
|
|
|
|
// Temperature sampling
|
|
|
|
llama_sample_top_k(ctx, &candidates_p, top_k, 1);
|
|
|
|
llama_sample_tail_free(ctx, &candidates_p, 1.0f, 1);
|
|
|
|
llama_sample_typical(ctx, &candidates_p, 1.0f, 1);
|
|
|
|
llama_sample_top_p(ctx, &candidates_p, top_p, 1);
|
|
|
|
llama_sample_temperature(ctx, &candidates_p, temp);
|
|
|
|
return llama_sample_token(ctx, &candidates_p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-15 19:57:32 +00:00
|
|
|
struct LLamaPrivate {
|
|
|
|
const std::string modelPath;
|
|
|
|
bool modelLoaded;
|
|
|
|
llama_context *ctx = nullptr;
|
|
|
|
llama_context_params params;
|
|
|
|
int64_t n_threads = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
LLamaModel::LLamaModel()
|
|
|
|
: d_ptr(new LLamaPrivate) {
|
|
|
|
d_ptr->modelLoaded = false;
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:17:34 +00:00
|
|
|
// default hparams (LLaMA 7B)
|
|
|
|
struct llama_file_hparams {
|
|
|
|
uint32_t n_vocab = 32000;
|
|
|
|
uint32_t n_embd = 4096;
|
|
|
|
uint32_t n_mult = 256;
|
|
|
|
uint32_t n_head = 32;
|
|
|
|
uint32_t n_layer = 32;
|
|
|
|
uint32_t n_rot = 64;
|
|
|
|
enum llama_ftype ftype = LLAMA_FTYPE_MOSTLY_F16;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t LLamaModel::requiredMem(const std::string &modelPath) {
|
|
|
|
auto fin = std::ifstream(modelPath, std::ios::binary);
|
|
|
|
fin.seekg(0, std::ios_base::end);
|
|
|
|
size_t filesize = fin.tellg();
|
|
|
|
fin.seekg(0, std::ios_base::beg);
|
|
|
|
uint32_t magic = 0;
|
|
|
|
fin.read(reinterpret_cast<char*>(&magic), sizeof(magic));
|
|
|
|
if (magic != 0x67676a74) return 0;
|
|
|
|
uint32_t version = 0;
|
|
|
|
fin.read(reinterpret_cast<char*>(&version), sizeof(version));
|
|
|
|
llama_file_hparams hparams;
|
|
|
|
fin.read(reinterpret_cast<char*>(&hparams.n_vocab), sizeof(hparams.n_vocab));
|
|
|
|
fin.read(reinterpret_cast<char*>(&hparams.n_embd), sizeof(hparams.n_embd));
|
|
|
|
fin.read(reinterpret_cast<char*>(&hparams.n_head), sizeof(hparams.n_head));
|
|
|
|
fin.read(reinterpret_cast<char*>(&hparams.n_layer), sizeof(hparams.n_layer));
|
|
|
|
fin.read(reinterpret_cast<char*>(&hparams.n_rot), sizeof(hparams.n_rot));
|
|
|
|
fin.read(reinterpret_cast<char*>(&hparams.ftype), sizeof(hparams.ftype));
|
|
|
|
const size_t n_ctx = 2048;
|
|
|
|
const size_t kvcache_element_size = 2; // fp16
|
|
|
|
const size_t est_kvcache_size = hparams.n_embd * hparams.n_layer * 2u * n_ctx * kvcache_element_size;
|
|
|
|
return filesize + est_kvcache_size;
|
|
|
|
}
|
|
|
|
|
2023-04-15 19:57:32 +00:00
|
|
|
bool LLamaModel::loadModel(const std::string &modelPath)
|
|
|
|
{
|
|
|
|
// load the model
|
|
|
|
d_ptr->params = llama_context_default_params();
|
2023-04-20 16:07:43 +00:00
|
|
|
|
|
|
|
gpt_params params;
|
2023-04-20 21:13:00 +00:00
|
|
|
d_ptr->params.n_ctx = 2048;
|
2023-04-20 16:07:43 +00:00
|
|
|
d_ptr->params.seed = params.seed;
|
|
|
|
d_ptr->params.f16_kv = params.memory_f16;
|
|
|
|
d_ptr->params.use_mmap = params.use_mmap;
|
2023-06-03 00:15:38 +00:00
|
|
|
#if defined (__APPLE__)
|
|
|
|
d_ptr->params.use_mlock = true;
|
|
|
|
#else
|
2023-05-21 14:13:35 +00:00
|
|
|
d_ptr->params.use_mlock = params.use_mlock;
|
2023-06-04 12:59:24 +00:00
|
|
|
#endif
|
2023-05-31 21:04:01 +00:00
|
|
|
#if LLAMA_DATE <= 230511
|
|
|
|
d_ptr->params.n_parts = params.n_parts;
|
2023-05-21 14:13:35 +00:00
|
|
|
#endif
|
2023-06-09 20:48:46 +00:00
|
|
|
#ifdef GGML_USE_METAL
|
|
|
|
std::cerr << "llama.cpp: using Metal" << std::endl;
|
|
|
|
// metal always runs the whole model if n_gpu_layers is not 0, at least
|
|
|
|
// currently
|
|
|
|
d_ptr->params.n_gpu_layers = 1;
|
|
|
|
#endif
|
2023-08-30 13:43:56 +00:00
|
|
|
#ifdef GGML_USE_KOMPUTE
|
|
|
|
if (ggml_vk_has_device()) {
|
|
|
|
// vulkan always runs the whole model if n_gpu_layers is not 0, at least
|
|
|
|
// currently
|
|
|
|
d_ptr->params.n_gpu_layers = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2023-04-20 16:07:43 +00:00
|
|
|
|
2023-04-15 19:57:32 +00:00
|
|
|
d_ptr->ctx = llama_init_from_file(modelPath.c_str(), d_ptr->params);
|
|
|
|
if (!d_ptr->ctx) {
|
|
|
|
std::cerr << "LLAMA ERROR: failed to load model from " << modelPath << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-30 13:43:56 +00:00
|
|
|
#ifdef GGML_USE_KOMPUTE
|
|
|
|
if (ggml_vk_has_device()) {
|
|
|
|
std::cerr << "llama.cpp: using Vulkan on " << ggml_vk_current_device().name << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-15 19:57:32 +00:00
|
|
|
d_ptr->n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
|
|
|
d_ptr->modelLoaded = true;
|
2023-04-25 15:20:51 +00:00
|
|
|
fflush(stderr);
|
2023-04-15 19:57:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLamaModel::setThreadCount(int32_t n_threads) {
|
|
|
|
d_ptr->n_threads = n_threads;
|
|
|
|
}
|
|
|
|
|
2023-05-31 21:04:01 +00:00
|
|
|
int32_t LLamaModel::threadCount() const {
|
2023-04-15 19:57:32 +00:00
|
|
|
return d_ptr->n_threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLamaModel::~LLamaModel()
|
|
|
|
{
|
2023-06-26 21:53:17 +00:00
|
|
|
if(d_ptr->ctx) {
|
|
|
|
llama_free(d_ptr->ctx);
|
|
|
|
}
|
2023-04-15 19:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LLamaModel::isModelLoaded() const
|
|
|
|
{
|
|
|
|
return d_ptr->modelLoaded;
|
|
|
|
}
|
|
|
|
|
2023-05-04 19:31:41 +00:00
|
|
|
size_t LLamaModel::stateSize() const
|
|
|
|
{
|
|
|
|
return llama_get_state_size(d_ptr->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t LLamaModel::saveState(uint8_t *dest) const
|
|
|
|
{
|
|
|
|
return llama_copy_state_data(d_ptr->ctx, dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t LLamaModel::restoreState(const uint8_t *src)
|
|
|
|
{
|
2023-05-31 21:04:01 +00:00
|
|
|
// const_cast is required, see: https://github.com/ggerganov/llama.cpp/pull/1540
|
|
|
|
return llama_set_state_data(d_ptr->ctx, const_cast<uint8_t*>(src));
|
2023-05-04 19:31:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 23:31:00 +00:00
|
|
|
std::vector<LLModel::Token> LLamaModel::tokenize(PromptContext &ctx, const std::string &str) const
|
2023-06-04 12:59:24 +00:00
|
|
|
{
|
2023-06-04 23:31:00 +00:00
|
|
|
const bool useBOS = ctx.n_past == 0 && (ctx.tokens.empty() || ctx.tokens.front() != llama_token_bos());
|
2023-06-04 12:59:24 +00:00
|
|
|
std::vector<LLModel::Token> fres(str.size()+4);
|
2023-06-04 23:31:00 +00:00
|
|
|
auto fres_len = llama_tokenize(d_ptr->ctx, str.c_str(), fres.data(), fres.size(), useBOS);
|
2023-06-04 12:59:24 +00:00
|
|
|
fres.resize(fres_len);
|
|
|
|
return fres;
|
|
|
|
}
|
2023-04-15 19:57:32 +00:00
|
|
|
|
2023-06-13 11:14:02 +00:00
|
|
|
std::string LLamaModel::tokenToString(Token id) const
|
2023-06-04 12:59:24 +00:00
|
|
|
{
|
|
|
|
return llama_token_to_str(d_ptr->ctx, id);
|
|
|
|
}
|
2023-04-15 19:57:32 +00:00
|
|
|
|
2023-06-04 12:59:24 +00:00
|
|
|
LLModel::Token LLamaModel::sampleToken(PromptContext &promptCtx) const
|
|
|
|
{
|
|
|
|
const size_t n_prev_toks = std::min((size_t) promptCtx.repeat_last_n, promptCtx.tokens.size());
|
|
|
|
return llama_sample_top_p_top_k(d_ptr->ctx,
|
|
|
|
promptCtx.tokens.data() + promptCtx.tokens.size() - n_prev_toks,
|
|
|
|
n_prev_toks, promptCtx.top_k, promptCtx.top_p, promptCtx.temp,
|
|
|
|
promptCtx.repeat_penalty);
|
|
|
|
}
|
2023-04-15 19:57:32 +00:00
|
|
|
|
2023-06-04 12:59:24 +00:00
|
|
|
bool LLamaModel::evalTokens(PromptContext &ctx, const std::vector<int32_t> &tokens) const
|
|
|
|
{
|
2023-06-12 13:21:28 +00:00
|
|
|
// When we recalculate context we could have erased the original BOS token... we need to replace it
|
|
|
|
const bool useBOS = ctx.n_past == 0 && (ctx.tokens.empty() || ctx.tokens.front() != llama_token_bos());
|
|
|
|
if (useBOS) {
|
|
|
|
std::vector<int32_t> myTokens;
|
|
|
|
myTokens.push_back(llama_token_bos());
|
|
|
|
myTokens.insert(myTokens.end(), tokens.begin(), tokens.end());
|
|
|
|
ctx.n_past += 1;
|
|
|
|
return llama_eval(d_ptr->ctx, myTokens.data(), myTokens.size(), ctx.n_past, d_ptr->n_threads) == 0;
|
|
|
|
} else
|
|
|
|
return llama_eval(d_ptr->ctx, tokens.data(), tokens.size(), ctx.n_past, d_ptr->n_threads) == 0;
|
2023-06-04 12:59:24 +00:00
|
|
|
}
|
2023-04-15 19:57:32 +00:00
|
|
|
|
2023-06-04 12:59:24 +00:00
|
|
|
int32_t LLamaModel::contextLength() const
|
|
|
|
{
|
|
|
|
return llama_n_ctx(d_ptr->ctx);
|
2023-04-15 19:57:32 +00:00
|
|
|
}
|
2023-04-25 15:20:51 +00:00
|
|
|
|
2023-06-04 12:59:24 +00:00
|
|
|
const std::vector<LLModel::Token> &LLamaModel::endTokens() const
|
2023-04-25 15:20:51 +00:00
|
|
|
{
|
2023-06-04 12:59:24 +00:00
|
|
|
static const std::vector<LLModel::Token> fres = {llama_token_eos()};
|
|
|
|
return fres;
|
2023-04-25 15:20:51 +00:00
|
|
|
}
|
2023-05-31 21:04:01 +00:00
|
|
|
|
2023-08-30 13:43:56 +00:00
|
|
|
#if defined(GGML_USE_KOMPUTE)
|
|
|
|
#include "ggml-vulkan.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<LLModel::GPUDevice> LLamaModel::availableGPUDevices(size_t memoryRequired)
|
|
|
|
{
|
|
|
|
#if defined(GGML_USE_KOMPUTE)
|
|
|
|
std::vector<ggml_vk_device> vkDevices = ggml_vk_available_devices(memoryRequired);
|
|
|
|
|
|
|
|
std::vector<LLModel::GPUDevice> devices;
|
|
|
|
for(const auto& vkDevice : vkDevices) {
|
|
|
|
LLModel::GPUDevice device;
|
|
|
|
device.index = vkDevice.index;
|
|
|
|
device.type = vkDevice.type;
|
|
|
|
device.heapSize = vkDevice.heapSize;
|
|
|
|
device.name = vkDevice.name;
|
|
|
|
device.vendor = vkDevice.vendor;
|
|
|
|
|
|
|
|
devices.push_back(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
#else
|
|
|
|
return std::vector<LLModel::GPUDevice>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LLamaModel::initializeGPUDevice(size_t memoryRequired, const std::string& device)
|
|
|
|
{
|
|
|
|
#if defined(GGML_USE_KOMPUTE)
|
|
|
|
return ggml_vk_init_device(memoryRequired, device);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LLamaModel::initializeGPUDevice(const LLModel::GPUDevice &device)
|
|
|
|
{
|
|
|
|
#if defined(GGML_USE_KOMPUTE)
|
|
|
|
ggml_vk_device vkDevice;
|
|
|
|
vkDevice.index = device.index;
|
|
|
|
vkDevice.type = device.type;
|
|
|
|
vkDevice.heapSize = device.heapSize;
|
|
|
|
vkDevice.name = device.name;
|
|
|
|
vkDevice.vendor = device.vendor;
|
|
|
|
return ggml_vk_init_device(vkDevice);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LLamaModel::initializeGPUDevice(int device)
|
|
|
|
{
|
|
|
|
#if defined(GGML_USE_KOMPUTE)
|
|
|
|
return ggml_vk_init_device(device);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LLamaModel::hasGPUDevice()
|
|
|
|
{
|
|
|
|
#if defined(GGML_USE_KOMPUTE)
|
|
|
|
return ggml_vk_has_device();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-05-31 21:04:01 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#define DLL_EXPORT __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
#define DLL_EXPORT __attribute__ ((visibility ("default")))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
DLL_EXPORT bool is_g4a_backend_model_implementation() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLL_EXPORT const char *get_model_type() {
|
|
|
|
return modelType_;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLL_EXPORT const char *get_build_variant() {
|
|
|
|
return GGML_BUILD_VARIANT;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLL_EXPORT bool magic_match(std::istream& f) {
|
|
|
|
// Check magic
|
|
|
|
uint32_t magic = 0;
|
|
|
|
f.read(reinterpret_cast<char*>(&magic), sizeof(magic));
|
|
|
|
if (magic != 0x67676a74) return false;
|
|
|
|
// Check version
|
|
|
|
uint32_t version = 0;
|
|
|
|
f.read(reinterpret_cast<char*>(&version), sizeof(version));
|
2023-06-09 20:48:46 +00:00
|
|
|
if (!(version LLAMA_VERSIONS)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-27 02:53:57 +00:00
|
|
|
llama_file_hparams hparams;
|
|
|
|
f.read(reinterpret_cast<char*>(&hparams), sizeof(hparams));
|
|
|
|
if (!(hparams.n_vocab >= 32000 && hparams.n_vocab <= 32100)) {
|
|
|
|
return false; // not a llama.
|
|
|
|
}
|
2023-06-09 20:48:46 +00:00
|
|
|
#ifdef GGML_USE_METAL
|
|
|
|
// Check quant supported on metal
|
|
|
|
// skip fields
|
2023-06-27 02:53:57 +00:00
|
|
|
switch(hparams.ftype) {
|
2023-06-09 20:48:46 +00:00
|
|
|
// currently supported on Metal https://github.com/ggerganov/llama.cpp/blob/ae9663f1887513e152839e91f61c513075a19422/ggml-metal.m#L51-L55
|
|
|
|
case LLAMA_FTYPE_MOSTLY_F16:
|
|
|
|
case LLAMA_FTYPE_MOSTLY_Q2_K:
|
|
|
|
case LLAMA_FTYPE_MOSTLY_Q4_0:
|
|
|
|
case LLAMA_FTYPE_MOSTLY_Q6_K:
|
|
|
|
case LLAMA_FTYPE_MOSTLY_Q4_K_S:
|
|
|
|
case LLAMA_FTYPE_MOSTLY_Q4_K_M:
|
|
|
|
return true;
|
|
|
|
default: // unsupported quant-type for Metal
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
2023-05-31 21:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DLL_EXPORT LLModel *construct() {
|
|
|
|
return new LLamaModel;
|
|
|
|
}
|
|
|
|
}
|