Hacker News ★ 107 4 min

Smaller, faster, safer: running Kimi and GLM at scale

🔗 https://blog.cloudflare.com/smaller-faster-safer-models/

📌 【Cloudflare Workers AI】如何在大規模部署下,讓 Kimi 與 GLM 跑得更快、更省成本?

TL;DR:透過量化 KV cache 與模型權重,Cloudflare 在不犧牲準確度的前提下,大幅提升了長文本模型的記憶體容量與推理效能。

面對 Moonshot 的 Kimi K-series 與 Z.ai 的 GLM 等大型 Mixture-of-Experts (MoE) 模型,工程師面臨的最大挑戰往往不是運算能力,而是 GPU 記憶體的限制。當處理長文本時,記憶體通常會先被 KV cache 佔滿,而非模型權重。

Cloudflare Workers AI 透過結合 SGLang 推理框架,開發了三項核心優化技術來解決這個問題。

🧩 量化 KV cache:讓長文本容量翻倍

當模型生成文字時,會將處理過的 token 的 attention keys (K) 與 values (V) 儲存在 KV cache 中。對於長文本模型,這個 cache 會迅速膨脹並耗盡記憶體。

  • 技術手段:將預設的 16-bit (BF16) 精度改為 8-bit 浮點數 (FP8, e4m3)。
  • 實測結果:以 Kimi K2.6 為例,在 H200 部署環境下,上下文容量從約 68.6 萬 tokens 提升至 137 萬 tokens,直接翻倍。
  • 效能與成本:雖然 FP8 在單個 token 的運算上會因轉換而微幅變慢,但它讓 GPU 能同時處理更多併發請求。在 64 併發時,FP8 的吞吐量比 BF16 高出約 41%,且每 token 成本降低約 30%。

📊 壓縮模型權重:提升解碼階段的吞吐量

除了 KV cache,模型權重本身也是記憶體大戶。

  • 技術手段:針對 GLM 5.2,將權重從 8-bit 浮點數壓縮至 4-bit 整數 (INT4)。
  • 實測結果:模型 Checkpoint 從 705 GB 縮減至 421 GB(約減少 40%)。在 8 路 tensor-parallel 部署下,單顆 GPU 的記憶體佔用從 88 GB 降至 52 GB,剩餘空間可容納約 118 萬 tokens 的 KV cache。
  • 解碼與預填 (Prefill) 的差異化策略
    • 解碼階段 (Decode):受限於記憶體頻寬。INT4 權重減少了傳輸量,在低併發時,效能提升最高可達 55%。
    • 預填階段 (Prefill):屬於計算密集型 (compute-bound)。由於 INT4 需要展開回原格式,效能反而略低於 FP8。
    • 實務做法:Cloudflare 採用分離設計,在解碼階段使用 INT4 以求快速,在預填階段使用 FP8 以求高吞吐。

💡 建立 KV cache 完整性檢查:在高併發下保護資料正確性

當我們透過量化技術讓更多請求共享單一 GPU 記憶體時,隨之而來的是資料安全性問題。為了防止 paged attention 或 continuous batching 在高壓下發生錯誤,Cloudflare 建立了一層防禦機制。

  • 設計理念:為每個物理 cache page 分配一個標籤 (tag),並在讀取前檢查該 page 與請求預期是否匹配。若不匹配則直接中止請求,避免回傳錯誤資料。
  • 效能成本:透過將驗證改為獨立的 batch check(而非融合進 attention kernel),將對吞吐量與 p95 延遲的影響控制在 1% 以內。

🎯 實務啟示

對於需要部署長文本模型 (Long-context LLM) 的工程師來說,這項研究提供了兩個關鍵啟示:

  1. 解耦設計的重要性:針對預填 (Prefill) 與解碼 (Decode) 階段採用不同的量化策略,可以在不犧牲效能的情況下達到最佳化。
  2. 記憶體與精度的平衡:量化 KV cache 能極大化單一硬體的容量,且在經過測試後,對模型精準度(如 MMLU、GSM8K 等指標)的影響幾乎可以忽略不計。

🔗 來源

#AI #LLM #Cloudflare #MachineLearning #GPU #Inference #Quantization #Kimi #GLM #SGLang

原始資料 Hacker News · 收集於 2026-08-04
來源原標題
Smaller, faster, safer: running Kimi and GLM at scale
作者
ascorbic
原始標籤
hackernews
來源訊號
HN points 207 HN 留言 50
原始連結
https://blog.cloudflare.com/smaller-faster-safer-models/

摘要原文

