Skip to content

torch.inverse based on cuSOLVER does not raise error for singular input #46557

@IvanYashchuk

Description

@IvanYashchuk

🐛 Bug

The cuSOLVER path in torch.inverse seems not to raise an error for non-invertible input.

To Reproduce

Single matrix and 2xNxN batched input inversion is based on cuSOLVER

import torch
batch_dim = 2
A = torch.eye(3, 3, dtype=dtype, device=device)
A = A.reshape((1, 3, 3))
A = A.repeat(batch_dim, 1, 1)
A[0, -1, -1] = 0  # Now A[0] is singular
Ainv = torch.inverse(A) # Doesn't raise errors Ainv[0] contains NaNs

General batched input case is based on MAGMA

import torch
batch_dim = 3
A = torch.eye(3, 3, dtype=dtype, device=device)
A = A.reshape((1, 3, 3))
A = A.repeat(batch_dim, 1, 1)
A[0, -1, -1] = 0  # Now A[0] is singular
Ainv = torch.inverse(A) # Raises RuntimeError because input is singular

Expected behavior

torch.inverse should raise an error for singular input when input is single matrix or a batch of matrices with batch size less or equal than 2 (cuSOLVER's code path).

cc: @xwang233

cc @ngimel @vishwakftw @jianyuh @nikitaved @pearu @mruberry @heitorschueroff

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: correctness (silent)issue that returns an incorrect result silentlymodule: cudaRelated to torch.cuda, and CUDA support in generalmodule: linear algebraIssues related to specialized linear algebra operations in PyTorch; includes matrix multiply matmultriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions