char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Ed Addarioquant : Optimise memory usage by evicting weights after processing each layer (#22877)059670428

char/aliceai-t5-moe
80.3 KiB2098 linesraw
1#include "arg.h"
2#include "common.h"
3#include "fit.h"
4#include "log.h"
5#include "llama.h"
6
7#include <algorithm>
8#include <array>
9#include <atomic>
10#include <chrono>
11#include <clocale>
12#include <cmath>
13#include <cstdio>
14#include <cstring>
15#include <ctime>
16#include <fstream>
17#include <mutex>
18#include <random>
19#include <sstream>
20#include <thread>
21#include <vector>
22
23#if defined(_MSC_VER)
24#pragma warning(disable: 4244 4267) // possible loss of data
25#endif
26
27struct results_perplexity {
28    std::vector<llama_token> tokens;
29    double                   ppl_value;
30    std::vector<float>       logits;
31    std::vector<float>       probs;
32};
33
34struct results_log_softmax {
35    double log_softmax;
36    float  logit;
37    float  prob;
38};
39
40static std::vector<float> softmax(const std::vector<float>& logits) {
41    std::vector<float> probs(logits.size());
42    float max_logit = logits[0];
43    for (float v : logits) {
44        max_logit = std::max(max_logit, v);
45    }
46    double sum_exp = 0.0;
47    for (size_t i = 0; i < logits.size(); i++) {
48        // Subtract the maximum logit value from the current logit value for numerical stability
49        const float logit = logits[i] - max_logit;
50        const float exp_logit = expf(logit);
51        sum_exp += exp_logit;
52        probs[i] = exp_logit;
53    }
54    for (size_t i = 0; i < probs.size(); i++) {
55        probs[i] /= sum_exp;
56    }
57    return probs;
58}
59
60static results_log_softmax log_softmax(int n_vocab, const float * logits, int tok) {
61    float max_logit = logits[0];
62    for (int i = 1; i < n_vocab; ++i) {
63        max_logit = std::max(max_logit, logits[i]);
64    }
65    double sum_exp = 0.0;
66    for (int i = 0; i < n_vocab; ++i) {
67        sum_exp += expf(logits[i] - max_logit);
68    }
69    return {logits[tok] - max_logit - log(sum_exp), logits[tok], expf(logits[tok] - max_logit) / (float) sum_exp};
70}
71
72static inline int nearest_int(float fval) {
73    //assert(fval <= 4194303.f);
74    float val = fval + 12582912.f;
75    int i; memcpy(&i, &val, sizeof(int));
76    return (i & 0x007fffff) - 0x00400000;
77}
78
79static double log_softmax(int n_vocab, const float * logits, uint16_t * log_prob, int tok) {
80    float max_logit = logits[0];
81    float min_logit = logits[0];
82    for (int i = 1; i < n_vocab; ++i) {
83        max_logit = std::max(max_logit, logits[i]);
84        min_logit = std::min(min_logit, logits[i]);
85    }
86    min_logit = std::max(min_logit, max_logit - 16);
87    double sum_exp = 0.0;
88    for (int i = 0; i < n_vocab; ++i) {
89        sum_exp += expf(logits[i] - max_logit);
90    }
91    const float log_sum_exp = log(sum_exp);
92    const float min_log_prob = min_logit - max_logit - log_sum_exp;
93    const float scale = (max_logit - min_logit)/65535.f;
94    float * d = (float *)log_prob;
95    d[0] = scale;
96    d[1] = min_log_prob;
97    log_prob += 4;
98    if (scale) {
99        const float inv_scale = 1/scale;
100        for (int i = 0; i < n_vocab; ++i) {
101            log_prob[i] = logits[i] > min_logit ? nearest_int(inv_scale*(logits[i] - min_logit)) : 0;
102        }
103    } else {
104        std::memset(log_prob, 0, n_vocab*sizeof(uint16_t));
105    }
106    return max_logit + log_sum_exp - logits[tok];
107}
108
109static void process_logits(
110    int n_vocab, const float * logits, const int * tokens, int n_token, std::vector<std::thread> & workers,
111    double & nll, double & nll2, float * logit_history, float * prob_history
112) {
113    std::mutex mutex;
114    int counter = 0;
115    auto compute = [&mutex, &counter, &nll, &nll2, logit_history, prob_history, n_vocab, logits, tokens, n_token] () {
116        double local_nll  = 0;
117        double local_nll2 = 0;
118        while (true) {
119            std::unique_lock<std::mutex> lock(mutex);
120            int i = counter++;
121            if (i >= n_token) {
122                nll += local_nll; nll2 += local_nll2;
123                break;
124            }
125            lock.unlock();
126            const results_log_softmax results = log_softmax(n_vocab, logits + size_t(i)*n_vocab, tokens[i+1]);
127            const double v = -results.log_softmax;
128            local_nll += v;
129            local_nll2 += v*v;
130
131            logit_history[i] = results.logit;
132            prob_history[i]  = results.prob;
133        }
134    };
135    for (auto & w : workers) {
136        w = std::thread(compute);
137    }
138    compute();
139    for (auto & w : workers) {
140        w.join();
141    }
142}
143
144static void process_logits(std::ostream& out, int n_vocab, const float * logits, const int * tokens, int n_token,
145        std::vector<std::thread> & workers, std::vector<uint16_t> & log_probs, double & nll, double & nll2) {
146    std::mutex mutex;
147    const int nv = 2*((n_vocab + 1)/2) + 4;
148    int counter = 0;
149    auto compute = [&mutex, &counter, &log_probs, &nll, &nll2, n_vocab, logits, tokens, n_token, nv] () {
150        double local_nll  = 0;
151        double local_nll2 = 0;
152        while (true) {
153            std::unique_lock<std::mutex> lock(mutex);
154            int i = counter++;
155            if (i >= n_token) {
156                nll += local_nll; nll2 += local_nll2;
157                break;
158            }
159            lock.unlock();
160            const double v = log_softmax(n_vocab, logits + size_t(i)*n_vocab, log_probs.data() + size_t(i)*nv, tokens[i+1]);
161            local_nll += v;
162            local_nll2 += v*v;
163        }
164    };
165    for (auto & w : workers) {
166        w = std::thread(compute);
167    }
168    compute();
169    for (auto & w : workers) {
170        w.join();
171    }
172    out.write((const char *)log_probs.data(), size_t(n_token)*nv*sizeof(uint16_t));
173}
174
175struct kl_divergence_result {
176    double sum_nll          = 0.0;
177    double sum_nll2         = 0.0;
178    double sum_nll_base     = 0.0;
179    double sum_nll_base2    = 0.0;
180    double sum_nll_nll_base = 0.0;
181    double sum_kld          = 0.0;
182    double sum_kld2         = 0.0;
183    double sum_p_diff       = 0.0;
184    double sum_p_diff2      = 0.0;
185    double sum_p_diff4      = 0.0;
186    float  max_p_diff       = 0.0f;
187    size_t n_same_top       = 0.0;
188    size_t count            = 0.0;
189};
190
191static std::pair<double, float> log_softmax(int n_vocab, const float * logits, const uint16_t * base_log_prob, int tok, kl_divergence_result & kld) {
192    float max_logit = logits[0];
193    int imax = 0;
194    for (int i = 1; i < n_vocab; ++i) {
195        if (logits[i] > max_logit) {
196            max_logit = logits[i];
197            imax = i;
198        }
199    }
200    double sum_exp = 0.0;
201    for (int i = 0; i < n_vocab; ++i) {
202        sum_exp += expf(logits[i] - max_logit);
203    }
204    const float log_sum_exp = log(sum_exp);
205    const float * d = (const float *)base_log_prob;
206    const float scale = d[0];
207    const float min_log_prob = d[1];
208    base_log_prob += 4;
209
210    const float nll = max_logit + log_sum_exp - logits[tok];
211    kld.sum_nll  += nll;
212    kld.sum_nll2 += nll*nll;
213
214    const float nll_base = -(scale*base_log_prob[tok] + min_log_prob);
215    kld.sum_nll_base  += nll_base;
216    kld.sum_nll_base2 += nll_base*nll_base;
217
218    kld.sum_nll_nll_base += nll*nll_base;
219
220    max_logit += log_sum_exp;
221    double sum = 0;
222    int imax_base = -1;
223    float p_log_base_max = 0;
224    for (int i = 0; i < n_vocab; ++i) {
225        const float p_log_base = scale*base_log_prob[i] + min_log_prob;
226        if (i == 0 || p_log_base > p_log_base_max) {
227            p_log_base_max = p_log_base;
228            imax_base = i;
229        }
230        if (p_log_base > -16.f) {
231            const float p_base = expf(p_log_base);
232            sum += p_base * (p_log_base - logits[i] + max_logit);
233        }
234    }
235    kld.sum_kld  += sum;
236    kld.sum_kld2 += sum*sum;
237    ++kld.count;
238    if (imax == imax_base) {
239        ++kld.n_same_top;
240    }
241
242    const float p_base = expf(-nll_base);
243    const float p = expf(-nll);
244    const float p_diff = p - p_base;
245    kld.sum_p_diff  += p_diff;
246    const double p_diff2 = p_diff*p_diff;
247    kld.sum_p_diff2 += p_diff2;
248    kld.sum_p_diff4 += p_diff2*p_diff2;
249    kld.max_p_diff = std::max(kld.max_p_diff, std::fabs(p_diff));
250
251    return std::make_pair(sum, p_diff);
252}
253
254static void process_logits(int n_vocab, const float * logits, const int * tokens, int n_token,
255        std::vector<std::thread> & workers, const std::vector<uint16_t> & base_log_probs, kl_divergence_result & kld,
256        float * kld_values, float * p_diff_values) {
257    std::mutex mutex;
258    const int nv = 2*((n_vocab + 1)/2) + 4;
259    int counter = 0;
260    auto compute = [&mutex, &counter, &base_log_probs, &kld, n_vocab, logits, tokens, n_token, nv, kld_values, p_diff_values] () {
261        kl_divergence_result local_kld;
262        while (true) {
263            std::unique_lock<std::mutex> lock(mutex);
264            int i = counter++;
265            if (i >= n_token) {
266                kld.sum_nll          += local_kld.sum_nll;
267                kld.sum_nll2         += local_kld.sum_nll2;
268                kld.sum_nll_base     += local_kld.sum_nll_base;
269                kld.sum_nll_base2    += local_kld.sum_nll_base2;
270                kld.sum_nll_nll_base += local_kld.sum_nll_nll_base;
271                kld.sum_kld          += local_kld.sum_kld;
272                kld.sum_kld2         += local_kld.sum_kld2;
273                kld.sum_p_diff       += local_kld.sum_p_diff;
274                kld.sum_p_diff2      += local_kld.sum_p_diff2;
275                kld.sum_p_diff4      += local_kld.sum_p_diff4;
276                kld.n_same_top       += local_kld.n_same_top;
277                kld.max_p_diff        = std::max(kld.max_p_diff, local_kld.max_p_diff);
278                kld.count            += local_kld.count;
279                break;
280            }
281            lock.unlock();
282            std::pair<double, float> v = log_softmax(n_vocab, logits + size_t(i)*n_vocab, base_log_probs.data() + size_t(i)*nv, tokens[i+1], local_kld);
283            kld_values[i]    = (float)v.first;
284            p_diff_values[i] = v.second;
285        }
286    };
287    for (auto & w : workers) {
288        w = std::thread(compute);
289    }
290    compute();
291    for (auto & w : workers) {
292        w.join();
293    }
294}
295
296static results_perplexity perplexity_v2(llama_context * ctx, const common_params & params) {
297    // Download: https://huggingface.co/datasets/ggml-org/ci/resolve/main/wikitext-2-raw-v1.zip
298    // Run `./perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
299    // Output: `perplexity: 13.5106 [114/114]`
300    // BOS tokens will be added for each chunk before eval
301
302    const llama_model * model = llama_get_model(ctx);
303    const llama_vocab * vocab = llama_model_get_vocab(model);
304
305    const bool add_bos = llama_vocab_get_add_bos(vocab);
306    GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
307
308    LOG_INF("%s: tokenizing the input ..\n", __func__);
309
310    std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, true);
311
312    const int n_ctx = llama_n_ctx(ctx);
313
314    if (int(tokens.size()) < 2*n_ctx) {
315        LOG_ERR("%s: you need at least %d tokens to evaluate perplexity with a context of %d\n",__func__,2*n_ctx,
316                n_ctx);
317        LOG_ERR("%s: the data file you provided tokenizes to only %zu tokens\n",__func__,tokens.size());
318        return {std::move(tokens), 0., {}, {}};
319    }
320
321    std::vector<float> logit_history;
322    std::vector<float> prob_history;
323
324    logit_history.resize(tokens.size());
325    prob_history.resize(tokens.size());
326
327    if (params.ppl_stride <= 0) {
328        LOG_ERR("%s: stride is %d but must be greater than zero!\n",__func__,params.ppl_stride);
329        return {tokens, -1, logit_history, prob_history};
330    }
331
332    const int calc_chunk = n_ctx;
333
334    LOG_INF("%s: have %zu tokens. Calculation chunk = %d\n", __func__, tokens.size(), calc_chunk);
335
336    if (int(tokens.size()) <= calc_chunk) {
337        LOG_ERR("%s: there are only %zu tokens, this is not enough for a context size of %d and stride %d\n",__func__,
338                tokens.size(), n_ctx, params.ppl_stride);
339        return {tokens, -1, logit_history, prob_history};
340    }
341
342    const int n_chunk_max = (tokens.size() - calc_chunk + params.ppl_stride - 1)  / params.ppl_stride;
343
344    const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
345    const int n_batch = params.n_batch;
346
347    const int n_vocab = llama_vocab_n_tokens(vocab);
348
349    int count = 0;
350    double nll = 0.0;
351
352    const int n_seq = std::max(1, n_batch / n_ctx);
353    LOG_INF("%s: computing over %d chunks, n_ctx=%d, batch_size=%d, n_seq=%d\n", __func__, n_chunk, n_ctx, n_batch, n_seq);
354
355    for (int i = 0; i < n_chunk; ++i) {
356        const int start =     i * params.ppl_stride;
357        const int end   = start + calc_chunk;
358
359        const int num_batches = (calc_chunk + n_batch - 1) / n_batch;
360        //LOG_DBG("%s: evaluating %d...%d using %d batches\n", __func__, start, end, num_batches);
361
362        std::vector<float> logits;
363
364        const auto t_start = std::chrono::high_resolution_clock::now();
365
366        // clear the KV cache
367        llama_memory_clear(llama_get_memory(ctx), true);
368
369        llama_batch batch = llama_batch_init(n_batch, 0, 1);
370
371        for (int j = 0; j < num_batches; ++j) {
372            const int batch_start = start + j * n_batch;
373            const int batch_size  = std::min(end - batch_start, n_batch);
374
375            common_batch_clear(batch);
376            for (int i = 0; i < batch_size; i++) {
377                common_batch_add(batch, tokens[batch_start + i], j*n_batch + i, {0}, true);
378            }
379
380            //LOG_DBG("    Batch %d: starts at %d, size is %d, n_past is %d\n",j,batch_start,batch_size,j * n_batch);
381            if (llama_decode(ctx, batch)) {
382                //LOG_ERR("%s : failed to eval\n", __func__);
383                llama_batch_free(batch);
384                return {tokens, -1, logit_history, prob_history};
385            }
386
387            // save original token and restore it after eval
388            const auto token_org = tokens[batch_start];
389
390            // add BOS token for the first batch of each chunk
391            if (add_bos && j == 0) {
392                tokens[batch_start] = llama_vocab_bos(vocab);
393            }
394
395            const auto * batch_logits = llama_get_logits(ctx);
396            logits.insert(logits.end(), batch_logits, batch_logits + size_t(batch_size) * n_vocab);
397
398            if (j == 0) {
399                tokens[batch_start] = token_org;
400            }
401        }
402
403        llama_batch_free(batch);
404
405        const auto t_end = std::chrono::high_resolution_clock::now();
406
407        if (i == 0) {
408            const float t_total = std::chrono::duration<float>(t_end - t_start).count();
409            LOG_INF("%s: %.2f seconds per pass - ETA ", __func__, t_total);
410            int total_seconds = (int)(t_total * n_chunk);
411            if (total_seconds >= 60*60) {
412                LOG("%d hours ", total_seconds / (60*60));
413                total_seconds = total_seconds % (60*60);
414            }
415            LOG("%.2f minutes\n", total_seconds / 60.0);
416        }
417
418        //LOG_DBG("%s: using tokens %d...%d\n",__func__,params.n_ctx - params.ppl_stride + start, params.n_ctx + start);
419        for (int j = n_ctx - params.ppl_stride - 1; j < n_ctx - 1; ++j) {
420            // Calculate probability of next token, given the previous ones.
421            const std::vector<float> tok_logits(
422                logits.begin() + size_t(j + 0) * n_vocab,
423                logits.begin() + size_t(j + 1) * n_vocab);
424
425            const float prob = softmax(tok_logits)[tokens[start + j + 1]];
426            logit_history[start + j + 1] = tok_logits[tokens[start + j + 1]];
427            prob_history[start + j + 1]  = prob;
428
429            nll += -std::log(prob);
430            ++count;
431        }
432        // perplexity is e^(average negative log-likelihood)
433        if (params.ppl_output_type == 0) {
434            LOG("[%d]%.4lf,", i + 1, std::exp(nll / count));
435        } else {
436            LOG("%8d  %.4lf\n", i*params.ppl_stride, std::exp(nll / count));
437        }
438    }
439    LOG("\n");
440
441    return {tokens, std::exp(nll / count), logit_history, prob_history};
442}
443
444static results_perplexity perplexity(llama_context * ctx, const common_params & params, const int32_t n_ctx) {
445    if (params.ppl_stride > 0) {
446        return perplexity_v2(ctx, params);
447    }
448
449    // Download: https://huggingface.co/datasets/ggml-org/ci/resolve/main/wikitext-2-raw-v1.zip
450    // Run `./llama-perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
451    // Output: `perplexity: 13.5106 [114/114]`
452    // BOS tokens will be added for each chunk before eval
453
454    const llama_model * model = llama_get_model(ctx);
455    const llama_vocab * vocab = llama_model_get_vocab(model);
456
457    const bool add_bos = llama_vocab_get_add_bos(vocab);
458    GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
459
460    std::ofstream logits_stream;
461    if (!params.logits_file.empty()) {
462        logits_stream.open(params.logits_file.c_str(), std::ios::binary);
463        if (!logits_stream.is_open()) {
464            LOG_ERR("%s: failed to open %s for writing\n", __func__, params.logits_file.c_str());
465            return {};
466        }
467        LOG_INF("%s: saving all logits to %s\n", __func__, params.logits_file.c_str());
468        logits_stream.write("_logits_", 8);
469        logits_stream.write(reinterpret_cast<const char *>(&n_ctx), sizeof(n_ctx));
470    }
471
472    auto tim1 = std::chrono::high_resolution_clock::now();
473    LOG_INF("%s: tokenizing the input ..\n", __func__);
474
475    std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, true);
476
477    auto tim2 = std::chrono::high_resolution_clock::now();
478    LOG_INF("%s: tokenization took %g ms\n",__func__,1e-3*std::chrono::duration_cast<std::chrono::microseconds>(tim2-tim1).count());
479
480    if (int(tokens.size()) < 2*n_ctx) {
481        LOG_ERR("%s: you need at least %d tokens to evaluate perplexity with a context of %d\n",__func__,2*n_ctx,
482                n_ctx);
483        LOG_ERR("%s: the data file you provided tokenizes to only %zu tokens\n",__func__,tokens.size());
484        return {std::move(tokens), 0., {}, {}};
485    }
486
487    std::vector<float> logit_history;
488    logit_history.resize(tokens.size());
489
490    std::vector<float> prob_history;
491    prob_history.resize(tokens.size());
492
493    const int n_chunk_max = tokens.size() / n_ctx;
494
495    const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
496    const int n_batch = params.n_batch;
497
498    const int n_vocab = llama_vocab_n_tokens(vocab);
499
500    int count = 0;
501    double nll = 0.0;
502    double nll2 = 0.0;
503
504    const int num_batches = (n_ctx + n_batch - 1) / n_batch;
505    const int n_seq = std::max(1, n_batch / n_ctx);
506
507    GGML_ASSERT(n_batch < n_ctx || n_batch % n_ctx == 0);
508    GGML_ASSERT(params.n_ctx == n_seq * n_ctx);
509
510    llama_batch batch = llama_batch_init(std::min(n_batch, n_ctx*n_seq), 0, 1);
511
512    std::vector<float> logits;
513    if (num_batches > 1) {
514        logits.reserve(size_t(n_ctx) * n_vocab);
515    }
516
517    LOG_INF("%s: calculating perplexity over %d chunks, n_ctx=%d, batch_size=%d, n_seq=%d\n", __func__, n_chunk, n_ctx, n_batch, n_seq);
518
519    std::vector<std::thread> workers(std::thread::hardware_concurrency() - 1);
520
521    std::vector<uint16_t> log_probs;
522    if (!params.logits_file.empty()) {
523        logits_stream.write((const char *)&n_vocab, sizeof(n_vocab));
524        logits_stream.write((const char *)&n_chunk, sizeof(n_chunk));
525        logits_stream.write((const char *)tokens.data(), n_chunk*n_ctx*sizeof(tokens[0]));
526        const int nv = 2*((n_vocab + 1)/2) + 4;
527        log_probs.resize(size_t(n_ctx) * nv);
528    }
529
530    // We get the logits for all the tokens in the context window (params.n_ctx)
531    // from llama_decode below.  Now, based on https://huggingface.co/docs/transformers/perplexity,
532    // calculate the perplexity over the last half of the window (so the model always has
533    // some context to predict the token).
534    //
535    // We rely on the fact that attention in the forward pass only looks at previous
536    // tokens here, so the logits returned for each token are an accurate representation
537    // of what the model would have predicted at that point.
538    //
539    // Example, we have a context window of 512, we will compute perplexity for each of the
540    // last 256 tokens.  Then, we split the input up into context window size chunks to
541    // process the entire prompt.
542    const int first = n_ctx/2;
543
544    for (int i = 0; i < n_chunk; i += n_seq) {
545        const int start =     i * n_ctx;
546        const int end   = start + n_ctx;
547
548        const int n_seq_batch = std::min(n_seq, n_chunk - i);
549
550        const auto t_start = std::chrono::high_resolution_clock::now();
551
552        // clear the KV cache
553        llama_memory_clear(llama_get_memory(ctx), true);
554
555        for (int j = 0; j < num_batches; ++j) {
556            const int batch_start = start + j * n_batch;
557            const int batch_size  = std::min(end - batch_start, n_batch);
558
559            int n_outputs = 0;
560
561            batch.n_tokens = 0;
562            for (int seq = 0; seq < n_seq_batch; seq++) {
563                int seq_start = batch_start + seq*n_ctx;
564
565                // save original token and restore it after decode
566                const auto token_org = tokens[seq_start];
567
568                // add BOS token for the first batch of each chunk
569                if (add_bos && j == 0) {
570                    tokens[seq_start] = llama_vocab_bos(vocab);
571                }
572
573                for (int k = 0; k < batch_size; ++k) {
574                    const int idx = seq*n_ctx + k;
575                    batch.token   [idx]    = tokens[seq_start + k];
576                    batch.pos     [idx]    = j*n_batch + k;
577                    batch.n_seq_id[idx]    = 1;
578                    batch.seq_id  [idx][0] = seq;
579                    batch.logits  [idx]    = batch.pos[idx] >= first ? 1 : 0;
580
581                    n_outputs += batch.logits[idx] != 0;
582                }
583                batch.n_tokens += batch_size;
584
585                // restore the original token in case it was set to BOS
586                tokens[seq_start] = token_org;
587            }
588
589            if (llama_decode(ctx, batch)) {
590                LOG_INF("%s : failed to decode\n", __func__);
591                return {tokens, -1, logit_history, prob_history};
592            }
593
594            if (num_batches > 1 && n_outputs > 0) {
595                const auto * batch_logits = llama_get_logits(ctx);
596                logits.insert(logits.end(), batch_logits, batch_logits + size_t(n_outputs) * n_vocab);
597            }
598        }
599
600
601        if (i == 0) {
602            llama_synchronize(ctx);
603            const auto t_end = std::chrono::high_resolution_clock::now();
604            const float t_total = std::chrono::duration<float>(t_end - t_start).count();
605            LOG_INF("%s: %.2f seconds per pass - ETA ", __func__, t_total);
606            int total_seconds = (int)(t_total*n_chunk/n_seq);
607            if (total_seconds >= 60*60) {
608                LOG("%d hours ", total_seconds / (60*60));
609                total_seconds = total_seconds % (60*60);
610            }
611            LOG("%.2f minutes\n", total_seconds / 60.0);
612        }
613
614        for (int seq = 0; seq < n_seq_batch; seq++) {
615            const float * all_logits = num_batches > 1 ? logits.data() : llama_get_logits_ith(ctx, seq*n_ctx + first);
616
617            llama_token * tokens_data = tokens.data() + start + seq*n_ctx + first;
618            if (!params.logits_file.empty()) {
619                process_logits(logits_stream, n_vocab, all_logits,
620                        tokens_data, n_ctx - 1 - first,
621                        workers, log_probs, nll, nll2);
622            } else {
623                process_logits(n_vocab, all_logits,
624                        tokens_data, n_ctx - 1 - first,
625                        workers, nll, nll2,
626                        logit_history.data() + start + seq*n_ctx + first,
627                        prob_history.data()  + start + seq*n_ctx + first);
628            }
629            count += n_ctx - first - 1;
630
631            // perplexity is e^(average negative log-likelihood)
632            if (params.ppl_output_type == 0) {
633                LOG("[%d]%.4lf,", i + seq + 1, std::exp(nll / count));
634            } else {
635                double av = nll/count;
636                double av2 = nll2/count - av*av;
637                if (av2 > 0) {
638                    av2 = sqrt(av2/(count-1));
639                }
640                LOG("%8d  %.4lf  %4lf  %4lf\n", i*n_ctx, std::exp(nll / count), av, av2);
641            }
642        }
643
644        logits.clear();
645    }
646    LOG("\n");
647
648    nll2 /= count;
649    nll /= count;
650    const double ppl = exp(nll);
651    nll2 -= nll * nll;
652    if (nll2 > 0) {
653        nll2 = sqrt(nll2/(count-1));
654        LOG_INF("Final estimate: PPL = %.4lf +/- %.5lf\n", ppl, nll2*ppl);
655    } else {
656        LOG_ERR("Unexpected negative standard deviation of log(prob)\n");
657    }
658
659    llama_batch_free(batch);
660
661    return {tokens, ppl, logit_history, prob_history};
662}
663
664static bool decode_helper(llama_context * ctx, llama_batch & batch, std::vector<float> & batch_logits, int n_batch, int n_vocab) {
665    int prev_outputs = 0;
666    for (int i = 0; i < (int) batch.n_tokens; i += n_batch) {
667        const int n_tokens = std::min<int>(n_batch, batch.n_tokens - i);
668
669        llama_batch batch_view = {
670            n_tokens,
671            batch.token    + i,
672            nullptr,
673            batch.pos      + i,
674            batch.n_seq_id + i,
675            batch.seq_id   + i,
676            batch.logits   + i,
677        };
678
679        const int ret = llama_decode(ctx, batch_view);
680        if (ret != 0) {
681            LOG_ERR("failed to decode the batch, n_batch = %d, ret = %d\n", n_batch, ret);
682            return false;
683        }
684
685        int n_outputs = 0;
686        for (int i = 0; i < n_tokens; ++i) {
687            n_outputs += batch_view.logits[i] != 0;
688        }
689
690        memcpy(batch_logits.data() + size_t(prev_outputs)*n_vocab, llama_get_logits(ctx), size_t(n_outputs)*n_vocab*sizeof(float));
691
692        prev_outputs += n_outputs;
693    }
694
695    return true;
696}
697
698#define K_TOKEN_CHUNK 4
699
700static void compute_logprobs(const float * batch_logits, int n_vocab, std::vector<std::thread>& workers,
701        const std::vector<std::pair<size_t, llama_token>>& eval_pairs, std::vector<float>& eval_results) {
702    if (eval_results.size() != eval_pairs.size()) {
703        eval_results.resize(eval_pairs.size());
704    }
705    if (eval_pairs.empty()) {
706        return;
707    }
708
709    size_t max_threads = std::min((eval_pairs.size() + K_TOKEN_CHUNK - 1)/K_TOKEN_CHUNK, workers.size());
710
711    std::atomic<int> counter(0);
712    auto compute = [&counter, &eval_pairs, &eval_results, batch_logits, n_vocab] () {
713        float local_logprobs[K_TOKEN_CHUNK];
714        while (true) {
715            const size_t first = counter.fetch_add(K_TOKEN_CHUNK, std::memory_order_relaxed);
716            if (first >= eval_results.size()) {
717                break;
718            }
719            const size_t last = std::min(first + K_TOKEN_CHUNK, eval_results.size());
720            for (size_t i = first; i < last; ++i) {
721                const auto * logits = batch_logits + eval_pairs[i].first * n_vocab;
722                float max_logit = logits[0];
723                for (int j = 1; j < n_vocab; ++j) {
724                    max_logit = std::max(max_logit, logits[j]);
725                }
726                float sum_p = 0.f;
727                for (int j = 0; j < n_vocab; ++j) {
728                    sum_p += expf(logits[j] - max_logit);
729                }
730                local_logprobs[i - first] = logits[eval_pairs[i].second] - max_logit - std::log(sum_p);
731            }
732            std::memcpy(eval_results.data() + first, local_logprobs, (last - first)*sizeof(float));
733        }
734    };
735
736    for (size_t it = 0; it < max_threads; ++it) {
737        workers[it] = std::thread(compute);
738    }
739    for (size_t it = 0; it < max_threads; ++it) {
740        workers[it].join();
741    }
742}
743
744static void hellaswag_score(llama_context * ctx, const common_params & params) {
745    const llama_model * model = llama_get_model(ctx);
746    const llama_vocab * vocab = llama_model_get_vocab(model);
747
748    // Calculates hellaswag score (acc_norm) from prompt
749    //
750    // Data extracted from the HellaSwag validation dataset (MIT license) https://github.com/rowanz/hellaswag/blob/master/data/hellaswag_val.jsonl
751    // All used data fields are preprocessed as in https://github.com/EleutherAI/lm-evaluation-harness/blob/df3da98c5405deafd519c2ddca52bb7c3fe36bef/lm_eval/tasks/hellaswag.py#L62-L68
752    //
753    // All 10042 tasks should be extracted to keep the results standardized like other implementations.
754    //
755    // Datafile layout:
756    // ['??'] denotes json fields
757    // 6 lines per task:
758    // ['activity_label'] + ": " +['ctx']  - The first part of the query, the context
759    // ['label'] - The index the best common sense ending aka gold ending
760    // ['endings'][0] - Endings added to the first part of the query
761    // ['endings'][1]
762    // ['endings'][2]
763    // ['endings'][3]
764
765    std::vector<std::string> prompt_lines;
766    std::istringstream strstream(params.prompt);
767    std::string line;
768
769    while (std::getline(strstream,line,'\n')) {
770        prompt_lines.push_back(line);
771    }
772
773    if (prompt_lines.size() % 6 != 0) {
774        LOG_ERR("%s : number of lines in prompt not a multiple of 6.\n", __func__);
775        return;
776    }
777
778    size_t hs_task_count = prompt_lines.size()/6;
779    LOG_INF("%s : loaded %zu tasks from prompt.\n", __func__, hs_task_count);
780
781    const bool is_spm = llama_vocab_type(vocab) == LLAMA_VOCAB_TYPE_SPM;
782    LOG_INF("================================= is_spm = %d\n", is_spm);
783
784    // The tasks should be randomized so the score stabilizes quickly.
785    bool randomize_tasks = true;
786
787    // Number of tasks to use when computing the score
788    if (params.hellaswag_tasks < hs_task_count) {
789        hs_task_count = params.hellaswag_tasks;
790    }
791
792    // The random seed should not impact the final result if the computation is done over enough tasks, so kept hardcoded for now
793    std::mt19937 rng(1);
794
795    // Dataholder for hellaswag tasks
796    struct hs_data_t {
797        std::string context;
798        size_t gold_ending_idx;
799        std::string ending[4];
800        size_t ending_logprob_count[4];
801        double ending_logprob[4];
802
803        size_t i_logits;        // starting index of logits in the llama_batch
804        size_t common_prefix;   // max number of initial tokens that are the same in all sentences
805        size_t required_tokens; // needed number of tokens to evaluate all 4 endings
806        std::vector<llama_token> seq_tokens[4];
807    };
808
809    LOG_INF("%s : selecting %zu %s tasks.\n", __func__, hs_task_count, (randomize_tasks?"randomized":"the first")  );
810
811    // Select and read data from prompt lines
812    std::vector<hs_data_t> hs_data(hs_task_count);
813    for (size_t i = 0; i < hs_task_count; i++) {
814        size_t idx = i;
815
816        auto & hs_cur = hs_data[i];
817
818        // Select a random example of those left in the prompt
819        if (randomize_tasks) {
820            std::uniform_int_distribution<size_t> dist(0, prompt_lines.size()/6-1 ) ;
821            idx = dist(rng);
822        }
823
824        hs_cur.context = prompt_lines[idx*6];
825        hs_cur.gold_ending_idx = std::stoi( prompt_lines[idx*6+1] );
826        for (size_t j = 0; j < 4; j++) {
827            hs_cur.ending[j] = prompt_lines[idx*6+2+j];
828            hs_cur.seq_tokens[j] = common_tokenize(ctx, hs_cur.context + " " + hs_cur.ending[j], true);
829        }
830
831        // determine the common prefix of the endings
832        hs_cur.common_prefix = 0;
833        for (size_t k = 0; k < hs_cur.seq_tokens[0].size(); k++) {
834            if (hs_cur.seq_tokens[0][k] != hs_cur.seq_tokens[1][k] ||
835                hs_cur.seq_tokens[0][k] != hs_cur.seq_tokens[2][k] ||
836                hs_cur.seq_tokens[0][k] != hs_cur.seq_tokens[3][k]) {
837                break;
838            }
839            hs_cur.common_prefix++;
840        }
841        hs_cur.required_tokens = hs_cur.common_prefix +
842            hs_cur.seq_tokens[0].size() - hs_cur.common_prefix +
843            hs_cur.seq_tokens[1].size() - hs_cur.common_prefix +
844            hs_cur.seq_tokens[2].size() - hs_cur.common_prefix +
845            hs_cur.seq_tokens[3].size() - hs_cur.common_prefix;
846
847        //GGML_ASSERT(hs_cur.common_prefix >= ::llama_tokenize(ctx, hs_cur.context, true).size());
848
849        // Delete the selected random example from the prompt
850        if (randomize_tasks) {
851            prompt_lines.erase( std::next(prompt_lines.begin(),idx*6)  , std::next(prompt_lines.begin(),idx*6+6) );
852        }
853    }
854
855    LOG_INF("%s : calculating hellaswag score over selected tasks.\n", __func__);
856
857    LOG("\ntask\tacc_norm\t95%% confidence interval\n");
858
859    double acc = 0.0f;
860
861    const int n_ctx   = llama_n_ctx(ctx);
862    const int n_batch = params.n_batch;
863
864    const int n_vocab = llama_vocab_n_tokens(vocab);
865
866    const int max_tasks_per_batch = 32;
867    const int max_seq = std::min(4*max_tasks_per_batch, (int) llama_n_seq_max(ctx));
868
869    llama_batch batch = llama_batch_init(n_ctx, 0, 4);
870
871    std::vector<float> tok_logits(n_vocab);
872    // TODO: this could be made smaller; it's currently the worst-case size
873    std::vector<float> batch_logits(size_t(n_ctx)*n_vocab);
874
875    std::vector<std::pair<size_t, llama_token>> eval_pairs;
876    std::vector<float> eval_results;
877    std::vector<std::thread> workers(std::thread::hardware_concurrency());
878
879    for (size_t i0 = 0; i0 < hs_task_count; i0++) {
880        int n_cur = 0;
881
882        size_t i1 = i0;
883        size_t i_logits = 0; // this tells us how many logits were needed before this point in the batch
884
885        common_batch_clear(batch);
886
887        // batch as much tasks as possible into the available context
888        // each task has 4 unique sequence ids - one for each ending
889        // the common prefix is shared among the 4 sequences to save tokens
890        // we extract logits only from the last common token and from all ending tokens of each sequence
891        while (n_cur + (int) hs_data[i1].required_tokens <= n_ctx) {
892            auto & hs_cur = hs_data[i1];
893            int n_logits = 0;
894
895            const int s0 = 4*(i1 - i0);
896            if (s0 + 4 > max_seq) {
897                break;
898            }
899
900            for (size_t i = 0; i < hs_cur.common_prefix; ++i) {
901                common_batch_add(batch, hs_cur.seq_tokens[0][i], i, { s0 + 0, s0 + 1, s0 + 2, s0 + 3 }, false);
902            }
903            batch.logits[batch.n_tokens - 1] = true; // we need logits for the last token of the common prefix
904            n_logits += 1;
905
906            for (int s = 0; s < 4; ++s) {
907                const size_t seq_tokens_size = hs_cur.seq_tokens[s].size();
908                // TODO: don't evaluate the last token of each sequence
909                for (size_t i = hs_cur.common_prefix; i < seq_tokens_size; ++i) {
910                    const bool needs_logits = i < seq_tokens_size - 1;
911                    common_batch_add(batch, hs_cur.seq_tokens[s][i], i, { s0 + s }, needs_logits);
912                    n_logits += needs_logits;
913                }
914            }
915
916            hs_cur.i_logits = i_logits;
917            i_logits += n_logits;
918
919            n_cur += hs_data[i1].required_tokens;
920            if (++i1 == hs_task_count) {
921                break;
922            }
923        }
924
925        if (i0 == i1) {
926            LOG_ERR("%s : task %zu does not fit in the context window (requires %zu tokens)\n", __func__, i0, hs_data[i0].required_tokens);
927            return;
928        }
929
930        llama_memory_clear(llama_get_memory(ctx), true);
931
932        // decode all tasks [i0, i1)
933        if (!decode_helper(ctx, batch, batch_logits, n_batch, n_vocab)) {
934            LOG_ERR("%s: llama_decode() failed\n", __func__);
935            return;
936        }
937
938        // Compute log-probs in parallel
939        // First we collect all tasks
940        eval_pairs.clear();
941        for (size_t i = i0; i < i1; ++i) {
942            auto & hs_cur = hs_data[i];
943            size_t li = 1; // skip the last logit of the common prefix (computed separately below)
944            for (int s = 0; s < 4; ++s) {
945                for (size_t j = hs_cur.common_prefix; j < hs_cur.seq_tokens[s].size() - 1; j++) {
946                    eval_pairs.emplace_back(hs_cur.i_logits + li++, hs_cur.seq_tokens[s][j + 1]);
947                }
948            }
949        }
950        // Then we do the actual calculation
951        compute_logprobs(batch_logits.data(), n_vocab, workers, eval_pairs, eval_results);
952
953        size_t ir = 0;
954
955        // compute the logprobs for each ending of the decoded tasks
956        for (size_t i = i0; i < i1; ++i) {
957            auto & hs_cur = hs_data[i];
958
959            // get the logits of the last token of the common prefix
960            std::memcpy(tok_logits.data(), batch_logits.data() + hs_cur.i_logits*n_vocab, n_vocab*sizeof(float));
961
962            const auto first_probs = softmax(tok_logits);
963
964            for (int s = 0; s < 4; ++s) {
965                hs_cur.ending_logprob_count[s] = 1;
966                hs_cur.ending_logprob[s] = std::log(first_probs[hs_cur.seq_tokens[s][hs_cur.common_prefix]]);
967                for (size_t j = hs_cur.common_prefix; j < hs_cur.seq_tokens[s].size() - 1; j++) {
968                    hs_cur.ending_logprob[s] += eval_results[ir++];
969                    hs_cur.ending_logprob_count[s]++;
970                }
971                hs_cur.ending_logprob[s] /= hs_cur.ending_logprob_count[s];
972            }
973
974            // Find the ending with maximum logprob
975            size_t ending_logprob_max_idx = 0;
976            double ending_logprob_max_val = hs_cur.ending_logprob[0];
977            for (size_t s = 1; s < 4; s++) {
978                if (hs_cur.ending_logprob[s] > ending_logprob_max_val) {
979                    ending_logprob_max_idx = s;
980                    ending_logprob_max_val =  hs_cur.ending_logprob[s];
981                }
982            }
983
984            //LOG("max logprob ending idx %lu, gold ending idx %lu\n", ending_logprob_max_idx, hs_cur.gold_ending_idx);
985
986            // If the gold ending got the maximum logprobe add one accuracy point
987            if (ending_logprob_max_idx == hs_cur.gold_ending_idx) {
988                acc += 1.0;
989            }
990
991            double freq = acc / double(i + 1);
992
993            const double za = 1.95996398454;
994
995            // // Wald normal approx
996            // double conf =za*sqrt(freq*(1-freq)/double(i + 1));
997            // LOG("%zu\t%.8lf +/- %.8lf\n", i + 1, freq*100.0, conf*100.0);
998
999            // Wilson score interval, more accurate
1000            double z   = za * za / double(i + 1);
1001            double cnf = z * sqrt(double(i + 1) * (4.0 * freq * (1 - freq) + z)) / (za + za);
1002            double a   = (freq + z * 0.5 - cnf) / (1.0 + z);
1003            double b   = (freq + z * 0.5 + cnf) / (1.0 + z);
1004
1005            // Print the accumulated accuracy mean x 100 and confidence interval
1006            LOG("%zu\t%3.8lf%%\t[%3.4lf%%, %3.4lf%%]\n", i + 1, freq * 100.0, a * 100.0, b * 100.0);
1007        }
1008
1009        i0 = i1 - 1;
1010    }
1011
1012    llama_batch_free(batch);
1013
1014    LOG("\n");
1015}
1016
1017struct winogrande_entry {
1018    std::string first;
1019    std::string second;
1020    std::array<std::string, 2> choices;
1021    int answer;
1022
1023    size_t i_logits;
1024    size_t common_prefix;
1025    size_t required_tokens;
1026    size_t n_base1; // number of tokens for context + choice 1
1027    size_t n_base2; // number of tokens for context + choice 2
1028    std::vector<llama_token> seq_tokens[2];
1029};
1030
1031static std::vector<winogrande_entry> load_winogrande_from_csv(const std::string & prompt) {
1032    std::vector<winogrande_entry> result;
1033    std::istringstream in(prompt);
1034    std::string line;
1035    std::array<int, 4> comma_pos;
1036    while (true) {
1037        std::getline(in, line);
1038        if (in.fail() || in.eof()) break;
1039        int ipos = 0;
1040        bool quote_open = false;
1041        for (int i = 0; i < int(line.size()); ++i) {
1042            if (!quote_open) {
1043                if (line[i] == ',') {
1044                    comma_pos[ipos++] = i;
1045                    if (ipos == 4) break;
1046                }
1047                else if (line[i] == '"') {
1048                    quote_open = true;
1049                }
1050            }
1051            else {
1052                if (line[i] == '"') {
1053                    quote_open = false;
1054                }
1055            }
1056        }
1057        if (ipos != 4) {
1058            LOG_ERR("%s: failed to find comma separators in <%s>\n", __func__, line.c_str());
1059            continue;
1060        }
1061        auto sentence = line[comma_pos[0]+1] == '"' ? line.substr(comma_pos[0]+2, comma_pos[1] - comma_pos[0] - 3)
1062                                                    : line.substr(comma_pos[0]+1, comma_pos[1] - comma_pos[0] - 1);
1063        auto choice1 = line.substr(comma_pos[1]+1, comma_pos[2] - comma_pos[1] - 1);
1064        auto choice2 = line.substr(comma_pos[2]+1, comma_pos[3] - comma_pos[2] - 1);
1065        auto answer  = line.substr(comma_pos[3]+1, line.size() - comma_pos[3] - 1);
1066        auto index = line.substr(0, comma_pos[0]);
1067        int where = 0;
1068        for ( ; where < int(sentence.size()); ++where) {
1069            if (sentence[where] == '_') break;
1070        }
1071        if (where == int(sentence.size())) {
1072            LOG_ERR("%s: no _ in <%s>\n", __func__, sentence.c_str());
1073            continue;
1074        }
1075        std::istringstream stream(answer.c_str());
1076        int i_answer; stream >> i_answer;
1077        if (stream.fail() || i_answer < 1 || i_answer > 2) {
1078            LOG_ERR("%s: failed to parse answer <%s>\n", __func__, answer.c_str());
1079            continue;
1080        }
1081        result.emplace_back();
1082        auto& wg = result.back();
1083        wg.first = sentence.substr(0, where);
1084        wg.second = sentence.substr(where + 1, sentence.size() - where - 1);
1085        wg.choices[0] = std::move(choice1);
1086        wg.choices[1] = std::move(choice2);
1087        wg.answer = i_answer;
1088    }
1089    return result;
1090}
1091
1092/*
1093 * Evaluates the Winogrande score.
1094 * Uses a CSV containing task index, dentence, choice 1, choice 2, answer (1 or 2)
1095 * You can get one such dataset from e.g. https://huggingface.co/datasets/ikawrakow/winogrande-eval-for-llama.cpp
1096 * As an example, the 1st row in the above dataset is
1097 *
1098 *    0,Sarah was a much better surgeon than Maria so _ always got the easier cases.,Sarah,Maria,2
1099 *
1100 */
1101static void winogrande_score(llama_context * ctx, const common_params & params) {
1102    const llama_model * model = llama_get_model(ctx);
1103    const llama_vocab * vocab = llama_model_get_vocab(model);
1104
1105    constexpr int k_min_trailing_ctx = 3;
1106
1107    auto data = load_winogrande_from_csv(params.prompt);
1108    if (data.empty()) {
1109        LOG_ERR("%s: no tasks\n", __func__);
1110        return;
1111    }
1112
1113    LOG_INF("%s : loaded %zu tasks from prompt.\n", __func__, data.size());
1114
1115    if (params.winogrande_tasks > 0 && params.winogrande_tasks < data.size()) {
1116        LOG_INF("%s : selecting %zu random tasks\n", __func__, params.winogrande_tasks);
1117        std::mt19937 rng(1);
1118        std::vector<int> aux(data.size());
1119        for (int i = 0; i < int(data.size()); ++i) {
1120            aux[i] = i;
1121        }
1122        float scale = 1/(1.f + (float)rng.max());
1123        std::vector<winogrande_entry> selected;
1124        selected.resize(params.winogrande_tasks);
1125        for (int i = 0; i < int(params.winogrande_tasks); ++i) {
1126            int j = int(scale*rng()*aux.size());
1127            selected[i] = std::move(data[aux[j]]);
1128            aux[j] = aux.back();
1129            aux.pop_back();
1130        }
1131        data = std::move(selected);
1132    }
1133
1134    LOG_INF("%s : tokenizing selected tasks\n", __func__);
1135
1136    for (auto & task : data) {
1137        task.seq_tokens[0] = common_tokenize(ctx, task.first + task.choices[0] + task.second, true);
1138        task.seq_tokens[1] = common_tokenize(ctx, task.first + task.choices[1] + task.second, true);
1139
1140        task.common_prefix = 0;
1141        for (size_t k = 0; k < task.seq_tokens[0].size(); k++) {
1142            if (task.seq_tokens[0][k] != task.seq_tokens[1][k]) {
1143                break;
1144            }
1145            task.common_prefix++;
1146        }
1147
1148        // TODO: the last token of each of the sequences don't need to be evaluated
1149        task.required_tokens = task.common_prefix +
1150            task.seq_tokens[0].size() - task.common_prefix +
1151            task.seq_tokens[1].size() - task.common_prefix;
1152
1153        task.n_base1 = common_tokenize(ctx, task.first + task.choices[0], true).size();
1154        task.n_base2 = common_tokenize(ctx, task.first + task.choices[1], true).size();
1155    }
1156
1157    LOG_INF("%s : calculating winogrande score over selected tasks.\n", __func__);
1158
1159    const int n_ctx   = llama_n_ctx(ctx);
1160    const int n_batch = params.n_batch;
1161
1162    const int n_vocab = llama_vocab_n_tokens(vocab);
1163
1164    const int max_tasks_per_batch = 128;
1165    const int max_seq = std::min(2*max_tasks_per_batch, (int) llama_n_seq_max(ctx));
1166
1167    llama_batch batch = llama_batch_init(n_ctx, 0, 2);
1168
1169    std::vector<float> tok_logits(n_vocab);
1170    // TODO: this could be made smaller; it's currently the worst-case size
1171    std::vector<float> batch_logits(size_t(n_ctx)*n_vocab);
1172
1173    std::vector<std::pair<size_t, llama_token>> eval_pairs;
1174    std::vector<float> eval_results;
1175    std::vector<std::thread> workers(std::thread::hardware_concurrency());
1176
1177    int n_correct = 0;
1178    int n_done    = 0;
1179
1180    for (size_t i0 = 0; i0 < data.size(); i0++) {
1181        int n_cur = 0;
1182
1183        size_t i1 = i0;
1184        size_t i_logits = 0;
1185
1186        common_batch_clear(batch);
1187
1188        while (n_cur + (int) data[i1].required_tokens <= n_ctx) {
1189            int n_logits = 0;
1190            const int s0 = 2*(i1 - i0);
1191            if (s0 + 2 > max_seq) {
1192                break;
1193            }
1194
1195            for (size_t i = 0; i < data[i1].common_prefix; ++i) {
1196                common_batch_add(batch, data[i1].seq_tokens[0][i], i, { s0 + 0, s0 + 1 }, false);
1197            }
1198            batch.logits[batch.n_tokens - 1] = true;
1199            n_logits += 1;
1200
1201            for (int s = 0; s < 2; ++s) {
1202                // TODO: end before the last token, no need to predict past the end of the sequences
1203                for (size_t i = data[i1].common_prefix; i < data[i1].seq_tokens[s].size(); ++i) {
1204                    common_batch_add(batch, data[i1].seq_tokens[s][i], i, { s0 + s }, true);
1205                    n_logits += 1;
1206                }
1207            }
1208
1209            data[i1].i_logits = i_logits;
1210            i_logits += n_logits;
1211
1212            n_cur += data[i1].required_tokens;
1213            if (++i1 == data.size()) {
1214                break;
1215            }
1216        }
1217
1218        if (i0 == i1) {
1219            LOG_ERR("%s : task %zu does not fit in the context window (requires %zu tokens)\n", __func__, i0, data[i0].required_tokens);
1220            return;
1221        }
1222
1223        llama_memory_clear(llama_get_memory(ctx), true);
1224
1225        // decode all tasks [i0, i1)
1226        if (!decode_helper(ctx, batch, batch_logits, n_batch, n_vocab)) {
1227            LOG_ERR("%s: llama_decode() failed\n", __func__);
1228            return;
1229        }
1230
1231        eval_pairs.clear();
1232        for (size_t i = i0; i < i1; ++i) {
1233            auto & task = data[i];
1234
1235            const bool skip_choice =
1236                task.seq_tokens[0].size() - task.common_prefix > k_min_trailing_ctx &&
1237                task.seq_tokens[1].size() - task.common_prefix > k_min_trailing_ctx;
1238
1239            const auto& n_base1 = skip_choice ? task.n_base1 : task.common_prefix;
1240            const int last_1st = task.seq_tokens[0].size() - n_base1 > 1 ? 1 : 0;
1241            size_t li = n_base1 - task.common_prefix;
1242            for (size_t j = n_base1-1; j < task.seq_tokens[0].size()-1-last_1st; ++j) {
1243                eval_pairs.emplace_back(task.i_logits + li++, task.seq_tokens[0][j+1]);
1244            }
1245            const auto& n_base2 = skip_choice ? task.n_base2 : task.common_prefix;
1246            const int last_2nd = task.seq_tokens[1].size() - n_base2 > 1 ? 1 : 0;
1247            // FIXME: this uses the wrong first logits when not skipping the choice word
1248            li = task.seq_tokens[0].size() - task.common_prefix + n_base2 - task.common_prefix;
1249            for (size_t j = n_base2-1; j < task.seq_tokens[1].size()-1-last_2nd; ++j) {
1250                eval_pairs.emplace_back(task.i_logits + li++, task.seq_tokens[1][j+1]);
1251            }
1252        }
1253        compute_logprobs(batch_logits.data(), n_vocab, workers, eval_pairs, eval_results);
1254
1255        size_t ir = 0;
1256        for (size_t i = i0; i < i1; ++i) {
1257            auto & task = data[i];
1258
1259            const bool skip_choice =
1260                task.seq_tokens[0].size() - task.common_prefix > k_min_trailing_ctx &&
1261                task.seq_tokens[1].size() - task.common_prefix > k_min_trailing_ctx;
1262
1263            float score_1st = 0;
1264            const auto& n_base1 = skip_choice ? task.n_base1 : task.common_prefix;
1265            const int last_1st = task.seq_tokens[0].size() - n_base1 > 1 ? 1 : 0;
1266            for (size_t j = n_base1-1; j < task.seq_tokens[0].size()-1-last_1st; ++j) {
1267                score_1st += eval_results[ir++];
1268            }
1269            score_1st /= (task.seq_tokens[0].size() - n_base1 - last_1st);
1270
1271            float score_2nd = 0;
1272            const auto& n_base2 = skip_choice ? task.n_base2 : task.common_prefix;
1273            const int last_2nd = task.seq_tokens[1].size() - n_base2 > 1 ? 1 : 0;
1274            for (size_t j = n_base2-1; j < task.seq_tokens[1].size()-1-last_2nd; ++j) {
1275                score_2nd += eval_results[ir++];
1276            }
1277            score_2nd /= (task.seq_tokens[1].size() - n_base2 - last_2nd);
1278
1279            int result = score_1st > score_2nd ? 1 : 2;
1280
1281            if (result == task.answer) {
1282                ++n_correct;
1283            }
1284            ++n_done;
1285
1286            // print the accumulated accuracy mean x 100
1287            LOG("%zu\t%.4lf\t%10.6f  %10.6f  %d  %d\n", i+1, 100.0 * n_correct/n_done, score_1st, score_2nd, result, task.answer);
1288        }
1289
1290        i0 = i1 - 1;
1291    }
1292
1293    LOG("\n");
1294
1295    if (n_done < 100) return;
1296
1297    const float p = 1.f*n_correct/n_done;
1298    const float sigma = 100.f*sqrt(p*(1-p)/(n_done-1));
1299
1300    LOG_INF("Final Winogrande score(%d tasks): %.4lf +/- %.4lf\n", n_done, 100*p, sigma);
1301}
1302
1303static bool deserialize_string(std::istream & in, std::string & str) {
1304    uint32_t size;
1305    if (!in.read((char *)&size, sizeof(size)).fail()) {
1306        str.resize(size);
1307        if (!in.read((char *)&str[0], size).fail()) return true;
1308    }
1309    return false;
1310}
1311
1312struct multiple_choice_answers {
1313    std::vector<std::string> answers;
1314    std::vector<int>         labels;
1315    bool deserialize(std::istream& in) {
1316        uint32_t n;
1317        in.read((char *)&n, sizeof(n));
1318        if (in.fail() || n > 100) return false; // 100 as max. number of answers should be good enough for any practical purpose
1319        answers.resize(n);
1320        labels.resize(n);
1321        for (auto& a : answers) {
1322            if (!deserialize_string(in, a)) return false;
1323        }
1324        in.read((char *)labels.data(), n*sizeof(int));
1325        return !in.fail();
1326    }
1327};
1328
1329struct multiple_choice_task {
1330    std::string question;         // the question (or context that needs to be continued)
1331    multiple_choice_answers mc1;  // possible answers (continuations) with a single correct answer
1332    multiple_choice_answers mc2;  // possible answers (continuations) with multiple correct answers - not handled yet
1333    bool deserialize(std::istream& in) {
1334        if (!deserialize_string(in, question)) return false;
1335        return mc1.deserialize(in) && mc2.deserialize(in);
1336    }
1337
1338    // For evaluation
1339    size_t i_logits;        // starting index of logits in the llama_batch
1340    size_t common_prefix;   // max number of initial tokens that are the same in all sentences
1341    size_t required_tokens; // needed number of tokens to evaluate all answers
1342    std::vector<std::vector<llama_token>> seq_tokens;
1343    std::vector<float> log_probs;
1344};
1345
1346static bool multiple_choice_prepare_one_task(llama_context * ctx, multiple_choice_task& task, bool log_error) {
1347    if (task.question.empty() || task.mc1.answers.empty()) {
1348        if (log_error) {
1349            LOG_ERR("%s: found bad task with empty question and/or answers\n", __func__);
1350        }
1351        return false;
1352    }
1353    task.seq_tokens.reserve(task.mc1.answers.size());
1354    for (auto& answer : task.mc1.answers) {
1355        if (answer.empty()) {
1356            if (log_error) {
1357                LOG_ERR("%s: found empty answer\n", __func__);
1358            }
1359            return false;
1360        }
1361        task.seq_tokens.emplace_back(::common_tokenize(ctx, task.question + " " + answer, true));
1362    }
1363    auto min_len = task.seq_tokens.front().size();
1364    for (auto& seq : task.seq_tokens) {
1365        min_len = std::min(min_len, seq.size());
1366    }
1367    task.common_prefix = 0;
1368    for (size_t k = 0; k < min_len; ++k) {
1369        auto token = task.seq_tokens[0][k];
1370        bool all_same = true;
1371        for (size_t i = 1; i < task.seq_tokens.size(); ++i) {
1372            if (task.seq_tokens[i][k] != token) {
1373                all_same = false;
1374                break;
1375            }
1376        }
1377        if (!all_same) {
1378            break;
1379        }
1380        ++task.common_prefix;
1381    }
1382    task.required_tokens = task.common_prefix;
1383    for (auto& seq : task.seq_tokens) {
1384        task.required_tokens += seq.size() - task.common_prefix;
1385    }
1386    return true;
1387}
1388
1389//
1390// Calculates score for multiple choice tasks with single correct answer from prompt.
1391// Commonly used LLM evaluation metrics of this type are
1392//   * ARC
1393//   * HellaSwag
1394//   * MMLU
1395//   * TruthfulQA
1396//
1397// Validation datasets for these 4 tests can be found at
1398//     https://huggingface.co/datasets/ikawrakow/validation-datasets-for-llama.cpp
1399// The data for these datasets was extracted from
1400//     git@hf.co:datasets/allenai/ai2_arc
1401//     https://github.com/rowanz/hellaswag/blob/master/data/hellaswag_val.jsonl
1402//     git@hf.co:datasets/Stevross/mmlu
1403//     https://huggingface.co/datasets/truthful_qa
1404//
1405static void multiple_choice_score(llama_context * ctx, const common_params & params) {
1406    const llama_model * model = llama_get_model(ctx);
1407    const llama_vocab * vocab = llama_model_get_vocab(model);
1408
1409    std::istringstream strstream(params.prompt);
1410    uint32_t n_task;
1411    strstream.read((char *)&n_task, sizeof(n_task));
1412    if (strstream.fail() || n_task == 0) {
1413        LOG_ERR("%s: no tasks\n", __func__);
1414        return;
1415    }
1416    LOG_INF("%s: there are %u tasks in prompt\n", __func__, n_task);
1417    std::vector<uint32_t> task_pos(n_task);
1418    strstream.read((char *)task_pos.data(), task_pos.size()*sizeof(uint32_t));
1419    if (strstream.fail()) {
1420        LOG_ERR("%s: failed to read task positions from prompt\n", __func__);
1421        return;
1422    }
1423
1424    std::vector<multiple_choice_task> tasks;
1425    if (params.multiple_choice_tasks == 0 || params.multiple_choice_tasks >= (size_t)n_task) {
1426        // Use all tasks
1427        tasks.resize(n_task);
1428        LOG_INF("%s: reading tasks", __func__);
1429        int n_dot = std::max((int) n_task/100, 1);
1430        int i = 0;
1431        for (auto& task : tasks) {
1432            ++i;
1433            if (!task.deserialize(strstream)) {
1434                LOG_ERR("%s: failed to read task %d of %u\n", __func__, i, n_task);
1435                return;
1436            }
1437            if (i%n_dot == 0) LOG(".");
1438        }
1439        LOG("done\n");
1440    }
1441    else {
1442        LOG_INF("%s: selecting %zu random tasks from %u tasks available\n", __func__, params.multiple_choice_tasks, n_task);
1443        std::mt19937 rng(1);
1444        std::vector<int> aux(n_task);
1445        for (uint32_t i = 0; i < n_task; ++i) aux[i] = i;
1446        float scale = 1.f/(1.f + (float)std::mt19937::max());
1447        tasks.resize(params.multiple_choice_tasks);
1448        for (auto& task : tasks) {
1449            int j = (int)(scale * rng() * aux.size());
1450            int idx = aux[j];
1451            aux[j] = aux.back();
1452            aux.pop_back();
1453            strstream.seekg(task_pos[idx], std::ios::beg);
1454            if (!task.deserialize(strstream)) {
1455                LOG_ERR("%s: failed to read task %d at position %u\n", __func__, idx, task_pos[idx]);
1456                return;
1457            }
1458        }
1459        n_task = params.multiple_choice_tasks;
1460    }
1461
1462    LOG_INF("%s: preparing task data", __func__);
1463    if (n_task > 500) {
1464        LOG("...");
1465        std::atomic<int> counter(0);
1466        std::atomic<int> n_bad(0);
1467        auto prepare = [&counter, &n_bad, &tasks, ctx] () {
1468            int num_tasks = tasks.size();
1469            int n_bad_local = 0;
1470            while (true) {
1471                int first = counter.fetch_add(K_TOKEN_CHUNK);
1472                if (first >= num_tasks) {
1473                    if (n_bad_local > 0) n_bad += n_bad_local;
1474                    break;
1475                }
1476                int last = std::min(first + K_TOKEN_CHUNK, num_tasks);
1477                for (int i = first; i < last; ++i) {
1478                    if (!multiple_choice_prepare_one_task(ctx, tasks[i], false)) ++n_bad_local;
1479                }
1480            }
1481        };
1482        size_t max_thread = std::thread::hardware_concurrency();
1483        max_thread = std::min(max_thread, (tasks.size() + K_TOKEN_CHUNK - 1)/K_TOKEN_CHUNK);
1484        std::vector<std::thread> workers(max_thread-1);
1485        for (auto& w : workers) w = std::thread(prepare);
1486        prepare();
1487        for (auto& w : workers) w.join();
1488        LOG("done\n");
1489        int nbad = n_bad;
1490        if (nbad > 0) {
1491            LOG_ERR("%s: found %d malformed tasks\n", __func__, nbad);
1492            return;
1493        }
1494    } else {
1495        int n_dot = std::max((int) n_task/100, 1);
1496        int i_task = 0;
1497        for (auto& task : tasks) {
1498            ++i_task;
1499            if (!multiple_choice_prepare_one_task(ctx, task, true)) {
1500                return;
1501            }
1502            if (i_task%n_dot == 0) {
1503                LOG(".");
1504            }
1505        }
1506        LOG("done\n");
1507    }
1508
1509    LOG_INF("%s : calculating TruthfulQA score over %zu tasks.\n", __func__, tasks.size());
1510
1511    LOG("\ntask\tacc_norm\n");
1512
1513    const int n_ctx   = llama_n_ctx(ctx);
1514    const int n_batch = params.n_batch;
1515
1516    const int n_vocab = llama_vocab_n_tokens(vocab);
1517
1518    const int max_tasks_per_batch = 32;
1519    const int max_seq = std::min(4*max_tasks_per_batch, (int) llama_n_seq_max(ctx));
1520
1521    llama_batch batch = llama_batch_init(n_ctx, 0, max_seq);
1522
1523    std::vector<float> tok_logits(n_vocab);
1524    std::vector<float> batch_logits(size_t(n_ctx)*n_vocab);
1525
1526    std::vector<std::pair<size_t, llama_token>> eval_pairs;
1527    std::vector<float> eval_results;
1528    std::vector<std::thread> workers(std::thread::hardware_concurrency());
1529    std::vector<int> batch_indeces;
1530
1531    int n_done = 0;
1532    int n_correct = 0;
1533    int n_tot_answers = 0;
1534
1535    for (size_t i0 = 0; i0 < tasks.size(); i0++) {
1536        int n_cur = 0;
1537
1538        size_t i1 = i0;
1539        size_t i_logits = 0; // this tells us how many logits were needed before this point in the batch
1540
1541        common_batch_clear(batch);
1542
1543        // batch as much tasks as possible into the available context
1544        // each task has 4 unique sequence ids - one for each ending
1545        // the common prefix is shared among the 4 sequences to save tokens
1546        // we extract logits only from the last common token and from all ending tokens of each sequence
1547        int s0 = 0;
1548        while (n_cur + (int) tasks[i1].required_tokens <= n_ctx) {
1549            auto& cur_task = tasks[i1];
1550            int n_logits = 0;
1551
1552            int num_answers = cur_task.seq_tokens.size();
1553            if (s0 + num_answers > max_seq) {
1554                if (s0 == 0) {
1555                    LOG_ERR("%s : task %zu requires a higher -np|--parallel value (at least %d)\n", __func__, i0, num_answers);
1556                    return;
1557                }
1558                break;
1559            }
1560
1561            if (int(batch_indeces.size()) != num_answers) {
1562                batch_indeces.resize(num_answers);
1563            }
1564
1565            for (int s = 0; s < num_answers; ++s) {
1566                batch_indeces[s] = s0 + s;
1567            }
1568
1569            for (size_t i = 0; i < cur_task.common_prefix; ++i) {
1570                //llama_batch_add(batch, cur_task.seq_tokens[0][i], i, { s0 + 0, s0 + 1, s0 + 2, s0 + 3}, false);
1571                common_batch_add(batch, cur_task.seq_tokens[0][i], i, batch_indeces, false);
1572            }
1573            batch.logits[batch.n_tokens - 1] = true; // we need logits for the last token of the common prefix
1574            n_logits += 1;
1575
1576            for (int s = 0; s < int(cur_task.seq_tokens.size()); ++s) {
1577                const size_t seq_tokens_size = cur_task.seq_tokens[s].size();
1578                // TODO: don't evaluate the last token of each sequence
1579                for (size_t i = cur_task.common_prefix; i < seq_tokens_size; ++i) {
1580                    const bool needs_logits = i < seq_tokens_size - 1;
1581                    common_batch_add(batch, cur_task.seq_tokens[s][i], i, { s0 + s }, needs_logits);
1582                    n_logits += needs_logits;
1583                }
1584            }
1585
1586            s0 += num_answers;
1587
1588            cur_task.i_logits = i_logits;
1589            i_logits += n_logits;
1590
1591            n_cur += cur_task.required_tokens;
1592            if (++i1 == tasks.size()) {
1593                break;
1594            }
1595        }
1596
1597        if (i0 == i1) {
1598            LOG_ERR("%s : task %zu does not fit in the context window (requires %zu tokens)\n", __func__, i0, tasks[i0].required_tokens);
1599            return;
1600        }
1601
1602        llama_memory_clear(llama_get_memory(ctx), true);
1603
1604        // decode all tasks [i0, i1)
1605        if (!decode_helper(ctx, batch, batch_logits, n_batch, n_vocab)) {
1606            LOG_ERR("%s: llama_decode() failed\n", __func__);
1607            return;
1608        }
1609
1610        // Compute log-probs in parallel
1611        // First we collect all tasks
1612        eval_pairs.clear();
1613        for (size_t i = i0; i < i1; ++i) {
1614            auto& cur_task = tasks[i];
1615            size_t li = 1; // skip the last logit of the common prefix (computed separately below)
1616            for (int s = 0; s < int(cur_task.seq_tokens.size()); ++s) {
1617                for (size_t j = cur_task.common_prefix; j < cur_task.seq_tokens[s].size() - 1; j++) {
1618                    eval_pairs.emplace_back(cur_task.i_logits + li++, cur_task.seq_tokens[s][j + 1]);
1619                }
1620            }
1621        }
1622        // Then we do the actual calculation
1623        compute_logprobs(batch_logits.data(), n_vocab, workers, eval_pairs, eval_results);
1624
1625        size_t ir = 0;
1626
1627        // compute the logprobs for each ending of the decoded tasks
1628        for (size_t i = i0; i < i1; ++i) {
1629            auto & cur_task = tasks[i];
1630            //LOG("==== Evaluating <%s> with correct answer ", cur_task.question.c_str());
1631            //for (int j = 0; j < int(cur_task.mc1.labels.size()); ++j) {
1632            //    if (cur_task.mc1.labels[j] == 1) {
1633            //        LOG("%d", j+1);
1634            //    }
1635            //}
1636            //LOG("\n    common_prefix: %zu\n", cur_task.common_prefix);
1637
1638            // get the logits of the last token of the common prefix
1639            std::memcpy(tok_logits.data(), batch_logits.data() + cur_task.i_logits*n_vocab, n_vocab*sizeof(float));
1640
1641            const auto first_probs = softmax(tok_logits);
1642
1643            cur_task.log_probs.resize(cur_task.seq_tokens.size());
1644            for (int s = 0; s < int(cur_task.seq_tokens.size()); ++s) {
1645                size_t count = 1;
1646                float  log_prob  = std::log(first_probs[cur_task.seq_tokens[s][cur_task.common_prefix]]);
1647                for (size_t j = cur_task.common_prefix; j < cur_task.seq_tokens[s].size() - 1; j++) {
1648                    //LOG("        %zu  %g\n", ir, eval_results[ir]);
1649                    ++count;
1650                    log_prob += eval_results[ir++];
1651                }
1652                cur_task.log_probs[s] = log_prob / count;
1653                //LOG("        Final: %g\n", log_prob / count);
1654                //LOG("    <%s> : %g\n", cur_task.mc1.answers[s].c_str(), log_prob/count);
1655            }
1656
1657            // Find the ending with maximum logprob
1658            size_t logprob_max_idx = 0;
1659            float  logprob_max_val = cur_task.log_probs[0];
1660            for (size_t s = 1; s < cur_task.log_probs.size(); s++) {
1661                if (cur_task.log_probs[s] > logprob_max_val) {
1662                    logprob_max_val = cur_task.log_probs[s];
1663                    logprob_max_idx = s;
1664                }
1665            }
1666
1667            n_tot_answers += cur_task.log_probs.size();
1668            if (cur_task.mc1.labels[logprob_max_idx] == 1) {
1669                ++n_correct;
1670            }
1671            ++n_done;
1672
1673            // Print the accumulated accuracy mean x 100
1674            LOG("%d\t%.8lf\n", n_done, 100.*n_correct/n_done);
1675        }
1676
1677        i0 = i1 - 1;
1678    }
1679
1680    llama_batch_free(batch);
1681
1682    if (n_done < 100 && (params.multiple_choice_tasks != 0 && params.multiple_choice_tasks < (size_t)n_task)) return;
1683
1684    float p = 1.f*n_correct/n_done;
1685    float sigma = sqrt(p*(1-p)/(n_done-1));
1686    LOG("\n");
1687    LOG_INF("Final result: %.4f +/- %.4f\n", 100.f*p, 100.f*sigma);
1688    p = 1.f*n_done/n_tot_answers;
1689    sigma = sqrt(p*(1-p)/(n_done-1));
1690    LOG_INF("Random chance: %.4f +/- %.4f\n", 100.f*p, 100.f*sigma);
1691
1692    LOG_INF("\n");
1693}
1694
1695static void kl_divergence(llama_context * ctx, const common_params & params) {
1696    const llama_model * model = llama_get_model(ctx);
1697    const llama_vocab * vocab = llama_model_get_vocab(model);
1698
1699    if (params.logits_file.empty()) {
1700        LOG_ERR("%s: you must provide a name of a file containing the log probabilities of the base model\n", __func__);
1701        return;
1702    }
1703    std::ifstream in(params.logits_file.c_str(), std::ios::binary);
1704    if (!in) {
1705        LOG_ERR("%s: failed to open %s\n", __func__, params.logits_file.c_str());
1706        return;
1707    }
1708    {
1709        char check[9]; check[8] = 0;
1710        in.read(check, 8);
1711        if (in.fail() || strncmp("_logits_", check, 8) != 0) {
1712            LOG_ERR("%s: %s does not look like a file containing log-probabilities\n", __func__, params.logits_file.c_str());
1713            return;
1714        }
1715    }
1716
1717    uint32_t n_ctx;
1718    in.read((char *)&n_ctx, sizeof(n_ctx));
1719    if (n_ctx > llama_n_ctx(ctx)) {
1720        LOG_ERR("%s: %s has been computed with %u, while the current context is %d. Increase it with -c and retry\n",
1721                __func__, params.logits_file.c_str(), n_ctx, params.n_ctx);
1722    }
1723
1724    int n_vocab;
1725    int n_chunk;
1726    in.read((char *)&n_vocab, sizeof(n_vocab));
1727    in.read((char *)&n_chunk, sizeof(n_chunk));
1728    if (in.fail()) {
1729        LOG_ERR("%s: failed reading n_vocab, n_chunk from %s\n", __func__, params.logits_file.c_str());
1730        return;
1731    }
1732    if (n_vocab != llama_vocab_n_tokens(vocab)) {
1733        LOG_ERR("%s: inconsistent vocabulary (%d vs %d)\n", __func__, n_vocab, llama_vocab_n_tokens(vocab));
1734    }
1735
1736    std::vector<llama_token> tokens(size_t(n_ctx) * n_chunk);
1737    if (in.read((char *)tokens.data(), tokens.size()*sizeof(tokens[0])).fail()) {
1738        LOG_ERR("%s: failed reading evaluation tokens from %s\n", __func__, params.logits_file.c_str());
1739        return;
1740    }
1741
1742    const int n_batch = params.n_batch;
1743    const int num_batches = (static_cast<int>(n_ctx) + n_batch - 1) / n_batch;
1744    // Calculate n_seq based on the logits file's n_ctx, but cap it at what the context supports
1745    const int n_seq_max = llama_n_seq_max(ctx);
1746    int n_seq = std::max(1, n_batch / static_cast<int>(n_ctx));
1747    if (n_seq > n_seq_max) {
1748        LOG_WRN("%s: calculated n_seq=%d exceeds context's n_seq_max=%d, capping at %d\n",
1749                __func__, n_seq, n_seq_max, n_seq_max);
1750        n_seq = n_seq_max;
1751    }
1752    const int nv = 2*((n_vocab + 1)/2) + 4;
1753    const bool add_bos = llama_vocab_get_add_bos(vocab);
1754    GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
1755
1756    llama_batch batch = llama_batch_init(std::min(n_batch, static_cast<int>(n_ctx)*n_seq), 0, 1);
1757
1758    std::vector<uint16_t> log_probs_uint16(size_t(n_ctx - 1 - n_ctx/2) * nv);
1759    std::vector<float>    kld_values(size_t(n_ctx - 1 - n_ctx/2)*n_chunk);
1760    std::vector<float> p_diff_values(size_t(n_ctx - 1 - n_ctx/2)*n_chunk);
1761    std::vector<float> logits;
1762    if (num_batches > 1) {
1763        logits.reserve(size_t(n_ctx) * n_vocab);
1764    }
1765
1766    LOG_INF("%s: computing over %d chunks, n_ctx=%u, batch_size=%d, n_seq=%d\n", __func__, n_chunk, n_ctx, n_batch, n_seq);
1767
1768    std::vector<std::thread> workers(std::thread::hardware_concurrency() - 1);
1769
1770    auto mean_and_uncertainty = [] (double sum, double sum2, size_t count) {
1771        if (count < 1) {
1772            return std::make_pair(0., 0.);
1773        }
1774        double f = sum/count;
1775        double df = sum2/count - f*f;
1776        df = df > 0 && count > 10 ? sqrt(df/(count-1)) : 0.;
1777        return std::make_pair(f, df);
1778    };
1779    auto covariance = [] (double suma, double sumb, double sumab, size_t count) {
1780        if (count < 10) {
1781            return 0.0;
1782        }
1783        double var = sumab/count - (suma/count)*(sumb/count);
1784        var /= count - 1;
1785        return var;
1786    };
1787
1788    kl_divergence_result kld;
1789    auto    kld_ptr =    kld_values.data();
1790    auto p_diff_ptr = p_diff_values.data();
1791
1792    const int first = n_ctx/2;
1793
1794    for (int i = 0; i < n_chunk; i += n_seq) {
1795        const int start =     i * n_ctx;
1796        const int end   = start + n_ctx;
1797
1798        const int n_seq_batch = std::min(n_seq, n_chunk - i);
1799
1800        const auto t_start = std::chrono::high_resolution_clock::now();
1801
1802        // clear the KV cache
1803        llama_memory_clear(llama_get_memory(ctx), true);
1804
1805        for (int j = 0; j < num_batches; ++j) {
1806            const int batch_start = start + j * n_batch;
1807            const int batch_size  = std::min(end - batch_start, n_batch);
1808
1809            int n_outputs = 0;
1810
1811            common_batch_clear(batch);
1812            for (int seq = 0; seq < n_seq_batch; seq++) {
1813                int seq_start = batch_start + seq*n_ctx;
1814
1815                // save original token and restore it after eval
1816                const auto token_org = tokens[seq_start];
1817
1818                // add BOS token for the first batch of each chunk
1819                if (add_bos && j == 0) {
1820                    tokens[seq_start] = llama_vocab_bos(vocab);
1821                }
1822
1823                for (int k = 0; k < batch_size; ++k) {
1824                    const int pos = j*n_batch + k;
1825                    const bool need_logits = pos >= first;
1826                    common_batch_add(batch, tokens[seq_start + k], pos, { seq }, need_logits);
1827                    n_outputs += need_logits;
1828                }
1829
1830                // restore the original token in case it was set to BOS
1831                tokens[seq_start] = token_org;
1832            }
1833
1834            if (llama_decode(ctx, batch)) {
1835                LOG_ERR("%s : failed to decode\n", __func__);
1836                llama_batch_free(batch);
1837                return;
1838            }
1839
1840            if (num_batches > 1 && n_outputs > 0) {
1841                const auto * batch_logits = llama_get_logits(ctx);
1842                logits.insert(logits.end(), batch_logits, batch_logits + size_t(n_outputs) * n_vocab);
1843            }
1844        }
1845
1846        if (i == 0) {
1847            llama_synchronize(ctx);
1848            const auto t_end = std::chrono::high_resolution_clock::now();
1849            const float t_total = std::chrono::duration<float>(t_end - t_start).count();
1850            LOG_INF("%s: %.2f seconds per pass - ETA ", __func__, t_total);
1851            int total_seconds = (int)(t_total * n_chunk / n_seq);
1852            if (total_seconds >= 60*60) {
1853                LOG("%d hours ", total_seconds / (60*60));
1854                total_seconds = total_seconds % (60*60);
1855            }
1856            LOG("%.2f minutes\n", total_seconds / 60.0);
1857            LOG("\n");
1858            LOG("chunk             PPL               ln(PPL(Q)/PPL(base))          KL Divergence              Δp RMS            Same top p\n");
1859        }
1860
1861        // Read log probs for each sequence in the batch
1862        for (int seq = 0; seq < n_seq_batch; seq++) {
1863            if (in.read((char *)log_probs_uint16.data(), log_probs_uint16.size()*sizeof(uint16_t)).fail()) {
1864                LOG_ERR("%s: failed reading log-probs for chunk %d\n", __func__, i + seq);
1865                llama_batch_free(batch);
1866                return;
1867            }
1868
1869            const float * all_logits = num_batches > 1 ? logits.data() : llama_get_logits_ith(ctx, seq*n_ctx + first);
1870
1871            process_logits(n_vocab, all_logits, tokens.data() + start + seq*n_ctx + first, n_ctx - 1 - first,
1872                    workers, log_probs_uint16, kld, kld_ptr, p_diff_ptr);
1873            p_diff_ptr += n_ctx - 1 - first;
1874            kld_ptr    += n_ctx - 1 - first;
1875
1876            LOG("%4d", i + seq + 1);
1877
1878            auto log_ppl = mean_and_uncertainty(kld.sum_nll, kld.sum_nll2, kld.count);
1879            const double ppl_val = exp(log_ppl.first);
1880            const double ppl_unc = ppl_val * log_ppl.second;
1881            LOG("    %9.4lf ± %9.4lf", ppl_val, ppl_unc);
1882
1883            auto log_ppl_base = mean_and_uncertainty(kld.sum_nll_base, kld.sum_nll_base2, kld.count);
1884            const double log_ppl_cov = covariance(kld.sum_nll, kld.sum_nll_base, kld.sum_nll_nll_base, kld.count);
1885            const double log_ppl_ratio_val = log_ppl.first - log_ppl_base.first;
1886            const double log_ppl_ratio_unc = sqrt(log_ppl.second*log_ppl.second + log_ppl_base.second*log_ppl_base.second - 2.0*log_ppl_cov);
1887            LOG("    %10.5lf ± %10.5lf", log_ppl_ratio_val, log_ppl_ratio_unc);
1888
1889            auto kl_div = mean_and_uncertainty(kld.sum_kld, kld.sum_kld2, kld.count);
1890            LOG("    %10.5lf ± %10.5lf", kl_div.first, kl_div.second);
1891
1892            auto p_diff_mse   = mean_and_uncertainty(kld.sum_p_diff2, kld.sum_p_diff4, kld.count);
1893            const double p_diff_rms_val = sqrt(p_diff_mse.first);
1894            const double p_diff_rms_unc = 0.5/p_diff_rms_val * p_diff_mse.second;
1895            LOG("    %6.3lf ± %6.3lf %%", 100.0*p_diff_rms_val, 100.0*p_diff_rms_unc);
1896
1897            double p_top_val = 1.*kld.n_same_top/kld.count;
1898            double p_top_unc = sqrt(p_top_val*(1 - p_top_val)/(kld.count - 1));
1899            LOG("    %6.3lf ± %6.3lf %%", 100.0*p_top_val, 100.0*p_top_unc);
1900
1901            LOG("\n");
1902        }
1903
1904        logits.clear();
1905    }
1906
1907    llama_batch_free(batch);
1908    LOG("\n");
1909
1910    if (kld.count < 100) return; // we do not wish to do statistics on so few values
1911
1912    std::sort(kld_values.begin(), kld_values.end());
1913    std::sort(p_diff_values.begin(), p_diff_values.end());
1914
1915    LOG("====== Perplexity statistics ======\n");
1916
1917    auto log_ppl = mean_and_uncertainty(kld.sum_nll, kld.sum_nll2, kld.count);
1918    const double ppl_val = exp(log_ppl.first);
1919    const double ppl_unc = ppl_val * log_ppl.second; // ppl_unc = sqrt( (dexp(x) / dx) ** 2 * log_ppl.second ** 2 )
1920    LOG("Mean PPL(Q)                   : %10.6lf ± %10.6lf\n", ppl_val, ppl_unc);
1921
1922    auto log_ppl_base = mean_and_uncertainty(kld.sum_nll_base, kld.sum_nll_base2, kld.count);
1923    const double ppl_base_val = exp(log_ppl_base.first);
1924    const double ppl_base_unc = ppl_base_val * log_ppl_base.second; // ppl_base_unc = sqrt( (dexp(x) / dx) ** 2 * log_ppl_base.second ** 2 )
1925    LOG("Mean PPL(base)                : %10.6lf ± %10.6lf\n", ppl_base_val, ppl_base_unc);
1926
1927    const double log_ppl_cov = covariance(kld.sum_nll, kld.sum_nll_base, kld.sum_nll_nll_base, kld.count);
1928    // LOG("Cov(ln(PPL(Q)), ln(PPL(base))): %10.6lf\n", log_ppl_cov);
1929    const double log_ppl_cor = log_ppl_cov / (log_ppl.second*log_ppl_base.second);
1930    LOG("Cor(ln(PPL(Q)), ln(PPL(base))): %6.2lf%%\n", 100.0*log_ppl_cor);
1931
1932    const double log_ppl_ratio_val = log_ppl.first - log_ppl_base.first;
1933    const double log_ppl_ratio_unc = sqrt(log_ppl.second*log_ppl.second + log_ppl_base.second*log_ppl_base.second - 2.0*log_ppl_cov);
1934    LOG("Mean ln(PPL(Q)/PPL(base))     : %10.6lf ± %10.6lf\n", log_ppl_ratio_val, log_ppl_ratio_unc);
1935
1936    const double ppl_ratio_val = exp(log_ppl_ratio_val);
1937    const double ppl_ratio_unc = ppl_ratio_val * log_ppl_ratio_unc; // ppl_ratio_unc = sqrt( (dexp(x) / dx) ** 2 * log_ppl_ratio.second ** 2 )
1938    LOG("Mean PPL(Q)/PPL(base)         : %10.6lf ± %10.6lf\n", ppl_ratio_val, ppl_ratio_unc);
1939
1940    const double ppl_cov = ppl_val * ppl_base_val * log_ppl_cov;
1941    const double ppl_diff_val = ppl_val - ppl_base_val;
1942    const double ppl_diff_unc = sqrt(ppl_unc*ppl_unc + ppl_base_unc*ppl_base_unc - 2.0*ppl_cov);
1943    LOG("Mean PPL(Q)-PPL(base)         : %10.6lf ± %10.6lf\n", ppl_diff_val, ppl_diff_unc);
1944
1945    LOG("\n");
1946
1947    LOG("====== KL divergence statistics ======\n");
1948    auto kl_div = mean_and_uncertainty(kld.sum_kld, kld.sum_kld2, kld.count);
1949    LOG("Mean    KLD: %10.6lf ± %10.6lf\n", kl_div.first, kl_div.second);
1950    auto kld_median = kld_values.size()%2 == 0 ? 0.5f*(kld_values[kld_values.size()/2] + kld_values[kld_values.size()/2-1])
1951                                               : kld_values[kld_values.size()/2];
1952
1953    auto percentile = [] (std::vector<float> values, float fraction) {
1954        if (fraction <= 0) return values.front();
1955        if (fraction >= 1) return values.back();
1956        float p = fraction*(values.size() - 1);
1957        size_t ip = size_t(p); p -= ip;
1958        return (1 - p)*values[ip] + p*values[std::min(ip+1, values.size()-1)];
1959    };
1960
1961    LOG("Maximum KLD: %10.6f\n", kld_values.back());
1962    LOG("99.9%%   KLD: %10.6f\n", percentile(kld_values, 0.999f));
1963    LOG("99.0%%   KLD: %10.6f\n", percentile(kld_values, 0.990f));
1964    LOG("95.0%%   KLD: %10.6f\n", percentile(kld_values, 0.950f));
1965    LOG("90.0%%   KLD: %10.6f\n", percentile(kld_values, 0.900f));
1966    LOG("Median  KLD: %10.6f\n", kld_median);
1967    LOG("10.0%%   KLD: %10.6f\n", percentile(kld_values, 0.100f));
1968    LOG(" 5.0%%   KLD: %10.6f\n", percentile(kld_values, 0.050f));
1969    LOG(" 1.0%%   KLD: %10.6f\n", percentile(kld_values, 0.010f));
1970    LOG(" 0.1%%   KLD: %10.6f\n", percentile(kld_values, 0.001f));
1971    LOG("Minimum KLD: %10.6f\n", kld_values.front());
1972
1973    LOG("\n");
1974
1975    LOG("====== Token probability statistics ======\n");
1976
1977    auto p_diff = mean_and_uncertainty(kld.sum_p_diff, kld.sum_p_diff2, kld.count);
1978    LOG("Mean    Δp: %6.3lf ± %5.3lf %%\n",  100.0*p_diff.first, 100.0*p_diff.second);
1979
1980    auto p_diff_median = p_diff_values.size()%2 == 0 ? 0.5f*(p_diff_values[p_diff_values.size()/2] + p_diff_values[p_diff_values.size()/2-1])
1981                                               : p_diff_values[p_diff_values.size()/2];
1982
1983    LOG("Maximum Δp: %6.3lf%%\n",  100.0*p_diff_values.back());
1984    LOG("99.9%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.999f));
1985    LOG("99.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.990f));
1986    LOG("95.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.950f));
1987    LOG("90.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.900f));
1988    LOG("75.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.750f));
1989    LOG("Median  Δp: %6.3lf%%\n",  100.0*p_diff_median);
1990    LOG("25.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.250f));
1991    LOG("10.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.100f));
1992    LOG(" 5.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.050f));
1993    LOG(" 1.0%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.010f));
1994    LOG(" 0.1%%   Δp: %6.3lf%%\n", 100.0*percentile(p_diff_values, 0.001f));
1995    LOG("Minimum Δp: %6.3lf%%\n",  100.0*p_diff_values.front());
1996
1997    auto p_diff_mse = mean_and_uncertainty(kld.sum_p_diff2, kld.sum_p_diff4, kld.count);
1998    // LOG("MSE Δp    : %10.6lf ± %10.6lf\n", p_diff_mse.first, p_diff_mse.second);
1999
2000    const double p_diff_rms_val = sqrt(p_diff_mse.first);
2001    const double p_diff_rms_unc = 0.5/p_diff_rms_val * p_diff_mse.second;
2002    LOG("RMS Δp    : %6.3lf ± %5.3lf %%\n", 100.0*p_diff_rms_val, 100.0*p_diff_rms_unc);
2003
2004    const double same_top_p = 1.0*kld.n_same_top/kld.count;
2005    LOG("Same top p: %6.3lf ± %5.3lf %%\n", 100.0*same_top_p, 100.0*sqrt(same_top_p*(1.0 - same_top_p)/(kld.count - 1)));
2006}
2007
2008// satisfies -Wmissing-declarations
2009int llama_perplexity(int argc, char ** argv);
2010
2011int llama_perplexity(int argc, char ** argv) {
2012    std::setlocale(LC_NUMERIC, "C");
2013
2014    common_params params;
2015
2016    params.n_ctx = 512;
2017    params.escape = false;
2018
2019    common_init();
2020
2021    if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_PERPLEXITY)) {
2022        return 1;
2023    }
2024
2025    const int32_t n_ctx = params.n_ctx;
2026    if (n_ctx <= 0) {
2027        LOG_ERR("%s: perplexity tool requires '--ctx-size' > 0\n", __func__);
2028        return 1;
2029    }
2030
2031    if (params.hellaswag || params.winogrande || params.multiple_choice) {
2032        params.n_parallel = std::max(4, params.n_parallel);
2033        params.kv_unified = true;
2034    } else { // Perplexity & KL divergence
2035        params.n_parallel = std::max(1, params.n_batch / n_ctx);
2036    }
2037    params.n_ctx = params.n_parallel * n_ctx;
2038    params.n_batch = std::min(params.n_batch, params.n_ctx);
2039
2040    if (params.ppl_stride > 0) {
2041        LOG_INF("Will perform strided perplexity calculation -> adjusting context size from %d to %d\n",
2042                params.n_ctx, params.n_ctx + params.ppl_stride/2);
2043        params.n_ctx += params.ppl_stride/2;
2044    }
2045
2046    llama_backend_init();
2047    llama_numa_init(params.numa);
2048
2049    // load the model and apply lora adapter, if any
2050    auto llama_init = common_init_from_params(params);
2051
2052    auto * model = llama_init->model();
2053    auto * ctx   = llama_init->context();
2054
2055    if (model == nullptr) {
2056        LOG_ERR("%s: unable to load model\n", __func__);
2057        return 1;
2058    }
2059
2060    if (ctx == nullptr) {
2061        LOG_ERR("%s: failed to create context\n", __func__);
2062        return 1;
2063    }
2064
2065    const int n_ctx_train = llama_model_n_ctx_train(model);
2066
2067    if (params.n_ctx > n_ctx_train) {
2068        LOG_WRN("%s: model was trained on only %d context tokens (%d specified)\n",
2069                __func__, n_ctx_train, params.n_ctx);
2070    }
2071
2072    // print system information
2073    {
2074        LOG_INF("\n");
2075        LOG_INF("%s\n", common_params_get_system_info(params).c_str());
2076    }
2077
2078    struct results_perplexity results;
2079    if (params.hellaswag) {
2080        hellaswag_score(ctx, params);
2081    } else if (params.winogrande) {
2082        winogrande_score(ctx, params);
2083    } else if (params.multiple_choice) {
2084        multiple_choice_score(ctx, params);
2085    } else if (params.kl_divergence) {
2086        kl_divergence(ctx, params);
2087    } else {
2088        results = perplexity(ctx, params, n_ctx);
2089    }
2090
2091    LOG("\n");
2092    llama_perf_context_print(ctx);
2093    common_memory_breakdown_print(ctx);
2094
2095    llama_backend_free();
2096
2097    return 0;
2098}