8000 hftokenizer: fix ByteLevel spans & trim_offsets (#91); modeltests: teacher-forced generation checks by janpfeifer · Pull Request #93 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content

hftokenizer: fix ByteLevel spans & trim_offsets (#91); modeltests: teacher-forced generation checks - #93

Merged
janpfeifer merged 2 commits into
mainfrom
fix-91
Nov 30, 2026
Merged

janpfeifer merged 2 commits into
mainfrom
fix-91

Conversation

@janpfeifer
Copy link
Copy Markdown
Contributor

Fixes #91

Summary

1. tokenizers/hftokenizer: ByteLevel token spans and trim_offsets (fixes #91)

  • Fixed byteLevelPreTokenizeWithOffsets and byteLevelEncodeNoSplit to record one original byte offset per UTF-8 byte written for GPT-2 mapped characters (such as 2-byte Ġ, Ċ, and multi-byte UTF-8 characters), instead of under-filling word.offsets and falling back to remapped byte lengths that shifted spans past the end of the input.
  • Mapped multi-byte UTF-8 runes' constituent byte-level tokens to the full original character span [origStart, origLast+1) (matching HuggingFace ByteLevel::pre_tokenize, so split multi-byte runes never slice invalid UTF-8), and preserved normOffsets[0] on prepended spaces in ByteLevel (add_prefix_space: true) and Sequence pre-tokenizers.
  • Added TrimOffsets, AddPrefixSpace, UseRegex, and Processors fields to PostProcessor (and TrimOffsets to PreTokenizer), and implemented trimByteLevelOffsets matching HuggingFace tokenizers' byte_level::process_offsets for RobertaProcessing, ByteLevel, and Sequence post-processors.
  • Updated EncodeWithAnnotations to run post-processor offset trimming even when AddSpecialTokens is false (matching HuggingFace tokenizers), while still only inserting special tokens when AddSpecialTokens is true.
  • Added unit tests (TestByteLevelPreTokenizeSpans, TestPostProcessor_RobertaAndByteLevelSpans, plus span assertions in TestSplitPreTokenizer_WhitespaceLookaheadAndByteLevelNoRegex).

2. modeltests: Teacher-forced kvcache_parity and teacher_forced_generate

  • Switched kvcache_parity to teacher-forced multi-step logit parity (single-pass causal naive forward vs. KV-cache prefill + forced single-token decode steps), asserting full-vocabulary cosine similarity, full-vocabulary max abs diff, and top-1 agreement whenever logit margin exceeds top1_margin_tolerance.
  • Replaced free-running greedy_generate with teacher_forced_generate, feeding each golden greedy_token_ids[k-1] into step k and asserting both per-step logit gap (max(logits) - logits[wantID] <= 2 * logit_max_abs_diff) and minimum exact top-1 match count—preventing 1-ULP BFloat16 near-ties from cascading across subsequent tokens.
  • Updated docs/ModelTests.md, docs/CHANGELOG.md, .agents/AGENTS.md, and bumped github.com/gomlx/gomlx in go.mod/go.sum to include ml/zoo/transformer/generate: normalize integer prompt tensors to Int32 gomlx#505.

Testing

  • GOWORK=off go test -short ./... passes across all packages.
  • Verified live against Meyssa/gector-large-2024 (tokenizer.json at commit 93faea4b7a13a0384a37b10779a567dacea5ba41) vs HuggingFace tokenizers 0.22.2: exact byte-offset parity on "I has three cats" ([0,1), [2,5), [6,11), [12,16)), leading/multiple spaces, multi-byte UTF-8/CJK, added tokens, and AddSpecialTokens: false.
  • Verified go test ./modeltests/ -modeltests -backends="go;xla:cuda" on gemma4-e4b and qwen3.5-4b: all prefill_logits, variables, kvcache_parity, and teacher_forced_generate subtests pass on both backends.

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.

hftokenizer: ByteLevel token spans include the leading space and end one byte late

1 participant

0