Skip to content

[PERF] Eliminate varlen prefill GDN D2H synchronization with reusable metadata - #4532

Merged
valarLip merged 3 commits into
ROCm:mainfrom
yiijin:prefill_gdn_cpu
Aug 4, 2026
Merged

valarLip merged 3 commits into
ROCm:mainfrom
yiijin:prefill_gdn_cpu

Conversation

@yiijin

@yiijin yiijin commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Motivation

Variable-length GDR and causal-conv prefill currently derive chunk counts, launch grids, and sequence offsets from device-side sequence metadata during execution. This introduces blocking device-to-host synchronization in the forward path, and the overhead is repeated across layers processing the same batch.

This change moves the required sequence information outside the latency-sensitive forward path so it can be reused across operators and layers. The goal is to eliminate repeated D2H synchronization while preserving existing outputs, legacy call paths, and mixed decode-prefill behavior.

Technical Details

  • Add independent metadata types for GDR and causal-conv prefill.
  • Precompute chunk grids, offsets, and rebased sequence lengths.
  • Thread shared schedules through GDR K1-K6 kernels.
  • Reuse causal-conv grids across HIP, Triton, and FlyDSL backends.
  • Preserve legacy fallbacks and positional solve_tril compatibility.
  • Validate layouts, decode prefixes, tensor identity, and graph-capture constraints.

Test Result

HIP and Triton cold D2H events decreased from 1 to 0.

image

Submission Checklist

@yiijin
yiijin requested review from a team and Copilot August 3, 2026 11:47
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 4532 --add-label <label>

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 introduces reusable, typed prefill-batch metadata for variable-length Gated Delta Rule (GDR) and causal-conv prefill so chunk schedules (grids/offsets/rebased cu_seqlens) can be computed once and reused across layers/backends, eliminating repeated device→host synchronization in the forward path.

Changes:

  • Adds CausalConvPrefillMetadata / GatedDeltaRulePrefillMetadata and builders in a new shared aiter/ops/prefill_batch_metadata.py.
  • Threads prefill_metadata (or host seq_lens_cpu) through GDR K1–K6 prologues and updates Triton kernels to consume separate (sequence_ids, chunk_ids) (+ stride) instead of interleaved chunk indices.
  • Enables causal-conv prefill backends (HIP/Triton/FlyDSL) to reuse prebuilt chunk grids via CausalConvPrefillMetadata.

Reviewed changes

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

Show a summary per file
File Description
aiter/ops/triton/gated_delta_net/gated_delta_rule.py Adds optional seq_lens_cpu / prefill_metadata plumbing to the public Triton GDR entrypoint.
aiter/ops/triton/gated_delta_net/causal_conv1d_prefill.py Accepts typed causal-conv metadata and validates/reuses its chunk grid.
aiter/ops/triton/gated_delta_net/init.py Exposes GDR prefill metadata symbols/builders as part of the Triton gated-delta-net namespace.
aiter/ops/triton/_triton_kernels/gated_delta_rule/utils/solve_tril.py Refactors varlen indexing inputs to (sequence_ids, chunk_ids, INDEX_STRIDE) and updates the solve wrapper accordingly.
aiter/ops/triton/_triton_kernels/gated_delta_rule/utils/prefill_metadata.py Adds compatibility re-exports to the new shared metadata module.
aiter/ops/triton/_triton_kernels/gated_delta_rule/utils/init.py Re-exports GDR metadata/builder from utils.
aiter/ops/triton/_triton_kernels/gated_delta_rule/prefill/fused_solve_tril_recompute.py Adds prefill_metadata support and switches kernels to the new ID-pointer scheme.
aiter/ops/triton/_triton_kernels/gated_delta_rule/prefill/fused_cumsum_kkt.py Adds prefill_metadata support and switches kernels to the new ID-pointer scheme.
aiter/ops/triton/_triton_kernels/gated_delta_rule/prefill/chunk.py Builds/validates reusable metadata and threads it through GDR prefill stages and backend selection.
aiter/ops/triton/_triton_kernels/gated_delta_rule/prefill/chunk_o.py Adds prefill_metadata support and switches kernels to the new ID-pointer scheme.
aiter/ops/triton/_triton_kernels/gated_delta_rule/prefill/chunk_delta_h.py Adds prefill_metadata support for hidden-state prefill scheduling.
aiter/ops/prefill_batch_metadata.py Introduces shared metadata primitives and eager builders for causal-conv and GDR schedules.
aiter/ops/flydsl/linear_attention_prefill_kernels.py Allows FlyDSL GDR prefill wrapper to consume validated reusable metadata.
aiter/ops/flydsl/causal_conv1d_flydsl.py Enables FlyDSL causal-conv prefill to reuse typed chunk-grid metadata.
aiter/ops/chunk_gated_delta_rule_fwd_h.py Extends HIP GDR hidden-state wrapper to accept/build reusable metadata to avoid GPU readback.
aiter/ops/causal_conv1d_fwd_split_qkv.py Enables HIP causal-conv prefill to reuse typed chunk-grid metadata.

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

