Skip to content

feat: optimize MHC post + HC head + RMSNorm fusions for DeepSeek V4 - #51244

Open
OLIVER-XYP wants to merge 1 commit into
vllm-project:mainfrom
OLIVER-XYP:mhc-optimized-fusions
Open

OLIVER-XYP wants to merge 1 commit into
vllm-project:mainfrom
OLIVER-XYP:mhc-optimized-fusions

Conversation

@OLIVER-XYP

@OLIVER-XYP OLIVER-XYP commented Aug 6, 2026

Copy link
Copy Markdown

Summary

Optimize MHC post + HC head + RMSNorm operations for DeepSeek V4 via kernel
fusion. Three new TileLang kernels are introduced:

  1. mhc_post_hc_head_fused — 2-way fusion: MHC post + HC head
  2. mhc_post_hc_head_norm_fused — 3-way fusion: MHC post + HC head + RMSNorm
  3. mhc_post_mean_fused — MHC post + mean reduction for aux layers

All fusions are gated behind the VLLM_MHC_FUSED_KERNELS env var (default:
enabled). When TileLang is not available, the code gracefully falls back to the
original separate kernel calls.

Key Design Decisions

  • PP > 1 safe: The 3-way fusion is only applied on the last PP rank
  • MTP draft model compatible: Optional mtp_buffer output stashes the
    pre-hc_head residual for the MTP draft model
  • Zero overhead when disabled: The flag is checked at import time

End-to-End Benchmark Results

Hardware: 8 × H20 (NVIDIA)
Model: DeepSeek-V4-Flash (FP8)
Test Config: TP=8, EP enabled, 100 random prompts, input length=20000,
output length=2000, max concurrency=16, random dataset, avg 5 runs.

Metric (Avg 5) After Optimization Before Optimization Improvement
Benchmark duration (s) 360.69 362.99 +0.63%
Output token throughput (tok/s) 554.5 550.97 +0.64%
Total token throughput (tok/s) 6099.5 6060.71 +0.64%
Peak output token throughput (tok/s) 1344 1328 +1.20%
Peak concurrent requests 21.4 21.2 +0.94%
Request throughput (req/s) 0.28 0.28 0%
Mean TTFT (ms) 10701.31 10689.61 −0.11%
Median TTFT (ms) 10680.57 10705.71 +0.23%
P99 TTFT (ms) 30616.14 30599.72 −0.05%
Mean TPOT (ms) 22.39 22.58 +0.84%
Median TPOT (ms) 22.58 22.81 +1.01%
P99 TPOT (ms) 26.75 26.93 +0.67%
Mean ITL (ms) 22.39 22.58 +0.84%
Median ITL (ms) 12.02 12.17 +1.23%
P99 ITL (ms) 798.74 801.48 +0.34%

Note: TTFT shows a slight regression (−0.11% mean) due to the additional
computation in the fused kernel path for long-context (20K) prefill. The
decode-phase metrics (TPOT, ITL) consistently improve by 0.6–1.2%.

Changes

  • vllm/model_executor/kernels/mhc/optimized_fusions.py — new TileLang kernels
  • vllm/model_executor/kernels/mhc/optimized_wrappers.py — Python wrappers
  • vllm/model_executor/kernels/mhc/__init__.py — env var gating
  • vllm/models/deepseek_v4/nvidia/model.py — model integration
  • tests/kernels/test_mhc_optimized_fusions.py — correctness tests
  • benchmarks/kernels/benchmark_mhc_fusions.py — micro-benchmarks
  • benchmarks/kernels/profile_mhc_fusions.py — NCU profiling helpers

Correctness

All tests pass with rtol=1e-2 / atol=1e-2 against the original unfused
operations across multiple token counts (1, 8, 64, 256) and hidden sizes
(512, 1024, 2048).

@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 Aug 6, 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. Reviewers with write access and configured trusted contributors can comment /ci run whenever CI signals are needed.

Once the PR is approved or has the ready label, the PR author can also use /ci run or /ci retry. New commits do not start CI automatically.

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 deepseek Related to DeepSeek models performance Performance-related issues labels Aug 6, 2026
@OLIVER-XYP
OLIVER-XYP force-pushed the mhc-optimized-fusions branch from bedd04d to a29d237 Compare August 6, 2026 09:29
Comment thread vllm/models/deepseek_v4/nvidia/model.py Outdated
Comment thread vllm/models/deepseek_v4/nvidia/model.py Outdated
Comment thread vllm/models/deepseek_v4/nvidia/model.py Outdated
@mergify

mergify Bot commented Sep 5, 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, @OLIVER-XYP.

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 Sep 5, 2026
@OLIVER-XYP
OLIVER-XYP force-pushed the mhc-optimized-fusions branch from 81bb7f9 to 448154b Compare September 9, 2026 10:37
Fuse the final-layer MHC post, HC head and RMSNorm into single TileLang
kernels to cut HBM round-trips:

- mhc_post_hc_head_fused: 2-way fusion (MHC post + HC head)
- mhc_post_hc_head_norm_fused: 3-way fusion, with an MTP variant that
  stashes the pre-hc_head residual for the draft model
- mhc_post_mean_fused: MHC post + mean reduction for aux layers

Gated behind VLLM_MHC_FUSED_KERNELS (default: enabled); falls back to
the original separate kernel calls when TileLang is unavailable to the
environment because optimized_fusions.py raises ImportError.

Integration notes:
- The 3-way fusion only runs on the last PP rank.
- Under sequence parallel each rank holds a token shard and the MTP
  draft reads the stash in full-sequence order after the all-gather, so
  the kernel-side stash is bypassed when both SP and the MTP buffer are
  active (the unfused path gathers first, then stashes).
- Aux hidden states flow through pack_local_aux_hidden_states /
  collect_remote_aux_hidden_states so the fused path is compatible with
  aux-hidden-state shipping over PP.

Correctness tests compare the fused kernels against the original
separate operations across (1, 8, 64, 256) tokens and (512, 1024, 2048)
hidden sizes with rtol/atol=1e-2.

Co-authored-by: Claude <noreply@anthropic.com>

Not duplicated: the upstream ROCm/AITER mHC post/pre fusion (vllm-project#52737) is
a separate ROCm-only path; this PR adds the NVIDIA tail fusions.

Signed-off-by: OLIVER-XYP <13588039544@163.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepseek Related to DeepSeek models DSv4 performance Performance-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant