From c517862ce24aa567502d8e35884a3fffe2f76edd Mon Sep 17 00:00:00 2001 From: sergiywith Date: Wed, 30 Sep 2026 22:25:39 +0300 Subject: [PATCH] numerics: saturate the prompt path's FP16 SwiGLU products; log1pf in the unfused GDN gate - prefill/kernels.cu: the SwiGLU products that feed the prompt path's FP16 down projection are clamped to FP16's range (+-65504) before rounding. A token with a massive activation otherwise becomes inf there and NaN after the GEMM, where decode's q8_1 path (range ~8e6) stays finite. - native_gdn_preprocess.cu: the unfused gate softplus used logf(1 + expf(v)), which returns 0 for v below ~-16 (1 + e^v rounds to 1); the fused kernels, gdn.cu and verify_kernels.cu already use log1pf(expf(v)). The two paths now agree. On ordinary prompts nothing moves: first-token KL to main 0, 0.00013, 0 (the run-to-run noise). --- src/kernels/cuda/native_gdn_preprocess.cu | 2 +- src/prefill/kernels.cu | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/kernels/cuda/native_gdn_preprocess.cu b/src/kernels/cuda/native_gdn_preprocess.cu index 535cb31d..86b10453 100644 --- a/src/kernels/cuda/native_gdn_preprocess.cu +++ b/src/kernels/cuda/native_gdn_preprocess.cu @@ -92,7 +92,7 @@ __global__ void gate_softplus(const float* __restrict__ alpha, const float* __re const int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= count) return; const float value = __fadd_rn(alpha[i], dt[i]); - const float softplus = value > 20.0f ? value : logf(1.0f + expf(value)); + const float softplus = value > 20.0f ? value : log1pf(expf(value)); // 1 + e^v loses e^v below ~1e-7 gate[i] = softplus * ssm_a[i]; } diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu index 25226735..310a209d 100644 --- a/src/prefill/kernels.cu +++ b/src/prefill/kernels.cu @@ -34,6 +34,9 @@ __device__ __forceinline__ uint16_t bf(float f) { } __device__ __forceinline__ float sigm(float x) { return 1.0f / (1.0f + __expf(-x)); } __device__ __forceinline__ uint16_t hf(float f) { return __half_as_ushort(__float2half_rn(f)); } +// 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) +__device__ __forceinline__ uint16_t hf_sat(float f) { return hf(fminf(fmaxf(f, -65504.0f), 65504.0f)); } // 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; @@ -486,14 +489,14 @@ __global__ void swiglu_il_kernel(const float* __restrict__ gu, uint16_t* __restr if (i >= n * 640) return; const int64_t r = i / 640, k = i % 640; const float g = gu[r * 1280 + 2 * k], u = gu[r * 1280 + 2 * k + 1]; - h16[i] = hf(g / (1.0f + __expf(-g)) * u); + h16[i] = hf_sat(g / (1.0f + __expf(-g)) * u); } __global__ void swiglu_pair_kernel(const float* __restrict__ g, const float* __restrict__ u, uint16_t* __restrict__ h16, int64_t n) { const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; if (i >= n * 640) return; const float a = g[i]; - h16[i] = hf(a / (1.0f + __expf(-a)) * u[i]); + h16[i] = hf_sat(a / (1.0f + __expf(-a)) * u[i]); } __global__ void gather_rows16_kernel(const uint16_t* __restrict__ x, const int32_t* __restrict__ src, uint16_t* __restrict__ dst, int64_t n, int64_t width) {