-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
336 lines (299 loc) · 13.2 KB
/
index.html
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Parameter Calculator</title>
<style>
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
max-width: 800px;
margin: 2rem auto;
padding: 0 1rem;
background-color: #E8E4E1;
color: #4A4A4A;
line-height: 1.6;
}
h1, h2 {
color: #5C5552;
}
.input-grid {
display: flex;
flex-direction: column;
gap: 1rem;
margin-bottom: 2rem;
}
.input-group {
background: #F5F2F0;
padding: 1rem;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.08);
width: 100%;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #6B6461;
font-size: 0.9rem;
}
input[type="number"] {
width: 120px;
flex-shrink: 0;
padding: 0.5rem;
border: 1px solid #DDD;
border-radius: 6px;
background: white;
color: #4A4A4A;
font-size: 0.9rem;
transition: all 0.2s ease;
}
input[type="number"]:focus {
outline: none;
border-color: #9A8F8A;
box-shadow: 0 0 0 2px rgba(154, 143, 138, 0.2);
}
input[type="range"] {
-webkit-appearance: none;
height: 6px;
background: #DDD;
border-radius: 3px;
margin: 10px 0;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
background: #9A8F8A;
cursor: pointer;
border: 2px solid white;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
transition: all 0.2s ease-in-out;
}
input[type="range"]::-webkit-slider-thumb:hover {
background: #847571;
transform: scale(1.1);
}
input[type="range"]:focus {
outline: none;
}
.results {
background: #F5F2F0;
padding: 1.5rem;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.08);
}
.results p {
margin: 0.5rem 0;
display: flex;
justify-content: space-between;
padding: 0.5rem 0;
border-bottom: 1px solid #f0f0f0;
}
.results span {
font-weight: 500;
color: #847571;
}
.input-wrapper {
display: flex;
gap: 1rem;
align-items: center;
width: 100%;
padding: 0.5rem 0;
}
.input-wrapper input[type="number"] {
width: 120px;
flex-shrink: 0;
}
.input-wrapper input[type="range"] {
flex-grow: 1;
min-width: 200px;
}
.input-wrapper input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 16px;
height: 16px;
border-radius: 50%;
background: #9A8F8A;
cursor: pointer;
transition: all 0.2s ease-in-out;
}
.input-wrapper input[type="range"]::-webkit-slider-thumb:hover {
background: #847571;
transform: scale(1.1);
}
</style>
<script>
function calculateParameters() {
const d_model = parseInt(document.getElementById('d_model').value);
const num_heads = parseInt(document.getElementById('num_heads').value);
const v_head_dim = parseInt(document.getElementById('v_head_dim').value);
const kv_lora_rank = parseInt(document.getElementById('kv_lora_rank').value);
const q_lora_rank = 3 * kv_lora_rank;
const rope_head_dim = parseInt(document.getElementById('rope_head_dim').value);
const nope_head_dim = parseInt(document.getElementById('nope_head_dim').value);
const value_dim = num_heads * v_head_dim;
const nope_dim = num_heads * nope_head_dim;
const rope_dim = num_heads * rope_head_dim;
const compress_q_linear = d_model * q_lora_rank;
const decompress_q_nope = q_lora_rank * nope_dim;
const decompress_q_rope = q_lora_rank * rope_dim;
const compress_kv_linear = d_model * kv_lora_rank;
const decompress_k_nope = kv_lora_rank * nope_dim;
const decompress_v_linear = kv_lora_rank * value_dim;
const k_rope_linear = d_model * rope_head_dim;
const proj_final = value_dim * d_model;
const total_parameters_corrected = compress_q_linear +
decompress_q_nope +
decompress_q_rope +
compress_kv_linear +
decompress_k_nope +
decompress_v_linear +
k_rope_linear +
proj_final;
const total_parameters_corrected_in_millions = total_parameters_corrected / 1_000_000;
// Calculate MLA KV cache size
const mla_kv_cache_size = kv_lora_rank + rope_head_dim;
// Format the number with K, M, or B suffix
let formatted_parameters;
if (total_parameters_corrected >= 1_000_000_000) {
formatted_parameters = (total_parameters_corrected / 1_000_000_000).toFixed(4) + 'B';
} else if (total_parameters_corrected >= 1_000_000) {
formatted_parameters = (total_parameters_corrected / 1_000_000).toFixed(4) + 'M';
} else if (total_parameters_corrected >= 1_000) {
formatted_parameters = (total_parameters_corrected / 1_000).toFixed(4) + 'K';
} else {
formatted_parameters = Math.round(total_parameters_corrected).toString();
}
// Format KV cache size with K, M, B suffix
let formatted_cache_size;
if (mla_kv_cache_size >= 1_000_000_000) {
formatted_cache_size = (mla_kv_cache_size / 1_000_000_000).toFixed(4) + 'B';
} else if (mla_kv_cache_size >= 1_000_000) {
formatted_cache_size = (mla_kv_cache_size / 1_000_000).toFixed(4) + 'M';
} else if (mla_kv_cache_size >= 1_000) {
formatted_cache_size = (mla_kv_cache_size / 1_000).toFixed(4) + 'K';
} else {
formatted_cache_size = Math.round(mla_kv_cache_size).toString();
}
// Calculate MHA parameters and cache size
const mha_parameters = d_model * d_model * 4;
const mha_cache_size = d_model * 2;
// Format MHA parameters
let formatted_mha_parameters;
if (mha_parameters >= 1_000_000_000) {
formatted_mha_parameters = (mha_parameters / 1_000_000_000).toFixed(4) + 'B';
} else if (mha_parameters >= 1_000_000) {
formatted_mha_parameters = (mha_parameters / 1_000_000).toFixed(4) + 'M';
} else if (mha_parameters >= 1_000) {
formatted_mha_parameters = (mha_parameters / 1_000).toFixed(4) + 'K';
} else {
formatted_mha_parameters = Math.round(mha_parameters).toString();
}
// Format MHA cache size
let formatted_mha_cache_size;
if (mha_cache_size >= 1_000_000_000) {
formatted_mha_cache_size = (mha_cache_size / 1_000_000_000).toFixed(4) + 'B';
} else if (mha_cache_size >= 1_000_000) {
formatted_mha_cache_size = (mha_cache_size / 1_000_000).toFixed(4) + 'M';
} else if (mha_cache_size >= 1_000) {
formatted_mha_cache_size = (mha_cache_size / 1_000).toFixed(4) + 'K';
} else {
formatted_mha_cache_size = Math.round(mha_cache_size).toString();
}
// Calculate cache size reduction percentage
const cache_reduction = ((mha_cache_size - mla_kv_cache_size) / mha_cache_size * 100).toFixed(2);
document.getElementById('total_parameters_millions').textContent = formatted_parameters;
document.getElementById('mla_kv_cache_size').textContent = formatted_cache_size;
document.getElementById('mha_parameters').textContent = formatted_mha_parameters;
document.getElementById('mha_cache_size').textContent = formatted_mha_cache_size;
document.getElementById('cache_reduction').textContent = cache_reduction + '%';
document.getElementById('q_lora_rank_display').textContent = q_lora_rank;
// Update q_lora_rank slider
document.getElementById('q_lora_rank_slider').value = q_lora_rank;
document.getElementById('q_lora_rank_slider').nextElementSibling.value = q_lora_rank;
}
</script>
</head>
<body>
<h1>Multi Head Latent Attention (MLA) Calculator</h1>
<div class="results">
<h2>MLA</h2>
<p>Parameters <span id="total_parameters_millions">0.00</span></p>
<p>MLA KV Cache Size (per token) <span id="mla_kv_cache_size">0</span></p>
<p>Cache Size Reduction <span id="cache_reduction">0%</span></p>
<h2>MHA</h2>
<p>Parameters <span id="mha_parameters">0.00</span></p>
<p>MHA KV Cache Size (per token) <span id="mha_cache_size">0</span></p>
</div>
<h2>Parameters</h2>
<div class="input-grid">
<div class="input-group">
<label for="d_model">d_model</label>
<div class="input-wrapper">
<input type="range" min="256" max="8192" value="1024" step="128"
oninput="this.nextElementSibling.value=this.value; calculateParameters()">
<input type="number" id="d_model" value="1024" min="256" max="8192" step="128"
oninput="this.previousElementSibling.value=this.value; calculateParameters()">
</div>
</div>
<div class="input-group">
<label for="num_heads">num_heads</label>
<div class="input-wrapper">
<input type="range" min="1" max="256" value="70" step="1"
oninput="this.nextElementSibling.value=this.value; calculateParameters()">
<input type="number" id="num_heads" value="70" min="1" max="256"
oninput="this.previousElementSibling.value=this.value; calculateParameters()">
</div>
</div>
<div class="input-group">
<label for="v_head_dim">v_head_dim</label>
<div class="input-wrapper">
<input type="range" min="8" max="256" value="32" step="8"
oninput="this.nextElementSibling.value=this.value; calculateParameters()">
<input type="number" id="v_head_dim" value="32" min="8" max="256" step="8"
oninput="this.previousElementSibling.value=this.value; calculateParameters()">
</div>
</div>
<div class="input-group">
<label for="kv_lora_rank">kv_lora_rank</label>
<div class="input-wrapper">
<input type="range" min="16" max="512" value="64" step="16"
oninput="this.nextElementSibling.value=this.value; calculateParameters()">
<input type="number" id="kv_lora_rank" value="64" min="16" max="512" step="16"
oninput="this.previousElementSibling.value=this.value; calculateParameters()">
</div>
</div>
<div class="input-group">
<label for="q_lora_rank_slider">q_lora_rank (3× kv_lora_rank)</label>
<div class="input-wrapper">
<input type="range" id="q_lora_rank_slider" disabled
min="48" max="1536" value="192" step="48">
<input type="number" value="192" disabled>
</div>
</div>
<div class="input-group">
<label for="rope_head_dim">rope_head_dim</label>
<div class="input-wrapper">
<input type="range" min="16" max="512" value="64" step="16"
oninput="this.nextElementSibling.value=this.value; calculateParameters()">
<input type="number" id="rope_head_dim" value="64" min="16" max="512" step="16"
oninput="this.previousElementSibling.value=this.value; calculateParameters()">
</div>
</div>
<div class="input-group">
<label for="nope_head_dim">nope_head_dim</label>
<div class="input-wrapper">
<input type="range" min="8" max="256" value="32" step="8"
oninput="this.nextElementSibling.value=this.value; calculateParameters()">
<input type="number" id="nope_head_dim" value="32" min="8" max="256" step="8"
oninput="this.previousElementSibling.value=this.value; calculateParameters()">
</div>
</div>
</div>
<script>
// Initial calculation on page load
calculateParameters();
</script>
</body>
</html>