# Memory

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

Out of memory? This page lists the knobs that change GPU memory use, roughly in the order to try them: the cheap ones first, the ones that cost speed or change your setup later. Each knob is a config key; the [config reference](/engine/reference/config/) has the full list and defaults.

To see where the memory goes before you change anything, set `debug_memory_breakdown: true` (see [Debugging](/engine/guides/debugging/#training-memory-breakdown)).

## First-line levers

These change how much data each step holds on the GPU.

- Reduce `per_device_train_batch_size` and increase `gradient_accumulation_steps` by the same factor. The effective batch size (`per_device_train_batch_size × gradient_accumulation_steps × gpus`) stays the same, so training behaves the same; you only run more, smaller micro-steps. Gradient accumulation also amortizes the optimizer step over more micro-steps.
- Reduce `sequence_len` if your data allows it. Activation memory grows with sequence length, but shorter sequences truncate longer samples, so this does change what you train on.

```yaml
per_device_train_batch_size: 1   # was 2
gradient_accumulation_steps: 8   # was 4, same effective batch
sequence_len: 2048
```

## Framework knobs

Try these next, in this order.

### 1. Recomputation (`recompute`)

Recomputation recomputes activations in the backward pass instead of storing them. It is on by default (`recompute: true`) and costs a small amount of extra compute. If you turned it off for speed, turn it back on first.

```yaml
recompute: true
```

### 2. Chunking (`lmhead_chunks`, `attn_bwd_chunks`)

`lmhead_chunks: N` splits the LM-head computation into N chunks, which makes the logit tensor N times smaller. With a large vocabulary that tensor is often the biggest single buffer. `attn_bwd_chunks: N` splits the attention backward pass into N chunks to save workspace memory. Both default to `1`.

```yaml
lmhead_chunks: 4
attn_bwd_chunks: 2
```

### 3. Long sequences: [Tiled MLP for long context](/engine/guides/long-context/) (`long_context`) and `sequence_chunks`

At long context the MLP intermediates dominate. `long_context: true` runs the MLP in tiles, trading extra kernel launches and backward recomputation for memory. It requires `lora_dropout: 0`.

```yaml
long_context: true
lora_dropout: 0
```

For sequences far longer than activation memory allows, `sequence_chunks: N` runs the layer stack over N chunks of `sequence_len / N` tokens and carries attention KV between them, with exact gradients. It requires `per_device_train_batch_size: 1`, BF16 attention and `lora_dropout: 0`, and disables CUDA graphs. See [Chunked-Sequence Training](/engine/reference/config/#chunked-sequence-training-long-sequences).

```yaml
sequence_len: 32768
sequence_chunks: 4
per_device_train_batch_size: 1
lora_dropout: 0
```

### 4. Quantization / QLoRA (`qlora_*`)

If you train LoRA adapters, [QLoRA](/engine/guides/qlora/) quantizes the frozen base weights: about 50% smaller than FP16 with `qlora_fp8` (SM89+), about 75% smaller with `qlora_bnb` (NF4, any CUDA GPU) or `qlora_fp4` (Blackwell only). The adapters still train in higher precision.

```yaml
lora: true
qlora_bnb: true
```

### 5. Offloading (`offload_*`) and CPU-RAM centric training

When the model itself is too big, move state to CPU RAM. [CPU-RAM centric training](/engine/reference/config/#cpu-ram-centric-training) (`cpu_training: true`) keeps the weights and optimizer state on the CPU and streams them to the GPU one layer at a time, so GPU weight memory drops to about two layers. It is the simpler choice for most setups, but it doesn't work with QLoRA or pre-quantized models, and it is too slow for MoE models.

```yaml
cpu_training: true
```

The older `offload_*` options give finer control. `offload_residual: true` moves residual activations to pinned host memory; together with `recompute`, activation memory then no longer grows with network depth. `offload_optimizer` makes the optimizer step much slower because it goes over PCIe, but with enough gradient accumulation that cost becomes small. See [Offloading](/engine/guides/offloading/) for every option and its cost.

```yaml
offload_residual: true
```

### 6. More GPUs

On several GPUs, ZeRO shards optimizer states (the default), gradients (`zero_level: 2`) or weights too (`zero_level: 3`), so each GPU holds only its share. If the base weights alone don't fit on one GPU, use [Dispatch Pipeline Parallelism](/engine/guides/dispatch-pp/). See [Multi-GPU training](/engine/guides/multi-gpu/).

```yaml
gpus: 4
zero_level: 2
```

## See also

- [Offloading](/engine/guides/offloading/): how each offload option works and what it costs
- [Debugging](/engine/guides/debugging/): print a memory breakdown to find the biggest consumers
- [Config reference](/engine/reference/config/): every memory-related option and its default
- [Back to docs index](/engine/)
