BAB 05
Estimasi Waktu: 55 menit Level: Intermediate → Advanced
Paper “Attention Is All You Need” (Vaswani et al., 2017) adalah momen “iPhone” untuk AI. Semua yang kita sebut “AI” sekarang — GPT, Claude, Gemini, Llama — adalah turunan dari arsitektur ini.
INPUT TEXT
↓
TOKENIZATION (text → numbers)
↓
EMBEDDING (numbers → vectors)
↓
TRANSFORMER BLOCK (× N layers)
├── Multi-Head Self-Attention
├── Feed-Forward Network
├── Layer Normalization
└── Residual Connections
↓
OUTPUT HEAD (next token prediction)
Text dipecah jadi tokens (subwords).
# Contoh tokenization
text = "Hello, world!"
tokens = ["Hello", ",", " world", "!"]
token_ids = [15496, 11, 995, 0]
# GPT-4 tokenizer: ~100K vocabulary
# "makanan" → ["makan", "an"] (2 subwords)
# "transformer" → ["transform", "er"] (2 subwords)
Kenapa subwords? Keseimbangan antara vocabulary size (efisiensi) dan coverage (bisa handle kata baru).
Setiap token di-map ke vector (dense representation).
# Token ID → Embedding Vector
token_id = 15496
embedding = embedding_matrix[token_id] # shape: [768] (GPT-2 small)
# atau [4096] untuk GPT-3, [8192] untuk Llama 3 70B
# Positional Encoding: model perlu tahu urutan
# "Anjing gigit orang" ≠ "Orang gigit anjing"
position_vector = positional_encoding(position=0)
final_embedding = token_embedding + position_vector
Self-attention memungkinkan setiap token “melihat” dan “berkomunikasi” dengan semua token lain.
Untuk setiap token, hitung:
- Query (Q): "Apa yang aku cari?"
- Key (K): "Apa yang aku punya?"
- Value (V): "Informasi yang aku bawa"
Attention Score = softmax(Q @ K^T / √d_k)
Output = Attention Score @ V
Setelah attention, setiap token di-process oleh FFN (2-layer MLP):
FFN(x) = ReLU(x @ W1 + b1) @ W2 + b2
Ini adalah tempat “knowledge” disimpan. FFN layers mengandung factual knowledge, reasoning patterns, dan world knowledge.
# Residual connection: membantu gradient flow di network dalam
output = LayerNorm(x + Attention(x))
output = LayerNorm(output + FFN(output))
Data: Trillions of tokens (web, books, code)
Objective: Next Token Prediction
Duration: Weeks-Months
Cost: $10M-$100M+ (GPU clusters)
Output: Base model (bisa complete text, tapi gak bisa chat)
# Simplified pretraining loop
for batch in dataloader: # millions of batches
# Shift input: predict next token
inputs = batch[:, :-1] # "The cat sat on"
targets = batch[:, 1:] # "cat sat on the"
logits = model(inputs)
loss = cross_entropy(logits, targets)
loss.backward()
optimizer.step()
Data: 100K-1M instruction-response pairs
Objective: Follow instructions, helpful responses
Duration: Hours-Days
Cost: $1K-$100K
Output: Chat model (GPT-4, Claude, etc.)
Data: Human preference comparisons
Objective: Helpful, harmless, honest (HHH)
Duration: Days
Cost: $10K-$500K
Output: Aligned chat model
def generate(model, prompt, max_tokens=100):
tokens = tokenize(prompt)
for _ in range(max_tokens):
logits = model(tokens) # [seq_len, vocab_size]
next_token_logits = logits[-1] # last token's prediction
next_token = sample(next_token_logits, temperature=0.7)
tokens.append(next_token)
if next_token == EOS_TOKEN:
break
return detokenize(tokens)
| Parameter | Effect | Range |
|---|---|---|
| Temperature | Randomness (0 = deterministic) | 0.0 - 2.0 |
| Top-p | Nucleus sampling (cumulative probability cutoff) | 0.0 - 1.0 |
| Top-k | Only sample from top-k tokens | 1 - 100 |
| Max tokens | Output length limit | 1 - 128K+ |
| Model | Parameters | Context | Key Innovation |
|---|---|---|---|
| GPT-4o | ~1.8T (rumored) | 128K | Multimodal native |
| Claude 3.5 Sonnet | Unknown | 200K | Computer use, deep reasoning |
| Llama 3.1 405B | 405B | 128K | Open weights, MoE |
| DeepSeek-V3 | 671B (37B active) | 128K | MoE, cost-efficient training |
| Gemini 2.0 | Unknown | 1M+ | Ultra-long context, multimodal |
| Grok-2 | Unknown | 128K | Real-time X integration |
Chinchilla Scaling Law (DeepMind, 2022):
Target: Paham full transformer pipeline, bisa explain ke orang non-teknis.