okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge commit 'e0a932a32' into f-split-launchda4a3a476

main
4.4 KiB85 linesraw
1// src/kernels/ple_fp8_parity.cpp - the FP8 PLE table (tools/ple_fp8_pack.py) read through PleTable, against values
2// decoded from the checkpoint itself, and against the IQ4_NL table it replaces.
3//
4//     ple_fp8_parity <ple-fp8.gguf> <reference.bin> [<iq4nl shard.gguf>]
5//
6// reference.bin: int32 n, uint32 rows[n], float32 values[n][160] - the rows as torch decodes the checkpoint's
7// F8_E4M3 bytes, times its weight_scale (n a multiple of 16 for the gather_batch check). Every row must match BIT FOR BIT through
8// both I/O modes (Direct and Mmap) and through the prompt path's gather_batch. With the IQ4_NL shard the same rows
9// are compared too: same table, so a correlation near 0.997 - a shifted row order would be ~0.
10#include "strata/kernels/ngram.hpp"
11
12#include <cmath>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <fstream>
17#include <string>
18#include <vector>
19
20namespace k = strata::kernels;
21
22int main(int argc, char** argv) {
23    if (argc < 3) {
24        std::fprintf(stderr, "usage: ple_fp8_parity <ple-fp8.gguf> <reference.bin> [<iq4nl shard.gguf>]\n");
25        return 2;
26    }
27    std::ifstream f(argv[2], std::ios::binary);
28    int32_t n = 0;
29    f.read((char*) &n, 4);
30    std::vector<uint32_t> rows((size_t) n);
31    std::vector<float> ref((size_t) n * k::PLE_HEAD_DIM);
32    f.read((char*) rows.data(), (std::streamsize) rows.size() * 4);
33    f.read((char*) ref.data(), (std::streamsize) ref.size() * 4);
34    if (!f || n <= 0) { std::fprintf(stderr, "cannot read %s\n", argv[2]); return 2; }
35
36    int bad = 0;
37    for (const k::PleIo mode : {k::PleIo::Direct, k::PleIo::Mmap}) {
38        k::PleTable t;
39        std::string err;
40        k::PleIoOptions io;
41        io.mode = mode;
42        if (!t.open(argv[1], err, io)) { std::fprintf(stderr, "open: %s\n", err.c_str()); return 1; }
43        std::vector<float> got((size_t) n * k::PLE_HEAD_DIM);
44        for (int i = 0; i < n; ++i) t.read_row(rows[(size_t) i], got.data() + (size_t) i * k::PLE_HEAD_DIM);
45        int diff = 0;
46        for (size_t j = 0; j < got.size(); ++j) diff += std::memcmp(&got[j], &ref[j], 4) != 0;
47        std::vector<float> batch((size_t) n * k::PLE_HEAD_DIM);   // the prompt path: all rows as one request
48        const bool ok = t.gather_batch(rows.data(), (size_t) n / k::PLE_N_HEADS, batch.data(), err);
49        int bdiff = 0;
50        const size_t nb = (size_t) n / k::PLE_N_HEADS * k::PLE_N_HEADS * k::PLE_HEAD_DIM;
51        for (size_t j = 0; ok && j < nb; ++j) bdiff += std::memcmp(&batch[j], &ref[j], 4) != 0;
52        std::printf("%-6s %s, %llu rows: read_row %d/%zu values differ, gather_batch %s %d differ\n",
53                    mode == k::PleIo::Direct ? "Direct" : "Mmap", t.format(), (unsigned long long) t.rows(), diff,
54                    got.size(), ok ? "ok," : "FAILED,", bdiff);
55        bad += diff + bdiff + (ok ? 0 : 1) + (std::strcmp(t.format(), std::getenv("PLE_EXPECT_FORMAT") ? std::getenv("PLE_EXPECT_FORMAT") : "F8_E4M3") != 0);
56    }
57    if (argc > 3) {
58        k::PleTable q;
59        std::string err;
60        if (!q.open(argv[3], err)) { std::fprintf(stderr, "open %s: %s\n", argv[3], err.c_str()); return 1; }
61        double cmin = 1, rsum = 0;
62        int counted = 0;
63        std::vector<float> v(k::PLE_HEAD_DIM);
64        for (int i = 0; i < n; ++i) {
65            q.read_row(rows[(size_t) i], v.data());
66            const float* r = ref.data() + (size_t) i * k::PLE_HEAD_DIM;
67            double sa = 0, sb = 0, sab = 0, sa2 = 0, sb2 = 0, e2 = 0, r2 = 0;
68            for (int j = 0; j < k::PLE_HEAD_DIM; ++j) {
69                sa += r[j]; sb += v[j]; sab += (double) r[j] * v[j]; sa2 += (double) r[j] * r[j]; sb2 += (double) v[j] * v[j];
70                e2 += ((double) v[j] - r[j]) * ((double) v[j] - r[j]); r2 += (double) r[j] * r[j];
71            }
72            if (r2 == 0) continue;                                      // an all-zero row (the table has some)
73            const double m = k::PLE_HEAD_DIM;
74            const double c = (sab - sa * sb / m) / std::sqrt((sa2 - sa * sa / m) * (sb2 - sb * sb / m));
75            cmin = std::fmin(cmin, c);
76            rsum += std::sqrt(e2 / r2);
77            ++counted;
78        }
79        std::printf("IQ4_NL table, same rows: correlation min %.4f, error vs FP8 %.1f%% mean (%d non-zero rows)\n",
80                    cmin, 100 * rsum / (counted ? counted : 1), counted);
81        if (cmin < 0.98) ++bad;
82    }
83    std::printf("RESULT: %s\n", bad ? "MISMATCH" : "FP8 rows exact");
84    return bad ? 1 : 0;
85}