Skip to content

fix: include boundary token in top-p nucleus sampling#1171

Open
zhuxiaoxuhit wants to merge 2 commits intofishaudio:mainfrom
zhuxiaoxuhit:fix/top-p-nucleus-sampling
Open

fix: include boundary token in top-p nucleus sampling#1171
zhuxiaoxuhit wants to merge 2 commits intofishaudio:mainfrom
zhuxiaoxuhit:fix/top-p-nucleus-sampling

Conversation

@zhuxiaoxuhit
Copy link
Copy Markdown
Contributor

The current implementation uses cum_probs > top_p to build the removal mask,
which marks the token that first pushes the cumulative probability over top_p
as removed. This means the actual nucleus covers less probability mass than
intended.

Example with top_p=0.9 and probs [0.50, 0.35, 0.10, 0.05]:
cum_probs = [0.50, 0.85, 0.95, 1.00]
current: keeps tokens at 0.50+0.35=0.85 (under-samples the nucleus)
correct: keeps tokens at 0.50+0.35+0.10=0.95 (covers top_p)

The fix shifts the removal mask right by one position before applying it,
so the token that first crosses the threshold is included. This matches
the standard implementation in transformers (TopPLogitsWarper) and other
LLM inference libraries.

nucleus sampling with cum_probs > top_p excludes the token that first
pushes the cumulative probability over the threshold, so the actual
nucleus covers less than top_p of the probability mass.

shift the mask right by one to include that boundary token, matching
the standard implementation in transformers and other LLM toolkits.
@Stardust-minus
Copy link
Copy Markdown
Member

This fix is still needed, but the PR has merge conflicts with the current main branch (logits_to_probs has been refactored). Could you please rebase onto the latest main? Thanks!

@zhuxiaoxuhit
Copy link
Copy Markdown
Contributor Author

Hi @Stardust-minus , the merge conflicts have been resolved. Please review when you have a minute. Thanks!

@github-actions
Copy link
Copy Markdown
Contributor

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale label Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants