sm_60: stage IQ3_S's codebook and sign masks in shared memory too

IQ3_S was the one i-quant gate/up format whose codebook the grouped expert
kernels still read from global memory (IQ2_XXS / IQ2_XS / IQ2_S stage theirs
everywhere, IQ3_XXS on 6.x since the first commit of this branch). Its 512
codebook words are now staged per block with two 256-word tables of byte
masks - IQ3_S keeps one explicit sign byte per 8 values, so the tables hold
the masks of a sign byte's bits 0-3 and 4-7 - applied as
(g ^ mask) + (mask & 0x01010101): every iq3s_grid byte is odd, so a negated
byte never carries into the next. Bitwise the same output (iq_multi_parity
and native_grouped_parity on a GP100 die, with and without
STRATA_IQ_STAGE_GRID_MMVQ=1).

One GP100 die, a verify window's layer of real Swift 1.5 IQ3_S experts
(20 experts, 24 entries): 517 -> 393 us with Q2_0 down, 492 -> 369 us with
IQ4_NL down. Swift 1.5 IQ3_XXS (13 of 48 layers IQ3_S), four GP100 dies,
1M context, decode tok/s, medians of three interleaved engine starts:
36.0 -> 37.2 (10.7K document), 36.3 -> 37.4 (cached), 43.1 -> 44.5
(reasoning), 41.6 -> 43.0 (code), with the same drafts accepted.

The switch covers both formats and is renamed accordingly:
stage_tables_on(), STRATA_IQ_STAGE_TABLES=1|0 (was STRATA_IQ_STAGE_GRID18).
Still the default on compute capability 6.x only.
This commit is contained in:
ruibeikaa authored and Niko1221 committed 2026-10-14 07:08:59 +02:00
1 parent 080abf86a4
commit ac35eebf30
2 files changed
+49 -18

No files matched your search

