[V1] [Hybrid] Some additional clean-up in Mamba2 prefix caching (#26222)

Signed-off-by: Thomas Parnell <tpa@zurich.ibm.com>
This commit is contained in:
Thomas Parnell
2025-10-06 10:40:30 +08:00
committed by GitHub
parent d3c84297c3
commit 778f554157
4 changed files with 171 additions and 136 deletions
+52 -55
View File
@@ -122,11 +122,10 @@ class Mamba2AttentionMetadata:
last_chunk_indices_p: Optional[torch.Tensor]
state_indices_tensor: torch.Tensor # shape: [batch,]
current_last_idx: torch.Tensor
current_first_idx_p: torch.Tensor
last_state_idx: torch.Tensor
context_lens_p: torch.Tensor
last_computed_offset_p: torch.Tensor
block_idx_last_scheduled_token: torch.Tensor # shape: [batch,]
block_idx_first_scheduled_token_p: torch.Tensor # shape: [batch,]
block_idx_last_computed_token: torch.Tensor # shape: [batch,]
num_computed_tokens_p: torch.Tensor # shape: [batch,]
# The following attributes are for triton implementation of causal_conv1d
nums_dict: Optional[dict] = None
@@ -160,12 +159,12 @@ class Mamba2AttentionMetadataBuilder(
dtype=torch.int32,
device=device,
)
self.current_last_idx = torch.empty(
self.block_idx_last_scheduled_token = torch.empty(
(self.decode_cudagraph_max_bs,),
dtype=torch.int32,
device=device,
)
self.last_state_idx = torch.empty(
self.block_idx_last_computed_token = torch.empty(
(self.decode_cudagraph_max_bs,),
dtype=torch.int32,
device=device,
@@ -192,43 +191,38 @@ class Mamba2AttentionMetadataBuilder(
# for causal_conv1d
nums_dict, batch_ptr, token_chunk_offset_ptr = None, None, None
context_lens, context_lens_p = None, None
current_first_idx, current_first_idx_p = None, None
last_computed_offset, last_computed_offset_p = None, None
num_computed_tokens, num_computed_tokens_p = None, None
block_idx_first_scheduled_token = None
block_idx_first_scheduled_token_p = None
if self.vllm_config.cache_config.enable_prefix_caching:
# Return a tensor of shape (#requests, #max blocks)
state_indices_tensor = common_attn_metadata.block_table_tensor
# Additional cache-related varaiables:
mamba_block_size = self.kv_cache_spec.block_size
seq_lens_pending = (
torch.roll(common_attn_metadata.query_start_loc, -1, -1)
- common_attn_metadata.query_start_loc
)[:-1]
context_lens = common_attn_metadata.seq_lens - seq_lens_pending
last_computed_offset = context_lens % mamba_block_size
# Indices: last_computed <= current_first <= current_last
# Cases:
# last_computed == current_first if last state was partially
# computed and needs to be updated
# current_first == current_last if no block crossing occurs, and
# only one state will be stored
# 0th based indexing leads to "-1" -> e.g. 16 computed -> state[15]:
current_last_idx = (
cdiv(context_lens + seq_lens_pending, mamba_block_size) - 1
num_computed_tokens = common_attn_metadata.num_computed_tokens_cpu.to(
self.device
)
# Block index of the last computed token
block_idx_last_computed_token = (
cdiv(num_computed_tokens, mamba_block_size) - 1
)
# which is <= block index for the first scheduled token
block_idx_first_scheduled_token = (
cdiv(num_computed_tokens + 1, mamba_block_size) - 1
)
# which is <= block index of the last scheduled token
block_idx_last_scheduled_token = (
cdiv(common_attn_metadata.seq_lens, mamba_block_size) - 1
)
current_first_idx = cdiv(context_lens + 1, mamba_block_size) - 1
last_state_idx = cdiv(context_lens, mamba_block_size) - 1
# -1 in case it's non-computed and causes later issues with indexing
last_state_idx = last_state_idx.clamp(min=0)
block_idx_last_computed_token = block_idx_last_computed_token.clamp(min=0)
else:
# Always return just a single block per each request:
state_indices_tensor = common_attn_metadata.block_table_tensor[:, 0]
# Additional cache-related varaiables:
current_last_idx = None
last_state_idx = None
block_idx_last_scheduled_token = None
block_idx_last_computed_token = None
num_decodes, num_prefills, num_decode_tokens, num_prefill_tokens = (
split_decodes_and_prefills(
@@ -256,18 +250,15 @@ class Mamba2AttentionMetadataBuilder(
)
if self.vllm_config.cache_config.enable_prefix_caching:
assert context_lens is not None
context_lens_p = context_lens[num_reqs - num_prefills : num_reqs]
assert last_computed_offset is not None
last_computed_offset_p = last_computed_offset[
assert num_computed_tokens is not None
num_computed_tokens_p = num_computed_tokens[
num_reqs - num_prefills : num_reqs
]
assert current_first_idx is not None
current_first_idx_p = current_first_idx[
assert block_idx_first_scheduled_token is not None
block_idx_first_scheduled_token_p = block_idx_first_scheduled_token[
num_reqs - num_prefills : num_reqs
]
num_computed_tokens_p = common_attn_metadata.num_computed_tokens_cpu[
num_computed_tokens_p_cpu = common_attn_metadata.num_computed_tokens_cpu[
num_reqs - num_prefills : num_reqs
]
query_start_loc_p_cpu = (
@@ -290,7 +281,7 @@ class Mamba2AttentionMetadataBuilder(
last_chunk_indices = []
seqlen_pos = 0
for req_idx in range(num_prefills):
this_num_computed = num_computed_tokens_p[req_idx].item()
this_num_computed = num_computed_tokens_p_cpu[req_idx].item()
this_new_tokens = (
query_start_loc_p_cpu[req_idx + 1].item()
- query_start_loc_p_cpu[req_idx].item()
@@ -338,7 +329,10 @@ class Mamba2AttentionMetadataBuilder(
compute_causal_conv1d_metadata(query_start_loc_p)
)
elif num_decodes <= self.decode_cudagraph_max_bs:
elif (
num_decodes <= self.decode_cudagraph_max_bs
and self.compilation_config.full_cuda_graph
):
# Pad state tensor for CUDA graph
num_input_tokens = self.vllm_config.pad_for_cudagraph(num_decodes)
self.state_indices_tensor[:num_decodes].copy_(
@@ -348,17 +342,21 @@ class Mamba2AttentionMetadataBuilder(
state_indices_tensor[num_decodes:] = PAD_SLOT_ID
if self.vllm_config.cache_config.enable_prefix_caching:
self.current_last_idx[:num_decodes].copy_(
current_last_idx, non_blocking=True
self.block_idx_last_scheduled_token[:num_decodes].copy_(
block_idx_last_scheduled_token, non_blocking=True
)
current_last_idx = self.current_last_idx[:num_input_tokens]
current_last_idx[num_decodes:] = 0
block_idx_last_scheduled_token = self.block_idx_last_scheduled_token[
:num_input_tokens
]
block_idx_last_scheduled_token[num_decodes:] = 0
self.last_state_idx[:num_decodes].copy_(
last_state_idx, non_blocking=True
self.block_idx_last_computed_token[:num_decodes].copy_(
block_idx_last_computed_token, non_blocking=True
)
last_state_idx = self.last_state_idx[:num_input_tokens]
last_state_idx[num_decodes:] = 0
block_idx_last_computed_token = self.block_idx_last_computed_token[
:num_input_tokens
]
block_idx_last_computed_token[num_decodes:] = 0
attn_metadata = Mamba2AttentionMetadata(
num_prefills=num_prefills,
@@ -377,10 +375,9 @@ class Mamba2AttentionMetadataBuilder(
nums_dict=nums_dict,
batch_ptr=batch_ptr,
token_chunk_offset_ptr=token_chunk_offset_ptr,
current_last_idx=current_last_idx,
current_first_idx_p=current_first_idx_p,
last_state_idx=last_state_idx,
context_lens_p=context_lens_p,
last_computed_offset_p=last_computed_offset_p,
block_idx_last_scheduled_token=block_idx_last_scheduled_token,
block_idx_first_scheduled_token_p=block_idx_first_scheduled_token_p,
block_idx_last_computed_token=block_idx_last_computed_token,
num_computed_tokens_p=num_computed_tokens_p,
)
return attn_metadata
+1 -2
View File
@@ -584,8 +584,7 @@ class MambaManager(SingleTypeKVCacheManager):
# hit_length = len(hit_blocks_other_attn[0])
# * self.other_block_size
# so we insert dummy blocks at the beginning:
if i > 0:
computed.extend([block_pool.null_block] * i)
computed.extend([block_pool.null_block] * i)
computed.append(cached)
break # we just need the last match - early stopping