Skip to main content

NumPy on PyPy progress report

Hello.

A lot of things happened in March, like pycon. I was also busy doing other things (pictured), so apologies for the late numpy status update.

However, a lot of things have happened and numpy continues to be one of the main points of entry for hacking on PyPy. Apologies to all the people whose patches I don't review in timely manner, but seriously, you do a lot of work.

This list of changes is definitely not exhaustive, and I might be forgetting important contributions. In a loose order:

  • Matti Picus made out parameter work for a lot of (but not all) functions.

  • We merged record dtypes support. The only missing dtypes left are complex (important), datetime (less important) and object (which will probably never be implemented because it makes very little sense and is a mess with moving GCs).

  • Taavi Burns and others implemented lots of details, including lots of ufuncs. On the completely unscientific measure of "implemented functions" on numpypy status page, we're close to 50% of numpy working. In reality it might be more or less, but after complex dtypes we're getting very close to running real programs.

  • Bool indexing of arrays of the same size should work, leaving only arrays-of-ints indexing as the last missing element of fancy indexing.

  • I did some very early experiments on SSE. This work is seriously preliminary - in fact the only implemented operation is addition of float single-dimension numpy arrays. However, results are encouraging, given that our assembler generator is far from ideal:

     

    Numpy

    PyPy SSE

    PyPy

    GCC non-looped

    GCC looped

    a+b

    0.6s

    0.3s

    0.4s

    0.3s

    0.25s

    a+b+c

    1.9s

    0.35s

    0.5s

    0.7s

    0.32s

    a+b+c+d+e

    3.2s

    0.36s

    0.8s

    1.7s

    0.51s

    The benchmark repo is available. GCC was run with -O3, no further options specified. PyPy was run with default options, the SSE branch is under backend-vector-ops, but it's not working completely yet.

    One might argue that C and Python is not the same code - indeed it is not. It just shows some possible approach to writing numeric code.

Next step would be to just continue implementing missing features such as

  • specialised arrays i.e. masked arrays and matrixes
  • core modules such as fft, linalg, random.
  • numpy's testing framework

The future is hard to predict, but we're not far off!

Cheers,
fijal

UPDATE:Indeed, string and unicode dtypes are not supported yet. They're as important as complex dtype

Comments

Jeff Terrace wrote on 2012-04-17 18:53:

I think the string dtype is missing too?

Anonymous wrote on 2012-04-17 19:57:

Hello,

May you get a bit more precise on the GCC test ?

For instance, is the GCC code using SSE too ? Is it written in a single loop (x[i] = a[i] + b[i] + c[i]) or in several consecutive loops first a+b then (a+b) + c ?

Just to know :-)

Winston Ewert wrote on 2012-04-17 20:03:

One thing I'll note is that I do from time to time use the object dtype. Occasionally, I've got multidimensional arrays of objects, and the array operations from numpy are useful. I don't really get a speed advantage there, but the interface from numpy is useful. But its not super necessary and certainly not a priority.

Anonymous wrote on 2012-04-17 20:04:

Sorry, didn't RTFA completely. I just had a look at the C code.

Still, a question: is PyPy doing the optimization of combining operations in one step ?

A "good" Fortran compiler should be able to do those optimizations, for instance.

Gaël wrote on 2012-04-17 21:17:

You should compare to numpy with a JIT, such as numexpr, it would be interesting to see whether PyPy is able to beat the numexpr JIT.

x wrote on 2012-04-17 22:55:

Very cool!

Armin Rigo wrote on 2012-04-18 10:07:

"busy doing other things (pictured)". Pictured where? :-)

Ralf Gommers wrote on 2012-04-18 20:45:

Hi, Numpy masked arrays, matrices and the testing framework are pure Python, so why do you need to implement them?

Alex wrote on 2012-04-18 22:20:

Ralf, we don't have to implement the pure-python stuff, so much as we need to make sure the features of NumPy's core that they depend on are implemented.

EOL (Eric O LEBIGOT) wrote on 2012-04-19 10:17:

Support for objects is actually quite useful: please reconsider adding it.

Here is a very useful case: the manipulation of arrays of numbers with uncertainties (special uncertainties.UFloat objects). Numbers with uncertainties behave very much like regular numbers: it is very useful to be able to use the regular NumPy syntax for array operations, for calculating matrix inverses when the matrices contain number with uncertainties, etc. I know many people use these features.

It would be *great* (read: irreplaceable :) to have support for the object NumPy dtype.

Unknown wrote on 2012-04-19 13:47:

This sounds really cool!

And it would be awesome if you’d manage to coordinate with numpy, so the projects merge to a single python codebase with two separate backends: One C-Extension based for CPython and one Pure-Python based for pypy.

Anonymous wrote on 2012-04-19 17:19:

Any chance comparing with Fortran? There are assumptions about pointers and alignment that Fortran compiler can make.

Unknown wrote on 2012-04-20 15:26:

Nice...but what is the next step?
Numpy alone is not that useful.

"We" need at least scipy and matplotlib.

Are you going to port all these modules? I don't think so.

One way forward could be to have numpy in pypy and at least scipy and matplotlib working with the pypy C api at a decent speed.

What do you think?

Anonymous wrote on 2012-04-22 20:07:

What about pickling? I'd love to experiment with hybrid CPython/PyPy execution using some magic from the multiprocessing module or a similar parallel computation framework.

Anonymous wrote on 2012-07-30 21:02:

Hello,

This is a very promising result, thank you for sharing it.
Could you give a few more details about the differences wrt to numpy?

What would people have to do to use numpypy with scipy?

Raul Durand wrote on 2012-08-06 16:52:

I think the numpy.linalg module is pretty important.
How to move efforts into this?

Raul Durand wrote on 2012-08-06 16:53:

I think the numpy.linalg module is pretty important.
How to move efforts into this?