From 2715a75248e0bcddeda73d6c5b1108a33da13e89 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 5 Oct 2026 10:16:01 +0200 Subject: [PATCH] perf(iq): 16-lane sub-warp expert gate/up and down kernels, exact-N MMVQ columns, decoded-once small-format tables, two rows per block (#783 PR-h) iq_kernels.cu: row_dot_80_sub16 (two rows per warp, all lanes active) for the n_embd == 2560 gate/up of every split format, exact-N instantiations of the multi-column MMVQ (launch_mmvq_multi_nc), the 3-entry tail in the grouped gate/up and down kernels. native_mmvq.cu: SmallTraits for Q4_0, Q5_0, Q8_0 and IQ4_NL decode the weight chunks and block scale once per (row, block) and apply them to every activation column with the arithmetic of small_q8_dot, and the multi-column launch runs two rows per block for any K. Kill switches: STRATA_NO_SUB16_GU=1 (sub-warp gate/up/down), STRATA_OLD_IQ_MMVQ=1 (per-column kernels, existing), STRATA_NO_MMVQ_ROWS2=1 (one row per block at large K). mmvq_multi_parity, iq_multi_parity, native_grouped_parity and native_expert_parity pin the columns to the single-column results. Ported from #783 (stuchapin909, head 4327807), split per piece. Co-authored-by: stuchapin909 <178119976+stuchapin909@users.noreply.github.com> --- src/kernels/cuda/iq_kernels.cu | 220 ++++++++++++++++++++++++-------- src/kernels/cuda/native_mmvq.cu | 150 +++++++++++++++++++--- 2 files changed, 302 insertions(+), 68 deletions(-) diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu index bd0b92e0..7a9e4301 100644 --- a/src/kernels/cuda/iq_kernels.cu +++ b/src/kernels/cuda/iq_kernels.cu @@ -1081,6 +1081,42 @@ __device__ __forceinline__ void row_dot_20_sub16(const uint8_t* row, const block } } +// 16-lane sub-warp row dot for K = nb * ipb == 80 (all kSplit gate/up formats with n_embd == 2560): +// 2 rows per warp (16 rows per 256-thread block), 100% active lanes, bitwise identical addition tree to row_dot_multi. +template +__device__ __forceinline__ void row_dot_80_sub16(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n, + int t, float (&s)[NC], const uint32_t* __restrict__ s_grid = nullptr) { + using F = Fmt; + using S = Split; +#pragma unroll + for (int c = 0; c < NC; ++c) s[c] = 0.0f; + for (int k = t; k < 80; k += 32) { + const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) + if (EXACT_N || c < n) s[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + } + float s16[NC]; +#pragma unroll + for (int c = 0; c < NC; ++c) s16[c] = 0.0f; + for (int k = t + 16; k < 64; k += 32) { + const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) + if (EXACT_N || c < n) s16[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + } +#pragma unroll + for (int c = 0; c < NC; ++c) { + if (EXACT_N || c < n) { + s[c] = __fadd_rn(s[c], s16[c]); +#pragma unroll + for (int o = 8; o > 0; o >>= 1) s[c] += __shfl_xor_sync(0xffffffffu, s[c], o); + } + } +} + template> __device__ __forceinline__ const uint32_t* stage_iq_grid(uint32_t* s_buf, int tid, int nthreads) { if constexpr (!STAGE_GRID) { @@ -1128,7 +1164,7 @@ struct IqGridWords { #else #define STRATA_SHARED_ALIGN16_U32(name, ...) __shared__ alignas(16) uint32_t name[__VA_ARGS__] #endif -template> +template, bool EXACT_N = false> __global__ void __launch_bounds__(128) mmvq_multi_kernel(const uint8_t* __restrict__ w, size_t row_bytes, const block_q8_1* __restrict__ x, float* __restrict__ y, int n_in, int n_out, int ncols) { @@ -1139,16 +1175,27 @@ __global__ void __launch_bounds__(128) mmvq_multi_kernel(const uint8_t* __restri const int lane = threadIdx.x; const int nb = n_in / Fmt::qk, xb = n_in / 32; const uint8_t* wr = w + (size_t) row * row_bytes; - for (int c0 = 0; c0 < ncols; c0 += NC) { - const int n = min(NC, ncols - c0); + if constexpr (EXACT_N) { int off[NC]; #pragma unroll - for (int c = 0; c < NC; ++c) off[c] = (c0 + min(c, n - 1)) * xb; + for (int c = 0; c < NC; ++c) off[c] = c * xb; float s[NC]; - row_dot_multi(wr, x, off, n, nb, lane, s, s_grid); + row_dot_multi(wr, x, off, NC, nb, lane, s, s_grid); #pragma unroll for (int c = 0; c < NC; ++c) - if (c < n && lane == c) y[(size_t) (c0 + c) * n_out + row] = s[c]; + if (lane == c) y[(size_t) c * n_out + row] = s[c]; + } else { + for (int c0 = 0; c0 < ncols; c0 += NC) { + const int n = min(NC, ncols - c0); + int off[NC]; +#pragma unroll + for (int c = 0; c < NC; ++c) off[c] = (c0 + min(c, n - 1)) * xb; + float s[NC]; + row_dot_multi(wr, x, off, n, nb, lane, s, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) + if (c < n && lane == c) y[(size_t) (c0 + c) * n_out + row] = s[c]; + } } } @@ -1191,7 +1238,7 @@ __global__ void __launch_bounds__(256) native_gu_kernel(const unsigned long long // has halves of <= 4), so 4 takes such windows in one pass; 8 would take ~64-80 registers against ~48 (ptxas -v). constexpr int GRP_NC = 4; -template> +template, bool SUB16 = true> __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned long long* __restrict__ grp_ptr, const int32_t* __restrict__ grp_start, const int32_t* __restrict__ n_groups, @@ -1202,13 +1249,53 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lon if (blockIdx.y >= ng) return; STRATA_SHARED_ALIGN16_U32(s_grid_buf, IqGridWords::value); const uint32_t* s_grid = stage_iq_grid(s_grid_buf, threadIdx.x, 256); + const int nb = (int) (L.n_embd / Fmt::qk), xb = (int) (L.n_embd / 32); + if constexpr (SUB16) { + if (xb == 80) { + const int subwarp = threadIdx.x >> 4, t = threadIdx.x & 15; + const int row = blockIdx.x * 16 + subwarp; // 0 .. 2*n_ff + if (row >= 2 * L.n_ff) return; + const bool is_up = row >= L.n_ff; + const int r = is_up ? row - (int) L.n_ff : row; + const size_t w_off = (is_up ? L.up_off : 0) + (size_t) r * L.gu_row; + float* dst = is_up ? up : gate; + for (int g = blockIdx.y; g < ng; g += gridDim.y) { + const uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off; + 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); + if (n == 1) { + const int off1[1] = { ent_tok[e] * 80 }; + float s1[1]; + row_dot_80_sub16(wr, xq, off1, 1, t, s1, s_grid); + if (t == 0) dst[(size_t) e * L.n_ff + r] = s1[0]; + } else if (n == 2) { + const int off2[2] = { ent_tok[e] * 80, ent_tok[e + 1] * 80 }; + float s2[2]; + row_dot_80_sub16(wr, xq, off2, 2, t, s2, s_grid); + if (t < 2) dst[(size_t) (e + t) * L.n_ff + r] = s2[t]; + } else if (n == 3) { + const int off3[3] = { ent_tok[e] * 80, ent_tok[e + 1] * 80, ent_tok[e + 2] * 80 }; + float s3[3]; + row_dot_80_sub16(wr, xq, off3, 3, t, s3, s_grid); + if (t < 3) dst[(size_t) (e + t) * L.n_ff + r] = s3[t]; + } else { + const int off4[4] = { ent_tok[e] * 80, ent_tok[e + 1] * 80, ent_tok[e + 2] * 80, ent_tok[e + 3] * 80 }; + float s4[4]; + row_dot_80_sub16(wr, xq, off4, 4, t, s4, s_grid); + if (t < 4) dst[(size_t) (e + t) * L.n_ff + r] = s4[t]; + } + } + } + return; + } + } const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31; const int row = blockIdx.x * GU_ROWS + warp; // 0 .. 2*n_ff if (row >= 2 * L.n_ff) return; const bool is_up = row >= L.n_ff; const int r = is_up ? row - (int) L.n_ff : row; const size_t w_off = (is_up ? L.up_off : 0) + (size_t) r * L.gu_row; - const int nb = (int) (L.n_embd / Fmt::qk), xb = (int) (L.n_embd / 32); float* dst = is_up ? up : gate; for (int g = blockIdx.y; g < ng; g += gridDim.y) { // the group stride, as native_gu_kernel const uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off; @@ -1225,15 +1312,16 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lon float s2[2]; row_dot_multi(wr, xq, off2, 2, nb, lane, s2, s_grid); if (lane < 2) dst[(size_t) (e + lane) * L.n_ff + r] = s2[lane]; + } else if (n == 3) { + const int off3[3] = { ent_tok[e] * xb, ent_tok[e + 1] * xb, ent_tok[e + 2] * xb }; + float s3[3]; + row_dot_multi(wr, xq, off3, 3, nb, lane, s3, s_grid); + if (lane < 3) dst[(size_t) (e + lane) * L.n_ff + r] = s3[lane]; } else { - int off[GRP_NC]; -#pragma unroll - for (int c = 0; c < GRP_NC; ++c) off[c] = ent_tok[e + min(c, n - 1)] * xb; - float s[GRP_NC]; - row_dot_multi(wr, xq, off, n, nb, lane, s, s_grid); -#pragma unroll - for (int c = 0; c < GRP_NC; ++c) - if (c < n && lane == c) dst[(size_t) (e + c) * L.n_ff + r] = s[c]; + const int off4[4] = { ent_tok[e] * xb, ent_tok[e + 1] * xb, ent_tok[e + 2] * xb, ent_tok[e + 3] * xb }; + float s4[4]; + row_dot_multi(wr, xq, off4, 4, nb, lane, s4, s_grid); + if (lane < 4) dst[(size_t) (e + lane) * L.n_ff + r] = s4[lane]; } } } @@ -1271,7 +1359,7 @@ __global__ void __launch_bounds__(256) native_down_kernel(const unsigned long lo } // native_down_kernel with the entries taken GRP_NC at a time -template +template __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned long long* __restrict__ grp_ptr, const int32_t* __restrict__ grp_start, const int32_t* __restrict__ n_groups, @@ -1282,7 +1370,7 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned l if (blockIdx.y >= ng) return; __shared__ uint32_t s_hq_buf[GRP_NC * 20 * 9]; const int nb = (int) (L.n_ff / Fmt::qk), hb = (int) (L.n_ff / 32); - if constexpr (TD == 20) { + if constexpr (SUB_DOWN && TD == 20) { if (hb == 20) { const int r = blockIdx.x * 32 + (threadIdx.x >> 3); const int t = threadIdx.x & 7; @@ -1310,22 +1398,23 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned l float s2[2]; row_dot_40_sub8(wr, s_hq, off2, 2, t, s2); if (t < 2) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s2[t]; + } else if (n == 3) { + const int off3[3] = { 0, 20, 40 }; + float s3[3]; + row_dot_40_sub8(wr, s_hq, off3, 3, t, s3); + if (t < 3) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s3[t]; } else { - int off[GRP_NC]; -#pragma unroll - for (int c = 0; c < GRP_NC; ++c) off[c] = min(c, n - 1) * 20; - float s[GRP_NC]; - row_dot_40_sub8(wr, s_hq, off, n, t, s); -#pragma unroll - for (int c = 0; c < GRP_NC; ++c) - if (c < n && t == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c]; + const int off4[4] = { 0, 20, 40, 60 }; + float s4[4]; + row_dot_40_sub8(wr, s_hq, off4, 4, t, s4); + if (t < 4) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s4[t]; } } } } return; } - } else if constexpr (TD == 42) { + } else if constexpr (SUB_DOWN && TD == 42) { if (hb == 20) { const int r = blockIdx.x * 16 + (threadIdx.x >> 4); const int t = threadIdx.x & 15; @@ -1353,15 +1442,16 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned l float s2[2]; row_dot_20_sub16(wr, s_hq, off2, 2, t, s2); if (t < 2) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s2[t]; + } else if (n == 3) { + const int off3[3] = { 0, 20, 40 }; + float s3[3]; + row_dot_20_sub16(wr, s_hq, off3, 3, t, s3); + if (t < 3) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s3[t]; } else { - int off[GRP_NC]; -#pragma unroll - for (int c = 0; c < GRP_NC; ++c) off[c] = min(c, n - 1) * 20; - float s[GRP_NC]; - row_dot_20_sub16(wr, s_hq, off, n, t, s); -#pragma unroll - for (int c = 0; c < GRP_NC; ++c) - if (c < n && t == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c]; + const int off4[4] = { 0, 20, 40, 60 }; + float s4[4]; + row_dot_20_sub16(wr, s_hq, off4, 4, t, s4); + if (t < 4) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s4[t]; } } } @@ -1848,6 +1938,14 @@ int d_qk(int t) { default: return 0; } } +bool gu_split(int t) { + switch (t) { +#define STRATA_SP(T) case T: return kSplit; + STRATA_GU_FMTS(STRATA_SP) +#undef STRATA_SP + default: return false; + } +} bool env_on(const char* name) { const char* v = std::getenv(name); @@ -1855,6 +1953,7 @@ bool env_on(const char* name) { } // STRATA_OLD_IQ_MMVQ=1 keeps the per-column kernels (bitwise equal to the new ones; kept for A/B timing) bool g_old_kernels = env_on("STRATA_OLD_IQ_MMVQ"); +bool g_no_sub16_gu = env_on("STRATA_NO_SUB16_GU"); // STRATA_IQ_STAGE_GRID=0 disables staging 64-bit i-quant codebook tables into shared memory bool g_stage_grid = [] { const char* v = std::getenv("STRATA_IQ_STAGE_GRID"); @@ -1868,6 +1967,23 @@ bool g_stage_grid_mmvq = g_stage_grid && [] { return v != nullptr && v[0] == '1'; }(); +template> +void launch_mmvq_multi_nc(dim3 grid, dim3 block, cudaStream_t s, const uint8_t* W, size_t rb, const block_q8_1* X, + float* y, int n_in, int n_out, int ncols) { + switch (ncols) { + case 1: mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, 1); break; + case 2: mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, 2); break; + case 3: mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, 3); break; + case 4: mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, 4); break; + case 5: mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, 5); break; + case 6: mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, 6); break; + default: + if (ncols <= 1) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); + else mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); + break; + } +} + template void launch_mmvq(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int n_in, int n_out, int ncols, cudaStream_t s) { @@ -1876,21 +1992,12 @@ void launch_mmvq(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int else if (g_old_kernels) mmvq_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); else if constexpr (kStageIqGrid) { if (!g_stage_grid_mmvq) { - if (ncols <= 1) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else if (ncols == 2) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else if (ncols <= 4) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); + launch_mmvq_multi_nc(grid, block, s, W, rb, X, y, n_in, n_out, ncols); return; } - if (ncols <= 1) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else if (ncols == 2) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else if (ncols <= 4) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); + launch_mmvq_multi_nc(grid, block, s, W, rb, X, y, n_in, n_out, ncols); } else { - if (ncols <= 1) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else if (ncols == 2) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else if (ncols <= 4) mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); - else mmvq_multi_kernel<<>>(W, rb, X, y, n_in, n_out, ncols); // 8 at a time past 8 + launch_mmvq_multi_nc(grid, block, s, W, rb, X, y, n_in, n_out, ncols); } } @@ -2563,11 +2670,16 @@ void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, con else if (g_old_kernels) native_gu_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); else if constexpr (kStageIqGrid) { if (!g_stage_grid) { - native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); + if (g_no_sub16_gu) native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); + else native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); return; } - native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); - } else native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); + if (g_no_sub16_gu) native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); + else native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); + } else { + if (g_no_sub16_gu) native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); + else native_gu_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); + } } template @@ -2576,7 +2688,8 @@ void launch_down(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, c float* out) { if constexpr (!kSplit) native_down_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); else if (g_old_kernels) native_down_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); - else native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + else if (g_no_sub16_gu) native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + else native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); } } // namespace @@ -3301,7 +3414,8 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long } const bool v1 = g_grouped_v1; const int64_t gy = (v1 || grid_groups <= 0 || grid_groups > cap_groups) ? cap_groups : grid_groups; - const dim3 ggu((unsigned) ((2 * L.n_ff + GU_ROWS - 1) / GU_ROWS), (unsigned) gy); + const int gu_rows = (!g_old_kernels && !g_no_sub16_gu && L.n_embd == 2560 && gu_split(L.gu_type)) ? 16 : GU_ROWS; + const dim3 ggu((unsigned) ((2 * L.n_ff + gu_rows - 1) / gu_rows), (unsigned) gy); #if defined(STRATA_HIP_GFX906) const int em0 = exp_mode(); #endif @@ -3381,7 +3495,7 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long check("native_expert_grouped/swiglu"); } if (g_exp_phase == 1) return; - const int d_rows = (!g_old_kernels && L.n_ff == 640) ? (L.d_type == 20 ? 32 : (L.d_type == 42 ? 16 : 8)) : 8; + const int d_rows = (!g_old_kernels && !g_no_sub16_gu && L.n_ff == 640) ? (L.d_type == 20 ? 32 : (L.d_type == 42 ? 16 : 8)) : 8; const dim3 gd((unsigned) ((L.n_embd + d_rows - 1) / d_rows), (unsigned) gy); #if defined(STRATA_HIP_GFX906) if ((em0 == 7 || em0 == 8) && (L.d_type == 20 || L.d_type == 42)) { diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cu index 5a5b0105..d099532a 100644 --- a/src/kernels/cuda/native_mmvq.cu +++ b/src/kernels/cuda/native_mmvq.cu @@ -1014,16 +1014,130 @@ struct IQ4XSTraits { return d * sumi; } }; -// The four 32-element formats: `load` keeps the block pointer (their decode is a few integer ops) and `apply` is -// the unchanged small_q8_dot. Their per-column cost is small; the win above is for the K and IQ formats. +// The four 32-element formats: `load` decodes the two 32-bit weight chunks and block scale once per (row, block), +// and `apply` runs the exact same STRATA_DP4A and scale arithmetic per activation column as `small_q8_dot`. template -struct SmallTraits { - using Block = Weight; - static constexpr int DIV = 32, T = Qi / 2, KBY = 1, BPI = 2 * WARPS * WARP / Qi; - __device__ static int kqs(int tid) { return 2 * (tid % (Qi / 2)); } - struct W { const Weight* w; }; - __device__ static W load(const Block* __restrict__ w, int) { return W{w}; } - __device__ static float apply(const W& r, const Q81Block* __restrict__ x, int k) { return small_q8_dot(r.w, x, k); } +struct SmallTraits; + +template<> +struct SmallTraits { + using Block = Q40Block; + static constexpr int DIV = 32, T = 2, KBY = 1, BPI = 2 * WARPS * WARP / 4; + __device__ static int kqs(int tid) { return 2 * (tid % 2); } + struct W { int vi0[2], vi1[2]; float d; }; + __device__ static W load(const Block* __restrict__ w, int iqs) { + W r; +#pragma unroll + for (int i = 0; i < 2; ++i) { + const int v = load_int_b2(w->qs, iqs + i); + r.vi0[i] = (v >> 0) & 0x0f0f0f0f; + r.vi1[i] = (v >> 4) & 0x0f0f0f0f; + } + r.d = w->d; + return r; + } + __device__ static float apply(const W& r, const Q81Block* __restrict__ x, int iqs) { + int sumi = 0; +#pragma unroll + for (int i = 0; i < 2; ++i) { + sumi = STRATA_DP4A(r.vi0[i], reinterpret_cast(x->qs)[iqs + i], sumi); + sumi = STRATA_DP4A(r.vi1[i], reinterpret_cast(x->qs)[iqs + i + 4], sumi); + } + const float2 ds = __half22float2(x->ds); + return r.d * (sumi * ds.x - 4 * ds.y); + } +}; + +template<> +struct SmallTraits { + using Block = Q50Block; + static constexpr int DIV = 32, T = 2, KBY = 1, BPI = 2 * WARPS * WARP / 4; + __device__ static int kqs(int tid) { return 2 * (tid % 2); } + struct W { int vi0[2], vi1[2]; float d; }; + __device__ static W load(const Block* __restrict__ w, int iqs) { + W r; + const int qh = load_int_b2(w->qh, 0); +#pragma unroll + for (int i = 0; i < 2; ++i) { + const int vl = load_int_b2(w->qs, iqs + i); + const int vh = qh >> (4 * (iqs + i)); + int vi0 = (vl >> 0) & 0x0f0f0f0f; + vi0 |= (vh << 4) & 0x00000010; + vi0 |= (vh << 11) & 0x00001000; + vi0 |= (vh << 18) & 0x00100000; + vi0 |= (vh << 25) & 0x10000000; + r.vi0[i] = vi0; + int vi1 = (vl >> 4) & 0x0f0f0f0f; + vi1 |= (vh >> 12) & 0x00000010; + vi1 |= (vh >> 5) & 0x00001000; + vi1 |= (vh << 2) & 0x00100000; + vi1 |= (vh << 9) & 0x10000000; + r.vi1[i] = vi1; + } + r.d = w->d; + return r; + } + __device__ static float apply(const W& r, const Q81Block* __restrict__ x, int iqs) { + int sumi = 0; +#pragma unroll + for (int i = 0; i < 2; ++i) { + sumi = STRATA_DP4A(r.vi0[i], reinterpret_cast(x->qs)[iqs + i], sumi); + sumi = STRATA_DP4A(r.vi1[i], reinterpret_cast(x->qs)[iqs + i + 4], sumi); + } + const float2 ds = __half22float2(x->ds); + return r.d * (sumi * ds.x - 8 * ds.y); + } +}; + +template<> +struct SmallTraits { + using Block = Q80Block; + static constexpr int DIV = 32, T = 4, KBY = 1, BPI = 2 * WARPS * WARP / 8; + __device__ static int kqs(int tid) { return 2 * (tid % 4); } + struct W { int v[2]; float d0; }; + __device__ static W load(const Block* __restrict__ w, int iqs) { + W r; +#pragma unroll + for (int i = 0; i < 2; ++i) r.v[i] = load_int_b2(w->qs, iqs + i); + r.d0 = w->d; + return r; + } + __device__ static float apply(const W& r, const Q81Block* __restrict__ x, int iqs) { + int sumi = 0; +#pragma unroll + for (int i = 0; i < 2; ++i) { + const int u = reinterpret_cast(x->qs)[iqs + i]; + sumi = STRATA_DP4A(r.v[i], u, sumi); + } + const float d1 = __low2float(x->ds); + return r.d0 * d1 * float(sumi); + } +}; + +template<> +struct SmallTraits { + using Block = IQ4NLBlock; + static constexpr int DIV = 32, T = 2, KBY = 1, BPI = 2 * WARPS * WARP / 4; + __device__ static int kqs(int tid) { return 2 * (tid % 2); } + struct W { int2 v[2]; float dw; }; + __device__ static W load(const Block* __restrict__ w, int iqs) { + W r; +#pragma unroll + for (int i = 0; i < 2; ++i) r.v[i] = iq4_table_lookup(load_int_b2(w->qs, iqs + i)); + r.dw = __half2float(w->d); + return r; + } + __device__ static float apply(const W& r, const Q81Block* __restrict__ x, int iqs) { + const int* q8 = reinterpret_cast(x->qs) + iqs; + int sumi = 0; +#pragma unroll + for (int i = 0; i < 2; ++i) { + sumi = STRATA_DP4A(r.v[i].x, q8[i], sumi); + sumi = STRATA_DP4A(r.v[i].y, q8[i + 4], sumi); + } + const float d = r.dw * __low2float(x->ds); + return d * sumi; + } }; // NW warps per block and ROWS rows per block. The EXACT layout (NW = 4, ROWS = 1, or 4 for small K) is the @@ -1121,15 +1235,21 @@ void launch_multi_n(const void* weights, const void* x_q8_1, float* y, int n_in, return; } const dim3 threads(WARP, WARPS); - if (n_in / F::DIV < F::BPI) { - constexpr int ROWS = 2; - const unsigned blocks = unsigned((std::size_t(n_out) + ROWS - 1) / ROWS); - if (s26_tsum_on()) native_mmvq_multi_kernel<<>>(w, x, y, n_in, n_out); - else native_mmvq_multi_kernel<<>>(w, x, y, n_in, n_out); - } else { + // #783 PR-h (stuchapin909): two rows per block for every K, not only small K - each row keeps its own partial sums and + // reduction, so a row's result does not depend on its neighbour; STRATA_NO_MMVQ_ROWS2=1 keeps one row for large K + static const bool rows1 = [] { + const char* v = std::getenv("STRATA_NO_MMVQ_ROWS2"); + return v != nullptr && v[0] != '\0' && v[0] != '0'; + }(); + if (rows1 && n_in / F::DIV >= F::BPI) { if (s26_tsum_on()) native_mmvq_multi_kernel<<>>(w, x, y, n_in, n_out); else native_mmvq_multi_kernel<<>>(w, x, y, n_in, n_out); + return; } + constexpr int ROWS = 2; + const unsigned blocks = unsigned((std::size_t(n_out) + ROWS - 1) / ROWS); + if (s26_tsum_on()) native_mmvq_multi_kernel<<>>(w, x, y, n_in, n_out); + else native_mmvq_multi_kernel<<>>(w, x, y, n_in, n_out); } template