Skip to content

cuda: fix the q8_0 and iq4_nl dequantization kernels - #29292

Closed
yeahdongcn wants to merge 2 commits into
ggml-org:masterfrom
yeahdongcn:cuda-dequant-fixes
Closed

yeahdongcn wants to merge 2 commits into
ggml-org:masterfrom
yeahdongcn:cuda-dequant-fixes

Conversation

@yeahdongcn

@yeahdongcn yeahdongcn commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Two fixes to the ggml-cuda dequantization kernels:

  • dequantize_block_q8_0_f16 is guarded by __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL. MUSA's mcc defines __MUSA_ARCH__ in the device pass and not __CUDA_ARCH__, so the condition is false, the body is dropped and the kernel is launched empty — every q8_0 dequantization produces garbage. Also guard on __MUSA_ARCH__; NVIDIA builds are unaffected because defined(__MUSA_ARCH__) is false there.
  • dequantize_block_iq4_nl writes QK_K values per block, but an IQ4_NL row is only guaranteed to be a multiple of QK4_NL, and ops such as MUL_MAT do use shorter rows. Threads whose 32-value sub-block starts at or past the end of the row were reading and writing past the end of the output. Skip those sub-blocks. For rows that are a multiple of QK_K the check never fires.
    Both were found with test-backend-ops on an MTT S5000 (MUSA architecture 31); the q8_0 case is MUSA specific, the iq4_nl case is backend independent.

Additional information

Requirements

dequantize_block_q8_0_f16() was guarded by "#if __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL".
mcc never defines __CUDA_ARCH__, so the guard was false on MUSA and the kernel body
was dropped entirely - NO_DEVICE_CODE expands to nothing when __CUDA_ARCH__ is
undefined, so the launch succeeded and y was left untouched.

Every q8_0 -> f16 dequantization used an uninitialized buffer: the quantized matmul
fallback, MUL_MAT_ID, and quantized KV cache for flash attention. On an MTT S5000
this is 19 MUL_MAT, 31 MUL_MAT_ID and the q8_0 FLASH_ATTN_EXT test cases; all of
them pass after the fix.
dequantize_block_iq4_nl writes QK_K values per block, but a row can be shorter than that (an IQ4_NL row is only guaranteed to be a multiple of QK4_NL). Threads whose 32-value sub-block starts at or past k currently read and write past the end of the row. Skip those sub-blocks; for rows that are a multiple of QK_K the check never fires.
@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

This comment was marked as resolved.

@yeahdongcn
yeahdongcn marked this pull request as ready for review September 23, 2026 02:17
@yeahdongcn
yeahdongcn requested a review from a team as a code owner September 23, 2026 02:17
@CISC

CISC commented Sep 23, 2026

Copy link
Copy Markdown
Member

__CUDA_ARCH__ should be defined in vendors/musa.h.

@yeahdongcn

Copy link
Copy Markdown
Collaborator Author

__CUDA_ARCH__ should be defined in vendors/musa.h.

I see that __CUDA_ARCH__ is defined in vendors/hip.h, but this value has never been defined on the MUSA side. I think certain features need to be explicitly enabled by us.

Just wanted to clarify whether you'd like us to follow the HIP approach and disable features based on __CUDA_ARCH__. If so, it would be better to handle that in a separate PR.

@yeahdongcn

Copy link
Copy Markdown
Collaborator Author

Or we just define #define __CUDA_ARCH__ 600?

@CISC

CISC commented Sep 23, 2026

Copy link
Copy Markdown
Member

__CUDA_ARCH__ should be defined in vendors/musa.h.

I see that __CUDA_ARCH__ is defined in vendors/hip.h, but this value has never been defined on the MUSA side. I think certain features need to be explicitly enabled by us.

Traditionally you would then check defined(GGML_USE_MUSA).

Or we just define #define __CUDA_ARCH__ 600?

I'm not familiar with MUSA, but whichever would be the safe value I guess, as with HIP.

@yeahdongcn

Copy link
Copy Markdown
Collaborator Author

To follow HIP's approach, we would also need to add something like this to prevent the feature code from being compiled:

// common.cuh:283-302
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ == GGML_CUDA_CC_VOLTA            → #define VOLTA_MMA_AVAILABLE
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_TURING           → #define TURING_MMA_AVAILABLE
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE           → #define AMPERE_MMA_AVAILABLE
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_BLACKWELL && < RUBIN → #define BLACKWELL_MMA_AVAILABLE
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE           → #define CP_ASYNC_AVAILABLE

@CISC

CISC commented Sep 23, 2026

Copy link
Copy Markdown
Member

To follow HIP's approach, we would also need to add something like this to prevent the feature code from being compiled:

As is done right above:

#if defined(GGML_USE_HIP) || defined(GGML_USE_MUSA) || __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL
#define FP16_AVAILABLE
#endif // defined(GGML_USE_HIP) || defined(GGML_USE_MUSA) || __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL

@yeahdongcn

Copy link
Copy Markdown
Collaborator Author

To follow HIP's approach, we would also need to add something like this to prevent the feature code from being compiled:

As is done right above:

#if defined(GGML_USE_HIP) || defined(GGML_USE_MUSA) || __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL
#define FP16_AVAILABLE
#endif // defined(GGML_USE_HIP) || defined(GGML_USE_MUSA) || __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL

Can I open a new PR to align this with the HIP approach?

@CISC

CISC commented Sep 23, 2026

Copy link
Copy Markdown
Member

Can I open a new PR to align this with the HIP approach?

Up to @ggml-org/ggml-cuda, I'll stop butting in now. :)

@yeahdongcn

Copy link
Copy Markdown
Collaborator Author

Replaced with #29508

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