Skip to content

[BUG] Size of saved model checkpoint becomes much larger after deepspeed.initialize when using ZeRO-2 #3303

Description

@ArvinZhuang

Describe the bug
Originally reported here. @stas00 @tjruwase

For some models, the size of model checkpoints saved by model.save_prtrained() becomes much larger after calling deepspeed.initialize. See examples below.

To Reproduce

from transformers import AutoModelForCausalLM
import deepspeed

ds_config = {
    "optimizer": {
        "type": "AdamW",
    },
    "zero_optimization": {
        "stage": 2,
        "offload_optimizer": {
            "device": "cpu",
            "pin_memory": True
        },
        "allgather_partitions": True,
        "allgather_bucket_size": 2e8,
        "overlap_comm": True,
        "reduce_scatter": True,
        "reduce_bucket_size": 2e8,
        "contiguous_gradients": True
    },
    "offload_optimizer": {
        "device": "cpu",
        "pin_memory": True
    },
    "train_batch_size": 1,
    "train_micro_batch_size_per_gpu": 1
}

model = AutoModelForCausalLM.from_pretrained("decapoda-research/llama-7b-hf")
model.save_pretrained("before")
deepspeed_engine, _, _, _ = deepspeed.initialize(model=model, config_params=ds_config)
deepspeed_engine.module.save_pretrained("after")

File sizes:

du -a -h --max-depth=1 before/
512     before/config.json
32K     before/pytorch_model.bin.index.json
9.2G    before/pytorch_model-00001-of-00003.bin
9.3G    before/pytorch_model-00002-of-00003.bin
6.7G    before/pytorch_model-00003-of-00003.bin
512     before/generation_config.json
26G     before/

du -a -h --max-depth=1 after/
512     after/config.json
32K     after/pytorch_model.bin.index.json
26G     after/pytorch_model-00001-of-00003.bin
26G     after/pytorch_model-00002-of-00003.bin
26G     after/pytorch_model-00003-of-00003.bin
512     after/generation_config.json
76G     after/

This issue is not always occurred, for example, gpt2 does not have this problem. But I tested decapoda-research/llama-7b-hf, and decapoda-research/llama-13b-hf have this issue.

This can be fixed by re-clone states before the saving:

state_dict = deepspeed_engine.module.state_dict()
state_dict = type(state_dict)(
    {k: v.clone()
     for k,
     v in state_dict.items()})
deepspeed_engine.module.save_pretrained("after_fixed", state_dict=state_dict)

Expected behavior
The saved model size should be unchanged after deepspeed.initialize

System info (please complete the following information):

  • deepspeed: 0.8.3
  • transformers version: 4.28.0.dev0
  • Platform: Linux-4.18.0-372.32.1.el8_6.x86_64-x86_64-with-glibc2.17
  • Python version: 3.8.16
  • Huggingface_hub version: 0.13.3
  • Safetensors version: not installed
  • PyTorch version (GPU?): 1.12.1+cu116 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?: yes
  • Using distributed or parallel set-up in script?: yes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtraining

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions