2024-09-09 21:36:09 +00:00
|
|
|
#include "arg.h"
|
2024-02-11 11:44:51 +00:00
|
|
|
#include "ggml.h"
|
2024-03-23 00:24:36 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "ngram-cache.h"
|
2024-09-09 21:36:09 +00:00
|
|
|
#include "sampling.h"
|
2024-09-15 17:46:12 +00:00
|
|
|
#include "log.h"
|
2024-09-09 21:36:09 +00:00
|
|
|
#include "llama.h"
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-02-11 11:44:51 +00:00
|
|
|
#include <cstdint>
|
2023-12-22 16:05:56 +00:00
|
|
|
#include <cstdio>
|
2024-03-23 00:24:36 +00:00
|
|
|
#include <fstream>
|
2023-12-22 16:05:56 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
int main(int argc, char ** argv){
|
2024-10-10 20:57:42 +00:00
|
|
|
common_params params;
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LOOKUP)) {
|
2023-12-22 16:05:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_init();
|
2024-09-15 17:46:12 +00:00
|
|
|
|
2024-03-23 00:24:36 +00:00
|
|
|
// max. number of additional tokens to draft if match is found
|
2024-11-25 07:58:41 +00:00
|
|
|
const int n_draft = params.speculative.n_max;
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
const bool dump_kv_cache = params.dump_kv_cache;
|
|
|
|
|
|
|
|
// init llama.cpp
|
2024-02-16 09:31:07 +00:00
|
|
|
llama_backend_init();
|
|
|
|
llama_numa_init(params.numa);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
// load the model
|
2024-10-10 20:57:42 +00:00
|
|
|
common_init_result llama_init = common_init_from_params(params);
|
2024-08-05 16:14:10 +00:00
|
|
|
|
|
|
|
llama_model * model = llama_init.model;
|
|
|
|
llama_context * ctx = llama_init.context;
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
// tokenize the prompt
|
|
|
|
std::vector<llama_token> inp;
|
2024-10-10 20:57:42 +00:00
|
|
|
inp = common_tokenize(ctx, params.prompt, true, true);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_ngram_cache ngram_cache_context;
|
|
|
|
common_ngram_cache ngram_cache_dynamic;
|
|
|
|
common_ngram_cache ngram_cache_static;
|
2024-03-23 00:24:36 +00:00
|
|
|
int64_t t_draft_flat_us = 0;
|
|
|
|
int64_t t_draft_us = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Fill up context ngram cache with tokens from user input:
|
|
|
|
const int64_t t_start_draft_us = ggml_time_us();
|
2024-10-10 20:57:42 +00:00
|
|
|
common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, inp.size(), false);
|
2024-03-23 00:24:36 +00:00
|
|
|
|
|
|
|
if (!params.lookup_cache_static.empty()) {
|
|
|
|
try {
|
2024-10-10 20:57:42 +00:00
|
|
|
ngram_cache_static = common_ngram_cache_load(params.lookup_cache_static);
|
2024-03-23 00:24:36 +00:00
|
|
|
} catch (std::ifstream::failure const &) {
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_ERR("failed to open static lookup cache: %s", params.lookup_cache_static.c_str());
|
2024-03-23 00:24:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!params.lookup_cache_dynamic.empty()) {
|
|
|
|
try {
|
2024-10-10 20:57:42 +00:00
|
|
|
ngram_cache_dynamic = common_ngram_cache_load(params.lookup_cache_dynamic);
|
2024-03-23 00:24:36 +00:00
|
|
|
} catch (std::ifstream::failure const &) {} // if the file does not exist it will simply be created at the end of the program
|
|
|
|
}
|
|
|
|
|
|
|
|
t_draft_flat_us += ggml_time_us() - t_start_draft_us;
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:05:56 +00:00
|
|
|
const int max_context_size = llama_n_ctx(ctx);
|
|
|
|
const int max_tokens_list_size = max_context_size - 4;
|
|
|
|
|
|
|
|
if ((int) inp.size() > max_tokens_list_size) {
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_ERR("%s: prompt too long (%d tokens, max %d)\n", __func__, (int) inp.size(), max_tokens_list_size);
|
2023-12-22 16:05:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG("\n\n");
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
for (auto id : inp) {
|
2024-10-10 20:57:42 +00:00
|
|
|
LOG("%s", common_token_to_piece(ctx, id).c_str());
|
2023-12-22 16:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
const int n_input = inp.size();
|
|
|
|
|
|
|
|
const auto t_enc_start = ggml_time_us();
|
|
|
|
|
2024-10-18 21:18:01 +00:00
|
|
|
llama_decode(ctx, llama_batch_get_one( inp.data(), n_input - 1));
|
|
|
|
llama_decode(ctx, llama_batch_get_one(&inp.back(), 1));
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
const auto t_enc_end = ggml_time_us();
|
|
|
|
|
|
|
|
int n_predict = 0;
|
|
|
|
int n_drafted = 0;
|
|
|
|
int n_accept = 0;
|
|
|
|
|
|
|
|
int n_past = inp.size();
|
|
|
|
|
|
|
|
bool has_eos = false;
|
|
|
|
|
2024-11-25 07:58:41 +00:00
|
|
|
struct common_sampler * smpl = common_sampler_init(model, params.sampling);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
std::vector<llama_token> draft;
|
|
|
|
|
|
|
|
llama_batch batch_tgt = llama_batch_init(params.n_ctx, 0, 1);
|
|
|
|
|
|
|
|
// debug
|
|
|
|
struct llama_kv_cache_view kvc_view = llama_kv_cache_view_init(ctx, 1);
|
|
|
|
|
|
|
|
const auto t_dec_start = ggml_time_us();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// debug
|
|
|
|
if (dump_kv_cache) {
|
|
|
|
llama_kv_cache_view_update(ctx, &kvc_view);
|
2024-10-10 20:57:42 +00:00
|
|
|
common_kv_cache_dump_view_seqs(kvc_view, 40);
|
2023-12-22 16:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// print current draft sequence
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_DBG("drafted %s\n", string_from(ctx, draft).c_str());
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
int i_dft = 0;
|
|
|
|
while (true) {
|
|
|
|
// sample from the target model
|
2024-10-10 20:57:42 +00:00
|
|
|
llama_token id = common_sampler_sample(smpl, ctx, i_dft);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_sampler_accept(smpl, id, true);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
const std::string token_str = common_token_to_piece(ctx, id);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
if (!params.use_color) {
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG("%s", token_str.c_str());
|
2023-12-22 16:05:56 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 11:50:41 +00:00
|
|
|
if (llama_token_is_eog(model, id)) {
|
2023-12-22 16:05:56 +00:00
|
|
|
has_eos = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
++n_predict;
|
|
|
|
|
|
|
|
// check if the target token matches the draft
|
|
|
|
if (i_dft < (int) draft.size() && id == draft[i_dft]) {
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_DBG("the sampled target token matches the %dth drafted token (%d, '%s') - accepted\n", i_dft, id, token_str.c_str());
|
2023-12-22 16:05:56 +00:00
|
|
|
++n_accept;
|
|
|
|
++n_past;
|
|
|
|
++i_dft;
|
|
|
|
inp.push_back(id);
|
2024-03-23 00:24:36 +00:00
|
|
|
{
|
|
|
|
// Update context ngram cache with the newly accepted token:
|
|
|
|
const int64_t t_start_draft_us = ggml_time_us();
|
2024-10-10 20:57:42 +00:00
|
|
|
common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
|
2024-03-23 00:24:36 +00:00
|
|
|
t_draft_us += ggml_time_us() - t_start_draft_us;
|
|
|
|
}
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
if (params.use_color) {
|
|
|
|
// color accepted draft token
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG("\033[34m%s\033[0m", token_str.c_str());
|
2023-12-22 16:05:56 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.use_color) {
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG("%s", token_str.c_str());
|
2023-12-22 16:05:56 +00:00
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_DBG("the sampled target token (%d, '%s') did not match, or we ran out of drafted tokens\n", id, token_str.c_str());
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
draft.clear();
|
|
|
|
draft.push_back(id);
|
|
|
|
inp.push_back(id);
|
2024-03-23 00:24:36 +00:00
|
|
|
{
|
|
|
|
// Update context ngram cache with the newly accepted token:
|
|
|
|
const int64_t t_start_draft_us = ggml_time_us();
|
2024-10-10 20:57:42 +00:00
|
|
|
common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
|
2024-03-23 00:24:36 +00:00
|
|
|
t_draft_us += ggml_time_us() - t_start_draft_us;
|
|
|
|
}
|
2023-12-22 16:05:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((params.n_predict > 0 && n_predict > params.n_predict) || has_eos) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// KV cache management
|
|
|
|
// clean the cache of draft tokens that weren't accepted
|
|
|
|
llama_kv_cache_seq_rm(ctx, 0, n_past, -1);
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_batch_clear(batch_tgt);
|
|
|
|
common_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-03-23 00:24:36 +00:00
|
|
|
// Draft already contains a single token sampled from the model:
|
|
|
|
GGML_ASSERT(draft.size() == 1);
|
|
|
|
GGML_ASSERT(draft[0] == inp.back());
|
2024-02-11 11:44:51 +00:00
|
|
|
const int64_t t_start_draft_us = ggml_time_us();
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_ngram_cache_draft(inp, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
|
2024-03-23 00:24:36 +00:00
|
|
|
|
|
|
|
for (size_t i = 1; i < draft.size(); ++i) {
|
2024-10-10 20:57:42 +00:00
|
|
|
common_batch_add(batch_tgt, draft[i], n_past + i, { 0 }, true);
|
2024-03-23 00:24:36 +00:00
|
|
|
}
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-02-11 11:44:51 +00:00
|
|
|
t_draft_us += ggml_time_us() - t_start_draft_us;
|
2024-03-23 00:24:36 +00:00
|
|
|
n_drafted += draft.size() - 1;
|
2024-02-11 11:44:51 +00:00
|
|
|
|
2023-12-22 16:05:56 +00:00
|
|
|
llama_decode(ctx, batch_tgt);
|
|
|
|
++n_past;
|
|
|
|
|
|
|
|
draft.erase(draft.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto t_dec_end = ggml_time_us();
|
|
|
|
|
2024-03-23 00:24:36 +00:00
|
|
|
// Update dynamic ngram cache with context ngram cache and save it to disk:
|
2024-10-10 20:57:42 +00:00
|
|
|
common_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
|
|
|
|
common_ngram_cache_save(ngram_cache_dynamic, params.lookup_cache_dynamic);
|
2024-03-23 00:24:36 +00:00
|
|
|
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG("\n\n");
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_INF("encoded %4d tokens in %8.3f seconds, speed: %8.3f t/s\n", n_input, (t_enc_end - t_enc_start) / 1e6f, inp.size() / ((t_enc_end - t_enc_start) / 1e6f));
|
|
|
|
LOG_INF("decoded %4d tokens in %8.3f seconds, speed: %8.3f t/s\n", n_predict, (t_dec_end - t_dec_start) / 1e6f, n_predict / ((t_dec_end - t_dec_start) / 1e6f));
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_INF("\n");
|
|
|
|
LOG_INF("n_draft = %d\n", n_draft);
|
|
|
|
LOG_INF("n_predict = %d\n", n_predict);
|
|
|
|
LOG_INF("n_drafted = %d\n", n_drafted);
|
|
|
|
LOG_INF("t_draft_flat = %.2f ms\n", t_draft_flat_us*1e-3);
|
|
|
|
LOG_INF("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n",
|
2024-02-11 11:44:51 +00:00
|
|
|
t_draft_us*1e-3, 1.0f*t_draft_us/n_drafted, n_drafted/(1e-6*t_draft_us));
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_INF("n_accept = %d\n", n_accept);
|
|
|
|
LOG_INF("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG_INF("\ntarget:\n\n");
|
2024-10-10 20:57:42 +00:00
|
|
|
common_perf_print(ctx, smpl);
|
2024-09-07 12:16:19 +00:00
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_sampler_free(smpl);
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
llama_batch_free(batch_tgt);
|
|
|
|
|
|
|
|
llama_free(ctx);
|
|
|
|
llama_free_model(model);
|
|
|
|
|
|
|
|
llama_backend_free();
|
|
|
|
|
2024-09-15 17:46:12 +00:00
|
|
|
LOG("\n\n");
|
2023-12-22 16:05:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|