[Perf] Optimize clamp to clamp_ (#48143)

Signed-off-by: yewentao256 <zhyanwentao@126.com>
Signed-off-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Wentao Ye
2026-07-16 18:41:07 -04:00
committed by GitHub
co-authored by mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 2cab53ddee
commit f17be06fbe
7 changed files with 48 additions and 32 deletions
+24 -12
View File
@@ -341,23 +341,36 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
num_computed_tokens = common_attn_metadata.compute_num_computed_tokens()
# Block index of the last computed token
block_idx_last_computed_token = cdiv(num_computed_tokens, mamba_block_size) - 1
block_idx_last_computed_token = (
torch.div(
num_computed_tokens + mamba_block_size - 1,
mamba_block_size,
rounding_mode="floor",
)
- 1
)
# which is <= block index for the first scheduled token
block_idx_first_scheduled_token = (
cdiv(num_computed_tokens + 1, mamba_block_size) - 1
torch.div(
num_computed_tokens + mamba_block_size,
mamba_block_size,
rounding_mode="floor",
)
- 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
torch.div(
common_attn_metadata.seq_lens + mamba_block_size - 1,
mamba_block_size,
rounding_mode="floor",
)
- 1
)
# -1 in case it's non-computed and causes later issues with indexing
block_idx_last_computed_token = torch.clamp(
block_idx_last_computed_token, min=0
)
block_idx_last_computed_token.clamp_(min=0)
# -1 in the case we have a padded request (0 seq-len)
block_idx_last_scheduled_token = torch.clamp(
block_idx_last_scheduled_token, min=0
)
block_idx_last_scheduled_token.clamp_(min=0)
return (
block_idx_last_computed_token,
@@ -447,9 +460,8 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
common_attn_metadata, mamba_block_size
)
if self.use_spec_decode and prev_last_scheduled_idx is not None:
fallback = torch.clamp(
(num_computed_tokens - 1) // mamba_block_size, min=0
)
fallback = (num_computed_tokens - 1) // mamba_block_size
fallback.clamp_(min=0)
block_idx_last_scheduled_token_prev_step = torch.where(
prev_last_scheduled_idx >= 0,
prev_last_scheduled_idx,
+2 -3
View File
@@ -593,9 +593,8 @@ class AiterFlashAttentionMetadataBuilder(
chunk_ends = torch.min(
computed_kv_lens.unsqueeze(0), chunk_starts + max_context_chunk
)
chunk_seq_lens = (chunk_ends - chunk_starts).clamp(
min=0
) # [num_chunks, num_extends]
chunk_seq_lens = chunk_ends - chunk_starts
chunk_seq_lens.clamp_(min=0) # [num_chunks, num_extends]
cu_seq_lens_cpu = torch.zeros(
[num_chunks, num_extends + 1], dtype=torch.int32, pin_memory=True
)
+2 -4
View File
@@ -950,10 +950,8 @@ def mamba_get_block_table_tensor(
assert isinstance(kv_cache_spec, MambaSpec)
# NOTE: For 0-length requests in CUDA graph, use a start_index of 0
# to handle the invalid block table.
start_indices = torch.clamp(
(seq_lens - 1) // kv_cache_spec.block_size,
min=0,
)
start_indices = (seq_lens - 1) // kv_cache_spec.block_size
start_indices.clamp_(min=0)
# Use int32 for arithmetic to avoid dtype promotion overhead,
# then convert to int64 for gather (which requires Long indices)
offsets = torch.arange(