LlamaTokenizer encodes some runs of repeated characters differently from SentencePiece
メンテナーはふだん 2 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 88/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- csharp
調査の方向性
SentencePieceBpeModel.cs で SymbolPair.CompareTo を読み、その同順位の処理を CodeGenTokenizer の実装と比較することから始めます。次に LlamaTestData を調べ、一覧にある同じ文字の繰り返しケースを対象とする tokenizer テストを実行します。追加したケースが SentencePiece の ID と一致し、既存の tokenizer テストが通れば完了です。
索引モデルが issue の本文から書いたものです。
説明
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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dotnet/machinelearning のほかの issue
-
BertTokenizer merges words separated only by `\n`, `\t` or `\r`対応中かも @rosebyte が 21 日前に担当しました。 オープンarea-Tokenizers green
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
dotnet/machinelearning#7724 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 2 日以内に返信
-
area-TimeSeries documentation untriaged
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
dotnet/machinelearning#7205 ·
メンテナーはふだん 2 日以内に返信
-
area-ONNX documentation Priority:3
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
dotnet/machinelearning#5894 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
Add a .vsconfig fileオープンarea-Infrastructure Priority:2
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
dotnet/machinelearning#3260 · コメント 2 件 · リアクション 1 件 ·
メンテナーはふだん 2 日以内に返信
-
area-Core enhancement Priority:3
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
dotnet/machinelearning#2669 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
dotnet/machinelearning の issue をすべて見る
似ている issue
-
documentation
難易度 1/5 1〜3時間 初心者へのやさしさ 62/100
-
bug misleading tooling trivial
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
Docs example projects pinned to a literal library version instead of 3.*対応中かも @DaveSkender が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
facioquo/stock-indicators-dotnet#2372 ·
メンテナーはふだん 1 日以内に返信
-
[Docs] officecli-docx SKILL.md still describes `\n` as a soft line break after #262対応中かも @giprime が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
Facepunch/sbox-public#12097 ·
メンテナーはふだん 2 日以内に返信