mojolearn.tokenizer#
Byte-level BPE tokenization with a vocabulary you supply or train.
mojolearn ships no vocabulary. BpeTokenizer encodes and decodes
byte-level BPE by merge rank, splitting text into pre-tokens with the GPT-2
pre-tokenization pattern, with <|endoftext|> as the id after the last
rank. Load a vocabulary you have, or train one with
BpeVocabularyTrainer:
tok = BpeTokenizer.from_files("encoder.json", "vocab.bpe")
tok = BpeTokenizer.from_ranks_file("ranks.tsv") # rank<TAB>hex lines
tok = BpeTokenizer.from_token_bytes(list_of_bytes) # rank = list index
tok = BpeVocabularyTrainer(vocab_size=32000).train(docs).tokenizer()
With the encoder.json and vocab.bpe files OpenAI publishes with
GPT-2 (download them yourself), the ids are GPT-2’s. Tokenization runs in
compiled code on the CPU and reads no data file other than the vocabulary.
For the Llama 3 and Qwen 2 pre-tokenization patterns, see
mojolearn.models.Tokenizer.
GPT2Tokenizer is a deprecated alias of BpeTokenizer (accessing it
issues a DeprecationWarning).
Classes
|
Byte-level BPE tokenizer in the GPT-2 format, over your vocabulary. |
|
Train a byte-level BPE vocabulary deterministically. |
|
A vocabulary trained by |
- class mojolearn.tokenizer.BpeTokenizer(ranks_file=None)[source]#
Bases:
objectByte-level BPE tokenizer in the GPT-2 format, over your vocabulary.
Ids are in
[0, n_vocab): the vocabulary’s ranks, then<|endoftext|>atn_vocab - 1. The special token is recognized only withallow_endoftext=True; otherwise its characters are ordinary text. There are no other special tokens.Bytes are the interface: encode_bytes() and decode_bytes() do the work. encode() also accepts
str(encoded as UTF-8 first); decode() decodes the bytes as UTF-8 witherrors="replace", since a token sequence may end inside a character. Invalid UTF-8 input round trips.GPT2Tokenizeris a deprecated alias of this class.- Parameters:
ranks_file (str or path-like) – A rank file (see from_ranks_file()). Required: there is no built-in vocabulary, so omitting it raises ValueError.
- Raises:
FileNotFoundError – If
ranks_filedoes not exist.
Examples
>>> from mojolearn.tokenizer import BpeTokenizer >>> tok = BpeTokenizer.from_files("encoder.json", "vocab.bpe") >>> ids = tok.encode("Hello, world") >>> tok.decode(ids) 'Hello, world'
- classmethod from_ranks_file(path)[source]#
Load a rank file.
One
rank<TAB>hex_of_token_bytesline per rank, ascending from 0, with all 256 single bytes present (the formatTrainedBpeVocabulary.write_ranks()writes).- Returns:
BpeTokenizer
- classmethod from_token_bytes(tokens)[source]#
Build a tokenizer from a list of token byte strings.
A token’s rank (and id) is its index. Tokens must be unique and non-empty, and all 256 single bytes must be present; otherwise ValueError is raised before anything is loaded.
- Parameters:
tokens (sequence of bytes-like)
- Returns:
BpeTokenizer
- classmethod from_files(encoder_json, vocab_bpe)[source]#
Load the two files of the GPT-2 format.
- Parameters:
encoder_json (str or path-like) –
encoder.json: token spelling to id.vocab_bpe (str or path-like) –
vocab.bpe: the merge list.
- Returns:
BpeTokenizer
- Raises:
ValueError – If the files are inconsistent: a spelling that does not decode through the byte-to-unicode table, ids that are not 0 to n-1 (with
<|endoftext|>, if present, at n), a merge whose parts or result are not tokens, merge results whose ids do not increase above both parts, or a multi-byte token no merge makes. These are the conditions under which merging by rank reproduces the merge list.
- property n_vocab#
the ranks plus
<|endoftext|>.- Type:
Number of ids
- property eot_token#
The id of
<|endoftext|>,n_vocab - 1.
- property vocabulary_source#
Where the vocabulary was loaded from.
- property identity#
The vocabulary’s identity, independent of the file it came from.
dict(schema, sha256, n_ranks, n_vocab, endoftext_id), withsha256over the canonical rank file (rank<TAB>lowercase hexper line). The same table loaded from a rank file, fromencoder.json+vocab.bpeor from a trained vocabulary has the same identity. Models trained throughmojolearn.lm_corpusrecord it, so their ids are never decoded with another table.
- encode_bytes(data, allow_endoftext=False)[source]#
Encode a byte string.
- Parameters:
data (bytes-like) – The input;
strraises TypeError (use encode()).allow_endoftext (bool, default False) – Recognize
<|endoftext|>as the special token.
- Returns:
list of int
- encode(text, allow_endoftext=False)[source]#
Encode text.
- Parameters:
text (str or bytes-like) –
stris encoded as UTF-8 first.allow_endoftext (bool, default False) – Recognize
<|endoftext|>as the special token.
- Returns:
list of int
- encode_batch(documents, allow_endoftext=False)[source]#
Encode several documents in one call.
Each document is encoded alone:
encode_batch(docs)[k] == encode(docs[k]), whatever else is in the batch. One call is faster than many for short documents.- Parameters:
documents (sequence of str or bytes-like)
allow_endoftext (bool, default False) – Recognize
<|endoftext|>as the special token.
- Returns:
list of list of int
- decode_bytes(ids)[source]#
Decode ids to bytes.
- Parameters:
ids (sequence of int) – Each in
[0, n_vocab); an invalid id raises an error naming its value and position before anything is decoded.- Returns:
bytes
- decode(ids, errors='replace')[source]#
Decode ids to text.
- Parameters:
ids (sequence of int)
errors (str, default “replace”) – Passed to
bytes.decode.
- Returns:
str
- class mojolearn.tokenizer.BpeVocabularyTrainer(vocab_size=32000, min_frequency=2, backend='auto')[source]#
Bases:
objectTrain a byte-level BPE vocabulary deterministically.
The same corpus and settings give the same vocabulary bytes on any machine: ties are broken by a total order (highest count, then smallest
(left_id, right_id)), counting is exact and single-threaded, and no floating point is involved. Training runs on the CPU in compiled code.Each document is pre-tokenized alone, so no pre-token spans two documents and the order of the documents does not affect the result.
- Parameters:
vocab_size (int, default 32000) – Number of ranks to train, including the 256 single bytes; at least 256.
min_frequency (int, default 2) – Minimum pair count for a merge; at least 1.
backend ({“auto”, “mojo”}, default “auto”) – Both name the compiled trainer.
Examples
>>> from mojolearn.tokenizer import BpeVocabularyTrainer >>> docs = ["the cat sat on the mat", "the dog sat on the log"] >>> vocab = BpeVocabularyTrainer(vocab_size=300).train(docs) >>> tok = vocab.tokenizer() >>> tok.decode(tok.encode("the cat")) 'the cat'
- class mojolearn.tokenizer.TrainedBpeVocabulary(tokens, merges, stats)[source]#
Bases:
objectA vocabulary trained by
BpeVocabularyTrainer.train().- Variables:
- property n_tokens#
Number of ranks;
<|endoftext|>takes the id after the last one.
- property n_ties_broken#
How many merge selections had two or more pairs at the top count.
- property tie_break#
The rule that settles equal counts, as text.
- render_tokenizer_json()[source]#
The vocabulary as Hugging Face
tokenizer.jsontext, loadable by thetokenizerslibrary.
- property identity#
The
BpeTokenizer.identityof this vocabulary, without loading it.
- tokenizer()[source]#
A
BpeTokenizerover this vocabulary, without writing a file.