Pre-training teaches the GPT model to predict the next token in a sequence using cross-entropy loss over a tokenized corpus. The optimizer is AdamW with gradient clipping, and the model is checkpointed periodically throughout the run. All hyperparameters — model architecture, optimizer settings, batch size, and iteration count — are configurable through CLI flags passed toDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/gpt/llms.txt
Use this file to discover all available pages before exploring further.
cmd/pretrain.
CLI flags
| Flag | Default | Description |
|---|---|---|
-vocab-size | 1000 | Vocabulary size (must match tokenizer) |
-context-len | 256 | Maximum context length (sequence length) |
-embed-dim | 192 | Token embedding dimension |
-num-of-heads | 6 | Number of attention heads per block |
-num-of-blocks | 6 | Number of transformer blocks |
-theta | 10000 | Theta constant for RoPE positional encoding |
-max-learning-rate | 3e-4 | Peak learning rate for AdamW |
-beta1 | 0.9 | AdamW first moment decay |
-beta2 | 0.999 | AdamW second moment decay |
-weight-decay | 0.01 | L2 weight decay for AdamW |
-clip | 1.0 | Gradient clipping norm threshold |
-max-iters | 20000 | Total number of training iterations |
-batch-size | 32 | Number of sequences per batch |
-tokens-path | testdata/tiny_codes.bin | Path to the tokenized corpus .bin file |
-model-path | testdata/model_gpt.gob | Path to save the trained model |
-min-loss | 1.0 | Loss threshold — saves model.min when beaten |
-pprof | false | Enable CPU profiling to cpu.prof |
Running pre-training
Training loop
The training loop insidecmd/pretrain/main.go follows the standard forward → loss → backward → update pattern:
TokenDataset yields sliding-window pairs: for position i, the input window is tokens[i:i+contextLen] and the target window is tokens[i+1:i+contextLen+1], implementing next-token prediction across every position in the context simultaneously.
Checkpointing
The model is saved to disk at two points during training:- Every 100 iterations — writes
model_gpt.gobregardless of loss. - When loss beats
min-loss— writesmodel_gpt.gob.min, capturing the best checkpoint seen so far.
Loss logging
Every iteration appends a row toloss.csv with the iteration index and the current loss value:
plot loss.csv (installed via make install) to render a loss curve image after training completes.
Default model size
With the default flags, the model has approximately 300,000 trainable parameters:Data loading
TheDataLoader wraps a TokenDataset and provides shuffled, batched iteration over the token sequence:
TokenDataset.Len() returns len(Tokens) - ContextLen, so every valid sliding-window position is accessible as an independent training example.
Pass
-pprof to write a CPU profile to cpu.prof during training. Visualise it with make pprof, which opens go tool pprof -http=:8080 cpu.prof in your browser. This is useful for identifying bottlenecks in the forward or backward pass.