封面圖

日期:2026-08-17
適合讀者:研究生、LLM 系統入門研究者、有工程背景的 AI 讀者
檢索日期:2026-08-17

摘要

Efficient Inference 研究的核心不是“讓模型跑得快一點”,而是理解自迴歸生成的成本結構:prefill 階段通常更像大矩陣計算,decode 階段則被 KV cache 讀寫、顯存帶寬、調度和 batch 形態限制。KV cache、PagedAttention、continuous batching、speculative decoding、量化、prefix cache、prefill/decode disaggregation 和 serving runtime,都是圍繞這條成本鏈條做手術。

本文給出一條論文精讀路線:先把推理拆成 TTFT、ITL、throughput、顯存、質量和調度六本賬,再按 KV cache、speculative decoding、quantization、kernel/runtime、serving system 五條線讀代表論文。最後給出可復現的小實驗設計,幫助研究生判斷一個推理優化方法到底省掉了什麼、犧牲了什麼、能不能在自己的模型和負載上成立。

目錄

  1. 為什麼這個主題重要
  2. 核心閱讀方法
  3. 代表論文路線圖
  4. 方法與實驗對比表
  5. 復現建議
  6. 常見誤區
  7. 適合研究生繼續做的選題
  8. 總結與參考資料

為什麼這個主題重要

訓練效率回答“模型如何被做出來”,推理效率回答“模型如何被反覆使用”。對研究生而言,後者同樣是嚴肅研究問題:同一個開源模型,在不同 serving engine、batch policy、cache policy、quantization recipe 和 decoding strategy 下,TTFT、ITL、吞吐、顯存峰值和輸出質量可能完全不同。論文如果只報告“快了幾倍”,但沒有說明 prompt 長度、輸出長度、batch 到達過程、GPU 型號、併發、sampling 設置和質量檢查,結論很難複用。

現代 LLM 推理的第一條物理約束是自迴歸。生成 100 個 token 不是一次 forward,而是 100 次帶狀態的迭代。prefill 先處理輸入 prompt,生成並保存每層 attention 的 key/value;decode 每步只新增一個或少量 token,但要讀取已有 KV cache。長上下文、多輪對話、RAG 和 agent 任務會把 KV cache 變成主要顯存資產。TMLR 2025 的 KV cache survey 將相關方法分成 token-level、model-level 和 system-level 優化,說明這個方向已經從單個 trick 變成獨立研究地圖。

第二條約束是 workload。離線批處理追求 tokens/sec;交互式問答關注 TTFT 和 ITL;代碼補全關注短延遲和取消請求;RAG 關注 prefix reuse;agent 工作流關注多次小請求和結構化輸出;MoE 模型還多出 expert routing 和跨卡通信。Efficient Inference 論文必須和負載綁定閱讀,不能把一個場景裡的速度結論直接外推到另一個場景。

核心閱讀方法

讀推理論文,先畫六本賬。

第一是 latency 賬。TTFT 是從請求到第一個 token 的時間,主要受 prefill、排隊、調度和 cache 命中影響;ITL 是連續輸出 token 之間的間隔,decode、顯存帶寬、KV cache 讀取和 sampling kernel 更關鍵。只報告平均延遲不夠,最好看 p50/p95/p99。

第二是 throughput 賬。tokens/sec 要區分 prompt tokens、generated tokens、總 tokens 和每 GPU tokens/sec。批量越大吞吐通常越高,但用戶等待可能變長。continuous batching、chunked prefill、request preemption 和 admission control 都是在吞吐與延遲之間做調度。

第三是 memory 賬。模型權重、KV cache、activation 臨時量、CUDA graph、workspace、fragmentation、LoRA adapter、paged blocks 都要分開看。PagedAttention 的貢獻就在於把 KV cache 從連續分配改成類似虛擬內存的 block 管理,減少碎片和重複存儲,從而允許更大的有效 batch。

第四是 quality 賬。量化、speculative decoding、cache eviction、prefix reuse、structured decoding 都可能影響輸出分佈或任務指標。Leviathan 等人的 speculative decoding 明確追求不改變目標模型分佈;Medusa-2、近似 cache 複用或低比特量化則需要額外質量評估,不能只看速度。

