pytorch - 💡(How to fix) Fix float32 cumsum CPU exactly matches float64; GPU drifts [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#180153Fetched 2026-04-12 13:23:40
View on GitHub
Comments
0
Participants
1
Timeline
17
Reactions
0
Participants
Timeline (top)
mentioned ×6subscribed ×6labeled ×5

Error Message

print(f"CPU error vs float64: {cpu_err:.4e} ") print(f"GPU error vs float64: {gpu_err:.4e} ") CPU error vs float64: 0.0000e+00 GPU error vs float64: 6.2242e+06

Code Example

import torch
import numpy as np

torch.manual_seed(0)
x = torch.randn(10_000, dtype=torch.float32) * 1e10

ref = torch.cumsum(x.double(), dim=0).float()   # float64 reference
cpu = torch.cumsum(x, dim=0)
gpu = torch.cumsum(x.cuda(), dim=0).cpu()

cpu_err = (cpu - ref).norm().item()
gpu_err = (gpu - ref).norm().item()

print(f"Reference (float64): cumsum of N=10,000 float32 values scaled to ~1e10")
print(f"CPU error vs float64: {cpu_err:.4e} ")
print(f"GPU error vs float64: {gpu_err:.4e} ")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
import numpy as np

torch.manual_seed(0)
x = torch.randn(10_000, dtype=torch.float32) * 1e10

ref = torch.cumsum(x.double(), dim=0).float()   # float64 reference
cpu = torch.cumsum(x, dim=0)
gpu = torch.cumsum(x.cuda(), dim=0).cpu()

cpu_err = (cpu - ref).norm().item()
gpu_err = (gpu - ref).norm().item()

print(f"Reference (float64): cumsum of N=10,000 float32 values scaled to ~1e10")
print(f"CPU error vs float64: {cpu_err:.4e} ")
print(f"GPU error vs float64: {gpu_err:.4e} ")

Versions

Reference (float64): cumsum of N=10,000 float32 values scaled to ~1e10 CPU error vs float64: 0.0000e+00
GPU error vs float64: 6.2242e+06

2.9.0

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

extent analysis

TL;DR

The issue can be mitigated by using a higher precision data type, such as torch.float64, for the cumulative sum calculation on the GPU.

Guidance

  • The large error on the GPU suggests a precision issue, likely due to the use of torch.float32 for the cumulative sum calculation.
  • To verify the issue, compare the results of the cumulative sum calculation on the CPU and GPU using torch.float64 instead of torch.float32.
  • The error on the GPU can be reduced by casting the input tensor to torch.float64 before performing the cumulative sum calculation, like this: gpu = torch.cumsum(x.cuda().double(), dim=0).cpu().float().
  • The version of PyTorch being used (2.9.0) may also be a factor, but without more information, it's unclear if upgrading to a newer version would resolve the issue.

Example

gpu = torch.cumsum(x.cuda().double(), dim=0).cpu().float()

Notes

The issue may be specific to the GPU architecture or the version of PyTorch being used. Further testing would be needed to determine the root cause and the most effective solution.

Recommendation

Apply workaround: the code change suggested in the example above can reduce the error on the GPU, but it may come at the cost of increased memory usage and computation time due to the use of torch.float64.

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