Fix bytes_to_scalar for float/complex on RISC-V - #193334
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/193334
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
|
|
| # Write bytes directly into storage to preserve exact bit patterns | ||
| # (e.g. NaN payloads, which are not preserved when round-tripping through | ||
| # Python float/complex, especially on architectures like RISC-V that | ||
| # canonicalize NaNs). | ||
| res = torch.empty((), dtype=dtype, device=device) | ||
| src = torch.tensor(byte_list, dtype=torch.uint8, device=device) | ||
| res.untyped_storage().copy_(src.untyped_storage()) |
There was a problem hiding this comment.
We support cross-dtype views, so torch.tensor(byte_list, dtype=torch.uint8, device=device).view(dtype=dtype) or similar would work?
There was a problem hiding this comment.
After editing with torch.tensor(byte_list, dtype=torch.uint8, device=device).view(dtype=dtype), the test result on my x86 machine is not affected by the patch. Could be merged now?
|
Also any regression test? |
bytes_to_scalar previously round-tripped raw bytes through Python
float/complex values (via ctypes) before constructing the tensor. This
loses NaN bit patterns on architectures (such as RISC-V) that
canonicalize NaNs in floating-point loads/conversions, causing
test_bytes_to_scalar_cpu_{float32,float64,complex64,complex128} to
fail with mismatched storage bytes.
Construct the scalar tensor by reinterpreting the raw bytes as the
target dtype via tensor.view(dtype), so all input bit patterns
(including NaN payloads) are preserved exactly.
Test Plan:
python test/test_torch.py TestTorchDeviceTypeCPU.test_bytes_to_scalar_cpu_complex64
6fcfe08 to
3e4cf80
Compare
Hero comes the regression test result, follow the link below could download the log: Drop this patch core-failures.log could show the difference. |
bytes_to_scalar previously round-tripped raw bytes through Python float/complex values (via ctypes) before constructing the tensor. This loses NaN bit patterns on architectures (such as RISC-V) that canonicalize NaNs in floating-point loads/conversions, causing test_bytes_to_scalar_cpu_{float32,float64,complex64,complex128} to fail with mismatched storage bytes.
Construct the scalar tensor by writing the raw bytes directly into its untyped storage so all input bit patterns (including NaN payloads) are preserved exactly.
Tested python test/test_torch.py TestTorchDeviceTypeCPU.test_bytes_to_scalar_cpu_complex64 with sg2044, pass.