-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_moe_vectorized.py
More file actions
124 lines (90 loc) · 4 KB
/
Copy pathtest_moe_vectorized.py
File metadata and controls
124 lines (90 loc) · 4 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
"""Verify vectorized MoE forward pass correctness and performance."""
import time
import torch
from server.hyperion_policy_network import SoftMoEPolicyNetwork
def test_shapes():
"""Test all output shapes are correct."""
policy = SoftMoEPolicyNetwork(num_experts=12, top_k=2, router_noise=0.0)
policy.eval()
for batch_size in [1, 8, 32, 64, 128]:
batch = torch.randn(batch_size, 384)
with torch.no_grad():
out = policy(batch, use_system2=True, training=False)
assert out["logits"].shape == (batch_size, 16), f"FAIL logits: {out['logits'].shape}"
assert out["log_probs"].shape == (batch_size, 16)
assert out["policy_dist"].shape == (batch_size, 16)
assert out["value"].shape == (batch_size,)
assert out["entropy"].shape == (batch_size,)
assert out["confidence"].shape[0] == batch_size
assert out["model_confidence"].shape[0] == batch_size
assert out["process_reward"].shape[0] == batch_size
assert out["gates"].shape == (batch_size, 2)
assert out["expert_indices"].shape == (batch_size, 2)
assert out["meta_weights"].shape == (batch_size, 2)
print(f" batch_size={batch_size:3d}: ALL SHAPES OK")
def test_gradients():
"""Test gradient flow through vectorized MoE."""
policy = SoftMoEPolicyNetwork(num_experts=4, top_k=2, router_noise=0.0)
policy.train()
batch = torch.randn(16, 384, requires_grad=True)
out = policy(batch, use_system2=True, training=True)
# All outputs should require grad
assert out["balance_loss"].requires_grad
loss = out["logits"].sum() + out["balance_loss"]
loss.backward()
assert batch.grad is not None, "Gradient should flow to input"
assert batch.grad.shape == batch.shape
print(" GRADIENTS: OK")
def test_system2_toggle():
"""Test System 1/2 toggle works correctly."""
policy = SoftMoEPolicyNetwork(num_experts=4, top_k=2, router_noise=0.0)
policy.eval()
batch = torch.randn(16, 384)
with torch.no_grad():
out_s2_on = policy(batch, use_system2=True, training=False)
out_s2_off = policy(batch, use_system2=False, training=False)
# Outputs should differ
diff = (out_s2_on["logits"] - out_s2_off["logits"]).abs().mean().item()
assert diff > 0, "System 1 and System 2 outputs should differ"
print(f" System 1/2 diff: {diff:.6f}: OK")
def test_expert_coverage():
"""Test that all experts get used with diverse inputs."""
policy = SoftMoEPolicyNetwork(num_experts=12, top_k=2, router_noise=0.0)
policy.eval()
# Large diverse batch should hit all experts
batch = torch.randn(256, 384)
with torch.no_grad():
out = policy(batch, use_system2=False, training=False)
used_experts = set(out["expert_indices"].view(-1).tolist())
print(f" Experts used: {len(used_experts)}/12: {'OK' if len(used_experts) > 6 else 'WARN (expected more)'}")
def benchmark():
"""Benchmark forward pass at different batch sizes."""
policy = SoftMoEPolicyNetwork(num_experts=12, top_k=2, router_noise=0.0)
policy.eval()
print("\nBenchmark (CPU, avg of 5 runs):")
for batch_size in [16, 64, 128, 256]:
batch = torch.randn(batch_size, 384)
# Warmup
with torch.no_grad():
policy(batch, use_system2=True, training=False)
# Benchmark
times = []
for _ in range(5):
start = time.time()
with torch.no_grad():
policy(batch, use_system2=True, training=False)
times.append(time.time() - start)
avg_ms = (sum(times) / len(times)) * 1000
print(f" batch_size={batch_size:4d}: {avg_ms:7.1f}ms")
if __name__ == "__main__":
print("=== Vectorized MoE Tests ===\n")
print("1. Shape tests:")
test_shapes()
print("\n2. Gradient test:")
test_gradients()
print("\n3. System 1/2 toggle test:")
test_system2_toggle()
print("\n4. Expert coverage test:")
test_expert_coverage()
benchmark()
print("\n=== All tests passed ===")