Skip to content

Allow SentencePiece Unigram models without BOS or EOS, such as T5 - #7757

Open
Laurianti wants to merge 2 commits into
dotnet:mainfrom
Laurianti:fix-unigram-optional-bos-eos
Open

Laurianti wants to merge 2 commits into
dotnet:mainfrom
Laurianti:fix-unigram-optional-bos-eos

Conversation

@Laurianti

@Laurianti Laurianti commented Oct 1, 2026 •

Copy link
Copy Markdown

Loading a Unigram SentencePiece model trained without BOS, such as T5 (bos_id = -1), threw IndexOutOfRangeException in the SentencePieceUnigramModel constructor, whatever addBeginningOfSentence was. A model read from a ModelProto now treats an absent BOS or EOS as a model built from a vocabulary does: the id stays -1 instead of becoming 0, which is <pad> in T5, the piece is not mapped, and requesting it throws ArgumentException.

Models from the Hugging Face hub, created with Create(stream, addBeginningOfSentence: false, addEndOfSentence: false); the columns give the English sentences of the XNLI test split (1,670) that encode as in SentencePiece 0.2.2:

Model bos_id Before Now
google-t5/t5-small -1 IndexOutOfRangeException 1,670 of 1,670
google-t5/t5-base -1 IndexOutOfRangeException 1,670 of 1,670
google/t5-v1_1-small -1 IndexOutOfRangeException 1,670 of 1,670
google/flan-t5-small -1 IndexOutOfRangeException 1,670 of 1,670
google/flan-t5-base -1 IndexOutOfRangeException 1,670 of 1,670
google/mt5-small -1 IndexOutOfRangeException 1,670 of 1,670
google/mt5-base -1 IndexOutOfRangeException 1,670 of 1,670
google/long-t5-tglobal-base -1 IndexOutOfRangeException 1,670 of 1,670
google/pegasus-xsum -1 IndexOutOfRangeException 1,670 of 1,670
google/umt5-small 2 1,670 of 1,670 1,670 of 1,670
FacebookAI/xlm-roberta-base 1 1,670 of 1,670 1,670 of 1,670

Requesting the BOS of such a model, as Create(stream) does by default, now throws ArgumentException for addBeginningOfSentence, as when the model is built from a vocabulary; SentencePiece also rejects a BOS the model does not define. The two models with a BOS are unchanged.

Four tests in SentencePieceTests build Unigram models without BOS, laid out as T5's, and without EOS, and cover encoding, decoding and the request for the missing token; all fail without the change.

Fixes #7756

Copilot AI balanced review requested due to automatic review settings October 1, 2026 15:58
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Pull requests submitted by community contributors label Oct 1, 2026

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.

Copilot review overview

🟡 Changes recommended

The new missing-EOS behavior lacks corresponding test coverage.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Enables Unigram SentencePiece models without BOS/EOS tokens, including T5-style models.

Changes:

  • Preserves absent BOS/EOS IDs as -1.
  • Avoids mapping absent special tokens.
  • Adds missing-BOS behavior tests.
File Description
SentencePieceUnigramModel.cs Handles optional BOS/EOS IDs safely.
SentencePieceTests.cs Tests T5-style models without BOS.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.91%. Comparing base (13dc9c5) to head (100257a).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7757      +/-   ##
==========================================
+ Coverage   69.89%   69.91%   +0.01%     
==========================================
  Files        1487     1487              
  Lines      276323   276390      +67     
  Branches    28295    28297       +2     
==========================================
+ Hits       193142   193238      +96     
+ Misses      75694    75667      -27     
+ Partials     7487     7485       -2     
Flag Coverage Δ
Debug 69.91% <100.00%> (+0.01%) ⬆️
production 64.09% <100.00%> (+0.01%) ⬆️
test 89.84% <100.00%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Δ
...t.ML.Tokenizers/Model/SentencePieceUnigramModel.cs 68.08% <100.00%> (+0.65%) ⬆️
...icrosoft.ML.Tokenizers.Tests/SentencePieceTests.cs 96.95% <100.00%> (+0.31%) ⬆️

... and 5 files with indirect coverage changes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Pull requests submitted by community contributors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SentencePieceTokenizer.Create throws IndexOutOfRangeException for Unigram models without BOS, such as T5

2 participants