deepspeedai / deepspeedai/DeepSpeed

[BUG] DeepSpeedEngine did not gather parameters automatically when doing forward() with zero-3 enabled

Open
#3,570 13 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug training
Dominant language
Python
Stars
43.1k
Forks
5k
Avg merge
4d 15h
Merged PRs (30d)
112

Description

Describe the bug
I was using deepspeed zero3 in a compression script. When the model was instantiated, I found that every module was injected with a post_init method, which partitioned the parameters into multiple groups and erase the original parameter. However, when the model was doing forward propagation, the parameters were not gathered again, leading to empty weights for each layer.

To Reproduce
My zero configuration was simply:

"zero_optimization": {
      "stage": 3,
      "offload_optimizer": {
        "device": "cpu",
        "pin_memory": true
      },
      "offload_param": {
        "device": "cpu",
        "pin_memory": true
      },
    },

I have tried chatglm-6b and opt-350m and they led to the same issue.

Expected behavior
Reported error should be something like this:

Traceback (most recent call last):
  File ".../opt-350m/run_glue_no_trainer.py", line 571, in <module>
    main()
  File ".../opt-350m/run_glue_no_trainer.py", line 529, in main
    outputs = model(**batch)
  File ".../.conda/envs/deepspeed/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File ".../deepspeed/lib/python3.9/site-packages/deepspeed/utils/nvtx.py", line 15, in wrapped_fn
    ret_val = func(*args, **kwargs)
  File ".../deepspeed/lib/python3.9/site-packages/deepspeed/runtime/engine.py", line 1675, in forward
    loss = self.module(*inputs, **kwargs)
  File ".../deepspeed/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1212, in _call_impl
    result = forward_call(*input, **kwargs)
  File ".../deepspeed/lib/python3.9/site-packages/transformers/models/opt/modeling_opt.py", line 781, in forward
    decoder_outputs = self.decoder(
  File ".../deepspeed/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1212, in _call_impl
    result = forward_call(*input, **kwargs)
  File ".../deepspeed/lib/python3.9/site-packages/transformers/models/opt/modeling_opt.py", line 631, in forward
    inputs_embeds = self.embed_tokens(input_ids)
  File ".../deepspeed/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1212, in _call_impl
    result = forward_call(*input, **kwargs)
  File ".../deepspeed/lib/python3.9/site-packages/deepspeed/compression/basic_layer.py", line 129, in forward
    out = nn.functional.embedding(input, weight, self.padding_idx, self.max_norm, self.norm_type,
  File ".../deepspeed/lib/python3.9/site-packages/torch/nn/functional.py", line 2192, in embedding
    assert padding_idx < weight.size(0), "Padding_idx must be within num_embeddings"
AssertionError: Padding_idx must be within num_embeddings

I printed the padding_idx and the weight:

At forward(), padding_idx: 1, embedding layer weight:
Parameter containing:
tensor([], device='cuda:0', dtype=torch.float16, requires_grad=True)

You can see that the weight is empty.

ds_report output

--------------------------------------------------
DeepSpeed C++/CUDA extension op report
--------------------------------------------------
NOTE: Ops not installed will be just-in-time (JIT) compiled at
      runtime if needed. Op compatibility means that your system
      meet the required dependencies to JIT install the op.
--------------------------------------------------
JIT compiled ops requires ninja
ninja .................. [OKAY]
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
 [WARNING]  async_io requires the dev libaio .so object and headers but these were not found.
 [WARNING]  async_io: please install the libaio-devel package with yum
 [WARNING]  If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found.
async_io ............... [NO] ....... [NO]
cpu_adagrad ............ [NO] ....... [OKAY]
cpu_adam ............... [NO] ....... [OKAY]
fused_adam ............. [NO] ....... [OKAY]
fused_lamb ............. [NO] ....... [OKAY]
quantizer .............. [NO] ....... [OKAY]
random_ltd ............. [NO] ....... [OKAY]
sparse_attn ............ [NO] ....... [OKAY]
spatial_inference ...... [NO] ....... [OKAY]
transformer ............ [NO] ....... [OKAY]
stochastic_transformer . [NO] ....... [OKAY]
transformer_inference .. [NO] ....... [OKAY]
utils .................. [NO] ....... [OKAY]
--------------------------------------------------
No CUDA runtime is found, using CUDA_HOME='/mnt/lustre/share/cuda-10.1'
DeepSpeed general environment info:
torch install path ............... ['.../deepspeed/lib/python3.9/site-packages/torch']
torch version .................... 1.13.1+cu117
deepspeed install path ........... ['.../deepspeed/lib/python3.9/site-packages/deepspeed']
deepspeed info ................... 0.9.1, unknown, unknown
torch cuda version ............... 11.7
torch hip version ................ None
nvcc version ..................... 10.1
deepspeed wheel compiled w. ...... torch 0.0, cuda 0.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with deepspeed/compression/basic_layer.py and deepspeed/runtime/engine.py, following the forward() path and the parameter partitioning described in the report. Reproduce with the shown ZeRO stage 3 configuration and verify that parameters are gathered before the layer runs, with the embedding weight no longer empty.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.