Check if `torch.gesv` cannot be backprogated on forward


(Changmao Cheng) #1

I don’t know how to check if LU is singular for torch.gesv. And it emits errors during backward when a singular U appears. I want to check them during forward. Any solutions?
A test example is shown below:

A = torch.tensor([[-6.05, -3.30,  5.36, -4.44,  1.08],
                      [-6.05, -3.30,  5.36, -4.44,  1.08],
                      [-6.05, -3.30,  5.36, -4.44,  1.08],
                      [-6.05, -3.30,  5.36, -4.44,  1.08],
                      [-6.05, -3.30,  5.36, -4.44,  1.08]]).t().requires_grad_()
B = torch.tensor([[4.02,  6.19, -8.22, -7.57, -3.03],
                      [4.02,  6.19, -8.22, -7.57, -3.03],
                      [4.02,  6.19, -8.22, -7.57, -3.03]]).t().requires_grad_()
X, LU = torch.gesv(B, A)
print(X)
print(LU)
print(X.sum().backward()) # wrong

The error is:

RuntimeError: Lapack Error in gesv : U(2,2) is zero, singular U. at /Users/administrator/nightlies/pytorch-1.0.0/wheel_build_dirs/wheel_3.6/pytorch/aten/src/TH/generic/THTensorLapack.cpp:155