torch.linalg.inv_ex(A, *, check_errors=False, out=None)

Computes the inverse of a square matrix if it is invertible.

Returns a namedtuple (inverse, info). inverse contains the result of inverting A and info stores the LAPACK error codes.

If A is not an invertible matrix, or if it’s a batch of matrices and one or more of them is not an invertible matrix, then info stores a positive integer for the corresponding matrix. The positive integer indicates the diagonal element of the LU decomposition of the input matrix that is exactly zero. info filled with zeros indicates that the inversion was successful. If check_errors=True and info contains positive integers, then a RuntimeError is thrown.

Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if A is a batch of matrices then the output has the same batch dimensions.


When the inputs are on a CUDA device, this function synchronizes only when check_errors= True.


This function is “experimental” and it may change in a future PyTorch release.

See also

torch.linalg.inv() is a NumPy compatible variant that always checks for errors.

  • A (Tensor) – tensor of shape (*, n, n) where * is zero or more batch dimensions consisting of square matrices.

  • check_errors (bool, optional) – controls whether to check the content of info. Default: False.

Keyword Arguments

out (tuple, optional) – tuple of two tensors to write the output to. Ignored if None. Default: None.


>>> A = torch.randn(3, 3)
>>> Ainv, info = torch.linalg.inv_ex(A)
>>> torch.dist(torch.linalg.inv(A), Ainv)
>>> info
tensor(0, dtype=torch.int32)


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources