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

Hi,
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, torch.to(device) don’t work.

Thanks

class MyFunction(torch.autograd.Function):

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

    @staticmethod
    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 your_tensor.to() function.
Note: set a constant string value for the device is not an only option (if you want use tensor.to() for transfering to device), you may pass there a device value of some other tensor