Skip to content

BUG: linalg.inv gives wrong result #22502

Closed

Description

@mmiletic95

Describe the issue:

At one PC, starting with numpy version 1.23.*, I have noticed that linalg.inv gives wrong result. We have tried to reproduce it on few more PCs, but we didn't succeed. Even on this PC older versions of numpy are working fine. In the code example, you can find matrices. Matrix p_inv_exp is result got with older numpy versions, while p_inv_wrong is matrix got with 1.23.* version.

PC is equipped with 12th Gen Intel(R) Core(TM) i5-12400 2.50 GHz processor.

Does anyone have an idea what the problem could be? Which PC specifics you need to know?

Reproduce the code example:

import numpy as np

p = np.array([[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
              [0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 330.0, 0.0],
              [0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0],
              [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2700.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0]])

p_inv_exp = [[-1.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0],
             [-0.0, -0.8910891089108911, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, 0.10891089108910891, -0.0],
             [-0.0, -0.0, -1.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0],
             [-0.0, -0.0, -0.0, -1.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0],
             [-0.0, -0.0, -0.0, -0.0, -1.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0],
             [-0.0, -0.0, -0.0, -0.0, -0.0, -1.0, -0.0, -0.0, -0.0, -0.0, -0.0],
             [-0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -1.0, -0.0, -0.0, -0.0, -0.0],
             [-0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -1.0, -0.0, -0.0, -0.0],
             [-0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -1.0, -0.0, -0.0],
             [0.0, 0.00033003300330033004, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.00033003300330033004, 0.0],
             [-0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -1.0]]

p_inv_wrong = [[-1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [-0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -3.30000000e+02],
               [-0.00000000e+00, -0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [-0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [-0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [-0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [-0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [-0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [-0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -1.00000000e+00, -0.00000000e+00, -0.00000000e+00],
               [ 0.00000000e+00,  3.30033003e-04,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  3.30033003e-04,  0.00000000e+00],
               [-0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, -1.00000000e+00]]



print(p@p_inv_wrong)

Error message:

No response

NumPy/Python version information:

1.23.4 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)]

Context for the issue:

No response

Activity

  1. charris commented on Oct 31, 2022

    @charris
    Member

    Same MSC version on all machines? How did you install NumPy?

  2. seberg commented on Nov 1, 2022

    @seberg
    Member

    Another piece (additionally to the two what @charris asked), can you report what:

    python -m threadpoolctl -i numpy
    

    reports? (requires installing threadpoolctl) That should tell us what BLAS/lapack backend you are using.

  3. mmiletic95 commented on Nov 1, 2022

    @mmiletic95
    Author

    UPDATE: We found one more PC with the same processor and it behaves exactly the same.

    1. Yes, on most of the machines we are using same MSC version. At these PCs (that are not working well) we tried different python versions and different MSCs, and it is still not working with numpy 1.23.*. Earlier numpy versions are working correctly.
      Some MSCs we also tried: 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)], 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:38) [MSC v.1933 64 bit (AMD64)],

    2. NumPy is installed using pip install procedure

    3. With NumPy 1.23 - not working

    [
      {
        "user_api": "blas",
        "internal_api": "openblas",
        "prefix": "libopenblas",
        "filepath": "C:\\Users\\snjezana.simic\\Documents\\GitHub\\t_sw\\venv\\Lib\\site-packages\\numpy\\.libs\\libopenblas.FB5AE2TYXYH2IJRDKGDGQ3XBKLKTF43H.gfortran-win_amd64.dll",
        "version": "0.3.20",
        "threading_layer": "pthreads",
        "architecture": "Cooperlake",
        "num_threads": 12
      }
    ]
    

    with NumPy 1.20 - working:

    [
      {
        "user_api": "blas",        
        "internal_api": "openblas",
        "prefix": "libopenblas",   
        "filepath": "C:\\Users\\tijana.ostojic\\work\\t_sw\\venv\\Lib\\site-packages\\numpy\\.libs\\libopenblas.JPIJNSWNNAN3CE6LLI5FWSPHUT2VXMTH.gfortran-win_amd64.dll",
        "version": "0.3.13.dev",
        "threading_layer": "pthreads",
        "architecture": "SkylakeX",
        "num_threads": 12
      }
    ]
    
  4. seberg commented on Nov 1, 2022

    @seberg
    Member

    @mmiletic95 , thanks. It might be a problem related to the Copperlake kernels in OpenBLAS,. Just to be sure, there is nothing like a virtual machines or so involved here, right?

    Could you try on the bad machine after first setting the environment variable:

    export OPENBLAS_CORETYPE=SkylakeX
    

    (you can rerun the threadpoolctl command to confirm that it says architecture=SkylakeX afterwards, rather than CopperLake).

  5. mmiletic95 commented on Nov 1, 2022

    @mmiletic95
    Author

    @seberg Thank you! If we set OPENBLAS_CORETYPE=SkylakeX at bad machine, everything works as expected.

  6. seberg commented on Nov 1, 2022

    @seberg
    Member

    Ok, cool. So it has to do with the Copperlake. But, one thing I am not yet certain about is whether this is actually a bug, and not just bad input plus fluctuations in numerical accuracy. Looking at:

    In [2]: np.linalg.cond(p)
    Out[2]: 2720.092049757419
    

    The matrix seems rather badly conditioned, so you may just be expecting too much and should maybe use the pseudo-inverse pinv instead?

  7. mmiletic95 commented on Nov 1, 2022

    @mmiletic95
    Author

    Thank you @seberg. Probably it is candidate for pseudo-inverse. But, the difference in results is really significant. Shouldn't result be the "same" at all "architectures"? I mean, at the same processor with just two different setups, we are getting completely different results. Shouldn't we get deterministic result even if it is hard to be computed?

  8. mattip commented on Nov 1, 2022

    @mattip
    Member

    I wonder if this is solved on OpenBLAS 0.3.21. I didn't see anything relevant in the release notes but maybe I am not reading close enough.

    We should probably update anyway, that version was released in August.

  9. seberg commented on Nov 1, 2022

    @seberg
    Member

    No, unfortunatly you cannot expect the same results, you can expect the same results within numerical accuracy, but that may be tricky thing often...
    That is a problem clearly, since it means that it can be very hard to reproduce work-flows precisely, but identical software versions are just as important as hardware here.

    OTOH, it is very possible that this is a bug (and eveven that it is long fixed). I have not thought about whether this is within the expected numerical accuracy range.

  10. mmiletic95 commented on Nov 2, 2022

    @mmiletic95
    Author

    @seberg "No, unfortunatly you cannot expect the same results, you can expect the same results within numerical accuracy, but that may be tricky thing often..." Yes, that is exactly what I wanted to say. I mean, we tested this at like 50+ PCs and everything works fine except specific PCs. Then, on these PCs, every version of NumPy except 1.23.* works fine. Furthermore, even if we just change architecture in OpenBLAS, everything works fine even on these PCs even with 1.23.*. For me, it is hard to believe that somehow magically behavior is changed in this corner case, while in all other cases everything is the same (ofc, with small differences - in range of 1e-18), meanwhile keeping all calculations under numerical tolerance. What are probabilities that we can hit exactly that behavior with certain settings and that everything stays within tolerance?

    However, we are here if we can anyhow help to pinpont exact issue, or prove that everything is withing expected numerical tolerance. Will some live debugging help?

  11. seberg commented on Nov 2, 2022

    @seberg
    Member

    @martin-frbg, do you think this is an issue with the Cooperlake kernels or rather just bad numerics here? I think inv is using dgesv in the background here.

  12. martin-frbg commented on Nov 2, 2022

    @martin-frbg

    Cooperlake kernels and parameters in OpenBLAS should be identical to SkylakeX ones except for the added BFLOAT16 GEMM.
    I'd like to see how 0.3.21 fares before investigating further (and I do not have Cooperlake hardware currently)

  13. martin-frbg commented on Nov 3, 2022

    @martin-frbg

    Actually there is a small, unwanted difference in the kernel selection - both ?ASUM and ?ROT kernels employ an assembly microkernel when the target is SKYLAKEX, but fall back to C code for Cooperlake. (Somebody forgot to expand the ifdef SKYLAKEX to its successors that use the same source file.) Not checked yet if this matters for your inv problem, but the difference in numerical accuracy is apparent in the LAPACK testsuite when I build for either TARGET on my SkylakeX

  14. martin-frbg commented on Nov 3, 2022

    @martin-frbg

    Nevertheless, I cannot reproduce the "p_inv_wrong" result with 0.3.21 on my hardware. (Compiler version /options may play a role here of course)

  15. mattip commented on Nov 17, 2022

    @mattip
    Member

    There are wheels available at the bottom of https://github.com/numpy/numpy/actions/runs/3485912390 with OpenBLAS 0.3.21. @mmiletic95 could you give the new build a go?

  16. mmiletic95 commented on Nov 17, 2022

    @mmiletic95
    Author

    @mattip Tested at 3 "bad" machines, it seems it works. Thank you!

  17. added
    57 - Close?
    on Nov 17, 2022
  18. elliot-kulakow-ent commented on Apr 17, 2023

    @elliot-kulakow-ent

    I am getting this same behavior on Sapphire Rapids with numpy 1.23.5 - which version includes the openBLAS 0.23.1 upgrade? I'm seeing the PR was merged in November but can't see if it was included in 1.24.x

  19. SamZhangQingChuan commented on Nov 20, 2023

    @SamZhangQingChuan

    Hi everyone,

    I seem to have encountered the same issue on my box and it can be replicated consistently

    • numpy: 1.23.4
    • CPU: Intel(R) Xeon(R) Platinum 8488C(Sapphire Rapids)
    • aws instance type: m7i.large
    • OpenBlas: 0.3.20

    Replicate method:

    >>> import numpy as np
    >>> mat=np.identity(24)
    >>> mat[17][15]=10
    >>> np.linalg.norm(mat@np.linalg.inv(mat))**2
    124.02 //should be 24 

    A few ways to fix(from my observations):

    1. change cpu type
    2. upgrade numpy to 1.24.0 and the openblas will be upgraded to 0.3.21
    3. export OPENBLAS_CORETYPE=SkylakeX (as per @seberg)
  20. seberg commented on Nov 21, 2023

    @seberg
    Member

    Thanks for the note, not sieving through the full discussion, but if 0.3.21 of openblas fixed it, there seems nothing actionable to do here anymore (we are not going to release new 1.23 wheeels for sure).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - Bug57 - Close?

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions