Add fp4 quantize before all-gather for Flashinfer cutlass MoE DP (max throughput)#7667
Add fp4 quantize before all-gather for Flashinfer cutlass MoE DP (max throughput)#7667ch-wan merged 1 commit intosgl-project:mainfrom
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @trevor-m, 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 focuses on optimizing communication patterns for Data Parallel (DP) operations within FlashInfer Mixture-of-Experts (MoE) models. My primary goal is to enhance efficiency by introducing and integrating new variable-sized collective communication primitives, allgatherv and reducescatterv, into the MoE forward pass. A key aspect of this work involves quantizing data prior to communication to minimize data transfer overhead.
Highlights
- New NCCL Collectives: I've introduced
allgathervandreducescattervto the PyNCCL communicator. These new primitives are designed to efficiently handle collective communication for tensors where the size of data contributed by each rank can vary, mimicking TRT-LLM's approach. - Optimized MoE Communication for DP: For FlashInfer Mixture-of-Experts (MoE) in a Data Parallel (DP) setup, I've refactored the communication path. Instead of the previous
pad+allreducefor token dispatch, we now utilize the newallgatherv. Similarly,reducescattervis used to combine results, replacing the priorall_reduceoperation. - Quantization Before Communication: A significant optimization implemented is moving the FP4 quantization step to occur before the
allgathervcommunication. This reduces the size of the data being transferred across the network, aiming for improved communication efficiency. - Dynamic MoE Layer Handling: I've added logic to allow the MoE layer itself to manage dispatch and combine operations under specific configurations (e.g., when FlashInfer MoE is enabled with Data Parallelism), bypassing the general
LayerCommunicatorfor more specialized handling.
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
-
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. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces allgatherv and reducescatterv collectives to optimize MoE communication with data parallelism, and moves quantization before communication to reduce data volume. The changes are well-structured, adding the new communication primitives and integrating them into the MoE layers. However, there's a potential bug in the quantization logic, an incorrect return type hint, and missing tests for the new functionalities.
|
Does varying sizes per rank allgather compatible with cuda graph? |
@Alcanderian Yes it is working with cuda graph. Although I am not super familiar with cuda graph in torch, is it possible that certain operations are excluded from the graph if they aren't compatible? |
3252d2f to
d0e835f
Compare
|
Hi, please resolve conflicts and provide some performance report. Thanks! |
Hi @Alcanderian I updated the PR description with performance results - is there any more benchmarks I should run? Thanks! |
Thanks! IMO we should disable it in decode stage accroding to the benchmark result |
There was a problem hiding this comment.
What are the needed fields? Passing the whole forward_batch makes this function kind of opaque.
We want the function to be more explicit about the argument.
There was a problem hiding this comment.
Hi @merrymercy thank you for reviewing. In modelopt_quant.py, we use forward_batch.dp_padding_mode.is_max_len(), forward_batch.input_ids.shape[0], forward_batch.gathered_buffer, forward_batch.global_num_tokens_cpu.
Other backends like deepep which also integrate communication pass the whole forward_batch to MOE also:
6a9b7ae to
bd12687
Compare
|
@ch-wan could you help review/merge this PR plz? |
fdaeec7 to
e7980df
Compare
There was a problem hiding this comment.
The code logic is getting increasingly messy. It seems that forward_batch is only need for dp communication. We need to refactor dp_attention.py so that any part of the code can handle dp communication without using forward_batch. Let me try this later.
There was a problem hiding this comment.
Thanks @ch-wan - anything I can do for this PR to help?
There was a problem hiding this comment.
we should explicitly list all the required arguments instead of passing a big forward_batch.
Passing a big forward_batch makes the input of this function very opaque.
|
@trevor-m DP attn refactor is almost ready. I am doing extra local tests. Could you update this PR by using the util function developed in #9136? This should make the code much cleaner. Thanks. UPD: I've just noticed that you did some refactor in this PR :) How about adding |
Thanks @ch-wan I added a helper so I don't need to pass |
ch-wan
left a comment
There was a problem hiding this comment.
LGTM. I only have some minor comments. Could you fix conflicts? Thanks.
Flashinfer MoE FP4 communication optimization for DP Fix quantize before comm and only use for dp, use numel(), fix return type annotatin Use enable_flashinfer_fp4_allgather to toggle lint Fix forwardbatch Automatically enable fp4 allgather Enable automatically and improve server arg descriptions Switch from server arg to should_use_flashinfer_cutlass_moe_fp4_allgather(). Add server arg to disable formatting Remove forward_batch arg Use helper function for empty topk Add get_dp_global_num_tokens() helper
| self.nccl.ncclGroupStart() | ||
| for root, split_size in enumerate(sizes): | ||
| dst_slice = output_tensor[split_offset : split_offset + split_size] | ||
| self.nccl.ncclBroadcast( |
There was a problem hiding this comment.
Is this broadcast or allgather?
There was a problem hiding this comment.
It’s equivalent to all gather. Each rank does a broadcast but we group them to avoid overheads. This is done to allow each rank to have a different size
Motivation
The goal of this PR is to optimize communications for DP with FlashInfer Cutlass MoE.
Modifications
Improvements include:
Usage
Enabled automatically when applicable:
--enable-flashinfer-cutlass-moe,--enable-dp-attention, anddp_size == ep_sizemust all be true.Can be disabled with
--disable-flashinfer-cutlass-moe-fp4-allgather.Results
Accuracy
Benchmark
End to end speedup: 9.38%
BEFORE
AFTER
Checklist