-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_golden_moe.py
More file actions
437 lines (350 loc) · 16.5 KB
/
Copy pathbench_golden_moe.py
File metadata and controls
437 lines (350 loc) · 16.5 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python3
"""bench_golden_moe.py — Golden MoE general ML benchmark
Compares MLP baseline, Top-1, Top-2, and Golden MoE (v2) on synthetic
classification tasks (MNIST-like, CIFAR10-like).
Key metric: expert_usage_ratio near 1/e (0.3679) indicates golden zone.
Usage:
python bench_golden_moe.py # MNIST default
python bench_golden_moe.py --dataset cifar10 # CIFAR10
python bench_golden_moe.py --experts 4,8 # multiple expert counts
python bench_golden_moe.py --all # all datasets
"""
import argparse
import math
import time
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
INV_E = 1.0 / math.e # 0.3679 — golden zone target
# ═══════════════════════════════════════════════════════════════════
# Result dataclass
# ═══════════════════════════════════════════════════════════════════
@dataclass
class MoEBenchResult:
method: str
dataset: str
n_experts: int
accuracy: float
expert_usage_ratio: float
convergence_step: int
balance_loss: float
wall_time: float
params: int
extra: Dict[str, Any] = field(default_factory=dict)
def summary(self) -> str:
golden_dist = abs(self.expert_usage_ratio - INV_E)
return (
f"{self.method:<14s} | experts={self.n_experts:2d} | "
f"acc={self.accuracy:.4f} | usage={self.expert_usage_ratio:.4f} | "
f"|usage-1/e|={golden_dist:.4f} | conv_step={self.convergence_step:3d} | "
f"bal_loss={self.balance_loss:.6f} | time={self.wall_time:.2f}s | "
f"params={self.params:,}"
)
# ═══════════════════════════════════════════════════════════════════
# Expert and TopKMoE
# ═══════════════════════════════════════════════════════════════════
class Expert(nn.Module):
"""2-layer MLP expert: Linear -> GELU -> Linear."""
def __init__(self, input_dim: int, hidden_dim: int, output_dim: int):
super().__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, output_dim)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.fc2(F.gelu(self.fc1(x)))
class TopKMoE(nn.Module):
"""Standard Top-K Mixture of Experts.
Args:
input_dim: Input feature dimension.
hidden_dim: Expert hidden dimension.
output_dim: Output dimension.
n_experts: Number of experts.
k: Number of experts selected per token.
"""
def __init__(self, input_dim: int, hidden_dim: int, output_dim: int,
n_experts: int = 4, k: int = 2):
super().__init__()
self.n_experts = n_experts
self.k = k
self.gate = nn.Linear(input_dim, n_experts)
self.experts = nn.ModuleList([
Expert(input_dim, hidden_dim, output_dim)
for _ in range(n_experts)
])
self.usage_counts = torch.zeros(n_experts)
def forward(self, x: torch.Tensor) -> tuple:
"""Returns (output, aux_loss)."""
B = x.shape[0]
gate_scores = self.gate(x) # (B, n_experts)
topk_vals, topk_idx = torch.topk(gate_scores, self.k, dim=-1) # (B, k)
topk_weights = F.softmax(topk_vals, dim=-1) # (B, k)
# Compute all expert outputs
expert_outputs = torch.stack(
[expert(x) for expert in self.experts], dim=1
) # (B, n_experts, output_dim)
# Gather selected experts
idx_expanded = topk_idx.unsqueeze(-1).expand(-1, -1, expert_outputs.shape[-1])
selected = torch.gather(expert_outputs, 1, idx_expanded) # (B, k, output_dim)
# Weighted combination
output = (topk_weights.unsqueeze(-1) * selected).sum(dim=1) # (B, output_dim)
# Track usage
with torch.no_grad():
usage = torch.zeros(self.n_experts, device=x.device)
for i in range(self.k):
usage.scatter_add_(0, topk_idx[:, i], torch.ones(B, device=x.device))
self.usage_counts = self.usage_counts.to(x.device) + usage
# Balance loss: MSE(expert_usage, uniform)
usage_frac = usage / (B * self.k)
uniform = torch.ones_like(usage_frac) / self.n_experts
aux_loss = F.mse_loss(usage_frac, uniform)
return output, aux_loss
# ═══════════════════════════════════════════════════════════════════
# MoE Classifier
# ═══════════════════════════════════════════════════════════════════
class MoEClassifier(nn.Module):
"""Classifier wrapping a MoE layer.
proj: Linear(input_dim, hidden_dim)
moe_layer: hidden_dim -> n_classes
head: Identity (moe output is already n_classes dim)
"""
def __init__(self, input_dim: int, hidden_dim: int, n_classes: int,
moe_layer: nn.Module):
super().__init__()
self.proj = nn.Linear(input_dim, hidden_dim)
self.moe_layer = moe_layer
self.head = nn.Identity()
def forward(self, x: torch.Tensor) -> tuple:
h = F.gelu(self.proj(x))
out, aux_loss = self.moe_layer(h)
logits = self.head(out)
return logits, aux_loss
# ═══════════════════════════════════════════════════════════════════
# Synthetic data
# ═══════════════════════════════════════════════════════════════════
def get_synthetic_data(dataset: str, n_samples: int = 1000):
"""Generate synthetic classification data.
Args:
dataset: "mnist" (784d, 10 classes) or "cifar10" (3072d, 10 classes).
n_samples: Number of samples.
Returns:
(X, y, input_dim, n_classes)
"""
if dataset == "mnist":
input_dim = 784
n_classes = 10
elif dataset == "cifar10":
input_dim = 3072
n_classes = 10
else:
raise ValueError(f"Unknown dataset: {dataset}")
X = torch.randn(n_samples, input_dim)
y = torch.randint(0, n_classes, (n_samples,))
return X, y, input_dim, n_classes
# ═══════════════════════════════════════════════════════════════════
# Training and evaluation
# ═══════════════════════════════════════════════════════════════════
def train_and_evaluate(model: nn.Module, X: torch.Tensor, y: torch.Tensor,
epochs: int = 20, batch_size: int = 64,
lr: float = 1e-3) -> dict:
"""Train and evaluate a MoEClassifier.
Returns dict with: accuracy, expert_usage_ratio, convergence_step,
balance_loss, wall_time, acc_history, usage_history.
"""
# 80/20 split
n = len(X)
n_train = int(0.8 * n)
perm = torch.randperm(n)
X_train, y_train = X[perm[:n_train]], y[perm[:n_train]]
X_test, y_test = X[perm[n_train:]], y[perm[n_train:]]
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
model.train()
acc_history = []
usage_history = []
t0 = time.time()
for epoch in range(epochs):
# Train
for i in range(0, n_train, batch_size):
xb = X_train[i:i + batch_size]
yb = y_train[i:i + batch_size]
logits, aux_loss = model(xb)
loss = F.cross_entropy(logits, yb) + 0.01 * aux_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Evaluate
model.eval()
with torch.no_grad():
logits, aux = model(X_test)
preds = logits.argmax(dim=-1)
acc = (preds == y_test).float().mean().item()
acc_history.append(acc)
# Get usage ratio (max single expert fraction)
moe = model.moe_layer
if hasattr(moe, 'usage_counts') and moe.usage_counts.sum() > 0:
usage_ratio = (moe.usage_counts / moe.usage_counts.sum()).max().item()
elif hasattr(moe, 'psi_tracker'):
usage_ratio = moe.psi_tracker.usage_ratio.max().item()
else:
usage_ratio = 1.0
usage_history.append(usage_ratio)
model.train()
wall_time = time.time() - t0
# Final metrics
final_acc = acc_history[-1] if acc_history else 0.0
# Convergence step: first epoch reaching 90% of final accuracy
threshold = 0.9 * final_acc
convergence_step = epochs
for i, a in enumerate(acc_history):
if a >= threshold:
convergence_step = i + 1
break
# Final balance loss
model.eval()
with torch.no_grad():
_, final_aux = model(X_test)
balance_loss = final_aux.item()
# Final usage ratio
final_usage = usage_history[-1] if usage_history else 1.0
return {
"accuracy": final_acc,
"expert_usage_ratio": final_usage,
"convergence_step": convergence_step,
"balance_loss": balance_loss,
"wall_time": wall_time,
"acc_history": acc_history,
"usage_history": usage_history,
}
# ═══════════════════════════════════════════════════════════════════
# Comparison runner
# ═══════════════════════════════════════════════════════════════════
def run_comparison(dataset: str = "mnist", expert_counts: List[int] = None,
epochs: int = 20, n_samples: int = 1000) -> List[MoEBenchResult]:
"""Run comparison across MLP baseline, Top-1, Top-2, and Golden MoE.
Args:
dataset: "mnist" or "cifar10".
expert_counts: List of expert counts to test.
epochs: Training epochs.
n_samples: Number of synthetic samples.
Returns:
List of MoEBenchResult.
"""
from golden_moe_v2 import GoldenMoEv2
if expert_counts is None:
expert_counts = [4]
X, y, input_dim, n_classes = get_synthetic_data(dataset, n_samples)
hidden_dim = 128
results = []
for n_experts in expert_counts:
configs = [
("MLP", None),
("Top-1", lambda: TopKMoE(hidden_dim, hidden_dim * 2, n_classes,
n_experts=n_experts, k=1)),
("Top-2", lambda: TopKMoE(hidden_dim, hidden_dim * 2, n_classes,
n_experts=n_experts, k=min(2, n_experts))),
("Golden", lambda: GoldenMoEv2(hidden_dim, hidden_dim * 2, n_classes,
n_experts=n_experts)),
]
for method, moe_factory in configs:
if method == "MLP":
# MLP baseline: use TopKMoE with 1 expert, k=1
moe = TopKMoE(hidden_dim, hidden_dim * 2, n_classes,
n_experts=1, k=1)
else:
moe = moe_factory()
model = MoEClassifier(input_dim, hidden_dim, n_classes, moe)
params = sum(p.numel() for p in model.parameters())
res = train_and_evaluate(model, X, y, epochs=epochs)
results.append(MoEBenchResult(
method=method,
dataset=dataset,
n_experts=n_experts if method != "MLP" else 1,
accuracy=res["accuracy"],
expert_usage_ratio=res["expert_usage_ratio"],
convergence_step=res["convergence_step"],
balance_loss=res["balance_loss"],
wall_time=res["wall_time"],
params=params,
extra={
"acc_history": res["acc_history"],
"usage_history": res["usage_history"],
},
))
return results
# ═══════════════════════════════════════════════════════════════════
# Pretty printing
# ═══════════════════════════════════════════════════════════════════
def print_results(results: List[MoEBenchResult]):
"""Print results as ASCII table + 1/e check + accuracy bar graph."""
if not results:
print("No results.")
return
# Header
print()
print("=" * 100)
print("Golden MoE Benchmark Results")
print("=" * 100)
print()
# Table
header = (f"{'Method':<14s} | {'Dataset':<8s} | {'#Exp':>4s} | "
f"{'Acc':>7s} | {'Usage':>7s} | {'|u-1/e|':>7s} | "
f"{'Conv':>4s} | {'BalLoss':>9s} | {'Time':>6s} | {'Params':>10s}")
print(header)
print("-" * len(header))
for r in results:
golden_dist = abs(r.expert_usage_ratio - INV_E)
marker = " *" if golden_dist < 0.05 else ""
print(
f"{r.method:<14s} | {r.dataset:<8s} | {r.n_experts:4d} | "
f"{r.accuracy:7.4f} | {r.expert_usage_ratio:7.4f} | {golden_dist:7.4f}{marker} | "
f"{r.convergence_step:4d} | {r.balance_loss:9.6f} | "
f"{r.wall_time:5.2f}s | {r.params:>10,}"
)
# 1/e check
print()
print(f"1/e = {INV_E:.4f} (golden zone target)")
golden_results = [r for r in results if abs(r.expert_usage_ratio - INV_E) < 0.05]
if golden_results:
print(f" {len(golden_results)} method(s) in golden zone (* marked above)")
else:
print(" No methods in golden zone (|usage - 1/e| < 0.05)")
# Accuracy bar graph
print()
print("Accuracy comparison:")
max_acc = max(r.accuracy for r in results) if results else 1.0
bar_width = 40
for r in results:
bar_len = int(bar_width * r.accuracy / max(max_acc, 1e-8))
bar = "#" * bar_len
print(f" {r.method:<14s} [{bar:<{bar_width}s}] {r.accuracy:.4f}")
print()
# ═══════════════════════════════════════════════════════════════════
# CLI
# ═══════════════════════════════════════════════════════════════════
def main():
parser = argparse.ArgumentParser(description="Golden MoE ML Benchmark")
parser.add_argument("--dataset", type=str, default="mnist",
choices=["mnist", "cifar10"],
help="Dataset (default: mnist)")
parser.add_argument("--experts", type=str, default="4",
help="Comma-separated expert counts (default: 4)")
parser.add_argument("--epochs", type=int, default=20,
help="Training epochs (default: 20)")
parser.add_argument("--samples", type=int, default=1000,
help="Number of samples (default: 1000)")
parser.add_argument("--all", action="store_true",
help="Run all datasets")
args = parser.parse_args()
expert_counts = [int(x) for x in args.experts.split(",")]
if args.all:
datasets = ["mnist", "cifar10"]
else:
datasets = [args.dataset]
all_results = []
for ds in datasets:
print(f"\nRunning benchmark on {ds}...")
results = run_comparison(ds, expert_counts, args.epochs, args.samples)
all_results.extend(results)
print_results(all_results)
if __name__ == "__main__":
main()