Skip to content

[BugFix] bound FlashMLA sparse decode intermediate tensors size - #49357

Open
cjackal wants to merge 5 commits into
vllm-project:mainfrom
cjackal:flashmla-fp8-subchunk
Open

[BugFix] bound FlashMLA sparse decode intermediate tensors size#49357
cjackal wants to merge 5 commits into
vllm-project:mainfrom
cjackal:flashmla-fp8-subchunk

Conversation

@cjackal

@cjackal cjackal commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Purpose

Fix #44545 - not in an ideal way though.

When FlashMLA sparse kernel is selected with FP8 KV cache dtype, FlashMLA sparse decode kernel(https://github.com/vllm-project/FlashMLA/blob/a8f794d1251cbfd88a5011445dd5582289c727e4/csrc/api/sparse_decode.h#L184) is called in mixed batch mode. This kernel internally allocates intermediate tensors (o_accum) on call, which quickly dominates the usual unallocated workspace size (~2GB per the comment in flashmla_sparse.py) for long prefill:

For 32k input on H200, the bytesize of o_accum is

# b = 1
# num_sm_parts = 1
# s_q = 32768
# h_q = 64
# d_v = 512
(b + num_sm_parts) * s_q * h_q * d_v * 4 bytes = 8 GiB

For BF16 KV cache, a different kernel (FlashMLA sparse prefill kernel - https://github.com/vllm-project/FlashMLA/blob/a8f794d1251cbfd88a5011445dd5582289c727e4/csrc/api/sparse_fwd.h#L101) is called, which does not allocate a massive VRAM on kernel call and thus does not suffer an OOM contrary to the FP8 code branch. (Note the additional d_v factor in the formular for o_accum, no other scratch tensors in FlashMLA have this factor thus negligible in terms of VRAM occupancy)

Hence we chunk the mixed-batch input into manageable bytes (VLLM_FLASHMLA_SPARSE_MAX_SCRATCH_MB) and loop over the chunks to aggregate the output attention tensor, which is in the same spirit as the indexer budget management logic in #36178.

Due to the mechanism (looping the kernel over sub-chunks instead of single big shot), it may incur a throughput penalty as a tradeoff, but I have no better idea yet.

As the context window for MLA model families keep growing, FP8 KV cache is kind of inevitable, so I think this PR can work as a bandaid.

Test Plan

No OOM for the reproducer in #44545

Test Result

pass


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

@cjackal
cjackal requested a review from pavanimajety as a code owner July 21, 2026 18:06

@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.

@mergify mergify Bot added the v1 label Jul 21, 2026
@cjackal cjackal changed the title bound FlashMLA sparse decode intermediate tensors size [BugFix] bound FlashMLA sparse decode intermediate tensors size Jul 21, 2026
@mergify mergify Bot added the bug Something isn't working label Jul 21, 2026
@cjackal

cjackal commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

@MatthewBonanni ping in case if this issue is still of interest to you.

@mergify

mergify Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @cjackal.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Aug 13, 2026
@cjackal
cjackal force-pushed the flashmla-fp8-subchunk branch from 44fdd0e to de7fcad Compare August 13, 2026 13:45
Comment thread vllm/envs.py
@mergify mergify Bot removed the needs-rebase label Aug 13, 2026
Leoyzen added a commit to Leoyzen/vllm that referenced this pull request Aug 14, 2026
…ap long lines

- mla_attention.py: add get_current_vllm_config_or_none, cp_lse_ag_out_rs,
  dcp_a2a_lse_reduce imports for _dcp_merge_and_v_up_proj (vllm-project#48196); drop
  unused finalize_mla_pcp_decode import
- test_mtp.py: add missing torch import for prefix-cache test (vllm-project#50897)
- cli_args.py: wrap >88-col lines (vllm-project#50668)
- flashmla_sparse.py: wrap long comment (vllm-project#49357)
- flashinfer_mla_sparse.py: drop unused np_to_pinned_tensor import (vllm-project#48196)
- kv_cache_coordinator.py: fix factory-function param indentation (vllm-project#50897)

Co-authored-by: Sisyphus <sisyphus@openci.dev>
@mergify

mergify Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @cjackal.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Aug 19, 2026
@cjackal
cjackal force-pushed the flashmla-fp8-subchunk branch from e40912e to 7b1fc6d Compare August 19, 2026 17:55
@mergify mergify Bot removed the needs-rebase label Aug 19, 2026
Signed-off-by: cjackal <44624812+cjackal@users.noreply.github.com>
Signed-off-by: cjackal <44624812+cjackal@users.noreply.github.com>
Signed-off-by: cjackal <44624812+cjackal@users.noreply.github.com>
Signed-off-by: cjackal <44624812+cjackal@users.noreply.github.com>
Signed-off-by: cjackal <44624812+cjackal@users.noreply.github.com>
@cjackal
cjackal force-pushed the flashmla-fp8-subchunk branch from 4e11c2a to e2236c6 Compare August 20, 2026 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working v1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: GLM-5 FP8 OOM for long inputs at flash_mla_cuda.sparse_decode_fwd on H200

1 participant