8000 hftokenizer: apply GPT-2 ByteLevel regex when use_regex is true (#94) by janpfeifer · Pull Request #95 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content

hftokenizer: apply GPT-2 ByteLevel regex when use_regex is true (#94) - #95

Merged
janpfeifer merged 1 commit into
mainfrom
fix-94
Nov 30, 2026
Merged

janpfeifer merged 1 commit into
mainfrom
fix-94

Conversation

@janpfeifer
Copy link
Copy Markdown
Contributor

Fixes #94

Summary

  • Updated byteLevelPreTokenizeWithOffsets (tokenizers/hftokenizer/pretokenizer.go) to split input using HuggingFace's GPT-2 ByteLevel regex ('s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+) before byte remapping whenever use_regex is true (or omitted), while keeping the no-split path (byteLevelEncodeNoSplit) unchanged when use_regex is false.
  • Bridged Go RE2 differences against Rust tokenizers (onig/fancy-regex):
    • Expanded \s to full Unicode White_Space ([\t\n\x0b\f\r \x{85}\p{Z}]) so characters such as \u00a0, \x0b, \u0085, \u2028, and \u3000 match whitespace rather than punctuation.
    • Emulated \s+(?!\S) via a named capturing group that trims the trailing whitespace rune when at least two whitespace runes precede a non-whitespace rune.
  • Added regression tests in tokenizers/hftokenizer/pretokenizer_test.go:
  • Updated docs/CHANGELOG.md.

Testing

  • go test -short ./... passes across all packages.
  • go test ./modeltests/ -modeltests -cached_only -v -run "TestModels/.*/.*/tokenize" passes across all cached models.

@janpfeifer
janpfeifer merged commit f2b2561 into main Nov 30, 2026
5 checks passed
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 with use_regex splits only at spaces, not at the GPT-2 regex boundaries

1 participant

0