27. Transformers
Self-Attention
Modeling sequence without recurrence
Review: Sequence-to-Sequence with Attention

If Not Recurrence, Then What? How About Attention?
- Attention์ ๊ฐ ๋จ์ด์ ํํ์ Query๋ก ์ทจ๊ธํ์ฌ Value ์งํฉ์ ์ ๋ณด์ ์ ๊ทผ ๋ฐ ํตํฉํจ.
- Decoder์์ Encoder๋ก์ Attention์ด ์๋, ๋จ์ผ ๋ฌธ์ฅ ๋ด์์์ Attention Self-Attention!
- Attention ์ฌ์ฉ ์, sequence ๊ธธ์ด์ ๋ฐ๋ฅธ ๋ณ๋ ฌํ ๋ถ๊ฐ๋ฅ ์ฐ์ฐ ์๊ฐ ์ฆ๊ฐํ์ง ์์.
- ์ต๋ ์ํธ์์ฉ ๊ฑฐ๋ฆฌ: (๋ชจ๋ ๋จ์ด๊ฐ ๋ชจ๋ ๋ ์ด์ด์์ ์ํธ์์ฉํ๊ธฐ ๋๋ฌธ).

Self-Attention: Keys, Queries, Values from the Same Sequence
- ๋จ์ด sequence (์ดํ ) ๊ฐ์
- ์: "Zuko made his uncle tea"
- ๊ฐ ์ ๋ํด (๋ ์๋ฒ ๋ฉ ํ๋ ฌ)
- ๊ฐ ๋จ์ด ์๋ฒ ๋ฉ์ ๊ฐ์ค์น ํ๋ ฌ (๊ฐ )๋ก ๋ณํํจ.
- (Queries), (Keys), (Values).
- Key์ Query ๊ฐ์ ์๋ณ(pairwise) ์ ์ฌ๋ ๊ณ์ฐ ๋ฐ ๋ก ์ ๊ทํ
- Value์ ๊ฐ์ค ํฉ์ผ๋ก ๊ฐ ๋จ์ด์ ์ถ๋ ฅ ๊ณ์ฐ
Self-Attention as a Building Block
- ๋ค์ด์ด๊ทธ๋จ๊ณผ ๊ฐ์ด LSTM ๋ ์ด์ด๋ฅผ ์๋ ๊ฒ์ฒ๋ผ Self-Attention ๋ธ๋ก์ ์์.
- Self-Attention์ด ์ํ(Recurrence)์ ์์ ํ ๋์ฒด์ ๊ฐ ๋ ์ ์๋๊ฐ?
- No. ๋ช ๊ฐ์ง ๋ฌธ์ ๊ฐ ์กด์ฌํ๋ฉฐ ์ด๋ฅผ ์ดํด๋ณด๊ณ ์ ํจ.
- ์ฒซ์งธ, Self-Attention์ ์งํฉ(Set)์ ๋ํ ์ฐ์ฐ์. ์์(Order)์ ๋ํ ๋ด์ฌ์ ๊ฐ๋ ์ด ์์.
Self-Attention์ ์ ๋ ฅ์ ์์์ ๋ํด์ ์์ง ๋ชปํจ.
Barriers & Solutions for Self-Attention as A Building Block

