Skip to content

Permute page table in benchmarking#2194

Merged
yzh119 merged 2 commits intoflashinfer-ai:mainfrom
jhjpark:jopark/fix_benchmark_page_table
Dec 11, 2025
Merged

Permute page table in benchmarking#2194
yzh119 merged 2 commits intoflashinfer-ai:mainfrom
jhjpark:jopark/fix_benchmark_page_table

Conversation

@jhjpark
Copy link
Copy Markdown
Contributor

@jhjpark jhjpark commented Dec 10, 2025

📌 Description

This PR will permute page tables during benchmarking of different backends and ensure the correct page table gets passed to the backends.

🔍 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

  • Tests
    • Improved batch operation tests by introducing randomized page ordering and updated indexing to validate behavior under non-sequential page arrangements, increasing robustness and coverage.
    • Confirmed no changes to public interfaces or external behavior; these updates only strengthen internal test validation.

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 10, 2025

Walkthrough

Changed benchmark tests to generate page tables by applying a random permutation per batch instead of sequential block indices; updated kv_indices construction to pull entries from the permuted block_tables in the three affected tests.

Changes

Cohort / File(s) Summary
Benchmark test updates
benchmarks/routines/attention.py
Replaced sequential block-table generation with per-batch random permutations in testBatchDecodeWithPagedKVCacheWrapper, testBatchPrefillWithPagedKVCacheWrapper, and testBatchMLAPagedAttentionWrapper; updated kv_indices population to use permuted block_tables instead of a consecutive arithmetic range.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Verify permutation indices are in valid range and no duplicates/omissions occur.
  • Check kv_indices mapping logic for off-by-one or ordering mistakes.
  • Confirm deterministic seeding or test stability if randomness matters.

Possibly related PRs

Suggested reviewers

  • cyx-6
  • nvmbreughe

Poem

I hopped through pages, shuffled them bright, 🐇
Each batch now dances in randomized light.
Indices that once marched in a row,
Now twirl and tumble in merry flow.
A rabbit's small cheer for tests on the go.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: permuting page tables in benchmarking routines. It aligns well with the actual modifications to the benchmark code.
Description check ✅ Passed The description provides clear context about permuting page tables during benchmarking and includes most required template sections. However, the Tests section shows tests were not added/updated, and no related issues are documented.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee0f239 and 1b5503c.

📒 Files selected for processing (1)
  • benchmarks/routines/attention.py (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • benchmarks/routines/attention.py

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.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @jhjpark, 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 enhances the benchmarking of paged KV cache operations by introducing random permutation of page tables. This change aims to provide a more robust and realistic evaluation of different backends by ensuring they correctly handle non-sequential page allocations, which is a common scenario in real-world memory management. The modification ensures that the kv_indices accurately reflect these permuted page tables during the benchmark runs.

Highlights

  • Page Table Permutation: The block_tables initialization in benchmarking routines now uses torch.randperm to randomly permute page indices, simulating more realistic, non-sequential memory access patterns.
  • Correct Page Table Application: The kv_indices are now directly populated from the permuted block_tables, ensuring that the benchmark accurately reflects the shuffled page mappings for both batch decode and prefill operations.
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.

@jhjpark jhjpark marked this pull request as ready for review December 10, 2025 00:46
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 modifies the benchmarks/routines/attention.py file, specifically within the testBatchDecodeWithPagedKVCacheWrapper and testBatchPrefillWithPagedKVCacheWrapper functions. The changes introduce randomization to the initialization of block_tables by using torch.randperm for page assignments, moving away from a sequential range. Additionally, the assignment of kv_indices is simplified by directly slicing from the block_tables instead of generating values with torch.arange.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
benchmarks/routines/attention.py (1)

397-405: Block table permutation per sequence is correct; consider a slightly cleaner construction

The new block_tables definition correctly:

  • Keeps each sequence i within its own page range [i * num_pages_per_seq, (i + 1) * num_pages_per_seq).
  • Applies an independent random permutation of pages per sequence, which matches the PR goal of testing non‑trivial page layouts.

One minor style nit: iterating over torch.randperm(num_pages_per_seq) in a list comprehension yields 0‑D tensors, which torch.tensor(...) then converts. Functionally this is fine, but you could make it a bit clearer and avoid 0‑D tensors by constructing directly in Torch, e.g.:

perm = torch.stack(
    [torch.randperm(num_pages_per_seq) for _ in range(batch_size)],
    dim=0,
)
block_tables = (
    perm + torch.arange(batch_size).unsqueeze(1) * num_pages_per_seq
).to(device=device, dtype=torch.int)

Not critical at all, just a readability/clarity option for benchmark setup code.

Also applies to: 833-841

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6bb01d1 and 38ef143.

📒 Files selected for processing (1)
  • benchmarks/routines/attention.py (4 hunks)
🔇 Additional comments (1)
benchmarks/routines/attention.py (1)

421-425: kv_indices now correctly respect the per‑sequence page permutation

Using kv_indices[start_idx:end_idx] = block_tables[i, : end_idx - start_idx] ties the linear kv_indices view directly to the permuted block_tables rows, while end_idx - start_idx matches the page count derived from kv_indptr. This ensures:

  • Each sequence uses exactly its required number of pages.
  • The actual page IDs seen by the backends via kv_indices stay consistent with the permuted block_tables, avoiding mismatches between kernels that consult one vs the other.

This aligns with the PR objective of making the paged‑KV benchmarks exercise non‑trivial page tables while still passing a coherent mapping to all backends.

Also applies to: 883-887

@bkryu
Copy link
Copy Markdown
Collaborator

bkryu commented Dec 10, 2025

Thanks @jhjpark. Let's confirm that we get expected results when we run a larger testlist and leave a comment confirming it is good to go

Copy link
Copy Markdown
Collaborator

@bkryu bkryu left a comment

Choose a reason for hiding this comment

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

Thanks @jhjpark, LGTM

@yzh119 yzh119 enabled auto-merge (squash) December 10, 2025 19:02
jhjpark and others added 2 commits December 11, 2025 09:12
auto-merge was automatically disabled December 11, 2025 17:12

Head branch was pushed to by a user without write access

@jhjpark jhjpark force-pushed the jopark/fix_benchmark_page_table branch from ee0f239 to 1b5503c Compare December 11, 2025 17:12
@yzh119 yzh119 merged commit f2b9cc0 into flashinfer-ai:main Dec 11, 2025
3 of 4 checks passed
BingooYang pushed a commit to BingooYang/flashinfer that referenced this pull request Mar 13, 2026
<!-- .github/pull_request_template.md -->

## 📌 Description
This PR will permute page tables during benchmarking of different
backends and ensure the correct page table gets passed to the backends.
<!-- What does this PR do? Briefly describe the changes and why they’re
needed. -->

## 🔍 Related Issues

<!-- Link any related issues here -->

## 🚀 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

- [x] I have installed `pre-commit` by running `pip install pre-commit`
(or used your preferred method).
- [x] I have installed the hooks with `pre-commit install`.
- [x] 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](https://pre-commit.com/).

## 🧪 Tests

- [ ] Tests have been added or updated as needed.
- [ ] All tests are passing (`unittest`, etc.).

## Reviewer Notes

<!-- Optional: anything you'd like reviewers to focus on, concerns, etc.
-->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Tests**
* Improved batch operation tests by introducing randomized page ordering
and updated indexing to validate behavior under non-sequential page
arrangements, increasing robustness and coverage.
* Confirmed no changes to public interfaces or external behavior; these
updates only strengthen internal test validation.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshua Park <jopark@nvidia.com>
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