okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
5.9 KiB128 linesraw
1// A token's native expert result must not depend on how many other tokens share its group.
2// Compare the invariant entry points with the production singleton dispatch, including SwiGLU.
3// Synthetic packed blocks only: no GPU, model download or private fixtures.
4#include "ggml.h"
5#include "strata/kernels/cpu/native_expert.hpp"
6#include "strata/kernels/cpu/tq_cpu.hpp"
7#include <cmath>
8#include <cstdio>
9#include <cstring>
10#include <random>
11#include <vector>
12#include <utility>
13
14namespace cpu = strata::kernels::cpu;
15namespace {
16constexpr int width = 8, hidden = 512;
17size_t checked = 0;
18bool equal(const std::vector<float> &actual, const std::vector<float> &expected) {
19    for (size_t i = 0; i < actual.size(); ++i) {
20        ++checked;
21        if (!std::isfinite(actual[i]) || std::memcmp(&actual[i], &expected[i], sizeof(float)))
22            return false;
23    }
24    return true;
25}
26
27bool check_format(int gu, int down, int ff, bool folded = false) {
28    cpu::NativeFmt fmt;
29    std::string error;
30    if (!cpu::native_fmt(gu, down, hidden, ff, fmt, error)) {
31        std::fprintf(stderr, "%s\n", error.c_str());
32        return false;
33    }
34    std::vector<float> gu_signs(hidden), down_signs(ff);
35    if (folded) {
36        for (int i = 0; i < hidden; ++i) gu_signs[i] = i % 3 ? 1.f : -1.f;
37        for (int i = 0; i < ff; ++i) down_signs[i] = i % 5 ? 1.f : -1.f;
38        fmt.had_block = 128;
39        fmt.had_gu = fmt.had_d = true;
40        fmt.gu_signs = gu_signs.data();
41        fmt.d_signs = down_signs.data();
42    }
43    std::mt19937 rng(20261016 + gu * 100 + down + ff);
44    std::vector<uint8_t> blob(fmt.bytes);
45    for (auto &byte : blob)
46        byte = static_cast<uint8_t>(rng());
47    // Use finite block scales and random packed values. NVFP4 has four UE4M3 sub-block scales.
48    const uint16_t scale = 0x2400; // 2^-6; exclude NaN/Inf scales from the random bytes.
49    auto scales = [&](size_t first, size_t last, int type) {
50        const size_t block = cpu::tq_supported(type) ? cpu::tq_block_bytes(type) : ggml_type_size(static_cast<ggml_type>(type));
51        for (size_t at = first; at < last; at += block) {
52            if (type == cpu::kNvfp4Type) std::memset(blob.data() + at, 0x28, 4); // finite UE4M3 sub-block scales
53            else {
54                std::memcpy(blob.data() + at, &scale, sizeof(scale));
55                if (type == 12 || type == 7) std::memcpy(blob.data() + at + 2, &scale, sizeof(scale));
56            }
57        }
58    };
59    scales(0, fmt.down_off, gu);
60    scales(fmt.down_off, fmt.tail_off ? fmt.tail_off : blob.size(), down);
61    if (fmt.tail_off) {
62        const float tail[4] = {gu == cpu::kNvfp4Type ? .5f : 1.f, gu == cpu::kNvfp4Type ? .75f : 1.f,
63                               down == cpu::kNvfp4Type ? 1.25f : 1.f, 0.f};
64        std::memcpy(blob.data() + fmt.tail_off, tail, sizeof(tail));
65    }
66
67    std::vector<std::vector<uint8_t>> act(width, std::vector<uint8_t>(fmt.act_bytes));
68    std::vector<std::vector<uint8_t>> hq(width, std::vector<uint8_t>(fmt.h_bytes));
69    std::vector<std::vector<float>> gates(width, std::vector<float>(ff)), downs(width, std::vector<float>(hidden));
70    auto solo_gates = gates, solo_downs = downs;
71    const void *ap[width], *hp[width];
72    float *gp[width], *dp[width], *sg[width], *sd[width];
73    for (int t = 0; t < width; ++t) {
74        ap[t] = act[t].data();
75        hp[t] = hq[t].data();
76        gp[t] = gates[t].data();
77        dp[t] = downs[t].data();
78        sg[t] = solo_gates[t].data();
79        sd[t] = solo_downs[t].data();
80    }
81    for (float amplitude : {0.f, .01f, .3f, 3.f}) {
82        std::vector<float> x(hidden), h(ff);
83        for (int t = 0; t < width; ++t) {
84            for (auto &v : x)
85                v = (static_cast<int>(rng() % 2049) - 1024) * amplitude / 1024.f;
86            for (auto &v : h)
87                v = (static_cast<int>(rng() % 2049) - 1024) * amplitude / 1024.f;
88            cpu::native_quant_act(fmt, x.data(), act[t].data());
89            cpu::native_quant_h(fmt, h.data(), hq[t].data());
90            cpu::native_gu_rows(fmt, blob.data(), ap + t, 1, sg + t, 0, ff);
91            cpu::native_down_rows(fmt, blob.data(), hp + t, 1, sd + t, 0, hidden);
92        }
93        for (int nt = 1; nt <= width; ++nt) {
94            cpu::native_gu_rows_invariant(fmt, blob.data(), ap, nt, gp, 0, ff);
95            cpu::native_down_rows_invariant(fmt, blob.data(), hp, nt, dp, 0, hidden);
96            for (int t = 0; t < nt; ++t) {
97                if (!equal(gates[t], solo_gates[t]) || !equal(downs[t], solo_downs[t])) {
98                    std::fprintf(stderr, "native arithmetic mismatch: gu=%d down=%d ff=%d nt=%d token=%d scale=%g\n",
99                                 gu, down, ff, nt, t, amplitude);
100                    return false;
101                }
102            }
103        }
104    }
105    return true;
106}
107} // namespace
108
109int main() {
110    // 16/17 and IQ4_XS exercise the per-token legacy fallback as well as optimized 18/21/22.
111    for (int gu : {16, 17, 18, 21, 22, 23}) {
112        if (!check_format(gu, 20, 64) || !check_format(gu, 20, 96) || !check_format(gu, 42, 64))
113            return 1;
114    }
115    // NVFP4's kernels already preserve singleton arithmetic. Mixed formats also carry scale tails.
116    for (auto pair : {std::pair{40, 40}, {40, 20}, {40, 42}, {22, 40}, {23, 40}})
117        if (!check_format(pair.first, pair.second, 64)) return 1;
118    // STRATA_KQ256 selects grouped Q4_K/Q5_1/Q8_0 kernels only when their target arithmetic matches.
119    if (!check_format(12, 7, 64) || !check_format(12, 8, 64)) return 1;
120    // 0.2 adds Trellis formats and folded expert inputs. Exercise both their
121    // production singleton dispatch and every grouped width through the new API.
122    for (int gu : {144, 145, 146})
123        for (int down : {20, 144, 145, 146})
124            for (bool folded : {false, true})
125                if (!check_format(gu, down, 256, folded)) return 1;
126    if (!check_format(22, 20, 256, true)) return 1;
127    std::printf("native_arithmetic_parity: %zu finite bitwise comparisons passed\n", checked);
128}