Skip to content

Commit f9a6364

Browse files
authored
llama : require first token to be BOS (#1303)
* llama : require first token to be BOS * scripts : add ppl-run-all.sh * perplexity : add BOS for each chunk * readme : update perplexity values after BOS fix * perplexity : add clarifying comments
1 parent 95078cc commit f9a6364

File tree

7 files changed

+116
-50
lines changed

7 files changed

+116
-50
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ zig-out/
4343
zig-cache/
4444

4545
ppl-*.txt
46+
qnt-*.txt
4647

4748
examples/jeopardy/results.txt

README.md

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,25 @@ Several quantization methods are supported. They differ in the resulting model d
298298
299299
| Model | Measure | F16 | Q4_0 | Q4_1 | Q4_2 | Q5_0 | Q5_1 | Q8_0 |
300300
|------:|--------------|-------:|-------:|-------:|-------:|-------:|-------:|-------:|
301-
| 7B | perplexity | 5.9565 | 6.2103 | 6.1286 | 6.1698 | 6.0139 | 5.9934 | 5.9571 |
301+
| 7B | perplexity | 5.9066 | 6.1620 | 6.0910 | 6.1466 | 5.9862 | 5.9481 | 5.9069 |
302302
| 7B | file size | 13.0G | 4.0G | 4.8G | 4.0G | 4.4G | 4.8G | 7.1G |
303303
| 7B | ms/tok @ 4th | 128 | 56 | 61 | 84 | 91 | 95 | 75 |
304304
| 7B | ms/tok @ 8th | 128 | 47 | 55 | 48 | 53 | 59 | 75 |
305305
| 7B | bits/weight | 16.0 | 5.0 | 6.0 | 5.0 | 5.5 | 6.0 | 9.0 |
306-
| 13B | perplexity | 5.2455 | 5.3748 | 5.3471 | 5.3433 | 5.2768 | 5.2582 | 5.2458 |
306+
| 13B | perplexity | 5.2543 | 5.3863 | 5.3607 | 5.3513 | 5.2856 | 5.2706 | 5.2548 |
307307
| 13B | file size | 25.0G | 7.6G | 9.1G | 7.6G | 8.4G | 9.1G | 14G |
308308
| 13B | ms/tok @ 4th | 239 | 104 | 113 | 160 | 176 | 185 | 141 |
309309
| 13B | ms/tok @ 8th | 240 | 85 | 99 | 97 | 108 | 117 | 147 |
310310
| 13B | bits/weight | 16.0 | 5.0 | 6.0 | 5.0 | 5.5 | 6.0 | 9.0 |
311311
312+
### Perplexity (measuring model quality)
313+
314+
You can use the `perplexity` example to measure perplexity over a given prompt (lower perplexity is better).
315+
For more information, see [https://huggingface.co/docs/transformers/perplexity](https://huggingface.co/docs/transformers/perplexity).
316+
317+
The perplexity measurements in table above are done against the `wikitext2` test dataset (https://paperswithcode.com/dataset/wikitext-2), with context length of 512.
318+
The time per token is measured on a MacBook M1 Pro 32GB RAM using 4 and 8 threads.
319+
312320
### Interactive mode
313321
314322
If you want a more ChatGPT-like experience, you can run in interactive mode by passing `-i` as a parameter.
@@ -407,26 +415,6 @@ If your issue is with model generation quality, then please at least scan the fo
407415
- [Aligning language models to follow instructions](https://openai.com/research/instruction-following)
408416
- [Training language models to follow instructions with human feedback](https://arxiv.org/abs/2203.02155)
409417

410-
### Perplexity (measuring model quality)
411-
412-
You can use the `perplexity` example to measure perplexity over the given prompt. For more background, see [https://huggingface.co/docs/transformers/perplexity](https://huggingface.co/docs/transformers/perplexity). However, in general, lower perplexity is better for LLMs.
413-
414-
#### Latest measurements
415-
416-
The latest perplexity scores for the various model sizes and quantizations are being tracked in [discussion #406](https://github.com/ggerganov/llama.cpp/discussions/406). `llama.cpp` is measuring very well compared to the baseline implementations. Quantization has a small negative impact on quality, but, as you can see, running
417-
13B at q4_0 beats the 7B f16 model by a significant amount.
418-
419-
All measurements are done against the wikitext2 test dataset (https://paperswithcode.com/dataset/wikitext-2), with default options (512 length context).
420-
Note that changing the context length will have a significant impact on perplexity (longer context = better perplexity).
421-
```
422-
Perplexity - model options
423-
5.5985 - 13B, q4_0
424-
5.9565 - 7B, f16
425-
6.3001 - 7B, q4_1
426-
6.5949 - 7B, q4_0
427-
6.5995 - 7B, q4_0, --memory_f16
428-
```
429-
430418
#### How to run
431419

432420
1. Download/extract: https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip?ref=salesforce-research

examples/common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ std::string gpt_random_prompt(std::mt19937 & rng) {
438438
// TODO: not great allocating this every time
439439
std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::string & text, bool add_bos) {
440440
// initialize to prompt numer of chars, since n_tokens <= n_prompt_chars
441-
std::vector<llama_token> res(text.size() + (int)add_bos);
442-
int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
441+
std::vector<llama_token> res(text.size() + (int) add_bos);
442+
const int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
443443
assert(n >= 0);
444444
res.resize(n);
445445

examples/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ int main(int argc, char ** argv) {
313313
if (n_past + (int) embd.size() > n_ctx) {
314314
const int n_left = n_past - params.n_keep;
315315

316-
n_past = params.n_keep;
316+
// always keep the first token - BOS
317+
n_past = std::max(1, params.n_keep);
317318

318319
// insert n_left/2 tokens at the start of embd from last_n_tokens
319320
embd.insert(embd.begin(), last_n_tokens.begin() + n_ctx - n_left/2 - embd.size(), last_n_tokens.end() - embd.size());
@@ -331,7 +332,6 @@ int main(int argc, char ** argv) {
331332
}
332333

333334
// try to reuse a matching prefix from the loaded session instead of re-eval (via n_past)
334-
// REVIEW
335335
if (n_session_consumed < (int) session_tokens.size()) {
336336
size_t i = 0;
337337
for ( ; i < embd.size(); i++) {

examples/perplexity/perplexity.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,68 @@ void perplexity(llama_context * ctx, const gpt_params & params) {
2525
// Download: https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip?ref=salesforce-research
2626
// Run `./perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
2727
// Output: `perplexity: 13.5106 [114/114]`
28+
// BOS tokens will be added for each chunk before eval
2829
auto tokens = ::llama_tokenize(ctx, params.prompt, true);
2930

30-
int count = 0;
31-
int seq_count = tokens.size() / params.n_ctx;
32-
int n_vocab = llama_n_vocab(ctx);
31+
int count = 0;
32+
33+
const int n_chunk = tokens.size() / params.n_ctx;
34+
const int n_vocab = llama_n_vocab(ctx);
35+
const int n_batch = params.n_batch;
3336

3437
double nll = 0.0;
35-
fprintf(stderr, "%s : calculating perplexity over %d chunks, batch_size=%d\n", __func__, seq_count, params.n_batch);
38+
fprintf(stderr, "%s: calculating perplexity over %d chunks, batch_size=%d\n", __func__, n_chunk, n_batch);
39+
40+
for (int i = 0; i < n_chunk; ++i) {
41+
const int start = i * params.n_ctx;
42+
const int end = start + params.n_ctx;
3643

37-
for (int i = 0; i < seq_count; ++i) {
38-
int start = i * params.n_ctx;
39-
int end = start + params.n_ctx;
44+
const int num_batches = (params.n_ctx + n_batch - 1) / n_batch;
4045

4146
std::vector<float> logits;
42-
int num_batches = (params.n_ctx + params.n_batch - 1) / params.n_batch;
43-
auto start_t = std::chrono::high_resolution_clock::now();
47+
48+
const auto t_start = std::chrono::high_resolution_clock::now();
49+
4450
for (int j = 0; j < num_batches; ++j) {
45-
int batch_start = start + j * params.n_batch;
46-
int batch_size = std::min(end - batch_start, params.n_batch);
47-
if (llama_eval(ctx, tokens.data() + batch_start, batch_size, j * params.n_batch, params.n_threads)) {
51+
const int batch_start = start + j * n_batch;
52+
const int batch_size = std::min(end - batch_start, n_batch);
53+
54+
// save original token and restore it after eval
55+
const auto token_org = tokens[batch_start];
56+
57+
// add BOS token for the first batch of each chunk
58+
if (j == 0) {
59+
tokens[batch_start] = llama_token_bos();
60+
}
61+
62+
if (llama_eval(ctx, tokens.data() + batch_start, batch_size, j * n_batch, params.n_threads)) {
4863
fprintf(stderr, "%s : failed to eval\n", __func__);
4964
return;
5065
}
51-
auto batch_logits = llama_get_logits(ctx);
66+
67+
// restore the original token in case it was set to BOS
68+
tokens[batch_start] = token_org;
69+
70+
const auto batch_logits = llama_get_logits(ctx);
5271
logits.insert(logits.end(), batch_logits, batch_logits + batch_size * n_vocab);
5372
}
54-
auto end_t = std::chrono::high_resolution_clock::now();
73+
74+
const auto t_end = std::chrono::high_resolution_clock::now();
75+
5576
if (i == 0) {
56-
const float seconds = std::chrono::duration<float>(end_t - start_t).count();
57-
printf("%.2f seconds per pass - ETA ", seconds);
58-
int total_seconds = (int)(seconds * seq_count);
77+
const float t_total = std::chrono::duration<float>(t_end - t_start).count();
78+
fprintf(stderr, "%s: %.2f seconds per pass - ETA ", __func__, t_total);
79+
int total_seconds = (int)(t_total * n_chunk);
5980
if (total_seconds >= 60*60) {
60-
printf("%d hours ", total_seconds / (60*60));
81+
fprintf(stderr, "%d hours ", total_seconds / (60*60));
6182
total_seconds = total_seconds % (60*60);
6283
}
63-
printf("%d minutes\n", total_seconds / 60);
84+
fprintf(stderr, "%d minutes\n", total_seconds / 60);
6485
}
86+
6587
// We get the logits for all the tokens in the context window (params.n_ctx)
6688
// from llama_eval above. Now, based on https://huggingface.co/docs/transformers/perplexity,
67-
// calculate the perplexity over the last half the window (so the model always has
89+
// calculate the perplexity over the last half of the window (so the model always has
6890
// some context to predict the token).
6991
//
7092
// We rely on the fact that attention in the forward pass only looks at previous
@@ -76,10 +98,12 @@ void perplexity(llama_context * ctx, const gpt_params & params) {
7698
// process the entire prompt.
7799
for (int j = std::min(512, params.n_ctx / 2); j < params.n_ctx - 1; ++j) {
78100
// Calculate probability of next token, given the previous ones.
79-
std::vector<float> tok_logits(
80-
logits.begin() + j * n_vocab,
101+
const std::vector<float> tok_logits(
102+
logits.begin() + (j + 0) * n_vocab,
81103
logits.begin() + (j + 1) * n_vocab);
82-
float prob = softmax(tok_logits)[tokens[start + j + 1]];
104+
105+
const float prob = softmax(tok_logits)[tokens[start + j + 1]];
106+
83107
nll += -std::log(prob);
84108
++count;
85109
}

llama.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,13 @@ static bool llama_eval_internal(
10521052
const int n_tokens,
10531053
const int n_past,
10541054
const int n_threads) {
1055+
1056+
// enforce that the first token is BOS
1057+
if (n_past == 0 && tokens[0] != llama_token_bos()) {
1058+
fprintf(stderr, "%s: first token must be BOS\n", __func__);
1059+
return false;
1060+
}
1061+
10551062
const int64_t t_start_us = ggml_time_us();
10561063

10571064
const int N = n_tokens;
@@ -1482,7 +1489,7 @@ static std::vector<llama_vocab::id> llama_tokenize(const llama_vocab & vocab, co
14821489
}
14831490

14841491
if (bos) {
1485-
output.push_back(1);
1492+
output.push_back(llama_token_bos());
14861493
}
14871494

14881495
tokenizer.tokenize(text, output);
@@ -2727,11 +2734,14 @@ int llama_eval(
27272734
fprintf(stderr, "%s: failed to eval\n", __func__);
27282735
return 1;
27292736
}
2737+
27302738
// get a more accurate load time, upon first eval
2739+
// TODO: fix this
27312740
if (!ctx->has_evaluated_once) {
27322741
ctx->t_load_us = ggml_time_us() - ctx->t_start_us;
27332742
ctx->has_evaluated_once = true;
27342743
}
2744+
27352745
return 0;
27362746
}
27372747

scripts/ppl-run-all.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
#
4+
# quantize
5+
#
6+
7+
# 7B
8+
time ./bin/quantize ../models/7B/ggml-model-f16.bin ../models/7B/ggml-model-q4_0.bin q4_0 2>&1 | tee ../qnt-7b-q4_0.txt
9+
time ./bin/quantize ../models/7B/ggml-model-f16.bin ../models/7B/ggml-model-q4_1.bin q4_1 2>&1 | tee ../qnt-7b-q4_1.txt
10+
time ./bin/quantize ../models/7B/ggml-model-f16.bin ../models/7B/ggml-model-q4_2.bin q4_2 2>&1 | tee ../qnt-7b-q4_2.txt
11+
time ./bin/quantize ../models/7B/ggml-model-f16.bin ../models/7B/ggml-model-q5_0.bin q5_0 2>&1 | tee ../qnt-7b-q5_0.txt
12+
time ./bin/quantize ../models/7B/ggml-model-f16.bin ../models/7B/ggml-model-q5_1.bin q5_1 2>&1 | tee ../qnt-7b-q5_1.txt
13+
time ./bin/quantize ../models/7B/ggml-model-f16.bin ../models/7B/ggml-model-q8_0.bin q8_0 2>&1 | tee ../qnt-7b-q8_0.txt
14+
15+
# 13B
16+
time ./bin/quantize ../models/13B/ggml-model-f16.bin ../models/13B/ggml-model-q4_0.bin q4_0 2>&1 | tee ../qnt-13b-q4_0.txt
17+
time ./bin/quantize ../models/13B/ggml-model-f16.bin ../models/13B/ggml-model-q4_1.bin q4_1 2>&1 | tee ../qnt-13b-q4_1.txt
18+
time ./bin/quantize ../models/13B/ggml-model-f16.bin ../models/13B/ggml-model-q4_2.bin q4_2 2>&1 | tee ../qnt-13b-q4_2.txt
19+
time ./bin/quantize ../models/13B/ggml-model-f16.bin ../models/13B/ggml-model-q5_0.bin q5_0 2>&1 | tee ../qnt-13b-q5_0.txt
20+
time ./bin/quantize ../models/13B/ggml-model-f16.bin ../models/13B/ggml-model-q5_1.bin q5_1 2>&1 | tee ../qnt-13b-q5_1.txt
21+
time ./bin/quantize ../models/13B/ggml-model-f16.bin ../models/13B/ggml-model-q8_0.bin q8_0 2>&1 | tee ../qnt-13b-q8_0.txt
22+
23+
#
24+
# perplexity
25+
#
26+
27+
# 7B
28+
time ./bin/perplexity -m ../models/7B/ggml-model-f16.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-7b-f16.txt
29+
time ./bin/perplexity -m ../models/7B/ggml-model-q4_0.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-7b-q4_0.txt
30+
time ./bin/perplexity -m ../models/7B/ggml-model-q4_1.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-7b-q4_1.txt
31+
time ./bin/perplexity -m ../models/7B/ggml-model-q4_2.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-7b-q4_2.txt
32+
time ./bin/perplexity -m ../models/7B/ggml-model-q5_0.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-7b-q5_0.txt
33+
time ./bin/perplexity -m ../models/7B/ggml-model-q5_1.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-7b-q5_1.txt
34+
time ./bin/perplexity -m ../models/7B/ggml-model-q8_0.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-7b-q8_0.txt
35+
36+
# 13B
37+
time ./bin/perplexity -m ../models/13B/ggml-model-f16.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-13b-f16.txt
38+
time ./bin/perplexity -m ../models/13B/ggml-model-q4_0.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-13b-q4_0.txt
39+
time ./bin/perplexity -m ../models/13B/ggml-model-q4_1.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-13b-q4_1.txt
40+
time ./bin/perplexity -m ../models/13B/ggml-model-q4_2.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-13b-q4_2.txt
41+
time ./bin/perplexity -m ../models/13B/ggml-model-q5_0.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-13b-q5_0.txt
42+
time ./bin/perplexity -m ../models/13B/ggml-model-q5_1.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-13b-q5_1.txt
43+
time ./bin/perplexity -m ../models/13B/ggml-model-q8_0.bin -f ./wiki.test.raw --no-mmap -t 12 2>&1 | tee ../ppl-13b-q8_0.txt

0 commit comments

Comments
 (0)