Skip to content

Your First Real Training

The quickstart used a public dataset. Here's how to train on your own data with production settings.


How much data do you need?

This depends on your goal. A rule of thumb (not a measured result; general chat ability tends to plateau after about a thousand samples, while math and code keep improving with more data, Dong et al. 2023):

Dataset size Epochs Expected result
500–2,000 samples 3–5 Format learning. Model follows your template but capabilities are limited.
2,000–10,000 samples 2–3 Skill acquisition. Model learns specific behaviors (tool-calling patterns, coding style).
10,000–50,000 samples 1–2 Deep specialization. Model becomes genuinely competent at the domain.
50,000+ samples 1 Diminishing returns unless data is highly diverse.

Repetition can beat new data at a fixed number of updates: Kopiczko et al. (2026, arXiv:2602.11149) trained base Qwen3/Olmo3 models on long chain-of-thought data and found 400 random samples for 128 epochs beat 51,200 samples for one epoch on AIME/GPQA, with gains saturating at 32–64 epochs. It is a result about repetition in long-CoT SFT of base models, not about quality versus quantity; for an instruct model, watch your regression evals when you repeat data.


Prepare your data

JSONL with chat messages:

{"messages": [{"role": "system", "content": "You are helpful."}, {"role": "user", "content": "What is 2+2?"}, {"role": "assistant", "content": "4"}]}

Multi-turn is supported. Only assistant tokens get loss.

(Optional) Clean the data and hold out an eval set

Point your config's data.dataset at the raw data, add a preprocess: section, and run:

pgs prepare --config configs/qwen35_4b/a100_80gb.yaml
preprocess:
  enabled: true          # training will auto-use the prepared output
  output_dir: ./prepared
  eval_holdout: 200      # held-out eval set, shares no prompt with the training data
  budget: 5000           # a random subset of 5,000 (0 = keep everything)

No GPU needed: the rows are rendered and tokenized as training will, the ones training would drop and exact duplicates are removed, and prepared/prepared_meta.json reports what was removed and why, how many rows are cut to max_seq_length, and the token counts. It does not know which samples will teach the model most: scoring rows with the model is available but off by default, because no selection by that score beat a random subset in our measurements. With enabled: true, training picks the output up automatically — no path editing needed. (The flag-based pgs prepare --model ... --data ... mode still exists; see the Data guide.)

Choose your config

GPU Command
RTX 4090 / A100-40GB ./run.sh configs/qwen35_4b/a100_40gb.yaml
A100-80GB ./run.sh configs/qwen35_4b/a100_80gb.yaml
H100-80GB ./run.sh configs/qwen35_4b/h100_80gb.yaml
8× A100/H100 ./scripts/train_multi_gpu.sh configs/qwen35_4b/a100_80gb_multigpu.yaml

Edit the config

data:
  dataset: my_data.jsonl        # raw data (prepared output is used when preprocess.enabled)
  eval_dataset: my_data.jsonl
  eval_split: test

Output

output/
├── best/       ← Lowest eval loss (USE THIS)
│   └── model/
├── final/      ← Last step
└── step-*/     ← Periodic (auto-purged)

Packing: when to use it

Packing (packing: true, off by default) places several whole conversations in one sequence of up to max_seq_length tokens. A conversation is never split across two sequences, and each one only attends to itself: position_ids restart at every conversation and the trainer passes the arguments that make every attention and linear-attention layer respect them.

Packing: three whole conversations in one sequence, each attending only to itself One packed sequence holds conversations A, B and C back to back, followed by padding. Position ids restart at 0 at the start of each conversation. Only the assistant tokens carry a training label. The attention mask is block-diagonal: each token attends causally to earlier tokens of its own conversation and never to another conversation. one packed sequence (max_seq_length) conversation position_ids role trained A 0 u A 1 u A 2 a A 3 a A 4 a B 0 u B 1 u B 2 a B 3 a C 0 u C 1 a C 2 a · · u: user, system or tool result · a: assistant (trained) padding (·) carries no label attention mask rows: queries · columns: keys causal, inside each conversation only
Packing three conversations into one sequence: position ids restart at every conversation, only assistant tokens are trained, and attention is block-diagonal, so no conversation sees another.

Without packing, batches are already cut to the length of their longest row, and length-grouped batching (length_group_buffer, on by default) puts rows of similar length together, so little compute goes to padding. On short chat data (Qwen3-0.6B, perfectblend) the two measured about the same (packing somewhat slower with sdpa, which needs an explicit block-diagonal mask), so packing is not a free speedup. It pays off when rows are few and of very different lengths, or with attn_implementation: flash_attention_2, whose variable-length kernel needs no mask.

Qwen3.5 and other linear-attention hybrids need the hybrid extra to pack (see Installation); the trainer refuses otherwise.


Load your model

from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("output/best/model")

Troubleshooting

Symptom Fix
Loss NaN Reduce learning_rate by 3×
Loss stuck Check the masking (pgs inspect) and the learning rate; read the pgs prepare report for dropped and cut rows
OOM Reduce per_device_batch_size
Slow Set model.compile: true