Skip to content

Conversation

@blueswhen
Copy link
Collaborator

No description provided.

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 @blueswhen, 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!

I've addressed a bug in the flashinfer integration within LightLLM. This fix specifically targets how sequence lengths and KV cache indices are handled when interacting with flashinfer operations for both DeepSeek2 and Llama models. The changes ensure that the correct maximum KV sequence length and KV start indices are passed, resolving potential issues with KV cache dimensions and improving the stability of the flashinfer usage.

Highlights

  • DeepSeek2 FlashInfer Fix: Corrected the parameter passed to flashinfer initialization in lightllm/models/deepseek2/flashinfer_struct.py from "self.max_len_in_batch" to "self.max_kv_seq_len" to ensure the correct maximum KV sequence length is used.
  • Llama FlashInfer Parameter Correction: Updated the flashinfer initialization in lightllm/models/llama/flashinfer_struct.py by replacing "self.max_len_in_batch" with "self.max_kv_seq_len".
  • Llama KV Starts Adjustment: Modified the "kv_starts" parameter passed to the "BatchPrefillWithPagedKVCacheWrapper" in lightllm/models/llama/flashinfer_struct.py from "kv_starts" to "kv_starts[:-1]", likely to correct an off-by-one error in indexing.
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 aims to fix a bug in flashinfer integration. The changes correctly adjust arguments for repack_kv_index calls. Specifically, using kv_starts[:-1] for the prefill path fixes a shape mismatch, and using self.max_kv_seq_len instead of self.max_len_in_batch is a step towards fixing an issue in the decode path. However, the fix for the decode path is incomplete and introduces performance inefficiencies by launching a large number of idle kernel threads. I've added comments with details on this.

self.b_seq_len,
self.b_start_loc,
self.max_len_in_batch,
self.max_kv_seq_len,
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While changing this argument to self.max_kv_seq_len is necessary to correctly size the kernel's execution grid, the fix is incomplete. The repack_kv_index function is still called with self.b_seq_len (on line 39), which is a tensor of 1s during the decode phase. This causes the kernel to only process a single token index per sequence. The change to max_kv_seq_len without also correcting b_seq_len leads to launching many idle kernel threads, causing inefficiency. To fully fix the issue, the seq_len argument should be updated to provide the total sequence lengths.

self.b_seq_len,
self.b_start_loc,
self.max_len_in_batch,
self.max_kv_seq_len,
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While changing this argument to self.max_kv_seq_len is necessary to correctly size the kernel's execution grid, the fix is incomplete. The repack_kv_index function is still called with self.b_seq_len (on line 42), which is a tensor of 1s during the decode phase. This causes the kernel to only process a single token index per sequence. The change to max_kv_seq_len without also correcting b_seq_len leads to launching many idle kernel threads, causing inefficiency. To fully fix the issue, the seq_len argument should be updated to provide the total sequence lengths.

@hiworldwzj hiworldwzj changed the title fix: fix a bug in flashinfer fix: fix accuracy bug in flashinfer and fa3 kernel. Aug 4, 2025
@hiworldwzj hiworldwzj merged commit 10a9b66 into main Aug 4, 2025
1 check passed
@hiworldwzj hiworldwzj deleted the fix_flashinfer branch August 4, 2025 13:37
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