Fix Gemma softcap F16 overflow NaN and scheduler hang (#2058)#2076
Closed
glaziermag wants to merge 1 commit intoEricLBuehler:masterfrom
Closed
Fix Gemma softcap F16 overflow NaN and scheduler hang (#2058)#2076glaziermag wants to merge 1 commit intoEricLBuehler:masterfrom
glaziermag wants to merge 1 commit intoEricLBuehler:masterfrom
Conversation
Contributor
Author
|
Update (2026-04-15): Rebased onto |
This was referenced Apr 16, 2026
Contributor
Author
|
Closing in favor of atomic split PRs for single-responsibility review:
The two fixes are independent and should be reviewable/mergeable separately. |
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.
Fixes #2058.
Two independent fixes
1. Softcap NaN in F16/BF16 (naive attention backend)
Gemma models use an attention softcap (logit / softcap → tanh → logit * softcap). The naive SDPA backend was performing the tanh in the input dtype (F16 or BF16). For F16, values outside approximately ±65504 become ±Inf before tanh, and tanh(Inf) = NaN when computed in reduced precision, producing silent NaN logits.
Fix: Promote attention scores to F32 before computing the softcap tanh, then cast back to the original dtype.
Scope note: This fix applies to the CPU/naive fallback path in
attention/backends/naive.rs. The primary CUDA (FlashAttention) and Metal SDPA backends have their own softcap handling and are not changed here.2.
SequenceState::Errormissing fromis_finished_paged_attnWhen a sequence enters
SequenceState::Error, it was not recognized as "finished" by the paged attention scheduler. This caused KV blocks to remain allocated and the scheduler to stall waiting for capacity that would never be returned.Fix: Add
SequenceState::Errorto the match arms inis_finished_paged_attn().Files changed
mistralrs-core/src/attention/backends/naive.rsmistralrs-core/src/sequence.rs