Skip to content

[Refactor] Replace npu_ring_mla with FIA in MLA prefill - #5704

Merged
weijinqian0 merged 1 commit into
vllm-project:mainfrom
LICO1314:refactor/mla-use-fia
Mar 16, 2026
Merged

weijinqian0 merged 1 commit into
vllm-project:mainfrom
LICO1314:refactor/mla-use-fia

Conversation

@LICO1314

@LICO1314 LICO1314 commented Jan 8, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Refactor: Replace npu_ring_mla with FIA in MLA prefill

This PR refactors the MLA (Multi-Layer Attention) prefill implementation by replacing npu_ring_mla with npu_fused_infer_attention_score (FIA) operator, unifying the attention backend with the standard attention implementation.

Key changes:

  1. Core prefill refactoring (mla_v1.py)

    • Replace npu_ring_mla with npu_fused_infer_attention_score in _forward_prefill and _compute_prefill_context
    • Use TND layout with softmax_lse_flag=True for prefill attention
    • Use npu_attention_update to merge multiple chunk outputs with LSE (Log-Sum-Exp)
    • Change attn_mask from get_final_mla_mask() to get_splitfuse_attn_mask() for FIA compatibility
  2. Data type handling

    • Add automatic float16 → bfloat16 conversion (FIA with TND layout only supports bfloat16)
    • Convert output back to original dtype after FIA computation
  3. Metadata optimization

    • Pre-calculate actual_seq_lengths_q in AscendMLAPrefillMetadata
    • Pre-calculate chunk_actual_seq_lengths_kv_list in ChunkedContextMetadata
    • Move torch.cumsum operations from forward pass to metadata building phase
  4. CP compatibility (mla_cp.py)

    • Add _ring_mla_mask_builder to get npu_ring_mla-compatible masks for Context Parallel scenarios
    • Add chunk_actual_seq_lengths_kv_list field to CPChunkedContextMetadata

Why we need it:

  • Backend unification: Aligns MLA prefill with standard attention implementation (attention_v1.py)
  • Better chunked context support: FIA + npu_attention_update provides native LSE-based output merging
  • Future compatibility: Prepares for eventual npu_ring_mla removal across the codebase

Does this PR introduce any user-facing change?

No. This is a pure refactoring with no functional changes - same behavior, unified backend.


@github-actions

github-actions Bot commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:‌‌

  • A PR should do only one thing, smaller PRs enable faster reviews.
  • Every PR should include unit tests and end-to-end tests ‌to ensure it works and is not broken by other future PRs.
  • Write the commit message by fulfilling the PR description to help reviewer and future developers understand.

If CI fails, you can run linting and testing checks locally according Contributing and Testing.

@LICO1314
LICO1314 force-pushed the refactor/mla-use-fia branch from d9c21a8 to ce906fa Compare January 8, 2026 02:18

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the MLA prefill attention path by replacing npu_ring_mla with the more performant npu_fused_infer_attention_score (FIA) for current token self-attention. The changes are well-structured and improve code clarity by unifying the attention backend and adding explanatory comments. I have one suggestion to further improve performance by replacing a Python loop with a more efficient torch.cumsum operation.

Comment thread vllm_ascend/attention/mla_v1.py Outdated
Comment on lines +1072 to +1077
query_lens_list = prefill_meta.query_lens.tolist()
actual_seq_lengths_q = []
cumsum = 0
for qlen in query_lens_list:
cumsum += qlen
actual_seq_lengths_q.append(cumsum)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The calculation of actual_seq_lengths_q using a Python loop is inefficient, especially for a large number of requests. This can be simplified and made more performant by using torch.cumsum directly on the tensor.

Suggested change
query_lens_list = prefill_meta.query_lens.tolist()
actual_seq_lengths_q = []
cumsum = 0
for qlen in query_lens_list:
cumsum += qlen
actual_seq_lengths_q.append(cumsum)
actual_seq_lengths_q = torch.cumsum(prefill_meta.query_lens, dim=0).tolist()

@LICO1314
LICO1314 force-pushed the refactor/mla-use-fia branch 3 times, most recently from fb4026f to b8dc6b4 Compare January 8, 2026 02:29
@jianzs

jianzs commented Jan 8, 2026

Copy link
Copy Markdown
Collaborator

How much performance improvement does this change bring? If it's not significant, I suggest waiting until FIA supports chunk prefill before making this modification. Otherwise, this PR won't achieve your goal and will only add unnecessary code complexity.

@LICO1314
LICO1314 force-pushed the refactor/mla-use-fia branch 14 times, most recently from be27e3e to 29ddf38 Compare January 9, 2026 07:43
@LICO1314 LICO1314 changed the title [Refactor] Replace npu_ring_mla with FIA in MLA prefill attention [Refactor] Replace npu_ring_mla with FIA in MLA prefill Jan 9, 2026
@LICO1314
LICO1314 force-pushed the refactor/mla-use-fia branch 6 times, most recently from d1ad8fe to dd087fd Compare January 12, 2026 08:25
@LICO1314
LICO1314 force-pushed the refactor/mla-use-fia branch 3 times, most recently from ee075a8 to f3b1f61 Compare January 16, 2026 07:12
@weijinqian0
weijinqian0 self-requested a review January 16, 2026 12:12
Comment thread vllm_ascend/attention/mla_v1.py Outdated
query_lens = prefill_metadata.query_lens
if isinstance(query_lens, list):
query_lens = torch.tensor(query_lens)
actual_seq_lengths_q = torch.cumsum(query_lens, dim=0).tolist()

@weijinqian0 weijinqian0 Jan 16, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why add this? This parameter can be added in AscendMlaMetadataBuilder like decode_metdata.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@LICO1314
LICO1314 force-pushed the refactor/mla-use-fia branch 3 times, most recently from ed1768b to c6e7132 Compare January 19, 2026 02:19
@LICO1314
LICO1314 force-pushed the refactor/mla-use-fia branch 11 times, most recently from 52ecb32 to e07ea6d Compare January 20, 2026 01:40
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@jianzs

jianzs commented Jan 30, 2026

Copy link
Copy Markdown
Collaborator

@LICO1314 Please rebase your code

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Use FIA to replace npu_ring_mla in MLA prefill path and keep CP-related behavior aligned after rebasing on upstream/main. Precision test results: DeepSeek GSM8K 96.51, AIME24 93.3.

Made-with: Cursor
Signed-off-by: lico67373 <918688502@qq.com>
Made-with: Cursor
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.

4 participants