pytorch - 💡(How to fix) Fix CPU promotes bf16→f32 for cumprod and11 GPU in bfloat16. [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#180155Fetched 2026-04-12 13:23:37
View on GitHub
Comments
0
Participants
1
Timeline
33
Reactions
0
Participants
Timeline (top)
mentioned ×13subscribed ×13labeled ×7

Error Message

print(f"CPU error vs float64 reference: {cpu_err:.4e}") print(f"GPU error vs float64 reference: {gpu_err:.4e} ") CPU error vs float64 reference: 1.6893e-02 GPU error vs float64 reference: 5.6472e-01

Code Example

import torch
import numpy as np

torch.manual_seed(0)
x = torch.randn(1000, dtype=torch.bfloat16).abs() * 0.01 + 0.99

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

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

print(f"CPU error vs float64 reference: {cpu_err:.4e}")
print(f"GPU error vs float64 reference: {gpu_err:.4e} ")
print(f"GPU is {gpu_err / cpu_err:.0f}x less accurate than CPU")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
import numpy as np

torch.manual_seed(0)
x = torch.randn(1000, dtype=torch.bfloat16).abs() * 0.01 + 0.99

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

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

print(f"CPU error vs float64 reference: {cpu_err:.4e}")
print(f"GPU error vs float64 reference: {gpu_err:.4e} ")
print(f"GPU is {gpu_err / cpu_err:.0f}x less accurate than CPU")

Versions

CPU error vs float64 reference: 1.6893e-02 GPU error vs float64 reference: 5.6472e-01 GPU is 33x less accurate than CPU

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

extent analysis

TL;DR

The issue can be mitigated by using a higher precision data type for the GPU calculation.

Guidance

  • The large difference in error between CPU and GPU calculations suggests a precision issue, likely due to the use of torch.bfloat16 on the GPU.
  • To verify the cause, try converting the GPU tensor to a higher precision type, such as torch.float32, before performing the calculation.
  • Consider using torch.cumprod with a higher precision data type, such as torch.float32 or torch.double, to reduce the error.
  • If the issue persists, investigate the specific GPU architecture and its support for torch.bfloat16 to determine if there are any known limitations or workarounds.

Example

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

Notes

The issue may be specific to certain GPU architectures or drivers, and further investigation may be needed to determine the root cause.

Recommendation

Apply workaround: use a higher precision data type, such as torch.float32, for the GPU calculation to reduce the error. This is because the torch.bfloat16 type may not provide sufficient precision for the calculation, leading to the large error difference between 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