Skip to content

Improve small size performance in cutedsl fp4 #2517

Closed
vincentzed wants to merge 4 commits intoflashinfer-ai:mainfrom
bzhng-development:clean-cutedsl-fp4-moe
Closed

Improve small size performance in cutedsl fp4 #2517
vincentzed wants to merge 4 commits intoflashinfer-ai:mainfrom
bzhng-development:clean-cutedsl-fp4-moe

Conversation

@vincentzed
Copy link
Contributor

@vincentzed vincentzed commented Feb 7, 2026

📌 Description

As a hunch, cuteDSL should be able to match trt performance for most low tok/s. This PR is meant to more often choose CuteDSL for fp4 moe. So here we compare ncu measure, cutlass, trt, cutedsl, and find an easy way to more often select cutedsl on toks = small case.

void at::native::vectorized_elementwise_kernel<4, ...FillFunctor<c10::BFloat16>...> is moved to the overlap with gemm (this happen before gemm2 finalize), unless output = None.

It is 4.3 us in ncu.

At 8 toks, CuteDSL wins over TRT, and the Deltas are as follows:

Before: CuteDSL/Trt: 1,2,4,8: 0.88x,0.92x,0.93x,0.99x

After:

Tokens Before CuteDSL (ms) Before TRTLLM (ms) Before Ratio (CuteDSL/TRTLLM) Before Winner After CuteDSL (ms) After TRTLLM (ms) After Ratio (CuteDSL/TRTLLM) After Winner Note
1 0.033 0.029 0.86x TRTLLM 0.031 0.029 0.91x TRTLLM gap narrowed significantly
2 0.034 0.029 0.86x TRTLLM 0.031 0.029 0.92x TRTLLM gap narrowed
4 0.034 0.030 0.87x TRTLLM 0.032 0.030 0.95x TRTLLM similar
8 0.034 0.031 0.91x TRTLLM 0.031 0.032 1.01x CuteDSL CuteDSL now wins

Which is consistent with remove the fill (~4us)- which only matters at small size.

I can upload ncu as well, if it is useful.

🔍 Related Issues

🚀 Pull Request Checklist

Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.

✅ Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).
  • I have installed the hooks with pre-commit install.
  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.

If you are unsure about how to set up pre-commit, see the pre-commit documentation.

🧪 Tests

  • Tests have been added or updated as needed.
  • All tests are passing (unittest, etc.).

Reviewer Notes

Signed-off-by: vincentzed
<207368749+vincentzed@users.noreply.github.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 7, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @vincentzed, 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 optimizes the performance of CuteDSL for FP4 Mixture-of-Experts (MoE) operations, particularly for small token counts. The primary improvement involves streamlining the output buffer initialization process by removing a redundant zeroing operation and allowing this initialization to occur asynchronously, overlapping with other computations. This change reduces overhead, leading to better latency for low token/s scenarios, as evidenced by the provided benchmarks showing CuteDSL now outperforming TRTLLM at 8 tokens.

Highlights

  • Output Buffer Optimization: Introduced a zero_output parameter in blockscaled_contiguous_grouped_gemm_finalize_fusion_nvfp4 to control internal output buffer initialization, allowing external pre-zeroing.
  • Redundant Memset Removal: Eliminated the moe_output_memset function call, replacing it with a direct moe_output.zero_() operation for explicit and more efficient buffer clearing.
  • Asynchronous Zeroing: Enabled overlapping of the output buffer zeroing with GEMM1 computation on an auxiliary stream, reducing overhead for small token sizes.
  • Performance Improvement: Demonstrated significant performance gains for small token sizes (e.g., 8 tokens), making CuteDSL competitive or superior to TRTLLM.

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

Changelog
  • flashinfer/fused_moe/cute_dsl/blockscaled_contiguous_grouped_gemm_finalize_fusion.py
    • Added a zero_output boolean parameter to blockscaled_contiguous_grouped_gemm_finalize_fusion_nvfp4 with a default value of True.
    • Modified the output zeroing logic within blockscaled_contiguous_grouped_gemm_finalize_fusion_nvfp4 to conditionally zero the output based on the new zero_output parameter.
  • flashinfer/fused_moe/cute_dsl/fused_moe.py
    • Removed the import and usage of moe_output_memset from moe_utils.
    • Replaced the moe_output_memset call with a direct moe_output[:num_tokens].zero_() for explicit output buffer zeroing.
    • Updated the call to blockscaled_contiguous_grouped_gemm_finalize_fusion_nvfp4 to pass zero_output=False, indicating the output is pre-zeroed externally.
    • Adjusted comments to clarify the new zeroing strategy and its overlap with GEMM1.
Activity
  • The author has provided a detailed description including performance benchmarks and a checklist.
  • No human review comments or additional activity have been recorded yet.
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 introduces a performance optimization for small batch sizes in the CuteDSL FP4 MoE implementation. The change replaces a custom kernel for zeroing the output buffer with a standard PyTorch zero_() call and avoids a redundant zeroing operation inside the GEMM kernel. This is a sensible optimization that, according to the description, yields significant performance improvements. The implementation looks correct and the logic is sound. My only suggestion is to add documentation for the new parameter introduced to control the zeroing behavior, which will improve code clarity and maintainability.

cluster_shape_mn: Tuple[int, int] = (2, 1),
raster_along_m: bool = False,
sm_count: Optional[int] = None,
zero_output: bool = True,
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 new zero_output parameter should be documented in the function's docstring to explain its purpose. Additionally, the docstring for the out parameter should be updated to clarify that its zero-initialization is now conditional on this new parameter.

For example:

        out: Optional output tensor, shape (seq_len, n). Created if None.
             This tensor is used for atomic accumulation. If `zero_output` is True (the default),
             it will be zero-initialized by this function. Otherwise, the caller is responsible
             for ensuring it is zero-initialized before calling.
...
        sm_count: Number of SMs to use. Default: max available.
        zero_output: If True, zero-initialize the `out` tensor before the kernel launch.
                     Set to False if the output tensor is already zeroed. Default: True.

Signed-off-by: vincentzed <207368749+vincentzed@users.noreply.github.com>
Signed-off-by: vincentzed <207368749+vincentzed@users.noreply.github.com>
This reverts commit 0d36051.
@vincentzed
Copy link
Contributor Author

Closing as covered by #2811

@vincentzed vincentzed closed this Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant