ggml-omega
aien-dev/ggml-omega
Brought by drakestapleton
ggml-omega / tests / cmp.c
| 1 | // tests/cmp.c: test infrastructure (not shipped). Compares two e2e logits files (n_vocab f32 per step). |
| 2 | // usage: cmp N_VOCAB A.logits B.logits [EXPECTED_STEPS] |
| 3 | // stdout, one JSON line: steps, top1_agree (argmax equal per step), first_divergence (-1 if none), |
| 4 | // max_abs_diff over steps before the first divergence (same context in both runs), and its step. |
| 5 | // Exit 0 only for nonempty, finite, complete rows with identical greedy tokens. |
| 6 | // EXPECTED_STEPS additionally refuses equally truncated files. |
| 7 | #include <errno.h> |
| 8 | #include <limits.h> |
| 9 | #include <math.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | static long positive(const char *s) { |
| 15 | char *end; |
| 16 | errno = 0; |
| 17 | long v = strtol(s, &end, 10); |
| 18 | return errno || end == s || *end || v <= 0 ? -1 : v; |
| 19 | } |
| 20 | |
| 21 | int main(int argc, char **argv) { |
| 22 | if (argc != 4 && argc != 5) { fprintf(stderr, "usage: cmp N_VOCAB A.logits B.logits [EXPECTED_STEPS]\n"); return 2; } |
| 23 | long nv = positive(argv[1]), expected = argc == 5 ? positive(argv[4]) : -1; |
| 24 | if (nv <= 0 || (uintmax_t) nv > SIZE_MAX / sizeof(float) || (argc == 5 && expected <= 0)) { |
| 25 | fprintf(stderr, "invalid vocabulary or step count\n"); return 2; |
| 26 | } |
| 27 | FILE *a = fopen(argv[2], "rb"), *b = fopen(argv[3], "rb"); |
| 28 | if (!a || !b) { fprintf(stderr, "cannot open inputs\n"); if (a) fclose(a); if (b) fclose(b); return 1; } |
| 29 | float *x = malloc(sizeof(float) * nv), *y = malloc(sizeof(float) * nv); |
| 30 | if (!x || !y) { fprintf(stderr, "allocation failed\n"); free(x); free(y); fclose(a); fclose(b); return 1; } |
| 31 | long steps = 0, agree = 0, first_div = -1, worst_step = -1; |
| 32 | double worst = 0; |
| 33 | int failed = 0; |
| 34 | for (;;) { |
| 35 | size_t bytes = sizeof(float) * (size_t) nv; |
| 36 | size_t ra = fread(x, 1, bytes, a), rb = fread(y, 1, bytes, b); |
| 37 | if (ferror(a) || ferror(b)) { fprintf(stderr, "step %ld: read error\n", steps); failed = 1; break; } |
| 38 | if (ra == 0 && rb == 0) break; |
| 39 | if (ra != bytes || rb != bytes) { fprintf(stderr, "step %ld: incomplete row\n", steps); failed = 1; break; } |
| 40 | for (long v = 0; v < nv; v++) if (!isfinite(x[v]) || !isfinite(y[v])) { |
| 41 | fprintf(stderr, "step %ld: nonfinite logits\n", steps); failed = 1; break; |
| 42 | } |
| 43 | if (failed) break; |
| 44 | long ia = 0, ib = 0; |
| 45 | for (long v = 1; v < nv; v++) { if (x[v] > x[ia]) ia = v; if (y[v] > y[ib]) ib = v; } |
| 46 | if (ia == ib) agree++; |
| 47 | if (first_div < 0) { |
| 48 | for (long v = 0; v < nv; v++) { |
| 49 | double d = fabs((double) x[v] - (double) y[v]); |
| 50 | if (!(d <= worst)) { worst = d; worst_step = steps; } |
| 51 | } |
| 52 | if (ia != ib) first_div = steps; |
| 53 | } |
| 54 | steps++; |
| 55 | } |
| 56 | if (steps == 0 || (expected > 0 && steps != expected)) { |
| 57 | fprintf(stderr, "unexpected step count: %ld\n", steps); failed = 1; |
| 58 | } |
| 59 | if (first_div >= 0) failed = 1; |
| 60 | printf("{\"steps\":%ld,\"top1_agree\":%ld,\"first_divergence\":%ld,\"max_abs_diff\":%.9g,\"max_abs_diff_step\":%ld}\n", |
| 61 | steps, agree, first_div, worst, worst_step); |
| 62 | free(x); free(y); |
| 63 | if (fclose(a) != 0) failed = 1; |
| 64 | if (fclose(b) != 0) failed = 1; |
| 65 | return failed; |
| 66 | } |