目錄
本篇對應 CS336 Spring 2026 Lecture 10: Inference,2026 年 4 月 29 日由 Percy Liang 主講。主要來源是官方可執行講義 lecture_10.py。
訓練把整段序列放進矩陣乘法;autoregressive inference 每次只產生下一個 token。這個差異讓同一個 Transformer 在 serving 時變成另一種系統問題:權重與 KV cache 反覆從記憶體讀取,requests 又以不同時間抵達和結束。
三個指標代表三種產品需求
**Time to first token(TTFT)**是使用者送出 prompt 到第一個 token 的等待,主要受 prefill 影響。Inter-token latency是單一 request 後續 token 出現速度。Throughput是整個服務在多 requests 下每秒產生多少 tokens。
增大 batch 常改善 throughput,因為一次讀取權重可服務更多 sequences;但每個 request 要分享資源,latency 與 KV cache memory 可能惡化。互動聊天、離線 batch processing 與大量 RL rollout 不會選同一個 operating point。
Prefill 與 decode 的 arithmetic intensity 不同
Prefill 已知完整 prompt,可沿 sequence 平行,matrix multiplication 有較高 arithmetic intensity,通常偏 compute-bound。Decode 一次只有一個新 token,sequence 維度無法平行;MLP 每一步都要讀完整 weights,batch 小時容易 memory-bound。
Attention decode 還要讀每個 request 自己的 KV cache。Batching 能攤平共享 weights,卻無法共享各自的 K/V;context 越長,每個新 token 要掃過的 cache 越大。因此 generation attention 的 memory pressure 特別難靠單純增大 batch 解決。
第一條路:縮小 KV cache
GQA/MQA 減少 key/value heads;MLA 把 K/V 壓到較低維 latent;cross-layer attention 讓 layers 共享部分 cache。Local/sliding-window attention 截斷可見範圍,hybrid architecture 則在少數 layers 保留全域 attention。
Linear attention 與 state-space model 更激進地把歷史壓進固定 state。所有方法都交換容量、精確回看能力與 kernel 複雜度。「支援很長 context」不能只看最大長度,也要看 long-context accuracy、decode latency 與 cache bytes/token。
第二條路:量化與壓縮 weights
Inference 常從 bf16 降到 fp8、int8 或 int4。較少 bytes 同時降低 memory capacity 與 bandwidth 壓力,但 scale granularity、outlier channels 與硬體 kernel 會決定實際誤差與速度。
Quantization-aware training 在訓練 forward 模擬量化,模型能適應誤差,但代價高。Post-training quantization 便宜得多,可用 calibration data 決定 scales。GPTQ 類方法利用二階資訊補償逐步量化誤差;activation-aware 方法則讓重要權重保留較高 precision。
Pruning、distillation 與新架構也能降低成本,但要分清楚:量化改 representation,pruning 移除結構,distillation 重新訓練較小模型。它們的 quality loss 與 kernel availability 不同。
Speculative decoding 利用「驗證比生成容易」
較小 draft model 先猜數個 tokens,target model 一次平行評估。透過 acceptance/rejection 與 residual distribution,輸出仍能保持 target distribution 的 exact sampling;draft 猜得越準,一次 target pass 接受的 tokens 越多。
它沒有減少 target model 每次 pass 的大小,而是把 memory-bound 的逐 token passes 合併。收益取決於 draft cost、acceptance rate、batch 與 target hardware。Medusa、EAGLE 等方法則改造 draft 產生方式。
Continuous batching 與 PagedAttention 管動態 requests
Static batching 要等整批 sequences 都結束,短 request 會空等長 request。Continuous batching 每個 decode step 都可移除完成項目、加入新 request。不同長度形成 ragged batch:attention 需知道每段邊界,其他 operations 則可把 tokens concatenate 後處理。
KV cache 若為每個 request 預留最大連續區塊,會同時產生 internal 與 external fragmentation。PagedAttention 借用作業系統 paging,把 cache 切成非連續 blocks,以 page table 對應 logical sequence。它也方便共享 system prompt prefix 或同一 prompt 的多個 samples。
這就是 vLLM 成為重要 serving baseline 的核心想法:不是改模型答案,而是讓動態記憶體配置更接近實際使用量。
一個完整的 inference 測試
固定模型與硬體,分別掃 prompt length、output length、concurrency 與 precision。同時記 TTFT、p50/p99 inter-token latency、throughput、peak memory 與 quality regression,再把時間拆成 queue、prefill、decode 與 communication。
單報 tokens/s 會掩蓋使用者等待,單報 latency 則可能忽略 GPU 閒置。第十講的核心是把模型計算、記憶體與動態排程放進同一份帳。
材料完整度
本講有 Spring 2026 當期 schedule 與完整可執行講義。本文依講義的 inference accounting、KV cache、quantization、speculation 與 paging 結構整理。
參考資料
Loading...