main
py 65 lines 1.98 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Unit tests for CUDA kernels in cache_kernels.cu."""
4
5 import pytest
6 import torch
7
8 try:
9 from vllm import _custom_ops as ops
10 except ImportError:
11 pytest.skip(
12 "Could not import vllm._custom_ops. (pip install -e .)", allow_module_level=True
13 )
14
15
16 @pytest.mark.skipif(torch.accelerator.device_count() < 1, reason="Need CUDA device")
17 def test_gather_cache_oob():
18 """Tests for OOB read in gather_and_maybe_dequant_cache (Issue #27909).
19 This test constructs a boundary case identified in the issue where
20 seq_starts causes the block_table offset to read out of bounds.
21 """
22 block_size = 64
23 # The kernel only supports the MLA entry sizes.
24 entry_size = 576
25
26 block_table = torch.tensor([[1, 2]], dtype=torch.int32, device="cuda")
27
28 # This will result in offset = 128 / block_size = 128 / 64 = 2
29 # This will cause the kernel to try to read from
30 # block_table[0, 2], but its size is only 2.
31 seq_starts = torch.tensor([128], dtype=torch.int32, device="cuda")
32
33 seq_len = 65
34 cu_seq_lens = torch.tensor([0, seq_len], dtype=torch.int32, device="cuda")
35 token_to_seq = torch.zeros(seq_len, dtype=torch.int32, device="cuda")
36
37 # src_cache: [num_blocks, block_size, entry_size]
38 num_blocks = 5
39 src_cache = torch.randn(
40 (num_blocks, block_size, entry_size), dtype=torch.float16, device="cuda"
41 )
42
43 dst = torch.empty((seq_len, entry_size), dtype=torch.float16, device="cuda")
44
45 scale = torch.tensor([1.0], dtype=torch.float32, device="cuda")
46
47 # Calling the C++ function gather_and_maybe_dequant_cache
48 ops.gather_and_maybe_dequant_cache(
49 src_cache,
50 dst,
51 block_table,
52 cu_seq_lens,
53 token_to_seq,
54 seq_len,
55 "auto", # kv_cache_dtype
56 scale,
57 seq_starts,
58 )
59
60 torch.accelerator.synchronize()
61 assert True
62
63
64 if __name__ == "__main__":
65 pytest.main([__file__])