# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import math from types import SimpleNamespace import pytest import torch from vllm.v1.kv_cache_interface import ( ChunkedLocalAttentionSpec, SlidingWindowSpec, ) from vllm.v1.worker import utils as worker_utils from vllm.v1.worker.utils import ( AttentionGroup, KVBlockZeroer, _zero_kv_blocks_kernel, ) class _BlockFirstBackend: @staticmethod def get_kv_cache_block_dim(*args, **kwargs): return 0 @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") @pytest.mark.parametrize( "spec", [ SlidingWindowSpec( block_size=2, num_kv_heads=1, head_size=1, dtype=torch.uint8, sliding_window=4, ), ChunkedLocalAttentionSpec( block_size=2, num_kv_heads=1, head_size=1, dtype=torch.uint8, attention_chunk_size=4, ), ], ids=["sliding-window", "chunked-local"], ) def test_attention_blocks_are_zeroed(spec): device = torch.device("cuda") storage = torch.ones((4, 1, 2, 2), dtype=torch.uint8, device=device) layer_name = "draft.self_attn" zeroer = KVBlockZeroer( device, attn_groups_iter=[ AttentionGroup(_BlockFirstBackend, [layer_name], spec, 0) # type: ignore[arg-type] ], kernel_block_sizes=[2], cache_dtype="fp8", static_forward_context={ layer_name: SimpleNamespace(kv_cache=storage), }, ) zeroer.zero_block_ids([1]) torch.accelerator.synchronize() expected = torch.ones_like(storage) expected[1] = 0 assert torch.equal(storage, expected) @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") def test_block_ids_are_not_overwritten_while_copy_is_in_flight(): device = torch.device("cuda") num_blocks = 4 page_size_el = 4 storage = torch.ones((num_blocks, page_size_el), dtype=torch.int32, device=device) # Build the minimal zeroer state directly so the test can focus on the # in-flight copy behavior without constructing model attention groups. zeroer = KVBlockZeroer.__new__(KVBlockZeroer) zeroer.device = device zeroer._meta = ( torch.tensor([storage.data_ptr()], dtype=torch.uint64, device=device), torch.tensor([page_size_el], dtype=torch.int64, device=device), torch.tensor([page_size_el], dtype=torch.int64, device=device), page_size_el // page_size_el, # max_chunks = 1 page_size_el, # blk_size 1, # n_segs ) stream = torch.cuda.Stream() with torch.cuda.stream(stream): # Keep the first nonblocking H2D copy pending while the host submits the # second call. Each call must stage from its own pinned source so the # first copy is not corrupted before it runs. torch.cuda._sleep(10_000_000) zeroer.zero_block_ids([1]) zeroer.zero_block_ids([2]) stream.synchronize() assert torch.all(storage[0] == 1) assert torch.all(storage[1] == 0) assert torch.all(storage[2] == 0) assert torch.all(storage[3] == 1) @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") def test_non_uniform_page_sizes(): """Two segments with different page sizes (e.g. MLA + DSA indexer).""" device = torch.device("cuda") num_blocks = 4 page_size_a = 10496 # int32 elements page_size_b = 2112 storage_a = torch.ones((num_blocks, page_size_a), dtype=torch.int32, device=device) storage_b = torch.ones((num_blocks, page_size_b), dtype=torch.int32, device=device) zeroer = KVBlockZeroer.__new__(KVBlockZeroer) zeroer.device = device seg_page_sizes = [page_size_a, page_size_b] max_ps = max(seg_page_sizes) blk_size = min(1 << (max_ps - 1).bit_length(), 1024) zeroer._meta = ( torch.tensor( [storage_a.data_ptr(), storage_b.data_ptr()], dtype=torch.uint64, device=device, ), torch.tensor(seg_page_sizes, dtype=torch.int64, device=device), torch.tensor(seg_page_sizes, dtype=torch.int64, device=device), (max_ps + blk_size - 1) // blk_size, blk_size, 2, ) stream = torch.cuda.Stream() with torch.cuda.stream(stream): zeroer.zero_block_ids([1, 2]) stream.synchronize() for storage in (storage_a, storage_b): assert torch.all(storage[0] == 1) assert torch.all(storage[1] == 0) assert torch.all(storage[2] == 0) assert torch.all(storage[3] == 1) @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") def test_packed_segment_zeros_only_its_last_block_page(): """A packed KV segment steps by block stride but clears only its page.""" device = torch.device("cuda") num_blocks = 4 block_stride_el = 12 page_size_el = 4 page_offset_el = 3 backing = torch.ones( (num_blocks, block_stride_el), dtype=torch.int32, device=device ) zeroer = KVBlockZeroer.__new__(KVBlockZeroer) zeroer.device = device zeroer._meta = ( torch.tensor( [backing.data_ptr() + page_offset_el * backing.element_size()], dtype=torch.uint64, device=device, ), torch.tensor([block_stride_el], dtype=torch.int64, device=device), torch.tensor([page_size_el], dtype=torch.int64, device=device), 1, page_size_el, 1, ) zeroer.zero_block_ids([num_blocks - 1]) torch.accelerator.synchronize() expected = torch.ones_like(backing) expected[-1, page_offset_el : page_offset_el + page_size_el] = 0 assert torch.equal(backing, expected) def test_large_dsv4_launch_geometry(monkeypatch): """Keep the failing DSV4 shape efficient and within launch limits.""" device = torch.device("cpu") n_blocks, n_segs = 6870, 181 layer_names = [f"layer.{i}" for i in range(n_segs)] page_sizes = [9344 if i % 2 == 0 else 292 for i in range(n_segs)] spec = SlidingWindowSpec( block_size=1, num_kv_heads=1, head_size=1, dtype=torch.int32, sliding_window=1, ) storages = { name: torch.ones((1, page_size), dtype=torch.int32) for name, page_size in zip(layer_names, page_sizes) } zeroer = KVBlockZeroer( device, attn_groups_iter=[ AttentionGroup( _BlockFirstBackend, # type: ignore[arg-type] [name], spec, group_id, ) for group_id, name in enumerate(layer_names) ], kernel_block_sizes=[1] * n_segs, cache_dtype="auto", static_forward_context={ name: SimpleNamespace(kv_cache=storage) for name, storage in storages.items() }, ) assert zeroer._meta is not None _, _, seg_page_sizes, max_chunks, blk_size, n_segs = zeroer._meta assert seg_page_sizes.tolist() == page_sizes assert (max_chunks, blk_size, n_segs) == (10, 1024, 181) captured_grids = [] class FakeKernel: def __getitem__(self, grid): captured_grids.append(grid) return lambda *args, **kwargs: None monkeypatch.setattr(worker_utils, "_zero_kv_blocks_kernel", FakeKernel()) monkeypatch.setattr( worker_utils, "async_tensor_h2d", lambda values, **kwargs: torch.tensor(values, dtype=torch.int64), ) zeroer.zero_block_ids(list(range(n_blocks))) old_max_chunks = max(page_sizes) // 4 assert math.prod((n_blocks, n_segs, old_max_chunks)) > 2**31 - 1 assert captured_grids == [(n_blocks, n_segs, max_chunks)] @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") def test_warmup_compiles_for_all_block_counts(): """After warmup, no launch should trigger a first-request JIT compile. The block count is carried by the launch grid, so changing it must reuse the warmup's compiled kernel. """ device = torch.device("cuda") num_blocks = 64 page_size_el = 4 storage = torch.ones((num_blocks, page_size_el), dtype=torch.int32, device=device) zeroer = KVBlockZeroer.__new__(KVBlockZeroer) zeroer.device = device zeroer._meta = ( torch.tensor([storage.data_ptr()], dtype=torch.uint64, device=device), torch.tensor([page_size_el], dtype=torch.int64, device=device), torch.tensor([page_size_el], dtype=torch.int64, device=device), 1, # max_chunks page_size_el, # blk_size 1, # n_segs ) def compiled_variants() -> set: return { key for caches in _zero_kv_blocks_kernel.device_caches.values() for key in caches[0] } zeroer.warmup(num_blocks) torch.accelerator.synchronize() warmed = compiled_variants() assert warmed for n_blocks in (1, 2, 3, 16, 32): zeroer.zero_block_ids(list(range(n_blocks))) torch.accelerator.synchronize() assert compiled_variants() == warmed @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") def test_warmup_respects_available_block_count(): """An empty KV cache must not be warmed with out-of-range block IDs.""" device = torch.device("cuda") page_size_el = 4 storage = torch.ones((1, page_size_el), dtype=torch.int32, device=device) zeroer = KVBlockZeroer.__new__(KVBlockZeroer) zeroer.device = device zeroer._meta = ( torch.tensor([storage.data_ptr()], dtype=torch.uint64, device=device), torch.tensor([page_size_el], dtype=torch.int64, device=device), torch.tensor([page_size_el], dtype=torch.int64, device=device), 1, page_size_el, 1, ) zeroer.warmup(0) torch.accelerator.synchronize() assert torch.all(storage == 1)