Repository navigation
cuda : fix KQ mask offset integer overflow in flash attention MMA kernel - #23610
JohannesGaessler merged 1 commit into
Conversation
| #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); |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Did you actually check NSight Compute for the register usage? In terms of the dependencies between registers I don't think it would increase.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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);
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Ultimately either way is fine I think. I just wanted to check the resulting PTX for possible differences.
…org#23610) Co-authored-by: Stanisław Szymczyk <[email protected]>
…org#23610) Co-authored-by: Stanisław Szymczyk <[email protected]> (cherry picked from commit 09e7b76)
…s-groupA ggml/cuda: backport upstream correctness fixes (ggml-org#23610, ggml-org#24533)
…org#23610) Co-authored-by: Stanisław Szymczyk <[email protected]>
…org#23610) Co-authored-by: Stanisław Szymczyk <[email protected]>
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_maskmask_h array offset in flash attention MMA kernel as confirmed by printfs:Casting
j_vramtoint64_tso that the calculated offset is alsoint64_tresolves this problem.Fixes #23574.
Requirements