TTSKit: guard degenerate distributions in token sampling#481
Open
yemreak wants to merge 2 commits into
Open
Conversation
When the sampled probability distribution sums to zero (or negative) the existing code calls `Float.random(in: 0..<0)` which traps, or yields an undefined index. Replace the random draw with an argmax fallback over the same distribution so the generator stays deterministic instead of crashing. Three call sites updated (top-k path, plain top-p path, in-place logits normalization). Encountered with rare numerical edge cases during Qwen3 TTS generation in long contexts.
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
Three sites in
Sampling.swiftdrawFloat.random(in: 0..<probSum)over a sampled distribution. When that sum is zero (or negative) the call traps (0..<0is an invalid range), or yields an undefined index further down. Replace the random draw with an argmax fallback over the same distribution so the generator stays deterministic instead of crashing.Motivation
Encountered with rare numerical edge cases during Qwen3 TTS generation in long contexts (the resampled logits collapse to all-zero after the top-k mask). The crash is hard to diagnose because the trace points inside
Foundation, not the model code.Changes
argmax(probs)whenprobSum <= 0.vDSP_vsdivnormalization — fall back tovDSP_maxviwhen the running sum is non-positive.Testing
Black-box: regenerated the offending Qwen3 prompt and confirmed the crash no longer occurs and output stays sensible. No behavioural change on healthy distributions.