C.W.K.
Stream
Lesson 07 of 13 · published

Self-attention vs cross-attention

~10 min · self-attention, cross-attention

Level 0Token
0 XP0/94 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

attention 두 가지 — 아키텍처의 다른 부분에서 쓰여:

Self-attention

Q, K, V가 다 같은 시퀀스에서 옴. 각 토큰이 같은 입력의 다른 토큰들에 attend. encoder-only 모델(BERT)이 전반적으로 쓰고, decoder-only 모델(Llama)이 유일한 attention 종류로 써.

Cross-attention

Q는 한 시퀀스(decoder)에서, K와 V는 다른 시퀀스(encoder의 출력)에서. decoder의 토큰들이 encoder의 입력 표현을 "본다." encoder-decoder 모델(T5, Whisper)에서 source에 대한 encoder의 이해와 target에 대한 decoder의 생성을 연결.

아키텍처self-attncross-attn
BERT (encoder-only)O (양방향)X
GPT / Llama (decoder-only)O (causal)X
T5 / Whisper (encoder-decoder)encoder에서 O, decoder에서 O (causal)O (decoder Q가 encoder K, V에 attend)

source 문장 S와 생성 중인 target 문장 T가 있는 번역 task에서, cross-attention은 decoder가 n번째 target 토큰 생성 중 S의 전체 encoder 표현을 돌아보고 지금 어떤 source 토큰이 관련 있는지 알아내게 해주는 거야.

Code

Cross-attention in PyTorch·python
class CrossAttention(nn.Module):
    def __init__(self, d_model, n_heads):
        super().__init__()
        self.attn = nn.MultiheadAttention(d_model, n_heads, batch_first=True)
    def forward(self, decoder_x, encoder_out):
        # Q from decoder, K/V from encoder
        # PyTorch's MultiheadAttention takes (query, key, value)
        out, _ = self.attn(decoder_x, encoder_out, encoder_out)
        return out
# Inside an encoder-decoder block:
# x = self_attn(x)              # decoder self-attention (causal)
# x = cross_attn(x, enc_out)    # decoder looks at encoder output
# x = ffn(x)

External links

Exercise

작은 T5 모델 로드. decoder의 layer들 출력. self-attention vs cross-attention layer 구분. 번역 예제 돌리고 한 decoder layer의 cross-attention 가중치 검사 — 각 target 위치에 대해 합리적인 source 토큰을 'looking' 하나?

Progress

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

댓글 0

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

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