Preserve CLS/SEP for cls/sep-style tokenizers on transformers v5#373
Merged
Ingvarstep merged 1 commit intoJul 6, 2026
Merged
Conversation
Collaborator
|
@yuriihavrylko , awesome catch, thank you for fixing it! |
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.
Addresses the remaining (ModernBERT-family) case of #324.
Problem
#326 fixed the transformers-v5 "uniform low scores" issue for spm-based backbones by setting
add_bos_token/add_eos_tokenin_set_tokenizer_spec_tokens. That helper assigns:On transformers 4.x, cls/sep-style tokenizers don't expose
add_bos_token, so thehasattrgate skips them. On transformers v5 the unified tokenizer backend exposes these attributes on every tokenizer — and for cls/sep-style tokenizers (ModernBERT, ettin, etc.)bos_token_idisNone, so the helper assignsFalse. On v5 that assignment rebuilds the backend post-processor, which strips [CLS]/[SEP] from all encodings:The model then sees inputs without special tokens and returns uniformly near-zero scores — no crash, no warning. Affected checkpoints include
knowledgator/modern-gliner-bi-base/large-v1.0andknowledgator/gliner-pii-edge-v1.0(weights, config and rope translation are all fine — we verified tensor-identical weight loading and identical raw-tokenizer output before isolating this).Fix
Only opt in to bos/eos when the tokenizer actually defines them; never assign
False. This preserves the #326 behavior for spm models (ids exist → setTrue) and leaves cls/sep tokenizers' post-processors intact.Verification (transformers 5.6.2, this branch)
Test input:
"Marie Curie worked at the University of Paris in France."with labels["person", "organization", "location"], threshold 0.3.[](all scores ≤0.01)[('Marie Curie','person',0.85), ('University of Paris','organization',0.88), ('France','location',0.45)]— identical to gliner 0.2.16 + transformers 4.48.3[]