okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
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_rows(src0)); 28 29 const int64_t ncols = src0->ne[0]; 30 const int64_t nrows = ggml_nrows(src0); 31 32 if (ggml_is_contiguous(src0)) { 33 sum_rows_f32_cuda(src0_d, dst_d, ncols, nrows, stream); 34 return; 35 } 36 37 const dim3 block_nums(nrows, 1, 1); 38 39 const int id = ggml_cuda_get_device(); 40 const int nsm = ggml_cuda_info().devices[id].nsm; 41 dim3 block_dims; 42 if ((nrows / nsm) < 2) { 43 // Increase num threads to 512 for small nrows to better hide the latency 44 block_dims = dim3(512, 1, 1); 45 } else { 46 // Enough active SMs to hide latency, use smaller blocks to allow better scheduling 47 block_dims = dim3(ncols < 1024 ? 32 : 128, 1, 1); 48 } 49 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream); 50 const char * src0_d_bytes = (const char *) src0->data; 51 ggml_cuda_kernel_launch(reduce_rows_f32_strided</*norm=*/false>, launch_params, src0_d_bytes, dst_d, ncols, 52 src0->ne[1], src0->ne[2], src0->nb[1], src0->nb[2], src0->nb[3]); 53}