Skip to content

feat(rocke): add ragged cu_seqlens to the SDPA length adapter - #13262

Closed
arai713 wants to merge 2 commits into
developfrom
users/arai/ragged_varlen
Closed

arai713 wants to merge 2 commits into
developfrom
users/arai/ragged_varlen

Conversation

@arai713

@arai713 arai713 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Motivation

Ticket 12 (varlen / THD forward) needs hipDNN's ragged per-sequence lengths turned into the cumulative cu_seqlens the varlen kernels address with. This is the host-side, non-blocked slice of that ticket, and it extends the same length adapter introduced for padding (ticket 10) rather than adding a parallel one.

Technical Details

  • Add sdpa_cu_seqlens: validates hipDNN's ragged per-sequence counts (integer dtype, exactly batch counts, non-negative) and returns (cu_seqlens_q, cu_seqlens_kv) as contiguous (B+1,) int32 exclusive prefix sums with a leading 0, whose last element is the packed token total.
  • This is exactly the form fmha_varlen consumes: it takes cu_seqlens_q / cu_seqlens_k as (B+1,) i32 exclusive prefix sums and recovers each span as cu[i+1] - cu[i]. (The adapter names the key side kv to match hipDNN's SEQ_LEN_KV and the padded path, the kernel param is cu_seqlens_k.)
  • Ragged packing has no padding, so there is no per-sequence upper bound, the shared length validator is refactored to make the S_max check optional, with the negative-length check split out to its own message. The padded path (sdpa_seqlens) is behaviorally unchanged.

Test Plan

  • Ragged adapter tests: exclusive prefix sum, zero-length sequences, negative / wrong-count / non-integer rejection
  • Existing padded adapter tests still pass after the shared-validator refactor
  • Padding-mask spec suite unregressed
  • Library layering guard (kernels imports nothing upward)

Test Result

  • Host-only suites green with no GPU and no torch: the length-adapter suite (padded + ragged), the padding-mask spec suite, the numeric-reference oracle, and the layering guard all pass.
  • Adapter output was cross-checked against fmha_varlen's documented cu_seqlens contract ((B+1,) i32 exclusive prefix sum).

Related

JIRA ID : AICK-2521

Submission Checklist

…port

- dense_attention_reference gains optional seqlen_q / seqlen_kv. None keeps
  the exact unpadded arithmetic. When set, keys past seqlen_kv are masked,
  a row with no valid key yields O = 0, and padded query rows are zeroed.
- _sdpa_seqlen.py: host adapter validating hipDNN's per-sequence lengths at
  the boundary (integer dtype, per-batch count, 0 <= len <= S_max) and
  returning the flat per-batch int32 arrays the shell reads.
- _sdpa_padding_mask.py: host specification of the kernel-side mask --
  keep_key (the shell's extra_mask_predicate hook) and row_is_valid (the
  epilogue's padded-row zeroing), with the emitter form documented. The
  live IR emitter is deferred to the shell, where it carries its
  byte-identity C++ mirror. This module is the host reference it must match,
  cross-checked against dense_attention_reference.

Kernel wiring and GPU validation wait on the shell (ST4) and the arbitrary-length
buffer clipping (ticket 8).
Ticket 12 (varlen/THD forward) needs hipDNN's ragged per-sequence lengths
converted into the cumulative cu_seqlens the varlen kernels (fmha_varlen)
address with. Extend the shared SDPA length adapter with sdpa_cu_seqlens,
which validates the per-sequence counts and returns (B+1,) int32 exclusive
prefix sums (leading 0, last element = packed token total). The validator
is refactored to make the upper bound optional, since ragged tokens are
packed with no padding and therefore no per-sequence S_max.
Host numpy only, no kernel emission. Covers the host/adapter half of
ticket 12, the non-causal unified no-mask path and
arbitrary-length tails are separate work.
@github-actions github-actions Bot added project: hip-kernel-provider rocke-lib: attention Touches rocKE library attention/FMHA kernels rocke: platform Touches rocKE platform labels Oct 7, 2026
@therock-pr-bot

therock-pr-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

⏳ Waiting on Required Checks

⏳ Required checks have not all concluded yet. This bot run is exiting without failing so those checks can report independently.

Check Status Details
📝 PR Description ✅ Pass —
⛔ Forbidden Files ✅ Pass —
🧪 Unit Test ✅ Pass —
🔎 pre-commit ⏳ Pending ⏳ Still running…
🚫 Draft PR 🔜 To Be Enabled —
🚩 Feature Flag 🔜 To Be Enabled —
📊 Code Coverage 🔜 To Be Enabled —

⏳ Required checks are still running or have not started yet.
This PR is not ready for review until they complete.

📖 Need help? See the Policy FAQ for details on every check and how to fix failures.

🙋 Wish to Override Policy?

@therock-pr-bot

therock-pr-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

⏳ Waiting for required checks to start or finish:

  • ⏳ pre-commit

@arai713
arai713 marked this pull request as ready for review October 15, 2026 19:17
@arai713
arai713 requested review from a team as code owners October 15, 2026 19:17
@arai713 arai713 closed this Oct 19, 2026
@arai713

arai713 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

Moved to this PR: #13953

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

organization: ROCm project: hip-kernel-provider rocke: platform Touches rocKE platform rocke-lib: attention Touches rocKE library attention/FMHA kernels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant