Supervised fine-tuning (SFT) adapts a pre-trained GPT model to follow instructions by continuing training on (instruction, response) pairs formatted in the Alpaca style. The prompt portion of each sequence is masked from the loss with label indexDocumentation 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.
-100, so only the response tokens contribute to gradient updates. This focuses learning on generating correct completions rather than re-learning the prompt structure.
Alpaca format
Each training example is assembled from anAlpaca struct containing instruction and response fields:
AlpacaFormat() function builds the prompt string:
Label masking
TheNewAlpacaDataset() function encodes each example and fills the prompt positions with -100:
cmd/sft/main.go uses the default ignore index of -100:
contextLen are right-padded with 0 (input) and -100 (labels). Sequences longer than contextLen are truncated.
CLI flags
| Flag | Default | Description |
|---|---|---|
-context-len | 256 | Maximum context length |
-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 | 500 | Total fine-tuning iterations |
-batch-size | 32 | Number of sequences per batch |
-merge-rules-path | testdata/merge_rules.gob | Path to BPE merge rules for the tokenizer |
-model-path | testdata/model_gpt.gob | Path to the pre-trained base model |
-alpaca-path | testdata/tiny_codes_sft.json | Path to Alpaca-format instruction JSON |
-sft-model-path | testdata/model_gpt_sft.gob | Output path for the fine-tuned model |
-min-loss | 1.0 | Saves model_gpt_sft.gob.min when beaten |
-pprof | false | Enable CPU profiling to cpu_sft.prof |
Running SFT
loss_sft.csv every iteration for plotting.
SFT vs pre-training
| Pre-Training | Fine-Tuning | |
|---|---|---|
| Iterations | 20,000 | 500 |
| Dataset | tiny_codes.bin (token IDs) | tiny_codes_sft.json (instruction pairs) |
| Loss target | All tokens | Response tokens only |
| Model weights | Randomly initialised | Loaded from model_gpt.gob |
| Output | model_gpt.gob | model_gpt_sft.gob |
Example output
After SFT, the model responds to instructions in the structured format:The pre-trained fine-tuned model (
model_gpt_sft.gob) is available for download. Run make testdata to fetch all four pre-built artifacts — merge rules, token binary, base model, and SFT model — without running any training yourself.