# Optimizers

Source: https://docs.surogate.ai/engine/guides/optimizers/

Surogate gives you two optimizers: AdamW 8-bit and NorMuon. Both store their state in 8-bit quantized form, which cuts memory use without hurting training stability.

## Selecting an Optimizer

Pick one with the `optimizer` option (all optimizer keys are in the [config reference](/engine/reference/config/#optimizer-settings)):

```yaml
optimizer: adamw_8bit  # Default
# or
optimizer: normuon
```

---

## AdamW 8-bit

AdamW 8-bit is the default. It is standard AdamW with the optimizer states stored in 8-bit blockwise quantization, following the bitsandbytes approach.

### How It Works

AdamW keeps two state tensors per parameter:
- First moment (m): an exponential moving average of the gradients
- Second moment (v): an exponential moving average of the squared gradients

Both are quantized to 8-bit with dynamic block quantization and per-block absmax scaling. That makes optimizer state about 4x smaller than in full-precision AdamW, with the same training quality.

Each block of 32 elements keeps two FP32 scales: the largest |m| and the largest √v. Until 2026-11 these scales were FP16. FP16 rounds to 0 below 2.98e-8, and LoRA gradients on long sequences sit below that per coordinate, so a block lost its moments at every step; its updates were too large and no longer followed the gradient. A checkpoint with FP16 scales still loads (the scales widen exactly), but moments it already lost stay lost, and the trainer reports how many blocks are affected. To restart Adam cleanly, load the adapter alone.

The moments are still 8-bit relative to their block's largest element, so √v resolves down to about 1/510 of that element. With up to 100x spread between neighbours in one block, updates stay within cos 0.98 and 4 % in norm of full-precision Adam. On real LoRA gradients (7 groups of long rows), every update over 100 steps stayed within cos 0.9997 and 0.4 % in norm (`integration-tests "[optim][flash]"`).

The update rule is:
```
m = β₁ · m + (1 - β₁) · g
v = β₂ · v + (1 - β₂) · g²
p = p - lr · m / (√v + ε) - lr · wd · p
```

### Configuration Options

| Option | Default | Description |
|--------|---------|-------------|
| `learning_rate` | `2e-4` | Initial learning rate |
| `adamw_beta1` | `0.9` | Exponential decay rate for first moment |
| `adamw_beta2` | `0.999` | Exponential decay rate for second moment |
| `adamw_epsilon` | `1e-8` | Small constant for numerical stability |
| `weight_decay` | `0.1` | Weight decay coefficient (decoupled from gradient) |

### Example Configuration

```yaml
optimizer: adamw_8bit
learning_rate: 2e-4
adamw_beta1: 0.9
adamw_beta2: 0.999
adamw_epsilon: 1e-8
weight_decay: 0.1
```

### When to Use AdamW

- General fine-tuning: it works well for most tasks.
- LoRA training: it is the recommended choice for adapters.
- Stability: it copes better with small learning rates and noisy gradients.
- Familiarity: its behavior is well understood and well researched.

---

## NorMuon

NorMuon (Normalized Momentum with Orthogonalization) applies orthogonalized momentum updates to 2D weight matrices. It is inspired by the Muon optimizer and uses the Polar Express algorithm to orthogonalize matrices efficiently.

### How It Works

NorMuon treats parameters in two groups:
- 2D weight matrices (attention projections, MLP weights) get orthogonalized momentum updates.
- 0D/1D parameters (embeddings, layer norms, biases, lm_head) get standard AdamW 8-bit updates.

For each 2D weight, every update runs these steps:
1. Momentum update: smooth the gradients with an exponential moving average.
2. Polar Express orthogonalization: map the momentum matrix to its nearest orthogonal matrix with Newton-Schulz iterations.
3. Variance reduction: apply Adafactor-style adaptive scaling from row and column statistics.
4. Cautious weight decay: apply weight decay only where the update direction matches the parameter sign.

Orthogonalization keeps the weight matrices well conditioned during training, which can help transformer models converge.

### Configuration Options

| Option | Default | Description |
|--------|---------|-------------|
| `learning_rate` | `2e-4` | Learning rate for AdamW parameters (embeddings, norms, lm_head) |
| `normuon_momentum` | `0.95` | Momentum coefficient (β₁) for NorMuon 2D updates |
| `normuon_beta2` | `0.95` | EMA coefficient for variance estimation |
| `normuon_cautious_wd` | `true` | Enable cautious (sign-aware) weight decay |
| `weight_decay` | `0.1` | Weight decay coefficient |

NorMuon also scales the learning rate by weight shape, `√(max(1, rows/cols))`, which raises the effective learning rate for weights with more inputs than outputs.

### Example Configuration

```yaml
optimizer: normuon
learning_rate: 2e-4
normuon_momentum: 0.95
normuon_beta2: 0.95
normuon_cautious_wd: true
weight_decay: 0.1
```

### When to Use NorMuon

- Full fine-tuning: it can converge better when you train the whole model.
- Pre-training: it was designed for training transformers from scratch.
- Large models: the benefit of orthogonalization grows with model size.
- Multi-GPU training: it works with data-parallel training.

### Cautious Weight Decay

With `normuon_cautious_wd` on (the default), weight decay applies only where the update direction agrees with the parameter sign:

```
mask = sign(update) == sign(param)
p = p - lr · wd · p · mask - lr · update
```

Weight decay then never pulls against the gradient direction, which can make training more stable.

---

## Common Options

These options apply to both optimizers:

| Option | Default | Description |
|--------|---------|-------------|
| `max_grad_norm` | `0.0` | Maximum gradient norm for clipping (0 = disabled) |
| `gradient_accumulation_steps` | `4` | Number of micro-steps before optimizer update |
| `warmup_steps` | `0` | Linear warmup steps from 0 to learning_rate |
| `warmup_ratio` | `0.0` | Warmup as fraction of total steps |
| `lr_scheduler_type` | `linear` | Learning rate schedule: `linear`, `cosine`, or `wsd` |
| `cooldown_steps` | `0` | Linear cooldown steps at end of training |
| `final_lr_fraction` | `0.0` | Final LR as fraction of initial (for cooldown) |

---

## Memory Considerations

Both optimizers keep 8-bit states, which take roughly:
- AdamW 8-bit: 2.25 bytes per parameter (1 byte for m, 1 byte for v, two FP32 scales per 32 parameters)
- NorMuon: ~2 bytes per parameter for 2D weights, ~2 bytes per parameter for AdamW-managed parameters

NorMuon needs some extra memory on top:
- Variance buffers: O(max(rows, cols)) per 2D weight tensor
- Polar Express workspace: temporary buffers for the matrix operations

If memory is tight:
- Set `offload_optimizer: true` to keep optimizer state in pinned host memory (see [Offloading](/engine/guides/offloading/#offloading-optimizer-states)).
- Increase `gradient_accumulation_steps` to reduce activation memory.

---

## Performance Comparison

| Aspect | AdamW 8-bit | NorMuon |
|--------|-------------|---------|
| Memory | ~2 bytes/param | ~2 bytes/param + workspace |
| Speed | Faster | ~15% slower due to orthogonalization |
| Stability | Very stable | Stable with proper hyperparameters |
| Best for | LoRA, general fine-tuning | Full fine-tuning, pre-training |

---

## Multi-GPU Training

Both optimizers work with data-parallel [multi-GPU training](/engine/guides/multi-gpu/). Gradients are synchronized across GPUs before the optimizer step, and with ZeRO each GPU holds its own shard of the optimizer state.

To train on several GPUs:
```yaml
gpus: 4  # Number of GPUs to use
zero_level: 1  # ZeRO optimization level (1, 2, or 3)
```

---

## See also

- [Config reference](/engine/reference/config/): every optimizer key and its default
- [Memory](/engine/guides/memory/): what to try when optimizer state doesn't fit
- [Back to docs index](/engine/)
