Numba: A High Performance Python Compiler
numba.pydata.org
numba.pydata.org
It’s a phenomenal library for developing novel computationally intensive algorithms on numpy arrays. It’s also more versatile than Jax.
In presentations, I’ve heard Leland McInnes credits numba often when he speaks of his development of UMAP. We built a very computationally intensive portion of our application with it and it has been running in production, stable, for several years now.
It’s not suitable for all use cases. But I recommend testing it if you need to do somewhat complex calculations iterating over numpy arrays for which standard numpy or scipy functions don’t exist. Even then, often we were surprised that we could speed up some of those calculations by placing them inside numba.
Edit: ex of a very small function I wrote with numba that speeds up an existing numpy function (note - written years ago and numba has undergone quite some amount of changes since!): https://github.com/grej/pure_numba_alias_sampling
Disclosure - I now work for Anaconda, the company that sponsors the numba project.
Does numba do automatic differentiation?
I view JAX as primarily an automatic differentiation tool with the bonus that it makes great use of XLA and can easy make use of GPU/TPUs.
I don’t usually see numba and JAX as solving the same problem, but would be excited to be wrong
I had never heard of tai chi until now, I’m curious how it compares.
Numba compiles functions down to machine code or cuda kernels, that's it. XLA is "higher level" than what Numba produces.
You may be able to get the equivalent of jax via numba+numpy+autograd[1], but I haven't tried it before.
IMHO, jax is best thought of as a numerical computation library that happens to include autograd, vmapping, pmapping and provides a high level interface for XLA.
I have built a numerical optimisation library with it, and although a few things became verbose, it was a rather pleasant experience as the natural vmapping made everything a breeze, I didn't have to write the gradients for my testing functions, except for special cases that involved exponents and logs that needed a bit of delicate care.
I have tried it before [0]. And it works. As expected. Not much friction.
[0]: sorry no github
If I had to do it again I would just use plain numpy or use the JAX from Google if JIT is really necessary.
[1] https://github.com/pybind/pybind11 [2] https://github.com/fwilliams/numpyeigen
Reduces calendar time, risk, cost. And I'm likely to make better decisions once the code and market is better understood after the prototype is tested under real world conditions and the requirements have changed (like they always seem to do).
cibuildwheel makes this easy.
Example I can think of include:
1. pyyaml’s parser in C vs the Python version get a huge speed up on large files
2. some parsing table (~GB size) using pandas vs self-implemented Python code with a lot of for loop gain 20x speed up at least.
I remember the team being really responsive to feature requests back then and I had a lot of fun working with it. IIRC compared to using numpy we managed to get speedups of up to 60x for the most critical pieces of code.
[1]: https://github.com/fbpic/fbpic [2]: https://github.com/fbpic/fbpic/blob/1867a4f216baf4269f2314ab...
The main problem with Numba is that simple functions are easy enough, and this lulls you into a false sense of security- that things will work.
Unfortunately, every time it turns into an a hair tearing exercise of trying to structure the code such that Numba's vast array of unpredictable edge cases isn't hit.
The error messages are often infuriatingly bad.
At this point I've banned Numba from our codebase. If there's a case for Numba, we just do it in C++ instead.
Edit: we've been looking at Taichi https://www.taichi-lang.org/
It cannot use NumPy and other libraries (since it is Rust), but at the same time, I see its potential in creating high-performance code to be used in Python numerical environment.
I didn't think Pypy uses LLVM so I wonder who produced better code.
That said, they're targeted at different audiences. I feel Numba is targeted at data science and machine learning and even AI.
I feel a large portion of using or programming a computer is structural and not the actual work of adding numbers together. Very little of the code generated does the useful part a computer does: addition. The rest is control flow management and data placement! It's all preparation for the code to do an addition. The hard part is putting together the structure for the computer to do things that are useful.
So we invented methods, variables, classes, functions, closures, expressions to create that structure easier.
I thought about creating a language which tries to eliminate the structure that most programs accumulate and focus on the critical addition or calculation and let the computer do the arrangement. A JIT compiler for structure.
Isn't that constraint propagation?
I'm discovering JS at the moment. I don't fully understand the async model, but the promise seems like a generic constraint of "the result is now available"
Maybe you could have the "flow managements" as other constraints?
I'm thinking the code for your average CRUD or even desktop compositor. A compositor copies pixels from multiple places into one place. Surely that can be defined with a simple loop? But no there's hundreds of APIs in the way. Add Wayland and X11 and you have something that is opaque and understood by very few people.
The motivation behind my comment was that most of programming computers is gluing together APIs to shift data from one place to another before doing something useful with it. The APIs themselves do very little addition or subtraction of data but actual just moving data around and placing it into the right place.
Maybe defining where things should be, declaratively, in order to do a calculation would be useful. So the shape of the calculation defines the data structure, rather than the data structure defining the caclulation.
Unchanged? Don't bother, leave it as-is. I think that's how Intel power saving works.
Now think about the MVC model: some changes in the data could result in a change in the view if the data currently shown on screen is what has changed - like triggers in SQL.
I wonder if you could have everything work like that?
And thankyou for bringing up constraint propagation.
One of my ideas is the definition of formulas that act as materialized views over other materialised views. So we can layer materialized views over other materialized views and then work out a derived formula that is potentially nearer to what we want and potentially summarise the formula without needing to calculate the underlying views, we can compute the formula directly.
Is this differential dataflow?
I think it's an application of algebra and JIT compilers could do it to expressions if we fed symbolic expressions of programming languages into sympy or machine algebra.
In react, react does diffing between virtual DOM nodes to see if there are changed. There is also dirty region checking in old games and damage regions. These problems are mathematically defined.
Here's my writings on the idea https://github.com/samsquire/ideas4#31-algebraic-materialise...
Yes and the constraints could be the used to reduce the computational costs, giving higher performance and lower latency.
A while back, a good friend (we even shared HN accounts for a while lol) pointed me to pipelinedb: a PostgreSQL timeseries plugin for continuously updating """materialized views"""
I use a lot of quotes around, because it wasn't either like a regular view (computed when you query it, which introduces latency) or a materialized view (frozen, needs to be refreshed, same problem) but more like the NO_HZ tickless kernel: the update of the calculations was caused by the introduction of new data, not the passage of time (which would be wasteful)
The general approach makes a lot of sense to me, and I see how it could be used for more generic problems.
Faster Python calculations with Numba - https://news.ycombinator.com/item?id=30392367 - Feb 2022 (66 comments)
Numba: a JIT compiler for Python that works best on code that uses NumPy - https://news.ycombinator.com/item?id=21614533 - Nov 2019 (9 comments)
How Numba and Cython speed up Python code - https://news.ycombinator.com/item?id=17678758 - Aug 2018 (45 comments)
Numba: High-Performance Python with CUDA Acceleration - https://news.ycombinator.com/item?id=15301766 - Sept 2017 (62 comments)
Numba - JIT specializing compiler for annotated Python and NumPy code to LLVM - https://news.ycombinator.com/item?id=5927787 - June 2013 (8 comments)
Accelerating Python Libraries with Numba (Part 2) - https://news.ycombinator.com/item?id=5757231 - May 2013 (23 comments)
Accelerating Python Libraries with Numba - https://news.ycombinator.com/item?id=5680722 - May 2013 (30 comments)
Numba: NumPy-aware optimizing compiler for Python - https://news.ycombinator.com/item?id=4430780 - Aug 2012 (23 comments)
NumPy aware dynamic Python compiler using LLVM - https://news.ycombinator.com/item?id=3864659 - April 2012 (9 comments)
Numba - A NumPy aware (LLVM-based) optimizing compiler for Python - https://news.ycombinator.com/item?id=3692055 - March 2012 (6 comments)
* PyPy JITs everything, so it can do _normal_ Python numerical code that is quite fast and regular Python code that is fast. However, its interactions with libraries like NumPy add overhead, and it seems like it can't JIT code that interacts with NumPy in a useful way (AFAIK, would be happy to be proven wrong). So not useful for optimizing numeric functions that interact with libraries like NumPy.
* Plain old NumPy and friends. This is great... if the operation you want is already available as a "vectorized" API. "Vectorized" in this context does NOT mean SIMD, it's a Python-specific usage, see below.
* Numba: JIT compilation specifically focusing on interop with NumPy and similar libraries. Lets you write subset of Python but unlike NumPy you can use for loops and go fast.
* AOT compilation: Cython, Rust, C++, etc.. You have a longer feedback loop, but you have a full programming language, especially if you avoid Cython. OTOH Cython has nicer Python interop so for simple just-a-little-addon it can be easier to use if you don't already know Rust. You really shouldn't be writing new C++ in this day and age (but wrapping an existing library is useful). Like C++, Cython doesn't help with memory safety. Cython also suffers from two compilers, so debugging can be harder, especially if you use the C++ interop; if you are wrapping existing C++ library, I'd probably start with PyBind11 based on long-ago experience with Boost::Python.
Longer form:
* "Vectorization" in the context of Python: https://pythonspeed.com/articles/vectorization-python/
* PyPy and Numba as alternatives to vectorization: https://pythonspeed.com/articles/vectorization-python-altern...
* Choosing a compiled language: https://pythonspeed.com/articles/rust-cython-python-extensio...
* The performance overhead of AOT compiled libraries (less relevant if you're doing anything numeric): https://pythonspeed.com/articles/python-extension-performanc...
* Numba intro: https://pythonspeed.com/articles/numba-faster-python/
https://gist.github.com/nadavrot/5b35d44e8ba3dd718e595e40184...
Vectorizing code was fairly straightforward using the latter versions of fortran. It was all quite sweet and productive but could not provide the required hpc scaling so was eventually abandoned in favor of massively parallel designs.
Edit: There's a period write-up of the general Cray 1 design here: https://inst.eecs.berkeley.edu/~n252/sp07/Papers/Cray.pdf
(Modules like PyJulia facilitate a more incremental approach.)
I’m very interested in adding something like this to some projects but it needs to be 10-100x faster to be worth the hassle. Otherwise, for our applications, it’s a better time investment to rewrite in Go and get the speed and pro tooling than to further optimize python.
JAX introduced a lot of cool concepts (e.g. autobatching (vmap), autoparallel (pmap)) and supported a lot of things that PyTorch didn't (e.g. forward mode autodiff).
And at least for my applications (scientific computing), it was much faster (~100x) due to a much better JIT compiler and reduced Python overhead.
...but! PyTorch has worked hard to introduce all of the former, and the recent PyTorch 2 announcement was primarily about a better JIT compiler for PyTorch. (I don't think anyone has done serious non-ML benchmarks for this though, so it remains to be seen how this holds up.)
There are still a few differences. E.g. JAX has a better differential equation solving ecosystem. PyTorch has a better protein language model ecosystem. JAX offers some better power-user features like custom vmap rules. PyTorch probably has a lower barrier to entry.
(FWIW I don't know how either hold up specifically for DSP.)
I'd honestly suggest just trying both; always nice to have a broader selection of tools available.