Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion vllm/v1/worker/gpu/block_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ def append_block_ids(
start = self.num_blocks.np[i, req_index] if not overwrite else 0
block_ids = new_block_ids[i]
bpk = self.blocks_per_kv_block[i]
end = start + len(block_ids) * bpk
capacity = self.block_tables[i].gpu.shape[1]
if end > capacity:
raise RuntimeError(
f"Block table write for request {req_index}, group {i} "
f"exceeds row capacity ({end} > {capacity})"
)
if bpk > 1:
block_ids = [b * bpk + k for b in block_ids for k in range(bpk)]
self.block_tables[i].stage_write(req_index, start, block_ids)
self.num_blocks.np[i, req_index] = start + len(block_ids)
self.num_blocks.np[i, req_index] = end

def apply_staged_writes(self) -> None:
if self.num_kv_cache_groups == 0:
Expand Down
13 changes: 9 additions & 4 deletions vllm/v1/worker/gpu/model_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
from vllm.utils.mem_utils import DeviceMemoryProfiler, format_gib
from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE
from vllm.v1.core.sched.output import GrammarOutput, SchedulerOutput
from vllm.v1.kv_cache_interface import KVCacheConfig, MambaSpec
from vllm.v1.kv_cache_interface import AttentionSpec, KVCacheConfig, MambaSpec
from vllm.v1.outputs import (
DraftTokenIds,
ModelRunnerOutput,
Expand Down Expand Up @@ -478,9 +478,14 @@ def initialize_kv_cache(self, kv_cache_config: KVCacheConfig) -> None:
# When using DCP, each request's KV cache is sharded among different ranks.
# As a result, one block on the current rank covers `block_size * cp_size`
# tokens in the full, global (unsharded) sequence.
max_num_blocks = cdiv(
block_table_max_model_len, spec.block_size * self.dcp_size
)
dcp_size = self.dcp_size if isinstance(spec, AttentionSpec) else 1
max_num_blocks = cdiv(block_table_max_model_len, spec.block_size * dcp_size)
# Align to a multiple of (128 / block_size) as required by some attention
# backends such as TRTLLM (#39324)
if spec.block_size <= 128:
alignment = 128 // spec.block_size
max_num_blocks = cdiv(max_num_blocks, alignment) * alignment

# For Mamba/Hybrid Model, KVCaches need extra blocks for speculative tokens
if isinstance(spec, MambaSpec):
max_num_blocks = (
Expand Down
Loading