From 563a1a165efe3d739610ed413ff9057531d89cf3 Mon Sep 17 00:00:00 2001 From: genmingz Date: Wed, 25 Nov 2026 01:37:03 -0700 Subject: [PATCH] cuda : speed up transposed dim-0 concat on RDNA3.5 Assisted-by: GPT-5.6 Sol Co-authored-by: Cursor --- ggml/src/ggml-cuda/concat.cu | 86 +++++++++++++++++++++++++++++++++--- tests/test-backend-ops.cpp | 2 + 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/ggml/src/ggml-cuda/concat.cu b/ggml/src/ggml-cuda/concat.cu index f8dcd95ab008..7895cf8b466c 100644 --- a/ggml/src/ggml-cuda/concat.cu +++ b/ggml/src/ggml-cuda/concat.cu @@ -2,6 +2,10 @@ #include +static constexpr int CUDA_CONCAT_TRANSPOSE_TILE = 64; +static constexpr int CUDA_CONCAT_TRANSPOSE_COLS = 32; +static constexpr int CUDA_CONCAT_TRANSPOSE_ROWS = 8; + // contiguous kernels template static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE) concat_cont(const T * x, @@ -79,6 +83,60 @@ static void concat_cont_cuda(const T * x, concat_cont<<>>(x, y, dst, ne00, ne01, ne02, ne0, ne1, ne2); } +static __global__ void concat_transpose_dim0_f32( + const uint32_t * src0, + const uint32_t * src1, + uint32_t * dst, + int64_t ne00, + int64_t ne10, + int64_t ne1, + int64_t ne0) { + __shared__ uint32_t tile[CUDA_CONCAT_TRANSPOSE_TILE][CUDA_CONCAT_TRANSPOSE_TILE + 1]; + + const int64_t token_load = (int64_t) blockIdx.x * CUDA_CONCAT_TRANSPOSE_TILE + threadIdx.y; + const int64_t channel_load = (int64_t) blockIdx.y * CUDA_CONCAT_TRANSPOSE_TILE + threadIdx.x; + + ggml_cuda_pdl_sync(); +#pragma unroll + for (int j = 0; j < CUDA_CONCAT_TRANSPOSE_TILE; j += CUDA_CONCAT_TRANSPOSE_ROWS) { + if (token_load + j < ne10) { +#pragma unroll + for (int k = 0; k < CUDA_CONCAT_TRANSPOSE_TILE; k += CUDA_CONCAT_TRANSPOSE_COLS) { + if (channel_load + k < ne1) { + tile[threadIdx.y + j][threadIdx.x + k] = src1[(token_load + j) * ne1 + channel_load + k]; + } + } + } + } + + __syncthreads(); + + const int64_t token_store = (int64_t) blockIdx.x * CUDA_CONCAT_TRANSPOSE_TILE + threadIdx.x; + const int64_t channel_store = (int64_t) blockIdx.y * CUDA_CONCAT_TRANSPOSE_TILE + threadIdx.y; +#pragma unroll + for (int j = 0; j < CUDA_CONCAT_TRANSPOSE_TILE; j += CUDA_CONCAT_TRANSPOSE_ROWS) { + if (channel_store + j < ne1) { +#pragma unroll + for (int k = 0; k < CUDA_CONCAT_TRANSPOSE_TILE; k += CUDA_CONCAT_TRANSPOSE_COLS) { + if (token_store + k < ne10) { + dst[(channel_store + j) * ne0 + ne00 + token_store + k] = tile[threadIdx.x + k][threadIdx.y + j]; + } + } + } + } + + if (blockIdx.x == 0) { + for (int64_t i = threadIdx.y * blockDim.x + threadIdx.x; + i < CUDA_CONCAT_TRANSPOSE_TILE * ne00; i += blockDim.x * blockDim.y) { + const int64_t channel = (int64_t) blockIdx.y * CUDA_CONCAT_TRANSPOSE_TILE + i / ne00; + const int64_t i0 = i % ne00; + if (channel < ne1) { + dst[channel * ne0 + i0] = src0[channel * ne00 + i0]; + } + } + } +} + // non-contiguous kernel (slow) template static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE) @@ -140,7 +198,22 @@ static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE) } template -static void concat_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, int dim, cudaStream_t stream) { +static void concat_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, int dim, cudaStream_t stream, int cc) { + if (GGML_CUDA_CC_IS_RDNA3_5(cc) && src0->type == GGML_TYPE_F32 && dim == 0 && src0->ne[0] <= CUDA_CONCAT_TRANSPOSE_TILE && + src1->ne[0] >= CUDA_CONCAT_TRANSPOSE_TILE && src1->ne[1] >= CUDA_CONCAT_TRANSPOSE_TILE && + src0->ne[2] == 1 && src0->ne[3] == 1 && src1->ne[2] == 1 && src1->ne[3] == 1 && + ggml_is_contiguous(src0) && ggml_is_contiguous(dst) && + src1->nb[0] == src1->ne[1] * sizeof(uint32_t) && src1->nb[1] == sizeof(uint32_t)) { + const dim3 block_dim(CUDA_CONCAT_TRANSPOSE_COLS, CUDA_CONCAT_TRANSPOSE_ROWS); + const dim3 grid_dim( + (src1->ne[0] + CUDA_CONCAT_TRANSPOSE_TILE - 1) / CUDA_CONCAT_TRANSPOSE_TILE, + (src1->ne[1] + CUDA_CONCAT_TRANSPOSE_TILE - 1) / CUDA_CONCAT_TRANSPOSE_TILE); + concat_transpose_dim0_f32<<>>( + (const uint32_t *) src0->data, (const uint32_t *) src1->data, (uint32_t *) dst->data, + src0->ne[0], src1->ne[0], dst->ne[1], dst->ne[0]); + return; + } + if (dim != 3 && ggml_is_contiguous_to_3(src0) && ggml_is_contiguous_to_3(src1)) { const T * src0_d = (const T *) src0->data; const T * src1_d = (const T *) src1->data; @@ -198,6 +271,7 @@ void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { const ggml_tensor * src1 = dst->src[1]; cudaStream_t stream = ctx.stream(); + const int cc = ggml_cuda_info().devices[ctx.device].cc; const int32_t dim = ((int32_t *) dst->op_params)[0]; @@ -216,22 +290,22 @@ void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { GGML_ASSERT(src1->ne[0] % ggml_blck_size(src1->type) == 0); // if first 3 dimensions are contiguous and ne[0] is multiple of the block size we can concat both tensors as byte tensors - concat_cuda(src0, src1, dst, dim, stream); + concat_cuda(src0, src1, dst, dim, stream, cc); } else { GGML_ASSERT(ggml_blck_size(src0->type) == 1); switch (ggml_type_size(src0->type)) { case 1: - concat_cuda(src0, src1, dst, dim, stream); + concat_cuda(src0, src1, dst, dim, stream, cc); break; case 2: - concat_cuda(src0, src1, dst, dim, stream); + concat_cuda(src0, src1, dst, dim, stream, cc); break; case 4: - concat_cuda(src0, src1, dst, dim, stream); + concat_cuda(src0, src1, dst, dim, stream, cc); break; case 8: - concat_cuda(src0, src1, dst, dim, stream); + concat_cuda(src0, src1, dst, dim, stream, cc); break; default: GGML_ABORT("Unsupported type size: %zu", ggml_type_size(src0->type)); diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 5b20219bc50e..6a2096950a1e 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -13114,6 +13114,7 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {1, 37, 1, 1}, ne_b_d, 0, 16)); } test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {64, 37, 1, 1}, 32, 0, 16)); + test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {9, 10240, 1, 1}, 2048, 0, 16)); // Empty src0 test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {0, 64, 1, 1}, 33, 0, 16)); @@ -14064,6 +14065,7 @@ static std::vector> make_test_cases_perf() { test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {3, channels, 1, 1}, n_tokens, 0, 16)); } } + test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {9, 10240, 1, 1}, 2048, 0, 16)); // the same conv input fused into the conv, and the unfused conv it replaces for (int64_t n_tokens : {512, 2048}) { test_cases.emplace_back(new test_ssm_conv_concat(4, 10240, n_tokens, 1, false, true, false));