question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`np.sum` lost precision for large array

See original GitHub issue

A 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:closed
  • Created 4 years ago
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
ekelsencommented, Apr 13, 2019

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…

2reactions
mattjjcommented, Apr 12, 2019

@jlebar of the XLA team made this acute observation:

Huh, this is counterintuitive to me, but I also suspect it’s WAI.

It’s counterintuitive because I’d have expected precision loss can’t occur when the relative difference between the smallest summand (0.1) and the total sum (1e5) is less than 2^24 (number of mantissa bits in f32). In this case, log2(1e5/0.1) = 20 < 24, so…there should be no precision loss?

But the above analysis assumes that the summand and total sum are precisely-representable as floating-point numbers. My guess is that this behavior you’re seeing is related to the fact that 0.1 can’t be precisely expressed in float. If you change it to 0.125 or 0.0625, do you get a precise answer?

And indeed, as he predicts:

In [1]: import jax.numpy as np

In [2]: print np.sum(np.ones(int(1e6)) * 0.125)
jax/lib/xla_bridge.py:144: UserWarning: No GPU/TPU found, falling back to CPU.
  warnings.warn('No GPU/TPU found, falling back to CPU.')
125000.0

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found