Skip to content

Fix: Skip invalid GQA tile candidates with stepQ < headsQPerKv.#2383

Closed
KungYork wants to merge 1 commit intoflashinfer-ai:mainfrom
KungYork:fix-gqa-selection
Closed

Fix: Skip invalid GQA tile candidates with stepQ < headsQPerKv.#2383
KungYork wants to merge 1 commit intoflashinfer-ai:mainfrom
KungYork:fix-gqa-selection

Conversation

@KungYork
Copy link
Copy Markdown

@KungYork KungYork commented Jan 20, 2026

📌 Description

Skip candidates where kernelMeta.mStepQ < params.mNumHeadsQPerKv in GQA tile selection to avoid numTokensPerCtaQ=0, resulting divide-by-zero crash.

🔍 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

Summary by CodeRabbit

  • Bug Fixes
    • Improved kernel configuration validation to prevent invalid token and head grouping combinations, enhancing system stability and avoiding potential runtime errors in certain processing scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

Skip candidates where kernelMeta.mStepQ < params.mNumHeadsQPerKv in GQA tile selection to avoid numTokensPerCtaQ=0, resulting divide-by-zero crash.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @KungYork, 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 addresses a critical bug in the GQA (Grouped Query Attention) tile selection logic within the FlashInfer library. By introducing a specific validation step, it prevents a potential divide-by-zero crash that could occur when certain kernel metadata conditions lead to an invalid number of tokens per CTA, ensuring more robust and stable operation.

Highlights

  • Crash Prevention in GQA Tile Selection: Implemented a check to skip invalid GQA tile candidates where kernelMeta.mStepQ is less than params.mNumHeadsQPerKv. This condition previously led to numTokensPerCtaQ=0, causing a divide-by-zero crash during kernel execution.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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

  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.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jan 20, 2026

📝 Walkthrough

Walkthrough

The change adds validation guard conditions to skip invalid kernel configurations during candidate selection. Guards check if per-CTA token budgeting conditions are violated when grouping tokens and heads in Q, applied in the main loop and tileSizeQ heuristic selection.

Changes

Cohort / File(s) Summary
Kernel Configuration Validation
include/flashinfer/trtllm/fmha/fmhaKernels.cuh
Adds guards to skip kernel candidates when mGroupsTokensHeadsQ is true and mStepQ is less than mNumHeadsQPerKv. Guards placed in main run loop and selectTileSizeQForGqaGeneration tileSizeQ heuristic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through kernel land,
Adding guards both firm and grand,
"Skip this config!" she declares with care,
Keeping invalid ones out of there,
With careful bounds, the paths run straight,
Flashinfer kernels execute great! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a guard to skip invalid GQA tile candidates when stepQ is less than headsQPerKv.
Description check ✅ Passed The description explains the fix and its purpose (preventing divide-by-zero crash), but the Related Issues section is empty and checklist items are unchecked.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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 fix to prevent a divide-by-zero crash in the GQA tile selection logic by skipping invalid kernel candidates. The change correctly addresses the issue within the kernel selection loop.

My review includes a critical comment about a potential remaining crash scenario where the same bug could manifest earlier in the function. I've also included a medium-severity suggestion to simplify the conditional logic for better readability. Please review the detailed comments for more information.

Comment on lines +629 to +634
// Handle the exception where computeCtaAndClusterConfig results in numTokensPerCtaQ == 0.
if (kernelMeta.mGroupsTokensHeadsQ) {
if (kernelMeta.mStepQ < params.mNumHeadsQPerKv) {
continue; // Skip this candidate as it's invalid
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

This check is crucial for preventing a division-by-zero error. However, the fix appears to be incomplete. A similar crash can occur earlier in the selectTileSizeQForGqaGeneration function.

The computeCtaAndClusterConfig function is also called on line 591, which is before this loop. If the initial kernel parameters result in kernelMeta.mStepQ < params.mNumHeadsQPerKv, the program will crash at line 591, as the check is not present there.

To fully resolve the issue, a similar check should be added before the call to computeCtaAndClusterConfig on line 591.

Comment on lines +630 to +634
if (kernelMeta.mGroupsTokensHeadsQ) {
if (kernelMeta.mStepQ < params.mNumHeadsQPerKv) {
continue; // Skip this candidate as it's invalid
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

For improved readability and conciseness, you can combine these nested if statements into a single condition using the logical AND (&&) operator.

      if (kernelMeta.mGroupsTokensHeadsQ && kernelMeta.mStepQ < params.mNumHeadsQPerKv) {
        continue; // Skip this candidate as it's invalid
      }

@yzh119
Copy link
Copy Markdown
Collaborator

yzh119 commented Jan 20, 2026

Looks like a duplicate of #2380 ?

@KungYork
Copy link
Copy Markdown
Author

Looks like a duplicate of #2380 ?
Yes, the fix is the same. I’ll close this PR.

@KungYork KungYork closed this Jan 20, 2026
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