Transformer๋ sequence๋ฅผ ์์๋๋ก ํ๋์ฉ ์ฒ๋ฆฌํ๋ ๋์ , sequence ์์ token๋ค์ด ์๋ก ์ด๋ค ๊ด๊ณ๋ฅผ ๋งบ๋์ง attention์ผ๋ก ์ง์ ๊ณ์ฐํ๋ ์ ๊ฒฝ๋ง ๊ตฌ์กฐ์ ๋๋ค. 2017๋ ๋ ผ๋ฌธ Attention Is All You Need์์ ์ ์๋์๊ณ , ์ดํ BERT์ GPT ๊ณ์ด ๋ชจ๋ธ์ ๊ธฐ๋ณธ ๋ผ๋๊ฐ ๋์์ต๋๋ค.
ํ ๋ฌธ์ฅ์ผ๋ก ๋งํ๋ฉด Transformer๋ ๊ฐ token์ด ๋ค๋ฅธ token์ ์ผ๋ง๋ ์ฐธ๊ณ ํด์ผ ํ๋์ง๋ฅผ ๊ณ์ฐํ๊ณ , ๊ทธ ์ ๋ณด๋ฅผ ์ฌ๋ฌ ์ธต์ผ๋ก ์์ ๋ฌธ๋งฅ ํํ์ ๋ง๋๋ ๊ตฌ์กฐ์ ๋๋ค.
์ ํ์ํ๊ฐ
๋ฌธ์ฅ์ ์์๋ฅผ ๊ฐ์ต๋๋ค. ๊ทธ๋์ ๊ณผ๊ฑฐ์๋ RNN์ฒ๋ผ ์์์ ๋ค๋ก ์ํ๋ฅผ ๋๊ธฐ๋ ๋ฐฉ์์ด ์์ฐ์ค๋ฝ๊ฒ ์ฐ์์ต๋๋ค. ํ์ง๋ง ์ด ๋ฐฉ์์๋ ๋ ๊ฐ์ง ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
์ฒซ์งธ, ๊ณ์ฐ์ด ์์ฐจ์ ์ ๋๋ค. ์ token์ ๊ณ์ฐ์ด ๋๋์ผ ๋ค์ token์ ๊ณ์ฐํ ์ ์์ผ๋ฏ๋ก ๊ธด ๋ฌธ์ฅ์ ๋น ๋ฅด๊ฒ ๋ณ๋ ฌ ์ฒ๋ฆฌํ๊ธฐ ์ด๋ ต์ต๋๋ค.
๋์งธ, ๋ฉ๋ฆฌ ๋จ์ด์ง ๋จ์ด ์ฌ์ด์ ๊ด๊ณ๋ฅผ ๋ฐฐ์ฐ๊ธฐ ์ด๋ ต์ต๋๋ค. ์๋ถ๋ถ์ ์ ๋ณด๊ฐ ๋ค์ชฝ๊น์ง ์ ๋ฌ๋๋ ค๋ฉด ์ฌ๋ฌ ๋จ๊ณ๋ฅผ ์ง๋์ผ ํ๊ณ , ๊ทธ ๊ณผ์ ์์ ์ ๋ณด๊ฐ ์ฝํด์ง ์ ์์ต๋๋ค.
Transformer๋ ์ด ๋ฌธ์ ๋ฅผ self-attention์ผ๋ก ์ฐํํฉ๋๋ค. ๋ชจ๋ token์ด ๊ฐ์ layer ์์์ ์๋ก๋ฅผ ์ง์ ๋ฐ๋ผ๋ณผ ์ ์๊ฒ ๋ง๋ค์ด, ๊ธด ๊ฑฐ๋ฆฌ์ ๊ด๊ณ๋ฅผ ์งง์ ๊ฒฝ๋ก๋ก ์ฐ๊ฒฐํฉ๋๋ค.
๊ธฐ๋ณธ ๊ตฌ์กฐ
์๋ ๋ ผ๋ฌธ์ Transformer๋ encoder-decoder ๊ตฌ์กฐ์ ๋๋ค.
์
๋ ฅ ๋ฌธ์ฅ
โ
Encoder stack
โ
๋ฌธ๋งฅ ํํ
โ
Decoder stack
โ
์ถ๋ ฅ ๋ฌธ์ฅEncoder๋ ์ ๋ ฅ sequence๋ฅผ ์ฝ๊ณ , decoder๋ encoder์ ํํ์ ์ฐธ๊ณ ํ๋ฉด์ ์ถ๋ ฅ sequence๋ฅผ ์์ฑํฉ๋๋ค. ์ค๋๋ ์ GPT ๊ณ์ด ๋ชจ๋ธ์ ์ด ์ค decoder ์ชฝ ์์ด๋์ด๋ฅผ ์ค์ฌ์ผ๋ก ๋ณํํ ๊ตฌ์กฐ์ ๊ฐ๊น๊ณ , BERT๋ encoder ์ชฝ ๊ตฌ์กฐ๋ฅผ ์ค์ฌ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
๊ทธ๋ฆผ์ผ๋ก ์ดํดํ๊ธฐ

