Repository navigation
cuda: support IQ4_NL in the flash attention KV cache - #29293
yeahdongcn wants to merge 2 commits into
Conversation
The iq4_nl type could be selected for the KV cache but the flash attention
kernels rejected it in three places, so such a cache silently disabled FA and
the model fell back to the non-FA attention path:
- ggml_cuda_fattn_kv_type_supported() did not list GGML_TYPE_IQ4_NL
- the nc f16 converter set had no iq4_nl entry, so a quantized cache of that
type had no conversion kernel
- dequantize_block_iq4_nl() indexes 8 sub-blocks of QK4_NL per launch block
but has no bounds check, so the tail of a 32-wide block read out of range
Add dequantize_iq4_nl_pair() next to the other pair converters, register it in
the converter sets, add the bounds check in convert.cu and list IQ4_NL in the
flash attention type whitelist.
test-backend-ops -o FLASH_ATTN_EXT on an MTT S5000 (mp_31, MUSA SDK 5.2.0) and
on an H20-3e (CUDA 12.4) both report 4320 ok / 0 fail / 987 unsupported: the 336
iq4_nl KV cases move from unsupported to supported, and none of the remaining
unsupported cases is iq4_nl (the same build without this patch reports 3984 ok /
1323 unsupported). With Qwen3.8-27B-UD-Q4_K_M on the S5000 and an iq4_nl KV
cache, -fa on prefill goes from 547.16 to 751.77 t/s (-t 64), and perplexity is
4.6453 against 4.6271 for an f16 KV cache.
Note: with the default GGML_CUDA_FA_QUANTS list the vector kernel still has no
iq4_nl KV kernel and converts K and V to f16 (the dispatch logs that warning),
so this makes the type usable rather than giving the quantized inner loop.
__MUSA_ARCH__ is only defined in the device pass. In the host pass the existing guard evaluated it as 0, so FLASH_ATTN_AVAILABLE was never defined for a MUSA build and ggml_cuda_get_best_fattn_kernel() always returned BEST_FATTN_KERNEL_NONE: the MUSA backend reported every FLASH_ATTN_EXT case as unsupported (0/5305 supported on S5000/mp_31). Test for the macro being defined so the guard only prunes device code for pre-QY2 targets, and gate the host-side dispatch on the runtime cc so a multi-arch build cannot dispatch FA to a device without FA code.
|
Hi @yeahdongcn, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
@JohannesGaessler I'm afraid I can't make this PR |
|
I'm not convinced that IQ4_NL actually makes sense as a KV cache type and you're not showing any data to support that assumption either. So this PR is not worth the opportunity cost to review. |
Overview
ggml_cuda_fattn_kv_type_supportednow acceptsGGML_TYPE_IQ4_NLas a KV cache type, together with the dequantization counterpart the flash attention kernels use to read that cache:dequantize_iq4_nl_pairand theto_fp16_nc_cuda/to_bf16_nc_cuda/to_fp32_nc_cudadispatch entries forIQ4_NL. This makes-ctk iq4_nl -ctv iq4_nl -fa onwork instead of falling back.FLASH_ATTN_AVAILABLEis a property of the compilation pass rather than of the device:__MUSA_ARCH__is only defined in the device pass, so in every host pass the old condition removed the FA device code from the translation unit, and availability was decided at runtime today. The MUSA part of the guard is now additionally conditioned ondefined(__MUSA_ARCH__), and [the runtime check in the host code] rejects devices whose MUSA architecture has no FA device code (onlyQY2/mp_22and later have it).The first change affects CUDA as well; the second is MUSA specific and does not change behaviour on devices that have FA code, such as the MTT S5000 (
mp_31) used below.Testing Done
test-backend-ops, full run on one MTT S5000 (MUSA architecture31), MUSA SDK 5.2.0:End to end, Qwen3.8-27B UD-Q4_K_M with
-ngl 999:iq4_nl(-ctk iq4_nl -ctv iq4_nl -fa on)Additional information
Requirements