Restore tokenizer padding after callback generation - #6664
Open
RerankerGuo wants to merge 1 commit into
Open
Conversation
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?
LogCompletionsCallbackandWeaveCallbackcurrently set their shared tokenizer'spadding_sideto"left"and leave it there after completion generation. Because the tokenizer belongs to the trainer, this changes padding behavior for later training and evaluation batches.This change moves the temporary setting into
_generate_completions, which is shared by both callbacks:Fixes #6663
PR #5625 also touches callback generation, but it adds
generation_kwargssupport and does not address tokenizer state restoration.Verification
The regression coverage verifies three behaviors:
_generate_completionsuses left padding during successful generation and restores the original value;LogCompletionsCallbackno longer changes the trainer tokenizer after returning.Before the fix:
After the fix:
Commands:
The two skipped full-file tests require optional W&B and Comet integrations. Six warnings report that pinned memory is unavailable on MPS and are unrelated. Pre-commit passed Ruff check, Ruff format, and doc-builder style.
Before submitting
AI writing disclosure
We welcome the use of AI tools to help with contributions. For transparency and to help us improve our review process, please indicate the level of AI involvement in this PR.
Who can review?
Anyone familiar with trainer callbacks or generation padding can review this focused change.
Note
Low Risk
Scoped state-restoration fix in callback generation with tests; no change to generation outputs or training logic beyond fixing tokenizer side effects.
Overview
Fixes a bug where LogCompletionsCallback and WeaveCallback set the trainer tokenizer’s
padding_sideto"left"for generation and never put it back, which could affect later train/eval batches.Generation now applies left padding only inside shared
_generate_completionsvia a_temporary_padding_sidecontext manager. The previouspadding_sideis restored on success and if tokenization or generation raises. The callbacks no longer mutate the tokenizer directly.Regression tests cover temporary left padding in
_generate_completions(including error paths) and thatLogCompletionsCallbackleavespadding_sideunchanged after logging.Reviewed by Cursor Bugbot for commit 344676b. Bugbot is set up for automated code reviews on this repo. Configure here.