Skip to content

Conversation

ttyio
Copy link
Collaborator

@ttyio ttyio commented Jul 30, 2025

feature: add fp4 mm using trtllm backend

📌 Description

  1. support both 128x4 and 8x4 block quant layout
  2. support autotuning

🔍 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 @ttyio, 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 significantly enhances FlashInfer's capabilities for efficient quantized matrix multiplication by integrating the TensorRT-LLM backend for FP4 operations. It introduces support for flexible 128x4 and 8x4 block quantization layouts and incorporates an autotuning framework to dynamically select optimal GEMM kernels, leading to improved performance and broader applicability for various model architectures.

Highlights

  • New FP4 GEMM Backend: I've added support for FP4 matrix multiplication using the TensorRT-LLM (TRTLLM) backend, providing an alternative to the existing cuDNN backend for quantized GEMM operations.
  • Flexible Scale Factor Layouts: The implementation now supports both 128x4 and 8x4 block quantization layouts for scale factors. This allows for more granular control and potential optimization based on the specific matrix dimensions and hardware characteristics.
  • Autotuning for TRTLLM GEMM: A new autotuning mechanism has been integrated for the TRTLLM FP4 GEMM. This enables dynamic selection of the most performant kernel tactic at runtime, adapting to different input shapes and maximizing efficiency.
  • Unified FP4 Quantization API: I've introduced a new nvfp4_quantize API that streamlines the quantization process. This function handles both the core quantization logic and the necessary data shuffling required for the TRTLLM backend, simplifying its usage.
  • Code Refactoring and Centralization: Matrix shuffling utilities have been refactored and centralized into flashinfer/utils.py and flashinfer/fp4_quantization.py. This improves code organization and maintainability by consolidating related functionalities.
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.

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 support for FP4 matrix multiplication using the TensorRT-LLM backend, which is a significant feature addition. The changes include support for both 128x4 and 8x4 block quantization layouts and autotuning capabilities. The code is well-structured, with a major refactoring of the GEMM runner into a dedicated class and clear separation of concerns.

I've identified a critical bug in csrc/trtllm_gemm_runner.cu where an incorrect type is passed for the device index, which will likely lead to a compilation failure. Additionally, there's a documentation error in flashinfer/gemm.py regarding the scale factor layout requirements for the new backend, which could mislead users. Once these issues are addressed, the PR should be in good shape.

@ttyio ttyio force-pushed the feature/fp4-trtllm-gen branch 10 times, most recently from dc75fd3 to 961e759 Compare July 31, 2025 15:43
1. support both 128x4 and 8x4 block quant layout
2. support autotuning

Signed-off-by: Vincent Huang <vincenth@nvidia.com>
@ttyio ttyio force-pushed the feature/fp4-trtllm-gen branch from 961e759 to 08bb8f8 Compare July 31, 2025 16:11
@ttyio
Copy link
Collaborator Author

ttyio commented Jul 31, 2025

@yzh119 can I get this reviewed/merged when you have time? thank you!

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.

Overall LGTM, left some suggestions on coding style.

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 @ttyio !

@yzh119 yzh119 merged commit d8e7d6a into flashinfer-ai:main Aug 1, 2025
2 checks passed
yzh119 added a commit that referenced this pull request Aug 3, 2025
…ZZLED (#1371)

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

## 📌 Description

cutlass fused moe modules are broken after
#1355 because the
structure of `FP4QuantizationSFLayout` has changed. This PR fixes the
issue.

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

cc @wenscarl @ttyio
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants