Skip to content

Add llvmlite usage example python notebook#1192

Open
yashssh wants to merge 3 commits intonumba:mainfrom
yashssh:yashwants/llvmlite-notebook
Open

Add llvmlite usage example python notebook#1192
yashssh wants to merge 3 commits intonumba:mainfrom
yashssh:yashwants/llvmlite-notebook

Conversation

@yashssh
Copy link
Copy Markdown
Contributor

@yashssh yashssh commented Apr 11, 2025

Python notebook containing examples on llvmlite use cases

@yashssh yashssh marked this pull request as draft April 11, 2025 05:36
@esc
Copy link
Copy Markdown
Member

esc commented May 12, 2025

@yashssh can you rebase this? Seems like there has not been a review, so rebase is fine. Looks like the CI fail is stale in comp. to main.

@yashssh yashssh force-pushed the yashwants/llvmlite-notebook branch from 69eb06e to 93dfc21 Compare May 13, 2025 07:05
@yashssh
Copy link
Copy Markdown
Contributor Author

yashssh commented May 13, 2025

@yashssh can you rebase this? Seems like there has not been a review, so rebase is fine. Looks like the CI fail is stale in comp. to main.

Done

@gmarkall
Copy link
Copy Markdown
Member

@yashssh Is this still a draft, or ready for a review?

@yashssh
Copy link
Copy Markdown
Contributor Author

yashssh commented May 14, 2025

@yashssh Is this still a draft, or ready for a review?

Some of the examples in the notebook need LLVM19(I created this mainly for my EuroLLVM 2025 presentation). If that's okay I can mark it ready for review.

@gmarkall
Copy link
Copy Markdown
Member

I think that's OK - I think we can review it independently of the LLVM 19 support, and merge it with / after the LLVM 19 support is merged so we're consistent.

@yashssh yashssh marked this pull request as ready for review May 14, 2025 16:08
@esc
Copy link
Copy Markdown
Member

esc commented Mar 16, 2026

I took a look at this and would like to propose the following patch:

commit ae825edc939934c1c8154f2c36eea9b6f180693b (HEAD -> yashwants/llvmlite-notebook)
Author: Emergency Self-Construct <esc@users.noreply.github.com>
Date:   Mon Mar 16 16:38:49 2026 +0100

    fix-up Numba example

    As title

diff --git a/examples/notebooks/llvmlite.ipynb b/examples/notebooks/llvmlite.ipynb
index e648fb552e..d6649386f8 100644
--- a/examples/notebooks/llvmlite.ipynb
+++ b/examples/notebooks/llvmlite.ipynb
@@ -45,39 +45,34 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 11,
    "metadata": {},
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "60.3 μs ± 79.2 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n",
-      "5.13 ms ± 27.3 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
+      "3.17 ms ± 39 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
+      "48.8 μs ± 408 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
      ]
     }
    ],
    "source": [
     "import numpy as np\n",
-    "from numba import jit\n",
+    "from numba import njit\n",
     "\n",
     "arr = np.random.randn(100000)\n",
     "\n",
-    "@jit\n",
-    "def get_sum_jit(arr):\n",
+    "@njit\n",
+    "def get_sum(arr):\n",
     "    s = 0.0\n",
     "    for x in arr:\n",
     "        s += x\n",
     "    return s\n",
     "\n",
-    "def get_sum_no_jit(arr):\n",
-    "    s = 0.0\n",
-    "    for x in arr:\n",
-    "        s += x\n",
-    "    return s\n",
     "\n",
-    "%timeit get_sum_jit(arr) \n",
-    "%timeit get_sum_no_jit(arr)"
+    "%timeit get_sum.py_func(arr) \n",
+    "%timeit get_sum(arr)"
    ]
   },
   {
@@ -2264,7 +2259,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.10.12"
+   "version": "3.14.3"
   }
  },

This makes two changes to the source code example:

a) use @njit over @jit -- this is the preferred spelling
b) instead of duplicating the function, use .py_func to execute the non-numba variant

