Skip to content

[HIP] [kernel] mrope cache-quant: accept strided flash KV-cache view - #4531

Merged
valarLip merged 5 commits into
ROCm:mainfrom
vorapolsiloai:mrope-cache-quant-strided-kv
Aug 21, 2026
Merged

valarLip merged 5 commits into
ROCm:mainfrom
vorapolsiloai:mrope-cache-quant-strided-kv

Conversation

@vorapolsiloai

@vorapolsiloai vorapolsiloai commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Allow fused_qk_norm_mrope_3d_cache_pts_quant_shuffle to write correctly into non-contiguous paged KV-cache views used by vLLM unified attention.

The current vLLM view is produced by:

kv_cache.transpose(1, 2).split(head_size, dim=-1)

It has logical shape [num_blocks, block_size, num_heads, head_size] and strides (H*N*2hs, 2hs, N*2hs, 1). Forwarding only stride(0) is not sufficient: token and head strides also differ from contiguous layout and incorrect addressing can silently corrupt the cache.

Changes

This PR contains four focused commits:

  1. Accept a flash-layout view and forward the K/V block strides.
  2. Forward all six K/V block, token, and head strides required by the unified-attention transpose/split view.
  3. Optimize the same kernel with AITER's shared packed FP8 conversion path and power-of-two address arithmetic; launch-time checks enforce the address-math preconditions.
  4. Add an operator regression covering the exact vLLM view and compare its cache writes bit-for-bit with the contiguous path.

The innermost head-size dimension must remain dense. Contiguous callers retain the existing zero-stride fallback behavior.

Dependency and integration

  • vLLM #50212: hard-depends on this PR for its fused Qwen3-VL attention prologue.
  • This PR — AITER #4531: full strided MRoPE KV-cache correctness.
  • AITER #4554: gfx950 Qwen3-VL MXFP4 tuning rows; not required for the public FP8 reproduction below.
  • AITER #4556: optional MXFP4 FlyDSL stage-2 override; not exercised by the FP8 checkpoint below.
  • AITER #4761: independent Triton unified-attention prefill/decode optimization; not required by this stride fix.

Rebase provenance

  • Frozen AITER main: fc2e5d57fb5b8ad8e7e23f7103071dde798ea618
  • Current PR head: 7c9e637ee070c2daf573a12dd66cc518fe7cc91c

Validation

  • Targeted gfx950 MRoPE/FP8 operator tests using AITER's shared scaled_cast conversion, the vLLM transpose/split cache view, and returned K/V buffers — passed.
  • The test performs a strict bit-for-bit comparison of strided and contiguous cache writes for the same inputs.
  • BF16 query output and FP8 value-cache reference checks passed; the existing reference harness's normal FP8 key quantization tolerance is unchanged from the contiguous case.
  • Black and Ruff checks on the operator test — passed.
  • Python compile and git diff --check — passed.
  • GitHub reports the branch mergeable.

Public FP8 downstream reproduction

This PR and vLLM #50212 were reproduced without MXFP4 weights using the public checkpoint Qwen/Qwen3-VL-30B-A3B-Instruct-FP8, revision d9748a51ae66354c4dad665aab2c71f26cf2c8cd.

Test configuration: MI355X (gfx950), vLLM 0.26.0 plus the two PR heads above, TP1, ROCM_AITER_UNIFIED_ATTN, FP8 model weights, FP8 E4M3 KV cache, one 512×512 image per request, random input/output lengths 256/128, max concurrency 4, 16 requests and 4 warmups per repeat.

  • The PR's operator regression passed with the exact unified-attention strided view and FP8 cache, including strict bit-for-bit strided-vs-contiguous cache writes.
  • vLLM #50212's focused safety/layout suite passed: 3/3 tests.
  • The end-to-end server resolved Qwen3VLMoeForConditionalGeneration with quantization=fp8; the fused attention-prologue log was observed only in the enabled arm.
  • All 96 measured requests succeeded. Paired runs used the same seeds and generated-token counts and differed only by VLLM_Q3VL_FUSE_ATTN_PROLOGUE={0,1}.

Warm median of three paired repeats:

metric fusion on fusion off delta
output throughput 568.36 tok/s 500.20 tok/s +13.6%
mean TPOT 5.97 ms 6.93 ms -13.8%
mean end-to-end latency 798.65 ms 920.30 ms -13.2%

All three paired throughput results improved: +10.0%, +13.6%, and +18.4%. This is downstream validation of the safe fused stack, not an isolated performance claim for the stride fix. It demonstrates that MXFP4 weights are not required to validate this PR or its vLLM integration.

Submission checklist

  • Reviewed the ROCm contribution guidelines.
  • Added coverage for the actual downstream layout.
  • Preserved contiguous behavior.
  • Documented both correctness and included optimizations.
  • Reproduced downstream integration with a public FP8 checkpoint.

This PR was developed with AI assistance. The human author reviewed the changes, ran the listed validation, and owns the final implementation. Commit metadata contains only human attribution and DCO trailers.

@vorapolsiloai
vorapolsiloai requested a review from a team August 3, 2026 11:06
@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 4531 --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 updates the fused MRoPE QK-norm + KV-cache quantization path to correctly write into non-contiguous (strided) KV-cache views used by vLLM unified attention, and adds a regression test for that layout.

Changes:

  • Allow non-contiguous K/V cache tensors by forwarding block/token/head strides into the kernel instead of assuming contiguous layout.
  • Optimize address math for paged/shuffle layouts via power-of-two div/mod helpers and add launch-time precondition checks.
  • Add an operator regression that constructs the exact vLLM transpose/split KV-cache view and compares strided vs contiguous writes.

Reviewed changes

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

File Description
op_tests/test_fused_qk_norm_mrope_cache_quant.py Adds a regression covering the vLLM unified-attention strided KV-cache view and compares cache writes against the contiguous path.
csrc/kernels/rope/rope_common.h Adds fp8 packed conversion fast-path and power-of-two address arithmetic helpers with host-side precondition checks.
csrc/kernels/fused_qk_norm_mrope_cache_quant.cu Removes contiguity requirement for K/V caches, validates dense innermost stride, and forwards cache strides into the kernel.

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

Comment on lines +6 to +11
// The innermost dim must be dense: the kernel stores whole vectors per head, so a
// non-unit element stride cannot be expressed by the block/token/head strides below.
static inline bool has_unit_element_stride(const aiter_tensor_t& t)
{
return t.numel() == 0 || t.stride(t.dim() - 1) == 1;
}
Comment on lines +52 to +60
// Block/token/head strides taken from the tensors rather than assumed: a transposed or
// interleaved view diverges from the contiguous (num_heads*hs, hs) layout on dims 1 and 2,
// not just dim 0. Missing dims pass 0, which the kernel treats as "assume contiguous".
int64_t k_block_stride_ = k_cache.stride(0);
int64_t v_block_stride_ = v_cache.stride(0);
int64_t k_token_stride_ = k_cache.dim() >= 2 ? k_cache.stride(1) : 0;
int64_t v_token_stride_ = v_cache.dim() >= 2 ? v_cache.stride(1) : 0;
int64_t k_head_stride_ = k_cache.dim() >= 3 ? k_cache.stride(2) : 0;
int64_t v_head_stride_ = v_cache.dim() >= 3 ? v_cache.stride(2) : 0;
@eppaneamd
eppaneamd self-requested a review August 6, 2026 10:31
@yzhou103
yzhou103 requested a review from xytpai August 7, 2026 07:52
@xytpai xytpai added the ci:atom label Aug 10, 2026
vorapolsiloai and others added 4 commits August 18, 2026 13:45
fused_qk_norm_mrope_3d_cache_pts_quant_shuffle asserts that k_cache and
v_cache are contiguous, which aborts when the KV cache is a strided
flash-layout view (num_blocks, 2, block, heads, hs)[:, 0] -- non-contiguous
but well-formed. This blocks callers whose flash KV cache is exactly that
view, e.g. vLLM.

Relax the assert to slot_mapping only, compute k_cache.stride(0) /
v_cache.stride(0), and thread them through fused_mrope_rms_set_kv as
k_block_stride / v_block_stride instead of the hardcoded 0.

fused_mrope_rms_kv_kernel already accepts these parameters, and the
non-mrope fused_rope_rms_set_kv already exposes them; this closes the
asymmetry for the mrope path. Both default to 0, preserving existing
behaviour for contiguous callers.

Co-authored-by: Santosh Hiremath <[email protected]>
Signed-off-by: Vorapol Assavasangthong <[email protected]>
…lock

