Avoid inline .item() sync in decoder start token check#43094
Open
pythongiant wants to merge 3 commits into
Open
Avoid inline .item() sync in decoder start token check#43094pythongiant wants to merge 3 commits into
pythongiant wants to merge 3 commits into
Conversation
….all().item()` condition into a separate tensor boolean to avoid inline GPU syncs and allow future batching of boolean checks. No functional change.
zucchini-nlp
reviewed
Jan 5, 2026
| ) | ||
| model_kwargs["decoder_attention_mask"] = decoder_attention_mask | ||
|
|
||
| else: |
Member
There was a problem hiding this comment.
The main change is structural/ stylistic, not a speedup. We're calling .item() once per generation in the very beginning, so the overhead is tiny and can be ignored
Author
There was a problem hiding this comment.
The intent here was to make the boolean check composable with others and avoid inline .item() usage. This aligns with the broader cleanup discussed in #43089 essentially to keep boolean logic on-device and batch sync points when multiple conditions exist.
zucchini-nlp
reviewed
Jan 5, 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?
This PR refactors the decoder start token check in
_prepare_decoder_input_ids_for_generationto avoid an inline.all().item()call.The boolean condition is now computed as a tensor on-device and converted to a Python boolean at a single, explicit synchronization point. This keeps the logic composable with other boolean checks and enables future batching of multiple conditions into a single GPU–CPU sync, in line with the motivation discussed in #43089.
There is no functional or behavioral change. The refactor is purely structural and preserves all existing semantics and special-case handling (e.g. Donut and Whisper models).
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
This change does not require new tests as it preserves existing behavior
and only restructures the boolean check to avoid an inline synchronization.
Who can review?
This change affects generation utilities.
Tagging: