pytorch - 💡(How to fix) Fix GPU gives wrong answer for rank-deficient lstsq [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#180157Fetched 2026-04-12 13:23:33
View on GitHub
Comments
1
Participants
2
Timeline
55
Reactions
0
Participants
Timeline (top)
mentioned ×24subscribed ×24labeled ×5closed ×1

Code Example

import torch
import numpy as np

A = torch.tensor([
    [1., 2., 3.],
    [4., 5., 6.],
    [7., 8., 9.],
], dtype=torch.float32)
B = A.clone()

# Reference: NumPy uses LAPACK gelsd (handles rank deficiency correctly)
ref = np.linalg.lstsq(A.numpy(), B.numpy(), rcond=None)[0]

cpu = torch.linalg.lstsq(A, B).solution
gpu = torch.linalg.lstsq(A.cuda(), B.cuda()).solution.cpu()

cpu_l2 = np.linalg.norm(ref - cpu.numpy())
gpu_l2 = np.linalg.norm(ref - gpu.numpy())

print("NumPy (reference):", ref[0])
print("CPU:              ", cpu[0].numpy())
print("GPU:              ", gpu[0].numpy())
print(f"CPU vs NumPy L2: {cpu_l2:.4e}")
print(f"GPU vs NumPy L2: {gpu_l2:.4e} ")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
import numpy as np

A = torch.tensor([
    [1., 2., 3.],
    [4., 5., 6.],
    [7., 8., 9.],
], dtype=torch.float32)
B = A.clone()

# Reference: NumPy uses LAPACK gelsd (handles rank deficiency correctly)
ref = np.linalg.lstsq(A.numpy(), B.numpy(), rcond=None)[0]

cpu = torch.linalg.lstsq(A, B).solution
gpu = torch.linalg.lstsq(A.cuda(), B.cuda()).solution.cpu()

cpu_l2 = np.linalg.norm(ref - cpu.numpy())
gpu_l2 = np.linalg.norm(ref - gpu.numpy())

print("NumPy (reference):", ref[0])
print("CPU:              ", cpu[0].numpy())
print("GPU:              ", gpu[0].numpy())
print(f"CPU vs NumPy L2: {cpu_l2:.4e}")
print(f"GPU vs NumPy L2: {gpu_l2:.4e} ")

Versions

NumPy (reference): [ 0.8333333 0.33333334 -0.16666667] CPU: [ 0.83333385 0.3333333 -0.1666666 ] GPU: [ 0.58141476 0.7630715 -0. ] CPU vs NumPy L2: 7.5424e-07 GPU vs NumPy L2: 1.2867e+00

cc @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia @jianyuh @nikitaved @mruberry @walterddr @xwang233 @Lezcano

extent analysis

TL;DR

The issue can be mitigated by ensuring consistent numerical stability and potentially using a different solver or configuration for GPU computations in torch.linalg.lstsq.

Guidance

  • The discrepancy between CPU and GPU results suggests a numerical stability or precision issue, potentially due to differences in how torch.linalg.lstsq is implemented on CPU versus GPU.
  • Verify that the GPU has sufficient precision and that the input matrices A and B are correctly transferred to the GPU, as indicated by the .cuda() method.
  • Consider using a different solver or configuration for GPU computations, such as specifying a different algorithm or increasing the numerical precision, if possible.
  • To further diagnose the issue, compare the results of torch.linalg.lstsq on the CPU and GPU with a simpler or more robust solver, if available.

Example

No specific code example is provided due to the lack of detailed implementation specifics in the issue description.

Notes

The issue may be related to the inherent differences in floating-point precision between CPU and GPU architectures or the specific implementation of torch.linalg.lstsq on each platform. Further investigation into the solver's configuration and numerical stability is necessary.

Recommendation

Apply a workaround by using the CPU version of torch.linalg.lstsq for critical computations or exploring alternative solvers that can provide more consistent results across different hardware platforms.

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