HIP: the grouped decode experts, IQ3_S gate/up + IQ4_NL down, faster and bitwise (opt-in STRATA_EXPERT_V2=1)

native_expert_grouped for the UD-IQ4_XS shapes (n_embd 2560, n_ff 640): s26_gu_l_kernel / s26_down_l_kernel keep
every row's lane-strided k order, Split<TY>::apply expression and warp_sum, so every output is bitwise the old one's;
a warp interleaves 4 rows (more loads in flight), the chunk's activation q8_1 rows are read from LDS, and for IQ3_S
the 512-entry grid table too (the same values); SwiGLU and its q8_1 run as one launch (contraction off in the
quantizer). Other formats / shapes keep native_gu_multi_kernel / native_down_multi_kernel.

gfx1151 harness (S26 ws3: real expert blobs, 32 experts x 4 layers = 298 MB, decode-style windows planned as
resident_plan does, memcmp of every output, paired HIP events): one layer's experts T=1 140.3 -> 133.3 us, T=2
225.1 -> 204.1, T=3 300.2 -> 265.9 (166 -> 175-185 GB/s). Tried and rejected: non-temporal loads (no gain), whole
rows staged in LDS (0.6-0.8x: occupancy), 8 rows per warp for down.
Engine (UD-IQ4_XS, c-best3 env + the switch, same binary, S25's 18 prompts): outputs 18/18 identical; 56.6 / 57.0
-> 55.65 / 55.27 (and 55.61 with the fused SwiGLU q8_1) ms per verify window, 48.6-48.9 -> 50.3 tok/s;
59K prompts 2/2 identical, 58.65 -> 56.77 / 56.66 ms per window, 39.70 -> 41.01 / 41.09 tok/s.
This commit is contained in:
Niko1221 committed 2026-10-03 15:33:07 +00:00
1 parent db43f526d0
commit 59a7e22fc2
1 file changed
+281
+281
View File
@@ -1306,6 +1306,280 @@ void launch_mmvq(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int
else mmvq_multi_kernel<TY, 8><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); // 8 at a time past 8
}
// ---------------------------------------------------------------- S26 STRATA_EXPERT_V2=1 (opt-in): IQ3_S gate/up + IQ4_NL down
// Bitwise equal to native_gu_multi_kernel / native_down_multi_kernel (S26 harness, real UD-IQ4_XS expert blobs, T 1-3,
// memcmp of every output): each row's lane sums and warp_sum are the same; a warp interleaves RPW rows (more loads in
// flight) and reads the chunk's activation rows from LDS and, for IQ3_S, the grid table from LDS (the same values).
template<bool NT> __device__ __forceinline__ int ld16i(const uint16_t* p) {
if constexpr (NT) return (int) __builtin_nontemporal_load(p); else return (int) *p;
}
template<bool NT> __device__ __forceinline__ int ldb2(const void* x, int i32) {
const uint16_t* x16 = (const uint16_t*) x;
int x32 = ld16i<NT>(x16 + 2 * i32 + 0) << 0;
x32 |= ld16i<NT>(x16 + 2 * i32 + 1) << 16;
return x32;
}
template<bool NT> __device__ __forceinline__ int ld8i(const uint8_t* p) {
if constexpr (NT) return (int) __builtin_nontemporal_load(p); else return (int) *p;
}
template<bool NT> __device__ __forceinline__ float ldhalf(const half* p) {
return __half2float(__ushort_as_half((unsigned short) ld16i<NT>(reinterpret_cast<const uint16_t*>(p))));
}
template<int TY, bool NT> struct S26Split;
template<bool NT> struct S26Split<21, NT> { // IQ3_S: Split<21>::load with the loads made explicit
using W = Split<21>::W;
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq3_s* bq3 = (const block_iq3_s*) vbq + kbx;
const int2 qs_packed = make_int2(ldb2<NT>(bq3->qs, iqs + 0), ldb2<NT>(bq3->qs, iqs + 1));
const uint8_t* qs = (const uint8_t*) &qs_packed;
const int qh = ld8i<NT>(bq3->qh + iqs / 2);
const int signs_packed_32 = ldb2<NT>(bq3->signs, iqs / 2);
const uint8_t* signs_packed_8 = (const uint8_t*) &signs_packed_32;
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const int2 grid_pos = make_int2(iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],
iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);
const int signs0 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x03) << 7) | ((signs_packed_8[l0 / 2] & 0x0C) << 21), 0x00000000);
const int signs1 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x30) << 3) | ((signs_packed_8[l0 / 2] & 0xC0) << 17), 0x00000000);
r.g[l0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
r.g[l0 + 1] = __vsub4(grid_pos.y ^ signs1, signs1);
}
r.ls = 1 + 2 * ((ld8i<NT>(bq3->scales + iqs / 4) >> ((iqs << 1) & 0x04)) & 0x0F);
r.dw = ldhalf<NT>(&bq3->d);
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ b, int iqs) { return Split<21>::apply(r, b, iqs); }
};
template<bool NT> struct S26Split<20, NT> { // IQ4_NL
using W = Split<20>::W;
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq4_nl* bq4 = (const block_iq4_nl*) vbq + kbx;
W r;
#pragma unroll
for (int l = 0; l < 2; ++l) r.v[l] = get_int_from_table_16(ldb2<NT>(bq4->qs, iqs + l), kvalues_iq4nl);
r.dw = ldhalf<NT>(&bq4->d);
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ b, int iqs) { return Split<20>::apply(r, b, iqs); }
};
// S26 v13+: v6 (4 interleaved rows per warp) with the IQ3_S grid table (LT) and / or the chunk's activation rows (LX)
// read from LDS: the same table values and the same q8_1 bytes, so the same arithmetic and bits.
struct S26IQ3S { // Split<21>::load with the grid from `grid` (LT) or iq3s_grid itself
using W = Split<21>::W;
template<bool LT = true>
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* grid) {
const block_iq3_s* bq3 = (const block_iq3_s*) vbq + kbx;
const int2 qs_packed = make_int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1));
const uint8_t* qs = (const uint8_t*) &qs_packed;
const int qh = bq3->qh[iqs / 2];
const int signs_packed_32 = get_int_b2(bq3->signs, iqs / 2);
const uint8_t* signs_packed_8 = (const uint8_t*) &signs_packed_32;
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const int i0 = qs[l0 + 0] | ((qh << (8 - l0)) & 0x100), i1 = qs[l0 + 1] | ((qh << (7 - l0)) & 0x100);
const int2 grid_pos = LT ? make_int2(grid[i0], grid[i1]) : make_int2(iq3s_grid[i0], iq3s_grid[i1]);
const int signs0 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x03) << 7) | ((signs_packed_8[l0 / 2] & 0x0C) << 21), 0x00000000);
const int signs1 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x30) << 3) | ((signs_packed_8[l0 / 2] & 0xC0) << 17), 0x00000000);
r.g[l0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
r.g[l0 + 1] = __vsub4(grid_pos.y ^ signs1, signs1);
}
r.ls = 1 + 2 * ((bq3->scales[iqs / 4] >> ((iqs << 1) & 0x04)) & 0x0F);
r.dw = __half2float(bq3->d);
return r;
}
};
constexpr 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>
__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,
const int32_t* __restrict__ ent_tok,
const block_q8_1* __restrict__ xq, NativeExpertLayout L,
float* __restrict__ gate, float* __restrict__ up) {
__shared__ uint32_t s_grid[LT ? 512 : 1];
__shared__ block_q8_1 s_x[LX ? GRP_NC * S26_XMAX : 1];
const int g = blockIdx.y;
if (g >= *n_groups) return;
if constexpr (LT)
for (int i = threadIdx.x; i < 512; i += 256) s_grid[i] = iq3s_grid[i];
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int row0 = blockIdx.x * GU_ROWS * RPW + warp;
const bool live = row0 + GU_ROWS * (RPW - 1) < 2 * L.n_ff;
const uint8_t* blob = (const uint8_t*) grp_ptr[g];
const uint8_t* wr[RPW];
int rr[RPW];
bool upq[RPW];
#pragma unroll
for (int q = 0; q < RPW; ++q) {
const int row = row0 + q * GU_ROWS;
upq[q] = row >= L.n_ff;
rr[q] = upq[q] ? row - (int) L.n_ff : row;
wr[q] = blob + (upq[q] ? L.up_off : 0) + (size_t) rr[q] * L.gu_row;
}
const uint32_t* grid = LT ? s_grid : reinterpret_cast<const uint32_t*>(iq3s_grid);
const int nb = (int) (L.n_embd / 256), xb = (int) (L.n_embd / 32);
const int e0 = grp_start[g], e1 = grp_start[g + 1];
for (int e = e0; e < e1; e += GRP_NC) {
const int n = min(GRP_NC, e1 - e);
const block_q8_1* xbase = xq;
int off[GRP_NC];
if constexpr (LX) {
__syncthreads(); // the previous chunk is done with s_x
for (int i = threadIdx.x; i < n * xb; i += 256) {
const int c = i / xb, j = i - c * xb;
s_x[c * S26_XMAX + j] = xq[(size_t) ent_tok[e + c] * xb + j];
}
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) off[c] = min(c, n - 1) * S26_XMAX;
xbase = s_x;
} else {
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) off[c] = ent_tok[e + min(c, n - 1)] * xb;
}
if constexpr (LT || LX) __syncthreads(); // s_grid / s_x ready
if (!live) continue;
float s[RPW][GRP_NC];
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) s[q][c] = 0.0f;
for (int k = lane; k < nb * 8; k += 32) {
const int kbx = k / 8, iqs = 2 * (k % 8);
Split<21>::W w[RPW];
#pragma unroll
for (int q = 0; q < RPW; ++q) { if constexpr (SL && !LT) w[q] = S26Split<21, false>::load(wr[q], kbx, iqs); else w[q] = S26IQ3S::load<LT>(wr[q], kbx, iqs, grid); }
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
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);
}
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n) s[q][c] = warp_sum(s[q][c]);
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n && lane == c) (upq[q] ? up : gate)[(size_t) (e + c) * L.n_ff + rr[q]] = s[q][c];
}
}
constexpr int S26_HMAX = 640 / 32; // q8_1 blocks of one SwiGLU row (n_ff 640)
template<bool LX, int RPW, bool SL = 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,
const int32_t* __restrict__ ent_dst,
const block_q8_1* __restrict__ hq, NativeExpertLayout L,
float* __restrict__ out) {
__shared__ block_q8_1 s_h[LX ? GRP_NC * S26_HMAX : 1];
const int g = blockIdx.y;
if (g >= *n_groups) return;
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int r0 = blockIdx.x * 8 * RPW + warp;
const bool live = r0 + 8 * (RPW - 1) < L.n_embd;
const uint8_t* blob = (const uint8_t*) grp_ptr[g];
const uint8_t* wr[RPW];
#pragma unroll
for (int q = 0; q < RPW; ++q) wr[q] = blob + L.down_off + (size_t) (r0 + 8 * q) * L.d_row;
const int nb = (int) (L.n_ff / 32), hb = (int) (L.n_ff / 32);
const int e0 = grp_start[g], e1 = grp_start[g + 1];
for (int e = e0; e < e1; e += GRP_NC) {
const int n = min(GRP_NC, e1 - e);
const block_q8_1* hbase = hq;
int off[GRP_NC];
if constexpr (LX) {
__syncthreads();
for (int i = threadIdx.x; i < n * hb; i += 256) {
const int c = i / hb, j = i - c * hb;
s_h[c * S26_HMAX + j] = hq[(size_t) (e + c) * hb + j];
}
__syncthreads();
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) off[c] = min(c, n - 1) * S26_HMAX;
hbase = s_h;
} else {
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) off[c] = (e + min(c, n - 1)) * hb;
}
if (!live) continue;
float s[RPW][GRP_NC];
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) s[q][c] = 0.0f;
for (int k = lane; k < nb * 2; k += 32) {
const int kbx = k / 2, iqs = 2 * (k % 2);
Split<20>::W w[RPW];
#pragma unroll
for (int q = 0; q < RPW; ++q) { if constexpr (SL) w[q] = S26Split<20, false>::load(wr[q], kbx, iqs); else w[q] = Split<20>::load(wr[q], kbx, iqs); }
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n) s[q][c] += Split<20>::apply(w[q], hbase + off[c] + kbx, iqs);
}
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n) s[q][c] = warp_sum(s[q][c]);
#pragma unroll
for (int q = 0; q < RPW; ++q)
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r0 + 8 * q] = s[q][c];
}
}
// S26: swiglu_entries_kernel + quantize_q8_1_kernel in one launch (the same h expression, then the same block quantizer,
// contraction off so the product cannot fuse into the first sum); h is still written (it is the scratch the old pair used)
__global__ void s26_swiglu_q8_1_kernel(const float* __restrict__ gate, const float* __restrict__ up, float* __restrict__ h,
block_q8_1* __restrict__ y, long long n) {
#pragma clang fp contract(off)
const long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x;
if (i >= n) return; // n is a multiple of 32: whole warps only
const float g = gate[i];
const float xi = (g / (1.0f + __expf(-g))) * up[i];
h[i] = xi;
float amax = fabsf(xi), sum = xi;
#pragma unroll
for (int o = 16; o > 0; o >>= 1) {
amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));
sum += __shfl_xor_sync(0xffffffffu, sum, o);
}
const float d = amax / 127.0f;
const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
const long long ib = i / 32, iqs = i % 32;
y[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>
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);
if (FQ) {
s26_swiglu_q8_1_kernel<<<(unsigned) ((nh + 255) / 256), 256, 0, s>>>(gate, up, h, hq, nh);
} else {
swiglu_entries_kernel<<<(unsigned) ((nh + 255) / 256), 256, 0, s>>>(gate, up, h, nh);
quantize_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);
}
template<int TG>
void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, const int32_t* grp_start,
const int32_t* n_groups, const int32_t* ent_tok, const block_q8_1* X, const NativeExpertLayout& L,
@@ -1450,6 +1724,13 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long
float* h = (float*) ((uint8_t*) scratch + 2 * fa);
block_q8_1* hq = (block_q8_1*) ((uint8_t*) scratch + 3 * fa);
const 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);
check("native_expert_grouped (v2)");
return;
}
const dim3 ggu((unsigned) ((2 * L.n_ff + GU_ROWS - 1) / GU_ROWS), (unsigned) cap_groups);
switch (L.gu_type) {
#define STRATA_GU(T) case T: launch_gu<T>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;