okami.mom/strata

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

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

main
7.6 KiB140 linesraw
1// src/prefill/lat_q3k_swiglu_test.cpp - M2b of the q3k latent A stage: SwiGLU + the q8_1 of H.
2// Two gates, kept apart so a failure says which half is wrong:
3//   1. SwiGLU: the float H the kernel computed against a double silu(g) * u   (rel_mean <= 1e-6)
4//   2. the quantizer: the kernel's staged codes and d's against q8_1_store's rule applied on the host to the SAME
5//      float H the kernel wrote out   (codes byte for byte, d's bit for bit)
6#include "strata/prefill/lat_q3k_swiglu.hpp"
7
8#include <cuda_runtime.h>
9#include <ggml.h>
10
11#include <cmath>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <random>
16#include <vector>
17
18#define CK(e) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \
19    std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0)
20
21int main() {
22    constexpr int64_t FF = strata::prefill::lat_q3k_swiglu_ff;
23    constexpr int64_t HQ = strata::prefill::lat_q3k_swiglu_hq_row;
24    constexpr int E = 512;                        // entries (the engine's 4K shape: 512 entries a launch)
25    constexpr int NB = (int) (FF / 32);           // 20 q8_1 blocks per entry (n_ff / 32 - the width that bit me in M1)
26    std::printf("M2b: %d entries x %lld features, hq %lld B an entry (%d blocks)\n", E, (long long) FF, (long long) HQ, NB);
27
28    // gate/up as M2a writes them: real products, entry-major, a spread of magnitudes so the amax varies per block
29    std::mt19937 rng(23);
30    std::normal_distribution<float> ng(0.f, 2.0f), nu(0.f, 1.5f);
31    std::vector<float> gate((size_t) E * FF), up((size_t) E * FF);
32    for (size_t i = 0; i < gate.size(); ++i) { gate[i] = ng(rng); up[i] = nu(rng); }
33    // a few hard blocks: all zeros, one huge value, a negative gate, a near-tie (rounding boundary)
34    gate[0] = 0.f; up[0] = 0.f;
35    for (int f = 0; f < 32; ++f) { gate[(size_t) FF + f] = 0.f; up[(size_t) FF + f] = 0.f; }        // an all-zero block
36    gate[(size_t) 2 * FF] = 12.5f; up[(size_t) 2 * FF] = 3.0f;                                       // one big value
37    for (int f = 0; f < 32; ++f) gate[(size_t) 3 * FF + f] = -8.0f + 0.01f * f;                      // silu of negatives
38
39    float* dgate = nullptr; float* dup = nullptr; uint8_t* dhq = nullptr; float* dhout = nullptr;
40    CK(cudaMalloc(&dgate, gate.size() * 4)); CK(cudaMalloc(&dup, up.size() * 4));
41    CK(cudaMalloc(&dhq, (size_t) E * HQ)); CK(cudaMalloc(&dhout, gate.size() * 4));
42    CK(cudaMemcpy(dgate, gate.data(), gate.size() * 4, cudaMemcpyHostToDevice));
43    CK(cudaMemcpy(dup, up.data(), up.size() * 4, cudaMemcpyHostToDevice));
44    CK(cudaMemset(dhq, 0xAA, (size_t) E * HQ));       // so an unwritten byte shows
45    strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, dhout, nullptr);
46    CK(cudaDeviceSynchronize());
47    std::vector<float> hout(gate.size());
48    std::vector<uint8_t> hq((size_t) E * HQ);
49    CK(cudaMemcpy(hout.data(), dhout, hout.size() * 4, cudaMemcpyDeviceToHost));
50    CK(cudaMemcpy(hq.data(), dhq, hq.size(), cudaMemcpyDeviceToHost));
51
52    // ---- gate 1: the SwiGLU floats against double silu(g) * u ------------------------------------------------
53    {
54        double sabs = 0, sref = 0, num = 0, aa = 0, bb = 0;
55        float dmax = 0.f;
56        int bad = 0;
57        for (size_t i = 0; i < hout.size(); ++i) {
58            const double g = gate[i], u = up[i];
59            const double ref = (g / (1.0 + std::exp(-g))) * u;
60            const double d = std::fabs((double) hout[i] - ref);
61            sabs += d; sref += std::fabs(ref); dmax = std::fmax(dmax, (float) d);
62            num += (double) hout[i] * ref; aa += (double) hout[i] * hout[i]; bb += ref * ref;
63            if (d > 1e-6 * std::fabs(ref) + 1e-12) ++bad;
64        }
65        std::printf("swiglu: rel_mean %.3e  max|d| %.6e  cos %.9f  values off by >1e-6 rel: %d\n",
66                    sref > 0 ? sabs / sref : 0.0, (double) dmax, num / (std::sqrt(aa) * std::sqrt(bb) + 1e-30), bad);
67        if (!((sref > 0 ? sabs / sref : 1.0) <= 1e-6)) { std::printf("FAIL swiglu\n"); return 1; }
68    }
69
70    // ---- gate 2: the quantizer against q8_1_store's rule on the host, same floats ----------------------------
71    {
72        int code_bad = 0, d_bad = 0, blocks = 0, zeros = 0;
73        double dmax_rel = 0;
74        for (int e = 0; e < E; ++e) {
75            const uint8_t* row = hq.data() + (size_t) e * HQ;
76            for (int b = 0; b < NB; ++b) {
77                const float* hv = hout.data() + (size_t) e * FF + 32 * b;
78                float am = 0.f;
79                for (int v = 0; v < 32; ++v) am = std::fmax(am, std::fabs(hv[v]));
80                float d = am / 127.0f;                                  // q8_1_finite: the clamp is > 65504
81                if (std::fabs(d) > 65504.0f) d = std::copysign(65504.0f, d);
82                if (am == 0.f) ++zeros;
83                const uint8_t* ch = row + (size_t) (b >> 1) * 80;
84                uint16_t dh;
85                std::memcpy(&dh, ch + 64 + 2 * (b & 1), 2);
86                const float dmine = ggml_fp16_to_fp32(dh);
87                dmax_rel = std::max(dmax_rel, std::fabs((double) dmine - (double) d) / (std::fabs((double) d) + 1e-30));
88                if (dh != ggml_fp32_to_fp16(d)) ++d_bad;
89                for (int v = 0; v < 32; ++v) {
90                    const float q = am == 0.f ? 0.f : roundf(hv[v] / d);
91                    const int8_t qc = (int8_t) (q > 127.f ? 127.f : q < -127.f ? -127.f : q);
92                    const uint8_t got = ch[16 * (2 * (b & 1) + (v >> 4)) + 4 * (v & 3) + ((v & 15) >> 2)];
93                    if ((int8_t) got != qc) ++code_bad;
94                }
95                ++blocks;
96            }
97        }
98        std::printf("q8_1: %d blocks (%d all-zero)  code mismatches %d  d mismatches %d  max d rel %.3e\n", blocks, zeros,
99                    code_bad, d_bad, dmax_rel);
100        if (code_bad != 0 || d_bad != 0) { std::printf("FAIL quantizer\n"); return 1; }
101    }
102
103    // ---- informational: the dequantized H against the float H (the q8_1's own noise) ------------------------
104    {
105        double sabs = 0, sref = 0;
106        for (int e = 0; e < E; ++e) {
107            const uint8_t* row = hq.data() + (size_t) e * HQ;
108            for (int b = 0; b < NB; ++b) {
109                const uint8_t* ch = row + (size_t) (b >> 1) * 80;
110                uint16_t dh;
111                std::memcpy(&dh, ch + 64 + 2 * (b & 1), 2);
112                const double d = ggml_fp16_to_fp32(dh);
113                for (int v = 0; v < 32; ++v) {
114                    const uint8_t c = ch[16 * (2 * (b & 1) + (v >> 4)) + 4 * (v & 3) + ((v & 15) >> 2)];
115                    const double deq = d * (double) (int8_t) c;
116                    const double x = hout[(size_t) e * FF + 32 * b + v];
117                    sabs += std::fabs(deq - x); sref += std::fabs(x);
118                }
119            }
120        }
121        std::printf("hq dequantized vs the float H: rel_mean %.3e (q8_1's own resolution)\n", sabs / sref);
122    }
123
124    // ---- timing (informational) ------------------------------------------------------------------------------
125    {
126        cudaEvent_t t0, t1; CK(cudaEventCreate(&t0)); CK(cudaEventCreate(&t1));
127        strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, nullptr, nullptr);
128        CK(cudaDeviceSynchronize());
129        CK(cudaEventRecord(t0));
130        for (int i = 0; i < 100; ++i) strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, nullptr, nullptr);
131        CK(cudaEventRecord(t1)); CK(cudaEventSynchronize(t1));
132        float ms = 0; CK(cudaEventElapsedTime(&ms, t0, t1));
133        std::printf("M2b swiglu+q8_1: %.3f ms / 100 = %.4f ms  %.3f us/entry (%d entries)\n", ms, ms / 100.0,
134                    ms * 1000.0 / 100.0 / E, E);
135        CK(cudaEventDestroy(t0)); CK(cudaEventDestroy(t1));
136    }
137
138    std::printf("PASS: M2b (SwiGLU + q8_1 of H)\n");
139    return 0;
140}