CUDA玄学问题?我不理解

最近跑基于Stable diffusion的代码时,遇到过几次一个神奇问题,解决方法也很玄学,全程懵,希望有大佬来指点一下。

我的目的:

在有8张显卡的服务器上跑程序,此前一直默认在cuda:0第一张显卡上跑,我想要指定另一张卡跑程序。

我的尝试:

  1. Stable diffusion看起来是通过config文件指定gpu序号的,我就先改了那里,没用,程序跑起来还是默认cuda:0

  1. 在文件中通过torch.cuda.set_device(6)指定所用显卡:

if __name__ == "__main__":
    torch.cuda.set_device(6)
    main()

报错:说cuda中6这个序号不存在

 File "scripts/inference.py", line 587, in <module>
    torch.cuda.set_device(6)
 File "anaconda3/envs/PbE/lib/python3.8/site-packages/torch/cuda/__init__.py", line 311, in set_device
    torch._C._cuda_setDevice(device)
RuntimeError: CUDA error: invalid device ordinal
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

于是我在torch.cuda.set_device(6)之前先print(torch.cuda.device_count()),发现显示可用cuda数只有一个。然而,这是一个8卡机器啊!

我别的.py文件中print(torch.cuda.device_count())可以正常得到8,在终端通过python命令,手打如下两行也能正常得到8的答案。只有我要跑的这个文件不正常。

>>>import torch
>>>print(torch.cuda.device_count())

出于直觉,我在要跑的文件一堆import中找到import torch这一行,在它之后插入一行print(torch.cuda.device_count()),结果!!!得到了8的正确答案,并且可以成功地调用torch.cuda.set_device(6),把程序丢到cuda:6去跑了。

但实在难以理解背后的原因。仅仅把print(torch.cuda.device_count())放到import torch后,就不报错了;import完其他包以后再print(torch.cuda.device_count())就有错。莫非import操作还能改变cuda可用数目不成?太玄学了吧。

  1. 直接通过终端输入CUDA_VISIBLE_DEVICES=6 python xxx.py指定显卡

发现实际跑的是cuda:1,把6改成4或者0仍然跑的是cuda:1...不指定CUDA_VISIBLE_DEVICES,直接python xxx.py,好家伙,还是cuda:1。为什么默认显卡从cuda0变成cuda1了,这什么灵异事件?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值