Skip to content

cuda : fix KQ mask offset integer overflow in flash attention MMA kernel - #23610

Merged
JohannesGaessler merged 1 commit into
ggml-org:masterfrom
fairydreaming:fattn-mma-kq-mask-integer-overflow
May 28, 2026
Merged

JohannesGaessler merged 1 commit into
ggml-org:masterfrom
fairydreaming:fattn-mma-kq-mask-integer-overflow

Conversation

@fairydreaming

Copy link
Copy Markdown
Contributor

Overview

When using long context and large ubatch size KQ mask becomes very large during prompt processing with the number of elements exceeding INT32_MAX (for example in GLM-5.1 max context size is 202752, so with 16k ubatch we have 3321888768 KQ mask elements). This results in negative j_vram*stride_mask mask_h array offset in flash attention MMA kernel as confirmed by printfs:

j_sram*(nbatch_fa + 8) = 1600, j_vram*stride_mask = -1976369152
j_sram*(nbatch_fa + 8) = 1600, j_vram*stride_mask = -1976369152
j_sram*(nbatch_fa + 8) = 1600, j_vram*stride_mask = -1976369152
j_sram*(nbatch_fa + 8) = 1600, j_vram*stride_mask = -1976369152
j_sram*(nbatch_fa + 8) = 1640, j_vram*stride_mask = -1976221696
j_sram*(nbatch_fa + 8) = 1640, j_vram*stride_mask = -1976221696
j_sram*(nbatch_fa + 8) = 1640, j_vram*stride_mask = -1976221696
j_sram*(nbatch_fa + 8) = 1640, j_vram*stride_mask = -1976221696

Casting j_vram to int64_t so that the calculated offset is also int64_t resolves this problem.

Fixes #23574.

Requirements

