[v1][Bugfix] Only cache blocks that are not in the prefix cache (#14073)

This commit is contained in:
Chen Zhang
2025-03-01 08:25:54 +00:00
committed by GitHub
parent b28246f6ff
commit b9f1d4294e
2 changed files with 9 additions and 22 deletions
+4 -18
View File
@@ -107,34 +107,20 @@ class BlockPool:
assert prev_block.block_hash is not None
prev_block_hash_value = prev_block.block_hash.hash_value
# Find the first uncached block.
# FIXME: num_cached_blocks should be corrected by the caller
# so this should never happen.
offset = 0
for blk in new_full_blocks:
if blk.block_hash is None:
break
else:
prev_block_hash_value = blk.block_hash.hash_value
offset += 1
else:
# All blocks are cached.
return
for i, blk in enumerate(new_full_blocks[offset:]):
blk_idx = num_cached_blocks + offset + i
for i, blk in enumerate(new_full_blocks):
assert blk.block_hash is None
if i + offset < len(new_block_hashes):
if i < len(new_block_hashes):
# The block hash may already be computed in
# "get_computed_blocks" if the tokens are not generated by
# this request (either the prompt tokens or the previously
# generated tokens with preemption). In this case we simply
# reuse the block hash.
block_hash = new_block_hashes[i + offset]
block_hash = new_block_hashes[i]
else:
# Otherwise compute the block hash and cache it in the request
# in case it will be preempted in the future.
blk_idx = num_cached_blocks + i
start_token_idx = blk_idx * block_size
end_token_idx = (blk_idx + 1) * block_size
block_tokens = request.all_token_ids[