第五是 implementation 賬。FlashAttention、FlashInfer、TensorRT-LLM、vLLM、SGLang、TGI、Transformers cache classes 的結論都依賴 CUDA、ROCm、GPU 架構、kernel 版本、模型結構、dtype 和框架版本。正式復現實驗必須寫清楚 commit、鏡像、驅動和啟動參數。

第六是 workload 賬。用固定 batch 的 synthetic benchmark 不能代表線上 Poisson 到達;用 128-token prompt 不能代表長上下文;用 greedy decoding 不能代表 top-p sampling;用單輪 QA 不能代表 RAG 或 agent。讀論文時要把作者的負載生成器復刻出來,否則速度數字沒有座標系。

Efficient Inference 閱讀路線圖

代表論文路線圖

1. KV Cache 與 PagedAttention:推理顯存的基本賬本

KV cache 的直覺很簡單:自迴歸模型每步都需要過去 token 的 key/value,如果每次重新計算,代價不可接受;保存並複用 KV 可以減少重複計算,但會佔用大量顯存。Hugging Face Transformers 文檔把 cache strategy 區分為 dynamic、static、offloaded、quantized 等類型,強調不同 cache 在速度、顯存和編譯兼容性之間有取捨。

PagedAttention 是這條線的關鍵論文。Kwon 等人在 SOSP 2023 / arXiv 2309.06180 中指出,LLM serving 要高吞吐就要 batching,但每個 request 的 KV cache 巨大且動態變化,低效管理會造成碎片和重複。PagedAttention 借鑑操作系統 paging,把 KV cache 存在非連續 blocks 中;vLLM 在此基礎上做 serving system。精讀這篇時,不要只記“2-4x throughput”這樣的摘要數字,而要追問:baseline 是什麼,prompt/output 長度如何分佈,latency constraint 怎樣固定,beam search 或 parallel sampling 為什麼更受益。

2025 之後的 KV cache 研究繼續向三類擴展。第一類是壓縮和量化,例如 KIVI 研究 key cache 和 value cache 的分佈差異,提出 tuning-free asymmetric 2-bit KV cache quantization。第二類是選擇、丟棄、合併和預算分配,試圖在長上下文裡保留最有用的 KV。第三類是系統化複用,例如 LMCache 將 KV cache 從單次請求狀態變成可跨請求、跨 engine、跨存儲層級複用的基礎設施;SGLang 的 RadixAttention 也服務於複雜程序裡的 prefix/cache reuse。

2. Speculative Decoding:減少串行 decode 步數

speculative decoding 的問題設定是:decode 必須逐 token 串行,但 GPU 在單 token decode 時利用率低。Leviathan、Kalman 和 Matias 在 ICML 2023 中提出用小 draft model 先猜多個 token,再由大 target model 並行驗證;如果驗收通過,就一次前進多個 token。該論文的重要約束是“不改變輸出分佈”,因此它不只是 heuristic acceleration,而是帶採樣校正的算法。

讀 speculative decoding 要抓三點。第一,draft model 的質量決定 acceptance rate;draft 太弱會浪費驗證,draft 太強又可能抵消速度收益。第二,速度受 batch size、sequence length、sampling temperature、target/draft 比例影響,不是固定常數。第三,系統實現很關鍵:兩個模型如何放置、KV cache 如何管理、draft 與 target 是否共享 tokenizer、通信和調度是否抵消收益。

Medusa 和 EAGLE 是後續代表。Medusa 不維護獨立 draft model,而是在同一 backbone 上加多個 decoding heads,用 tree attention 驗證候選;其 arXiv 版本報告了 Medusa-1/Medusa-2 兩種訓練設置。EAGLE 則把草稿生成轉到 feature level,並在 2025 修訂版中繼續完善。讀這些論文時,尤其要區分“lossless acceleration”“保持質量近似不變”和“需要額外訓練”的邊界。

3. Quantization:權重、激活和 KV Cache 的低精度推理

量化論文最容易被誤讀成“bit 越低越好”。實際要分清 weight-only、weight+activation、KV cache quantization、FP8/FP4、INT8/INT4、per-channel/per-token/group-wise、calibration set 和 kernel 支持。

GPTQ 是 post-training weight quantization 的經典起點,使用近似二階信息做 one-shot quantization。SmoothQuant 針對 activation outlier,把量化困難從 activation 平滑遷移到 weights,從而支持 W8A8 推理。AWQ 則觀察到保護少量 salient weights 可以顯著降低誤差,使用 activation-aware scaling 做低比特 weight-only quantization,並在 MLSys 2024 發表。KIVI 把注意力轉向 KV cache,指出 key cache 更適合 per-channel,value cache 更適合 per-token。

