pytorch - 💡(How to fix) Fix Inconsistent logdet behavior between CPU and GPU on near-singular matrices (Ada GPU) [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#180637Fetched 2026-04-17 08:25:49
View on GitHub
Comments
3
Participants
2
Timeline
35
Reactions
0
Participants
Timeline (top)
mentioned ×13subscribed ×13labeled ×5commented ×3

Code Example

import torch
import torch.nn as nn

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(8, 16)
        self.fc2 = nn.Linear(16, 4)

    def forward(self, x):
        y = self.fc1(x)
        v = torch.arccosh(y + 2)
        u = self.fc2(v)
        return torch.logdet(u), u.reciprocal_()

torch.manual_seed(42)
m_cpu = Model().eval()
torch.manual_seed(42)
m_gpu = Model().cuda().eval()
m_gpu.load_state_dict(m_cpu.state_dict())

x = torch.full((4, 8), 0.4033372700214386)

with torch.no_grad():
    cpu_log, cpu_u = m_cpu(x)
    gpu_log, gpu_u = [o.cpu() for o in m_gpu(x.cuda())]

print("CPU logdet:", cpu_log.item())
print("GPU logdet:", gpu_log.item())
print("CPU is inf:", torch.isinf(cpu_log).item(), "GPU is nan:", torch.isnan(gpu_log).item())

---

CPU logdet: -inf
  GPU logdet: -108.34196472167969
  CPU is inf: True GPU is nan: False
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
import torch.nn as nn

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(8, 16)
        self.fc2 = nn.Linear(16, 4)

    def forward(self, x):
        y = self.fc1(x)
        v = torch.arccosh(y + 2)
        u = self.fc2(v)
        return torch.logdet(u), u.reciprocal_()

torch.manual_seed(42)
m_cpu = Model().eval()
torch.manual_seed(42)
m_gpu = Model().cuda().eval()
m_gpu.load_state_dict(m_cpu.state_dict())

x = torch.full((4, 8), 0.4033372700214386)

with torch.no_grad():
    cpu_log, cpu_u = m_cpu(x)
    gpu_log, gpu_u = [o.cpu() for o in m_gpu(x.cuda())]

print("CPU logdet:", cpu_log.item())
print("GPU logdet:", gpu_log.item())
print("CPU is inf:", torch.isinf(cpu_log).item(), "GPU is nan:", torch.isnan(gpu_log).item())

Versions

I observe a consistent discrepancy between CPU and GPU when computing logdet on near-singular matrices.

Environment:

  • PyTorch: 2.9.1+cu128
  • CUDA: 12.8
  • GPU: NVIDIA RTX 6000 Ada (sm_89)
  • CPU: AMD Threadripper PRO 7985WX

On my setup, the issue is reproducible across runs.

CPU logdet: -inf
  GPU logdet: -108.34196472167969
  CPU is inf: True GPU is nan: False

Since both paths operate on identical inputs, this suggests a difference in numerical handling (e.g., LU factorization or reduction order) rather than simple floating-point noise.

This may be particularly relevant for near-singular matrices, where logdet is highly sensitive.

Question: Should CPU and GPU follow consistent behavior in this regime (e.g., both underflow or both return finite values)?

This could affect downstream applications relying on log-determinant computations.

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

extent analysis

TL;DR

The discrepancy between CPU and GPU logdet results for near-singular matrices may be due to differences in numerical handling, suggesting a need for consistent behavior or careful handling of such cases.

Guidance

  • Investigate the numerical stability and handling of near-singular matrices in PyTorch's logdet computation on both CPU and GPU to understand the source of the discrepancy.
  • Consider implementing a check for near-singularity before computing logdet to handle such cases consistently across devices.
  • Review downstream applications relying on log-determinant computations to assess potential impacts of this discrepancy and plan for mitigation strategies.
  • Evaluate the use of alternative numerical methods or libraries that may offer more consistent or robust handling of near-singular matrices.

Example

No specific code example is provided as the issue is more related to understanding and addressing the numerical discrepancy rather than fixing a particular code snippet.

Notes

The discrepancy observed may be specific to the versions of PyTorch and CUDA used, as well as the hardware (NVIDIA RTX 6000 Ada and AMD Threadripper PRO 7985WX). Testing with different versions or hardware may yield different results.

Recommendation

Apply workaround: Given the potential for near-singular matrices to cause discrepancies in logdet computations between CPU and GPU, applying a workaround such as checking for near-singularity and handling it consistently may be necessary to ensure reliable downstream computations.

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