Fix crash in GreedyTokenSampler when top-K probability sum is zero#487
Open
tsushanth wants to merge 1 commit into
Open
Fix crash in GreedyTokenSampler when top-K probability sum is zero#487tsushanth wants to merge 1 commit into
tsushanth wants to merge 1 commit into
Conversation
When softmax output is numerically degenerate (all logits at -inf, NaN, or extreme underflow), the top-K probabilities sum to zero or NaN. `Float.random(in: 0..<probSum)` then traps with "Fatal error: Can't get random value with an empty range". Guard against `probSum <= 0` and non-finite sums by falling back to argmax over the top-K probabilities. Adds a regression test that reproduces the original crash without the guard. Fixes argmaxinc#478
There was a problem hiding this comment.
Pull request overview
This PR addresses a runtime trap in GreedyTokenSampler when numerically degenerate softmax output causes the top‑K probability sum to be zero (or non-finite), by adding a guard and an argmax-style fallback. It also adds a regression test intended to reproduce the original crash scenario.
Changes:
- Add a
probSumfiniteness/positivity guard inGreedyTokenSampler.sampleFromProbsto avoidFloat.random(in: 0..<probSum)trapping on an empty/invalid range. - Fall back to selecting a top‑K token when
probSumis zero or non-finite. - Add a new unit test covering degenerate softmax behavior that previously caused a crash.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| Sources/TTSKit/Utilities/Sampling.swift | Adds guard + fallback path to prevent Float.random(in:) from trapping when top‑K probability sum is zero/non-finite. |
| Tests/TTSKitTests/TTSKitUnitTests.swift | Adds a regression test intended to reproduce the degenerate-softmax crash scenario. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+241
to
+244
| guard probSum.isFinite, probSum > 0 else { | ||
| let maxIdx = probsArray.enumerated().max(by: { $0.element < $1.element })?.offset ?? 0 | ||
| return Int32(idxArray[maxIdx]) | ||
| } |
Comment on lines
+304
to
+307
| // Every logit at -inf → softmax → all zeros (or NaN). Tag index 9 with a | ||
| // slightly less-negative value so argmax has a clear winner to fall back to. | ||
| for i in 0..<vocabSize { ptr[i] = FloatType(-Float.infinity) } | ||
| ptr[9] = FloatType(-1e30) |
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.
Summary
Float.random(in: 0..<probSum)inGreedyTokenSampler.sampleFromProbsagainst degenerate softmax output.Problem
When softmax output becomes numerically degenerate — all logits at
-inf, NaN propagation, or extreme underflow — the top-K probabilities sum to zero.Float.random(in: 0..<probSum)then traps with:I confirmed this locally with the new test: temporarily reverting the guard reproduces the exact crash from Swift's
FloatingPointRandom.swift:52.Fix
Inline guard in
Sources/TTSKit/Utilities/Sampling.swift. WhenprobSumis zero or non-finite, return the highest-probability token from the top-K window (argmax fallback). Lock-free, no new dependencies, no API changes.Test plan
testGreedySamplerZeroProbSumFallsBackToArgmaxconstructs[1, 1, vocabSize]logits at-Float.infinity, callssampleCodec0withtopK=4, asserts no crash and a valid token index.GreedyTokenSamplertests pass.swift buildsucceeds.Fixes #478