讀量化論文時必須同時檢查三張表:質量表、速度表、硬件表。質量表要看任務是否覆蓋困惑度、問答、代碼、數學、長上下文和多模態;速度表要確認是端到端 serving 還是單 kernel microbenchmark;硬件表要確認 A100/H100/4090/Jetson/MI300X/CPU 的差異。TensorRT-LLM 和 vLLM 文檔顯示,FP8 KV cache、AWQ、GPTQ、FP4/NVFP4、不同 GPU 後端支持都在持續變化,因此引用時要標註版本和檢索日期。

4. Kernel 與 Runtime:從 FlashAttention 到 FlashInfer

推理優化不是隻在論文公式裡發生。attention kernel、GEMM kernel、sampling kernel、CUDA graph、torch.compile、paged/ragged KV layout、MoE grouped GEMM 和 fused operators 都會決定論文方法是否真的更快。

FlashAttention 系列解決的是 IO-aware attention,訓練和 prefill 都受益。FlashInfer 更直接面向 LLM serving kernel,官方倉庫列出 prefill、decode、append、paged/ragged KV cache、MLA attention、cascade attention、sparse attention、FP8/FP4、fused MoE、sorting-free sampling 和 speculative decoding 等能力。讀 kernel/runtime 文檔時,研究問題不是“哪個庫最快”,而是弄清楚為什麼某個 workload 落到某個 kernel:batch 是否 ragged,KV blocks 是否 paged,是否混合 prefill/decode,是否 MoE,是否 structured output,是否需要低精度。

5. Serving System:vLLM、SGLang、TensorRT-LLM、TGI、LMCache

serving system 論文和文檔要按“請求生命週期”來讀:入隊、tokenize、prefill、KV block 分配、decode step scheduling、sampling、detokenize、streaming、finish、釋放 cache、指標上報。vLLM 官網強調 PagedAttention、advanced scheduling 和 continuous batching;SGLang 的 NeurIPS 2024 論文把 structured language model programs 作為對象,使用 RadixAttention 和 runtime 優化支持 agent、few-shot、JSON decoding、RAG、多輪對話等複雜程序;LMCache 把 KV cache 管理獨立出來,支持 GPU/CPU/disk/remote storage、非 prefix 複用、prefill/decode disaggregation 和觀測指標;TensorRT-LLM 提供量化、speculative decoding、KV cache connector/offloading 等生產級功能;Hugging Face TGI 文檔則顯示早期 optimized inference engine 的 common feature:continuous batching、Flash/Paged Attention、tensor parallelism、streaming、quantization 和 metrics。

這一類資料更新非常快。閱讀時不要把官網宣傳語當成論文結論,而要回到 release note、benchmark 腳本、commit、配置文件和可復現實驗。尤其是“支持某模型”“支持某量化格式”“支持某 GPU 架構”會隨版本變化,文章中只能寫檢索日期下的狀態,正式發佈前應重新核對。

方法與實驗對比表

分支 代表資料 主要節省對象 主要代價 精讀檢查
PagedAttention / vLLM PagedAttention, vLLM KV cache 碎片、重複存儲、有效 batch block 管理與調度複雜度 prompt/output 分佈、latency constraint、baseline
KV cache quantization KIVI, Transformers QuantizedCache KV cache 顯存、長上下文 batch 量化誤差、額外 dequant 開銷 key/value 分佈、長短上下文、質量指標
Speculative decoding Leviathan et al., Medusa, EAGLE 串行 decode 步數 draft/heads 訓練、驗收率、系統複雜度 acceptance rate、distribution/quality、batch size
Weight/activation quantization GPTQ, SmoothQuant, AWQ, TensorRT-LLM 權重顯存、帶寬、GEMM 成本 校準、kernel 支持、精度下降 W/A/KV 位寬、校準集、端到端吞吐
Kernel runtime FlashAttention, FlashInfer HBM IO、attention/sampling/MoE kernel 開銷 硬件和版本依賴 kernel benchmark vs serving benchmark
Serving framework vLLM, SGLang, TGI, TensorRT-LLM 調度、batch、並行、streaming 配置複雜、負載敏感 TTFT、ITL、p95、取消請求、監控
KV reuse layer LMCache, RadixAttention 重複 prefill、長上下文複用 cache 一致性、存儲和傳輸開銷 cache hit rate、reuse 粒度、質量恢復