Fixing the First Self-Attention Problem: Sequence Order
- Self-Attention์๋ ์์ ์ ๋ณด๊ฐ ์์ผ๋ฏ๋ก Key, Query, Value์ ๋ฌธ์ฅ์ ์์๋ฅผ ์ธ์ฝ๋ฉํด์ผ ํจ.
- ๊ฐ sequence ์ธ๋ฑ์ค๋ฅผ ๋ฒกํฐ๋ก ํํํ๋ค๊ณ ๊ฐ์
- , ์ ์์น ๋ฒกํฐ์.
- ์ ๊ตฌ์ฑ ๋ฐฉ์์ ์์ง ๊ณ ๋ คํ์ง ์์.
- ์ด ์ ๋ณด๋ฅผ Self-Attention ๋ธ๋ก์ ํตํฉํ๋ ๊ฒ์ ๊ฐ๋จํจ: ์ ๋ ฅ์ ๋ฅผ ๋ํจ.
- ๋ ์ธ๋ฑ์ค ์ ๋จ์ด ์๋ฒ ๋ฉ์์ ๊ธฐ์ต
- ์์น ์ ๋ณด๊ฐ ํฌํจ๋ ์๋ฒ ๋ฉ
Position Representation Vectors Through Sinusoids
- Sinusoidal position representations(์ฌ์ธํ ์์น ํํ)
- ๋ค์ํ ์ฃผ๊ธฐ์ ์ฌ์ธ ํจ์๋ฅผ ์ฐ๊ฒฐ
- ์๋์ transformer ๋ ผ๋ฌธ์์ ์ ์๋ ๋ฐฉ์
- ์ฅ์
- ์ฃผ๊ธฐ์ฑ์ "์ ๋ ์์น"๊ฐ ์ค์ํ์ง ์์ ์ ์์์ ์์ฌ
- ์ฃผ๊ธฐ๊ฐ ๋ค์ ์์๋๋ฏ๋ก ๋ ๊ธด sequence๋ก extrapolate(์ธ์ฝ) ๊ฐ๋ฅํ ์ ์์.
- ๋จ์
- Not learnable(ํ์ต๋์ง ์์).
- ์ค์ ๋ก๋ ์ธ์ฝ์ด ์ ์๋ํ์ง ์์.

Position Representation Vectors Learned from Scratch
- Learned absolute position representations(ํ์ต๋ ์ ๋ ์์น ํํ)
- ๋ชจ๋ ๋ฅผ ํ์ต ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ๋ก ์ค์
- ํ๋ ฌ ์ ํ์ตํ๊ณ ๊ฐ ๋ฅผ ํด๋น ํ๋ ฌ์ ์ด๋ก ์ฌ์ฉ
- ๋๋ถ๋ถ์ ์์คํ ์์ ์ด ๋ฐฉ์์ ์ฌ์ฉ
- ์ฅ์ : ์ ์ฐ์ฑ(Flexibility)
- ๊ฐ ์์น๊ฐ ๋ฐ์ดํฐ์ ์ ํฉํ๋๋ก ํ์ต๋จ.
- ๋จ์ : ์ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ์ธ๋ฑ์ค๋ก๋ ์ ๋ ์ธ์ฝ ๋ถ๊ฐ๋ฅ
- ๋๋๋ก ๋ ์ ์ฐํ ์์น ํํ์ ์๋ํ๊ธฐ๋ ํจ
- Relative linear position Attention(์๋ ์ ํ ์์น ์ดํ ์ )
- Dependency syntax-based position(์์กด ๊ตฌ๋ฌธ-๊ธฐ๋ฐ ์์น)
- Rotary position embedding (๋กํฐ๋ฆฌ ์์น ์๋ฒ ๋ฉ, RoPE)
Adding Non-Linearities in Self-Attention
- Self-Attention์๋ elementwise(์์๋ณ) ๋น์ ํ์ฑ์ด ์กด์ฌํ์ง ์์.
- Self-Attention ๋ ์ด์ด๋ฅผ ๋ ์๋ ๊ฒ์ ๋จ์ํ Value ๋ฒกํฐ๋ฅผ re-averaging(์ฌํ๊ท ํ)ํ๋ ๊ฒ์ ๋ถ๊ณผํจ.
- ๊ฐ๋จํ ํด๊ฒฐ์ฑ
- ๊ฐ ์ถ๋ ฅ ๋ฒกํฐ๋ฅผ ํ์ฒ๋ฆฌํ๊ธฐ ์ํด Feed-Forward Network ์ถ๊ฐ
Masking the Future in Self-Attention
- Decoder์์ Self-Attention์ ์ฌ์ฉํ๋ ค๋ฉด ๋ฏธ๋๋ฅผ ๋ณผ ์ ์๋๋ก ํด์ผ ํจ.
- ๋งค timestep๋ง๋ค Key์ Query ์งํฉ์ ๊ณผ๊ฑฐ ๋จ์ด๋ง ํฌํจํ๋๋ก ๋ณ๊ฒฝํ๋ ๊ฒ๋ ๊ฐ๋ฅ
- Parallelization(๋ณ๋ ฌํ)๋ฅผ ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ ์ํด, ๋ฏธ๋ ๋จ์ด์ ๋ํ Attention ์ ์๋ฅผ ๋ก ์ค์ ํ์ฌ ๋ง์คํน ์ฒ๋ฆฌ
Necessities for a Self-Attention Building Block
- Self-Attention
- ๋ฐฉ๋ฒ๋ก ์ ๊ธฐ์ด
- Position Representations
- Self-Attention์ ์ ๋ ฅ์ ์์๋ฅผ ๊ณ ๋ คํ์ง ์๋ ํจ์์ด๋ฏ๋ก sequence ์์๋ฅผ ๋ช ์ํด์ผ ํจ.
- Nonlinearities
- Self-Attention ๋ธ๋ก์ ์ถ๋ ฅ ๋ถ๋ถ์ ์์น
- ์ฃผ๋ก ๊ฐ๋จํ Feed-Forward Network๋ก ๊ตฌํ๋จ.
- Masking
- ๋ฏธ๋๋ฅผ ๋ณด์ง ์์ผ๋ฉด์ ์ฐ์ฐ์ ๋ณ๋ ฌํํ๊ธฐ ์ํจ
- ๋ฏธ๋์ ์ ๋ณด๊ฐ ๊ณผ๊ฑฐ๋ก "์ ์ถ"๋๋ ๊ฒ์ ๋ฐฉ์ง
Transformers
Self-Attention์ ๊ธฐ๋ฐํ Neural Networks
Transformer
- ์ค์ง Attention ๋ฉ์ปค๋์ฆ์ผ๋ก๋ง ์ค๊ณ๋ ์ ๊ฒฝ๋ง ์ํคํ
์ฒ (CNN์ด๋ RNN ์์ด).
"Attention is all you need." (Ashish Vaswani et al., NeurIPS 2017)
- Self-Attention ๋ฉ์ปค๋์ฆ์ด ํต์ฌ์ ์์น
- ํํ์ ๋ณ๋ ฌ ๊ณ์ฐ ๊ฐ๋ฅ Scalability(ํ์ฅ์ฑ)
- ๋ณธ๋ Machine Translation(Seq2Seq ์ํคํ
์ฒ)์ ์ํด ์ ์๋จ.

The Transformer Decoder
- Transformer Decoder๋ Language Model๊ณผ ๊ฐ์ ์์คํ
์ ๊ตฌ์ถํ๋ ๋ฐฉ์
- ์ต์ํ์ Self-Attention ์ํคํ ์ฒ์ ์ ์ฌํ์ง๋ง ๋ช ๊ฐ์ง ๊ตฌ์ฑ ์์๊ฐ ์ถ๊ฐ๋จ.
- Embedding๊ณผ position embedding์ ๋์ผ
- ๋ค์์ผ๋ก Self-Attention์ Multi-Head Self-Attention์ผ๋ก ๋์ฒดํ ๊ฒ

Recall the Self-Attention Hypothetical Example

Hypothetical Example of Multi-Head Attention

- Attention head 1์ entity(๊ฐ์ฒด)๋ค์ ์ง์ค
- Attention head 2๋ ๊ตฌ๋ฌธ์ (๋ฌธ๋ฒ์ )์ผ๋ก ๊ด๋ จ๋ ๋จ์ด๋ค์ ์ง์ค
Sequence-Stacked Form of Attention
- ํ๋ ฌ์ ํตํ Key-Query-Value Attention ๊ณ์ฐ ๋ฐฉ์
- ๋ฅผ ์ ๋ ฅ ๋ฒกํฐ์ concatenation์ผ๋ก ์ ์
- ์ถ๋ ฅ ์ ์

Multi-Headed Attention
- ๋ฌธ์ฅ์ ์ฌ๋ฌ ์์น๋ฅผ ๋์์ ๋ณด๊ณ ์ถ๋ค๋ฉด?
- ๋จ์ด ์ ๋ํด Self-Attention์ ๊ฐ ๋์ ๊ณณ์ ๋ณด์ง๋ง, ๋ค๋ฅธ ์ด์ ๋ก ๋ค๋ฅธ ์ ์ง์คํด์ผ ํ ์๋ ์์.
- ์ฌ๋ฌ ๊ฐ์ ํ๋ ฌ์ ํตํด ๋ค์์ Attention "Head"๋ฅผ ์ ์
- (๋ Attention Head์ ์, ์ 1๋ถํฐ ๊น์ง).
- ๊ฐ Attention Head๋ ๋ ๋ฆฝ์ ์ผ๋ก Attention ์ํ
- ์ฌ๊ธฐ์ .
- ๊ทธ ํ ๋ชจ๋ head์ ์ถ๋ ฅ์ ๊ฒฐํฉ!
- ์ฌ๊ธฐ์ .
- ๊ฐ head๋ ์๋ก ๋ค๋ฅธ ๊ฒ์ ๋ณด๊ณ , Value ๋ฒกํฐ๋ฅผ ๋ค๋ฅด๊ฒ ๊ตฌ์ฑ
Multi-Head Self-Attention is Computationally Efficient
- ๊ฐ์ Attention head๋ฅผ ๊ณ์ฐํ๋๋ผ๋ ๋น์ฉ์ด ํฌ๊ฒ ์ฆ๊ฐํ์ง ์์.
- ๋ฅผ ๊ณ์ฐํ ํ ๋ก reshapeํจ (, ๋ ๋์ผ).
- ๊ทธ ํ ๋ก transpose.
- ์ด์ head ์ถ์ด batch ์ถ์ฒ๋ผ ๋์
- ๊ฑฐ์ ๋ชจ๋ ๊ณผ์ ์ด ๋์ผํ๋ฉฐ, ํ๋ ฌ ํฌ๊ธฐ๋ ๋์ผ
Scaled Dot Product [Vaswani et al., 2017]
- "Scaled Dot Product" Attention์ ํ์ต์ ๋์.
- ์ฐจ์ ๊ฐ ์ปค์ง๋ฉด ๋ฒกํฐ ๊ฐ ๋ด์ (Dot product) ๊ฐ์ด ์ปค์ง๋ ๊ฒฝํฅ์ด ์์.
- ์ด๋ก ์ธํด ํจ์์ ์ ๋ ฅ๊ฐ์ด ์ปค์ ธ ๊ธฐ์ธ๊ธฐ(Gradient)๊ฐ ์์์ง.
- ํด๊ฒฐ์ฑ
- ๊ธฐ์กด์ ๋ณธ Self-Attention ํจ์ ๋์ , Attention ์ ์๋ฅผ ๋ก ๋๋์ด ์ฐจ์ ์()์ ๋ฐ๋ผ ์ ์๊ฐ ์ปค์ง๋ ๊ฒ์ ๋ฐฉ์ง
The Transformer Decoder
- ๋ ๊ฐ์ง ์ต์ ํ trick
- Residual Connections(์์ฐจ ์ฐ๊ฒฐ)
- Layer Normalization(๋ ์ด์ด ์ ๊ทํ)
- ๋๋ถ๋ถ์ Transformer ๋ค์ด์ด๊ทธ๋จ์์ ์ด๋ค์ "Add & Norm"์ผ๋ก ํจ๊ป ํ๊ธฐ๋จ.
Residual Connections [He et al., 2016]
- Residual connections๋ ๋ชจ๋ธ ํ์ต์ ๋๋ trick
- (๋ ๋ ์ด์ด) ๋์ ,
- ๋ก ์ค์ (์ด์ ๋ ์ด์ด๋ก๋ถํฐ์ "์์ฐจ"๋ง ํ์ตํ๋ฉด ๋จ)

- ์์ฐจ ์ฐ๊ฒฐ์ ํตํ ๊ธฐ์ธ๊ธฐ ์ ํ๊ฐ ์ํ (๊ธฐ์ธ๊ธฐ๊ฐ 1์)


