okami.mom/strata

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

konijiwa110qsa top-k: Turing prefill above ~90K cells takes the wide kernel (from #743; STRATA_TOPK_STREAM=0 restores)1bb5c7248

main
4.4 KiB97 linesraw
1// Capacity 262144, live prefill up to the register boundary, and the legacy fallbacks.
2// Compare selected IDs only: unused output padding is not part of the API contract.
3#include "strata/kernels/qsa.hpp"
4#include "strata/kernels/qsa_select.hpp"
5#include <cuda_runtime.h>
6#include <algorithm>
7#include <cstdio>
8#include <cstdlib>
9#include <random>
10#include <vector>
11
12namespace k = strata::kernels;
13namespace {
14void ck(cudaError_t e) {
15    if (e != cudaSuccess) {
16        std::fprintf(stderr, "CUDA: %s\n", cudaGetErrorString(e));
17        std::exit(2);
18    }
19}
20struct Case { int64_t ctx, queries; int mode; };
21bool check(Case c) {
22    constexpr int64_t stride = 262144 / 4 + 2;
23    const auto s = k::qsa_real_shapes();
24    const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s);
25    std::vector<int32_t> steps((size_t)(c.queries * k::kStepCount));
26    std::vector<float> scores((size_t)(c.queries * stride), -999.f);
27    std::mt19937 rng(742913);
28    std::uniform_real_distribution<float> random(0.f, 200.f);
29    for (int64_t i = 0; i < c.queries; ++i) {
30        auto* st = steps.data() + i * k::kStepCount;
31        const int64_t pos = c.ctx - c.queries + i;
32        st[k::kStepPos] = (int32_t)pos;
33        st[k::kStepNKv] = (int32_t)(pos + 1);
34        st[k::kStepNBid] = (int32_t)((pos + 1) / s.idx_block);
35        st[k::kStepWidth] = (int32_t)k::qsa_selection_width(pos + 1, s);
36        for (int64_t j = 0; j <= st[k::kStepNBid]; ++j)
37            scores[(size_t)(i * stride + j)] = c.mode == 1 ? 0.f : c.mode == 2 ? float(j % 7) : random(rng);
38        if ((pos + 1) % s.idx_block)
39            scores[(size_t)(i * stride + st[k::kStepNBid])] += 1e9f;
40    }
41    int64_t active = steps[(size_t)((c.queries - 1) * k::kStepCount + k::kStepNBid)] + 1;
42    if (c.mode == 3) active = 0;
43    if (c.mode == 4) active = stride + 1;
44    float* ds = nullptr;
45    int32_t *dt = nullptr, *da = nullptr, *db = nullptr;
46    ck(cudaMalloc(&ds, scores.size() * sizeof(float)));
47    ck(cudaMalloc(&dt, steps.size() * sizeof(int32_t)));
48    ck(cudaMalloc(&da, (size_t)(c.queries * cap) * sizeof(int32_t)));
49    ck(cudaMalloc(&db, (size_t)(c.queries * cap) * sizeof(int32_t)));
50    ck(cudaMemcpy(ds, scores.data(), scores.size() * sizeof(float), cudaMemcpyHostToDevice));
51    ck(cudaMemcpy(dt, steps.data(), steps.size() * sizeof(int32_t), cudaMemcpyHostToDevice));
52    // No bound: the exact capacity-based dispatch used by captured decode.
53    k::qsa_block_topk(ds, dt, c.queries, stride, cap, s, da, nullptr);
54    k::qsa_block_topk(ds, dt, c.queries, stride, cap, s, db, nullptr, active);
55    ck(cudaDeviceSynchronize());
56    std::vector<int32_t> a((size_t)(c.queries * cap)), b(a.size());
57    ck(cudaMemcpy(a.data(), da, a.size() * sizeof(int32_t), cudaMemcpyDeviceToHost));
58    ck(cudaMemcpy(b.data(), db, b.size() * sizeof(int32_t), cudaMemcpyDeviceToHost));
59    bool ok = true;
60    for (int64_t i = 0; i < c.queries; ++i) {
61        const int64_t w = steps[(size_t)(i * k::kStepCount + k::kStepWidth)];
62        if (!std::equal(a.begin() + i * cap, a.begin() + i * cap + w, b.begin() + i * cap)) {
63            std::fprintf(stderr, "FAIL ctx=%lld queries=%lld mode=%d row=%lld\n",
64                         (long long)c.ctx, (long long)c.queries, c.mode, (long long)i);
65            ok = false;
66            break;
67        }
68    }
69    ck(cudaFree(ds)); ck(cudaFree(dt)); ck(cudaFree(da)); ck(cudaFree(db));
70    return ok;
71}
72}
73
74int main() {
75    int devices = 0;
76    if (cudaGetDeviceCount(&devices) != cudaSuccess || devices == 0) {
77        std::puts("SKIP: no CUDA device");
78        return 77;
79    }
80    // the active-bound dispatch is Turing's in the engine; forced here so every CUDA card checks it
81#if defined(_WIN32)
82    _putenv_s("STRATA_TOPK_ACTIVE_ANY", "1");
83#else
84    setenv("STRATA_TOPK_ACTIVE_ANY", "1", 1);
85#endif
86    const Case cases[] = {
87        {1111, 256, 0}, {8192, 256, 0}, {32768, 256, 0}, {131072, 256, 0},
88        {131075, 255, 0}, {135164, 256, 0}, {135168, 256, 0}, {250022, 256, 0},
89        {262144, 256, 0}, {131072, 1, 0}, {131072, 8, 0}, {131072, 257, 0},
90        {131072, 256, 1}, {131072, 256, 2}, {131072, 256, 3}, {131072, 256, 4},
91        // either side of the Turing wide-kernel bound (22,528 blocks), and ties above the register limit
92        {90108, 256, 0}, {90112, 256, 0}, {200000, 256, 2}, {262144, 256, 1},
93    };
94    for (const auto c : cases) if (!check(c)) return 1;
95    std::puts("PASS: 20 top-k active-bound cases, selected IDs bitwise identical");
96    return 0;
97}