Skip to content

[Kernel] DeepGemm MoE : Integrate triton permute / unpermute kernels #20903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

varun-sundar-rabindranath
Copy link
Contributor

@varun-sundar-rabindranath varun-sundar-rabindranath commented Jul 14, 2025

Purpose

Optimizations for DeepEPHTPrepareAndFinalize + DeepGEMM combination.

  1. Use local_num_experts and CPU expert_num_tokens to avoid over allocating workspaces
  2. Use ep_scatter and ep_gather kernels from lightllm to perform the deep_gemm_moe permute and unpermute.

Optimizations

Torch trace for a single modular_kernel execution:
python3 -m tests.kernels.moe.modular_kernel_tools.profile_modular_kernel --pf-type DeepEPHTPrepareAndFinalize --experts-type DeepGemmExperts --quant-dtype torch.float8_e4m3fn --block-shape 128 128 --torch-trace-dir-path ./torch_trace_files/ --topk 4

main + #20725: - m64_0_trace.json
this PR (changes on top of main + 20725) - m64_0_trace.json

From the torch traces, you could see that using dedicated scatter / gather kernels results in much lesser torch ops.

Test Plan

pytest -s tests/kernels/moe/test_modular_kernel_combinations.py - all tests pass

e2e :
VLLM_FUSED_MOE_CHUNK_SIZE=64 VLLM_ALL2ALL_BACKEND="deepep_high_throughput" VLLM_USE_DEEP_GEMM=1 vllm serve Qwen/Qwen3-30B-A3B-FP8 --trust-remote-code --enable-expert-parallel --data-parallel-size 2 --port 9010
lm_eval --model local-completions --tasks gsm8k --model_args model=Qwen/Qwen3-30B-A3B-FP8,base_url=http://127.0.0.1:9010/v1/completions,num_concurrent=30,max_retries=3 --limit 100

Test Result

|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value|   |Stderr|
|-----|------:|----------------|-----:|-----------|---|----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  | 0.85|±  |0.0359|
|     |       |strict-match    |     5|exact_match|↑  | 0.93|±  |0.0256|

Performance

commands:
VLLM_ALL2ALL_BACKEND="deepep_high_throughput" VLLM_USE_DEEP_GEMM=1 vllm serve Qwen/Qwen3-30B-A3B-FP8 --trust-remote-code --enable-expert-parallel --data-parallel-size 2 --port 9010 --no-enable-prefix-caching
python3 ./benchmarks/benchmark_serving.py --model Qwen/Qwen3-30B-A3B-FP8 --dataset-name sharegpt --sharegpt-output-len 1 --port 9010 --dataset-path ./ShareGPT_V3_unfiltered_cleaned_split.json`

This PR

============ Serving Benchmark Result ============
Successful requests:                     1000      
Benchmark duration (s):                  4.85      
Total input tokens:                      229334    
Total generated tokens:                  1000      
Request throughput (req/s):              206.27    
Output token throughput (tok/s):         206.27    
Total Token throughput (tok/s):          47510.74  
---------------Time to First Token----------------
Mean TTFT (ms):                          3005.18   
Median TTFT (ms):                        2854.88   
P99 TTFT (ms):                           4725.21   
-----Time per Output Token (excl. 1st token)------
Mean TPOT (ms):                          0.00      
Median TPOT (ms):                        0.00      
P99 TPOT (ms):                           0.00      
---------------Inter-token Latency----------------
Mean ITL (ms):                           0.00      
Median ITL (ms):                         0.00      
P99 ITL (ms):                            0.00      
==================================================

main

============ Serving Benchmark Result ============
Successful requests:                     1000      
Benchmark duration (s):                  7.56      
Total input tokens:                      229334    
Total generated tokens:                  1000      
Request throughput (req/s):              132.19    
Output token throughput (tok/s):         132.19    
Total Token throughput (tok/s):          30447.50  
---------------Time to First Token----------------
Mean TTFT (ms):                          4317.56   
Median TTFT (ms):                        4162.15   
P99 TTFT (ms):                           7365.30   
-----Time per Output Token (excl. 1st token)------
Mean TPOT (ms):                          0.00      
Median TPOT (ms):                        0.00      
P99 TPOT (ms):                           0.00      
---------------Inter-token Latency----------------
Mean ITL (ms):                           0.00      
Median ITL (ms):                         0.00      
P99 ITL (ms):                            0.00      
==================================================

(Optional) Documentation Update

Copy link

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

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

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.

🚀

Copy link

mergify bot commented Jul 14, 2025

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

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 Jul 14, 2025
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Summary of Changes

Hello @varun-sundar-rabindranath, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant kernel-level optimizations for the Mixture of Experts (MoE) layers, specifically targeting the DeepEPHTPrepareAndFinalize and DeepGEMM combination. The changes aim to improve computational efficiency and memory utilization by refining how workspaces are allocated and by integrating specialized, high-performance permute and unpermute kernels. These enhancements are crucial for scaling MoE models effectively.

Highlights

  • DeepGEMM Optimization - Workspace Allocation: Implemented more precise workspace allocation for DeepGEMM by leveraging local_num_experts and CPU-side expert_num_tokens. This avoids overallocation and improves memory efficiency for MoE layers, particularly within the deep_gemm_moe.py and deep_gemm_utils.py modules.
  • DeepGEMM Optimization - Permute/Unpermute Kernels: Integrated new ep_scatter and ep_gather Triton kernels (adapted from LightLLM) to handle the permute and unpermute operations within the DeepGEMM MoE flow. These new kernels, found in deep_gemm_utils.py, replace the previous _moe_permute implementation for enhanced performance.
  • Unified Weight Application and Reduction: Modified the DeepGEMM kernels to internally handle the application of router weights and the final reduction step. This change simplifies the overall MoE pipeline by making the TopKWeightAndReduceNoOP class a pass-through, as these operations are now performed directly within the optimized DeepGEMM kernel.
  • Modular Kernel Interface Enhancements: Updated the core modular kernel interfaces across various MoE implementations (modular_kernel.py, fused_moe.py, batched_deep_gemm_moe.py, cutlass_moe.py, fused_batched_moe.py, triton_deep_gemm_moe.py) to support the new topk_weights and apply_router_weight_on_input parameters, ensuring compatibility and flexibility for future optimizations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

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

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 introduces optimizations for the DeepEP and DeepGEMM MoE kernels, focusing on workspace allocation and leveraging efficient scatter/gather kernels. The changes span multiple files within the fused MoE framework, aiming to improve performance and memory utilization.

Signed-off-by: Varun Sundar Rabindranath <[email protected]>
@mergify mergify bot removed the needs-rebase label Jul 15, 2025
@varun-sundar-rabindranath varun-sundar-rabindranath changed the title [Kernel] Prefill DeepEP + DeepGEMM Optimizations [Kernel] DeepGemm MoE : Integrate triton permute / unpermute kernels Jul 15, 2025
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add unit tests for this, or do you think this is sufficiently covered by your moe testing framework, @varun-sundar-rabindranath?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The deepgemm_unpermute_and_reduce and deepgemm_moe_permute pass on the functionality to the lightllm ep_scatter / ep_gather kernels. I am fairly confident that those aren't buggy.
But, I'll add some unit test anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we land this without any unit tests for the lightllm kernels ? I can add unit tests in a followup PR. Thanks.

Signed-off-by: Varun Sundar Rabindranath <[email protected]>
@tlrmchlsmth tlrmchlsmth added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 16, 2025
@tlrmchlsmth tlrmchlsmth enabled auto-merge (squash) July 16, 2025 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants