Skip to content

Commit 420caf7

Browse files
authored
[UT] Add ut for none hash (#17892)
Signed-off-by: Andy Xie <[email protected]>
1 parent 4f07a64 commit 420caf7

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

tests/v1/core/test_kv_cache_utils.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: Apache-2.0
2+
import importlib
23

34
import pytest
45
import torch
@@ -10,8 +11,7 @@
1011
from vllm.v1.core.kv_cache_manager import KVCacheManager
1112
# disable yapf here as it formats differently than isort such that both fail
1213
# yapf: disable
13-
from vllm.v1.core.kv_cache_utils import (NONE_HASH, BlockHashType,
14-
FreeKVCacheBlockQueue, KVCacheBlock,
14+
from vllm.v1.core.kv_cache_utils import (FreeKVCacheBlockQueue, KVCacheBlock,
1515
PrefixCachingMetrics,
1616
estimate_max_model_len,
1717
generate_block_hash_extra_keys,
@@ -65,13 +65,29 @@ def new_kv_cache_spec(block_size=16,
6565
sliding_window=sliding_window)
6666

6767

68-
def test_none_hash():
69-
assert NONE_HASH is not None
70-
assert isinstance(NONE_HASH, int)
71-
assert NONE_HASH != 0
68+
def test_none_hash(monkeypatch):
69+
import vllm.v1.core.kv_cache_utils
70+
71+
# case 1: PYTHONHASHSEED is not set, use random
72+
with monkeypatch.context() as m:
73+
m.delenv('PYTHONHASHSEED', raising=False)
74+
reloaded_kv_cache_utils = importlib.reload(vllm.v1.core.kv_cache_utils)
75+
assert reloaded_kv_cache_utils.NONE_HASH is not None
76+
assert isinstance(reloaded_kv_cache_utils.NONE_HASH, int)
77+
assert reloaded_kv_cache_utils.NONE_HASH != 0
78+
79+
# case 2: PYTHONHASHSEED is set, use the seed
80+
with monkeypatch.context() as m:
81+
m.setenv('PYTHONHASHSEED', 'python hash seed')
82+
reloaded_kv_cache_utils = importlib.reload(vllm.v1.core.kv_cache_utils)
83+
assert reloaded_kv_cache_utils.NONE_HASH is not None
84+
assert isinstance(reloaded_kv_cache_utils.NONE_HASH, int)
85+
assert sha256('python hash seed') == reloaded_kv_cache_utils.NONE_HASH
7286

7387

7488
def test_kv_cache_block():
89+
import vllm.v1.core.kv_cache_utils
90+
7591
# Test KVCacheBlock initialization
7692
block = KVCacheBlock(block_id=0)
7793
assert block.block_id == 0
@@ -85,7 +101,8 @@ def test_kv_cache_block():
85101
assert block.ref_cnt == 0
86102

87103
# Test block hash setting and resetting
88-
block_hash = BlockHashType(hash_value=123, token_ids=(1, 2, 3))
104+
block_hash = vllm.v1.core.kv_cache_utils.BlockHashType(hash_value=123,
105+
token_ids=(1, 2, 3))
89106
block.block_hash = block_hash
90107
assert block.block_hash == block_hash
91108

@@ -259,13 +276,14 @@ def test_generate_block_hash_extra_keys_cache_salt():
259276

260277
@pytest.mark.parametrize("hash_fn", [sha256, hash])
261278
def test_hash_block_tokens(hash_fn):
279+
import vllm.v1.core.kv_cache_utils
262280
parent_block_hash = 123
263281
curr_block_token_ids = (1, 2, 3)
264282
extra_keys = ("key1", "key2")
265283

266284
block_hash = hash_block_tokens(hash_fn, parent_block_hash,
267285
curr_block_token_ids, extra_keys)
268-
assert isinstance(block_hash, BlockHashType)
286+
assert isinstance(block_hash, vllm.v1.core.kv_cache_utils.BlockHashType)
269287
assert block_hash.hash_value == hash_fn(
270288
(parent_block_hash, curr_block_token_ids, extra_keys))
271289
assert block_hash.token_ids == curr_block_token_ids
@@ -274,6 +292,7 @@ def test_hash_block_tokens(hash_fn):
274292

275293
@pytest.mark.parametrize("hash_fn", [sha256, hash])
276294
def test_hash_request_tokens(hash_fn):
295+
import vllm.v1.core.kv_cache_utils
277296
request = make_request(
278297
request_id=0,
279298
prompt_token_ids=[_ for _ in range(6)],
@@ -288,8 +307,10 @@ def test_hash_request_tokens(hash_fn):
288307
block_hashes = hash_request_tokens(hash_fn, block_size, request)
289308

290309
assert len(block_hashes) == 2
291-
assert isinstance(block_hashes[0], BlockHashType)
292-
assert isinstance(block_hashes[1], BlockHashType)
310+
assert isinstance(block_hashes[0],
311+
vllm.v1.core.kv_cache_utils.BlockHashType)
312+
assert isinstance(block_hashes[1],
313+
vllm.v1.core.kv_cache_utils.BlockHashType)
293314

294315
# Check the first block
295316
assert block_hashes[0].token_ids == (0, 1, 2)

0 commit comments

Comments
 (0)