Skip to content

feat: Add weight layout option for trtllm-gen fused moe #1297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 26, 2025

Conversation

aleozlx
Copy link
Contributor

@aleozlx aleozlx commented Jul 21, 2025

📌 Description

Expose weight layout for BlockMajorK usage

🔍 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
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 pull request enhances the flashinfer library by adding explicit support for different weight layouts, particularly BlockMajorK, within its TensorRT-LLM (TRT-LLM) fused Mixture-of-Experts (MoE) kernels. This change allows for more flexible and potentially optimized weight storage and access patterns, which can be beneficial for performance. The modifications span both the Python API and the underlying C++ implementation, ensuring end-to-end support for the new layout.

Highlights

  • New Weight Layout Support: Introduced a new WeightLayout enum in Python and C++ to support different matrix layouts for GEMM operations, specifically BlockMajorK for optimized performance in certain scenarios.
  • API Extension: Extended the trtllm_fp8_block_scale_moe function in the Python API to accept a weight_layout parameter, allowing users to specify the desired weight organization.
  • C++ Backend Integration: Propagated the weight_layout parameter through the C++ TrtllmGenBatchedGemmRunnerOptions and various Runner constructors within the trtllm_fused_moe components, ensuring the chosen layout is respected during kernel execution.
  • Weight Conversion Utility: Added a convert_to_block_layout utility function in Python to facilitate the transformation of weight tensors into the BlockMajorK format, which is crucial for leveraging the new layout.
  • Test Coverage Expansion: Updated the test_moe_fp8 suite to include test cases for the BlockMajorK weight layout, ensuring the new functionality behaves as expected.
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 aleozlx marked this pull request as draft July 21, 2025 23:54
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

The pull request introduces weight layout functionality for BlockMajorK usage, including modifications to C++ and Python code to support different weight layouts in the fused MoE kernels. The changes involve adding a new WeightLayout enum in Python, modifying the kernel launcher and runner in C++, and updating the test suite to include different weight layouts. The code includes checks for valid weight layout values and incorporates the new layout parameter into the kernel execution.

@aleozlx aleozlx marked this pull request as ready for review July 23, 2025 06:47
@aleozlx
Copy link
Contributor Author

aleozlx commented Jul 23, 2025

local testing results

$ pytest -x tests/test_trtllm_gen_fused_moe{,_fp4}.py
====================================================================== test session starts ======================================================================
platform linux -- Python 3.12.3, pytest-8.1.1, pluggy-1.5.0
rootdir: /workspace/flashinfer
configfile: pyproject.toml
plugins: anyio-4.9.0, shard-0.1.2, xdist-3.6.1, flakefinder-1.1.0, rerunfailures-15.0, xdoctest-1.0.2, hypothesis-6.130.8, typeguard-4.3.0
collected 102 items
Running 102 items in this shard

tests/test_trtllm_gen_fused_moe.py ......................................................                                                                 [ 52%]
tests/test_trtllm_gen_fused_moe_fp4.py ................................................                                                                   [100%]

================================================================ 102 passed in 477.47s (0:07:57) ================================================================

@aleozlx
Copy link
Contributor Author

aleozlx commented Jul 23, 2025

after slight refactor, double checked fp8

$pytest -x tests/test_trtllm_gen_fused_moe.py
54 passed in 67.26s (0:01:07)

@aleozlx
Copy link
Contributor Author

aleozlx commented Jul 24, 2025

ready to merge!

pytest -x -v tests/test_trtllm_gen_fused_moe.py

60 passed, 210 skipped in 239.06s (0:03:59)

@aleozlx
Copy link
Contributor Author

aleozlx commented Jul 25, 2025

after update branch

60 passed, 210 skipped in 250.24s (0:04:10)

@aleozlx aleozlx mentioned this pull request Jul 25, 2025
5 tasks
@aleozlx
Copy link
Contributor Author

aleozlx commented Jul 25, 2025

the latest cubin refresh addresses another integration request

tested:

60 passed, 210 skipped in 271.29s (0:04:31)

as discussed we won't wait for the cubin refactor. pls merge soon when you get a chance @yzh119 thx!

Copy link
Collaborator

@yzh119 yzh119 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the great work!

@yzh119 yzh119 changed the title Add weight layout Add weight layout option for trtllm-gen fused moe Jul 26, 2025
@yzh119 yzh119 changed the title Add weight layout option for trtllm-gen fused moe feat: Add weight layout option for trtllm-gen fused moe Jul 26, 2025
@yzh119 yzh119 merged commit 8cd1800 into flashinfer-ai:main Jul 26, 2025
2 checks passed
Edenzzzz pushed a commit to Edenzzzz/flashinfer that referenced this pull request Jul 27, 2025
…i#1297)

<!-- .github/pull_request_template.md -->

## 📌 Description

Expose weight layout for BlockMajorK usage

## 🔍 Related Issues

<!-- Link any related issues here -->

## 🚀 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

- [x] I have installed `pre-commit` by running `pip install pre-commit`
(or used your preferred method).
- [x] I have installed the hooks with `pre-commit install`.
- [x] 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](https://pre-commit.com/).

## 🧪 Tests

- [x] Tests have been added or updated as needed.
- [x] All tests are passing (`unittest`, etc.).

## Reviewer Notes

<!-- Optional: anything you'd like reviewers to focus on, concerns, etc.
-->
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.

2 participants