deepspeedai / deepspeedai/DeepSpeed

[REQUEST] Avoid CUDA initialisation on import

Open
#7,252 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is your feature request related to a problem? Please describe.

If one runs import deepspeed, CUDA is initialised as shown in the following script:

import deepspeed
import torch, subprocess, re

def nvidia_smi_memory():
    smi = subprocess.check_output(["nvidia-smi", "-q", "-d", "MEMORY"]).decode()
    return int(re.search(r"Used\s+:\s+(\d+)\s+MiB", smi).group(1))

print("after import :", torch.cuda.is_initialized(), nvidia_smi_memory(), "MiB")
torch.randn(1, device="cuda")
print("after tensor :", torch.cuda.is_initialized(), nvidia_smi_memory(), "MiB")

# after import : True 4 MiB
# after tensor : True 528 MiB

This leads to various issues if you try to fork the processes or run libraries like vllm together with transformers (see https://github.com/vllm-project/vllm/issues/17079 for an example).

On the Hugging Face side, we've refactored our libraries to guard against deepspeed imports as follows:

Although this works, it would be much better if we could do a single check for deepspeed being installed and then import it without having to worry about CUDA being initialised.

Describe the solution you'd like

Being able to run import deepspeed without initialising CUDA.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request 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 the issue's minimal import deepspeed reproduction and inspect the import path that causes torch.cuda.is_initialized() to become true. Compare behavior before and after importing PyTorch and DeepSpeed, then verify that importing DeepSpeed leaves CUDA uninitialized while preserving normal library availability.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.