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
{{ message }}
Repository navigation
Commit 8d73de3
Browse filesBrowse the repository at this point in the historyBrowse files
Move imports to top, restructure functional programming discussion,
add inline comments to timing blocks, and streamline examples.
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Copy file name to clipboardExpand all lines: lectures/jax_intro.md
+68-70Lines changed: 68 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,9 @@ kernelspec:
15
15
16
16
This lecture provides a short introduction to [Google JAX](https://github.com/jax-ml/jax).
17
17
18
+
```{include} _admonition/gpu.md
19
+
```
20
+
18
21
JAX is a high-performance scientific computing library that provides
19
22
20
23
* a [NumPy](https://en.wikipedia.org/wiki/NumPy)-like interface that can automatically parallelize across CPUs and GPUs,
@@ -33,9 +36,19 @@ In addition to what's in Anaconda, this lecture will need the following librarie
33
36
!pip install jax quantecon
34
37
```
35
38
36
-
```{include} _admonition/gpu.md
39
+
We'll use the following imports
40
+
41
+
```{code-cell} ipython3
42
+
import jax
43
+
import jax.numpy as jnp
44
+
import matplotlib.pyplot as plt
45
+
import numpy as np
46
+
import quantecon as qe
37
47
```
38
48
49
+
Notice that we import `jax.numpy as jnp`, which provides a NumPy-like interface.
50
+
51
+
39
52
## JAX as a NumPy Replacement
40
53
41
54
One of the attractive features of JAX is that, whenever possible, its array
@@ -47,17 +60,6 @@ Let's look at the similarities and differences between JAX and NumPy.
47
60
48
61
### Similarities
49
62
50
-
We'll use the following imports
51
-
52
-
```{code-cell} ipython3
53
-
import jax
54
-
import jax.numpy as jnp
55
-
import matplotlib.pyplot as plt
56
-
import numpy as np
57
-
import quantecon as qe
58
-
```
59
-
60
-
Notice that we import `jax.numpy as jnp`, which provides a NumPy-like interface.
61
63
62
64
Here are some standard array operations using `jnp`:
63
65
@@ -73,10 +75,6 @@ print(a)
73
75
print(jnp.sum(a))
74
76
```
75
77
76
-
```{code-cell} ipython3
77
-
print(jnp.mean(a))
78
-
```
79
-
80
78
```{code-cell} ipython3
81
79
print(jnp.dot(a, a))
82
80
```
@@ -91,30 +89,12 @@ a
91
89
type(a)
92
90
```
93
91
94
-
Even scalar-valued maps on arrays return JAX arrays.
92
+
Even scalar-valued maps on arrays return JAX arrays rather than scalars!
95
93
96
94
```{code-cell} ipython3
97
95
jnp.sum(a)
98
96
```
99
97
100
-
Operations on higher dimensional arrays are also similar to NumPy:
101
-
102
-
```{code-cell} ipython3
103
-
A = jnp.ones((2, 2))
104
-
B = jnp.identity(2)
105
-
A @ B
106
-
```
107
-
108
-
JAX's array interface also provides the `linalg` subpackage:
109
-
110
-
```{code-cell} ipython3
111
-
jnp.linalg.inv(B) # Inverse of identity is identity
112
-
```
113
-
114
-
```{code-cell} ipython3
115
-
eigvals, eigvecs = jnp.linalg.eigh(B) # Computes eigenvalues and eigenvectors
116
-
eigvals
117
-
```
118
98
119
99
120
100
### Differences
@@ -137,13 +117,15 @@ Let's try with NumPy
137
117
138
118
```{code-cell}
139
119
with qe.Timer():
120
+
# First NumPy timing
140
121
y = np.cos(x)
141
122
```
142
123
143
124
And one more time.
144
125
145
126
```{code-cell}
146
127
with qe.Timer():
128
+
# Second NumPy timing
147
129
y = np.cos(x)
148
130
```
149
131
@@ -165,7 +147,9 @@ Let's time the same procedure.
165
147
166
148
```{code-cell}
167
149
with qe.Timer():
150
+
# First run
168
151
y = jnp.cos(x)
152
+
# Hold the interpreter until the array operation finishes
169
153
jax.block_until_ready(y);
170
154
```
171
155
@@ -184,7 +168,9 @@ And let's time it again.
184
168
185
169
```{code-cell}
186
170
with qe.Timer():
171
+
# Second run
187
172
y = jnp.cos(x)
173
+
# Hold interpreter
188
174
jax.block_until_ready(y);
189
175
```
190
176
@@ -212,14 +198,18 @@ x = jnp.linspace(0, 10, n + 1)
212
198
213
199
```{code-cell}
214
200
with qe.Timer():
201
+
# First run
215
202
y = jnp.cos(x)
203
+
# Hold interpreter
216
204
jax.block_until_ready(y);
217
205
```
218
206
219
207
220
208
```{code-cell}
221
209
with qe.Timer():
210
+
# Second run
222
211
y = jnp.cos(x)
212
+
# Hold interpreter
223
213
jax.block_until_ready(y);
224
214
```
225
215
@@ -294,7 +284,7 @@ functional programming style, which we discuss below.
294
284
295
285
#### A workaround
296
286
297
-
We note that JAX does provide a version of in-place array modification
287
+
We note that JAX does provide an alternative to in-place array modification
298
288
using the [`at` method](https://docs.jax.dev/en/latest/_autosummary/jax.numpy.ndarray.at.html).
299
289
300
290
```{code-cell} ipython3
@@ -387,11 +377,26 @@ This pure version makes all dependencies explicit through function arguments, an
387
377
388
378
### Why Functional Programming?
389
379
390
-
JAX represents functions as computational graphs, which are then compiled or transformed (e.g., differentiated)
380
+
At QuantEcon we love pure functions because they
381
+
382
+
* Help testing: each function can operate in isolation
383
+
* Promote deterministic behavior and hence reproducibility
384
+
* Prevent bugs that arise from mutating shared state
385
+
386
+
The JAX compiler loves pure functions and functional programming because
387
+
388
+
* Data dependencies are explicit, which helps with optimizing complex computations
389
+
* Pure functions are easier to differentiate (autodiff)
390
+
* Pure functions are easier to parallelize and optimize (don't depend on shared mutable state)
391
+
392
+
Another way to think of this is as follows:
393
+
394
+
JAX represents functions as computational graphs, which are then compiled or
395
+
transformed (e.g., differentiated)
391
396
392
397
These computational graphs describe how a given set of inputs is transformed into an output.
393
398
394
-
They are pure by construction.
399
+
JAX's computational graphs are pure by construction.
395
400
396
401
JAX uses a functional programming style so that user-built functions map
397
402
directly into the graph-theoretic representations supported by JAX.
@@ -520,8 +525,8 @@ plt.tight_layout()
520
525
plt.show()
521
526
```
522
527
523
-
This syntax will seem unusual for a NumPy or Matlab user --- but will make a lot
524
-
of sense when we progress to parallel programming.
528
+
This syntax will seem unusual for a NumPy or Matlab user --- but will make more
529
+
sense when we get to parallel programming.
525
530
526
531
The function below produces `k` (quasi-) independent random `n x n` matrices using `split`.
527
532
@@ -664,6 +669,7 @@ x = np.linspace(0, 10, n)
664
669
665
670
```{code-cell}
666
671
with qe.Timer():
672
+
# Time NumPy code
667
673
y = f(x)
668
674
```
669
675
@@ -679,27 +685,31 @@ As a first pass, we replace `np` with `jnp` throughout:
0 commit comments