Skip to content

fix: Inconsistent Token Alignment Between Fast and Slow Tokenizers - #426

Open
mayuriphad wants to merge 1 commit into
explosion:masterfrom
mayuriphad:fix/fast-tokenizer-alignment
Open

mayuriphad wants to merge 1 commit into
explosion:masterfrom
mayuriphad:fix/fast-tokenizer-alignment

Conversation

@mayuriphad

Copy link
Copy Markdown

Fixes explosion/spaCy#13730

When using fast tokenizers (like DeBERTa), the alignment IDs were being duplicated across adjacent spaCy tokens. This happened because char_to_sp_token mapped the trailing whitespace (which isn't part of the oken.text) to the preceding token.

Fast tokenizers often produce offset_mapping that includes the preceding space for wordpieces (e.g. _word). When that space character mapped to the previous token, the wordpiece was aligned to BOTH the previous token and the current token, resulting in duplicate alignments (�lign_lengths = 2).

This patch removes the + 1 from the range loop so that trailing spaces are not mapped to the preceding token. Wordpieces will only align to tokens whose non-whitespace characters they overlap with.

Copilot AI balanced review requested due to automatic review settings September 29, 2026 05:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@mayuriphad

Copy link
Copy Markdown
Author

Hi maintainers, a gentle ping on this PR whenever you have a moment to review. I'm happy to make any changes or add tests if something needs adjusting. Thank you for your time!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent Token Alignment Between Fast and Slow Tokenizers in spacy-transformers

2 participants