The Attention Computation on One Page Attention 계산 한 장 정리

scores  = Q @ K.T / sqrt(d_k)   # score for where to look
weights = softmax(scores)       # normalize into probabilities
out     = weights @ V           # weighted sum of what was looked at
  • Q: what the current token wants to find (the query)
  • K: each token’s index entry, describing itself
  • V: the content that actually gets passed along

The same thing as an equation:

Attention(Q,K,V)=softmax ⁣(QKdk)V\mathrm{Attention}(Q, K, V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

Why divide by dk\sqrt{d_k}: as the dimension grows, the variance of the dot product grows, and softmax saturates. Multi-head is this same computation run hh times in parallel at lower dimension, so it looks at different relationships at once.

scores  = Q @ K.T / sqrt(d_k)   # 어디를 볼지 점수
weights = softmax(scores)       # 확률로 정규화
out     = weights @ V           # 본 곳의 내용을 가중합
  • Q — 지금 토큰이 찾고 싶은 것(질의)
  • K — 각 토큰이 자신을 설명하는 색인
  • V — 실제로 전달할 내용

같은 식을 수식으로 쓰면:

Attention(Q,K,V)=softmax ⁣(QKdk)V\mathrm{Attention}(Q, K, V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

dk\sqrt{d_k} 로 나누는 이유: 차원이 커지면 내적의 분산이 커져 softmax가 포화되기 때문. multi-head는 이 계산을 저차원에서 hh번 병렬로 하는 것 — 서로 다른 관계를 동시에 본다.

← home← 랜딩으로