You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
hftokenizer: map Metaspace replacement bytes back to the original space - #86
Metaspace pre-tokenization replaces each 1-byte space with the 3-byte ▁, but each word's byte offsets kept one entry per original byte. With len(offsets) != len(text), spanForSubword fell back to positions in the ▁-prefixed text, so every token after a space got a span 2 bytes too long per ▁.
Each word now builds its offsets byte for byte next to its text, and every byte of ▁ maps to the space it replaces. Spans match HuggingFace tokenizers (byte offsets in Go):
Text
HuggingFace
Before
After
hello world
(0,5) (5,11)
{0 8} {5 13}
{0 5} {5 11}
a b
(0,1) (1,2) (2,4)
{0 4} {1 4} {2 6}
{0 1} {1 2} {2 4}
éé
(0,2) chars
{0 7}
{0 4}
A prepended ▁ takes the offset of the segment's first character, so segments after an added token keep their start offset (<cls>hello → {5 10}).
TestMetaspacePreTokenizeSpans checks these cases, split=false, and subword spans. Same class of drift as #67, here in the pre-tokenizer.
Thanks for following up with this fix! I always let the AI do the first review pass (also helps me understand). I attach it here for you:
(By Gemini Neon:)
The change is clean, concise, and fixes the offset drift properly for both split=true and split=false.
Two small suggestions for your consideration (from an AI-assisted review):
Preserve non-zero starting offset when prepending a space (tokenizers/hftokenizer/pretokenizer.go)
Right above the diff (lines 238–244), newOffsets[0] is hardcoded to 0:
When encodeCore splits on an added token (e.g. "<cls>hello"), "hello" arrives with normOffsets = [5, 6, 7, 8, 9], so the prepended ▁ gets offset 0 instead of 5 — giving "▁hello" span {0, 10} instead of {5, 10} (and {0, 1} if ▁ is emitted as its own subword). Setting:
handles post-added-token segments cleanly while keeping 0 for segments starting at byte 0.
Avoid mutating j inside for j := range size (tokenizers/hftokenizer/pretokenizer.go)
In Go 1.22+ range-over-int, j = 0 inside for j := range size doesn't affect the loop counter, so it works — but it looks like an infinite loop at a glance (and would become one if ever rewritten as for j := 0; j < size; j++). Appending offsets directly inside the if r == ' ' / else branches is a bit clearer and avoids re-checking r == ' ' per byte:
Optional test additions (tokenizers/hftokenizer/pretokenizer_test.go)
A subword slice check (e.g. "▁" → {5, 6} and "world" → {6, 11} inside "▁world"), a split=false case, and a non-zero starting normOffsets case if you include item 1.
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
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.
Metaspace pre-tokenization replaces each 1-byte space with the 3-byte
▁, but each word's byte offsets kept one entry per original byte. Withlen(offsets) != len(text),spanForSubwordfell back to positions in the▁-prefixed text, so every token after a space got a span 2 bytes too long per▁.Each word now builds its offsets byte for byte next to its text, and every byte of
▁maps to the space it replaces. Spans match HuggingFacetokenizers(byte offsets in Go):hello worlda bééA prepended
▁takes the offset of the segment's first character, so segments after an added token keep their start offset (<cls>hello→{5 10}).TestMetaspacePreTokenizeSpanschecks these cases,split=false, and subword spans. Same class of drift as #67, here in the pre-tokenizer.