summaryrefslogtreecommitdiff
path: root/examples/lookup/lookup.cpp
diff options
context:
space:
mode:
authorJohannes Gäßler <johannesg@5d6.de>2024-03-23 01:24:36 +0100
committerGitHub <noreply@github.com>2024-03-23 01:24:36 +0100
commit50ccaf5eacb50a2ca378a4ef0dc7aeb45fead652 (patch)
tree3ebcfdadf96bb6f3aadd752a1bfe9771ac182d3b /examples/lookup/lookup.cpp
parent56a00f0a2f48a85376f48b5ce77699df781631ae (diff)
lookup: complement data from context with general text statistics (#5479)
* lookup: evaluation tools, use corpus/previous gens * fixup! lookup: evaluation tools, use corpus/previous gens * fixup! lookup: evaluation tools, use corpus/previous gens * fixup! lookup: evaluation tools, use corpus/previous gens * fixup! lookup: evaluation tools, use corpus/previous gens
Diffstat (limited to 'examples/lookup/lookup.cpp')
-rw-r--r--examples/lookup/lookup.cpp116
1 files changed, 68 insertions, 48 deletions
diff --git a/examples/lookup/lookup.cpp b/examples/lookup/lookup.cpp
index b53fae11..2e8c35de 100644
--- a/examples/lookup/lookup.cpp
+++ b/examples/lookup/lookup.cpp
@@ -1,12 +1,15 @@
-#include "common.h"
#include "ggml.h"
#include "llama.h"
+#include "common.h"
+#include "ngram-cache.h"
#include <cmath>
#include <cstdint>
#include <cstdio>
+#include <fstream>
#include <string>
#include <vector>
+#include <unordered_map>
int main(int argc, char ** argv){
gpt_params params;
@@ -15,11 +18,7 @@ int main(int argc, char ** argv){
return 1;
}
- // max/min n-grams size to search for in prompt
- const int ngram_max = 4;
- const int ngram_min = 1;
-
- // length of the candidate / draft sequence, if match is found
+ // max. number of additional tokens to draft if match is found
const int n_draft = params.n_draft;
const bool dump_kv_cache = params.dump_kv_cache;
@@ -39,6 +38,8 @@ int main(int argc, char ** argv){
// load the model
std::tie(model, ctx) = llama_init_from_gpt_params(params);
+ llama_set_rng_seed(ctx, params.seed);
+ GGML_ASSERT(llama_n_vocab(model) < (1 << 16));
// tokenize the prompt
const bool add_bos = llama_should_add_bos_token(model);
@@ -47,6 +48,35 @@ int main(int argc, char ** argv){
std::vector<llama_token> inp;
inp = ::llama_tokenize(ctx, params.prompt, add_bos, true);
+ llama_ngram_cache ngram_cache_context;
+ llama_ngram_cache ngram_cache_dynamic;
+ llama_ngram_cache ngram_cache_static;
+ 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();
+ llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, inp.size(), false);
+
+ if (!params.lookup_cache_static.empty()) {
+ try {
+ ngram_cache_static = llama_ngram_cache_load(params.lookup_cache_static);
+ } catch (std::ifstream::failure const &) {
+ fprintf(stderr, "error: failed to open static lookup cache: %s", params.lookup_cache_static.c_str());
+ exit(1);
+ }
+ }
+
+ if (!params.lookup_cache_dynamic.empty()) {
+ try {
+ ngram_cache_dynamic = llama_ngram_cache_load(params.lookup_cache_dynamic);
+ } 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;
+ }
+
const int max_context_size = llama_n_ctx(ctx);
const int max_tokens_list_size = max_context_size - 4;
@@ -76,8 +106,6 @@ int main(int argc, char ** argv){
int n_drafted = 0;
int n_accept = 0;
- int64_t t_draft_us = 0;
-
int n_past = inp.size();
bool has_eos = false;
@@ -129,6 +157,12 @@ int main(int argc, char ** argv){
++n_past;
++i_dft;
inp.push_back(id);
+ {
+ // Update context ngram cache with the newly accepted token:
+ const int64_t t_start_draft_us = ggml_time_us();
+ llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
+ t_draft_us += ggml_time_us() - t_start_draft_us;
+ }
if (params.use_color) {
// color accepted draft token
@@ -149,6 +183,12 @@ int main(int argc, char ** argv){
draft.clear();
draft.push_back(id);
inp.push_back(id);
+ {
+ // Update context ngram cache with the newly accepted token:
+ const int64_t t_start_draft_us = ggml_time_us();
+ llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
+ t_draft_us += ggml_time_us() - t_start_draft_us;
+ }
break;
}
@@ -163,44 +203,19 @@ int main(int argc, char ** argv){
llama_batch_clear(batch_tgt);
llama_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
- // generate n_pred tokens through prompt lookup
- auto prompt_lookup = [&]() -> void {
- const int inp_size = inp.size();
- for (int ngram_size = ngram_max ; ngram_size > ngram_min; --ngram_size){
- const llama_token * ngram = &inp[inp_size - ngram_size];
-
- for (int i = 0; i <= (int) inp_size - (ngram_size * 2); ++i) {
- bool match = true;
- for (int j = 0; j < ngram_size; ++j) {
- if (inp[i + j] != ngram[j]) {
- match = false;
- break;
- }
- }
-
- if (match) {
- const int startIdx = i + ngram_size;
- const int endIdx = startIdx + n_draft;
- if (endIdx < inp_size) {
- for (int j = startIdx; j < endIdx; ++j) {
- LOG(" - draft candidate %d: %d\n", j, inp[j]);
- draft.push_back(inp[j]);
- llama_batch_add(batch_tgt, inp[j], n_past + (j - startIdx) + 1, { 0 }, true);
- ++n_drafted;
- }
- return;
- }
- }
- }
- }
- return;
- };
-
+ // Draft already contains a single token sampled from the model:
+ GGML_ASSERT(draft.size() == 1);
+ GGML_ASSERT(draft[0] == inp.back());
const int64_t t_start_draft_us = ggml_time_us();
- prompt_lookup();
+ llama_ngram_cache_draft(inp, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
+
+ for (size_t i = 1; i < draft.size(); ++i) {
+ llama_batch_add(batch_tgt, draft[i], n_past + i, { 0 }, true);
+ }
t_draft_us += ggml_time_us() - t_start_draft_us;
+ n_drafted += draft.size() - 1;
llama_decode(ctx, batch_tgt);
++n_past;
@@ -210,19 +225,24 @@ int main(int argc, char ** argv){
auto t_dec_end = ggml_time_us();
+ // Update dynamic ngram cache with context ngram cache and save it to disk:
+ llama_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
+ llama_ngram_cache_save(ngram_cache_dynamic, params.lookup_cache_dynamic);
+
LOG_TEE("\n\n");
LOG_TEE("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_TEE("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));
LOG_TEE("\n");
- LOG_TEE("n_draft = %d\n", n_draft);
- LOG_TEE("n_predict = %d\n", n_predict);
- LOG_TEE("n_drafted = %d\n", n_drafted);
- LOG_TEE("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n",
+ LOG_TEE("n_draft = %d\n", n_draft);
+ LOG_TEE("n_predict = %d\n", n_predict);
+ LOG_TEE("n_drafted = %d\n", n_drafted);
+ LOG_TEE("t_draft_flat = %.2f ms\n", t_draft_flat_us*1e-3);
+ LOG_TEE("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n",
t_draft_us*1e-3, 1.0f*t_draft_us/n_drafted, n_drafted/(1e-6*t_draft_us));
- LOG_TEE("n_accept = %d\n", n_accept);
- LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
+ LOG_TEE("n_accept = %d\n", n_accept);
+ LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
LOG_TEE("\ntarget:\n");
llama_print_timings(ctx);