RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the ‘spawn’ start method
fork | spawn | 分叉產卵繼承非繼承從頭構建一個子進程,父進程的數據拷貝到子進程的空間中,擁有自己的Python解釋器變量具有與父進程中相同的idN在每個子進程開始時導入模塊子進程獲取在 name == main 塊中定義的變量N
解決方案
if __name__=='__main__':
torch.multiprocessing.set_start_method('spawn')
參考與更多
- 日志提示“RuntimeError: Cannot re-initialize CUDA in forked subprocess”
- https://britishgeologicalsurvey.github.io/science/python-forking-vs-spawn/
- https://www.pythonf.cn/read/65459
- https://stackoverflow.com/questions/64095876/multiprocessing-fork-vs-spawn