Skip to content

TTSKit: guard degenerate distributions in token sampling#481

Open
yemreak wants to merge 2 commits into
argmaxinc:mainfrom
yemreak:fix/ttskit-sampling-probsum-guards
Open

TTSKit: guard degenerate distributions in token sampling#481
yemreak wants to merge 2 commits into
argmaxinc:mainfrom
yemreak:fix/ttskit-sampling-probsum-guards

Conversation

@yemreak
Copy link
Copy Markdown

@yemreak yemreak commented May 18, 2026

Summary

Three sites in Sampling.swift draw Float.random(in: 0..<probSum) over a sampled distribution. When that sum is zero (or negative) the call traps (0..<0 is 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

  • Top-k branch — fall back to argmax(probs) when probSum <= 0.
  • Top-p / plain branch — same guard before the random draw.
  • In-place vDSP_vsdiv normalization — fall back to vDSP_maxvi when 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.

a2they and others added 2 commits May 1, 2026 16:11
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.
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.

2 participants