pytorch - 💡(How to fix) Fix torch.fmin with CUDA out=int16 casts inputs before computation, producing incorrect result

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…

Code Example

import torch

a = torch.tensor([100000], dtype=torch.int64)
b = torch.tensor([-1], dtype=torch.int64)

out_cpu = torch.empty(1, dtype=torch.int16)
out_gpu = torch.empty(1, dtype=torch.int16, device="cuda")

torch.fmin(a, b, out=out_cpu)
torch.fmin(a.cuda(), b.cuda(), out=out_gpu)

print("expected:", torch.fmin(a, b).to(torch.int16))
print("cpu:", out_cpu)
print("gpu:", out_gpu.cpu())

# extra checks
print("cuda without out:", torch.fmin(a.cuda(), b.cuda()).cpu())
print("cuda then cast:", torch.fmin(a.cuda(), b.cuda()).to(torch.int16).cpu())
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch

a = torch.tensor([100000], dtype=torch.int64)
b = torch.tensor([-1], dtype=torch.int64)

out_cpu = torch.empty(1, dtype=torch.int16)
out_gpu = torch.empty(1, dtype=torch.int16, device="cuda")

torch.fmin(a, b, out=out_cpu)
torch.fmin(a.cuda(), b.cuda(), out=out_gpu)

print("expected:", torch.fmin(a, b).to(torch.int16))
print("cpu:", out_cpu)
print("gpu:", out_gpu.cpu())

# extra checks
print("cuda without out:", torch.fmin(a.cuda(), b.cuda()).cpu())
print("cuda then cast:", torch.fmin(a.cuda(), b.cuda()).to(torch.int16).cpu())

Versions

torch 2.11

expected: tensor([-1], dtype=torch.int16) cpu: tensor([-1], dtype=torch.int16) gpu: tensor([-31072], dtype=torch.int16)

cuda without out: tensor([-1]) cuda then cast: tensor([-1], dtype=torch.int16)

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

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 torch.fmin with CUDA out=int16 casts inputs before computation, producing incorrect result