The previous commit passes k_cache.stride(0) / v_cache.stride(0) and lets the
kernel assume the remaining dimensions are contiguous. That holds for a
blocks-first view -- (num_blocks, 2, block, heads, hs)[:, 0] differs from a
contiguous cache on dim 0 only -- but not for every strided view a caller can
hand us.

vLLM main splits its unified-attention KV cache as
kv_cache.transpose(1, 2).split(head_size, dim=-1), producing strides
(H*N*2hs, 2hs, N*2hs, 1). Both the token stride (2hs, not num_heads*hs) and
the head stride (N*2hs, not hs) diverge from the contiguous assumption, so the
kernel writes each token and each head to the wrong offset. Nothing faults and
throughput is unchanged -- the cache is simply wrong, which a perf benchmark
will not catch.

Read all six strides off the tensors and forward them.
fused_mrope_rms_kv_kernel already accepts k/v token and head strides and
falls back to the contiguous layout when they are 0, so contiguous callers are
unaffected. Replace the blanket is_contiguous assert with a check that the
innermost dimension is dense, which is the one property the strides cannot
express.

Co-authored-by: Santosh Hiremath <[email protected]>
Co-authored-by: Rony Leppanen <[email protected]>
Signed-off-by: Vorapol Assavasangthong <[email protected]>
… math

Three address- and convert-path optimisations in the mrope fused kernel, all
confined to namespace mrope_utils (vec_t::from_ has two callers, both here).

- vec_t::from_ hoists the type dispatch out of the element loop and, on MI300
  and gfx950, converts fp8 output two lanes at a time with
  __builtin_amdgcn_cvt_pk_fp8_f32. Values are clamped with fmed3 first, since
  the packed convert saturates differently from the scalar cast.
- The scalar fallback computes 1.0f / scale once and multiplies, instead of a
  full divide per element. scale is uniform across the vector.
- Paged and shuffle-layout offsets use shift/mask instead of integer division.
  block_size and x are kernel parameters and therefore warp-uniform, so
  __builtin_ctz lowers to a single s_ff1_i32_b32 and each lane pays a shift
  rather than the ~30-cycle division emulation.

The last one only holds if block_size and x are powers of two. Every vLLM
block_size and every x = 16 / sizeof(elem) satisfies it, but the failure mode
would be a silently misaddressed cache, so both launchers now TORCH_CHECK the
precondition rather than assume it.

Co-authored-by: Rony Leppanen <[email protected]>
Signed-off-by: Vorapol Assavasangthong <[email protected]>
Exercise the exact transpose-and-split KV view used by vLLM and compare it
bit-for-bit with the contiguous cache path.

Signed-off-by: Vorapol Assavasangthong <[email protected]>
@vorapolsiloai
vorapolsiloai force-pushed the mrope-cache-quant-strided-kv branch from f3e13b5 to 2b90634 Compare August 18, 2026 14:02
@github-actions github-actions Bot changed the title [kernel] mrope cache-quant: accept strided flash KV-cache view [HIP] [kernel] mrope cache-quant: accept strided flash KV-cache view Aug 18, 2026
Comment thread csrc/kernels/rope/rope_common.h Resolved
@vorapolsiloai

Copy link
Copy Markdown
Contributor Author

The branch is mergeable and the PR-specific validation passes, but one unrelated standard-test shard needs a maintainer rerun:

  • linux-aiter-mi35x-1, shard 4 failed in op_tests/test_moe_2stage.py with torch.OutOfMemoryError (requested 21.05 GiB; 14.64 GiB free; 229.52 GiB already allocated).
  • The same unrelated shard also exhausted GPU memory in [PERF] Add opt-in atomic stage2 override #4556, which points to a suite resource issue rather than this MRoPE/KV-cache change.
  • Fork authors do not have permission to invoke the job rerun API.

Could a maintainer please rerun the failed shard?

Follow the established AITER scaled_cast path so FP8 clamping, architecture format selection, and packed conversion stay consistent across kernels.

Signed-off-by: Vorapol Assavasangthong <[email protected]>
@vorapolsiloai
vorapolsiloai requested a review from valarLip August 19, 2026 06:11
@valarLip
valarLip merged commit b0cc539 into ROCm:main Aug 21, 2026
49 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants