Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Avoid inline .item() sync in decoder start token check#43094

Open
pythongiant wants to merge 3 commits into
huggingface:mainfrom
pythongiant:patch-2
Open

Avoid inline .item() sync in decoder start token check#43094
pythongiant wants to merge 3 commits into
huggingface:mainfrom
pythongiant:patch-2

Conversation

@pythongiant

Copy link
Copy Markdown

What does this PR do?

This PR refactors the decoder start token check in _prepare_decoder_input_ids_for_generation to 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

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

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:

pythongiant and others added 3 commits January 4, 2026 10:05
….all().item()` condition into a separate tensor boolean to avoid inline GPU syncs and allow future batching of boolean checks. No functional change.
)
model_kwargs["decoder_attention_mask"] = decoder_attention_mask

else:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zucchini-nlp what do you think?

Comment thread src/transformers/generation/utils.py
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