On CUDA the register kernel holds 33,792 blocks (~135K cells). Past it qsa_block_topk took the original kernel: 256 threads, each reading its own stretch of the scores on all six passes, one shared histogram updated with atomics. The dispatch follows the capacity (--max-context), so with a context past ~135K that was every prompt batch and every decode window on a card without the sm_75 active-bound dispatch (#512), whatever the prompt's length, and on sm_75 every prompt batch past 33,792 blocks and every decode window. block_topk_wide_kernel is the register kernel's layout (1,024 threads, a histogram per warp, warp scans) with the keys read from memory: - the four radix passes: thread t reads blocks t, t + 1024, ... - a histogram has no order, so a warp reads 32 neighbours at a time; - the count and the output: warp w holds `per` rows of 32 consecutive blocks, and the cells are written in the order (warp, row, lane); a row without a selected block is skipped. The selection rule is the original kernel's (radix threshold, ties to the lowest index, cells ascending): identical ids. One launch per call, no scratch memory, no capacity limit. Dispatch (CUDA; HIP is unchanged and does not compile the kernel): where the blocks do not fit the registers the new kernel, except a call that names fewer than 4,608 blocks (a prompt batch below ~18K cells), which keeps the original kernel: its fixed cost is lower there. A decode window (no block count) takes the new kernel at any context. The register kernel's range, the sm_75 active-bound dispatch and the sm_90+ cluster path are unchanged. STRATA_TOPK_OLD=1 keeps the original kernel, as before. qsa_topk_parity (new, with the kernel parity tests): the dispatched top-k against qsa_block_topk_ref on both sides of the register kernel's reach, at short contexts under a long capacity and up to 524,288 cells, called as decode and as the prompt path call it, with continuous scores, 64 score levels and equal scores. RTX 3060 12 GB (sm_86), 256 queries with their block count under a 262,144-cell capacity (524,288 for the last two), original -> dispatched: 16K cells 0.199 -> 0.199 ms (the original kernel) 32K 0.845 -> 0.328 131K 4.70 -> 0.97 262K 16.09 -> 1.71 393K 26.02 -> 2.49 524K 37.53 -> 3.24 4 queries without a count (a decode window): 0.031 -> 0.028 ms at 8K, 0.176 -> 0.083 at 131K, 0.309 -> 0.127 at 262K. IQ3_XXS on that card (--resident-experts, --max-context 262144, --kv int8 --kv-resident 32768), tools/needle_bench.py, STRATA_TOPK_OLD=1 against the default: tok/s of the prompt, and the prompt path's selection phase (STRATA_PREFILL_TIMING): 16,471 tokens 1004.5, 1016.2 -> 989.0, 1013.6 195 -> 195 ms 31,970 1043.3, 1055.5 -> 1060.1, 1060.5 900 -> 731 61,333 932.9 -> 1013.8 4,061 -> 2,428 122,755 904.7, 904.2 -> 992.7, 989.6 20,019 -> 9,034 185,729 819.7 -> 954.7 50,055 -> 20,761 243,176 743.3 -> 933.7 96,326 -> 35,544 The needles are found in every run.
140 lines
7.6 KiB
C++
140 lines
7.6 KiB
C++
// src/kernels/qsa_topk_parity.cpp - the QSA top-k the engine dispatches (qsa_block_topk) against the original kernel
|
|
// (qsa_block_topk_ref): the selected cells must be identical (GPU, synthetic, no model).
|
|
//
|
|
// A case is a context under a capacity (the engine's --max-context; max_blocks = capacity / 4 + 2, and without an
|
|
// active-block count the dispatch follows it), with consecutive queries at the context's end so that the tail block
|
|
// holds 0 to 3 cells. Three score sets per case: continuous, 64 levels (many ties at the threshold) and all equal
|
|
// (the rule's ties go to the lowest index). The dispatch is called as a decode window calls it (no block count) and as
|
|
// the prompt path does (with the call's largest block count). The reference and the dispatched kernel are also timed.
|
|
//
|
|
// Usage: qsa_topk_parity --selftest
|
|
// qsa_topk_parity CONTEXT [QUERIES=256] [REPS=10] [CAPACITY=CONTEXT] [COUNT=1] (COUNT=0: no block count)
|
|
#include "strata/kernels/qsa.hpp"
|
|
#include "strata/kernels/qsa_select.hpp"
|
|
|
|
#include <cuda_runtime.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
namespace k = strata::kernels;
|
|
|
|
namespace {
|
|
void ck(cudaError_t e, const char* w) {
|
|
if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e)); std::exit(2); }
|
|
}
|
|
|
|
bool run_case(int64_t ctx, int64_t nq, int reps, int64_t capacity, bool counted) {
|
|
const k::QsaShapes s = k::qsa_real_shapes();
|
|
const int64_t max_blocks = capacity / s.idx_block + 2, cap = k::qsa_selection_width(k::kTopkMaxCells, s);
|
|
std::vector<int32_t> steps((size_t) (nq * k::kStepCount));
|
|
for (int64_t i = 0; i < nq; ++i) {
|
|
int32_t* st = steps.data() + i * k::kStepCount;
|
|
const int64_t pos = ctx - nq + i;
|
|
st[k::kStepPos] = (int32_t) pos;
|
|
st[k::kStepNKv] = (int32_t) (pos + 1);
|
|
st[k::kStepNBid] = (int32_t) ((pos + 1) / s.idx_block);
|
|
st[k::kStepWidth] = (int32_t) k::qsa_selection_width(pos + 1, s);
|
|
}
|
|
std::mt19937 rng((uint32_t) (ctx * 31 + capacity));
|
|
std::normal_distribution<float> nd(0.f, 1.f);
|
|
std::vector<float> scores((size_t) (nq * max_blocks)), tied(scores.size()), equal(scores.size(), 0.25f);
|
|
for (int64_t i = 0; i < nq; ++i) {
|
|
const int32_t* st = steps.data() + i * k::kStepCount;
|
|
float* row = scores.data() + i * max_blocks;
|
|
for (int64_t b = 0; b < max_blocks; ++b) row[b] = nd(rng);
|
|
// the tail block as the scorers leave it: far above the others when it holds cells
|
|
if (st[k::kStepNKv] % s.idx_block != 0) row[st[k::kStepNBid]] += 1e9f;
|
|
for (int64_t b = 0; b < max_blocks; ++b)
|
|
tied[(size_t) (i * max_blocks + b)] = row[b] > 1e8f ? row[b] : std::floor(row[b] * 8.0f) / 8.0f; // ~64 levels
|
|
}
|
|
int32_t *d_steps = nullptr, *ids_ref = nullptr, *ids_new = nullptr;
|
|
float* d_scores = nullptr;
|
|
ck(cudaMalloc(&d_steps, steps.size() * 4), "malloc");
|
|
ck(cudaMalloc(&d_scores, scores.size() * 4), "malloc");
|
|
ck(cudaMalloc(&ids_ref, (size_t) (nq * cap) * 4), "malloc");
|
|
ck(cudaMalloc(&ids_new, (size_t) (nq * cap) * 4), "malloc");
|
|
ck(cudaMemcpy(d_steps, steps.data(), steps.size() * 4, cudaMemcpyHostToDevice), "upload");
|
|
const int64_t active = counted ? steps[(size_t) ((nq - 1) * k::kStepCount + k::kStepNBid)] + 1 : -1;
|
|
std::vector<int32_t> a((size_t) (nq * cap)), b(a.size());
|
|
auto same = [&](const std::vector<float>& sc) -> int64_t {
|
|
ck(cudaMemcpy(d_scores, sc.data(), sc.size() * 4, cudaMemcpyHostToDevice), "upload");
|
|
ck(cudaMemset(ids_ref, 0xff, a.size() * 4), "memset");
|
|
ck(cudaMemset(ids_new, 0xff, a.size() * 4), "memset");
|
|
k::qsa_block_topk_ref(d_scores, d_steps, nq, max_blocks, cap, s, ids_ref, nullptr);
|
|
k::qsa_block_topk(d_scores, d_steps, nq, max_blocks, cap, s, ids_new, nullptr, active);
|
|
ck(cudaDeviceSynchronize(), "sync");
|
|
ck(cudaMemcpy(a.data(), ids_ref, a.size() * 4, cudaMemcpyDeviceToHost), "down");
|
|
ck(cudaMemcpy(b.data(), ids_new, b.size() * 4, cudaMemcpyDeviceToHost), "down");
|
|
int64_t n = 0;
|
|
for (int64_t i = 0; i < nq; ++i) {
|
|
const int64_t w = steps[(size_t) (i * k::kStepCount + k::kStepWidth)];
|
|
n += std::equal(a.begin() + i * cap, a.begin() + i * cap + w, b.begin() + i * cap);
|
|
}
|
|
return n;
|
|
};
|
|
const int64_t same_tied = same(tied), same_equal = same(equal), same_cont = same(scores); // d_scores: continuous
|
|
cudaEvent_t e0, e1;
|
|
ck(cudaEventCreate(&e0), "event");
|
|
ck(cudaEventCreate(&e1), "event");
|
|
auto timed = [&](auto f) {
|
|
ck(cudaEventRecord(e0), "record");
|
|
for (int r = 0; r < reps; ++r) f();
|
|
ck(cudaEventRecord(e1), "record");
|
|
ck(cudaEventSynchronize(e1), "time");
|
|
float ms = 0;
|
|
ck(cudaEventElapsedTime(&ms, e0, e1), "elapsed");
|
|
return ms / (float) reps;
|
|
};
|
|
const float t_ref = timed([&] { k::qsa_block_topk_ref(d_scores, d_steps, nq, max_blocks, cap, s, ids_ref, nullptr); });
|
|
const float t_new = timed([&] { k::qsa_block_topk(d_scores, d_steps, nq, max_blocks, cap, s, ids_new, nullptr, active); });
|
|
cudaEventDestroy(e0); cudaEventDestroy(e1);
|
|
cudaFree(d_steps); cudaFree(d_scores); cudaFree(ids_ref); cudaFree(ids_new);
|
|
const bool ok = same_cont == nq && same_tied == nq && same_equal == nq;
|
|
std::printf("%s context %lld under a capacity of %lld (%lld blocks), %lld queries %s: reference %.3f ms, "
|
|
"dispatched %.3f ms (%.1fx); identical ids: continuous %lld/%lld, 64 levels %lld/%lld, equal scores "
|
|
"%lld/%lld\n", ok ? "PASS" : "FAIL", (long long) ctx, (long long) capacity, (long long) max_blocks,
|
|
(long long) nq, counted ? "with their block count" : "without a block count", t_ref, t_new, t_ref / t_new,
|
|
(long long) same_cont, (long long) nq, (long long) same_tied,
|
|
(long long) nq, (long long) same_equal, (long long) nq);
|
|
return ok;
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc > 1 && std::strcmp(argv[1], "--selftest") == 0) {
|
|
// {context, capacity}: both sides of the register kernel's reach (33,792 blocks = 135,168 cells), short
|
|
// contexts under a long capacity (fewer blocks than the kernel's threads; a prompt batch below the dispatch's
|
|
// 4,608 blocks), and the long contexts themselves
|
|
const int64_t cases[][2] = {{9000, 9000}, {135160, 135160}, {135176, 135176}, {2100, 262144},
|
|
{9000, 262144}, {32768, 262144}, {262144, 262144}, {300001, 524288},
|
|
{524288, 524288}};
|
|
bool ok = true;
|
|
for (const auto& c : cases) {
|
|
ok = run_case(c[0], 8, 3, c[1], false) && ok; // a decode window's call
|
|
ok = run_case(c[0], 64, 3, c[1], true) && ok; // the prompt path's
|
|
}
|
|
std::printf("%s\n", ok ? "qsa_topk_parity: PASS" : "qsa_topk_parity: FAIL");
|
|
return ok ? 0 : 1;
|
|
}
|
|
if (argc < 2) {
|
|
std::fprintf(stderr, "usage: qsa_topk_parity --selftest | CONTEXT [QUERIES=256] [REPS=10] [CAPACITY=CONTEXT] "
|
|
"[COUNT=1]\n");
|
|
return 2;
|
|
}
|
|
const int64_t ctx = std::atoll(argv[1]);
|
|
const int64_t nq = argc > 2 ? std::atoll(argv[2]) : 256;
|
|
const int reps = argc > 3 ? std::atoi(argv[3]) : 10;
|
|
const int64_t capacity = argc > 4 ? std::atoll(argv[4]) : ctx;
|
|
if (ctx < nq || nq <= 0 || reps <= 0 || capacity < ctx) {
|
|
std::fprintf(stderr, "qsa_topk_parity: CONTEXT >= QUERIES > 0, REPS > 0, CAPACITY >= CONTEXT\n");
|
|
return 2;
|
|
}
|
|
return run_case(ctx, nq, reps, capacity, argc <= 5 || std::atoi(argv[5]) != 0) ? 0 : 1;
|
|
}
|