How to put tensor on a custom Function to cuda device?

I custom a Function, in forward method. I have a numpy array x and a cuda tensor input.How to copy value of x to input tensor ? I have try, in forward method, don’t work.


class MyFunction(torch.autograd.Function):

    def forward(ctx, input):
        x = np.zeros(shape)
        # do something for x
        # and how to copy data of x to gpu tensor input
        return ...

    def backward(ctx, grad_output):

Here is an answer, that might work for you.
You should create Tensor from NumPy array and then transfer it to device

Hi, I create a tensor from numpy array. But the tensor to cuda device don’t work.

At first you should check if CUDA devices are available.
Then set the device variable with some value (e.g. 'cpu', 'cuda:0') and pass it to function.
Note: set a constant string value for the device is not an only option (if you want use for transfering to device), you may pass there a device value of some other tensor