Layer Normalization [Ba et al., 2016]
- Layer normalization์ ๋ชจ๋ธ ํ์ต ์๋๋ฅผ ๋์ด๋ trick
- Idea: ๊ฐ ๋ ์ด์ด ๋ด์์ ํ๊ท ์ 0, ํ์คํธ์ฐจ๋ฅผ 1๋ก ์ ๊ทํํ์ฌ ์๋ ๋ฒกํฐ ๊ฐ์ ์ ๋ณด ์๋ ๋ณ๋์ ์ค์.
- LayerNorm์ ์ฑ๊ณต ์์ธ์ด ๊ธฐ์ธ๊ธฐ ์ ๊ทํ๋ก ์ถ์ [Xu et al., 2019]
- ์ธ๋ถ ์ฌํญ
- ๋ฅผ ๋ชจ๋ธ์ ๊ฐ๋ณ (๋จ์ด) ๋ฒกํฐ๋ผ ๊ฐ์
- : ํ๊ท ().
- : ํ์คํธ์ฐจ ().
- ์ ๋ ํ์ต๋ "Gain"๊ณผ "Bias" ํ๋ผ๋ฏธํฐ (์๋ต ๊ฐ๋ฅ)
- Layer Normalization ๊ณ์ฐ ์ํ
Output = $\frac{x - \mu}{\sigma} \cdot \gamma + \beta
The Transformer Decoder
- Transformer Decoder Block๋ค์ Stack ๊ตฌ์กฐ์ธ Transformer Decoder
- ๊ฐ Block ๊ตฌ์ฑ ์์
- Self-Attention
- Add & Norm
- Feed-Forward
- Add & Norm

The Transformer Encoder
- Transformer Decoder๋ Language Model๊ณผ ๊ฐ์ด Unidirectional(๋จ๋ฐฉํฅ) ๋ฌธ๋งฅ์ผ๋ก ์ ํ๋จ.
- Bidirectional(์๋ฐฉํฅ) RNN์ฒ๋ผ ์๋ฐฉํฅ ๋ฌธ๋งฅ์ ์ํ๋ค๋ฉด?
- ์ด๊ฒ์ด Transformer Encoder
- ์ ์ผํ ์ฐจ์ด์ ์ Self-Attention์์ ๋ง์คํน์ ์ ๊ฑฐํ๋ค๋ ์
The Transformer Encoder-Decoder
- Machine Translation์์ ์์ค ๋ฌธ์ฅ์ ์๋ฐฉํฅ ๋ชจ๋ธ๋ก ์ฒ๋ฆฌํ๊ณ ํ๊ฒ์ ๋จ๋ฐฉํฅ ๋ชจ๋ธ๋ก ์์ฑํ๋ ๊ฒ์ ์๊ธฐ
- ์ด๋ฌํ Seq2Seq ํ์์ ์ํด ์ฃผ๋ก Transformer Encoder-Decoder๋ฅผ ์ฌ์ฉ
- ์ผ๋ฐ์ ์ธ Transformer Encoder ์ฌ์ฉ
- Transformer Decoder๋ Encoder์ ์ถ๋ ฅ์ ๋ํด Cross-Attention์ ์ํํ๋๋ก ์์ ๋จ.
Cross-Attention
- Self-Attention์ Key, Query, Value๊ฐ ๋์ผํ ์์ค์์ ์ด.
- Decoder์์๋ ์ด์ ์ ๋ณธ ๊ฒ๊ณผ ๋ ์ ์ฌํ ํํ์ Attention์ ๊ฐ์ง.
- ์ Transformer Encoder์ ์ถ๋ ฅ ๋ฒกํฐ๋ผ ํจ ().
- ์ Transformer Decoder์ ์ ๋ ฅ ๋ฒกํฐ๋ผ ํจ ().
- Key์ Value๋ Encoder์์ ์ถ์ถ๋จ (๋ฉ๋ชจ๋ฆฌ ์ญํ ).
- Query๋ Decoder์์ ์ถ์ถ๋จ.

A Graphical Explanation of Transformers (3Blue 1Brown)
- Transformers (how LLMs work) explained visually
- Attention in transformers, visually explained



