pythonRuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing
郁烨
2023-12-01
RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the ‘spawn’ start method
fork | spawn |
---|
分叉 | 产卵 |
继承 | 非继承 |
| 从头构建一个子进程,父进程的数据拷贝到子进程的空间中,拥有自己的Python解释器 |
变量具有与父进程中相同的id | |
N | 在每个子进程开始时导入模块 |
子进程获取在 name == main 块中定义的变量 | N |
解决方案
if __name__=='__main__':
torch.multiprocessing.set_start_method('spawn')
参考与更多