mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 15:58:53 +00:00
Engine: the one-read top-k doubles its sampling stride past 524,288 cells instead of failing every query
block_topk_sample_kernel (#2093) reads every 32nd block's key to bound the top-k, then reads the row once. Its sample buffer holds TS_CAP = 4,096 keys, so a row of more than 131,072 blocks (524,288 cells) failed at `ns > TS_CAP` before reading anything, and block_topk_wide_kernel<true> re-read the whole row six times. On a 1M-token prompt that was the second half of every selection batch. The stride now doubles while the samples would not fit (64 up to 1M cells, 128 up to 2M); the candidate budget, the threshold and the emit are the same, so the ids stay the reference's. RTX 4090 (sm_89), CUDA 13.0, qsa_select_bench 1000000 256 10 with STRATA_TOPK_ACTIVE_ANY=1 STRATA_TOPK_SAMPLE=1: top-k 1.60 -> 0.52 ms per 256-query batch at 250,001 blocks, ids identical to the reference radix kernel; at 524,288 cells 2.90 -> 0.30 ms. In the engine (976K-token prompt, STRATA_PREFILL_TIMING=1) the selection phase lost 14 s of 61 s with the switches on. Off Turing nothing changes without STRATA_TOPK_SAMPLE=1. qsa_topk_active_parity: the score-row stride follows the case's context, eight cases either side of the stride doubling and at 1M cells (random scores, ties, 257 queries, a batch of one); qsa_topk_active_parity_sample runs every case with STRATA_TOPK_SAMPLE=1 on any CUDA card.
This commit is contained in:
1 parent
ffa80d7313
commit
3ea40c9c4d
3 files changed
+21
-6
No files matched your search
@@ -583,6 +583,10 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
|
||||
target_link_libraries(qsa_topk_active_parity PRIVATE strata_kernels CUDA::cudart)
|
||||
add_test(NAME qsa_topk_active_parity COMMAND qsa_topk_active_parity)
|
||||
set_tests_properties(qsa_topk_active_parity PROPERTIES SKIP_RETURN_CODE 77)
|
||||
# the same cases through the one-read top-k (#2093, STRATA_TOPK_SAMPLE=1, Turing's default) on any CUDA card: the
|
||||
# same ids, including the rows past 524,288 cells where its sampling stride doubles
|
||||
add_test(NAME qsa_topk_active_parity_sample COMMAND qsa_topk_active_parity)
|
||||
set_tests_properties(qsa_topk_active_parity_sample PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "STRATA_TOPK_SAMPLE=1")
|
||||
# STRATA_QSA_SLOTS: a batch window's scores, top-k and attention as one launch over rows of different slots
|
||||
# against one call per row, bitwise (synthetic, no model)
|
||||
add_executable(qsa_slots_parity src/kernels/qsa_slots_parity.cpp)
|
||||
|
||||
@@ -2703,6 +2703,10 @@ bool qsa_block_topk_split(const float* scores, const int32_t* steps, int64_t nq,
|
||||
// order. Otherwise (too few cells above LB, or more than SM_CAP candidates) the query is flagged and
|
||||
// block_topk_wide_kernel<true> runs it. STRATA_TOPK_SAMPLE=0 keeps the wide kernel alone.
|
||||
constexpr int TS_STRIDE = 32, TS_CAP = 4096;
|
||||
// Past TS_CAP * TS_STRIDE blocks (524,288 cells) the sampling stride doubles (64 up to 1M cells, 128 up to 2M) instead of
|
||||
// failing every query at `ns > TS_CAP`, which handed the whole second half of a 1M prompt to the six-read wide kernel.
|
||||
// The candidate budget, the threshold and the emit are unchanged, so the ids stay the reference's.
|
||||
constexpr int TS_STRIDE_MAX = 128;
|
||||
|
||||
__device__ __forceinline__ int ts_block_scan(int v, int* s_w) { // exclusive scan over the CTA's TK_T threads
|
||||
const int t = threadIdx.x, lane = t & 31, w = t >> 5;
|
||||
@@ -2745,14 +2749,16 @@ __global__ void __launch_bounds__(TK_T) block_topk_sample_kernel(const float* __
|
||||
const float* sc = scores + qi * max_blocks;
|
||||
const int64_t nb = n_bid + 1;
|
||||
auto weight = [&](int64_t b) -> int { return b < n_bid ? R : (int) (n_kv - n_bid * R); };
|
||||
const int ns = (int) ((nb + TS_STRIDE - 1) / TS_STRIDE);
|
||||
int ts = TS_STRIDE; // the sampling stride: a function of nb, so uniform over the CTA
|
||||
while (ts < TS_STRIDE_MAX && nb > (int64_t) TS_CAP * ts) ts <<= 1;
|
||||
const int ns = (int) ((nb + ts - 1) / ts);
|
||||
if (ns > TS_CAP) { if (t == 0) g_tk_fail[qi] = 1; return; }
|
||||
// 1. the bound: the KS-th largest of every 32nd block's key (a block without cells counts as the smallest key)
|
||||
// 1. the bound: the KS-th largest of every ts-th block's key (a block without cells counts as the smallest key)
|
||||
for (int i = t; i < ns; i += TK_T) {
|
||||
const int64_t b = (int64_t) i * TS_STRIDE;
|
||||
const int64_t b = (int64_t) i * ts;
|
||||
s_key[i] = weight(b) ? order_key(sc[b]) : 0u;
|
||||
}
|
||||
const int ks_want = (int) (2 * ((width + R * TS_STRIDE - 1) / (R * TS_STRIDE)) + 16);
|
||||
const int ks_want = (int) (2 * ((width + R * ts - 1) / (R * ts)) + 16);
|
||||
int need = ks_want < ns ? ks_want : ns;
|
||||
uint32_t prefix = 0;
|
||||
for (int shift = 24; shift >= 0; shift -= 8) {
|
||||
|
||||
@@ -19,7 +19,8 @@ void ck(cudaError_t e) {
|
||||
}
|
||||
struct Case { int64_t ctx, queries; int mode; };
|
||||
bool check(Case c) {
|
||||
constexpr int64_t stride = 262144 / 4 + 2;
|
||||
// the score-row stride: the 262144 capacity, or the case's own context past it (the 1M cases)
|
||||
const int64_t stride = std::max<int64_t>(262144, c.ctx) / 4 + 2;
|
||||
const auto s = k::qsa_real_shapes();
|
||||
const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s);
|
||||
std::vector<int32_t> steps((size_t)(c.queries * k::kStepCount));
|
||||
@@ -90,8 +91,12 @@ int main() {
|
||||
{131072, 256, 1}, {131072, 256, 2}, {131072, 256, 3}, {131072, 256, 4},
|
||||
// either side of the Turing wide-kernel bound (22,528 blocks), and ties above the register limit
|
||||
{90108, 256, 0}, {90112, 256, 0}, {200000, 256, 2}, {262144, 256, 1},
|
||||
// either side of the one-read top-k's first stride doubling (TS_CAP * 32 blocks = 524,288 cells), and 1M-cell
|
||||
// rows with random scores, ties, an odd query count and a batch of one
|
||||
{524284, 256, 0}, {524288, 256, 0}, {600000, 256, 0}, {1000000, 256, 0}, {1000000, 257, 0}, {1000000, 1, 0},
|
||||
{1000000, 256, 1}, {1000000, 256, 2},
|
||||
};
|
||||
for (const auto c : cases) if (!check(c)) return 1;
|
||||
std::puts("PASS: 20 top-k active-bound cases, selected IDs bitwise identical");
|
||||
std::puts("PASS: 28 top-k active-bound cases, selected IDs bitwise identical");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user