pytorch - 💡(How to fix) Fix float16 cumsum GPU 11–15x less accurate [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#180150Fetched 2026-04-12 13:23:43
View on GitHub
Comments
0
Participants
1
Timeline
28
Reactions
0
Participants
Timeline (top)
mentioned ×12subscribed ×12labeled ×4

Error Message

print(f"CPU error vs float64 reference: {cpu_err:.4e} (CPU promotes f16→f32 internally)") print(f"GPU error vs float64 reference: {gpu_err:.4e} <-- BUG (GPU stays in f16)") CPU error vs float64 reference: 5.0529e-01 GPU error vs float64 reference: 5.5936e+00

Code Example

import torch
import numpy as np

torch.manual_seed(0)
n = 500_000
x = torch.randn(n, dtype=torch.float16)

ref = np.cumsum(x.numpy().astype(np.float64))
cpu = torch.cumsum(x, dim=0).numpy()
gpu = torch.cumsum(x.cuda(), dim=0).cpu().numpy()

cpu_err = float(np.max(np.abs(cpu.astype(np.float64) - ref)))
gpu_err = float(np.max(np.abs(gpu.astype(np.float64) - ref)))
ratio = gpu_err / cpu_err

print(f"N = {n:,}")
print(f"CPU error vs float64 reference: {cpu_err:.4e}   (CPU promotes f16→f32 internally)")
print(f"GPU error vs float64 reference: {gpu_err:.4e}   <-- BUG (GPU stays in f16)")
print(f"GPU is {ratio:.0f}x less accurate than CPU")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
import numpy as np

torch.manual_seed(0)
n = 500_000
x = torch.randn(n, dtype=torch.float16)

ref = np.cumsum(x.numpy().astype(np.float64))
cpu = torch.cumsum(x, dim=0).numpy()
gpu = torch.cumsum(x.cuda(), dim=0).cpu().numpy()

cpu_err = float(np.max(np.abs(cpu.astype(np.float64) - ref)))
gpu_err = float(np.max(np.abs(gpu.astype(np.float64) - ref)))
ratio = gpu_err / cpu_err

print(f"N = {n:,}")
print(f"CPU error vs float64 reference: {cpu_err:.4e}   (CPU promotes f16→f32 internally)")
print(f"GPU error vs float64 reference: {gpu_err:.4e}   <-- BUG (GPU stays in f16)")
print(f"GPU is {ratio:.0f}x less accurate than CPU")

Versions

2.9.0 N = 500,000 CPU error vs float64 reference: 5.0529e-01
GPU error vs float64 reference: 5.5936e+00

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

extent analysis

TL;DR

The issue can be mitigated by promoting the data type of the tensor on the GPU to a higher precision, such as torch.float32, to reduce the error in the cumulative sum calculation.

Guidance

  • The error is likely due to the lower precision of torch.float16 on the GPU, which can be verified by checking the data type of the tensor after it is moved to the GPU.
  • To mitigate this issue, the data type of the tensor can be promoted to torch.float32 before performing the cumulative sum calculation on the GPU.
  • The cpu_err and gpu_err values can be used to verify the effectiveness of the mitigation.
  • The ratio of gpu_err to cpu_err can be used to quantify the improvement in accuracy.

Example

gpu = torch.cumsum(x.cuda().to(torch.float32), dim=0).cpu().numpy()

Notes

The issue may still exist even after promoting the data type, due to the inherent limitations of floating-point precision. However, promoting to torch.float32 should significantly reduce the error.

Recommendation

Apply workaround: promote the data type of the tensor on the GPU to torch.float32, as this should improve the accuracy of the cumulative sum calculation without requiring a version upgrade.

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