跳到正文

算法·理论

Mamba Quickstart

DarkMoon_Dragon约 50 分钟
原文
文章信息与版本

文章信息

发布
2024年5月12日
收录
2026年1月23日
更新
2026年11月10日
篇幅
约 50 分钟

版本

  1. v2当前
  2. v1

Mamba Quickstart

Linear time-invariant state space models

Linear time invariant (LTI) state space models (SSM) or S4

Continious form

The state space model is defined by this simple equation. It maps a 1-D input signal x(t)x(t) to an NN-D latent state h(t)\mathbf h(t) before projecting to a 1-D output signal y(t)y(t).

Cont

h′(t)=A⋅h(t)+B⋅x(t)y(t)=C⋅h(t)+D⋅x(t)\begin{aligned} \mathbf h'(t) &= \mathbf A \cdot \mathbf h(t) + \mathbf B \cdot x(t) \\ y(t) &= \mathbf C \cdot \mathbf h(t) + D \cdot x(t) \end{aligned}
  • A∈RN×N,B∈RN×1,C∈R1×N\mathbf A \in \mathbb R^{N\times N}, \mathbf B \in \mathbb R^{N\times 1}, \mathbf C \in \mathbb R^{1\times N} are learnable matrices, comprise the actual parameters of the SSM.
  • x(t)∈Rx(t) \in \mathbb R is the 1-D input signal at time t∈Rt \in \mathbb R
    • Raw audio files, t∈R+t \in \mathbb R^+, and x(t)\mathbf x(t) is the amplitude at time tt
    • Text token embeddings, t∈Z+t \in \mathbb Z^+, and the SSM is applied independently to each channel
  • y(t)∈Ry(t) \in \mathbb R is the corresponding output of the model at time t∈Rt \in \mathbb R
    • Binary classification, V=1,t∈R+V=1,t\in \mathbb R^+ and y(t)∈{0,1}y(t) \in \{0,1\}
  • State vector h(t)∈RN\mathbf h(t) \in \mathbb R^N encapsulates all prior inputs at time tt

LTI illu

Discrete form

hk=A‾⋅hk−1+B‾⋅xkyk=C‾⋅hk+D⋅xk\begin{aligned} \mathbf h_k &= \mathbf{\overline A} \cdot \mathbf h_{k-1} + \mathbf{\overline B} \cdot x_k \\ 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 x∈RL=(x0,…,xL−1)↦\mathbf x \in \mathbb R^L =(x_0,\ldots,x_{L-1}) \mapsto y∈RL=(y0,…,yL−1)\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 hk\mathbf h_k (hidden state), allowing the discrete SSM to be computed like an RNN.

Learn a “step size” parameter Δ∈R+\Delta \in \mathbb R^+ (in practice it is learned in the log space) and fixed choices for discretization functions: fA,fB,fC,fDf_{\mathbf A}, f_{\mathbf B}, f_{\mathbf C}, f_{\mathbf D}, converts the state matrices into discretized approximations.

A‾=fA(Δ,A)B‾=fB(Δ,A,B)C‾=fC(Δ,C)D‾=fD(Δ,D)\begin{aligned} \overline{\mathbf A} &= f_{\mathbf A}(\Delta, \mathbf A) \\ \overline{\mathbf B} &= f_{\mathbf B}(\Delta, \mathbf A, \mathbf B) \\ \overline{\mathbf C} &= f_{\mathbf C}(\Delta, \mathbf C) \\ \overline{\mathbf D} &= f_{\mathbf D}(\Delta, \mathbf D) \\ \end{aligned}
  • Bilinear

    A‾=(I−Δ/2⋅A)−1(I+Δ/2⋅A)B‾=(I−Δ/2⋅A)−1ΔBC‾=C\begin{aligned} \overline{\mathbf A} &= (\mathbf I - \Delta/2 \cdot \mathbf A)^{-1}(\mathbf I + \Delta/2 \cdot \mathbf A) \\ \overline{\mathbf B} &= (\mathbf I - \Delta/2 \cdot \mathbf A)^{-1} \Delta \mathbf B \\ \overline{\mathbf C} &= \mathbf C \\ \end{aligned}
    Python
    def discretize(A, B, C, step):
        I = np.eye(A.shape[0])
        BL = inv(I - (step / 2.0) * A)
        Ab = BL @ (I + (step / 2.0) * A)
        Bb = (BL * step) @ B
        return Ab, Bb, C
  • ZOH:

    A‾=exp⁡(ΔA)B‾=(ΔA)−1(exp⁡(ΔA)−I)ΔBC‾=C\begin{aligned} \overline{\mathbf A} &= \exp(\Delta \mathbf A) \\ \overline{\mathbf B} &= (\Delta \mathbf A)^{-1} (\exp(\Delta \mathbf A) - \mathbf I) \Delta \mathbf B \\ \overline{\mathbf C} &= \mathbf C\\ \end{aligned}

    Proof: The solution of h′(t)=A⋅h(t)+B⋅x(t)\mathbf h'(t) = \mathbf A \cdot \mathbf h(t) + \mathbf B \cdot x(t):

    h(t)=eA(t−t0)h(t0)+∫t0teAt−τB⋅x(τ) dτ\mathbf h(t) = e^{\mathbf A(t-t_0)} \mathbf h(t_0) + \int_{t_0}^{t} e^{\mathbf A{t-\tau}} \mathbf B \cdot x(\tau) \, \mathrm{d} \tau

    Let t0=tk,t=tk+1t_0 = t_k, t = t_{k+1} and tk+1−tk=Δt_{k+1} - t_k = \Delta, we have

    h(tk+1)=eAΔh(tk)+x(tk)⋅∫tktk+1eA(tk+1−τ)B dτ\mathbf h(t_{k+1}) = e^{\mathbf A\Delta} \mathbf h(t_k) + x(t_k) \cdot \int_{t_k}^{t_{k+1}}e^{\mathbf A(t_{k+1} - \tau)} \mathbf B \, \mathrm d{\tau}

    Thus we have:

    A‾=eAΔ=exp⁡(ΔA)B‾=∫tktk+1eA(tk+1−τ)B dτ=∫0ΔeA(Δ−τ)B dτ=∫0ΔeA(u)B du=B‾=A−1(eAΔ−I)B\begin{aligned} \overline{\mathbf A} &= e^{\mathbf A \Delta} = \exp{(\Delta \mathbf A)} \\ \overline{\mathbf B} &= \int_{t_k}^{t_{k+1}}e^{\mathbf A(t_{k+1} - \tau)} \mathbf B \, \mathrm d{\tau} = \int_{0}^{\Delta}e^{\mathbf A(\Delta - \tau)} \mathbf B \, \mathrm d{\tau} \\ &= \int_{0}^{\Delta}e^{\mathbf A(u)} \mathbf B \, \mathrm d{u} = \overline{\mathbf{B}} = \mathbf{A}^{-1} \left( e^{\mathbf{A}\Delta} - \mathbf{I} \right) \mathbf{B} \end{aligned}

[!WARNING]

你说 State Spaces 离散化我笑.jpg。首先 data dependent 的 decay 完全丧失了 LTI 的性质,非要叫 State Space 多多少少有点强行。其次个人完全不信离散化能有什么用。如果真有用,论文实现里也不至于把B的离散化直接简化成 linear attention 的外积形式了 1 2

作者:sonta 链接:https://www.zhihu.com/question/644981978/answer/3406436860

With these methods, the discrete-time SSM output can be written as a 1D-convolution, which can be done in O(Llog⁡L)\mathcal O(L \log L)​ time via FFT.

yk=CA‾kB‾⋅x0+CA‾k−1B‾⋅x1+⋯+CB‾⋅xk=∑j=0kCA‾jB‾⋅xk−j=∑j=0kK‾j⋅xk−j\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}\\ &= \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} y=x∗K‾where K‾∈RL=(CB‾,CA‾B‾,…,CA‾L−1B‾)\mathbf y = \mathbf x * \overline{\mathbf K} \quad \text{where } \overline {\mathbf K} \in \mathbb R^{L} = (\mathbf C\overline{\mathbf B}, \mathbf C \overline{\mathbf A}\overline{\mathbf B},\ldots,\mathbf C \overline{\mathbf A}^{L-1} \overline{\mathbf B})
  • Recall that C∈R1×N,A‾∈RN×N\mathbf C \in \mathbb R^{1\times N}, \overline{\mathbf A} \in \mathbb R^{N\times N} and B‾∈RN×1\overline{\mathbf B} \in \mathbb R^{N\times 1}.
