Skip to content
SurogateSurogateDocs
↑↓ to move↵ to openesc to close

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 has the full list and defaults.

To see where the memory goes before you change anything, set debug_memory_breakdown: true (see Debugging).

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

Try these next, in this order.

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)

Section titled “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

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.

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

If you train LoRA adapters, 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

Section titled “5. Offloading (offload_*) and CPU-RAM centric training”

When the model itself is too big, move state to CPU RAM. 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 for every option and its cost.

yaml
offload_residual: true

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. See Multi-GPU training.

yaml
gpus: 4
zero_level: 2