C.W.K.
Stream
Lesson 01 of 05 · published

RWKV 가 하려는 거

~14 min · rwkv, rnn, linear-attention

Level 0Observer
0 XP0/50 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Dual-form 트릭, 다시 — 근데 다르게

RWKV (Receptance Weighted Key Value, RwaKuv 로 발음) 는 처음부터 한 목표로 구상된 linear-attention/RNN hybrid: Transformer 처럼 parallel 학습, 추론 때 RNN 으로 실행. SSM 도 비슷한 duality 가 있지만, RWKV 는 attention 에 구조적으로 더 가까워 — parameter 명명 (R, W, K, V) 이 우연 아냐; attention 의 QKV 의 의도적 echo.

네 parameter: R 은 receptance gate (이 토큰이 running summary 를 얼마나 흡수해야 해?), W 는 exponential decay weight (오래된 토큰이 얼마나 빨리 fade?), K 는 key (내가 뭘 가지고 있어?), V 는 value (내가 뭘 출력해?). WKV 메커니즘이 시간에 걸쳐 exponential decay 와 함께 weighted key-value sum 계산 — T×T matrix 절대 materialize 안 해.

왜 SSM 과 다른 sales pitch 인가

SSM 은 control-theory frame 에서 efficiency 팔아 — "history 를 state 로 압축". RWKV 는 attention-frame 에서 같은 efficiency 팔아 — "exponentially decay 하는 recurrent state 로 attention 을 근사". 수학은 인접하게 나오고 (SSD framework 가 결국 관련 있음 보여줌), 엔지니어링과 ergonomic 스토리는 달라. RWKV 모델은 Transformer-shaped checkpoint 처럼 보이고, llama.cpp/MLX/ggml-style stack 에 더 쉽게 port 가능하고, "이 state stable 한가" 걱정 없이 RNN 으로 실행.

토큰 당 constant time 보장

RWKV 가 사주는 거 중 다른 건 그렇게 깔끔한 형태로 안 사주는 거: 구성에 의해 추론 시 토큰 당 constant cost. 모델이 fixed-size state 인 RNN 과 provably equivalent, 그래서 generation latency 가 context length 에 flat. 이게 on-device serving 케이스에 RWKV 를 옳은 선택으로 만드는 거 — 토큰 N+1 생성 cost 가 토큰 1 이든 토큰 100,000 이든 같아.

Code

RWKV 의 WKV recurrence (단순화)·python
# 매 timestep t, input x_t 주어졌을 때:
#   k_t, v_t = K(x_t), V(x_t)
#   r_t = sigmoid(R(x_t))
# 두 running quantity 유지 ('state'):
#   num   <- num * exp(-w) + exp(k_t) * v_t
#   denom <- denom * exp(-w) + exp(k_t)
# Output:
#   wkv_t = num / denom
#   y_t   = r_t * wkv_t
# 'num' 과 'denom' 이 전체 state. t 무관 constant size.

External links

Exercise

Hugging Face 에서 작은 RWKV 모델 가져와 (예: RWKV/rwkv-4-169m-pile) text 생성. 그 다음 prompt length 를 256 → 8K → 64K 토큰으로 늘리고 새 토큰 당 latency 측정. Latency 가 모든 prompt length 에서 flat 해야 (WKV state 가 같은 size), prefill 이 quadratic 으로 자라고 KV-cache memory 가 linear 로 자라는 Transformer 와 대비.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.