SSM
State Space Model
State space model. Sequence modeling based on linear systems theory. The core architecture of Mamba. Emerging as an alternative to Transformers.
State Space Model
State space model. Sequence modeling based on linear systems theory. The core architecture of Mamba. Emerging as an alternative to Transformers.
SSM (State Space Model) is a sequence modeling technique that originated in control theory and signal processing. It maps an input sequence u(t) to an output y(t) through a hidden state h(t), and is used by discretizing the continuous-time differential equations h'(t) = Ah(t) + Bu(t), y(t) = Ch(t) + Du(t). The A, B, C, and D matrices determine the dynamic characteristics of the system.
It was effectively applied to deep learning in 2021 in Stanford's S4 (Structured State Spaces for Sequence Modeling) paper. HiPPO (High-order Polynomial Projection Operators) initialization effectively captures long-range dependencies. In 2023, Carnegie Mellon's Mamba drew great attention by showing performance comparable to Transformer with Selective SSM. It reduces the O(n^2) complexity of Transformer to O(n), making it remarkably advantageous for processing long sequences.
The key advantages are linear time complexity and, in theory, unlimited context. In a Transformer, the KV cache grows in proportion to the sequence length, whereas an SSM compresses past information into a fixed-size state (usually 16-64 dimensions). In Mamba's Selective SSM, the A, B, and C parameters change dynamically depending on the input, so it selectively remembers relevant information like Attention. The computation per token during inference is constant, making it ideal for streaming generation.
Mamba 2 (2024) achieved 2-8x faster training than Mamba 1 through a hardware-friendly design. Large-scale SSM-based models such as Jamba (AI21, a Mamba+Attention hybrid), Zamba (Zyphra), and Falcon Mamba are emerging. It shows strengths in DNA/protein sequences (100K+ length), audio (16kHz x several minutes), long document processing, and real-time streaming, and is attracting attention as a next-generation architecture that can complement or replace Transformer.
import torch
import torch.nn as nn
from mamba_ssm import Mamba
# Mamba ๋ธ๋ก ์ฌ์ฉ ์์ (mamba-ssm ๋ผ์ด๋ธ๋ฌ๋ฆฌ)
class MambaLM(nn.Module):
"""Mamba ๊ธฐ๋ฐ ์ธ์ด ๋ชจ๋ธ"""
def __init__(
self,
vocab_size: int = 50257,
d_model: int = 768,
n_layer: int = 12,
d_state: int = 16,
expand: int = 2
):
super().__init__()
self.embedding = nn.Embedding(vocab_size, d_model)
# Mamba ๋ ์ด์ด ์คํ
self.layers = nn.ModuleList([
Mamba(
d_model=d_model,
d_state=d_state, # SSM ์ํ ์ฐจ์
d_conv=4, # ๋ก์ปฌ ์ปจ๋ณผ๋ฃจ์
ํฌ๊ธฐ
expand=expand # ํ์ฅ ๋น์จ
)
for _ in range(n_layer)
])
self.norm = nn.LayerNorm(d_model)
self.lm_head = nn.Linear(d_model, vocab_size, bias=False)
def forward(self, input_ids: torch.Tensor) -> torch.Tensor:
x = self.embedding(input_ids)
for layer in self.layers:
x = layer(x) + x # Residual connection
x = self.norm(x)
logits = self.lm_head(x)
return logits
# ๋ชจ๋ธ ์์ฑ ๋ฐ ์ถ๋ก
model = MambaLM(vocab_size=50257, d_model=768, n_layer=12)
input_ids = torch.randint(0, 50257, (1, 2048)) # ๋ฐฐ์น 1, ๊ธธ์ด 2048
with torch.no_grad():
logits = model(input_ids)
print(f"์ถ๋ ฅ shape: {logits.shape}") # [1, 2048, 50257]
# Transformer ๋๋น ์ฅ์ :
# - ์ถ๋ก : O(n) vs O(n^2) - ๊ธด ์ํ์ค์์ 5-10๋ฐฐ ๋น ๋ฆ
# - ๋ฉ๋ชจ๋ฆฌ: ๊ณ ์ ์ํ ํฌ๊ธฐ vs ์ฆ๊ฐํ๋ KV ์บ์
# - ํ์ต: FlashAttention ์์ด๋ ํจ์จ์
# === Mamba 2 (State Space Duality) ===
# Mamba 2๋ SSM๊ณผ Attention์ ์ด๋ก ์ ์ฐ๊ฒฐ์ฑ์ ํ์ฉ
# ๋ ํ๋์จ์ด ์นํ์ ์ธ ์ค๊ณ๋ก 2-8๋ฐฐ ๋น ๋ฅธ ํ์ต
# === Jamba: Hybrid SSM + Attention ===
# AI21์ Jamba๋ Mamba์ Attention์ ๊ต์ฐจ ๋ฐฐ์น
# SSM์ ํจ์จ์ฑ + Attention์ in-context learning ๋ฅ๋ ฅ
from transformers import AutoModelForCausalLM
# Jamba ๋ชจ๋ธ ์ฌ์ฉ ์์ (52B ํ๋ผ๋ฏธํฐ, 140K ์ปจํ
์คํธ)
jamba_model = AutoModelForCausalLM.from_pretrained(
"ai21labs/Jamba-v0.1",
device_map="auto",
torch_dtype=torch.bfloat16
)
# === ๋ณต์ก๋ ๋น๊ต ===
def complexity_comparison(seq_len):
"""Transformer vs SSM ๋ณต์ก๋ ๋น๊ต"""
transformer_attention = seq_len ** 2 # O(n^2)
transformer_kv_cache = seq_len * 2 * 768 # 2 * d_model per layer
ssm_compute = seq_len # O(n)
ssm_state = 64 # ๊ณ ์ ์ํ ํฌ๊ธฐ (d_state)
print(f"์ํ์ค ๊ธธ์ด: {seq_len:,}")
print(f"Transformer Attention ์ฐ์ฐ: {transformer_attention:,}")
print(f"Transformer KV Cache (1 layer): {transformer_kv_cache:,} floats")
print(f"SSM ์ฐ์ฐ (ํ ํฐ๋น): {ssm_compute:,}")
print(f"SSM ์ํ ํฌ๊ธฐ: {ssm_state} (๊ณ ์ )")
print(f"ํจ์จ์ฑ ๋น์จ: {transformer_attention / seq_len:.0f}x")
complexity_comparison(100000) # 100K ํ ํฐ
# === HiPPO ์ด๊ธฐํ (๊ธด ์์กด์ฑ ํ์ต) ===
# HiPPO(High-order Polynomial Projection Operators)๋
# ๊ธด ์ํ์ค ์์กด์ฑ์ ํจ๊ณผ์ ์ผ๋ก ํฌ์ฐฉํ๋ A ํ๋ ฌ ์ด๊ธฐํ
# === SSM vs RNN vs Transformer ๋น๊ต ===
# | | ํ์ต ๋ณต์ก๋ | ์ถ๋ก ๋ณต์ก๋ | ๋ฉ๋ชจ๋ฆฌ | ๊ธด ์์กด์ฑ |
# |-----------|------------|------------|--------|----------|
# | RNN | O(n) | O(1)/ํ ํฐ | O(1) | ์ด๋ ค์ |
# | Transformer| O(n^2) | O(n)/ํ ํฐ | O(n) | ์ข์ |
# | SSM | O(n) | O(1)/ํ ํฐ | O(1) | ์ข์ |
# SSM์ RNN์ ํจ์จ์ฑ + Transformer์ ์ฑ๋ฅ์ ๊ฒฐํฉ!
# === ์ค์ ์์ฉ ๋ถ์ผ ===
# 1. DNA/๋จ๋ฐฑ์ง ์ํ์ค (์์ญ๋ง base pair)
# 2. ์ค๋์ค ์ฒ๋ฆฌ (16kHz x ์ ๋ถ = ์๋ฐฑ๋ง ์ํ)
# 3. ์ด์ฅ๋ฌธ ๋ฌธ์ (๋ฒ๋ฅ , ์๋ฃ, ์ฝ๋๋ฒ ์ด์ค)
# 4. ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ (์ผ์ ํ ์ง์ฐ์๊ฐ ๋ณด์ฅ)
# === Mamba ์คํ์ผ Selective SSM ํต์ฌ ===
# ๊ธฐ์กด SSM: A, B, C๊ฐ ์
๋ ฅ๊ณผ ๋ฌด๊ดํ ๊ณ ์ ๊ฐ
# Selective SSM: A, B, C๊ฐ ์
๋ ฅ x์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์
# x -> Linear -> B, C, delta ์์ฑ
# delta๋ก A๋ฅผ ์ด์ฐํ (๋ค๋ฅธ ์๊ฐ ํด์๋ ์ ์ฉ)
# ์ด๋ฅผ ํตํด ์
๋ ฅ์ ๋ฐ๋ผ ๋ฌด์์ ๊ธฐ์ต/๋ง๊ฐํ ์ง ์ ํ
# === State Space Duality (Mamba 2) ===
# SSM๊ณผ Attention์ด ์ํ์ ์ผ๋ก ์ฐ๊ฒฐ๋๋ค๋ ๋ฐ๊ฒฌ
# ํน์ ์กฐ๊ฑด์์ SSM์ ์ถ๋ ฅ = ๊ตฌ์กฐํ๋ Attention๊ณผ ๋์ผ
# ์ด๋ฅผ ํ์ฉํด ๋ ํ๋์จ์ด ์นํ์ ์ธ ๊ตฌํ ๊ฐ๋ฅ
# === Zamba (Zyphra) ===
# Mamba + Shared Attention ํ์ด๋ธ๋ฆฌ๋
# ์ผ๋ถ ๋ ์ด์ด๋ง Attention์ ๊ณต์ ํ์ฌ ํจ์จ์ฑ ๊ทน๋ํ
# 7B ๊ท๋ชจ์์ Llama 2 13B ์์ค ์ฑ๋ฅ
# === Falcon Mamba (Technology Innovation Institute) ===
# ์์ Mamba ์ํคํ
์ฒ๋ก 7B ๋ชจ๋ธ
# Transformer ์์ด ๊ฒฝ์๋ ฅ ์๋ ์ฑ๋ฅ ๋ฌ์ฑ
# === ์ฃผ์ SSM ๋ชจ๋ธ ๋น๊ต ===
# | ๋ชจ๋ธ | ํฌ๊ธฐ | ์ปจํ
์คํธ | ํน์ง |
# |----------|-------|---------|-------------------|
# | Mamba | 3B | ๋ฌด์ ํ | ์์ SSM |
# | Mamba 2 | 2.8B | ๋ฌด์ ํ | ํ๋์จ์ด ์ต์ ํ |
# | Jamba | 52B | 256K | SSM + Attention |
# | Zamba | 7B | ๋ฌด์ ํ | Shared Attention |
# | RecurrentGemma | 9B | ๋ฌด์ ํ | RG-LRU ๊ธฐ๋ฐ |
# === SSM ํ์ต ํ ===
# 1. ์ด๊ธฐํ: HiPPO ์ด๊ธฐํ๊ฐ ๊ธด ์์กด์ฑ ํ์ต์ ํ์
# 2. ์ ๊ทํ: LayerNorm๋ณด๋ค RMSNorm์ด ๋ ํจ๊ณผ์
# 3. ์์ฐจ ์ฐ๊ฒฐ: ๋ชจ๋ ๋ธ๋ก์ residual connection ์ ์ฉ
# 4. ํผํฉ: Mamba + MLP ๋๋ Mamba + Attention ํผํฉ ๊ณ ๋ ค
# === ๋ฒค์น๋งํฌ ์ฑ๋ฅ (Mamba 3B vs Transformer 3B) ===
# | ํ์คํฌ | Mamba 3B | Transformer 3B | ๋น๊ณ |
# |-------|----------|----------------|-----|
# | HellaSwag | 74.2 | 73.8 | ๋๋ฑ |
# | WinoGrande | 68.5 | 68.1 | ๋๋ฑ |
# | ์ถ๋ก ์๋ | 5x faster | baseline | Mamba ์ฐ์ |
"If you need to process long documents of 100K tokens or more, consider Mamba or Jamba. With Transformer, a KV cache at 100K requires tens of GB of memory, but with SSM the state size is fixed, so it can be processed in under 1GB."
"SSM is a state space model that processes sequences by discretizing the differential equations x'=Ax+Bu, y=Cx. Mamba uses Selective SSM, so the A, B, C parameters change dynamically depending on the input, making context awareness possible like Attention. The complexity is O(n)."
"For real-time streaming inference, SSM is clearly advantageous. Transformer references the entire KV cache for every token, but Mamba only needs to update a small state, so the per-token latency is constant."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Transformer is still superior in in-context learning and complex reasoning. SSM has strengths in long sequence processing and streaming.
Mamba depends on CUDA kernels and requires a specific GPU (Ampere+) and CUDA version. Check your environment setup first.
Consider a hybrid model (such as Jamba, which combines Transformer + Mamba), or choose according to your use case. SSM is strong in DNA, audio, and long documents.