Skip to content

Conversation

yyihuang
Copy link
Collaborator

📌 Description

Update the static scaling factor interface to take:
q_scale, k_scale, v_scale, sm_scale, o_scale.

🔍 Related Issues

TODOs in #1222, last comment.

🚀 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 @yyihuang, 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 more explicit and flexible interface for handling scaling factors within the trtllm-gen mla kernels. By replacing a single scale parameter with a set of five specific scales (for query, key, value, softmax, and output), the changes enable finer-grained control over quantization, particularly in preparation for future FP8 quantization support. The modifications span both the Python API and the underlying C++ CUDA kernels, ensuring consistent behavior and improved extensibility for advanced quantization schemes.

Highlights

  • API Interface Update: The trtllm_batch_decode_with_kv_cache_mla function in flashinfer/decode.py and its corresponding C++ kernel launcher (trtllm_paged_attention_mla_launcher in csrc/trtllm_mla_kernel_launcher.cu) have been refactored to replace a single generic scale parameter with five distinct, more granular scaling factors: q_scale, k_scale, v_scale, sm_scale, and o_scale.
  • Dynamic Scale Factor Derivation: The bmm1_scale and bmm2_scale values, which are consumed by the C++ kernel, are now dynamically computed within the Python wrapper (flashinfer/decode.py) based on the newly introduced granular input scales. Specifically, bmm1_scale is derived from q_scale, k_scale, and sm_scale (and the head dimension), while bmm2_scale is derived from v_scale and o_scale.
  • Internal Kernel Logic Adjustment: In the C++ kernel (csrc/trtllm_mla_kernel_launcher.cu), the runner_params.mScaleQ (previously controlled by the generic scale parameter) is now hardcoded to 1.0. The runner_params.scaleSoftmaxLog2 calculation has been updated to bmm1_scale * std::sqrt((float)(runner_params.mHeadDimQk)), aligning with the new scale derivation logic from the Python frontend.
  • Test Suite Adaptation: The test cases in tests/test_trtllm_gen_decode.py have been updated to reflect the new function signature and parameter passing for trtllm_batch_decode_with_kv_cache_mla, ensuring the correctness and compatibility of the changes.
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 is currently in preview and 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 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 refactors the scaling factor interface for the TRT-LLM MLA kernels, moving from a single scale parameter to a more detailed set of quantization and model scales (q_scale, k_scale, v_scale, sm_scale, o_scale). This improves clarity and flexibility. The changes are mostly in the C++ kernel launcher and the Python interface.

I've found two high-severity issues that should be addressed:

  1. In csrc/trtllm_mla_kernel_launcher.cu, there's a bug in the calculation of the softmax scaling factor, which uses an incorrect head dimension.
  2. In flashinfer/decode.py, there's an incorrect shape and dtype check for the pre-allocated output tensor, which would cause runtime errors.

@yyihuang yyihuang mentioned this pull request Jul 14, 2025
5 tasks
@yyihuang yyihuang mentioned this pull request Jul 16, 2025
5 tasks
yzh119 pushed a commit that referenced this pull request Jul 16, 2025
<!-- .github/pull_request_template.md -->

## 📌 Description

Enable deepseek MTP with q>1.
Recommend small batch size and small MTP = 2 / 3.
Update tests to reduce test time.
Draft dynamic scale factor interface of mla (part1, part2 - cubin
loading & fmha in #1248)

--ready to merge now.


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

---------

Co-authored-by: averyhuang <averyh@nvidia.com>
@yyihuang yyihuang changed the title feat: update scale factor interface for trtllm-gen mla kernels. feat: update cubins for dynamic scale factor interface and one-copy kvcache on trtllm-gen mla Jul 16, 2025
@yyihuang
Copy link
Collaborator Author

Close to skip rebasing on last refactor. Moved to #1342.

@yyihuang yyihuang closed this Jul 29, 2025
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.

3 participants