Fail fast on inconsistent class_token_index/vocab_size (fixes #332)#371
Open
Toadoum wants to merge 1 commit into
Open
Fail fast on inconsistent class_token_index/vocab_size (fixes #332)#371Toadoum wants to merge 1 commit into
Toadoum wants to merge 1 commit into
Conversation
0400a95 to
3310f49
Compare
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.
Fail fast on inconsistent class_token_index/vocab_size instead of training with silent zero loss
Fixes #332
Problem
When
class_token_indexandvocab_sizeare set explicitly in the config but the tokenizer does not actually contain the GLiNER special tokens,_resize_token_embeddingsskips adding them and trusts the configured indices. The class-token mask (input_ids == class_token_index) then never matches anything, the entity prompt setup is effectively empty, and training runs to completion withloss=0andgrad_norm=0— no error, no warning. As reported in #332, this looks like a normal run and is expensive to discover on long jobs.Change
Adds
BaseGLiNER.validate_special_token_config(config, tokenizer), called from_resize_token_embeddingswhen bothclass_token_indexandvocab_sizeare explicit (i.e., exactly the branch where special tokens are not auto-added). It:ValueErrorwhenclass_token_indexis out of range for the tokenizer (the mmBERT-base case from the issue: index 256001 with a 256000-entry vocab);ValueErrorwhenent_token(<<ENT>>) is missing from the tokenizer vocabulary, since that guarantees the zero-loss failure mode;sep_token/rel_tokenare missing, whenclass_token_indexpoints at a different token id thanent_token, or whenvocab_sizedisagrees with the tokenizer length.Both error messages tell the user the concrete fix: set
class_token_index: -1andvocab_size: -1so GLiNER adds the tokens and detects indices automatically (the workaround confirmed in the issue).Validation is deliberately skipped when either value is
-1or whenresize_token_embeddings=Falseis combined with a-1sentinel, so manual setup workflows are unaffected. Loading published checkpoints (tokens present, indices consistent) passes silently.Design notes / open questions
ent_tokenabsence is a hard error; a missingsep_tokendegrades prompts but does not zero out the loss, so it warns. Happy to make it strict if you prefer.Testing
tests/test_special_token_validation.py(6 tests, offline, fake tokenizer following the mock style oftests/test_models.py): out-of-range index raises, missing<<ENT>>raises, consistent config passes with zero warnings, index/ent-id mismatch warns, missing<<SEP>>warns, vocab-size mismatch warns.test_infer_packing.py, mock-basedtest_models.pytests).