fix: decouple collator template search from mask offset for BPE robustness#2248
Closed
fix: decouple collator template search from mask offset for BPE robustness#2248
Conversation
… merges Strip trailing \n from instruction/response templates before tokenizing for subsequence search, while preserving the original template token count for masking offsets. This prevents BPE merge mismatches (e.g. \n\n merging into a single token on Qwen/Llama tokenizers) from causing the collator to silently skip 100% of training examples, without including deterministic \n tokens in the loss computation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Important Upgrade your plan to unlock code review, CI analysis, custom rules, and more. |
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.
Summary
An alternative to #2247 for fixing the BPE token boundary mismatch in
DataCollatorForCompletionOnlyLMthat caused training job 630-7257 to produceloss=0across all steps.Same root cause fix, but avoids including the deterministic
\ntoken in loss computation.Problem
When a chat template like
<|im_start|>user\nis followed by message content starting with\n, the tokenizer merges the two newlines into a single BPE token (e.g.\n\n→ token271on Qwen). The collator tokenizes the template standalone and gets the single-\ntoken (198), then searches for that token sequence in the training data — and never finds it. Result: all labels are set toignore_index=-100, producingloss=0for the entire training run.This isn't just a data quality issue — it can also happen at inference time if a user prompt starts with
\n.Approach: Decouple search from masking
Instead of using the same token IDs for both searching and masking, this PR decouples them:
<|im_start|>assistant\n)tokenizer.encode("...assistant")→[151644, 77091]len(tokenizer.encode("...assistant\n"))→3\nexclusion from lossHow it works in practice
Clean data (content doesn't start with
\n):Messy data (content starts with
\n):In the merged case, token
271contains both the template's\nand the content's leading\n. Since you can't split a BPE token, masking it is correct — the template\nshould be masked, and the content's leading\nis whitespace noise.Why not just fix the data?
\ncan also appear at inference time in user promptsTest plan
Related
\nbut includes it in loss)loss=0across all 62 steps, 2 epochs, 8×H100🤖 Generated with Claude Code