pytorch - 💡(How to fix) Fix torch.linspace with integer dtype produces inconsistent results on CPU and CUDA [1 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#181807Fetched 2026-04-29 06:10:52
View on GitHub
Comments
1
Participants
2
Timeline
11
Reactions
0
Timeline (top)
labeled ×8unlabeled ×2commented ×1

torch.linspace produces inconsistent integer results between CPU and CUDA when dtype=torch.int64.

For the same start, end, steps, and dtype, CPU and CUDA generate different integer sequences.

Root Cause

This does not appear to be a normal tolerance issue, because the requested output dtype is torch.int64.

RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Summary

torch.linspace produces inconsistent integer results between CPU and CUDA when dtype=torch.int64.

For the same start, end, steps, and dtype, CPU and CUDA generate different integer sequences.

Reproduction

import torch

cpu = torch.linspace(3.7, -3, 10, dtype=torch.int64, device="cpu")
gpu = torch.linspace(3.7, -3, 10, dtype=torch.int64, device="cuda").cpu()

print("cpu:", cpu.tolist())
print("gpu:", gpu.tolist())
print("differing indices:", (cpu != gpu).nonzero(as_tuple=True)[0].tolist())




### Versions

cpu: [3, 2, 1, 1, 0, 0, -1, -1, -2, -3]
gpu: [3, 2, 1, 0, 0, 0, 0, -1, -2, -3]
differing indices: [3, 6]

Expected behavior

CPU and CUDA should produce the same integer tensor for the same torch.linspace arguments.

Why this seems like a bug

This does not appear to be a normal tolerance issue, because the requested output dtype is torch.int64.

The discrepancy occurs in the integer values themselves:

index 3: CPU = 1,  CUDA = 0
index 6: CPU = -1, CUDA = 0

This suggests that CPU and CUDA use different rounding or casting behavior when generating integer linspace values. Since torch.linspace is a deterministic tensor construction operator, the result should ideally be backend-consistent.

PyTorch 2.11.0+cu128, CUDA 12.8

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

extent analysis

TL;DR

The discrepancy in torch.linspace results between CPU and CUDA for dtype=torch.int64 may be due to different rounding behaviors, and a potential workaround is to use a consistent rounding method.

Guidance

  • Verify that the issue persists across different PyTorch and CUDA versions to rule out version-specific bugs.
  • Consider using the torch.round function to explicitly round the results of torch.linspace to the nearest integer, ensuring consistency between CPU and CUDA.
  • Check if using dtype=torch.float64 and then casting to torch.int64 produces consistent results, which could help identify if the issue is related to integer rounding.
  • Investigate if there are any open issues or discussions in the PyTorch repository related to this specific problem, as it may have been addressed or have a known workaround.

Example

import torch

# Explicitly round the results to the nearest integer
cpu = torch.round(torch.linspace(3.7, -3, 10, device="cpu")).int()
gpu = torch.round(torch.linspace(3.7, -3, 10, device="cuda")).int().cpu()

print("cpu:", cpu.tolist())
print("gpu:", gpu.tolist())

Notes

The provided code snippet and example are based on the information given in the issue and may not fully resolve the problem but should help in identifying a consistent rounding behavior.

Recommendation

Apply workaround: Use explicit rounding with torch.round to ensure consistent results between CPU and CUDA, as this approach directly addresses the suspected cause of the issue related to different rounding behaviors.

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.linspace with integer dtype produces inconsistent results on CPU and CUDA [1 comments, 2 participants]