Commit a97b37999 for llama.cpp
commit a97b37999e9d181ca1c90c409a567cef14620993
Author: anujj <ajalota@nvidia.com>
Date: Mon Dec 7 01:03:23 2026 +0530
CUDA: speed up im2col when IC*KH*KW is small (#33791)
diff --git a/ggml/src/ggml-cuda/im2col.cu b/ggml/src/ggml-cuda/im2col.cu
index d377f2856..bb10c38c9 100644
--- a/ggml/src/ggml-cuda/im2col.cu
+++ b/ggml/src/ggml-cuda/im2col.cu
@@ -3,7 +3,7 @@
#define MAX_GRIDDIM_Y 65535
#define MAX_GRIDDIM_Z 65535
-template <typename T>
+template <typename T, bool several_pos>
static __global__ void im2col_kernel(
const float * x, T * dst,
int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH,
@@ -16,7 +16,8 @@ static __global__ void im2col_kernel(
const int64_t start_ch = tid;
const int64_t stride_ch = threads_per_pos;
- for (int64_t iow = blockIdx.x; iow < OW; iow += MAX_GRIDDIM_Y) {
+ // without several_pos iow is the same for the whole block, which keeps the index math warp-uniform
+ for (int64_t iow = several_pos ? (int64_t) blockIdx.x*blockDim.y + threadIdx.y : (int64_t) blockIdx.x; iow < OW; iow += several_pos ? (int64_t) gridDim.x*blockDim.y : (int64_t) MAX_GRIDDIM_Y) {
for (int64_t iz = blockIdx.y; iz < N_OH; iz += MAX_GRIDDIM_Z) {
const int64_t in = iz / OH;
const int64_t ioh = iz - in * OH;
@@ -53,10 +54,16 @@ static void im2col_cuda(const float * x, T* dst,
const int64_t IC_KH_KW = IC * KH * KW;
const int64_t N_OH = N * OH;
const int64_t KH_KW = KW*KH;
- const int threads_per_block = MIN((int)IC_KH_KW, CUDA_IM2COL_BLOCK_SIZE);
- dim3 block_nums(MIN(OW, MAX_GRIDDIM_Y), MIN(N_OH, MAX_GRIDDIM_Z));
-
- im2col_kernel<<<block_nums, threads_per_block, 0, stream>>>(
+ const int threads_per_pos = (int) MIN(IC_KH_KW, CUDA_IM2COL_BLOCK_SIZE);
+ // a small IC*KH*KW leaves most of the block idle: give each block several output positions instead
+ // only if at least 3 fit: with room for just 2 the gain is small and the per-thread index math can make it slower
+ const int pos_per_block_max = CUDA_IM2COL_BLOCK_SIZE / threads_per_pos;
+ const int pos_per_block = pos_per_block_max >= 3 ? (int) MIN(pos_per_block_max, OW) : 1;
+ const dim3 block_dims(threads_per_pos, pos_per_block, 1);
+ dim3 block_nums(MIN((OW + pos_per_block - 1) / pos_per_block, MAX_GRIDDIM_Y), MIN(N_OH, MAX_GRIDDIM_Z));
+
+ const auto kernel = pos_per_block > 1 ? im2col_kernel<T, true> : im2col_kernel<T, false>;
+ kernel<<<block_nums, block_dims, 0, stream>>>(
x, dst, IC, IW, IH, OH, OW, KW, KH,
N, IC_IH_IW, IH_IW, N_OH, KH_KW, IC_KH_KW,
s0, s1, p0, p1, d0, d1);
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index b2cd823df..3dd3e2179 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -12767,6 +12767,10 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
for (int64_t n : {5, 7}) {
test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {600, 8, n, 1}, {3, 8, 8, 1}, 1, 0, 1, 0, 1, 0, false));
}
+ // depthwise dilated conv of qwen4exp PLE: one channel, so a CUDA block covers many output positions, the last one partly
+ test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {2050, 1, 16, 1}, {4, 1, 1, 1}, 1, 0, 0, 0, 3, 0, false));
+ // OW 200000 needs more than 65535 CUDA blocks of 3 output positions, so the blocks loop over OW
+ test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F16, {200064, 1, 1, 1}, {65, 1, 1, 1}, 1, 0, 0, 0, 1, 0, false));
// 2D with a kernel one row tall: with p1 > 0 some output rows fall outside the input height
test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {40, 6, 8, 1}, {3, 1, 8, 4}, 1, 1, 1, 1, 1, 1, true));
test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {40, 6, 8, 1}, {3, 1, 8, 4}, 1, 1, 1, 0, 1, 1, true));
@@ -16503,6 +16507,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
}
}
+ // qwen4exp PLE depthwise conv at ubatch 2048: 4 dilated taps over 8192 channels
+ test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {2057, 1, 8192, 1}, {4, 1, 1, 1}, 1, 0, 0, 0, 3, 0, false));
+
// Qwen3-VL-8B https://github.com/ggml-org/llama.cpp/issues/17012
test_cases.emplace_back(new test_flash_attn_ext(72, 72, 16, {1, 1}, 5776, 5776, false, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
// Same non-causal vision-encoder shape for the other head size padded in-kernel by the CUDA MMA kernels