解决Device API gpu is not enabled. Please install the cuda version of dgl.

复现RGCL遇到的错误:

Device API gpu is not enabled. Please install the cuda version of dgl. 当前安装的 DGL 是 CPU 版本,不支持 GPU ,代码试图把异构图 .to('cuda:0') 迁移到 GPU,DGL 库无 CUDA 后端直接崩溃。

解决方案

重装支持 CUDA 的 DGL,先说一下,如果已经安装了错的dgl,要先卸载,比如我装了两个错误的(一个是GPU的,一个是版本不对)

先卸载:

复制代码
pip uninstall dgl dgl-cu113 -y

正确安装代码 是:根据自己的环境选择版本,我的是0.9.1我的环境 PyTorch 1.11.0 Python 3.8(ubuntu20.04) CUDA 11.3,不知道可以问下AI

复制代码
pip install dgl-cu113==0.9.1 -f https://data.dgl.ai/wheels/repo.html --trusted-host data.dgl.ai

注意一定要加后面一场串 ,不然报错:

复制代码
(base) root@autodl-container-6d25459816-01b42b10:~/Work/ReviewGraph-main/RGCL# pip install dgl-cu113==0.7.2
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
ERROR: Could not find a version that satisfies the requirement dgl-cu113==0.7.2 (from versions: none)
ERROR: No matching distribution found for dgl-cu113==0.7.2

避坑1:一开始装错就是用了错误代码安装的是CPU版本:

使用的是:pip install dgl==0.9.1 -f https://data.dgl.ai/wheels/cu113/repo.html

避坑2:安装了不匹配自己环境的GPU版本也不可以

比如我的环境 PyTorch 1.11.0 Python 3.8(ubuntu20.04) CUDA 11.3下dgl-cu113==0.7.2不可以

,所以不能用:

pip install dgl-cu113==0.7.2 -f https://data.dgl.ai/wheels/repo.html --trusted-host data.dgl.ai

完整报错

复制代码
(base) root@autodl-container-6d25459816-01b42b10:~/Work/ReviewGraph-main/RGCL# python rgc_nd_ed.py
Namespace(dataset_name='Beauty_and_Personal_Care_5', dataset_path='/root/data/Beauty_and_Personal_Care_5/Beauty_and_Personal_Care_dedup_renamed1_renamed2.json', device=device(type='cuda', index=0), distributed=False, ed_alpha=1.0, gcn_agg_units=64, gcn_dropout=0.7, gcn_out_units=64, model_save_path='log/RGC-ND-ED_Beauty_and_Personal_Care_5_P5.pkl', model_short_name='RGC-ND-ED', nd_alpha=0.3, rating_alpha=1.0, review_feat_size=64, share_param=False, train_classification=True, train_decay_patience=20, train_early_stopping_patience=50, train_grad_clip=1.0, train_log_interval=1, train_lr=0.01, train_lr_decay_factor=0.5, train_max_iter=400, train_min_lr=0.001, train_optimizer='Adam', train_valid_interval=1)
Feature dim: 
user: (660178, 660178)
movie: (165067, 165067)
Train enc graph: 	#user:660178	#movie:165067	#pairs:901181
Train dec graph: 	#user:660178	#movie:165067	#pairs:901181
Valid enc graph: 	#user:660178	#movie:165067	#pairs:901181
Valid dec graph: 	#user:660178	#movie:165067	#pairs:36210
Test enc graph: 	#user:660178	#movie:165067	#pairs:901181
Test dec graph: 	#user:660178	#movie:165067	#pairs:36372
Loading data finished ...

Loading network finished ...

