不管是 GPT、Claude、Gemini 還是 Llama,底層都在做同一件事:給定一段 token 序列,預測下一個 token 的機率分布。所謂的智能,其實是規模、結構、訓練資料疊出來的,模型本身就是個機率性的下個 token 預測器。
這篇筆記拆 Transformer 的五件套(Tokenization、Embedding、Self-Attention、Feed-Forward、Residual + LayerNorm),再往下談訓練的三個階段、RAG、Fine-tuning、量化和推理框架。
概觀:Transformer 五件套
源自 2017 年的 Attention Is All You Need,現代 LLM 的核心結構可以拆成五個組件:
| 組件 | 做什麼 |
|---|---|
| Tokenization | 文字 → token id 序列(BPE / SentencePiece) |
| Embedding | token id → vector(可學習表) |
| Self-Attention | 每個 token 看其他 token,決定該關注哪些(Q · K → 權重 → 加權 V) |
| Feed-Forward(FFN) | 對每個 token 獨立做非線性轉換 |
| Residual + LayerNorm | 訊號穩定 + 梯度通路 |
說到底它是台 token machine,不是「智能體」。它沒有意圖、也沒有記憶,除非你把對話重新塞回 context;它表現出的「理解」其實是統計關聯。
LLM 是什麼
Input: "今天天氣"
↓ tokenize
[12345, 67890]
↓ embed
[[0.12, -0.5, ...], [0.8, 0.1, ...]]
↓ N 層 Transformer block
↓ 最後一層投影
Logits: [..., 0.01, 0.95, 0.04, ...] ← 字典大小的機率分布
↓ sample / argmax
next token = "好"
「生成一段話」其實就是「反覆預測下一個 token,每次把它接到輸入再預測下一個」(autoregressive)。
Tokenization
文字 → 整數序列。切法不是按字母、也不是按 word,而是 subword unit。
BPE(Byte-Pair Encoding,GPT 系用)
- 從字元開始
- 找出最常出現的兩個相鄰 token,合成新 token
- 重複到字典大小目標
- 訓練後用 greedy match 切詞
WordPiece(BERT 用)
類似 BPE,但合併 criteria 改成 likelihood。
SentencePiece / Unigram(T5 / Llama 用)
更通用,可直接處理任意 unicode 字串(包含 CJK 與空白)。
為什麼這樣切
- 全字母 → 太多 token,序列爆長
- 全 word → 詞典太大、生僻字爆字典外(OOV)
- subword:折衷做法,常見詞變一個 token,罕見詞拆成 subword
中文的 token 成本
"我今天去吃飯"
GPT-4 tokenizer: 6 tokens (一字 = 1-2 tokens)
英文同義: "I went to eat today" = ~6 tokens
中文跟 code 的 token efficiency 比 GPT-2 時代好很多(GPT-2 中文要 2-3 tokens/字),現在多數模型約 1.0-1.5 tokens/字。
想看自己的文字會切成多少 token,可用 tiktokenizer.vercel.app 或
tiktokenlibrary 實測。
Embedding
每個 token id 對應一個 vector(通常 768 / 1024 / 4096 維)。
- 可學習的 lookup table:
embedding[token_id]→ vector - vector 編碼了該 token 的「語意位置」
- 相近語意的 token,vector 在高維空間距離近(
king - man + woman ≈ queen)
Embedding 模型(獨立於 LLM 的)
像 text-embedding-3-large、BGE、E5、Sentence-BERT 等,專門把整段文字壓縮成 vector,用於:
- RAG(retrieval-augmented generation)
- 語意搜尋
- 分類 / cluster
Self-Attention 核心
每個 token 看其他所有 token,決定該借多少「資訊」。
公式
給定輸入序列 X = [x_1, x_2, ..., x_n](每 x_i 是 vector)
Query: Q = X · W_Q (這 token「想找什麼」)
Key: K = X · W_K (這 token「能提供什麼」)
Value: V = X · W_V (這 token「實際內容」)
Attention: A = softmax(Q · K^T / √d_k) · V
↑ ↑
權重 (n × n) 加權平均
直覺
把每對 token 算「相關度」(Q · K),做 softmax 變權重,然後用權重加權所有 Value。
"The cat sat on the mat"
↓ self-attention on "sat"
"sat" 的 Q 對所有 token 算 score:
cat: high(主詞)
mat: medium(地點 metaphor)
the: low
on: medium(關係詞)
the: low
Multi-Head
不只一組 Q/K/V,而是 H 組(H = 12 / 16 / 32 / …),最後 concat:
head_1: 可能學「主詞-動詞」
head_2: 可能學「介系詞-受詞」
head_3: 可能學「代名詞-先行詞」
...
每個 head 學不同的 attention pattern,組合起來更豐富。
Causal Mask(decoder-only,LLM 用)
LLM 是 autoregressive,生成第 t 個 token 時不能看見未來。Q · K^T 上三角部分填 -∞,softmax 後變 0 → 只看左邊。
t1 t2 t3 t4 t5
t1 ● - - - -
t2 ● ● - - - ← attention 矩陣的上三角 mask 掉
t3 ● ● ● - -
t4 ● ● ● ● -
t5 ● ● ● ● ●
Complexity
Self-attention 對序列長度 n 是 O(n²)。Context 8K → 64M ops;128K → 16B ops 一層 → context window 是大模型最痛的瓶頸。
改進方法
- FlashAttention:重排計算讓 GPU memory access 更快
- Sparse attention(Longformer / BigBird):只看某些 token
- Linear attention:近似算法,降到 O(n)
- Sliding window attention:Mistral 用,只看最近 W 個 token
- GQA(Grouped Query Attention):多 query 共享 K/V → 推理省記憶體
- MLA(Multi-Latent Attention,DeepSeek):K/V 壓縮
Feed-Forward Network(FFN)
每個 token 獨立過一個 2 層 MLP:
FFN(x) = W_2 · GeLU(W_1 · x + b_1) + b_2
↑
非線性 activation(GeLU / SwiGLU / ReLU)
FFN 是模型參數量的大頭(約 67%);self-attention 反而少(約 33%)。
MoE(Mixture-of-Experts):把 FFN 換成多個 expert + routing,稀疏啟用 → 參數規模可拉很大但 inference 成本不爆。Mixtral、DeepSeek-V3 用。
Residual + LayerNorm
每個 sub-layer 後加 x + f(x)(residual):
- 訊號穩定(不會被深度抹平)
- 梯度通路(深層 backprop 不消失)
LayerNorm 把每個 token vector normalize(平均 0、std 1),穩定訓練。
Pre-norm vs Post-norm:現代 LLM 多用 pre-norm(x + f(LayerNorm(x))),更穩。
RMSNorm:更輕量的變體,Llama 系用。
Positional Encoding
Transformer 本身無序(self-attention permutation-equivariant),要外加位置訊息。
| 方法 | 用法 | 代表模型 |
|---|---|---|
| Absolute(sinusoidal) | 原始論文,固定 sin/cos | 早期 transformer |
| Learned absolute | 可學位置 embedding | BERT |
| RoPE(Rotary) | 把位置編成旋轉矩陣,套在 Q/K 上 | Llama、Mistral、Qwen(主流) |
| ALiBi | attention score 加位置 bias | Bloom、MPT |
| NoPE | 不要 positional encoding | 實驗中 |
RoPE 的好處:外推性(可訓 4K context、推到 32K 還能用,加 scaling)+ 相對位置自然編碼。
Decoding Strategy
模型輸出機率分布後,怎麼選 token?
| 策略 | 做法 | 特性 |
|---|---|---|
| Greedy | argmax 取機率最大 | 確定 / 重複 |
| Beam Search | 同時保留 top-K 路徑 | 翻譯 / summarization 常用 |
| Temperature | 把 logits 除 T,T<1 銳化、T>1 平滑 | 控制 random 度 |
| Top-K sampling | 只從 top K 中 sample | 排除尾部 |
| Top-P(nucleus) | 累積機率 P 以下的最小集合 sample | 自適應 K |
| Min-P | 最低機率閾值 | 比 top-p 更穩 |
實務組合:temperature=0.7, top_p=0.9 是很多 default。
temperature=0:確定模式(deterministic for same prompt),但仍受 floating point + sampling library 細節影響。
訓練三階段(post-2022 主流)
1. Pre-training(自監督)
- 拿一堆網路文本(C4 / RedPajama / FineWeb / Common Crawl)
- 目標:預測下個 token
- 規模:1-15 trillion tokens、幾千張 GPU、幾個月
- 結果:base model(像 GPT-3 base、Llama base),不會「聽指令」,只會接著文本續寫
2. Supervised Fine-Tuning(SFT)
- 用 instruction-response pair 微調
- 例如
{"instruction": "解釋光合作用", "response": "..."} - 開源資料集:Alpaca、ShareGPT、OpenHermes
- 結果:會聽指令的 model
3. Preference Tuning(RLHF / DPO / GRPO)
- 蒐集 human preference:同一 prompt 兩個 response,人選哪個比較好
- 過去用 RLHF(PPO),現代多用 DPO(Direct Preference Optimization,免 RL)、GRPO(reasoning 加強)
- 結果:aligned model,風格 / 安全 / helpfulness 提升
Reasoning 模型(o1 / DeepSeek-R1 / Claude thinking)
- 在 RL 訓練中放大 reward「有沒有正確 reasoning 步驟」
- 模型學會「思考 → 修正 → 再思考」
- inference 時花更多 token 思考(chain-of-thought 內生化)
Context Window
模型一次能看多少 token。
| 模型 | context |
|---|---|
| GPT-3.5 | 4K → 16K |
| GPT-4 turbo | 128K |
| Claude 3.5 / 4 / 4.7 | 200K |
| Gemini 1.5 | 1M-2M |
| 開源(Llama 3.1+) | 128K |
限制
- O(n²) self-attention → 越長越慢
- 「lost in the middle」效應:中段資訊比頭尾容易被忽略
- KV cache 隨 context 增加佔記憶體
擴展手法
- RoPE scaling / interpolation
- 訓練資料用 long context 加強
- 推理時用 sparse attention 或 attention sink
RAG(Retrieval-Augmented Generation)
問題:LLM 知識凍結在訓練截止;企業內部資料不在訓練集。
解法:給 LLM「外部知識」當輔助。
基本流程
Query → embed → 向量資料庫 ANN 搜尋 → top-K 相關文檔
↓
Prompt: "用以下資料回答:[chunks]\n\n問題:{query}"
↓
LLM 生成 answer
關鍵元件
| 元件 | 用途 | 工具 |
|---|---|---|
| Chunking | 切文檔成段(200-1000 tokens) | 固定大小 / sentence / semantic |
| Embedding | 段 → vector | OpenAI / BGE / E5 / cohere |
| Vector DB | 存 vector + ANN 搜尋 | Pinecone / Weaviate / Qdrant / pgvector |
| Re-ranker | 從 top-50 精排到 top-10 | Cohere rerank / BGE reranker |
| Prompt template | 把 chunks + query 組成 prompt | LangChain / 自寫 |
常見坑
- chunk 切太細 → 失去上下文
- chunk 切太粗 → 相關段落被淹沒
- 純向量搜尋漏重要關鍵字 → 混合 BM25 / hybrid search
- 沒 re-rank → top-K 噪音多
Fine-tuning vs LoRA
Full fine-tuning
更新所有參數。需要大量 GPU memory(7B 模型大概要 100GB+ 訓練時記憶體)。
LoRA(Low-Rank Adaptation)
凍結原 model,加小 rank-r 矩陣 ΔW = A · B(A: d×r, B: r×k,r 通常 8-64)只訓 A、B。
- 訓練只動約 1% 參數
- 一個 LoRA adapter 幾 MB,可熱插拔
- 推理時
W + ΔW合併進去 - QLoRA:base model 用 4-bit 量化 + LoRA → 24GB GPU 訓 65B model
何時 fine-tune?
- format 一致性需求(JSON 輸出、特定 style)
- 領域知識深度(法律、醫療,prompt 帶不完)
- 降低 latency / cost(用小模型代替大模型 + 大 prompt)
何時 RAG > fine-tune?
- 知識更新快(每天加文檔)
- 需要 source citation
- 不想冒 hallucination 風險
實務常兩者並用:base model + LoRA(format / style)+ RAG(knowledge)。
量化(Quantization)
把 weights 從 FP16/BF16 降到 INT8 / INT4 / INT2,inference 省 memory 並加速。
| 精度 | size / 7B model | 品質損失 |
|---|---|---|
| FP16 / BF16 | 14 GB | baseline |
| INT8 | 7 GB | 微小 |
| INT4(GPTQ/AWQ/GGUF q4_K_M) | 3.5 GB | 小到可忽略 |
| INT2 | 1.7 GB | 明顯 |
工具:llama.cpp(GGUF 格式)、vllm(AWQ)、ExLlama、MLX(Mac)。
推理框架對比
| 框架 | 適合 | 特性 |
|---|---|---|
| llama.cpp | CPU / 消費級 GPU / Mac | 量化、輕量 |
| vLLM | Server GPU 高吞吐 | PagedAttention、continuous batching |
| TensorRT-LLM | NVIDIA 極致優化 | 編譯式 |
| Ollama | 本機 model server | 包 llama.cpp + UX |
| MLX | Apple Silicon | Metal 加速 |
| Transformers(HF) | 研究 / 原型 | 慢但通用 |
評估 LLM
| benchmark | 測 |
|---|---|
| MMLU | 多領域知識 / 推理 |
| HumanEval / MBPP | 程式生成 |
| HellaSwag / WinoGrande | 常識推理 |
| GSM8K / MATH | 數學 |
| BBH(BIG-Bench Hard) | 多任務 |
| MT-Bench / Arena Elo | 對話品質 |
| 長 context:RULER / Needle in Haystack | 長序列檢索 |
benchmark 污染:多數 model 訓練資料含 benchmark 題目 → 分數虛高。看公開 leaderboard 要看「未發布的 holdout set」(如 LMSYS Arena)。
Prompt Engineering 速記
針對 LLM 結構性質,有效的手法:
| 技巧 | 為什麼有效 |
|---|---|
| Few-shot examples | LLM 強 pattern matching;例子比解釋有效 |
| Chain-of-thought(“Let’s think step by step”) | 模型生成中間 token 提供「思考空間」 |
| System prompt 設角色 | 把模型 attention 拉到特定領域 |
| 明確 output format(JSON schema) | 減少 free-form noise |
| 負面指令較弱(“don’t do X”) | LLM 對 negation 處理不穩,改正向描述 |
| 長 prompt 把重點放頭尾 | 避免 lost-in-the-middle |
| Self-consistency / 多 sample 投票 | sample 多次取多數,降錯誤率 |
常見誤解
- 「LLM 真的理解語言」:它學到的是統計關聯,沒有 grounded meaning,雖然 emergent 出來的行為很像理解
- 「LLM 有記憶」:沒有,只能靠 context window 重塞,再加外部 store(向量 DB)
- 「temperature=0 就是確定」:多數 framework 仍非完全 deterministic(GPU floating point 加 sampling library 的細節)
- 「更大的 model 一定更好」:看任務,Phi-3 / Gemma 3 在多數對話接近 GPT-3.5
- 「fine-tune 能加知識」:加 format / style 可以,加事實知識效率低又容易遺忘,RAG 更適合
- 「prompt engineering 是門手藝」:模型一升級就會抹平很多 prompt trick,別太依賴脆弱的 prompt
延伸閱讀
- Attention Is All You Need(Vaswani et al. 2017):Transformer 的原始 paper
- The Illustrated Transformer(Jay Alammar):必看圖解
- Sebastian Raschka’s LLM blog:magazine.sebastianraschka.com
- Hugging Face NLP Course:huggingface.co/learn/nlp-course
- LLM Visualization(Brendan Bycroft):bbycroft.net/llm,互動視覺化每個算子
- 3Blue1Brown:Neural Networks 系列的 Transformer 章節(YouTube)