Skip to content

Commit dab4757

Browse files
authored
Fix potential overflow in convert_image_dtype (#3107)
We could have errors such as aten/src/ATen/native/cpu/PowKernel.cpp:41:5: runtime error: 5.7896e+76 is outside the range of representable values of type 'float'
1 parent 0a75a0c commit dab4757

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

torchvision/transforms/functional_tensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,15 @@ def convert_image_dtype(image: torch.Tensor, dtype: torch.dtype = torch.float) -
105105
return result.to(dtype)
106106
else:
107107
input_max = _max_value(image.dtype)
108-
output_max = _max_value(dtype)
109108

110109
# int to float
111110
# TODO: replace with dtype.is_floating_point when torchscript supports it
112111
if torch.tensor(0, dtype=dtype).is_floating_point():
113112
image = image.to(dtype)
114113
return image / input_max
115114

115+
output_max = _max_value(dtype)
116+
116117
# int to int
117118
if input_max > output_max:
118119
# factor should be forced to int for torch jit script

0 commit comments

Comments
 (0)