MoE: Skip SiLU/GELU activation for masked experts#15539
MoE: Skip SiLU/GELU activation for masked experts#15539ch-wan merged 17 commits intosgl-project:mainfrom
Conversation
…odularised the code
Summary of ChangesHello @yuchengz816-bot, 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 enhances the Mixture-of-Experts (MoE) architecture by optimizing the activation function computation. It introduces a smart bypass for SiLU and GELU activations when tokens are assigned to inactive or 'masked' experts, preventing unnecessary calculations. This optimization is achieved through new, unified Triton kernels that efficiently manage conditional execution based on expert routing, leading to improved performance without compromising accuracy. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new unified Triton kernel, act_and_mul_kernel, and its Python wrapper, act_and_mul_triton, to optimize activation and multiplication operations within the fused Mixture of Experts (MoE) layer. This new kernel consolidates SiLU and GELU activations and handles both sorted and unsorted routing layouts, especially when expert filtering is active. Review comments indicate that topk_ids needs to be flattened to prevent shape mismatches, the docstring for act_and_mul_triton should be updated to remove non-existent parameters, and the BLOCK_SIZE_N parameter should use the optimal value from the config dictionary instead of a hardcoded value for better performance.
| """ | ||
| grid = (down_input.shape[0],) | ||
| hidden_size = gateup_output.shape[1] | ||
| expert_ids_row = topk_ids if not down_moe_use_tma else expert_ids |
There was a problem hiding this comment.
| """ | ||
| Unified activation and multiply wrapper that dispatches to the unified kernel | ||
| with appropriate compile-time constants. | ||
|
|
||
| Args: | ||
| gateup_output: Input tensor containing gate and up outputs concatenated | ||
| down_input: Output tensor for the result | ||
| hidden_size: Size of the hidden dimension | ||
| config: Configuration dictionary with BLOCK_SIZE_M and BLOCK_SIZE_N | ||
| topk_ids: Expert IDs for unsorted routing (used when down_moe_use_tma=False) | ||
| expert_ids: Expert IDs for sorted routing (used when down_moe_use_tma=True) | ||
| num_tokens_post_padded: Number of tokens after padding (used for sorted routing) | ||
| sorted_token_ids: Sorted token IDs (used for sorted routing) | ||
| down_moe_use_tma: Whether to use sorted routing layout | ||
| activation: Activation type ("silu" or "gelu") | ||
| """ |
There was a problem hiding this comment.
The docstring for act_and_mul_triton lists several parameters that are not actually part of the function's signature: hidden_size, num_tokens_post_padded, and sorted_token_ids. These should be removed to avoid confusion and keep the documentation accurate.
"""
Unified activation and multiply wrapper that dispatches to the unified kernel
with appropriate compile-time constants.
Args:
gateup_output: Input tensor containing gate and up outputs concatenated
down_input: Output tensor for the result
config: Configuration dictionary with BLOCK_SIZE_M and BLOCK_SIZE_N
topk_ids: Expert IDs for unsorted routing (used when down_moe_use_tma=False)
expert_ids: Expert IDs for sorted routing (used when down_moe_use_tma=True)
down_moe_use_tma: Whether to use sorted routing layout
activation: Activation type ("silu" or "gelu")
"""| hidden_size, | ||
| expert_ids_row, | ||
| expert_step, | ||
| BLOCK_SIZE_N=512, |
There was a problem hiding this comment.
|
/tag-and-rerun-ci |
…n_eagle3_dp * 'main' of https://github.com/sgl-project/sglang: (208 commits) MoE: Skip SiLU/GELU activation for masked experts (sgl-project#15539) [GLM-ASR] GLM-ASR Support (sgl-project#15570) Improve engine customization interface (sgl-project#15635) chore: bump sgl-kernel version to 0.3.20 (sgl-project#15590) bugfix[schedule]: Refactor sort method and add related UT (sgl-project#13576) Adjust wrong `mtp` meaning introduce by mimo (sgl-project#15632) Tiny add back missing router per attempt response metric (sgl-project#15621) Fix router gRPC mode launch error caused by async loading (sgl-project#15368) [model-gateway] return 503 when all workers are circuit-broken (sgl-project#15611) [Diffusion] Support peak memory record in offline generate and serving (sgl-project#15610) [VLM] Tiny: Unify VLM environment variables (sgl-project#15572) [diffusion] chore: remove default post-denoising dit offload in local mode (sgl-project#15573) Tiny enable soft watchdog in CI for stuck without logs (sgl-project#15616) Tiny add stuck simulation (sgl-project#15613) Support soft watchdog for tokenizer/detokenizer/dp-controller processes (sgl-project#15607) Tiny avoid EnvField misuse (sgl-project#15612) add decode round robin policy (sgl-project#15164) Add glm-4.6-fp8 with/without mtp in nightly ci (sgl-project#15566) Adapt fixture-kit to gsm8k mixin (sgl-project#15599) [model-gateway] add retry support to OpenAI router chat endpoint (sgl-project#15589) ...
Co-authored-by: Runkai Tao <rt572@physics.rutgers.edu>
Co-authored-by: Runkai Tao <rt572@physics.rutgers.edu>
Motivation
When
filter_expertis enabled, some tokens are routed to masked experts (expert_id == -1) and should skip activation computation. This PR adds Triton SiLU/GELU multiply kernels that correctly bypass compute for these cases while still producing valid outputs, for both sorted and non-sorted routing layouts.Modifications
Accuracy Tests
gsm8k's accuracy is correct under different concurrency.
Benchmarking and Profiling
python -m sglang.launch_server --model-path mistralai/Mixtral-8x7B-Instruct-v0.1 --tp 8 --ep 8 --trust-remote-code --chunked-prefill-size 10000python3 -m sglang.bench_serving --backend sglang --dataset-name random --num-prompt 500 --random-input 8000 --random-output 2 --random-range-ratio 1 --host 127.0.0.1 --port 30000 --max-concurrency 1Checklist