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-metal / kernels / pool.metal

200 lines · 5733 bytes Raw Blame History
1#include "common.h"
2
3kernel void kernel_pool_2d_max_f32(
4 constant ggml_metal_kargs_pool_2d & args,
5 device const float * src0,
6 device float * dst,
7 uint gid[[thread_position_in_grid]]) {
8
9 if (gid >= args.np) {
10 return;
11 }
12
13 const int idx = gid;
14 const int I_HW = args.IH * args.IW;
15 const int O_HW = args.OH * args.OW;
16 const int nc = idx / O_HW;
17 const int cur_oh = idx % O_HW / args.OW;
18 const int cur_ow = idx % O_HW % args.OW;
19
20 device const float * i_ptr = src0 + nc * I_HW;
21 device float * o_ptr = dst + nc * O_HW;
22
23 const int start_h = cur_oh * args.s1 - args.p1;
24 const int bh = MAX(0, start_h);
25 const int eh = MIN(args.IH, start_h + args.k1);
26 const int start_w = cur_ow * args.s0 - args.p0;
27 const int bw = MAX(0, start_w);
28 const int ew = MIN(args.IW, start_w + args.k0);
29
30 float res = -INFINITY;
31
32 for (int i = bh; i < eh; i += 1) {
33 for (int j = bw; j < ew; j += 1) {
34 res = MAX(res, i_ptr[i * args.IW + j]);
35 }
36 }
37
38 o_ptr[cur_oh * args.OW + cur_ow] = res;
39}
40
41kernel void kernel_pool_2d_avg_f32(
42 constant ggml_metal_kargs_pool_2d & args,
43 device const float * src0,
44 device float * dst,
45 uint gid[[thread_position_in_grid]]) {
46
47 if (gid >= args.np) {
48 return;
49 }
50
51 const int idx = gid;
52 const int I_HW = args.IH * args.IW;
53 const int O_HW = args.OH * args.OW;
54 const int nc = idx / O_HW;
55 const int cur_oh = idx % O_HW / args.OW;
56 const int cur_ow = idx % O_HW % args.OW;
57
58 device const float * i_ptr = src0 + nc * I_HW;
59 device float * o_ptr = dst + nc * O_HW;
60
61 const int start_h = cur_oh * args.s1 - args.p1;
62 const int bh = MAX(0, start_h);
63 const int eh = MIN(args.IH, start_h + args.k1);
64 const int start_w = cur_ow * args.s0 - args.p0;
65 const int bw = MAX(0, start_w);
66 const int ew = MIN(args.IW, start_w + args.k0);
67 // const float scale = 1. / ((eh - bh) * (ew - bw));
68 const float scale = 1. / (args.k0 * args.k1);
69
70 float res = 0;
71
72 for (int i = bh; i < eh; i += 1) {
73 for (int j = bw; j < ew; j += 1) {
74 float cur = i_ptr[i * args.IW + j];
75 res += cur * scale;
76 }
77 }
78
79 o_ptr[cur_oh * args.OW + cur_ow] = res;
80}
81
82
83kernel void kernel_pool_1d_max_f32(
84 constant ggml_metal_kargs_pool_1d & args,
85 device const float * src,
86 device float * dst,
87 threadgroup char * shmem [[threadgroup(0)]],
88 uint tgpig[[threadgroup_position_in_grid]],
89 uint tpitg[[thread_position_in_threadgroup]],
90 uint sgitg[[simdgroup_index_in_threadgroup]],
91 uint tiisg[[thread_index_in_simdgroup]],
92 uint ntg[[threads_per_threadgroup]]) {
93 if (tgpig >= args.np) {
94 return;
95 }
96
97 const int out = (int) tgpig;
98 const int ow = out % (int) args.OW;
99 const int row = out / (int) args.OW;
100
101 const int base = ow * args.s0 - args.p0;
102 const int src_off = row * args.IW;
103 const int dst_off = row * args.OW;
104
105 float lmax = -INFINITY;
106
107 for (int ki = tpitg; ki < args.k0; ki += ntg) {
108 const int j = base + ki;
109 if (j >= 0 && j < args.IW) {
110 lmax = max(lmax, src[src_off + j]);
111 }
112 }
113
114 float max_val = simd_max(lmax);
115
116 threadgroup float * shared_max = (threadgroup float *) shmem;
117
118 if (ntg > N_SIMDWIDTH) {
119 if (sgitg == 0) {
120 shared_max[tiisg] = -INFINITY;
121 }
122 threadgroup_barrier(mem_flags::mem_threadgroup);
123
124 if (tiisg == 0) {
125 shared_max[sgitg] = max_val;
126 }
127 threadgroup_barrier(mem_flags::mem_threadgroup);
128
129 max_val = shared_max[tiisg];
130 max_val = simd_max(max_val);
131 }
132
133 if (tpitg == 0) {
134 dst[dst_off + ow] = max_val;
135 }
136}
137
138kernel void kernel_pool_1d_avg_f32(
139 constant ggml_metal_kargs_pool_1d & args,
140 device const float * src,
141 device float * dst,
142 threadgroup char * shmem [[threadgroup(0)]],
143 uint tgpig[[threadgroup_position_in_grid]],
144 uint tpitg[[thread_position_in_threadgroup]],
145 uint sgitg[[simdgroup_index_in_threadgroup]],
146 uint tiisg[[thread_index_in_simdgroup]],
147 uint ntg[[threads_per_threadgroup]]) {
148 if (tgpig >= args.np) {
149 return;
150 }
151
152 const int out = (int) tgpig;
153 const int ow = out % (int) args.OW;
154 const int row = out / (int) args.OW;
155
156 const int base = ow * args.s0 - args.p0;
157 const int src_off = row * args.IW;
158 const int dst_off = row * args.OW;
159
160 float lsum = 0.0f;
161 int lcnt = 0;
162
163 for (int ki = tpitg; ki < args.k0; ki += ntg) {
164 const int j = base + ki;
165 if (j >= 0 && j < args.IW) {
166 lsum += src[src_off + j];
167 lcnt += 1;
168 }
169 }
170
171 float sum = simd_sum(lsum);
172 float cnt = simd_sum((float) lcnt);
173
174 threadgroup float * shared_sum = (threadgroup float *) shmem;
175 threadgroup float * shared_cnt = (threadgroup float *) shmem + N_SIMDWIDTH;
176
177 if (ntg > N_SIMDWIDTH) {
178 if (sgitg == 0) {
179 shared_sum[tiisg] = 0.0f;
180 shared_cnt[tiisg] = 0.0f;
181 }
182 threadgroup_barrier(mem_flags::mem_threadgroup);
183
184 if (tiisg == 0) {
185 shared_sum[sgitg] = sum;
186 shared_cnt[sgitg] = cnt;
187 }
188 threadgroup_barrier(mem_flags::mem_threadgroup);
189
190 sum = shared_sum[tiisg];
191 cnt = shared_cnt[tiisg];
192
193 sum = simd_sum(sum);
194 cnt = simd_sum(cnt);
195 }
196
197 if (tpitg == 0) {
198 dst[dst_off + ow] = (cnt > 0.0f) ? (sum / cnt) : 0.0f;
199 }
200}