`np.sum` lost precision for large array
See original GitHub issueA simple replicate (in CPU) is
np.sum(np.ones(1000000) * 0.1).copy()
which gives the result 100958.34
while it is expected to return a number around 1000000 +- 10
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (17 by maintainers)
Top Results From Across the Web
How to avoid less precise sum for numpy-arrays with multiple ...
In this special case, with only 2 elements in a row, the overhead is just too big (see also similar behavior explained here)....
Read more >numpy.sum — NumPy v1.24 Manual
The default, axis=None, will sum all of the elements of the input array. ... Especially when summing a large number of lower precision...
Read more >Quantity — Astropy v5.2
The array may be recreated via a = np.array(a.tolist()) , although this may sometimes lose precision. Examples. For a 1D array, a.tolist() ...
Read more >Loss of result precision from function convereted from numpy ...
I am trying to move a model from Tf1 to Torch. The model is quite involved and I have been unable to get...
Read more >Chapter 4. NumPy Basics: Arrays and Vectorized Computation
One of the key features of NumPy is its N-dimensional array object, or ndarray, which is a fast, flexible container for large data...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
np.sum uses a numerically stable pairwise summation algorithm (https://github.com/numpy/numpy/pull/3685)
Tensorflow’s CPU implementation should also now do the same thing (exact commit is somewhere in Eigen, ask Rasmus). As does the GPU implementation.
I think the issue here is that XLA does not do this. So basically, the problem has been fixed everywhere except XLA and probably XLA should implement numerically stable summation algorithms as well…
@jlebar of the XLA team made this acute observation:
And indeed, as he predicts:
The fact that enabling fast math makes the precision go up seems like just coincidence, since fast math can do a lot of things.
I think this might be working as intended, but if you have a problem around this example, maybe we should figure out a workaround. What do you think?