|
|
import numpy as np |
|
|
import matplotlib.pyplot as plt |
|
|
|
|
|
def kv_cache_size_bytes(bs, seq): |
|
|
"""计算 Qwen2ForCausalLM 模型的 KV cache 大小(字节)""" |
|
|
n_layers = 28 |
|
|
n_kv_heads = 2 |
|
|
hidden_size = 1536 |
|
|
num_attention_heads = 12 |
|
|
head_dim = hidden_size // num_attention_heads |
|
|
bytes_per_elem = 2 |
|
|
return bs * seq * n_kv_heads * head_dim * 2 * n_layers * bytes_per_elem |
|
|
|
|
|
|
|
|
|
|
|
avg_ns_prefill_640 = np.array([ |
|
|
1418534991, 1033939782, 494241378, 252646178, 135127951, |
|
|
69450854, 42089250, 35031755, 28423503, 22718985, 15558545 |
|
|
], dtype=np.float64) |
|
|
bs_prefill_640 = np.array([1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]) |
|
|
seq_prefill_640 = 640 |
|
|
throughput_prefill_640 = bs_prefill_640 * seq_prefill_640 / (avg_ns_prefill_640 * 1e-9) |
|
|
norm_prefill_640 = throughput_prefill_640 / throughput_prefill_640.max() |
|
|
kv_prefill_640 = kv_cache_size_bytes(bs_prefill_640, seq_prefill_640) / 1024**3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
avg_ns_prefill_1152 = np.array([ |
|
|
1417657581, 1040325509, 389808438, 200051514, |
|
|
110723882, 61663721, 39934013, 30382401, 21185162, 15818426 |
|
|
], dtype=np.float64) |
|
|
bs_prefill_1152 = np.array([ 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]) |
|
|
seq_prefill_1152 = 1152 |
|
|
throughput_prefill_1152 = bs_prefill_1152 * seq_prefill_1152 / (avg_ns_prefill_1152 * 1e-9) |
|
|
norm_prefill_1152 = throughput_prefill_1152 / throughput_prefill_1152.max() |
|
|
kv_prefill_1152 = kv_cache_size_bytes(bs_prefill_1152, seq_prefill_1152) / 1024**3 |
|
|
|
|
|
|
|
|
|
|
|
avg_ns_decoding_512 = np.array([ |
|
|
25285906551, 12679311252, 7440608085, 4841914697, 4272889441, |
|
|
3997075015, 3825710172, 3726603655, 3648294896, 3635960724, 3319210677 |
|
|
], dtype=np.float64) |
|
|
bs_decoding_512 = np.array([1024, 512, 256, 128, 64, 32, 16, 8, 2, 4, 1]) |
|
|
seq_decoding_512 = 512 |
|
|
throughput_decoding_512 = bs_decoding_512 * seq_decoding_512 / (avg_ns_decoding_512 * 1e-9) |
|
|
norm_decoding_512 = throughput_decoding_512 / throughput_decoding_512.max() |
|
|
kv_decoding_512 = kv_cache_size_bytes(bs_decoding_512, seq_decoding_512) / 1024**3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
avg_ns_decoding_1024 = np.array([ |
|
|
30598009745, 16552100314, 11165129518, 8835508288, |
|
|
8037503827, 8020861613, 7502439278, 7275415153, 7204870191, 6423331403 |
|
|
], dtype=np.float64) |
|
|
bs_decoding_1024 = np.array([ 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]) |
|
|
seq_decoding_1024 = 1024 |
|
|
throughput_decoding_1024 = bs_decoding_1024 * seq_decoding_1024 / (avg_ns_decoding_1024 * 1e-9) |
|
|
norm_decoding_1024 = throughput_decoding_1024 / throughput_decoding_1024.max() |
|
|
kv_decoding_1024 = kv_cache_size_bytes(bs_decoding_1024, seq_decoding_1024) / 1024**3 |
|
|
|
|
|
|
|
|
fig, axes = plt.subplots(1, 2, figsize=(14, 5)) |
|
|
|
|
|
|
|
|
axes[0].plot(kv_prefill_640, norm_prefill_640, marker='o', label="seq=640") |
|
|
axes[0].plot(kv_prefill_1152, norm_prefill_1152, marker='s', label="seq=1152") |
|
|
axes[0].set_xscale('log') |
|
|
axes[0].set_xlabel("KV Cache Size (GB, log scale)") |
|
|
axes[0].set_ylabel("Normalized GPU Utilization") |
|
|
axes[0].set_title("Prefill") |
|
|
axes[0].grid(True, which="both", ls="--", alpha=0.5) |
|
|
axes[0].legend() |
|
|
|
|
|
|
|
|
axes[1].plot(kv_decoding_512, norm_decoding_512, marker='o', label="seq=512") |
|
|
axes[1].plot(kv_decoding_1024, norm_decoding_1024, marker='s', label="seq=1024") |
|
|
axes[1].set_xscale('log') |
|
|
axes[1].set_xlabel("KV Cache Size (GB, log scale)") |
|
|
axes[1].set_ylabel("Normalized GPU Utilization") |
|
|
axes[1].set_title("Decoding") |
|
|
axes[1].grid(True, which="both", ls="--", alpha=0.5) |
|
|
axes[1].legend() |
|
|
|
|
|
plt.suptitle("Normalized GPU Utilization vs KV Cache Size") |
|
|
plt.savefig("kv_cache_vs_util_gb.png", dpi=300, bbox_inches='tight') |
|
|
plt.savefig("kv_cache_vs_util_gb.pdf", dpi=300, bbox_inches='tight') |
|
|
plt.show() |
|
|
|