okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
8.0 KiB176 linesraw
1// src/kernels/latent_cpu_verify.cpp - CPU latent expert path, per-stage vs the test bundle's
2// Python float32 reference.  Isolates D (Q8_0 matvec), A (native NVFP4 gu+down), U (Q8_0 matvec).
3//
4//     latent_cpu_verify <layerL_test.bin> <latent_a_pack> <latent_dense_q8.bin> <layer>
5//
6// Each stage is fed the *reference* output of the previous stage so the errors don't compound:
7//   xl = D . x              (compare ref_xl)
8//   yl = A(xl := ref_xl)    (compare ref_yl)   -- A = swiglu(A_gu . xl) -> A_dn . ff
9//   y  = U(yl := ref_yl)    (compare ref_y)
10//   out = combine           (compare ref_out, from my own xl/yl/y)
11#include "strata/core/latent_dense.hpp"
12#include "strata/core/expert_source.hpp"
13#include "strata/kernels/cpu/expert_layout.hpp"
14#include "strata/kernels/cpu/native_expert.hpp"
15
16#include <cmath>
17#include <cstdint>
18#include <cstdio>
19#include <cstdlib>
20#include <cstring>
21#include <fstream>
22#include <string>
23#include <vector>
24
25namespace cpu = strata::kernels::cpu;
26
27// ---- copied verbatim from expert_source.cpp (latent_cpu_run's helpers) ----
28static inline float latent_f16_to_f32(uint16_t h) {
29    const uint32_t sign = (uint32_t)(h >> 15) & 1u;
30    uint32_t e = (h >> 10) & 0x1Fu, m = h & 0x3FFu, f;
31    if (e == 0) {
32        if (m == 0) f = sign << 31;
33        else { e = 127 - 15 + 1; while (!(m & 0x400u)) { m <<= 1; --e; } m &= 0x3FFu; f = (sign << 31) | (e << 23) | (m << 13); }
34    } else if (e == 31) f = (sign << 31) | 0x7F800000u | (m << 13);
35    else f = (sign << 31) | ((e - 15 + 127) << 23) | (m << 13);
36    float out; std::memcpy(&out, &f, 4); return out;
37}
38static void q8_0_matvec_f32(const uint8_t* W, size_t row_bytes, int nrows, int ncols, const float* x, float* y) {
39    const int nb = ncols / 32;
40    for (int r = 0; r < nrows; ++r) {
41        const uint8_t* row = W + (size_t) r * row_bytes;
42        float acc = 0.f;
43        for (int b = 0; b < nb; ++b) {
44            const uint8_t* blk = row + (size_t) b * 34;
45            const float d = latent_f16_to_f32((uint16_t)(blk[0] | (blk[1] << 8)));
46            const int8_t* q = (const int8_t*)(blk + 2);
47            const float* xb = x + (size_t) b * 32;
48            float s = 0.f;
49            for (int j = 0; j < 32; ++j) s += (float) q[j] * xb[j];
50            acc += d * s;
51        }
52        y[r] = acc;
53    }
54}
55// ---- end copied ----
56
57namespace {
58template<typename T>
59std::vector<T> read_n(std::ifstream& f, size_t n, const char* what) {
60    std::vector<T> v(n);
61    if (!f.read((char*) v.data(), (std::streamsize) (n * sizeof(T)))) {
62        std::fprintf(stderr, "short read: %s\n", what);
63        std::exit(1);
64    }
65    return v;
66}
67double rel_l2(const float* got, const float* ref, size_t n) {
68    double num = 0, den = 0;
69    for (size_t i = 0; i < n; ++i) {
70        const double d = (double) got[i] - (double) ref[i];
71        num += d * d; den += (double) ref[i] * (double) ref[i];
72    }
73    return std::sqrt(num / (den + 1e-30));
74}
75float max_abs(const float* got, const float* ref, size_t n) {
76    float m = 0;
77    for (size_t i = 0; i < n; ++i) m = std::fmax(m, std::fabs(got[i] - ref[i]));
78    return m;
79}
80void report(const char* name, const std::vector<float>& got, const std::vector<float>& ref) {
81    std::printf("  %-4s relL2 %.4f%%  max_abs %.6g\n", name,
82                100 * rel_l2(got.data(), ref.data(), got.size()),
83                max_abs(got.data(), ref.data(), got.size()));
84}
85}  // namespace
86
87int main(int argc, char** argv) {
88    if (argc < 5) { std::fprintf(stderr, "usage: %s test.bin latent_a_pack dense_q8.bin layer\n", argv[0]); return 2; }
89    const int layer = std::atoi(argv[4]);
90    std::ifstream f(argv[1], std::ios::binary);
91    if (!f) { std::fprintf(stderr, "cannot open %s\n", argv[1]); return 2; }
92    char magic[4];
93    f.read(magic, 4);
94    if (std::memcmp(magic, "LAT0", 4) != 0) { std::fprintf(stderr, "bad magic\n"); return 2; }
95    int32_t hdr[7];
96    if (!f.read((char*) hdr, sizeof hdr)) return 2;
97    const int T = hdr[0], K = hdr[1], G = hdr[2], E = hdr[3], H = hdr[4], ELL = hdr[5], I = hdr[6];
98    std::printf("layer=%d T=%d K=%d G=%d E=%d H=%d ELL=%d I=%d\n", layer, T, K, G, E, H, ELL, I);
99    if (T * K > 128) { std::fprintf(stderr, "cap too large\n"); return 2; }
100
101    const auto x = read_n<float>(f, (size_t) T * H, "x");
102    const auto topk = read_n<int32_t>(f, (size_t) T * K, "topk");
103    const auto w = read_n<float>(f, (size_t) T * K, "w");
104    const auto labels = read_n<int32_t>(f, (size_t) E, "labels");
105    const size_t drow = (size_t) H / 32 * 34, urow = (size_t) ELL / 32 * 34;
106    f.seekg((std::streamoff) ((size_t) G * ELL * drow + (size_t) G * H * urow), std::ios::cur);
107    const auto ref_xl = read_n<float>(f, (size_t) T * G * ELL, "ref_xl");
108    const auto ref_yl = read_n<float>(f, (size_t) T * K * ELL, "ref_yl");
109    const auto ref_y = read_n<float>(f, (size_t) T * K * H, "ref_y");
110    const auto ref_out = read_n<float>(f, (size_t) T * H, "ref_out");
111
112    std::string err;
113    if (!cpu::expert_layout_load(argv[2], 48, 512, err)) { std::fprintf(stderr, "layout: %s\n", err.c_str()); return 2; }
114    strata::core::FileExpertSource src;
115    if (!src.open(argv[2], 48, 512, err)) { std::fprintf(stderr, "source: %s\n", err.c_str()); return 2; }
116    strata::core::LatentDense dense;
117    if (!dense.open(argv[3], 48, G, H, ELL, err)) { std::fprintf(stderr, "dense: %s\n", err.c_str()); return 2; }
118
119    const cpu::NativeFmt& nf = cpu::expert_layout().fmt[(size_t) layer];
120    std::printf("NativeFmt: gu_type=%d d_type=%d n_embd=%lld n_ff=%lld gu_row=%zu up_off=%zu down_off=%zu act_bytes=%zu h_bytes=%zu tail_off=%zu\n",
121                nf.gu_type, nf.d_type, (long long) nf.n_embd, (long long) nf.n_ff,
122                nf.gu_row, nf.up_off, nf.down_off, nf.act_bytes, nf.h_bytes, nf.tail_off);
123
124    const uint8_t* D = dense.d(layer);
125    const uint8_t* U = dense.u(layer);
126
127    // ---- D stage: xl = D . x, compare ref_xl ----
128    std::vector<float> my_xl((size_t) T * G * ELL);
129    for (int t = 0; t < T; ++t)
130        for (int g = 0; g < G; ++g)
131            q8_0_matvec_f32(D + (size_t) g * ELL * drow, drow, ELL, H, x.data() + (size_t) t * H,
132                            my_xl.data() + ((size_t) t * G + g) * ELL);
133    report("xl", my_xl, ref_xl);
134
135    // ---- A stage: yl = A(ref_xl), compare ref_yl (isolated: feed ref_xl, not my_xl) ----
136    std::vector<float> my_yl((size_t) T * K * ELL);
137    {
138        std::vector<uint8_t> act((size_t) cpu::kNativeActBytes), hq((size_t) cpu::kNativeHBytes);
139        std::vector<float> ff((size_t) I);
140        const void* actp = act.data(); float* ffp = ff.data();
141        const void* hqp = hq.data(); float* hp = nullptr;
142        for (int i = 0; i < T * K; ++i) {
143            const int e = topk[i], t = i / K, g = labels[(size_t) e];
144            const uint8_t* blob = src.blob(layer, e);
145            if (blob == nullptr) { std::fprintf(stderr, "no blob for e=%d\n", e); return 2; }
146            const float* xl_tg = ref_xl.data() + ((size_t) t * G + g) * ELL;
147            cpu::native_quant_act(nf, xl_tg, act.data());
148            cpu::native_gu_rows(nf, blob, &actp, 1, &ffp, 0, I);
149            cpu::native_quant_h(nf, ff.data(), hq.data());
150            hp = my_yl.data() + (size_t) i * ELL;
151            cpu::native_down_rows(nf, blob, &hqp, 1, &hp, 0, ELL);
152        }
153    }
154    report("yl", my_yl, ref_yl);
155
156    // ---- U stage: y = U(ref_yl), compare ref_y (isolated: feed ref_yl, not my_yl) ----
157    std::vector<float> my_y((size_t) T * K * H);
158    for (int i = 0; i < T * K; ++i) {
159        const int e = topk[i], g = labels[(size_t) e];
160        q8_0_matvec_f32(U + (size_t) g * H * urow, urow, H, ELL, ref_yl.data() + (size_t) i * ELL,
161                        my_y.data() + (size_t) i * H);
162    }
163    report("y", my_y, ref_y);
164
165    // ---- combine (using my own xl/yl/y): out[t] = sum_k w[r] * y[r] ----
166    std::vector<float> my_out((size_t) T * H, 0.f);
167    for (int i = 0; i < T * K; ++i) {
168        const int t = i / K;
169        const float wi = w[(size_t) i];
170        for (int c = 0; c < H; ++c) my_out[(size_t) t * H + c] += wi * my_y[(size_t) i * H + c];
171    }
172    report("out", my_out, ref_out);
173
174    std::printf("DONE\n");
175    return 0;
176}