pytorch - 💡(How to fix) Fix float32/16/bf16 NaN/Inf → int: CPU and GPU disagree [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#180159Fetched 2026-04-12 13:23:30
View on GitHub
Comments
0
Participants
1
Timeline
59
Reactions
0
Participants
Timeline (top)
mentioned ×26subscribed ×26labeled ×7

Code Example

import torch

cases = [
    (float("nan"),  torch.int32,  "float32(nan)  -> int32"),
    (float("inf"),  torch.int32,  "float32(inf)  -> int32"),
    (float("inf"),  torch.int64,  "float32(inf)  -> int64"),
]

for val, dtype, name in cases:
    x = torch.tensor(val, dtype=torch.float32)
    cpu_v = x.to(dtype).item()
    gpu_v = x.cuda().to(dtype).cpu().item()
    print(f"{name}:  CPU={cpu_v}  GPU={gpu_v}  -> {'BUG' if cpu_v != gpu_v else 'ok'}")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch

cases = [
    (float("nan"),  torch.int32,  "float32(nan)  -> int32"),
    (float("inf"),  torch.int32,  "float32(inf)  -> int32"),
    (float("inf"),  torch.int64,  "float32(inf)  -> int64"),
]

for val, dtype, name in cases:
    x = torch.tensor(val, dtype=torch.float32)
    cpu_v = x.to(dtype).item()
    gpu_v = x.cuda().to(dtype).cpu().item()
    print(f"{name}:  CPU={cpu_v}  GPU={gpu_v}  -> {'BUG' if cpu_v != gpu_v else 'ok'}")

Versions

float32(nan) -> int32: CPU=-2147483648 GPU=0 -> BUG float32(inf) -> int32: CPU=-2147483648 GPU=2147483647 -> BUG float32(inf) -> int64: CPU=-9223372036854775808 GPU=9223372036854775807 -> BUG

2.9.0

cc @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @aditew01

extent analysis

TL;DR

The issue can be addressed by ensuring consistent handling of NaN and infinity values when converting between float and integer tensors on CPU and GPU.

Guidance

  • Verify that the PyTorch version being used is up-to-date, as the issue might be related to a known bug in version 2.9.0.
  • Check the documentation for torch.tensor() and to() methods to ensure correct usage, especially when dealing with NaN and infinity values.
  • Consider using a try-except block to catch and handle potential errors when converting between data types.
  • Test the code on both CPU and GPU to identify any inconsistencies in behavior.

Example

import torch

try:
    x = torch.tensor(float("nan"), dtype=torch.float32)
    cpu_v = x.to(torch.int32).item()
    gpu_v = x.cuda().to(torch.int32).cpu().item()
    print(f"CPU={cpu_v}  GPU={gpu_v}")
except RuntimeError as e:
    print(f"Error: {e}")

Notes

The provided code snippet and output suggest a potential issue with the conversion of NaN and infinity values between float and integer tensors on CPU and GPU. However, without further information about the expected behavior, it is difficult to provide a definitive solution.

Recommendation

Apply workaround: The code should be modified to handle NaN and infinity values explicitly, using try-except blocks or conditional statements to ensure consistent behavior on both CPU and GPU.

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