char-fork/llama.cpp

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

Andreas KieslingerProgrammatic Dependent Launch (PDL) for more performance on newer NVIDIA GPUs (Hopper+) (#22522)e94722822

char/aliceai-t5-moe
2.3 KiB47 linesraw
1#include "reduce_rows.cuh"
2#include "sumrows.cuh"
3
4void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
5    const int  id  = ggml_cuda_get_device();
6    const int  nsm = ggml_cuda_info().devices[id].nsm;
7    const dim3 block_nums(nrows, 1, 1);
8    if ((nrows / nsm) < 2) {
9        const dim3 block_dims(512, 1, 1);
10        const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
11        ggml_cuda_kernel_launch(reduce_rows_f32</*norm=*/false>, launch_params, x, dst, ncols);
12    } else {
13        const dim3 block_dims(ncols < 1024 ? 32 : 128, 1, 1);
14        const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
15        ggml_cuda_kernel_launch(reduce_rows_f32</*norm=*/false>, launch_params, x, dst, ncols);
16    }
17}
18
19void ggml_cuda_op_sum_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
20    const ggml_tensor * src0 = dst->src[0];
21    const float * src0_d = (const float *)src0->data;
22    float * dst_d = (float *)dst->data;
23    cudaStream_t stream = ctx.stream();
24
25    GGML_ASSERT(src0->type == GGML_TYPE_F32);
26    GGML_ASSERT( dst->type == GGML_TYPE_F32);
27    GGML_ASSERT(ggml_is_contiguous(src0));
28
29    const int64_t ncols = src0->ne[0];
30    const int64_t nrows = ggml_nrows(src0);
31
32    const dim3 block_nums(nrows, 1, 1);
33
34    const int id  = ggml_cuda_get_device();
35    const int nsm = ggml_cuda_info().devices[id].nsm;
36    if ((nrows / nsm) < 2) {
37        // Increase num threads to 512 for small nrows to better hide the latency
38        const dim3 block_dims(512, 1, 1);
39        const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
40        ggml_cuda_kernel_launch(reduce_rows_f32</*norm=*/false>, launch_params, src0_d, dst_d, ncols);
41    } else {
42        // Enough active SMs to hide latency, use smaller blocks to allow better scheduling
43        const dim3 block_dims(ncols < 1024 ? 32 : 128, 1, 1);
44        const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
45        ggml_cuda_kernel_launch(reduce_rows_f32</*norm=*/false>, launch_params, src0_d, dst_d, ncols);
46    }
47}