Python
def K_conv(Ab, Bb, Cb, L):
    return np.array(
        [(Cb @ matrix_power(Ab, l) @ Bb).reshape() for l in range(L)]
    )

Addressing Long-range Dependencies with HiPPO

HiPPO MatrixAnk={(2n+1)1/2(2k+1)1/2if n>kn+1if n=k0if n<k\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 \\ \end{cases}
  • Previous work found that simply modifying an SSM from a random matrix A\mathbf A to HiPPO\textbf{HiPPO} improved its performance on the sequential MNIST classification benchmark from 60%60\% to 98%98\%​.

  • Python
    def make_HiPPO(N):
        P = np.sqrt(1 + 2 * np.arange(N))
        A = P[:, np.newaxis] * P[np.newaxis, :]
        A = np.tril(A) - np.diag(np.arange(N))
        return -A

Diagonal form

The fundamental bottleneck in computing the discrete-time SSM is that it involves repeated matrix multiplication by A‾\overline{\mathbf A}. For example, computing naively involves 𝐿𝐿 successive multiplications by A‾\overline{\mathbf A}, requiring O(N2L)\mathcal O(N^2L) matrix multiplications and O(NL)\mathcal O(NL)​​ space.

Recall that the kernel has the form:

K‾=[K‾0,K‾1,…,K‾L−1], where K‾j=CA‾jB‾∈R\quad \overline{\mathbf K} = [\overline{\mathbf K}_0, \overline{\mathbf K}_1,\ldots, \overline{\mathbf K}_{L-1}], \quad \text{ where } \overline{\mathbf K}_j= \mathbf C \overline{\mathbf A}^j\overline{\mathbf B} \in \mathbb R

When A\mathbf{A} is diagonal, A‾=exp⁡(ΔA)\overline{\mathbf{A}} = \exp(\Delta \mathbf{A}) is also diagonal, making the computation nearly trivial. For notational simplicity, let Ci\mathbf{C}_i denote the ii-th element of vector C\mathbf{C} (i.e., [C]1,i[\mathbf{C}]_{1,i}), B‾i\overline{\mathbf{B}}_i denote the ii-th element of vector B‾\overline{\mathbf{B}} (i.e., [B‾]i,1[\overline{\mathbf{B}}]_{i,1}), and Ai\mathbf{A}_i denote the ii-th diagonal element of A‾\overline{\mathbf{A}} (i.e., [A‾]i,i[\overline{\mathbf{A}}]_{i,i}).

K‾j=CA‾jB‾=∑i=0N−1CiA‾ijB‾iK‾=(B‾⊤∘C)⏟1×N⋅VL(A‾)⏟N×N=[B0‾C0,B1‾C1,…,BN−1‾CN−1][1A‾0A‾02⋯A‾0L−11A‾1A‾12⋯A‾1L−1⋮⋮⋮⋱⋮1A‾N−1A‾N−12⋯A‾N−1L−1]\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}] \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 \\ 1 &\overline{\mathbf A}_{N-1} &\overline{\mathbf A}_{N-1}^2 &\cdots &\overline{\mathbf A}_{N-1}^{L-1} \\ \end{bmatrix} \end{aligned}

This calculation can be parallelized by work efficient scan.

[!NOTE]

In Mamba: Structure and Dimensions. Finally, we note that structured SSMs are so named because computing them efficiently also requires imposing structure on the A matrix. The most popular form of structure is diagonal (Gu, Gupta, et al. 2022; Gupta, Gu, and Berant 2022; Smith, Warrington, and Linderman 2023), which we also use.

Mamba SSM

Mamba (S6) removes linear time invariance for B,C,Δ\mathbf B, \mathbf C, \Delta. They’re now functions of xt\mathbf x_t, i.e. the parameters are selective.

