Memory
EngineSurogate EngineOut 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).
First-line levers
Section titled “First-line levers”These change how much data each step holds on the GPU.
- Reduce
per_device_train_batch_sizeand increasegradient_accumulation_stepsby 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_lenif your data allows it. Activation memory grows with sequence length, but shorter sequences truncate longer samples, so this does change what you train on.
per_device_train_batch_size: 1 # was 2gradient_accumulation_steps: 8 # was 4, same effective batchsequence_len: 2048Framework knobs
Section titled “Framework knobs”Try these next, in this order.
1. Recomputation (recompute)
Section titled “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.
recompute: true2. 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.
lmhead_chunks: 4attn_bwd_chunks: 23. Long sequences: Tiled MLP for long context (long_context) and sequence_chunks
Section titled “3. Long sequences: Tiled MLP for 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.
long_context: truelora_dropout: 0For 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.
sequence_len: 32768sequence_chunks: 4per_device_train_batch_size: 1lora_dropout: 04. Quantization / QLoRA (qlora_*)
Section titled “4. Quantization / QLoRA (qlora_*)”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.
lora: trueqlora_bnb: true5. 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.
cpu_training: trueThe 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.
offload_residual: true6. More GPUs
Section titled “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. See Multi-GPU training.
gpus: 4zero_level: 2See also
Section titled “See also”- Offloading: how each offload option works and what it costs
- Debugging: print a memory breakdown to find the biggest consumers
- Config reference: every memory-related option and its default
- Back to docs index