Tokenizers for MoonBit: byte-level BPE and SentencePiece.
/// `table` is the text of a published `cl100k_base.tiktoken` file.
fn ids_of(table : String, text : String) -> Array[Int] raise {
let ranks = @vocab.Ranks::from_tiktoken(table)
let tokenizer = @moontoken.Tokenizer::cl100k(ranks~)
tokenizer.encode(text)
}text the world and the thing
tokens 8
ids 258 262 261 32 258 32 256 260
bytes 23
decoded the world and the thing
count 8
vocabulary 263| Call | What it does |
|---|---|
| Tokenizer::cl100k(ranks~) | cl100k_base: its split, its reserved tokens, over the table you supply. ::o200k, ::o200k_harmony, ::r50k and ::p50k_edit are the same call for the other published encodings; gpt2 and p50k_base are ::r50k's rules over their own tables. |
| Tokenizer::byte_bpe(ranks~, split~) | A table with a split of your own: any rule in split, or a sequence of them. |
| Tokenizer::of_proto(bytes) | A SentencePiece .model file, unigram or BPE, read whole. |
| Tokenizer::parse(text) | A tokenizer.json document. ::of takes the same thing as a Json you already parsed. |
| encode(text) | The ids. special= overrides the policy for this one call. |
| encode_ordinary(text) | The ids of text that holds no reserved token, whatever the policy says. |
| count(text) | How many ids the text costs when every reserved token is recognised — the number to budget a context window with. |
| decode(ids) | The text, with U+FFFD standing in for the byte sequences that are not text. |
| decode_bytes(ids) | The bytes, exact. reserved=false drops the reserved tokens instead of spelling them. |
| token_bytes(id) | The bytes one id stands for, reserved ids included. |
| length() | How many entries the vocabulary has, reserved ones not counted. |
| Package | What it is |
|---|---|
| moontoken | The tokenizer: encode, encode_ordinary, decode, count, and the ways to build one |
| split | The pre-tokenizers of the published encodings, written as scanners |
| vocab | A rank table, from the base64 file OpenAI publishes or from pairs |
| bpe | Byte-pair merging: the byte-level model, and the word-level one a tokenizer.json names |
| piece | The SentencePiece model file: both trainer types, and the trie it compiles |
| normalizer | The rule table a model file carries, and the operations a tokenizer.json lists |
| special | The tokens a model reserves, and what may happen to them |
| refused | The one error shape the whole library raises |
fn four_ways(tokenizer : @moontoken.Tokenizer, text : String) -> Array[Array[Int]] raise {
[
tokenizer.encode(text, special=@special.Refuse), // refuse it
tokenizer.encode(text, special=@special.Allow), // take it as the token
tokenizer.encode(text, special=@special.Ignore), // encode it as text
tokenizer.encode(text, special=@special.Only(["<|endoftext|>"])),
]
}| What | Rows | Passing |
|---|---|---|
| cl100k_base and o200k_base, over a 600-entry slice of each published table | 84 | 84 |
| The reserved tokens, under each policy | 6 | 6 |
| SentencePiece unigram, BPE, and unigram without the dummy prefix | 258 | 258 |
| Total | 348 | 348 |
| Reference | Encoders | Comparisons | Agreeing |
|---|---|---|---|
| tiktoken 0.14.0 | 6 | 2988 | 2988 |
| sentencepiece 0.2.3 | 3 | 1494 | 1494 |
| tokenizers 0.23.1 | 2 | 996 | 996 |
| Total | 11 | 5478 | 5478 |
moon build --target native
pip install tiktoken==0.14.0 sentencepiece==0.2.3 tokenizers==0.23.1
python scripts/diff_reference.py --preparemoon add moonbitstack/moontokenInstall
Download zipTokenizers for MoonBit: byte-level BPE and SentencePiece.