復現建議

第一,做一個最小 serving benchmark。選 7B-8B 級模型,固定 GPU、dtype、prompt 長度分佈和 output 長度分佈,比較 Transformers generate、vLLM、SGLang 或 TGI。至少記錄 TTFT、ITL、request/sec、prompt tokens/sec、decode tokens/sec、GPU memory、GPU utilization、p50/p95。不要只記錄單條請求時間。

第二,復現 KV cache 賬本。改變 max context、batch size、output length,打開/關閉 paged attention、prefix caching、offloading 或 quantized cache。把顯存拆成 model weights、KV cache、其他 runtime workspace。長上下文實驗要額外記錄 OOM 邊界和是否出現 cache eviction。

第三,復現 speculative decoding。用一個 target model 和一個小 draft model,比較 greedy 與 sampling 兩種設置。記錄 acceptance rate、平均每步接受 token 數、target forward 次數、draft forward 次數、TTFT、ITL、輸出一致性或任務指標。如果使用 Medusa/EAGLE,要說明 heads 或 feature predictor 是否重新訓練。

第四,復現量化。選擇 GPTQ/AWQ/SmoothQuant/FP8 中一個 recipe,固定同一模型和同一評測集。報告 perplexity 或任務指標、端到端 latency、吞吐、顯存和加載時間。不要把單個 GEMM kernel 的加速直接等同於應用端到端加速。

第五,做 serving workload ablation。生成三類請求:短 prompt 短輸出、長 prompt 短輸出、長 prompt 長輸出。再分別用低併發、高併發、burst 到達。觀察 continuous batching、chunked prefill、prefix cache 和 P/D disaggregation 在不同場景下的變化。負結果要保留,因為推理系統的邊界條件比單次最高吞吐更重要。

常見誤區

  1. 把 prefill 和 decode 混成一個指標。TTFT 和 ITL 反映不同瓶頸,優化方向也不同。
  2. 只看 tokens/sec。高吞吐可能來自大 batch,但 p95 latency 可能不可接受。
  3. 把 KV cache 當成免費狀態。長上下文和高併發下,KV cache 往往比權重更決定 batch 上限。
  4. 只引用速度倍數,不引用 workload。prompt/output 長度、併發、sampling、GPU、dtype、框架版本缺一不可。
  5. 忽略質量。量化、近似 cache、structured decoding、non-lossless speculative heads 都需要質量或分佈檢查。
  6. 把官網支持矩陣當永久事實。vLLM、SGLang、TensorRT-LLM、FlashInfer、Transformers 和 LMCache 都在快速更新,模型、GPU、量化格式和 API 行為必須按檢索日期核對。

適合研究生繼續做的選題

  1. 長上下文 KV cache 策略比較:在 LongBench 或自建長文檔 QA 上比較 full cache、quantized cache、offload、selection 和 eviction。
  2. Speculative decoding workload map:系統研究 draft size、temperature、batch size、輸出長度與 acceptance rate 的關係。
  3. 端到端量化復現報告:比較 AWQ/GPTQ/FP8 在同一 serving engine 下的質量、顯存和延遲。
  4. Prefix cache for RAG:構造多文檔複用負載,測量 prefix hit rate、TTFT 和質量。
  5. P/D disaggregation 小型實驗:在兩張或多張 GPU 上模擬 prefill worker 與 decode worker,分析傳輸瓶頸。
  6. Serving benchmark 可信度研究:比較 synthetic benchmark、真實 trace 和 agent workload 對系統排名的影響。
  7. Structured output decoding 成本分析:比較 JSON schema、tool call、grammar decoding 在不同 runtime 中的延遲和失敗率。

總結

Efficient Inference 的正確讀法,是把“更快”拆成可復現的成本賬本。KV cache 決定長上下文和併發的顯存邊界;PagedAttention 和 serving scheduler 決定 batch 是否真正有效;speculative decoding 試圖減少串行 decode 步數;quantization 減少權重、激活或 KV 的字節流;kernel/runtime 決定算法能否落到硬件;LMCache、SGLang、vLLM、TensorRT-LLM 等系統則把這些技術組合成請求生命週期。