ht=A(xt)‾⋅ht−1+B(xt)‾⋅xtyt=C(xt)‾⋅ht+D‾⋅xt\begin{aligned} \mathbf h_t &= \overline{\mathbf A(\mathbf x_t)} \cdot \mathbf h_{t-1} + \overline {\mathbf{B}(\mathbf x_t)} \cdot \mathbf x_t \\ \mathbf y_t &= \overline {\mathbf {C}(\mathbf x_t)} \cdot \mathbf h_t + \overline {\mathbf{D}} \cdot \mathbf x_t \end{aligned}
  • A\mathbf A and D\mathbf D remain linear time-invariant.
  • B\mathbf B and C\mathbf C are first parametrized by xt\mathbf x_t​​, and then discretized.

Mamba Code Highlight

Fixed Initialization

A initialization

Python
# S4D real initialization
# A shape = (d_inner, d_state) / (D, N)
A = repeat(
    torch.arange(1, d_state + 1, dtype=torch.float32, device=device),
    "n -> d n",
    d=d_inner,
).contiguous()
A_log = torch.log(A)  # Keep A_log in fp32
A_log = nn.Parameter(A_log)
A_log._no_weight_decay = True

A = -torch.exp(A_log.float())  # (d_inner, d_state)
  • Default initialization is based on HIPPO theroy, which defines the nn​-th element of A\mathbf A​ in the diagonal as −(n+1)-(n+1)​​.
  • dinnerd_{\text{inner}} independent A\mathbf{A} matrices act on each channel of the input x\mathbf{x} (shape B×dinner×LB \times d_{\text{inner}} \times L).

D initialization

Python
# D "skip" parameter
# D shape = (D_inner, )
D = nn.Parameter(torch.ones(d_inner, device=device))
D._no_weight_decay = True

dt bias initialization

Python
# Initialize dt bias so that F.softplus(dt_bias) is between dt_min and dt_max
        dt = torch.exp(
            torch.rand(self.d_inner, **factory_kwargs) * (math.log(dt_max) - math.log(dt_min))
            + math.log(dt_min)
        ).clamp(min=dt_init_floor)
        # Inverse of softplus: https://github.com/pytorch/pytorch/issues/72759
        inv_dt = dt + torch.log(-torch.expm1(-dt))
        with torch.no_grad():
            self.dt_proj.bias.copy_(inv_dt)
        # Our initialization would set all Linear.bias to zero, need to mark this one as _no_reinit
        self.dt_proj.bias._no_reinit = True
Forward

image-20240515000654650

image-20240515143735507

Python
batch, seqlen, dim = hidden_states.shape

# in_proj shape = (d_inner * 2, d_model)
# hidden_states shape = (batch, seqlen, dim)

# xz shape = (batch, d_inner * 2, seqlen), x and z are concatenated
xz = rearrange(
        in_proj.weight @ rearrange(hidden_states, "b l d -> d (b l)"),
        "d (b l) -> b d l",
        l=seqlen,
    )
Python
# 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_dbl.shape = (batch * seqlen, dt_rank + d_state * 2)
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.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()
C = rearrange(C, "(b l) dstate -> b dstate l", l=seqlen).contiguous()
  • Δ\Delta 的形状为 (B,L,D)(B,L,D),即对每个 token (总共 B×LB \times L 个) 的每个维度都有一个独特的 Δ\Delta,由于 Δ\Delta 变为 input-depend, 从而 A‾\overline{\mathbf A} 也间接变为 input dependent
  • B,C\mathbf B, \mathbf C 的形状为 (B,N,L)(B,N,L),意味着这两个参数是 time-dependent 但不是 channel- dependent。作者这么做的一个原因是减少 DD 维度,可以把这两个参数放在 SRAM 中去
SSM
Python
y = selective_scan_ref(
                x,
                dt,
                A,
                B,
                C,
                D.float(),
                z=z,
                delta_bias=dt_proj.bias.float(),
                delta_softplus=True,
                return_last_state=True,
                return_last_state=ssm_state is not None,
            )

Discretize A\mathbf A. Mamba takes A‾\overline{\mathbf A}​ of the ZOH form, which is exp⁡(ΔA)\exp(\Delta \mathbf A). Δ\Delta 在 NN 维度广播(对 state space 每个维度施加相同步长)。对输入 x(t)x(t),每个维度独立的进行 SSM,A\mathbf A 有独立的 DD 份,乘上 B 维度和 L 维度的步长,得到 A‾\overline{\mathbf A}​

[!NOTE]

Mamba: We remark that while the A\mathbf A parameter could also be selective, it ultimately affects the model only through its interaction with Δ\Delta via A‾=exp⁡(ΔA)\overline{\mathbf A} = \exp(\Delta \mathbf A) (the discretization). Thus selectivity in Δ\Delta is enough to ensure selectivity in (A‾,B‾)(\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))
  • image-20240515175907221

Discretize B. 这里作者直接采用了欧拉形式,B‾=ΔB\overline{\mathbf B} = \Delta \mathbf B,然后计算 B(xi)‾⋅xi\overline{\mathbf B({\mathbf x_i})}\cdot \mathbf x_i.

Python
deltaB_u = torch.einsum("bdl, bnl, bdl -> bdln", delta, B, u)

理解: torch.einsum("bdln, bdl -> bdln", torch.einsum("bdl, bnl -> bdln", delta, B), u)

  • 先是 Δ=(B,D,L)\Delta = (B,D,L) 与 B=(B,N,L)\mathbf {B} = (B,N,L) 广播乘法,得到 B‾=(B,D,N,L)\overline{\mathbf B}=(B,D,N,L),对 state space 每个维度施加相同步长,然后相当于得到了 B×D×LB\times D\times L 个 B‾\overline {\mathbf B} (每个的维度都是 NN),然后再与 B×D×LB\times D\times L 个 scaler x(t)x(t) 单独计算

Scan!!!

Python
for i in range(u.shape[2]):  # L
    # ith [B, D, N] * [B, D, N] + ith [B, D, N]
    x = deltaA[:, :, i] * x + deltaB_u[:, :, i]
    y = torch.einsum('bdn,bn->bd', x, C[:, :, i])
    if i == u.shape[2] - 1:
        last_state = x
    ys.append(y)

由于 A\mathbf A (以及 A(xt)‾\overline{\mathbf A(\mathbf x_t)}) 已经是 diagonal, A(xt)‾⋅ht\overline{\mathbf A(\mathbf x_t)}\cdot \mathbf h_t 也为 element wise product

Final Post Processing

Python
# y.shape = (B, D, L)
y = torch.stack(ys, dim=2)
# skip connection
out = y + u * rearrange(D, "d -> d 1")
out = out * F.silu(z)

CUDA work efficient scan

Tri Dao, FlashAttention, introduced hardware accelerated self-attention, bringing memory requirements from quadratic to linear and also provideing dramatic wall-clock time speedups.

The Blelloch parallel scan

For any binary associative operators, we have a parrallel scan for prefix ops.

https://developer.nvidia.com/gpugems/gpugems3/part-vi-gpu-computing/chapter-39-parallel-prefix-sum-scan-cuda#:~:text=39.2%20Implementation

image-20240511233701898

General Idea: segment tree. For any particular node u:

  • downsweep[N]->left->value = downsweep[N]->value
  • downsweep[N]->right->value = downsweep[N]->value + upsweep[N]->left->value
Mamba parallel scan

With some abuse of notation, let at=A‾,a_t = \overline{\mathbf A}, bt=B(xt)‾⋅xt,b_t = \overline {\mathbf{B}(\mathbf x_t)}\cdot \mathbf x_t, xt≡ht≡[at,bt]x_t \equiv \mathbf h_t \equiv [a_t,b_t]. We can rewrite ht=A‾ht−1+B(xt)‾⋅xt\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:

