Skip to content

Conversation

@Fridge003
Copy link
Collaborator

@Fridge003 Fridge003 commented Nov 5, 2025

Motivation

Currently in DeepSeek fp4 and pure TP scenario, flashinfer::trtllm_allreduce_fusion is applied to batches with bs <= FUSE_ALLREDUCE_MAX_BATCH_SIZE.

However, with medium batch size like 2048(current threshold), we found that the cost for trtllm_allreduce_fusion might be much larger than all_reduce+norm with symmetric memory enabled. So we are lowering this threshold to a smaller value (like 128, as discussed in flashinfer-ai/flashinfer#1223 (comment))

Benchmarking and Profiling

DeepSeek fp4+TP 8 on B200

python3 -m sglang.launch_server --model-path nvidia/DeepSeek-R1-0528-FP4-v2 --tp-size 8 --mem-fraction-static 0.90 --quantization modelopt_fp4 --kv-cache-dtype fp8_e4m3 --attention-backend trtllm_mla --moe-runner-backend flashinfer_trtllm  --trust-remote --enable-symm-mem --cuda-graph-max-bs 64

Profile (BS=1, ISL=2048, OSL=4):

Before this PR, the time of allreduce+norm fusion is 186us
截屏2025-11-04 16 31 59

After this PR, the time of allreduce kernel plus norm kernel with symmetric memory is 107us

截屏2025-11-04 16 34 07

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Fridge003, 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 refines the conditions for applying allreduce norm fusion, a performance optimization technique. By substantially lowering the maximum batch size threshold for this fusion, the change aims to optimize performance by restricting the fusion to smaller batch sizes, potentially improving efficiency or stability under specific operational loads.

Highlights

  • Allreduce Norm Fusion Threshold: The maximum batch size for triggering allreduce norm fusion has been significantly reduced from 2048 to 128, impacting when this performance optimization is applied.
  • Dynamic Threshold Application: The hardcoded batch size limit of 4096 for the forward_with_allreduce_fusion method has been replaced with the newly defined FUSE_ALLREDUCE_MAX_BATCH_SIZE constant, ensuring consistent application of the new threshold.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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 modifies the threshold for triggering all-reduce norm fusion, changing FUSE_ALLREDUCE_MAX_BATCH_SIZE from 2048 to 128. It also commendably replaces a magic number with this constant, improving code maintainability. The new threshold value is well-justified by an inline comment referencing a FlashInfer discussion. I have one suggestion to improve the clarity of the constant's name.

from sglang.srt.layers.quantization.rocm_mxfp4_utils import fused_rms_mxfp4_quant

FUSE_ALLREDUCE_MAX_BATCH_SIZE = 2048
FUSE_ALLREDUCE_MAX_BATCH_SIZE = 128
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The constant name FUSE_ALLREDUCE_MAX_BATCH_SIZE appears to be misleading. Its usage throughout the file, such as the comparison with hidden_states.shape[0] on line 562, and the explanatory comment on line 556 which refers to "max token num", indicates that it represents a token limit, not a batch size limit. Renaming it to FUSE_ALLREDUCE_MAX_NUM_TOKENS would more accurately reflect its purpose and improve code clarity. If you make this change, please ensure all usages in the file are updated accordingly.

@Fridge003
Copy link
Collaborator Author

Fridge003 commented Nov 5, 2025

@yuan-luo @Qiaolin-Yu Can you have a look

@Fridge003 Fridge003 marked this pull request as ready for review November 5, 2025 00:35
@zhyncs
Copy link
Member

zhyncs commented Nov 5, 2025

@averyhNV @yyihuang

Copy link
Collaborator

@yyihuang yyihuang left a comment

Choose a reason for hiding this comment

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

LGTM.

and hasattr(layernorm, "forward_with_allreduce_fusion")
and get_global_server_args().enable_flashinfer_allreduce_fusion
and hidden_states.shape[0] <= 4096
and hidden_states.shape[0] <= FUSE_ALLREDUCE_MAX_BATCH_SIZE
Copy link
Collaborator

Choose a reason for hiding this comment

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

FLASHINFER_ALLREDUCE_FUSION_MAX_BATCH_SIZE might be better.

@yuan-luo
Copy link
Collaborator

yuan-luo commented Nov 5, 2025

Have you tried on H20 or H800? The result might be slightly different.

@Fridge003 Fridge003 marked this pull request as draft November 5, 2025 02:30
@Fridge003
Copy link
Collaborator Author

Have you tried on H20 or H800? The result might be slightly different.

Not yet... I was doing benchmark on B200 and the result shows this change has negative effect.
Still underworking

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.

7 participants