Fix Gemma softcap F16 overflow NaN and scheduler hang (#2058)#2076
Open
glaziermag wants to merge 1 commit intoEricLBuehler:masterfrom
Open
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
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 an inference hang specific to Gemma models (#2058) caused by numerical NaN propagation during softcapping operations.
Cause
Gemmaarchitectures multiply attention scores by asoftcapscaling factor (50.0). When running withf16precision, this multiplication can exceedf16::MAX(65504), which overflows and producesNaNvalues.NaNgeneration, they were not fully cleaned up by the scheduler.SequenceState::Errorcaused the scheduler state-machine to enter an infinite retry loop instead of fully dropping the sequence.Changes
mistralrs-core/src/attention/backends/naive.rs: Temporarily casts intermediate tensors tof32exclusively during thesoftcapscaling step to provide sufficient mathematical headroom during the scaling/tanh phase, before returning cleanly to the targetdtype(f16orbf16). This prevents regression on CPU or standard models.mistralrs-core/src/sequence.rs: AddedSequenceState::Errorevaluation explicitly intois_finished_paged_attn()to assure erroneous sequences are correctly recognized as finished and properly garbage-collected by the engine.Testing
hf-internal-testing/tiny-random-Gemma2ForCausalLMgenerations. Execution CPU times showed no regressions with the addedf32cast block (average batch latency ~7.9s on tested CPU environment).Gemmacompletions with a standard200 OKwithout triggering infinite retries on the console.Before
$ curl -s -X POST http://localhost:1234/v1/completions \ -d '{"model": "gemma-2-2b-it", "prompt": "Explain gravity.", "max_tokens": 20}'After
$ curl -s -X POST http://localhost:1234/v1/completions \ -d '{"model": "gemma-2-2b-it", "prompt": "Explain gravity.", "max_tokens": 20}'(Successfully completes generation without errors or retries)