๐ก ํ ์ค ์์ฝ#
RNN๊ณผ CNN์ ์์ ํ ์ ๊ฑฐํ๊ณ ์ค์ง Attention ๋ฉ์ปค๋์ฆ๋ง์ผ๋ก ์ธ์ฝ๋-๋์ฝ๋๋ฅผ ๊ตฌ์ฑํ Transformer๋ฅผ ์ ์ํ์ฌ, ๊ธฐ๊ณ ๋ฒ์ญ์์ ๊ณ์ฐ ๋ณต์ก๋ $\mathcal{O}(1)$ ๊ฒฝ๋ก ๊ธธ์ด์ ์์ ํ ๋ณ๋ ฌ์ฑ์ ๋ฌ์ฑํ๊ณ ํ๋ ๋ชจ๋ ๋ํ ๋ชจ๋ธ์ ์๋ฅ๊ฐ ๋์๋ค.
๐ ๊ฐ์ ๋ฐ ํต์ฌ ๊ธฐ์ฌ (Key Contributions)#
- ์ ์: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, ลukasz Kaiser, Illia Polosukhin (Google Brain, Google Research, Univ. of Toronto)
- ๋ฐํ๋ ๋: 2017 (arXiv:1706.03762, NeurIPS 2017)
- ์ฃผ์ ๊ธฐ์ฌ์ :
- Self-Attention ์ค์ฌ ์ํคํ ์ฒ: ์์ฐจ์ Recurrence(RNN) ๋ฐ ์ ๊ด ์์ฉ ์์ญ์ Convolution(CNN)์ ์ ๊ฑฐํ๊ณ , ์ํ์ค ๋ด ์์น ๊ฐ ๊ด๋ จ์ฑ์ ์ผ์์ ํ์ ํ๋ Self-Attention ๊ตฌ์กฐ ์ ๋ฆฝ.
- Scaled Dot-Product & Multi-Head Attention: $Q, K, V$ ํ๋ ฌ ๋ด์ ์ $\sqrt{d_k}$๋ก ์ค์ผ์ผ๋งํ๊ณ , $h$๊ฐ ํค๋๋ก ๋ถํ ํ์ฌ ๋ค์ํ ๊ด์ ์ ๊ธฐํํ์ ยท์๋ฏธ๋ก ์ ๊ด๋ จ์ฑ์ ๋ณ๋ ฌ ์ถ์ถ.
- Sinusoidal Positional Encoding: ์ผ๊ฐํจ์ ํ์ฅ์ ์ด์ฉํด ์์ ์ ๋ณด(Position)๋ฅผ ํ๋ผ๋ฏธํฐ ์ถ๊ฐ ์์ด ์๋ฒ ๋ฉ์ ์ฃผ์ ํ์ฌ ๊ธธ์ด์ ๊ตฌ์ ๋ฐ์ง ์๋ ์ธ์ฝ(Extrapolation) ํ์ฉ.
- Warmup Learning Rate Schedule: ๋ชจ๋ธ ์ฐจ์์ ์ญ์ ๊ณฑ๊ทผ์ผ๋ก ๋น๋กํ๋ ๋์ ์ค์ผ์ค๋ง์ผ๋ก ๊ธฐ์ธ๊ธฐ ํญ์ฃผ/์์ค ๋ฐฉ์ง.
๐ฏ ๊ด๋ จ ์ฐ๊ตฌ ํ๋ฆ ๋ฐ ๊ธฐ์กด ํ๊ณ (Related Work & Motivation)#
๊ด๋ จ ์ฐ๊ตฌ ํ๋ฆ#
- RNN/LSTM ๊ธฐ๋ฐ Seq2Seq: ์ํ์ค๋ฅผ 1๊ฐ ํ ํฐ์ฉ ์์ฐจ ์ฒ๋ฆฌํด์ผ ํ๋ฏ๋ก GPU ๋ณ๋ ฌํ๊ฐ ๋ถ๊ฐ๋ฅํ๊ณ , ์ฅ๊ฑฐ๋ฆฌ ์์กด์ฑ(Long-range Dependency) ์ ๋ณด๊ฐ ํฌ์๋จ.
- CNN ๊ธฐ๋ฐ Seq2Seq (ConvS2S): ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ ๊ฐ๋ฅํ๋ ๋ฉ๋ฆฌ ๋จ์ด์ง ํ ํฐ ๊ฐ ๊ด๊ณ๋ฅผ ๋ฌถ๊ธฐ ์ํด $\mathcal{O}(\log_k n)$์ ๊น์ ๋ ์ด์ด ์คํ ํ์.
- Transformer: ์์์ ๋ ํ ํฐ ๊ฐ ์ต๋ ๊ฒฝ๋ก ๊ธธ์ด๋ฅผ $\mathcal{O}(1)$๋ก ๋จ์ถํ๊ณ ์์ ํ ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ฅผ ์์ฑ.
๐ ๋ชฉ์ฐจ#
- Chapter 1: Scaled Dot-Product Attention ์์ ํด์ค
- Chapter 2: Multi-Head Attention (MHA) ๋ณ๋ ฌ ๊ตฌ์กฐ
- Chapter 3: Position-wise Feed-Forward Networks (FFN)
- Chapter 4: Sinusoidal Positional Encoding ์์น ์ธ์ฝ๋ฉ
- Chapter 5: Warmup Learning Rate Scheduler ์์
- Chapter 6: ์ฃผ์ ์คํ ๋ฐ ๊ฒฐ๊ณผ
- Chapter 7: ๊ฒฐ๋ก ๋ฐ ์์ฌ์
๐ ๏ธ Chapter 1: Scaled Dot-Product Attention ์์ ํด์ค#
1. ์์ฝ#
Query($Q$)์ Key($K$)์ ๋ด์ ๊ฐ์ $\frac{1}{\sqrt{d_k}}$ ์ค์ผ์ผ์ ๊ณฑํด Softmax ํ๋ฅ ์ ์ฐ์ถํ ๋ค Value($V$)๋ฅผ ๊ฐ์คํฉํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\text{Attention}(Q, K, V) = \text{Softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right) V$$- $Q \in \mathbb{R}^{N \times d_k}, K \in \mathbb{R}^{M \times d_k}, V \in \mathbb{R}^{M \times d_v}$: Query, Key, Value ํ๋ ฌ
- $\sqrt{d_k}$: $d_k$ ์ฐจ์์ด ์ปค์ง ๋ ๋ด์ ๊ฐ์ด ์ปค์ ธ Softmax ๊ธฐ์ธ๊ธฐ๊ฐ ์์ค(Gradient Vanishing)๋๋ ํ์์ ๋ฐฉ์งํ๋ ์ค์ผ์ผ๋ง ์ธ์
import torch
import torch.nn.functional as F
def scaled_dot_product_attention(
Q: torch.Tensor, # (B, N, d_k)
K: torch.Tensor, # (B, M, d_k)
V: torch.Tensor, # (B, M, d_v)
mask: torch.Tensor = None # (B, N, M) ๋ง์คํฌ (์ต์
)
) -> tuple:
"""
Scaled Dot-Product Attention ์์ ๊ตฌํ
Attention(Q, K, V) = Softmax( Q @ K^T / sqrt(d_k) ) @ V
"""
d_k = Q.size(-1)
# 1. Q @ K^T / sqrt(d_k)
scores = torch.matmul(Q, K.transpose(-2, -1)) / (d_k ** 0.5)
# 2. Masking ์ฒ๋ฆฌ (๋ฏธ๋ ํ ํฐ ๊ฐ๋ฆฌ๊ธฐ ๋ฑ)
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
# 3. Softmax ํ๋ฅ ์ฐ์ถ
attn_weights = F.softmax(scores, dim=-1) # (B, N, M)
# 4. Value ๊ฐ์คํฉ
output = torch.matmul(attn_weights, V) # (B, N, d_v)
return output, attn_weights
# --- ์ฌ์ฉ ์์ ---
q_in = torch.randn(2, 10, 64) # Batch=2, Seq_len=10, d_k=64
k_in = torch.randn(2, 10, 64)
v_in = torch.randn(2, 10, 64)
out_attn, weights = scaled_dot_product_attention(q_in, k_in, v_in)
print("Attention ์ถ๋ ฅ Shape:", out_attn.shape, "๊ฐ์ค์น Shape:", weights.shape)๐ ๏ธ Chapter 2: Multi-Head Attention (MHA) ๋ณ๋ ฌ ๊ตฌ์กฐ#
1. ์์ฝ#
๋จ์ผ Attention ์ฐ์ฐ ๋์ , $d_{\text{model}}$ ์ฐจ์์ $h$๊ฐ ํค๋๋ก ๋ถํ ($d_k = d_{\text{model}} / h$)ํ์ฌ ๋ณ๋ ฌ ๊ณ์ฐํ ํ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์(Concat & Project)ํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \dots, \text{head}_h) W^O$$$$\text{head}_i = \text{Attention}(Q W_i^Q, K W_i^K, V W_i^V)$$import torch
import torch.nn as nn
class MultiHeadAttention(nn.Module):
"""
Multi-Head Attention (MHA) ๋ชจ๋
"""
def __init__(self, d_model: int = 512, num_heads: int = 8):
super().__init__()
self.d_model = d_model
self.num_heads = num_heads
self.d_k = d_model // num_heads
self.W_q = nn.Linear(d_model, d_model)
self.W_k = nn.Linear(d_model, d_model)
self.W_v = nn.Linear(d_model, d_model)
self.W_o = nn.Linear(d_model, d_model)
def forward(self, Q: torch.Tensor, K: torch.Tensor, V: torch.Tensor, mask: torch.Tensor = None) -> torch.Tensor:
B, N, _ = Q.shape
M = K.shape[1]
# 1. ์ ํ ์ฌ์ ํ h๊ฐ ํค๋๋ก ๋ถํ (B, h, N, d_k)
q_heads = self.W_q(Q).view(B, N, self.num_heads, self.d_k).transpose(1, 2)
k_heads = self.W_k(K).view(B, M, self.num_heads, self.d_k).transpose(1, 2)
v_heads = self.W_v(V).view(B, M, self.num_heads, self.d_k).transpose(1, 2)
# 2. Scaled Dot-Product Attention ๋ณ๋ ฌ ๊ณ์ฐ
scores = torch.matmul(q_heads, k_heads.transpose(-2, -1)) / (self.d_k ** 0.5)
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
attn_weights = torch.softmax(scores, dim=-1)
attn_out = torch.matmul(attn_weights, v_heads) # (B, h, N, d_k)
# 3. Concat & Output Projection
concat_out = attn_out.transpose(1, 2).contiguous().view(B, N, self.d_model)
return self.W_o(concat_out)
# --- ์ฌ์ฉ ์์ ---
mha = MultiHeadAttention(d_model=512, num_heads=8)
x_in = torch.randn(2, 10, 512)
print("Multi-Head Attention ๊ฒฐ๊ณผ Shape:", mha(x_in, x_in, x_in).shape)๐ ๏ธ Chapter 3: Position-wise Feed-Forward Networks (FFN)#
1. ์์ฝ#
๊ฐ ์์น๋ณ๋ก ๋ ๋ฆฝ์ ์ ์ฉ๋๋ 2์ธต Fully-Connected ๋คํธ์ํฌ๋ก, ์ค๊ฐ ์ฐจ์ $d_{ff} = 2048$๋ก ํ์ถฉ ํ ๋ค์ $d_{\text{model}} = 512$๋ก ์ถ์ํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\text{FFN}(x) = \max(0, x W_1 + b_1) W_2 + b_2$$import torch
import torch.nn as nn
class PositionwiseFeedForward(nn.Module):
"""
Position-wise Feed-Forward Network (FFN)
"""
def __init__(self, d_model: int = 512, d_ff: int = 2048):
super().__init__()
self.fc1 = nn.Linear(d_model, d_ff)
self.fc2 = nn.Linear(d_ff, d_model)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.fc2(torch.relu(self.fc1(x)))
# --- ์ฌ์ฉ ์์ ---
ffn = PositionwiseFeedForward()
x_dummy = torch.randn(2, 10, 512)
print("FFN ์ถ๋ ฅ Shape:", ffn(x_dummy).shape)๐ ๏ธ Chapter 4: Sinusoidal Positional Encoding ์์น ์ธ์ฝ๋ฉ#
1. ์์ฝ#
์ํ์ค์ ํ ํฐ ์์น $pos$์ ์ฐจ์ $i$์ ๋ฐ๋ผ ์ฃผ๊ธฐ(์ฃผํ์)๊ฐ ๋ค๋ฅธ ์ฌ์ธ/์ฝ์ฌ์ธ ๊ณก์ ์ ์กฐํฉํ์ฌ ๊ณ ์ ํ ์์น ์๋ฒ ๋ฉ์ ๊ตฌ์ฑํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)$$$$PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)$$import torch
import torch.nn as nn
class SinusoidalPositionalEncoding(nn.Module):
"""
Sinusoidal Positional Encoding ์์น ์ธ์ฝ๋ฉ ํ
์ ์์ฑ
"""
def __init__(self, d_model: int = 512, max_len: int = 5000):
super().__init__()
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-torch.log(torch.tensor(10000.0)) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
self.register_buffer('pe', pe.unsqueeze(0)) # (1, max_len, d_model)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
x: (B, N, d_model)
"""
return x + self.pe[:, :x.size(1)]
# --- ์ฌ์ฉ ์์ ---
pe_module = SinusoidalPositionalEncoding()
embeddings = torch.zeros(1, 20, 512)
print("Positional Encoding ์ฃผ์
ํ Shape:", pe_module(embeddings).shape)๐ ๏ธ Chapter 5: Warmup Learning Rate Scheduler ์์#
1. ์์ฝ#
ํ์ต ์ด๊ธฐ $warmup\_steps=4000$ ๋์ ํ์ต๋ฅ ์ ์ ํ ์ฆ๊ฐ์ํค๊ณ , ์ดํ step์ ์ญ์ ๊ณฑ๊ทผ์ ๋น๋กํด ๊ฐ์์ํต๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$lrate = d_{\text{model}}^{-0.5} \cdot \min\left( step\_num^{-0.5}, \ step\_num \cdot warmup\_steps^{-1.5} \right)$$import torch
def compute_transformer_lr(step_num: int, d_model: int = 512, warmup_steps: int = 4000) -> float:
"""
Transformer ๋์ Warmup Learning Rate ์ค์ผ์ค๋ฌ
"""
step_num = max(1, step_num)
arg1 = step_num ** -0.5
arg2 = step_num * (warmup_steps ** -1.5)
return (d_model ** -0.5) * min(arg1, arg2)
# --- ์ฌ์ฉ ์์ ---
print("Step 1000 ํ์ต๋ฅ :", compute_transformer_lr(1000))
print("Step 4000 (์ต๋) ํ์ต๋ฅ :", compute_transformer_lr(4000))
print("Step 20000 ํ์ต๋ฅ :", compute_transformer_lr(20000))๐ ์ฃผ์ ์คํ ๋ฐ ๊ฒฐ๊ณผ (Experiments & Results)#
1. WMT 2014 ๊ธฐ๊ณ ๋ฒ์ญ ๋ฒค์น๋งํฌ ๋น๊ต#
| ๋ชจ๋ธ (Method) | EN-DE (BLEU) โ | EN-FR (BLEU) โ | ํ์ต ์ฐ์ฐ๋ (FLOPs) โ |
|---|---|---|---|
| ConvS2S | 25.16 | 40.46 | $9.6 \times 10^{18}$ |
| GNMT + RL | 26.30 | 41.16 | $1.8 \times 10^{20}$ |
| Transformer-base | 27.30 | 38.10 | $3.3 \times 10^{18}$ (์ด์ ๋น์ฉ) |
| Transformer-big | 28.40 (+2.1) | 41.80 (+0.64) | $2.3 \times 10^{19}$ |
- ๊ฒฐ๊ณผ: ๊ธฐ์กด RNN/CNN SOTA ๋๋น BLEU ์ ์ +2.1 ์ ํฅ์๊ณผ ๋์์ ํ์ต ์ฐ์ฐ ๋น์ฉ์ ์์ญ ๋ถ์ 1๋ก ์ ๊ฐ.
๐ก ๊ฒฐ๋ก ๋ฐ ์์ฌ์ (Conclusion & Insights)#
1. ๊ฒฐ๋ก #
Transformer๋ “Attention Is All You Need"๋ผ๋ ๋ช ์ ํ์ ์์ฐจ์ ๊ณ์ฐ ์ฐ๊ฒฐ์ ํํผํ๊ณ ๋ชจ๋ AI ์ ๊ฒฝ๋ง ํจ๋ฌ๋ค์์ Self-Attention ๊ธฐ๋ฐ์ผ๋ก ์ ํํ ๊ฐ์ฅ ํ๊ดด์ ์ธ ์ธ๋ฏธ๋ ๋ ผ๋ฌธ์ ๋๋ค.
2. ์์จ์ฃผํ ๋ถ์ผ์์์ ํ์ฉ#
| ํ์ ๋ ผ๋ฌธ | Transformer ํ์ฉ ๋ฐฉ์ |
|---|---|
| BEVFormer | Spatial Cross-Attention์ผ๋ก ์นด๋ฉ๋ผ feature โ BEV ๊ณต๊ฐ ๋ณํ |
| DETR3D | 3D object query์ 2D back-projection์ Cross-Attention ํ์ฉ |
| UniAD | 5๊ฐ ํ์คํฌ๋ฅผ Query๋ก ์ฐ๊ฒฐํ๋ ํตํฉ Transformer ํ์ดํ๋ผ์ธ |
| LDM | UNet ์ค๊ฐ ๋ ์ด์ด์ Cross-Attention ์ฝ์ ํด ํ ์คํธยท์กฐ๊ฑด ํตํฉ |
| GAIA-1 | GPT ๋ฐฉ์(Decoder-only Transformer)์ผ๋ก AV World Model ํ์ต |
| MagicDrive | BEV ๋งตยท3D ๋ฐ์ค๋ฅผ Cross-Attention์ผ๋ก ์ด๋ฏธ์ง ์์ฑ์ ์กฐ๊ฑดํ |
| VAD | ๋ฒกํฐํ ์ฅ๋ฉด ํํ์ Transformer๋ก ์ฒ๋ฆฌํด ๊ฒฝ๋ ๊ณํ ์ํ |
BEV ์ธ์๋ถํฐ World Model๊น์ง ์ ํ์ดํ๋ผ์ธ์ด Transformer ๊ธฐ๋ฐ์ด๋ฏ๋ก, ์ด ๋ ผ๋ฌธ์ Self-Attention / Cross-Attention / Positional Encoding ๊ฐ๋ ์ ์ ํํ ์ดํดํ๋ ๊ฒ์ด ์ดํ ๋ ผ๋ฌธ ํด์์ ํ์ ์ ์ ์กฐ๊ฑด์ด๋ค.
3. ํ๊ณ์ ๋ฐ ์์ฌ์ด ์ #
- $O(n^2)$ ๋ณต์ก๋๋ก ์ธํด ๋งค์ฐ ๊ธด ์ํ์ค(๊ณ ํด์๋ ์ด๋ฏธ์ง, ๊ธด ๋ฌธ์)์๋ ๊ทธ๋๋ก ์ ์ฉํ๊ธฐ ์ด๋ ค์ โ ์ดํ Longformer, Linformer, FlashAttention ๋ฑ์ด ์ด๋ฅผ ํด๊ฒฐ.
- Sinusoidal Positional Encoding์ ๊ณ ์ ๊ฐ์ด๋ผ ์ ์ฐ์ฑ์ด ๋จ์ด์ง๋ฉฐ, ์ดํ ์ฐ๊ตฌ๋ค์ ํ์ต ๊ฐ๋ฅํ ์์น ์๋ฒ ๋ฉ์ด๋ RoPE ๋ฑ์ผ๋ก ๋์ฒด.
- ๋ ผ๋ฌธ ์์ฒด๋ ๋ฒ์ญ ํ์คํฌ์ ์ง์ค๋์ด ์์ด, ๋น์ ยท๋ฉํฐ๋ชจ๋ฌ ๋ฑ์ผ๋ก์ ์ผ๋ฐํ ๊ฐ๋ฅ์ฑ์ ํ์ ์ฐ๊ตฌ(ViT, DETR ๋ฑ)์์ ๋น๋ก์ ๊ฒ์ฆ๋จ.