Figure 1. Transformer ์ ์ฒด ๊ตฌ์กฐ. ์ผ์ชฝ์ encoder, ์ค๋ฅธ์ชฝ์ decoder์ ๋๋ค. ์ถ์ฒ: Vaswani et al., Attention Is All You Need.
๋
ผ๋ฌธ PDF์์ ์ถ์ถํ Figure 1์ Transformer๋ฅผ encoder์ decoder ๋ ๋ฉ์ด๋ฆฌ๋ก ๋๋์ด ๋ณด์ฌ์ค๋๋ค. ์ผ์ชฝ encoder๋ ์
๋ ฅ embedding์ positional encoding์ ๋ํ ๋ค, ๊ฐ์ layer๋ฅผ N๋ฒ ๋ฐ๋ณตํฉ๋๋ค. ๊ฐ layer๋ multi-head self-attention, Add & Norm, feed-forward network, Add & Norm ์์๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
์ค๋ฅธ์ชฝ decoder๋ ๋น์ทํ์ง๋ง attention์ด ํ๋ ๋ ์์ต๋๋ค. ๋จผ์ masked multi-head self-attention์ผ๋ก ์ด์ ์ถ๋ ฅ token๋ค๋ง ์ฐธ๊ณ ํ๊ณ , ๊ทธ ๋ค์ encoder ์ถ๋ ฅ์ attention์ ๊ฒ๋๋ค. ๋ง์ง๋ง์๋ feed-forward network๋ฅผ ๊ฑฐ์ณ linear layer์ softmax๊ฐ ๋ค์ token ํ๋ฅ ์ ๋ง๋ญ๋๋ค. ์ด ๊ทธ๋ฆผ์ ๋ณด๋ฉด Transformer๊ฐ ๋จ์ผ attention ๋ธ๋ก์ด ์๋๋ผ, attention๊ณผ feed-forward network๋ฅผ residual/normalization ๊ตฌ์กฐ๋ก ๋ฐ๋ณตํด์ ์์ architecture๋ผ๋ ์ ์ด ๋ณด์ ๋๋ค.

Figure 2 ์ผ์ชฝ. Scaled dot-product attention์ ๋ด๋ถ ๊ณ์ฐ ํ๋ฆ์ ๋๋ค.

