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++

HIP gfx103x: the prompt path's 16-bit GEMMs in FP16 in and out (prompt reads ~2x on RX 6900 XT)

xjc10972cad1bc561
diff --git a/bench/results/2026-10-04-rdna2-fp16-prompt/README.md b/bench/results/2026-10-04-rdna2-fp16-prompt/README.md
new file mode 100644
index 000000000..9253715b4
--- /dev/null
+++ b/bench/results/2026-10-04-rdna2-fp16-prompt/README.md
@@ -0,0 +1,114 @@
+# RX 6900 XT (gfx1030): the prompt path's 16-bit GEMMs in FP16 in and out (2026-10-04)
+
+On gfx103x (RDNA2) the prompt path ran its 16-bit GEMMs as rocBLAS FP16-in / FP32-out (`Gemm::f16`, `Gemm::native`) and
+BF16-in / FP32-out (`Gemm::bf16`). rocBLAS on gfx1030 has tuned kernels for FP16-in / **FP16-out** only; the other two
+fall back to generic kernels about 6.6x slower. This change runs those GEMMs FP16 in and out on gfx103x, writing the FP16
+result into the start of each FP32 row of Y and widening it there (no extra buffer), and has the kernels that produce the
+BF16 GEMMs' activation images (`gr_*`, `to_bf16`) write FP16 instead. `STRATA_HIP_PROMPT_F16=0` is the old path, so every
+number below is the same binary with and without that switch.
+
+## Rig
+
+- 2x AMD Radeon RX 6900 XT 16 GB (gfx1030), each PCIe 4.0 x8, no P2P used. AMD Ryzen 5 5600X, 128 GB DDR4-3200.
+- Ubuntu 26.04, kernel 7.0.0-38-generic, ROCm 10.0.0 (HIP 7.15.26333, rocBLAS 5.6).
+- Engine: `origin/main` at `6f32ec0` (0.1.39) with this change, `-DSTRATA_ENABLE_HIP=ON -DCMAKE_HIP_ARCHITECTURES=gfx1030
+ -DSTRATA_PREFILL_MMQ=ON -DSTRATA_NATIVE_EXPERTS=ON`.
+- Model: Qwen3.8-Flash-Next GSQ-RCO IQ3_S (2 shards, `--native`), MTP draft layer on, `--kv int8 --kv-resident 32768
+ --max-context 131072`, `--expert-cache auto`. Not one of setup's models (see the limits).
+
+## Why: rocBLAS on gfx1030
+
+A 7.3K-token prompt on one card (rocprofv3, 0.1.38): rocBLAS `Cijk_..._HSS_..._MT64x32x8` (FP16 in, FP32 out, a fallback
+tile) was 54% of the prompt's 15.2 s of GPU time, the BF16 products (`BSS`) another ~15%. Microbenchmark, N = 10240,
+T = 7313, K = 2560:
+
+| rocBLAS GEMM on gfx1030 | TFLOPS |
+| --- | ---: |
+| FP16 in, FP32 out (HSS) | 5.6 |
+| BF16 in, any out | ~5.3 |
+| **FP16 in, FP16 out (HH, fp32 accumulate)** | **37.7** |
+
+## Prompt speed
+
+One card, cold server per run, the first message of each chat below read by the batched prompt path; the
+server's own line `strata serve: prompt N tokens = 0 reused + N read in T ms`. Three runs per path (two of them the
+distribution check's), all within 0.3%:
+
+| Prompt tokens | old path (`STRATA_HIP_PROMPT_F16=0`) | FP16 | gain | read time, old -> FP16 |
+| ---: | ---: | ---: | ---: | ---: |
+| 9,427 | 439 tok/s | 744 tok/s | +69% | 21.5 s -> 12.7 s |
+| 34,659 | 466 tok/s | 915 tok/s | +96% | 74.4 s -> 37.9 s |
+| 105,811 | 461 tok/s | 926 tok/s | +101% | 229.5 s -> 114.3 s |
+
+Decode is unchanged within the noise (44-48 tok/s on one card): it does not use these GEMMs.
+
+Both cards, cold server per run, one 8,275- and one 33,586-token prompt of repository text (128 tokens answered):
+
+| | old path | FP16 | gain |
+| --- | ---: | ---: | ---: |
+| `--layer-split auto` (25 / 23 layers), 8.3K | 444 tok/s | 833 tok/s | +88% |
+| `--layer-split auto`, 33.6K | 707 tok/s | 1,356 tok/s | +92% |
+| one card + the second as expert helper (`--expert-cache-device1 auto --remote-expert-opt`), 8.3K | 432 tok/s | 798 tok/s | +85% |
+| the same, 33.6K | 454 tok/s | 887 tok/s | +95% |
+
+Decode is the same within the noise in both setups (these short answers: 51-58 tok/s split, 36-42 helper).
+
+## Range check
+
+FP16 ends at 65504 where BF16 reaches 3.4e38, so the precondition is that nothing the prompt path feeds these GEMMs or gets
+out of them leaves FP16's range. Measured on 0.1.38 with a diagnostic counter (not part of this change) over 33.7K tokens of
+English docs, 43.3K of Chinese logs and 32.4K of C++ on one card: every BF16 GEMM's X and W and every FP16 output - **0**
+values beyond 65504, **0** infinities. Non-zero |x| < 6.1e-5 (FP16's subnormal band): 0.008-1.8% of X, 0.1-0.9% of W.
+Out-of-range values would still be finite: the activation writers saturate to +-65504 (`hf_sat`, as the FP16 images
+already did), a NaN stays a NaN.
+
+## Distribution check (teacher-forced)
+
+The method of `bench/results/2026-10-03-v100-prompt-attn` (`docs/UNSLOTH_Q4.md`): `STRATA_LOGPOS=<file>
+STRATA_LOGPOS_TOPK=256` on a serve engine (one card, `--short-read 640 --adapt-every 100000`, greedy). Each chat is a long
+first message of this repository's docs and source (9,427 / 34,659 / 105,811 tokens, disjoint, read by the batched prompt
+path - the code under test), a fixed assistant reply, and a last message of ~580 tokens of other repository text that is
+read through the verify windows, where every position is scored: 578 / 519 / 481 positions. KL is P || Q over P's top 256
+plus one bucket for the rest.
+
+| comparison | KL mean (9K / 35K / 106K) | KL median | argmax same | top-10 overlap |
+| --- | --- | --- | --- | --- |
+| old path vs old path (a second run) | 0 / 0 / 0 | 0 | 100% | 100% |
+| FP16 vs FP16 (a second run) | 0 / 0 / 0 | 0 | 100% | 100% |
+| old path vs FP16 | 0.032 / 0.021 / 0.019 | 0.0056 / 0.0041 / 0.0038 | 94.5 / 95.0 / 96.5% | 91.4 / 92.8 / 91.5% |
+| **reference vs old path** | 0.061 / 0.033 / 0.023 | 0.0092 / 0.0096 / 0.0055 | 93.1 / 93.3 / 93.8% | 90.6 / 92.8 / 91.9% |
+| **reference vs FP16** | 0.020 / 0.015 / 0.028 | 0.0034 / 0.0044 / 0.0038 | 95.5 / 96.5 / 95.8% | 92.7 / 93.2 / 91.6% |
+
+The reference is the old path with `STRATA_PREFILL_BF16X2=1` (every BF16 activation image carries its low part too, close
+to fp32 activations; 393-411 tok/s). The FP16 path is closer to it than the old path in all three chats by median KL and
+argmax agreement, and by mean KL in two of three (in the 106K chat the FP16 path's p99 is lower, 0.20 vs 0.24, and its mean is pulled up
+by a few positions beyond that). That is the expected direction: an FP16 activation keeps 10 mantissa bits where BF16 keeps 7, which outweighs
+rounding the GEMM's output to FP16 before it is widened.
+
+A rounding-order control as in the V100 report did not work here: `--prefill 2048` instead of auto (8192; the smaller
+chunk did take effect, 1,059 lent slots instead of the 8192-token ring) gave results **bit-identical** to the old path, so on
+this path the chunking does not change the arithmetic and cannot serve as a control.
+
+## Output checks
+
+- **Needle recall** (`tools/needle_bench.py --lengths 8k,32k,128k --depths 10,30,50,70,90`, greedy, thinking off, a fresh
+ server per path, one card; prompts of 7.9K, 32.2K and 125.4K tokens): **15 of 15 found with the old path, 15 of 15 with
+ FP16.** A 125K-token read takes 275 s on the old path, 139 s with FP16 (the later depths are shorter because the server
+ reuses the checkpoint of the text before the needle).
+- **Repeatability:** two runs of each path give bit-identical log-probabilities (table above), and no run of this report
+ (17 server starts) stalled.
+
+## What changed
+
+- `Gemm::set_f16_io(bool)`: off by default, so every caller other than the prompt path (e.g. `gemm_bf16_parity`) runs what
+ it ran. `Prefill::init` turns it on together with `set_act_f16` when `prompt_f16()` is true for its device.
+- `prompt_f16()`: gfx103x, cached per device (a layer split can mix cards); `STRATA_HIP_PROMPT_F16=0/1` overrides.
+- With it on: `Gemm::f16` / `Gemm::native` run HH into Y's own rows (`ldc = 2 ldy` halves) and `widen_rows_f16` widens each
+ row from its end; `Gemm::bf16` converts W row slices to FP16 through the existing dequantization scratch (as
+ `native()` does) and takes X as the FP16 image. `bf16x2_mode()` is 0 there (the BF16 low part has no FP16 meaning).
+- No new device memory, no host sync, no fallback branch; CUDA builds are unchanged (`#if defined(__HIPCC__)`).
+
+## Limits
+
+- One machine, one model (IQ3_S, packed from the GSQ-RCO GGUF, not one of setup's), one corpus for the distribution check.
+- gfx1031/1032 (the same rocBLAS family) are enabled by the same `gfx103` prefix but were not run.
diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hpp
index d2960a2f4..1eae68b45 100644
--- a/include/strata/prefill/gemm.hpp
+++ b/include/strata/prefill/gemm.hpp
@@ -47,6 +47,11 @@ public:
void native(const uint16_t* X, int ggml_type, const void* W_blocks, float* Y, int64_t T, int64_t N, int64_t K,
int64_t ldy = 0, float beta = 0.0f, int64_t ldx = 0);
+ /// HIP gfx103x (prompt_f16()): bf16() takes X as the FP16 image the prompt path's kernels write (set_act_f16),
+ /// and bf16() / f16() / native() run the GEMM with FP16 out (rocBLAS's tuned kernels there), widened in place in
+ /// Y's own rows. Off (default): every call is what it was. Set by Prefill::init, together with set_act_f16.
+ void set_f16_io(bool on) { f16_io_ = on; }
+
/// Caller-owned buffers only: the scratch and workspace moved (the prompt path laid its buffers out again).
void rebind(uint16_t* scratch, int64_t scratch_elems, void* workspace, size_t ws_bytes);
@@ -62,6 +67,7 @@ private:
void* workspace_ = nullptr;
bool external_ = false;
void* hipblaslt_state_ = nullptr;
+ bool f16_io_ = false;
// 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;
@@ -72,8 +78,15 @@ private:
void* mmq_ctx_ = nullptr;
void* mmq_buf_ = nullptr;
bool mmq_failed_ = false;
+ /// Y = X . W^T with FP16 out, written into Y's own rows and widened there (no buffer): prompt_f16() only.
+ void f16_inplace(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy);
};
+/// HIP on gfx103x (RDNA2), for the current device: rocBLAS's tuned GEMMs there are FP16 in -> FP16 out only (FP16 or
+/// BF16 in -> FP32 out runs ~6x slower), so the prompt path's 16-bit GEMMs run in FP16 (Gemm::set_f16_io,
+/// set_act_f16). Cached per device (a layer split can mix cards). STRATA_HIP_PROMPT_F16=0/1 overrides.
+bool prompt_f16();
+
} // namespace strata::prefill
diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp
index 18f51be15..f7a058c1a 100644
--- a/include/strata/prefill/kernels.hpp
+++ b/include/strata/prefill/kernels.hpp
@@ -40,6 +40,8 @@ void gr_write_cvec_norm_rs(float* R, const float* bo, const float* inj, int64_t
uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr, int64_t ldx = 0);
/// lo16[t, k] = bf16(silu(lo[t, k] / hc))
void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream, uint16_t* lo16_lo = nullptr);
+/// The BF16-weight GEMMs' activation image (gr_* kernels, to_bf16) in FP16 instead of BF16, on the current device.
+void set_act_f16(bool on);
/// mixed[t, d] = mean_c xn[t, c, d] * sigmoid(gated[t, c, d]); FP32, BF16 and FP16 (either image may be null).
void gr_mix(const float* xn, const float* gated, float* mixed, uint16_t* mixed16, int64_t T, void* stream,
uint16_t* mixed_h = nullptr, uint16_t* mixed16_lo = nullptr);
diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu
index 762cf8dcc..7299e6345 100644
--- a/src/prefill/gemm.cu
+++ b/src/prefill/gemm.cu
@@ -27,6 +27,7 @@
#undef __ballot_sync
#endif
+#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
@@ -43,8 +44,36 @@
#include <tuple>
#endif
+#if defined(__HIPCC__)
+#include <hip/hip_fp16.h>
+#endif
+
namespace strata::prefill {
namespace {
+#if defined(__HIPCC__)
+// Y's rows, written by an FP16-out GEMM as FP16 at the start of each FP32 row (ldc = 2 ldy halves), widened in place.
+// Float c overwrites halves 2c and 2c+1, so a row is walked from its end in blocks: a block's halves are read into
+// registers, the block syncs, then writes its floats - which only cover halves of blocks already read.
+__global__ void widen_rows_f16(float* __restrict__ Y, int64_t n, int64_t ldy) {
+ float* y = Y + (int64_t) blockIdx.x * ldy;
+ const __half* h = reinterpret_cast<const __half*>(y);
+ const int64_t nb = (n + blockDim.x - 1) / blockDim.x;
+ for (int64_t b = nb - 1; b >= 0; --b) {
+ const int64_t c = b * blockDim.x + threadIdx.x;
+ const float v = c < n ? __half2float(h[c]) : 0.0f;
+ __syncthreads();
+ if (c < n) y[c] = v;
+ __syncthreads();
+ }
+}
+// BF16 weight rows -> FP16, saturated (bench/results/2026-10-04-rdna2-fp16-prompt: none leaves FP16's range)
+__global__ void bf16_to_f16_rows(const uint16_t* __restrict__ s, __half* __restrict__ d, int64_t n) {
+ for (int64_t i = blockIdx.x * (int64_t) blockDim.x + threadIdx.x; i < n; i += (int64_t) gridDim.x * blockDim.x) {
+ const float f = __uint_as_float((uint32_t) s[i] << 16);
+ d[i] = __float2half(isnan(f) ? f : fminf(fmaxf(f, -65504.0f), 65504.0f)); // as hf_sat: a NaN stays NaN
+ }
+}
+#endif
void ck(cublasStatus_t s, const char* what) {
if (s != CUBLAS_STATUS_SUCCESS) {
@@ -315,6 +344,30 @@ bool try_hipblaslt(void* opaque_state, strata::prefill::hipblaslt::InputType typ
} // namespace
+bool prompt_f16() {
+#if defined(__HIPCC__)
+ constexpr int kMaxDev = 64;
+ static std::atomic<int8_t> cached[kMaxDev] = {}; // 0 unknown, 1 off, 2 on
+ int dev = 0;
+ if (hipGetDevice(&dev) != hipSuccess || dev < 0 || dev >= kMaxDev) { (void) hipGetLastError(); return false; }
+ if (const int8_t c = cached[dev].load(std::memory_order_relaxed)) return c == 2;
+ bool on = false;
+ const char* e = std::getenv("STRATA_HIP_PROMPT_F16");
+ hipDeviceProp_t p{};
+ if (e && e[0] == '0') on = false;
+ else if (e && e[0] == '1') on = true;
+ else if (hipGetDeviceProperties(&p, dev) == hipSuccess) on = std::strncmp(p.gcnArchName, "gfx103", 6) == 0;
+ else (void) hipGetLastError();
+ if (on) std::fprintf(stderr, "strata prefill: HIP device %d%s%s - the prompt's 16-bit GEMMs run FP16 in and out (rocBLAS "
+ "is tuned only for that there; STRATA_HIP_PROMPT_F16=0 keeps BF16 in / FP32 out)\n",
+ dev, p.gcnArchName[0] ? " " : "", p.gcnArchName);
+ cached[dev].store(on ? 2 : 1, std::memory_order_relaxed);
+ return on;
+#else
+ return false;
+#endif
+}
+
Gemm::~Gemm() {
#if defined(STRATA_PREFILL_MMQ) && defined(__HIPCC__)
delete static_cast<strata::prefill::mmq::Context*>(mmq_ctx_);
@@ -523,6 +576,20 @@ void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64
strata_wmma_gemm_bf16(X, W, Y, T, N, K, ldy, beta, stream_)) {
return;
}
+#endif
+#if defined(__HIPCC__)
+ if (f16_io_) { // X is the FP16 image (set_act_f16); W goes through the dequantization scratch as FP16
+ // the only accumulating BF16 product is the BF16X2 low part, which bf16x2_mode() turns off with this path
+ if (beta != 0.0f) { std::fprintf(stderr, "prefill gemm: an accumulating BF16 GEMM on the FP16 prompt path\n"); std::exit(1); }
+ const int64_t rows = std::min<int64_t>(N, scratch_elems_ / K);
+ if (rows <= 0) { std::fprintf(stderr, "prefill gemm: scratch too small for K=%lld\n", (long long) K); std::exit(1); }
+ for (int64_t r0 = 0; r0 < N; r0 += rows) {
+ const int64_t n = std::min(rows, N - r0);
+ bf16_to_f16_rows<<<1024, 256, 0, (cudaStream_t) stream_>>>(W + r0 * K, (__half*) scratch_, n * K);
+ f16_inplace(X, scratch_, Y + r0, T, n, K, ldy);
+ }
+ return;
+ }
#endif
const float alpha = 1.0f;
#if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE)
@@ -589,6 +656,9 @@ void Gemm::f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_
strata_wmma_gemm_f16(X, W, Y, T, N, K, ldy, beta, stream_)) {
return;
}
+#endif
+#if defined(__HIPCC__)
+ if (f16_io_ && beta == 0.0f) { f16_inplace(X, W, Y, T, N, K, ldy); return; }
#endif
const float alpha = 1.0f;
#if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE)
@@ -671,6 +741,18 @@ bool Gemm::native_mmq(const uint16_t* X, int type, const void* W, float* Y, int6
return true;
}
#endif
+void Gemm::f16_inplace(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy) {
+#if defined(__HIPCC__)
+ const float one = 1.0f, zero = 0.0f;
+ ck(cublasGemmEx((cublasHandle_t) handle_, CUBLAS_OP_T, CUBLAS_OP_N, (int) N, (int) T, (int) K, &one, W, CUDA_R_16F,
+ (int) K, X, CUDA_R_16F, (int) K, &zero, Y, CUDA_R_16F, (int) (2 * ldy), CUBLAS_COMPUTE_32F,
+ CUBLAS_GEMM_DEFAULT),
+ "cublasGemmEx f16 out");
+ widen_rows_f16<<<(unsigned) T, 256, 0, (cudaStream_t) stream_>>>(Y, N, ldy);
+#else
+ (void) X; (void) W; (void) Y; (void) T; (void) N; (void) K; (void) ldy;
+#endif
+}
void Gemm::native(const uint16_t* X, int ggml_type, const void* W_blocks, float* Y, int64_t T, int64_t N, int64_t K,
int64_t ldy, float beta, int64_t ldx) {
diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu
index 00490a80b..b21757d25 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -45,6 +45,10 @@ __device__ __forceinline__ uint16_t hf(float f) { return __half_as_ushort(__floa
// NaN in the down projection (decode's q8_1 has room to ~8e6; FP16 ends at 65504). A NaN stays NaN (fminf/fmaxf
// would make it -65504 and hide where it came from); finite values below 65504 round exactly as before.
__device__ __forceinline__ uint16_t hf_sat(float f) { return hf(isnan(f) ? f : fminf(fmaxf(f, -65504.0f), 65504.0f)); }
+// The prompt path's 16-bit activation image for the BF16-weight GEMMs: BF16, or FP16 where the GEMM library is fast
+// only in FP16 (prompt_f16() in gemm.cu: rocBLAS on gfx103x). Set once per device before the first prompt.
+__device__ int g_act_f16 = 0;
+__device__ __forceinline__ uint16_t act16(float f) { return g_act_f16 ? hf_sat(f) : bf(f); }
// block-wide sum for blockDim.x <= 1024, result broadcast
__device__ float block_sum(float v, float* sh) {
const int lane = threadIdx.x & 31, w = threadIdx.x >> 5;
@@ -78,7 +82,7 @@ __global__ void gr_norm_kernel(const float* __restrict__ R, const float* __restr
for (int d = threadIdx.x; d < N; d += blockDim.x) {
const float v = r[d] * rs * w[c * N + d];
xn[row * N + d] = v;
- const uint16_t h = bf(v);
+ const uint16_t h = act16(v);
xn16[row * N + d] = h;
if (xn16_lo) xn16_lo[row * N + d] = bf_lo(v, h);
}
@@ -99,7 +103,7 @@ __global__ void gr_norm_rs_kernel(const float* __restrict__ R, const float* __re
if (threadIdx.x == 0) rs_out[row] = rs;
for (int d = threadIdx.x; d < N; d += blockDim.x) {
const float v = r[d] * rs * w[c * N + d];
- const uint16_t h = bf(v);
+ const uint16_t h = act16(v);
xn16[xo + d] = h;
if (xn16_lo) xn16_lo[xo + d] = bf_lo(v, h);
}
@@ -120,7 +124,7 @@ __global__ void gr_mix_r_kernel(const float* __restrict__ R, const float* __rest
s /= (float) HC;
mixed[i] = s;
if (mixed16) {
- const uint16_t h = bf(s);
+ const uint16_t h = act16(s);
mixed16[i] = h;
if (mixed16_lo) mixed16_lo[i] = bf_lo(s, h);
}
@@ -158,7 +162,7 @@ __global__ void __launch_bounds__(256) gr_write_norm_rs_kernel(float* __restrict
#pragma unroll
for (int d = threadIdx.x; d < N; d += 256, ++k) {
const float x = v[k] * rs * w[c * N + d];
- const uint16_t h = bf(x);
+ const uint16_t h = act16(x);
xn16[xo + d] = h;
if (xn16_lo) xn16_lo[xo + d] = bf_lo(x, h);
}
@@ -220,7 +224,7 @@ __global__ void __launch_bounds__(256) gr_write_cvec_norm_rs_kernel(
#pragma unroll
for (int d = threadIdx.x; d < N; d += 256, ++k) {
const float y = x[k] * rs * w[c * N + d];
- const uint16_t h = bf(y);
+ const uint16_t h = act16(y);
xn16[t * ldx + (int64_t) c * N + d] = h;
if (xn16_lo) xn16_lo[t * ldx + (int64_t) c * N + d] = bf_lo(y, h);
}
@@ -231,7 +235,7 @@ __global__ void gr_silu_kernel(const float* __restrict__ lo, uint16_t* __restric
if (i >= n) return;
const float x = lo[i] / (float) HC;
const float v = x / (1.0f + __expf(-x));
- const uint16_t h = bf(v);
+ const uint16_t h = act16(v);
lo16[i] = h;
if (lo16_lo) lo16_lo[i] = bf_lo(v, h);
}
@@ -250,7 +254,7 @@ __global__ void gr_mix_kernel(const float* __restrict__ xn, const float* __restr
s /= (float) HC;
mixed[i] = s;
if (mixed16) {
- const uint16_t h = bf(s);
+ const uint16_t h = act16(s);
mixed16[i] = h;
if (mixed16_lo) mixed16_lo[i] = bf_lo(s, h);
}
@@ -1357,7 +1361,7 @@ __global__ void round_f16_kernel(const float* __restrict__ x, float* __restrict_
__global__ void to_bf16_kernel(const float* __restrict__ x, uint16_t* __restrict__ y, uint16_t* __restrict__ ylo,
int64_t n) {
for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (int64_t) gridDim.x * blockDim.x) {
- const uint16_t h = bf(x[i]);
+ const uint16_t h = act16(x[i]);
y[i] = h;
if (ylo) ylo[i] = bf_lo(x[i], h);
}
@@ -1429,7 +1433,7 @@ __global__ void __launch_bounds__(256) gr_upmix_kernel(const uint16_t* __restric
}
s /= (float) HC;
mixed[t * N + d] = s;
- if (mixed16) mixed16[t * N + d] = bf(s);
+ if (mixed16) mixed16[t * N + d] = act16(s);
if (mixed_h) mixed_h[t * N + d] = hf(s);
}
}
@@ -1445,6 +1449,13 @@ void kv_append(const float* K, const float* V, int64_t T, int64_t pos0, const in
host ? *host : strata::kernels::KvHostPools{}, stage ? *stage : strata::kernels::KvHostPools{});
check("kv_append");
}
+void set_act_f16(bool on) {
+ const int v = on ? 1 : 0;
+ if (cudaMemcpyToSymbol(g_act_f16, &v, sizeof v) != cudaSuccess) {
+ std::fprintf(stderr, "prefill: setting the FP16 activation image failed: %s\n", cudaGetErrorString(cudaGetLastError()));
+ std::exit(1);
+ }
+}
void to_f16(const float* x, uint16_t* y, int64_t n, void* stream) {
if (n <= 0) return;
to_f16_kernel<<<(unsigned) ((n + 255) / 256 < 4096 ? (n + 255) / 256 : 4096), 256, 0, (cudaStream_t) stream>>>(x, y, n);
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 37da380fa..cf1a03fab 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -218,6 +218,8 @@ inline int bf16x2_mode() {
const char* e = std::getenv("STRATA_PREFILL_BF16X2");
return e != nullptr ? std::atoi(e) : 0;
}();
+ // FP16 activations carry 11 mantissa bits; the BF16 low part does not apply (per device: a split can mix cards)
+ if (v != 0 && prompt_f16()) return 0;
return v;
}
inline bool bf16x2() { return bf16x2_mode() != 0; }
@@ -897,6 +899,9 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
void* ws = o.take<uint8_t>(GEMM_WS, ok);
if (!ok) { err = "prefill: GEMM scratch does not fit"; return false; }
if (!m.gemm.init_external(stream, gs, GEMM_SCRATCH, ws, GEMM_WS, err)) return false;
+ const bool f16_io = prompt_f16(); // this device's gr_* kernels write the image its GEMMs read
+ m.gemm.set_f16_io(f16_io);
+ set_act_f16(f16_io);
}
if (!carve(T, &o)) {
err = "prefill: device buffers for a chunk of " + std::to_string(chunk) + " tokens do not fit";