[Perf] Batch Mamba2 prefill SSM state saves, removing GPU<->CPU syncs (#49371)
Signed-off-by: samuelkim7 <samuelmwkim@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -156,7 +156,10 @@ class Mamba2AttentionMetadataBuilder(
|
||||
# data so it needs no D2H. `seq_lens_cpu_upper_bound` is precise
|
||||
# for prefill rows, which is all this slice covers.
|
||||
num_computed_tokens_p_cpu, _ = self._prefill_cpu_metadata(
|
||||
common, common_attn_metadata
|
||||
common_attn_metadata,
|
||||
common.num_reqs,
|
||||
common.num_prefills,
|
||||
common.num_decode_tokens,
|
||||
)
|
||||
prep_initial_states = bool((num_computed_tokens_p_cpu > 0).any())
|
||||
|
||||
|
||||
@@ -70,6 +70,9 @@ class BaseMambaAttentionMetadata:
|
||||
# last_chunk_indices_p is a tensor of shape (batch,) that contains the
|
||||
# index of the last chunk for every sequence in the (prefill) batch.
|
||||
last_chunk_indices_p: torch.Tensor | None = None
|
||||
# Number of block-aligned SSM states the prefill path writes, i.e.
|
||||
# sum(block_idx_last_scheduled_token_p - block_idx_first_scheduled_token_p).
|
||||
num_state_writes_p: int = 0
|
||||
|
||||
# The following attributes are for triton implementation of causal_conv1d
|
||||
nums_dict: dict | None = None
|
||||
@@ -348,8 +351,10 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
|
||||
def _prefill_cpu_metadata(
|
||||
self,
|
||||
common: M,
|
||||
common_attn_metadata: CommonAttentionMetadata,
|
||||
num_reqs: int,
|
||||
num_prefills: int,
|
||||
num_decode_tokens: int,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Prefill context lengths and query offsets, from CPU data only.
|
||||
|
||||
@@ -361,11 +366,9 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
"""
|
||||
seq_lens_cpu = common_attn_metadata.seq_lens_cpu_upper_bound
|
||||
assert seq_lens_cpu is not None
|
||||
num_reqs = common.num_reqs
|
||||
num_prefills = common.num_prefills
|
||||
query_start_loc_p_cpu = (
|
||||
common_attn_metadata.query_start_loc_cpu[-num_prefills - 1 :]
|
||||
- common.num_decode_tokens
|
||||
- num_decode_tokens
|
||||
)
|
||||
prefill_query_lens_cpu = query_start_loc_p_cpu[1:] - query_start_loc_p_cpu[:-1]
|
||||
num_computed_tokens_p_cpu = (
|
||||
@@ -385,7 +388,10 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
num_prefills = common.num_prefills
|
||||
|
||||
num_computed_tokens_p_cpu, query_start_loc_p_cpu = self._prefill_cpu_metadata(
|
||||
common, common_attn_metadata
|
||||
common_attn_metadata,
|
||||
common.num_reqs,
|
||||
num_prefills,
|
||||
common.num_decode_tokens,
|
||||
)
|
||||
|
||||
cu_chunk_seqlen, seq_idx, last_chunk_indices = self._compute_chunk_metadata(
|
||||
@@ -533,6 +539,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
block_idx_last_computed_token = None
|
||||
block_idx_last_scheduled_token = None
|
||||
block_idx_last_scheduled_token_prev_step = None
|
||||
num_state_writes_p = 0
|
||||
|
||||
# for causal_conv1d
|
||||
nums_dict, batch_ptr, token_chunk_offset_ptr = None, None, None
|
||||
@@ -625,6 +632,23 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
num_reqs - num_prefills : num_reqs
|
||||
]
|
||||
|
||||
# How many block-aligned states prefill will write: the same
|
||||
# (block_idx_last - block_idx_first) as above, from CPU data so
|
||||
# the mixer needs no D2H read. The -1 in both indices cancels.
|
||||
block_size = self.kv_cache_spec.block_size
|
||||
seq_lens_p_cpu = seq_lens_cpu[num_reqs - num_prefills : num_reqs]
|
||||
num_computed_tokens_p_cpu, _ = self._prefill_cpu_metadata(
|
||||
common_attn_metadata, num_reqs, num_prefills, num_decode_tokens
|
||||
)
|
||||
num_state_writes_p = int(
|
||||
(
|
||||
(seq_lens_p_cpu + block_size - 1) // block_size
|
||||
- (num_computed_tokens_p_cpu + block_size) // block_size
|
||||
)
|
||||
.clamp(min=0)
|
||||
.sum()
|
||||
)
|
||||
|
||||
if self.use_replayssm and not self.use_flashinfer_replayssm and num_decodes > 0:
|
||||
decode_base_cpu = common_attn_metadata.replayssm_decode_base_cpu
|
||||
seq_lens_cpu = common_attn_metadata.seq_lens_cpu_upper_bound
|
||||
@@ -728,6 +752,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
query_start_loc_d=query_start_loc_d,
|
||||
block_idx_last_scheduled_token=block_idx_last_scheduled_token,
|
||||
block_idx_first_scheduled_token_p=block_idx_first_scheduled_token_p,
|
||||
num_state_writes_p=num_state_writes_p,
|
||||
block_idx_last_computed_token=block_idx_last_computed_token,
|
||||
block_idx_last_scheduled_token_prev_step=(
|
||||
block_idx_last_scheduled_token_prev_step
|
||||
|
||||
Reference in New Issue
Block a user