Comment thread aiter/ops/prefill_batch_metadata.py Resolved
@zufayu
zufayu requested a review from coderfeli August 4, 2026 01:26
@valarLip
valarLip merged commit 1f19459 into ROCm:main Aug 4, 2026
56 checks passed
@IzacharyI IzacharyI mentioned this pull request Aug 11, 2026
5 tasks done
huizzhan added a commit that referenced this pull request Aug 12, 2026
The K5 BV selector needs total_chunks and max_seq_chunks and took them from
chunk_offsets.tolist() on every forward.  That blocking D2H sits between the
K1..K4 launches and K5's, so the host waited for the whole front end to retire
and the GPU then idled through the remaining host work for K5 and K6 rather
than the host running ahead.  The copy itself is 4us; the bubble it opened was
66us of a 738us block.

Prefer the host-side counts prebuilt metadata already carries
(GatedDeltaRuleChunkSchedule.total_chunks / .max_seq_chunks, derived from
seq_lens_cpu).  The old call sat outside the prefill_metadata branch, so
passing metadata did not avoid the transfer -- the case #4532 missed here.
Without metadata, cache the counts on chunk_offsets itself, the same
tensor-attribute pattern as _as_int32 and _resolve_prologue: it comes from the
tensor_cache'd prologue, so its identity is stable across forwards and the
counts cannot change underneath the cache.

varlen-64k-qwen-ptpc-ali T=8192 (Hg=2, H=8, packed varlen with state I/O, one
80-CU gfx942): the block goes 738.1us -> 667.5us, its launch gap 66.1 -> 0.7us,
and Memcpy DtoH disappears from the profile.  The 79 non-perf cases in
test_flydsl_linear_attention_prefill.py are unchanged.

Co-authored-by: Cursor <[email protected]>
huizzhan added a commit that referenced this pull request Aug 12, 2026
The K5 BV selector needs total_chunks and max_seq_chunks and took them from
chunk_offsets.tolist() on every forward.  That blocking D2H sits between the
K1..K4 launches and K5's, so the host waited for the whole front end to retire
and the GPU then idled through the remaining host work for K5 and K6 rather
than the host running ahead.  The copy itself is 4us; the bubble it opened was
66us of a 738us block.

Prefer the host-side counts prebuilt metadata already carries
(GatedDeltaRuleChunkSchedule.total_chunks / .max_seq_chunks, derived from
seq_lens_cpu).  The old call sat outside the prefill_metadata branch, so
passing metadata did not avoid the transfer -- the case #4532 missed here.
Without metadata, cache the counts on chunk_offsets itself, the same
tensor-attribute pattern as _as_int32 and _resolve_prologue: it comes from the
tensor_cache'd prologue, so its identity is stable across forwards and the
counts cannot change underneath the cache.

varlen-64k-qwen-ptpc-ali T=8192 (Hg=2, H=8, packed varlen with state I/O, one
80-CU gfx942): the block goes 738.1us -> 667.5us, its launch gap 66.1 -> 0.7us,
and Memcpy DtoH disappears from the profile.  The 79 non-perf cases in
test_flydsl_linear_attention_prefill.py are unchanged.

Co-authored-by: Cursor <[email protected]>
(cherry picked from commit 24b6f80)
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