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).
This commit is contained in:
1 parent
4da8bfcbf9
commit
c517862ce2
2 files changed
+6
-3
No files matched your search
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user