Repository navigation
[HIP] [kernel] mrope cache-quant: accept strided flash KV-cache view - #4531
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
9a2a1fa to
f3e13b5
Compare
There was a problem hiding this comment.
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.
| // 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; | ||
| } |
| // 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; |
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]>
f3e13b5 to
2b90634
Compare
|
The branch is mergeable and the PR-specific validation passes, but one unrelated standard-test shard needs a maintainer rerun:
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]>
Motivation
Allow
fused_qk_norm_mrope_3d_cache_pts_quant_shuffleto write correctly into non-contiguous paged KV-cache views used by vLLM unified attention.The current vLLM view is produced by:
It has logical shape
[num_blocks, block_size, num_heads, head_size]and strides(H*N*2hs, 2hs, N*2hs, 1). Forwarding onlystride(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:
The innermost head-size dimension must remain dense. Contiguous callers retain the existing zero-stride fallback behavior.
Dependency and integration
Rebase provenance
main:fc2e5d57fb5b8ad8e7e23f7103071dde798ea6187c9e637ee070c2daf573a12dd66cc518fe7cc91cValidation
scaled_castconversion, the vLLM transpose/split cache view, and returned K/V buffers — passed.git diff --check— passed.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, revisiond9748a51ae66354c4dad665aab2c71f26cf2c8cd.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.Qwen3VLMoeForConditionalGenerationwithquantization=fp8; the fused attention-prologue log was observed only in the enabled arm.VLLM_Q3VL_FUSE_ATTN_PROLOGUE={0,1}.Warm median of three paired repeats:
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