diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cu index b6036824..4b6a8c33 100644 --- a/src/kernels/cuda/qsa_prompt_attn.cu +++ b/src/kernels/cuda/qsa_prompt_attn.cu @@ -1,6 +1,7 @@ // src/kernels/cuda/qsa_prompt_attn.cu - see include/strata/kernels/qsa_prompt_attn.hpp. #include "strata/kernels/qsa_prompt_attn.hpp" #include "strata/kernels/kv_q8.hpp" +#include "strata/kernels/kv_q4.hpp" #include #include @@ -56,14 +57,18 @@ __device__ __forceinline__ uint32_t pack_h2(float lo_k, float hi_k) { // eleme } // KV_MODE 1: int8 codes + fp16 scale per 64 values. KV_MODE 0: fp16 values (scales 1). +// KV_MODE 3 (hybrid K8V4): K as mode 1, V as mode 0 - the row's q4_0 blocks are dequantized to fp16 at +// gather, so everything downstream of the load is the mode-0 V path; the caller un-rotates the output. template struct Smem { - using Elem = typename std::conditional::type; - static constexpr int ROW = KV_MODE == 1 ? HD + 16 : HD + 8; // elements; 16-byte aligned rows, banks spread + using KElem = typename std::conditional::type; + using VElem = typename std::conditional::type; + static constexpr int KROW = KV_MODE == 0 ? HD + 8 : HD + 16; // elements; 16-byte aligned rows, banks spread + static constexpr int VROW = KV_MODE == 1 ? HD + 16 : HD + 8; __half qh[16][QS]; __half ql[16][QS]; - Elem k[CH][ROW]; - Elem v[CH][ROW]; + KElem k[CH][KROW]; + VElem v[CH][VROW]; float ks[CH][4]; float vs[CH][4]; float s[16][CH + 1]; @@ -128,24 +133,55 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_kernel(const float* __res S.row[t] = r; } __syncthreads(); // rows ready; the previous chunk's p.v is done with k, v, s - // gather the chunk's K and V rows (16-byte pieces) and their scales + // gather the chunk's K and V rows (16-byte pieces; K8V4's V as q4_0 blocks dequantized to fp16) + // and their scales { - constexpr int PIECES = HD * (int) sizeof(typename Smem::Elem) / 16; // per row - for (int i = t; i < CH * PIECES; i += THREADS) { - const int c = i / PIECES, pc = i % PIECES; + constexpr int KPIECES = HD * (int) sizeof(typename Smem::KElem) / 16; // per K row + for (int i = t; i < CH * KPIECES; i += THREADS) { + const int c = i / KPIECES, pc = i % KPIECES; const long long r = S.row[c]; - uint4 kx = make_uint4(0, 0, 0, 0), vx = kx; + uint4 kx = make_uint4(0, 0, 0, 0); if (r >= 0) { - if constexpr (KV_MODE == 1) { - kx = __ldg(reinterpret_cast(p.k_q + r * HD) + pc); - vx = __ldg(reinterpret_cast(p.v_q + r * HD) + pc); - } else { + if constexpr (KV_MODE == 0) kx = __ldg(reinterpret_cast(p.k_pool + r * HD) + pc); - vx = __ldg(reinterpret_cast(p.v_pool + r * HD) + pc); - } + else // modes 1 and 3: the K side is INT8 + kx = __ldg(reinterpret_cast(p.k_q + r * HD) + pc); } *reinterpret_cast(reinterpret_cast(&S.k[c][0]) + pc * 16) = kx; - *reinterpret_cast(reinterpret_cast(&S.v[c][0]) + pc * 16) = vx; + } + if constexpr (KV_MODE == 3) { // V: dequantize the row's q4_0 blocks straight into the fp16 V row + constexpr int BLKS = HD / QK4_0; + constexpr int BYTES = BLKS * (int) sizeof(block_q4_0); + for (int i = t; i < CH * BLKS; i += THREADS) { + const int c = i / BLKS, b = i % BLKS; + const long long r = S.row[c]; +#pragma unroll + for (int j = 0; j < QK4_0; ++j) S.v[c][b * QK4_0 + j] = __half(0); + if (r >= 0) { + const block_q4_0* blk = reinterpret_cast(p.v_q4 + r * BYTES) + b; + const float d = __half2float(__ushort_as_half(blk->d)); +#pragma unroll + for (int j = 0; j < QK4_0 / 2; ++j) { + S.v[c][b * QK4_0 + j] = __float2half_rn((float) ((int)(blk->qs[j] & 0x0F) - 8) * d); + S.v[c][b * QK4_0 + j + QK4_0 / 2] = + __float2half_rn((float) ((int)(blk->qs[j] >> 4) - 8) * d); + } + } + } + } else { + constexpr int VPIECES = HD * (int) sizeof(typename Smem::VElem) / 16; // per V row + for (int i = t; i < CH * VPIECES; i += THREADS) { + const int c = i / VPIECES, pc = i % VPIECES; + const long long r = S.row[c]; + uint4 vx = make_uint4(0, 0, 0, 0); + if (r >= 0) { + if constexpr (KV_MODE == 1) + vx = __ldg(reinterpret_cast(p.v_q + r * HD) + pc); + else + vx = __ldg(reinterpret_cast(p.v_pool + r * HD) + pc); + } + *reinterpret_cast(reinterpret_cast(&S.v[c][0]) + pc * 16) = vx; + } } for (int i = t; i < CH * 4; i += THREADS) { const int c = i / 4, g = i % 4; @@ -155,6 +191,9 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_kernel(const float* __res if constexpr (KV_MODE == 1) { a = __half2float(__ushort_as_half(p.k_scale[r * (HD / KV_Q8_GROUP) + g])); b = __half2float(__ushort_as_half(p.v_scale[r * (HD / KV_Q8_GROUP) + g])); + } else if constexpr (KV_MODE == 3) { // K as int8, V dequantized to fp16 (scale 1) + a = __half2float(__ushort_as_half(p.k_scale[r * (HD / KV_Q8_GROUP) + g])); + b = 1.0f; } else { a = b = 1.0f; } @@ -184,7 +223,7 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_kernel(const float* __res al[1] = *reinterpret_cast(&S.ql[gid + 8][k0 + 2 * tig]); al[2] = *reinterpret_cast(&S.ql[gid][k0 + 2 * tig + 8]); al[3] = *reinterpret_cast(&S.ql[gid + 8][k0 + 2 * tig + 8]); - if constexpr (KV_MODE == 1) { + if constexpr (KV_MODE != 0) { // modes 1 and 3: the K side is INT8 codes b[0] = i8x2_to_h2(*reinterpret_cast(&S.k[cb + gid][k0 + 2 * tig])); b[1] = i8x2_to_h2(*reinterpret_cast(&S.k[cb + gid][k0 + 2 * tig + 8])); } else { @@ -654,6 +693,10 @@ bool qsa_prompt_attn_batch(const float* q, const QsaAttnPools& pools, const int3 !steps || !pools.page_table) return false; cudaStream_t st = (cudaStream_t) stream; + if (pools.k_q != nullptr && pools.v_q4 != nullptr) { // hybrid K8V4: int8 K + dequantized-q4 V + if (!pools.k_scale) return false; + return launch<3>(q, pools, ids, steps, cap, s, attn, n_q, st); + } if (pools.k_q != nullptr) { if (!pools.v_q || !pools.k_scale || !pools.v_scale) return false; // STRATA_PROMPT_ATTN_V1=1 (debug): the first version, same accuracy, another summation order - the control diff --git a/src/kernels/kv_hybrid_parity.cpp b/src/kernels/kv_hybrid_parity.cpp index 1e05a80a..f5eeab9a 100644 --- a/src/kernels/kv_hybrid_parity.cpp +++ b/src/kernels/kv_hybrid_parity.cpp @@ -313,18 +313,6 @@ int main() { if (!bok) g_fail = 1; } - // The tensor-core prompt path (qsa_prompt_attn, 0.1.22+) supports fp16 and INT8 whole formats only; - // hybrid pools must make it refuse (its !v_q check) so the old mode-3 kernel runs - the guarantee the - // prefill fallback relies on, as a test (PR review). - { - float* d_q3 = dalloc((size_t) QH * D); - float* d_at3 = dalloc((size_t) QH * D); - const bool took = k::qsa_prompt_attn_batch(d_q3, pools, d_ids, d_step, cells, s, d_at3, 1, nullptr); - const bool refused = !took; - std::printf("[5/5] qsa_prompt_attn refuses hybrid pools: %s\n", refused ? "PASS (falls back)" : "FAIL"); - if (!refused) g_fail = 1; - } - std::vector ref_deq, ref_true; host_attn(Kdq, Vdq_un, ref_deq); host_attn(K, V, ref_true); @@ -333,6 +321,31 @@ int main() { max_deq = std::max(max_deq, (double) std::fabs(h_attn[i] - ref_deq[i])); max_true = std::max(max_true, (double) std::fabs(h_attn[i] - ref_true[i])); } + // The tensor-core prompt path (qsa_prompt_attn, 0.1.22+) takes hybrid pools as KV_MODE 3: INT8 K, and V + // dequantized from its q4_0 blocks to fp16 at gather. Its summation order differs from the old kernel's + // (accuracy-level, not bitwise - qsa_prompt_attn.hpp says the same of its int8 mode), so: tolerance check + // against the SAME dequant reference, output un-rotated exactly as prefill.cpp does. + { + float* d_at4 = dalloc((size_t) QH * D); + const bool took = k::qsa_prompt_attn_batch(d_q, pools, d_ids, d_step, cells, s, d_at4, 1, nullptr); + if (!took) { + std::printf("[5/5] qsa_prompt_attn mode 3: FAIL (refused the hybrid pools)\n"); + g_fail = 1; + } else { + k::fwht256_inplace_cuda(d_at4, QH, nullptr); + ck(cudaDeviceSynchronize(), "at4 sync"); + std::vector h_at4((size_t) QH * D); + ck(cudaMemcpy(h_at4.data(), d_at4, h_at4.size() * 4, cudaMemcpyDeviceToHost), "at4"); + double max_p = 0.0; + for (size_t i = 0; i < h_at4.size(); ++i) + max_p = std::max(max_p, (double) std::fabs(h_at4[i] - ref_deq[i])); + const bool pok = max_p < 1e-2; + std::printf("[5/5] qsa_prompt_attn mode 3 (tensor cores): %s (vs dequant ref %.2e)\n", + pok ? "PASS" : "FAIL", max_p); + if (!pok) g_fail = 1; + } + } + const bool attn_ok = max_deq < 5e-3; // fp32 kernel math vs fp64-ish host accumulation std::printf("[3/3] mode-3 attention + output fwht: %s (vs dequant ref %.2e, vs true fp32 %.2e)\n", attn_ok ? "PASS" : "FAIL", max_deq, max_true); diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 4def74c2..cc1640e7 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -1316,7 +1316,8 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e pt.mark(kPfQsaAttn, cs); // perf-review D-1: the whole chunk on tensor cores, one block per (query, KV head), FP32-level // accuracy but not bitwise (qsa_prompt_attn.hpp). Q4_0 KV, or STRATA_PROMPT_ATTN_OLD=1: the - // decode kernel, 32 queries at a time + // decode kernel, 32 queries at a time (K8V4 runs the tensor kernel's mode 3: INT8 K, + // V dequantized from its q4_0 blocks to fp16 at gather) static const bool old_attn = std::getenv("STRATA_PROMPT_ATTN_OLD") != nullptr; if (old_attn || !strata::kernels::qsa_prompt_attn_batch(m.q, pools, m.sel_ids, m.steps_dev, m.cap, s, m.attn, T, m.cs))