Skip to content

Pass block table pointer instead of 64 individual scalar args#321

Merged
poursoul merged 1 commit intohw-native-sys:mainfrom
hw-native-sys-bot:pass-block-table-ptr-instead-of-scalars
Mar 18, 2026
Merged

Pass block table pointer instead of 64 individual scalar args#321
poursoul merged 1 commit intohw-native-sys:mainfrom
hw-native-sys-bot:pass-block-table-ptr-instead-of-scalars

Conversation

@hw-native-sys-bot
Copy link
Collaborator

Summary

  • Replace add_scalars_i32(bt_base + bn, N_UNROLL) with a single add_scalar of the device pointer in the paged attention unroll orchestration
  • AICore QK/PV kernels now read block indices directly from GM via pointer instead of unpacking 64 scalars from the args array
  • Reduces QK/PV task args from 68 to 5 per submission; removes copy_scalars_from dependency between QK and PV params

Testing

  • Hardware device test passes (batch=256, num_heads=16, head_dim=128, context_len=8192) — all 524,288 elements match golden
  • Benchmark: performance neutral (~1459us original vs ~1468us modified, within noise)

Replace add_scalars_i32(bt_base + bn, N_UNROLL) with a single
add_scalar of the device pointer. AICore kernels now read block
indices directly from GM via the pointer instead of unpacking
64 scalars from the args array. Reduces QK/PV task args from
68 to 5 per submission.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link

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 refactors the paged attention unroll orchestration and associated AICore kernels to optimize how block table information is passed. Instead of sending 64 individual scalar block indices, the system now passes a single device pointer to the block table, significantly reducing the number of task arguments and simplifying the data transfer mechanism. This change aims to improve efficiency and maintain performance neutrality.

Highlights

  • Argument Reduction: Reduced the number of arguments passed to QK/PV tasks from 68 to 5 per submission, streamlining kernel calls.
  • Pointer-based Block Table Access: AICore QK/PV kernels now directly read block indices from global memory via a pointer, eliminating the need to unpack 64 individual scalars from the arguments array.
  • Orchestration Simplification: Replaced the add_scalars_i32 function with a single add_scalar for the device pointer in the paged attention unroll orchestration, and removed the copy_scalars_from dependency between QK and PV parameters.

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

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

@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 paged attention kernels to pass a pointer to the block table instead of 64 individual scalar arguments. This simplifies the orchestration logic by removing the add_scalars_i32 and copy_scalars_from calls, and reduces the number of arguments passed to the kernels. The changes in the AICore kernels correctly adapt to read the block indices from the pointer. My feedback includes a suggestion to further optimize performance by pre-loading the block indices from global memory into a local array within the kernels to avoid repeated GMEM accesses in loops.

__gm__ float* oi_base,
uint64_t n_blocks,
uint64_t* block_indices) {
__gm__ int32_t* block_table) {

Choose a reason for hiding this comment

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

medium

Similar to the qk_matmul kernel, this function now accesses block_table directly from global memory. This happens at line 75 (block_table[0]) and inside the loop at line 112 (block_table[i + 1]).

To avoid potential performance degradation from repeated GMEM reads, you could pre-load the necessary block indices into a local array at the start of the function. This would consolidate GMEM access into a single burst read.

Example:

// At the top of the file:
constexpr int kMaxBlocks = 64;

// In pv_matmul_n_impl:
int32_t local_block_table[kMaxBlocks];
for (uint64_t i = 0; i < n_blocks; ++i) {
    local_block_table[i] = block_table[i];
}

// Then use local_block_table for accesses.
GlobalB vjGlobal_0(val_base + local_block_table[0] * K * N);
// ... and in the loop ...
GlobalB vjGlobal_next(val_base + local_block_table[i + 1] * K * N);

This change would make the kernel more robust to memory latency variations.

__gm__ float* sij_base,
uint64_t n_blocks,
uint64_t* block_indices) {
__gm__ int32_t* block_table) {

Choose a reason for hiding this comment

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

medium

While passing the block_table pointer is a great simplification for the orchestration, accessing it directly from global memory inside the loop at line 70 (block_table[i]) might introduce performance overhead due to repeated GMEM reads.

Consider pre-loading the block indices into a stack-allocated local array at the beginning of this function. This would perform a single burst read from GMEM and subsequent accesses within the loop would be much faster.

For example:

// At the top of the file, you could define:
constexpr int kMaxBlocks = 64;

// Then, inside qk_matmul_n_impl:
int32_t local_block_table[kMaxBlocks];
// A simple loop or a memcpy-like instruction could be used to load the data.
for (uint64_t i = 0; i < n_blocks; ++i) {
    local_block_table[i] = block_table[i];
}

// And in the main loop:
for (uint64_t i = 0; i < n_blocks; i++) {
    GlobalB kjGlobal(key_base + local_block_table[i] * N * K);
    // ...
}

Although you've noted performance is neutral, this is a good practice that could yield benefits, especially if n_blocks were larger or if memory access patterns change.

@poursoul poursoul merged commit d809f52 into hw-native-sys:main Mar 18, 2026
5 checks passed
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