当前位置: 移动技术网 > IT编程>数据库>Access > 解决]RuntimeError: cuda runtime error (700) : an illegal memory access was encountered

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

2020年10月12日  | 移动技术网IT编程  | 我要评论
摘要:这个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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网