Skip to content

[fix][whisper]: fix max_new_tokens handling - #46795

Merged
eustlb merged 1 commit into
mainfrom
fix-audio-ci-maxlength
Jul 23, 2026
Merged

[fix][whisper]: fix max_new_tokens handling#46795
eustlb merged 1 commit into
mainfrom
fix-audio-ci-maxlength

Conversation

@eustlb

@eustlb eustlb commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

PR #44130 changed how the generation loop enforces max_length: it is now the strict total decoder length (forced/initial tokens + generated content), whereas the old cache_position based approach effectively didn't count one position.

Whisper prepends forced decoder tokens before the transcript ([<|startoftranscript|>, <|lang|>, <|transcribe|>, <|notimestamps|>]). So when a user passes max_length=20 they mean "20 content tokens," and _set_max_new_tokens_and_length is supposed to inflate the limit to leave room for that forced prefix. Two defects broke this:

  1. The compensation was computed but discarded.
    The function computed max_length = min(generation_config.max_length + num_initial_tokens, …) but a refactor Fix whisper kwargs and generation config #30018 dropped the line that wrote it back. That refactor
    reworked _set_max_new_tokens_and_length from returning a kwargs dict (which set kwargs["max_length"]) to mutating generation_config in place. The max_new_tokens branch got its in-place replacement, but the max_length branch's equivalent (generation_config.max_length = max_length) was never carried over. It became dead code, harmless under the old loop, actively harmful once [generate] Completely stop relying on cache_position to prepare inputs #44130 made max_length strict.

  2. An off-by-one hidden in num_initial_tokens.
    The compensation used decoder_input_ids.shape[-1] - 1. That -1 existed to cancel the old loop's free (uncounted) position, so the net inflation still came out to the full prefix length. PR [generate] Completely stop relying on cache_position to prepare inputs #44130 removed that quirk, so the -1 now under-counts the prefix by one, leaving the output one token short even after re-adding the assignment.

TD;DR: #30018 planted latent dead code, #44130 activated it

Fixes:

  • test_modeling_whisper.py::WhisperModelIntegrationTests::test_tiny_generation
  • test_modeling_whisper.py::WhisperModelIntegrationTests::test_tiny_en_generation

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: whisper

@eustlb

eustlb commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator Author

run-slow: whisper

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/whisper"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN fe761868 workflow commit (merge commit)
PR 5c10ba10 branch commit (from PR)
main 1048e9af base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@eustlb
eustlb requested review from Copilot and vasqu and removed request for Copilot July 20, 2026 12:29

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wow thanks for catching this. It's really hard to keep up with custom generation loops 😢

@eustlb
eustlb added this pull request to the merge queue Jul 23, 2026
Merged via the queue into main with commit ee48a63 Jul 23, 2026
104 checks passed
@eustlb
eustlb deleted the fix-audio-ci-maxlength branch July 23, 2026 13:36
stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
Sainava pushed a commit to Sainava/Sai-transformers that referenced this pull request Aug 3, 2026
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.

3 participants