|
| 1 | +#### Overview |
| 2 | + |
| 3 | +[Numba](https://numba.pydata.org/) is the standard |
| 4 | +just-in-time (JIT) compiler for numerical Python, widely used |
| 5 | +across the scientific stack to accelerate compute-heavy code. |
| 6 | +[Pyodide](https://pyodide.org/) brings CPython to the browser |
| 7 | +through WebAssembly and powers JupyterLite, while |
| 8 | +[emscripten-forge](https://github.com/emscripten-forge) |
| 9 | +provides a conda-based package distribution for the same |
| 10 | +target. Today, Numba cannot run in either environment. |
| 11 | + |
| 12 | +We propose to make Numba and its JIT backend, |
| 13 | +[llvmlite](https://github.com/numba/llvmlite), |
| 14 | +browser-compatible using WebAssembly, and thus enabling |
| 15 | +Pyodide and emscripten-forge users to JIT-compile numerical |
| 16 | +code just like they do on a native CPython interpreter. |
| 17 | + |
| 18 | +#### Why Numba in the Browser Matters |
| 19 | + |
| 20 | +In a native CPython environment, many paths exist to make |
| 21 | +numerical Python fast: Numba's JIT compilation, |
| 22 | +multiprocessing, native C extensions, or offloading to a GPU. |
| 23 | +In the browser, multithreading/processing and GPU access are |
| 24 | +mostly unavailable, and shipping pre-compiled native |
| 25 | +extensions for everything is impractical. In addition, the |
| 26 | +overhead of the Python interpreter is larger in the browser |
| 27 | +and many existing libraries use Numba, sometimes as hard |
| 28 | +requirements. JIT compilation is therefore an interesting |
| 29 | +route to improved performance in a fully client-side, |
| 30 | +browser-based Python environment. |
| 31 | + |
| 32 | +#### Current Progress |
| 33 | + |
| 34 | +We have already patched llvmlite and demonstrated that it runs |
| 35 | +in the browser including compilation and execution of the WASM |
| 36 | +code it generates. A live demo is available at |
| 37 | +https://notebook.link/@anutosh491/llvmlite. |
| 38 | + |
| 39 | +We also have a working demo of Numba's `@jit` decorator |
| 40 | +operating on scalar functions in the browser. However, several |
| 41 | +issues have been identified that must be overcome to make |
| 42 | +Numba work generally (e.g. with arrays). These likely include |
| 43 | +WebAssembly's lack of writable-and-executable memory pages |
| 44 | +(which rules out MCJIT), unsupported atomic instructions |
| 45 | +without shared memory, variadic C calls that cannot be lowered |
| 46 | +to WASM, and linkage incompatibilities in the LLVM WASM PIC |
| 47 | +backend. But these seem surmountable based on our experience. |
| 48 | + |
| 49 | +#### Why QuantStack |
| 50 | + |
| 51 | +QuantStack is uniquely positioned to deliver this work. We |
| 52 | +have shipped several JIT and WebAssembly projects in the |
| 53 | +scientific Python ecosystem, including |
| 54 | +[xeus-cpp](https://github.com/jupyter-xeus/xeus-cpp), a C++ |
| 55 | +interpreter running in the browser via Clang and LLVM compiled |
| 56 | +to WebAssembly. Our team also includes Numba core developers, |
| 57 | +giving us a direct upstream relationship. |
| 58 | + |
| 59 | +#### Proposed Work |
| 60 | + |
| 61 | +The goal of this project is to deliver: |
| 62 | + |
| 63 | +- upstream llvmlite compatibility with browser environments, |
| 64 | + based on our existing patches. |
| 65 | +- build a proof of concept for general Numba use in the browser to |
| 66 | + - demonstrate, ideally all of Numba, or at least a large part of it, in the browser |
| 67 | + - demonstrate the ability to run libraries that depend on it (such as pytensor/pymc) in the browser |
| 68 | + - gather the changes required to upstream |
| 69 | +- upstream the required changes to Numba |
| 70 | + |
| 71 | +Note that this is a proof of concept effort, we will advance |
| 72 | +incrementally, deliver sub-parts, and adjust the plan based |
| 73 | +on new learnings. |
| 74 | + |
| 75 | +##### Are you interested in this project? Either entirely or |
| 76 | +partially, contact us for more information on how to help us |
| 77 | +fund it. |
0 commit comments