ColossalAI icon indicating copy to clipboard operation
ColossalAI copied to clipboard

[BUG]: loading a sharded optimizer checkpoint fails on CPU: `CpuAccelerator.synchronize` raises

Open JazyJiang opened this issue 1 month ago • 0 comments

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

On a machine without GPUs, booster.load_optimizer raises for a checkpoint saved with shard=True. An unsharded checkpoint loads fine, and so does the same sharded checkpoint on GPU.

load_states_into_optimizer calls get_accelerator().synchronize() (checkpoint_io/utils.py#L880), and CpuAccelerator.synchronize raises unconditionally (cpu_accelerator.py#L57):

RuntimeError: this method is not supported for cpu accelerator

CPU work is synchronous, so a no-op synchronize would be correct (torch.cpu.synchronize() is one). That would also fix any other caller that synchronizes without checking the device.

Reproduction (no GPU, or CUDA_VISIBLE_DEVICES=):

# CUDA_VISIBLE_DEVICES= torchrun --nproc_per_node=1 sharded_optimizer_load.py
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")

On GPU the script prints loaded. On CPU it fails with the error above.

Environment

  • ColossalAI: main at d414d565
  • PyTorch 2.13.0+cu130, Python 3.10.12; CPU only (CUDA_VISIBLE_DEVICES=)

JazyJiang avatar Nov 03 '26 06:11 JazyJiang