Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: numpy.linalg.svd returns wrong results in certain architecture on numpy 1.19.3 and later #22945

Closed
jlevin opened this issue Jan 5, 2023 · 2 comments
Labels

Comments

@jlevin
Copy link

jlevin commented Jan 5, 2023

Describe the issue:

In numpy 1.19.3 or later, when using np.linalg.svd on a certain architecture, the 2nd and 3rd columns of the u result array and the 2nd and 3rd rows of the vh result arrays are switched.

Reproduce the code example:

import numpy as np
array = np.array([[-4.0, -2.0, -2.0], [-2.0, -4.0, -2.0], [-2.0, -2.0, -4.0]])
expected_u = np.array(
    [
        [-5.77350269e-01, 8.16496581e-01, -5.04179082e-17],
        [-5.77350269e-01, -4.08248290e-01, -7.07106781e-01],
        [-5.77350269e-01, -4.08248290e-01, 7.07106781e-01],
    ]
)
expected_vh = np.array(
    [
        [0.57735027, 0.57735027, 0.57735027],
        [-0.81649658, 0.40824829, 0.40824829],
        [-0.0, 0.70710678, -0.70710678],
    ]
)
(u, _, vh) = np.linalg.svd(array, full_matrices=False)
np.testing.assert_almost_equal(expected_u, u)
np.testing.assert_almost_equal(expected_vh, vh)

Error message:

Mismatched elements: 6 / 9 (66.7%)
Max absolute difference: 1.11535507
Max relative difference: 1.60871119e+16
 x: array([[-5.7735027e-01,  8.1649658e-01, -5.0417908e-17],
       [-5.7735027e-01, -4.0824829e-01, -7.0710678e-01],
       [-5.7735027e-01, -4.0824829e-01,  7.0710678e-01]])
 y: array([[-5.7735027e-01, -5.0754703e-17,  8.1649658e-01],
       [-5.7735027e-01, -7.0710678e-01, -4.0824829e-01],
       [-5.7735027e-01,  7.0710678e-01, -4.0824829e-01]])

Exited with code exit status 1

Runtime information:

1.19.3
3.9.13 (main, May 27 2022, 22:45:39)
[GCC 9.4.0]

Note: This behavior is also occurring with numpy 1.24.1

Context for the issue:

The behavior is observed in CircleCI with the following info:
Docker with Python using cimg/python:3.9.13
Model name: Intel(R) Xeon(R) Platinum 8124M CPU @ 3.00GHz

Branch featuring bug: https://github.com/lace/entente/tree/numpy-bug
Failure in CI: https://app.circleci.com/pipelines/github/lace/entente/1829/workflows/8973da1a-38f4-46a3-8dc5-63a8a1e4ee17/jobs/5937
CI config: https://github.com/lace/entente/blob/numpy-bug/.circleci/config.yml

Not reproducible on MacOS or Linux native.
Not reproducible on MacOS or Linux with same Docker image.
Not reproducible in Numpy 1.19.2.

@jlevin jlevin added the 00 - Bug label Jan 5, 2023
@rkern
Copy link
Member

rkern commented Jan 5, 2023

When you have duplicate singular values, as you do here, the SVD is not unique. The vectors associated with the duplicate singular values can be rotated freely. Different versions of the underlying linear algebra library may take different paths and return different choices in such cases. Both versions of the returned matrices are correct.

@rkern rkern closed this as completed Jan 5, 2023
@jlevin
Copy link
Author

jlevin commented Jan 5, 2023

Ah, I see. Thank you @rkern for your quick response. Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants