You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve clarity and fix errors in Numba lecture (#524)
* Improve clarity and fix errors in Numba lecture
Fix typos (deficiency, JULIA, That's is, duplicate label), drop Julia
from vectorization comparison (Julia uses JIT, not traditional
vectorization), replace bootstrap example with clearer function
iteration example, compress decorator section into a remark, add fix
for global variables gotcha, remove redundant import, and add JAX
cross-reference for GPU parallelization.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Fix build: replace invalid {remark} with {admonition}, fix label clash
{remark} is not a valid MyST directive — use {admonition} with title
"Remark" instead. Also rename (numba)= label to (numba_lecture)= to
avoid ambiguity with the document name.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@@ -49,7 +49,7 @@ In an {doc}`earlier lecture <need_for_speed>` we discussed vectorization,
49
49
which can improve execution speed by sending array processing operations in batch to efficient low-level code.
50
50
51
51
However, as {ref}`discussed in that lecture <numba-p_c_vectorization>`,
52
-
traditional vectorization schemes, such as those found in MATLAB, Julia, and NumPy, have weaknesses.
52
+
traditional vectorization schemes, such as those found in MATLAB and NumPy, have weaknesses.
53
53
54
54
* Highly memory-intensive for compound array operations
55
55
* Ineffective or impossible for some algorithms.
@@ -59,16 +59,16 @@ One way to circumvent these problems is by using [Numba](https://numba.pydata.or
59
59
60
60
Numba compiles functions to native machine code instructions during runtime.
61
61
62
-
When it succeeds, Numba will be on par with machine code from low-level languages.
62
+
When it succeeds, the result is performance comparable to compiled C or Fortran.
63
63
64
64
In addition, Numba can do other useful tricks, such as {ref}`multithreading` or
65
65
interfacing with GPUs (through `numba.cuda`).
66
66
67
-
Numba's JIT compiler is in many ways similar to the JIT compiler in JULIA
67
+
Numba's JIT compiler is in many ways similar to the JIT compiler in Julia
68
68
69
69
The main difference is that it is less ambitious, attempting to compile a smaller subset of the language.
70
70
71
-
Although this might sound like a defficiency, it is in some ways an advantage.
71
+
Although this might sound like a deficiency, it is in some ways an advantage.
72
72
73
73
Numba is lean, easy to use, and very good at what it does.
74
74
@@ -184,7 +184,7 @@ The basic idea is this:
184
184
* Moreover, the types of *other variables* in `qm`*can be inferred once the input types are known*.
185
185
* So the strategy of Numba and other JIT compilers is to *wait until the function is called*, and then compile.
186
186
187
-
That's is called "just-in-time" compilation.
187
+
That is called "just-in-time" compilation.
188
188
189
189
Note that, if you make the call `qm(0.5, 10)` and then follow it with `qm(0.9,
190
190
20)`, compilation only takes place on the first call.
@@ -193,50 +193,10 @@ This is because compiled code is cached and reused as required.
193
193
194
194
This is why, in the code above, `time3` is smaller than `time2`.
195
195
196
-
197
-
198
-
## Decorator Notation
199
-
200
-
In the code above we created a JIT compiled version of `qm` via the call
201
-
202
-
```{code-cell} ipython3
203
-
qm_numba = jit(qm)
204
-
```
205
-
206
-
In practice this would typically be done using an alternative *decorator* syntax.
207
-
208
-
(We discuss decorators in a {doc}`separate lecture <python_advanced_features>` but you can skip the details at this stage.)
209
-
210
-
Specifically, to target a function for JIT compilation we can put `@jit` before the function definition.
211
-
212
-
Here's what this looks like for `qm`
213
-
214
-
```{code-cell} ipython3
215
-
@jit
216
-
def qm(x0, n):
217
-
x = np.empty(n+1)
218
-
x[0] = x0
219
-
for t in range(n):
220
-
x[t+1] = α * x[t] * (1 - x[t])
221
-
return x
222
-
```
223
-
224
-
This is equivalent to adding `qm = jit(qm)` after the function definition.
225
-
226
-
The following now uses the jitted version:
227
-
228
-
```{code-cell} ipython3
229
-
with qe.Timer(precision=4):
230
-
qm(0.1, 100_000)
231
-
```
232
-
233
-
```{code-cell} ipython3
234
-
with qe.Timer(precision=4):
235
-
qm(0.1, 100_000)
196
+
```{admonition} Remark
197
+
In practice, rather than writing `qm_numba = jit(qm)`, we use *decorator* syntax and put `@jit` before the function definition. This is equivalent to adding `qm = jit(qm)` after the definition. We use this syntax throughout the rest of the lecture. (See {doc}`python_advanced_features` for more on decorators.)
236
198
```
237
199
238
-
Numba also provides several arguments for decorators to accelerate computation and cache functions -- see [here](https://numba.readthedocs.io/en/stable/user/performance-tips.html).
239
-
240
200
241
201
## Type Inference
242
202
@@ -253,41 +213,35 @@ This allows it to generate efficient native machine code, without having to call
253
213
254
214
When Numba cannot infer all type information, it will raise an error.
255
215
256
-
For example, in the (artificial) setting below, Numba is unable to determine the type of function `mean` when compiling the function `bootstrap`
216
+
For example, in the setting below, Numba is unable to determine the type of the function `g` when compiling `iterate`
0 commit comments