Traceback (most recent call last):
  File "rgc_nd_ed.py", line 535, in <module>
    train(config_args)
  File "rgc_nd_ed.py", line 455, in train
    dataset.train_enc_graph = dataset.train_enc_graph.int().to(params.device)
  File "/root/miniconda3/lib/python3.8/site-packages/dgl/heterograph.py", line 5448, in to
    ret._graph = self._graph.copy_to(utils.to_dgl_context(device))
  File "/root/miniconda3/lib/python3.8/site-packages/dgl/heterograph_index.py", line 237, in copy_to
    return _CAPI_DGLHeteroCopyTo(self, ctx.device_type, ctx.device_id)
  File "dgl/_ffi/_cython/./function.pxi", line 293, in dgl._ffi._cy3.core.FunctionBase.__call__
  File "dgl/_ffi/_cython/./function.pxi", line 225, in dgl._ffi._cy3.core.FuncCall
  File "dgl/_ffi/_cython/./function.pxi", line 215, in dgl._ffi._cy3.core.FuncCall3
dgl._ffi.base.DGLError: [21:30:03] /opt/dgl/src/runtime/c_runtime_api.cc:88: Check failed: allow_missing: Device API gpu is not enabled. Please install the cuda version of dgl.
Stack trace:
  [bt] (0) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x4f) [0x7fdf0fb526ef]
  [bt] (1) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dgl::runtime::DeviceAPIManager::GetAPI(std::string, bool)+0x37c) [0x7fdf0fe207dc]
  [bt] (2) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dgl::runtime::DeviceAPI::Get(DLContext, bool)+0x1f4) [0x7fdf0fe1ab44]
  [bt] (3) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dgl::runtime::NDArray::Empty(std::vector<long, std::allocator<long> >, DLDataType, DLContext)+0x15b) [0x7fdf0fe3c24b]
  [bt] (4) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dgl::runtime::NDArray::CopyTo(DLContext const&) const+0xc0) [0x7fdf0fe79000]
  [bt] (5) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dgl::UnitGraph::CSR::CopyTo(DLContext const&) const+0x259) [0x7fdf0ff992b9]
  [bt] (6) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dgl::UnitGraph::CopyTo(std::shared_ptr<dgl::BaseHeteroGraph>, DLContext const&)+0x9f) [0x7fdf0ff8a44f]
  [bt] (7) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(dgl::HeteroGraph::CopyTo(std::shared_ptr<dgl::BaseHeteroGraph>, DLContext const&)+0xf5) [0x7fdf0fe8ad65]
  [bt] (8) /root/miniconda3/lib/python3.8/site-packages/dgl/libdgl.so(+0x4e748b) [0x7fdf0fe9848b]


(base) root@autodl-container-6d25459816-01b42b10:~/Work/ReviewGraph-main/RGCL#
相关推荐
金斗潼关4 小时前
使用MLP神经网络模型预测质数
人工智能·深度学习·神经网络
心运软件8 小时前
基于深度学习的IMDB电影评论情感分析完整实现
人工智能·pytorch·深度学习·数据分析
wuling1299 小时前
李沐《动手学深度学习》(Dive into Deep Learning)d2l 安装记录
人工智能·深度学习
过期的秋刀鱼!10 小时前
项目实战-神经网络预测
人工智能·神经网络·机器学习
满怀冰雪10 小时前
09-使用 paddle.nn 构建第一个多层感知机
python·深度学习·神经网络·paddle
handsomestWei10 小时前
RNN和QKV区别
人工智能·rnn·深度学习
m沐沐11 小时前
【计算机视觉】OpenCV 人脸检测与 Haar 级联分类器——原理、实现与实战案例
人工智能·pytorch·python·深度学习·opencv·计算机视觉·人脸检测
m沐沐11 小时前
【计算机视觉】OpenCV 物体跟踪——原理、算法与CSRT跟踪器实战
人工智能·python·深度学习·opencv·算法·计算机视觉·人脸识别
LaughingZhu12 小时前
Product Hunt 每日热榜 | 2026-07-26
人工智能·深度学习·神经网络·搜索引擎·百度
STLearner13 小时前
ICML 2026 | LLM×Graph论文总结[2]【Graph4LLM,Graph4Agent,智能体记忆(Memory)
大数据·人工智能·python·深度学习·学习·机器学习·数据挖掘