okami.mom/strata

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

Niko1221Merge branch 'f-132-pr' into rel-0.1.32b0abdeb44

main
2.8 KiB62 linesraw
1// Q5_1 shared-expert projection: Strata CUDA dequantization vs ggml.
2#define NOMINMAX
3#include "strata/artifact/gguf_reader.hpp"
4#include "strata/kernels/dequant_bf16.hpp"
5#include "ggml.h"
6
7#include <cuda_runtime.h>
8#include <algorithm>
9#include <cmath>
10#include <cstdio>
11#include <cstdint>
12#include <cstring>
13#include <vector>
14
15int main(int argc, char** argv) {
16    if (argc != 2) {
17        std::fprintf(stderr, "usage: q5_projection_parity <shard1.gguf>\n");
18        return 2;
19    }
20    strata::GgufFile gguf(argv[1]);
21    const auto* t = gguf.find("blk.0.ffn_down_shexp.weight");
22    if (!t || t->type != 7 || t->shape.size() != 2) {
23        std::fprintf(stderr, "expected blk.0.ffn_down_shexp.weight Q5_1\n");
24        return 2;
25    }
26    const int64_t cols = (int64_t) t->shape[0], rows = std::min<int64_t>(4, (int64_t) t->shape[1]);
27    const size_t weight_bytes = (size_t) rows * (size_t) (cols / 32) * 24;
28    const size_t elements = (size_t) rows * (size_t) cols;
29    const uint8_t* source = gguf.tensor_data(*t);
30    void *dweight = nullptr, *dfloat = nullptr, *dbf16 = nullptr;
31    if (cudaMalloc(&dweight, weight_bytes) != cudaSuccess ||
32        cudaMalloc(&dfloat, elements * sizeof(float)) != cudaSuccess ||
33        cudaMalloc(&dbf16, elements * sizeof(uint16_t)) != cudaSuccess) {
34        std::fprintf(stderr, "CUDA allocation failed\n");
35        return 1;
36    }
37    cudaMemcpy(dweight, source, weight_bytes, cudaMemcpyHostToDevice);
38    strata::kernels::dequant_f32(7, dweight, 0, rows, cols, (float*) dfloat, nullptr);
39    strata::kernels::dequant_bf16(7, dweight, 0, rows, cols, (uint16_t*) dbf16, nullptr);
40    std::vector<float> got(elements), ref(elements);
41    std::vector<uint16_t> got_bf16(elements);
42    cudaMemcpy(got.data(), dfloat, elements * sizeof(float), cudaMemcpyDeviceToHost);
43    cudaMemcpy(got_bf16.data(), dbf16, elements * sizeof(uint16_t), cudaMemcpyDeviceToHost);
44    cudaFree(dweight); cudaFree(dfloat); cudaFree(dbf16);
45    const auto* traits = ggml_get_type_traits(GGML_TYPE_Q5_1);
46    for (int64_t r = 0; r < rows; ++r)
47        traits->to_float(source + (size_t) r * (size_t) (cols / 32) * 24,
48                         ref.data() + (size_t) r * (size_t) cols, cols);
49    double max_abs = 0;
50    size_t bf16_bad = 0;
51    for (size_t i = 0; i < elements; ++i) {
52        max_abs = std::max(max_abs, (double) std::fabs(got[i] - ref[i]));
53        uint32_t bits;
54        std::memcpy(&bits, &ref[i], sizeof(bits));
55        const uint16_t expected = (uint16_t) ((bits + 0x7fffu + ((bits >> 16) & 1u)) >> 16);
56        if (got_bf16[i] != expected) ++bf16_bad;
57    }
58    const bool ok = max_abs <= 1e-6 && bf16_bad == 0;
59    std::printf("Q5_1 projection: %zu elements, max_abs %.3e, bf16 differences %zu %s\n",
60                elements, max_abs, bf16_bad, ok ? "PASS" : "FAIL");
61    return ok ? 0 : 1;
62}