Repository navigation
Fix word alignment when tokenization drops input words - #394
Merged
Merged
Conversation
Collaborator
|
Thanks for fixing it, @issacchan26 ! |
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.
Problem
When a pretokenized word produces no subtokens (for example, an empty string, newline, tab, or spaces),
prepare_word_maskassigns consecutive positions to the remaining words. Span annotations still refer to the original input positions, so the model can silently receive supervision for the wrong word.For example, with the tokens "Alice", a standalone newline, "joined", "Acme", and "yesterday", an Organization annotation at zero-based position 3 should supervise "Acme". Previously, the representation at position 3 belonged to "yesterday".
Change
Build the word mask from the tokenizer's original word IDs, subtracting the prompt length and converting to one-based mask indices. Tokens without subtokens retain a gap in the word positions. Keep the existing first/last/mean/max subtoken selection and token-level behavior.
The regression tests use a local WordPiece tokenizer and a tiny randomly initialized model. They require no network access or pretrained weights and cover:
Validation
main: 29 failed, 4 passed. With the fix: all 33 passed.python -m pytest -q --tb=short --deselect tests/test_models.py::test_span_model: 591 passed, 4 skipped, 1 deselected. The deselected test requires downloading a pretrained GLiNER checkpoint; the other tests ran offline after caching public tokenizer/configuration files.ruff check gliner tests/test_word_alignment.py: passed.ruff format --check tests/test_word_alignment.py: passed.Test environment: Python 3.12, PyTorch 2.14.0, Transformers 4.57.6, Tokenizers 0.22.2. Modified files also parse with Python 3.10 grammar.