How to Make LLMs 3X Faster
語言模型中的文字生成是以 autoregressive 方式運作,透過跨所有模型層的循序 forward pass,一次生成一個 token。由於每個新 token 都嚴格依賴所有先前 token 的存在,因此 forward pass 無法在不破壞連貫性的情況下同時執行。因此,總生成時間等於 token 數量乘以每次 pass 的 latency,而這會隨著模型大小而增加。現代 inference 系統透過使用 KV cache 儲存先前的 attention states 來減輕後續 pass 的運算負擔,但每個 token 需要一次 pass 的要求仍然存在。在模型 inference 期間,forward pass 將大部分執行時間花在搬移資料而非算術運算上。在 token generation 過程中,每個單一 token 都必須將模型權重從 VRAM 傳輸到 compute units,導致 compute utilization 偏低(介於 20% 到 40% 之間),而 memory bus 則幾乎滿載運作。相較之下,prompt processing 可達到 90% 到 95% 的 utilization,因為權重會在數千個輸入 token 之間被重複使用。因此,提高 memory bandwidth 對加速 token generation 的效果,顯著大於單純增加 raw compute capacity。Transformers 可以在單次 forward pass 中同時評估整個序列,一次為每個位置生成 next-token prediction。attention 機制中的 causal masking 確保每個位置僅 attend 至前面的 token,從而維持 autoregressive conditioning 的有效性。因此,candidate tokens 可以在單次 pass 中平行驗證而非循序驗證,這與 prompt processing 的運算效率如出一轍。
閱讀原文 ↗目錄
Autoregressive Decoding
語言模型中的文字生成是以 autoregressive 方式運作,透過跨所有模型層的循序 forward pass,一次生成一個 token。由於每個新 token 都嚴格依賴所有先前 token 的存在,因此 forward pass 無法在不破壞連貫性的情況下同時執行。因此,總生成時間等於 token 數量乘以每次 pass 的 latency,而這會隨著模型大小而增加。現代 inference 系統透過使用 KV cache 儲存先前的 attention states 來減輕後續 pass 的運算負擔,但每個 token 需要一次 pass 的要求仍然存在。
- 文字生成是循序執行的,計算機率分佈並一次附加一個 token。
- 一個 500 個 token 的輸出需要 500 次循序的 forward pass,因為每個 token 都依賴先前的 token。
- 總生成 latency 是由生成的 token 數量乘以單次 forward pass 的時間所決定。
- 無論輸出是文字還是程式碼,每個 token 的成本都是一致的,但在相同硬體上運行較大的模型時成本會增加。
- KV cache 儲存先前已處理 token 的 attention states,能減少每次 pass 的工作負載,但無法免除每個 token 需執行一次 pass 的要求。
Memory Bandwidth
在模型 inference 期間,forward pass 將大部分執行時間花在搬移資料而非算術運算上。在 token generation 過程中,每個單一 token 都必須將模型權重從 VRAM 傳輸到 compute units,導致 compute utilization 偏低(介於 20% 到 40% 之間),而 memory bus 則幾乎滿載運作。相較之下,prompt processing 可達到 90% 到 95% 的 utilization,因為權重會在數千個輸入 token 之間被重複使用。因此,提高 memory bandwidth 對加速 token generation 的效果,顯著大於單純增加 raw compute capacity。
- 單次 forward pass 的大部分時間都花在從記憶體搬移資料,而非執行算術運算。
- 對於 16-bit 精度的 700 億參數模型,每生成一個 token 大約必須傳輸 140 GB 的權重。
- Compute utilization 從 prompt processing 期間的 90-95% 下降到 token generation 期間的 20-40%。
- Prompt processing 會一次將載入的權重套用在數千個 token 上,而 token generation 則是為了單一 token 重新載入權重。
- 相較於 raw compute capability,GPU memory bandwidth 是影響 token generation 速度更為關鍵的因素。
Parallel Verification
Transformers 可以在單次 forward pass 中同時評估整個 sequence,一次為每個 position 生成 next-token prediction。attention mechanism 中的 causal masking 能確保每個 position 僅 attend 至先前的 token,維持 autoregressive conditioning 的有效性。因此,candidate token 可以在單次 pass 中平行驗證,而非循序處理,這與 prompt processing 的運算效率如出一轍。
- Transformer 在單次 forward pass 中同時評估所有 sequence position,並在每個 position 生成 next-token prediction。
- attention mechanism 內部的 causal masking 可防止各 position attend 到後續的 token,保有與 sequential generation 完全相同的 conditioning。
- Prompt processing 利用這種平行 forward pass 在單次 pass 中處理數千個 token,而非一次僅處理一個 token。
- 將 candidate token 接在 context 之後並執行單次 forward pass,即可同時對其進行驗證。
- Verification 與 generation 在每個 position 上執行的運算量相同,但 parallel verification 透過將多次評估整合至單次 pass 中來節省成本。
Draft and Verify
Draft and verify 架構透過結合小型 draft model 與大型 target model 來加速語言模型 inference。在每個 round 中,draft model 會循序生成一系列 candidate token,接著 target model 會在單次 forward pass 中同時驗證這些 token。Token 會依序被接受,直到出現第一個 mismatch 為止,此時 target model 已經計算出的 prediction 會被保留作為額外的正確 token。此設定將最差情況下的 throughput 限制在標準 single-token decoding 的水準,同時在 draft length 通常設為 3 到 5 時帶來顯著的 speedup。
- 此 inference loop 依賴兩個模型:主要 target model,以及參數量少 10 到 20 倍且通常共享同一個 tokenizer 的 draft model。
- 每個 round 包含三個執行步驟:循序 draft 出 K 個 token、由 target model 進行單次 pass 的 batched evaluation,以及由左至右(left-to-right)的 verification。
- 吻合的 token 會被保留,且在無需額外運算成本的情況下,第一個 mismatch 位置處的 target model prediction 也會被接受。
- 在所有 candidate token 皆被拒絕的最差情況下,系統仍會輸出一個正確的 token,與標準 decoding 的 baseline 產出相同。
- Draft length K 通常調整在 3 到 5 之間,以在潛在 speedup 與後續 token 被接受機率遞減之間取得平衡。
Lossless Guarantee
Speculative decoding 透過 acceptance rule 來確保生成的文本具有與 target model 獨立運作時完全相同的統計特性。在 greedy decoding 中,只有當 candidate token 與 target model 的 top prediction 吻合時才會被接受。當採用帶有隨機性的 sampling 時,candidate 的接受與否取決於 target model 與 draft model 之間的機率分佈比較,被拒絕的 token 則會從調整後的分佈(adjusted distribution)中重新 sampling。最終產生的 token 機率嚴格依賴於 target model,僅會因為固有的 sampling variance 或數值精度限制(numerical precision limits)而產生偏差。
- Speculative decoding 透過 acceptance rule 來保留 target model 精確的 output distribution。
- 在 greedy decoding 下,只有當 candidate token 等同於 target model 機率最高的選擇時,才會被保留。
- 在 sampling 下,若 target model 分配給 candidate 的機率大於或等於 draft model,則該 candidate 一律被接受;若機率較低,則依機率接受。
- 被拒絕的 candidate 會透過從扣除 draft model 機率後的 adjusted distribution 中 sampling 來取代,從而確保與 target model 在數學上的等價性。
- 由於固有的 sampling 隨機性以及硬體層級的浮點數捨入限制(floating-point rounding limits),不同 run 之間的輸出仍可能存在差異。
Acceptance Rate
在 candidate-token 加速方法中,speedup 主要取決於 acceptance rate,它代表 target model 保留 proposed token 的比例。具重複性或結構化的任務(如 code generation 與 summarization)具有較高的 acceptance rate,而開放式任務(如 creative writing)則常導致模型 divergence 以及較低的 acceptance。較高的 sampling temperature 會透過拉平 output probability distribution 進一步降低 acceptance,當 acceptance 降至約 50% 以下時,該流程的效率便會顯著下降。在實際應用中,DeepSeek 記錄了在 DeepSeek-V3 的 production serving 期間,第二個預測 token 的 acceptance rate 達到 80% 至 90%,帶來約 1.8x 的 generation throughput。
- Acceptance rate 是 target model 所保留 candidate token 的比例,而 acceptance length 則包含確認的 token 加上一個隨附的 trailing free token。
- 結構化與重複性的 workload(例如 code generation、summarization 與 retrieval-augmented answers)能帶來高 acceptance rate。
- 開放式輸出(例如創意寫作與對話)會因小型模型的 divergence 而導致較低的 acceptance rate。
- 較高的 sampling temperature 會使 token 機率分佈平坦化,並降低 acceptance。
- 當 acceptance rate 低於約 50% 時,計算 overhead 將會超過效率收益。
- DeepSeek 在 DeepSeek-V3 的 production serving 中,第二個預測 token 達到了 80% 至 90% 的 acceptance,實現約 1.8 倍的 generation throughput。
Candidate 或 Draft 來源
Speculative drafting 依賴四種主要機制來獲取快速、低成本的 token 預測:部署獨立的小型 sibling model、在 target architecture 中加入 multi-token prediction heads、執行相同權重的低精度(lower-precision)或 pruned 變體,或從先前 context 中檢索匹配的 sequence。每種選項在硬體 overhead、訓練需求、實作複雜度以及 context 適用性之間都有不同的 trade-off。Tokenizer 的 alignment 嚴格決定了 draft 的相容性,其門檻往往高於模型品質的差異。歸根結底,這四種 drafting 策略在 serving 期間都需要足夠的 spare compute capacity 才能帶來淨加速(net speedup)。
- 使用獨立的 smaller model 需要相同的 tokenizer 與 family,並會佔用伺服器原本可用於 KV cache budget 的 VRAM。
- 輕量級的額外 prediction heads 可以在 pretraining 期間與 base model 一同訓練,並轉用於 inference drafting,如 DeepSeek-V3 所示。
- QuantSpec 使用 4-bit 權重與 4-bit KV cache 執行 draft generation,在 acceptance rate 超過 90% 的情況下實現了 1.78 倍以上的加速。
- 基於 context 的文字搜尋不需要任何 memory overhead,並在文件編輯與摘要等重複性任務中提供 2 倍至 4 倍的加速。
- 除非實作額外的 translation 機制,否則 draft model 無法與 vocabulary 不同的 target model 配對。
Concurrency 限制
Speculative decoding 帶來的加速幅度高度取決於伺服器負載與運行的 concurrency。隨著並行請求增加,運算單元趨於飽和,verification 必須與傳入的請求競爭資源,導致 latency 的改善減少,甚至可能低於 baseline throughput。如 vLLM 等 serving 系統透過在超過特定 batch size 時動態縮短 draft length 或直接停用 speculation 來平衡這項 trade-off。此外,由於該技術針對的是 token generation 而非 prompt evaluation,因此對於長 prompt、短 output 特徵的 workload 而言,能帶來的 latency 效益非常有限。
- 隨著伺服器負載增加且運算容量趨於飽和,speculative decoding 的效益會逐漸遞減。
- 在 70B 模型上的實證評估顯示,加速效果從 batch size 為 1 時的 1.96 倍下降至 batch size 為 128 時的 1.21 倍,且在高 concurrency 下可能導致淨 throughput 損失。
- vLLM 允許使用者設定 batch size 門檻值,以動態縮短 draft length 或完全停用 speculation。
- Time to first token (TTFT) 不會因 speculative decoding 而改變,因而限制了它在長 prompt、短 completion workload 中的實用性。
- DeepSeek 觀察到 multi-token prediction 會略微降低 throughput,但能顯著改善 end-to-end 的 generation latency。
結論
Speculative decoding 重新調整了 token generation 的時序,而非減少 target model 執行的總運算量。它利用了 Transformer 架構能以大約評估單一 token 的成本來平行評估多個 candidate tokens 的能力。當 drafted tokens 被拒絕時,verification 階段仍會在 mismatch 的位置產出一個正確的 token,避免運算浪費。因此,透過 acceptance rule 在設計上就能維持 baseline 的輸出品質,無需額外的 tuning。
- Token generation 屬於 memory-bandwidth bound,因為讀取模型權重佔據的主導地位遠高於算術運算的執行。
- Transformers 可以在單次平行 pass 中評估多個 candidate tokens,其成本與評估單個 token 大致相同。
- Draft 被拒絕時會被截斷(truncate)而非浪費運算資源,因為 verification 步驟會在 mismatch 位置產出一個有效的 token。
- 透過 acceptance rule 直接保留輸出品質,無需進行 tuning。
- 效能加速取決於輸出的可預測性、伺服器的 spare compute capacity 以及 draft generation 的成本。