xi={b0,i=0,(ai⊗xi−1)⊕bi,0<i<n.x_i = \begin{cases} b_0, &i=0, \\ (a_i \otimes x_{i-1})\oplus b_i,\quad &0<i<n. \end{cases}
  • aia_i can be constant matrices / scalers and bib_i can be constant vectors / scalers.

There are some additional requirents for operators ⊗\otimes and ⊕\oplus:

  1. ⊕\oplus is associative, i.e. (a⊕b)⊕c=a⊕(b⊕c)(a\oplus b) \oplus c = a \oplus (b \oplus c)​
    • Here is vector-vector addition
  2. ⊗\otimes is semiassociative, i.e. a⊗(b⊗c)=(a⊙b)⊗ca \otimes (b \otimes c) = (a \odot b) \otimes c
    • Here ⊙\odot is matrix multiplication and ⊗\otimes is matrix-vector multiplication
  3. ⊗\otimes distributes over ⊕\oplus, i.e. a⊗(b⊕c)=(a⊗b)⊕(a⊕c)a \otimes (b \oplus c) = (a \otimes b) \oplus(a \oplus c)

我们先递归展开:

xi=(ai⊗xi−1)⊕bi=(ai⊗((ai−1⊗xi−2)⊕bi−1))⊕bi=(ai⊗ai−1⊗xi−2)⊕(ai⊗bi−1)⊕bi=(ai⊗ai−1⊗((ai−2⊗xi−3)⊕bi−2)⊕(ai⊗bi−1)⊕bi=(ai⊗ai−1⊗ai−2⊗xi−3)⊕(ai⊗ai−1⊗bi−2)⊕(ai⊗bi−1)⊕bi=⋯=(ai⊗ai−1⊗⋯⊗a1⊗b0)⊕(ai⊗ai−1⊗⋯⊗a2⊗b1)⊕(ai⊗ai−1⊗⋯⊗a3⊗b2)⊕(ai⊗ai−1⊗⋯⊗a4⊗b3)⊕⋯⊕(ai⊗bi−1)⊕bi\begin{aligned} x_i &= (a_i\otimes x_{i-1}) \oplus b_i \\ &= (a_i \otimes ((a_{i-1} \otimes x_{i-2})\oplus b_{i-1})) \oplus b_i \\ &= (a_i \otimes a_{i-1} \otimes x_{i-2}) \oplus (a_i \otimes b_{i-1}) \oplus b_i \\ \\ &= (a_i \otimes a_{i-1} \otimes ((a_{i-2} \otimes x_{i-3} )\oplus b_{i-2}) \oplus (a_i \otimes b_{i-1}) \oplus b_i \\ &= (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 \end{aligned}

and one step forward:

xi+1=(ai+1⊗x1)⊕bi+1=(ai+1⊗ai⊗ai−1⊗⋯⊗a1⊗b0)⊕(ai+1⊗ai⊗ai−1⊗⋯⊗a2⊗b1)⊕(ai+1⊗ai⊗ai−1⊗⋯⊗a3⊗b2)⊕(ai+1⊗ai⊗ai−1⊗⋯⊗a4⊗b3)⊕⋯⊕(ai+1⊗ai⊗bi−1)⊕(ai+1⊗bi)⊕bi+1\begin{aligned} x_{i+1} &= (a_{i+1} \otimes x_1) \oplus b_{i+1}\\ &= (a_{i+1}\otimes a_i\otimes a_{i-1} \otimes \cdots \otimes a_1 \otimes b_0) \oplus(a_{i+1}\otimes a_i\otimes a_{i-1} \otimes \cdots \otimes a_2 \otimes b_1) \\ & \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}
  • 观察发现,我们只用维护两个部分:
  • xl,xl+1,…,xmidx_l, x_{l+1}, \ldots, x_{\text{mid}} 和 xmid+1,xmid+2,…,xrx_{\text{mid}+1}, x_{\text{mid+2}},\ldots,x_r
  • 合并成一个大的序列 xl,xl+1,…,xmid,xmid+1,…,xrx_l,x_{l+1}, \ldots, x_{\text{mid}}, x_{\text{mid}+1},\ldots, x_r
  • 只需要把前半段的 {b}\{b\} 乘上后半段的 ∏a\prod a

Consider the pairs:

ci=[ai,bi]c_i = [a_i, b_i]

we can define a new binaty operator ∗* as follows:

ci∗cj:=[aj⊙ai,(aj⊗bi)⊕bj]c_i * c_j := [a_j \odot a_i, (a_j \otimes b_i) \oplus b_j]

The operator ∗* is associative:

Apply the definition of ∗ :(ci∗cj)∗ck=[aj⊙ai,(aj⊗bi)⊕bj]∗ckApply the definition of ∗ again :=[aj⊙ai,(aj⊗bi)⊕bj]∗[ak,bk]=[ak⊙(aj⊙ai),(ak⊗((aj⊗bi)⊕bj))⊕bk]Associativity of ⊙ :=[(ak⊙aj)⊙ai,(ak⊗((aj⊗bi)⊕bj))⊕bk]⊗ distributes ak over ⊕ :=[(ak⊙aj)⊙ai,((ak⊗(aj⊗bi))⊕(ak⊗bj))⊕bk]Associativity of ⊕ :=[(ak⊙aj)⊙ai,(ak⊗(aj⊗bi))⊕((ak⊗bj)⊕bk)]Semi-associativity of ⊗ :=[(ak⊙aj)⊙ai,((ak⊙aj)⊗bi)⊕((ak⊗bj)⊕bk)]Apply the definition of ∗ :=[ai,bi]∗[ak⊙aj,(ak⊗bj)⊕bk]=ci∗(cj∗ck)\begin{aligned} &\text{Apply the definition of $*$ :}\\ (c_i * c_j) * c_k &= [a_j \odot a_i, (a_j \otimes b_i) \oplus b_j] * c_k \\ &\text{Apply the definition of $*$ again :}\\ &= [a_j \odot a_i, (a_j \otimes b_i) \oplus b_j] * [a_k, b_k] \\ &= [a_k \odot (a_j \odot a_i), (a_k \otimes ((a_j \otimes b_i) \oplus b_j)) \oplus b_k]\\ &\text{Associativity of $\odot$ :} \\ &= [(a_k \odot a_j) \odot a_i, (a_k \otimes ((a_j \otimes b_i) \oplus b_j)) \oplus b_k]\\ &\text{$\otimes$ distributes $a_k$ over $\oplus$ :}\\ &=[(a_k \odot a_j) \odot a_i, ((a_k\otimes (a_j \otimes b_i)) \oplus (a_k \otimes b_j)) \oplus b_k] \\ &\text{Associativity of $\oplus$ :} \\ &=[(a_k \odot a_j) \odot a_i, (a_k\otimes (a_j \otimes b_i)) \oplus ((a_k \otimes b_j) \oplus b_k)] \\ &\text{Semi-associativity of $\otimes$ :} \\ &= [(a_k \odot a_j) \odot a_i, ((a_k\odot a_j) \otimes b_i) \oplus ((a_k \otimes b_j) \oplus b_k)] \\ &\text{Apply the definition of $*$ :}\\ &= [a_i,b_i] * [a_k \odot a_j, (a_k \otimes b_j) \oplus b_k] \\ &= c_i * (c_j * c_k) \end{aligned}

We now difine the ordered set

si=[pi,xi]=[a production,{b} segment]\begin{aligned} s_i &= [p_i, x_i]\\ &= [\text{$a$ production}, \text{$\{b\}$ segment}] \end{aligned}

In the case of Mamba, Set s0=[p0,x0]=[A‾,h0]=c0s_0 = [p_0,x_0] = [\overline{\mathbf A}, \mathbf h_0] = c_0, and we have s1=[p1,x1]=[A‾2,A‾h0+B(x1)‾⋅x1]=[A‾,h0]∗[a1,b1]=s0∗c1s_1 = [p_1,x_1] = [\mathbf{\overline A}^2,\mathbf{\overline A} \mathbf h_{0} + \overline {\mathbf{B}(\mathbf x_1)} \cdot \mathbf x_1] = [\overline{\mathbf A}, \mathbf h_0] * [a_1, b_1] = s_0 * c_1​.

By induction, we obtain:

si=[pi,xi]0<i<n=[ai⊙pi−1,(ai⊗xi−1)⊕bi]=[pi−1,xi]∗[ai,bi]=si−1∗ci=si−2∗ci−1∗ci=⋯=s0∗c1∗c2∗⋯∗ci\begin{aligned} s_i &= [p_i, x_i] \quad \quad 0 <i <n\\ &= [a_i \odot p_{i-1}, (a_i \otimes x_{i-1}) \oplus b_i] \\ &=[p_{i-1},x_i] *[a_i,b_i]\\ &= s_{i-1} * c_i \\ &= s_{i-2} * c_{i-1} * c_i \\ &= \cdots \\ &= s_0 * c_1 * c_2 * \cdots * c_i \end{aligned}

https://github.com/state-spaces/mamba/blob/009bec5ee37f586844a3fc89c040a9c1a9d8badf/csrc/selective_scan/selective_scan_common.h#L113

C++
template<>
struct SSMScanOp<float> {
    __device__ __forceinline__ float2 operator()(const float2 &ab0, const float2 &ab1) const {
        return make_float2(ab1.x * ab0.x, ab1.x * ab0.y + ab1.y);
    }
};

Jax implementation, credit

Python
# Various imports
from einops import einsum
import jax
# Jax.lax already has a convenient parallel scan implementation.
import jax.lax as lax
import jax.numpy as jnp

key = jax.random.PRNGKey(seed=42)

B = 1  # batch size
L = 8192 # context length
N = 64  # hidden state size
D = 2  # num in channels
V = 1  # num out channels

# Gets the various fake x_t inputs.
def generate_random_xs(key, num_inputs=L, num_channels=D):
    key, subkey = jax.random.split(key)
    xs = jax.random.lognormal(subkey, shape=(L, D))
    return key, xs

# Gets various fake A matrices. This isshape= actually constant in the paper,
# but it doesn't have to be.
def generate_random_As(key, num_inputs=L, state_size=N):
    key, subkey = jax.random.split(key)
    As = jax.random.lognormal(subkey, shape=(L, N, N))
    return key, As

# Gets various fake B(x_t) matrices.
def generate_random_Bxs(key, num_inputs=L, state_size=N, num_channels=D):
    key, subkey = jax.random.split(key)
    Bxs = jax.random.lognormal(subkey, shape=(L, N, D))
    return key, Bxs

# Gets the b_t term.
def get_bs(xs, Bxs):
    return einsum(Bxs, xs, "l n d, l d -> l n")

# Jax plays nicest with jnp.arrays, so we'll stuff the values inside a
# single array and just unpack things here. I suppose I could use PyTrees
# but please forgive a bit of laziness/hackiness on my part.
def extract(c, state_size):
    assert c.ndim == 1
    assert c.shape[0] == state_size * state_size + state_size
    return (
        c[:state_size * state_size].reshape((state_size, state_size)),
        c[-state_size:].reshape((state_size,))
    )

The operator implementation and test logic

Python
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_b + curr_b)
    ])
