欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决]RuntimeError: cuda runtime error (700) : an illegal memory access was encountered

程序员文章站 2021-12-08 19:22:37
摘要:这个error发生在我得到训练好的Decoder之后,对latent space采样并生成新数据的时候,如下RuntimeError: cuda runtime error (700) : an illegal memory access was encountered at ..\aten\src\THC\THCCachingHostAllocator.cpp:278问题出在如何把数据sample存储在gpu上:torch.tensor(sample, device=device)。其中de...

摘要:
这个error发生在我得到训练好的Decoder之后,对latent space采样并生成新数据的时候,如下

RuntimeError: cuda runtime error (700) : an illegal memory access was encountered at ..\aten\src\THC\THCCachingHostAllocator.cpp:278 

问题出在如何把数据sample存储在gpu上:torch.tensor(sample, device=device)。其中device变量为cuda:0。\

正文:
看了很多别人的问题,发现可能是我生成的sample没有存储在GPU上,所以对cuda来说储存在CPU上的数据就是illegal memory access,非法存储访问。代码和Traceback如下:

# we will sample N embeddings, then decode and visualize them def vis_samples(model): ####################################### TODO ####################################### #################################################################################### sampled_embeddings = torch.FloatTensor(batch_size,nz).normal_() decoded_samples = model.decoder(sampled_embeddings) # decoder output images for sampled embeddings #################################### END TODO ###################################### fig = plt.figure(figsize = (10, 10)) ax1 = plt.subplot(111) ax1.imshow(torchvision.utils.make_grid(decoded_samples[:16], nrow=4, pad_value=1.)\ .data.cpu().numpy().transpose(1, 2, 0), cmap='gray') plt.show() vis_samples(ae_model) 
RuntimeError                              Traceback (most recent call last)
<ipython-input-13-09e84dc7a8e5> in <module>
     24     plt.show()
     25 
---> 26 vis_samples(ae_model)

<ipython-input-13-09e84dc7a8e5> in vis_samples(model)
     20     fig = plt.figure(figsize = (10, 10))
     21     ax1 = plt.subplot(111)
---> 22     ax1.imshow(torchvision.utils.make_grid(decoded_samples[:16], nrow=4, pad_value=1.)\
     23                 .data.cpu().numpy().transpose(1, 2, 0), cmap='gray')
     24     plt.show()

~\AppData\Local\Continuum\anaconda3\envs\TF_GPU\lib\site-packages\torchvision\utils.py in make_grid(tensor, nrow, padding, normalize, range, scale_each, pad_value)
     53 
     54     if tensor.dim() == 4 and tensor.size(1) == 1:  # single-channel images
---> 55         tensor = torch.cat((tensor, tensor, tensor), 1)
     56 
     57     if normalize is True:

RuntimeError: cuda runtime error (700) : an illegal memory access was encountered at ..\aten\src\THC\THCCachingHostAllocator.cpp:278 

于是我把生成sample的代码改为

 ####################################### TODO ####################################### #################################################################################### sampled_embeddings = torch.FloatTensor(batch_size,nz).normal_() decoded_samples = model.decoder(torch.tensor(sampled_embeddings, device=device)) #################################### END TODO ###################################### 

所以问题的关键在于如何让sample存储在gpu上:torch.tensor(sample, device=device)。其中device变量为cuda:0

本文地址:https://blog.csdn.net/weixin_40986679/article/details/109020428