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

Offloading in Surogate

Offloading lets you fit a larger model on a smaller GPU. Surogate moves model components that are accessed less often to CPU or NVMe storage, which is slower but much larger than GPU memory. It works for both training and inference.

These are the offloading options. The config reference lists them too, and for most setups recommends CPU-RAM centric training as the simpler replacement for the legacy ones.

ParameterDescriptionDefault
offload_residualOffloads residual activations to CPU to reduce GPU memory usage during training.False
offload_optimizerOffloads optimizer states (e.g., momentum, variance) to CPU to save GPU memory during training.False
offload_masterOffloads master weights to CPU to save GPU memory during optimizer updates.False
offload_gradsOffloads gradients to CPU to reduce GPU memory usage during backpropagation.False
offload_quantsOffloads quantized weights to CPU to reduce GPU memory usage during forward/backward passes.False
persistent_quantsKeeps quantized weights in GPU memory between iterations to speed up training at the cost of memory.False

With offload_residual on, Surogate copies residual activations from the GPU to pinned CPU memory during the forward pass and prefetches them back during the backward pass. Together with recompute: true (see Recomputation), this makes activation memory O(1) instead of O(L), where L is the number of layers.

With offloading enabled:

  • GPU buffers: 2 double-buffered device tensors (shared across all layers via round-robin)
  • CPU buffers: L pinned host buffers (one per layer to store each layer’s residual)

Without offloading:

  • L device buffers (one per layer, all on GPU)
ModeGPU MemoryCPU MemoryTransfers/step
Offload ONO(1) - 2 buffersO(L) pinned2L (D2H + H2D)
Offload OFFO(L) - L buffers00

With offload_optimizer on, Surogate keeps the optimizer states (the first moment m and second moment v for AdamW) in pinned CPU memory instead of on the GPU. For large models this frees a lot of GPU memory.

The optimizer keeps these states for each parameter:

  • First moment (m): Exponential moving average of gradients (momentum)
  • Second moment (v): Exponential moving average of squared gradients (variance)
  • FP8 scales: Additional scale tensors when using 8-bit optimizer states

Surogate double-buffers the states so data transfers overlap with computation:

Buffer 0: layer 0, 2, 4, ... (layer_idx % 2 == 0)
Buffer 1: layer 1, 3, 5, ... (layer_idx % 2 == 1)

Two temporary GPU buffers exist during the optimizer pass. One receives data while the other is used for computation.

ConfigurationGPU MemoryCPU Memory
Offload OFF~8 bytes/param (m + v)0
Offload ON2 temporary buffers~8 bytes/param (pinned)
Offload ON + FP82 temporary buffers~2 bytes/param + scales

Offloading makes the optimizer step much slower, because the step is memory-bound and now has to go over PCIe. With enough gradient accumulation steps, though, its share of the total time becomes negligible. With gradient_accumulation_steps=4 (a training loop setting), for example, the optimizer runs once per 4 forward/backward passes.

With offload_master on, Surogate keeps the master copy of the model weights in pinned CPU memory instead of on the GPU. Master weights (see Precision & recipes) are the full-precision (typically FP32) copies the optimizer updates. The forward and backward passes use lower-precision work weights (BF16/FP8/FP4).

Mixed-precision training keeps two copies of the weights:

  • Master weights (master_dtype): Full-precision (FP32 or BF16) weights updated by the optimizer
  • Work weights (matmul_dtype): Lower-precision weights (BF16, FP8, FP4) used for forward/backward passes

The optimizer updates the master weights, and Surogate converts them to work weights for the next iteration.

Master weights are double-buffered in the same way as optimizer states:

Layer 0 → Buffer 0
Layer 1 → Buffer 1
Layer 2 → Buffer 0 (reuse)
Layer 3 → Buffer 1 (reuse)
...

Two temporary GPU buffers exist during the optimizer pass. While one layer is updated, the next layer’s weights are prefetched.

ConfigurationGPU MemoryCPU Memory
Offload OFFL × master_weight_size0
Offload ON2 × master_weight_sizeL × master_weight_size (pinned)

