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 d14b1cf
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: lectures/_admonition/gpu.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
4
4
This lecture was built using [hardware](status:machine-details) that has access to a GPU.
5
5
6
-
Free GPUs are available on [Google Colab](https://colab.research.google.com/). To run this lecture on a Colab machine, click on the "play" icon top right, select Colab, and set the runtime environment to include a GPU.
6
+
To run this lecture on [Google Colab](https://colab.research.google.com/), click on the "play" icon top right, select Colab, and set the runtime environment to include a GPU.
7
7
8
-
To run this lecture on your own GPU, you need to install [Google JAX](https://github.com/google/jax). (JAX can also run without a GPU, although execution will be slower.)
8
+
To run this lecture on your own machine, you need to install [Google JAX](https://github.com/google/jax).
Copy file name to clipboardExpand all lines: lectures/about.md
+42-4Lines changed: 42 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,47 @@
1
1
2
2
# About
3
3
4
-
This lecture series provides an introduction to quantitative economics using [Google JAX](https://github.com/google/jax).
4
+
Perhaps the single most notable feature of scientific computing in the past
5
+
two decades is the rise and rise of parallel computation.
6
+
7
+
For example, the advanced artificial intelligence applications now shaking the
8
+
worlds of business and academia require massive computer power to train, and the
9
+
great majority of that computer power is supplied by GPUs.
10
+
11
+
For us economists, with our ever-growing need for more compute cycles,
12
+
parallel computing provides both opportunities and new difficulties.
13
+
14
+
The main difficulty we face vis-a-vis parallel computation is accessibility.
15
+
16
+
Even for those with time to invest in careful parallelization of their programs,
17
+
exploiting the full power of parallel hardware is challenging for non-experts.
18
+
19
+
Moreover, that hardware changes from year to year, so any human capital
20
+
associated with mastering intricacies of a particular GPU has a very high
21
+
depreciation rate.
22
+
23
+
For these reasons, we find [Google JAX](https://github.com/google/jax) compelling.
24
+
25
+
In short, JAX makes high performance and parallel computing accessible (and fun!).
26
+
27
+
It provides a familiar array programming interface based on NumPy, and, as long as
28
+
some simple conventions are adhered to, this code compiles to extremely
29
+
efficient and well-parallelized machine code.
30
+
31
+
One of the most agreeable features of JAX is that the same code set and be run on
32
+
either CPUs or GPUs, which allows users to test and develop locally, before
33
+
deploying to a more powerful machine for heavier computations.
34
+
35
+
JAX is relatively easy to learn and highly portable, allowing us programmers to
36
+
focus on the algorithms we want to implement, rather than particular features of
37
+
our hardware.
38
+
39
+
This lecture series provides an introduction to using Google JAX for
40
+
quantitative economics.
41
+
42
+
The rest of this page provides some background information on JAX, notes on
43
+
how to run the lectures, and credits for our colleagues and RAs.
44
+
5
45
6
46
7
47
## What is JAX?
@@ -18,9 +58,6 @@ Loosely speaking, JAX is like [NumPy](https://numpy.org/) with the addition of
18
58
* automated GPU/TPU support
19
59
* a just-in-time compiler
20
60
21
-
One of the great benefits of JAX is that exactly the same code can be run either
22
-
on the CPU or on a hardware accelerator, such as a GPU or TPU.
23
-
24
61
In short, JAX delivers
25
62
26
63
1. high execution speeds on CPUs due to efficient parallelization and JIT
@@ -32,6 +69,7 @@ In short, JAX delivers
32
69
These features make JAX ideal for almost all quantitative economic modeling
33
70
problems that require heavy-duty computing.
34
71
72
+
35
73
## How to run these lectures
36
74
37
75
The easiest way to run these lectures is via [Google Colab](https://colab.research.google.com/).
Copy file name to clipboardExpand all lines: lectures/intro.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,13 @@ kernelspec:
9
9
name: python3
10
10
---
11
11
12
-
# Quantitative Economics with Python (JAX)
12
+
# Quantitative Economics with JAX
13
13
14
-
This website presents a set of lectures on quantitative economic modeling, designed and written by
15
-
[Thomas J. Sargent](http://www.tomsargent.com/) and [John Stachurski](https://johnstachurski.net/)
16
-
specifically using [JAX](https://jax.readthedocs.io) and accelerating using the `GPU`.
14
+
This website presents a set of lectures on quantitative economic modeling
15
+
using GPUs and [Google JAX](https://jax.readthedocs.io). The lectures
16
+
are designed and written by [Thomas J. Sargent](http://www.tomsargent.com/) and
For an overview of the series, see [this page](https://quantecon.org/projects/#filter=lecture)
Copy file name to clipboardExpand all lines: lectures/jax_intro.md
+26-21Lines changed: 26 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,26 +68,20 @@ a
68
68
type(a)
69
69
```
70
70
71
-
Even scalar-valued maps on arrays return objects of type `DeviceArray`:
71
+
Even scalar-valued maps on arrays return JAX arrays.
72
72
73
73
```{code-cell} ipython3
74
74
jnp.sum(a)
75
75
```
76
76
77
-
The term `Device` refers to the hardware accelerator (GPU or TPU), although JAX falls back to the CPU if no accelerator is detected.
77
+
JAX arrays are also called "device arrays," where term "device" refers to a
78
+
hardware accelerator (GPU or TPU).
78
79
79
80
(In the terminology of GPUs, the "host" is the machine that launches GPU operations, while the "device" is the GPU itself.)
80
81
81
-
```{note}
82
-
Note that `DeviceArray` is a [*future*](https://en.wikipedia.org/wiki/Futures_and_promises); it allows Python to continue execution when the results of computation are not available immediately.
83
-
84
-
This means that Python can dispatch more jobs without waiting for the computation results to be returned by the device.
85
-
86
-
This feature is called [*asynchronous dispatch*](https://jax.readthedocs.io/en/latest/async_dispatch.html), which hides Python overheads and reduces wait time.
87
-
```
88
82
89
83
90
-
Operations on higher dimensional arrays is also similar to NumPy:
84
+
Operations on higher dimensional arrays are also similar to NumPy:
91
85
92
86
```{code-cell} ipython3
93
87
A = jnp.ones((2, 2))
@@ -169,7 +163,8 @@ a_new = a.sort()
169
163
a, a_new
170
164
```
171
165
172
-
The designers of JAX chose to make arrays immutable because JAX uses a functional programming style. More on this below.
166
+
The designers of JAX chose to make arrays immutable because JAX uses a
167
+
functional programming style. More on this below.
173
168
174
169
Note that, while mutation is discouraged, it is in fact possible with `at`, as in
175
170
@@ -293,15 +288,18 @@ How long does the function take to execute?
293
288
```
294
289
295
290
```{note}
296
-
With asynchronous dispatch, the `%time` magic is only evaluating the time to dispatch by the Python interpreter, without taking into account the computation time on the device.
291
+
Here, in order to measure actual speed, we use the `block_until_ready()` method
292
+
to hold the interpreter until the results of the computation are returned from
293
+
the device.
297
294
298
-
Here, to measure the actual speed, the `block_until_ready()` method prevents asynchronous dispatch by asking Python to wait until the computation results are ready.
299
-
```
295
+
This is necessary because JAX uses asynchronous dispatch, which allows the
296
+
Python interpreter to run ahead of GPU computations.
300
297
298
+
```
301
299
302
300
This code is not particularly fast.
303
301
304
-
While it is run on the GPU (since `x` is a `DeviceArray`), each vector `k * x` has to be instantiated before the final sum is computed.
302
+
While it is run on the GPU (since `x` is a JAX array), each vector `k * x` has to be instantiated before the final sum is computed.
305
303
306
304
If we JIT-compile the function with JAX, then the operations are fused and no intermediate arrays are created.
307
305
@@ -321,6 +319,9 @@ And now let's time it.
321
319
%time f_jit(x).block_until_ready()
322
320
```
323
321
322
+
Note the large speed gain.
323
+
324
+
324
325
## Functional Programming
325
326
326
327
From JAX's documentation:
@@ -330,16 +331,18 @@ From JAX's documentation:
330
331
331
332
In other words, JAX assumes a functional programming style.
332
333
333
-
The major implication is that JAX functions should be pure:
334
+
The major implication is that JAX functions should be pure.
334
335
335
-
* no dependence on global variables
336
+
A pure function will always return the same result if invoked with the same inputs.
337
+
338
+
In particular, a pure function has
339
+
340
+
* no dependence on global variables and
336
341
* no side effects
337
342
338
-
"A pure function will always return the same result if invoked with the same inputs."
339
343
340
344
JAX will not usually throw errors when compiling impure functions but execution becomes unpredictable.
341
345
342
-
343
346
Here's an illustration of this fact, using global variables:
344
347
345
348
```{code-cell} ipython3
@@ -387,9 +390,11 @@ Moral of the story: write pure functions when using JAX!
387
390
388
391
JAX can use automatic differentiation to compute gradients.
389
392
390
-
This can be extremely useful in optimization, root finding and other applications.
393
+
This can be extremely useful for optimization and solving nonlinear systems.
394
+
395
+
We will see significant applications later in this lecture series.
391
396
392
-
Here's a very simple illustration, involving the function
397
+
For now, here's a very simple illustration involving the function
0 commit comments