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

kernels (sm_70): the prompt attention's latency pipeline (mma884 kernel)

ATIVX9281e14e6469e26
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e28abd9b0..38c341279 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -400,6 +400,13 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
target_include_directories(qsa_prompt_attn_parity PRIVATE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
target_link_libraries(qsa_prompt_attn_parity PRIVATE strata_kernels CUDA::cudart)
endif()
+ # PR #600: the Volta (sm_70) prompt attention's latency pipeline against the FP32 kernel it replaces. Only in the
+ # experimental sm_60/sm_70 build, where prompt_attn_v70_kernel is compiled in.
+ if(STRATA_BUILD_TESTS AND STRATA_ENABLE_CUDA AND STRATA_EXPERIMENTAL_SM60)
+ add_executable(qsa_prompt_attn_sm70_bench bench/qsa_prompt_attn_sm70_bench.cpp)
+ target_include_directories(qsa_prompt_attn_sm70_bench PRIVATE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
+ target_link_libraries(qsa_prompt_attn_sm70_bench PRIVATE strata_kernels CUDA::cudart)
+ endif()
# #512: the top-k's active-block bound (Turing's dispatch, forced on any card by STRATA_TOPK_ACTIVE_ANY) against
# the capacity rule, selected ids bitwise
if(STRATA_BUILD_TESTS AND STRATA_ENABLE_CUDA)
diff --git a/bench/qsa_prompt_attn_sm70_bench.cpp b/bench/qsa_prompt_attn_sm70_bench.cpp
new file mode 100644
index 000000000..74f5475c9
--- /dev/null
+++ b/bench/qsa_prompt_attn_sm70_bench.cpp
@@ -0,0 +1,198 @@
+// bench/qsa_prompt_attn_sm70_bench.cpp - the Volta (sm_70) prompt attention against the FP32 kernel it replaces.
+//
+// Same synthetic pools and prompt-shaped selections as src/kernels/qsa_prompt_attn_parity.cpp (GPU, no model), but
+// timing is the point: for each KV format (int8, fp16) and a sweep of query counts it times
+// `qsa_decode_attn_batch` (the FP32 fallback, in batches of 32 as prefill.cpp calls it) against
+// `qsa_prompt_attn_batch` with CUDA events, and reports the output difference. Run it on one card:
+// flock /tmp/opencode/gpu.lock -c 'CUDA_VISIBLE_DEVICES=0 ./qsa_prompt_attn_sm70_bench [ctx=32768] [nq=2048] [reps=10]'
+#include "strata/kernels/qsa.hpp"
+#include "strata/kernels/qsa_decode_attn.hpp"
+#include "strata/kernels/qsa_prompt_attn.hpp"
+
+#include <cuda_fp16.h>
+#include <cuda_runtime.h>
+
+#include <algorithm>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <functional>
+#include <random>
+#include <vector>
+
+namespace k = strata::kernels;
+
+namespace {
+void ck(cudaError_t e, const char* w) {
+ if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e)); std::exit(2); }
+}
+template <typename T> T* up(const std::vector<T>& h) {
+ T* d = nullptr;
+ ck(cudaMalloc(&d, h.size() * sizeof(T) + 64), "malloc");
+ ck(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "upload");
+ return d;
+}
+uint16_t f2h(float f) { __half h = __float2half(f); return *reinterpret_cast<uint16_t*>(&h); }
+
+// one synthetic prompt: the widest selection the shape has (2,051 cells at 32K), a recent window plus older cells
+// that drift slowly from query to query, as qsa_prompt_attn_parity builds them. fmt 1 int8 KV, 0 fp16 KV,
+// 2 hybrid K8V4 (int8 K, q4_0 V - the Q2_0 engine's pools)
+void build(int fmt, int64_t ctx, int64_t nq, std::vector<int8_t>& kq, std::vector<int8_t>& vq, std::vector<uint16_t>& ks,
+ std::vector<uint16_t>& vs, std::vector<uint16_t>& kh, std::vector<uint16_t>& vh, std::vector<uint8_t>& vq4,
+ std::vector<int32_t>& table, std::vector<int32_t>& ids, std::vector<int32_t>& steps, std::vector<float>& q,
+ int64_t& cap) {
+ const k::QsaShapes s = k::qsa_real_shapes();
+ const int64_t HD = s.head_dim, NKV = s.n_head_kv, NH = s.n_head, PS = s.page_size;
+ const int64_t pages = (ctx + PS - 1) / PS, rows = pages * NKV * PS;
+ std::mt19937 rng(1234 + fmt);
+ std::normal_distribution<float> nd(0.f, 1.f);
+ std::uniform_int_distribution<int> code(-127, 127);
+ std::uniform_real_distribution<float> sc(0.005f, 0.03f);
+ if (fmt == 1 || fmt == 2) {
+ kq.resize(rows * HD); ks.resize(rows * 4);
+ for (auto& x : kq) x = (int8_t) code(rng);
+ for (auto& x : ks) x = f2h(sc(rng));
+ if (fmt == 1) {
+ vq.resize(rows * HD); vs.resize(rows * 4);
+ for (auto& x : vq) x = (int8_t) code(rng);
+ for (auto& x : vs) x = f2h(sc(rng));
+ } else {
+ vq4.resize((size_t) (rows * (HD / 32) * 18)); // q4_0 blocks: a finite fp16 scale + random nibbles
+ for (size_t i = 0; i < vq4.size(); i += 18) {
+ const uint16_t d = f2h(sc(rng));
+ vq4[i] = (uint8_t) d; vq4[i + 1] = (uint8_t) (d >> 8);
+ for (int j = 2; j < 18; ++j) vq4[i + j] = (uint8_t) rng();
+ }
+ }
+ } else {
+ kh.resize(rows * HD); vh.resize(rows * HD);
+ for (auto& x : kh) x = f2h(nd(rng) * 1.5f);
+ for (auto& x : vh) x = f2h(nd(rng));
+ }
+ table.resize(pages);
+ for (int64_t i = 0; i < pages; ++i) table[i] = (int32_t) i;
+ std::shuffle(table.begin(), table.end(), rng);
+ cap = k::qsa_selection_width(ctx, s);
+ ids.assign((size_t) (nq * cap), 0);
+ steps.assign((size_t) (nq * k::kStepCount), 0);
+ q.assign((size_t) (nq * NH * HD), 0.f);
+ for (auto& x : q) x = nd(rng) * 2.0f;
+ std::vector<int32_t> old_cells;
+ for (int64_t i = 0; i < nq; ++i) {
+ const int64_t pos = ctx - nq + i, nkv = pos + 1;
+ const int64_t w = k::qsa_selection_width(nkv, s);
+ int32_t* sel = ids.data() + i * cap;
+ steps[i * k::kStepCount + k::kStepWidth] = (int32_t) w;
+ if (w == nkv) {
+ for (int64_t c = 0; c < w; ++c) sel[c] = (int32_t) c;
+ continue;
+ }
+ const int64_t recent = 512, older = w - recent;
+ if ((int64_t) old_cells.size() != older) {
+ std::vector<int32_t> all((size_t) (nkv - recent));
+ for (int64_t c = 0; c < nkv - recent; ++c) all[c] = (int32_t) c;
+ std::shuffle(all.begin(), all.end(), rng);
+ old_cells.assign(all.begin(), all.begin() + older);
+ } else {
+ std::uniform_int_distribution<int64_t> pick(0, older - 1), any(0, nkv - recent - 1);
+ for (int r = 0; r < older / 32; ++r) {
+ const int32_t c = (int32_t) any(rng);
+ if (std::find(old_cells.begin(), old_cells.end(), c) == old_cells.end()) old_cells[pick(rng)] = c;
+ }
+ }
+ std::vector<int32_t> v(old_cells);
+ for (int64_t c = nkv - recent; c < nkv; ++c) v.push_back((int32_t) c);
+ std::sort(v.begin(), v.end());
+ std::copy(v.begin(), v.end(), sel);
+ }
+}
+
+float time_ms(const std::function<void()>& f, int reps) {
+ cudaEvent_t e0, e1;
+ ck(cudaEventCreate(&e0), "event");
+ ck(cudaEventCreate(&e1), "event");
+ f();
+ ck(cudaDeviceSynchronize(), "warm");
+ ck(cudaEventRecord(e0), "event");
+ for (int r = 0; r < reps; ++r) f();
+ ck(cudaEventRecord(e1), "event");
+ ck(cudaEventSynchronize(e1), "time");
+ float ms = 0;
+ ck(cudaEventElapsedTime(&ms, e0, e1), "time");
+ cudaEventDestroy(e0);
+ cudaEventDestroy(e1);
+ return ms / reps;
+}
+
+void run(int fmt, int64_t ctx, int64_t nq, int reps) {
+ const k::QsaShapes s = k::qsa_real_shapes();
+ const int64_t HD = s.head_dim, NH = s.n_head;
+ std::vector<int8_t> kq, vq;
+ std::vector<uint16_t> ks, vs, kh, vh;
+ std::vector<uint8_t> vq4;
+ std::vector<int32_t> table, ids, steps;
+ std::vector<float> q;
+ int64_t cap = 0;
+ build(fmt, ctx, nq, kq, vq, ks, vs, kh, vh, vq4, table, ids, steps, q, cap);
+ k::QsaAttnPools pl;
+ if (fmt == 1) { pl.k_q = up(kq); pl.v_q = up(vq); pl.k_scale = up(ks); pl.v_scale = up(vs); }
+ else if (fmt == 2) { pl.k_q = up(kq); pl.k_scale = up(ks); pl.v_q4 = up(vq4); }
+ else { pl.k_pool = up(kh); pl.v_pool = up(vh); }
+ pl.page_table = up(table);
+ const int32_t* d_ids = up(ids);
+ const int32_t* d_steps = up(steps);
+ const float* d_q = up(q);
+ float *d_old = nullptr, *d_new = nullptr, *scratch = nullptr;
+ const int64_t batch = 32;
+ ck(cudaMalloc(&d_old, nq * NH * HD * 4), "malloc");
+ ck(cudaMalloc(&d_new, nq * NH * HD * 4), "malloc");
+ ck(cudaMalloc(&scratch, batch * k::qsa_decode_attn_scratch_floats(cap, s) * 4), "malloc");
+ std::printf("# %s KV, ctx %lld, selection %lld cells, %lld queries of %lld heads x %lld dims\n",
+ fmt == 1 ? "int8" : fmt == 2 ? "int8+q4 V (K8V4)" : "fp16", (long long) ctx, (long long) cap,
+ (long long) nq, (long long) NH, (long long) HD);
+ const int64_t counts[] = {16, 64, 256, 1024, 2048, 4096};
+ for (int64_t nqi : counts) {
+ if (nqi > nq) continue;
+ auto old_run = [&]() {
+ for (int64_t t0 = 0; t0 < nqi; t0 += batch)
+ k::qsa_decode_attn_batch(d_q + t0 * NH * HD, pl, d_ids + t0 * cap, d_steps + t0 * k::kStepCount, cap,
+ s, scratch, d_old + t0 * NH * HD, std::min(batch, nqi - t0), nullptr);
+ };
+ auto new_run = [&]() {
+ if (!k::qsa_prompt_attn_batch(d_q, pl, d_ids, d_steps, cap, s, d_new, nqi, nullptr)) {
+ std::fprintf(stderr, "qsa_prompt_attn_batch refused the pools\n");
+ std::exit(2);
+ }
+ };
+ old_run(); new_run();
+ ck(cudaDeviceSynchronize(), "run");
+ std::vector<float> o((size_t) (nqi * NH * HD)), nw(o.size());
+ ck(cudaMemcpy(o.data(), d_old, o.size() * 4, cudaMemcpyDeviceToHost), "down");
+ ck(cudaMemcpy(nw.data(), d_new, nw.size() * 4, cudaMemcpyDeviceToHost), "down");
+ double diff = 0, scale = 0;
+ for (size_t i = 0; i < o.size(); ++i) {
+ diff = std::max(diff, (double) std::fabs(o[i] - nw[i]));
+ scale = std::max(scale, (double) std::fabs(o[i]));
+ }
+ const float ms_old = time_ms(old_run, reps), ms_new = time_ms(new_run, reps);
+ std::printf("q %5lld: fp32 %8.3f ms volta %8.3f ms %5.2fx max|old-new| %.3g (%.2g of scale)\n",
+ (long long) nqi, ms_old, ms_new, ms_old / ms_new, diff, scale > 0 ? diff / scale : 0.0);
+ }
+ cudaFree((void*) d_ids); cudaFree((void*) d_steps); cudaFree((void*) d_q); cudaFree(d_old); cudaFree(d_new);
+ cudaFree(scratch);
+ cudaFree((void*) pl.k_q); cudaFree((void*) pl.v_q); cudaFree((void*) pl.k_scale); cudaFree((void*) pl.v_scale);
+ cudaFree((void*) pl.v_q4);
+ cudaFree((void*) pl.k_pool); cudaFree((void*) pl.v_pool); cudaFree((void*) pl.page_table);
+}
+} // namespace
+
+int main(int argc, char** argv) {
+ const int64_t ctx = argc > 1 ? std::atoll(argv[1]) : 32768;
+ const int64_t nq = argc > 2 ? std::atoll(argv[2]) : 2048;
+ const int reps = argc > 3 ? std::atoi(argv[3]) : 10;
+ run(1, ctx, nq, reps);
+ run(2, ctx, nq, reps);
+ run(0, ctx, nq, reps);
+ return 0;
+}
diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cu
index bd99609db..8ec9acb3f 100644
--- a/src/kernels/cuda/qsa_prompt_attn.cu
+++ b/src/kernels/cuda/qsa_prompt_attn.cu
@@ -789,7 +789,7 @@ struct Smem70 {
float alpha[16];
float lsum[16];
float mrow[16];
- long long row[CH];
+ long long row[2][CH]; // double-buffered pool rows: the next chunk's lookups hide under p.v
};
template <int KV_MODE>
@@ -840,40 +840,54 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_v70_kernel(const float* _
#pragma unroll
for (int i = 0; i < 8; ++i) acc[rb][nt][i] = 0.0f;
- for (int c0 = 0; c0 < n; c0 += CH) {
- const int nh = min(CH, n - c0);
- if (t < CH) {
- long long r = -1;
- if (t < nh) {
- const int cell = ids[c0 + t];
- const long long page = (long long) p.page_table[cell / page_size];
- r = (page * n_kv_heads + kvh) * page_size + (cell % page_size);
- }
- S.row[t] = r;
+ // the pool rows of a chunk: -1 past the selection and for a page the KV streaming left non-resident (the
+ // decode kernel masks those, so this kernel does too). Chunk 0's rows fill one buffer here; every later
+ // chunk's lookups fill the other just before the previous p.v, where their global latency hides.
+ int rbuf = 0;
+ if (t < CH) {
+ long long r = -1;
+ if (t < min(CH, n)) {
+ const int cell = ids[t];
+ const long long page = (long long) p.page_table[cell / page_size];
+ r = (page * n_kv_heads + kvh) * page_size + (cell % page_size);
}
- __syncthreads(); // rows ready; the previous chunk's p.v is done with k, v, s
+ S.row[0][t] = r;
+ }
+ for (int c0 = 0; c0 < n; c0 += CH, rbuf ^= 1) {
+ const int nh = min(CH, n - c0);
+ __syncthreads(); // rows ready; the previous chunk's p.v is done with k, v
// gather the chunk's K and V rows (16-byte pieces; K8V4's V as q4_0 blocks dequantized to fp16)
// and their scales
{
constexpr int KPIECES = HD * (int) sizeof(typename Smem<KV_MODE>::KElem) / 16; // per K row
- for (int i = t; i < CH * KPIECES; i += THREADS) {
- const int c = i / KPIECES, pc = i % KPIECES;
- const long long r = S.row[c];
- uint4 kx = make_uint4(0, 0, 0, 0);
+ constexpr int KPT = (CH * KPIECES + THREADS - 1) / THREADS;
+ static_assert((CH * KPIECES) % THREADS == 0, "the staging pieces divide evenly");
+ // issue every global load of a thread before any of its stores: they then overlap each other instead
+ // of paying the HBM latency one at a time
+ uint4 kx[KPT];
+ int kc[KPT], kp[KPT];
+#pragma unroll
+ for (int s = 0; s < KPT; ++s) {
+ const int i = t + s * THREADS;
+ kc[s] = i / KPIECES; kp[s] = i % KPIECES;
+ const long long r = S.row[rbuf][kc[s]];
+ kx[s] = make_uint4(0, 0, 0, 0);
if (r >= 0) {
if constexpr (KV_MODE == 0)
- kx = __ldg(reinterpret_cast<const uint4*>(p.k_pool + r * HD) + pc);
+ kx[s] = __ldg(reinterpret_cast<const uint4*>(p.k_pool + r * HD) + kp[s]);
else // modes 1 and 3: the K side is INT8
- kx = __ldg(reinterpret_cast<const uint4*>(p.k_q + r * HD) + pc);
+ kx[s] = __ldg(reinterpret_cast<const uint4*>(p.k_q + r * HD) + kp[s]);
}
- *reinterpret_cast<uint4*>(reinterpret_cast<unsigned char*>(&S.k[c][0]) + pc * 16) = kx;
}
+#pragma unroll
+ for (int s = 0; s < KPT; ++s)
+ *reinterpret_cast<uint4*>(reinterpret_cast<unsigned char*>(&S.k[kc[s]][0]) + kp[s] * 16) = kx[s];
if constexpr (KV_MODE == 3) { // V: dequantize the row's q4_0 blocks straight into the fp16 V row
constexpr int BLKS = HD / QK4_0;
constexpr int BYTES = BLKS * (int) sizeof(block_q4_0);
for (int i = t; i < CH * BLKS; i += THREADS) {
const int c = i / BLKS, b = i % BLKS;
- const long long r = S.row[c];
+ const long long r = S.row[rbuf][c];
#pragma unroll
for (int j = 0; j < QK4_0; ++j) S.v[c][b * QK4_0 + j] = __half(0);
if (r >= 0) {
@@ -889,22 +903,30 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_v70_kernel(const float* _
}
} else {
constexpr int VPIECES = HD * (int) sizeof(typename Smem<KV_MODE>::VElem) / 16; // per V row
- for (int i = t; i < CH * VPIECES; i += THREADS) {
- const int c = i / VPIECES, pc = i % VPIECES;
- const long long r = S.row[c];
- uint4 vx = make_uint4(0, 0, 0, 0);
+ constexpr int VPT = (CH * VPIECES + THREADS - 1) / THREADS;
+ static_assert((CH * VPIECES) % THREADS == 0, "the staging pieces divide evenly");
+ uint4 vx[VPT];
+ int vc[VPT], vp[VPT];
+#pragma unroll
+ for (int s = 0; s < VPT; ++s) {
+ const int i = t + s * THREADS;
+ vc[s] = i / VPIECES; vp[s] = i % VPIECES;
+ const long long r = S.row[rbuf][vc[s]];
+ vx[s] = make_uint4(0, 0, 0, 0);
if (r >= 0) {
if constexpr (KV_MODE == 1)
- vx = __ldg(reinterpret_cast<const uint4*>(p.v_q + r * HD) + pc);
+ vx[s] = __ldg(reinterpret_cast<const uint4*>(p.v_q + r * HD) + vp[s]);
else
- vx = __ldg(reinterpret_cast<const uint4*>(p.v_pool + r * HD) + pc);
+ vx[s] = __ldg(reinterpret_cast<const uint4*>(p.v_pool + r * HD) + vp[s]);
}
- *reinterpret_cast<uint4*>(reinterpret_cast<unsigned char*>(&S.v[c][0]) + pc * 16) = vx;
}
+#pragma unroll
+ for (int s = 0; s < VPT; ++s)
+ *reinterpret_cast<uint4*>(reinterpret_cast<unsigned char*>(&S.v[vc[s]][0]) + vp[s] * 16) = vx[s];
}
for (int i = t; i < CH * 4; i += THREADS) {
const int c = i / 4, g = i % 4;
- const long long r = S.row[c];
+ const long long r = S.row[rbuf][c];
float a = 0.0f, b = 0.0f;
if (r >= 0) {
if constexpr (KV_MODE == 1) {
@@ -1067,6 +1089,17 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_v70_kernel(const float* _
acc[rb][nt][i] = fmaf(acc[rb][nt][i], a_[(i >> 1) & 1], (tmp[rb][nt][i] + tmpl[rb][nt][i]) * vdown);
}
}
+ // the next chunk's pool rows, into the other buffer: the two global lookups per cell hide under p.v
+ if (c0 + CH < n && t < CH) {
+ long long r = -1;
+ const int idx = c0 + CH + t;
+ if (idx < n) {
+ const int cell = ids[idx];
+ const long long page = (long long) p.page_table[cell / page_size];
+ r = (page * n_kv_heads + kvh) * page_size + (cell % page_size);
+ }
+ S.row[rbuf ^ 1][t] = r;
+ }
}
__syncthreads();
#pragma unroll