Repository navigation
Expand file tree
/
Copy pathmethod_utils.py
More file actions
299 lines (257 loc) · 9.95 KB
/
Copy pathmethod_utils.py
File metadata and controls
299 lines (257 loc) · 9.95 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
#!/usr/bin/env python3
"""Chipmunk-inspired sparse SwiGLU reference math for Ouro.
This module measures algorithm quality only. The current dense MLP still runs
before a forward hook can replace its output, so none of its timings represent
a sparse-kernel speedup.
"""
from __future__ import annotations
import math
import torch
import torch.nn.functional as F
EVALUATION_METHODS = (
"dense",
"sparse_attention",
"sparse_attention_sparse_mlp",
"cached_mass",
"cached_mass_sparse_mlp",
"cached_mass_token_sparse_prefill",
"flashloop_quantized_kv",
"flashloop_kivi2_channel_delta",
"flashloop_kivi4_channel_delta",
"flashloop_full_kivi4_per_loop",
"cross_loop_kivi_channel_delta",
"cross_loop_kivi_token_delta",
"cross_loop_kivi4_channel_delta",
"cross_loop_kivi4_token_delta",
)
def method_uses_sparse_attention(method: str) -> bool:
if method not in EVALUATION_METHODS:
raise ValueError(method)
return method not in (
"dense",
"cross_loop_kivi_channel_delta",
"cross_loop_kivi_token_delta",
"cross_loop_kivi4_channel_delta",
"cross_loop_kivi4_token_delta",
)
def method_uses_cached_global_mass(method: str) -> bool:
if method not in EVALUATION_METHODS:
raise ValueError(method)
return method in (
"cached_mass",
"cached_mass_sparse_mlp",
"cached_mass_token_sparse_prefill",
"flashloop_quantized_kv",
"flashloop_kivi2_channel_delta",
"flashloop_kivi4_channel_delta",
"flashloop_full_kivi4_per_loop",
)
def method_uses_cached_mass(method: str) -> bool:
"""Backward-compatible alias for cached global attention mass."""
return method_uses_cached_global_mass(method)
def method_uses_sparse_mlp(method: str) -> bool:
if method not in EVALUATION_METHODS:
raise ValueError(method)
return method in (
"sparse_attention_sparse_mlp",
"cached_mass_sparse_mlp",
)
def method_uses_token_sparse_prefill(method: str) -> bool:
if method not in EVALUATION_METHODS:
raise ValueError(method)
return method in (
"cached_mass_token_sparse_prefill",
"flashloop_quantized_kv",
"flashloop_kivi2_channel_delta",
"flashloop_kivi4_channel_delta",
"flashloop_full_kivi4_per_loop",
)
def method_uses_quantized_cross_loop_kv(method: str) -> bool:
if method not in EVALUATION_METHODS:
raise ValueError(method)
return method in (
"flashloop_quantized_kv",
"flashloop_kivi2_channel_delta",
"flashloop_kivi4_channel_delta",
"cross_loop_kivi_channel_delta",
"cross_loop_kivi_token_delta",
"cross_loop_kivi4_channel_delta",
"cross_loop_kivi4_token_delta",
)
def method_uses_absolute_loop_kv(method: str) -> bool:
if method not in EVALUATION_METHODS:
raise ValueError(method)
return method == "flashloop_full_kivi4_per_loop"
def attention_replacement_method(method: str) -> str | None:
if method not in EVALUATION_METHODS:
raise ValueError(method)
if method in (
"dense",
"cross_loop_kivi_channel_delta",
"cross_loop_kivi_token_delta",
"cross_loop_kivi4_channel_delta",
"cross_loop_kivi4_token_delta",
):
return None
if method in ("sparse_attention", "sparse_attention_sparse_mlp"):
return "official"
return "cached_source_mass"
def select_topk_column_mask(
scores: torch.Tensor,
fraction: float,
) -> torch.Tensor:
"""Select ceil(fraction * columns) independently for every token."""
if not 0 < fraction <= 1:
raise ValueError("fraction must be in (0, 1]")
if scores.ndim < 1 or scores.shape[-1] == 0:
raise ValueError("scores must have a non-empty column dimension")
keep = min(scores.shape[-1], math.ceil(scores.shape[-1] * fraction))
indices = torch.topk(scores, keep, dim=-1, sorted=False).indices
mask = torch.zeros_like(scores, dtype=torch.bool)
return mask.scatter_(-1, indices, True)
def cached_sparse_swiglu_output(
cached_output: torch.Tensor,
cached_intermediate: torch.Tensor,
current_intermediate: torch.Tensor,
mask: torch.Tensor,
down_weight: torch.Tensor,
) -> torch.Tensor:
"""Correct a cached MLP output with selected SwiGLU-column deltas."""
selected_delta = (current_intermediate - cached_intermediate) * mask
return cached_output + F.linear(selected_delta, down_weight)
def chipmunk_swiglu_scores(
cached_gate: torch.Tensor,
current_gate: torch.Tensor,
cached_up: torch.Tensor,
current_up: torch.Tensor,
activation,
) -> torch.Tensor:
"""First-order magnitude proxy for a gated intermediate-column change."""
gate_term = (current_gate - cached_gate).abs() * cached_up.abs()
up_term = (current_up - cached_up).abs() * activation(cached_gate).abs()
return gate_term + up_term
class SparseMLPIntervention:
"""Apply rolling-cache sparse SwiGLU corrections on decode Loops 3/4."""
def __init__(
self,
model: torch.nn.Module,
fraction: float = 0.30,
record_metrics: bool = False,
) -> None:
if not 0 < fraction <= 1:
raise ValueError("fraction must be in (0, 1]")
self.fraction = fraction
self.record_metrics = record_metrics
self.current_loops: dict[int, int] = {}
self.states: dict[int, dict[str, torch.Tensor]] = {}
self.rows: list[dict[str, float | int]] = []
self.prefill_mlp_calls = 0
self.source_captures = 0
self.target_replacements = 0
self.selected_columns = 0
self.handles = []
for layer_index, layer in enumerate(model.model.layers):
self.handles.append(
layer.self_attn.register_forward_pre_hook(
self._attention_pre_hook(layer_index),
with_kwargs=True,
)
)
self.handles.append(
layer.mlp.register_forward_hook(self._mlp_hook(layer_index))
)
def _attention_pre_hook(self, layer_index: int):
def hook(_module, _inputs, kwargs):
if "current_ut" not in kwargs:
raise RuntimeError("Ouro attention call is missing current_ut")
self.current_loops[layer_index] = int(kwargs["current_ut"])
return None
return hook
def _mlp_hook(self, layer_index: int):
def hook(module, inputs, output):
current_loop = self.current_loops.get(layer_index)
if current_loop is None:
return None
x = inputs[0]
if x.shape[-2] != 1:
self.prefill_mlp_calls += 1
return None
if current_loop == 0:
return None
with torch.no_grad():
current_gate = module.gate_proj(x).detach()
current_up = module.up_proj(x).detach()
current_intermediate = (
module.act_fn(current_gate) * current_up
).detach()
if current_loop == 1:
self.states[layer_index] = {
"gate": current_gate,
"up": current_up,
"intermediate": current_intermediate,
"output": output.detach(),
}
self.source_captures += 1
return None
if current_loop not in (2, 3):
return None
if layer_index not in self.states:
raise RuntimeError(
f"missing Loop-2 MLP cache for layer {layer_index}"
)
state = self.states[layer_index]
scores = chipmunk_swiglu_scores(
state["gate"],
current_gate,
state["up"],
current_up,
module.act_fn,
)
mask = select_topk_column_mask(scores, self.fraction)
approximation = cached_sparse_swiglu_output(
state["output"],
state["intermediate"],
current_intermediate,
mask,
module.down_proj.weight,
)
if self.record_metrics:
error = output.float() - approximation.float()
self.rows.append(
{
"layer": layer_index + 1,
"from_loop": current_loop,
"to_loop": current_loop + 1,
"fraction": self.fraction,
"output_relative_l2_error": float(
(
torch.linalg.vector_norm(error)
/ torch.linalg.vector_norm(output.float()).clamp_min(
1e-12
)
).item()
),
}
)
state["gate"] = torch.where(mask, current_gate, state["gate"])
state["up"] = torch.where(mask, current_up, state["up"])
state["intermediate"] = torch.where(
mask,
current_intermediate,
state["intermediate"],
)
state["output"] = approximation.detach()
self.target_replacements += 1
self.selected_columns += (
mask.shape[0]
* mask.shape[1]
* math.ceil(mask.shape[-1] * self.fraction)
)
return approximation
return hook
def close(self) -> None:
for handle in self.handles:
handle.remove()
self.handles.clear()
self.current_loops.clear()
self.states.clear()