c/c++语言开发共享解决]RuntimeError: cuda runtime error (700) : an illegal memory access was encountered

摘要:这个error发生在我得到训练好的Decoder之后,对latent space采样并生成新数据的时候,如下RuntimeError: cuda runtime error (700) : an illegal memory access was encountered at ..atensrcTHCTHCCachingHostAllocator.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 ..atensrcTHCTHCCachingHostAllocator.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()    ~AppDataLocalContinuumanaconda3envsTF_GPUlibsite-packagestorchvisionutils.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 ..atensrcTHCTHCCachingHostAllocator.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

c/c++开发分享解决]RuntimeError: cuda runtime error (700) : an illegal memory access was encountered地址:https://blog.csdn.net/weixin_40986679/article/details/109020428

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/595609.html

(0)
上一篇 2021年5月8日
下一篇 2021年5月8日

精彩推荐