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 / pool2d.cu

160 lines · 5717 bytes Raw Blame History
1#include "pool2d.cuh"
2
3template <typename Ti, typename To>
4static __global__ void pool2d_nchw_kernel(
5 const int ih, const int iw, const int oh, const int ow,
6 const int kh, const int kw, const int sh, const int sw,
7 const int ph, const int pw, const int parallel_elements,
8 const Ti* src, To* dst, const enum ggml_op_pool op) {
9 int idx = threadIdx.x + blockIdx.x * blockDim.x;
10 if (idx >= parallel_elements) {
11 return;
12 }
13
14 const int I_HW = ih * iw;
15 const int O_HW = oh * ow;
16 const int nc = idx / O_HW;
17 const int cur_oh = idx % O_HW / ow;
18 const int cur_ow = idx % O_HW % ow;
19 const Ti* i_ptr = src + nc * I_HW;
20 To* o_ptr = dst + nc * O_HW;
21 const int start_h = cur_oh * sh - ph;
22 const int bh = max(0, start_h);
23 const int eh = min(ih, start_h + kh);
24 const int start_w = cur_ow * sw - pw;
25 const int bw = max(0, start_w);
26 const int ew = min(iw, start_w + kw);
27 const To scale = 1. / (kh * kw);
28 To res = 0;
29
30 switch (op) {
31 case GGML_OP_POOL_AVG: res = 0; break;
32 case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
33 default: assert(false);
34 }
35
36 for (int i = bh; i < eh; i += 1) {
37 for (int j = bw; j < ew; j += 1) {
38#if __CUDA_ARCH__ >= 350
39 Ti cur = __ldg(i_ptr + i * iw + j);
40#else
41 Ti cur = i_ptr[i * iw + j];
42#endif
43 switch (op) {
44 case GGML_OP_POOL_AVG: res += cur * scale; break;
45 case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
46 default: assert(false);
47 }
48 }
49 }
50 o_ptr[cur_oh * ow + cur_ow] = res;
51}
52
53template <typename Ti, typename To>
54static __global__ void pool2d_nchw_kernel_warp(
55 const int ih, const int iw, const int oh, const int ow,
56 const int kh, const int kw, const int sh, const int sw,
57 const int ph, const int pw, const int parallel_elements,
58 const Ti * __restrict__ src, To * __restrict__ dst, const enum ggml_op_pool op) {
59 const int warp_id = (threadIdx.x + blockIdx.x * blockDim.x) / WARP_SIZE;
60 const int lane = threadIdx.x % WARP_SIZE;
61 if (warp_id >= parallel_elements) {
62 return;
63 }
64
65 const int I_HW = ih * iw;
66 const int O_HW = oh * ow;
67 const int nc = warp_id / O_HW;
68 const int cur_oh = warp_id % O_HW / ow;
69 const int cur_ow = warp_id % O_HW % ow;
70 const Ti* i_ptr = src + nc * I_HW;
71
72 const int start_h = cur_oh * sh - ph;
73 const int bh = max(0, start_h);
74 const int eh = min(ih, start_h + kh);
75 const int start_w = cur_ow * sw - pw;
76 const int bw = max(0, start_w);
77 const int ew = min(iw, start_w + kw);
78
79 const int win_w = ew - bw;
80 const int win_elems = (eh - bh) * win_w;
81 const To scale = 1. / (kh * kw);
82
83 To res;
84 switch (op) {
85 case GGML_OP_POOL_AVG: res = 0; break;
86 case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
87 default: res = 0; assert(false);
88 }
89
90 for (int t = lane; t < win_elems; t += WARP_SIZE) {
91 const int i = bh + t / win_w;
92 const int j = bw + t % win_w;
93 const Ti cur = i_ptr[i * iw + j];
94 switch (op) {
95 case GGML_OP_POOL_AVG: res += cur * scale; break;
96 case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
97 default: break;
98 }
99 }
100
101#pragma unroll
102 for (int offset = WARP_SIZE/2; offset > 0; offset >>= 1) {
103 const To other = __shfl_xor_sync(0xFFFFFFFF, res, offset, WARP_SIZE);
104 res = (op == GGML_OP_POOL_MAX) ? max(res, other) : res + other;
105 }
106
107 if (lane == 0) {
108 dst[nc * O_HW + cur_oh * ow + cur_ow] = res;
109 }
110}
111
112static void pool2d_nchw_kernel_f32_f32_cuda(
113 const int ih, const int iw, const int oh, const int ow,
114 const int kh, const int kw, const int sh, const int sw,
115 const int ph, const int pw, const int parallel_elements,
116 const float * src, float * dst, const enum ggml_op_pool op,
117 cudaStream_t stream) {
118
119 if (kh * kw >= CUDA_POOL2D_WARP_KERNEL_MIN_WINDOW) {
120 const int warps_per_block = CUDA_POOL2D_BLOCK_SIZE / WARP_SIZE;
121 const int num_blocks = (parallel_elements + warps_per_block - 1) / warps_per_block;
122 pool2d_nchw_kernel_warp<<<num_blocks, CUDA_POOL2D_BLOCK_SIZE, 0, stream>>>(ih, iw, oh, ow, kh, kw, sh, sw, ph, pw, parallel_elements, src, dst, op);
123 return;
124 }
125
126 const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
127 dim3 block_nums(num_blocks);
128 pool2d_nchw_kernel<<<block_nums, CUDA_POOL2D_BLOCK_SIZE, 0, stream>>>(ih, iw, oh, ow, kh, kw, sh, sw, ph, pw, parallel_elements, src, dst, op);
129}
130
131void ggml_cuda_op_pool2d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
132 const ggml_tensor * src0 = dst->src[0];
133 const float * src0_d = (const float *)src0->data;
134 float * dst_d = (float *)dst->data;
135 cudaStream_t stream = ctx.stream();
136
137 GGML_ASSERT(src0->type == GGML_TYPE_F32);
138 GGML_ASSERT( dst->type == GGML_TYPE_F32);
139
140 const int32_t * opts = (const int32_t *)dst->op_params;
141 enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
142 const int k0 = opts[1];
143 const int k1 = opts[2];
144 const int s0 = opts[3];
145 const int s1 = opts[4];
146 const int p0 = opts[5];
147 const int p1 = opts[6];
148
149 const int64_t IH = src0->ne[1];
150 const int64_t IW = src0->ne[0];
151
152 const int64_t N = dst->ne[3];
153 const int64_t OC = dst->ne[2];
154 const int64_t OH = dst->ne[1];
155 const int64_t OW = dst->ne[0];
156
157 const int parallel_elements = N * OC * OH * OW;
158
159 pool2d_nchw_kernel_f32_f32_cuda(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0, parallel_elements, src0_d, dst_d, op, stream);
160}