Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

prefill gemm below sm_80: Volta on the FP16 path by default, Turing opt-in, Pascal through fp32 (#655 #540 #395)

Niko1221456950beecaf
diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hpp
index 6915ee691..60d1c7174 100644
--- a/include/strata/prefill/gemm.hpp
+++ b/include/strata/prefill/gemm.hpp
@@ -26,7 +26,8 @@ public:
std::string& err);
/// Y[T, N] (fp32, row stride ldy) = X[T, K] (bf16, row-major) . W[N, K]^T (bf16, row-major). `beta` = 1 adds.
- /// sm_7x (no BF16 tensor cores) converts both to FP16 and runs the FP16 tensor-core GEMM (STRATA_BF16_TC).
+ /// Below sm_80 (no BF16 tensor cores): Volta converts both to FP16 and runs the FP16 tensor-core GEMM (Turing with
+ /// STRATA_BF16_TC=1), Pascal widens both to fp32 (cublasSgemm); see gemm.cu.
void bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy = 0,
float beta = 0.0f);
@@ -53,7 +54,7 @@ private:
void* workspace_ = nullptr;
bool external_ = false;
void* hipblaslt_state_ = nullptr;
- // sm_7x: FP16 copies of a BF16 product's weight and activation slice (Gemm::bf16, STRATA_BF16_TC)
+ // below sm_80: FP16 (Pascal: fp32) copies of a BF16 product's weight and activation slice (Gemm::bf16)
uint16_t* tc_w_ = nullptr;
int64_t tc_w_elems_ = 0;
uint16_t* tc_x_ = nullptr;
diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu
index babeee80f..1d71bbb70 100644
--- a/src/prefill/gemm.cu
+++ b/src/prefill/gemm.cu
@@ -7,6 +7,7 @@
#include <cuda_runtime.h>
#include <algorithm>
+#include <atomic>
#if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE)
// The HIP compatibility shim maps CUDA shuffle spellings to Strata helpers.
@@ -377,33 +378,74 @@ bool Gemm::init(void* stream, int64_t scratch_elems, std::string& err) {
return true;
}
-// ---- sm_7x: BF16 GEMMs through the FP16 tensor cores ---------------------------------------------------------------
+#if !defined(__HIPCC__)
+// ---- below sm_80: BF16 GEMMs without BF16 tensor cores (PR #655, #540, #395) ----------------------------------------
// Turing and Volta have no BF16 tensor cores: cublasGemmEx on CUDA_R_16BF inputs falls back to a SIMT fp32 kernel
-// (magma_sgemmEx), a fifth of a 4K prompt on an RTX 2080 Ti. BF16 -> FP16 is exact for every value inside
-// FP16's range (the 7-bit mantissa fits in 10 bits); weights and normalized activations sit there. With
-// STRATA_BF16_TC (default on for compute capability 7.x), the weight is converted once per call and the activations in row slices,
-// into the instance's own scratch, and the product runs as the FP16 GEMM (fp32 accumulate) on the tensor cores.
+// (magma_sgemmEx), a fifth of a 4K prompt on an RTX 2080 Ti (#655) and ~10% of a V100's prompt (#540). BF16 -> FP16
+// is exact for every value inside FP16's normal range (the 7-bit mantissa fits in 10 bits); weights and normalized
+// activations sit there. With the FP16 path, the weight is converted once per call and the activations in row
+// slices, into the instance's own buffers, and the product runs as the FP16 GEMM (fp32 accumulate) on the tensor
+// cores. Finite values beyond FP16's range are clamped to +-65504 (#540) instead of becoming Inf. A beta = 1
+// product (STRATA_PREFILL_BF16X2's remainder, ~2^-9 of the original, deep in FP16's subnormal band) keeps cuBLAS.
+// Default: on for compute capability 7.0 - 7.4 (Volta: only the experimental STRATA_EXPERIMENTAL_SM60 build runs
+// there), OFF for 7.5 (RTX 20 in the ready-made engine: the sums round differently, so it is opt-in until it has
+// been gated). STRATA_BF16_TC=1 / =0 turns it on / off on any 7.x card.
+// Pascal (6.x) has no tensor cores and cuBLAS has no BF16 GEMM for it (#395, measured NOT_SUPPORTED on a P40): both
+// operands are widened to fp32 by an exact shift and the product is cublasSgemm with the same fp32 accumulator.
namespace {
__global__ void bf16_to_f16_kernel(const uint16_t* __restrict__ in, __half* __restrict__ out, int64_t n) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
- if (i < n) out[i] = __float2half_rn(__uint_as_float((uint32_t) in[i] << 16));
+ if (i < n) {
+ float f = __uint_as_float((uint32_t) in[i] << 16);
+ if (f > 65504.0f && !isinf(f)) f = 65504.0f;
+ else if (f < -65504.0f && !isinf(f)) f = -65504.0f;
+ out[i] = __float2half_rn(f);
+ }
+}
+__global__ void bf16_to_f32_kernel(const uint16_t* __restrict__ in, float* __restrict__ out, int64_t n) {
+ const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
+ if (i < n) out[i] = __uint_as_float((uint32_t) in[i] << 16);
}
void bf16_to_f16(const uint16_t* in, uint16_t* out, int64_t n, cudaStream_t st) {
if (n <= 0) return;
bf16_to_f16_kernel<<<(unsigned) ((n + 255) / 256), 256, 0, st>>>(in, reinterpret_cast<__half*>(out), n);
}
-int bf16_tc_mode() {
- static int mode = [] {
+void bf16_to_f32(const uint16_t* in, float* out, int64_t n, cudaStream_t st) {
+ if (n <= 0) return;
+ bf16_to_f32_kernel<<<(unsigned) ((n + 255) / 256), 256, 0, st>>>(in, out, n);
+}
+// The current device's compute capability as 10 * major + minor, per device (a layer split can mix cards); 0: not
+// known, read as "not an old card" so a failed query keeps the cuBLAS BF16 call.
+int current_cc() {
+ static std::atomic<int> cc[64] = {};
+ int dev = 0;
+ if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return 0; }
+ int v = cc[dev].load(std::memory_order_relaxed);
+ if (v == 0) {
+ int major = 0, minor = 0;
+ if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess ||
+ cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev) != cudaSuccess) {
+ cudaGetLastError();
+ return 0;
+ }
+ v = 10 * major + minor;
+ cc[dev].store(v, std::memory_order_relaxed);
+ }
+ return v;
+}
+// 0: cuBLAS's BF16 GEMM, 1: through FP16 (tensor cores), 2: through FP32 (Pascal)
+int bf16_path() {
+ const int cc = current_cc();
+ if (cc <= 0 || cc >= 80) return 0;
+ if (cc < 70) return 2;
+ static const int forced = [] {
const char* v = std::getenv("STRATA_BF16_TC");
- if (v != nullptr && v[0] != '\0') return std::atoi(v) != 0 ? 1 : 0;
- int dev = 0, maj = 0;
- cudaGetDevice(&dev);
- cudaDeviceGetAttribute(&maj, cudaDevAttrComputeCapabilityMajor, dev);
- return maj == 7 ? 1 : 0; // Volta (sm_70, sm_72) and Turing (sm_75)
+ return v != nullptr && v[0] != '\0' ? (std::atoi(v) != 0 ? 1 : 0) : -1;
}();
- return mode;
+ if (forced >= 0) return forced;
+ return cc < 75 ? 1 : 0;
}
-bool grow(uint16_t*& p, int64_t& have, int64_t want) {
+bool grow(uint16_t*& p, int64_t& have, int64_t want) { // `have`, `want`: 2-byte elements
if (have >= want) return true;
if (p) cudaFree(p);
p = nullptr;
@@ -412,8 +454,9 @@ bool grow(uint16_t*& p, int64_t& have, int64_t want) {
have = want;
return true;
}
-constexpr int64_t kXSliceElems = 16ll << 20; // 32 MiB of FP16 activations per slice
+constexpr int64_t kXSliceElems = 16ll << 20; // 32 MiB of FP16 activations per slice (64 MiB as fp32)
} // namespace
+#endif
void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy,
float beta) {
@@ -428,7 +471,8 @@ void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64
}
#endif
#if !defined(__HIPCC__)
- if (bf16_tc_mode() && N > 1) { // a single output row stays cuBLAS's GEMV, faster than the conversions
+ if (const int path = K > 0 ? bf16_path() : 0; path == 1 && N > 1 && beta == 0.0f) {
+ // a single output row stays cuBLAS's GEMV, faster than the conversions; beta = 1: see above
const int64_t x_rows = std::max<int64_t>(1, std::min<int64_t>(T, kXSliceElems / K));
if (grow(tc_w_, tc_w_elems_, N * K) && grow(tc_x_, tc_x_elems_, x_rows * K)) {
bf16_to_f16(W, tc_w_, N * K, (cudaStream_t) stream_);
@@ -439,6 +483,23 @@ void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64
}
return;
}
+ } else if (path == 2) {
+ // Pascal: fp32 copies (2 elements of the 2-byte buffers each). Every tile is a disjoint block of Y, so each
+ // gets the caller's beta.
+ const int64_t x_rows = std::max<int64_t>(1, std::min<int64_t>(T, kXSliceElems / K));
+ if (grow(tc_w_, tc_w_elems_, 2 * N * K) && grow(tc_x_, tc_x_elems_, 2 * x_rows * K)) {
+ float* const wf = reinterpret_cast<float*>(tc_w_);
+ float* const xf = reinterpret_cast<float*>(tc_x_);
+ bf16_to_f32(W, wf, N * K, (cudaStream_t) stream_);
+ for (int64_t t0 = 0; t0 < T; t0 += x_rows) {
+ const int64_t n = std::min<int64_t>(x_rows, T - t0);
+ bf16_to_f32(X + t0 * K, xf, n * K, (cudaStream_t) stream_);
+ ck(cublasSgemm((cublasHandle_t) handle_, CUBLAS_OP_T, CUBLAS_OP_N, (int) N, (int) n, (int) K, &alpha,
+ wf, (int) K, xf, (int) K, &beta, Y + t0 * ldy, (int) ldy),
+ "cublasSgemm (bf16 on Pascal)");
+ }
+ return;
+ }
}
#endif
// Column-major view: Y^T[N, T] = W[N, K] (stored K x N col-major, transposed) . X^T[K, T].
diff --git a/src/prefill/gemm_bf16_parity.cu b/src/prefill/gemm_bf16_parity.cu
index 00d3d0642..a94700517 100644
--- a/src/prefill/gemm_bf16_parity.cu
+++ b/src/prefill/gemm_bf16_parity.cu
@@ -1,7 +1,9 @@
// src/prefill/gemm_bf16_parity.cu - Gemm::bf16 against cuBLAS's own BF16 product (GPU, synthetic, no model).
//
-// On sm_7x (Volta / Turing, no BF16 tensor cores) Gemm::bf16 converts the weight and the activations to FP16 and runs the FP16
-// tensor-core GEMM (STRATA_BF16_TC, default on there); everywhere else it is the cuBLAS BF16 call itself. This
+// Below sm_80 (no BF16 tensor cores) Gemm::bf16 converts the weight and the activations to FP16 and runs the FP16
+// tensor-core GEMM (Volta by default, Turing with STRATA_BF16_TC=1) or widens them to fp32 (Pascal); everywhere else
+// it is the cuBLAS BF16 call itself. (On Pascal the cuBLAS BF16 reference itself may be refused: the test then fails
+// at the reference, which says so.) This
// checks the result against cublasGemmEx on the same BF16 inputs, within fp32-accumulation rounding, over the prompt
// path's shapes: the hyper-connection down / up projections, the router and indexer rows, the PLE value matrix, a T
// large enough to slice the activations, beta = 1 accumulation (the bf16x2 low parts) and an output row stride wider