Skip to content

[Kernel][Perf] Optimize paged-attention metadata decode - #910

Open
fsx950223 wants to merge 8 commits into
mainfrom
refactor/pa-metadata-tile-programming
Open

[Kernel][Perf] Optimize paged-attention metadata decode#910
fsx950223 wants to merge 8 commits into
mainfrom
refactor/pa-metadata-tile-programming

Conversation

@fsx950223

Copy link
Copy Markdown
Contributor

Summary

Paged-attention metadata generation now uses the tile-programming path and shape/device-tuned persistent grids. The resulting decode-plus-reduce path is faster across representative query lengths and quantization modes while retaining a safe one-grid-per-CU default for untuned shapes.

Motivation

The previous metadata implementation mixed legacy data movement with a fixed grid oversubscription. A single grid choice performed well for some Q4 per-token workloads but regressed lower-Q and per-tensor shapes, so the scheduling choice needs to follow measured shape and device characteristics.

Changes

  • Move block-1024 metadata decode and reduction data movement onto FlyDSL layout/copy APIs.
  • Reduce exposed scale, LDS, and reducer overhead in the persistent decode path.
  • Load exact grid multipliers from a validated CSV keyed by device and shape; unknown entries default to grid_multiplier=1.
  • Add an offline CUDA-Graph-based tuner that atomically updates the CSV and checks candidate correctness.

Performance (MI308X, gfx942, 80 CUs)

GPU-only p50 from 35 rocprofv3 dispatches. Times cover the metadata decode plus split reducer for block size 1024, 16 query heads, one KV head, and head dimension 128.

Configuration Before After Improvement
B1 / C1024 / Q1 / per-tensor 11.84 us 12.08 us -2.0%
B16 / C4096 / Q1 / per-tensor 25.72 us 22.52 us 12.4%
B81 / C8192 / Q1 / per-tensor 66.52 us 65.84 us 1.0%
B81 / C8192 / Q4 / per-tensor 219.40 us 184.72 us 15.8%
B3 / C1027 / Q4 / per-token 21.80 us 21.16 us 2.9%
B64 / C4096 / Q2 / per-token 71.32 us 59.92 us 16.0%
B64 / C4096 / Q3 / per-token 114.24 us 81.80 us 28.4%
B16 / C8192 / Q4 / per-token 105.40 us 75.00 us 28.8%
B81 / C8192 / Q4 / per-token 330.80 us 263.40 us 20.4%
B128 / C8192 / Q4 / per-token 502.48 us 415.64 us 17.3%
B8 / C32768 / Q4 / per-token 164.36 us 122.20 us 25.7%
B256 / C2048 / Q4 / per-token 273.92 us 232.00 us 15.3%

Geometric-mean speedup is 1.19x; 11 of 12 measured shapes improve. The sole underfill delta is 0.24 us at an approximately 12 us launch-scale runtime.

Testing

  • python3 -m pytest -q tests/unit/test_pa_metadata_tuning.py
  • FLYDSL_RUNTIME_ENABLE_CACHE=0 python3 -m pytest -q tests/kernels/test_pa.py -k metadata
  • Black, Ruff, and git diff --check
  • Performance sweep on AMD Instinct MI308X

Dependencies

  • No new third-party dependencies added

Breaking Changes

None. Existing callers remain valid; tuned precomputed metadata may optionally provide per_token_kv for scale-mode-specific lookup.

fsx950223 and others added 3 commits July 24, 2026 08:25
Signed-off-by: fsx950223 <fsx950223@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: fsx950223 <fsx950223@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: fsx950223 <fsx950223@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings July 27, 2026 05:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes paged-attention (PA) metadata generation and the block-1024 persistent decode path by moving metadata decode/reduction onto FlyDSL tile-programming APIs and introducing shape/device-tuned persistent grid sizing via a CSV lookup + an offline tuner.

Changes:

  • Refactors PA metadata scheduler + persistent decode kernel to use FlyDSL layout/copy primitives and reduce per-token scale/reduction overhead.
  • Adds a CSV-backed grid-multiplier lookup (pa_metadata_grid_tuning.csv) with read/write utilities plus an offline CUDA-Graph-based tuner script.
  • Extends PA tests with metadata-specific accuracy and validation coverage (incl. block-size and dtype rejection cases).

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/unit/test_pa_metadata_tuning.py Adds unit tests for tuning CSV read/write/upsert and lookup behavior.
tests/kernels/test_pa.py Adds metadata accuracy + validation tests; passes per_token_kv through metadata creation.
scripts/tune_pa_metadata_grid.py Adds an offline tuner that benchmarks candidate grid multipliers and writes winners into the CSV.
kernels/attention/pa_metadata.py Major refactor/optimization: new tile-programming decode path (block_size=1024 only) and updated reduction kernel.
kernels/attention/pa_metadata_tuning.py Implements CSV schema, normalization, cached lookup, and atomic upsert writer.
kernels/attention/pa_metadata_grid_tuning.csv Provides initial validated tuning entries (gfx942/80CU shapes).
kernels/attention/pa_decode_fp8.py Integrates tuning lookup into metadata generation and updates metadata API usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# resident per CU → more waves in flight → better latency hiding.
base_cu = props.multi_processor_count
num_sm = base_cu * _PA_METADATA_GRID_OVERSUB
num_blocks = key_cache.shape[0]
Comment thread kernels/attention/pa_metadata_tuning.py Outdated
fsx950223 and others added 5 commits July 27, 2026 06:00
Signed-off-by: fsx950223 <fsx950223@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: fsx950223 <fsx950223@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
…tile-programming

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	python/flydsl/autotune.py
Signed-off-by: fsx950223 <fsx950223@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: fsx950223 <fsx950223@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.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.

2 participants