-
Notifications
You must be signed in to change notification settings - Fork 469
[DRAFT] feat: add support of fp4_batched_quantize #1552
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 @yicwang, 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 introduces a new fp4_batched_quantize function, adapted from TensorRT-LLM, to enable efficient batched quantization of tensors to FP4 format. This enhancement allows for reduced memory footprint and potentially faster computations for supported tensor types (FP16, BF16, FP8).
Highlights
- Batched FP4 Quantization: Implements fp4_batched_quantize to convert input tensors to FP4 (E2M1x2) format, along with per-block scale factors.
- TensorRT-LLM Port: The core quantization logic is ported from the TensorRT-LLM library, leveraging existing optimized implementations.
- Multi-Dtype Support: The function supports input tensors of torch.float16, torch.bfloat16, and torch.float8_e4m3fn (FP8-quantized) types.
- Python and C++ Integration: The new functionality is exposed through a Python API (nvfp4_batched_quantize) which calls a C++ backend implementation.
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. 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
-
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. ↩
There was a problem hiding this 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 adds support for batched FP4 quantization by porting fp4_batched_quantize
from TensorRT-LLM. The changes include new C++ kernels, Python bindings, and exporting the new functionality. The C++ implementation appears solid and consistent with the existing codebase. However, there are a couple of critical issues in the Python wrapper code. The fake implementation for the custom op is incorrect and will crash due to mismatched assumptions about the input tensor's dimensions. Additionally, the public-facing Python function nvfp4_batched_quantize
has a misleading docstring that will cause runtime errors if followed. I've provided suggestions to fix these issues.
m, k = input.shape | ||
return ( | ||
input.new_empty([m, k // 2], dtype=torch.int64), # float4_e2m1_x2 | ||
input.new_empty([m * k // sf_vec_size], dtype=torch.int32), # Scale factors | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fake implementation _fp4_batched_quantize_sm100
has several issues that will cause it to fail at runtime and not correctly model the real operator's behavior:
- Incorrect Shape Unpacking: It unpacks the input shape as 2D (
m, k = input.shape
), but the function expects a 3D tensor of shape[B, M, K]
. This will raise aValueError
. - Incorrect Output Shape: The shapes of the returned empty tensors are missing the batch dimension.
- Incorrect Dtypes: The dtypes of the returned tensors are
torch.int64
andtorch.int32
, but they should both betorch.uint8
to match the real implementation's output (FLOAT4_E2M1X2
and scale factors are bothuint8
).
These issues will break functionality that relies on the fake op, such as model tracing or CPU-based debugging.
m, k = input.shape | |
return ( | |
input.new_empty([m, k // 2], dtype=torch.int64), # float4_e2m1_x2 | |
input.new_empty([m * k // sf_vec_size], dtype=torch.int32), # Scale factors | |
) | |
b, m, k = input.shape | |
return ( | |
input.new_empty([b, m, k // 2], dtype=torch.uint8), # float4_e2m1_x2 | |
input.new_empty([b, m * k // sf_vec_size], dtype=torch.uint8), # Scale factors | |
) |
""" | ||
Quantize input tensor to NVFP4 format. | ||
|
||
Parameters: | ||
a (torch.Tensor): Input tensor of shape [M, K] with dtype fp16/bf16. | ||
a_global_sf (torch.Tensor): Global scale factor of shape [1] with dtype float32. | ||
sf_vec_size (int, optional): Scale factor vector size. Defaults to 16. | ||
|
||
Returns: | ||
Tuple[torch.Tensor, torch.Tensor]: A tuple containing: | ||
- Quantized tensor of shape [M, K/2] with dtype FLOAT4_E2M1X2 | ||
- Scale factors tensor with shape determined by layout and sf_vec_size | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring for nvfp4_batched_quantize
is incorrect. It states that the input tensor a
should have a shape of [M, K]
. However, the underlying C++ implementation requires a 3D tensor of shape [B, M, K]
. Passing a 2D tensor as documented will cause a runtime error due to a rank check failure in the C++ code.
The docstring for the return values is also incorrect, as it's missing the batch dimension.
Please update the docstring to reflect the requirement for a 3D input tensor and the correct output shapes.
""" | |
Quantize input tensor to NVFP4 format. | |
Parameters: | |
a (torch.Tensor): Input tensor of shape [M, K] with dtype fp16/bf16. | |
a_global_sf (torch.Tensor): Global scale factor of shape [1] with dtype float32. | |
sf_vec_size (int, optional): Scale factor vector size. Defaults to 16. | |
Returns: | |
Tuple[torch.Tensor, torch.Tensor]: A tuple containing: | |
- Quantized tensor of shape [M, K/2] with dtype FLOAT4_E2M1X2 | |
- Scale factors tensor with shape determined by layout and sf_vec_size | |
""" | |
""" | |
Quantize input tensor to NVFP4 format. | |
Parameters: | |
a (torch.Tensor): Input tensor of shape [B, M, K] with dtype fp16/bf16. | |
a_global_sf (torch.Tensor): Global scale factor of shape [1] with dtype float32. | |
sf_vec_size (int, optional): Scale factor vector size. Defaults to 16. | |
Returns: | |
Tuple[torch.Tensor, torch.Tensor]: A tuple containing: | |
- Quantized tensor of shape [B, M, K/2] with dtype FLOAT4_E2M1X2 | |
- Scale factors tensor with shape determined by layout and sf_vec_size | |
""" |
📌 Description
Add the support of fp4_batched_quantize(), ported from TensorRT-LLM.
🔍 Related Issues
#1551
🚀 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
pre-commit
by runningpip install pre-commit
(or used your preferred method).pre-commit install
.pre-commit run --all-files
and fixed any reported issues.🧪 Tests
unittest
, etc.).Reviewer Notes