-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.cpp
More file actions
567 lines (490 loc) · 20.3 KB
/
bench.cpp
File metadata and controls
567 lines (490 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// Minimal llama.cpp WASM benchmark wrapper.
// Exports C functions for JavaScript to call: init, load, run, exit.
// Based on llama.cpp/examples/simple/simple.cpp
#include "llama.h"
#include "ggml.h"
#include "ggml-backend.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
// Global state
static llama_model * g_model = nullptr;
static llama_context * g_ctx = nullptr;
static llama_sampler * g_sampler = nullptr;
static const llama_vocab * g_vocab = nullptr;
static int g_n_ctx = 2048;
// Snapshot of the KV cache after a successful bench_set_depth call. Lets
// repeated reps at the same depth skip the prefill cost — same trick
// llama-bench's `cstate` plays in tools/llama-bench/llama-bench.cpp.
// Reset whenever the model/context is (re)created.
static int g_depth_cached = -1;
static std::vector<uint8_t> g_depth_state;
// Static buffer for returning JSON strings to JS
// Sized for bench_run output: ~200 fixed fields + 4096 text + 900 token_ids
static char g_result_buf[16384];
// Parse a comma-separated list of integers into token IDs
static std::vector<llama_token> parse_token_ids(const char* csv) {
std::vector<llama_token> ids;
const char* p = csv;
while (*p) {
while (*p == ' ' || *p == ',') p++;
if (!*p) break;
ids.push_back((llama_token)atoi(p));
while (*p && *p != ',') p++;
}
return ids;
}
extern "C" {
int bench_init() {
ggml_backend_load_all();
return 0;
}
int bench_load(const char * model_path, int n_ctx, int n_gpu_layers, int use_mmap) {
if (g_sampler) { llama_sampler_free(g_sampler); g_sampler = nullptr; }
if (g_ctx) { llama_free(g_ctx); g_ctx = nullptr; }
if (g_model) { llama_model_free(g_model); g_model = nullptr; }
// Old depth snapshot is bound to the freed context — invalidate it so
// the next bench_set_depth doesn't try to restore stale bytes.
g_depth_cached = -1;
g_depth_state.clear();
g_n_ctx = n_ctx;
// use_mmap=0 forces fread-based loading via the C library — required for
// OPFS-backed models, where the file is exposed via patched MEMFS
// stream_ops that route reads to a FileSystemSyncAccessHandle.
llama_model_params model_params = llama_model_default_params();
model_params.n_gpu_layers = n_gpu_layers;
model_params.use_mmap = use_mmap != 0;
g_model = llama_model_load_from_file(model_path, model_params);
if (!g_model) {
fprintf(stderr, "bench_load: failed to load model from %s\n", model_path);
return -1;
}
g_vocab = llama_model_get_vocab(g_model);
llama_context_params ctx_params = llama_context_default_params();
ctx_params.n_ctx = n_ctx;
ctx_params.n_batch = n_ctx;
ctx_params.no_perf = false;
g_ctx = llama_init_from_model(g_model, ctx_params);
if (!g_ctx) {
fprintf(stderr, "bench_load: failed to create context\n");
llama_model_free(g_model);
g_model = nullptr;
return -2;
}
auto sparams = llama_sampler_chain_default_params();
sparams.no_perf = false;
g_sampler = llama_sampler_chain_init(sparams);
llama_sampler_chain_add(g_sampler, llama_sampler_init_greedy());
return 0;
}
const char * bench_run(const char * prompt, int n_predict) {
if (!g_model || !g_ctx || !g_sampler) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"model not loaded\"}");
return g_result_buf;
}
// Tokenize
const int n_prompt_max = g_n_ctx;
const int n_prompt = -llama_tokenize(g_vocab, prompt, strlen(prompt), NULL, 0, true, true);
if (n_prompt <= 0 || n_prompt > n_prompt_max) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"tokenization failed, n_prompt=%d\"}", n_prompt);
return g_result_buf;
}
std::vector<llama_token> prompt_tokens(n_prompt);
if (llama_tokenize(g_vocab, prompt, strlen(prompt),
prompt_tokens.data(), prompt_tokens.size(), true, true) < 0) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"tokenization failed\"}");
return g_result_buf;
}
// Reset perf counters
llama_perf_context_reset(g_ctx);
// Prepare batch for prompt (prefill)
llama_batch batch = llama_batch_get_one(prompt_tokens.data(), prompt_tokens.size());
// Handle encoder-decoder models
if (llama_model_has_encoder(g_model)) {
if (llama_encode(g_ctx, batch)) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"encode failed\"}");
return g_result_buf;
}
llama_token decoder_start = llama_model_decoder_start_token(g_model);
if (decoder_start == LLAMA_TOKEN_NULL) {
decoder_start = llama_vocab_bos(g_vocab);
}
batch = llama_batch_get_one(&decoder_start, 1);
}
// Decode prompt (prefill)
if (llama_decode(g_ctx, batch)) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"prefill decode failed\"}");
return g_result_buf;
}
// Generate tokens
std::string output_text;
std::vector<llama_token> generated_ids;
int n_decoded = 0;
int n_pos = batch.n_tokens;
for (int i = 0; i < n_predict; i++) {
llama_token new_token = llama_sampler_sample(g_sampler, g_ctx, -1);
if (llama_vocab_is_eog(g_vocab, new_token)) {
break;
}
generated_ids.push_back(new_token);
// Convert token to text
char piece_buf[256];
int piece_len = llama_token_to_piece(g_vocab, new_token, piece_buf, sizeof(piece_buf), 0, true);
if (piece_len > 0) {
output_text.append(piece_buf, piece_len);
}
// Prepare next batch
batch = llama_batch_get_one(&new_token, 1);
if (llama_decode(g_ctx, batch)) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"decode failed at token %d\"}", i);
return g_result_buf;
}
n_decoded++;
n_pos++;
}
llama_perf_context_data perf = llama_perf_context(g_ctx);
// Escape output text for JSON (basic: replace " and \ and newlines)
std::string escaped_output;
for (char c : output_text) {
if (c == '"') escaped_output += "\\\"";
else if (c == '\\') escaped_output += "\\\\";
else if (c == '\n') escaped_output += "\\n";
else if (c == '\r') escaped_output += "\\r";
else if (c == '\t') escaped_output += "\\t";
else escaped_output += c;
}
// Truncate output for JSON if too long
if (escaped_output.size() > 4096) {
escaped_output.resize(4096);
escaped_output += "...(truncated)";
}
// Serialize generated token IDs as a compact JSON int array
std::string token_ids_str;
token_ids_str.reserve(generated_ids.size() * 6 + 2);
token_ids_str = "[";
for (size_t i = 0; i < generated_ids.size(); i++) {
if (i > 0) token_ids_str += ",";
token_ids_str += std::to_string(generated_ids[i]);
}
token_ids_str += "]";
snprintf(g_result_buf, sizeof(g_result_buf),
"{"
"\"success\":true,"
"\"n_prompt_tokens\":%d,"
"\"n_generated\":%d,"
"\"t_p_eval_ms\":%.2f,"
"\"t_eval_ms\":%.2f,"
"\"n_p_eval\":%d,"
"\"n_eval\":%d,"
"\"output\":\"%s\","
"\"token_ids\":%s"
"}",
n_prompt,
n_decoded,
perf.t_p_eval_ms,
perf.t_eval_ms,
perf.n_p_eval,
perf.n_eval,
escaped_output.c_str(),
token_ids_str.c_str()
);
return g_result_buf;
}
// Forced-decoding consistency check against a CPU reference token sequence.
// Feeds each reference token into the (already loaded) model one at a time and
// checks whether this backend independently agrees on the same top-1 token.
// This is the same "same top-1" metric used by llama.cpp's perplexity tool.
// ref_ids_csv: comma-separated token IDs from the CPU baseline run.
const char* bench_eval_tokens(const char* prompt, const char* ref_ids_csv) {
if (!g_model || !g_ctx) {
snprintf(g_result_buf, sizeof(g_result_buf), "{\"error\":\"model not loaded\"}");
return g_result_buf;
}
std::vector<llama_token> ref_ids = parse_token_ids(ref_ids_csv);
if (ref_ids.empty()) {
snprintf(g_result_buf, sizeof(g_result_buf), "{\"error\":\"no reference token ids\"}");
return g_result_buf;
}
// Start fresh — clear the memory (KV cache) from the bench_run that just completed
llama_memory_clear(llama_get_memory(g_ctx), false);
// Tokenize and prefill prompt (same as bench_run)
const int n_prompt = -llama_tokenize(g_vocab, prompt, strlen(prompt), NULL, 0, true, true);
if (n_prompt <= 0) {
snprintf(g_result_buf, sizeof(g_result_buf), "{\"error\":\"tokenization failed\"}");
return g_result_buf;
}
std::vector<llama_token> prompt_tokens(n_prompt);
llama_tokenize(g_vocab, prompt, strlen(prompt), prompt_tokens.data(), n_prompt, true, true);
llama_batch batch = llama_batch_get_one(prompt_tokens.data(), prompt_tokens.size());
if (llama_decode(g_ctx, batch)) {
snprintf(g_result_buf, sizeof(g_result_buf), "{\"error\":\"prefill failed\"}");
return g_result_buf;
}
// Forced decoding: at each position, check what this backend would pick,
// then advance context with the reference token (not our prediction).
const int n_vocab = llama_vocab_n_tokens(g_vocab);
const int n_tokens = (int)ref_ids.size();
int n_agree = 0;
int first_disagreement = -1;
// matches: compact "0"/"1" array for per-token agreement
std::string matches_str;
matches_str.reserve(n_tokens * 2 + 2);
matches_str = "[";
for (int i = 0; i < n_tokens; i++) {
float* logits = llama_get_logits(g_ctx);
// Argmax over vocabulary
llama_token top1 = 0;
float max_logit = logits[0];
for (int v = 1; v < n_vocab; v++) {
if (logits[v] > max_logit) {
max_logit = logits[v];
top1 = v;
}
}
const bool match = (top1 == ref_ids[i]);
if (match) n_agree++;
if (!match && first_disagreement < 0) first_disagreement = i;
if (i > 0) matches_str += ",";
matches_str += match ? "1" : "0";
// Feed reference token (forced) to build the correct context for next position
batch = llama_batch_get_one(&ref_ids[i], 1);
if (llama_decode(g_ctx, batch)) break;
}
matches_str += "]";
const float agreement_rate = n_tokens > 0 ? (float)n_agree / n_tokens : 0.0f;
snprintf(g_result_buf, sizeof(g_result_buf),
"{"
"\"agreement_rate\":%.4f,"
"\"n_agree\":%d,"
"\"n_tokens\":%d,"
"\"first_disagreement\":%d,"
"\"matches\":%s"
"}",
agreement_rate,
n_agree,
n_tokens,
first_disagreement,
matches_str.c_str()
);
return g_result_buf;
}
// Random-token prefill loop shared by bench_pp and bench_set_depth. Uses BOS
// as the first token if the vocab expects one (only when n_pos_start == 0,
// i.e. the cache is empty), random fillers otherwise. Returns 0 on success,
// or the position at which llama_decode failed.
static int prefill_random_tokens(int n_tokens_total, int n_pos_start) {
const int32_t n_vocab = llama_vocab_n_tokens(g_vocab);
const int n_batch = g_n_ctx;
std::vector<llama_token> tokens(n_batch);
int n_processed = 0;
while (n_processed < n_tokens_total) {
const int n_tokens = std::min(n_tokens_total - n_processed, n_batch);
const bool first_in_ctx = (n_pos_start == 0 && n_processed == 0);
tokens[0] = (first_in_ctx && llama_vocab_get_add_bos(g_vocab))
? llama_vocab_bos(g_vocab)
: std::rand() % n_vocab;
for (int i = 1; i < n_tokens; i++) {
tokens[i] = std::rand() % n_vocab;
}
if (llama_decode(g_ctx, llama_batch_get_one(tokens.data(), n_tokens))) {
return n_processed > 0 ? n_processed : 1;
}
n_processed += n_tokens;
}
return 0;
}
// Prefill the KV cache to a given depth so the subsequent bench_pp/bench_tg
// runs at non-zero context. Mirrors the depth setup in llama-bench's main
// loop (tools/llama-bench/llama-bench.cpp lines 2320-2358): clear the cache,
// random-token prefill, then snapshot via llama_state_seq_get_data so a
// follow-up call at the same depth restores the snapshot instead of
// recomputing. Caller times bench_pp/bench_tg only — this call is untimed
// setup, the same way llama-bench's depth fill is excluded from t_start.
//
// n_depth == 0 just clears the cache (no prefill, no snapshot reuse).
const char * bench_set_depth(int n_depth) {
if (!g_model || !g_ctx) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"model not loaded\"}");
return g_result_buf;
}
if (n_depth < 0) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"n_depth must be >= 0\"}");
return g_result_buf;
}
llama_memory_clear(llama_get_memory(g_ctx), false);
if (n_depth == 0) {
llama_synchronize(g_ctx);
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"success\":true,\"n_depth\":0,\"cached\":false}");
return g_result_buf;
}
// Cache hit: restore the snapshot we took on a previous call. set_data
// returns the number of bytes consumed; 0 means the snapshot is
// incompatible with the current context (e.g. n_ctx changed) and we
// fall through to re-prefill.
if (n_depth == g_depth_cached && !g_depth_state.empty()) {
const size_t ret = llama_state_seq_set_data(
g_ctx, g_depth_state.data(), g_depth_state.size(), 0);
if (ret > 0) {
llama_synchronize(g_ctx);
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"success\":true,\"n_depth\":%d,\"cached\":true}", n_depth);
return g_result_buf;
}
g_depth_cached = -1;
g_depth_state.clear();
}
const int err_pos = prefill_random_tokens(n_depth, 0);
if (err_pos != 0) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"depth decode failed near processed=%d\"}", err_pos);
return g_result_buf;
}
llama_synchronize(g_ctx);
g_depth_cached = n_depth;
g_depth_state.resize(llama_state_seq_get_size(g_ctx, 0));
llama_state_seq_get_data(g_ctx, g_depth_state.data(), g_depth_state.size(), 0);
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"success\":true,\"n_depth\":%d,\"cached\":false}", n_depth);
return g_result_buf;
}
// llama-bench-style synthetic-token prefill test. Mirrors test_prompt() in
// tools/llama-bench/llama-bench.cpp: BOS as the first token (when the cache
// is empty AND the vocab expects one) and uniformly-random fillers
// thereafter, batched up to n_ctx. Caller times the call; we just run the
// work, synchronize, and return. Unlike bench_run there is no sampler.
//
// Cache state is owned by the caller — bench_set_depth() must run first to
// either clear (d=0) or pre-fill the KV. We don't clear here so depth runs
// stay intact for the timed measurement.
const char * bench_pp(int n_prompt) {
if (!g_model || !g_ctx) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"model not loaded\"}");
return g_result_buf;
}
if (n_prompt <= 0) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"n_prompt must be > 0\"}");
return g_result_buf;
}
const int n_pos_start = (int) llama_memory_seq_pos_max(llama_get_memory(g_ctx), 0) + 1;
const int err_pos = prefill_random_tokens(n_prompt, n_pos_start);
if (err_pos != 0) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"prefill decode failed at processed=%d\"}", err_pos);
return g_result_buf;
}
llama_synchronize(g_ctx);
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"success\":true,\"n_prompt\":%d}", n_prompt);
return g_result_buf;
}
// llama-bench-style decode test. Mirrors test_gen() in
// tools/llama-bench/llama-bench.cpp: prime the first decode with BOS (only
// when the cache is empty AND the vocab expects BOS), random token
// otherwise, then loop n_gen single-token decodes with a synchronize after
// each — the synchronize is what makes wall time reflect per-token GPU
// latency rather than dispatch queue depth.
//
// Cache state is owned by the caller — bench_set_depth() must run first.
// When depth > 0 the cache already holds a BOS at position 0, so we never
// re-prime with BOS here.
const char * bench_tg(int n_gen) {
if (!g_model || !g_ctx) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"model not loaded\"}");
return g_result_buf;
}
if (n_gen <= 0) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"n_gen must be > 0\"}");
return g_result_buf;
}
const int32_t n_vocab = llama_vocab_n_tokens(g_vocab);
const int n_pos_start = (int) llama_memory_seq_pos_max(llama_get_memory(g_ctx), 0) + 1;
llama_token token = (n_pos_start == 0 && llama_vocab_get_add_bos(g_vocab))
? llama_vocab_bos(g_vocab)
: (llama_token)(std::rand() % n_vocab);
for (int i = 0; i < n_gen; i++) {
if (llama_decode(g_ctx, llama_batch_get_one(&token, 1))) {
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"error\":\"decode failed at token %d\"}", i);
return g_result_buf;
}
llama_synchronize(g_ctx);
token = std::rand() % n_vocab;
}
snprintf(g_result_buf, sizeof(g_result_buf),
"{\"success\":true,\"n_gen\":%d}", n_gen);
return g_result_buf;
}
// Memory snapshot from llama.cpp's perspective: loaded model size, current
// state-buffer size, and per-device free/total from every registered ggml
// backend. Intended to be called after bench_load so model_size reflects the
// loaded model and the per-device free counters reflect what's left after
// allocation. Safe to call before bench_load too (model_size + state_size
// will be 0). Returns the same g_result_buf — caller must consume before the
// next bench_* call overwrites it.
const char* bench_memory_info() {
const uint64_t model_size = g_model ? llama_model_size(g_model) : 0;
const size_t state_size = g_ctx ? llama_state_get_size(g_ctx) : 0;
std::string devices_json = "[";
const size_t n_dev = ggml_backend_dev_count();
for (size_t i = 0; i < n_dev; i++) {
ggml_backend_dev_t dev = ggml_backend_dev_get(i);
const char* name = ggml_backend_dev_name(dev);
// The function name `ggml_backend_dev_type` shadows the enum tag, so
// we have to spell it `enum ggml_backend_dev_type` to refer to the type.
const enum ggml_backend_dev_type t = ggml_backend_dev_type(dev);
const char* type_str = (t == GGML_BACKEND_DEVICE_TYPE_GPU) ? "GPU"
: (t == GGML_BACKEND_DEVICE_TYPE_ACCEL) ? "ACCEL"
: "CPU";
size_t free_mem = 0, total_mem = 0;
ggml_backend_dev_memory(dev, &free_mem, &total_mem);
if (i > 0) devices_json += ",";
char buf[512];
snprintf(buf, sizeof(buf),
"{\"name\":\"%s\",\"type\":\"%s\",\"free\":%zu,\"total\":%zu}",
name ? name : "", type_str, free_mem, total_mem);
devices_json += buf;
}
devices_json += "]";
snprintf(g_result_buf, sizeof(g_result_buf),
"{"
"\"model_size\":%llu,"
"\"state_size\":%zu,"
"\"devices\":%s"
"}",
(unsigned long long)model_size,
state_size,
devices_json.c_str()
);
return g_result_buf;
}
void bench_exit() {
if (g_sampler) { llama_sampler_free(g_sampler); g_sampler = nullptr; }
if (g_ctx) { llama_free(g_ctx); g_ctx = nullptr; }
if (g_model) { llama_model_free(g_model); g_model = nullptr; }
g_vocab = nullptr;
g_depth_cached = -1;
g_depth_state.clear();
llama_backend_free();
}
} // extern "C"
// Dummy main (--no-entry is set, but some linkers want it)
int main() { return 0; }