Skip to content

ggml : use log1p in softplus so negative inputs do not underflow to 0 - #290

Open
danielhanchen wants to merge 1 commit into
base/upstream-797de63b2from
ggml-softplus-log1p
Open

danielhanchen wants to merge 1 commit into
base/upstream-797de63b2from
ggml-softplus-log1p

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes softplus underflowing to exactly 0 for negative inputs. 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.

Based on upstream 797de63b2 (tag b11716), independent of the DeepSeek-V4.1 stack. The diff is 4 files, +63/-3.

What it adds

  • log1pf in the CPU and CUDA SOFTPLUS ops 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 torch softplus. 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_RANGE fails on the base and passes with this change; SOFTPLUS 12/12 and SOFTPLUS_RANGE 1/1.
  • DeepSeek-V4.1 fixture with F32 weights: logits change by KL about 1e-9 (noise). With MXFP4 experts: 2e-4 to 4e-4, from near-tie routing flips.

Known gaps

  • Other softplus copies (ssm_scan, Metal, Vulkan and the other backends) are not changed.
  • Validated on CPU and CUDA only.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T07:56:27.237277Z a444b1e PR opened
🔒 Security Review ✅ Completed 2026-10-19T07:53:36.058100Z a444b1e PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant