Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions ggml/src/ggml-cuda/concat.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <stdint.h>

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 <typename T, int dim>
static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE) concat_cont(const T * x,
Expand Down Expand Up @@ -79,6 +83,60 @@ static void concat_cont_cuda(const T * x,
concat_cont<T, 2><<<num_blocks, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(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 <typename T, int dim>
static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE)
Expand Down Expand Up @@ -140,7 +198,22 @@ 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) {
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<<<grid_dim, block_dim, 0, stream>>>(
(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;
Expand Down Expand Up @@ -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];

Expand All @@ -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<uint8_t>(src0, src1, dst, dim, stream);
concat_cuda<uint8_t>(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<uint8_t>(src0, src1, dst, dim, stream);
concat_cuda<uint8_t>(src0, src1, dst, dim, stream, cc);
break;
case 2:
concat_cuda<uint16_t>(src0, src1, dst, dim, stream);
concat_cuda<uint16_t>(src0, src1, dst, dim, stream, cc);
break;
case 4:
concat_cuda<uint32_t>(src0, src1, dst, dim, stream);
concat_cuda<uint32_t>(src0, src1, dst, dim, stream, cc);
break;
case 8:
concat_cuda<uint64_t>(src0, src1, dst, dim, stream);
concat_cuda<uint64_t>(src0, src1, dst, dim, stream, cc);
break;
default:
GGML_ABORT("Unsupported type size: %zu", ggml_type_size(src0->type));
Expand Down
2 changes: 2 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13114,6 +13114,7 @@ static std::vector<std::unique_ptr<test_case>> 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));

Expand Down Expand Up @@ -14064,6 +14065,7 @@ static std::vector<std::unique_ptr<test_case>> 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));
Expand Down