相关文章推荐
近视的咖啡  ·  报错解决:CUDA Runtime ...·  18 小时前    · 
愤怒的西瓜  ·  cuda an illegal ...·  18 小时前    · 
冲动的梨子  ·  CUDA kernel failed : ...·  18 小时前    · 
行走的眼镜  ·  PyramidCU::GenerateFea ...·  18 小时前    · 
暴躁的李子  ·  Cuda ...·  18 小时前    · 
精明的铁链  ·  揭秘Python ...·  1 月前    · 

【解决bug】RuntimeError: cuda runtime error (77) : an illegal memory access was encountered at XXXXX

【解决bug】RuntimeError: cuda runtime error (77) : an illegal memory access was encountered at XXXXX

项目场景:

​虚拟试穿​ ​项目训练:

项目场景:数据集准备完成,构建模型开始训练时,模型内部编码数据时:

​input_label, masked_label, all_clothes_label = self.encode_input(label, clothes_mask, all_clothes_label)​ ​报错:

​RuntimeError: cuda runtime error (77) : an illegal memory access was encountered at XXXXXX​

def encode_input(self, label_map, clothes_mask, all_clothes_label):
size = label_map.size()
oneHot_size = (size[0], 14, size[2], size[3])
input_label = torch.cuda.FloatTensor(torch.Size(oneHot_size)).zero_()
input_label = input_label.scatter_(1, label_map.data.long().cuda(), 1.0)
# print(label_map.shape)
masked_label = torch.cuda.FloatTensor(torch.Size(oneHot_size)).zero_()
masked_label = masked_label.scatter_(1, (label_map * (1 - clothes_mask)).data.long().cuda(), 1.0)

c_label = torch.cuda.FloatTensor(torch.Size(oneHot_size)).zero_()
c_label = c_label.scatter_(1, all_clothes_label.data.long().cuda(), 1.0)

input_label = Variable(input_label)

return input_label, masked_label, c_label

问题描述:

​RuntimeError: cuda runtime error (77) : an illegal memory access was encountered at XXXXXX​

运行时出错:CUDA运行时错误(77):在以下位置遇到了非法的内存访问。

masked_label = masked_label.scatter_(1, (label_map * (1 - clothes_mask)).data.long().cuda(), 1.0)  # [1, 1, 256, 192],[1, 1, 256, 192]

发生运算的两个tensor不在一个数量级,或者运算结果发生越界。


原因分析:

RuntimeError:运行时检查出来的错误。

​an illegal memory access was encountered at 地址​ ​:内存非法访问,属于数据出了问题。

定位问题在:​ ​(label_map * (1 - clothes_mask)​

检查发现,是​ ​label_map​ ​的元素有问题,它与​ ​clothes_mask​ ​来自输入数据集,追溯,就是数据集组成batch前,‘label’标签读取数据出了问题。shape是正确的,是值的问题,因为没有标准化。

此处:​ ​label_tensor = transform_A(label_data)​


解决方案:

提示:具体解决方案:

解决办法:将运算的两个tensor的数量级统一,以免运算结果越界。

​abel_tensor = transform_A(label_data)* 255.0​

注,如果你不能判断你的bug是不是这类型,可以先排查,​ ​排查RuntimeError: cuda runtime error (77)bug请参见bolg​ ​。