vectorized_operator = jax.vmap(operator, in_axes=(0, 0), out_axes=0)

# Actually generate some fake test data.
key, xs = generate_random_xs(key)
key, Bxs = generate_random_Bxs(key)
key, As = generate_random_As(key)

bs = get_bs(xs, Bxs)
cs = jnp.concatenate([As.reshape(-1, N * N), bs], axis=1)

# %%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):
    output = [h_0]
    for a, bx, x in zip(As, Bxs, xs):
        b = einsum(bx, x, "n d, d -> n")
        output.append(a @ output[-1] + b)
    return output[1:]

# %%timeit results on a freebie Google Colab VM:
# 3.34 s ± 313 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
naive_hs = jnp.vstack(
    naive_scan_hs(jnp.zeros((N,)), As, Bxs, xs)
)

# The following returns Array(True, dtype=bool)! Which means that
# we're getting identical results, (allowing for some floating point
# imprecision), regardless of if we're using the naive iterative
# implementation, or the fast parallel implementation.
jnp.allclose(naive_hs, lax_scanned)

Vision Mamba

Vim

VMamba

LocalMamba

Mamba2

[!TIP]

(剧透)大家可以期待一下之后变得越来越像Linear Attention的Mamba2,通过shared data-dependent decay for each head写成matmul的方式来继续scale up N(越来越像RetNet/GLA了)

作者:sonta 链接:https://www.zhihu.com/question/644981978/answer/3406436860

Credits

  1. https://jameschen.io/jekyll/update/2024/02/12/mamba.html#fnref:expensive
  2. https://newsletter.maartengrootendorst.com/p/a-visual-guide-to-mamba-and-state
  3. https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf
  4. https://srush.github.io/annotated-s4/
原文