版本比较
Mamba Quickstart
正文
增加 17 行删除 17 行
33 行未改动3435363738383940414256 行未改动9910010110210310310410510610711 行未改动11912012112212312312412512612725 行未改动15315415515615715715815916016176 行未改动2382392402412422422432442452469 行未改动25625725825926026026126226326326426526626726826926927027127227329 行未改动3033043053063073073083093103116 行未改动31831932032132232232332432532634 行未改动36136236336436536536636736836936937037137237320 行未改动3943953963973983993983994004014024034 行未改动408409410411412412413414415416121 行未改动全部展开5385395405415425425435445455464 行未改动55155255355455555555655755855944 行未改动33 行未改动 y_k &= \mathbf {\overline C} \cdot \mathbf h_k + D\cdot x_k\end{aligned}$$
The horizontal bars over SSM metrics indaties **discretized** parameters. This equation is now a *sequence-to-sequence* map $\mathbf x \in \mathbb R^L =(x_0,\ldots,x_{L-1}) \mapsto$ $\mathbf y \in \mathbb R^L=(y_0,\ldots,y_{L-1})$ instead of function-to-function. Moreover the state equation is now a recurrence in $\mathbf h_k$ (hidden state), allowing the discrete SSM to be computed like an RNN. The horizontal bars over SSM metrics indaties **discretized** parameters. This equation is now a *sequence-to-sequence* map $\mathbf x \in \mathbb R^L =(x_0,\ldots,x_{L-1}) \mapsto$ $\mathbf y \in \mathbb R^L=(y_0,\ldots,y_{L-1})$ instead of function-to-function. Moreover the state equation is now a recurrence in $\mathbf h_k$ (hidden state), allowing the discrete SSM to be computed like an RNN.
Learn a "**step size**" parameter $\Delta \in \mathbb R^+$ (in practice it is learned in the log space) and fixed choices for discretization functions: $f_{\mathbf A}, f_{\mathbf B}, f_{\mathbf C}, f_{\mathbf D}$, converts the state matrices into discretized approximations.$$\begin{aligned}56 行未改动
With these methods, the discrete-time SSM output can be written as a 1D-convolution, which can be done in $\mathcal O(L \log L)$ time via FFT.$$\begin{aligned}y_k &= \mathbf C \overline{\mathbf A}^k\overline{\mathbf B} \cdot x_{0} + \mathbf C \overline{\mathbf A}^{k-1}\overline{\mathbf B} \cdot x_{1} + \cdots + \mathbf C \overline{\mathbf B} \cdot x_{k}\\ y_k &= \mathbf C \overline{\mathbf A}^k\overline{\mathbf B} \cdot x_{0} + \mathbf C \overline{\mathbf A}^{k-1}\overline{\mathbf B} \cdot x_{1} + \cdots + \mathbf C \overline{\mathbf B} \cdot x_{k}\\&= \sum_{j=0}^{k} \mathbf C \overline{\mathbf A}^j\overline{\mathbf B} \cdot x_{k-j} = \sum_{j=0}^{k} \overline{\mathbf K}_j \cdot x_{k-j}\end{aligned}$$
11 行未改动```
Addressing Long-range Dependencies with HiPPO$$\textbf{HiPPO Matrix} \quad \mathbf A_{nk} = \textbf{HiPPO Matrix} \quad \mathbf A_{nk} =\begin{cases}(2n+1)^{1/2}(2k+1)^{1/2} & \text{if } n > k \\n+1 & \text{if } n = k \\0 & \text{if } n < k \\25 行未改动$$\begin{aligned}\overline{\mathbf K}_j &= \mathbf C \overline{\mathbf A}^j\overline{\mathbf B}=\sum_{i=0}^{N-1} \mathbf C_i \overline{\mathbf A}_{i}^j \overline{\mathbf B}_i\\\overline{\mathbf K} &= \underbrace{(\overline{\mathbf B}^\top \circ \mathbf C)}_{1\times N} \cdot \underbrace{\mathcal V_L(\overline{\mathbf A})}_{N\times N} \\&=[\overline{\mathbf B_0} \mathbf C_0, \overline{\mathbf B_1} \mathbf C_1, \ldots, \overline{\mathbf B_{N-1}} \mathbf C_{N-1}] &=[\overline{\mathbf B_0} \mathbf C_0, \overline{\mathbf B_1} \mathbf C_1, \ldots, \overline{\mathbf B_{N-1}} \mathbf C_{N-1}] \begin{bmatrix} 1 &\overline{\mathbf A}_0 &\overline{\mathbf A}_0^2 &\cdots &\overline{\mathbf A}_0^{L-1} \\ 1 &\overline{\mathbf A}_1 &\overline{\mathbf A}_1^2 &\cdots &\overline{\mathbf A}_1^{L-1} \\ \vdots &\vdots &\vdots &\ddots &\vdots \\76 行未改动

```pythonbatch, seqlen, dim = hidden_states.shape batch, seqlen, dim = hidden_states.shape
# in_proj shape = (d_inner * 2, d_model)# hidden_states shape = (batch, seqlen, dim)
9 行未改动# x, z shape both are (batch, d_inner, seqlen)x, z = xz.chunk(2, dim=1)
# x.shape = (batch, d_inner, seqlen)x = act(conv1d(x))[..., :seqlen] x = act(conv1d(x))[..., :seqlen]
# x_dbl.shape = (batch * seqlen, dt_rank + d_state * 2)x_dbl = x_proj(rearrange(x, "b d l -> (b l) d")) x_dbl = x_proj(rearrange(x, "b d l -> (b l) d"))
# dt.shape = (batch * seqlen, dt_rank), B.shape = C.shape = (batch * seqlen, d_state)dt, B, C = torch.split(x_dbl, [dt_rank, d_state, d_state], dim=-1)
# After projection, dt.shape = (d_inner, batch * seqlen)dt = dt_proj.weight @ dt.t() dt = dt_proj.weight @ dt.t()# dt.shape = (batch, d_inner, seqlen)dt = rearrange(dt, "d (b l)-> b d l", l=seqlen)
B = rearrange(B, "(b l) dstate -> b dstate l", l=seqlen).contiguous()29 行未改动> Mamba: We remark that while the $\mathbf A$ parameter could also be selective, it ultimately affects the model only through its interaction with $\Delta$ via $\overline{\mathbf A} = \exp(\Delta \mathbf A)$ (the discretization). Thus selectivity in $\Delta$ is enough to ensure selectivity in $(\overline{\mathbf A}, \overline{\mathbf B})$, and is the main source of improvement. We hypothesize that making A selective in addition to (or instead of) $\Delta$ would have similar performance, and leave it out for simplicity.
```python# discretized A.shape = (B, D, N, L)deltaA = torch.exp(torch.einsum("bdl, dn -> bdln", delta, A)) deltaA = torch.exp(torch.einsum("bdl, dn -> bdln", delta, A))```
- 
6 行未改动理解: `torch.einsum("bdln, bdl -> bdln", torch.einsum("bdl, bnl -> bdln", delta, B), u)`
- 先是 $\Delta = (B,D,L)$ 与 $\mathbf {B} = (B,N,L)$ 广播乘法,得到 $\overline{\mathbf B}=(B,D,N,L)$,对 state space 每个维度施加相同步长,然后相当于得到了 $B\times D\times L$ 个 $\overline {\mathbf B}$ (每个的维度都是 $N$),然后再与 $B\times D\times L$ 个 scaler $x(t)$ 单独计算
**Scan!!!** **Scan!!!**
```pythonfor i in range(u.shape[2]): # L # ith [B, D, N] * [B, D, N] + ith [B, D, N]34 行未改动- `downsweep[N]->right->value = downsweep[N]->value + upsweep[N]->left->value`
#### Mamba parallel scan
With some abuse of notation, let $a_t = \overline{\mathbf A},$ $b_t = \overline {\mathbf{B}(\mathbf x_t)}\cdot \mathbf x_t,$ $x_t \equiv \mathbf h_t \equiv [a_t,b_t]$. We can rewrite $\mathbf h_t = \mathbf{\overline A} \mathbf h_{t-1} + \overline {\mathbf{B}(\mathbf x_t)} \cdot \mathbf x_t $ With some abuse of notation, let $a_t = \overline{\mathbf A},$ $b_t = \overline {\mathbf{B}(\mathbf x_t)}\cdot \mathbf x_t,$ $x_t \equiv \mathbf h_t \equiv [a_t,b_t]$. We can rewrite $\mathbf h_t = \mathbf{\overline A} \mathbf h_{t-1} + \overline {\mathbf{B}(\mathbf x_t)} \cdot \mathbf x_t $
The general form of first-order recursion can be written as:$$x_i = x_i = \begin{cases} b_0, &i=0, \\ (a_i \otimes x_{i-1})\oplus b_i,\quad &0<i<n. \end{cases}20 行未改动 &= (a_i \otimes a_{i-1} \otimes a_{i-2} \otimes x_{i-3}) \oplus(a_i \otimes a_{i-1} \otimes b_{i-2}) \oplus (a_i \otimes b_{i-1}) \oplus b_i \\ &= \cdots \\\\ &= (a_i\otimes a_{i-1} \otimes \cdots \otimes a_1 \otimes b_0) \oplus(a_i\otimes a_{i-1} \otimes \cdots \otimes a_2 \otimes b_1) \\ & \oplus (a_i\otimes a_{i-1} \otimes \cdots \otimes a_{3} \otimes b_2) \oplus (a_i\otimes a_{i-1} \otimes \cdots \otimes a_{4} \otimes b_3) \\ & \oplus \cdots\oplus (a_i \otimes b_{i-1}) \oplus b_i & \oplus \cdots\oplus (a_i \otimes b_{i-1}) \oplus b_i
\end{aligned}$$and one step forward:$$4 行未改动
& \oplus (a_{i+1}\otimes a_i\otimes a_{i-1} \otimes \cdots \otimes a_{3} \otimes b_2) \oplus (a_{i+1}\otimes a_i\otimes a_{i-1} \otimes \cdots \otimes a_{4} \otimes b_3) \\
& \oplus \cdots \oplus (a_{i+1}\otimes a_i \otimes b_{i-1}) \oplus (a_{i+1}\otimes b_i) \oplus b_{i+1}
\end{aligned}$$
- 观察发现,我们只用维护两个部分:121 行未改动全部展开def operator(c_prev, c_curr, num_inputs=L, state_size=N, num_channels=D): prev_a, prev_b = extract(c_prev, state_size) curr_a, curr_b = extract(c_curr, state_size) return jnp.concatenate([ jnp.ravel(curr_a @ prev_a), jnp.ravel(curr_a @ prev_a), jnp.ravel(curr_a @ prev_b + curr_b) ])vectorized_operator = jax.vmap(operator, in_axes=(0, 0), out_axes=0)
4 行未改动
bs = get_bs(xs, Bxs)cs = jnp.concatenate([As.reshape(-1, N * N), bs], axis=1)
# %%timeit results on a freebie Google Colab VM: # %%timeit results on a freebie Google Colab VM:# 283 ms ± 16.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)lax_scanned = lax.associative_scan(vectorized_operator, cs)[:, -N:]
def naive_scan_hs(h_0, As, Bxs, xs):44 行未改动