|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 | 7 | "## Python object vs nopython mode\n",
|
8 |
| - "A lof of times, we will run into compilation problem that we will never exepriecne with Numba. It is because, in pure Python we do not complie any code. Here is an example of how we can run into a compilation error when using nopython mode" |
| 8 | + "A lof of times, we will run into compilation problem that we will never exepriecne with Numba. It is because, in pure Python we do not complie any code. Here is an example of how we can run into a compilation error when using nopython mode\n", |
| 9 | + "\n", |
| 10 | + "First consider the code below:" |
9 | 11 | ]
|
10 | 12 | },
|
11 | 13 | {
|
|
14 | 16 | "metadata": {},
|
15 | 17 | "outputs": [],
|
16 | 18 | "source": [
|
17 |
| - "from numba import njit\n", |
18 |
| - "\n", |
19 |
| - "@njit #remember that njit is the short hand for @jit(nopython=True)\n", |
20 | 19 | "def adding_numbers(x, y):\n",
|
21 |
| - " return int(x) + int(y)" |
| 20 | + " return x + y" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "It is a simple function adding two numbers so" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "adding_numbers(1,2)" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "would gives you 3, let's try change the first input into a string." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "adding_numbers(\"1\",2)" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "Would have an error as we are trying to add a string to an integer.\n", |
| 60 | + "\n", |
| 61 | + "**Exercise: change the code in \"adding_numbers\" to \"fix\" the above**" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "**Exercise: use `njit` on the \"fixed\" function**" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": null, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "# Un-commend the line below to see how we do it.\n", |
| 92 | + "#%load adding_numbers.py" |
22 | 93 | ]
|
23 | 94 | },
|
24 | 95 | {
|
|
59 | 130 | "source": [
|
60 | 131 | "We see a compilation error that we would never see when using pure Python. This is an error that came from Numba. From the Traceback we see there is an error message that mension TypingError and it's something to do with the nopython mode pipeline.\n",
|
61 | 132 | "\n",
|
62 |
| - "So let's try to make Python object avaliable." |
| 133 | + "So let's try to make Python object avaliable.\n", |
| 134 | + "\n", |
| 135 | + "**Exercise: rename \"adding_numbers\" to \"adding_numbers_again\" and use `jit` on it**" |
63 | 136 | ]
|
64 | 137 | },
|
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": null, |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [], |
| 143 | + "source": [] |
| 144 | + }, |
65 | 145 | {
|
66 | 146 | "cell_type": "code",
|
67 | 147 | "execution_count": null,
|
68 | 148 | "metadata": {},
|
69 | 149 | "outputs": [],
|
70 | 150 | "source": [
|
71 |
| - "from numba import jit\n", |
72 |
| - "@jit\n", |
73 |
| - "def adding_numbers_again(x, y):\n", |
74 |
| - " return int(x) + int(y)" |
| 151 | + "# Un-commend the line below to see how we do it.\n", |
| 152 | + "#%load adding_numbers_again.py" |
75 | 153 | ]
|
76 | 154 | },
|
77 | 155 | {
|
|
94 | 172 | "cell_type": "markdown",
|
95 | 173 | "metadata": {},
|
96 | 174 | "source": [
|
97 |
| - "Now we see that the g function does compiled, we ony get a warning from Numba, which says we are now using object mode. (so it will be slower than expected, see **Extra Task** below)\n", |
| 175 | + "Now we see that the function does compiled, we ony get a warning from Numba, which says we are now using object mode. (so it will be slower than expected, see **Extra Task** below)\n", |
98 | 176 | "\n",
|
99 | 177 | "Now it is your turn to try.\n",
|
100 | 178 | "\n",
|
|
130 | 208 | "source": [
|
131 | 209 | "It is quite fustriting as we cannot compile this function, it is due to Numba not able to determine the return type of a function for compilation. Maybe the Python interpreter can help?\n",
|
132 | 210 | "\n",
|
133 |
| - "**TODO: use the proper decorator for the function below**" |
| 211 | + "**Exercise: use the proper decorator for the function below**" |
134 | 212 | ]
|
135 | 213 | },
|
136 | 214 | {
|
|
250 | 328 | "cell_type": "markdown",
|
251 | 329 | "metadata": {},
|
252 | 330 | "source": [
|
253 |
| - "Now, can you figure it out a way to return an empty list using `njit`? We have to somehow state the type of the items in the list without putting any items in the list..." |
| 331 | + "Now, can you figure it out a way to return an empty list using `njit`? We have to somehow state the type of the items in the list without putting any items in the list...\n", |
| 332 | + "\n", |
| 333 | + "**Exercise: try to \"fix\" the above**" |
254 | 334 | ]
|
255 | 335 | },
|
256 | 336 | {
|
|
0 commit comments