Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

s_gemv: the q8 split kernel reaches its barrier before a rowless warp returns

Guillaume PUTIER91e8d21227a7
diff --git a/src/kernels/cuda/s_gemv.cu b/src/kernels/cuda/s_gemv.cu
index cdfa4d0bb..d0db17837 100644
--- a/src/kernels/cuda/s_gemv.cu
+++ b/src/kernels/cuda/s_gemv.cu
@@ -147,14 +147,20 @@ __global__ void s_gemv_q8_split_kernel(const uint8_t* __restrict__ x, const uint
constexpr int PER_BYTE = 8 / CODE_BITS;
const int warps_per_block = (int) (blockDim.x >> 5);
const long long o = (long long) blockIdx.x * warps_per_block + (threadIdx.x >> 5);
- if (o >= n_out) return;
const int lane = threadIdx.x & 31;
// THE CODEBOOK, IN SHARED MEMORY. 16 bytes, loaded once per block by the first 16 threads, instead of a
// divergent constant read per element - measured at 2.12x of this whole kernel (see the note on `kIq4Nl`).
+ //
+ // THE LOAD AND THE BARRIER COME BEFORE THE EARLY RETURN, as in `s_gemv_q8k_kernel`. The return used to
+ // sit above them, so whenever `n_out % 8 != 0` the last block's surplus warps left before a barrier the
+ // others still waited at - undefined behaviour that every shape in the pack (a multiple of 8) happened to
+ // avoid. `o` is per-WARP, so the return below keeps whole warps together and the shuffle reduction's
+ // full mask stays valid.
__shared__ signed char s_iq4nl[16];
if (threadIdx.x < 16) s_iq4nl[threadIdx.x] = kIq4Nl[threadIdx.x];
__syncthreads();
+ if (o >= n_out) return;
const long long n_groups = n_in >> group_shift;
const long long codes_per_row = n_in / PER_BYTE;
@@ -655,8 +661,10 @@ void s_gemv_q8_0_split(const uint8_t* x_q8_0, const uint8_t* codes, const float*
form.group_elems);
std::exit(1);
}
- if (form.group_elems % 4 != 0) {
- std::fprintf(stderr, "s_gemv_q8_0_split: group_elems %d is not a multiple of 4\n", form.group_elems);
+ // SIXTEEN, NOT FOUR: a lane-iteration takes QE = 16 consecutive elements under ONE scale (see the kernel),
+ // so a group of 4 or 8 would read the wrong scale for most of them. The Q8_K launcher already said 16.
+ if (form.group_elems % 16 != 0) {
+ std::fprintf(stderr, "s_gemv_q8_0_split: group_elems %d is not a multiple of 16\n", form.group_elems);
std::exit(1);
}
int group_shift = 0;
diff --git a/src/kernels/s_gemv_q8k_parity.cpp b/src/kernels/s_gemv_q8k_parity.cpp
index 882122b06..5bfb475a0 100644
--- a/src/kernels/s_gemv_q8k_parity.cpp
+++ b/src/kernels/s_gemv_q8k_parity.cpp
@@ -72,6 +72,108 @@ struct Case {
bool has_offset;
};
+/// THE RAGGED LAST BLOCK. The split kernels give each warp one row and a block eight warps, so with
+/// `n_out % 8 != 0` the last block holds warps that have no row. Those warps used to return BEFORE the
+/// codebook barrier - undefined behaviour that every real shape (a multiple of 8) avoided. Here `n_out` is
+/// deliberately ragged, every row is checked against the host reference over the same bytes, and a guard
+/// band past `n_out` must come back untouched: a warp without a row must neither hang the block nor write.
+/// (`compute-sanitizer --tool synccheck` on this binary is the direct check of the barrier itself.)
+int ragged_rows(const Case& cs, long long n_out) {
+ const long long n_in = 512;
+ const long long guard = 64;
+ std::mt19937 rng(77 + (unsigned) n_out);
+ std::normal_distribution<float> g(0.0f, 1.0f);
+ std::vector<float> xf((size_t) n_in);
+ for (auto& v : xf) v = g(rng);
+ SForm f;
+ f.code_bits = cs.bits;
+ f.code_bias = cs.bias;
+ f.group_elems = cs.group;
+ f.codebook = cs.cb;
+ f.has_offset = cs.has_offset;
+ const int per_byte = 8 / cs.bits;
+ std::vector<uint8_t> codes((size_t) (n_out * n_in / per_byte));
+ for (auto& v : codes) v = (uint8_t) (rng() & 0xFF);
+ const long long n_groups = n_in / cs.group;
+ std::vector<float> scales((size_t) (n_out * n_groups)), offs((size_t) (n_out * n_groups));
+ for (auto& v : scales) v = 0.01f + 0.001f * (float) (rng() % 20);
+ for (auto& v : offs) v = 0.2f * g(rng);
+
+ float* d_xf = nullptr;
+ uint8_t *d_xk = nullptr, *d_x0 = nullptr, *d_codes = nullptr;
+ float *d_scales = nullptr, *d_offs = nullptr, *d_y = nullptr;
+ std::vector<uint8_t> xk((size_t) (n_in / 256) * 292), x0((size_t) (n_in / 32) * 34);
+ check(cudaMalloc(&d_xf, (size_t) n_in * 4), "rxf");
+ check(cudaMalloc(&d_xk, xk.size()), "rxk");
+ check(cudaMalloc(&d_x0, x0.size()), "rx0");
+ check(cudaMalloc(&d_codes, codes.size()), "rcodes");
+ check(cudaMalloc(&d_scales, scales.size() * 4), "rscales");
+ check(cudaMalloc(&d_offs, offs.size() * 4), "roffs");
+ check(cudaMalloc(&d_y, (size_t) (n_out + guard) * 4), "ry");
+ check(cudaMemcpy(d_xf, xf.data(), (size_t) n_in * 4, cudaMemcpyHostToDevice), "rcxf");
+ check(cudaMemcpy(d_codes, codes.data(), codes.size(), cudaMemcpyHostToDevice), "rcc");
+ check(cudaMemcpy(d_scales, scales.data(), scales.size() * 4, cudaMemcpyHostToDevice), "rcs");
+ check(cudaMemcpy(d_offs, offs.data(), offs.size() * 4, cudaMemcpyHostToDevice), "rco");
+ strata::kernels::quantize_q8_K(d_xf, d_xk, n_in, nullptr);
+ strata::kernels::quantize_q8_0(d_xf, d_x0, n_in, nullptr);
+ check(cudaMemcpy(xk.data(), d_xk, xk.size(), cudaMemcpyDeviceToHost), "rcxk");
+ check(cudaMemcpy(x0.data(), d_x0, x0.size(), cudaMemcpyDeviceToHost), "rcx0");
+
+ int bad = 0;
+ for (int kind = 0; kind < 2; ++kind) { // 0: Q8_K activation, 1: Q8_0 activation
+ std::vector<float> want((size_t) n_out);
+ std::vector<double> mag((size_t) n_out); // sum |w_i a_i| per row: a row whose sum cancels near 0
+ for (long long o = 0; o < n_out; ++o) { // must not turn f32 rounding into a relative failure
+ double acc = 0, am = 0;
+ for (long long i = 0; i < n_in; ++i) {
+ double a;
+ if (kind == 0) {
+ a = q8k_at(xk, i);
+ } else {
+ const uint8_t* blk = x0.data() + (size_t) (i / 32) * 34;
+ uint16_t dbits;
+ std::memcpy(&dbits, blk, 2);
+ a = (double) strata::kernels::f32_from_f16(dbits) * (double) ((const int8_t*) (blk + 2))[i % 32];
+ }
+ const double wa = weight_at(codes, scales, offs, f, n_in, o, i) * a;
+ acc += wa;
+ am += std::fabs(wa);
+ }
+ want[(size_t) o] = (float) acc;
+ mag[(size_t) o] = am;
+ }
+ // the guard band holds a NaN pattern; any write there shows up as a changed bit
+ std::vector<uint32_t> sentinel((size_t) (n_out + guard), 0x7FC0DEADu);
+ check(cudaMemcpy(d_y, sentinel.data(), sentinel.size() * 4, cudaMemcpyHostToDevice), "rsent");
+ if (kind == 0)
+ strata::kernels::s_gemv_q8k_split(d_xk, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y, n_in,
+ n_out, f, nullptr);
+ else
+ strata::kernels::s_gemv_q8_0_split(d_x0, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y, n_in,
+ n_out, f, nullptr);
+ check(cudaDeviceSynchronize(), "ragged split");
+ std::vector<uint32_t> got_bits(sentinel.size());
+ check(cudaMemcpy(got_bits.data(), d_y, got_bits.size() * 4, cudaMemcpyDeviceToHost), "rcy");
+ double m = 0, d = 0;
+ for (long long o = 0; o < n_out; ++o) {
+ float v;
+ std::memcpy(&v, &got_bits[(size_t) o], 4);
+ m += mag[(size_t) o];
+ d += std::fabs((double) want[(size_t) o] - (double) v);
+ }
+ long long touched = 0;
+ for (long long o = n_out; o < n_out + guard; ++o) touched += got_bits[(size_t) o] != 0x7FC0DEADu;
+ const double rel = d / (m > 1e-30 ? m : 1e-30);
+ const bool ok = rel <= 1e-5 && touched == 0;
+ std::printf(" %-28s %s n_out %3lld: rel %.3e, guard words written %lld %s\n", cs.what,
+ kind == 0 ? "Q8_K" : "Q8_0", n_out, rel, touched, ok ? "ok" : "*** WRONG ***");
+ if (!ok) ++bad;
+ }
+ cudaFree(d_xf); cudaFree(d_xk); cudaFree(d_x0); cudaFree(d_codes); cudaFree(d_scales); cudaFree(d_offs);
+ cudaFree(d_y);
+ return bad;
+}
+
} // namespace
int main(int argc, char** argv) {
@@ -229,6 +331,11 @@ int main(int argc, char** argv) {
cudaFree(d_xf); cudaFree(d_xq); cudaFree(d_codes); cudaFree(d_scales); cudaFree(d_offs); cudaFree(d_y);
}
+ // ---- 4. output widths that are NOT a multiple of the eight rows per block
+ std::printf("\n");
+ for (const Case& cs : cases)
+ for (long long n_out : {1LL, 7LL, 9LL, 61LL}) bad += ragged_rows(cs, n_out);
+
std::printf("\ns_gemv_q8k: %d failures\n", bad);
if (bad) return 1;
if (selftest) std::printf("s_gemv_q8k_parity OK\n");