From d976db345462093606efdb493cb7733542c2dade Mon Sep 17 00:00:00 2001 From: imanu Date: Mon, 12 Oct 2026 00:57:11 +0200 Subject: [PATCH] #2081 dequant: the prompt projections' BF16 dequant with 16-byte stores on sm_75 (bit-exact; STRATA_TC_SM75=0 keeps the per-group kernel) The per-group dequant kernel writes 2 bytes every 64 bytes; 0.1.43 coalesced only the Q8_0 path (#1720). On compute capability 7.5 the BF16 projections of the prompt path take a kernel that writes 16 bytes per thread; the values are the same (a conversion, no arithmetic), so the output is bit for bit the old kernel's. Other cards are untouched. --- include/strata/core/tc_sm75.hpp | 28 ++++++++++++++++++++++++++ src/kernels/cuda/dequant_bf16.cu | 34 +++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 include/strata/core/tc_sm75.hpp diff --git a/include/strata/core/tc_sm75.hpp b/include/strata/core/tc_sm75.hpp new file mode 100644 index 00000000..5b727607 --- /dev/null +++ b/include/strata/core/tc_sm75.hpp @@ -0,0 +1,28 @@ +// include/strata/core/tc_sm75.hpp - sm_75 (RTX 20 / Turing) tensor-core paths. +// +// Turing has no tf32 MMA and no cp.async, so three of this engine's prompt-path kernels are compiled out below +// sm_80 and fall back to slower paths (see `qsa_prompt_attn.cu`, `qsa_select.cu`). It does have fp16 MMA +// (m16n8k8; m16n8k16 is sm_80+) and cuBLAS has no bf16 GEMM kernel below sm_80 - it runs an FFMA one, which +// measured 19.5% of the prompt's GPU time on a 2x2080 Ti. These switches select the Turing paths. +// +// **ONLY sm_75 IS EVER AFFECTED.** The predicate below answers true for compute capability 7.5 and for nothing +// else: cc 7.0/7.2 (Volta) keeps its own behaviour, and every cc >= 8 keeps the paths the engine was written +// with. `STRATA_TC_SM75` can only turn the Turing paths OFF (for an A/B or a bisect); it cannot turn them on +// somewhere else, so a machine that is not a 2080 Ti cannot be moved onto them by an environment variable. +// The kernels themselves are gated a second time, at compile time, by `__CUDA_ARCH__ == 750`. +#pragma once + +#include + +namespace strata::core { + +/// The Turing paths are for compute capability 7.5 exactly. `auto` (the default): on. "0": off (the +/// pre-patch behaviour). Any other value, and any other device, is off. +inline bool tc_sm75_enabled(int cc_major, int cc_minor) { + if (cc_major != 7 || cc_minor != 5) return false; // 7.5 and only 7.5 - never another SM model + const char* e = std::getenv("STRATA_TC_SM75"); + if (e != nullptr && e[0] == '0') return false; + return true; +} + +} // namespace strata::core diff --git a/src/kernels/cuda/dequant_bf16.cu b/src/kernels/cuda/dequant_bf16.cu index 97759ae7..fe118639 100644 --- a/src/kernels/cuda/dequant_bf16.cu +++ b/src/kernels/cuda/dequant_bf16.cu @@ -3,6 +3,7 @@ // Arithmetic transcribed from ggml/src/ggml-quants.c at the pinned llama.cpp (MIT License, Copyright (c) 2023-2026 // The ggml authors): dequantize_row_q2_0/q4_0/q5_0/q8_0/q3_K/q4_K/q5_K/q6_K/iq4_nl/iq4_xs. #include "strata/kernels/dequant_bf16.hpp" +#include "strata/core/tc_sm75.hpp" #include "strata/kernels/iq_kernels.hpp" #include @@ -166,13 +167,28 @@ __device__ __forceinline__ void group32(const uint8_t* row_blocks, int gi_in_row } } -template +/// `VEC` (fork, Daily be4cf1c, sm_75 only): the group is dequantised into a register buffer and flushed with 16-byte +/// stores. The plain form writes each element where it is computed, which for a warp means 32 store instructions +/// whose lanes are 64 bytes apart - 32 two-byte transactions per instruction. **The values are identical** (the +/// same arithmetic, the same order), only the stores are wider; the Turing build measured this kernel at 18% of the +/// row's bandwidth. (0.1.42's Q8_0 kernel below already writes coalesced and is left as it is.) +template __global__ void dequant_kernel(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, int64_t rows, int64_t groups_per_row, int64_t ld, T* __restrict__ out) { const int64_t g = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; if (g >= rows * groups_per_row) return; const int64_t r = g / groups_per_row, gi = g % groups_per_row; - group32(blocks + (row0 + r) * row_bytes, (int) gi, out + r * ld + gi * 32); + T* dst = out + r * ld + gi * 32; + if constexpr (!VEC) { + group32(blocks + (row0 + r) * row_bytes, (int) gi, dst); + } else { + __align__(16) T buf[32]; // the uint4 flush below reads it as 16-byte vectors + group32(blocks + (row0 + r) * row_bytes, (int) gi, buf); + // 32 elements: 64 B for the 16-bit forms (4 x uint4), 128 B for the float one (8 x uint4) + constexpr int CHUNKS = (sizeof(T) == 2 ? 4 : 8); +#pragma unroll + for (int c = 0; c < CHUNKS; ++c) reinterpret_cast(dst)[c] = reinterpret_cast(buf)[c]; + } } // ---- the wide kernels: a thread per 8 values (Pascal, sm_61) -------------------------------------------------------- @@ -359,7 +375,19 @@ void launch(int type, const void* blocks, int64_t row0, int64_t rows, int64_t co if (e != cudaSuccess) { std::fprintf(stderr, "dequant launch: %s\n", cudaGetErrorString(e)); std::exit(1); } return; } -#define STRATA_DQ(TY) dequant_kernel<<>>(p, row_bytes, row0, rows, gpr, ld, out); break + // sm_75 (and only cc 7.5) takes the vectorised-store form when every group's 32 outputs start on 16 bytes (an + // aligned `out` and a row stride of whole 16-byte units); every other SM model keeps the plain kernel + int dev = 0, cmaj = 0, cmin = 0; + const bool vec = (reinterpret_cast(out) & 15) == 0 && ((uint64_t) ld * sizeof(T)) % 16 == 0 && + cudaGetDevice(&dev) == cudaSuccess && + cudaDeviceGetAttribute(&cmaj, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && + cudaDeviceGetAttribute(&cmin, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess && + strata::core::tc_sm75_enabled(cmaj, cmin); + if (!vec) cudaGetLastError(); +#define STRATA_DQ(TY) \ + if (vec) dequant_kernel<<>>(p, row_bytes, row0, rows, gpr, ld, out); \ + else dequant_kernel<<>>(p, row_bytes, row0, rows, gpr, ld, out); \ + break switch (type) { case 2: STRATA_DQ(2); case 6: STRATA_DQ(6);