MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / perplexity_v2

Function perplexity_v2

tools/perplexity/perplexity.cpp:296–442  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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);

Callers 1

perplexityFunction · 0.85

Calls 15

llama_get_modelFunction · 0.85
llama_model_get_vocabFunction · 0.85
llama_vocab_get_add_bosFunction · 0.85
llama_vocab_get_add_eosFunction · 0.85
common_tokenizeFunction · 0.85
llama_n_ctxFunction · 0.85
minFunction · 0.85
llama_vocab_n_tokensFunction · 0.85
maxFunction · 0.85
llama_memory_clearFunction · 0.85
llama_get_memoryFunction · 0.85
llama_batch_initFunction · 0.85

Tested by

no test coverage detected