Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions lectures/jax_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ translation:
Vectorization with `vmap`: 使用 `vmap` 进行向量化
Vectorization with `vmap`::A simple example: 一个简单的示例
Vectorization with `vmap`::Combining transformations: 组合变换
Automatic differentiation: a preview: 自动微分:预览
Exercises: 练习
---

Expand Down Expand Up @@ -82,18 +83,18 @@ import numpy as np
import quantecon as qe
```

注意我们导入了 `jax.numpy as jnp`,它提供了类似 NumPy 的接口。

## JAX 作为 NumPy 的替代品

JAX 的一个吸引人之处在于,它的数组处理操作在尽可能的情况下遵循 NumPy API。

这意味着在许多情况下,我们可以将 JAX 作为 NumPy 的直接替代品使用。

让我们来看看 JAX 和 NumPy 之间的异同。

### 相似之处

上面我们导入了 `jax.numpy as jnp`,它为数组操作提供了类似 NumPy 的接口。

JAX 的一个吸引人之处在于,只要有可能,这一接口就会遵循 NumPy API。

因此,我们通常可以将 JAX 作为 NumPy 的直接替代品使用。

以下是使用 `jnp` 进行的一些标准数组操作:

```{code-cell} ipython3
Expand Down Expand Up @@ -135,11 +136,13 @@ jnp.sum(a)
(jax_speed)=
#### 速度!

假设我们想在许多点上计算余弦函数。
一个主要差异是 JAX 更快——有时快得多。

为了说明这一点,假设我们想在许多点上计算余弦函数。

```{code-cell}
n = 50_000_000
x = np.linspace(0, 10, n)
x = np.linspace(0, 10, n) # NumPy array
```

##### 使用 NumPy
Expand Down Expand Up @@ -180,15 +183,11 @@ with qe.Timer():
# First run
y = jnp.cos(x)
# Hold the interpreter until the array operation finishes
jax.block_until_ready(y);
y.block_until_ready()
```

```{note}
这里,为了测量实际速度,我们使用 `block_until_ready` 方法来阻塞解释器,直到计算结果返回。

这是必要的,因为 JAX 使用异步调度,允许 Python 解释器在数值计算之前运行。

对于非计时代码,可以删除包含 `block_until_ready` 的那一行。
上面的 `block_until_ready` 方法会阻塞解释器,直到计算结果返回。这对于计时是必要的,因为 JAX 使用异步调度,允许 Python 解释器在数值计算完成之前继续向前运行。
```

再来计时一次。
Expand All @@ -198,7 +197,7 @@ with qe.Timer():
# Second run
y = jnp.cos(x)
# Hold interpreter
jax.block_until_ready(y);
y.block_until_ready()
```

在 GPU 上,此代码的运行速度远快于其 NumPy 等效代码。
Expand Down Expand Up @@ -226,15 +225,15 @@ with qe.Timer():
# First run
y = jnp.cos(x)
# Hold interpreter
jax.block_until_ready(y);
y.block_until_ready()
```

```{code-cell}
with qe.Timer():
# Second run
y = jnp.cos(x)
# Hold interpreter
jax.block_until_ready(y);
y.block_until_ready()
```

运行时间先增加后减少(这在 GPU 上会更明显)。
Expand Down Expand Up @@ -283,7 +282,7 @@ a[0] = 1
a
```

在 JAX 中,这会失败!
在 JAX 中,这会失败 😱。

```{code-cell} ipython3
a = jnp.linspace(0, 1, 3)
Expand All @@ -297,12 +296,17 @@ except Exception as e:
print(e)
```

JAX 的设计者选择将数组设为不可变的,因为 JAX 使用函数式编程风格,我们将在下面讨论这一点。
JAX 的设计者选择将数组设为不可变的,因为

1. JAX 使用*函数式编程风格*,并且
2. 函数式编程通常避免可变数据

我们将在{ref}`下文 <jax_func>`中讨论这些思想。

(jax_at_workaround)=
#### 变通方法

我们注意到 JAX 确实提供了一种替代原地数组修改的方式,使用 [`at` 方法](https://docs.jax.dev/en/latest/_autosummary/jax.numpy.ndarray.at.html)。
JAX 确实提供了一种直接替代原地数组修改的方式,即 [`at` 方法](https://docs.jax.dev/en/latest/_autosummary/jax.numpy.ndarray.at.html)。

```{code-cell} ipython3
a = jnp.linspace(0, 1, 3)
Expand All @@ -324,6 +328,7 @@ a

(尽管它在 JIT 编译的函数中实际上可以很高效——但现在先把这个放在一边。)

(jax_func)=
## 函数式编程

来自 JAX 的文档:
Expand Down
Loading