Skip to content

Faster weight processing (moe nvfp4)#1412

Merged
yzh119 merged 12 commits intoflashinfer-ai:mainfrom
aleozlx:feature/weight_proc_cache
Aug 9, 2025
Merged

Faster weight processing (moe nvfp4)#1412
yzh119 merged 12 commits intoflashinfer-ai:mainfrom
aleozlx:feature/weight_proc_cache

Conversation

@aleozlx
Copy link
Copy Markdown
Collaborator

@aleozlx aleozlx commented Aug 7, 2025

📌 Description

🔍 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

Copy link
Copy Markdown
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 @aleozlx, 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 PR aims to improve the performance of weight processing for Mixture-of-Experts (MoE) models, specifically for NVFP4 quantized weights. It achieves this by introducing a caching mechanism for permutation indices, which are costly to recompute. This optimization will speed up the preparation of static weights for kernel operations.

Highlights

  • Optimized Weight Permutation: Introduced new utility functions (_maybe_get_cached_w3_w1_permute_indices, _maybe_get_cached_w2_permute_indices) in flashinfer/fused_moe/core.py to cache and reuse permutation indices for MoE weights (W3/W1 and W2). This avoids redundant and expensive recomputations.
  • Integration into Weight Preparation: The prepare_static_weights_for_kernel function in the test suite (tests/test_trtllm_gen_fused_moe.py) has been updated to leverage these new caching functions, demonstrating their intended use and benefit in speeding up weight preparation.
  • Enhanced Test Coverage: A new pytest fixture cache_permute_indices and modifications to test_moe_quantization_classes were added to properly test the new caching mechanism for weight permutation.
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 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 or fill out our survey 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.

@aleozlx
Copy link
Copy Markdown
Collaborator Author

aleozlx commented Aug 7, 2025

pytest -x tests/test_trtllm_gen_fused_moe.py -k FP4

18 passed, 72 skipped, 180 deselected in 132.28s (0:02:12)

Copy link
Copy Markdown
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 caching mechanism for permutation indices to speed up weight processing in the MoE implementation, which is a great performance enhancement. My review focuses on a bug in the caching logic that could lead to incorrect cache hits, and some opportunities for code refactoring to improve maintainability in both the core logic and the tests.

@zhyncs
Copy link
Copy Markdown
Member

zhyncs commented Aug 7, 2025

@azhurkevich

@azhurkevich
Copy link
Copy Markdown
Contributor

@aleozlx awesome work!

@aleozlx
Copy link
Copy Markdown
Collaborator Author

aleozlx commented Aug 7, 2025

@azhurkevich i'll check sgl, got cluster access recently

@aleozlx
Copy link
Copy Markdown
Collaborator Author

aleozlx commented Aug 7, 2025

testing results as of lastest change

pytest -x tests/test_trtllm_gen_fused_moe.py

60 passed, 210 skipped in 243.93s (0:04:03)

ready to merge from my perspective!

@aleozlx
Copy link
Copy Markdown
Collaborator Author

aleozlx commented Aug 8, 2025

pytest -x tests/test_trtllm_gen_fused_moe.py -k FP4

18 passed, 72 skipped, 180 deselected in 129.88s (0:02:09)

@aleozlx
Copy link
Copy Markdown
Collaborator Author

aleozlx commented Aug 8, 2025

after some discussion on if we want to only test cached code path and remove older version. we are aligned on only testing cached path

ready to merge!

@yzh119 yzh119 merged commit 160e4b8 into flashinfer-ai:main Aug 9, 2025
2 checks passed
@yzh119 yzh119 mentioned this pull request Aug 9, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants