You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SentencePieceTokenizer (Unigram) emits one <unk> per unknown character — SentencePiece merges a run of unknown characters into one <unk> #7859
SentencePieceTokenizer.Create(stream) on a SentencePiece Unigram model returns one <unk> id for every character the vocabulary lacks. The reference implementation (google/sentencepiece unigram_model.cc, "merges continuous run of unknown pieces") and HF tokenizers return a single<unk> for a run of such characters. A model trained with SentencePiece therefore receives different ids from .NET than it was trained on.
Encode "ABC 전각 ①" (NFKC → ABC …, three characters absent from the vocabulary):
sentencepiece (Python), HF XLMRobertaTokenizer and XLMRobertaTokenizerFast: ▁ <unk> ▁ <unk> ▁ <unk> — one <unk> per run.
SentencePieceTokenizer.EncodeToIds(text, addBeginningOfSentence: false, addEndOfSentence: false): ▁ <unk> <unk> <unk> ▁ <unk> <unk> ▁ <unk> <unk> — one per character.
Same for "처음 보는 글자 魑魅 😀": each unseen word becomes N <unk> instead of 1.
With a large vocabulary (XLM-RoBERTa's 250k sentencepiece.bpe.model) the two paths agreed on 12 everyday texts, because unknown runs are rare there — the difference shows on rare scripts and characters.
Expected
EncodeToIds merges consecutive unknown pieces into one <unk>, as SentencePiece's Unigram encoder does. (The BPE model path may be worth checking for the same behaviour.)
Workaround
Collapsing consecutive UnknownId entries after EncodeToIds. With that, a sentence encoder's vectors match sentence-transformers to cosine 1.000000 on 14 texts.
Activity