Figure 2 ์ค๋ฅธ์ชฝ. Multi-head attention์ ์ฌ๋ฌ scaled dot-product attention์ ๋ณ๋ ฌ๋ก ์ํํฉ๋๋ค.
Figure 2๋ attention ์์ฒด๋ฅผ ์ค๋ช ํฉ๋๋ค. Scaled dot-product attention์ Q์ K๋ฅผ ํ๋ ฌ ๊ณฑ์ผ๋ก ๋น๊ตํ๊ณ , scale๊ณผ optional mask๋ฅผ ์ ์ฉํ ๋ค softmax๋ฅผ ๊ฑฐ์ณ V๋ฅผ ์์ต๋๋ค. Multi-head attention์ ์ด scaled dot-product attention์ ์ฌ๋ฌ head์์ ๋ณ๋ ฌ๋ก ์ํํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ concatํ ๋ค linear layer๋ก ๋ค์ ํฉ์นฉ๋๋ค. ๋ฐ๋ผ์ multi-head attention์ โattention์ ์ฌ๋ฌ ๊ด์ ์์ ๋์์ ๋ณด๋ ์ฅ์นโ๋ผ๊ณ ์ดํดํ ์ ์์ต๋๋ค.
Encoder layer
๋ ผ๋ฌธ์ encoder layer๋ ๋ ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
- multi-head self-attention
- position-wise feed-forward network
๊ฐ ๋ถ๋ถ ๋ค์๋ residual connection๊ณผ layer normalization์ด ๋ถ์ต๋๋ค. Residual connection์ ์ ๋ ฅ์ ๊ทธ๋๋ก ์ฐํ์์ผ sub-layer ์ถ๋ ฅ๊ณผ ๋ํ๋ ๊ตฌ์กฐ์ด๊ณ , layer normalization์ ๊ฐ์ ๋ถํฌ๋ฅผ ์์ ํํฉ๋๋ค. ์ด ์กฐํฉ์ ๊น์ ๋ชจ๋ธ์ ํ์ตํ ๋ gradient์ ํํ์ด ๋ถ์์ ํด์ง๋ ๋ฌธ์ ๋ฅผ ์ค์ ๋๋ค.
Encoder์ self-attention์์๋ ์ ๋ ฅ ๋ฌธ์ฅ์ ๊ฐ token์ด ๊ฐ์ ๋ฌธ์ฅ ์์ ๋ค๋ฅธ ๋ชจ๋ token์ ์ฐธ๊ณ ํ ์ ์์ต๋๋ค.
Decoder layer
Decoder layer๋ encoder๋ณด๋ค ํ ๋จ๊ณ๊ฐ ๋ ์์ต๋๋ค.
- masked multi-head self-attention
- encoder-decoder attention
- position-wise feed-forward network
Masked self-attention์ ๋ฏธ๋ token์ ๋ณด์ง ๋ชปํ๊ฒ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ฒ์ญ ๊ฒฐ๊ณผ๋ฅผ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์์ฑํ ๋, ์์ง ์์ฑํ์ง ์์ ๋จ์ด๋ฅผ ๋ณด๊ณ ๋ค์ ๋จ์ด๋ฅผ ๋งํ๋ฉด ์ ๋ฉ๋๋ค.
Encoder-decoder attention์ decoder๊ฐ ์ถ๋ ฅ token์ ๋ง๋ค ๋ encoder์ ์ ๋ ฅ ๋ฌธ์ฅ ํํ์ ์ฐธ๊ณ ํ๋ ๋ถ๋ถ์ ๋๋ค. ๋ฒ์ญ์ผ๋ก ๋น์ ํ๋ฉด, ์ง๊ธ ์์ฑํ๋ ค๋ ๋จ์ด๊ฐ ์๋ฌธ ๋ฌธ์ฅ์ ์ด๋ ๋ถ๋ถ์ ๋ด์ผ ํ๋์ง ์ฐพ๋ ๊ณผ์ ์ ๋๋ค.
Self-attention
Self-attention์ ๊ฐ์ sequence ์์์ token๋ผ๋ฆฌ ์๋ก๋ฅผ ์ฐธ๊ณ ํ๋ ์ฐ์ฐ์ ๋๋ค. ๊ฐ token์ query, key, value๋ผ๋ ์ธ ์ข ๋ฅ์ ๋ฒกํฐ๋ก ๋ณํ๋ฉ๋๋ค.
- Query: ์ง๊ธ ์ด token์ด ์ฐพ๊ณ ์ถ์ ์ ๋ณด
- Key: ๋ค๋ฅธ token๋ค์ด ๊ฐ์ง ์๋ณ ์ ๋ณด
- Value: ์ค์ ๋ก ๊ฐ์ ธ์ฌ ๋ด์ฉ
Query์ key์ ์ ์ฌ๋๋ฅผ ๊ณ์ฐํ๋ฉด โ์ด token์ด ์ token์ ์ผ๋ง๋ ์ฐธ๊ณ ํด์ผ ํ๋๊ฐโ๋ผ๋ ์ ์๊ฐ ๋์ต๋๋ค. ์ด ์ ์์ softmax๋ฅผ ์ ์ฉํด ๊ฐ์ค์น๋ก ๋ง๋ค๊ณ , value๋ฅผ ๊ฐ์คํฉํ๋ฉด attention ๊ฒฐ๊ณผ๊ฐ ๋ฉ๋๋ค.
Scaled dot-product attention
๋ ผ๋ฌธ์์ ์ฌ์ฉํ attention์ scaled dot-product attention์ ๋๋ค. Query์ key์ ๋ด์ ์ ๊ณ์ฐํ ๋ค, key ์ฐจ์์ ์ ๊ณฑ๊ทผ์ผ๋ก ๋๋๋๋ค. ๊ทธ ๋ค์ softmax๋ฅผ ํต๊ณผ์์ผ value์ ๊ณฑํฉ๋๋ค.
scale์ ๋ฃ๋ ์ด์ ๋ ์ฐจ์์ด ์ปค์ง์๋ก ๋ด์ ๊ฐ์ด ์ปค์ ธ softmax๊ฐ ๋๋ฌด ๋พฐ์กฑํด์ง ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค. softmax๊ฐ ์ง๋์น๊ฒ ํ์ชฝ์ผ๋ก ์ ๋ฆฌ๋ฉด gradient๊ฐ ์์์ง๊ณ ํ์ต์ด ์ด๋ ค์์ง ์ ์์ต๋๋ค.
Multi-head attention
Multi-head attention์ attention์ ์ฌ๋ฌ ๋ฒ ๋ณ๋ ฌ๋ก ์ํํ๋ ๋ฐฉ์์ ๋๋ค. ํ๋์ ํฐ attention์ ํ ๋ฒ ๊ณ์ฐํ๋ ๋์ , query/key/value๋ฅผ ์ฌ๋ฌ ๊ฐ์ ์์ ๊ณต๊ฐ์ผ๋ก ๋๋์ด ๊ฐ๊ฐ attention์ ๊ณ์ฐํ ๋ค ๋ค์ ํฉ์นฉ๋๋ค.
์ด๋ ๊ฒ ํ๋ฉด ๋ชจ๋ธ์ ์ฌ๋ฌ ๊ด์ ์ ๊ด๊ณ๋ฅผ ๋์์ ๋ณผ ์ ์์ต๋๋ค.
- ์ด๋ค head๋ ๊ฐ๊น์ด ๋จ์ด ๊ด๊ณ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
- ์ด๋ค head๋ ๋ฉ๋ฆฌ ๋จ์ด์ง ์์กด์ฑ์ ๋ณผ ์ ์์ต๋๋ค.
- ์ด๋ค head๋ ๋๋ช ์ฌ๊ฐ ๊ฐ๋ฆฌํค๋ ๋์์ ๋ณผ ์ ์์ต๋๋ค.
- ์ด๋ค head๋ ๋ฌธ๋ฒ์ ์ญํ ์ ํฌ์ฐฉํ ์ ์์ต๋๋ค.
๋ ผ๋ฌธ์ attention visualization์์ ์ผ๋ถ head๊ฐ โmaking โฆ more difficultโ์ฒ๋ผ ๋ฉ๋ฆฌ ๋จ์ด์ง ํํ์ ์ฐ๊ฒฐํ๊ฑฐ๋, โitsโ๊ฐ ๊ฐ๋ฆฌํค๋ ๋์์ ๋ ์นด๋กญ๊ฒ ๋ณด๋ ๋ชจ์ต์ ๋ณด์ฌ์ค๋๋ค.
Positional encoding
Self-attention์ ์์ ์ ๋ณด๋ฅผ ์ง์ ๊ฐ๊ณ ์์ง ์์ต๋๋ค. โ๋๋ ํ๊ต์ ๊ฐ๋คโ์ โํ๊ต๊ฐ ๋์๊ฒ ์๋คโ๋ ๋จ์ด ์งํฉ์ด ๋น์ทํด๋ ์์๊ฐ ๋ค๋ฅด๋ฉด ์๋ฏธ๊ฐ ๋ค๋ฆ ๋๋ค. ๋ฐ๋ผ์ Transformer๋ token embedding์ positional encoding์ ๋ํฉ๋๋ค.
์ ๋ ผ๋ฌธ์ sine/cosine ํจ์๋ฅผ ์ด์ฉํ ๊ณ ์ positional encoding์ ์ฌ์ฉํ์ต๋๋ค. ์์น๋ง๋ค ์๋ก ๋ค๋ฅธ ์ฃผ๊ธฐ์ ๊ฐ์ ๋ฃ์ผ๋ฉด ๋ชจ๋ธ์ด token์ ์์น์ ์๋์ ๊ฑฐ๋ฆฌ ์ ๋ณด๋ฅผ ํ์ฉํ ์ ์์ต๋๋ค.
์ค์ํ ์ ์ Transformer๊ฐ ์์๋ฅผ ๋ฒ๋ฆฐ ๊ฒ์ด ์๋๋ผ, ์์ ์ฒ๋ฆฌ๋ฅผ recurrence์ ๋งก๊ธฐ์ง ์๊ณ ๋ณ๋ ์์น ์ ๋ณด๋ก ์ฃผ์ ํ๋ค๋ ๊ฒ์ ๋๋ค.
Feed-forward network
๊ฐ attention layer ๋ค์๋ position-wise feed-forward network๊ฐ ์์ต๋๋ค. ์ด๋ ๊ฐ token ์์น์ ๋ ๋ฆฝ์ ์ผ๋ก ์ ์ฉ๋๋ ์์ MLP์ ๋๋ค. Attention์ด token ์ฌ์ด์ ์ ๋ณด๋ฅผ ์๋ ์ญํ ์ ํ๋ค๋ฉด, feed-forward network๋ ๊ฐ ์์น์ ํํ์ ๋น์ ํ์ ์ผ๋ก ๋ณํํด ๋ ํ๋ถํ ํน์ง์ ๋ง๋ญ๋๋ค.
์ ๋
ผ๋ฌธ base ๋ชจ๋ธ์์๋ d_model = 512, feed-forward ๋ด๋ถ ์ฐจ์์ d_ff = 2048์ ์ฌ์ฉํ์ต๋๋ค.
์ ํ์ฅ์ ์ ๋ฆฌํ๋
Transformer๊ฐ ์ค์ํด์ง ์ด์ ๋ ์ฑ๋ฅ๋ง์ด ์๋๋๋ค. ๊ตฌ์กฐ์ ์ผ๋ก ๋ณ๋ ฌํ์ ์ ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
RNN์ sequence ๊ธธ์ด ๋ฐฉํฅ์ผ๋ก ๊ณ์ฐ์ด ์ด์ด์ง์ง๋ง, Transformer์ self-attention์ ํ layer ์์์ ์ฌ๋ฌ ์์น๋ฅผ ๋์์ ๊ณ์ฐํ ์ ์์ต๋๋ค. ์ด๊ฒ์ GPU/TPU ๊ฐ์ ๋ณ๋ ฌ ํ๋์จ์ด์ ์ ๋ง์ต๋๋ค. ๋ชจ๋ธ๊ณผ ๋ฐ์ดํฐ๊ฐ ์ปค์ง์๋ก ์ด ์ฐจ์ด๋ ๋งค์ฐ ์ค์ํด์ง๋๋ค.
๋ํ token ์ฌ์ด์ path length๊ฐ ์งง์ต๋๋ค. RNN์์๋ ๋ฉ๋ฆฌ ๋จ์ด์ง ๋ token์ด ์ฌ๋ฌ recurrent step์ ๊ฑฐ์ณ ์ฐ๊ฒฐ๋์ง๋ง, self-attention์์๋ ํ layer ์์์ ์ง์ ์ฐ๊ฒฐ๋ ์ ์์ต๋๋ค. ์ด ํน์ฑ์ ๊ธด ๋ฌธ๋งฅ์ ์์กด์ฑ์ ํ์ตํ๋ ๋ฐ ์ ๋ฆฌํฉ๋๋ค.
ํ๊ณ
Transformer์๋ ํ๊ณ๊ฐ ์์ต๋๋ค. ๊ฐ์ฅ ๋ํ์ ์ธ ๊ฒ์ attention์ ๊ณ์ฐ๋์
๋๋ค. ๋ชจ๋ token ์์ ๋น๊ตํ๋ฏ๋ก sequence ๊ธธ์ด n์ ๋ํด self-attention ๋น์ฉ์ ๋๋ต nยฒ์ ๋น๋กํฉ๋๋ค. ์งง๊ฑฐ๋ ์ค๊ฐ ๊ธธ์ด์ ๋ฌธ์ฅ์์๋ ๊ฐ๋ ฅํ์ง๋ง, ๋งค์ฐ ๊ธด ๋ฌธ์๋ ๊ณ ํด์๋ ์ด๋ฏธ์ง, ๊ธด ์ค๋์ค๋ฅผ ๊ทธ๋๋ก ์ฒ๋ฆฌํ ๋๋ ๋น์ฉ์ด ์ปค์ง๋๋ค.
๊ทธ๋์ ์ดํ ์ฐ๊ตฌ์์๋ sparse attention, local attention, linear attention, retrieval, chunking, long-context architecture ๊ฐ์ ๋ค์ํ ๋ณํ์ด ๋ฑ์ฅํ์ต๋๋ค.
ํท๊ฐ๋ฆฌ์ง ๋ง์์ผ ํ ์
Transformer๋ โattention๋ง ์์ผ๋ฉด ๋๋คโ๋ ๋ง๋ก ๋จ์ํํ๋ฉด ์คํด๊ฐ ์๊น๋๋ค. ์ค์ ๊ตฌ์กฐ๋ attention, positional encoding, residual connection, layer normalization, feed-forward network, masking, optimizer์ regularization ์ ํ์ด ํจ๊ป ์๋ํฉ๋๋ค.
๋ํ ํ์ฌ์ LLM์ด ๋ชจ๋ ๋ ผ๋ฌธ ์ํ์ encoder-decoder Transformer๋ผ๋ ๋ป๋ ์๋๋๋ค. GPT ๊ณ์ด์ decoder-only ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๊ณ , BERT ๊ณ์ด์ encoder ์ค์ฌ ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ ํ๋ฆ ๋ชจ๋ ์ด ๋ ผ๋ฌธ์ด ์ ๋ฆฌํ self-attention ๊ธฐ๋ฐ architecture์์ ์ถ๋ฐํ์ต๋๋ค.