Fix Id2Token Bugs#1060
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR fixes Marian case-encoding decoding issues in SpmUgmDecoder::Id2Token by replacing a position-0-only marker check with a byte-level per-piece state machine, and adds regression tests to cover real-world failure cases.
Changes:
- Reworked
Id2Tokento scan markers throughout each piece and propagate case mode across piece boundaries. - Added inline handling for SPM space markers and implicit mode resets at non-letter boundaries.
- Added 4 C API roundtrip regression tests covering the three reported bugs and a combined scenario.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| operators/tokenizer/ugm_kernels.hpp | Rewrites Marian case-decoding logic in Id2Token using a per-piece UTF-8 aware state machine and persists mode in decoding state. |
| test/pp_api_test/test_tokenizer_capi.cc | Adds regression tests that tokenize→detokenize and assert exact text roundtrip for the three bugs plus a combined case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
apsonawane
approved these changes
May 13, 2026
sayanshaw24
added a commit
to microsoft/onnxruntime-genai
that referenced
this pull request
May 13, 2026
Includes fixes in microsoft/onnxruntime-extensions#1060. Co-authored-by: Sayan Shaw <sayanshaw@microsoft.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix three Id2Token bugs in case-encoder decoding
Problem
Three bugs were found in
SpmUgmDecoder::Id2Token(operators/tokenizer/ugm_kernels.hpp) when decoding tokens from models that use case-encoding. The previous implementation only recognized case-encoder markers at position 0 of a unigram piece, which broke decoding in three scenarios:U) mode was not carried across SPM piece boundaries. For example,"Umc"+"p"decoded to"MCp"instead of"MCP"."iTphone"whereTis a titlecase marker), the marker was emitted literally instead of being applied, producing"iTphone"instead of"iPhone".L(lowercase) marker at a non-letter codepoint boundary, the decoder failed to reset the mode. For example,"Upp"+"v"+"-"+"mp"decoded to"PPV-MP"instead of"PPV-mp".Fix
Replaced the position-0-only marker check in
Id2Tokenwith a proper per-piece byte-level state machine that:U,A,T,L,P), not just position 0signature_) across piece boundaries via theTokenizerDecodingStateU/Tmodes at word boundaries while allowingA(all-uppercase) to persist across spacesU/Tmode at non-letter codepoints when the encoder's explicitLwas dropped by SPM scoringChanges
operators/tokenizer/ugm_kernels.hpp— RewroteSpmUgmDecoder::Id2Tokencase-encoding path (-51/+138 lines). Added#include <cstring>forstd::memcmp.test/pp_api_test/test_tokenizer_capi.cc— Added 4 regression tests using NMT tokenizer roundtrip (tokenize → detokenize → compare):MarianId2Token_CrossPieceModePropagate— Bug 1MarianId2Token_MidPieceMarker— Bug 2MarianId2Token_ImplicitLReset— Bug 3MarianId2Token_CombinedBugs— All three in one sentenceValidation