convert_image_dtype
overflows with low precision floating point dtypes
#6799
Labels
convert_image_dtype
overflows with low precision floating point dtypes
#6799
While working on improving performance of
convert_image_dtype
in #6795, I found several cases whereconvert_image_dtype
is silently failing for low precision floating point dtypestorch.float16
andtorch.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 thateps
is 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
eps
is 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
eps
dependent 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
The text was updated successfully, but these errors were encountered: