[Engine] Size unfused QSA prefill rows for the compiled model

Use the compiled query geometry for both launches, their bounds, and gate strides. Verified the 16-head guard-overwrite regression and stock 24-head output on synthetic CPU fixtures. Implemented by gpt-6.2-astra high in 54c67c0ab7bcaefe2c1ed6980f3b5beb71ddf261.
This commit is contained in:
Codex (gpt-6.2-astra) authored and Sean Carey committed 2026-12-04 18:40:52 +11:00
1 parent ac85058cad
commit c1b6a2cb8f
6 files changed
+127 -28

No files matched your search

+4
View File
@@ -499,6 +499,10 @@ if(STRATA_BUILD_TESTS)
add_executable(qsa_batch_check_test tests/prefill/qsa_batch_check_test.cpp)
target_include_directories(qsa_batch_check_test PRIVATE include)
add_test(NAME qsa_batch_check_test COMMAND qsa_batch_check_test)
# Unfused prompt query/gate rows, including a padded output projection (host arithmetic, no GPU).
add_executable(qsa_query_rows_test tests/prefill/qsa_query_rows_test.cpp)
target_include_directories(qsa_query_rows_test PRIVATE include)
add_test(NAME qsa_query_rows_test COMMAND qsa_query_rows_test)
# #5715: strata-q36's one-session context check (the reporter's 12 GB card from its log): header-only, no GPU
add_executable(q36_context_fit_test tests/program/q36_context_fit_test.cpp)
+2 -2
View File
@@ -159,9 +159,9 @@ void rope(float* x, int64_t T, int64_t heads, int64_t dim, int64_t ld, int64_t p
void rms_rope_rows(const float* in, int64_t in_ts, int64_t in_hs, float* out, int64_t out_ts, const float* w, int64_t T,
int64_t heads, int64_t dim, float eps, int64_t pos0, const strata::kernels::RopeScaling& scaling,
void* stream);
/// q_full [T, 24, 512] (q | gate per head) -> q [T, 24, 256]
/// q_full [T, n_head, 2 * head_dim] (q | gate per head) -> q [T, n_head, head_dim]
void split_q(const float* q_full, float* q, int64_t T, void* stream);
/// attn[t, h, d] *= sigmoid(q_full[t, h, 256 + d]) -> out16 (fp16 bits: the o-projection is quantized)
/// out16[t, h, d] = fp16(attn[t, h, d] * sigmoid(q_full[t, h, head_dim + d])). ld16 = 0 selects packed rows.
void gate_attn(const float* attn, const float* q_full, uint16_t* out16, int64_t T, void* stream, int64_t ld16 = 0);
/// K and V of T consecutive cells (positions pos0..pos0+T-1; K normed and rotated) into the paged pools: FP16
@@ -0,0 +1,35 @@
#pragma once
#include "strata/core/geometry.hpp"
#if defined(__CUDACC__) || defined(__HIPCC__)
#define STRATA_QSA_ROWS_HD __host__ __device__
#else
#define STRATA_QSA_ROWS_HD
#endif
namespace strata::prefill {
// i indexes packed [token, query head, lane]. Each projected head stores Q followed by its gate.
template<int64_t Heads, int64_t HeadDim>
struct QsaQueryRows {
static constexpr int64_t width = Heads * HeadDim;
static constexpr int64_t head_dim = HeadDim;
static constexpr int64_t elements(int64_t tokens) { return tokens * width; }
STRATA_QSA_ROWS_HD static constexpr int64_t query_offset(int64_t i) {
return (i / HeadDim) * (2 * HeadDim) + i % HeadDim;
}
STRATA_QSA_ROWS_HD static constexpr int64_t output_offset(int64_t i, int64_t stride) {
return (i / width) * stride + i % width;
}
static constexpr int64_t output_stride(int64_t ld16) { return ld16 > 0 ? ld16 : width; }
};
using CompiledQsaQueryRows = QsaQueryRows<core::kCompiledGeometry.n_head, core::kCompiledGeometry.head_dim>;
} // namespace strata::prefill
#undef STRATA_QSA_ROWS_HD
+14 -12
View File
@@ -1,5 +1,6 @@
// src/prefill/kernels.cu - see include/strata/prefill/kernels.hpp.
#include "strata/prefill/kernels.hpp"
#include "strata/prefill/qsa_query_rows.hpp"
#include "strata/prefill/gdn_chunk.hpp"
#include "strata/prefill/gdn_probe.hpp"
#include "strata/kernels/mrope.hpp"
@@ -2532,7 +2533,7 @@ __global__ void rope_kernel(float* __restrict__ x, int64_t heads, int64_t dim, i
// the query) in ONE pass over a head's row. The same arithmetic in the same order as the pair: the sum of squares goes
// through the same block_sum, the scaled value is (s * x) * w as a float (the pair stored it and reloaded it), and a
// rotated pair is rotated with rope_kernel's own expressions. What changes is the traffic: the q chain read and wrote
// the 24 heads' rows four times (split, norm read, norm write, rope read/write), this reads the source once and writes
// the query heads' rows four times (split, norm read, norm write, rope read/write), this reads the source once and writes
// the result once, and the rope runs in the block that already holds the row instead of 32-thread blocks.
// Idea: halo-box/strix-llama.cpp #213 ("fused RMS norm + multi-section RoPE", MIT, The ggml authors); the kernel is ours.
// in[t * in_ts + h * in_hs + d] -> out[t * out_ts + h * dim + d]; in and out may be the same buffer (a block reads its whole
@@ -2579,18 +2580,17 @@ __global__ void __launch_bounds__(256) rms_rope_rows_kernel(const float* in, int
o[tid] = sv[tid];
}
}
__global__ void split_q_kernel(const float* __restrict__ qf, float* __restrict__ q, int64_t T) {
__global__ void split_q_kernel(const float* __restrict__ qf, float* __restrict__ q, int64_t n) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (i >= T * 24 * 256) return;
const int64_t t = i / (24 * 256), h = (i / 256) % 24, d = i % 256;
q[i] = qf[t * 24 * 512 + h * 512 + d];
if (i >= n) return;
q[i] = qf[CompiledQsaQueryRows::query_offset(i)];
}
__global__ void gate_attn_kernel(const float* __restrict__ a, const float* __restrict__ qf, uint16_t* __restrict__ o16,
int64_t T, int64_t ld16) {
int64_t n, int64_t ld16) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (i >= T * 24 * 256) return;
const int64_t t = i / (24 * 256), h = (i / 256) % 24, d = i % 256;
o16[t * ld16 + h * 256 + d] = hf(a[i] * (1.0f / (1.0f + expf(-qf[t * 24 * 512 + h * 512 + 256 + d]))));
if (i >= n) return;
const float gate = qf[CompiledQsaQueryRows::query_offset(i) + CompiledQsaQueryRows::head_dim];
o16[CompiledQsaQueryRows::output_offset(i, ld16)] = hf(a[i] * (1.0f / (1.0f + expf(-gate))));
}
// one block per (token, kv head, 64-value group); 64 threads. KV streaming: the pool page only if the block is
@@ -3578,12 +3578,14 @@ void rms_rope_rows(const float* in, int64_t in_ts, int64_t in_hs, float* out, in
check("rms_rope_rows");
}
void split_q(const float* q_full, float* q, int64_t T, void* stream) {
split_q_kernel<<<blocks_for(T * 24 * 256), 256, 0, (cudaStream_t) stream>>>(q_full, q, T);
const int64_t n = CompiledQsaQueryRows::elements(T);
split_q_kernel<<<blocks_for(n), 256, 0, (cudaStream_t) stream>>>(q_full, q, n);
check("split_q");
}
void gate_attn(const float* attn, const float* q_full, uint16_t* out16, int64_t T, void* stream, int64_t ld16) {
gate_attn_kernel<<<blocks_for(T * 24 * 256), 256, 0, (cudaStream_t) stream>>>(attn, q_full, out16, T,
ld16 > 0 ? ld16 : 24 * 256);
const int64_t n = CompiledQsaQueryRows::elements(T);
gate_attn_kernel<<<blocks_for(n), 256, 0, (cudaStream_t) stream>>>(attn, q_full, out16, n,
CompiledQsaQueryRows::output_stride(ld16));
check("gate_attn");
}
+17 -14
View File
@@ -1,6 +1,6 @@
// src/prefill/qsa_nr_parity.cu - the QSA layer's fused norm + rope (rms_rope_rows, STRATA_QSA_FUSE_NR) against the chain it
// replaces (split_q + rms_rows + rope), compared BIT FOR BIT over the whole output, for the three uses in the prompt path:
// query Qf [T, 24, 512] -> q [T, 24, 256] (split, norm, rotate)
// query Qf [T, heads, 512] -> q [T, heads, 256] (split, norm, rotate)
// key Kc [T, 2, 256] in place (norm, rotate)
// indexer q_idx [T, 4, 128] with row stride 512, in place (norm, rotate)
// Every output element is checked (not a sample), the output buffers are poisoned with NaN first, and T runs through
@@ -8,6 +8,7 @@
// qsa_nr_parity the checks; qsa_nr_parity --bench and the timings at T = 2048 / 8191
#include "strata/kernels/rope_scaling.hpp"
#include "strata/prefill/kernels.hpp"
#include "strata/core/geometry.hpp"
#include <cuda_runtime.h>
@@ -66,9 +67,11 @@ int main(int argc, char** argv) {
}
const bool bench = argc > 1 && std::string(argv[1]) == "--bench";
const strata::kernels::RopeScaling& rs = strata::kernels::rope_scaling();
constexpr int64_t heads = strata::core::kCompiledGeometry.n_head;
constexpr int64_t width = heads * strata::core::kCompiledGeometry.head_dim;
const int64_t TMAX = 8191;
std::mt19937 rng(12345);
std::vector<float> hqf((size_t) TMAX * 24 * 512), hk((size_t) TMAX * 512), hqi((size_t) TMAX * 512), wq(256), wk(256), wi(128);
std::vector<float> hqf((size_t) TMAX * (2 * width)), hk((size_t) TMAX * 512), hqi((size_t) TMAX * 512), wq(256), wk(256), wi(128);
fill(hqf, rng, 2.0f);
fill(hk, rng, 3.0f);
fill(hqi, rng, 1.5f);
@@ -76,9 +79,9 @@ int main(int argc, char** argv) {
for (float& x : wk) x = 0.5f + std::abs(std::normal_distribution<float>(0.0f, 1.0f)(rng));
for (float& x : wi) x = 0.5f + std::abs(std::normal_distribution<float>(0.0f, 1.0f)(rng));
// a few rows with large and tiny magnitudes, and an all-zero row (rsqrt(eps))
for (int d = 0; d < 256; ++d) { hqf[(size_t) 3 * 24 * 512 + 512 * 5 + d] = 0.0f; hqf[(size_t) 4 * 24 * 512 + d] *= 1e4f; hk[(size_t) 7 * 512 + d] *= 1e-6f; }
for (int d = 0; d < 256; ++d) { hqf[(size_t) 3 * (2 * width) + 512 * 5 + d] = 0.0f; hqf[(size_t) 4 * (2 * width) + d] *= 1e4f; hk[(size_t) 7 * 512 + d] *= 1e-6f; }
float *dqf = dalloc<float>(hqf.size()), *dk0 = dalloc<float>(hk.size()), *dk1 = dalloc<float>(hk.size()),
*dq0 = dalloc<float>((size_t) TMAX * 24 * 256), *dq1 = dalloc<float>((size_t) TMAX * 24 * 256),
*dq0 = dalloc<float>((size_t) TMAX * width), *dq1 = dalloc<float>((size_t) TMAX * width),
*di0 = dalloc<float>(hqi.size()), *di1 = dalloc<float>(hqi.size()), *dwq = dalloc<float>(256), *dwk = dalloc<float>(256),
*dwi = dalloc<float>(128);
ck(cudaMemcpy(dqf, hqf.data(), hqf.size() * 4, cudaMemcpyHostToDevice), "up");
@@ -92,30 +95,30 @@ int main(int argc, char** argv) {
for (int64_t T : Ts) {
for (int64_t p0 : P0s) {
// old chain
ck(cudaMemset(dq0, 0xff, (size_t) T * 24 * 256 * 4), "poison");
ck(cudaMemset(dq1, 0xff, (size_t) T * 24 * 256 * 4), "poison");
ck(cudaMemset(dq0, 0xff, (size_t) T * width * 4), "poison");
ck(cudaMemset(dq1, 0xff, (size_t) T * width * 4), "poison");
ck(cudaMemcpy(dk0, hk.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up");
ck(cudaMemcpy(dk1, hk.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up");
ck(cudaMemcpy(di0, hqi.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up");
ck(cudaMemcpy(di1, hqi.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up");
split_q(dqf, dq0, T, nullptr);
rms_rows(dq0, dwq, T * 24, 256, 256, eps, nullptr);
rope(dq0, T, 24, 256, 6144, p0, rs, nullptr);
rms_rows(dq0, dwq, T * heads, 256, 256, eps, nullptr);
rope(dq0, T, heads, 256, width, p0, rs, nullptr);
rms_rows(dk0, dwk, T * 2, 256, 256, eps, nullptr);
rope(dk0, T, 2, 256, 512, p0, rs, nullptr);
rms_rows(di0, dwi, T * 4, 128, 128, eps, nullptr);
rope(di0, T, 4, 128, 512, p0, rs, nullptr);
// fused
rms_rope_rows(dqf, 24 * 512, 512, dq1, 6144, dwq, T, 24, 256, eps, p0, rs, nullptr);
rms_rope_rows(dqf, 2 * width, 512, dq1, width, dwq, T, heads, 256, eps, p0, rs, nullptr);
rms_rope_rows(dk1, 512, 256, dk1, 512, dwk, T, 2, 256, eps, p0, rs, nullptr);
rms_rope_rows(di1, 512, 128, di1, 512, dwi, T, 4, 128, eps, p0, rs, nullptr);
if (!ck(cudaDeviceSynchronize(), "sync")) return 1;
size_t f0 = 0, f1 = 0, f2 = 0;
const size_t nq = diffs(download(dq0, (size_t) T * 24 * 256), download(dq1, (size_t) T * 24 * 256), f0);
const size_t nq = diffs(download(dq0, (size_t) T * width), download(dq1, (size_t) T * width), f0);
const size_t nk = diffs(download(dk0, (size_t) T * 512), download(dk1, (size_t) T * 512), f1);
const size_t ni = diffs(download(di0, (size_t) T * 512), download(di1, (size_t) T * 512), f2);
// the poison must be gone everywhere in the fused query output
const std::vector<float> q1 = download(dq1, (size_t) T * 24 * 256);
const std::vector<float> q1 = download(dq1, (size_t) T * width);
size_t nan = 0;
for (float x : q1) nan += x != x;
const bool ok = nq == 0 && nk == 0 && ni == 0 && nan == 0;
@@ -141,15 +144,15 @@ int main(int argc, char** argv) {
};
const float old_ms = time([&] {
split_q(dqf, dq0, T, nullptr);
rms_rows(dq0, dwq, T * 24, 256, 256, eps, nullptr);
rope(dq0, T, 24, 256, 6144, 100, rs, nullptr);
rms_rows(dq0, dwq, T * heads, 256, 256, eps, nullptr);
rope(dq0, T, heads, 256, width, 100, rs, nullptr);
rms_rows(dk0, dwk, T * 2, 256, 256, eps, nullptr);
rope(dk0, T, 2, 256, 512, 100, rs, nullptr);
rms_rows(di0, dwi, T * 4, 128, 128, eps, nullptr);
rope(di0, T, 4, 128, 512, 100, rs, nullptr);
});
const float new_ms = time([&] {
rms_rope_rows(dqf, 24 * 512, 512, dq1, 6144, dwq, T, 24, 256, eps, 100, rs, nullptr);
rms_rope_rows(dqf, 2 * width, 512, dq1, width, dwq, T, heads, 256, eps, 100, rs, nullptr);
rms_rope_rows(dk1, 512, 256, dk1, 512, dwk, T, 2, 256, eps, 100, rs, nullptr);
rms_rope_rows(di1, 512, 128, di1, 512, dwi, T, 4, 128, eps, 100, rs, nullptr);
});
@@ -0,0 +1,55 @@
// Exercise the split/gate kernels' launch span and offsets. Distinct signed tags identify both
// halves of every projected head without depending on float16 or sigmoid rounding.
#include "strata/prefill/qsa_query_rows.hpp"
#include <cstdio>
#include <vector>
using strata::prefill::QsaQueryRows;
template<class Rows>
bool check(int heads, int padding) {
constexpr int tokens = 65, dim = 256;
const int width = heads * dim, stride = width + padding;
const int count = tokens * width;
const int64_t ld16 = Rows::output_stride(padding ? stride : 0);
if (Rows::elements(tokens) != count || ld16 != stride) {
std::fprintf(stderr, "wrong launch span or stride (heads %d, padding %d)\n", heads, padding);
return false;
}
std::vector<int> projected(2 * count);
std::vector<int> want_gate(tokens * stride, 0), got_gate(tokens * stride, 0);
for (int t = 0; t < tokens; ++t) {
for (int h = 0; h < heads; ++h) {
for (int d = 0; d < dim; ++d) {
const int tag = t * width + h * dim + d + 1;
const int head_start = t * (2 * width) + h * (2 * dim);
projected[head_start + d] = tag;
projected[head_start + dim + d] = -tag;
want_gate[t * stride + h * dim + d] = -tag;
}
}
}
for (int64_t i = 0; i < Rows::elements(tokens); ++i) {
const int64_t q = Rows::query_offset(i);
if (projected.at(q) != i + 1) {
std::fprintf(stderr, "wrong Q at lane %lld (heads %d)\n", (long long) i, heads);
return false;
}
got_gate.at(Rows::output_offset(i, ld16)) = projected.at(q + Rows::head_dim);
}
if (got_gate != want_gate) { // includes every padding slot
std::fprintf(stderr, "wrong gate rows (heads %d, padding %d)\n", heads, padding);
return false;
}
return true;
}
int main() {
for (int pad : {0, 64}) {
if (!check<QsaQueryRows<16, 256>>(16, pad) || !check<QsaQueryRows<24, 256>>(24, pad) ||
!check<strata::prefill::CompiledQsaQueryRows>(strata::core::kCompiledGeometry.n_head, pad)) return 1;
}
return 0;
}