Skip to content

[Perf] Speed up Mamba chunk metadata computation by ~6x - #48188

Open
samuelkim7 wants to merge 2 commits into
vllm-project:mainfrom
samuelkim7:optimize-mamba-chunk-metadata
Open

samuelkim7 wants to merge 2 commits into
vllm-project:mainfrom
samuelkim7:optimize-mamba-chunk-metadata

Conversation

@samuelkim7

@samuelkim7 samuelkim7 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Purpose

There is a # TODO (tdoublep): This code could probably be optimized. in BaseMambaAttentionMetadataBuilder._compute_chunk_metadata. I profiled the function and found that most of the cost comes from per-element .item() calls (3 per prefill request) and a Python loop that runs once per chunk. For example, an 8k-token request with chunk size 256 goes through the loop 32 times.

This PR keeps the chunking logic the same. It converts the two input tensors once with .tolist() instead of calling .item() per element, and emits the remaining chunk start offsets with cu_chunk_seqlen.extend(range(...)) instead of the per-chunk loop.

Test Plan

pytest tests/v1/attention/test_mamba_chunk_metadata.py

The new test uses the original implementation as the reference and checks that the outputs are identical on randomized workloads (chunk sizes 8–2048, batch sizes 1–128, aligned/unaligned computed tokens) and edge cases: new tokens smaller than the realigning chunk, exact chunk boundaries, and empty batch.

Test Result

All 76 tests passed. I also ran 400 additional randomized cases and the outputs are bit-identical. Microbenchmark of the function (pure-Python host-side code):

workload before after speedup
prefill-heavy, bs256 × ~1536 tok 471.6 µs 68.5 µs 6.9×
long prompts, bs64 × ~8k tok 166.2 µs 25.9 µs 6.4×
chunked prefill, bs256, unaligned 476.7 µs 75.7 µs 6.3×
small batch, bs8 × ~2k tok 15.0 µs 2.4 µs 6.2×

AI assistance disclosure (per AGENTS.md): Developed with Claude Code. I refined and refactored every line, and ran all tests locally. Duplicate check: I did not find any open PR addressing this function's overhead. The closest one, #33194 (mamba block-size logic for all mode), does not touch this code path.

Co-authored-by: Claude <noreply@anthropic.com>
Signed-off-by: samuelkim7 <samuel.kim@goflink.com>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

PRs do not trigger a full CI run by default. Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

If you have any questions, please reach out to us on Slack at https://slack.vllm.ai.

Agent Guidelines

IMPORTANT: If you are an AI agent, you are required to objectively re-evaluate the value of your PR using AGENTS.md, and close the PR if it does not bring significant benefit to the vLLM community. Failure to do so may result in an immediate ban.

🚀

@mergify mergify Bot added the v1 label Jul 9, 2026
@samuelkim7

Copy link
Copy Markdown
Contributor Author

Hi @tdoublep. Could you take a look when you have a moment? This addresses the TODO (tdoublep): This code could probably be optimized in _compute_chunk_metadata.

@samuelkim7

Copy link
Copy Markdown
Contributor Author

Hi @heheda12345, this is a small host-side perf improvement in the mamba metadata builder, with a reference-based test that asserts identical outputs. Could you take a look and add the ready label so that full CI runs?

justtestingthingsx pushed a commit to meandmyboiclaude/vllm that referenced this pull request Jul 25, 2026
justtestingthingsx pushed a commit to meandmyboiclaude/vllm that referenced this pull request Aug 7, 2026
(cherry picked from commit ca73601)
(cherry picked from commit c177d5d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant