|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +from collections.abc import Iterable |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from vllm.v1.core.kv_cache_utils import BlockHash |
| 10 | +from vllm.v1.kv_offload.abstract import (LoadStoreSpec, OffloadingEvent, |
| 11 | + PrepareStoreOutput) |
| 12 | +from vllm.v1.kv_offload.backends.cpu import CPUBackend |
| 13 | +from vllm.v1.kv_offload.lru_manager import LRUOffloadingManager |
| 14 | +from vllm.v1.kv_offload.mediums import CPULoadStoreSpec |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class ExpectedPrepareStoreOutput: |
| 19 | + block_hashes_to_store: list[int] |
| 20 | + store_block_ids: list[int] |
| 21 | + block_hashes_evicted: list[int] |
| 22 | + |
| 23 | + |
| 24 | +def to_hashes(int_hashes: list[int]) -> list[BlockHash]: |
| 25 | + return [BlockHash(str(i).encode()) for i in int_hashes] |
| 26 | + |
| 27 | + |
| 28 | +def verify_store_output( |
| 29 | + prepare_store_output: Optional[PrepareStoreOutput], |
| 30 | + expected_prepare_store_output: ExpectedPrepareStoreOutput): |
| 31 | + assert prepare_store_output is not None |
| 32 | + assert (prepare_store_output.block_hashes_to_store == to_hashes( |
| 33 | + expected_prepare_store_output.block_hashes_to_store)) |
| 34 | + assert (prepare_store_output.block_hashes_evicted == to_hashes( |
| 35 | + expected_prepare_store_output.block_hashes_evicted)) |
| 36 | + store_spec = prepare_store_output.store_spec |
| 37 | + assert isinstance(store_spec, CPULoadStoreSpec) |
| 38 | + expected_array = np.array(expected_prepare_store_output.store_block_ids, |
| 39 | + dtype=np.int64) |
| 40 | + assert np.array_equal(expected_array, store_spec.block_ids) |
| 41 | + |
| 42 | + |
| 43 | +def verify_load_output(prepare_load_output: LoadStoreSpec, |
| 44 | + expected_prepare_load_output: list[int]): |
| 45 | + assert isinstance(prepare_load_output, CPULoadStoreSpec) |
| 46 | + expected_array = np.array(expected_prepare_load_output, dtype=np.int64) |
| 47 | + assert np.array_equal(expected_array, prepare_load_output.block_ids) |
| 48 | + |
| 49 | + |
| 50 | +def verify_events(events: Iterable[OffloadingEvent], |
| 51 | + block_size: int, |
| 52 | + expected_stores: tuple[set[int], ...] = (), |
| 53 | + expected_evictions: tuple[set[int], ...] = ()): |
| 54 | + stores: list[set[BlockHash]] = [] |
| 55 | + evictions: list[set[BlockHash]] = [] |
| 56 | + for event in events: |
| 57 | + assert event.medium == CPULoadStoreSpec.medium() |
| 58 | + assert event.block_size == block_size |
| 59 | + if event.removed: |
| 60 | + evictions.append(set(event.block_hashes)) |
| 61 | + else: |
| 62 | + stores.append(set(event.block_hashes)) |
| 63 | + |
| 64 | + def to_hash_sets( |
| 65 | + int_sets: tuple[set[int], ...]) -> tuple[set[BlockHash], ...]: |
| 66 | + return tuple([set(to_hashes(list(int_set))) for int_set in int_sets]) |
| 67 | + |
| 68 | + assert tuple(evictions) == to_hash_sets(expected_evictions) |
| 69 | + assert tuple(stores) == to_hash_sets(expected_stores) |
| 70 | + |
| 71 | + |
| 72 | +def test_cpu_manager(): |
| 73 | + """ |
| 74 | + Tests LRUOffloadingManager with a CPUBackend. |
| 75 | + """ |
| 76 | + # initialize a CPU backend with a capacity of 4 blocks |
| 77 | + block_size = 256 |
| 78 | + cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) |
| 79 | + cpu_manager = LRUOffloadingManager(cpu_backend, enable_events=True) |
| 80 | + |
| 81 | + # prepare store [1, 2] |
| 82 | + prepare_store_output = cpu_manager.prepare_store(to_hashes([1, 2])) |
| 83 | + verify_store_output( |
| 84 | + prepare_store_output, |
| 85 | + ExpectedPrepareStoreOutput( |
| 86 | + block_hashes_to_store=[1, 2], |
| 87 | + store_block_ids=[0, 1], |
| 88 | + block_hashes_evicted=[], |
| 89 | + )) |
| 90 | + |
| 91 | + # lookup [1, 2] -> not ready |
| 92 | + assert cpu_manager.lookup(to_hashes([1, 2])) == 0 |
| 93 | + |
| 94 | + # no events so far |
| 95 | + assert list(cpu_manager.take_events()) == [] |
| 96 | + |
| 97 | + # complete store [1, 2] |
| 98 | + cpu_manager.complete_store(to_hashes([1, 2])) |
| 99 | + verify_events(cpu_manager.take_events(), |
| 100 | + block_size=block_size, |
| 101 | + expected_stores=({1, 2}, )) |
| 102 | + |
| 103 | + # lookup [1, 2] |
| 104 | + assert cpu_manager.lookup(to_hashes([1])) == 1 |
| 105 | + assert cpu_manager.lookup(to_hashes([1, 2])) == 2 |
| 106 | + assert cpu_manager.lookup(to_hashes([1, 2, 3])) == 2 |
| 107 | + |
| 108 | + # prepare store [2, 3, 4, 5] -> evicts [1] |
| 109 | + prepare_store_output = cpu_manager.prepare_store(to_hashes([2, 3, 4, 5])) |
| 110 | + verify_store_output( |
| 111 | + prepare_store_output, |
| 112 | + ExpectedPrepareStoreOutput( |
| 113 | + block_hashes_to_store=[3, 4, 5], |
| 114 | + store_block_ids=[2, 3, 0], |
| 115 | + block_hashes_evicted=[1], |
| 116 | + )) |
| 117 | + |
| 118 | + # verify eviction event |
| 119 | + verify_events(cpu_manager.take_events(), |
| 120 | + block_size=block_size, |
| 121 | + expected_evictions=({1}, )) |
| 122 | + |
| 123 | + # prepare store with no space |
| 124 | + assert cpu_manager.prepare_store(to_hashes([1, 6])) is None |
| 125 | + |
| 126 | + # complete store [2, 3, 4, 5] |
| 127 | + cpu_manager.complete_store(to_hashes([2, 3, 4, 5])) |
| 128 | + |
| 129 | + # prepare load [2, 3] |
| 130 | + prepare_load_output = cpu_manager.prepare_load(to_hashes([2, 3])) |
| 131 | + verify_load_output(prepare_load_output, [1, 2]) |
| 132 | + |
| 133 | + # prepare store with no space ([2, 3] is being loaded) |
| 134 | + assert cpu_manager.prepare_store(to_hashes([6, 7, 8])) is None |
| 135 | + |
| 136 | + # complete load [2, 3] |
| 137 | + cpu_manager.complete_load(to_hashes([2, 3])) |
| 138 | + |
| 139 | + # prepare store [6, 7, 8] -> evicts [2, 3, 4] (oldest) |
| 140 | + prepare_store_output = cpu_manager.prepare_store(to_hashes([6, 7, 8])) |
| 141 | + verify_store_output( |
| 142 | + prepare_store_output, |
| 143 | + ExpectedPrepareStoreOutput( |
| 144 | + block_hashes_to_store=[6, 7, 8], |
| 145 | + store_block_ids=[3, 2, 1], |
| 146 | + block_hashes_evicted=[2, 3, 4], |
| 147 | + )) |
| 148 | + |
| 149 | + # complete store [6, 7, 8] |
| 150 | + cpu_manager.complete_store(to_hashes([6, 7, 8])) |
| 151 | + |
| 152 | + # touch [5, 6, 7] (move to end of LRU order) |
| 153 | + cpu_manager.touch(to_hashes([5, 6, 7])) |
| 154 | + |
| 155 | + # prepare store [7, 9] -> evicts [8] (oldest following previous touch) |
| 156 | + prepare_store_output = cpu_manager.prepare_store(to_hashes([9])) |
| 157 | + verify_store_output( |
| 158 | + prepare_store_output, |
| 159 | + ExpectedPrepareStoreOutput( |
| 160 | + block_hashes_to_store=[9], |
| 161 | + store_block_ids=[1], |
| 162 | + block_hashes_evicted=[8], |
| 163 | + )) |
| 164 | + |
| 165 | + # complete store [7, 9] with failure |
| 166 | + cpu_manager.complete_store(to_hashes([7, 9]), success=False) |
| 167 | + |
| 168 | + # assert [7] is still stored, but [9] is not |
| 169 | + assert cpu_manager.lookup(to_hashes([7])) == 1 |
| 170 | + assert cpu_manager.lookup(to_hashes([9])) == 0 |
| 171 | + |
| 172 | + verify_events(cpu_manager.take_events(), |
| 173 | + block_size=block_size, |
| 174 | + expected_stores=({3, 4, 5}, {6, 7, 8}), |
| 175 | + expected_evictions=({2, 3, 4}, {8})) |
0 commit comments