Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

LlamaTokenizer encodes some runs of repeated characters differently from SentencePiece

Aperta Adatta ai principianti
#7,751 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@Laurianti ci sta già lavorando.

Dal 1/10/2026.

  • #7752 di @Laurianti — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
88/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp

Direzione di ricerca

Inizia in SentencePieceBpeModel.cs leggendo SymbolPair.CompareTo e confronta la gestione dei pareggi con l’implementazione di CodeGenTokenizer. Poi esamina LlamaTestData ed esegui i test del tokenizer che coprono i casi elencati di caratteri ripetuti. Il lavoro è completato quando i casi aggiunti corrispondono agli ID di SentencePiece e i test esistenti del tokenizer hanno esito positivo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C#
Stelle
9.4k
Fork
2k
Merge medio
4g 14h
PR unite (30g)
33

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/machinelearning

Tutte le issue di dotnet/machinelearning

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.