fix(template): truncate token type IDs with retained positions - #10176
Open
taking-lying-flat wants to merge 1 commit into
Open
taking-lying-flat wants to merge 1 commit into
taking-lying-flat wants to merge 1 commit into
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.
PR type
PR information
When an encoded multimodal sample exceeds
max_length,Template._truncate()truncatesinput_ids, labels, loss scales, andmm_token_type_idsbut leavestoken_type_idsat its original length. For example, left-truncating a 12-token Gemma3-Vision sample to 8 tokens leaves 12 token types and moves the image tokens away from their original positions. The collator pads these fields independently, preserving the mismatch.Truncate
token_type_idswith the same retained-position mask as the other per-token fields. This also handles non-contiguous retained positions caused by protected image placeholders. The production change is three lines in the shared template implementation.Extend the existing Gemma3 template tests with left/right truncation cases containing text around one or two images. Check the exact values of all five per-token fields and their shapes and values after collation, including non-contiguous retention cases.
Experiment results
python -m pytest tests/general/test_gemma3_template.py tests/general/test_moss_vl.py -q: 14 tests and 4 subtests passed.pre-commit run --files swift/template/base.py tests/general/test_gemma3_template.py: all applicable hooks passed.git diff --check: passed.Validation uses synthetic encoded samples through the real truncation and collator paths. Full-model forward and training were not run.