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

perf(quantize): one-warp-per-block Q8_0 scaled quantize and fused SwiGLU + Q8_0 in the grouped S2 experts (#783 PR-k)

Niko12213281ac326d46
diff --git a/src/kernels/cuda/quantize_act.cu b/src/kernels/cuda/quantize_act.cu
index 0e8e03a5a..9d8d9b116 100644
--- a/src/kernels/cuda/quantize_act.cu
+++ b/src/kernels/cuda/quantize_act.cu
@@ -100,31 +100,32 @@ __global__ void quantize_q8_0_kernel(const float* __restrict__ x, uint8_t* __res
#if defined(__HIPCC__) // AMD keeps the thread-a-block kernel
__global__ void quantize_q8_0_scaled_kernel(const float* __restrict__ x, uint8_t* __restrict__ blocks,
float* __restrict__ scales, long long n_blocks) {
- const long long b = (long long) blockIdx.x * blockDim.x + threadIdx.x;
+ const long long b = ((long long) blockIdx.x * blockDim.x + threadIdx.x) >> 5;
if (b >= n_blocks) return;
- const float* xb = x + b * QK8_0;
+ const int lane = threadIdx.x & 31;
+ const float xv = x[b * QK8_0 + lane];
uint8_t* out = blocks + b * 34;
- float amax = 0.0f;
- for (int i = 0; i < QK8_0; ++i) amax = fmaxf(amax, fabsf(xb[i]));
+ float amax = fabsf(xv);
+#pragma unroll
+ for (int off = 16; off > 0; off >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, off));
// VERBATIM from `cpu/expert.cpp:144-145`, including the `amax > 0` guard, so the fp32 value written here
// is bit-identical to the `s` the CPU path used.
const float s = amax > 0.f ? amax / 127.f : 0.f;
const float inv = s > 0.f ? 1.f / s : 0.f;
- scales[b] = s;
-
- const uint16_t d16bits = f16_from_f32(s);
- out[0] = (uint8_t) (d16bits & 0xFF);
- out[1] = (uint8_t) (d16bits >> 8);
- for (int i = 0; i < QK8_0; ++i) {
- // VERBATIM from `cpu/expert.cpp:159-162`: reciprocal multiply, then `t + copysign(0.5, t)` truncated
- // toward zero, which is `lround`'s rule - round half away from zero.
- const float t = xb[i] * inv;
- const float r = t + (t >= 0.f ? 0.5f : -0.5f);
- int v = (int) r;
- v = v < -127 ? -127 : (v > 127 ? 127 : v);
- out[2 + i] = (uint8_t) (int8_t) v;
+ if (lane == 0) {
+ scales[b] = s;
+ const uint16_t d16bits = f16_from_f32(s);
+ out[0] = (uint8_t) (d16bits & 0xFF);
+ out[1] = (uint8_t) (d16bits >> 8);
}
+ // VERBATIM from `cpu/expert.cpp:159-162`: reciprocal multiply, then `t + copysign(0.5, t)` truncated
+ // toward zero, which is `lround`'s rule - round half away from zero.
+ const float t = xv * inv;
+ const float r = t + (t >= 0.f ? 0.5f : -0.5f);
+ int v = (int) r;
+ v = v < -127 ? -127 : (v > 127 ? 127 : v);
+ out[2 + lane] = (uint8_t) (int8_t) v;
}
#else
diff --git a/src/kernels/cuda/s2_expert_grouped.cu b/src/kernels/cuda/s2_expert_grouped.cu
index 925ac3a9e..bb8890299 100644
--- a/src/kernels/cuda/s2_expert_grouped.cu
+++ b/src/kernels/cuda/s2_expert_grouped.cu
@@ -15,6 +15,7 @@
// the same reason. `bench/micro/moe_hit_parity.cu` is the check.
#include "strata/kernels/s2_expert_grouped.hpp"
#include "strata/kernels/dp4a.hpp"
+#include "strata/kernels/f16_bits.hpp"
#include "strata/kernels/quantize_act.hpp"
#include "strata/kernels/verify_kernels.hpp"
@@ -171,6 +172,37 @@ __global__ void swiglu_kernel(float* __restrict__ gate_up, long long n_pairs) {
gate_up[i] = (g / (1.0f + __expf(-g))) * u;
}
+__global__ void swiglu_quantize_q8_0_scaled_kernel(float* __restrict__ gate_up, long long n_pairs,
+ uint8_t* __restrict__ blocks, float* __restrict__ scales) {
+ const long long n_blocks = n_pairs >> 5;
+ const long long b = ((long long) blockIdx.x * blockDim.x + threadIdx.x) >> 5;
+ if (b >= n_blocks) return;
+ const int lane = threadIdx.x & 31;
+ const long long idx = b * 32 + lane;
+ const float g = gate_up[idx];
+ const float u = gate_up[n_pairs + idx];
+ const float xv = (g / (1.0f + __expf(-g))) * u;
+ gate_up[idx] = xv;
+ uint8_t* out = blocks + b * 34;
+
+ float amax = fabsf(xv);
+#pragma unroll
+ for (int off = 16; off > 0; off >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, off));
+ const float s = amax > 0.f ? amax / 127.f : 0.f;
+ const float inv = s > 0.f ? 1.f / s : 0.f;
+ if (lane == 0) {
+ scales[b] = s;
+ const uint16_t d16bits = f16_from_f32(s);
+ out[0] = (uint8_t) (d16bits & 0xFF);
+ out[1] = (uint8_t) (d16bits >> 8);
+ }
+ const float t = xv * inv;
+ const float r = t + (t >= 0.f ? 0.5f : -0.5f);
+ int v = (int) r;
+ v = v < -127 ? -127 : (v > 127 ? 127 : v);
+ out[2 + lane] = (uint8_t) (int8_t) v;
+}
+
/// DOWN, ONE WARP PER ROW, reading the quantized intermediate the caller produced.
///
/// `dst_index[h]` is which row of the shared output buffer hit `h` fills - see the header. It is the router's
@@ -1117,13 +1149,27 @@ void moe_grouped_s2(const unsigned long long* grp_ptr, const int32_t* grp_start,
(int) cap_entries);
check("moe_grouped_s2/gu", stream);
}
- {
+ // #783 PR-k (stuchapin909): SwiGLU and the scaled Q8_0 quantize of its result in one kernel (gate_up still gets the
+ // activation written back, so the parity checks read the same buffer); STRATA_S2_SWIGLU_Q8=0 keeps the two launches
+ static const bool swiglu_q8 = [] {
+ const char* e = std::getenv("STRATA_S2_SWIGLU_Q8");
+ return e == nullptr || e[0] != '0';
+ }();
+ static_assert(FF % 32 == 0, "the fused SwiGLU + Q8_0 kernel works in whole 32-value blocks");
+ if (fast && x_scales != nullptr && swiglu_q8) {
+ const long long pairs = cap_entries * (long long) FF;
+ const long long n_blocks = pairs >> 5;
+ const int warps = THREADS / 32;
+ swiglu_quantize_q8_0_scaled_kernel<<<(unsigned) ((n_blocks + warps - 1) / warps), THREADS, 0, cs>>>(
+ gate_up, pairs, h_q8_0, h_scales);
+ check("moe_grouped_s2/swiglu_q8", stream);
+ } else {
const long long pairs = cap_entries * (long long) FF;
swiglu_kernel<<<(unsigned) ((pairs + THREADS - 1) / THREADS), THREADS, 0, cs>>>(gate_up, pairs);
check("moe_grouped_s2/swiglu", stream);
+ if (x_scales != nullptr) quantize_q8_0_scaled(gate_up, h_q8_0, h_scales, cap_entries * (int64_t) FF, stream);
+ else quantize_q8_0(gate_up, h_q8_0, cap_entries * (int64_t) FF, stream);
}
- if (x_scales != nullptr) quantize_q8_0_scaled(gate_up, h_q8_0, h_scales, cap_entries * (int64_t) FF, stream);
- else quantize_q8_0(gate_up, h_q8_0, cap_entries * (int64_t) FF, stream);
{
const dim3 grid((unsigned) (H / D_ROWS), (unsigned) cap_groups);
const float* hs = x_scales != nullptr ? h_scales : nullptr;