Tensor.to() specification for out-of-range value

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]

Perhaps someone else can provide the specific answer to your question regarding the default value.

However, there is a simple way to set the value to anything you want it to be via masks:

cutoff = 0
newval = 127

t = torch.randn((100, 100), dtype=torch.float) * 127 #10000 values between -127 and 127
t[t < cutoff] = newval # mask to replace values below cutoff
tt = torch.to(t, dtype=torch.uint8)
1 Like

I think this is correct as I’ve also forwarded a similar issue showing different behavior between x86 and ARM in this numpy issue:

Float to int casts are undefined behaviour in C. And NumPy currently inherits it being undefined. Since it is undefined, it can depend a bit on how you do it or which system.

1 Like

@ptrblck Thank you for referring to your answer. I was not able to include three links in my post since I am new to this forum :slight_smile: