pytorch - 💡(How to fix) Fix Inconsistent Signed-Zero Handling Between CPU and CUDA Backends [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#181801Fetched 2026-04-29 06:10:57
View on GitHub
Comments
0
Participants
1
Timeline
28
Reactions
0
Participants
Timeline (top)
mentioned ×11subscribed ×11labeled ×6

Code Example

import torch
import torch.nn.functional as F

z = torch.tensor([-0.0])
p = torch.tensor([0.0])

cpu = F.relu(z)
gpu = F.relu(z.cuda()).cpu()

print("cpu relu:", cpu, "signbit:", torch.signbit(cpu), "1/x:", 1 / cpu)
print("gpu relu:", gpu, "signbit:", torch.signbit(gpu), "1/x:", 1 / gpu)

print("cpu max :", torch.maximum(z, p), "signbit:", torch.signbit(torch.maximum(z, p)))
print("gpu max :", torch.maximum(z.cuda(), p.cuda()).cpu(),
      "signbit:", torch.signbit(torch.maximum(z.cuda(), p.cuda()).cpu()))
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
import torch.nn.functional as F

z = torch.tensor([-0.0])
p = torch.tensor([0.0])

cpu = F.relu(z)
gpu = F.relu(z.cuda()).cpu()

print("cpu relu:", cpu, "signbit:", torch.signbit(cpu), "1/x:", 1 / cpu)
print("gpu relu:", gpu, "signbit:", torch.signbit(gpu), "1/x:", 1 / gpu)

print("cpu max :", torch.maximum(z, p), "signbit:", torch.signbit(torch.maximum(z, p)))
print("gpu max :", torch.maximum(z.cuda(), p.cuda()).cpu(),
      "signbit:", torch.signbit(torch.maximum(z.cuda(), p.cuda()).cpu()))

Output:

cpu relu: tensor([-0.]) signbit: tensor([True]) 1/x: tensor([-inf]) gpu relu: tensor([0.]) signbit: tensor([False]) 1/x: tensor([inf]) cpu max : tensor([-0.]) signbit: tensor([True]) gpu max : tensor([0.]) signbit: tensor([False])

Versions

PyTorch 2.11.0+cu128, CUDA 12.8

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia

extent analysis

TL;DR

The issue can be addressed by handling the difference in behavior between CPU and GPU for negative zero values in PyTorch.

Guidance

  • Verify that the issue is specific to the version of PyTorch (2.11.0+cu128) and CUDA (12.8) being used.
  • Check the documentation for torch.relu and torch.maximum to see if there are any known differences in behavior between CPU and GPU implementations.
  • Consider adding a small value to the input tensors to avoid the negative zero case, which may cause inconsistent results.
  • Test the code on different hardware configurations to see if the issue is specific to certain GPUs or CUDA versions.

Example

No code example is provided as the issue is more related to understanding the behavior of PyTorch functions on different hardware.

Notes

The behavior of PyTorch functions may vary between CPU and GPU due to differences in floating-point representation and handling of special values like negative zero.

Recommendation

Apply workaround: The best course of action would be to apply a workaround, such as adding a small value to the input tensors, to avoid the negative zero case and ensure consistent results across different hardware configurations.

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 Inconsistent Signed-Zero Handling Between CPU and CUDA Backends [1 participants]