pythonRuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing
編輯:Python
RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the ‘spawn’ start method
fork and spawn Are different ways to build subprocesses :
fork
spawn
Bifurcation lay eggs Inherit Not inherited Build a child process from scratch , The data of the parent process is copied to the space of the child process , Own your own Python Interpreter The variable has the same... As in the parent process idN Import modules at the beginning of each child process Subprocess get at name == main Variables defined in the block N
Solution
if __name__=='__main__':
torch.multiprocessing.set_start_method('spawn')
Reference and more
The log says “RuntimeError: Cannot re-initialize CUDA in forked subprocess”