both sampling modes work fine in 32 bit training.
My inputs and the grid does not contain NaNs or Infs , what could be the possible reason for this and the solution.
Could you post the shapes as well as the min. and max. values for input and grid so that we can reproduce this issue, as this code works fine in float16:
@ptrblck thanks for the kind feedback.
Actually on investigation Grid infact does have the inf as maximum value, sorry for the incorrect information above.
Then does it have undefined behavior or the following still applies according to the documentation ?
If grid has values outside the range of [-1, 1], the corresponding outputs are handled as defined by padding_mode. Options are ....
It depends on your use case what the valid workaround would be.
I.e. why are Inf values in the grid and what would they mean?
If you think these grid values should use the padding, your workaround might work, on the other hand you might want to investigate why these Inf values are created and avoid them.
float16 can easily overflow if you are using values with a value close to the min. and max. values:
torch.finfo(torch.float16).max
> 65504.0
E.g. this code snippet overflows in the second approach and yields Infs in the result after applying torch.mm:
x = torch.randn(1024, 1024, device='cuda').half()
y = torch.mm(x, x)
print(torch.isfinite(y).all())
> tensor(True, device='cuda:0')
x = torch.randn(1024, 1024, device='cuda').half() * 2**13
print(torch.isfinite(x).all())
> tensor(True, device='cuda:0')
y = torch.mm(x, x)
print(torch.isfinite(y).all())
> tensor(False, device='cuda:0')
Since the grid would contain valid values in [-1, 1], this would usually not be a problem.
However, it seems you are expecting to work with large values (which would then use the padding values), so you might disable autocast for the grid sampling operation and the grid creation.
thanks so much for the suggestion of disabling autocast , that indeed is nice solution.
and also i found out that it depends on the input data , it overflows for certain datasets (with larger values for camera intrinsics values for example.)
Automatic mixed-precision is not supported yet in torchscript and we are working on it.
If your model doesn’t use data dependent conditions etc. you might be able to use tracing (although you should verify the correctness).