Repository navigation
Keep special token ids the tokenizer does not define #48708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SunMarc
merged 5 commits into
huggingface:main
from
albertvillanova:align-special-tokens-keep-declared-ids
Sep 22, 2026
+59
−48
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b0f2af
Keep special token ids the tokenizer does not define
albertvillanova 4011005
Merge remote-tracking branch 'upstream/main' into align-special-token…
albertvillanova 60e31f5
Extract a helper for the single valued special tokens
albertvillanova fc0ed51
Align the EOS token through the same helper
albertvillanova 12757ca
Merge remote-tracking branch 'upstream/main' into align-special-token…
albertvillanova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1365,6 +1365,25 @@ def test_special_token_alignment_composite_config(self): | |
| self.assertEqual(text_config.bos_token_id, tokenizer.bos_token_id) | ||
| self.assertEqual(text_config.pad_token_id, tokenizer.pad_token_id) | ||
|
|
||
| def test_special_token_alignment_keeps_tokens_the_tokenizer_does_not_define(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||
| """ | ||
| Tests that a special token the tokenizer does not define is left untouched on the model configs, rather than | ||
| being removed from them. `_special_tokens_map` is initialized with `None` for every special token, so a | ||
| tokenizer that never declared one is indistinguishable from a tokenizer whose token was deliberately cleared. | ||
| Dropping the value in that case would silently remove an id the checkpoint declares. | ||
| """ | ||
| model = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-LlamaForCausalLM") | ||
| tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-LlamaForCausalLM") | ||
|
|
||
| model.config.bos_token_id = 1 | ||
| model.generation_config.bos_token_id = 1 | ||
| tokenizer.bos_token = None | ||
|
|
||
| align_special_tokens(model, tokenizer) | ||
|
|
||
| self.assertEqual(model.config.bos_token_id, 1) | ||
| self.assertEqual(model.generation_config.bos_token_id, 1) | ||
|
|
||
| def test_trainer_works_without_model_config(self): | ||
| """ | ||
| Tests that models without a `config` parameter can still be trained. | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should warn/inform about this no somehow ? maybe we already do but just checking
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can, and I think it would be counterproductive.
The two cases are indistinguishable at this point:
_special_tokens_mapis initialized withNonefor every special token, so a tokenizer that never declared one looks exactly like a tokenizer whose token was deliberately cleared. The case worth informing about is the second, and it is the one we cannot detect.What we can detect is the first, and it is the common case: every Qwen model defines no BOS, so informing there would fire on every
train()for a very large share of users, which is the noise this PR removes. If someone does want the removal, they can set it on the config directly, which is the explicit opt-in you suggested on #48592.The previous behaviour was that warning: when the tokenizer did not define a token, the config value was overwritten with
Noneand reported as an update. On a TRL CI run that produced 311Updated tokens: {'bos_token_id': None}lines out of 457 warning lines, one per model whose tokenizer defines no BOS, which is most of them.Now nothing is changed in that case, so there is nothing to report. The warning still covers every token that is actually modified.