Skip to content

cuda: support IQ4_NL in the flash attention KV cache - #29293

Closed
yeahdongcn wants to merge 2 commits into
ggml-org:masterfrom
yeahdongcn:cuda-fattn-iq4-nl-kv
Closed

yeahdongcn wants to merge 2 commits into
ggml-org:masterfrom
yeahdongcn:cuda-fattn-iq4-nl-kv

Conversation

@yeahdongcn

@yeahdongcn yeahdongcn commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

  • ggml_cuda_fattn_kv_type_supported now accepts GGML_TYPE_IQ4_NL as a KV cache type, together with the dequantization counterpart the flash attention kernels use to read that cache: dequantize_iq4_nl_pair and the to_fp16_nc_cuda / to_bf16_nc_cuda / to_fp32_nc_cuda dispatch entries for IQ4_NL. This makes -ctk iq4_nl -ctv iq4_nl -fa on work instead of falling back.
  • On MUSA, FLASH_ATTN_AVAILABLE is 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 on defined(__MUSA_ARCH__), and [the runtime check in the host code] rejects devices whose MUSA architecture has no FA device code (only QY2 / mp_22 and 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 architecture 31), MUSA SDK 5.2.0:

ggml_cuda_init: found 1 MUSA devices (Total VRAM: 81829 MiB):
  Device 0: MTT S5000, compute capability 3.1, VMM: yes, VRAM: 81829 MiB
...
  16499/16499 tests passed
Running FA vec slice tests (env LLAMA_TEST_FA_VEC_DISABLE=1 to skip)
  Backend MUSA0: OK
2/2 backends passed

End to end, Qwen3.8-27B UD-Q4_K_M with -ngl 999:

KV cache PPL
iq4_nl (-ctk iq4_nl -ctv iq4_nl -fa on) 4.6453
f16 4.6271

Additional information

Requirements

yeahdongcn and others added 2 commits September 23, 2026 08:42
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.
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Sep 23, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Sep 23, 2026

Copy link
Copy Markdown

Hi @yeahdongcn, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • Multiple open PRs from a new contributor: We limit new contributors (those without a previously merged PR) to 1 open PR at a time. You currently have 3 open PRs.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@yeahdongcn

Copy link
Copy Markdown
Collaborator Author

@JohannesGaessler I'm afraid I can't make this PR ready for review due to the repo restrictions.

@JohannesGaessler

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants