You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While working on improving performance of convert_image_dtype in #6795, I found several cases where convert_image_dtype is silently failing for low precision floating point dtypes torch.float16 and torch.bfloat16:
importtorchfromtorchvision.transformsimportfunctionalasF# torch.{float16, bfloat16} to any integer dtypeimage=torch.tensor(1.0, dtype=torch.float16)
print(image, F.convert_image_dtype(image, torch.uint8), F.convert_image_dtype(image, torch.int8))
# torch.{int32, int64} to torch.float16image=torch.tensor(2**31-1, dtype=torch.int32)
print(image, F.convert_image_dtype(image, torch.float16))
Converting an valid (b)float16 image in the value range [0.0, 1.0] to any integer dtype overflows the computation. This stems from the fact that eps is fixed:
Converting a int{32, 64} image to float16 should not be possible since it can't hold the maximum values:
>>>torch.finfo(torch.float16).max65504.0>>>torch.iinfo(torch.int16).max# ok32767>>>torch.iinfo(torch.int32).max# not ok2147483647>>>torch.iinfo(torch.int64).max# not ok9223372036854775807>>>torch.finfo(torch.bfloat16).max# bfloat does not have this issue3.3895313892515355e+38
We are already raising an error for unsafe float to int conversions
While working on improving performance of
convert_image_dtypein #6795, I found several cases whereconvert_image_dtypeis silently failing for low precision floating point dtypestorch.float16andtorch.bfloat16:Converting an valid (b)float16 image in the value range
[0.0, 1.0]to any integer dtype overflows the computation. This stems from the fact thatepsis fixed:vision/torchvision/transforms/functional_tensor.py
Lines 90 to 93 in 7a62a54
This value is simply to large for (b)float16:
The whole point of
epsis to be as small as possible to have an even value distribution. See Add convert_image_dtype to functionals #2078 (comment) for details.We could simply make
epsdependent on the input dtype in a function similar tovision/torchvision/transforms/functional_tensor.py
Line 47 in 7a62a54
Converting a int{32, 64} image to float16 should not be possible since it can't hold the maximum values:
We are already raising an error for unsafe float to int conversions
vision/torchvision/transforms/functional_tensor.py
Lines 78 to 83 in 7a62a54
so we could simply do the same here.
cc @vfdev-5 @datumbox