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 FP16 prompt: STRATA_F16_RANGE counts what enters and leaves FP16; the range measured (outputs peak at 410 of 65504) and the FP32 route compared
xjc10c211cb3ebebd
diff --git a/bench/results/2026-10-04-rdna2-fp16-prompt/README.md b/bench/results/2026-10-04-rdna2-fp16-prompt/README.mdindex 90f670a6c..be2ca3be3 100644--- a/bench/results/2026-10-04-rdna2-fp16-prompt/README.md+++ b/bench/results/2026-10-04-rdna2-fp16-prompt/README.md@@ -53,14 +53,57 @@ Both cards, cold server per run, one 8,275- and one 33,586-token prompt of reposDecode is the same within the noise in both setups (these short answers: 51-58 tok/s split, 36-42 helper).+## Against the FP32 route (#1006), same card (2026-10-06)++#1006 takes the same gfx103x GEMMs the other way: both inputs widened to FP32 and the product as SGEMM (exact widening,+FP32 accumulate, `STRATA_RDNA2_SGEMM=0` for the old path). Built at its `63a92c2` and run next to this change's build+(`6add1a7`, `main` + this) on one RX 6900 XT: the same model and arguments as above, three prompts from one corpus, cold+server per arm, temperature 0, 8 tokens answered, `STRATA_PREFILL_TIMING=1`. The old path here is #1006's branch with+its switch off. One run per cell; a second cold run of this change's arm repeated it within 1.3%.++| prompt tokens | old path | #1006 (SGEMM) | this change (FP16 HGEMM) |+| ---: | ---: | ---: | ---: |+| 8,368 | 412 tok/s | 622 tok/s (+51%) | 730 tok/s (+77%) |+| 32,813 | 455 tok/s | 742 tok/s (+63%) | 900 tok/s (+98%) |+| 104,412 | 456 tok/s | 750 tok/s (+65%) | 904 tok/s (+98%) |++The 32,813-token prompt's GPU timeline, the phases these GEMMs sit in (old / SGEMM / FP16): gdn 22.0 -> 6.2 -> 4.0 s,+qsa proj 8.6 -> 3.2 -> 2.1 s, hc read 8.1 -> 6.1 -> 4.4 s, router+shared 4.0 -> 2.5 -> 0.7 s; `wait copy` is 1.0 s in+all three (PCIe 4.0 x8), so the GEMMs are not hidden behind the expert stream here. Per call this is the card's FP32+rate against its FP16 rate: the N 10240 x K 2560 projection at ~15 TFLOPS as SGEMM (#1006's 27.7 ms at T 8192) and+37.7 as HGEMM, about 2x on the GEMM itself, 17-21% on the prompt. What the SGEMM route has that this change does not:+no FP16 output rounding, `beta != 0` and N < 64 covered. The three arms gave the same answer on each prompt.+## Range checkFP16 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.+out of them leaves FP16's range. The engine now counts it: `STRATA_F16_RANGE=1` records, per device, the largest |value|+and the count beyond 65504 (or not finite) at the three places a value enters or leaves FP16 - the activation images+(`hf_sat`, every image writer's one funnel), the BF16 weights converted to FP16 (`bf16_to_f16_rows`) and the FP16 GEMM+outputs as they are widened (`widen_rows_f16`; an output rocBLAS wrote beyond the range is Inf there, not saturated) -+and prints one line per prompt (`strata f16 range: ...`). Off, the cost is one flag read per value; on, a 34.4K-token+prompt read at 889.8 tok/s against 888.5 without it.++Measured on one card over 466K tokens in nine prompts: three 32-34K slices of the earlier corpus (English docs, Chinese+logs, C++), two prompts (118K and 64K tokens) built to push activations - delimiter floods (`<|im_start|>`, 200 newlines,+500 spaces), one token repeated 4,000 times, 6,000-character runs, 18-digit numbers, 20K characters of base64, emoji and+Japanese / Russian / Arabic / Hindi text, 2,000 SQL lines - and four batches (40-49K tokens) of real requests recorded+from this machine's API:++| | activation images, max abs | BF16 weights, max abs | FP16 GEMM outputs, max abs | beyond 65504 / not finite |+| --- | ---: | ---: | ---: | ---: |+| corpus slices (3) | 84.0 / 103.4 / 112.2 | 11.56 | 387.0 / 392.0 / 391.5 | 0 |+| built to push (2) | 76.6 / 99.0 | 11.56 | 409.5 / 399.2 | 0 |+| real requests (4) | 73.9 - 92.7 | 11.56 | 374.2 - 382.5 | 0 |++The largest output is 409.5, 160x below 65504; the largest activation 112, 580x below. The prompts built to push+activations did not: their maxima are below the ordinary corpus's. Offline, the pack's dense BF16 tensors (484 in the+first GGUF shard) peak at 8.56 (`hc_ffn_up`); 11.56 is in the second shard (the PLE). This is one model (GSQ-RCO IQ3_S);+the other models setup installs are fine-tunes of the same architecture, so the headroom is the same kind of number+there but has not been measured. Out-of-range activations would still be finite: the image writers saturate to++-65504 (`hf_sat`), a NaN stays a NaN; an out-of-range GEMM output would be Inf, which is what the counter is for.+`to_f16` (the hc image for the FP16-weight GEMMs and the PLE key image) saturates and counts too now, so every FP16+image writer is under the same rule; three of the prompts rerun with it in gave the same maxima.## Distribution check (teacher-forced)diff --git a/docs/AMD_HIP.md b/docs/AMD_HIP.mdindex a8fe88349..19b5c0fdc 100644--- a/docs/AMD_HIP.md+++ b/docs/AMD_HIP.md@@ -440,7 +440,10 @@ run it; the report below is from a community machine: an RX 6900 XT 16 GB (gfx10N = 10240, T = 7313, K = 2560). `STRATA_HIP_PROMPT_F16=1` (opt-in, off by default; the engine prints a tip on gfx103x) runs the 16-bit prompt GEMMs FP16 in and out;unset is the old path. `STRATA_DBG_NAN=1` also counts the non-finite FP16 outputs (a sum past 65504). The output is not bit-identical to it (an FP16 rounding of each GEMM's output replaces the BF16 rounding ofthe BF16 GEMMs' activations); what the distribution check showed and did not show is in- [bench/results/2026-10-04-rdna2-fp16-prompt](../bench/results/2026-10-04-rdna2-fp16-prompt/README.md). Measured on a+ [bench/results/2026-10-04-rdna2-fp16-prompt](../bench/results/2026-10-04-rdna2-fp16-prompt/README.md). FP16 ends at+ 65504: `STRATA_F16_RANGE=1` prints, per prompt, the largest value that entered or left FP16 and how many were beyond+ the range (466K tokens there: outputs peak at 410, activations at 112, none beyond; the image writers saturate, a GEMM+ output beyond the range would be Inf and is what the counter is for). Measured on asecond community machine - 2x RX 6900 XT 16 GB (one card for these numbers), Ryzen 5 5600X (6 cores, AVX2), 128 GB,Ubuntu 26.04, ROCm 10.0.0, engine 0.1.39 built by hand with `-DSTRATA_PREFILL_MMQ=ON` as setup does, GSQ-RCO IQ3_S:a 9.4K / 34.7K / 105.8K-token prompt reads at 744 / 915 / 926 tok/s instead of 439 / 466 / 461; decode is unchanged.diff --git a/include/strata/hip_compat/cuda_runtime.h b/include/strata/hip_compat/cuda_runtime.hindex d669f0fe6..4eec06843 100644--- a/include/strata/hip_compat/cuda_runtime.h+++ b/include/strata/hip_compat/cuda_runtime.h@@ -142,5 +142,6 @@ inline hipError_t cudaGraphInstantiate(hipGraphExec_t* exec, hipGraph_t graph,}#define __trap() __builtin_trap() // the compiled-out sm_80 paths (never selected on AMD)#define cudaMemcpyToSymbol(symbol, ...) hipMemcpyToSymbol(HIP_SYMBOL(symbol), __VA_ARGS__)+#define cudaMemcpyFromSymbol(dst, symbol, ...) hipMemcpyFromSymbol(dst, HIP_SYMBOL(symbol), __VA_ARGS__)#include "intrinsics.hpp"diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hppindex 0740fd0d2..e8b4e9a9e 100644--- a/include/strata/prefill/gemm.hpp+++ b/include/strata/prefill/gemm.hpp@@ -12,6 +12,13 @@namespace strata::prefill {+/// STRATA_F16_RANGE: count what the gfx103x FP16 route converts on this device - the BF16 weights to FP16 (slot 0)+/// and the FP16 GEMM outputs widened to FP32 (slot 1): the largest |value| and how many were beyond 65504 or not+/// finite. gemm_f16_range_read returns them (and resets when asked); false where the route does not exist (CUDA).+void gemm_set_f16_range(bool on);+bool gemm_f16_range_read(float max_abs[2], unsigned long long over[2], bool reset);++class Gemm {public:Gemm() = default;diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hppindex f12f34b94..7ea690b59 100644--- a/include/strata/prefill/kernels.hpp+++ b/include/strata/prefill/kernels.hpp@@ -42,6 +42,11 @@ void gr_write_cvec_norm_rs(float* R, const float* bo, const float* inj, int64_tvoid 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);+/// STRATA_F16_RANGE: count what the activation images convert to FP16 on this device (hf_sat): the largest |value| and+/// how many were beyond 65504 or NaN. f16_range_read returns them (and resets when asked); false if the device+/// refused the copy.+void set_f16_range(bool on);+bool f16_range_read(float& max_abs, unsigned long long& over, bool reset);/// 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.cuindex 300da53b7..2a0bfc9a8 100644--- a/src/prefill/gemm.cu+++ b/src/prefill/gemm.cu@@ -51,6 +51,18 @@namespace strata::prefill {namespace {#if defined(__HIPCC__)+// STRATA_F16_RANGE (gemm_set_f16_range): the largest |value| and the count beyond 65504 / non-finite, for the BF16+// weights converted to FP16 (slot 0, bf16_to_f16_rows) and the FP16 GEMM outputs widened (slot 1, widen_rows_f16 -+// an output rocBLAS wrote as FP16 is Inf there, not saturated). As the activation counter in kernels.cu.+__device__ int g_f16_range_on_g = 0;+__device__ unsigned g_f16_range_max_g[2] = {0, 0};+__device__ unsigned long long g_f16_range_over_g[2] = {0, 0};+__device__ __forceinline__ void f16_range_note_g(int k, float f) {+ const float a = fabsf(f);+ const unsigned bits = __float_as_uint(a);+ if (bits > g_f16_range_max_g[k]) atomicMax(&g_f16_range_max_g[k], bits);+ if (a > 65504.0f || isnan(f)) atomicAdd(&g_f16_range_over_g[k], 1ull);+}// 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.@@ -64,6 +76,7 @@ __global__ void widen_rows_f16(float* __restrict__ Y, int64_t n, int64_t ldy, inconst int64_t c = b * blockDim.x + threadIdx.x;const float v = c < n ? __half2float(h[c]) : 0.0f;if (count_nonfinite && c < n && !isfinite(v)) atomicAdd(&g_f16_nonfinite, 1ull);+ if (c < n && g_f16_range_on_g) f16_range_note_g(1, v);__syncthreads();if (c < n) y[c] = v;__syncthreads();@@ -73,10 +86,42 @@ __global__ void widen_rows_f16(float* __restrict__ Y, int64_t n, int64_t ldy, in__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);+ if (g_f16_range_on_g) f16_range_note_g(0, f);d[i] = __float2half(isnan(f) ? f : fminf(fmaxf(f, -65504.0f), 65504.0f)); // as hf_sat: a NaN stays NaN}}#endif+} // namespace++void gemm_set_f16_range(bool on) {+#if defined(__HIPCC__)+ const int v = on ? 1 : 0;+ if (cudaMemcpyToSymbol(g_f16_range_on_g, &v, sizeof v) != cudaSuccess) {+ std::fprintf(stderr, "prefill gemm: setting the FP16 range check failed: %s\n", cudaGetErrorString(cudaGetLastError()));+ std::exit(1);+ }+#else+ (void) on;+#endif+}+bool gemm_f16_range_read(float max_abs[2], unsigned long long over[2], bool reset) {+#if defined(__HIPCC__)+ unsigned bits[2] = {0, 0}; unsigned long long o[2] = {0, 0};+ if (cudaMemcpyFromSymbol(bits, g_f16_range_max_g, sizeof bits) != cudaSuccess ||+ cudaMemcpyFromSymbol(o, g_f16_range_over_g, sizeof o) != cudaSuccess) { cudaGetLastError(); return false; }+ for (int k = 0; k < 2; ++k) { union { unsigned u; float f; } c; c.u = bits[k]; max_abs[k] = c.f; over[k] = o[k]; }+ if (reset) {+ const unsigned z[2] = {0, 0}; const unsigned long long zz[2] = {0, 0};+ cudaMemcpyToSymbol(g_f16_range_max_g, z, sizeof z); cudaMemcpyToSymbol(g_f16_range_over_g, zz, sizeof zz);+ }+ return true;+#else+ (void) max_abs; (void) over; (void) reset;+ return false;+#endif+}++namespace {void ck(cublasStatus_t s, const char* what) {if (s != CUBLAS_STATUS_SUCCESS) {diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cuindex f58a189c6..a0b622dc5 100644--- a/src/prefill/kernels.cu+++ b/src/prefill/kernels.cu@@ -45,7 +45,23 @@ __device__ __forceinline__ uint16_t hf(float f) { return __half_as_ushort(__floa// A SwiGLU product for an FP16 GEMM: saturated, so a token with a massive activation cannot turn into inf and then// 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)); }+// STRATA_F16_RANGE=1 (set_f16_range): what reaches FP16's range on this device - the largest |value| the activation+// images converted (hf_sat is their one funnel) and how many were beyond 65504 or NaN; W and Y are counted in gemm.cu.+// Off, the cost is one flag read per value. The max is kept as float bits (positive floats order as unsigned ints),+// and a thread takes the atomic only when it beats the value it read, so the atomics stop once the max has settled.+__device__ int g_f16_range_on = 0;+__device__ unsigned g_f16_range_max = 0;+__device__ unsigned long long g_f16_range_over = 0;+__device__ __forceinline__ void f16_range_note(float f) {+ const float a = fabsf(f);+ const unsigned bits = __float_as_uint(a);+ if (bits > g_f16_range_max) atomicMax(&g_f16_range_max, bits);+ if (a > 65504.0f || isnan(f)) atomicAdd(&g_f16_range_over, 1ull);+}+__device__ __forceinline__ uint16_t hf_sat(float f) {+ if (g_f16_range_on) f16_range_note(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.#if defined(__HIPCC__) // HIP only (#835): the CUDA kernels stay exactly as they were, they never read the flag@@ -1718,7 +1734,7 @@ __global__ void kv_append_kernel(const float* __restrict__ K, const float* __res}__global__ void to_f16_kernel(const float* __restrict__ x, uint16_t* __restrict__ y, int64_t n) {for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (int64_t) gridDim.x * blockDim.x)- y[i] = hf(x[i]);+ y[i] = hf_sat(x[i]); // saturated and counted like every other FP16 image (the hc / embedding image on gfx103x)}__global__ void round_f16_kernel(const float* __restrict__ x, float* __restrict__ y, int64_t n) {for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (int64_t) gridDim.x * blockDim.x)@@ -1827,6 +1843,24 @@ void set_act_f16(bool on) {}#endif}+void set_f16_range(bool on) {+ const int v = on ? 1 : 0;+ if (cudaMemcpyToSymbol(g_f16_range_on, &v, sizeof v) != cudaSuccess) {+ std::fprintf(stderr, "prefill: setting the FP16 range check failed: %s\n", cudaGetErrorString(cudaGetLastError()));+ std::exit(1);+ }+}+bool f16_range_read(float& max_abs, unsigned long long& over, bool reset) {+ unsigned bits = 0; unsigned long long o = 0;+ if (cudaMemcpyFromSymbol(&bits, g_f16_range_max, sizeof bits) != cudaSuccess ||+ cudaMemcpyFromSymbol(&o, g_f16_range_over, sizeof o) != cudaSuccess) { cudaGetLastError(); return false; }+ union { unsigned u; float f; } c; c.u = bits; max_abs = c.f; over = o;+ if (reset) {+ const unsigned z = 0; const unsigned long long zz = 0;+ cudaMemcpyToSymbol(g_f16_range_max, &z, sizeof z); cudaMemcpyToSymbol(g_f16_range_over, &zz, sizeof zz);+ }+ return true;+}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.cppindex 2fa71c7ce..5724f04de 100644--- a/src/prefill/prefill.cpp+++ b/src/prefill/prefill.cpp@@ -1223,6 +1223,8 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, com.f16_io = prompt_f16();m.gemm.set_f16_io(m.f16_io);set_act_f16(m.f16_io);+ static const bool f16_range = std::getenv("STRATA_F16_RANGE") != nullptr; // what reaches FP16's range+ if (f16_range) { set_f16_range(true); gemm_set_f16_range(true); }}if (!carve(T, &o)) {size_t fb = 0, tb = 0;@@ -4265,6 +4267,14 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri(long long) kv_prefetches);}stats_.ms_total += ms_since(t_start);+ {+ static const bool f16_range = std::getenv("STRATA_F16_RANGE") != nullptr;+ float xm = 0.0f, wy[2] = {0.0f, 0.0f}; unsigned long long xo = 0, wyo[2] = {0, 0};+ if (f16_range && f16_range_read(xm, xo, true) && gemm_f16_range_read(wy, wyo, true))+ std::fprintf(stderr, "strata f16 range: %lld tokens: activation images max |x| %.1f (%llu beyond 65504 or NaN), "+ "BF16 weights max |w| %.3f (%llu), FP16 GEMM outputs max |y| %.1f (%llu not finite)\n",+ (long long) n, xm, xo, wy[0], wyo[0], wy[1], wyo[1]);+ }if (pt.on) {pt.fold();double total = 0.0;