-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdeepseek_v4_routing.py
More file actions
198 lines (177 loc) · 7.75 KB
/
Copy pathdeepseek_v4_routing.py
File metadata and controls
198 lines (177 loc) · 7.75 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
from dataclasses import dataclass, field
from typing import Any
import torch
_ROUTE_WEIGHT_SAMPLE_ROWS = 256
def validate_deepseek_v4_routes(
hidden_states: torch.Tensor,
top_k_index: torch.Tensor,
top_k_weights: torch.Tensor,
) -> None:
if hidden_states.ndim != 2 or hidden_states.shape[-1] != 4096:
raise ValueError(
f"DeepSeek V4 routed hidden states must be [tokens,4096], got {tuple(hidden_states.shape)}."
)
expected = (hidden_states.shape[0], 6)
if tuple(top_k_index.shape) != expected or tuple(top_k_weights.shape) != expected:
raise ValueError(
f"DeepSeek V4 routes must both have shape {expected}, got "
f"{tuple(top_k_index.shape)} and {tuple(top_k_weights.shape)}."
)
if top_k_index.dtype != torch.long:
raise TypeError(
f"DeepSeek V4 expert indices must be int64, got {top_k_index.dtype}."
)
if bool(torch.any((top_k_index < 0) | (top_k_index >= 256)).item()):
raise ValueError("DeepSeek V4 expert indices must be in [0,256).")
if not bool(torch.isfinite(top_k_weights).all().item()):
raise ValueError("DeepSeek V4 expert weights must be finite.")
if bool(torch.any(top_k_weights < 0).item()):
raise ValueError("DeepSeek V4 expert weights must be nonnegative.")
def _sample_sorted_route_weights(top_k_weights: torch.Tensor) -> torch.Tensor:
sorted_weights = top_k_weights.detach().float().sort(dim=-1).values
stride = max(sorted_weights.shape[0] // _ROUTE_WEIGHT_SAMPLE_ROWS, 1)
return sorted_weights[::stride][:_ROUTE_WEIGHT_SAMPLE_ROWS].cpu()
def summarize_deepseek_v4_routes(
top_k_index: torch.Tensor,
top_k_weights: torch.Tensor,
*,
layer: int | None = None,
router_kind: str | None = None,
) -> dict[str, Any]:
if top_k_index.ndim != 2 or top_k_index.shape[-1] != 6:
raise ValueError(
f"DeepSeek V4 route indices must be [tokens,6], got {tuple(top_k_index.shape)}."
)
if top_k_weights.shape != top_k_index.shape:
raise ValueError(
"DeepSeek V4 route weights must match route indices, got "
f"{tuple(top_k_weights.shape)} and {tuple(top_k_index.shape)}."
)
counts = torch.bincount(top_k_index.detach().reshape(-1), minlength=256).cpu()
active = counts[counts > 0]
sorted_counts = active.sort().values
median = (
float(sorted_counts.float().median().item()) if sorted_counts.numel() else 0.0
)
weights = top_k_weights.detach().float()
row_sums = weights.sum(dim=-1)
weight_sample = _sample_sorted_route_weights(weights)
return {
"layer": layer,
"router_kind": router_kind,
"tokens": top_k_index.shape[0],
"routes": top_k_index.numel(),
"active_experts": active.numel(),
"rows_min": int(active.min().item()) if active.numel() else 0,
"rows_max": int(active.max().item()) if active.numel() else 0,
"rows_mean": float(active.float().mean().item()) if active.numel() else 0.0,
"rows_median": median,
"groups_at_least_16": int(torch.count_nonzero(active >= 16).item()),
"groups_at_least_32": int(torch.count_nonzero(active >= 32).item()),
"groups_at_least_64": int(torch.count_nonzero(active >= 64).item()),
"groups_at_least_128": int(torch.count_nonzero(active >= 128).item()),
"rows_per_expert": counts.tolist(),
"weight_min": float(weights.min()),
"weight_max": float(weights.max()),
"weight_rms": float(weights.square().mean().sqrt()),
"weight_sum_mean": float(row_sums.mean()),
"weight_sum_max_error": float((row_sums - row_sums.mean()).abs().max()),
"sorted_weight_sample": weight_sample.tolist(),
}
def compare_deepseek_v4_route_weights(
reference: list[dict[str, Any]],
candidate: list[dict[str, Any]],
) -> dict[str, float | int]:
"""Compare selected score weights without comparing expert identities."""
if len(reference) != len(candidate):
raise ValueError(
f"route summary count mismatch: {len(reference)} != {len(candidate)}"
)
reference_samples = []
candidate_samples = []
for reference_entry, candidate_entry in zip(reference, candidate, strict=True):
reference_key = (
reference_entry.get("layer"),
reference_entry.get("router_kind"),
reference_entry.get("tokens"),
)
candidate_key = (
candidate_entry.get("layer"),
candidate_entry.get("router_kind"),
candidate_entry.get("tokens"),
)
if reference_key != candidate_key:
raise ValueError(
f"route summary identity mismatch: {reference_key} != {candidate_key}"
)
reference_sample = torch.tensor(
reference_entry["sorted_weight_sample"], dtype=torch.float32
)
candidate_sample = torch.tensor(
candidate_entry["sorted_weight_sample"], dtype=torch.float32
)
if reference_sample.shape != candidate_sample.shape:
raise ValueError(
"route weight sample shape mismatch: "
f"{tuple(reference_sample.shape)} != {tuple(candidate_sample.shape)}"
)
reference_samples.append(reference_sample.reshape(-1))
candidate_samples.append(candidate_sample.reshape(-1))
reference_weights = torch.cat(reference_samples)
candidate_weights = torch.cat(candidate_samples)
delta = candidate_weights - reference_weights
reference_rms = reference_weights.square().mean().sqrt()
candidate_rms = candidate_weights.square().mean().sqrt()
denominator = reference_weights.norm() * candidate_weights.norm()
cosine = (
torch.dot(reference_weights, candidate_weights) / denominator
if float(denominator) != 0.0
else torch.ones(())
)
return {
"summaries": len(reference),
"sampled_weights": reference_weights.numel(),
"cosine": float(cosine),
"rmse": float(delta.square().mean().sqrt()),
"relative_rmse": float(delta.square().mean().sqrt() / (reference_rms + 1e-20)),
"max_abs": float(delta.abs().max()),
"reference_rms": float(reference_rms),
"candidate_rms": float(candidate_rms),
}
@dataclass
class DeepseekV4RouteCollector:
"""Forward-hook collector used only during validation and profiling."""
records: list[tuple[int | None, str, torch.Tensor, torch.Tensor]] = field(
default_factory=list
)
_handles: list[Any] = field(default_factory=list)
def install(self, model: torch.nn.Module):
self.remove()
for name, module in model.named_modules():
if not (name.endswith(".mlp.gate") and hasattr(module, "top_k")):
continue
layer_text = name.split(".layers.", 1)[-1].split(".", 1)[0]
layer = int(layer_text) if layer_text.isdigit() else None
router_kind = "hash" if hasattr(module, "tid2eid") else "learned"
def hook(_module, _inputs, output, *, layer=layer, router_kind=router_kind):
self.records.append(
(layer, router_kind, output[2].detach(), output[1].detach())
)
self._handles.append(module.register_forward_hook(hook))
return self
def summaries(self) -> list[dict[str, Any]]:
return [
summarize_deepseek_v4_routes(
indices,
weights,
layer=layer,
router_kind=router_kind,
)
for layer, router_kind, indices, weights in self.records
]
def clear(self) -> None:
self.records.clear()
def remove(self) -> None:
for handle in self._handles:
handle.remove()
self._handles.clear()