qpth icon indicating copy to clipboard operation
qpth copied to clipboard

torch.eig is deprecated for a long time and is being removed

Open kit1980 opened this issue 3 years ago • 2 comments
trafficstars

PyTorch's torch.eig was deprecated since version 1.9 and is being removed by https://github.com/pytorch/pytorch/pull/70982. Please use the torch.linalg.eig function instead if you want your code to continue to work with the latest PyTorch.

Affected file: https://github.com/locuslab/qpth/blob/bc121d59802d43fc6cbd685959e9156030dc1baf/qpth/qp.py#L83

kit1980 avatar Aug 26 '22 22:08 kit1980

FWIW, if you want to check that a matrix is SPD, as you're doing in that test, you should check that A = A.mT and then that linalg.eigvalsh(A) (perhaps with some tolerance)

lezcano avatar Aug 31 '22 08:08 lezcano

Also, note that linalg.eigvalsh supports batches (as all the linalg functions do) so you can remove the for loop, which should make this function much more efficient.

lezcano avatar Sep 04 '22 09:09 lezcano

Taking the advice of @lezcano, I simply had to replace the following lines of code

if check_Q_spd:
    for i in range(nBatch):
        e, _ = torch.eig(Q[i])
        if not torch.all(e[:,0] > 0):
            raise RuntimeError('Q is not SPD.')

with

if check_Q_spd:
    e, _ = torch.linalg.eig(Q)
    if not torch.all(e.real > 0):
        raise RuntimeError('Q is not SPD.')

FYI, I am using torch == 1.13.1.

acl21 avatar Feb 08 '23 17:02 acl21

Thanks all! I've merged in #48 with this update

bamos avatar Feb 08 '23 17:02 bamos

That check does not check that a matrix is SPD, as the matrix may not even be symmetric.

When looking closer at what you want to do, actually the best way of doing that would be yo try to perform a cholesky decomposition on it, and seeing if it succeeds.

lezcano avatar Feb 12 '23 11:02 lezcano

Thanks @lezcano! I just switched to trying a Cholesky to check if it's SPD: https://github.com/locuslab/qpth/commit/97b1f98794d95902411308c14d1726d826f20d9e

bamos avatar Feb 12 '23 19:02 bamos

LGTM!

lezcano avatar Feb 12 '23 22:02 lezcano