char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

adavyasggml-cuda: optimize conv_transpose_1d indexing (#25310)72874f559

char/aliceai-t5-moe
3.3 KiB88 linesraw
1#include "conv-transpose-1d.cuh"
2
3static  __global__ void conv_transpose_1d_kernel(
4        const int s0, const int p0, const int d0, const int output_size,
5        const int src0_ne0, const int src0_ne1, const int src0_ne2, const int src0_ne3,
6        const int src1_ne0, const int src1_ne1, const int src1_ne2, const int src1_ne3,
7        const int dst_ne0, const int dst_ne1, const int dst_ne2, const int dst_ne3,
8        const float * src0, const float * src1,  float * dst) {
9    int global_index = threadIdx.x + blockIdx.x * blockDim.x;
10    if (global_index >= output_size) {
11        return;
12    }
13
14    int out_t = global_index % dst_ne0;
15    int out_ch = (global_index / dst_ne0) % dst_ne1;
16    int plane = global_index / (dst_ne0 * dst_ne1);
17
18    float accumulator = 0;
19
20    for (int c = 0; c < src0_ne2; c++) {
21        int kernel_offset = src0_ne0 * (out_ch + src0_ne1 * c);
22        int input_offset = src1_ne0 * (c + src1_ne1 * plane);
23
24        for (int k = 0; k < src0_ne0; k++) {
25            int input_numer = out_t + p0 - k*d0;
26            if (input_numer < 0 || input_numer % s0 != 0) {
27                continue;
28            }
29
30            int input_t = input_numer / s0;
31            if (input_t >= src1_ne0) {
32                continue;
33            }
34
35            accumulator += src0[kernel_offset + k] * src1[input_offset + input_t];
36        }
37    }
38    dst[global_index] = accumulator;
39    GGML_UNUSED_VARS(src0_ne3, src1_ne2, src1_ne3, dst_ne2, dst_ne3);
40}
41
42static void conv_transpose_1d_f32_f32_cuda(
43        const int s0, const int p0, const int d0, const int output_size,
44        const int src0_ne0, const int src0_ne1, const int src0_ne2, const int src0_ne3,
45        const int src1_ne0, const int src1_ne1, const int src1_ne2, const int src1_ne3,
46        const int dst_ne0, const int dst_ne1, const int dst_ne2, const int dst_ne3,
47        const float * src0, const float * src1,  float * dst,
48        cudaStream_t stream) {
49
50    const int num_blocks = (output_size + CUDA_CONV_TRANPOSE_1D_BLOCK_SIZE - 1) / CUDA_CONV_TRANPOSE_1D_BLOCK_SIZE;
51    conv_transpose_1d_kernel<<<num_blocks,CUDA_CONV_TRANPOSE_1D_BLOCK_SIZE, 0, stream>>>(
52        s0,p0,d0,output_size,
53        src0_ne0, src0_ne1,  src0_ne2, src0_ne3,
54        src1_ne0, src1_ne1,  src1_ne2, src1_ne3,
55        dst_ne0,  dst_ne1,   dst_ne2,  dst_ne3,
56        src0,src1, dst);
57}
58
59void ggml_cuda_op_conv_transpose_1d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
60    const ggml_tensor * src0 = dst->src[0];
61    const float * src0_d = (const float *)src0->data;
62
63    const ggml_tensor * src1 = dst->src[1];
64    const float * src1_d = (const float *)src1->data;
65
66    float * dst_d = (float *)dst->data;
67    cudaStream_t stream = ctx.stream();
68
69    GGML_ASSERT(src0->type == GGML_TYPE_F32);
70    GGML_ASSERT( dst->type == GGML_TYPE_F32);
71
72    GGML_ASSERT(ggml_is_contiguous(src0));
73    GGML_ASSERT(ggml_is_contiguous(src1));
74
75    const int32_t * opts = (const int32_t *)dst->op_params;
76
77    const int s0 = opts[0];
78    const int p0 = 0;//opts[3];
79    const int d0 = 1;//opts[4];
80
81    const int64_t output_size = ggml_nelements(dst);
82
83    conv_transpose_1d_f32_f32_cuda(s0, p0, d0, output_size,
84        src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
85        src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3],
86        dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
87        src0_d, src1_d, dst_d, stream);
88}