在部署大型語言模型(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
對大型語言模型內存消耗的詳細計算和并發情況的分析,我們可以更全面地了解模型部署中的內存需求,為實際應用提供有力的支持和指導。