How to use spawn function in torch.multiprocessing module

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the ‘spawn’ start method

This is the error that I get when I want to use mutiple gpus to evaluate different models using multiprocessing.

1 Like

Try mp.set_start_method('spawn', force=True) at your main; like the following:

if __name__ == '__main__':

    mp.set_start_method('spawn', force=True)

    main()