在部署大型语言模型(LLM)时,显存(VRAM)的合理规划是决定模型能否高效运行的核心问题。本文将通过详细的公式推导和示例计算,系统解析模型权重、键值缓存(KV Cache)、激活内存及额外开销的计算逻辑,并探讨并发场景下显存需求的变化规律。
一、模型权重内存消耗计算
公式推导
模型权重的内存占用由参数数量、量化因子和数据类型共同决定:
Model Memory = Number of Parameters × Original Parameter Size Quantization Factor \text{Model Memory} = \text{Number of Parameters} \times \frac{\text{Original Parameter Size}}{\text{Quantization Factor}} Model Memory=Number of Parameters×Quantization FactorOriginal Parameter Size
其中:
- Number of Parameters:模型总参数量(如72亿参数)。
- Original Parameter Size:原始参数的字节大小(如FP32为4字节)。
- Quantization Factor:量化因子为原始参数字节大小与目标格式字节的比值(如FP32情况下,8位量化为4,16位量化为2)。
示例计算
假设模型参数为72亿,使用8位量化(Quantization Factor=4):
Model Memory = 72 , 000 , 000 , 000 × 4 4 = 72 亿字节 = 72 GB \text{Model Memory} = \frac{72,000,000,000 \times 4}{4} = 72 \text{亿字节} = 72 \text{GB} Model Memory=472,000,000,000×4=72亿字节=72GB
注意:若使用FP16(16位量化),则:
Model Memory = 72 , 000 , 000 , 000 × 4 2 = 144 GB \text{Model Memory} = \frac{72,000,000,000 \times 4}{2} = 144 \text{GB} Model Memory=272,000,000,000×4=144GB
二、键值缓存(KV Cache)内存消耗计算
公式推导
KV Cache是注意力机制中存储查询、键、值的缓存,其内存占用公式为:
KV Cache Memory = 2 × Layer Count × Head Dim × Heads × Context Len × Batch × Data Size \text{KV Cache Memory} = 2 \times \text{Layer Count} \times \text{Head Dim} \times \text{Heads} \times \text{Context Len} \times \text{Batch} \times \text{Data Size} KV Cache Memory=2×Layer Count×Head Dim×Heads×Context Len×Batch×Data Size
其中:
- Layer Count:模型层数(如48)。
- Head Dimension:每个注意力头的维度(如128)。
- Number of Heads:头的数量(如32)。
- Context Length:上下文长度(如12000)。
- Batch Size:单个请求的批处理样本数(如1)。
- Data Type Size:数据类型大小(FP16为2字节,FP32为4字节)。
示例计算
Batch Size=1,FP16 :
KV Cache Memory = 2 × 48 × 128 × 32 × 12 , 000 × 1 × 2 = 9 , 437 , 184 , 000 字节 ≈ 8.79 GB \text{KV Cache Memory} = 2 \times 48 \times 128 \times 32 \times 12,000 \times 1 \times 2 = 9,437,184,000\text{字节} \approx 8.79 \text{GB} KV Cache Memory=2×48×128×32×12,000×1×2=9,437,184,000字节≈8.79GB
三、激活函数输出与中间结果内存消耗
公式推导
假设所有中间结果同时驻留显存,激活内存由隐藏层维度、序列长度、层数及批次大小决定:
Activation Memory = Hidden Dimension × Sequence Length × Batch Size × Layer Count × Data Type Size \text{Activation Memory} = \text{Hidden Dimension} \times \text{Sequence Length} \times \text{Batch Size} \times \text{Layer Count} \times \text{Data Type Size} Activation Memory=Hidden Dimension×Sequence Length×Batch Size×Layer Count×Data Type Size
其中:
- Hidden Dimension:隐藏层维度(如4096)。
- Sequence Length:输入序列长度(通常与上下文长度一致)。
- Layer Count:模型层数(如48层)。
示例计算(FP16)
Activation Memory = 4096 × 12 , 000 × 1 × 48 × 2 = 4 , 718 , 592 , 000 字节 ≈ 4.72 GB \text{Activation Memory} = 4096 \times 12,000 \times 1 \times 48 \times 2 = 4,718,592,000 \text{字节} \approx 4.72 \text{GB} Activation Memory=4096×12,000×1×48×2=4,718,592,000字节≈4.72GB
四、额外开销
额外开销估算
额外开销包括临时缓冲区、框架开销等,通常按模型权重的10%-20%估算。
Overhead Memory = α × Model Memory ( α = 0.1 ∼ 0.2 ) \text{Overhead Memory} = \alpha \times \text{Model Memory} \quad (\alpha=0.1 \sim 0.2) Overhead Memory=α×Model Memory(α=0.1∼0.2)
示例 (α=0.15,模型权重72GB):
Overhead Memory = 0.15 × 72 GB = 10.8 GB \text{Overhead Memory} = 0.15 \times 72 \text{GB} = 10.8 \text{GB} Overhead Memory=0.15×72GB=10.8GB
五、总内存计算
Total Memory = Model Memory + KV Cache Memory + Activation Memory + Overhead Memory \text{Total Memory} = \text{Model Memory} + \text{KV Cache Memory} + \text{Activation Memory} + \text{Overhead Memory} Total Memory=Model Memory+KV Cache Memory+Activation Memory+Overhead Memory
六、并发场景下的显存变化分析
在并发场景中同时处理多个请求,以下参数会显著变化,导致总内存需求增加:
1. 模型权重(Fixed)
- 显存占用 :模型参数(权重)通常仅需加载一次 ,因此显存占用与并发数无关。
若模型权重占用72GB显存,无论并发数为1还是32,该部分始终为72GB。
2. 激活内存(Activation Memory)
- 显存占用 :每个请求的激活内存(前向传播中的中间结果)需独立存储,因此与并发数线性相关。
- 公式 :
总激活显存 = 并发数 × 单请求激活显存 \text{总激活显存} = \text{并发数} \times \text{单请求激活显存} 总激活显存=并发数×单请求激活显存
3. KV Cache(键值缓存)
- 显存占用 :在Transformer的自注意力机制模型中,需要上下文缓存,假设KV Cache不共享的情况下,显存占用与并发数和序列长度成正比。
- 公式 :
总 K V C a c h e 显存 = 并发数 × 单请求 K V C a c h e 总KV Cache显存=并发数×单请求KV Cache 总KVCache显存=并发数×单请求KVCache
对大型语言模型内存消耗的详细计算和并发情况的分析,我们可以更全面地了解模型部署中的内存需求,为实际应用提供有力的支持和指导。