Skip to content

np.linalg.eigvals() unexpected results on ARM CPUs #18422

Closed

Description

@millettmb

Reproducing code example:

  1. Procure 2x EC2 instances, ideally a t2.micro (x86) and t4g.micro (ARM) running Ubuntu 20.04 LTS (freetier)
  2. sudo apt update && sudo apt install python3-pip && pip3 install numpy to install requirements
  3. Open a python3 interactive shell to run the following code:
import numpy as np
values = [-20, 2, 3, 4, 5, 4, 3, 2]
values = values[int(non_zero[0]):int(np.non_zero[-1])+1]
_, e = np.frexp(values[::-1])
c = int(-0.5 * (np.max(e) + np.min(e)))
p = values[::-1]
p = np.ldexp(p, c)
A = np.diag(np.full(p.size - 2, p[0]), k=-1)
A[0,:] = -p[1:]
eigenvalues = np.linalg.eigvals(A)
print(eigenvalues)

(All values up until the eigvals function call are equal on both machines)

On the ARM based CPU, you will see the following output:
[-0.14294258 -0.05014252 -0.06678331 -0.06036967 0.01308302 -0.29849543 0.23065051]

On the x86 based CPU, you will see the following output:
[-0.35561591+0.14999412j -0.35561591-0.14999412j -0.10977392+0.35797579j -0.10977392-0.35797579j 0.15723786+0.31047243j 0.15723786-0.31047243j 0.24130395+0.j ]

There is no stack trace or error thrown.

Error:

I believe that the eigenvalues produced on the ARM CPU are incorrect, as they do not include complex conjugate pairs (with real number input). It's been a while since I've studied linear algebra, but I believe this to be the case.

I found this error originally due to an issue I encountered with the numpy_financial package, which calls the np.linalg.eigvals function from the irr function. More information on the issue over there: numpy/numpy-financial#37

NumPy/Python version information:

Both machines running Ubuntu 20.04 LTS, kernel 5.4.0-1047-aws. Python version 3.8.5. Numpy version 1.20.1.

