You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10下PyTorch无CUDA编译支持,GPU无法用于ML任务求助

PyTorch CUDA不可用问题解决(Windows 10 x64)

问题场景

运行以下代码尝试用GPU处理机器学习任务时触发错误:

import torch
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rubert-tiny2")
model = AutoModel.from_pretrained("cointegrated/rubert-tiny2")
model.cuda()  # uncomment it if you have a GPU

def embed_bert_cls(text, model, tokenizer):
    t = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
    with torch.no_grad():
        model_output = model(**{k: v.to(model.device) for k, v in t.items()})
    embeddings = model_output.last_hidden_state[:, 0, :]
    embeddings = torch.nn.functional.normalize(embeddings)
    return embeddings[0].cpu().numpy()

错误信息:

AssertionError: Torch not compiled with CUDA enabled

环境检查结果

  • torch.cuda.is_available() 返回 false
  • torch.__version__ 返回 2.0.1+cpu
  • nvcc --version 输出:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Mon_Apr__3_17:36:15_Pacific_Daylight_Time_2023
Cuda compilation tools, release 12.1, V12.1.105
Build cuda_12.1.r12.1/compiler.32688072_0
  • nvidia-smi 输出:
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 531.68                 Driver Version: 531.68       CUDA Version: 12.1     |
|-----------------------------------------+----------------------+----------------------+

已尝试操作

卸载所有pip库后执行以下命令重装,但PyTorch仍为CPU版本:

pip install torch torchvision torchaudio -f https://download.pytorch.org/whl/cu121/torch_stable.html

解决步骤(仅用pip)

  1. 彻底清理PyTorch残留

    • 卸载现有组件:
      pip uninstall -y torch torchvision torchaudio
      
    • 手动删除Python环境Lib/site-packages目录下的torch、torchvision、torchaudio文件夹(若存在)
  2. 指定CUDA版本安装GPU版PyTorch
    针对CUDA 12.1,执行以下命令安装适配版本:

    pip install torch==2.0.1+cu121 torchvision==0.15.2+cu121 torchaudio==2.0.2+cu121 --index-url https://download.pytorch.org/whl/cu121
    

    该命令明确指定了CUDA 12.1对应的GPU兼容包,避免默认安装CPU版。

  3. 验证安装
    运行以下代码确认:

    import torch
    print(torch.__version__)  # 预期输出:2.0.1+cu121
    print(torch.cuda.is_available())  # 预期输出:True
    

内容的提问来源于stack exchange,提问作者Evgeniy Shornikov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:08:20