科研新人可以從一個小模型和一張 GPU 開始訓練能力:固定 workload,改變一個推理開關,記錄 TTFT、ITL、吞吐、顯存、質量和失敗案例。只要賬本足夠清楚,一次“沒有加速”的復現實驗也可能比一張漂亮的速度表更有研究價值。

參考資料

檢索日期:2026-08-17。以下鏈接優先使用論文頁、官方項目頁、官方文檔、GitHub、PMLR、MLSys、TMLR/ML Anthology、Stanford MAST Lab、Hugging Face 和 NVIDIA 文檔。模型支持、GPU 架構、量化格式、benchmark 數字、倉庫 commit、API 行為和文檔內容變化很快,正式發佈前請重新核對;未在一手資料中確認的細節應標註“待人工核驗”。

  1. Efficient Memory Management for Large Language Model Serving with PagedAttention, arXiv 2309.06180 / SOSP 2023: https://arxiv.org/abs/2309.06180
  2. Efficient Memory Management for Large Language Model Serving with PagedAttention, ACM SOSP 2023: https://doi.org/10.1145/3600006.3613165
  3. vLLM official website: https://vllm.ai/
  4. vLLM GitHub repository: https://github.com/vllm-project/vllm
  5. vLLM quantization documentation: https://docs.vllm.ai/en/latest/features/quantization/
  6. A Survey on Large Language Model Acceleration Based on KV Cache Management, TMLR 2025 / ML Anthology: https://mlanthology.org/tmlr/2025/li2025tmlr-survey/
  7. Hugging Face Transformers cache strategies documentation: https://huggingface.co/docs/transformers/main/kv_cache
  8. KIVI: A Tuning-Free Asymmetric 2bit Quantization for KV Cache, arXiv 2402.02750: https://arxiv.org/abs/2402.02750
  9. KIVI official GitHub repository: https://github.com/jy-yuan/KIVI
  10. Fast Inference from Transformers via Speculative Decoding, ICML 2023 / PMLR: https://proceedings.mlr.press/v202/leviathan23a.html
  11. Medusa: Simple LLM Inference Acceleration Framework with Multiple Decoding Heads, arXiv 2401.10774: https://arxiv.org/abs/2401.10774
  12. Medusa official GitHub repository: https://github.com/FasterDecoding/Medusa
  13. EAGLE: Speculative Sampling Requires Rethinking Feature Uncertainty, arXiv 2401.15077: https://arxiv.org/abs/2401.15077
  14. GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers, ICLR 2023 / OpenReview record: https://openreview.net/forum?id=tcbBPnfwxS
  15. GPTQ publication record, ETH Research Collection: https://www.research-collection.ethz.ch/entities/publication/00736213-37b2-4e99-b015-141349b71413
  16. SmoothQuant official GitHub repository: https://github.com/mit-han-lab/smoothquant
  17. AWQ: Activation-aware Weight Quantization for On-Device LLM Compression and Acceleration, MLSys 2024: https://proceedings.mlsys.org/paper_files/paper/2024/hash/42a452cbafa9dd64e9ba4aa95cc1ef21-Abstract-Conference.html
  18. AWQ official GitHub repository: https://github.com/mit-han-lab/llm-awq
  19. TensorRT-LLM official documentation: https://nvidia.github.io/TensorRT-LLM/
  20. TensorRT-LLM quantization documentation: https://github.com/NVIDIA/TensorRT-LLM/blob/main/docs/source/features/quantization.md
  21. TensorRT-LLM speculative decoding documentation: https://github.com/NVIDIA/TensorRT-LLM/blob/main/docs/source/features/speculative-decoding.md
  22. FlashInfer official GitHub repository: https://github.com/flashinfer-ai/flashinfer
  23. SGLang: Efficient Execution of Structured Language Model Programs, NeurIPS 2024 / Stanford MAST Lab: https://mast.stanford.edu/pubs/sglang_efficient_execution_of_structured_language_model_programs/
  24. SGLang official documentation: https://docs.sglang.io/
  25. LMCache official documentation: https://docs.lmcache.ai/index.html
  26. LMCache official GitHub repository: https://github.com/LMCache/LMCache
  27. Hugging Face Text Generation Inference documentation: https://huggingface.co/docs/text-generation-inference/index