dequant: coalesced Q8_0 path (four threads per block, 16-byte stores)

The prompt path dequantizes each dense Q8_0 projection (GDN qkv/gate/out,
QSA q/k/v/out, shared expert, hc) into the GEMM scratch before cuBLAS.
dequant_kernel runs one thread per 32-value block, so a warp's 2-byte
stores land 64 bytes apart: about 70 GB/s of writes on a V100.

dequant_q8_0_kernel gives each block to four threads, 8 values each,
written with 16-byte stores: a warp reads 8 consecutive blocks and writes
512 consecutive bytes. The arithmetic per value is unchanged (d * q), so
the output is the same bits. It is used for Q8_0 when the output rows are
contiguous (ld == cols) and the pointers are aligned (blocks to 2 bytes,
output to 16); everything else takes dequant_kernel as before.

V100-SXM2-16GB, every 2-D Q8_0 tensor of Qwen3.8-Flash-Next UD-Q4_K_XL
(498 tensors), each call timed on its own:

  attn_qkv 10240 x 2560, FP16   0.740 -> 0.106 ms   (70 -> 495 GB/s written)
  attn_q   12288 x 2560, FP16   0.887 -> 0.126 ms
  ssm_out   2560 x 6144, FP16   0.451 -> 0.065 ms
  all 498 tensors, FP16        138.5 -> 20.4 ms
  all 498 tensors, BF16        140.8 -> 20.5 ms
  all 498 tensors, FP32        190.9 -> 35.4 ms

An FNV-1a hash over all outputs is equal before and after for FP32, BF16
and FP16. Whole prompts on 4x V100 (where this was first used), 4,096-token
chunks: 18K 4,537 -> 4,708, 65K 6,540 -> 6,766, 123K 6,783 -> 6,985 tok/s.
This commit is contained in:
Eli Elgaev authored and Niko1221 committed 2026-10-09 16:43:30 +02:00
1 parent aab7e30d01
commit e910e41034
1 file changed
+32
+32
View File
@@ -8,6 +8,7 @@
#include <cuda_fp16.h>
#include <cuda_runtime.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
@@ -174,6 +175,30 @@ __global__ void dequant_kernel(const uint8_t* __restrict__ blocks, int64_t row_b
group32<TYPE>(blocks + (row0 + r) * row_bytes, (int) gi, out + r * ld + gi * 32);
}
// Q8_0 with four threads per block, 8 values each: the warp reads 8 consecutive blocks and writes 512 consecutive
// bytes (a thread per block wrote 2-byte values 64 bytes apart: ~110 GB/s, a quarter of a prompt projection's time)
template <typename T>
__global__ void dequant_q8_0_kernel(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, int64_t rows,
int64_t groups_per_row, T* __restrict__ out) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (i >= rows * groups_per_row * 4) return;
const int64_t g = i >> 2, r = g / groups_per_row, gi = g % groups_per_row;
const int piece = (int) (i & 3);
const uint8_t* b = blocks + (row0 + r) * row_bytes + gi * 34;
const float d = h2f(b);
const uint16_t* q = (const uint16_t*) (b + 2 + 8 * piece); // blocks are 2-byte aligned
__align__(16) T v[8];
#pragma unroll
for (int k = 0; k < 4; ++k) {
const uint16_t w = q[k];
put(v, 2 * k, (float) (int8_t) (w & 0xFF) * d);
put(v, 2 * k + 1, (float) (int8_t) (w >> 8) * d);
}
uint4* o = (uint4*) (out + g * 32 + 8 * piece);
#pragma unroll
for (int k = 0; k < (int) (8 * sizeof(T) / 16); ++k) o[k] = ((const uint4*) v)[k];
}
bool geometry(int type, int& block_elems, int& block_bytes) {
switch (type) {
case 2: block_elems = 32; block_bytes = 18; return true;
@@ -205,6 +230,13 @@ void launch(int type, const void* blocks, int64_t row0, int64_t rows, int64_t co
const unsigned grid = (unsigned) ((total + 255) / 256);
const uint8_t* p = (const uint8_t*) blocks;
cudaStream_t st = (cudaStream_t) stream;
if (type == 8 && ld == cols && (reinterpret_cast<uintptr_t>(blocks) & 1) == 0 &&
(reinterpret_cast<uintptr_t>(out) & 15) == 0) { // contiguous rows only (the kernel writes out + g * 32)
dequant_q8_0_kernel<T><<<(unsigned) ((4 * total + 255) / 256), 256, 0, st>>>(p, row_bytes, row0, rows, gpr, out);
const cudaError_t e = cudaGetLastError();
if (e != cudaSuccess) { std::fprintf(stderr, "dequant launch: %s\n", cudaGetErrorString(e)); std::exit(1); }
return;
}
#define STRATA_DQ(TY) dequant_kernel<TY, T><<<grid, 256, 0, st>>>(p, row_bytes, row0, rows, gpr, ld, out); break
switch (type) {
case 2: STRATA_DQ(2);