$ cat ~/writing/2026-05-29-llm-transformer-internals.md

LLM Transformer 底層解析:Attention、Tokenization、RAG 與 Fine-tuning

DATE2026·05·29
TAGSAI · LLM · Transformer
READING8 min

不管是 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)
Embeddingtoken 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 系用)

  1. 從字元開始
  2. 找出最常出現的兩個相鄰 token,合成新 token
  3. 重複到字典大小目標
  4. 訓練後用 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.apptiktoken library 實測。

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-largeBGEE5、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可學位置 embeddingBERT
RoPE(Rotary)把位置編成旋轉矩陣,套在 Q/K 上Llama、Mistral、Qwen(主流)
ALiBiattention score 加位置 biasBloom、MPT
NoPE不要 positional encoding實驗中

RoPE 的好處:外推性(可訓 4K context、推到 32K 還能用,加 scaling)+ 相對位置自然編碼。

Decoding Strategy

模型輸出機率分布後,怎麼選 token?

策略做法特性
Greedyargmax 取機率最大確定 / 重複
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.54K → 16K
GPT-4 turbo128K
Claude 3.5 / 4 / 4.7200K
Gemini 1.51M-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段 → vectorOpenAI / BGE / E5 / cohere
Vector DB存 vector + ANN 搜尋Pinecone / Weaviate / Qdrant / pgvector
Re-ranker從 top-50 精排到 top-10Cohere rerank / BGE reranker
Prompt template把 chunks + query 組成 promptLangChain / 自寫

常見坑

  • 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 / BF1614 GBbaseline
INT87 GB微小
INT4(GPTQ/AWQ/GGUF q4_K_M)3.5 GB小到可忽略
INT21.7 GB明顯

工具:llama.cpp(GGUF 格式)、vllm(AWQ)、ExLlamaMLX(Mac)。

推理框架對比

框架適合特性
llama.cppCPU / 消費級 GPU / Mac量化、輕量
vLLMServer GPU 高吞吐PagedAttention、continuous batching
TensorRT-LLMNVIDIA 極致優化編譯式
Ollama本機 model server包 llama.cpp + UX
MLXApple SiliconMetal 加速
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 examplesLLM 強 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)