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++
HIP: several warp sums as one transposed butterfly, bitwise (opt-in STRATA_TSUM=1)
Niko12212a1c54e7da68
diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cuindex 2d53ffde1..dfca46c67 100644--- a/src/kernels/cuda/fused_gr.cu+++ b/src/kernels/cuda/fused_gr.cu@@ -3,6 +3,7 @@#include "strata/kernels/fused_gr.hpp"#include "strata/kernels/bf16_bits.hpp"#include "strata/kernels/verify_kernels.hpp"+#include "s26_tsum.cuh"#include <cuda_fp16.h>#include <cuda_runtime.h>@@ -1046,12 +1047,241 @@ __global__ void __launch_bounds__(THREADS) gr_up_q8_kernel(GrMulti m, const floaif (m.a[0].q8_mixed != nullptr) gr_q8_tail(m, d0); // S26 STRATA_QFUSE}++// S26 STRATA_TSUM=1: gr_down_q8_kernel / gr_up_q8_kernel with each row's T per-token warp sums (and the sums of+// squares) as one transposed butterfly (s26_tsum.cuh): bitwise the same values (S26 ws4 harness, every output equal,+// T 1-4: 1.00 / 1.03 / 1.03 / 1.15x per read). PF (all rows' weights in registers before the part / lo phase) was+// slower (0.92-1.03x) and is off.+using s26ts::tsum;+using s26ts::tsum_token;+using s26ts::tsum_lane;+using s26ts::pow2_ceil;+// gr_down_q8_kernel / gr_up_q8_kernel with every per-token warp sum done as one transposed butterfly per row (tsum)+template <int T, int RPW = Q8_RPW>+__global__ void __launch_bounds__(THREADS) gr_down_q8_fast_kernel(GrMulti m, float* __restrict__ part, float* __restrict__ ssg) {+ constexpr int P = pow2_ceil(T);+ constexpr int RG_ = LR / (WARPS * RPW);+ __shared__ __align__(16) float xs[T][Q8_KC];+ __shared__ float red[WARPS][T];+ const int t = threadIdx.x, lane = t & 31, warp = t >> 5;+ const int rg = blockIdx.x, kc = blockIdx.y, c = kc / Q8_CPS;+ const bool inj = rg == RG_;+ const bool inj_bf16 = inj && m.a[0].q8_inject == nullptr;+ const int nrows = inj ? (((m.a[0].q8_inject != nullptr || m.a[0].w_inject != nullptr) && warp < HC) ? 1 : 0) : RPW;+ const int row0 = inj ? warp : (rg * WARPS + warp) * RPW;+ const uint8_t* wb = inj ? m.a[0].q8_inject : m.a[0].q8_down;+ const int sub = lane & 7, bl = lane >> 3;+ const int tk = tsum_token<P>(lane);+ const bool owner = lane == tsum_lane<P>(tk) && tk < T;+ uint32_t q[RPW][Q8_SPB];+ float dq[RPW][Q8_SPB];+#pragma unroll+ for (int r = 0; r < RPW; ++r) {+ if (r >= nrows || inj_bf16) break;+ const uint8_t* row = wb + (size_t) (row0 + r) * (D / 32) * Q8B + (size_t) kc * (Q8_KC / 32) * Q8B;+#pragma unroll+ for (int s = 0; s < Q8_SPB; ++s) {+ const uint8_t* blk = row + (4 * s + bl) * Q8B;+ dq[r][s] = q8_half(q8_ld16(blk));+ q[r][s] = q8_ld16(blk + 2 + 4 * sub) | q8_ld16(blk + 4 + 4 * sub) << 16;+ }+ }+ float ssp[P];+#pragma unroll+ for (int k = 0; k < P; ++k) ssp[k] = 0.0f;+#pragma unroll+ for (int k = 0; k < T; ++k) {+ const FusedGrArgs& a = m.a[k];+ const float gw = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f;+ for (int i = t; i < Q8_KC / 4; i += THREADS) {+ const int col = kc * Q8_KC + 4 * i;+ float4 r = *reinterpret_cast<const float4*>(a.R + col);+ if (a.apply) {+ const float4 b = *reinterpret_cast<const float4*>(a.bo_prev + (col - c * N));+ r.x = fmaf(b.x, gw, r.x); r.y = fmaf(b.y, gw, r.y);+ r.z = fmaf(b.z, gw, r.z); r.w = fmaf(b.w, gw, r.w);+ }+ const float4 g = *reinterpret_cast<const float4*>(a.w_norm + col);+ ssp[k] += r.x * r.x + r.y * r.y + r.z * r.z + r.w * r.w;+ *reinterpret_cast<float4*>(&xs[k][4 * i]) = make_float4(r.x * g.x, r.y * g.y, r.z * g.z, r.w * g.w);+ }+ }+ {+ const float v = tsum<P>(ssp, lane);+ if (owner) red[warp][tk] = v;+ }+ __syncthreads();+ if (rg == 0 && t < T) {+ float s = 0.0f;+ for (int w = 0; w < WARPS; ++w) s += red[w][t];+ ssg[t * Q8_NKC + kc] = s;+ }+ if (nrows == 0) return;+#pragma unroll+ for (int r = 0; r < RPW; ++r) {+ if (r >= nrows) break;+ float acc[P];+#pragma unroll+ for (int k = 0; k < P; ++k) acc[k] = 0.0f;+#pragma unroll+ for (int s = 0; s < Q8_SPB; ++s) {+ const int v = 32 * (4 * s + bl) + 4 * sub;+ float4 w;+ if (inj_bf16) {+ const uint2 b = *reinterpret_cast<const uint2*>(m.a[0].w_inject + (size_t) row0 * D + (size_t) kc * Q8_KC + v);+ w = make_float4(__uint_as_float(b.x << 16), __uint_as_float(b.x & 0xffff0000u),+ __uint_as_float(b.y << 16), __uint_as_float(b.y & 0xffff0000u));+ } else {+ w = q8_four(q[r][s], dq[r][s]);+ }+#pragma unroll+ for (int k = 0; k < T; ++k) {+ const float4 x = *reinterpret_cast<const float4*>(&xs[k][v]);+ acc[k] = fmaf(w.x, x.x, acc[k]); acc[k] = fmaf(w.y, x.y, acc[k]);+ acc[k] = fmaf(w.z, x.z, acc[k]); acc[k] = fmaf(w.w, x.w, acc[k]);+ }+ }+ const int prow = inj ? LR + warp : row0 + r;+ const float v = tsum<P>(acc, lane);+ if (owner) part[((size_t) tk * Q8_NKC + kc) * PR + prow] = v;+ }+}++template <int T, bool PF = false>+__global__ void __launch_bounds__(THREADS) gr_up_q8_fast_kernel(GrMulti m, const float* __restrict__ part,+ const float* __restrict__ ssg) {+ constexpr int P = pow2_ceil(T);+ constexpr int RPW8 = HC * UPM_COLS / WARPS;+ __shared__ __align__(16) float lo[T][LR];+ __shared__ float rsS[T][HC];+ __shared__ float g[T][HC][UPM_COLS];+ const int t = threadIdx.x, lane = t & 31, warp = t >> 5;+ const int d0 = blockIdx.x * UPM_COLS;+ const int tk = tsum_token<P>(lane);+ const bool owner = lane == tsum_lane<P>(tk) && tk < T;+ // PF: every row's weights in registers before the part / lo phase (its latency hides the weight read)+ uint32_t pq[PF ? RPW8 : 1][3];+ float pd[PF ? RPW8 : 1][3];+ if constexpr (PF) {+#pragma unroll+ for (int qq = 0; qq < RPW8; ++qq) {+ const int r = warp + qq * WARPS;+ const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd;+ const uint8_t* row = m.a[0].q8_up + (size_t) i * (LR / 32) * Q8B;+#pragma unroll+ for (int s = 0; s < 3; ++s) {+ const int v = 4 * lane + 128 * s;+ if (v < LR) {+ const uint8_t* blk = row + (v >> 5) * Q8B;+ pq[qq][s] = q8_ld16(blk + 2 + (v & 31)) | q8_ld16(blk + 4 + (v & 31)) << 16;+ pd[qq][s] = q8_half(q8_ld16(blk));+ } else {+ pq[qq][s] = 0u; pd[qq][s] = 0.0f;+ }+ }+ }+ }+ if (t < T * HC) {+ const int k = t / HC, c = t - k * HC;+ float ss = 0.0f;+#pragma unroll+ for (int j = 0; j < Q8_CPS; ++j) ss += ssg[k * Q8_NKC + c * Q8_CPS + j];+ const float r = rsqrtf(ss / (float) N + m.a[k].eps);+ rsS[k][c] = r;+ if (blockIdx.x == 0) m.a[k].rs[c] = r;+ }+ __syncthreads();+ for (int i = t; i < T * PR; i += THREADS) {+ const int k = i / PR, r = i - k * PR;+ if (r >= LR && (blockIdx.x != 0 || m.a[k].w_inject == nullptr)) continue;+ float sum = 0.0f;+#pragma unroll+ for (int c = 0; c < HC; ++c) {+ float p = 0.0f;+#pragma unroll+ for (int j = 0; j < Q8_CPS; ++j) p += part[((size_t) k * Q8_NKC + c * Q8_CPS + j) * PR + r];+ sum = fmaf(rsS[k][c], p, sum);+ }+ if (r < LR) {+ const float x = sum / (float) HC;+ lo[k][r] = x / (1.0f + __expf(-x));+ } else {+ m.a[k].inject_out[r - LR] = sum;+ }+ }+ __syncthreads();+#pragma unroll(PF ? RPW8 : 2)+ for (int qq = 0; qq < RPW8; ++qq) {+ const int r = warp + qq * WARPS;+ const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd;+ const uint8_t* row = m.a[0].q8_up + (size_t) i * (LR / 32) * Q8B;+ float4 w[3];+#pragma unroll+ for (int s = 0; s < 3; ++s) {+ const int v = 4 * lane + 128 * s;+ if (PF) {+ w[s] = v < LR ? q8_four(pq[PF ? qq : 0][s], pd[PF ? qq : 0][s]) : make_float4(0.0f, 0.0f, 0.0f, 0.0f);+ } else if (v < LR) {+ const uint8_t* blk = row + (v >> 5) * Q8B;+ w[s] = q8_four(q8_ld16(blk + 2 + (v & 31)) | q8_ld16(blk + 4 + (v & 31)) << 16, q8_half(q8_ld16(blk)));+ } else {+ w[s] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);+ }+ }+ float rv = 0.0f, wn = 0.0f, bo = 0.0f, ip = 0.0f;+ bool apply = false;+ if (owner) {+ const FusedGrArgs& a = m.a[tk];+ rv = a.R[i];+ wn = a.w_norm[i];+ apply = a.apply;+ if (apply) { bo = a.bo_prev[d0 + dd]; ip = a.inj_prev[c]; }+ }+ float acc[P];+#pragma unroll+ for (int k = 0; k < P; ++k) acc[k] = 0.0f;+#pragma unroll+ for (int k = 0; k < T; ++k) {+#pragma unroll+ for (int s = 0; s < 3; ++s) {+ const int v = 4 * lane + 128 * s;+ if (v < LR) {+ const float4 x = *reinterpret_cast<const float4*>(&lo[k][v]);+ acc[k] = fmaf(w[s].x, x.x, acc[k]); acc[k] = fmaf(w[s].y, x.y, acc[k]);+ acc[k] = fmaf(w[s].z, x.z, acc[k]); acc[k] = fmaf(w[s].w, x.w, acc[k]);+ }+ }+ }+ const float mine = tsum<P>(acc, lane);+ if (owner) {+ if (apply) {+ rv = fmaf(bo, 2.0f * sigmoidf_(ip / (float) HC), rv);+ m.a[tk].R_out[i] = rv;+ }+ const float x = rv * wn * rsS[tk][c];+ g[tk][c][dd] = x * sigmoidf_(mine);+ }+ }+ __syncthreads();+ for (int i = t; i < T * UPM_COLS; i += THREADS) {+ const int k = i / UPM_COLS, col = i - k * UPM_COLS;+ float sum = 0.0f;+#pragma unroll+ for (int c = 0; c < HC; ++c) sum += g[k][c][col];+ m.a[k].mixed[d0 + col] = sum / (float) HC;+ }+ if (m.a[0].q8_mixed != nullptr) gr_q8_tail(m, d0); // S26 STRATA_QFUSE+}+template <int T> void launch_q8_t(const GrMulti& m, float* part, float* ssg, cudaStream_t st, unsigned long long* stamp_buf,int stamp_i0) {+ static const bool ts = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }();if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st);- gr_down_q8_kernel<T><<<dim3(Q8_RG + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);+ if (ts) gr_down_q8_fast_kernel<T, 4><<<dim3(LR / (WARPS * 4) + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);+ else gr_down_q8_kernel<T><<<dim3(Q8_RG + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st);- gr_up_q8_kernel<T><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);+ if (ts) gr_up_q8_fast_kernel<T, false><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);+ else gr_up_q8_kernel<T><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);}void launch_q8(const GrMulti& m, float* scratch, cudaStream_t st, unsigned long long* stamp_buf, int stamp_i0) {float* part = scratch;diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cuindex 4a493403c..eaaed00d7 100644--- a/src/kernels/cuda/iq_kernels.cu+++ b/src/kernels/cuda/iq_kernels.cu@@ -6,6 +6,7 @@// included unchanged.#include "strata/kernels/iq_kernels.hpp"#include "strata/kernels/dp4a.hpp"+#include "s26_tsum.cuh"#include <cuda_fp16.h>#include <cuda_runtime.h>@@ -1395,7 +1396,7 @@ struct S26IQ3S { // Split<21>::load with the grid from `grid` (LT) or iq3s_griconstexpr int S26_XMAX = 2560 / 32; // q8_1 blocks of one activation row (n_embd 2560)-template<bool LT, bool LX, int RPW, bool SL = false>+template<bool LT, bool LX, int RPW, bool SL = false, bool TS = false>__global__ void __launch_bounds__(256) s26_gu_l_kernel(const unsigned long long* __restrict__ grp_ptr,const int32_t* __restrict__ grp_start,const int32_t* __restrict__ n_groups,@@ -1460,6 +1461,21 @@ __global__ void __launch_bounds__(256) s26_gu_l_kernel(const unsigned long long*for (int c = 0; c < GRP_NC; ++c)if (c < n) s[q][c] += Split<21>::apply(w[q], xbase + off[c] + kbx * 8, iqs);}+ if constexpr (TS) { // S26: all RPW x GRP_NC sums in one transposed butterfly (bitwise the same sums)+ constexpr int P = s26ts::pow2_ceil(RPW * GRP_NC);+ float v[P];+#pragma unroll+ for (int j = 0; j < P; ++j) v[j] = j < RPW * GRP_NC ? s[j / GRP_NC][j % GRP_NC] : 0.0f;+ const float sum = s26ts::tsum<P>(v, lane);+ const int j = s26ts::tsum_token<P>(lane);+ if (lane == s26ts::tsum_lane<P>(j) && j < RPW * GRP_NC) {+ const int c = j % GRP_NC;+#pragma unroll+ for (int q = 0; q < RPW; ++q)+ if (j / GRP_NC == q && c < n) (upq[q] ? up : gate)[(size_t) (e + c) * L.n_ff + rr[q]] = sum;+ }+ continue;+ }#pragma unrollfor (int q = 0; q < RPW; ++q)#pragma unroll@@ -1475,7 +1491,7 @@ __global__ void __launch_bounds__(256) s26_gu_l_kernel(const unsigned long long*constexpr int S26_HMAX = 640 / 32; // q8_1 blocks of one SwiGLU row (n_ff 640)-template<bool LX, int RPW, bool SL = false>+template<bool LX, int RPW, bool SL = false, bool TS = false>__global__ void __launch_bounds__(256) s26_down_l_kernel(const unsigned long long* __restrict__ grp_ptr,const int32_t* __restrict__ grp_start,const int32_t* __restrict__ n_groups,@@ -1529,6 +1545,18 @@ __global__ void __launch_bounds__(256) s26_down_l_kernel(const unsigned long lonfor (int c = 0; c < GRP_NC; ++c)if (c < n) s[q][c] += Split<20>::apply(w[q], hbase + off[c] + kbx, iqs);}+ if constexpr (TS) { // S26: all RPW x GRP_NC sums in one transposed butterfly (bitwise the same sums)+ constexpr int P = s26ts::pow2_ceil(RPW * GRP_NC);+ float v[P];+#pragma unroll+ for (int j = 0; j < P; ++j) v[j] = j < RPW * GRP_NC ? s[j / GRP_NC][j % GRP_NC] : 0.0f;+ const float sum = s26ts::tsum<P>(v, lane);+ const int j = s26ts::tsum_token<P>(lane);+ const int q = j / GRP_NC, c = j % GRP_NC;+ if (lane == s26ts::tsum_lane<P>(j) && j < RPW * GRP_NC && c < n)+ out[(size_t) ent_dst[e + c] * L.n_embd + r0 + 8 * q] = sum;+ continue;+ }#pragma unrollfor (int q = 0; q < RPW; ++q)#pragma unroll@@ -1565,12 +1593,12 @@ __global__ void s26_swiglu_q8_1_kernel(const float* __restrict__ gate, const floy[ib].qs[iqs] = q;if (iqs == 0) y[ib].ds = make_half2(d, sum);}-template<bool LT, bool LX, int RG, int RD, bool SL = false, bool FQ = false>+template<bool LT, bool LX, int RG, int RD, bool SL = false, bool FQ = false, bool TS = false>void s26_launch_l(const NativeExpertLayout& L, int64_t cap_groups, cudaStream_t s, const unsigned long long* grp_ptr,const int32_t* grp_start, const int32_t* n_groups, const int32_t* ent_dst, const int32_t* ent_tok,const block_q8_1* X, float* gate, float* up, float* h, block_q8_1* hq, float* out, long long nh) {const dim3 ggu((unsigned) (2 * L.n_ff / (GU_ROWS * RG)), (unsigned) cap_groups);- s26_gu_l_kernel<LT, LX, RG, SL><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);+ s26_gu_l_kernel<LT, LX, RG, SL, TS><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);if (FQ) {s26_swiglu_q8_1_kernel<<<(unsigned) ((nh + 255) / 256), 256, 0, s>>>(gate, up, h, hq, nh);} else {@@ -1578,7 +1606,7 @@ void s26_launch_l(const NativeExpertLayout& L, int64_t cap_groups, cudaStream_tquantize_q8_1_kernel<<<(unsigned) ((nh + 255) / 256), 256, 0, s>>>(h, hq, nh);}const dim3 gd((unsigned) (L.n_embd / (8 * RD)), (unsigned) cap_groups);- s26_down_l_kernel<LX, RD, SL><<<gd, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);+ s26_down_l_kernel<LX, RD, SL, TS><<<gd, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);}template<int TG>void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, const int32_t* grp_start,@@ -1726,8 +1754,12 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long longconst auto* X = (const block_q8_1*) x_q8_1;static const bool v2 = [] { const char* v = std::getenv("STRATA_EXPERT_V2"); return v && v[0] == '1'; }();if (v2 && L.gu_type == 21 && L.d_type == 20 && L.n_embd == 2560 && L.n_ff == 640) { // S26: see s26_gu_l_kernel- s26_launch_l<true, true, 4, 4, true, true>(L, cap_groups, s, grp_ptr, grp_start, n_groups, ent_dst, ent_tok, X, gate, up, h,- hq, out, (long long) cap_entries * L.n_ff);+ // S26 STRATA_TSUM=1: the sums as one transposed butterfly per warp (s26_tsum.cuh; bitwise the same values)+ static const bool ts = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }();+ if (ts) s26_launch_l<true, true, 4, 4, true, true, true>(L, cap_groups, s, grp_ptr, grp_start, n_groups, ent_dst, ent_tok, X, gate, up, h,+ hq, out, (long long) cap_entries * L.n_ff);+ else s26_launch_l<true, true, 4, 4, true, true>(L, cap_groups, s, grp_ptr, grp_start, n_groups, ent_dst, ent_tok, X, gate, up, h,+ hq, out, (long long) cap_entries * L.n_ff);check("native_expert_grouped (v2)");return;}diff --git a/src/kernels/cuda/native_bf16.cu b/src/kernels/cuda/native_bf16.cuindex 57b89f093..ce83fd2bc 100644--- a/src/kernels/cuda/native_bf16.cu+++ b/src/kernels/cuda/native_bf16.cu@@ -1,4 +1,5 @@#include "strata/kernels/bf16_gemv.hpp"+#include "s26_tsum.cuh"#include "strata/kernels/bf16_bits.hpp"#include <cuda_runtime.h>@@ -123,7 +124,7 @@ __global__ void bf16_f32_mmvf_multi_kernel(const float* __restrict__ x, int64_t// rows again (n_out blocks x T x n_in floats through L2 - the part that grew with T); here a block reads the// activations once for RPB rows. Per output, thread t still walks pairs t, t + BLOCK_SIZE, ... with the same two// ordered FMAs and the same warp and block reductions: bit-identical to bf16_f32_mmvf_multi_kernel.-template <int BLOCK_SIZE, int NT, int RPB>+template <int BLOCK_SIZE, int NT, int RPB, bool TS = false>__global__ void bf16_f32_mmvf_rows_kernel(const float* __restrict__ x, int64_t ldx, const uint16_t* __restrict__ w,float* __restrict__ y, int64_t ldy, int n_in, int n_out, int n_tok) {const int t = threadIdx.x;@@ -161,6 +162,26 @@ __global__ void bf16_f32_mmvf_rows_kernel(const float* __restrict__ x, int64_t l}}}+ if constexpr (TS) { // S26 STRATA_TSUM=1: the RPB x NT warp sums (both stages) as transposed butterflies, bitwise the same+ constexpr int V = RPB * NT, P = s26ts::pow2_ceil(V);+ const int lane = t & 31, j = s26ts::tsum_token<P>(lane);+ const bool own = lane == s26ts::tsum_lane<P>(j) && j < V;+ float v[P];+#pragma unroll+ for (int q = 0; q < P; ++q) v[q] = q < V ? acc[q / NT][q % NT] : 0.0f;+ float sum = s26ts::tsum<P>(v, lane);+ if constexpr (BLOCK_SIZE > 32) {+ if (own) partials[j / NT][j % NT][t / 32] = sum;+ __syncthreads();+ if (t >= 32) return;+#pragma unroll+ for (int q = 0; q < P; ++q) v[q] = q < V ? partials[q / NT][q % NT][t] : 0.0f;+ sum = s26ts::tsum<P>(v, lane);+ }+ const int r = j / NT, k = j % NT;+ if (own && o0 + r < n_out && k < n_tok) y[(size_t) k * ldy + o0 + r] = sum;+ return;+ }#pragma unrollfor (int r = 0; r < RPB; ++r)#pragma unroll@@ -192,6 +213,12 @@ void launch_rows(const float* x, int64_t ldx, const uint16_t* w, float* y, int64cudaStream_t st) {constexpr int RPB = 4;const unsigned nb = (unsigned) ((n_out + RPB - 1) / RPB);+ static const bool ts = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }();+ if (ts) {+ if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<B, 4, RPB, true><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok);+ else bf16_f32_mmvf_rows_kernel<B, 8, RPB, true><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok);+ return;+ }if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<B, 4, RPB><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok);else bf16_f32_mmvf_rows_kernel<B, 8, RPB><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok);}diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cuindex 175ca08d1..673a68677 100644--- a/src/kernels/cuda/native_mmvq.cu+++ b/src/kernels/cuda/native_mmvq.cu@@ -26,6 +26,7 @@#include "strata/kernels/native_mmvq.hpp"#include "strata/kernels/dp4a.hpp"#include "strata/kernels/iq_kernels.hpp"+#include "s26_tsum.cuh"#include <cuda_fp16.h>#include <cuda_runtime.h>@@ -1002,7 +1003,13 @@ struct SmallTraits {// equal to ncols = 1 only to float rounding (the cross-warp reduction groups partial sums differently).bool g_multi_exact = true; // until the upstream layout is timed on an idle GPU (plan rule: default only what is measured)-template<typename F, int NCOLS, int NW, int ROWS>+// S26 STRATA_TSUM=1 (TS): warp 0's NCOLS x ROWS sums as one transposed butterfly (s26_tsum.cuh), bitwise the same+static bool s26_tsum_on() {+ static const bool on = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }();+ return on;+}++template<typename F, int NCOLS, int NW, int ROWS, bool TS = false>__launch_bounds__(NW * WARP, 1)__global__ void native_mmvq_multi_kernel(const typename F::Block* __restrict__ w,const Q81Block* __restrict__ x,@@ -1036,6 +1043,25 @@ __global__ void native_mmvq_multi_kernel(const typename F::Block* __restrict__ w}__syncthreads();if (threadIdx.y > 0) return;+ if constexpr (TS) {+ constexpr int V = NCOLS * ROWS, P = s26ts::pow2_ceil(V);+ float v[P];+#pragma unroll+ for (int j = 0; j < NCOLS; ++j)+#pragma unroll+ for (int i = 0; i < ROWS; ++i) {+#pragma unroll+ for (int l = 0; l < NW - 1; ++l) tmp[j][i] += partial[l][j][i][threadIdx.x];+ v[j * ROWS + i] = tmp[j][i];+ }+#pragma unroll+ for (int k = V; k < P; ++k) v[k] = 0.0f;+ const float sum = s26ts::tsum<P>(v, int(threadIdx.x));+ const int k = s26ts::tsum_token<P>(int(threadIdx.x));+ const int j = k / ROWS, i = k % ROWS;+ if (int(threadIdx.x) == s26ts::tsum_lane<P>(k) && k < V && row0 + i < n_out) y[std::size_t(j) * n_out + row0 + i] = sum;+ return;+ }#pragma unrollfor (int j = 0; j < NCOLS; ++j) {#pragma unroll@@ -1061,9 +1087,11 @@ void launch_multi_n(const void* weights, const void* x_q8_1, float* y, int n_in,const dim3 threads(WARP, WARPS);if (n_in / F::DIV < F::BPI) {const unsigned blocks = unsigned((std::size_t(n_out) + WARPS - 1) / WARPS);- native_mmvq_multi_kernel<F, NCOLS, WARPS, WARPS><<<blocks, threads, 0, s>>>(w, x, y, n_in, n_out);+ if (s26_tsum_on()) native_mmvq_multi_kernel<F, NCOLS, WARPS, WARPS, true><<<blocks, threads, 0, s>>>(w, x, y, n_in, n_out);+ else native_mmvq_multi_kernel<F, NCOLS, WARPS, WARPS><<<blocks, threads, 0, s>>>(w, x, y, n_in, n_out);} else {- native_mmvq_multi_kernel<F, NCOLS, WARPS, 1><<<unsigned(n_out), threads, 0, s>>>(w, x, y, n_in, n_out);+ if (s26_tsum_on()) native_mmvq_multi_kernel<F, NCOLS, WARPS, 1, true><<<unsigned(n_out), threads, 0, s>>>(w, x, y, n_in, n_out);+ else native_mmvq_multi_kernel<F, NCOLS, WARPS, 1><<<unsigned(n_out), threads, 0, s>>>(w, x, y, n_in, n_out);}}@@ -1166,7 +1194,7 @@ void small_f32(const void* weights, const float* x, void* scratch_q8_1,constexpr int Q8P_PERSIST_GRID = 640;constexpr int Q8P_PERSIST_MAX_IN = 4096;-template<int NCOLS>+template<int NCOLS, bool TS = false>__launch_bounds__(WARPS * WARP, 1)__global__ void native_q8_0_packed_kernel(const int8_t* __restrict__ qs, const half* __restrict__ dpl,const Q81Block* __restrict__ x, float* __restrict__ y,@@ -1200,7 +1228,21 @@ __global__ void native_q8_0_packed_kernel(const int8_t* __restrict__ qs, const hfor (int j = 0; j < NCOLS; ++j) partial[buf][threadIdx.y - 1][j][threadIdx.x] = tmp[j];}__syncthreads();- if (threadIdx.y == 0) {+ if (TS && threadIdx.y == 0) { // S26 STRATA_TSUM=1: one transposed butterfly, bitwise the same sums+ constexpr int P = s26ts::pow2_ceil(NCOLS);+ float v[P];+#pragma unroll+ for (int j = 0; j < NCOLS; ++j) {+#pragma unroll+ for (int l = 0; l < WARPS - 1; ++l) tmp[j] += partial[buf][l][j][threadIdx.x];+ v[j] = tmp[j];+ }+#pragma unroll+ for (int j = NCOLS; j < P; ++j) v[j] = 0.0f;+ const float sum = s26ts::tsum<P>(v, int(threadIdx.x));+ const int j = s26ts::tsum_token<P>(int(threadIdx.x));+ if (int(threadIdx.x) == s26ts::tsum_lane<P>(j) && j < NCOLS) y[std::size_t(j) * n_out + row] = sum;+ } else if (threadIdx.y == 0) {#pragma unrollfor (int j = 0; j < NCOLS; ++j) {#pragma unroll@@ -1222,7 +1264,11 @@ template<int NCOLS>void q8_packed_launch(const Q8Packed& w, const Q81Block* x, float* y, cudaStream_t s) {const bool persist = w.n_in <= Q8P_PERSIST_MAX_IN;const int grid = persist ? (std::min)(w.n_out, Q8P_PERSIST_GRID) : w.n_out;- native_q8_0_packed_kernel<NCOLS><<<unsigned(grid), dim3(WARP, WARPS), 0, s>>>(w.qs, w.d, x, y, w.n_in, w.n_out,+ if (s26_tsum_on())+ native_q8_0_packed_kernel<NCOLS, true><<<unsigned(grid), dim3(WARP, WARPS), 0, s>>>(w.qs, w.d, x, y, w.n_in, w.n_out,+ persist ? grid : w.n_out);+ else+ native_q8_0_packed_kernel<NCOLS><<<unsigned(grid), dim3(WARP, WARPS), 0, s>>>(w.qs, w.d, x, y, w.n_in, w.n_out,persist ? grid : w.n_out);}@@ -1272,7 +1318,7 @@ int q6p_grid() {return grid;}-template<int NCOLS>+template<int NCOLS, bool TS = false>__launch_bounds__(WARPS * WARP, 1)__global__ void native_q6_k_packed_kernel(const int* __restrict__ ql, const int* __restrict__ qh,const int8_t* __restrict__ sc, const half* __restrict__ dpl,@@ -1315,7 +1361,21 @@ __global__ void native_q6_k_packed_kernel(const int* __restrict__ ql, const int*for (int j = 0; j < NCOLS; ++j) partial[buf][threadIdx.y - 1][j][threadIdx.x] = tmp[j];}__syncthreads();- if (threadIdx.y == 0) {+ if (TS && threadIdx.y == 0) { // S26 STRATA_TSUM=1: one transposed butterfly, bitwise the same sums+ constexpr int P = s26ts::pow2_ceil(NCOLS);+ float v[P];+#pragma unroll+ for (int j = 0; j < NCOLS; ++j) {+#pragma unroll+ for (int l = 0; l < WARPS - 1; ++l) tmp[j] += partial[buf][l][j][threadIdx.x];+ v[j] = tmp[j];+ }+#pragma unroll+ for (int j = NCOLS; j < P; ++j) v[j] = 0.0f;+ const float sum = s26ts::tsum<P>(v, int(threadIdx.x));+ const int j = s26ts::tsum_token<P>(int(threadIdx.x));+ if (int(threadIdx.x) == s26ts::tsum_lane<P>(j) && j < NCOLS) y[std::size_t(j) * n_out + row] = sum;+ } else if (threadIdx.y == 0) {#pragma unrollfor (int j = 0; j < NCOLS; ++j) {#pragma unroll@@ -1339,7 +1399,11 @@ std::unordered_map<const void*, Q6Packed>& q6_packed_registry() {template<int NCOLS>void q6_packed_launch(const Q6Packed& w, const Q81Block* x, float* y, cudaStream_t s) {const int grid = (std::min)(w.n_out, q6p_grid());- native_q6_k_packed_kernel<NCOLS><<<unsigned(grid), dim3(WARP, WARPS), 0, s>>>(w.ql, w.qh, w.sc, w.d, x, y, w.n_in,+ if (s26_tsum_on())+ native_q6_k_packed_kernel<NCOLS, true><<<unsigned(grid), dim3(WARP, WARPS), 0, s>>>(w.ql, w.qh, w.sc, w.d, x, y, w.n_in,+ w.n_out, grid);+ else+ native_q6_k_packed_kernel<NCOLS><<<unsigned(grid), dim3(WARP, WARPS), 0, s>>>(w.ql, w.qh, w.sc, w.d, x, y, w.n_in,w.n_out, grid);}diff --git a/src/kernels/cuda/s26_tsum.cuh b/src/kernels/cuda/s26_tsum.cuhnew file mode 100644index 000000000..38cfc97c0--- /dev/null+++ b/src/kernels/cuda/s26_tsum.cuh@@ -0,0 +1,69 @@+// S26: several warp sums at once (P per-lane values -> P sums), bitwise equal to P separate xor-butterfly warp sums+// (`for (o = 16; o > 0; o >>= 1) v += shfl_xor(v, o)`). At offset 16 a lane keeps half of its P values and adds the+// partner's copy of that half, at 8 half again, ... until one value is left, then plain xor steps for the remaining+// offsets. Every level adds the same pair of values as the butterfly does (fp add commutes, so x + y == y + x+// bitwise), only each lane carries fewer of them: P - 1 + (5 - log2 P) cross-lane moves instead of 5 P. Value j's sum+// ends in the lanes whose bits 4, 3, .. (MSB first) spell j: tsum_lane<P>(j) is one of them. On AMD the moves are+// DPP row_xmask / permlanex16 instead of ds_bpermute.+#pragma once++namespace strata::kernels::s26ts {++#if defined(__HIP_PLATFORM_AMD__)+__device__ __forceinline__ float xmov16(float v) {+ return __int_as_float(__builtin_amdgcn_permlanex16(__float_as_int(v), __float_as_int(v), 0x76543210u, 0xfedcba98u,+ false, false));+}+template <int O>+__device__ __forceinline__ float xmov(float v) {+ if constexpr (O == 16) return xmov16(v);+ else return __int_as_float(__builtin_amdgcn_update_dpp(0, __float_as_int(v), 0x160 + O, 0xf, 0xf, false));+}+#else+template <int O>+__device__ __forceinline__ float xmov(float v) { return __shfl_xor_sync(0xffffffffu, v, O); }+#endif++constexpr int pow2_ceil(int t) { return t <= 1 ? 1 : t <= 2 ? 2 : t <= 4 ? 4 : t <= 8 ? 8 : t <= 16 ? 16 : 32; }+constexpr int log2c(int p) { return p <= 1 ? 0 : p == 2 ? 1 : p == 4 ? 2 : p == 8 ? 3 : p == 16 ? 4 : 5; }++template <int O>+__device__ __forceinline__ float tplain(float x) {+ if constexpr (O >= 1) { x += xmov<O>(x); return tplain<O / 2>(x); }+ else return x;+}+// v[0..CNT) (CNT a power of two <= 32): this lane's values; returns the sum of value tsum_token<CNT>(lane)+template <int CNT, int O = 16>+__device__ __forceinline__ float tsum(float* v, int lane) {+ if constexpr (CNT > 1) {+ constexpr int H = CNT / 2;+ const bool hi = (lane & O) != 0;+#pragma unroll+ for (int j = 0; j < H; ++j) {+ const float send = hi ? v[j] : v[j + H];+ const float keep = hi ? v[j + H] : v[j];+ v[j] = keep + xmov<O>(send);+ }+ return tsum<H, O / 2>(v, lane);+ } else {+ return tplain<O>(v[0]);+ }+}+template <int P>+__device__ __forceinline__ int tsum_token(int lane) {+ constexpr int L = log2c(P);+ int k = 0;+#pragma unroll+ for (int b = 0; b < L; ++b) k = (k << 1) | ((lane >> (4 - b)) & 1);+ return k;+}+template <int P>+__device__ __forceinline__ int tsum_lane(int k) {+ constexpr int L = log2c(P);+ int lane = 0;+#pragma unroll+ for (int b = 0; b < L; ++b) lane |= ((k >> (L - 1 - b)) & 1) << (4 - b);+ return lane;+}++} // namespace strata::kernels::s26ts