fix(generation): beam sample when num_beams * vocab_size exceeds multinomial limit#45251
Closed
balgaly wants to merge 2 commits intohuggingface:mainfrom
Closed
fix(generation): beam sample when num_beams * vocab_size exceeds multinomial limit#45251balgaly wants to merge 2 commits intohuggingface:mainfrom
balgaly wants to merge 2 commits intohuggingface:mainfrom
Conversation
…inomial limit PyTorch multinomial requires the last dimension to be at most 2**24. Beam search with do_sample flattens num_beams * vocab_size into one dimension; large beams + large vocabs (e.g. huggingface#45245) crash on CUDA. Use Gumbel-top-k when flat_dim >= 2**24, equivalent to sampling without replacement from softmax(accumulated_log_probs). Adds a unit test with a patched limit for small tensors. Made-with: Cursor
Contributor
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45251&sha=490973 |
Author
|
The |
Member
|
No agent PRs on random issues please! (Swapping a multinomial for a gumbel-topk trick that adds loads of code bloat for a very rare edge case is the most code agent solution imaginable) |
This was referenced Apr 11, 2026
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.
Problem
torch.multinomialrejects last dimensions >= 2**24. Beam search withdo_sample=Truebuilds a flat distribution of sizenum_beams * vocab_size, which can exceed that limit (e.g. large beams + ~164k vocab), crashing during generation (#45245).Solution
When the flat dimension is at or above
2**24, selectbeams_to_keepcontinuations via Gumbel-top-k on accumulated_log_probs, equivalent tomultinomial(softmax(logits), k, replacement=False)without using an oversized multinomial.Tests
tests/generation/test_beam_search_multinomial_limit.pypatches the limit to exercise the fallback on small tensors.Fixes #45245