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
3.9 KiB94 linesraw
1// prefill_swiglu_quant_test - the prompt path's fused MMQ expert step against the kernels it replaces, byte for byte:
2// mmq::swiglu_quant against swiglu + quantize (the down product's q8_1 rows of H), for down types the packs have
3// (Q2_0, IQ4_NL, Q8_0), with the gate/up rows split (GGUF) and interleaved (the Strata pack), a zero row and a row
4// with one huge value.  Exit 77 without a CUDA device.
5#include "strata/prefill/moe_mmq.hpp"
6
7#include "ggml.h"
8
9#include <cuda_runtime.h>
10
11#include <cstdint>
12#include <cstdio>
13#include <cstring>
14#include <random>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
19namespace {
20namespace mmq = strata::prefill::mmq;
21
22void ck(cudaError_t e, const char* what) {
23    if (e != cudaSuccess) throw std::runtime_error(std::string(what) + ": " + cudaGetErrorString(e));
24}
25
26struct Dev {
27    void* p = nullptr;
28    explicit Dev(size_t n) { ck(cudaMalloc(&p, n), "cudaMalloc"); }
29    ~Dev() { cudaFree(p); }
30    Dev(const Dev&) = delete;
31    Dev& operator=(const Dev&) = delete;
32    template <class T> T* as() const { return (T*) p; }
33};
34
35int swiglu_quant_case(ggml_type t, int64_t rows, bool interleaved, uint32_t seed) {
36    constexpr int64_t NFF = 640;
37    std::mt19937 rng(seed);
38    std::normal_distribution<float> nd(0.f, 1.f);
39    std::uniform_real_distribution<float> mag(0.05f, 40.f);
40    std::vector<float> gu((size_t) rows * 2 * NFF);
41    for (int64_t r = 0; r < rows; ++r) {
42        const float m = mag(rng);
43        for (int64_t k = 0; k < 2 * NFF; ++k) gu[(size_t) (r * 2 * NFF + k)] = nd(rng) * m;
44    }
45    if (rows > 3) {   // a zero row, a row with one huge value
46        std::memset(gu.data() + 2 * NFF, 0, 2 * NFF * sizeof(float));
47        gu[(size_t) (3 * 2 * NFF + 5)] = 3e4f;
48    }
49    const size_t qb = mmq::q8_bytes(rows, NFF);
50    Dev dgu(gu.size() * 4), dh((size_t) rows * NFF * 4 + 16), dq0(qb), dq1(qb);
51    ck(cudaMemcpy(dgu.p, gu.data(), gu.size() * 4, cudaMemcpyHostToDevice), "upload");
52    ck(cudaMemset(dq0.p, 0x5a, qb), "memset");
53    ck(cudaMemset(dq1.p, 0x5a, qb), "memset");
54    // the two kernels it replaces, as prefill.cpp launches them
55    mmq::swiglu(dgu.as<float>(), dh.as<float>(), rows, NFF, interleaved, nullptr);
56    mmq::quantize(dh.as<float>(), nullptr, dq0.p, (int) t, NFF, NFF, rows, nullptr);
57    mmq::swiglu_quant(dgu.as<float>(), dq1.p, rows, interleaved, nullptr);
58    ck(cudaDeviceSynchronize(), "run");
59    const size_t used = (size_t) rows * (1024 / 128) * 144;   // the rows' 8 blocks of 128 values (144 bytes each)
60    std::vector<uint8_t> a(used), b(used);
61    ck(cudaMemcpy(a.data(), dq0.p, used, cudaMemcpyDeviceToHost), "download");
62    ck(cudaMemcpy(b.data(), dq1.p, used, cudaMemcpyDeviceToHost), "download");
63    size_t bad = 0;
64    for (size_t i = 0; i < used; ++i) bad += a[i] != b[i];
65    std::printf("  swiglu_quant %-6s %5lld rows%s: %zu of %zu bytes differ\n", ggml_type_name(t), (long long) rows,
66                interleaved ? " interleaved" : "", bad, used);
67    return bad == 0 ? 0 : 1;
68}
69
70}  // namespace
71
72int main() {
73    int ndev = 0;
74    if (cudaGetDeviceCount(&ndev) != cudaSuccess || ndev == 0) {
75        std::printf("prefill_swiglu_quant_test: no CUDA device (skipped)\n");
76        return 77;
77    }
78    int fails = 0;
79    try {
80        for (ggml_type t : {GGML_TYPE_Q2_0, GGML_TYPE_IQ4_NL, GGML_TYPE_Q8_0}) {
81            if (!mmq::swiglu_quant_ok((int) t)) { std::printf("  %s: not on the fused path here\n", ggml_type_name(t)); continue; }
82            for (bool il : {false, true}) {
83                fails += swiglu_quant_case(t, 1, il, 1);
84                fails += swiglu_quant_case(t, 37, il, 2);
85                fails += swiglu_quant_case(t, 4113, il, 3);
86            }
87        }
88    } catch (const std::exception& e) {
89        std::fprintf(stderr, "prefill_swiglu_quant_test: %s\n", e.what());
90        return 1;
91    }
92    std::printf("prefill_swiglu_quant_test: %s\n", fails ? "FAILED" : "ok");
93    return fails ? 1 : 0;
94}