ONE is preparing: 01 / INITIALIZING MODEL ROUTER. Evaluating routing across providers.
Current state
01 / INITIALIZING MODEL ROUTER.

Evaluating routing across providers.

Start learning
← Kiến trúc tiên tiến

AI Researcher

Mô hình không gian trạng thái — Đối thủ của Transformer

Vietnamese onlyThe learning content is available in Vietnamese only. The surrounding interface follows your language.

Kiến trúc mô hình tuần tự hiệu quả dựa trên lý thuyết hệ thống điều khiển, xử lý chuỗi dài nhanh hơn Transformer.

lesson115 minKiến trúc tiên tiếnAI Researcher

Dự đoán

Transformer attention tốn O(N^2) theo sequence length. Với context 1M tokens, chi phí attention tăng 1000000^2 = 1 nghìn tỷ phép tính. Có kiến trúc nào đạt chất lượng tương đương mà chỉ O(N)?

SSM (đặc biệt Mamba) là đối thủ thực sự của Transformer! Nén toàn bộ chuỗi vào hidden state — giống ghi chú thay vì đọc lại toàn bộ sách mỗi lần. O(N) thay vì O(N^2), inference nhanh 5x cho chuỗi dài. Nhưng trade-off: kém hơn Transformer ở in-context retrieval.

Khoảnh khắc Aha

Transformer = đọc lại toàn bộ sách mỗi khi cần thông tin (O(N^2)). SSM = ghi chú tóm tắt khi đọc, tra ghi chú khi cần (O(N)). Với chuỗi 100K tokens, Transformer tốn 10.000x compute so với SSM! Nhưng Transformer "nhớ chính xác" hơn (attention), SSM "nhớ tóm tắt" (state compression).

Thử thách

SSM Mamba nén 100K tokens vào hidden state 1024 dims. Bạn hỏi: 'Số điện thoại được nhắc ở đoạn 3 là gì?' SSM có thể trả lời chính xác không?

Nén 100K tokens vào 1024 dims = compression ratio 100:1 → mất chi tiết cụ thể (số điện thoại, tên riêng). Transformer attention CÓ THỂ attend trực tiếp đến token chứa số điện thoại. Đây là điểm yếu chính của SSM — và lý do hybrid (SSM + attention) là xu hướng.

Phương trình SSM

State Space Models (SSM) là kiến trúc tuần tự dựa trên hệ phương trình vi phân, kế thừa ý tưởng hidden state từ RNN nhưng với O(N) complexity thay vì O(N^2) của Transformer, đặc biệt hiệu quả cho bài toán long context.

Phương trình SSM cơ bản:

ht=Aˉht−1+Bˉxt(cập nhật state)h_t = \bar{A} h_{t-1} + \bar{B} x_t \quad (\text{cập nhật state})
yt=Cht+Dxt(output)y_t = C h_t + D x_t \quad (\text{output})

hth_t là hidden state (ghi chú tóm tắt), Aˉ\bar{A} quyết định nhớ/quên, Bˉ\bar{B} quyết định input nào đưa vào state.

Vì sao SSM là O(N)

Tại sao O(N)?

Transformer: Attention(Q,K,V)=softmax(QKTd)V→O(N2⋅d)\text{Transformer: } \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d}}\right)V \quad \rightarrow O(N^2 \cdot d)
SSM: ht=f(ht−1,xt)→O(N⋅d)\text{SSM: } h_t = f(h_{t-1}, x_t) \quad \rightarrow O(N \cdot d)

Dual form: recurrence và convolution

Dual form: Recurrence vs Convolution

  • Recurrence mode: O(1) per token — cho inference (autoregressive generation)
  • Convolution mode: Parallelizable trên GPU — cho training
Recurrence: ht=Aˉht−1+Bˉxt(sequential, inference)\text{Recurrence: } h_t = \bar{A}h_{t-1} + \bar{B}x_t \quad (\text{sequential, inference})
Convolution: y=K∗x,Ki=CAˉiBˉ(parallel, training)\text{Convolution: } y = K * x, \quad K_i = C\bar{A}^i\bar{B} \quad (\text{parallel, training})

Hybrid và code mẫu

Hybrid Architectures (xu hướng hiện tại):

  • Jamba (AI21): Xen kẽ Mamba layers + Attention layers. Throughput 3x, context 256K
  • Mamba-2: Kết hợp structured state space + attention trong 1 framework
  • Zamba: SSM backbone + shared attention layers — parameter efficient

Mamba model inference

from mamba_ssm import Mamba

# Mamba block — thay thế Transformer attention + FFN
mamba = Mamba(
    d_model=2048,      # Hidden dimension
    d_state=128,       # State dimension (ghi chú)
    d_conv=4,          # Local convolution width
    expand=2,          # FFN expansion factor
).cuda()

# Forward pass — O(N) thay vì O(N^2)
import torch
x = torch.randn(1, 100000, 2048).cuda()  # 100K tokens
y = mamba(x)  # O(N) compute, O(1) memory per token

# So sánh inference speed (100K tokens):
# Transformer: 45 seconds (KV cache 32GB)
# Mamba: 3 seconds (state 1MB)
# → 15x nhanh hơn, 32000x ít memory!

Tóm tắt

  • SSM nén chuỗi vào hidden state cố định: O(N) compute, O(1) memory per token vs O(N^2) Transformer.
  • Mamba: selective SSM với input-dependent parameters — 'chọn lọc' thông tin giống attention nhưng O(N).
  • Dual mode: Recurrence cho inference nhanh (O(1)/token), Convolution cho training song song.
  • Nhược điểm: kém Transformer ở in-context retrieval (lossy compression). Hybrid là xu hướng.
  • Jamba, Mamba-2: SSM backbone + attention layers = throughput 3-5x, context 256K+, chất lượng tương đương.

Continue the module

Lesson 9 of 12