4 ms·
NumPy is insanely fast for most use cases, I've (grudgingly) come to the conclusion that if NumPy doesn't hack it then I should re-think the problem or my way o
by jacquesm 4y ago
NumPy is insanely fast for most use cases, I've (grudgingly) come to the conclusion that if NumPy doesn't hack it then I should re-think the problem or my way of solving it rather than to try to optimize that particular bit of code if it isn't meant for something that is going to be run in production on a large number of machines. Likely there are better uses of my time. It's interesting how what is nominally a scripting language can perform so well for compute intensive tasks. Of course, technically you are just stringing together highly optimized low level functions together using the Python language but the advantages of doing it that way have more often than not surprised me.
- deleted 4y ago[deleted]
- rapatel0 4y agoThis Op is better off recompiling numpy to target his specific hardware thank trying to speed it up using rust. This would be to try different underlying math implementations that are linked with numpy ( blas, mkl, eigen). Each of these haha several internal simulation frameworks to optimize not just the actual instructions but memory layout for various math kernels.
- kzrdude 4y agoHow do you handle it in numpy if you want something like ((x - y)/2 + z) * w all elementwise over the arrays? Naively that's 3 intermediate unused arrays being created.
- zarzavat 4y agoThere's plenty of CPU and GPU numpy accelerators available. * Numba: https://numba.pydata.org/ https://numba.pydata.org/ * JAX: https://jax.readthedocs.io/en/latest/notebooks/quickstart.html https://jax.readthedocs.io/en/latest/notebooks/quickstart.ht...
- kzrdude 4y agonumba - instead of writing in Rust, you write it in numba, which is also almost like another language. Not bad, but needs to be taken into account and is not pure numpy.
- europeanguy 4y agoYou use Julia and let loop fusion handle it. You get 1 allocation if you want to store the result in a new array, or zero allocations if you already have the array allocated. https://julialang.org/blog/2017/01/moredots/ https://julialang.org/blog/2017/01/moredots/
- korijn 4y agoInplace operators?
- zozbot234 4y agoIsn't that what Theano/Aesara is for? Still based on a NumPy interface AIUI, but automagically compiling from NumPy-based to efficient code on the CPU or GPU.
- CraigJPerry 4y agoIs there an optimal solution to that for all sizes of the arrays? E.g. I’d expect for small/medium then re-writing as a fold over the input arrays would be fastest because you’d only traverse once & everything would fit in cache. However if the 3 arrays combined size is larger than L1 cache, I’d be strongly tempted to bet on the naïve 3 operation approach to be faster. You’d save so much time on cache line flush / reload activities. But then if 2 arrays are larger than L3 i’d expect going back to the fold / single traversal to win again because the iterating 2 arrays at a time behaviour is no longer any different to iterating 3 at a time. Untested hypothesis.
- pmeira 4y agoYou could use NumExpr: https://github.com/pydata/numexpr#what-is-numexpr https://github.com/pydata/numexpr#what-is-numexpr
- pillefitz 4y agoImplementing the hot path in e.g. C, compiling it into a lib an calling it using e.g. ctypes is very little effort and can yield dramatic runtime improvements / reduced bill. It's been an excellent use of my time, at least. I'd recommend using Numba first, though.