[megatron] optimizer_offload_fraction < 1 crashes at the first update_actor when optimizer_offload=True
What happens
Megatron engine, actor.megatron.param_offload=True, optimizer_offload=True,
optim.override_optimizer_config.optimizer_cpu_offload=True, overlap_cpu_optimizer_d2h_h2d=True:
optimizer_offload_fraction=1.0(default): works (hundreds of steps across runs);optimizer_offload_fraction=0.5: oneWorkerDictprocess dies during the firstupdate_actorwith no Python traceback (RayActorDiedError,SYSTEM_ERROR, "connection error code 2"); host memory is healthy (73 GB RSS per worker, 445 of 952 GB per node), nooom_kill;optimizer_offload_fraction=0.0: Python traceback
File "verl/workers/engine/megatron/transformer_impl.py", line 1120, in __exit__
File "verl/workers/engine/base.py", line 323, in _context_switch
File "verl/workers/engine/megatron/transformer_impl.py", line 694, in to
File "verl/utils/megatron_utils.py", line 698, in offload_megatron_optimizer
torch.AcceleratorError: CUDA error: invalid argument
Reproduced on a 4-layer truncated DeepSeek-V4-Pro (8 nodes, EP16) and on the 61-layer model (64 nodes, PP16/EP16); same behaviour on both.
Reading
offload_megatron_optimizer / load_megatron_optimizer walk HybridDeviceOptimizer.sub_optimizers and move every state
tensor to CPU and back; with a fraction < 1 the GPU sub-optimizer (FusedAdam) keeps its own device assumptions and the
per-step round trip leaves its state on the wrong device (0.0: the move itself raises; 0.5: the next fused step crashes
natively). The all-CPU form (fraction 1.0) never has a GPU sub-optimizer, which is why it works. Megatron-LM #7232 (HDO
materialised twice under the distributed optimizer) is the nearest upstream thread. I have not isolated the exact tensor;
logs of all three runs are available.
Ask
Either support fractions < 1 in the offload/onload helpers (skip the GPU sub-optimizer's state, or offload it through
HDO's own d2h/h2d path) or reject optimizer_offload_fraction < 1 together with optimizer_offload=True at config
validation with a clear message. The use case: 61-layer DeepSeek-V4-Pro at 64 nodes has 63 GB of 189 GB GPU memory in use
and 905 of 952 GB host memory, so keeping part of the Adam state on the GPU is the natural lever for a checkpoint save.
Environment: verl 1d367a4f, Megatron-LM 43e45e13 (+ #4046/#7785), GB200 aarch64.
Hi @linmuchuiyang,
The root cause is in offload_megatron_optimizer:
hdo.state[orig_param][k] = state[k] = v.to("cpu")
With optimizer_offload_fraction < 1, the HDO has a GPU-resident FusedAdam sub-optimizer whose state must stay on GPU. This line moves it to CPU — and when master_param is a view into param_data freed by the model offload earlier in the same phase switch, it reads freed memory and raises the CUDA error: invalid argument you saw.
To resolve this, you can try #8286: HDO state is now left untouched in the offload; only non-HDO optimizers are offloaded. load_megatron_optimizer is unchanged.
Thanks @Jackie2049, that matches what we saw: fraction 0.0 raising inside offload_megatron_optimizer, fraction 0.5 dying natively in the next fused step. Applied #8286 on top of 1d367a4f and re-ran the 4-layer DeepSeek-V4-Pro smoke on GB200 (8 nodes, EP16, Megatron-LM 43e45e13, param_offload=optimizer_offload=True, optimizer_cpu_offload=True, overlap_cpu_optimizer_d2h_h2d=True):
optimizer_offload_fraction |
before #8286 | with #8286 |
|---|---|---|
| 0.5 | worker dies natively in the first update_actor |
2/2 GRPO steps complete |
| 0.0 | CUDA error: invalid argument in offload_megatron_optimizer |
2/2 GRPO steps complete |
Numbers on the same smoke (step 2), fraction 1.0 baseline / 0.5 / 0.0: step time 102.9 / 100.8 / 103.6 s, update_actor 47.0 / 46.3 / 46.7 s, peak GPU memory 69.6 / 81.3 / 93.9 GB, peak host memory on the head node 648 / 518 / 383 GB, rollout/actor log-prob mean abs diff 0.0037 / 0.0035 / 0.0037. So the fraction knob now does what the issue asked for: it trades GPU memory for host memory without changing the numerics. Fine from our side, thanks for the quick fix.
@linmuchuiyang Glad to help, and thanks for verifying the fix on your devices. Looking forward to more collaboration and contributions to the community. 🤝