【PyTorch】DataLoader 设置 num_workers > 0 时,出现 CUDA with multipr
1 报错信息RuntimeError: Caught RuntimeError in DataLoader worker process 0.
RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method
2 报错分析
2.1 原因
Tensor 默认是在 CPU 上创建的,当我在数据集 Class 的 __getitem__() 中 return 时,将 Tensor 转移到了 GPU 上
return (
color.to(self.device).type(self.dtype),
depth.to(self.device).type(self.dtype),
intrinsics.to(self.device).type(self.dtype),
pose.to(self.device).type(self.dtype),
# self.retained_inds.item(),
)
同时,我在 DataLoader 定义时,设置了 num_workers,导致数据在多历程加载时使用了 CUDA Tensor
data_loader = DataLoader(dataset, num_workers=dataset_config['num_workers'])
# Iterate over Scan
for time_idx, batch in tqdm(enumerate(data_loader)):
2.2 结论
参考文档: Link
https://i-blog.csdnimg.cn/direct/238abad8695148ab80becd1b396649e3.png#pic_center
[*]不建议在多历程加载时返回 CUDA Tensor,因为在多历程中使用 CUDA 以及共享 CUDA Tensor 有一些要留意的点
[*]相反,可以使用 DataLoader 中的 pin_memory,将数据传输到共享内存中,然后再将 Tensor 转移到 CUDA GPU 上
3 解决方法
参考discuss: Link
修改数据集 Class 的 __getitem__(),在 return 时不将 Tensor 转移到 GPU 上。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]