Skip to content

Feature: variable sequence length and chunked batching for batch PA#136

Merged
ChaoWao merged 1 commit into
mainfrom
migrate/variable-seq-length
Feb 28, 2026
Merged

Feature: variable sequence length and chunked batching for batch PA#136
ChaoWao merged 1 commit into
mainfrom
migrate/variable-seq-length

Conversation

@ChaoWao
Copy link
Copy Markdown
Collaborator

@ChaoWao 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))
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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

  • Variable Sequence Length Support: Introduced support for variable sequence lengths per batch, configurable via the PA_SEQ_LEN environment variable, allowing for more flexible input processing.
  • Chunked Batching Implementation: Implemented a chunked batching strategy where large batches are split into parallel chunks, defined by IN_CORE_BATCH, to optimize resource utilization and parallel execution.
  • Kernel batch_start Offset: Modified all core kernels (QK_MATMUL, SOFTMAX_PREPARE, PV_MATMUL, ONLINE_UPDATE) to accept a batch_start offset, enabling them to process specific chunks of the overall batch.
  • Softmax NaN Prevention: Added robust handling in the Softmax kernel for valid_len=0 blocks, preventing potential NaN issues by explicitly setting mij to -1e30, lij to 0, and pij to 0.

🧠 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
  • examples/tensormap_and_ringbuffer/batch_paged_attention/golden.py
    • Added new test cases (CaseVarSeq2, CaseVarSeq4) to validate variable sequence length functionality.
    • Updated generate_inputs to dynamically create context_lens based on context_lens_list for variable sequence lengths.
    • Modified compute_golden to print variable context lengths when applicable.
  • examples/tensormap_and_ringbuffer/batch_paged_attention/kernels/aic/aic_pv_matmul.cpp
    • Modified pv_matmul_batch_impl and kernel_entry to accept a batch_start parameter.
    • Adjusted block table indexing to incorporate the batch_start offset.
  • examples/tensormap_and_ringbuffer/batch_paged_attention/kernels/aic/aic_qk_matmul.cpp
    • Modified qk_matmul_batch_impl and kernel_entry to accept a batch_start parameter.
    • Adjusted query and block table indexing to incorporate the batch_start offset.
  • examples/tensormap_and_ringbuffer/batch_paged_attention/kernels/aiv/aiv_online_update.cpp
    • Modified online_update_batch_impl and kernel_entry to accept a batch_start parameter.
    • Adjusted output tensor indexing to incorporate the batch_start offset.
  • examples/tensormap_and_ringbuffer/batch_paged_attention/kernels/aiv/aiv_softmax_prepare.cpp
    • Modified softmax_prepare_batch_impl and kernel_entry to accept a batch_start parameter.
    • Adjusted context length array indexing to incorporate the batch_start offset.
    • Implemented logic to handle valid_len=0 blocks by setting mij to -1e30, lij to 0, and pij to 0 to prevent NaNs.
  • examples/tensormap_and_ringbuffer/batch_paged_attention/kernels/orchestration/paged_attention_orch.cpp
    • Updated documentation comments to describe the new chunked batched architecture.
    • Introduced IN_CORE_BATCH constant and calculated num_chunks for chunked processing.
    • Refactored the orchestration loop to iterate over q_idx and batch_start for chunked processing.
    • Adjusted tensor shapes and kernel parameters to use chunk_bc (chunk batch count) instead of the full batch size.
    • Passed the batch_start offset to all kernel calls (QK_MATMUL, SOFTMAX_PREPARE, PV_MATMUL, ONLINE_UPDATE).
    • Updated logging to include num_chunks and IN_CORE_BATCH for better debug information.
Activity
  • No specific activity (comments, reviews, progress) was provided in the context for this pull request.
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.

Copy link
Copy Markdown

@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 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.

@ChaoWao ChaoWao merged commit f690b10 into main Feb 28, 2026
3 checks passed
PKUZHOU pushed a commit to PKUZHOU/simpler that referenced this pull request Mar 31, 2026
…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))
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.

1 participant