-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_memory_estimates_swa.py
More file actions
231 lines (196 loc) · 6.82 KB
/
Copy pathplot_memory_estimates_swa.py
File metadata and controls
231 lines (196 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt).
# Source for "Build a Large Language Model From Scratch"
# - https://www.manning.com/books/build-a-large-language-model-from-scratch
# Code: https://github.com/rasbt/LLMs-from-scratch
#
# Sliding Window Attention (SWA) memory usage vs context length plot.
#
# This script mirrors the style and structure of plot_memory_estimates_mla.py.
import argparse
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
# Bytes per element
DTYPE_BYTES = {
"fp32": 4,
"bf16": 2,
"fp16": 2,
"fp8": 1,
"int8": 1,
}
def bytes_to_gb(n_bytes):
return n_bytes / (1000.0 ** 3)
def parse_ratio(ratio_str):
# "--swa_ratio a:b" means a SWA layers for every b full layers within a block
try:
a_str, b_str = ratio_str.split(":")
a, b = int(a_str), int(b_str)
assert a >= 0 and b >= 0 and (a + b) > 0
return a, b
except Exception:
raise ValueError("--swa_ratio must be in the form 'a:b' with nonnegative integers and a+b>0")
def kv_bytes_total_mha(batch, context_length, emb_dim, n_layers, bytes_per_elem):
# For MHA, n_kv_heads = n_heads, which cancels out:
# total = B * L * E * 2 (K,V) * bytes * n_layers
return batch * context_length * emb_dim * 2 * bytes_per_elem * n_layers
def kv_bytes_total_gqa(
batch, context_length, emb_dim, n_layers, bytes_per_elem, n_kv_groups
):
# For GQA, n_kv_heads = n_heads / n_kv_groups
# => scale the MHA total by 1 / n_kv_groups
base = kv_bytes_total_mha(batch, context_length, emb_dim, n_layers, bytes_per_elem)
return base / n_kv_groups
def kv_bytes_total_mha_swa(
batch, context_length, emb_dim, n_layers, bytes_per_elem, window, swa_ratio
):
# Split layers into SWA vs Full
a, b = parse_ratio(swa_ratio)
total_blocks = a + b
n_swa_layers = int(round(n_layers * (a / total_blocks)))
n_full_layers = n_layers - n_swa_layers
total_full = kv_bytes_total_mha(
batch, context_length, emb_dim, n_full_layers, bytes_per_elem
)
total_swa = kv_bytes_total_mha(
batch, window, emb_dim, n_swa_layers, bytes_per_elem
)
return total_full + total_swa
def kv_bytes_total_gqa_swa(
batch,
context_length,
emb_dim,
n_layers,
bytes_per_elem,
n_kv_groups,
window,
swa_ratio,
):
a, b = parse_ratio(swa_ratio)
total_blocks = a + b
n_swa_layers = int(round(n_layers * (a / total_blocks)))
n_full_layers = n_layers - n_swa_layers
total_full = kv_bytes_total_gqa(
batch,
context_length,
emb_dim,
n_full_layers,
bytes_per_elem,
n_kv_groups,
)
total_swa = kv_bytes_total_gqa(
batch, window, emb_dim, n_swa_layers, bytes_per_elem, n_kv_groups
)
return total_full + total_swa
def main():
p = argparse.ArgumentParser(
description="KV-cache vs Context Length — MHA vs GQA with SWA overlays"
)
p.add_argument("--emb_dim", type=int, required=True)
p.add_argument("--n_heads", type=int, required=True)
p.add_argument("--n_layers", type=int, required=True)
p.add_argument("--batch_size", type=int, default=1)
p.add_argument("--dtype", choices=DTYPE_BYTES.keys(), default="bf16")
p.add_argument(
"--sliding_window_size", type=int, required=True, help="SWA window size W"
)
p.add_argument("--swa_ratio", type=str, default="5:1", help="SWA:Full ratio, e.g., 5:1")
p.add_argument(
"--output", type=Path, default=Path("kv_bytes_vs_context_length.pdf")
)
args = p.parse_args()
batch_size = args.batch_size
emb_dim = args.emb_dim
n_heads = args.n_heads
n_layers = args.n_layers
bytes_per_elem = DTYPE_BYTES[args.dtype]
kv_groups = 4
valid_g4 = (n_heads % kv_groups == 0)
context_lengths = [
256, 512, 1024, 2048, 4096, 8192,
16384, 32768, 65536, 131072
]
series = {
"MHA (KV total)": [],
f"SWA on MHA (ratio {args.swa_ratio}, W={args.sliding_window_size})": [],
}
if valid_g4:
series["GQA kv_groups=4 (full)"] = []
series[
f"SWA on GQA kv_groups=4 (ratio {args.swa_ratio}, W={args.sliding_window_size})"
] = []
for L in context_lengths:
total_mha = kv_bytes_total_mha(
batch_size, L, emb_dim, n_layers, bytes_per_elem
)
total_mha_swa = kv_bytes_total_mha_swa(
batch_size,
L,
emb_dim,
n_layers,
bytes_per_elem,
window=args.sliding_window_size,
swa_ratio=args.swa_ratio,
)
series["MHA (KV total)"].append(bytes_to_gb(total_mha))
series[
f"SWA on MHA (ratio {args.swa_ratio}, W={args.sliding_window_size})"
].append(bytes_to_gb(total_mha_swa))
if valid_g4:
total_gqa = kv_bytes_total_gqa(
batch_size, L, emb_dim, n_layers, bytes_per_elem, n_kv_groups=kv_groups
)
total_gqa_swa = kv_bytes_total_gqa_swa(
batch_size,
L,
emb_dim,
n_layers,
bytes_per_elem,
n_kv_groups=kv_groups,
window=args.sliding_window_size,
swa_ratio=args.swa_ratio,
)
series["GQA kv_groups=4 (full)"].append(bytes_to_gb(total_gqa))
series[
f"SWA on GQA kv_groups=4 (ratio {args.swa_ratio}, W={args.sliding_window_size})"
].append(bytes_to_gb(total_gqa_swa))
plt.figure(figsize=(10, 5))
x = np.array(context_lengths, dtype=float)
colors = {
"MHA": "#1f77b4",
"GQA": "#ff7f0e",
}
for label, yvals in series.items():
y = np.array(yvals, dtype=float)
if np.all(np.isnan(y)):
continue
linestyle = "--" if "SWA" in label else "-"
if "MHA" in label:
color = colors["MHA"]
elif "GQA" in label:
color = colors["GQA"]
else:
color = None
plt.plot(x, y, marker="o", label=label, linestyle=linestyle, color=color)
plt.xscale("log")
plt.xlabel("context_length (log scale)")
plt.ylabel("Total KV cache (GB)")
plt.title(
"KV-cache vs Context Length — MHA vs GQA (SWA overlays)\n"
f"(n_heads={n_heads}, emb_dim={emb_dim}, n_layers={n_layers}, "
f"batch={batch_size}, dtype={args.dtype}; "
f"SWA ratio={args.swa_ratio}, W={args.sliding_window_size})",
fontsize=8,
)
plt.grid(True, which="both")
plt.legend()
plt.tight_layout()
plt.savefig(args.output)
plt.close()
if not valid_g4:
print(
f"Skipped GQA kv_groups=4 because n_heads={args.n_heads} "
"is not divisible by 4."
)
print(f"Saved plot to: {args.output}")
if __name__ == "__main__":
main()