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
4.6 KiB101 linesraw
1// src/kernels/ple_iq4nl_dequant_test.cpp - STRATA_PLE_COMPACT: ple_iq4nl_dequant (raw IQ4_NL PLE rows to floats on the
2// device) against iq4nl_dequant_row (the host decode the prompt path used before), bit for bit.  Synthetic bytes, no model.
3//
4//   - every one of the 65,536 fp16 scale patterns (infinities and NaNs included: those are compared as "both NaN") in
5//     every block position, with random codes;
6//   - rows of random bytes;
7//   - the row the table gives for an index past its end (scale 0, code 8 in every nibble: +0.0f, what PleTable::read_row
8//     writes for such a row);
9//   - an odd row count (not a multiple of the 256-thread block).
10// Exit 77 without a CUDA device.
11#include "strata/kernels/ngram.hpp"
12#include "strata/kernels/ple.hpp"
13
14#include <cuda_runtime.h>
15
16#include <cmath>
17#include <cstdint>
18#include <cstdio>
19#include <cstring>
20#include <random>
21#include <vector>
22
23namespace {
24using namespace strata::kernels;
25
26int check(const std::vector<uint8_t>& raw, int64_t rows, const char* what) {
27    float* dout = nullptr;
28    uint8_t* draw = nullptr;
29    if (cudaMalloc((void**) &dout, (size_t) rows * PLE_HEAD_DIM * 4) != cudaSuccess ||
30        cudaMalloc((void**) &draw, raw.size()) != cudaSuccess ||
31        cudaMemcpy(draw, raw.data(), raw.size(), cudaMemcpyHostToDevice) != cudaSuccess) {
32        std::printf("CUDA setup failed for %s\n", what);
33        return 1;
34    }
35    cudaMemset(dout, 0xff, (size_t) rows * PLE_HEAD_DIM * 4);
36    ple_iq4nl_dequant(draw, rows, dout, nullptr);
37    if (cudaDeviceSynchronize() != cudaSuccess) { std::printf("CUDA error for %s: %s\n", what, cudaGetErrorString(cudaGetLastError())); return 1; }
38    std::vector<float> got((size_t) rows * PLE_HEAD_DIM), want(got.size());
39    cudaMemcpy(got.data(), dout, got.size() * 4, cudaMemcpyDeviceToHost);
40    cudaFree(dout);
41    cudaFree(draw);
42    for (int64_t r = 0; r < rows; ++r) iq4nl_dequant_row(raw.data() + (size_t) r * PLE_ROW_BYTES, want.data() + (size_t) r * PLE_HEAD_DIM);
43    long long diff = 0, first = -1;
44    for (size_t i = 0; i < got.size(); ++i) {
45        uint32_t a, b;
46        std::memcpy(&a, &got[i], 4);
47        std::memcpy(&b, &want[i], 4);
48        if (std::isnan(got[i]) && std::isnan(want[i])) continue;   // a NaN scale: any payload is a NaN
49        if (a != b) { if (first < 0) first = (long long) i; ++diff; }
50    }
51    std::printf("  %-52s %lld rows: %s\n", what, (long long) rows, diff ? "*** DIFFERS ***" : "bitwise equal");
52    if (diff) std::printf("      %lld values differ, first at %lld\n", diff, first);
53    return diff ? 1 : 0;
54}
55}  // namespace
56
57int main() {
58    int n_dev = 0;
59    if (cudaGetDeviceCount(&n_dev) != cudaSuccess || n_dev < 1) { std::printf("no CUDA device (skipped)\n"); return 77; }
60    int bad = 0;
61    std::mt19937 rng(20261026u);
62    {   // all fp16 patterns: 65,536 scales over 5 blocks a row = 13,108 rows (the last row pads with 0)
63        const int64_t rows = (65536 + 4) / 5;
64        std::vector<uint8_t> raw((size_t) rows * PLE_ROW_BYTES);
65        for (auto& b : raw) b = (uint8_t) (rng() & 0xff);
66        uint32_t d = 0;
67        for (int64_t r = 0; r < rows; ++r)
68            for (int b = 0; b < PLE_HEAD_DIM / 32; ++b, ++d) {
69                const uint16_t h = (uint16_t) (d & 0xffff);
70                std::memcpy(&raw[(size_t) r * PLE_ROW_BYTES + (size_t) b * 18], &h, 2);
71            }
72        bad += check(raw, rows, "every fp16 scale pattern, random codes");
73    }
74    {   // random rows
75        const int64_t rows = 4096 + 37;
76        std::vector<uint8_t> raw((size_t) rows * PLE_ROW_BYTES);
77        for (auto& b : raw) b = (uint8_t) (rng() & 0xff);
78        bad += check(raw, rows, "random bytes (odd row count)");
79    }
80    {   // the out-of-table row of PleTable::gather_raw_iq4nl
81        const int64_t rows = 16;
82        std::vector<uint8_t> raw((size_t) rows * PLE_ROW_BYTES);
83        for (int64_t r = 0; r < rows; ++r)
84            for (int b = 0; b < PLE_HEAD_DIM / 32; ++b) {
85                uint8_t* blk = &raw[(size_t) r * PLE_ROW_BYTES + (size_t) b * 18];
86                std::memset(blk, 0, 2);
87                std::memset(blk + 2, 0x88, 16);
88            }
89        bad += check(raw, rows, "out-of-table row (scale 0, code 8)");
90        // and it is +0.0f, not -0.0f, on both sides
91        std::vector<float> want(PLE_HEAD_DIM);
92        iq4nl_dequant_row(raw.data(), want.data());
93        for (float v : want) {
94            uint32_t a;
95            std::memcpy(&a, &v, 4);
96            if (a != 0) { std::printf("  *** the out-of-table row does not decode to +0.0f\n"); ++bad; break; }
97        }
98    }
99    std::printf("ple_iq4nl_dequant_test: %s\n", bad ? "FAILED" : "OK");
100    return bad ? 1 : 0;
101}