+8 -3
View File
@@ -114,9 +114,14 @@ and i-quant blocks (Q6_K, IQ4_XS, Q4_K, IQ3_S, ...) one int at a time. Two chang
(+3.5 GiB; the experts keep their offsets, so the native pack is reused). Point `--native` and
`--ple-gguf` at the new folder. Requantizing costs at most 0.4% of a tensor's |w|max (Q8_0 is finer than the
sources).
- IQ3_XXS's codebook and sign-mask tables are staged in shared memory by the grouped expert kernels on compute
capability 6.x (ported from shinbunbun/llama-cpp-p100-patches 29 and 30; bitwise the same output;
`STRATA_IQ_STAGE_GRID18=0|1` forces it off or on, on any card). Within noise on the PH402 (+0-4%).
- IQ3_XXS's and IQ3_S's codebooks and sign-mask tables are staged in shared memory by the grouped expert kernels on
compute capability 6.x (IQ3_XXS's ported from shinbunbun/llama-cpp-p100-patches 29 and 30, IQ3_S's the same for its
sign bytes; bitwise the same output; `STRATA_IQ_STAGE_TABLES=0|1` forces it off or on, on any card). IQ3_S was the
one gate/up format whose codebook those kernels still read from global memory: a verify window's layer of IQ3_S
experts (20 experts, 24 entries) takes 24% less on a GP100 die (517 -> 393 us with a Q2_0 down projection, 492 ->
369 us with IQ4_NL), and Swift 1.5 IQ3_XXS (13 of its 48 layers are IQ3_S) decodes 3.2% faster with the settings
below at 1M context (39.3 -> 40.5 tok/s, mean of the four prompts; the same drafts accepted). IQ3_XXS's tables
alone were within noise (+0-4%).
Measured on a PH402 SKU 200 (two boards, four GP100 dies of 48 SMs and 32 GB HBM2), application clocks locked at
1050 MHz, Windows 11, driver 581.80 (TCC), CUDA 12.9, engine 0.1.40.2; Flash-Next IQ3_XXS, 32K, `--layer-split 12,24,36
+41 -15
View File
@@ -701,7 +701,7 @@ template<int TY> struct Split;
// per-entry ones, which call Fmt<TY>::dot per column exactly as before #242 (the launchers test kSplit at compile
// time, so the multi kernels are never instantiated for a type without a Split).
template<int TY> inline constexpr bool kSplit = false;
template<int TY> inline constexpr bool kStageIqGrid = (TY == 16 || TY == 17 || TY == 18 || TY == 22 || TY == 29);
template<int TY> inline constexpr bool kStageIqGrid = (TY == 16 || TY == 17 || TY == 18 || TY == 21 || TY == 22 || TY == 29);
template<> inline constexpr bool kSplit<16> = true;
template<> struct Split<16> { // IQ2_XXS
@@ -852,22 +852,31 @@ template<> inline constexpr bool kSplit<21> = true;
template<> struct Split<21> { // IQ3_S
struct W { int g[8]; int ls; float dw; };
template<bool STAGE_GRID = false>
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) {
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ s_grid = nullptr) {
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;
const uint32_t* grid = STAGE_GRID ? s_grid : iq3s_grid;
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);
const int2 grid_pos = make_int2(grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],
grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);
if constexpr (STAGE_GRID) {
// The staged tables hold the byte masks of a sign byte's bits 0-3 and 4-7; every iq3s_grid byte is
// odd, so (g ^ mask) + (mask & 0x01010101) negates the masked bytes with no carry into the next.
const uint32_t ml = s_grid[512 + signs_packed_8[l0 / 2]], mh = s_grid[768 + signs_packed_8[l0 / 2]];
r.g[l0 + 0] = (int) (((uint32_t) grid_pos.x ^ ml) + (ml & 0x01010101u));
r.g[l0 + 1] = (int) (((uint32_t) grid_pos.y ^ mh) + (mh & 0x01010101u));
} else {
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);
@@ -1295,6 +1304,23 @@ __device__ __forceinline__ const uint32_t* stage_iq_grid(uint32_t* s_buf, int ti
}
__syncthreads();
return s_buf;
} else if constexpr (TY == 21) {
// 512 codebook words, then two 256-word tables: the byte masks of a sign byte's bits 0-3 and of its bits
// 4-7 (IQ3_S stores one sign byte per 8 values). As for IQ3_XXS, one pass per block replaces the
// per-lookup __vcmpne4 / __vsub4 chain, and the codebook reads leave global memory.
for (int i = tid; i < 512; i += nthreads) s_buf[i] = iq3s_grid[i];
for (int u = tid; u < 256; u += nthreads) {
uint32_t ml = 0, mh = 0;
#pragma unroll
for (int i = 0; i < 4; ++i) {
ml |= (((uint32_t) u >> i) & 1u) * (0xffu << (8 * i));
mh |= (((uint32_t) u >> (4 + i)) & 1u) * (0xffu << (8 * i));
}
s_buf[512 + u] = ml;
s_buf[768 + u] = mh;
}
__syncthreads();
return s_buf;
} else {
return nullptr;
}
@@ -1302,7 +1328,7 @@ __device__ __forceinline__ const uint32_t* stage_iq_grid(uint32_t* s_buf, int ti
template<int TY, bool STAGE_GRID = kStageIqGrid<TY>>
struct IqGridWords {
static constexpr int value = !STAGE_GRID ? 4 : (TY == 22 || TY == 29) ? 2048 : (TY == 17) ? 1024 : (TY == 16 || TY == 18) ? 512 : 4;
static constexpr int value = !STAGE_GRID ? 4 : (TY == 22 || TY == 29) ? 2048 : (TY == 17 || TY == 21) ? 1024 : (TY == 16 || TY == 18) ? 512 : 4;
};
// mmvq_kernel with the columns taken NC at a time. After warp_sum every lane holds the same sum, so lane c stores
@@ -2175,13 +2201,13 @@ bool g_stage_grid_mmvq = g_stage_grid && [] {
const char* v = std::getenv("STRATA_IQ_STAGE_GRID_MMVQ");
return v != nullptr && v[0] == '1';
}();
// IQ3_XXS's codebook and sign-mask tables staged in shared memory by the grouped expert kernels (the single-matrix
// mmvq does not): bitwise the same either way. Measured on Pascal GP100 only, so it is the default on compute
// capability 6.x and off elsewhere; STRATA_IQ_STAGE_GRID18=1 / 0 forces it on / off on any card.
bool stage_grid18_on() {
// IQ3_XXS's and IQ3_S's codebooks and sign-mask tables staged in shared memory by the grouped expert kernels (the
// single-matrix mmvq does not): bitwise the same either way. Measured on Pascal GP100 only, so it is the default on
// compute capability 6.x and off elsewhere; STRATA_IQ_STAGE_TABLES=1 / 0 forces it on / off on any card.
bool stage_tables_on() {
if (!g_stage_grid) return false;
static const int forced = [] {
const char* v = std::getenv("STRATA_IQ_STAGE_GRID18");
const char* v = std::getenv("STRATA_IQ_STAGE_TABLES");
return (v && v[0]) ? (v[0] != '0' ? 1 : 0) : -1;
}();
if (forced >= 0) return forced == 1;
@@ -2897,7 +2923,7 @@ void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, con
if constexpr (!kSplit<TG>) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
else if (g_old_kernels) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
else if constexpr (kStageIqGrid<TG>) {
if (!g_stage_grid || (TG == 18 && !stage_grid18_on())) {
if (!g_stage_grid || ((TG == 18 || TG == 21) && !stage_tables_on())) {
if (g_no_sub16_gu) native_gu_multi_kernel<TG, false, false><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
else native_gu_multi_kernel<TG, false, true><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
return;