interplane.Get Omega
Read in your language

ggml-omega

aien-dev/ggml-omega

Brought by drakestapleton

branch main Branches Tags History

ggml-omega / tests / e2e.c

91 lines · 4572 bytes Raw Blame History
1// tests/e2e.c: test infrastructure (not shipped). Greedy decode of a fixed prompt through llama.cpp's public
2// API with whatever backends ggml_backend_load_all() finds (GGML_BACKEND_PATH=libggml-omega.so adds Omega).
3// usage: e2e MODEL N_TOKENS LOGITS_OUT [THREADS]
4// stdout: one JSON line per generated step {"step":i,"token":t}, then {"tok_per_s":x,"prompt_tokens":p}.
5// LOGITS_OUT: n_vocab f32 values per step, raw, for tests/cmp.c. Diagnostics on stderr. Exit 0 on success.
6#include "llama.h"
7#include "ggml-backend.h"
8#include <errno.h>
9#include <limits.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <time.h>
14
15static const char *PROMPT = "The three primary colors of light are";
16
17static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + t.tv_nsec * 1e-9; }
18static void quiet(enum ggml_log_level l, const char *s, void *u) { (void) u; if (l >= GGML_LOG_LEVEL_WARN) fputs(s, stderr); }
19
20static int positive(const char *s) {
21 char *end;
22 errno = 0;
23 long v = strtol(s, &end, 10);
24 return errno || end == s || *end || v <= 0 || v > INT_MAX ? -1 : (int) v;
25}
26
27// Optional verification snapshot after inference, including libraries loaded lazily.
28static int save_maps(void) {
29 const char *path = getenv("E2E_MAPS_OUT");
30 if (!path) return 0;
31 FILE *in = fopen("/proc/self/maps", "rb"), *out = fopen(path, "wx");
32 if (!in || !out) { if (in) fclose(in); if (out) fclose(out); return 1; }
33 char buf[4096];
34 size_t n;
35 int bad = 0;
36 while ((n = fread(buf, 1, sizeof buf, in)) != 0)
37 if (fwrite(buf, 1, n, out) != n) { bad = 1; break; }
38 if (ferror(in)) bad = 1;
39 if (fclose(in) != 0) bad = 1;
40 if (fclose(out) != 0) bad = 1;
41 return bad;
42}
43
44int main(int argc, char **argv) {
45 if (argc < 4) { fprintf(stderr, "usage: e2e MODEL N_TOKENS LOGITS_OUT [THREADS]\n"); return 2; }
46 int n_gen = positive(argv[2]), threads = argc > 4 ? positive(argv[4]) : 2;
47 if (n_gen < 1 || threads < 1) { fprintf(stderr, "invalid token or thread count\n"); return 2; }
48 llama_log_set(quiet, NULL);
49 ggml_backend_load_all();
50 for (size_t i = 0; i < ggml_backend_dev_count(); i++)
51 fprintf(stderr, "device %zu: %s\n", i, ggml_backend_dev_name(ggml_backend_dev_get(i)));
52 struct llama_model_params mp = llama_model_default_params();
53 mp.n_gpu_layers = 0;
54 struct llama_model *model = llama_model_load_from_file(argv[1], mp);
55 if (!model) { fprintf(stderr, "model load failed\n"); return 1; }
56 const struct llama_vocab *vocab = llama_model_get_vocab(model);
57 int n_vocab = llama_vocab_n_tokens(vocab);
58 struct llama_context_params cp = llama_context_default_params();
59 cp.n_ctx = 256; cp.n_batch = 64; cp.n_ubatch = 64; cp.n_threads = threads; cp.n_threads_batch = threads;
60 cp.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_DISABLED;
61 cp.no_perf = true;
62 struct llama_context *ctx = llama_init_from_model(model, cp);
63 if (!ctx) { fprintf(stderr, "context failed\n"); return 1; }
64 llama_token toks[64];
65 int n_p = llama_tokenize(vocab, PROMPT, (int) strlen(PROMPT), toks, 64, true, false);
66 if (n_p <= 0) { fprintf(stderr, "tokenize failed\n"); return 1; }
67 if ((uint64_t) n_p + (uint64_t) n_gen - 1 > llama_n_ctx(ctx)) { fprintf(stderr, "generation exceeds context\n"); return 2; }
68 FILE *lf = fopen(argv[3], "wx");
69 if (!lf) { perror(argv[3]); return 1; }
70 struct llama_batch b = llama_batch_get_one(toks, n_p);
71 double t0 = now(), t_first = 0;
72 llama_token cur;
73 for (int i = 0; i < n_gen; i++) {
74 if (llama_decode(ctx, b) != 0) { fprintf(stderr, "decode failed at step %d\n", i); return 1; }
75 if (i == 0) t_first = now();
76 const float *lg = llama_get_logits_ith(ctx, -1);
77 if (fwrite(lg, sizeof(float), (size_t) n_vocab, lf) != (size_t) n_vocab) { perror("write"); return 1; }
78 cur = 0;
79 for (int v = 1; v < n_vocab; v++) if (lg[v] > lg[cur]) cur = v;
80 printf("{\"step\":%d,\"token\":%d}\n", i, cur);
81 b = llama_batch_get_one(&cur, 1);
82 }
83 double t1 = now();
84 if (fclose(lf) != 0) { perror("close logits"); return 1; }
85 if (save_maps() != 0) { fprintf(stderr, "cannot save process maps\n"); return 1; }
86 // decode rate over the single-token steps (prompt step excluded)
87 printf("{\"n_vocab\":%d,\"prompt_tokens\":%d,\"prompt_s\":%.6f,\"gen_steps\":%d,\"gen_s\":%.6f,\"threads\":%d}\n", n_vocab, n_p, t_first - t0, n_gen - 1, t1 - t_first, threads);
88 llama_free(ctx);
89 llama_model_free(model);
90 return 0;
91}