prefill: register-lean Q6_K gate/up loads on the CUDA fused prompt kernels (opt-in STRATA_PF_FUSED_Q6K=1, same bits)

Phase 4. The Q6_K gate/up kernels of 6f041b336 sat at the 512-thread kernel's 128-register cap with 32 / 16 bytes of stack and lost
to MMQ on 48 SMs (RTX A4000, one layer at 8,192 tokens: 0.48x of the MMQ path). The row's two decode threads take sub-blocks 2k and
2k + 1 of a stage: their low nibbles are different ql bytes, but their two high bits come from the same 32 qh bytes, which both
loaded. Now each loads half of the qh bytes (bytes 16 uj ..) and the halves are swapped by a shuffle when the stage is decoded, as
A1b (18027b91c) does for Q4_K / Q5_K: 14 raw words across a stage instead of 18. The words the expand gives are load_unit<T_Q6_K>'s,
so convert and the arithmetic are unchanged: the fused output bits are the same (prefill_fused_iq_test's hashes, 3 shapes, on an RTX
PRO 5000 and an RTX A4000).

Line numbers of this commit's src/prefill/moe_fused_iq.cu:
- 651-677: explicit specializations kq_lean_words<T_Q6_K> (14), kq_lean_load<T_Q6_K>, kq_lean_expand<T_Q6_K>, inside
  __CUDA_ARCH__ >= 750 (the Q4_K / Q5_K forms of these templates are not edited).
- 693: native_kernel's KLEAN also for a Q6_K gate/up (a constant per instance; false and unchanged for every other instance).
Device code: only native_kernel<T_Q6_K, true, 2 | 4> change.

One layer (prefill_fused_iq_test timing, uniform routing over 512 experts), fused / MMQ path at 2,048 / 3,584 / 8,192 tokens:
RTX PRO 5000 1.08 / 1.10 / 0.97 -> 1.29 / 1.16 / 1.02; RTX A4000 0.88 / 1.09 / 0.48 -> 1.17 / 1.11 / 0.70.
This commit is contained in:
Niko1221 committed 2026-11-11 02:43:54 +01:00
1 parent 878ddd8523
commit c915680bd3
1 file changed
+28 -1
+28 -1
View File
@@ -648,6 +648,33 @@ template <int T> __device__ __forceinline__ void kq_lean_expand(const uint32_t (
}
}
}
// (CUDA, STRATA_PF_FUSED_Q6K=1) the same for the Q6_K gate/up: the row's two decode threads take sub-blocks 2 k and 2 k + 1 of a stage,
// whose low nibbles are different ql bytes (each loads its own 32) but whose two high bits come from the same 32 qh bytes: each loads
// half of them (bytes 16 uj ..) and the halves are swapped when the stage is decoded. 14 raw words across a stage instead of 18; the
// words kq_lean_expand gives are load_unit<T_Q6_K>'s, so convert and the arithmetic are unchanged. (Explicit specializations: the
// Q4_K / Q5_K forms above are not touched.)
template <> __host__ __device__ constexpr int kq_lean_words<T_Q6_K>() { return 14; }
template <> __device__ __forceinline__ void kq_lean_load<T_Q6_K>(const uint8_t* bp, int ib, int uj, uint32_t (&w)[14]) {
const int n = ib >> 2, r = ib & 3;
#pragma unroll
for (int k = 0; k < 8; ++k) w[k] = ld32(bp + 64 * n + 32 * (r & 1) + 4 * k);
#pragma unroll
for (int k = 0; k < 4; ++k) w[8 + k] = ld32(bp + 128 + 32 * n + 16 * uj + 4 * k);
w[12] = ld16(bp + 208) | (ld16(bp + 192 + 8 * n + 2 * r) << 16);
w[13] = (uint32_t) r;
}
template <> __device__ __forceinline__ void kq_lean_expand<T_Q6_K>(const uint32_t (&w)[14], int uj, uint32_t (&f)[raw_words(T_Q6_K)]) {
#pragma unroll
for (int k = 0; k < 8; ++k) f[k] = w[k];
#pragma unroll
for (int k = 0; k < 4; ++k) {
const uint32_t o = __shfl_xor_sync(0xffffffffu, w[8 + k], 1);
f[8 + k] = uj ? o : w[8 + k];
f[12 + k] = uj ? w[8 + k] : o;
}
f[16] = w[12];
f[17] = w[13];
}
#endif
template <int NT> using BatchOf = std::conditional_t<NT == THREADS, Batch, WideBatch>;
@@ -663,7 +690,7 @@ native_kernel(const BatchOf<NT> b, const NativeGeom geo, const Tables tb, const
constexpr bool TWO_PASS = NT != THREADS; // the two-pass loads (load_unit_l) only in the opt-in NT = 256 kernels
static_assert(!(TWO_PASS && WT == T_Q5_0), "the NT = 256 kernels have no Q5_0 form");
static_assert(!(TWO_PASS && (has_min(WT) || WT == T_Q8_0 || WT == T_Q6_K)), "the NT = 256 kernels have no K-quant / Q5_1 / Q8_0 / Q6_K form");
constexpr bool KLEAN = GU && (WT == T_Q4_K || WT == T_Q5_K); // (A1b) the K-quant gate/up's lean loads (kq_lean_load)
constexpr bool KLEAN = GU && (WT == T_Q4_K || WT == T_Q5_K || WT == T_Q6_K); // (A1b) the K-quant gate/up's lean loads (kq_lean_load)
constexpr int RAW = KLEAN ? kq_lean_words<WT>() : raw_words(WT) > 5 ? raw_words(WT) : 5; // the raw words of a sub-block: 5, Q5_0's 6
constexpr int NS = (GU ? GU_ROWS_K : D_ROWS_K) / 64; // 64-value stages along K
constexpr int NFB = (GU ? GU_ROWS_N : D_ROWS_N) / WR; // weight-row blocks per tile