Skip to content

SentencePieceTokenizer: Unigram ties in multi-word text differ from Hugging Face #7959

Backlog

Description

@krwq

SentencePieceTokenizer.CreateFromTokenizerJson (Unigram) can still pick a different split than Hugging Face tokenizers when several splits have the same total score and the text has more than one word. #7956 fixes the single-word case from #7909 by scoring in double precision like Hugging Face, but not this one.

Hugging Face scores each pre-tokenized word on its own, starting from zero. SentencePieceUnigramModel scores the whole normalized string in one pass, so the running total from earlier words changes how ties in later words round.

Repro with the Paraphrase-multilingual-MiniLM-L12-v2 tokenizer.json from the test data, with #7956 applied:

using var stream = File.OpenRead("tokenizer.json");
var tokenizer = SentencePieceTokenizer.CreateFromTokenizerJson(stream, addBeginningOfSentence: false, addEndOfSentence: false);
Console.WriteLine(string.Join(", ", tokenizer.EncodeToIds("a test9999999")));
Input ML.NET Hugging Face tokenizers 0.23.3
a test9999999 [10, 3034, 5046, 5046, 65040] (▁a, ▁test, 99, 99, 999) [10, 3034, 5046, 65040, 5046] (▁a, ▁test, 99, 999, 99)

test9999999 on its own already gives the Hugging Face result.

Scoring each pre-tokenized word separately, like Hugging Face, matched it on every input I checked. Create with a .model stream should keep scoring the whole string, since that's what SentencePiece does.

Activity

  1. added
    area-Tokenizers
    on Nov 26, 2026
  2. added
    untriaged
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tokenizersuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions