-
Notifications
You must be signed in to change notification settings - Fork 201
Add a Numba-enabled JupyterLite deployment #2336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anutosh491
wants to merge
5
commits into
pymc-devs:main
Choose a base branch
from
anutosh491:add-jupyterlite-numba-ci
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0ec717
Add a generic pure-Python build option
anutosh491 8bd41a3
Use target-sized indices in Numba vectorized codegen
anutosh491 775ddf9
Add Numba-enabled JupyterLite deployment
anutosh491 a9cece2
Fix and secure JupyterLite deployment workflow
anutosh491 3aeba62
Add Jupyter Server to JupyterLite build environment
anutosh491 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "7fb27b941602401d91542211134fc71a", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# PyTensor and Numba in JupyterLite\n", | ||
| "\n", | ||
| "This example is adapted from PyTensor's introduction notebook and runs with the Numba linker entirely in the browser." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "acae54e37e7d407bbb7b55eff062a284", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import sys\n", | ||
| "from importlib.metadata import version\n", | ||
| "\n", | ||
| "import numba\n", | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "import pytensor\n", | ||
| "import pytensor.tensor as pt\n", | ||
| "\n", | ||
| "\n", | ||
| "assert sys.platform == \"emscripten\"\n", | ||
| "{\n", | ||
| " \"Platform\": sys.platform,\n", | ||
| " \"PyTensor\": version(\"pytensor\"),\n", | ||
| " \"Numba\": numba.__version__,\n", | ||
| " \"NumPy\": np.__version__,\n", | ||
| "}" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "9a63283cbaf04dbcab1f6479b197f3a8", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "x = pt.vector(\"x\", shape=(None,))\n", | ||
| "z = pt.exp(pt.sin(x))\n", | ||
| "out = pt.cos((z[None, :] @ z[:, None]).squeeze())\n", | ||
| "\n", | ||
| "out.dprint()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "8dd0d8092fe74a7c96281538738b07e2", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "numba_fn = pytensor.function([x], out, mode=\"NUMBA\")\n", | ||
| "numba_fn.dprint(print_destroy_map=True)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "72eea5119410473aa328ad9291626812", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "values = np.array([0.25, -0.5, 1.0])\n", | ||
| "result = numba_fn(values)\n", | ||
| "expected_z = np.exp(np.sin(values))\n", | ||
| "expected = np.cos(expected_z @ expected_z)\n", | ||
| "\n", | ||
| "np.testing.assert_allclose(result, expected)\n", | ||
| "{\"Result\": result, \"Expected\": expected}" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "8edb47106e1a46a883d545849b8ab81b", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Reuse the compiled function with a different vector length.\n", | ||
| "values = np.linspace(-1.0, 1.0, 5)\n", | ||
| "result = numba_fn(values)\n", | ||
| "expected_z = np.exp(np.sin(values))\n", | ||
| "np.testing.assert_allclose(result, np.cos(expected_z @ expected_z))\n", | ||
| "result" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python (XPython)", | ||
| "language": "python", | ||
| "name": "xpython" | ||
| }, | ||
| "language_info": { | ||
| "name": "python", | ||
| "version": "3.13" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| name: Build and deploy JupyterLite | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pages: write | ||
| id-token: write | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v5 | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| with: | ||
| fetch-depth: 0 | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| - name: Install build environment | ||
| uses: mamba-org/setup-micromamba@v2 | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| with: | ||
| environment-file: environment-wasm-build.yml | ||
| environment-name: pytensor-wasm-build | ||
| init-shell: bash | ||
|
|
||
| - name: Build PyTensor from this checkout | ||
| shell: bash -l {0} | ||
| run: | | ||
| set -euxo pipefail | ||
| PYTENSOR_PURE_PYTHON=1 python -m build --wheel --no-isolation | ||
|
|
||
| mkdir -p build/pytensor-wheel | ||
| wheels=(dist/pytensor-*.whl) | ||
| test "${#wheels[@]}" -eq 1 | ||
| python -m zipfile -e "${wheels[0]}" build/pytensor-wheel | ||
|
|
||
| - name: Create the WebAssembly environment | ||
| shell: bash -l {0} | ||
| run: | | ||
| set -euxo pipefail | ||
| micromamba create -y \ | ||
| -f environment-wasm-host.yml \ | ||
| --platform=emscripten-wasm32 | ||
|
|
||
| echo "PYTENSOR_WASM_PREFIX=${MAMBA_ROOT_PREFIX}/envs/pytensor-wasm-host" >> "${GITHUB_ENV}" | ||
|
|
||
| - name: Build JupyterLite | ||
| shell: bash -l {0} | ||
| run: | | ||
| set -euxo pipefail | ||
| jupyter lite build \ | ||
| --XeusAddon.prefix="${PYTENSOR_WASM_PREFIX}" \ | ||
| --XeusAddon.mounts="$(pwd)/build/pytensor-wheel:/lib/python3.13/site-packages" \ | ||
| --contents=.github/jupyterlite/pytensor_numba.ipynb \ | ||
| --output-dir=dist-jupyterlite | ||
|
|
||
| - name: Upload Pages artifact | ||
| uses: actions/upload-pages-artifact@v4 | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| with: | ||
| path: dist-jupyterlite | ||
|
|
||
| deploy: | ||
| needs: build | ||
| if: github.ref == 'refs/heads/main' | ||
| runs-on: ubuntu-latest | ||
|
|
||
| environment: | ||
| name: github-pages | ||
| url: ${{ steps.deployment.outputs.page_url }} | ||
|
|
||
| steps: | ||
| - name: Deploy to GitHub Pages | ||
| id: deployment | ||
| uses: actions/deploy-pages@v4 | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| name: pytensor-wasm-build | ||
|
|
||
| channels: | ||
| - conda-forge | ||
|
|
||
| dependencies: | ||
| - python=3.13 | ||
| - python-build | ||
| - setuptools | ||
| - cython | ||
| - numpy=2.4.* | ||
| - versioneer=0.29 | ||
| - jupyterlite-core | ||
| - jupyterlite-xeus | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| name: pytensor-wasm-host | ||
|
|
||
| channels: | ||
| - https://prefix.dev/emscripten-forge-4x | ||
| - https://prefix.dev/conda-forge | ||
|
|
||
| dependencies: | ||
| - python=3.13 | ||
| - xeus-python | ||
| - numpy=2.4.* | ||
| - scipy | ||
| - numba=0.66.* | ||
| - llvmlite=0.48.* | ||
| - setuptools | ||
| - filelock | ||
| - etuples | ||
| - logical-unification | ||
| - minikanren | ||
| - cons |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from llvmlite import ir | ||
| from numba import types | ||
|
|
||
| from pytensor.link.numba.dispatch.vectorize_codegen import compute_itershape | ||
|
|
||
|
|
||
| class Mock32BitContext: | ||
| """Minimal Numba context whose ``intp`` LLVM representation is i32.""" | ||
|
|
||
| class CallConv: | ||
| @staticmethod | ||
| def return_user_exc(builder, exc, args): | ||
| pass | ||
|
|
||
| call_conv = CallConv() | ||
|
|
||
| @staticmethod | ||
| def get_constant(typ, value): | ||
| assert typ is types.intp | ||
| return ir.Constant(ir.IntType(32), value) | ||
|
|
||
|
|
||
| def test_compute_itershape_uses_target_intp_width(): | ||
| module = ir.Module() | ||
| function_type = ir.FunctionType(ir.VoidType(), ()) | ||
| function = ir.Function(module, function_type, "compute_itershape") | ||
| builder = ir.IRBuilder(function.append_basic_block("entry")) | ||
|
|
||
| one_i32 = ir.Constant(ir.IntType(32), 1) | ||
| shape = compute_itershape( | ||
| Mock32BitContext(), | ||
| builder, | ||
| in_shapes=[[one_i32]], | ||
| broadcast_pattern=((True,),), | ||
| size=None, | ||
| ) | ||
| builder.ret_void() | ||
|
|
||
| assert shape == [one_i32] | ||
| assert "icmp ne i32" in str(module) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.