
CPU Offloading
OfficialFreeOptimize memory usage by offloading CPU tasks in Megatron Bridge.
Free · Opens the source repo
What CPU Offloading does
The CPU Offloading skill for Megatron Bridge enables developers to efficiently manage memory usage by offloading specific tasks from GPU to CPU. This skill provides two primary mechanisms: activation offloading and optimizer offloading. Activation offloading allows for the transfer of activations and optionally weights from transformer layers to CPU memory, while optimizer offloading focuses on moving Adam optimizer states, including momentum and variance, to the CPU. This flexibility can significantly enhance the performance of large models, particularly those with high memory demands.
For large models, especially those exceeding 30 billion parameters, optimizer offloading is recommended due to its compatibility with pipeline parallelism, which is crucial for handling extensive datasets. In contrast, activation offloading is suitable for smaller to medium models where memory constraints are a concern, as it allows for the offloading of activations while maintaining a pipeline model parallel size of one. The skill is designed for developers working with deep learning models who need to optimize their resource usage without compromising training efficiency.
The skill also allows for tunable memory-speed tradeoffs, enabling users to adjust the fraction of optimizer states offloaded to the CPU. However, it is important to note that enabling offloading can introduce overhead, which may impact throughput. Therefore, users should carefully evaluate their specific use cases and memory requirements before implementation. The skill is particularly useful in scenarios where memory pressure is moderate, allowing for a balanced approach to resource management.
When to use it
Use this skill when working with large models that require efficient memory management and when pipeline parallelism is needed.
When not to use it
Avoid using this skill if your model does not exceed memory capacity or if maximum throughput is a priority, as offloading can introduce overhead.
What you can build with it
Training Large Models
Utilize optimizer offloading for training large models that exceed GPU memory capacity, ensuring efficient use of resources.
Managing Memory Pressure
Implement activation offloading in smaller models to alleviate memory pressure while maintaining training performance.
Optimizing Resource Allocation
Adjust optimizer offloading fractions to find the right balance between memory usage and training speed in various scenarios.
How to install CPU Offloading
View source1. Install with the skills CLI
npx skills add nvidia/skills/nemo-mbridge-perf-cpu-offloading --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by nvidiaCPU Offloading
References
- Stable docs: @docs/training/cpu-offloading.md
- Structured metadata: @skills/nemo-mbridge-perf-cpu-offloading/card.yaml
What It Is
Two independent mechanisms to move data from GPU to CPU memory:
| Mechanism | Config namespace | What gets offloaded | PP restriction |
|---|---|---|---|
| Activation offloading | model.cpu_offloading* | Activations (and optionally weights) per transformer layer | PP must be 1 |
| Optimizer offloading | optimizer.optimizer_cpu_offload | Adam optimizer states (momentum + variance) via HybridDeviceOptimizer | None |
Quick Decision
| Situation | Recommendation |
|---|---|
| Large MoE model (30B+), needs PP > 1 | Optimizer offloading — activation offloading is blocked by PP=1 |
| Small/medium model, PP=1 fits, activation memory dominates | Activation offloading |
| Want tunable memory-speed tradeoff | Optimizer offloading with fractional optimizer_offload_fraction |
| Throughput is top priority | Don't enable — offloading always adds overhead |
| CUDA graphs are needed | Only optimizer offloading — activation offloading is incompatible |
| Memory pressure is moderate | Optimizer offload at 25–50% fraction for best efficiency |
Enablement
Optimizer CPU offloading (recommended for large models)
cfg.optimizer.optimizer_cpu_offload = True
cfg.optimizer.optimizer_offload_fraction = 1.0
cfg.optimizer.overlap_cpu_optimizer_d2h_h2d = True
CLI overrides:
optimizer.optimizer_cpu_offload=True \
optimizer.optimizer_offload_fraction=0.5 \
optimizer.overlap_cpu_optimizer_d2h_h2d=True
Activation CPU offloading (small/medium models only)
cfg.model.cpu_offloading = True
cfg.model.cpu_offloading_num_layers = 16
cfg.model.cpu_offloading_activations = True
cfg.model.cpu_offloading_weights = False
cfg.model.pipeline_model_parallel_size = 1
cfg.model.recompute_granularity = None
cfg.model.cuda_graph_impl = "none"
Config Parameter Reference
Optimizer offloading
| Parameter | Default | Description |
|---|---|---|
optimizer_cpu_offload | False | Master switch |
optimizer_offload_fraction | 0.0 | Fraction of optimizer states on CPU (0.0–1.0) |
overlap_cpu_optimizer_d2h_h2d | False | Overlap GPU↔CPU transfers with compute |
use_torch_optimizer_for_cpu_offload | False | Use torch.optim instead of fused optimizer for CPU portion |
Activation offloading
| Parameter | Default | Description |
|---|---|---|
cpu_offloading | False | Master switch |
cpu_offloading_num_layers | 0 | Number of transformer layers to offload (0 to num_layers-1) |
cpu_offloading_activations | True | Offload activations |
cpu_offloading_weights | False | Offload weights |
cpu_offloading_double_buffering | False | Double-buffer across layers while reloading |
Compatibility And Constraints
Activation offloading
pipeline_model_parallel_sizemust be 1recompute_granularitymust beNone- Cannot combine with
fine_grained_activation_offloading - Cannot combine with CUDA graphs
cpu_offloading_num_layersmust be in[0, num_layers-1)
Optimizer offloading
- Requires
use_distributed_optimizer = True(default in most recipes) - No PP, recompute, or CUDA graph restrictions
optimizer_offload_fractionmust be in[0.0, 1.0]
Practical: large MoE models
Activation offloading is blocked for Qwen3-30B-A3B and similar large MoE models. The PP=1 constraint means each GPU holds all 48 layers; model weights + optimizer states alone (~70 GB) exceed H100 80 GB capacity.
Minimal Runnable Command
uv run python scripts/training/run_recipe.py \
--recipe qwen3_30b_a3b_pretrain_config \
optimizer.optimizer_cpu_offload=True \
optimizer.optimizer_offload_fraction=0.5 \
train.train_iters=20 \
train.global_batch_size=8 \
train.micro_batch_size=1
Verification
Unit tests
uv run python -m pytest \
tests/unit_tests/models/test_gpt_full_te_layer_autocast_spec.py -k "cpu_offload" \
tests/unit_tests/peft/test_utils.py -k "cpu_offload" -q
Success criteria
- Config validation passes for the selected offloading mode
- Training completes without OOM or NCCL errors
- Loss matches the non-offloaded baseline (max delta < 0.001)
- Memory usage drops proportionally to offload fraction
Code Anchors
MCore activation offload constraints
if self.cpu_offloading and (
self.cpu_offloading_num_layers < 0 or self.cpu_offloading_num_layers >= self.num_layers
):
raise ValueError(...)
if self.cpu_offloading and self.pipeline_model_parallel_size > 1:
raise ValueError(
"Currently there is no support for Pipeline parallelism with CPU offloading"
)
if self.cpu_offloading and self.recompute_granularity is not None:
raise ValueError(
"CPU offloading does not work when activation recomputation is enabled"
)
MCore CUDA graph incompatibility
if self.cpu_offloading:
raise ValueError("CUDA graphs not supported with CPU offloading.")
MCore fine-grained offloading mutual exclusion
if self.fine_grained_activation_offloading:
assert (
not self.cpu_offloading
), "fine_grained_activation_offloading cannot be enabled with cpu_offloading."
MCore HybridDeviceOptimizer instantiation
if config.optimizer_cpu_offload:
# ... setup cpu/gpu optimizer classes ...
optimizer = HybridDeviceOptimizer(
param_groups,
offload_fraction=config.optimizer_offload_fraction,
cpu_optimizer_cls=cpu_optimizer_cls,
gpu_optimizer_cls=gpu_optimizer_cls,
overlap_cpu_optimizer_d2h_h2d=config.overlap_cpu_optimizer_d2h_h2d,
pin_cpu_grads=config.pin_cpu_grads,
pin_cpu_params=config.pin_cpu_params,
)
Bridge CUDA graph guard
assert not config.cpu_offloading and config.recompute_granularity is None, "Cudagraphs not supported"
Bridge activation offloading in PEFT
if self.config.cpu_offloading and self.config.cpu_offloading_activations:
x.activation_offloading = True
x, _ = self.linear_in(x)
x = self.activation(x)
if self.config.cpu_offloading and self.config.cpu_offloading_activations:
x.activation_offloading = True
x, _ = self.linear_out(x)
Failure Diagnosis
| Symptom | Likely Cause | How To Confirm | Fix |
|---|---|---|---|
Currently there is no support for Pipeline parallelism with CPU offloading | Activation offload + PP > 1 | Check pipeline_model_parallel_size | Set PP=1 or use optimizer offloading |
CPU offloading does not work when activation recomputation is enabled | Activation offload + recompute | Check recompute_granularity | Set recompute_granularity=null |
fine_grained_activation_offloading cannot be enabled with cpu_offloading | Both offloading modes enabled | Check both flags | Use one or the other |
CUDA graphs not supported with CPU offloading | CUDA graphs + activation offload | Check cuda_graph_impl | Set cuda_graph_impl="none" |
| OOM with activation offloading | Model too large for PP=1 | Check allocated memory vs 80 GB | Use optimizer offloading with PP > 1 |
| Extreme slowdown (>4x) | 100% optimizer offload, CPU Adam bottleneck | Compare iter time at different fractions | Reduce fraction or enable overlap_cpu_optimizer_d2h_h2d |
| OOM at partial optimizer offload | Insufficient offload for this config | Check memory at different fractions | Increase fraction or add PP |
Known Limitations
- Activation offloading requires PP=1, making it impractical for large models (30B+ MoE) that need pipeline parallelism.
- Optimizer offloading throughput penalty scales linearly (~1.9x at 25%, ~4.2x at 100% for Qwen3-30B-A3B).
- D2H/H2D overlap provides only ~7% speedup because CPU Adam compute is the dominant bottleneck.
fine_grained_activation_offloadingis a separate module-level approach that works with PP > 1 but cannot be combined with layer-levelcpu_offloading.
Frequently asked questions about CPU Offloading
Similar skills
Heap Snapshot Analysis
Investigate V8 heap snapshots for memory issues.
VS Code Performance Workflow
Automate performance investigations in VS Code.
Memory Leak Audit
Prevent memory leaks with effective coding patterns.
CPU Profile Analysis
Analyze V8 and Chrome performance profiles for optimization.
Chat Performance Testing
Benchmark and validate chat UI performance in VS Code.
Vercel React Best Practices
Optimize your React and Next.js applications for performance.
