ColossalAI icon indicating copy to clipboard operation
ColossalAI copied to clipboard

[BUG]: TorchFSDPPlugin writes no config.json in a sharded model checkpoint, so `from_pretrained` cannot load it

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

booster.save_model(model, path, shard=True) with TorchFSDPPlugin writes the weights and the index but no config.json (nor generation_config.json). from_pretrained(path) then builds the model from a default config and fails on every shape. The same script with TorchDDPPlugin writes both files and loads.

TorchFSDPCheckpointIO.save_sharded_model calls utils.save_config_file(model.unwrap(), checkpoint_path) (torch_fsdp_plugin.py#L230). For FSDP, model.unwrap() is the FullyShardedDataParallel wrapper, not the HuggingFace model, so save_config_file returns without writing at its isinstance(model, PreTrainedModel) check (checkpoint_io/utils.py#L605). Passing model.unwrap().module (the module FSDP wraps) would fix it.

The weights themselves are right: their names and shapes match the model, and training resumed from such a checkpoint with booster.load_model continues the uninterrupted run bit for bit.

Reproduction (1 GPU):

# torchrun --nproc_per_node=1 fsdp_config.py {TorchFSDPPlugin,TorchDDPPlugin}
import os
import sys
import tempfile

import torch.distributed as dist
from transformers import LlamaConfig, LlamaForCausalLM

import colossalai
from colossalai.booster import Booster
from colossalai.booster import plugin as plugins

colossalai.launch_from_torch()
config = LlamaConfig(hidden_size=64, intermediate_size=128, num_hidden_layers=2, num_attention_heads=4, vocab_size=128)
booster = Booster(plugin=getattr(plugins, sys.argv[1])())
model, *_ = booster.boost(LlamaForCausalLM(config).cuda())
with tempfile.TemporaryDirectory() as ckpt:
    booster.save_model(model, ckpt, shard=True, use_safetensors=True)
    if dist.get_rank() == 0:
        print(f"{sys.argv[1]} wrote: {sorted(os.listdir(ckpt))}")
        loaded = LlamaForCausalLM.from_pretrained(ckpt)
        print(f"{sys.argv[1]}: from_pretrained loaded hidden_size={loaded.config.hidden_size}")

Output:

TorchFSDPPlugin wrote: ['model-00001.safetensors', 'model.safetensors.index.json']
model.layers.{0, 1}.self_attn.q_proj.weight | MISMATCH | Reinit due to size mismatch - ckpt: torch.Size([64, 64]) vs model:torch.Size([4096, 4096])
...
RuntimeError: You set `ignore_mismatched_sizes` to `False`, thus raising an error. For details look at the above report!

TorchDDPPlugin wrote: ['config.json', 'generation_config.json', 'model-00001.safetensors', 'model.safetensors.index.json']
TorchDDPPlugin: from_pretrained loaded hidden_size=64

Environment

  • ColossalAI: main at d414d565
  • PyTorch 2.13.0+cu130, CUDA 13.0, transformers 5.17.0, Python 3.10.12
  • 1× NVIDIA H20, driver 595.71.05

JazyJiang avatar Nov 03 '26 09:11 JazyJiang