{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Numba 0.50.0 Release Demo\n", "=======================\n", "\n", "This notebook contains a demonstration of new features present in the 0.50.0 release of Numba. Whilst release notes are produced as part of the [`CHANGE_LOG`](https://github.com/numba/numba/blob/release0.50/CHANGE_LOG), there's nothing like seeing code in action! This release contains a few new features, but it's mainly internals that have changed!\n", "\n", "Key internal changes:\n", "\n", "* Numba is now backed by LLVM 9.\n", "* NumPy 1.18 support is added.\n", "\n", "\n", "Demonstrations of new features/changes include:\n", "* [New error messages](#New-error-messages)\n", "* [Jitclass ``@staticmethod``](#Jitclass-@staticmethod-support)\n", "* [NumPy enhancements](#Newly-supported-NumPy-functions/features)\n", "* [All predicates are booleans!](#All-predicates-are-boolean!)\n", "\n", "First, import the necessary from Numba and NumPy..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numba import jit, njit, config, __version__, errors\n", "from numba.extending import overload\n", "import numba\n", "import numpy as np\n", "assert tuple(int(x) for x in __version__.split('.')[:2]) >= (0, 50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "New error messages\n", "=================\n", "\n", "Providing error messages about any compilation issues Numba encounters is quite challenging, in Numba 0.50 the first of two important changes have been made to error reporting. In this change error messages have been made more compact, are more specific if information is available, and some exceptions that were being hidden are now correctly handled. Quick example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@njit\n", "def illegal():\n", " return 2 + 'a'\n", "\n", "try:\n", " illegal()\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jitclass `@staticmethod` support\n", "===========================\n", "\n", "In 0.50.0 a member of the Numba community, [@EPronovost](https://github.com/EPronovost), (with thanks!) added ``@staticmethod`` support for Numba's experimental ``jitclass`` feature, an example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from numba import int32, float32 # import the types\n", "from numba.experimental import jitclass\n", "\n", "spec = [\n", " ('value', int32), # a simple scalar field\n", " ('array', float32[:]), # an array field\n", "]\n", "\n", "@jitclass(spec)\n", "class Bag(object):\n", " def __init__(self, value):\n", " self.value = value\n", " self.array = np.zeros(value, dtype=np.float32)\n", "\n", " @property\n", " def size(self):\n", " return self.array.size\n", "\n", " def increment(self, val):\n", " for i in range(self.size):\n", " self.array[i] += val\n", " return self.array\n", "\n", " @staticmethod\n", " def add(x, y):\n", " return x + y\n", "\n", "n = 21\n", "mybag = Bag(n)\n", "print(Bag.add(4, 5))\n", "print(mybag.add(4.5, 5.6))\n", "print(Bag.add.signatures)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Newly supported NumPy functions/features\n", "=====\n", "This release contains support for `np.sum` on the `timedelta64` type, again, this written by a member of the Numba community, [@luk-f-a](https://github.com/luk-f-a) (with thanks!). NumPy 1.18 support is also implemented." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(20).astype(np.dtype('timedelta64[M]'))\n", "\n", "@njit\n", "def demo_numpy(x):\n", " return np.sum(x)\n", " \n", "demo_numpy(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All predicates are boolean!\n", "======================\n", "\n", "A long standing (literally years) bug was finally fixed which means that this common mistake of using a NumPy array as a predicate is at last reported correctly!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@njit\n", "def demo_predicates_are_bools():\n", " a = np.ones(5)\n", " if a:\n", " return 1\n", " else:\n", " return 0\n", "\n", "try:\n", " demo_predicates_are_bools()\n", "except ValueError as e:\n", " print(e) " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 2 }