@fairydreaming
fairydreaming requested a review from a team as a code owner May 24, 2026 12:18
@github-actions github-actions Bot added Nvidia GPU Issues specific to Nvidia GPUs ggml changes relating to the ggml tensor library for machine learning labels May 24, 2026
#pragma unroll
for (int j1 = 0; j1 < ncols1; j1 += stride_j) {
const int j_sram = j1 + threadIdx.y*cols_per_warp + threadIdx.x / (warp_size/cols_per_warp);
const int j_vram = fastmodulo(j0 + j_sram, ne01);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const int64_t j_vram = fastmodulo(j0 + j_sram, ne01);

I would say it's better to do the upcast on this line instead because an overflow of a 32 bit signed integer here would be upcast to a negative 64 bit integer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically I agree as there's unsigned int put into int there, but since it's result of something modulo ne01 which is ubatch size, overflow here is I suppose purely theoretical. Besides, I don't like that it increases register usage. I'd stick with casts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you actually check NSight Compute for the register usage? In terms of the dependencies between registers I don't think it would increase.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I add -Xptxas=-v to llama.cpp CUDA compilation command and check the console output, like this:

/usr/local/cuda-12.8/bin/nvcc -forward-unknown-to-host-compiler -DGGML_BACKEND_BUILD -DGGML_BACKEND_SHARED -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128 -DGGML_CUDA_USE_GRAPHS -DGGML_SCHED_MAX_COPIES=4 -DGGML_SHARED -D_GNU_SOURCE -D_XOPEN_SOURCE=600 -Dggml_cuda_EXPORTS --options-file CMakeFiles/ggml-cuda.dir/includes_CUDA.rsp -std=c++17 "--generate-code=arch=compute_120a,code=[sm_120a]" -Xcompiler=-fPIC -use_fast_math -extended-lambda -compress-mode=size -Xcompiler "-Wmissing-declarations -Wmissing-noreturn -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-array-bounds -Wextra-semi -Wno-pedantic" -MD -MT ggml/src/ggml-cuda/CMakeFiles/ggml-cuda.dir/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu.o -MF CMakeFiles/ggml-cuda.dir/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu.o.d -x cu -c /home/phm/projects/llama.cpp-fattn-mma-kq-mask-integer-overflow/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu -o CMakeFiles/ggml-cuda.dir/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu.o -Xptxas=-v

Output:
ptxas-info-types.txt
ptxas-info-casts.txt

Diff:

(base) phm@edge:/tmp$ diff -I "Compile time" /tmp/ptxas-info-casts.txt /tmp/ptxas-info-types.txt 
20,21c20,21
< ptxas info    : Used 228 registers, used 1 barriers, 48 bytes cumulative stack size
< ptxas info    : Compile time = 538.515 ms
---
> ptxas info    : Used 236 registers, used 1 barriers, 48 bytes cumulative stack size
> ptxas info    : Compile time = 547.013 ms
25,26c25,26
< ptxas info    : Used 228 registers, used 1 barriers, 48 bytes cumulative stack size
< ptxas info    : Compile time = 530.295 ms
---
> ptxas info    : Used 236 registers, used 1 barriers, 48 bytes cumulative stack size
> ptxas info    : Compile time = 531.840 ms
50,51c50,51
< ptxas info    : Used 202 registers, used 1 barriers, 48 bytes cumulative stack size
< ptxas info    : Compile time = 248.945 ms
---
> ptxas info    : Used 206 registers, used 1 barriers, 48 bytes cumulative stack size
> ptxas info    : Compile time = 247.254 ms
75,76c75,76
< ptxas info    : Used 193 registers, used 1 barriers, 48 bytes cumulative stack size
< ptxas info    : Compile time = 248.518 ms
---
> ptxas info    : Used 194 registers, used 1 barriers, 48 bytes cumulative stack size
> ptxas info    : Compile time = 246.289 ms
100,101c100,101
< ptxas info    : Used 172 registers, used 1 barriers, 48 bytes cumulative stack size
< ptxas info    : Compile time = 222.996 ms
---
> ptxas info    : Used 176 registers, used 1 barriers, 48 bytes cumulative stack size
> ptxas info    : Compile time = 221.798 ms

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JohannesGaessler I checked the ptxas sources of both solutions, the solution with cast results in mul.wide.s32 instruction being used, while your solution with type change produces code that first converts stride_mask to 64-bit and then multiplies both with mul.lo.s64. This conversion requires one extra register.

In my opinion the best would be to keep the casts and solve the problem with fastmodulo() result being assigned to int instead of uint32_t in another PR, as from what I see it happens in multiple other places:

ggml/src/ggml-cuda/binbcast.cu:    const int i11 = fastmodulo(i1, ne11);
ggml/src/ggml-cuda/binbcast.cu:    const int i12 = fastmodulo(i2, ne12);
ggml/src/ggml-cuda/binbcast.cu:    const int i13 = fastmodulo(i3, ne13);
ggml/src/ggml-cuda/binbcast.cu:    const int i10 = fastmodulo(i0, ne10);
ggml/src/ggml-cuda/fattn-mma-f16.cuh:            const int j_vram = fastmodulo(j0 + j_sram, ne01);
ggml/src/ggml-cuda/fattn-mma-f16.cuh:            const int j_vram = fastmodulo(j0 + j_sram, ne01);
ggml/src/ggml-cuda/fattn-mma-f16.cuh:            const int j_vram = fastmodulo(j0 + j_sram, ne01);
ggml/src/ggml-cuda/fattn-mma-f16.cuh:            const int j_vram = fastmodulo(j0 + j_sram, ne01);
ggml/src/ggml-cuda/fattn-tile.cuh:        const int j = fastmodulo(col_Q_0 + (jc0 + (threadIdx.y / np)*cpw)/ncols2, ne01);
ggml/src/ggml-cuda/mmq.cuh:    int kb0_start = fastmodulo(kbc, blocks_per_ne00);
ggml/src/ggml-cuda/norm.cu:        const int add_row     = fastmodulo(row, add_nrows_packed);
ggml/src/ggml-cuda/norm.cu:        const int add_channel = fastmodulo(channel, add_nchannels_packed);
ggml/src/ggml-cuda/norm.cu:        const int add_sample  = fastmodulo(sample, add_nsamples_packed);
ggml/src/ggml-cuda/norm.cu:            const int mul_col = fastmodulo(col, mul_ncols_packed);
ggml/src/ggml-cuda/norm.cu:            const int add_col = fastmodulo(col, add_ncols_packed);
ggml/src/ggml-cuda/norm.cu:            const int mul_col = fastmodulo(col, mul_ncols_packed);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now also got around to checking both variants in NSight Compute and can confirm that the register use with the inline conversion is lower. So let's go with that version.

@JohannesGaessler JohannesGaessler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately either way is fine I think. I just wanted to check the resulting PTX for possible differences.

@ggerganov
ggerganov requested a review from a team May 28, 2026 07:36
@JohannesGaessler
JohannesGaessler merged commit 09e7b76 into ggml-org:master May 28, 2026
46 of 47 checks passed
turbo-tan pushed a commit to turbo-tan/llama.cpp-tq3 that referenced this pull request Jun 2, 2026
davide221 pushed a commit to Luce-Org/lucebox-ggml that referenced this pull request Jun 25, 2026
davide221 added a commit to Luce-Org/lucebox-ggml that referenced this pull request Jun 25, 2026
…s-groupA

ggml/cuda: backport upstream correctness fixes (ggml-org#23610, ggml-org#24533)
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning Nvidia GPU Issues specific to Nvidia GPUs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eval bug: GLM 5.x crashes/gibberish with high ubatch and long prompts

4 participants