[BUG]: TorchDDPPlugin with fp16: loading a sharded optimizer checkpoint fails, the optimizer is unwrapped only once
Is there an existing issue for this bug?
- [x] I have searched the existing issues
The bug has not been fixed in the latest main branch
- [x] I have checked the latest main branch
Do you feel comfortable sharing a concise (minimal) script that reproduces the error? :)
Yes, I will share a minimal reproducible script.
🐛 Describe the bug
With TorchDDPPlugin and mixed_precision="fp16", booster.load_optimizer fails for a checkpoint saved with shard=True. It fails with an AttributeError on an OptimizerWrapper. Which attribute is missing depends on the model; we have seen both:
AttributeError: 'OptimizerWrapper' object has no attribute '_patch_step_function'
AttributeError: 'OptimizerWrapper' object has no attribute 'state'
Without mixed_precision, or with shard=False, the checkpoint loads fine.
Cause. With mixed precision the optimizer is wrapped twice:
TorchDDPPlugin.configurewraps it in anOptimizerWrapper(booster.py#L154, torch_ddp_plugin.py#L292).FP16TorchMixedPrecision.configurethen wraps that in aTorchAMPOptimizer(booster.py#L165).
TorchDDPCheckpointIO.load_sharded_optimizer passes optimizer.unwrap() on (torch_ddp_plugin.py#L167). That removes one wrapper and returns the inner OptimizerWrapper, not the torch.optim.Optimizer. GeneralCheckpointIO.load_sharded_optimizer then uses torch.optim.Optimizer attributes on it (checkpoint_io/utils.py#L893).
A possible fix is for OptimizerWrapper.unwrap() to unwrap until it reaches a plain torch.optim.Optimizer, or for TorchDDPPlugin.configure not to wrap again in this case.
Reproduction (1 GPU):
# torchrun --nproc_per_node=1 sharded_optimizer_load.py fp16
import sys
import tempfile
import torch
from torch import nn
import colossalai
from colossalai.accelerator import get_accelerator
from colossalai.booster import Booster
from colossalai.booster.plugin import TorchDDPPlugin
colossalai.launch_from_torch()
device = get_accelerator().get_current_device()
model = nn.Linear(8, 8).to(device)
booster = Booster(plugin=TorchDDPPlugin(), mixed_precision=sys.argv[1] if len(sys.argv) > 1 else None)
model, optimizer, *_ = booster.boost(model, torch.optim.Adam(model.parameters()))
loss = model(torch.randn(4, 8, device=device)).sum()
booster.backward(loss, optimizer)
optimizer.step()
with tempfile.TemporaryDirectory() as ckpt:
booster.save_optimizer(optimizer, ckpt, shard=True)
booster.load_optimizer(optimizer, ckpt)
print("loaded")
Without the fp16 argument the script prints loaded. With it:
File "colossalai/booster/plugin/torch_ddp_plugin.py", line 166, in load_sharded_optimizer
super().load_sharded_optimizer(
File "colossalai/checkpoint_io/general_checkpoint_io.py", line 105, in load_sharded_optimizer
sharded_optimizer_loading_epilogue(optimizer)
File "colossalai/checkpoint_io/utils.py", line 893, in sharded_optimizer_loading_epilogue
optimizer._patch_step_function() # To support multiprocessing pickle/unpickle
AttributeError: 'OptimizerWrapper' object has no attribute '_patch_step_function'
Environment
- ColossalAI: main at d414d565
- PyTorch 2.13.0+cu130, CUDA 13.0, Python 3.10.12
- 1× NVIDIA H20, driver 595.71.05