okami.mom/strata

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

Niko1221Merge f-026-qwen into f-0243-rel94a4a34b0

main
3.8 KiB76 linesraw
1// iq512_gather_parity: the 512-bit i-quant rows (iq_avx512.cpp) against ggml-cpu's own dot.
2//  (a) iq512x_rows, for 1..4 tokens, is ggml-cpu's single-token vec_dot bit for bit (IQ3_XXS, IQ3_S, IQ2_S), whatever the
3//      grid lookup is (scalar loads or gathers: STRATA_IQ_GATHER=0 / 1, ctest runs both);
4//  (b) iq512_rows (the engine's multi-token rows) agrees with it to float rounding (another summation order).
5// Synthetic blocks (random bytes, a fixed f16 scale), so no quantizer and no imatrix are needed.  SKIP without AVX-512.
6#include "strata/kernels/cpu/expert_layout.hpp"
7#include "strata/kernels/cpu/iq_avx512.hpp"
8
9#include "ggml.h"
10#include "ggml-cpu.h"
11
12#include <cmath>
13#include <cstdio>
14#include <cstring>
15#include <vector>
16
17namespace cpu = strata::kernels::cpu;
18
19static int g_fail = 0;
20#define CHECK(c)                                                              \
21    do {                                                                      \
22        if (!(c)) { std::fprintf(stderr, "FAIL %s:%d  %s\n", __FILE__, __LINE__, #c); ++g_fail; } \
23    } while (0)
24
25int main() {
26    ggml_cpu_init();
27    if (!cpu::cpu_avx512_ok()) {
28        std::printf("iq512_gather_parity: SKIP (no AVX-512)\n");
29        return 0;
30    }
31    std::printf("iq512_gather_parity: gather %d\n", (int) cpu::iq512_gather_on());
32    constexpr int n = 2560, rows = 96, ntmax = 4;
33    const ggml_type_traits_cpu* t8 = ggml_get_type_traits_cpu(GGML_TYPE_Q8_K);
34    uint32_t seed = 987654321u;
35    auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
36    const size_t ab = ggml_row_size(GGML_TYPE_Q8_K, n);
37    std::vector<std::vector<uint8_t>> act(ntmax, std::vector<uint8_t>(ab));
38    const void* ap[ntmax];
39    for (int t = 0; t < ntmax; ++t) {
40        std::vector<float> x(n);
41        for (auto& v : x) v = ((float) (rnd() % 2001) - 1000.f) * 0.001f * (1.f + (float) t);
42        t8->from_float(x.data(), act[t].data(), n);
43        ap[t] = act[t].data();
44    }
45    for (int type : {18, 21, 22}) {
46        const ggml_type_traits_cpu* tr = ggml_get_type_traits_cpu((ggml_type) type);
47        CHECK(tr != nullptr && tr->vec_dot != nullptr);
48        CHECK(cpu::iq512x_supported(type) && cpu::iq512_supported(type));
49        const size_t rb = ggml_row_size((ggml_type) type, n), nb = (size_t) n / 256, bb = rb / nb;
50        std::vector<uint8_t> w((size_t) rows * rb);
51        for (auto& b : w) b = (uint8_t) rnd();
52        for (int r = 0; r < rows; ++r)
53            for (size_t k = 0; k < nb; ++k) { uint8_t* d = &w[(size_t) r * rb + k * bb]; d[0] = 0x66; d[1] = 0x2e; }   // f16 ~0.1
54        std::vector<float> want((size_t) ntmax * rows), gx((size_t) ntmax * rows), ge((size_t) ntmax * rows);
55        for (int t = 0; t < ntmax; ++t)
56            for (int r = 0; r < rows; ++r)
57                tr->vec_dot(n, &want[(size_t) t * rows + r], 0, w.data() + (size_t) r * rb, 0, act[t].data(), 0, 1);
58        for (int nt = 1; nt <= ntmax; ++nt) {
59            float* ox[ntmax];
60            float* oe[ntmax];
61            for (int t = 0; t < ntmax; ++t) { ox[t] = &gx[(size_t) t * rows]; oe[t] = &ge[(size_t) t * rows]; }
62            cpu::iq512x_rows(type, w.data(), rb, n, ap, nt, ox, 0, rows);
63            cpu::iq512_rows(type, w.data(), rb, n, ap, nt, oe, 0, rows);
64            for (int t = 0; t < nt; ++t)
65                for (int r = 0; r < rows; ++r) {
66                    const float want_v = want[(size_t) t * rows + r];
67                    CHECK(std::memcmp(&gx[(size_t) t * rows + r], &want_v, 4) == 0);   // (a) bit for bit
68                    const float e = ge[(size_t) t * rows + r];
69                    CHECK(std::fabs(e - want_v) <= 1e-3f * std::fabs(want_v) + 1e-3f);   // (b) float-rounding agreement
70                }
71        }
72        std::printf("  type %d: ok\n", type);
73    }
74    if (g_fail == 0) std::printf("iq512_gather_parity: OK\n");
75    return g_fail == 0 ? 0 : 1;
76}