# Quickstart: Supervised Fine-Tuning (SFT)

Source: https://docs.surogate.ai/engine/getting-started/quickstart-sft/

In a few minutes you will fine-tune a small model with LoRA from a ready-made YAML config. Supervised fine-tuning (SFT) teaches a pretrained model from example conversations or instructions. Once the first run works, the hyperparameter advice below helps you tune your own.

## 1) Pick an example config

You'll find example configs in `examples/sft/`.

For MiniCPM5 and Spark-X2.5 checkpoints, see the [fine-tuning guide](/engine/guides/minicpm5-spark/) for examples and LoRA target names.

A good first choice:
- `examples/sft/qwen3/qwen3-lora-bf16.yaml`

## 2) Run

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

The [`sft` command](/engine/reference/cli/#sft) takes the config path and a few optional flags. If you use `uv` and want to be sure you're running inside the project environment:

```bash
uv run surogate sft examples/sft/qwen3/qwen3-lora-bf16.yaml
```

## 3) Outputs

Checkpoints, logs and other artifacts are written under the config's `output_dir`. Every key used in this page is described in the [Configuration Reference](/engine/reference/config/).

## 4) Recommended Hyperparameters

### Dense Models (Standard Fine-Tuning)

**Learning Rate:**
- **LoRA**: `1e-4` to `5e-4` (typically `2e-4`)
- **Full fine-tuning**: `5e-6` to `2e-5` (typically `1e-5`)
- **Warmup ratio**: `0.03` to `0.1` (3-10% of total steps)

**Batch Size:**
- **Global batch size**: 256K-1M tokens recommended
  - LoRA: 256K-512K tokens (less memory intensive)
  - Full fine-tuning: 512K-1M tokens
- Calculate: `per_device_train_batch_size × gradient_accumulation_steps × num_gpus × sequence_len`

**LoRA Configuration:**
- **Rank**: `8`, `16`, or `32`
  - `r=8`: Fast, memory-efficient, good for simple adaptations
  - `r=16`: Balanced (recommended default)
  - `r=32`: Higher capacity for complex tasks
- **Alpha**: Match rank (e.g., `lora_alpha=16` for `lora_rank=16`)
- **Target modules**: All linear layers by default (Q, K, V, O, gate, up, down)

**Regularization:**
- **Weight decay**: `0.01` to `0.1` (typically `0.01` for SFT)
- **Gradient clipping**: `1.0`

**Precision Options:**
- **BF16 LoRA** (default): Best accuracy
- **FP8 QLoRA**: 2x faster, minimal accuracy loss (Hopper+)
- **FP4 QLoRA**: 4x faster, slight accuracy tradeoff (Blackwell+)
- **BnB QLoRA**: Memory-efficient, works on all GPUs

**Example Dense Model Config:**
```yaml
model: Qwen/Qwen3-0.6B
output_dir: ./output

# Batch configuration (512K tokens global batch)
per_device_train_batch_size: 2
gradient_accumulation_steps: 8
sequence_len: 2048
gpus: 4

# Learning rate
learning_rate: 2e-4
lr_scheduler_type: cosine
warmup_ratio: 0.05

# LoRA
lora: true
lora_rank: 16
lora_alpha: 16

# Regularization
weight_decay: 0.01
max_grad_norm: 1.0

# Precision
recipe: bf16

# Parallelism
zero_level: 2
```

## 5) Fine-Tuning Mixture-of-Experts (MoE) Models

MoE models need different hyperparameters from dense models, especially upcycled MoE models (converted from dense checkpoints). The [MoE guide](/engine/guides/moe/) covers MoE training in depth.

### Recommended Settings for MoE

**Key Differences from Dense Models:**

| Parameter | Dense Models | MoE Models | Reason |
|-----------|-------------|------------|--------|
| Learning rate | `2e-4` | `1e-5` | MoE routers are sensitive to aggressive LR |
| Warmup ratio | `0.05` | `0.15` | Longer warmup for router stability |
| Gradient clipping | `1.0` | `0.5` | Prevents routing instability |
| Weight decay | `0.01` | `0.01` | Regularizes router weights |

**Learning Rate:**
- **Standard MoE**: `1e-5` to `5e-5` (start with `1e-5`)
- **Upcycled MoE**: `1e-5` (conservative, increase only if training is too slow)
- **Warmup ratio**: `0.15` (15% of total steps for router stability)

**Batch Size:**
- **Global batch size**: 256K-512K tokens (smaller than dense models)
- MoE models are more sample-efficient; smaller batches work well

**LoRA Configuration:**
- **Rank**: `16` or `32` (same as dense models)
- **Alpha**: Match rank
- **Target modules**: Apply to expert layers for maximum adaptation

**Regularization:**
- **Weight decay**: `0.01`
- **Gradient clipping**: `0.5` (tighter than dense models)
- **Auxiliary loss coefficient**: Use default router aux loss settings

**Monitoring MoE Training:**
- **Gradient norms**: Should stay below `0.4` during training
  - If norms exceed `0.5`: reduce learning rate or increase `router_aux_loss_coef` in the model's `config.json` file
  - If norms spike above `0.8`: training is likely diverging
- **Router collapse symptoms**:
  - Sudden loss increases after initial decrease
  - Gradient norm spikes
  - Eval gap turning positive and growing

**Example MoE Config:**
```yaml
model: path/to/moe_model
model_type: qwen3_moe
output_dir: ./output

# Batch configuration (256K tokens global batch)
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
sequence_len: 2048
gpus: 4

# Learning rate (conservative for MoE)
learning_rate: 1e-5
lr_scheduler_type: cosine
warmup_ratio: 0.15

# LoRA
lora: true
lora_rank: 16
lora_alpha: 16

# Regularization (tighter for MoE)
weight_decay: 0.01
max_grad_norm: 0.5

# Precision
recipe: bf16

# Parallelism
zero_level: 2
```

### Upcycled MoE Models

If you upcycled a dense model to MoE with `scripts/upcycle_moe.py`, take extra care in the first phase of training.

**Critical Guidelines:**

1. **Use conservative learning rates**
   - Start with `1e-5` and increase only if training is too slow
   - Router weights are freshly initialized and require careful tuning

2. **Monitor gradient norms closely**
   - Target: Keep gradient norms below `0.4` during training
   - Warning signs:
     - Norms exceed `0.5`: Reduce learning rate or increase `router_aux_loss_coef`
     - Norms spike above `0.8`: Training is likely diverging, restart with lower LR

3. **Watch for router collapse**
   - Symptoms include:
     - Sudden loss increases after initial decrease
     - Gradient norm spikes
     - Eval gap turning positive and growing
   - Solution: Reduce learning rate and increase warmup duration

**Training Data Requirements:**

Based on the [upcycling paper](https://arxiv.org/abs/2410.07524):
- **Budget**: ~150k supervised samples (sufficient for router training)
- **Duration**: 1 epoch recommended
- **Hardware**: Single GPU with 24GB+ VRAM is sufficient
- **Focus**: Training primarily adapts the router; expert weights inherit from dense model

**Upcycled MoE Example Config:**
```yaml
model: path/to/upcycled_moe_model
model_type: qwen3_moe
output_dir: ./output

# Very conservative settings for upcycled models
learning_rate: 1e-5
warmup_ratio: 0.2           # Extra-long warmup for router
max_grad_norm: 0.3          # Very tight clipping

lora: true
lora_rank: 16
lora_alpha: 16

# Small batch, single GPU is fine
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
sequence_len: 2048
```


## 6) Serve what you trained

A LoRA run produces an adapter, but the engine serves whole models, so merge the adapter into the base first. To serve the
merged checkpoint quantized, produce a GGUF from it and point the server at that file ([`merge`](/engine/reference/cli/#merge) and [`quantize`](/engine/reference/cli/#quantize) list their options):

```bash
surogate merge --base-model Qwen/Qwen3.5-0.8B --checkpoint-dir output/step_00000050 --output merged
surogate quantize --model merged --output merged-Q4_K_M.gguf --type q4_k_m
surogate serve merged-Q4_K_M.gguf --port 8080
```

The quantization step uses llama.cpp, and it writes an ordinary GGUF file. To serve the merged
checkpoint at full precision, skip it.

## Notes

- For private Hugging Face models or datasets, pass `--hub_token`.
- To distill a larger teacher model into the student during SFT, add a `distillation:` block and run `surogate distill-capture` before `surogate sft`. See the [Knowledge Distillation guide](/engine/guides/distillation/).

## See also

- [Training modes: Pretraining vs Full Fine-Tuning vs LoRA](/engine/getting-started/training-modes/): when to choose LoRA, QLoRA or full fine-tuning
- [Serving models](/engine/inference/serving-models/): serve and query the model you trained
- [Quickstart: Pretraining](/engine/getting-started/quickstart-pretraining/)
- [Configuration](/engine/guides/configuration/): how a config file is put together
- [Back to docs index](/engine/)
