`torch.histc` with `out=` applies different argument checks on CPU and CUDA.
- Dominant language
- Python
- Stars
- 103k
- Forks
- 29.6k
- PR merge metrics
- PR metrics pending
Description
### 🐛 Describe the bug
`torch.histc` with `out=` applies different argument checks on CPU and CUDA.
CPU raises when the input is `float64` and `out` is `int64`; CUDA accepts the same call and writes an `int64` histogram. Without `out=`, both backends return a histogram with the input floating dtype. This is an argument-validation mismatch, not a numerical difference.
Related but different: #20208 (closed 2019) was about the *default* return dtype on CUDA vs CPU and was fixed. This report is only about `out=` validation still disagreeing.
```python
import torch
def try_histc(device):
x = torch.randn(8, dtype=torch.float64, device=device)
out = torch.empty(4, dtype=torch.int64, device=device)
try:
h = torch.histc(x, bins=4, min=-2.0, max=2.0, out=out)
return f"OK dtype={h.dtype}"
except Exception as e:
return f"{type(e).__name__}: {e}"
print("CPU :", try_histc("cpu"))
# RuntimeError: torch.histogram: input tensor and hist tensor should have the
# same dtype, but got input double and hist long int
print("CUDA:", try_histc("cuda"))
# OK dtype=torch.int64
```
Expected: the same out= dtype rule on both devices — either both reject the mismatch, or both accept it and document that out may be integer counts.
Same split on 2.13.0+cu130 and 2.8.0+cu128; CPU still raises on 2.14.0+cpu. Default histc without out= returns float64 on both backends.
### Versions
CPU arm: PyTorch 2.14.0+cpu, Python 3.12.3, Ubuntu 22.04.5 (x86_64)
CUDA arm: PyTorch 2.13.0+cu130, RTX 5090, driver 580.95.05
Also reproduces on 2.8.0+cu128 (original discovery run).
cc @ptrblck @msaroufim @eqy @tinglvv @nWEIdia @malfet
Contributor guide
Research direction
Start at the torch.histc entry point and trace how its CPU and CUDA implementations validate the out= tensor. Reproduce the float64 input and int64 output case on both devices, then add coverage showing that both backends apply the same dtype rule and preserve the documented default behavior without out=.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100