fix(cuda): saturate kernel_tanh argument (--use_fast_math tanhf overflow) - #125
Merged
Conversation
…low) elementwise.cu is compiled with --use_fast_math, whose approximate tanhf does NOT saturate for large arguments -- it returns a growing value. This turns the tanh-approximation GELU (0.5*x*(1+tanh(u)), u ~ 0.0357*x^3) into a quartic and blows activations up to 1e27/NaN in f32 (GPU CrossAsset cliff; CPU's accurate math.Tanh is unaffected). Clamp the argument to [-20,20] via tanh_sat in kernel_tanh and kernel_tanh_prime; tanh(+/-20) == +/-1.0f is exact in float32. Verified on GB10: rebuilt sm_121 libkernels.so eliminates the GELU forward over-amplification (per-node forward scan: 0 ops > 1e3, was ~1e27).
This was referenced Jun 12, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
internal/cuda/kernels/elementwise.cuis compiled with--use_fast_math(kernels Makefile). The fasttanhfdoes not saturate for large arguments -- it returns a growing value instead of ±1. This breaks the tanh-approximation GELU (0.5·x·(1+tanh(u)), u ≈ 0.0357·x³): for moderately large x the argument u reaches ~1e7, fasttanhf(u)returns a large value, GELU becomes ~quartic, and activations blow from ~1e5 to ~1e27 -> NaN in f32 (the GPU 'CrossAsset cliff'; Wolf docs/plan-gpu-f32-residual). The CPU path uses the accuratemath.Tanhand is unaffected -- which masked it.Fix
Clamp the
tanhfargument to [-20, 20] inkernel_tanhandkernel_tanh_primevia atanh_sathelper. tanh(±20) == ±1.0f exactly in float32, so this is numerically exact and keeps fast-mathtanhfinside its accurate range.Verification
Rebuilt the sm_121
libkernels.soand re-ran GB10 CrossAsset f32 training: the forward GELU over-amplification is eliminated (per-node forward scan shows zero ops exceeding 1e3, vs ~1e27 before). No Go-level unit test (GPU-kernel-only bug); belongs in the GPU parity activation suite when GPU CI is available. NOTE: production needs the kernels rebuilt/redeployed from this merge.Status
One of two fixes for the GPU f32 cliff (the other recomputes LayerNorm stats: zerfoo#842).