apex icon indicating copy to clipboard operation
apex copied to clipboard

RuntimeError: expected scalar type Float but found Half

Open superlwx opened this issue 3 years ago • 7 comments

def __init__(self, chi, cho):
    super(DeformConv, self).__init__()
    self.actf = nn.Sequential(
        nn.BatchNorm2d(cho, momentum=BN_MOMENTUM),
        nn.ReLU(inplace=True)
    )
    self.conv = DCN(chi, cho, kernel_size=(3, 3), stride=1, padding=1, dilation=1, deformable_groups=1)
def forward(self, x):
    x = self.conv(x)
    x = self.actf(x)
    return x

My model exsits a DCN module which compiled by c++. when I use amp.initialize(model, optimizer, opt_level="O1"), RuntimeError has happened(expected scalar type Float but found Half) in x=self.conv(x). I try to use x=self.conv(x.float()) to convert type, but not useful.

superlwx avatar Sep 24 '20 02:09 superlwx

I have the same problem Have you solved this problem?

ShellyLingling avatar Oct 12 '20 11:10 ShellyLingling

same problem in pytorch 1.6

Shimadaaaaa avatar Dec 03 '20 11:12 Shimadaaaaa

I have the same problem Have you solved this problem?

Check the input and convert all float16 parameters to float32, like x.float()

Shimadaaaaa avatar Dec 03 '20 13:12 Shimadaaaaa

I have the same problem Have you solved this problem?

Check the input and convert all float16 parameters to float32, like x.float()

Could you please tell me how to do it efficiently?

jsetty avatar Dec 03 '21 22:12 jsetty

i got the same error but strange thing is when i replace my nn.MSELoss to nn.BCEWithLogitsloss code runs perfectly error is coming when i use mseloss how to solve this error

arvind-nd avatar May 07 '22 09:05 arvind-nd

This error occurs when the 2 matrices you are multiplying are not of same dtype.

Half means dtype = torch.float16 while, Float means dtype = torch.float32

to resolve the error simply cast your model weights into float32

for param in model.parameters():
    # Check if parameter dtype is  Half (float16)
    if param.dtype == torch.float16:
        param.data = param.data.to(torch.float32)

RishitToteja avatar Jun 19 '23 06:06 RishitToteja

This error occurs when the 2 matrices you are multiplying are not of same dtype.

Half means dtype = torch.float16 while, Float means dtype = torch.float32

to resolve the error simply cast your model weights into float32

for param in model.parameters():
    # Check if parameter dtype is  Half (float16)
    if param.dtype == torch.float16:
        param.data = param.data.to(torch.float32)

I know I am way behind here. Could you tell me where to put this code? Thanks

Chester-droors420 avatar Jun 20 '23 23:06 Chester-droors420