This usually happens if you have not properly wrapped you main in if __name__ == '__main__': construct. Another issue might be that your project has multiple files which have that construct, and these will set the contexts when importing these files.

One option is to have only a single entry point in your project which is properly wrapped in that construct,
Or you can call the set_start_method with the argument force=True


import torch
torch.multiprocessing.set_start_method('spawn', force=True)

Hope it helps.