Where is the specification of torch.Tensor.to() for out-of-range values in the result? I cannot find the specification at torch.Tensor.to — PyTorch 2.0 documentation. This question is related to this issue.
The following conversion is from the negative value to unsigned value. Since uint8 can have a value from 0 to 255, the value -2 is out-of-range.
t = torch.tensor([-2], dtype=torch.float)
tt = torch.to(t, dtype=torch.uint8)
When I executed the following command on different platforms, I got the same result 254 for pytorch. So, I want to know where the specification, which leads to this result, exists.
On the other hand, I got different results for numpy. The numpy specialist explains that this difference comes from the undefined behavior of C language.
on x86_64
python -c "import torch; import numpy as np; vals = (2, -2); t = torch.tensor(vals, dtype=torch.float).to(torch.uint8); print(t); a = np.array(vals, dtype=np.float32).astype(np.uint8); print (a)"
tensor([ 2, 254], dtype=torch.uint8)
[ 2 254]
# on s390x
python -c "import torch; import numpy as np; vals = (2, -2); t = torch.tensor(vals, dtype=torch.float).to(torch.uint8); print(t); a = np.array(vals, dtype=np.float32).astype(np.uint8); print (a)"
tensor([ 2, 254], dtype=torch.uint8)
<string>:1: RuntimeWarning: invalid value encountered in cast
[2 0]