pytorch - 💡(How to fix) Fix CUDA torch.signbit returns false for negative float16 NaN [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#181806Fetched 2026-04-29 06:10:53
View on GitHub
Comments
0
Participants
1
Timeline
6
Reactions
0
Participants
Timeline (top)
labeled ×6

torch.signbit produces inconsistent results between CPU and CUDA for negative float16 NaN values.

The input values are constructed from the raw half-precision bit pattern 0xfe00, which represents a NaN with the sign bit set. On CPU, torch.signbit correctly returns True, while on CUDA it returns False.

Root Cause

torch.signbit produces inconsistent results between CPU and CUDA for negative float16 NaN values.

The input values are constructed from the raw half-precision bit pattern 0xfe00, which represents a NaN with the sign bit set. On CPU, torch.signbit correctly returns True, while on CUDA it returns False.

RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Summary

torch.signbit produces inconsistent results between CPU and CUDA for negative float16 NaN values.

The input values are constructed from the raw half-precision bit pattern 0xfe00, which represents a NaN with the sign bit set. On CPU, torch.signbit correctly returns True, while on CUDA it returns False.

Reproduction

import numpy as np
import torch

src = torch.from_numpy(
    np.array([0xfe00, 0xfe00, 0xfe00], dtype=np.uint16).view(np.float16)
)

cpu = torch.signbit(src)
cuda = torch.signbit(src.cuda()).cpu()

print("cpu:", cpu)
print("cuda:", cuda)

### Versions

cpu: tensor([True, True, True])
cuda: tensor([False, False, False])

Expected behavior

CPU and CUDA should return the same result. Since the input values have the sign bit set, CUDA should also return:

tensor([True, True, True])

Why this seems like a bug

This is not a numerical tolerance issue. torch.signbit is expected to test the sign bit of each input element.

The raw float16 bit pattern used here is:

0xfe00

Interpreted as IEEE 754 half precision, this is a NaN with the sign bit set. CPU detects the sign bit, while CUDA appears to treat the value as a positive NaN.

cc @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia

extent analysis

TL;DR

The inconsistency in torch.signbit results between CPU and CUDA for negative float16 NaN values can be addressed by ensuring consistent handling of NaN values across devices.

Guidance

  • Verify that the input values are correctly interpreted as NaN with the sign bit set on both CPU and CUDA.
  • Check the documentation for torch.signbit to see if there are any known issues or limitations related to NaN handling on CUDA devices.
  • Consider filing a bug report or seeking assistance from the PyTorch community if the issue persists.
  • Test the behavior with different types of input values to determine if the issue is specific to float16 NaN values.

Example

import torch

# Create a tensor with NaN values on CPU and CUDA
cpu_tensor = torch.tensor([float('nan')], dtype=torch.float16)
cuda_tensor = cpu_tensor.cuda()

# Test the behavior of torch.signbit on CPU and CUDA
cpu_result = torch.signbit(cpu_tensor)
cuda_result = torch.signbit(cuda_tensor).cpu()

print("CPU result:", cpu_result)
print("CUDA result:", cuda_result)

Notes

The issue may be related to differences in how NaN values are handled on CPU and CUDA devices. Further investigation is needed to determine the root cause of the inconsistency.

Recommendation

Apply a workaround by using a custom implementation to handle NaN values consistently across devices, as the current behavior of torch.signbit on CUDA devices appears to be incorrect.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix CUDA torch.signbit returns false for negative float16 NaN [1 participants]