okami.mom/strata

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

Niko1221Merge f-bench-q2-range e2f9db84f into the batch 19 candidate (expert paths bench: per-expert blob change leaves scale bytes alone; bench_blob_layout_test)06da32193

main
4.5 KiB98 linesraw
1// bench_blob_layout (tests/prefill/bench_blob_layout.hpp): the byte prefill_expert_paths_bench changes per expert is never a scale byte, for every
2// format in the table.  No GPU, no model, no ggml.
3#include <cstdint>
4#include <cstdio>
5#include <set>
6#include <vector>
7
8#include "bench_blob_layout.hpp"
9#if defined(BBL_HAVE_QUANT)
10#include "strata/quant/types.hpp"
11#endif
12
13using strata::bench_blob::BlockLayout;
14using strata::bench_blob::is_scale;
15using strata::bench_blob::layout_of;
16using strata::bench_blob::perturb_offset;
17
18static int failed = 0;
19#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
20
21int main() {
22    const int types[] = {2, 3, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 29, 40, 42};
23    for (int t : types) {
24        BlockLayout L;
25        CHECK(layout_of(t, L));
26        CHECK(L.type == t && L.bytes > 0 && L.values > 0);
27        int data = 0;
28        for (int b = 0; b < L.bytes; ++b) data += !is_scale(L, b);
29        CHECK(data > 0 && data < L.bytes);   // every format has a scale and quant bytes
30        for (const auto& s : L.scale) CHECK(s.begin >= 0 && s.begin < s.end && s.end <= L.bytes);
31#if defined(BBL_HAVE_QUANT)
32        // the geometry equals the engine's own type table (the one the bench checks against at run time)
33        const strata::quant::TypeInfo* ti = strata::quant::type_info(t);
34        CHECK(ti != nullptr && (int) ti->bytes == L.bytes && ti->blck == L.values);
35#endif
36
37        // a base blob of 40 blocks (the bench's gate row of 2,560 values is 10 to 80 blocks), every expert of a 512-expert layer
38        const size_t n = (size_t) L.bytes * 40;
39        std::vector<uint8_t> base(n);
40        uint32_t x = 2463534242u + (uint32_t) t;
41        for (uint8_t& v : base) { x ^= x << 13; x ^= x >> 17; x ^= x << 5; v = (uint8_t) x; }
42        std::set<size_t> offs;
43        for (int e = 0; e < 512; ++e) {
44            const size_t o = perturb_offset(L, e);
45            CHECK(o >= 4 && o < n);
46            CHECK(!is_scale(L, (int) (o % (size_t) L.bytes)));
47            offs.insert(o);
48            std::vector<uint8_t> b = base;
49            b[o] ^= (uint8_t) e;
50            int differ = 0, scale_differ = 0;
51            for (size_t i = 0; i < n; ++i) {
52                if (b[i] == base[i]) continue;
53                ++differ;
54                scale_differ += is_scale(L, (int) (i % (size_t) L.bytes));
55            }
56            CHECK(scale_differ == 0);                              // the scales stay bit-equal to the base
57            CHECK(differ == ((uint8_t) e != 0 ? 1 : 0));           // one quant byte, or none where e is a multiple of 256
58        }
59        CHECK(offs.size() == 16);                                  // sixteen distinct places, as the fixed offsets had
60    }
61
62    // Q2_0: the old offsets 18 and 19 were block 1's scale; the walk takes 4..17, then 20 and 21
63    BlockLayout q;
64    CHECK(layout_of(42, q));
65    CHECK(is_scale(q, 18 % q.bytes) && is_scale(q, 19 % q.bytes));
66    CHECK(perturb_offset(q, 0) == 4 && perturb_offset(q, 13) == 17 && perturb_offset(q, 14) == 20 && perturb_offset(q, 15) == 21);
67    CHECK(perturb_offset(q, 31) == 21 && perturb_offset(q, 16) == 4);
68    // IQ4_NL as a gate/up type had the same fault (18-byte blocks)
69    BlockLayout nl;
70    CHECK(layout_of(20, nl) && perturb_offset(nl, 14) == 20);
71    // formats whose bytes 4..19 hold no scale bits keep the old offsets (and so the blobs of earlier runs)
72    for (int t : {6, 7, 8, 14, 17, 18, 21, 22, 29}) {
73        BlockLayout L;
74        CHECK(layout_of(t, L));
75        for (int e = 0; e < 16; ++e) CHECK(perturb_offset(L, e) == (size_t) (4 + e));
76    }
77    // IQ2_XXS: byte 9 and 17 carry a group's scale nibble, so the walk steps over them
78    BlockLayout xx;
79    CHECK(layout_of(16, xx));
80    CHECK(is_scale(xx, 9) && is_scale(xx, 17) && perturb_offset(xx, 4) == 8 && perturb_offset(xx, 5) == 10);
81    // IQ4_XS: scales_l sits at 4..7, so the walk starts at 8
82    BlockLayout x4;
83    CHECK(layout_of(23, x4) && perturb_offset(x4, 0) == 8);
84    // a type the table does not know is refused, not guessed
85    BlockLayout u;
86    CHECK(!layout_of(24, u) && !layout_of(144, u) && !layout_of(-1, u));
87
88    if (failed) {
89        std::fprintf(stderr, "bench_blob_layout_test: %d check(s) failed\n", failed);
90        return 1;
91    }
92#if defined(BBL_HAVE_QUANT)
93    std::printf("bench_blob_layout_test: OK (geometry checked against strata::quant::type_info)\n");
94#else
95    std::printf("bench_blob_layout_test: OK (no strata_quant in this build: geometry not cross-checked)\n");
96#endif
97    return 0;
98}