feat: support NHD paged KV cache layout via kv_layout="NHD" - #14
Open
alumkal wants to merge 1 commit into
Open
Conversation
Both stacks previously accepted only HND paged caches [num_pages, Hkv, page_size, D]. Add kv_layout="HND"|"NHD" to fmha_sm100, sparse_atten_func, sparse_decode_atten_func, and SparseDecodePagedAttentionWrapper.run; NHD caches [num_pages, page_size, Hkv, D] are consumed zero-copy as strided views. - cute: normalize layout at the interface; replace unconditional k/v.contiguous() with _kv_kernel_view, which forwards tensors that satisfy the TMA contract (contiguous last dim, 16B-aligned base and outer strides) and falls back to the historical compacting copy otherwise. - csrc: plumb the within-page token stride (k_stride_t/v_stride_t) from torch strides through params/jinja into stride_K/stride_V, replacing the hardcoded head_dim token stride. Default 0 keeps stale JIT caches HND-correct; clear the JIT cache dir to pick up NHD support. - docs: fix cute README paged-layout description (claimed NHD while the code required HND) and document kv_layout, including the decode wrapper table row. Verified bitwise HND==NHD on both stacks (cute prefill bf16/fp8, cute dense decode fp8, csrc sparse prefill fp8/bf16, csrc sparse decode fp8); full cute test suite green (1169 passed, 141 pre-existing skips). Perf delta within noise: <=0.07% at 32k bs1 sparse prefill, +0.00% at bs256 32k sparse decode with 16k shared prefix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Serving engines differ on paged-KV cache layout: HND
[num_pages, Hkv, page_size, D]vs NHD[num_pages, page_size, Hkv, D](FlashInfer naming). MSA previously accepted only HND, forcing NHD-based engines to repack or transpose. Both MSA stacks already consume K/V through runtime-stride TMA, so NHD can be served zero-copy — the layout assumption was only skin-deep.What changed
New keyword
kv_layout="HND" | "NHD"(default"HND", fully backward compatible) on all runtime entry points:fmha_sm100(...)sparse_atten_func(...)sparse_decode_atten_func(...)/SparseDecodePagedAttentionWrapper.run(...)fmha_sm100_planis unchanged — plans never see KV tensors.CuTe-DSL (
cute/interface.py)_apply_kv_layoutnormalizes NHD input to a permuted HND-shaped view; kernels consume it through their existing dynamic-stride layouts — no kernel changes.k/v.contiguous()(which silently repacked any strided view) with_kv_kernel_view: forwards tensors satisfying the TMA contract (contiguous last dim, 16 B-aligned base pointer and outer strides), otherwise falls back to the historical compacting copy.csrc JIT (
params.h, both jinjas,fmha_cutlass_sm100.cuh)head_dim. Nowk_stride_t/v_stride_tare taken fromk.stride(2)/v.stride(2)and land instride_K/stride_V; the stride modes were already runtimeints, so this is pure plumbing. Default0selects the oldhead_dimbehavior.Docs (
cute/README.md)kv_layoutare now documented, including the decode-wrapper table.Out of scope
(page, head, token)— a separate contract, left unchanged.q2k_indices) is unimplemented upstream (sparse_decode_atten_funcraisesNotImplementedErrorfor it; sparse decode is served by the csrc sparse path instead).kv_layoutis still forwarded on that branch so NHD works as soon as the q2k gather kernel lands.Verification
kv_indicesraises a clearValueError.Performance (HND vs NHD, CUDA events, cold L2, median)
fmha_sm100sparse prefill 32k bs1 bf16fmha_sm100sparse prefill 32k bs1 fp8NHD is effectively free: TMA box rows are D-contiguous in either layout; only the inter-row stride changes.