After trying out with x.new(), it turns out it’s much slower than creating a Variable on CPU then moving it into GPU.
For example, I experimented with 3 ways of adding noise to input variable x:
Way 1:
noise =x.data.new(np.random.rand(*x.size()))
Way 2:
noise =torch.from_numpy(np.random.rand(*x.size()).astype(np.float32)).cuda()
Way 3:
noise = x.data.clone().normal_()
And then
x.data += noise
For batch size = 128, on my model way 1 runs 3 times slower than way 2 & 3 (~4s vs ~1.6s). Way3 is slightly faster than way 2.
Is this supposed to be or am I doing wrong? Besides, is there any way to get the device a tensor currently resides on?