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++
perf(qsa): cut the attention kernel's shuffles and q shared-memory traffic, bit-exact
Károly Sárdi6a4b1265c6b4
diff --git a/src/kernels/cuda/qsa_decode_attn.cu b/src/kernels/cuda/qsa_decode_attn.cuindex 4035999a9..f508ec22c 100644--- a/src/kernels/cuda/qsa_decode_attn.cu+++ b/src/kernels/cuda/qsa_decode_attn.cu@@ -80,6 +80,57 @@ __device__ __forceinline__ void load8(const QsaAttnPools& p, bool value, long lo} else load8_q4(p, value, row, d0, out);}+// The 12 heads' lane sums (`part[12..15]` zero), reduce-scattered with warp_sum's pairing order (xor 16, 8, 4, 2, 1):+// every output adds the same two operands at every level, so each sum is bit for bit warp_sum's (float add commutes),+// for 16 shuffles instead of 12 x 5. Lane l ends holding head head_of_lane(l); lanes l and l^1 agree.+__device__ __forceinline__ float reduce12(const float (&part)[16], int lane) {+ float r8[8];+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const bool hi = (lane & 16) != 0;+ r8[i] = (hi ? part[i + 8] : part[i]) + __shfl_xor_sync(0xffffffffu, hi ? part[i] : part[i + 8], 16);+ }+ float r4[4];+#pragma unroll+ for (int i = 0; i < 4; ++i) {+ const bool hi = (lane & 8) != 0;+ r4[i] = (hi ? r8[i + 4] : r8[i]) + __shfl_xor_sync(0xffffffffu, hi ? r8[i] : r8[i + 4], 8);+ }+ float r2[2];+#pragma unroll+ for (int i = 0; i < 2; ++i) {+ const bool hi = (lane & 4) != 0;+ r2[i] = (hi ? r4[i + 2] : r4[i]) + __shfl_xor_sync(0xffffffffu, hi ? r4[i] : r4[i + 2], 4);+ }+ const bool hi2 = (lane & 2) != 0;+ const float r1 = (hi2 ? r2[1] : r2[0]) + __shfl_xor_sync(0xffffffffu, hi2 ? r2[0] : r2[1], 2);+ return r1 + __shfl_xor_sync(0xffffffffu, r1, 1);+}+__device__ __forceinline__ int head_of_lane(int lane) {+ return ((lane >> 4) & 1) * 8 + ((lane >> 3) & 1) * 4 + ((lane >> 2) & 1) * 2 + ((lane >> 1) & 1);+}++// One V element of pool row `row`, dimension `d` (the value loop's own thread index), per KV format.+template <int KV_MODE>+__device__ __forceinline__ float load_v1(const QsaAttnPools& p, long long row, int d) {+ if constexpr (KV_MODE == 0) {+ return __half2float(__ushort_as_half(p.v_pool[row * HD + d]));+ } else if constexpr (KV_MODE == 1) {+ const float sc = __half2float(__ushort_as_half(p.v_scale[row * (HD / KV_Q8_GROUP) + d / KV_Q8_GROUP]));+ return (float) p.v_q[row * HD + d] * sc;+ } else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back+ constexpr int bytes_per_head = (HD / QK4_0) * sizeof(block_q4_0);+ const int b = d / QK4_0;+ const int rem = d % QK4_0;+ const block_q4_0* blk = reinterpret_cast<const block_q4_0*>(p.v_q4 + row * bytes_per_head) + b;+ const float dd = __half2float(__ushort_as_half(blk->d));+ const int j = rem < 16 ? rem : (rem - 16);+ const uint8_t byte = blk->qs[j];+ const int nibble = (rem < 16) ? ((byte & 0x0F) - 8) : ((byte >> 4) - 8);+ return (float) nibble * dd;+ }+}+template <int KV_MODE>__global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __restrict__ q, QsaAttnPools p,const int32_t* __restrict__ ids,@@ -95,7 +146,8 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __restpart_m += (size_t) blockIdx.z * (size_t) scratch_stride;part_l += (size_t) blockIdx.z * (size_t) scratch_stride;__shared__ __align__(16) float sq[G][HD]; // 12 KB: this KV head's query heads- __shared__ float sp[G][CHUNK]; // scores, then probabilities+ __shared__ float sp[G][CHUNK]; // scores+ __shared__ __align__(16) float spt[CHUNK][G]; // probabilities, cell-major: the value loop reads one cell's 12 as three float4__shared__ long long srow[CHUNK]; // pool row of each cell (page, kv head, slot)const int n_ids = __ldg(step + kStepWidth);const int chunk = blockIdx.x, kvh = blockIdx.y;@@ -120,22 +172,63 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __restsrow[t] = r;}__syncthreads();- // scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions.- for (int c = warp; c < CHUNK; c += WARPS) {- if (c >= n_here || srow[c] < 0) {- if (lane < G) sp[lane][c] = -FLT_MAX;- continue;+ // scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. NC cells per step+ // (warp + 8 * (i + j)): a lane's q slice comes out of shared memory once for all of them, which cuts the traffic+ // that bounds this phase (24 x 16 B per lane per cell, against the FMAs) by NC; every dot product is the same+ // expression as for a single cell, so the scores are unchanged.+ constexpr int NC = 2; // 4 measured slower on a V100 (the registers cost occupancy)+ const int hd = head_of_lane(lane);+ for (int i = 0; i < CHUNK / WARPS; i += NC) {+ int cc[NC];+ bool all_ok = true;+#pragma unroll+ for (int j = 0; j < NC; ++j) {+ cc[j] = warp + WARPS * (i + j);+ all_ok = all_ok && cc[j] < n_here && srow[cc[j]] >= 0;}- float k8[8];- load8<KV_MODE>(p, false, srow[c], lane * 8, k8);+ if (all_ok) {+ float kk[NC][8];+#pragma unroll+ for (int j = 0; j < NC; ++j) load8<KV_MODE>(p, false, srow[cc[j]], lane * 8, kk[j]);+ float pp[NC][16];+#pragma unroll+ for (int h = 0; h < G; ++h) {+ const float4 qa = *reinterpret_cast<const float4*>(&sq[h][lane * 8]);+ const float4 qb = *reinterpret_cast<const float4*>(&sq[h][lane * 8 + 4]);+#pragma unroll+ for (int j = 0; j < NC; ++j)+ pp[j][h] = kk[j][0] * qa.x + kk[j][1] * qa.y + kk[j][2] * qa.z + kk[j][3] * qa.w ++ kk[j][4] * qb.x + kk[j][5] * qb.y + kk[j][6] * qb.z + kk[j][7] * qb.w;+ }+#pragma unroll+ for (int j = 0; j < NC; ++j) {#pragma unroll- for (int h = 0; h < G; ++h) {- const float4 qa = *reinterpret_cast<const float4*>(&sq[h][lane * 8]);- const float4 qb = *reinterpret_cast<const float4*>(&sq[h][lane * 8 + 4]);- float s = k8[0] * qa.x + k8[1] * qa.y + k8[2] * qa.z + k8[3] * qa.w +- k8[4] * qb.x + k8[5] * qb.y + k8[6] * qb.z + k8[7] * qb.w;- s = warp_sum(s);- if (lane == 0) sp[h][c] = s * scale;+ for (int h = G; h < 16; ++h) pp[j][h] = 0.0f;+ const float sj = reduce12(pp[j], lane);+ if ((lane & 1) == 0 && hd < G) sp[hd][cc[j]] = sj * scale;+ }+ } else {+ for (int k = 0; k < NC; ++k) { // a masked or out-of-range cell in the group: one cell at a time+ const int c = cc[k];+ if (c >= n_here || srow[c] < 0) {+ if (lane < G) sp[lane][c] = -FLT_MAX;+ continue;+ }+ float k8[8];+ load8<KV_MODE>(p, false, srow[c], lane * 8, k8);+ float part[16];+#pragma unroll+ for (int h = 0; h < G; ++h) {+ const float4 qa = *reinterpret_cast<const float4*>(&sq[h][lane * 8]);+ const float4 qb = *reinterpret_cast<const float4*>(&sq[h][lane * 8 + 4]);+ part[h] = k8[0] * qa.x + k8[1] * qa.y + k8[2] * qa.z + k8[3] * qa.w ++ k8[4] * qb.x + k8[5] * qb.y + k8[6] * qb.z + k8[7] * qb.w;+ }+#pragma unroll+ for (int h = G; h < 16; ++h) part[h] = 0.0f;+ const float s = reduce12(part, lane);+ if ((lane & 1) == 0 && hd < G) sp[hd][c] = s * scale;+ }}}__syncthreads();@@ -145,8 +238,8 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __restconst float m = warp_max(fmaxf(a, b));const float ea = (lane < n_here && srow[lane] >= 0) ? __expf(a - m) : 0.0f;const float eb = (lane + 32 < n_here && srow[lane + 32] >= 0) ? __expf(b - m) : 0.0f;- sp[h][lane] = ea;- sp[h][lane + 32] = eb;+ spt[lane][h] = ea;+ spt[lane + 32][h] = eb;const float l = warp_sum(ea + eb);if (lane == 0) { part_m[slot * G + h] = m; part_l[slot * G + h] = l; }}@@ -155,27 +248,34 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __restfloat acc[G];#pragma unrollfor (int h = 0; h < G; ++h) acc[h] = 0.0f;- for (int c = 0; c < n_here; ++c) {+ // One cell's 12 weights (three broadcast float4 loads) into the 12 accumulators; the cells are folded in+ // ascending order, so the sums are the plain loop's, bit for bit.+ const auto fold = [&](int c, float v) {+ const float4* w4 = reinterpret_cast<const float4*>(spt[c]);+ const float4 w0 = w4[0], w1 = w4[1], w2 = w4[2];+ acc[0] = fmaf(w0.x, v, acc[0]); acc[1] = fmaf(w0.y, v, acc[1]);+ acc[2] = fmaf(w0.z, v, acc[2]); acc[3] = fmaf(w0.w, v, acc[3]);+ acc[4] = fmaf(w1.x, v, acc[4]); acc[5] = fmaf(w1.y, v, acc[5]);+ acc[6] = fmaf(w1.z, v, acc[6]); acc[7] = fmaf(w1.w, v, acc[7]);+ acc[8] = fmaf(w2.x, v, acc[8]); acc[9] = fmaf(w2.y, v, acc[9]);+ acc[10] = fmaf(w2.z, v, acc[10]); acc[11] = fmaf(w2.w, v, acc[11]);+ };+ int c = 0;+ // four cells at a time with their V loads issued together (the loop was one dependent load chain per cell);+ // a group holding a masked cell (page -1, rare) leaves this loop and the scalar one below finishes the chunk.+ for (; c + 4 <= n_here; c += 4) {+ const long long r0 = srow[c], r1 = srow[c + 1], r2 = srow[c + 2], r3 = srow[c + 3];+ if ((r0 | r1 | r2 | r3) < 0) break;+ const float v0 = load_v1<KV_MODE>(p, r0, t), v1 = load_v1<KV_MODE>(p, r1, t);+ const float v2 = load_v1<KV_MODE>(p, r2, t), v3 = load_v1<KV_MODE>(p, r3, t);+ fold(c, v0);+ fold(c + 1, v1);+ fold(c + 2, v2);+ fold(c + 3, v3);+ }+ for (; c < n_here; ++c) {if (srow[c] < 0) continue; // masked above, weight 0- float v;- if constexpr (KV_MODE == 0) {- v = __half2float(__ushort_as_half(p.v_pool[srow[c] * HD + t]));- } else if constexpr (KV_MODE == 1) {- const float sc = __half2float(__ushort_as_half(p.v_scale[srow[c] * (HD / KV_Q8_GROUP) + t / KV_Q8_GROUP]));- v = (float) p.v_q[srow[c] * HD + t] * sc;- } else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back- constexpr int bytes_per_head = (HD / QK4_0) * sizeof(block_q4_0);- const int b = t / QK4_0;- const int rem = t % QK4_0;- const block_q4_0* blk = reinterpret_cast<const block_q4_0*>(p.v_q4 + srow[c] * bytes_per_head) + b;- const float d = __half2float(__ushort_as_half(blk->d));- const int j = rem < 16 ? rem : (rem - 16);- const uint8_t byte = blk->qs[j];- const int nibble = (rem < 16) ? ((byte & 0x0F) - 8) : ((byte >> 4) - 8);- v = (float) nibble * d;- }-#pragma unroll- for (int h = 0; h < G; ++h) acc[h] = fmaf(sp[h][c], v, acc[h]);+ fold(c, load_v1<KV_MODE>(p, srow[c], t));}#pragma unrollfor (int h = 0; h < G; ++h) part_acc[((size_t) slot * G + h) * HD + t] = acc[h];