Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

LlamaTokenizer encodes some runs of repeated characters differently from SentencePiece

オープン 初心者向け
#7,751 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@Laurianti がすでに取り組んでいます。

2026年10月1日 から。

  • #7752 @Laurianti による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
88/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp

調査の方向性

SentencePieceBpeModel.cs で SymbolPair.CompareTo を読み、その同順位の処理を CodeGenTokenizer の実装と比較することから始めます。次に LlamaTestData を調べ、一覧にある同じ文字の繰り返しケースを対象とする tokenizer テストを実行します。追加したケースが SentencePiece の ID と一致し、既存の tokenizer テストが通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

untriaged

System Information (please complete the following information):

  • OS & Version: Windows 11
  • ML.NET Version: Microsoft.ML.Tokenizers 3.0.0-preview.26457.2, same code on main at 681cfb6b
  • .NET Version: .NET 10.0

Describe the bug
LlamaTokenizer encodes some runs of repeated characters differently from SentencePiece. When several pairs of symbols can merge into pieces with the same score, SentencePiece merges the leftmost pair first, while SentencePieceBpeModel merges the rightmost one.

To Reproduce

using Stream stream = File.OpenRead("tokenizer.model"); // https://huggingface.co/hf-internal-testing/llama-tokenizer
LlamaTokenizer tokenizer = LlamaTokenizer.Create(stream);
Console.WriteLine(string.Join(", ", tokenizer.EncodeToIds("____")));
Text LlamaTokenizer SentencePiece 0.2.2
____ 1, 4770, 1649 (▁__, __) 1, 903, 22359 (▁_, ___)
...... 1, 6317, 3045 (▁.., ....) 1, 13035, 636 (▁...., ..)
~~~~ 1, 3695, 30022, 7377 (▁~, ~, ~~) 1, 3695, 7377, 30022 (▁~, ~~, ~)

Hugging Face tokenizers gives the same ids as SentencePiece. The difference is rare in prose but common in source code, where indentation and separator lines such as // ---------------- are runs of one character. Compared with SentencePiece 0.2.2:

Corpus Texts Encoded differently from SentencePiece
WikiText-103, test split 2,891 paragraphs 1 (0.03%)
XNLI, test split, 15 languages 75,150 sentences 39 (0.05%)
Source code in C#, Java, Python, C++ and TypeScript from public repositories 7,560 blocks of 20 lines 1,567 (20.7%)
Total 85,601 1,607

In code, for instance, a line indented by 20 spaces before if starts with ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁, ▁▁▁, ▁if in SentencePiece and with ▁▁▁, ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁, ▁if in LlamaTokenizer. With the fix below, no text in the three sets differs.

Expected behavior
The ids SentencePiece produces. Its SymbolPairComparator in bpe_model.cc ranks a pair lower when the scores are equal and its left is greater (i1 == i2 && h1.left > h2.left), so the leftmost pair merges first.

Additional context
SymbolPair.CompareTo in SentencePieceBpeModel.cs breaks ties with other.Left.CompareTo(Left), which dequeues the rightmost pair first; the SymbolPair of CodeGenTokenizer uses Left.CompareTo(other.Left). I have a fix, Left.CompareTo(other.Left), with the three cases above added to LlamaTestData, and would like to contribute it.

主要言語
C#
スター
9.4k
フォーク
2k
平均マージ
3日 17時間
マージ済み PR(30日)
27

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/machinelearning のほかの issue

dotnet/machinelearning の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。