# Training features

Source: https://docs.surogate.ai/engine/examples/recipes/training/

<!-- From examples/training/README.md in the engine repo; configs and scripts it names are linked on GitHub. -->
Each config here shows one training feature on a small model, ready to run. They are
complete configs: from the repository root, run
`surogate sft examples/training/<file>.yaml`. All use Qwen3-0.6B and the same
instruction dataset as the basic [BF16 LoRA example](https://github.com/invergent-ai/surogate/blob/main/examples/sft/qwen3/qwen3-lora-bf16.yaml).
Swap in your own dataset and step budget. Each config writes to its own output
directory and starts a fresh run by default.

| Config | Feature | Resources |
|---|---|---|
| [full-finetune.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/training/full-finetune.yaml) | Update all parameters; FP32 master weights/gradients, AdamW, cosine schedule | One GPU; more memory than LoRA |
| [multi-gpu.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/training/multi-gpu.yaml) | Four-GPU data parallelism, ZeRO-3, FP8 persistent quants in host RAM, offloaded gradients/optimizer, communication overlap | Four FP8-capable GPUs and host RAM |
| [cpu-offload.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/training/cpu-offload.yaml) | Full fine-tuning with CPU weights/gradients/optimizer and saved activation offload | One GPU plus host RAM; GPU computes forward/backward |
| [long-context.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/training/long-context.yaml) | 8K context, tiled MLP, recomputation, activation offload, chunked LM head and attention backward | One GPU and host RAM |
| [adaptive.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/training/adaptive.yaml) | Automatic LR reduction, early stopping, epoch/token-budget adjustment, WSD schedule and local metrics | One GPU; epoch-derived step budget |
| [continue-adapter.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/training/continue-adapter.yaml) | Import an existing trainable PEFT adapter | Run the basic BF16 LoRA recipe first |
| [runtime-lora.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/training/runtime-lora.yaml) | Attention-only adapter for native runtime serving | One GPU; then use [serving examples](/engine/examples/recipes/serve/) |

For LoRA, online QLoRA and prequantized bases, use the
[model recipes](/engine/examples/recipes/#model-and-precision-recipes). BF16 runs on the widest
range of hardware; FP8 needs compatible hardware and NVFP4 compute needs Blackwell.
How the base is stored and what precision computes are separate settings: `qlora_bnb`,
`qlora_fp8`, and `qlora_fp4` pick online base quantization, and `recipe` picks compute.
Don't enable online QLoRA flags on a checkpoint that is already quantized.

## Multiple GPUs and large models

One Surogate process drives all the GPUs, so don't wrap the examples in `torchrun`
(see [multi-GPU training](/engine/guides/multi-gpu/)).

```bash
CUDA_VISIBLE_DEVICES=0,1,2,3 surogate sft examples/training/multi-gpu.yaml
surogate sft examples/sft/qwen3/qwen3-lora-bf16-ray.yaml
surogate sft examples/sft/qwen35/qwen36-text-lora-fp8-pp.yaml
```

The Ray config needs a running cluster with two nodes and four GPUs per node. Every
node needs the same checkout and environment, and must be able to reach the data and
model paths; see [multi-node setup](/engine/guides/multi-node/). Its
`ray_address: auto` connects to the existing cluster.

The dispatch pipeline config is a separate four-GPU LoRA workflow for a 27B model.
`offload_master` streams the frozen base from pinned host RAM. It can't be combined
with ZeRO sharding, expert parallelism, `cpu_training`, or residual offload. The
scheduler picks the stages for you; see [dispatch-PP](/engine/guides/dispatch-pp/).

For MoE expert parallelism and routing, use the Qwen3.5/3.6 MoE or Laguna recipes.
They show `ep_size`, load balancing, BF16 expert adapters, and router losses
(see the [MoE guide](/engine/guides/moe/)). `ep_size` must divide both the GPU
and the expert count. `train_router: true` also updates the router during LoRA
training; use the model's supported recipe and watch the routing metrics. The Qwen3
MoE BnB example shows expert offload and selective dequantization.

For chunked sequences, the [Laguna recipe](https://github.com/invergent-ai/surogate/blob/main/examples/sft/laguna/laguna-s-lora-fp8.yaml)
and [Qwen3.6 MoE recipe](https://github.com/invergent-ai/surogate/blob/main/examples/sft/qwen35moe/qwen36moe-lora-fp8.yaml) set
`sequence_chunks: 8`. The chunk count must divide `sequence_len`, the batch size must
be one, and LoRA dropout must be zero. Tiled MLP (`long_context`) is a separate
way to save memory on dense models; see [long context](/engine/guides/long-context/).

## Continue an adapter, stack an adapter, or resume a run

```bash
surogate sft examples/sft/qwen3/qwen3-lora-bf16.yaml
surogate sft examples/training/continue-adapter.yaml
```

The second command imports the first run's PEFT adapter and starts with a fresh
optimizer. The adapter's rank, alpha and targets must match. To stack a new adapter,
copy `continue-adapter.yaml`, set `adapter_init_mode: merge`, and give it a new
`output_dir`. That merges the parent into an unquantized base and trains a new
adapter on top; prequantized bases can't use this merge initialization.

To resume optimizer state and step count, set `resume_from_checkpoint: true`
in the original config and rerun it with the same output directory and training
budget. It picks up the latest `step_XXXXXXXX` checkpoint. `save_steps` sets how
often checkpoints are saved and `save_total_limit` how many are kept. A run that
already reached its step budget has nothing left to do; raise the budget to extend it.

SFT writes its final adapter straight into `output_dir` (next to
`adapter_config.json`); full fine-tuning writes the model there.
`merge_adapter: true` also writes merged model weights into that directory.
The [reverse-text example](https://github.com/invergent-ai/surogate/blob/main/examples/sft/reverse-text-qwen3.yaml) shows automatic
merging and split prompt/completion message columns. The
[serving guide](/engine/examples/recipes/serve/#runtime-adapters-and-merged-models) shows an
explicit merge into a separate directory, followed by GGUF quantization.

## Data, validation, and monitoring

```bash
surogate tokenize examples/datasets/mixed.yaml --debug
surogate sft examples/datasets/mixed.yaml
surogate sft examples/training/adaptive.yaml
```

The [dataset example](/engine/examples/recipes/datasets/) covers local instruction/chat data,
column mapping, dataset mixing and held-out validation. The standard model recipes use
packing and keep the default 10% validation split. Use `loss_scale: all` for
raw-text next-token training; see [pretraining](/engine/examples/recipes/pt/).

Adaptive training writes local metrics to `outputs/training/adaptive/metrics.jsonl`.
Its `max_steps: -1` lets the epoch and token-budget logic choose the step count.
To report to other services, install and configure the integration, then add these
fields to a copy of the config:

```yaml
report_to: [surogate, wandb, aim]
wandb_project: surogate-examples
wandb_name: qwen3-adaptive
aim_experiment: surogate-examples
aim_repo: ./outputs/aim
```

Log in to W&B first and install `aim` if needed. The built-in JSON training log
and `training_plot.png` work without either service. To watch local metrics in the
live dashboard:

```bash
SUROGATE_METRICS_PATH=./outputs/training/adaptive/metrics.jsonl surogate jackalope
```

For timing or memory diagnostics, add `debug_time_breakdown: true` or
`debug_memory_breakdown: true` to a copy (see [debugging](/engine/guides/debugging/)).
For a short smoke run, copy any base config and set `max_steps: 3`, `eval_steps: 0`,
`save_steps: 0`, and a fresh `output_dir`. To measure what recomputation costs on a
model that fits, compare `recompute: true` and `false`; QLoRA forces recomputation on.
Dedicated benchmark and one-step YAML duplicates are kept out of the public example
library.

Next: the [config reference](/engine/reference/config/) lists every option these
examples use, and [memory](/engine/guides/memory/) helps when a run doesn't fit.
