MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / perplexity_v2

Function perplexity_v2

examples/perplexity/perplexity.cpp:146–279  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

144}
145
146static results_perplexity perplexity_v2(llama_context * ctx, const gpt_params & params) {
147 // Download: https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip?ref=salesforce-research
148 // Run `./perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
149 // Output: `perplexity: 13.5106 [114/114]`
150 // BOS tokens will be added for each chunk before eval
151
152 const bool is_spm = llama_vocab_type(llama_get_model(ctx)) == LLAMA_VOCAB_TYPE_SPM;
153 const bool add_bos = is_spm;
154
155 fprintf(stderr, "%s: tokenizing the input ..\n", __func__);
156
157 std::vector<llama_token> tokens = ::llama_tokenize(ctx, params.prompt, add_bos);
158
159 const int n_ctx = llama_n_ctx(ctx);
160
161 if (int(tokens.size()) < 2*n_ctx) {
162 fprintf(stderr, "%s: you need at least %d tokens to evaluate perplexity with a context of %d\n",__func__,2*n_ctx,
163 n_ctx);
164 fprintf(stderr, "%s: the data file you provided tokenizes to only %zu tokens\n",__func__,tokens.size());
165 return {std::move(tokens), 0., {}, {}};
166 }
167
168 std::vector<float> logit_history;
169 std::vector<float> prob_history;
170
171 logit_history.resize(tokens.size());
172 prob_history.resize(tokens.size());
173
174 if (params.ppl_stride <= 0) {
175 fprintf(stderr, "%s: stride is %d but must be greater than zero!\n",__func__,params.ppl_stride);
176 return {tokens, -1, logit_history, prob_history};
177 }
178
179 const int calc_chunk = n_ctx;
180
181 fprintf(stderr, "%s: have %zu tokens. Calculation chunk = %d\n", __func__, tokens.size(), calc_chunk);
182
183 if (int(tokens.size()) <= calc_chunk) {
184 fprintf(stderr, "%s: there are only %zu tokens, this is not enough for a context size of %d and stride %d\n",__func__,
185 tokens.size(), n_ctx, params.ppl_stride);
186 return {tokens, -1, logit_history, prob_history};
187 }
188
189 const int n_chunk_max = (tokens.size() - calc_chunk + params.ppl_stride - 1) / params.ppl_stride;
190
191 const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
192 const int n_vocab = llama_n_vocab(llama_get_model(ctx));
193 const int n_batch = params.n_batch;
194
195 int count = 0;
196 double nll = 0.0;
197
198 fprintf(stderr, "%s: calculating perplexity over %d chunks, batch_size=%d\n", __func__, n_chunk, n_batch);
199
200 for (int i = 0; i < n_chunk; ++i) {
201 const int start = i * params.ppl_stride;
202 const int end = start + calc_chunk;
203

Callers 1

perplexityFunction · 0.70

Calls 15

fprintfFunction · 0.85
minFunction · 0.85
llama_kv_cache_clearFunction · 0.85
logFunction · 0.85
printfFunction · 0.85
expFunction · 0.85
softmaxFunction · 0.70
llama_vocab_typeEnum · 0.50
llama_get_modelFunction · 0.50
llama_tokenizeFunction · 0.50
llama_n_ctxFunction · 0.50
llama_n_vocabFunction · 0.50

Tested by

no test coverage detected