Repository navigation
Expand file tree
/
Copy pathtest_flash_attn.py
More file actions
514 lines (453 loc) · 23.9 KB
/
Copy pathtest_flash_attn.py
File metadata and controls
514 lines (453 loc) · 23.9 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vllm-ascend project
import sys
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from vllm.model_executor.layers.attention.attention import Attention
from vllm.v1.attention.backend import AttentionType
from vllm.v1.attention.selector import AttentionSelectorConfig
from vllm.v1.kv_cache_interface import FullAttentionSpec
from vllm_ascend.attention.attention_v1 import AscendAttentionBackend
from vllm_ascend.attention.fa_v1 import AscendFlashAttentionBackend, AscendFlashAttentionImpl
from vllm_ascend.models.minimax_m3 import msa_m3
from vllm_ascend.worker.v2 import attn_utils
@pytest.mark.parametrize("head_size", [64, 72, 128, 192, 256])
@pytest.mark.parametrize("padding", [None, 64, 1048576])
def test_flash_attn_physical_cache_budget(head_size, padding):
original = FullAttentionSpec(
block_size=128, num_kv_heads=8, head_size=head_size, dtype=torch.bfloat16, page_size_padded=padding
)
physical = AscendFlashAttentionBackend.customize_spec(original)
if head_size != 192:
assert physical is original
return
assert original.head_size == original.head_size_v == 192
assert physical.head_size == physical.head_size_v == 256
natural_bytes = 2 * 128 * 8 * 256 * 2
assert physical.page_size_bytes == max(padding or 0, natural_bytes)
assert AscendFlashAttentionBackend.customize_spec(physical) is physical
@pytest.mark.parametrize("num_tokens", [0, 1, 7])
@pytest.mark.parametrize("head_size", [128, 192, 256])
def test_flash_attn_padding_preserves_input(num_tokens, head_size):
tensor = torch.randn(num_tokens, 2, head_size)
padded = AscendFlashAttentionImpl._pad_head(tensor)
torch.testing.assert_close(padded[..., :head_size], tensor, rtol=0, atol=0)
if head_size == 192:
assert padded.shape == (num_tokens, 2, 256)
assert torch.count_nonzero(padded[..., head_size:]) == 0
else:
assert padded is tensor
@pytest.mark.parametrize("writer", ["reshape_and_cache", "do_kv_cache_update"])
@pytest.mark.parametrize("bnsd", [False, True])
def test_flash_attn_both_cache_writers_pad_kv(monkeypatch, writer, bnsd):
from vllm_ascend.attention import attention_v1
query = torch.randn(7, 2, 192)
key, value = torch.randn_like(query), torch.randn_like(query)
impl = make_impl(query, num_kv_heads=2, layout="PA_BNBD" if bnsd else "PA_BBND")
impl.attn_type = AttentionType.DECODER
impl.kv_sharing_target_layer_name = None
cache_shape = (2, 2, 128, 256) if bnsd else (2, 128, 2, 256)
cache = (torch.empty(cache_shape), torch.empty(cache_shape))
slots = torch.tensor([0, 3, 127, 128, 130, 150, 255], dtype=torch.int32)
cache_write = Mock()
monkeypatch.setattr(attention_v1.DeviceOperator, "reshape_and_cache", cache_write)
monkeypatch.setattr(attention_v1, "notify_kv_cache_written", lambda: None)
if writer == "reshape_and_cache":
impl.reshape_and_cache(
query, key, value, cache, SimpleNamespace(slot_mapping=slots, num_actual_tokens=7), torch.empty_like(query)
)
else:
impl.do_kv_cache_update(None, key, value, cache, slots)
cache_write.assert_called_once()
args = cache_write.call_args.kwargs
for name, original in (("key", key), ("value", value)):
assert args[name].shape == (7, 2, 256)
torch.testing.assert_close(args[name][..., :192], original, rtol=0, atol=0)
assert torch.count_nonzero(args[name][..., 192:]) == 0
assert args["key_cache"] is cache[0]
assert args["value_cache"] is cache[1]
assert args["use_bnsd"] is bnsd
torch.testing.assert_close(args["slot_mapping"], slots)
@pytest.mark.parametrize("causal", [False, True])
def test_flash_attn_192_preserves_scale_and_output_buffer(monkeypatch, causal):
from vllm_ascend.attention.fa_v1 import AscendFlashAttentionMetadata
query = torch.randn(7, 2, 192)
impl = make_impl(query)
result = torch.randn(7, 2, 256)
output = torch.full((8, 2, 192), float("nan"))
ops = SimpleNamespace(
flash_attn_metadata=Mock(return_value=torch.empty(16)),
flash_attn=Mock(return_value=(result, None)),
)
monkeypatch.setitem(sys.modules, "cann_ops_transformer.ops", ops)
metadata = AscendFlashAttentionMetadata(
query_start_loc_gpu=torch.tensor([0, 7], dtype=torch.int32),
seq_lens_gpu=torch.tensor([31], dtype=torch.int32),
block_tables=torch.zeros(1, 1, dtype=torch.int32),
max_query_len=7,
causal=causal,
)
assert impl.forward_impl(query, None, None, None, metadata, output) is output
assert ops.flash_attn_metadata.call_args.args[2] == 256
assert ops.flash_attn.call_args.kwargs["softmax_scale"] == 192**-0.5
padded_query = ops.flash_attn.call_args.args[0]
torch.testing.assert_close(padded_query[..., :192], query, rtol=0, atol=0)
assert torch.count_nonzero(padded_query[..., 192:]) == 0
torch.testing.assert_close(output[:7], result[..., :192], rtol=0, atol=0)
assert output[7].isnan().all()
@pytest.mark.parametrize("backend", [AscendFlashAttentionBackend, AscendAttentionBackend])
def test_flash_attn_192_cache_spec_integration(monkeypatch, backend):
layer = Attention.__new__(Attention)
torch.nn.Module.__init__(layer)
layer.attn_type = AttentionType.DECODER
layer.attn_backend = backend
layer.is_draft_layer = True
layer.kv_cache_dtype = "bfloat16"
layer.kv_cache_torch_dtype = torch.bfloat16
layer.sliding_window = None
layer.num_kv_heads = 8
layer.head_size = layer.head_size_v = 192
config = SimpleNamespace(
model_config=SimpleNamespace(dtype=torch.bfloat16),
parallel_config=SimpleNamespace(decode_context_parallel_size=1),
attention_config=SimpleNamespace(indexer_kv_dtype="auto"),
cache_config=SimpleNamespace(cache_dtype="fp8", block_size=128, skip_page_size_padded=None),
)
monkeypatch.setattr(attn_utils, "get_layers_from_vllm_config", lambda *_args: {"draft.attn": layer})
monkeypatch.setattr(attn_utils, "enable_sfa_dcp_replicated_indexer", lambda _config: False)
spec = attn_utils.get_kv_cache_spec(config)["draft.attn"]
physical_size = 256 if backend is AscendFlashAttentionBackend else 192
assert spec.head_size == spec.head_size_v == physical_size
assert spec.page_size_bytes == 2 * 128 * 8 * physical_size * 2
assert layer.head_size == layer.head_size_v == 192
def test_flash_attn_preserves_m3_fp8_target_kernel_pages(monkeypatch):
config = SimpleNamespace(
model_config=SimpleNamespace(hf_config=SimpleNamespace(architectures=["MiniMaxM3SparseForCausalLM"])),
cache_config=SimpleNamespace(cache_dtype="fp8", kv_cache_dtype_skip_layers=["0", "1", "2"]),
)
monkeypatch.setattr(msa_m3, "get_current_vllm_config_or_none", lambda: config)
monkeypatch.setattr(msa_m3, "get_current_hardware_profile", lambda: SimpleNamespace(supports=lambda _: True))
for backend in (
msa_m3.AscendMiniMaxM3DenseBackend,
msa_m3.AscendMiniMaxM3SparseBackend,
msa_m3.AscendMiniMaxM3IndexerBackend,
):
assert backend.get_supported_kernel_block_sizes() == [64, 128]
assert AscendAttentionBackend.get_supported_kernel_block_sizes() == [128]
assert AscendFlashAttentionBackend.get_supported_kernel_block_sizes() == [128]
@pytest.fixture
def dflash2_config():
return SimpleNamespace(
model_config=SimpleNamespace(runner_type="generate"),
speculative_config=SimpleNamespace(
method="dflash",
target_model_config=SimpleNamespace(hf_text_config=SimpleNamespace(model_type="glm_moe_dsa")),
draft_model_config=SimpleNamespace(architectures=["DFlash2DraftModel"]),
draft_sample_method="greedy",
enforce_eager=True,
),
)
def make_impl(query, *, num_kv_heads=1, window=None, layout="PA_BBND", sinks=None):
impl = AscendFlashAttentionImpl.__new__(AscendFlashAttentionImpl)
impl.num_heads, impl.num_kv_heads = query.shape[1], num_kv_heads
impl.scale, impl.sliding_window = query.shape[-1] ** -0.5, window
impl.use_bnsd_kv_cache, impl.sinks = layout == "PA_BNBD", sinks
impl.key_cache = impl.value_cache = None
return impl
@pytest.fixture
def select_flash_attn(monkeypatch):
from vllm_ascend import platform
from vllm_ascend.device.hardware_profile import AttentionBackendFamily
profile = SimpleNamespace(supports=lambda _: True, attention_backend_family=AttentionBackendFamily.STANDARD)
monkeypatch.setattr(platform, "get_current_hardware_profile", lambda: profile)
monkeypatch.setattr(platform, "_validate_fa3_backend", lambda *_: False)
def select(config, selector, tag="dflash_head"):
monkeypatch.setattr("vllm.config.get_current_vllm_config_or_none", lambda: config)
with monkeypatch.context() as context:
context.setattr("vllm.compilation.backends.model_tag", tag)
backend = platform.NPUPlatform.get_attn_backend_cls(None, selector)
return backend == "vllm_ascend.attention.fa_v1.AscendFlashAttentionBackend"
return select, profile
@pytest.mark.parametrize(
"architecture", ["MiniMaxM3SparseForCausalLM", "MiniMaxM3SparseForConditionalGeneration", "GlmMoeDsaForCausalLM"]
)
@pytest.mark.parametrize("head_size", [128, 192, 256])
@pytest.mark.parametrize("enforce_eager", [False, True])
def test_dspark_selects_flash_attn_for_eager_and_graph_drafts(
select_flash_attn, dflash2_config, architecture, enforce_eager, head_size
):
select, _ = select_flash_attn
config = dflash2_config
spec = config.speculative_config
spec.method = "dspark"
spec.enforce_eager = enforce_eager
spec.target_model_config = SimpleNamespace(hf_config=SimpleNamespace(architectures=[architecture]))
spec.draft_model_config.architectures = ["Qwen3DSparkModel"]
selector = AttentionSelectorConfig(head_size=head_size, block_size=128, dtype=torch.bfloat16, kv_cache_dtype="auto")
assert select(config, selector, "dspark_head")
assert not select(config, selector, "backbone")
assert not select(config, selector, "dflash_head")
assert not select(config, selector._replace(kv_cache_dtype="fp8"), "dspark_head")
spec.draft_model_config.architectures = ["DFlash2DraftModel"]
assert not select(config, selector, "dspark_head")
spec.draft_model_config.architectures = ["Qwen3DSparkModel"]
spec.target_model_config.hf_config.architectures = ["OtherModel"]
assert not select(config, selector, "dspark_head")
def test_flash_attn_is_selected_only_for_a5_dflash2_eager_drafts(select_flash_attn, dflash2_config):
select, profile = select_flash_attn
config = dflash2_config
selector = AttentionSelectorConfig(head_size=128, block_size=128, dtype=torch.bfloat16, kv_cache_dtype="auto")
assert select(config, selector)
profile.supports = lambda _: False
assert not select(config, selector)
profile.supports = lambda _: True
config.speculative_config = None
config.model_config = None
assert not select(config, selector)
assert not select(None, selector)
@pytest.mark.parametrize("tag", ["backbone", "eagle_head", "dflash2_candidate_selector"])
def test_flash_attn_requires_live_dflash_loader_tag(monkeypatch, select_flash_attn, dflash2_config, tag):
from vllm.compilation import backends
from vllm_ascend import platform
monkeypatch.setattr("vllm.config.get_current_vllm_config_or_none", lambda: dflash2_config)
monkeypatch.setattr(backends, "model_tag", tag)
selector = AttentionSelectorConfig(head_size=128, block_size=128, dtype=torch.bfloat16, kv_cache_dtype="auto")
assert not platform._validate_flash_attn_backend((False, False, False), selector)
@pytest.mark.parametrize("sampling", ["greedy", "probabilistic"])
def test_flash_attn_selection_is_independent_of_draft_sampling(select_flash_attn, dflash2_config, sampling):
select, _ = select_flash_attn
dflash2_config.speculative_config.draft_sample_method = sampling
selector = AttentionSelectorConfig(head_size=128, block_size=128, dtype=torch.bfloat16, kv_cache_dtype="auto")
assert select(dflash2_config, selector)
@pytest.mark.parametrize(
"field,value",
[
("method", "eagle3"),
("method", "mtp"),
("enforce_eager", False),
("enforce_eager", None),
("architectures", ["DFlashDraftModel"]),
("architectures", None),
],
)
def test_other_drafts_and_graph_mode_retain_legacy_backend(select_flash_attn, dflash2_config, field, value):
select, _ = select_flash_attn
spec = dflash2_config.speculative_config
setattr(spec.draft_model_config if field == "architectures" else spec, field, value)
selector = AttentionSelectorConfig(head_size=128, block_size=128, dtype=torch.bfloat16, kv_cache_dtype="auto")
assert not select(dflash2_config, selector)
@pytest.mark.parametrize("model_type", ["qwen3", "deepseek_v32", "glm5_next"])
def test_other_target_architectures_retain_legacy_backend(select_flash_attn, dflash2_config, model_type):
select, _ = select_flash_attn
dflash2_config.speculative_config.target_model_config.hf_text_config.model_type = model_type
selector = AttentionSelectorConfig(head_size=128, block_size=128, dtype=torch.bfloat16, kv_cache_dtype="auto")
assert not select(dflash2_config, selector)
@pytest.mark.parametrize(
"field,value",
[
("use_mla", True),
("use_sparse", True),
("use_pcp", True),
("use_dcp", True),
("use_compress", True),
("use_mm_prefix", True),
("use_rswa", True),
("use_batch_invariant", True),
("use_per_head_quant_scales", True),
("head_size", 160),
("dtype", torch.float32),
("kv_cache_dtype", "fp8"),
("kv_cache_dtype", "int8"),
],
)
def test_unsupported_gqa_features_retain_legacy_backend(select_flash_attn, dflash2_config, field, value):
select, _ = select_flash_attn
selector = AttentionSelectorConfig(head_size=128, block_size=128, dtype=torch.bfloat16, kv_cache_dtype="auto")
if field == "use_compress":
selector = SimpleNamespace(**{**selector._asdict(), "use_mla": True, field: value})
else:
selector = selector._replace(**{field: value})
if field == "use_sparse":
selector = selector._replace(use_mla=True)
assert not select(dflash2_config, selector)
@pytest.mark.parametrize(
"window,causal,mask_mode,win_right",
[(None, False, 0, -1), (None, True, 3, -1), (2048, False, 4, 2047), (2048, True, 4, 0)],
)
def test_flash_attn_reuses_plans_within_each_draft_step(monkeypatch, window, causal, mask_mode, win_right):
from vllm_ascend.attention.fa_v1 import AscendFlashAttentionMetadata
plan = torch.empty(16, dtype=torch.int32)
query = torch.zeros(12, 8, 128, dtype=torch.bfloat16)
sinks = torch.zeros(8)
impl = make_impl(query, window=window, sinks=sinks)
output = torch.empty_like(query)
ops = SimpleNamespace(flash_attn_metadata=Mock(return_value=plan), flash_attn=Mock(return_value=(query, None)))
monkeypatch.setitem(sys.modules, "cann_ops_transformer.ops", ops)
metadata_args = dict(
query_start_loc_gpu=torch.tensor([0, 6, 12, -1], dtype=torch.int32),
block_tables=torch.tensor([[1, 0], [3, 2], [0, 0]], dtype=torch.int32),
max_query_len=6,
causal=causal,
flash_attn_mask=torch.ones(2, 2, dtype=torch.int8),
)
for step, lengths in enumerate(([256, 254, 0], [253, 251, 0]), start=1):
metadata = AscendFlashAttentionMetadata(seq_lens_gpu=torch.tensor(lengths, dtype=torch.int32), **metadata_args)
for _ in range(6):
impl.forward_impl(query, None, None, None, metadata, output)
assert ops.flash_attn_metadata.call_count == step
assert ops.flash_attn.call_count == 6 * step
meta_args = ops.flash_attn_metadata.call_args.kwargs
args = ops.flash_attn.call_args.kwargs
assert args["metadata"] is plan
assert args["sinks"] is sinks
assert args["attn_mask"] is (metadata.flash_attn_mask if mask_mode else None)
torch.testing.assert_close(meta_args["seqused_kv"], torch.tensor(lengths, dtype=torch.int32).clamp(min=1))
torch.testing.assert_close(meta_args["cu_seqlens_q"], torch.tensor([0, 6, 12, 12], dtype=torch.int32))
assert meta_args["mask_mode"] == mask_mode
assert meta_args["win_left"] == (2047 if window else -1)
assert meta_args["win_right"] == win_right
@pytest.mark.parametrize("cpu_mirrors", [False, True])
def test_metadata_builder_does_not_materialize_exact_device_lengths(monkeypatch, cpu_mirrors):
from vllm_ascend.attention import fa_v1 as module
from vllm_ascend.attention.attention_v1 import AscendAttentionState
builder = module.AscendFlashAttentionMetadataBuilder.__new__(module.AscendFlashAttentionMetadataBuilder)
builder.model_config = SimpleNamespace(runner_type="draft")
builder.attn_mask_builder = SimpleNamespace(
get_splitfuse_attn_mask=Mock(return_value=torch.ones(2, 2)),
)
common = SimpleNamespace(
num_reqs=2,
num_actual_tokens=12,
query_start_loc_cpu=torch.tensor([0, 999, 1998], dtype=torch.int32) if cpu_mirrors else None,
query_start_loc=torch.tensor([0, 6, 12], dtype=torch.int32),
block_table_tensor=torch.zeros(2, 2, dtype=torch.int32),
seq_lens=torch.tensor([256, 254], dtype=torch.int32),
_seq_lens_cpu=torch.tensor([999, 999], dtype=torch.int32) if cpu_mirrors else None,
seq_lens_cpu=torch.tensor([888, 888], dtype=torch.int32) if cpu_mirrors else None,
max_seq_len=256,
slot_mapping=torch.arange(12),
causal=False,
max_query_len=6,
attn_mask=None,
attn_state=AscendAttentionState.ChunkedPrefill,
)
def forbidden_host_materialization(*args, **kwargs):
raise AssertionError("draft length metadata must not be materialized or staged on the host")
with monkeypatch.context() as patches:
for operation in ("tolist", "item", "cpu"):
patches.setattr(torch.Tensor, operation, forbidden_host_materialization)
result = builder.build_for_drafting(common, draft_index=0)
assert result.seq_lens_gpu.data_ptr() == common.seq_lens.data_ptr()
assert result.query_start_loc_gpu.data_ptr() == common.query_start_loc.data_ptr()
assert not result.causal
assert result.flash_attn_mask is builder.attn_mask_builder.get_splitfuse_attn_mask.return_value
def test_metadata_builder_slices_request_extent():
from vllm_ascend.attention.fa_v1 import AscendFlashAttentionMetadataBuilder
builder = AscendFlashAttentionMetadataBuilder.__new__(AscendFlashAttentionMetadataBuilder)
builder.model_config = SimpleNamespace(runner_type="generate")
builder.attn_mask_builder = SimpleNamespace(get_splitfuse_attn_mask=lambda: None)
common = SimpleNamespace(
num_reqs=2,
num_actual_tokens=12,
query_start_loc=torch.tensor([0, 6, 12, 999, 999], dtype=torch.int32),
seq_lens=torch.tensor([100, 200, 999, 999], dtype=torch.int32),
block_table_tensor=torch.zeros(4, 8, dtype=torch.int32),
slot_mapping=torch.arange(24, dtype=torch.int32),
max_query_len=6,
causal=False,
attn_state=None,
)
metadata = builder.build(0, common)
assert metadata.block_tables.shape == (2, 8)
assert metadata.slot_mapping.shape == (12,)
torch.testing.assert_close(metadata.query_start_loc_gpu, torch.tensor([0, 6, 12], dtype=torch.int32))
torch.testing.assert_close(metadata.seq_lens_gpu, torch.tensor([100, 200], dtype=torch.int32))
assert metadata.seq_lens_gpu.data_ptr() == common.seq_lens.data_ptr()
assert metadata.query_start_loc_gpu.data_ptr() == common.query_start_loc.data_ptr()
def test_distinct_attention_geometries_do_not_share_plans(monkeypatch):
from vllm_ascend.attention.fa_v1 import AscendFlashAttentionMetadata
ops = SimpleNamespace(
flash_attn_metadata=Mock(return_value=torch.empty(16)),
flash_attn=Mock(side_effect=lambda query, *args, **kwargs: (query, None)),
)
monkeypatch.setitem(sys.modules, "cann_ops_transformer.ops", ops)
metadata = AscendFlashAttentionMetadata(
query_start_loc_gpu=torch.tensor([0, 6], dtype=torch.int32),
seq_lens_gpu=torch.tensor([256], dtype=torch.int32),
block_tables=torch.zeros(1, 2, dtype=torch.int32),
max_query_len=6,
causal=False,
flash_attn_mask=torch.ones(2, 2),
)
geometries = [
(8, 1, 128, None, "PA_BBND"),
(16, 1, 128, None, "PA_BBND"),
(8, 2, 128, None, "PA_BBND"),
(8, 1, 64, None, "PA_BBND"),
(8, 1, 128, 2048, "PA_BBND"),
(8, 1, 128, None, "PA_BNBD"),
]
for _ in range(2):
for heads, kv_heads, dim, window, layout in geometries:
query = torch.empty(6, heads, dim)
impl = make_impl(query, num_kv_heads=kv_heads, window=window, layout=layout)
impl.forward_impl(query, None, None, None, metadata, torch.empty_like(query))
assert ops.flash_attn_metadata.call_count == len(geometries)
assert ops.flash_attn.call_count == 2 * len(geometries)
@pytest.mark.parametrize("backend", [AscendFlashAttentionBackend, AscendAttentionBackend], ids=["flash", "fia"])
@pytest.mark.parametrize("is_draft", [False, True], ids=["target", "draft"])
@pytest.mark.parametrize("window", [None, 1024], ids=["full", "swa"])
@pytest.mark.parametrize("num_kv_heads", [1, 2])
@pytest.mark.parametrize("padding_kind", ["none", "undersized", "exact", "surplus"])
def test_only_undersized_draft_swa_pages_drop_inherited_padding(
monkeypatch, backend, is_draft, window, num_kv_heads, padding_kind
):
layer = Attention.__new__(Attention)
torch.nn.Module.__init__(layer)
layer.attn_type = AttentionType.DECODER
layer.attn_backend = backend
layer.is_draft_layer = is_draft
layer.kv_cache_dtype = "bfloat16"
layer.kv_cache_torch_dtype = torch.bfloat16
layer.sliding_window = window
layer.num_kv_heads = num_kv_heads
layer.head_size = layer.head_size_v = 128
natural_page = 128 * num_kv_heads * 128 * 2 * 2
padding = {"none": None, "undersized": 32768, "exact": natural_page, "surplus": natural_page * 2}[padding_kind]
config = SimpleNamespace(
model_config=SimpleNamespace(dtype=torch.bfloat16),
parallel_config=SimpleNamespace(decode_context_parallel_size=1),
attention_config=SimpleNamespace(indexer_kv_dtype="auto"),
cache_config=SimpleNamespace(cache_dtype="fp8", block_size=128, skip_page_size_padded=padding),
)
original = layer.get_kv_cache_spec(config)
incompatible = window is not None and padding_kind == "undersized"
if incompatible:
with pytest.raises(AssertionError):
_ = original.page_size_bytes
get_spec = Mock(wraps=layer.get_kv_cache_spec)
monkeypatch.setattr(layer, "get_kv_cache_spec", get_spec)
monkeypatch.setattr(attn_utils, "get_layers_from_vllm_config", lambda *_args: {"draft.attn": layer})
monkeypatch.setattr(attn_utils, "enable_sfa_dcp_replicated_indexer", lambda _config: False)
spec = attn_utils.get_kv_cache_spec(config)["draft.attn"]
recover = incompatible and is_draft
if incompatible and not is_draft:
with pytest.raises(AssertionError):
_ = spec.page_size_bytes
else:
expected_padding = None if recover else original.page_size_padded
assert spec.page_size_bytes == (expected_padding or natural_page)
assert spec.page_size_padded == expected_padding
# Both supported versions propagate the draft marker.
if hasattr(original, "is_draft"):
assert spec.is_draft is is_draft
assert config.cache_config.skip_page_size_padded == padding
assert get_spec.call_count == (2 if recover else 1)
assert get_spec.call_args_list[0].args[0] is config
if recover:
layer_config = get_spec.call_args_list[1].args[0]
assert layer_config is not config
assert layer_config.cache_config is not config.cache_config
assert layer_config.cache_config.skip_page_size_padded is None