mojolearn.lm_corpus#
Tokenize a training corpus once and train a language model on the ids.
The tokenizer is part of the model: it is chosen before the first training step, and the model’s ids mean something only through it. This module prepares a corpus with a BPE vocabulary, caches the token ids, serves training batches, and checks that a tokenizer is the one a model was trained on.
With no
vocab, prepare() trains a byte-level BPE vocabulary on the corpus’s training range (never validation or test data), tokenizes the whole corpus once and caches the ids.With
vocab=, your own token map is used and nothing is trained: a rank file, an(encoder.json, vocab.bpe)pair, aBpeTokenizeror aTrainedBpeVocabulary.
Cache#
Files are written under cache_dir (default $MOJOLEARN_LM_CACHE, else
~/.cache/mojolearn/lm):
vocab/<corpus sha16>-v<vocab_size>-f<min_frequency>-s<sample bytes>/
ranks.tsv, vocabulary.json (a trained vocabulary)
vocab/user-<vocabulary sha16>/ranks.tsv, vocabulary.json (yours)
tokens/<corpus sha16>-<vocabulary sha16>-d<document bytes>/
tokens.i32, manifest.json
A rerun with the same corpus and vocabulary reuses the cache after re-hashing it against its manifest; a mismatch raises ValueError rather than silently rebuilding. Nothing is written into the package.
Vocabulary identity#
TokenBatches.data_schedule() records the vocabulary’s identity (the
SHA-256 of its canonical rank file and its n_vocab). The trainer refuses
a schedule whose n_vocab differs from its vocab_size and keeps the
schedule in every checkpoint, and tokenizer_for() only returns a
tokenizer that matches it, so ids are never decoded with the wrong table.
Documents#
BPE never merges across a pre-token boundary, and where a long file is cut
changes the ids at every cut, so the cut is fixed and recorded: a document
never spans the corpus manifest’s train / validation / test boundaries, is
at most document_bytes long, and ends at the last newline at or before
that limit (or exactly at the limit when there is none). Each document is
encoded alone and the ids are concatenated. <|endoftext|> is neither
inserted nor recognized in the text.
Examples
>>> from mojolearn import lm_corpus, LanguageModelConfig, LanguageModelTrainer
>>> corpus = lm_corpus.prepare("corpus/input.txt") # train a vocabulary
>>> corpus = lm_corpus.prepare("corpus/input.txt", vocab="ranks.tsv") # use yours
>>> batches = corpus.batches(batch=1, length=2048)
>>> schedule = batches.data_schedule()
>>> ids = batches.ids(0)
>>> tok = lm_corpus.tokenizer_for(schedule, corpus.vocabulary_path)
Functions
|
Tokenize a corpus file once and return a |
|
Check that |
|
A tokenizer for |
Classes
|
A prepared corpus: cached token ids and the vocabulary they use. |
|
Training batches from a tokenized corpus. |
- mojolearn.lm_corpus.prepare(corpus, *, vocab=None, cache_dir=None, vocab_size=50256, min_frequency=2, vocab_sample_bytes=10000000, document_bytes=1048576, batch_documents=16, progress=None)[source]#
Tokenize a corpus file once and return a
TokenizedCorpus.If a
manifest.jsonwith schemamojolearn.byte-lm.corpus.v1sits beside the corpus, the file is checked against its SHA-256 and length and its train / validation / test ranges are used; otherwise the whole file is the training range. Results are cached (see the module documentation).- Parameters:
corpus (str or path-like) – Path to the corpus text file.
vocab (str, path-like, tuple, BpeTokenizer, TrainedBpeVocabulary or None, default None) – None trains a vocabulary of
vocab_sizeranks (n_vocab = vocab_size + 1with<|endoftext|>) on the firstvocab_sample_bytesof the training range. Otherwise your token map: a rank file, an(encoder.json, vocab.bpe)pair, aBpeTokenizeror aTrainedBpeVocabulary; nothing is trained."bytes"raises ValueError (byte-level training needs no vocabulary).cache_dir (str, path-like or None, default None) – Cache location; None uses default_cache_dir().
vocab_size (int, default 50256) – Ranks to train (GPT-2’s vocabulary size, without
<|endoftext|>).min_frequency (int, default 2) – Minimum pair frequency for a merge.
vocab_sample_bytes (int, default 10_000_000) – Bytes of the training range used to train the vocabulary.
document_bytes (int, default 1048576) – Maximum document length; see the module documentation.
batch_documents (int, default 16) – Documents tokenized per batch.
progress (callable or None, default None) – Optional progress callback.
- Returns:
TokenizedCorpus
- Raises:
ValueError – For a non-positive size argument, a corpus that does not match its manifest, or a cache that does not match its manifest.
- class mojolearn.lm_corpus.TokenizedCorpus(tokens_dir, vocabulary_path)[source]#
Bases:
objectA prepared corpus: cached token ids and the vocabulary they use.
Returned by prepare().
- Variables:
tokens_dir (pathlib.Path) – Directory holding
tokens.i32(the int32 ids) andmanifest.json.vocabulary_path (pathlib.Path) – The vocabulary’s canonical rank file.
manifest (dict) – The parsed token manifest.
- property vocabulary#
The vocabulary identity record (schema,
sha256,n_vocab).
- property n_vocab#
Number of token ids in the vocabulary,
<|endoftext|>included.
- tokenizer()[source]#
A
BpeTokenizerfor this corpus’s vocabulary.
- batches(batch, length)[source]#
Training batches over this corpus; see
TokenBatches.
- class mojolearn.lm_corpus.TokenBatches(tokens_dir, batch, length)[source]#
Bases:
objectTraining batches from a tokenized corpus.
Step
k(zero-based), rowbreads thelength + 1ids starting atlo + (k * batch * length + b * length) % (hi - lo - length - 1), where[lo, hi)is the training range in tokens, so a run of any length reads no validation or test id. Targets are the inputs shifted by one id. The token file is verified against its manifest when opened.- Parameters:
tokens_dir (str or path-like) – A
TokenizedCorpustokens_dir.batch (int) – Rows per batch.
length (int) – Tokens per row (each row holds
length + 1ids).
- Raises:
ValueError – If the token file does not match its manifest or the training range is shorter than one row.
- property vocabulary#
The vocabulary identity record (schema,
sha256,n_vocab).
- ids(step_index)[source]#
The batch for training step
step_index.- Returns:
Array of shape (batch, length + 1), int32
- prefetch(start_step, steps, *, depth=2)[source]#
Iterate over
(step, ids)forstepssteps fromstart_step.Each batch is made when the consumer asks for it, and an error is raised at its own step, before any later batch is made.
- Parameters:
start_step (int) – First step, at least 0.
steps (int) – Number of steps, at least 0.
depth (int, default 2) – Maximum number of batches alive at once; at least 1.
- Returns:
iterator of (int, Array)
- mojolearn.lm_corpus.require_vocabulary(model, tokenizer)[source]#
Check that
tokenizeris the vocabularymodelwas trained on.- Parameters:
model (trainer, dict) – A trainer, a checkpoint state, a data schedule or a tokens manifest.
tokenizer (BpeTokenizer) – The tokenizer to check.
- Returns:
BpeTokenizer –
tokenizer, unchanged.- Raises:
ValueError – If
modelcarries no vocabulary identity, or the tokenizer’s SHA-256 orn_vocabdiffers from it.
- mojolearn.lm_corpus.tokenizer_for(model, vocabulary)[source]#
A tokenizer for
vocabulary, checked against the vocabularymodelwas trained on.- Parameters:
model (trainer, dict) – A trainer, a checkpoint state, a data schedule or a tokens manifest.
vocabulary (str, path-like, tuple, BpeTokenizer or TrainedBpeVocabulary) – Any form prepare() accepts as
vocab.
- Returns:
BpeTokenizer
- Raises:
ValueError – If the vocabulary does not match the model’s (see require_vocabulary()).