Activity

  1. millettmb commented on Feb 16, 2021

    @millettmb
    Author

    So after a bit more tinkering, I've found out a way to solve this issue. You'll need to follow along with the guide here: https://github.com/aws/aws-graviton-getting-started/blob/master/python.md.

    1. If you already have numpy installed through pip, uninstall it using pip3 uninstall numpy
    2. Run the following code:
    sudo apt update
    sudo apt-get install build-essential python3-pip python3-dev libblas-dev gfortran liblapack-dev
    python3 -m pip install --user --upgrade pip
    sudo apt -y install python3-scipy python3-numpy libopenblas-dev libblis-dev
    sudo update-alternatives --set libblas.so.3-aarch64-linux-gnu \
        /usr/lib/aarch64-linux-gnu/blis-openmp/libblas.so.3
    
    1. Install numpy_financial if you haven't already, this will use the numpy version we have just installed
    2. Open a Python interactive shell and enter the following code to test that it's been installed properly
    import numpy_financial
    numpy_financial.irr([-20, 2, 3, 4, 5, 4, 3, 2])
    

    If all has gone well, you should get a result of 0.03603774756546341. If it's been installed incorrectly, you'll get a value of 0.08389095415117676.

    How should we best inform the ARM user base of this issue? Should we include some information on the numpy install guide (https://numpy.org/install)? Currently there's no information there about ARM based installations, and installing numpy following the guide there on ARM CPUs is susceptible to reproducing the issue listed above.

  2. millettmb commented on Feb 16, 2021

    @millettmb
    Author

    I've found that there is a specific repo for the numpy website, and have since moved the discussion to the issue listed here numpy/numpy.org#401.

  3. mattip commented on Feb 16, 2021

    @mattip
    Member

    I could not reproduce on an ARM64 machine. I used a virtualenv instead of installing via sudo pip3, which is really to be avoided. After deleting line 3 of your script, and then doing

    sudo apt install virtualenv
    virtualenv -p python3.7 venv3
    venv3/bin/python -m pip install numpy==1.20
    <run script>
    

    I got the correct complex output. I am confused why updating the system openblas library had any effect: we ship our own OpenBLAS in the wheel. When you do ldd $(venv3/bin/python -c "import numpy; print(numpy.core._multiarray_umath.__file__)") you should see

    • that it is using numpy from inside the virtualenv
    • that it links to numpy.libs/libopenblasp-r0-3c76c92e.3.13.dev.so, which is OpenBLAS 0.3.13 with a mangled name.

    Are you sure you are using the pip-installed numpy and not the system one?

  4. rgommers commented on Apr 5, 2021

    @rgommers
    Member

    Graviton is a custom chip from Amazon that AFAIK is only available on AWS. It does however look like pip will install aarch64 manylinux wheels from PyPI. And apparently OpenBLAS has an issue with the Graviton chip.

    I don't think it's reasonable for us to have specific install instructions for an AWS-specific chip on https://numpy.org/install/, especially since our wheels are supposed to just work. There is something really broken here though it looks like.

  5. rgommers commented on Apr 5, 2021

    @rgommers
    Member

    @millettmb if you could try the following, that'd be helpful:

    1. Install numpy with pip install numpy (use a separate virtualenv if needed to not mess up your working install).
    2. Run python -c "import numpy as np; np.test()" and report the output.
  6. boegel commented on May 24, 2021

    @boegel

    I'm seeing the same problem with numpy 1.20.3 using GCC 10.3, binutils 2.36.1, Python 3.9.5, OpenBLAS 0.3.15.

    I can reproduce the issue with the np.linalg.eigvals test script provided by @millettmb: on Intel and AMD systems I'm getting the correct result ([[-0.35561591...), likewise on POWER9.
    On an AWS Graviton2 instance (c6gd.2xlarge), I'm getting the wrong result ([-0.14294258 ...).

    54 tests in the numpy test suite fail (see numpy-1.20.3_tests_graviton2.txt), most of them with LinAlgError: Eigenvalues did not converge or LinAlgError: SVD did not converge in Linear Least Squares (not sure if those could have the same underlying cause?).

    The output also includes messages like this 51 times:

      ** On entry to DLASCL parameter number  4 had an illegal value
    

    (sometimes it's parameter number 5)

    Thanks to linking numpy with FlexiBLAS I can easily switch to a different BLAS library. When using BLIS 0.8.1, I'm not seeing the problem: the np.linalg.eigvals test case produces the correct result, and the numpy test suite passes fully.
    That tells me the problem is in OpenBLAS 0.3.15.

    Maybe @martin-frbg has any ideas here?

  7. martin-frbg commented on May 24, 2021

    @martin-frbg

    Graviton2 (NEOVERSEN1 target in OpenBLAS) is using the same GEMM kernels as most other supported ARMV8 cpus (originally written for ThunderX2T99 and provided by someone from Cavium in 2017/18). Unfortunately it is not covered by any of the current CI setups, I will need to look into getting AWS instances set up. The DLASCL errors suggest that NaNs are generated somewhere - as the input seems to be valid, perhaps things are getting miscompiled ?

  8. mattip commented on May 24, 2021

    @mattip
    Member

    @seiko2plus any thoughts?

  9. boegel commented on May 24, 2021

    @boegel

    @martin-frbg I can put you in touch with somebody at AWS who may be able to provide some free credits for testing on Graviton2, send me an email if you're interested.

  10. martin-frbg commented on May 24, 2021

    @martin-frbg

    @boegel thx - I belatedly remembered that there is/was Graviton2 access on Travis so if/while that is still avaiable I'll try to reproduce the issue there first. (I do notice that the regular LAPACK testsuite run does not look particularly bad there)

  11. seiko2plus commented on May 26, 2021

    @seiko2plus
    Member

    First, the issue appears in (neoverse-n1) processors, while other processors such as (Cortex-A53) do not show any errors. It seems the errors occur if one of the following SIMD features is available (asimdhp asimdrdm asimddp).

    Secondly, By testing LAPACKE_dgeev with args (LAPACK_ROW_MAJOR, 'N', 'N', ...) on Cortex-A53 returned zero while neoverse-n1 was returning the number of dimensions.

    Third, I tried to rebuild OpenBLAS with option DYNAMIC_ARCH on different versions of GCC but I was not able to reproduce test errors on neoverse-n1 and LAPACKE_dgeev was working just fine on both processors.

    Eventually, I think the problem is not general, and it might be specific to the compiler who built it or, with the compiler build options. I wonder if someone could provide me the version of the compiler that built the library and the build log so I could be able to reproduce the errors.

  12. 7 remaining items

  13. ggouaillardet commented on Jun 14, 2021

    @ggouaillardet

    yeah, I spotted some inline assembly that does not restore a register as it should ...

  14. ggouaillardet commented on Jun 14, 2021

    @ggouaillardet
    --- orig/OpenBLAS-0.3.15/kernel/arm64/dznrm2_thunderx2t99.c     2021-05-03 06:50:22.000000000 +0900
    +++ OpenBLAS-0.3.15/kernel/arm64/dznrm2_thunderx2t99.c  2021-06-14 15:54:26.087232321 +0900
    @@ -321,7 +321,7 @@
            : "cc",
              "memory",
              "x0", "x1", "x2", "x3", "x4", "x5", "x6",
    -         "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"
    +         "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", REGINF
            );
     
     }

    fixed one bug ... I am now checking if there are some remaining issues

  15. AGSaidi commented on Jun 16, 2021

    @AGSaidi

    @martin-frbg TravisCI does support Graviton as does CircleCI. There are instructions for using it here. I'm also happy to help get you access to a Graviton instance directly.

    Similarly, we're happy to send a PR for enabling testing with one of those CI services on Arm systems.

  16. martin-frbg commented on Jun 16, 2021

    @martin-frbg

    I was misled by the OpenBLAS Ci job still using travis-ci.org rather than .com (and the "Graviton" job silently running on Intel as a consequence), and I still need Xianyi to make administrative changes to CI setups (like initiating the migration to travis-ci.com) as the github project is still "owned" by him alone.

  17. AGSaidi commented on Jun 16, 2021

    @AGSaidi

    I ran make lapack-test manually having built both the NEOVERSEN1 target and the ARMV8 generic target on a 64c graviton2 system.

    For the n1 target there is one failure:

    Testing REAL              RFP-linear-equation-routines-LIN/xlintstrfs < stest_rfp.in > stest_rfp.out   STFSM auxiliary routine:     1 out of  7776 tests failed to pass the threshold
     passed: 5352
    failing to pass the threshold: 1
    
    			-->   LAPACK TESTING SUMMARY  <--
    SUMMARY             	nb test run 	numerical error   	other error
    ================   	===========	=================	================
    REAL             	1308369		1	(0.000%)	0	(0.000%)
    DOUBLE PRECISION	1316957		0	(0.000%)	0	(0.000%)
    COMPLEX          	776316		0	(0.000%)	0	(0.000%)
    COMPLEX16         	777128		0	(0.000%)	0	(0.000%)
    
    --> ALL PRECISIONS	4178770		1	(0.000%)	0	(0.000%)
    
    
  18. martin-frbg commented on Jun 16, 2021

    @martin-frbg

    There is an elusive single failure in REAL on most systems (can't remember right now what it is), usually accompanied by a single failure in the corresponding COMPLEX function - as far as I can tell, these may be actual inaccuracies in the respective tests.
    The carnage for GENERIC (I assume ARMV8 ?) looks like something fundamental went wrong - this should be almost all plain C kernels. I'll see if I can reproduce it on local hardware. (Reporting more numerical errors than tests run does not make much sense either, I wonder if the python script just went haywire)

  19. AGSaidi commented on Jun 16, 2021

    @AGSaidi

    (I assume ARMV8?)

    Correct:

    make TARGET=ARMV8
    
  20. martin-frbg commented on Jun 16, 2021

    @martin-frbg

    Should be nowhere near as bad (it is not in any of the CI runs, but I do run it semi-regularly locally). And the one in REAL on Graviton should be STFSM I think - if it is what I call the "usual" one... would you happen to have the testing_results.txt available ?

  21. AGSaidi commented on Jun 16, 2021

    @AGSaidi

    Tried to replicate the issue with TARGET=ARMV8 and they all passed but one. I'm trying to figure out what i did the first time around, but sorry about the noise.

    Both ARMV8 and the NEOVERSEN1 target fail on this test.

    ARMV8 output:

     *** Error(s) or Failure(s) while testing STFSM               ***
          Failure in STFSM, CFORM='T', SIDE='L', UPLO='U', TRANS='T', DIAG='N', M= 50, N = 50, test=  42.299
      STFSM auxiliary routine:     1 out of  7776 tests failed to pass the threshold
    

    Neoverse N1 output:

      *** Error(s) or Failure(s) while testing STFSM               ***
          Failure in STFSM, CFORM='T', SIDE='L', UPLO='U', TRANS='T', DIAG='N', M= 50, N = 50, test=  33.319
      STFSM auxiliary routine:     1 out of  7776 tests failed to pass the threshold
    
  22. martin-frbg commented on Jun 16, 2021

    @martin-frbg

    Most likely culprit would be a mismatch of gcc and (lib)gfortran, that would usually be blowing up left and right due to ABI mismatch. I can discuss the STFSM thing with the Reference-LAPACK people if you like, but IIIRC from the documentation of the tests and earlier discussions, a serious test failure would be orders of magnitude away from the threshold, not something like 42 instead of an expected 20 (again IIRC)

  23. mattip commented on Jul 27, 2021

    @mattip
    Member

    @millettmb could you update this issue after trying the latest release 1.21.1 with OpenBLAS v0.3.17?

  24. boegel commented on Sep 8, 2021

    @boegel

    For me the OpenBLAS patch by @ggouaillardet fixed the problems we were seeing with the numpy test suite on aarch64/Graviton2, cfr. easybuilders/easybuild-easyconfigs#11959

  25. mattip commented on May 7, 2022

    @mattip
    Member

    The original reporter did not update with new information, but another user did confirm the issue is fixed. I will close the issue, please reopen if I am mistaken.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions