sumrows.cu 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "sumrows.cuh"
  2. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  3. const int row = blockIdx.x;
  4. const int col = threadIdx.x;
  5. float sum = 0.0f;
  6. for (int i = col; i < ncols; i += blockDim.x) {
  7. sum += x[row * ncols + i];
  8. }
  9. sum = warp_reduce_sum(sum);
  10. if (col == 0) {
  11. dst[row] = sum;
  12. }
  13. }
  14. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  15. const dim3 block_dims(WARP_SIZE, 1, 1);
  16. const dim3 block_nums(nrows, 1, 1);
  17. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  18. }
  19. void 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. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  25. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  26. GGML_ASSERT(ggml_is_contiguous(src0));
  27. const int64_t ncols = src0->ne[0];
  28. const int64_t nrows = ggml_nrows(src0);
  29. sum_rows_f32_cuda(src0_d, dst_d, ncols, nrows, stream);
  30. }