Repository navigation
ggml : use log1p in softplus so negative inputs do not underflow to 0 - #290
danielhanchen wants to merge 1 commit into
Conversation
For x < -16.6, 1.0f + expf(x) rounds to 1.0f in F32 and logf() returns 0, although softplus(x) ~= exp(x) is a normal float down to x ~= -87. log1pf(expf(x)) keeps it. DeepSeek-V4/V4.1 route with sqrt(softplus(logits)): when every selected router logit is about -23 all expert weights became 0 and the token's routed experts were dropped (the reference uses torch softplus). Fix the CPU and CUDA SOFTPLUS ops and the fused CUDA sqrt-softplus top-k MoE path. Add SOFTPLUS_RANGE to test-backend-ops.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a444b1e67e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| } | ||
|
|
||
| test_cases.emplace_back(new test_softplus_range()); |
There was a problem hiding this comment.
Update all backends before enabling the range test
When test-backend-ops runs against Metal, Vulkan, OpenCL, or WebGPU, this unconditional case fails because their softplus kernels still compute log(1 + exp(x)) and return zero below approximately -16.6. The custom error function checks each backend output against the double reference, so those zeros produce an error of 1 against the 1e-4 limit. Update the remaining supporting backends (including their fused softplus paths where applicable) before registering this test globally, or restrict the case to implementations that received the fix.
Useful? React with 👍 / 👎.
Fixes
softplusunderflowing to exactly 0 for negative inputs. For x < -16.6,1.0f + expf(x)rounds to1.0fin F32 andlogf()returns 0, although softplus(x) ~= exp(x) is a normal float down to x ~= -87.log1pf(expf(x))keeps it.Based on upstream
797de63b2(tagb11716), independent of the DeepSeek-V4.1 stack. The diff is 4 files, +63/-3.What it adds
log1pfin the CPU and CUDASOFTPLUSops and in the fused CUDA sqrt-softplus top-k MoE path (ggml/src/ggml-cuda/topk-moe.cu).test-backend-ops:SOFTPLUS_RANGE, covering the negative range where the old formula underflows.Why it matters
DeepSeek-V4 and V4.1 route with
sqrt(softplus(logits)). When every selected router logit is about -23, all expert weights became 0 and the token's routed experts were dropped; the reference uses torchsoftplus. On a real V4.1 quant this hit 1 in 118,880 router rows. The old formula is also up to 0.13% off for x in [-10, -3].Verification
test-backend-ops:SOFTPLUS_RANGEfails on the base and passes with this change;SOFTPLUS12/12 andSOFTPLUS_RANGE1/1.Known gaps
ssm_scan, Metal, Vulkan and the other backends) are not changed.