Skip to main content
 首页 » 编程设计

python之每次我在 pytorch 中使用 cuda() 将变量从 CPU 移除到 GPU 时,大约需要 5 到 10 分钟

2025年01月19日8xing901022

我只是这样做:

t = Variable(torch.randn(5)) 
t =t.cuda() 
print(t) 

但每次都需要 5 到 10 分钟。 我用cuda样本测试带宽,没问题。 然后我使用 pdb 来查找哪个花费的时间最多。 我在 /anaconda3/lib/python3.6/site-packages/torch/cuda/__init__ 中找到:

def _lazy_new(cls, *args, **kwargs): 
    _lazy_init() 
    # We need this method only for lazy init, so we can remove it 
    del _CudaBase.__new__ 
    return super(_CudaBase, cls).__new__(cls, *args, **kwargs) 

返回大约需要 5 分钟 我不知道如何通过这些信息解决我的问题。 我的环境是:Ubuntu 16.04 + CUDA 9.1

请您参考如下方法:

我的pytorch编译的cuda和我运行的cuda有cuda版本不匹配,我分了官方安装commond

conda install pytorch torchvision cuda90 -c pytorch

分为两部分:

conda install -c soumith magma-cuda90

conda install pytorch torchvision -c soumith

第二个commond默认安装了pytorch-0.2.0,对应CUDA8.0。在我将我的 pytorch 更新到 0.3.0 之后,这个 commond 只需要一秒钟。