Repository navigation
Don't align wordpieces to the previous token via the preceding space - #429
Merged
Merged
Conversation
With fast tokenizers whose offsets include the preceding space (e.g. sentencepiece models like DeBERTa-v3), each wordpiece was also aligned to the previous spaCy token, because the character after each token was mapped to that token. Map trailing whitespace separately and only use it as a fallback for wordpieces that don't overlap any token characters, so whitespace-only wordpieces (e.g. GPT-2 "Ġ") are still aligned. Fixes explosion/spaCy#13730. Supersedes #426. Co-authored-by: Mayuri <mayuriphad656@gmail.com>
This was referenced Oct 29, 2026
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.
Fixes explosion/spaCy#13730 (the DeBERTa duplicate alignments). Supersedes #426.
Problem
get_span2wp_from_offset_mappingmapped the character after each spaCy token (its trailing space) to that token. With fast tokenizers whose offsets include the preceding space (SentencePiece models like DeBERTa-v3), every wordpiece was therefore also aligned to the previous token, e.g.▁is(10, 13)aligned to bothCopenhagenandis. When pooling, nearly every token got its neighbour's wordpiece mixed in.Fix
Simply dropping the
+1(as in #426) leaves whitespace-only wordpieces such as GPT-2'sĠunaligned, which breaks the existing invariant intest_model_wrapper.pythat all non-special fast-tokenizer wordpieces are aligned. Instead, trailing whitespace is mapped separately and only used as a fallback for wordpieces that don't overlap any token characters.Genuine many-to-many overlaps are unchanged, e.g.
▁worldcovering both a spaCy whitespace token' 'andworld.Tests
test_offset_alignment(the DeBERTa cases fail on master), plus a length check on the result.microsoft/deberta-v3-xsmall,roberta-base,tiny-random-gpt2andbert-base-uncasedoffsets: no spurious duplicates, no unaligned wordpieces. RoBERTa/BERT alignments are unchanged.The RoBERTa fast/slow differences also reported in spaCy#13730 are not addressed here.
Thanks to @mayuriphad for the diagnosis in #426.