Skip to content

Fix bytes_to_scalar for float/complex on RISC-V - #193334

Open
XYenChi wants to merge 1 commit into
pytorch:mainfrom
XYenChi:bytes_to_scalar_new
Open

Fix bytes_to_scalar for float/complex on RISC-V#193334
XYenChi wants to merge 1 commit into
pytorch:mainfrom
XYenChi:bytes_to_scalar_new

Conversation

@XYenChi

@XYenChi XYenChi commented Aug 13, 2026

Copy link
Copy Markdown

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.

@XYenChi
XYenChi requested a review from a team as a code owner August 13, 2026 09:32
@pytorch-bot

pytorch-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

🔗 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.

@pytorch-bot

pytorch-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 13, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: XYenChi / name: Yixuan Chen (6fcfe08)

@albanD albanD added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 15, 2026
Comment thread torch/testing/_internal/common_utils.py Outdated
Comment on lines +5988 to +5994
# 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())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support cross-dtype views, so torch.tensor(byte_list, dtype=torch.uint8, device=device).view(dtype=dtype) or similar would work?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@albanD

albanD commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

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
@XYenChi
XYenChi force-pushed the bytes_to_scalar_new branch from 6fcfe08 to 3e4cf80 Compare August 18, 2026 11:21
@XYenChi

XYenChi commented Sep 3, 2026

Copy link
Copy Markdown
Author

Also any regression test?

The regression pr:
Drop
Pick

Hero comes the regression test result, follow the link below could download the log:

Drop this patch
Pick this patch

core-failures.log could show the difference.

core-failures-pick.log
core-failures-drop.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants