pytorch - 💡(How to fix) Fix Bug: `torch.linalg.cond` returns `inf` instead of `nan` for NaN inputs [3 comments, 3 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#178773Fetched 2026-04-08 01:52:13
View on GitHub
Comments
3
Participants
3
Timeline
22
Reactions
0
Author
Timeline (top)
mentioned ×7subscribed ×7labeled ×4commented ×3

torch.linalg.cond produces results that are inconsistent with its documented definition when the input contains NaN.

According to the documentation, for p in ('fro', 'nuc', inf, -inf, 1, -1, 2, -2):

cond(A) = norm(A, p) * norm(inv(A), p)

However, when the input matrix contains nan, the function returns inf, the equivalent manual computation returns nan.

Root Cause

torch.linalg.cond produces results that are inconsistent with its documented definition when the input contains NaN.

According to the documentation, for p in ('fro', 'nuc', inf, -inf, 1, -1, 2, -2):

cond(A) = norm(A, p) * norm(inv(A), p)

However, when the input matrix contains nan, the function returns inf, the equivalent manual computation returns nan.

Code Example

import torch
import numpy as np
A = torch.tensor(np.array([[np.nan]]), dtype=torch.float32)

for p in [1, -1, 2, -2, float('inf'), float('-inf'), 'fro', 'nuc']:
    print(f"======p is: {p}======")
    out_cond = torch.linalg.cond(A,p)
    print(f"Condition number: {out_cond}")

    norm_a = torch.linalg.norm(A, p)
    print(f"Norm of A: {norm_a}")
    inv_a = torch.linalg.inv(A)
    print(f"Inverse of A: {inv_a}")
    norm_inv_a = torch.linalg.norm(inv_a, p)
    print(f"Norm of A^(-1): {norm_inv_a}")
    out_manual = norm_a * norm_inv_a
    print(f"Manual calculation: {out_manual}")

---

======p is: 1======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: -1======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: 2======
Condition number: nan
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: -2======
Condition number: nan
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: inf======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: -inf======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: fro======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: nuc======
Condition number: nan
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Description

torch.linalg.cond produces results that are inconsistent with its documented definition when the input contains NaN.

According to the documentation, for p in ('fro', 'nuc', inf, -inf, 1, -1, 2, -2):

cond(A) = norm(A, p) * norm(inv(A), p)

However, when the input matrix contains nan, the function returns inf, the equivalent manual computation returns nan.

Reproduction

import torch
import numpy as np
A = torch.tensor(np.array([[np.nan]]), dtype=torch.float32)

for p in [1, -1, 2, -2, float('inf'), float('-inf'), 'fro', 'nuc']:
    print(f"======p is: {p}======")
    out_cond = torch.linalg.cond(A,p)
    print(f"Condition number: {out_cond}")

    norm_a = torch.linalg.norm(A, p)
    print(f"Norm of A: {norm_a}")
    inv_a = torch.linalg.inv(A)
    print(f"Inverse of A: {inv_a}")
    norm_inv_a = torch.linalg.norm(inv_a, p)
    print(f"Norm of A^(-1): {norm_inv_a}")
    out_manual = norm_a * norm_inv_a
    print(f"Manual calculation: {out_manual}")

Actual Behavior

The output is:

======p is: 1======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: -1======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: 2======
Condition number: nan
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: -2======
Condition number: nan
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: inf======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: -inf======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: fro======
Condition number: inf
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan
======p is: nuc======
Condition number: nan
Norm of A: nan
Inverse of A: tensor([[nan]])
Norm of A^(-1): nan
Manual calculation: nan

Expected Behavior

torch.linalg.cond(A, p) should be consistent with its definition: cond(A, p) = norm(A, p) * norm(inv(A), p) Since the input is nan, the final result should also be nan.

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 @jianyuh @nikitaved @mruberry @walterddr @xwang233 @Lezcano

extent analysis

Fix Plan

To fix the inconsistency in torch.linalg.cond when the input contains NaN, we need to modify the function to handle NaN values correctly.

Here are the steps:

  • Check for NaN values in the input matrix before calculating the condition number.
  • If NaN values are found, return NaN as the condition number.

Code Changes

import torch
import numpy as np

def custom_cond(A, p):
    # Check for NaN values in the input matrix
    if torch.isnan(A).any():
        return torch.tensor(np.nan, dtype=A.dtype)
    
    # Calculate the condition number using the original formula
    norm_a = torch.linalg.norm(A, p)
    inv_a = torch.linalg.inv(A)
    norm_inv_a = torch.linalg.norm(inv_a, p)
    return norm_a * norm_inv_a

# Example usage:
A = torch.tensor(np.array([[np.nan]]), dtype=torch.float32)

for p in [1, -1, 2, -2, float('inf'), float('-inf'), 'fro', 'nuc']:
    print(f"======p is: {p}======")
    out_cond = custom_cond(A, p)
    print(f"Condition number: {out_cond}")

Verification

To verify that the fix worked, you can compare the output of the custom_cond function with the expected output for different input matrices, including those containing NaN values.

Extra Tips

  • When working with numerical computations, it's essential to handle edge cases like NaN values to ensure the correctness and reliability of the results.
  • The custom_cond function can be further optimized and refined to handle other edge cases and improve performance.

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