8000 hftokenizer: map Metaspace replacement bytes back to the original space by daescha · Pull Request #86 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content

hftokenizer: map Metaspace replacement bytes back to the original space - #86

Merged
janpfeifer merged 2 commits into
gomlx:mainfrom
daescha:metaspace-span-offsets
Nov 24, 2026
Merged

janpfeifer merged 2 commits into
gomlx:mainfrom
daescha:metaspace-span-offsets

Conversation

@daescha
@daescha daescha commented Nov 24, 2026 •
Copy link
Copy Markdown
Contributor

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.

@janpfeifer
janpfeifer commented Nov 24, 2026 •
Copy link
Copy Markdown
Contributor

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):

  1. 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:

    newOffsets := make([]int, len(normOffsets)+1)
    newOffsets[0] = 0
    copy(newOffsets[1:], normOffsets)

    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:

    if len(normOffsets) > 0 {
        newOffsets[0] = normOffsets[0]
    }

    handles post-added-token segments cleanly while keeping 0 for segments starting at byte 0.

  2. 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:

    for i, r := range text {
        if r == ' ' {
            if split {
                flush()
            }
            current.WriteString(replacement)
            spaceOrig := orig(i)
            for range len(replacement) {
                offsets = append(offsets, spaceOrig)
            }
        } else {
            current.WriteRune(r)
            for j := range utf8.RuneLen(r) {
                offsets = append(offsets, orig(i+j))
            }
        }
    }
  3. 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.

@janpfeifer
janpfeifer merged commit 2ab878a into gomlx:main Nov 24, 2026
1 check passed
@janpfeifer
Copy link
Copy Markdown
Contributor

Thanks again!

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.

2 participants

0