okami.mom/strata

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

Niko1221Strata: Qwen3.8-Flash-Next on a 12-24 GB NVIDIA GPU + 64 GB RAM, one-click install01529aaa8

main
6.6 KiB143 linesraw
1// src/kernels/dequant_s2_parity.cpp - P2.S2's parity test for the S2 decode.
2//
3// THE REFERENCE IS THE CHAIN, NOT A SECOND OPINION.  `strata::dequantize_q2_0` (include/strata/artifact/
4// dequant.hpp) is a scalar transcription that `bench/micro/dequant_xcheck` checks against **ggml's own**
5// dequantizer on real block bytes; and `tools/canonical_xcheck.py --full` checks that the canonical form
6// decodes to the same values on every block of the artifact.  So this compares "GPU canonical decode" against
7// "CPU decode already proven equal to ggml", and a fault in either the canonicaliser or the kernel shows up
8// here.  A parity test whose two sides were written together would show neither.
9#include "strata/artifact/dequant.hpp"
10#include "strata/kernels/dequant_s2.hpp"
11
12#include <cuda_runtime.h>
13
14#include <cstdint>
15#include <cstdio>
16#include <cstring>
17#include <random>
18#include <vector>
19
20namespace {
21
22constexpr int QK = 64;
23
24void check(cudaError_t e, const char* what) {
25    if (e != cudaSuccess) {
26        std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e));
27        std::exit(1);
28    }
29}
30
31// fp16 patterns that are finite, normal and exactly representable.  Random 16-bit patterns would include NaN
32// and infinity, and a NaN makes `!=` true for a reason that has nothing to do with the kernel - the test
33// would fail for the wrong cause, or worse, a real fault would hide behind a NaN it caused itself.
34const uint16_t kScales[] = {0x3C00, 0x3800, 0x3400, 0x3000, 0x2C00, 0x2800, 0x2000,
35                            0xBC00, 0xB800, 0xB400, 0xB000, 0xAC00, 0x1800, 0x1400};
36
37}  // namespace
38
39int main(int argc, char** argv) {
40    long long n_blocks = 200000;
41    bool selftest = false;
42    for (int i = 1; i < argc; ++i) {
43        if (std::strcmp(argv[i], "--selftest") == 0) selftest = true;
44        else if (std::strcmp(argv[i], "--blocks") == 0 && i + 1 < argc) n_blocks = std::atoll(argv[++i]);
45        else {
46            std::fprintf(stderr, "usage: dequant_s2_parity [--selftest] [--blocks N]\n");
47            return 2;
48        }
49    }
50    if (n_blocks <= 0) { std::fprintf(stderr, "--blocks must be positive\n"); return 2; }
51
52    std::mt19937 rng(12345);
53    const int n_scales = (int) (sizeof(kScales) / sizeof(kScales[0]));
54
55    // raw Q2_0 blocks: { fp16 d ; uint8 qs[16] } - what ggml produced and what the artifact stores
56    std::vector<uint8_t> raw((size_t) n_blocks * 18);
57    for (long long b = 0; b < n_blocks; ++b) {
58        const uint16_t d = kScales[rng() % n_scales];
59        raw[(size_t) b * 18 + 0] = (uint8_t) (d & 0xFF);
60        raw[(size_t) b * 18 + 1] = (uint8_t) (d >> 8);
61        for (int t = 0; t < 16; ++t) raw[(size_t) b * 18 + 2 + t] = (uint8_t) (rng() & 0xFF);
62    }
63
64    // the CPU reference, over the RAW blocks
65    std::vector<float> cpu((size_t) n_blocks * QK);
66    for (long long b = 0; b < n_blocks; ++b) {
67        strata::dequantize_q2_0(&raw[(size_t) b * 18], &cpu[(size_t) b * QK]);
68    }
69
70    // canonical planes: S2 packs 4 codes per byte exactly as Q2_0 stores them, so the codes plane is the raw
71    // qs bytes; the scales plane is the fp16 `d` widened.  Copied into planes of their own rather than
72    // aliased, because that is what the kernel will read in the engine.
73    std::vector<uint8_t> codes((size_t) n_blocks * 16);
74    std::vector<float> scales((size_t) n_blocks);
75    for (long long b = 0; b < n_blocks; ++b) {
76        std::memcpy(&codes[(size_t) b * 16], &raw[(size_t) b * 18 + 2], 16);
77        const uint16_t d = (uint16_t) (raw[(size_t) b * 18] | (raw[(size_t) b * 18 + 1] << 8));
78        scales[(size_t) b] = strata::fp16_to_fp32(d);
79    }
80
81    uint8_t* d_codes = nullptr;
82    float *d_scales = nullptr, *d_out = nullptr;
83    check(cudaMalloc(&d_codes, codes.size()), "cudaMalloc codes");
84    check(cudaMalloc(&d_scales, scales.size() * sizeof(float)), "cudaMalloc scales");
85    check(cudaMalloc(&d_out, cpu.size() * sizeof(float)), "cudaMalloc out");
86    check(cudaMemcpy(d_codes, codes.data(), codes.size(), cudaMemcpyHostToDevice), "copy codes");
87    check(cudaMemcpy(d_scales, scales.data(), scales.size() * sizeof(float), cudaMemcpyHostToDevice),
88          "copy scales");
89
90    strata::kernels::dequant_s2(d_codes, d_scales, d_out, n_blocks);
91
92    std::vector<float> gpu(cpu.size());
93    check(cudaMemcpy(gpu.data(), d_out, gpu.size() * sizeof(float), cudaMemcpyDeviceToHost), "copy back");
94
95    long long bad = 0, first_bad = -1;
96    for (size_t i = 0; i < cpu.size(); ++i) {
97        // bit comparison, not a tolerance: S2's decode is exact integer-to-float arithmetic, so ANY
98        // difference is a bug and a tolerance would only hide which one
99        if (std::memcmp(&cpu[i], &gpu[i], sizeof(float)) != 0) {
100            if (first_bad < 0) first_bad = (long long) i;
101            ++bad;
102        }
103    }
104
105    // NON-VACUITY.  Two buffers of zeros compare equal, and so do two buffers a broken test filled with the
106    // same constant - an agreement check that cannot distinguish "both correct" from "both empty" is the
107    // failure this project keeps finding.  So the data must actually vary, and the four codes must all occur:
108    // an S2 decode that only ever saw one code would agree while being unable to tell -1 from +2.
109    bool seen[4] = {false, false, false, false};
110    for (size_t i = 0; i < codes.size(); ++i) {
111        for (int t = 0; t < 4; ++t) seen[(codes[i] >> (2 * t)) & 3] = true;
112    }
113    int distinct = 0;
114    for (bool s : seen) distinct += s ? 1 : 0;
115    long long distinct_vals = 0;
116    for (size_t i = 0; i < cpu.size() && distinct_vals < 8; ++i) {
117        bool dup = false;
118        for (size_t j = 0; j < i && !dup; ++j) dup = (cpu[j] == cpu[i]);
119        if (!dup) ++distinct_vals;
120    }
121    std::printf("  non-vacuity: %d of 4 codes present, %lld distinct values in the first elements\n", distinct,
122                distinct_vals);
123    if (distinct != 4 || distinct_vals < 4) {
124        std::fprintf(stderr, "VACUOUS: the comparison could not have distinguished a wrong decode\n");
125        return 1;
126    }
127
128    std::printf("dequant_s2: %lld blocks, %lld elements, %lld mismatched\n", n_blocks, (long long) cpu.size(),
129                bad);
130    if (bad) {
131        std::printf("  first mismatch at element %lld (block %lld, offset %d): cpu %.9g gpu %.9g\n",
132                    (long long) first_bad, (long long) (first_bad / QK), (int) (first_bad % QK),
133                    (double) cpu[(size_t) first_bad], (double) gpu[(size_t) first_bad]);
134        return 1;
135    }
136    std::printf("  bit-exact against the scalar dequantizer that dequant_xcheck proved equal to ggml\n");
137    if (selftest) std::printf("dequant_s2_parity OK\n");
138
139    cudaFree(d_codes);
140    cudaFree(d_scales);
141    cudaFree(d_out);
142    return 0;
143}