Ferrotensor

Train a language model from scratch in Rust

Ferrotensor trains a small Llama-style language model from random weights on a text file, on your own GPU. It is a Rust framework with its own WGSL kernels, which run through Vulkan, Metal and DX12, on NVIDIA GPUs and Apple silicon. pretrain is meant for learning and experiments.

Train from a text file

Install the command with cargo install ferrotensor-cli (Rust 1.88 or newer). Then train a model on corpus.txt and sample from it:

ferrotensor pretrain --data corpus.txt --out outputs/tiny --json
ferrotensor generate --model outputs/tiny --raw --prompt "Once upon a time"

In order, pretrain writes tokenizer.json, writes config.json and the initial model in init/, writes the corpus as token ids in tokens.bin, trains every weight and the embedding in f32, and writes model.safetensors. --layers, --hidden, --heads, --mlp-hidden and --context set the size, and --seed makes the initial weights repeatable. pretrain --help lists every flag and its default.

Byte-level or BPE tokenizer

The default is a byte-level tokenizer, so any UTF-8 text encodes to its bytes and decodes back exactly. To learn a byte-level BPE tokenizer from the same file, add --tokenizer bpe and a size:

ferrotensor pretrain --data corpus.txt --out outputs/tiny-bpe --tokenizer bpe --vocab-size 4096 --json

The corpus must be UTF-8. The same corpus and size always give the same tokenizer. A --vocab-size that is too small, or larger than the corpus has pairs for, exits with code 2. With --json, the result names the tokenizer ("byte" or "bpe") and the vocab_size.

Data formats

Pretraining data is .txt lines, a .json array of strings or token ids, or packed little-endian u32 tokens in .bin. The examples here use a text file, which is what the BPE tokenizer learns from.

Resume a run

--resume continues a run in the same --out from its saved weights and training state. Pass the flags of the original run, with --max-steps counting the steps before the resume. The saved tokenizer is reused, never learned again:

ferrotensor pretrain --data corpus.txt --out outputs/tiny --resume --json

Without --resume, an --out that already holds model.safetensors or init/ is refused. A run that stops early leaves no model.safetensors, and generate then names pretrain --resume.

What it is for

The model is trained in f32 with untied embeddings and conservative defaults that are not tuned. There is no claim about speed or about quality beyond the CI test and the measured runs. The byte-level run with the defaults on one NVIDIA A10G is in the benchmarks. --tokenizer bpe was not run on that machine. GPU memory is the f32 weights, their gradients and two AdamW moments, plus activations, and plan does not cover pretrain.

Fine-tune a pretrained model instead

To adapt an existing model such as SmolLM2 to your data, LoRA fine-tuning trains a small adapter and needs much less memory. Fine-tune a language model in Rust is the short version, and the guide walks through a full run with output from a real one.

How to train a language model from scratch in Rust?

Install the command with cargo install ferrotensor-cli, then run ferrotensor pretrain --data corpus.txt --out outputs/tiny. It trains a small Llama-style model from random weights on the text file, and ferrotensor generate --model outputs/tiny --raw --prompt "Once upon a time" samples from the result.

Which tokenizer does it use?

A byte-level tokenizer by default. Add --tokenizer bpe --vocab-size 4096 to learn a byte-level BPE tokenizer from your corpus before training. The corpus must be UTF-8 for BPE.

Can a run be stopped and continued?

Yes. Run the same command again with --resume and the same --out. It continues from the saved weights and training state and reuses the saved tokenizer.

Is it meant for large models?

No. pretrain trains small models for learning and experiments, in f32, with defaults that are not tuned. For a pretrained model, use LoRA fine-tuning instead.

More