Commit c59af87cb for llama.cpp
commit c59af87cb91392d754ec61674829e3cd2644682c
Author: leejet <leejet714@gmail.com>
Date: Fri Dec 4 16:52:08 2026 +0800
cuda : batch contiguous concat slices in one launch (#33463)
* cuda : batch contiguous concat slices in one launch
* add concat test case
diff --git a/ggml/src/ggml-cuda/concat.cu b/ggml/src/ggml-cuda/concat.cu
index 7391f99f8..ea15eba03 100644
--- a/ggml/src/ggml-cuda/concat.cu
+++ b/ggml/src/ggml-cuda/concat.cu
@@ -12,71 +12,93 @@ static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE) concat_cont(con
int64_t ne02,
int64_t ne0,
int64_t ne1,
- int64_t ne2) {
+ int64_t ne2,
+ int64_t ne3,
+ size_t stride_x,
+ size_t stride_y,
+ size_t stride_dst) {
static_assert(dim >= 0 && dim <= 2, "dim must be in [0, 2]");
const int64_t n = ne0 * ne1 * ne2;
ggml_cuda_pdl_sync();
- for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (int64_t) blockDim.x * gridDim.x) {
- if constexpr (dim == 0) {
- const int64_t row = i / ne0;
- const int64_t i0 = i - row * ne0;
+ for (int64_t i3 = blockIdx.y; i3 < ne3; i3 += gridDim.y) {
+ const T * x_slice = x + i3 * stride_x;
+ const T * y_slice = y + i3 * stride_y;
+ T * dst_slice = dst + i3 * stride_dst;
- if (i0 < ne00) {
- dst[i] = x[row * ne00 + i0];
- } else {
- dst[i] = y[row * (ne0 - ne00) + (i0 - ne00)];
- }
- } else if constexpr (dim == 1) {
- const int64_t dst_plane = ne0 * ne1;
- const int64_t src0_plane = ne0 * ne01;
- const int64_t src1_plane = dst_plane - src0_plane;
- const int64_t i2 = i / dst_plane;
- const int64_t i01 = i - i2 * dst_plane;
+ for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (int64_t) blockDim.x * gridDim.x) {
+ if constexpr (dim == 0) {
+ const int64_t row = i / ne0;
+ const int64_t i0 = i - row * ne0;
- if (i01 < src0_plane) {
- dst[i] = x[i2 * src0_plane + i01];
- } else {
- dst[i] = y[i2 * src1_plane + (i01 - src0_plane)];
- }
- } else {
- const int64_t src0_size = ne0 * ne1 * ne02;
+ if (i0 < ne00) {
+ dst_slice[i] = x_slice[row * ne00 + i0];
+ } else {
+ dst_slice[i] = y_slice[row * (ne0 - ne00) + (i0 - ne00)];
+ }
+ } else if constexpr (dim == 1) {
+ const int64_t dst_plane = ne0 * ne1;
+ const int64_t src0_plane = ne0 * ne01;
+ const int64_t src1_plane = dst_plane - src0_plane;
+ const int64_t i2 = i / dst_plane;
+ const int64_t i01 = i - i2 * dst_plane;
- if (i < src0_size) {
- dst[i] = x[i];
+ if (i01 < src0_plane) {
+ dst_slice[i] = x_slice[i2 * src0_plane + i01];
+ } else {
+ dst_slice[i] = y_slice[i2 * src1_plane + (i01 - src0_plane)];
+ }
} else {
- dst[i] = y[i - src0_size];
+ const int64_t src0_size = ne0 * ne1 * ne02;
+
+ if (i < src0_size) {
+ dst_slice[i] = x_slice[i];
+ } else {
+ dst_slice[i] = y_slice[i - src0_size];
+ }
}
}
}
}
template <typename T>
-static void concat_cont_cuda(const T * x,
- const T * y,
- T * dst,
- int64_t ne00,
- int64_t ne01,
- int64_t ne02,
- int64_t ne0,
- int64_t ne1,
- int64_t ne2,
- int dim,
- cudaStream_t stream) {
- const int64_t n = ne0 * ne1 * ne2;
- const int num_blocks = (n + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
+static void concat_cont_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
+ int dim, cudaStream_t stream) {
+ const T * x = (const T *) src0->data;
+ const T * y = (const T *) src1->data;
+ T * dst_d = (T *) dst->data;
+
+ const int64_t ne00 = ggml_row_size(src0->type, src0->ne[0]) / sizeof(T);
+ const int64_t ne01 = src0->ne[1];
+ const int64_t ne02 = src0->ne[2];
+ const int64_t ne0 = ggml_row_size(dst->type, dst->ne[0]) / sizeof(T);
+ const int64_t ne1 = dst->ne[1];
+ const int64_t ne2 = dst->ne[2];
+ const int64_t ne3 = dst->ne[3];
+
+ const size_t stride_x = src0->nb[3] / sizeof(T);
+ const size_t stride_y = src1->nb[3] / sizeof(T);
+ const size_t stride_dst = dst->nb[3] / sizeof(T);
+ const int64_t n = ne0 * ne1 * ne2;
+ const int64_t blocks_x = (n + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
+ // Each block strides over additional slices when the outer extent exceeds the grid Y limit.
+ const dim3 grid(static_cast<unsigned int>(std::min<int64_t>(blocks_x, INT32_MAX)),
+ static_cast<unsigned int>(std::min<int64_t>(ne3, 65535)));
if (dim == 0) {
- const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(num_blocks, CUDA_CONCAT_BLOCK_SIZE, 0, stream);
- ggml_cuda_kernel_launch(concat_cont<T, 0>, launch_params, x, y, dst, ne00, ne01, ne02, ne0, ne1, ne2);
+ const ggml_cuda_kernel_launch_params launch_params(grid, CUDA_CONCAT_BLOCK_SIZE, 0, stream);
+ ggml_cuda_kernel_launch(concat_cont<T, 0>, launch_params, x, y, dst_d, ne00, ne01, ne02, ne0, ne1, ne2, ne3,
+ stride_x, stride_y, stride_dst);
return;
}
if (dim == 1) {
- concat_cont<T, 1><<<num_blocks, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne00, ne01, ne02, ne0, ne1, ne2);
+ concat_cont<T, 1><<<grid, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst_d, ne00, ne01, ne02, ne0, ne1, ne2, ne3,
+ stride_x, stride_y, stride_dst);
return;
}
- concat_cont<T, 2><<<num_blocks, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne00, ne01, ne02, ne0, ne1, ne2);
+ concat_cont<T, 2><<<grid, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst_d, ne00, ne01, ne02, ne0, ne1, ne2, ne3,
+ stride_x, stride_y, stride_dst);
}
template <typename T, int dim>
@@ -232,18 +254,7 @@ static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE)
template <typename T>
static void concat_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, int dim, cudaStream_t stream) {
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;
- T * dst_d = (T *) dst->data;
-
- for (int64_t i3 = 0; i3 < dst->ne[3]; i3++) {
- concat_cont_cuda(
- src0_d + i3*(src0->nb[3] / sizeof(T)),
- src1_d + i3*(src1->nb[3] / sizeof(T)),
- dst_d + i3*( dst->nb[3] / sizeof(T)),
- ggml_row_size(src0->type, src0->ne[0])/sizeof(T), src0->ne[1], src0->ne[2],
- ggml_row_size(dst->type, dst->ne[0])/sizeof(T), dst->ne[1], dst->ne[2], dim, stream);
- }
+ concat_cont_cuda<T>(src0, src1, dst, dim, stream);
} else if (dim == 3 && ggml_is_contiguous(src0) && ggml_is_contiguous(src1)) {
const size_t size0 = ggml_nbytes(src0);
const size_t size1 = ggml_nbytes(src1);
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 520f31470..325f727a0 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -14874,6 +14874,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_concat(GGML_TYPE_Q8_0, {8192, 2, 1, 1}, dim == 0 ? 4096 : 1, dim, 0));
}
+ // contiguous with ne[3] > 65535 (CUDA grid y limit)
+ test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {4, 2, 1, 70000}, 2, 2, 0));
+
// non-contiguous with ne[2] or ne[3] > 65535 (CUDA grid y/z limit)
test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {4, 2, 70000, 1}, 4, 0, 1));
test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {4, 2, 40000, 1}, 30000, 2, 1));
@@ -16041,6 +16044,12 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
test_cases.emplace_back(new test_concat(type, {2050, 8, 256, 1}, 255, 0, 0));
}
+ for (int64_t ne3 : { 1, 128, 1024 }) {
+ test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {32, 18, 2, ne3}, 5, 2, 0));
+ }
+ test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {64, 36, 2, 128}, 5, 2, 0));
+ test_cases.emplace_back(new test_concat(GGML_TYPE_Q8_0, {512, 1, 2, 1024}, 5, 2, 0));
+
// CONT of a 0<->2 permute at DeepSeek-V4 lightning-indexer shapes:
// indexer_kq is [n_kv, n_tokens, n_head=64] and gets ggml_cont(ggml_permute(.., 2,1,0,3)).
for (int64_t n_kv : { 1024, 1280, 2048, 2304 }) {