deepspeedai / deepspeedai/DeepSpeed

[BUG] Qwen3: model loading failed when using meta device

Open
#7,275 22 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug
I am running on a single node with 4 GPUs; each GPU has 24GB GPU memory.

With Deepspeed-Inference, I was trying to load Qwen/Qwen3-4B using meta device. However, the loading failed and I got the following error:

NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() when moving module from meta to a different device.

Although this small model doesn't need meta device, my ultimate goal is to use the bigger qwen3 models.

To Reproduce
Steps to reproduce the behavior:

  1. Simple inference script to reproduce.

First of all, download Qwen/Qwen3-4B to local directory "Qwen3-4B"

Then, put the following code snippet to "qwen3_meta_device.py"

import os

import deepspeed
import torch
from transformers import AutoConfig, AutoModelForCausalLM

kwargs = {"torch_dtype": torch.float16}

model_config = AutoConfig.from_pretrained("./Qwen3-4B", **kwargs)

with deepspeed.OnDevice(dtype=kwargs["torch_dtype"], device="meta", enabled=True):
    model = AutoModelForCausalLM.from_config(model_config, **kwargs)

ds_inference_config = {
    "dtype": kwargs["torch_dtype"],
    "replace_with_kernel_inject": False,
    "tensor_parallel": {
        "tp_size": int(os.getenv("WORLD_SIZE", "1"))
    },
    "checkpoint": {
        "checkpoints": [
            "./Qwen3-4B/model-00001-of-00003.safetensors",
            "./Qwen3-4B/model-00002-of-00003.safetensors",
            "./Qwen3-4B/model-00003-of-00003.safetensors"
        ],
        "type": "DS_MODEL",
        "version": 1.0
    }
}

ds_engine = deepspeed.init_inference(model, config=ds_inference_config)
model = ds_engine.module
model.eval()

Finally, run "accelerate launch qwen3_meta_device.py"

  1. What packages are required and their versions
    torch==2.5.1
    transformers==4.51.3
    deepspeed==0.16.7
    accelerate==1.6.0

  2. How to run the script
    Put the above code snippet into this file: qwen3_meta_device.py
    Then, run the following:
    accelerate launch qwen3_meta_device.py

  3. ...
    Expected behavior
    The model is expected to load successfully.

The code works fine for qwen2.5-7b-instruct (after replacing the checkpoint files in the config).

ds_report output
Please run ds_report to give us details about your setup.

Screenshots
If applicable, add screenshots to help explain your problem.

System info (please complete the following information):

  • OS: [e.g. Ubuntu 18.04]
  • GPU count and types [e.g. two machines with x8 A100s each]
  • (if applicable) what DeepSpeed-MII version are you using
  • (if applicable) Hugging Face Transformers/Accelerate/etc. versions
  • Python version
  • Any other relevant info about your setup

Docker context
Are you using a specific docker image that you can share?

Additional context
Add any other context about the problem here.

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 qwen3_meta_device.py and the deepspeed.OnDevice and deepspeed.init_inference entry points used in the reproduction. Run the provided accelerate launch command with the listed package versions and Qwen3-4B checkpoints, then compare the meta-device loading path with the working Qwen2.5 example. Done means the model loads successfully without the reported meta-tensor error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
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.