And then I reversed the order of running to have the slow variant run first. I believe this is more natural ordering and makes it easier to see the acceleration.

I choose to post a patch here, wasn't quite sure how to use the github interface to propose a diff otherwise -- let me know if I should submit it via any other format.

Copy link
Copy Markdown
Member

@esc esc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a proposal to modify the introduction.

@yashssh yashssh force-pushed the yashwants/llvmlite-notebook branch from 93dfc21 to d105be5 Compare April 1, 2026 06:48
@yashssh
Copy link
Copy Markdown
Contributor Author

yashssh commented Apr 1, 2026

Thanks for the review @esc! I have applied your suggestion and have also rebased the patch on top of main.

@esc
Copy link
Copy Markdown
Member

esc commented Apr 1, 2026

Thanks for the review @esc! I have applied your suggestion and have also rebased the patch on top of main.

No worries, I will continue to review this. Unfortunately I may have suggested to use @njit over @jit. That suggestion was a bit outdated. @jit defaults to npython=True these days and so, please use @jit instead of @njit. I think there is only once instance of this in the patch I suggested. Sorry about that.

@yashssh
Copy link
Copy Markdown
Contributor Author

yashssh commented Apr 2, 2026

No worries, have fixed it

@esc
Copy link
Copy Markdown
Member

esc commented Apr 10, 2026

@yashssh thank you again for fixing this, my review continues. I think the Jupyter notebook should start with a short motivation and glossary to make sure all readers are on the same page. The following diff is a suggestion so feel free to edit it as you see fit. I also modified two of the headings to use the word two instead of the number 2. It is commonplace in written text to use the word for number rather than the number itself for small integers.

diff --git c/examples/notebooks/llvmlite.ipynb i/examples/notebooks/llvmlite.ipynb
index d6649386f8..2da4afec2c 100644
--- c/examples/notebooks/llvmlite.ipynb
+++ i/examples/notebooks/llvmlite.ipynb
@@ -4,7 +4,32 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "### Check LLVM version"
+    "# llvmlite example\n",
+    "\n",
+    "In this Jupyter notebook, we explore llvmlite, the lightweight Python interface to LLVM."
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Glossary\n",
+    "\n",
+    "* LLVM : A collection of modular, reusable compiler and tool-chain technologies. The acronym used to mean Low Level Virtual Machine -- but the project has grown well beyond that scope over time and is now just known as \"LLVM\".\n",
+    "* JIT: Just-in-Time, a type of compiler that runs as and when needed, commonly as the target program runs. Contrast this with Ahead-of-Time (AOT) where the program is compiled before execution.\n",
+    "* llvmlite : A lightweight Python interface to LLVM exposing only the LLVM functionality required by JIT compilers, originally designed to enable Numba.\n",
+    "* Numba: A Python JIT compiler that uses LLVM via llvmlite.\n",
+    "* IR : Intermediary Representation, an internal representation used by compilers.\n",
+    "* LLVM-IR: The IR used by LLVM."
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Check LLVM version\n",
+    "\n",
+    "For reference, we introspect both the LLVM and the llvmlite versions."
    ]
   },
   {
@@ -82,7 +107,7 @@
    },
    "source": [
     "# Part 1: IR Builder\n",
-    "### How to use llvmlite's IRBuilder APIs to build your own llvm based compiler"
+    "### How to use llvmlite's IRBuilder APIs to build your own LLVM based compiler"
    ]
   },
   {
@@ -91,7 +116,7 @@
     "id": "hBP3iRdml4MY"
    },
    "source": [
-    "#### Example 1: Building a simple llvm function to add 2 32bit integers"
+    "#### Example 1: Building a simple LLVM function to add two 32 bit integers"
    ]
   },
   {
@@ -413,7 +438,7 @@
     "id": "_RTBxfhBh3H1"
    },
    "source": [
-    "Add another function called add3 to the moudle which take 3 integers as arguments and returns their sum"
+    "Add another function called `add3` to the module which take three integers as arguments and returns their sum"
    ]
   },
   {

Let me know what you think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants