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++

kv_q8: the gather decodes 8 int8 per thread (bitwise, +11% on the 32k microbench)

ATIVX928132faae7cf18
diff --git a/bench/results/2026-10-03-v100-kv-int8-read/README.md b/bench/results/2026-10-03-v100-kv-int8-read/README.md
index d2fe6bb15..75dcf88a3 100644
--- a/bench/results/2026-10-03-v100-kv-int8-read/README.md
+++ b/bench/results/2026-10-03-v100-kv-int8-read/README.md
@@ -25,7 +25,26 @@ The KV-streaming copy is already wide: `copy_kernel`, `src/kernels/cuda/kv_strea
## 2. Vectorization
-TBD
+The gather (`kv_gather_q8_kernel`) was 4 values/thread: two `char4` (4 B) loads + two `ushort4` (8 B) stores. The
+experimental build (`-DSTRATA_EXPERIMENTAL_SM60=ON`, Pascal/Volta) adds an 8-wide `kv_gather_q8_wide_kernel`: one
+`uint2` (8 B) load of codes, one `uint4` (16 B) store of the dequantized fp16, one scale per thread instead of two.
+The 8 values lie in one 64-value group, and the arithmetic is unchanged (`(float) code * scale -> f16_from_f32`,
+packed two halfs per 32-bit word), so the output is byte-identical. The ready-made engine keeps exactly the
+4-wide kernel.
+
+`kv_q8_parity`: OK, worst INT8-vs-FP16 error 0.590 steps (bitwise codes, scales and gather; unchanged).
+
+Microbench (`kv_q8_parity --bench`, 2,051 cells / 2 KV heads / 256 dims, 32k pool, random cells, V100):
+
+| | us/call | GB/s |
+|---|---|---|
+| before (4-wide) | 15.85 | 401 |
+| after (8-wide) | 14.05 | 452 |
+
++11.4% on the gather. The default decode path (`g_fast_attn`) does not gather; its decode (`load8_q8`,
+`qsa_decode_attn.cu:46-54`) already reads 8 int8 with one `uint2` and is arithmetic-bound (8 I2F + 8 FMUL per
+cell), which `__byte_perm`/half2 cannot improve while staying bit-exact. The gather is what the non-fast,
+streaming and hybrid paths use.
## 3. int8 vs q4_0 vs k8v4 at 32k
diff --git a/src/kernels/cuda/kv_q8.cu b/src/kernels/cuda/kv_q8.cu
index 2965f2339..a03e171be 100644
--- a/src/kernels/cuda/kv_q8.cu
+++ b/src/kernels/cuda/kv_q8.cu
@@ -73,7 +73,8 @@ __global__ void kv_append_q8_kernel(int8_t* __restrict__ k_q, int8_t* __restrict
}
}
-// One thread = 4 consecutive values of one cell and head (as the FP16 gather does with uint2).
+// One thread = 4 consecutive values of one cell and head (as the FP16 gather does with uint2). This is the gather
+// the ready-made engine runs; the experimental build's 8-wide kernel is below it.
__global__ void kv_gather_q8_kernel(const int8_t* __restrict__ k_q, const int8_t* __restrict__ v_q,
const uint16_t* __restrict__ k_scale, const uint16_t* __restrict__ v_scale,
const int32_t* __restrict__ table, const int32_t* __restrict__ ids,
@@ -106,6 +107,76 @@ __global__ void kv_gather_q8_kernel(const int8_t* __restrict__ k_q, const int8_t
reinterpret_cast<ushort4*>(v_scratch)[dst] = vo;
}
+#if defined(STRATA_EXPERIMENTAL_SM60)
+// One thread = 8 consecutive values of one cell and head: an 8-byte `uint2` load of codes and a 16-byte `uint4`
+// store of the dequantized fp16. The 8 values share one 64-value group, so one scale is loaded per thread and
+// the arithmetic stays `(float) code * scale -> f16_from_f32`, i.e. bitwise the same as the previous 4-wide
+// version (whose char4 loads became these uint2 loads). `load8_q8` in qsa_decode_attn.cu reads 8 int8 the
+// same way; this is the gather's other half. Only in the experimental build (-DSTRATA_EXPERIMENTAL_SM60=ON):
+// the ready-made engine keeps exactly the 4-wide kernel above, so its gather is bit for bit the release engine's.
+__device__ __forceinline__ uint32_t q8_pair_to_f16x2(const int8_t* c, float s) {
+ return (uint32_t) f16_from_f32((float) c[0] * s) | ((uint32_t) f16_from_f32((float) c[1] * s) << 16);
+}
+
+__global__ void kv_gather_q8_wide_kernel(const int8_t* __restrict__ k_q, const int8_t* __restrict__ v_q,
+ const uint16_t* __restrict__ k_scale, const uint16_t* __restrict__ v_scale,
+ const int32_t* __restrict__ table, const int32_t* __restrict__ ids,
+ const int32_t* __restrict__ step, int kv_heads, int head_dim, int page_size,
+ uint16_t* __restrict__ k_scratch, uint16_t* __restrict__ v_scratch) {
+ const long long n_ids = (long long) __ldg(step + kStepWidth);
+ const int per = head_dim / 8;
+ const long long total = n_ids * kv_heads * per;
+ const long long i = blockIdx.x * (long long) blockDim.x + threadIdx.x;
+ if (i >= total) return;
+ const long long id = i / (kv_heads * (long long) per);
+ const int rem = (int) (i % (kv_heads * (long long) per));
+ const int h = rem / per, q8 = rem - h * per;
+ const int cell = ids[id];
+ const long long page = (long long) table[cell / page_size];
+ const long long row = (page * kv_heads + h) * page_size + (cell % page_size);
+ const int d = q8 * 8; // 8 divides KV_Q8_GROUP, so all 8 values share the group's scale
+ const int groups = head_dim / KV_Q8_GROUP;
+ const float ks = f32_from_f16(k_scale[row * groups + d / KV_Q8_GROUP]);
+ const float vs = f32_from_f16(v_scale[row * groups + d / KV_Q8_GROUP]);
+ const uint2 kraw = *reinterpret_cast<const uint2*>(k_q + row * head_dim + d); // one 8-byte load
+ const uint2 vraw = *reinterpret_cast<const uint2*>(v_q + row * head_dim + d);
+ const int8_t* kc = reinterpret_cast<const int8_t*>(&kraw);
+ const int8_t* vc = reinterpret_cast<const int8_t*>(&vraw);
+ uint4 ko, vo;
+ ko.x = q8_pair_to_f16x2(kc + 0, ks); ko.y = q8_pair_to_f16x2(kc + 2, ks);
+ ko.z = q8_pair_to_f16x2(kc + 4, ks); ko.w = q8_pair_to_f16x2(kc + 6, ks);
+ vo.x = q8_pair_to_f16x2(vc + 0, vs); vo.y = q8_pair_to_f16x2(vc + 2, vs);
+ vo.z = q8_pair_to_f16x2(vc + 4, vs); vo.w = q8_pair_to_f16x2(vc + 6, vs);
+ const long long dst = (id * kv_heads + h) * (long long) per + q8;
+ reinterpret_cast<uint4*>(k_scratch)[dst] = ko;
+ reinterpret_cast<uint4*>(v_scratch)[dst] = vo;
+}
+
+// The current device is below sm_75 (per device: a layer split can mix cards); STRATA_Q8_GATHER_WIDE=0 turns the
+// 8-wide gather off (A/B). The wide kernel is bitwise identical, so this only decides speed. Mirrors
+// qsa_decode_attn.cu's pre75_attn.
+bool pre75_q8_gather() {
+ static const bool off = [] {
+ const char* e = std::getenv("STRATA_Q8_GATHER_WIDE");
+ return e != nullptr && e[0] == '0';
+ }();
+ static int cc[64] = {};
+ int dev = 0;
+ if (off) return false;
+ if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return false; }
+ if (cc[dev] == 0) {
+ int major = 0, minor = 0;
+ if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess ||
+ cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev) != cudaSuccess) {
+ cudaGetLastError();
+ return false;
+ }
+ cc[dev] = 10 * major + minor;
+ }
+ return cc[dev] < 75;
+}
+#endif // STRATA_EXPERIMENTAL_SM60
+
} // namespace
void kv_append_q8_step(int8_t* k_q, int8_t* v_q, uint16_t* k_scale, uint16_t* v_scale, const int32_t* page_table,
@@ -136,6 +207,17 @@ void kv_gather_q8_step(const int8_t* k_q, const int8_t* v_q, const uint16_t* k_s
const QsaShapes& s, uint16_t* k_scratch, uint16_t* v_scratch, void* stream) {
validate(s, "kv_gather_q8");
if (max_ids <= 0) return;
+#if defined(STRATA_EXPERIMENTAL_SM60)
+ if (pre75_q8_gather()) {
+ const long long total = max_ids * s.n_head_kv * (s.head_dim / 8);
+ const unsigned blocks = (unsigned) ((total + 255) / 256);
+ kv_gather_q8_wide_kernel<<<blocks, 256, 0, (cudaStream_t) stream>>>(
+ k_q, v_q, k_scale, v_scale, page_table, ids, step, (int) s.n_head_kv, (int) s.head_dim,
+ (int) s.page_size, k_scratch, v_scratch);
+ check("kv_gather_q8 wide launch");
+ return;
+ }
+#endif
const long long total = max_ids * s.n_head_kv * (s.head_dim / 4);
const unsigned blocks = (unsigned) ((total + 255) / 256);
kv_gather_q8_kernel<<<blocks, 256, 0, (cudaStream_t) stream>>>(
diff --git a/src/kernels/kv_q8_parity.cpp b/src/kernels/kv_q8_parity.cpp
index 733d7c7f8..f60eb93ad 100644
--- a/src/kernels/kv_q8_parity.cpp
+++ b/src/kernels/kv_q8_parity.cpp
@@ -29,7 +29,56 @@ void ck(cudaError_t e, const char* w) {
template <typename T> T* dalloc(size_t n) { T* p = nullptr; ck(cudaMalloc(&p, n * sizeof(T) + 64), "malloc"); ck(cudaMemset(p, 0, n * sizeof(T) + 64), "memset"); return p; }
} // namespace
-int main() {
+// A decode-shaped microbench: a 32k pool, the 2,051-cell selection the QSA decoder gathers per layer, random
+// cells (so the reads miss L2). Reports the gather's effective traffic per call and GB/s. Not part of the test.
+int bench() {
+ k::QsaShapes s = k::qsa_real_shapes();
+ const int H = (int) s.n_head_kv, D = (int) s.head_dim, P = (int) s.page_size, G = D / k::KV_Q8_GROUP;
+ const int ctx = 32768, pages = ctx / P;
+ const long long rows = (long long) pages * H * P;
+ const int max_ids = (int) k::qsa_selection_width(ctx, s);
+ std::mt19937 rng(7);
+ std::vector<int8_t> kq((size_t) rows * D), vq((size_t) rows * D);
+ std::vector<uint16_t> ks((size_t) rows * G), vs((size_t) rows * G);
+ for (auto& x : kq) x = (int8_t) (rng() & 0xFF);
+ for (auto& x : vq) x = (int8_t) (rng() & 0xFF);
+ for (auto& x : ks) x = (uint16_t) (0x3000 | (rng() & 0x3FF));
+ for (auto& x : vs) x = (uint16_t) (0x3000 | (rng() & 0x3FF));
+ std::vector<int32_t> table(pages);
+ for (int i = 0; i < pages; ++i) table[i] = i;
+ std::shuffle(table.begin(), table.end(), rng);
+ std::vector<int32_t> ids(max_ids);
+ for (auto& x : ids) x = (int32_t) (rng() % ctx);
+ int8_t* d_kq = dalloc<int8_t>(kq.size()); ck(cudaMemcpy(d_kq, kq.data(), kq.size(), cudaMemcpyHostToDevice), "up");
+ int8_t* d_vq = dalloc<int8_t>(vq.size()); ck(cudaMemcpy(d_vq, vq.data(), vq.size(), cudaMemcpyHostToDevice), "up");
+ uint16_t* d_ks = dalloc<uint16_t>(ks.size()); ck(cudaMemcpy(d_ks, ks.data(), ks.size() * 2, cudaMemcpyHostToDevice), "up");
+ uint16_t* d_vs = dalloc<uint16_t>(vs.size()); ck(cudaMemcpy(d_vs, vs.data(), vs.size() * 2, cudaMemcpyHostToDevice), "up");
+ int32_t* d_table = dalloc<int32_t>(pages); ck(cudaMemcpy(d_table, table.data(), pages * 4, cudaMemcpyHostToDevice), "up");
+ int32_t* d_ids = dalloc<int32_t>(max_ids); ck(cudaMemcpy(d_ids, ids.data(), max_ids * 4, cudaMemcpyHostToDevice), "up");
+ int32_t* d_step = dalloc<int32_t>(k::kStepCount);
+ int32_t hstep[k::kStepCount] = {0, 0, 0, max_ids};
+ ck(cudaMemcpy(d_step, hstep, sizeof hstep, cudaMemcpyHostToDevice), "up");
+ uint16_t* k8 = dalloc<uint16_t>((size_t) max_ids * H * D);
+ uint16_t* v8 = dalloc<uint16_t>((size_t) max_ids * H * D);
+ for (int i = 0; i < 5; ++i) k::kv_gather_q8_step(d_kq, d_vq, d_ks, d_vs, d_table, d_ids, d_step, max_ids, s, k8, v8, nullptr);
+ ck(cudaDeviceSynchronize(), "warmup");
+ const int reps = 200;
+ cudaEvent_t e0, e1; cudaEventCreate(&e0); cudaEventCreate(&e1);
+ cudaEventRecord(e0);
+ for (int r = 0; r < reps; ++r) k::kv_gather_q8_step(d_kq, d_vq, d_ks, d_vs, d_table, d_ids, d_step, max_ids, s, k8, v8, nullptr);
+ cudaEventRecord(e1); ck(cudaEventSynchronize(e1), "time");
+ float ms = 0; cudaEventElapsedTime(&ms, e0, e1);
+ const double bytes = (double) max_ids * H * D * 2 // K + V codes
+ + (double) max_ids * H * G * 2 * 2 // K + V scales
+ + (double) max_ids * H * D * 2 * 2; // K + V fp16 scratch write
+ const double us = ms * 1000.0 / reps;
+ std::printf("kv_gather_q8 bench: %d ids, %.1f us/call, %.1f GB/s (%.2f MB moved)\n", max_ids, us,
+ bytes / (us * 1e-6) / 1e9, bytes / 1e6);
+ return 0;
+}
+
+int main(int argc, char** argv) {
+ for (int i = 1; i < argc; ++i) if (!std::strcmp(argv[i], "--bench")) return bench();
k::QsaShapes s = k::qsa_real_shapes();
s.page_size = 64; // several pages in a small pool
const int H = (int) s.n_head_kv, D = (int) s.head_dim, P = (int) s.page_size, G = D / k::KV_Q8_GROUP;