‹ Lecture 3 07 / 21

Transformer Block & Residual Stream

Attention의 output은 기존 token state를 통째로 대체하는 값이 아니다. Transformer block에서는 현재 state를 읽어 계산한 update를 원래 state에 더한다. 이어 FFN도 같은 방식으로 update를 더한다. 현재 state를 흘려보내면서 각 sublayer가 읽고, 계산하고, 다시 쓰는 공통 경로를 Residual Stream이라고 생각하면 전체 architecture를 따라가기 쉽다.

  1. x0x_{0}
  2. Norm / Attention
  3. +Δ\Delta _attn
  4. x1x_{1}
  5. Norm / FFN
  6. +Δ\Delta _ffn
  7. x2x_{2}
  8. repeat
  9. Final Norm

The Transformer Block

GPT-2 style Pre-Norm block은 Norm으로 읽은 state에서 Attention update를 계산해 더하고, 이어 FFN update를 계산해 더한다.

x1=x0+Attention⁡(Norm⁡(x0))x_1=x_0+\operatorname{Attention}(\operatorname{Norm}(x_0)) x2=x1+FFN⁡(Norm⁡(x1))x_2=x_1+\operatorname{FFN}(\operatorname{Norm}(x_1))

Residual Stream

Xl+1=Xl+Δl(Xl)X_{l+1}=X_l+\Delta_l(X_l)

Residual Stream Xl∈Rn×dmodelX_l\in\mathbb{R}^{n\times d_{\mathrm{model}}}은 각 sublayer가 Norm으로 읽고 +Δ\Delta 를 써넣는 공통 state다. Xl+1=Xl+Δl(Xl)X_{l+1}=X_l+\Delta_l(X_l)로 이어진다. 폭이 유지돼 중간 state에 Logit lens를 적용할 수 있다.

LoRA의 ΔW=BA\Delta W=BA는 선택된 weights의 low-rank update로 sublayer가 쓰는 변화를 바꾼다. Stream 자체를 다른 dimension으로 교체한다는 뜻은 아니다.

Communicate, Then Compute

먼저 Norm으로 읽은 state에서 Causal Self-Attention을 계산한다. 이 sublayer는 허용된 positions의 Values를 섞으므로 token 사이의 communication이다. 그 결과를 x0x_0에 더한 x1x_1은 원래 정보와 읽어 온 정보를 함께 가진다.

다음 FFN은 x1x_1의 각 position에 같은 MLP를 독립적으로 적용한다. 다른 positions를 직접 읽지는 않지만, 입력 자체에는 이미 Attention이 가져온 문맥이 들어 있다. 따라서 position-wise라는 말은 context가 없다는 뜻도, position마다 다른 weights를 쓴다는 뜻도 아니다. Feature를 변환한 output을 다시 x1x_1에 더해 x2x_2를 만든다.

GPT-2 small은 이 block을 12번 반복한다. Attention 12회와 FFN 12회의 updates가 누적되며, hidden width는 768로 유지한다. FFN 내부의 768 → 3072 → 768 확장도 마지막에는 Residual addition을 위해 같은 폭으로 돌아온다. 마지막 block 뒤에는 Final Norm과 Vocabulary projection이 있다.

Reading and Writing the Stream

LoRA:ΔW=BA\text{LoRA:}\quad\Delta W=BA

Residual addition의 xx 경로는 sublayer를 거치지 않고 더해지는 identity path다. ResNet에서 알려진 이 구조는 깊은 network의 gradient 전달을 완화하는 데 연결된다. 다만 update를 계속 더하면 state의 scale도 변할 수 있으므로, sublayer 전후에 Norm을 어디에 두는지가 다음 section의 주제가 된다.

폭이 같은 중간 state를 unembed하여 token scores를 읽는 것이 앞에서 본 Logit lens다. 반면 LoRA는 state의 dimension을 바꾸는 것이 아니라 선택한 projection weight에 low-rank update BABA를 더하여 sublayer가 어떤 update를 쓰는지를 바꾼다. Stream 관점은 이 두 작업이 어디에서 일어나는지 구분하게 해 준다.

Residual Stream에 각 sublayer의 update가 더해지는 흐름을 이해하는 것이 중요하다.

GPT-1의 Post-Norm과 GPT-2의 Pre-Norm은 Norm 위치의 차이다. 이를 Residual connection의 유무와 혼동하지 않는다.

Attention=통신, FFN=위치별 계산. 둘 다 같은 stream에 더한다.

Check

Attention과 FFN의 역할을 Residual Stream의 read/write 관점으로 비교하라.

Answer

둘 다 현재 state를 읽고 update를 더한다. Attention은 다른 allowed positions의 Value를 섞고, FFN은 각 position의 features를 같은 network로 변환한다.

FFN이 4d로 확장한 뒤 다시 dd로 내려오는 것이 Residual 연결에서 중요한 이유는?

Answer

원래 Residual state와 같은 shape여야 더할 수 있다. 중간 width 확장은 내부 계산이며 block의 stream 폭은 dd로 유지된다.

Attention이나 FFN의 output이 Residual Stream을 완전히 교체한다고 생각하면 무엇을 놓치는가?

Answer

각 sublayer는 stream을 읽어 update를 만들고 원래 stream에 더한다. Attention은 allowed positions 사이의 정보를, FFN은 각 position의 features를 변환하는 update를 만든다. 같은 stream에 더하지만 두 계산의 역할은 다르다.

Pre-Norm Decoder block 한 개의 read/compute/add 순서를 말하라.

Answer

x0x_{0} → Norm → causal Attention → x0x_{0}+Δ\Delta _attn=x1x_{1} → Norm → FFN → x1x_{1}+Δ\Delta _ffn=x2x_{2}. 반복 stack 뒤 final Norm과 LM head.

Pre-Norm block과 output 경로의 빈 node를 채우고 두 residual addition을 설명하세요.

Answer

Residual x0x_{0} → Norm → Causal Attention → +Δ\Delta _attn = x1x_{1} → Norm → FFN / MLP → +Δ\Delta _ffn = x2x_{2} → Repeat / Final Norm → LM head / Logits 각 화살표가 입력/처리인지 사용 방식의 역사 흐름인지 구분해서 설명한다.

Contents