Offloading master weights adds two PCIe transfers to the optimizer step:

  • H2D transfer before each layer’s update
  • D2H transfer after each layer’s update

It pays off when GPU memory is tight and gradient accumulation spreads the optimizer cost over many passes.

With offload_grads on, Surogate keeps the persistent gradient shards in pinned CPU memory instead of on the GPU. This saves GPU memory, and gradient accumulation still works correctly.

With gradient sharding enabled:

  • Each GPU computes full gradients during the backward pass
  • Gradients are reduce-scattered across GPUs (each rank receives its local shard of the sum)
  • Each rank stores only its own shard persistently
  • During the optimizer step, each rank updates only its shard of the weights

GPU buffers (always on device):

  • 2 double-buffered full gradient tensors for backward computation
  • Used round-robin: layer L uses buffer (L % 2)

Persistent shards (HOST or GPU depending on offload_grads):

  • L sharded gradient tensors (one per layer, only local shard)
  • If offload_grads=true: stored in pinned host memory
  • If offload_grads=false: stored on GPU
ConfigurationGPU MemoryCPU Memory
Offload OFF2 full buffers + L shards0
Offload ON2 full buffersL shards (pinned)
  • GPU only holds double-buffered full gradients during backward
  • Persistent shards on host memory are accessed via CUDA’s host memory mapping
  • Reduce-scatter communication overlaps backward computation via double-buffering
  • Trade-off: reduced GPU memory vs. PCIe bandwidth for optimizer access
  • On a single GPU, there’s no sharding benefit

Quantized Weights: persistent_quants and offload_quants (Multi-GPU only)

Section titled “Quantized Weights: persistent_quants and offload_quants (Multi-GPU only)”

These two options control where quantized weights are stored in distributed training.

Important constraints:

  • persistent_quants requires shard_weights=true (ZeRO-3)
  • offload_quants requires persistent_quants=true

In weight-sharded (ZeRO-3) training, weights have to be gathered across GPUs before each forward/backward pass. Quantized weights are lower-precision copies (FP8, FP4, or INT8) of the master weights. They:

  • Reduce all-gather communication bandwidth
  • Reduce GPU memory for the gathered full weights
  • Are re-quantized after each optimizer step (when master weights change)

With persistent_quants on:

  • Quantized weights are computed once and stored persistently (one copy per layer)
  • No re-quantizing on every forward pass, which saves compute
  • After an optimizer update, version tracking marks the quantized weights as stale
  • Stale weights are re-quantized on the next gather_block() call

Without it, weights are quantized on the fly during each gather.

With offload_quants on (it requires persistent_quants):

  • Persistent quantized weights live in pinned host memory instead of on the GPU
  • Double-buffered GPU staging buffers handle H2D transfers
  • Quantized weights are streamed to GPU only when needed for gather/all-gather
ConfigurationGPU MemoryCPU Memory
NeitherQuantize on-the-fly (temporary)0
persistent_quants onlyL × quantized_weight_size0
persistent_quants + offload_quants2 staging buffersL × quantized_weight_size (pinned)
  • persistent_quants trades memory for compute (avoids repeated quantization)
  • offload_quants moves quantized weights to CPU, reducing GPU memory
  • In PCIe multi-GPU setups, H2D transfer may overlap with all-gather communication
  • Combined with --memcpy-all-gather (see Multi-GPU options), this can speed up training

Any configuration that streams weights (cpu_training, offload_master) tends to be limited by the PCIe link before compute. Before you optimize kernels, check which of the two you are waiting on:

bash
nsys profile -o prof --trace=cuda -y 50 -d 45 python -m surogate.cli.sft config.yaml
nsys stats --report cuda_gpu_mem_time_sum prof.nsys-rep

If [CUDA memcpy Host-to-Device] dominates and its achieved bandwidth is close to your link’s ceiling (~53 GB/s measured on PCIe 5.0 x16), the step is transfer-bound. Faster kernels would only turn busy time into idle time; the step would not get shorter. What helps is moving fewer bytes or overlapping them better. See Tuning knobs for SUROGATE_DISPATCH_PREFETCH_BLOCKS and SUROGATE_RESIDENT_LAYERS.