Fix Qwen3 deterministic generation when do_sample=False and num_beams=1 for Greedy Decoding#41075
Open
Flakes342 wants to merge 6 commits intohuggingface:mainfrom
Open
Fix Qwen3 deterministic generation when do_sample=False and num_beams=1 for Greedy Decoding#41075Flakes342 wants to merge 6 commits intohuggingface:mainfrom
Flakes342 wants to merge 6 commits intohuggingface:mainfrom
Conversation
4 tasks
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: qwen3 |
gante
suggested changes
Sep 23, 2025
Contributor
gante
left a comment
There was a problem hiding this comment.
Thank you for opening the PR @Flakes342 🤗
However, this is not the correct fix for this (very complex) problem. For more details and a temporary fix, have a look at this comment :)
This was referenced Apr 29, 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.
What does this PR do?
Fixes #41060
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@gante
Problem
Qwen3 generate() was non-deterministic even with do_sample=False due to merged model defaults
(top_k, top_p, temperature) overriding the requested greedy decoding (do_sample = False and num_beams = 1 as per the documentation).
Reproduction
Solution
Enforce temperature=1.0, top_k=0, top_p=1.0 whenever do_sample=False and num_beams = 1 in _prepare_generation_config in generation/utils.py
Tests
Added a simple regression test to ensure future releases maintain deterministic behavior by passing the same input twice to Qwen3-0.6B model.
Additional Notes
Please feel free to let me know if there are any mistakes or oversight and I'd be happy to fix it and resubmit this PR. Thank you!