A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 14, 05:30 UTC
[kv cache] update num_free_blocks in the end (#24228)
Signed-off-by: Andy Xie <andy.xning@gmail.com>
Ning Xie committed
Sep 15, 2025 at 13:15 UTC
3f3313981c0bb3e2366c56737b82f8a36aa3f91e
2 files changed
+14
-1
tests/v1/core/test_kv_cache_utils.py
+12
@@ -243,6 +243,18 @@ def test_free_kv_cache_block_queue_append_n():
243
assert blocks[3].next_free_block is queue.fake_free_list_tail
244
assert queue.fake_free_list_tail.prev_free_block is blocks[3]
245
246
+ # Create an empty FreeKVCacheBlockQueue
247
+ invalid_queue = FreeKVCacheBlockQueue([])
248
+ # set prev_free_block to None and this will cause assertation in append_n
249
+ invalid_queue.fake_free_list_tail.prev_free_block = None
250
+ with pytest.raises(AssertionError):
251
+ # Append 1 block
252
+ # fake_head->fake_tail
253
+ invalid_queue.append_n(blocks[0:1])
254
+ assert invalid_queue.num_free_blocks == 0
255
+ assert (invalid_queue.fake_free_list_head.next_free_block ==
256
+ invalid_queue.fake_free_list_tail)
257
+
258
259
def test_free_kv_cache_block_queue_popleft_n():
260
blocks = [KVCacheBlock(block_id=i) for i in range(6)]
vllm/v1/core/kv_cache_utils.py
+2
-1
@@ -370,7 +370,6 @@ class FreeKVCacheBlockQueue:
370
"""
371
if len(blocks) == 0:
372
return
373
- self.num_free_blocks += len(blocks)
373
374
last_block = self.fake_free_list_tail.prev_free_block
375
assert last_block is not None, (
@@ -385,6 +384,8 @@ class FreeKVCacheBlockQueue:
384
last_block.next_free_block = self.fake_free_list_tail
385
self.fake_free_list_tail.prev_free_block = last_block
386
387
+ self.num_free_blocks += len(blocks)
388
+
389
def get_all_free_blocks(self) -> list[KVCacheBlock]:
390
"""Get all free blocks in the free list. Mainly used for testing.
391