|
3 | 3 | [Numba](https://numba.pydata.org/) is the standard |
4 | 4 | just-in-time (JIT) compiler for numerical Python, widely used |
5 | 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. |
| 6 | +Until recently it could not run in the browser: its JIT |
| 7 | +backend, [llvmlite](https://github.com/numba/llvmlite), relies |
| 8 | +on execution engines that WebAssembly does not provide. |
11 | 9 |
|
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. |
| 10 | +We have made Numba work in the browser. Numba and llvmlite now |
| 11 | +run inside [JupyterLite](https://jupyterlite.readthedocs.io/), |
| 12 | +compiling Python functions to WebAssembly and executing them in |
| 13 | +the same page, with no server involved. We are looking for |
| 14 | +funding to turn this prototype into a capability the ecosystem |
| 15 | +can rely on, maintained upstream in Numba and llvmlite. |
17 | 16 |
|
18 | | -#### Why Numba in the Browser Matters |
| 17 | +See our announcement, |
| 18 | +[Numba in the Browser](https://notebook.link/blog/numba-in-the-browser/), |
| 19 | +for the full story. |
| 20 | + |
| 21 | +#### What Works Today |
19 | 22 |
|
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. |
| 23 | +- llvmlite runs in the browser, through a WebAssembly execution |
| 24 | + engine that emits WebAssembly objects from LLVM IR, links them |
| 25 | + in-process with LLVM's linker LLD, and loads each result as an |
| 26 | + Emscripten side module. |
| 27 | +- Numba's `@jit` and `@njit` compile and execute inside a |
| 28 | + JupyterLite kernel, on arrays as well as scalars. |
| 29 | +- Packages that depend on Numba run in the browser, including |
| 30 | + [PyTensor](https://github.com/pymc-devs/pytensor), |
| 31 | + [PyMC](https://github.com/pymc-devs/pymc), |
| 32 | + [Dolo.py](https://github.com/EconForge/dolo.py) and |
| 33 | + [interpolation.py](https://github.com/EconForge/interpolation.py). |
| 34 | +- On the example in our announcement, Numba gives a roughly |
| 35 | + 250x speedup in WebAssembly, against about 90x for the same |
| 36 | + code natively. |
31 | 37 |
|
32 | | -#### Current Progress |
| 38 | +#### Why Numba in the Browser Matters |
33 | 39 |
|
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. |
| 40 | +A substantial part of the scientific Python stack depends on |
| 41 | +Numba, frequently as a hard requirement rather than an optional |
| 42 | +accelerator. PyTensor, PyMC, QuantEcon, stumpy and others are in |
| 43 | +this category. Until now none of them could be installed in |
| 44 | +Pyodide or emscripten-forge at all: not a matter of running |
| 45 | +slower in the browser, but of not running. |
38 | 46 |
|
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. |
| 47 | +Numba also covers a case that pre-compiled extensions |
| 48 | +structurally cannot. Code written interactively in a notebook |
| 49 | +does not exist until the user types it, so it cannot be shipped |
| 50 | +ahead of time in a wheel or a conda package. A JIT compiler is |
| 51 | +the only way to make that code fast, and notebooks are precisely |
| 52 | +where browser-based Python is used. |
48 | 53 |
|
49 | 54 | #### Why QuantStack |
50 | 55 |
|
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 |
| 56 | +QuantStack has in-house Numba expertise, which is rare, combined |
| 57 | +with long experience of LLVM in WebAssembly. We develop |
54 | 58 | [xeus-cpp](https://github.com/jupyter-xeus/xeus-cpp), a C++ |
55 | 59 | 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. |
| 60 | +to WebAssembly, and the execution engine behind Numba in the |
| 61 | +browser grew directly out of that work. We also maintain |
| 62 | +[emscripten-forge](https://github.com/emscripten-forge) and are |
| 63 | +core contributors to Jupyter and JupyterLite, where this work is |
| 64 | +deployed. |
58 | 65 |
|
59 | 66 | #### Proposed Work |
60 | 67 |
|
61 | | -The goal of this project is to: |
| 68 | +The prototype establishes the end-to-end architecture. Making it |
| 69 | +dependable means: |
62 | 70 |
|
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 and understand the changes required |
69 | | -- upstream the required changes |
| 71 | +- upstreaming the llvmlite and Numba changes as focused, |
| 72 | + reviewable contributions; |
| 73 | +- expanding test coverage, running the llvmlite and Numba test |
| 74 | + suites in the browser; |
| 75 | +- improving compilation performance and adding persistent |
| 76 | + caching, so that compiled functions survive a page reload; |
| 77 | +- validating and packaging more of the Numba ecosystem for |
| 78 | + emscripten-forge. |
70 | 79 |
|
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. |
| 80 | +These are separable pieces of work: we advance incrementally and |
| 81 | +deliver sub-parts, so the project can be funded in full or in |
| 82 | +part. |
74 | 83 |
|
75 | 84 | ##### Are you interested in this project? Either entirely or |
76 | 85 | partially, contact us for more information on how to help us |
|
0 commit comments