-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_deepseek_v4_sliding_attention.py
More file actions
437 lines (394 loc) · 13.7 KB
/
Copy pathtest_deepseek_v4_sliding_attention.py
File metadata and controls
437 lines (394 loc) · 13.7 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
import math
from types import SimpleNamespace
from typing import Any, cast
import pytest
import torch
from torch.utils.checkpoint import checkpoint
from transformers.masking_utils import sliding_window_causal_mask_function
from transformers.models.deepseek_v4.configuration_deepseek_v4 import (
DeepseekV4Config,
)
from transformers.models.deepseek_v4.modeling_deepseek_v4 import (
DeepseekV4Attention,
DeepseekV4Model,
)
from deepseek_v4_attention import (
_CONFIG_MARKER,
_canonical_training_mask,
_deepseek_v4_attention_forward,
_validate_model_inputs,
configure_deepseek_v4_attention,
require_complete_deepseek_v4_attention,
)
from deepseek_v4_sliding_attention import (
deepseek_v4_sliding_attention,
deepseek_v4_sliding_attention_bshd,
)
_SEQUENCE_LENGTH = 2048
_QUERY_HEADS = 64
_HEAD_DIM = 512
_WINDOW = 128
def _reference_sliding_attention_bshd(
query: torch.Tensor,
shared_kv: torch.Tensor,
sink: torch.Tensor,
) -> torch.Tensor:
query_f32 = query.float().transpose(1, 2)
kv_f32 = shared_kv.float().transpose(1, 2)
outputs = []
scale = 1.0 / math.sqrt(query.shape[-1])
for query_start in range(0, _SEQUENCE_LENGTH, _WINDOW):
query_end = query_start + _WINDOW
key_start = max(0, query_start - (_WINDOW - 1))
key_end = query_end
query_block = query_f32[:, :, query_start:query_end]
key_block = kv_f32[:, :, key_start:key_end]
scores = torch.matmul(query_block, key_block.transpose(-1, -2)) * scale
query_positions = torch.arange(query_start, query_end, device=query.device)[
:, None
]
key_positions = torch.arange(key_start, key_end, device=query.device)[None, :]
visible = (key_positions <= query_positions) & (
key_positions > query_positions - _WINDOW
)
scores = scores.masked_fill(~visible[None, None], float("-inf"))
sink_logits = sink[None, :, None, None].expand(query.shape[0], -1, _WINDOW, -1)
probabilities = torch.softmax(torch.cat((scores, sink_logits), dim=-1), dim=-1)[
..., :-1
]
outputs.append(torch.matmul(probabilities, key_block))
return torch.cat(outputs, dim=2).transpose(1, 2)
def _relative_metrics(
candidate: torch.Tensor, reference: torch.Tensor
) -> tuple[float, float, float]:
candidate_f32 = candidate.detach().float().flatten()
reference_f32 = reference.detach().float().flatten()
difference = candidate_f32 - reference_f32
relative_rmse = difference.square().mean().sqrt() / (
reference_f32.square().mean().sqrt() + 1e-12
)
cosine = torch.nn.functional.cosine_similarity(candidate_f32, reference_f32, dim=0)
return (
float(relative_rmse),
float(cosine),
float(difference.abs().max()),
)
def test_sliding_attention_exact_shape_matches_blockwise_reference() -> None:
torch.manual_seed(1701)
batch = 1
query_storage = torch.randn(
batch,
_QUERY_HEADS,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
kv_storage = torch.randn(
batch,
1,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
sink_storage = torch.randn(_QUERY_HEADS, device="cuda", dtype=torch.float32)
output_gradient = torch.randn_like(query_storage).transpose(1, 2)
assert not output_gradient.is_contiguous()
assert output_gradient.stride(-1) == 1
candidate_query = query_storage.clone().requires_grad_()
candidate_kv = kv_storage.clone().requires_grad_()
candidate_sink = sink_storage.clone().requires_grad_()
candidate = deepseek_v4_sliding_attention(
candidate_query,
candidate_kv,
candidate_sink,
)
candidate_gradients = torch.autograd.grad(
candidate,
(candidate_query, candidate_kv, candidate_sink),
output_gradient,
)
reference_query = query_storage.clone().requires_grad_()
reference_kv = kv_storage.clone().requires_grad_()
reference_sink = sink_storage.clone().requires_grad_()
reference = _reference_sliding_attention_bshd(
reference_query.transpose(1, 2),
reference_kv.transpose(1, 2),
reference_sink,
)
reference_gradients = torch.autograd.grad(
reference,
(reference_query, reference_kv, reference_sink),
output_gradient.float(),
)
torch.cuda.synchronize()
thresholds = {
"output": (0.0022, 0.99999),
"query gradient": (0.003, 0.99999),
"shared-KV gradient": (0.0035, 0.99999),
"sink gradient": (0.0005, 0.999999),
}
values = (("output", candidate, reference),) + tuple(
(label, got, expected)
for label, got, expected in zip(
("query gradient", "shared-KV gradient", "sink gradient"),
candidate_gradients,
reference_gradients,
)
)
for label, got, expected in values:
assert torch.isfinite(got).all(), f"nonfinite {label}"
relative_rmse, cosine, _ = _relative_metrics(got, expected)
maximum_rmse, minimum_cosine = thresholds[label]
assert relative_rmse <= maximum_rmse, (label, relative_rmse)
assert cosine >= minimum_cosine, (label, cosine)
def test_sliding_attention_accepts_model_native_transpose_views() -> None:
query = torch.empty(
1,
_QUERY_HEADS,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
shared_kv = torch.empty(
1,
1,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
sink = torch.empty(_QUERY_HEADS, device="cuda", dtype=torch.float32)
assert query.is_contiguous()
assert not query.transpose(1, 2).is_contiguous()
with torch.no_grad():
output = deepseek_v4_sliding_attention(query, shared_kv, sink)
assert output.shape == (1, _SEQUENCE_LENGTH, _QUERY_HEADS, _HEAD_DIM)
assert output.is_contiguous()
@pytest.mark.parametrize("batch", [2, 8])
def test_sliding_attention_rejects_unsupported_batches(batch: int) -> None:
query = torch.empty(
batch, 1, _QUERY_HEADS, _HEAD_DIM, device="cuda", dtype=torch.bfloat16
)
shared_kv = torch.empty(batch, 1, 1, _HEAD_DIM, device="cuda", dtype=torch.bfloat16)
sink = torch.empty(_QUERY_HEADS, device="cuda", dtype=torch.float32)
with pytest.raises(ValueError, match="unsupported.*batch"):
deepseek_v4_sliding_attention_bshd(query, shared_kv, sink)
def test_sliding_attention_rejects_output_gradient_copy() -> None:
query = torch.empty(
1,
_QUERY_HEADS,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
requires_grad=True,
)
shared_kv = torch.empty(
1,
1,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
requires_grad=True,
)
sink = torch.empty(
_QUERY_HEADS, device="cuda", dtype=torch.float32, requires_grad=True
)
output_gradient = torch.empty(
1,
_SEQUENCE_LENGTH,
_HEAD_DIM,
_QUERY_HEADS,
device="cuda",
dtype=torch.bfloat16,
).transpose(2, 3)
output = deepseek_v4_sliding_attention(query, shared_kv, sink)
assert output_gradient.shape == output.shape
assert output_gradient.stride(-1) != 1
with pytest.raises(ValueError, match="contiguous last dimension"):
torch.autograd.grad(output, (query, shared_kv, sink), output_gradient)
def test_sliding_attention_rejects_wrong_dtype() -> None:
query = torch.empty(
1,
_SEQUENCE_LENGTH,
_QUERY_HEADS,
_HEAD_DIM,
device="cuda",
dtype=torch.float16,
)
shared_kv = torch.empty(
1,
_SEQUENCE_LENGTH,
1,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
sink = torch.empty(_QUERY_HEADS, device="cuda", dtype=torch.float32)
with pytest.raises(TypeError, match="bfloat16"):
deepseek_v4_sliding_attention_bshd(query, shared_kv, sink)
def test_attention_dispatch_uses_sliding_kernel_without_layout_copies() -> None:
query_storage = torch.randn(
1,
_QUERY_HEADS,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
kv_storage = torch.randn(
1,
1,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
sink = torch.randn(_QUERY_HEADS, device="cuda", dtype=torch.float32)
query = query_storage
shared_kv = kv_storage
module = SimpleNamespace(
layer_type="sliding_attention",
attention_dropout=0.0,
sliding_window=_WINDOW,
sinks=sink,
)
setattr(module, _CONFIG_MARKER, True)
canonical_mask = torch.empty(
1,
1,
_SEQUENCE_LENGTH,
_SEQUENCE_LENGTH,
device="cuda",
dtype=torch.bfloat16,
)
with torch.no_grad():
dispatched, weights = _deepseek_v4_attention_forward(
cast(Any, module),
query,
shared_kv,
shared_kv,
canonical_mask,
scaling=1.0 / math.sqrt(_HEAD_DIM),
dropout=0.0,
s_aux=sink,
)
direct = deepseek_v4_sliding_attention(query, shared_kv, sink)
assert weights is None
assert dispatched.is_contiguous()
assert torch.equal(dispatched, direct)
def test_canonical_mask_has_one_physical_batch() -> None:
mask = _canonical_training_mask(
batch_size=4,
q_length=_SEQUENCE_LENGTH,
kv_length=_SEQUENCE_LENGTH,
mask_function=sliding_window_causal_mask_function(_WINDOW),
attention_mask=torch.ones(4, _SEQUENCE_LENGTH, device="cuda", dtype=torch.bool),
dtype=torch.bfloat16,
device="cuda",
)
assert mask.shape == (4, 1, _SEQUENCE_LENGTH, _SEQUENCE_LENGTH)
assert mask.stride(0) == 0
assert mask.untyped_storage().nbytes() == 2 * _SEQUENCE_LENGTH**2
def test_model_input_guard_rejects_noncanonical_shape() -> None:
with pytest.raises(ValueError, match=r"B in \{1,4,16\} and S=2048"):
_validate_model_inputs(
cast(Any, None),
(),
{"input_ids": torch.zeros(1, 1024, device="cuda", dtype=torch.long)},
)
def test_non_reentrant_checkpoint_recomputation_preserves_gradients() -> None:
torch.manual_seed(2048)
query = torch.randn(
1,
_QUERY_HEADS,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
shared_kv = torch.randn(
1,
1,
_SEQUENCE_LENGTH,
_HEAD_DIM,
device="cuda",
dtype=torch.bfloat16,
)
sink = torch.randn(_QUERY_HEADS, device="cuda", dtype=torch.float32)
output_gradient = torch.randn_like(query).transpose(1, 2)
direct_inputs = (
query.clone().requires_grad_(),
shared_kv.clone().requires_grad_(),
sink.clone().requires_grad_(),
)
checkpoint_inputs = (
query.clone().requires_grad_(),
shared_kv.clone().requires_grad_(),
sink.clone().requires_grad_(),
)
def run(query_bhsd, kv_bhsd, sink_values):
return deepseek_v4_sliding_attention(query_bhsd, kv_bhsd, sink_values)
direct_output = run(*direct_inputs)
direct_gradients = torch.autograd.grad(
direct_output, direct_inputs, output_gradient
)
checkpoint_output = checkpoint(run, *checkpoint_inputs, use_reentrant=False)
checkpoint_gradients = torch.autograd.grad(
checkpoint_output, checkpoint_inputs, output_gradient
)
torch.cuda.synchronize()
assert torch.equal(checkpoint_output, direct_output)
assert torch.equal(checkpoint_gradients[0], direct_gradients[0])
assert torch.equal(checkpoint_gradients[1], direct_gradients[1])
sink_relative_rmse, sink_cosine, sink_max_abs = _relative_metrics(
checkpoint_gradients[2], direct_gradients[2]
)
assert sink_relative_rmse <= 1e-6
assert sink_cosine >= 0.999999
assert sink_max_abs <= 1e-4
def test_attention_configuration_is_complete_and_idempotent() -> None:
class AttentionShell(DeepseekV4Attention):
def __init__(self, layer_type: str) -> None:
torch.nn.Module.__init__(self)
self.layer_type = layer_type
class ModelShell(DeepseekV4Model):
def __init__(self, config: DeepseekV4Config) -> None:
torch.nn.Module.__init__(self)
self.config = config
layer_types = (
["sliding_attention"] * 2
+ ["compressed_sparse_attention"] * 21
+ ["heavily_compressed_attention"] * 20
)
self.attentions = torch.nn.ModuleList(
AttentionShell(layer_type) for layer_type in layer_types
)
config = DeepseekV4Config(
num_hidden_layers=43,
layer_types=(
["sliding_attention"] * 2
+ ["compressed_sparse_attention"] * 21
+ ["heavily_compressed_attention"] * 20
),
use_cache=False,
attention_dropout=0.0,
)
model = ModelShell(config)
first = configure_deepseek_v4_attention(model)
require_complete_deepseek_v4_attention(first)
second = configure_deepseek_v4_attention(model)
require_complete_deepseek_v4_attention(second)
assert first["configured_sliding"] == 2
assert first["already_configured"] == 0
assert first["configured_csa"] == 21
assert first["already_configured_csa"] == 0
assert second["configured_sliding"] == 0
assert second["already_configured"] == 2
assert second["configured_csa"] == 0
assert second["already_configured_csa"] == 21
assert config._attn_implementation == "deepseek_v4_project"