Skip to content

Numerical precision in dot product with MKL #11655

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

Closed
rth opened this issue Aug 1, 2018 · 3 comments
Closed

Numerical precision in dot product with MKL #11655

rth opened this issue Aug 1, 2018 · 3 comments

Comments

@rth
Copy link
Contributor

rth commented Aug 1, 2018

The output of numpy.dot appears to be impacted by the total array shape (possibly only with MKL.)

Example

import numpy as np
np.random.seed(42)

a = np.random.rand(4, 10000).astype('float32')
m1 = np.dot(a, a.T)
m2 = np.dot(a[:2], a[:2].T)

print(m1[:2, :2])
print(m2)

Expected output

Identical arrays printed in both cases,

[[3269.1648 2488.9185]
 [2488.9185 3382.3335]]
[[3269.1648 2488.9185]
 [2488.9185 3382.3335]]

Actual output

[[3269.1653 2488.9177]
 [2488.9177 3382.334 ]]
[[3269.1648 2488.918 ]
 [2488.918  3382.3337]]

i.e. only a 1e-4 absolute precision.

Environment information

Using the following conda environment,

conda create -n numpy-env numpy==1.15.0 python=3.7

the above output with bad precision is produced on the following 4 CPU core Linux system,

$ uname -ap
Linux [..] 4.4.30-mod-std-ipv6-64 #9 SMP Tue Nov 1 17:58:26 CET 2016 x86_64 GNU/Linux
$ cat /proc/cpuinfo  | grep "model name" | head -n 1
model name      : Intel(R) Xeon(R) CPU E3-1225 V2 @ 3.20GHz

while the same environment produces the correct result exactly on the following (2 CPU core) Linux system,

$ uname -a
Linux 4.11.6-gentoo #9 SMP Fri Sep 29 14:28:20 CEST 2017 x86_64 Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz GenuineIntel GNU/Linux

This issue was originally indirectly detected in scikit-learn/scikit-learn#11711 where it was reported for Linux, Mac OS, and Windows. Thanks to @TomDLT for a reproducible minimal numpy code snippet.

Additional information

  • Creating the python 3.7 env with conda then installing numpy OpenBLAS wheel with pip doesn't have this issue
  • in conda installing numpy together with openblas nomkl also doesn't have this issue, so it seem that it's due to MKL.
  • 64 bit floats don't appear to be impacted: the obtained absolute precision is 1e-12, for number of a few 1e3, which appears consistent with ~16 digits precision.
  • setting MKL_NUM_THREADS=1, MKL_DOMAIN_NUM_THREADS=1 doesn't help.
@rth
Copy link
Contributor Author

rth commented Aug 1, 2018

Though, the obtained 7 digit precision, does seem to be consistent with the 32bit float precision. Is this the expected behavior then?

@charris
Copy link
Member

charris commented Aug 1, 2018

You cannot expect better than 32 bit precision with 32 bit inputs. ISTR from the BLIS discussion that there may be an option in the future to have higher precision in the accumulation, but no plans for that at the moment.

@rth
Copy link
Contributor Author

rth commented Aug 7, 2018

Thanks for your response @charris !

Yes, it makes sense, I guess calculations in OpenBlas just happen to be unchanged, there is no guarantee that they would be (depending on the optimizations / implementation used internally). Closing.

@rth rth closed this as completed Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants