deepspeedai / deepspeedai/DeepSpeed
[BUG] DeepSpeed generation fails with a CPU/CUDA device mismatch when keep-module-on-host is enabled
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 43.1k
- Forks
- 5k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 112
Description
Describe the bug
DeepSpeed accepts keep-module-on-host, completes model initialization, and passes health checks. The first real generation request fails. The same failure family was reproduced on Qwen3-4B-Instruct-2507, DeepSeek-R1-Distill-Llama-8B, and Meta-Llama-3.1-8B-Instruct.
To Reproduce
Steps to reproduce the behavior:
- Save the following as a shell command and run it in a shell on a machine with a free CUDA device.
- Run the command in the target Python environment with the listed dependencies installed.
- Observe the startup or generation result.
import torch
import deepspeed
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "/root/.cache/huggingface/hub/models--Qwen--Qwen3-4B-Instruct-2507/snapshots/cdbee75f17c01a7cc42f958dc650907174af0554"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name, torch_dtype=torch.bfloat16
).to("cuda")
engine = deepspeed.init_inference(
model, config={"keep_module_on_host": True}
)
device = next(engine.module.parameters()).device
inputs = tokenizer("Reply with exactly OK.", return_tensors="pt")
inputs = {key: value.to(device) for key, value in inputs.items()}
with torch.no_grad():
output = engine.generate(**inputs, max_new_tokens=8, do_sample=False)
print(tokenizer.decode(output[0], skip_special_tokens=True))
Actual behavior
Startup and health checks succeed, but generation fails with a device-placement error: Expected all tensors to be on the same device, but index is on cuda:0 while other tensors are on cpu during index_select.
Expected behavior
The option should either preserve compatible device placement for all tensors or be rejected before serving with an actionable validation error.
System info (please complete the following information):
- OS: Ubuntu 22.04.3 LTS
- GPU count and types: 1 x NVIDIA H100 80GB HBM3
- DeepSpeed version: 0.19.1
- DeepSpeed-MII: not used
- PyTorch: 2.9.1+cu128
- Transformers: 5.15.0
- CUDA reported by PyTorch: 12.8
- nvcc: 13.3
- Python version: 3.12.12
- Target runtime: /root/anaconda3/envs/deepspeed128
Docker context
No Docker image was used. The reproduction ran directly on Ubuntu 22.04.3 LTS in the target conda environment.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied reproduction around deepspeed.init_inference with keep_module_on_host enabled and inspect where generation reaches the reported index_select device mismatch. Trace the device placement of the model inputs and related tensors during engine.generate. Done means compatible placement is preserved or startup rejects the configuration with an actionable validation error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100