interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cuda / reduce_rows.cuh

72 lines · 2105 bytes Raw Blame History
1#include "common.cuh"
2
3static __device__ __forceinline__ float reduce_row_f32(const float * x, const int ncols) {
4 const int col = threadIdx.x;
5
6 float sum = 0.0f;
7 const int num_unroll = 8;
8 float temp[num_unroll];
9 float sum_temp[num_unroll] = { 0.0f };
10
11 ggml_cuda_pdl_sync();
12 for (int i = col; i < ncols;) {
13 for (int j = 0; j < num_unroll; ++j) {
14 if (i < ncols) {
15 temp[j] = x[i];
16 } else {
17 temp[j] = 0;
18 }
19 i += blockDim.x;
20 }
21 for (int j = 0; j < num_unroll; ++j) {
22 sum_temp[j] += temp[j];
23 }
24 }
25 for (int j = 0; j < num_unroll; ++j) {
26 sum += sum_temp[j];
27 }
28
29 // sum up partial sums
30 __shared__ float shared_vals[32];
31 sum = block_reduce<block_reduce_method::SUM>(sum, shared_vals);
32
33 return sum;
34}
35
36// Row reduction kernel template - compute sum (norm=false) or mean (norm=true)
37template <bool norm>
38static __global__ void reduce_rows_f32(const float * x_ptr, float * dst_ptr, const int ncols) {
39 float * GGML_CUDA_RESTRICT dst = dst_ptr;
40 const int64_t row = blockIdx.x;
41 const int col = threadIdx.x;
42
43 const float * GGML_CUDA_RESTRICT x = x_ptr + row*ncols;
44 const float sum = reduce_row_f32(x, ncols);
45
46 if (col != 0) {
47 return;
48 }
49
50 dst[row] = norm ? sum / ncols : sum;
51}
52
53template <bool norm>
54static __global__ void reduce_rows_f32_strided(const char * x_ptr, float * dst_ptr, const int ncols,
55 const int64_t ne1, const int64_t ne2, const int64_t nb1, const int64_t nb2, const int64_t nb3) {
56 float * GGML_CUDA_RESTRICT dst = dst_ptr;
57 const int64_t row = blockIdx.x;
58 const int col = threadIdx.x;
59
60 const int64_t i1 = row % ne1;
61 const int64_t i2 = (row / ne1) % ne2;
62 const int64_t i3 = row / (ne1 * ne2);
63
64 const float * GGML_CUDA_RESTRICT x = (const float *) (x_ptr + i1*nb1 + i2*nb2 + i3*nb3);
65 const float sum = reduce_row_f32(x, ncols);
66
67 if (col != 0) {
68 return;
69 }
70
71 dst[row] = norm ? sum / ncols : sum;
72}