How to use torch to speed up some common computations?
I am trying make some common computations, like matrix multiplication, but without gradient computation. An example of my computation is like 

import numpy as np
from scipy.special import logsumexp

var = 1e-8
a = np.random.randint(0,10,(128,20))
result = np.logsumexp(a, axis=1) / 2. + np.log(np.pi * var)


I want to use torch (gpu) to speed up the computation. Here is the code

import numpy as np
import torch

var = 1e-8
a = np.random.randint(0,10,(128,20))
a = torch.numpy_from(a).cuda()

result = torch.logsumexp(a, dim=1)/ 2. + np.log(np.pi*var)



but i have some questions:


Could the above code speed up the computation? I don't know if it works.
Do I need to convert all values into torch.tensor, like from var to torch.tensor(var).cuda() and from np.log(np.pi*var) to a torch.tensor?
Do I need to convert all tensors into gpu by myself, especially for some intermediate variable?
If the above code doesn't work, how can I speed up the computation with gpu?


You could use torch only to do the computations. 

   import torch
   # optimization by passing device argument, tensor is created on gpu and hence move operation is saved
   # convert to float to use with logsumexp
   a = torch.randint(0,10, (128,20), device="cuda").float()
   result = torch.logsumexp(a, dim=1)/ 2.  


Answers to your some of your questions:


  Could the above code speed up the computation? 


It depends. If you have too many matrix multiplication, using gpu can give speed up. 


  Do I need to convert all values into torch.tensor, like from var to torch.tensor(var).cuda() and from np.log(np.pi*var) to a torch.tensor?


Yes


  Do I need to convert all tensors into gpu by myself, especially for some intermediate variable?


Only leaf variables need to converted, intermediate variable will be placed on device on which the operations are done. For ex: if a and b are on gpu, then as a result of operation c=a+b, c will also be on gpu. 
