attention 연산의 핵심은 3단계. 외워.
- Score:
scores = Q @ K.T / sqrt(d_k). 각 (i, j) 항목이 토큰 i가 토큰 j에 얼마나 attend해야 할지 측정. shape (n, n). - Normalize:
weights = softmax(scores, axis=-1). 각 행이 합 1인 확률 분포가 됨. 토큰 i의 attention이 모든 위치에 분산. - Weighted sum:
out = weights @ V. 각 행이 attention으로 가중된 value 벡터들의 convex combination. shape (n, d_v).
matmul 셋 + softmax. 메커니즘 전체가 이거야. multi-head, causal masking, RoPE, GQA, Flash Attention — 모던 attention의 모든 복잡성이 이 3단계 위에 얹힌 거지, 이걸 바꾸진 않아.