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.
def forward(ctx, input):
x = np.zeros(shape)
# do something for x
# and how to copy data of x to gpu tensor input
def backward(ctx, grad_output):
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