Workers AI runs inference for some of the best open models in the world on GPUs in Cloudflare data centers close to your users. Two of the most capable, and most demanding, are Moonshot's Kimi K-series and Z.ai 's GLM. They are large, long-context, mixture-of-experts models, and they are wonderful to use. They are also very hard to serve efficiently because of memory constraints. We've written before about how we serve large models on Workers AI and about separating the prefill and decode phases of inference to get more out of each GPU. This post looks at three techniques we layer on top of that to fit these models into memory and keep them fast: quantizing the KV cache, compressing the model weights, and, because both of those pack more requests onto shared hardware, protecting the cache those requests share. These optimizations enable us to support more customers at lower costs, with no change in model accuracy. All our experiments and production traffic are running and benchmarked with SGLang , an open-source inference serving framework. We found that SGLang offers the best performance in the market, and we work closely with the SGLang team to upstream patches and new features to make our work available to the open-source community. Quantizing the KV cache As a model generates text, it stores the attention keys (K) and values (V) for every token it has already processed in a structure called the KV cache. The cache is what lets the model extend a long conversation without re-reading the entire context on every new token. For a long-context model, it grows quickly, and it is usually the KV cache, not the model's weights, that fills up GPU memory first. By default, the cache is stored in 16-bit precision (BF16). We store it in 8-bit floating point instead (FP8, e4m3), which halves its size. On Kimi K2.6, that raises the amount of context we can hold in memory from roughly 686,000 tokens to about 1.37 million, twice as much. It's worth being precise about where the benefit comes from, because it isn't raw speed. Quantizing the cache adds a small amount of work per token, since the FP8 attention kernel has to convert values as it reads them. What it changes is how many requests we can keep resident at once. The following measurements are for Kimi K2.6 decoding on a disaggregated H200 deployment, comparing the attention kernels directly: Concurrent requests BF16 KV cache (tok/s) FP8 KV cache (tok/s) 1 137 125 8 731 689 16 1,106 1,028 32 1,558 1,489 64 Out of memory 2,192 At any single concurrency level, BF16 is a few percent faster per token. But BF16 runs out of cache at 32 concurrent requests and can't admit a 33rd, while FP8 keeps going to 64 and reaches 2,192 tokens per second, about 41% higher than BF16's peak, for roughly 30% less cost per token. Because we run prefill and decode as separate pools, we can apply this where it helps most: prefill is compute-bound rather than memory-bound, so there we leave the cache in BF16 and keep its slightly higher throughput. None of this would matter if it changed the model's answers, so we checked. Across our evaluation suite, FP8 and BF16 caches are indistinguishable: Benchmark BF16 KV FP8 KV GSM8K 94.24 94.09 ARC-Easy 89.06 89.14 ARC-Challenge 66.72 67.49 MMLU 89.11 89.04 MMLU-Pro 80.29 79.29 mcxams (internal benchmark) 61 / 63 61 / 63 Tool-call validity 92.2% 92.6% Compressing the model weights The KV cache is one demand on GPU memory; the model's weights are the other. For GLM 5.2, we compress the weights from 8-bit floating point down to 4-bit integers (INT4) with no loss in accuracy. The checkpoint shrinks from 705 GB to 421 GB, about 40%, and per-GPU memory across an 8-way tensor-parallel deployment drops from roughly 88 GB to 52 GB, which leaves room for around 1.18 million tokens of KV cache on the same hardware. Across our evaluation suite, INT4 and FP8 weights are indistinguishable: Benchmark / Capability Metric FP8 INT4 GSM8K Exact match 94.39% 93.56% GSM8K Flexible 94.24% 93.48% ARC-Easy Accuracy 86.62% 86.15% ARC-Easy Acc (norm) 84.51% 85.19% ARC-Challenge Accuracy 64.93% 64.85% ARC-Challenge Acc (norm) 67.24% 66.64% MMLU Average 86.60% 86.54% MMLU-Pro Exact 80.80% 80.47% mcxams (internal benchmark) Passed 62 / 63 62 / 63 Smaller weights make the decode phase faster, and for a clear reason: generating each token means streaming the model's weights out of GPU memory, so decode speed is limited by memory bandwidth. Move less data and every token arrives sooner. The effect is largest at low concurrency, where per-request latency matters most: Concurrent requests GLM FP8 (tok/s) GLM INT4 (tok/s) INT4 gain 1 60 92 +55% 8 425 513 +21% 16 683 825 +21% 32 994 1,267 +27% 64 1,672 1,933 +16% Prefill behaves differently. It is compute-bound, and INT4 weights have to be expanded back out before the model can multiply with them, so that extra step makes prefill slower rather than faster, GLM sustains about 10,160 tokens per second of prefill in FP8 versus 8,660 in INT4. As with the KV cache, the disaggregated design turns this into a choice rather than a compromise: we run INT4 for decode, where it wins, and FP8 for prefill, where it wins. Model accuracy stays within 0.8 points of the FP8 model across every benchmark we run, making its quality indistinguishable. Protecting a shared KV cache Both techniques above have the same effect: they let many more requests share one GPU's memory at the same time. That efficiency is the whole point, but it also means hundreds of requests are reading and writing pages of the same physical KV cache. The mechanisms that make this fast, paged attention, continuous batching, cache reuse, all rely on getting the bookkeeping exactly right, and at our request volumes, even a one-in-a-billion mistake would show up regularly. So we built KV cache integrity checking as a layer of defense. The idea is straightforward: every physical cache page gets a tag that changes whenever the page is reallocated, and the server records which pages and tags each request expects to use. Before supported decode operations read from the cache, those mappings are checked. If anything doesn't match, the affected request is aborted rather than allowed to return data from the wrong page. The question that decides whether a safety check ships is what it costs. We measured it on a mid-sized production model in a two-prefill, two-decode configuration, with 8,192-token inputs and 1,000-token outputs: Concurrency Throughput change p95 latency change 1 −0.53% +0.42% 2 −0.38% +0.54% 4 −0.79% +0.63% 8 −0.43% +0.80% The cost is under 1% on both throughput and tail latency, and even the upper bound of the 95% confidence interval stays near 1%. We kept it computationally cheap by running the validation as a separate batch check rather than fusing it into the attention kernel, which would have introduced a race between GPU thread groups. It's enabled per deployment, and the default path uses a no-op tracker with no measurable overhead, so deployments that don't need it pay nothing. What's next Serving frontier models efficiently is a moving target, and this is the ongoing work behind it. We're expanding FP8 KV caches across more of the fleet, validating NVFP4 weights on Blackwell (NVIDIA’s GPU architecture), and working toward making integrity checks something we can leave on everywhere at negligible cost. These optimizations will allow us to continue to support more customers at a lower cost and at the same accuracy. If squeezing the best open models onto GPUs and serving them to millions of developers sounds like your kind of problem, come work with us . (207 points, 50 comments on Hacker News)

tencent/hy3:free 自動生成