AI Skills AI技能 2h ago Updated 1h ago 更新于 1小时前 49

How Does One GPU Serve Hundreds of Users at the Same Time? 一块 GPU 如何同时服务数百名用户?

Model weights are loaded once into GPU memory and shared across all concurrent requests; only per-request state (tokens, KV cache) is duplicated, enabling hundreds of users to share a single GPU LLM inference has two distinct phases with opposite hardware characteristics: prefill is compute-bound (processing many tokens in parallel), while decode is memory-bandwidth-bound (reading all weights to produce one token) Continuous batching rebuilds the batch at every token step, allowing finished requ 模型权重在GPU内存中仅加载一次并供所有请求共享,每个请求仅需维护独立的KV缓存等状态,这是单卡服务数百并发用户的基础。 LLM推理分为预填充(计算密集型)和解码(内存带宽密集型)两个阶段,解码阶段需读取全部权重以生成单个token,是性能瓶颈所在。 连续批处理(Continuous Batching)在令牌级别动态调度,请求完成即释放槽位,新请求可立即填入,大幅减少静态批处理中的资源浪费。 并发能力通常受限于KV缓存内存而非计算能力,上下文长度越长,单卡可支持的并发请求数显著下降。 延迟需同时用TTFT(首token延迟)和TPOT(每token生成延迟)衡量,批处理大小提升吞吐量但会恶化两

65
Hot 热度
75
Quality 质量
70
Impact 影响力

Analysis 深度分析

TL;DR

  • Model weights are loaded once into GPU memory and shared across all concurrent requests; only per-request state (tokens, KV cache) is duplicated, enabling hundreds of users to share a single GPU
  • LLM inference has two distinct phases with opposite hardware characteristics: prefill is compute-bound (processing many tokens in parallel), while decode is memory-bandwidth-bound (reading all weights to produce one token)
  • Continuous batching rebuilds the batch at every token step, allowing finished requests to exit and new ones to enter immediately, eliminating the idle slot waste of static batching
  • KV cache memory, not compute capacity, is the primary bottleneck limiting concurrency; maximum concurrent users scale inversely with context length
  • Chunked prefill mitigates the prefill/decode scheduling conflict by splitting large prompts into smaller chunks interleaved with decode steps

Why It Matters

Understanding LLM inference mechanics is essential for anyone deploying or optimizing generative AI systems, as the architecture directly determines throughput, latency, and cost efficiency. The counterintuitive finding that memory bandwidth—not compute—bottlenecks decode, and that KV cache—not GPU compute—limits concurrency, challenges common assumptions and guides better resource allocation decisions.

Technical Details

  • Weight sharing architecture: Model weights are read-only during inference and loaded once into VRAM; each request only requires its own KV cache (attention state), tokens, and position bookkeeping, making the marginal cost of an additional user extremely small
  • Prefill vs. Decode asymmetry: Prefill processes the entire input prompt (e.g., 5,000 tokens) simultaneously as a dense matrix operation, saturating GPU compute units; decode generates one token at a time, requiring a full weight read (~140 GB for a 70B model at 16-bit) per token, making it memory-bandwidth-bound at roughly 21 tokens/second on a single request
  • Continuous batching: The scheduler rebuilds the batch before every forward pass, admitting new requests and releasing finished ones at the token level rather than waiting for batch boundaries, which dramatically reduces wasted GPU cycles from variable output lengths
  • Chunked prefill: Long prompts are split into chunks and interleaved with decode steps, smoothing inter-token latency for streaming users while keeping compute units busier than pure decode scheduling would allow
  • KV cache as the concurrency limiter: VRAM budget = model weights + KV cache + runtime buffers; since weights are fixed, the KV cache pool size—and thus maximum concurrent requests—is determined by context length, with 32K-token contexts reducing concurrency by an order of magnitude compared to 1K-token contexts

Industry Insight

  • When sizing inference infrastructure, prioritize KV cache memory management and context-length-aware capacity planning over raw compute benchmarks; a GPU's effective concurrency is defined by your average context length, not its FLOPS rating
  • Continuous batching should be considered a baseline requirement, not an optimization, for any production LLM serving system—static batching introduces unacceptable latency variance and GPU idle time given the high variance in LLM output lengths
  • The prefill/decode scheduling conflict suggests that high-throughput deployments should evaluate chunked prefill or even separate GPU pools for each phase to balance TTFT and TPOT according to their specific latency SLAs

TL;DR

  • 模型权重在GPU内存中仅加载一次并供所有请求共享,每个请求仅需维护独立的KV缓存等状态,这是单卡服务数百并发用户的基础。
  • LLM推理分为预填充(计算密集型)和解码(内存带宽密集型)两个阶段,解码阶段需读取全部权重以生成单个token,是性能瓶颈所在。
  • 连续批处理(Continuous Batching)在令牌级别动态调度,请求完成即释放槽位,新请求可立即填入,大幅减少静态批处理中的资源浪费。
  • 并发能力通常受限于KV缓存内存而非计算能力,上下文长度越长,单卡可支持的并发请求数显著下降。
  • 延迟需同时用TTFT(首token延迟)和TPOT(每token生成延迟)衡量,批处理大小提升吞吐量但会恶化两者。

为什么值得看

本文系统揭示了LLM推理服务器的核心工作机制与性能瓶颈,为AI从业者优化推理部署、设计调度策略提供了关键理论基础。理解权重共享、批处理机制及内存限制,有助于在实际工程中平衡吞吐量、延迟与成本,对构建高效、可扩展的LLM服务具有直接指导价值。

技术解析

  • 权重共享与状态隔离:模型权重在推理过程中只读,仅加载一次并共享给所有并发请求;每个请求独立维护输入token、位置信息及KV缓存,额外请求的边际成本仅为KV缓存空间。
  • 预填充与解码的硬件行为差异:预填充阶段并行处理整个提示词,计算密集,可饱和GPU算力;解码阶段每次仅生成一个token,需读取全部权重,受内存带宽限制,单请求理论上限约21 token/秒(以70B模型、3000 GB/s带宽估算)。
  • 连续批处理调度机制:调度器在每个token生成步动态重组批次,请求完成立即释放槽位,新请求可无缝接入;通过分块预填充(Chunked Prefill)将长提示词拆分为多个小批次,与解码步骤交错执行,以平滑现有用户的延迟抖动。
  • KV缓存内存决定并发上限:GPU显存分配为模型权重、KV缓存和运行时缓冲三部分;KV缓存池大小随上下文长度线性增长,因此并发数与上下文长度成反比,例如相同GPU在1000 token上下文下可服务200并发,但在32000 token上下文下并发数骤降。
  • 延迟双指标评估:TTFT(Time To First Token)反映新请求等待首个token的时间,受预填充调度优先级影响;TPOT(Time Per Output Token)反映生成阶段每token延迟,受批处理大小和内存带宽限制;两者常存在权衡关系。

行业启示

  • 调度策略是推理性能的核心:连续批处理已成为行业标准,但需根据负载特征(如请求长度分布、上下文长度)动态调整批处理大小与预填充分块策略,以在吞吐量和延迟间取得平衡。
  • 内存管理直接决定服务规模:KV缓存的分配、回收与预占机制是影响并发能力的关键,部署时应优先优化显存利用率,并考虑上下文长度对实际并发数的制约,避免过度承诺服务容量。
  • 硬件选型与 workload 匹配:预填充阶段适合高算力GPU,解码阶段受内存带宽限制,可考虑将预填充与解码任务分离至不同GPU池,以针对性优化两类阶段的硬件利用率。

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

LLM 大模型 Inference 推理 GPU GPU Deployment 部署