pytorch - 💡(How to fix) Fix torch.gcd returns incorrect results due to automatic dtype casting between signed and unsigned integers [3 comments, 2 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#178593Fetched 2026-04-08 01:40:36
View on GitHub
Comments
3
Participants
2
Timeline
28
Reactions
0
Author
Participants
Timeline (top)
mentioned ×8subscribed ×8labeled ×6commented ×3

Code Example

import torch
import numpy as np
input = torch.tensor(-13, dtype=torch.int8)
other = torch.tensor(np.array([18, 11]), dtype=torch.uint8)
out = torch.gcd(input,other)
print(out)

i1 = np.int8(-13)
i2 = np.array([18, 11], dtype=np.uint8)
out = np.gcd(i1, i2)
print(out)

---

tensor([9, 1], dtype=torch.uint8)
[1 1]
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

There is an issue in torch.gcd where the input tensor is automatically cast to the same data type as the other tensor. This causes incorrect results when the input is a negative integer (int8) and other is an unsigned integer (uint8). Specifically, when input is negative, it is incorrectly cast to an unsigned integer type, leading to unexpected results in the GCD computation.

import torch
import numpy as np
input = torch.tensor(-13, dtype=torch.int8)
other = torch.tensor(np.array([18, 11]), dtype=torch.uint8)
out = torch.gcd(input,other)
print(out)

i1 = np.int8(-13)
i2 = np.array([18, 11], dtype=np.uint8)
out = np.gcd(i1, i2)
print(out)

Actual Output

tensor([9, 1], dtype=torch.uint8)
[1 1]

Versions

PyTorch version: 2.11.0+cu130 Is debug build: False CUDA used to build PyTorch: 13.0 ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64) GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 Clang version: Could not collect CMake version: Could not collect Libc version: glibc-2.31

Python version: 3.10.20 (main, Mar 11 2026, 17:46:40) [GCC 14.3.0] (64-bit runtime) Python platform: Linux-5.15.0-67-generic-x86_64-with-glibc2.31

cc @nairbv @mruberry

extent analysis

Fix Plan

To fix the issue, we need to ensure that the input tensor is not automatically cast to the same data type as the other tensor. We can achieve this by explicitly casting the other tensor to the data type of the input tensor.

Step-by-Step Solution

  • Cast the other tensor to torch.int8 to match the data type of the input tensor.
  • Compute the GCD using the casted tensors.

Example Code

import torch
import numpy as np

input_tensor = torch.tensor(-13, dtype=torch.int8)
other_tensor = torch.tensor(np.array([18, 11]), dtype=torch.uint8)

# Cast other_tensor to torch.int8
other_tensor_casted = other_tensor.to(torch.int8)

# Compute GCD using casted tensors
out = torch.gcd(input_tensor, other_tensor_casted)
print(out)

Verification

Run the example code and verify that the output is correct. The GCD of -13 and 18 is 1, and the GCD of -13 and 11 is 1.

Extra Tips

  • Be cautious when working with different data types in PyTorch, as automatic casting can lead to unexpected results.
  • Always verify the data types of tensors before performing operations to ensure correct results.

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