RuntimeError: CUDA driver initialization failed, you might not have a CUDA gpu.

用户在使用 vLLM 部署推理服务时,当 vLLM 通过 fork 方式启动 EngineCore/Worker 子进程,且环境中安装了 Triton 3.8 版本,子进程在调用 torch.cuda.set_device(0) 或 torch.accelerator.set_device_inde

RuntimeError: CUDA driver initialization failed, you might not have a CUDA gpu.

RuntimeError: CUDA driver initialization failed, you might not have a CUDA gpu.

快速结论:该报错通常在 vLLM 使用 Triton 3.8+ 并采用 fork 方式启动子进程时出现。优先排查 Triton 版本是否升级到 3.8,并检查是否存在 from vllm.triton_utils import HAS_TRITONfrom triton.backends import backends; backends["nvidia"].driver.is_active() 这类预先探测 CUDA 驱动状态的代码。

问题场景

用户在使用 vLLM 部署推理服务时,当 vLLM 通过 fork 方式启动 EngineCore/Worker 子进程,且环境中安装了 Triton 3.8 版本,子进程在调用 torch.cuda.set_device(0)torch.accelerator.set_device_index 等 CUDA 初始化函数时触发此错误。

报错原文

RuntimeError: CUDA driver initialization failed, you might not have a CUDA gpu.

完整调用栈示例(来自 Issue 讨论):

File "vllm/v1/worker/gpu_worker.py", line 361, in init_device
    torch.accelerator.set_device_index(self.device)
File "torch/cuda/__init__.py", line 587, in _lazy_init
    torch._C._cuda_init()
RuntimeError: CUDA driver initialization failed, you might not have a CUDA gpu.

原因分析

此问题的根源在于 Triton 3.8 中 NVIDIA 后端的行为变更。在 vLLM 的 vllm.triton_utils 模块中,存在如下探测代码:

# vllm/triton_utils/importing.py
from triton.backends import backends

for backend in backends.values():
    if backend.driver and backend.driver.is_active():
        ...

在 Triton 3.8 中, backend.driver.is_active() 的实现会调用原始 CUDA 驱动初始化函数 cuInit(0),源码如下:

@staticmethod
def is_active():
    return _cuda_driver_is_active()

此调用会在父进程中隐式初始化 CUDA 驱动状态,尽管 torch.cuda.is_initialized() 仍然返回 False。当 vLLM 随后通过 fork 创建子进程时,该已初始化的 CUDA 驱动环境会污染子进程的内存空间,导致子进程无法正常重新初始化 CUDA,从而触发 RuntimeError

此问题与 vllm-project/vllm #32611 的失败模式相似,均是在 fork 前发生了隐藏的 cuInit() 调用。

注意:这是一个 Triton 侧的重大行为变更。即使不通过 vLLM,直接调用 from triton.backends import backends; backends["nvidia"].driver.is_active() 也能复现该问题。

环境排查

  • Triton 版本:确认是否使用 Triton 3.8.0 或更高版本(该问题在 3.7.1 中不出现)。
  • vLLM 版本:确认是否使用包含 vllm.triton_utils 模块的 vLLM 版本。
  • PyTorch 版本:确认 PyTorch 版本及 CUDA 版本(示例环境中 PyTorch 2.13.0a0+git0dd392f, CUDA 12.9)。
  • NVIDIA 驱动版本:确认驱动版本(示例环境中为 580.82.07,System CUDA 13.0)。
  • 进程启动方式:确认 vLLM 是否通过 fork 方式启动子进程(mp.get_context("fork"))。
  • CUDA 驱动初始化痕迹:在父进程中执行 torch.cuda.is_initialized() 是否返回 False,但子进程仍报错。

解决步骤

  1. 确认 Triton 版本:运行 import triton; print(triton.__version__) 查看当前 Triton 版本。如果是 3.8.0+,则问题可能与此相关。
  2. 临时修补方案:根据 Issue 评论,PyTorch 团队已在 pytorch/pytorch #190349 中包含一个临时补丁,用于修复 torch-nightly 构建中的问题。可优先尝试升级到包含此补丁的 PyTorch 版本。
  3. 回退 Triton 版本(可优先尝试):将 Triton 回退到 3.7.1 版本,该版本中 is_active() 不会触发 CUDA 驱动初始化。
  4. 修改 vLLM 代码(临时绕过):在 vLLM 中禁用或绕过 vllm.triton_utils 模块的导入,或在调用 fork 之前避免执行 from triton.backends import backends; backends["nvidia"].driver.is_active() 这类探测代码。
  5. 等待 Triton 长期修复:跟踪 triton-lang/triton #10938 以获取最终解决方案。

验证方法

运行以下最小复现代码(来自 Issue 正文),确认子进程能否成功初始化 CUDA,不再出现 RuntimeError

import multiprocessing as mp
import os

print("parent pid", os.getpid())

import torch
print("before probe: torch.cuda.is_initialized =", torch.cuda.is_initialized())

from vllm.triton_utils import HAS_TRITON
print("HAS_TRITON", HAS_TRITON)
print("after probe: torch.cuda.is_initialized =", torch.cuda.is_initialized())

def child():
    import os
    import torch
    print("child pid", os.getpid())
    print("child before cuda init:", torch.cuda.is_initialized())
    torch.cuda.set_device(0)
    torch.empty(1, device="cuda")
    print("child ok")

if __name__ == "__main__":
    p = mp.get_context("fork").Process(target=child)
    p.start()
    p.join()
    print("exitcode", p.exitcode)
    raise SystemExit(p.exitcode)

如果修复生效,子进程应能成功打印 child ok,且 exitcode 为 0。

参考来源

vllm-project/vllm #46996

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 14077

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注