LLM推理显存分配四要素与计算原理
Loading an LLM does not guarantee inference!
从第一性原理解析LLM推理显存构成,给出KV Cache计算公式与四部分拆解,工程落地必备知识,建议收藏对照压测。
Loading an LLM does not guarantee inference!
加载大语言模型并不能保证推理能够运行!
The model may load successfully and still run out of GPU memory when the first requests arrive.
模型可能成功加载,但当第一批请求到达时仍会耗尽 GPU 显存。
This happens because model weights are only one of four VRAM allocations:
发生这种情况是因为模型权重只是四种 VRAM 分配中的一项:
1) Model weights
1) 模型权重
The parameters are loaded into VRAM before inference begins.
参数会在推理开始前加载到 VRAM 中。
Ignoring quantization metadata and implementation overhead, a 70B parameter model requires approximately:
忽略量化元数据和实现开销,一个拥有 70B 参数的模型大约需要:
→ 140 GB in FP16 or BF16 → 70 GB in INT8 → 35 GB in INT4
→ FP16 或 BF16 格式下约 140 GB → INT8 格式下约 70 GB → INT4 格式下约 35 GB
This allocation remains mostly constant during inference.
在推理过程中,这部分分配量基本保持不变。
Quantization reduces it by storing each parameter with fewer bits. However, kernels may use higher-precision values for some operations.
量化通过用更少的位数存储每个参数来减少其占用。然而,某些操作的内核(kernels)可能会使用更高精度的值。
2) KV cache
2) KV 缓存
Each Transformer layer generates KV tensors for every processed token.
每个 Transformer 层都会为每个已处理的 token 生成 KV 张量。
The inference engine retains these tensors so new tokens can attend to earlier tokens without recomputing their key and value projections.
推理引擎保留这些张量,以便新 token 可以关注之前的 token,而无需重新计算它们的键(key)和值(value)投影。
The approximate KV cache memory per token is:
每个 token 的 KV 缓存内存近似计算公式为:
2 × layers × KV heads × head dimension × bytes per value
2 × 层数 × KV 头数 × 头维度 × 每个值的字节数
The factor of two represents the key and value tensors.
系数 2 代表键(key)和张量与值(value)张量。
This allocation increases with context length and the number of active requests.
这部分分配量会随着上下文长度和活跃请求数量的增加而增加。
Grouped-query attention and multi-query attention reduce it by sharing fewer key-value heads across multiple query heads.
分组查询注意力(Grouped-query attention)和多查询注意力(multi-query attention)通过在多个查询头之间共享较少的键值头来减少其占用。
3) Activations and workspace
3) 激活值和临时工作区
A forward pass creates intermediate tensors for attention, MLP operations, normalization, and matrix multiplication.
前向传播会为注意力机制、MLP 操作、归一化和矩阵乘法创建中间张量。
GPU kernels may also require temporary workspace for computation.
GPU 内核在进行计算时也可能需要临时的计算工作区。
The inference engine reuses much of this memory across layers and generation steps.
推理引擎会在不同的层和生成步骤之间重用大部分此类内存。
Its peak size depends on the model architecture, number of tokens processed together, selected kernels, and serving configuration.
其峰值大小取决于模型架构、同时处理的 token 数量、选定的内核以及服务配置。
Prefill processes multiple prompt tokens in parallel.
预填充(Prefill)阶段并行处理多个提示词 token。
Decode usually processes one new token per active request.
解码(Decode)阶段通常每个活跃请求每次只处理一个新 token。
The two phases therefore have different activation and workspace requirements.
因此,这两个阶段具有不同的激活和工作空间需求。
4) Runtime overhead
4) 运行时开销
CUDA contexts, loaded kernels, memory allocators, metadata, communication buffers, and serving-engine state also consume VRAM.
CUDA 上下文、加载的内核、内存分配器、元数据、通信缓冲区以及推理引擎状态也会消耗 VRAM。
The amount varies across runtimes and deployment configurations.
该数值因运行环境和部署配置而异。
Memory fragmentation can also leave enough total free VRAM but no sufficiently large contiguous region for a new allocation.
内存碎片化可能导致总空闲 VRAM 充足,但缺乏足够大的连续区域以进行新分配。
The total memory requirement is:
总内存需求为:
model weights + KV cache + peak activations and workspace + runtime overhead
模型权重 + KV 缓存 + 峰值激活与工作空间 + 运行时开销
- Model weights remain mostly constant. - KV cache memory increases with context length and concurrency. - Activation and workspace usage changes during execution. - Runtime overhead depends on the software stack.
- 模型权重基本保持不变。 - KV 缓存内存随上下文长度和并发量增加。 - 激活和工作空间的使用情况在执行过程中发生变化。 - 运行时开销取决于软件栈。
This is why model size alone cannot determine whether an LLM can serve a given workload.
这就是为什么仅凭模型大小无法确定 LLM 是否能服务特定工作负载的原因。
The visual below shows when each allocation is created, how long it remains in memory, and which workload parameters increase it.
下图展示了各项分配的创建时机、在内存中的驻留时长,以及哪些工作负载参数会增加其占用。
If you want to dive deeper, I wrote a first-principles guide that explains the complete inference process, including prefill, decode, KV caching, batching, and quantization.
如果你想深入了解,我撰写了一篇基于第一性原理的指南,详细解释了完整的推理过程,包括预填充(prefill)、解码(decode)、KV 缓存、批处理(batching)和量化(quantization)。
Read it below.
请在下方阅读。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力