Feature: variable sequence length and chunked batching for batch PA#136
Conversation
ChaoWao
commented
Feb 28, 2026
- PA_SEQ_LEN env var for per-batch variable sequence lengths (e.g. PA_SEQ_LEN=33,64,17,128 for different lengths per batch)
- IN_CORE_BATCH=16 chunking splits large batches into parallel chunks
- All kernels accept batch_start offset for chunked processing
- Softmax: handle valid_len=0 blocks (mij=-1e30, lij=0, pij=0) to avoid NaN from exp(-inf - (-inf))
- PA_SEQ_LEN env var for per-batch variable sequence lengths (e.g. PA_SEQ_LEN=33,64,17,128 for different lengths per batch) - IN_CORE_BATCH=16 chunking splits large batches into parallel chunks - All kernels accept batch_start offset for chunked processing - Softmax: handle valid_len=0 blocks (mij=-1e30, lij=0, pij=0) to avoid NaN from exp(-inf - (-inf))
Summary of ChangesHello, 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 the batch paged attention mechanism by introducing variable sequence length support and a chunked batching strategy. These changes improve flexibility and efficiency, particularly for handling diverse input sequences and optimizing resource utilization by processing large batches in smaller, parallelizable segments. Additionally, a critical fix was implemented in the softmax calculation to ensure numerical stability when encountering empty attention blocks. Highlights
🧠 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. Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces two significant features: support for variable sequence lengths and chunked batching for paged attention. While the implementation correctly handles chunked processing via a batch_start offset and the softmax kernel behavior, several memory safety issues were identified in the kernels. Specifically, the kernels assume that the number of heads is a multiple of the hardcoded tile size (16) and that the maximum number of blocks required by any sequence does not exceed the allocated capacity of the block_table. These assumptions could lead to potential out-of-bounds reads and writes when processing global tensors like query, out, and block_table. It is recommended to address these by adding proper validation in the orchestration function or implementing masking logic within the kernels. Additionally, there are a couple of minor suggestions to improve code clarity and style in the orchestration logic.
…w-native-sys#136) - PA_SEQ_LEN env var for per-batch variable sequence lengths (e.g. PA_SEQ_LEN=33,64,17,128 for different lengths per batch) - IN_CORE_BATCH=16 chunking splits large batches into parallel chunks - All kernels accept batch_start offset for chunked processing - Softmax: handle valid_len=0 blocks (mij=-1e30, lij=0, pij=0) to avoid NaN from exp(-inf - (-inf))