add asym feature for chunk prefill and kv cache#403
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR adds support for asymmetric attention where the V/O head dimension may differ from the QK head dimension, updating output sizing, kernel argument plumbing, and KV-cache reshape/copy logic accordingly.
Changes:
- Plumb a new
v_head_sizeparameter through chunk prefill / XE2 FMHA launch paths. - Allocate MHA output tensors using
v.size(-1)for asymmetric attention. - Update KV-cache reshape/copy kernels (including Flash-style paged cache) to handle differing K vs V head sizes and strides.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| csrc/xpu/attn/xe_2/fmha_xe2.cpp | Derives and passes v_head_size from value_cache into chunk prefill impl args. |
| csrc/xpu/attn/xe_2/chunk_prefill.hpp | Adds v_head_size to args and uses it to set VO head size in kernel shape. |
| csrc/flash_attn/flash_api.cpp | Allocates output with last dim following V head dim instead of Q head dim. |
| csrc/cache.cpp | Updates reshape-and-cache kernels to copy keys/values with different head dims and supports distinct value-cache strides in flash path. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
xinyu-intel
left a comment
There was a problem hiding this comment.
please add unit test
0d362fd to
731afce
Compare
added |
…#423) Signed-off-by: Yan Ma <yan.ma@intel.com> Signed-off-by: Yuan Tian <tian.yuan@intel.com>
Signed-off-by: Yuan Tian <tian.yuan@intel.com>
Signed-off-by: Yuan Tian <tian.yuan@intel.com>
624540d to
df6d65c
Compare
|
Description added, please help review, thank you! @xinyu-intel |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
csrc/cache.cpp:1014
- In the
is_strided_headpath, the dispatchedreshape_and_cache_flash_strided_kernelassumes the value head dimension equalshead_size(it indexesv_base + head_idx * head_size_and writes V at offset+ head_size_). With asymmetric attention (v_head_size != head_size), this will read past the end ofvalueand/or corrupt the fused KV-cache layout. Add an explicit check (or extend the strided kernel) so this layout is rejected unlessv_head_size == head_size.
bool is_strided_head = (head_stride != head_size);
if (is_strided_head) {
TORCH_CHECK(
reinterpret_cast<uint8_t*>(value_cache.data_ptr()) ==
reinterpret_cast<uint8_t*>(key_cache.data_ptr()) +
Signed-off-by: Yuan Tian <tian.yuan@intel.com>
|
Pre-commit checked & merged the latest commit to solve the unit-test fail. Could test again. @xinyu-intel |
|
This PR only touches attention code, which is built into a separate |
Signed-off-by: Yuan Tian <tian.yuan@intel.com>
…lm-xpu-kernels-fork into add-asym-feature Signed-off-by: Yuan Tian <tian.yuan@intel.com>
Signed-off-by: Yuan Tian <tian.yuan@intel.com>
Signed-off-by: Yuan Tian <tian.yuan@intel.com>
|
Hi Xinyu, the previous unit-test fail comes from fused-moe, and my asym attention code doesn't touch that part. Please retrigger the test, thanks! @xinyu-intel |
Purpose
Add asymmetric attention support (different QK and V/O head dimensions) to the XPU kernel stack, covering chunk prefill and KV-cache reshape/copy paths.
This is required by models such as MiMo (
MiniMax-M2.5) where the QK head dimension is 192 and the V/O head dimension is 128.Specifically:
fmha_utils.hpp– Add two new tile policies for asymmetric QK=192 / V=128:chunk_policy_head192_vo128(non-paged / block_size=32 path)chunk_policy_head192_vo128_b16(paged block_size=16 path)chunk_prefill.hpp– Addv_head_sizefield tochunk_prefill_args_t; kernel uses it to set the VO head dimension independently from QK. Falls back tohead_sizefor symmetric models (zero value = backward-compatible default).fmha_xe2.cpp– Derivev_head_sizefromvalue_cache.size(-1)and pass it through to args; dispatch to the asymmetric policy whenhead_size == 192 && v_head_size == 128.flash_api.cpp– Allocate the MHA output tensor withv.size(-1)instead ofq.size(-1)so the output shape is correct for asymmetric models.cache.cpp– Update bothreshape_and_cache_kernelandreshape_and_cache_flash_kernelto handle different K and V head sizes and strides:n_k = num_heads * head_size) and V (n_v = num_heads * v_head_size).value_block_stride/value_page_strideparameters to the flash kernel so K-cache and V-cache strides are no longer required to be equal.vectorize_with_alignment<VEC_SIZE>, so onlynum_elems / VEC_SIZEthreads are ever active. The new sizing isceil(num_elems / VEC_SIZE).chunk_prefill_configure.cmake– Register the two new asym policies in the build configuration.Test Plan
Run the existing unit-test suite plus the two new targeted tests:
Test Result
All new unit tests pass on Intel Arc B60 (BMG) XPU:
test_asym_reshape_and_cachetest_asym_reshape_and_cache_flashtest_varlen_with_asym_qk_vo