RuntimeError: Input type (float) and bias type (c10::Half) should be the same

3.7k views Asked by At

I know this error occurs when my model is training with GPU and I'm testing with CPU but I don't know how to fix it. Hope anyone can fix it to me. Thanks.

Here is some codes related this issue:

if torch.cuda.is_available():  
    device = torch.device("cuda") 
else: 
    device = torch.device("cpu")

img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() 
img /= 255.0 
pred = model(img, augment=augment)[0]

RuntimeError: Input type (float) and bias type (c10::Half) should be the same

Note: device = "cpu"

Change 1: img = img.to(device)
But it still have the same issue
Change 2: img = img.cuda()
RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx

1

There are 1 answers

0
Rakesh Raj On

I too came across this problem recently it might be because your model is in half precision i.e, the parameters have a 16-bit floating point representation, but the input passed through the network is not of that precision.

Seeing the code I take it that you know how to use mixed precision. The only possible problem is that anywhere in your code the inputs are not of type half or float16. Check all the inputs passed to the model, or the parameter 'half' is not handled properly.

If you still have problems just use full precision or float32 for the model you it won't affect the time of execution greatly.