It is necessary to have both the model, and the data on the same device, either CPU or GPU, for the model to process data. Data on CPU and model on GPU, or vice-versa, will result in a Runtime error.
You can set a variable device to cuda if it’s available, else it will be set to cpu, and then transfer data and model to device :
import torch
device="cuda" if torch.cuda.is_available() else 'cpu'
model.to(device)
data = data.to(device)