Allocate cuda tensor in subprocess
<SCODE>import torch
import torch.multiprocessing as mp

def put_in_q():
    x = torch.IntTensor(2, 2).fill_(22)
    x = x.cuda()
    print(x)

p = mp.Process(target=put_in_q, args=())
p.start()
p.join()
<ECODE> But not this one: <SCODE>import torch
import multiprocessing as mp

class CudaProcess(mp.Process):
    def __init__(self):
        mp.Process.__init__(self)

    def run(self):
        x = torch.IntTensor(2, 2).fill_(22)
        x = x.cuda()
        print(x)

p = CudaProcess()
p.start()
p.join()
<ECODE> The error I’m getting is: <SCODE>terminate called after throwing an instance of 'THException'
  what():  cuda runtime error (3) : initialization error at .../pytorch/torch/lib/THC/THCGeneral.c:70
<ECODE>
florin
CUDA multiprocessing is quite complicated, and I wouldn’t recommend it, unless it would give you huge performance benefits. The two most important things to remember are:
apaszke
I’m not familiar with all the architecture in pytorch, but I would like to share some thoughts if I may. Wouldnt multi-threading mult-gpu work if: I guess moving all c++ part to cffi would solve 1)  and would make it even possible to use pypy (lua torch  used JIT but in python its suddenly a bad idea?)
kmichaelkills
florin
edit:
florin
UB=undefined behaviour I guess
kmichaelkills
Yeah, UB is undefined behaviour = anything could happen now. Regarding cffi, we didn’t want to add more dependencies, and having more fine grained control over GIL and some other aspects makes it easier for us to develop the C code. I’d love to support PyPy, but there are very few people using it, so it’s not really a top priority for us.
apaszke