main
py 55 lines 1.45 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import pytest
5 import torch
6
7 from vllm.v1.core.kv_cache_utils import check_enough_kv_cache_memory
8 from vllm.v1.kv_cache_interface import FullAttentionSpec
9
10
11 def test_kv_cache_oom_no_memory():
12 from unittest.mock import MagicMock
13
14 config = MagicMock()
15 config.model_config.max_model_len = 2048
16
17 spec = {
18 "layer_0": FullAttentionSpec(
19 block_size=16,
20 num_kv_heads=8,
21 head_size=128,
22 dtype=torch.float16,
23 )
24 }
25
26 with pytest.raises(ValueError):
27 check_enough_kv_cache_memory(config, spec, 0)
28
29
30 def test_kv_cache_oom_insufficient_memory(monkeypatch):
31 from unittest.mock import MagicMock
32
33 config = MagicMock()
34 config.model_config.max_model_len = 2048
35 config.cache_config.block_size = 16
36 config.parallel_config.tensor_parallel_size = 1
37 config.parallel_config.pipeline_parallel_size = 1
38 config.parallel_config.decode_context_parallel_size = 1
39
40 monkeypatch.setattr(
41 "vllm.v1.core.kv_cache_utils.max_memory_usage_bytes",
42 lambda c, s: 100 * 1024**3, # 100 GiB
43 )
44
45 spec = {
46 "layer_0": FullAttentionSpec(
47 block_size=16,
48 num_kv_heads=8,
49 head_size=128,
50 dtype=torch.float16,
51 )
52 }
53
54 with pytest.raises(ValueError):
55 check_enough_kv_cache_memory(config, spec, 1024**3) # 1 GiB