pytorch / pytorch/pytorch

torch.fmin with CUDA out=int16 casts inputs before computation, producing incorrect result

Open
#183,391 1 comment 0 reactions 0 assignees View on GitHub
bot-triaged module: cuda module: edge cases module: type promotion topic: fuzzer triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

### 🐛 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.