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-hexagon / htp / glu-ops.c

1172 lines · 55626 bytes Raw Blame History
1#pragma clang diagnostic ignored "-Wunused-variable"
2#pragma clang diagnostic ignored "-Wunused-function"
3#pragma clang diagnostic ignored "-Wunused-but-set-variable"
4
5#include <HAP_farf.h>
6
7#include <math.h>
8#include <string.h>
9
10#include "dma-queue.h"
11#include "hvx-utils.h"
12
13#define GGML_COMMON_DECL_C
14#include "ggml-common.h"
15#include "htp-ctx.h"
16#include "htp-ops.h"
17#include "hex-common.h"
18#include "htp-tensor.h"
19#include "htp-vtcm.h"
20#include "glu-ops.h"
21
22#define htp_glu_preamble \
23 const struct htp_tensor * src0 = gctx->octx->src[0]; \
24 const struct htp_tensor * src1 = gctx->octx->src[1]; \
25 const struct htp_tensor * dst = gctx->octx->dst; \
26 \
27 const uint32_t ne00 = src0->ne[0]; \
28 const uint32_t ne01 = src0->ne[1]; \
29 const uint32_t ne02 = src0->ne[2]; \
30 const uint32_t ne03 = src0->ne[3]; \
31 \
32 const uint32_t nb00 = src0->nb[0]; \
33 const uint32_t nb01 = src0->nb[1]; \
34 const uint32_t nb02 = src0->nb[2]; \
35 const uint32_t nb03 = src0->nb[3]; \
36 \
37 const uint32_t ne10 = src1 ? src1->ne[0] : 0; \
38 const uint32_t ne11 = src1 ? src1->ne[1] : 0; \
39 const uint32_t ne12 = src1 ? src1->ne[2] : 0; \
40 const uint32_t ne13 = src1 ? src1->ne[3] : 0; \
41 \
42 const uint32_t nb10 = src1 ? src1->nb[0] : 0; \
43 const uint32_t nb11 = src1 ? src1->nb[1] : 0; \
44 const uint32_t nb12 = src1 ? src1->nb[2] : 0; \
45 const uint32_t nb13 = src1 ? src1->nb[3] : 0; \
46 \
47 const uint32_t ne0 = dst->ne[0]; \
48 const uint32_t ne1 = dst->ne[1]; \
49 const uint32_t ne2 = dst->ne[2]; \
50 const uint32_t ne3 = dst->ne[3]; \
51 \
52 const uint32_t nb0 = dst->nb[0]; \
53 const uint32_t nb1 = dst->nb[1]; \
54 const uint32_t nb2 = dst->nb[2]; \
55 const uint32_t nb3 = dst->nb[3];
56
57struct htp_glu_context;
58
59typedef void (*glu_compute_f32_fn_t)(const float * restrict src0,
60 const float * restrict src1,
61 float * restrict dst,
62 const uint32_t num_rows,
63 const struct htp_glu_context * gctx);
64
65typedef void (*glu_compute_f16_fn_t)(const _Float16 * restrict src0,
66 const _Float16 * restrict src1,
67 _Float16 * restrict dst,
68 const uint32_t num_rows,
69 const struct htp_glu_context * gctx);
70
71struct htp_glu_context {
72 struct htp_ops_context * octx;
73
74 void * compute;
75 const char * op_str;
76
77 // Precomputed values
78 dma_addr_t data_src0;
79 dma_addr_t data_src1;
80 dma_addr_t data_dst;
81
82 size_t src0_row_size;
83 size_t src1_row_size;
84 size_t dst_row_size;
85
86 size_t src0_row_stride;
87 size_t src1_row_stride;
88
89 size_t src0_row_size_aligned;
90 size_t src1_row_size_aligned;
91 size_t dst_row_size_aligned;
92
93 size_t src0_spad_half_size;
94 size_t src1_spad_half_size;
95 size_t dst_spad_half_size;
96
97 uint32_t block;
98 uint32_t src0_nrows;
99 uint32_t src0_nrows_per_thread;
100 uint32_t row_start;
101 int nc;
102
103 uint8_t * vtcm_src0;
104 uint8_t * vtcm_src1;
105 uint8_t * vtcm_dst;
106
107 size_t vtcm_src0_size_per_thread;
108 size_t vtcm_src1_size_per_thread;
109 size_t vtcm_dst_size_per_thread;
110};
111
112#define htp_glu_op_preamble \
113 const size_t src0_row_size_aligned = gctx->src0_row_size_aligned; \
114 const size_t src1_row_size_aligned = gctx->src1_row_size_aligned; \
115 const size_t dst_row_size_aligned = gctx->dst_row_size_aligned; \
116 const int nc = gctx->nc;
117
118// swiglu(x) = x1 * sigmoid(x0)
119static void swiglu_f32(const float * restrict src0,
120 const float * restrict src1,
121 float * restrict dst,
122 const uint32_t num_rows,
123 const struct htp_glu_context * gctx) {
124 htp_glu_op_preamble;
125
126 for (uint32_t ib = 0; ib < num_rows; ib++) {
127 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
128 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
129 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
130
131 hvx_sigmoid_f32_aa(dst_ptr, src0_ptr, nc);
132 hvx_mul_mul_f32_aa(dst_ptr, src0_ptr, dst_ptr, src1_ptr, nc);
133 }
134}
135
136// out = x * sigmoid(alpha * x) * (clamp(y, -limit, limit) + 1.f)
137static void swiglu_oai_f32(const float * restrict src0,
138 const float * restrict src1,
139 float * restrict dst,
140 const uint32_t num_rows,
141 const struct htp_glu_context * gctx) {
142 htp_glu_op_preamble;
143 const float alpha = ((const float *) (gctx->octx->op_params))[2];
144 const float limit = ((const float *) (gctx->octx->op_params))[3];
145
146 for (uint32_t ib = 0; ib < num_rows; ib++) {
147 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
148 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
149 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
150
151 // x (src0_ptr) = std::min(src0_p[k], limit);
152 hvx_min_scalar_f32((uint8_t *) src0_ptr, src0_ptr, limit, nc);
153 // y1 (src1_ptr) = std::clamp(src1_p[k], -limit, limit);
154 hvx_clamp_scalar_f32((uint8_t *) src1_ptr, src1_ptr, -limit, limit, nc);
155 // y (src1_ptr) = y1 + 1.f
156 hvx_add_scalar_f32((uint8_t *) src1_ptr, src1_ptr, 1.0, nc);
157 // x1 (dst_ptr) = alpha * x
158 hvx_mul_scalar_f32(dst_ptr, src0_ptr, alpha, nc);
159 // x2 (dst_ptr) = sigmoid(x1) = 1/(1+exp(-x1))
160 hvx_sigmoid_f32_aa(dst_ptr, dst_ptr, nc);
161 // out = x * sigmoid(alpha * x) * (y + 1.f)
162 hvx_mul_mul_f32_aa(dst_ptr, src0_ptr, dst_ptr, src1_ptr, nc);
163 }
164}
165
166static void swiglu_clamp_f32(const float * restrict src0,
167 const float * restrict src1,
168 float * restrict dst,
169 const uint32_t num_rows,
170 const struct htp_glu_context * gctx) {
171 htp_glu_op_preamble;
172 const float limit = ((const float *) (gctx->octx->op_params))[3];
173
174 for (uint32_t ib = 0; ib < num_rows; ib++) {
175 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
176 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
177 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
178
179 hvx_min_scalar_f32((uint8_t *) src0_ptr, src0_ptr, limit, nc);
180 hvx_clamp_scalar_f32((uint8_t *) src1_ptr, src1_ptr, -limit, limit, nc);
181 hvx_sigmoid_f32_aa(dst_ptr, src0_ptr, nc);
182 hvx_mul_mul_f32_aa(dst_ptr, src0_ptr, dst_ptr, src1_ptr, nc);
183 }
184}
185
186static const float GELU_COEF_A = 0.044715f;
187static const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
188
189static inline void hvx_geglu_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
190 assert((unsigned long) dst % 128 == 0);
191 assert((unsigned long) src0 % 128 == 0);
192 assert((unsigned long) src1 % 128 == 0);
193
194 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
195 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
196 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
197
198 const uint32_t epv = 128 / sizeof(float);
199 const uint32_t nvec = n / epv;
200 const uint32_t nloe = n % epv;
201
202 const float GELU_COEF_A_TIMES_SQRT = GELU_COEF_A * SQRT_2_OVER_PI;
203
204 const HVX_Vector v_coef_a_times_sqrt = hvx_vec_splat_f32(GELU_COEF_A_TIMES_SQRT);
205 const HVX_Vector v_sqrt_2_pi = hvx_vec_splat_f32(SQRT_2_OVER_PI);
206 const HVX_Vector v_one = hvx_vec_splat_f32(1.0f);
207 const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
208 const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
209
210 uint32_t i = 0;
211
212 _Pragma("unroll(4)")
213 for (; i < nvec; i++) {
214 HVX_Vector x = vsrc0[i];
215 HVX_Vector g = vsrc1[i];
216
217 HVX_Vector x2 = hvx_vec_mul_f32_f32(x, x);
218 HVX_Vector coef = hvx_vec_mul_f32_f32(x2, v_coef_a_times_sqrt);
219 coef = hvx_vec_add_f32_f32(coef, v_sqrt_2_pi);
220 HVX_Vector inner = hvx_vec_mul_f32_f32(x, coef);
221
222 // y2 = 2 * inner = inner + inner
223 HVX_Vector y2 = hvx_vec_add_f32_f32(inner, inner);
224
225 // Fast sigmoid approximation (2 iterations)
226 HVX_Vector sig2y = hvx_vec_fast_sigmoid_f32_guard_2it(y2, v_one, v_max_exp, v_min_exp);
227
228 HVX_Vector gelu_x = hvx_vec_mul_f32_f32(x, sig2y);
229 vdst[i] = hvx_vec_mul_f32_f32(gelu_x, g);
230 }
231
232 if (nloe) {
233 HVX_Vector x = vsrc0[i];
234 HVX_Vector g = vsrc1[i];
235
236 HVX_Vector x2 = hvx_vec_mul_f32_f32(x, x);
237 HVX_Vector coef = hvx_vec_mul_f32_f32(x2, v_coef_a_times_sqrt);
238 coef = hvx_vec_add_f32_f32(coef, v_sqrt_2_pi);
239 HVX_Vector inner = hvx_vec_mul_f32_f32(x, coef);
240
241 HVX_Vector y2 = hvx_vec_add_f32_f32(inner, inner);
242
243 HVX_Vector sig2y = hvx_vec_fast_sigmoid_f32_guard_2it(y2, v_one, v_max_exp, v_min_exp);
244
245 HVX_Vector gelu_x = hvx_vec_mul_f32_f32(x, sig2y);
246 HVX_Vector res = hvx_vec_mul_f32_f32(gelu_x, g);
247 hvx_vec_store_a((void *) &vdst[i], nloe * sizeof(float), res);
248 }
249}
250
251static inline void hvx_geglu_quick_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
252 assert((unsigned long) dst % 128 == 0);
253 assert((unsigned long) src0 % 128 == 0);
254 assert((unsigned long) src1 % 128 == 0);
255
256 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
257 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
258 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
259
260 const uint32_t epv = 128 / sizeof(float);
261 const uint32_t nvec = n / epv;
262 const uint32_t nloe = n % epv;
263
264 const HVX_Vector v_scale = hvx_vec_splat_f32(1.702f);
265 const HVX_Vector v_one = hvx_vec_splat_f32(1.0f);
266 const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
267 const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
268
269 uint32_t i = 0;
270
271 _Pragma("unroll(4)")
272 for (; i < nvec; i++) {
273 HVX_Vector x = vsrc0[i];
274 HVX_Vector g = vsrc1[i];
275 HVX_Vector scaled_x = hvx_vec_mul_f32_f32(x, v_scale);
276 HVX_Vector sigmoid_x = hvx_vec_fast_sigmoid_f32_guard_2it(scaled_x, v_one, v_max_exp, v_min_exp);
277 vdst[i] = hvx_vec_mul_f32_f32(hvx_vec_mul_f32_f32(x, sigmoid_x), g);
278 }
279
280 if (nloe) {
281 HVX_Vector x = vsrc0[i];
282 HVX_Vector g = vsrc1[i];
283 HVX_Vector scaled_x = hvx_vec_mul_f32_f32(x, v_scale);
284 HVX_Vector sigmoid_x = hvx_vec_fast_sigmoid_f32_guard_2it(scaled_x, v_one, v_max_exp, v_min_exp);
285 HVX_Vector result = hvx_vec_mul_f32_f32(hvx_vec_mul_f32_f32(x, sigmoid_x), g);
286 hvx_vec_store_a((void *) &vdst[i], nloe * sizeof(float), result);
287 }
288}
289
290static inline void hvx_geglu_erf_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
291 assert((unsigned long) dst % 128 == 0);
292 assert((unsigned long) src0 % 128 == 0);
293 assert((unsigned long) src1 % 128 == 0);
294
295 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
296 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
297 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
298
299 const uint32_t epv = 128 / sizeof(float);
300 const uint32_t nvec = n / epv;
301 const uint32_t nloe = n % epv;
302
303 uint32_t i = 0;
304
305 _Pragma("unroll(4)")
306 for (; i < nvec; i++) {
307 HVX_Vector x = vsrc0[i];
308 HVX_Vector g = vsrc1[i];
309 vdst[i] = hvx_vec_mul_f32_f32(hvx_vec_gelu_erf_f32(x), g);
310 }
311
312 if (nloe) {
313 HVX_Vector x = vsrc0[i];
314 HVX_Vector g = vsrc1[i];
315 HVX_Vector result = hvx_vec_mul_f32_f32(hvx_vec_gelu_erf_f32(x), g);
316 hvx_vec_store_a((void *) &vdst[i], nloe * sizeof(float), result);
317 }
318}
319
320static inline void hvx_geglu_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
321 assert((unsigned long) dst % 128 == 0);
322 assert((unsigned long) src0 % 128 == 0);
323 assert((unsigned long) src1 % 128 == 0);
324
325 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
326 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
327 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
328
329 const uint32_t epv = 128 / sizeof(_Float16);
330 const uint32_t nvec = n / epv;
331 const uint32_t nloe = n % epv;
332
333 const HVX_Vector v_coef_a = hvx_vec_splat_f16((_Float16) GELU_COEF_A);
334 const HVX_Vector v_sqrt_2_pi = hvx_vec_splat_f16((_Float16) SQRT_2_OVER_PI);
335 const HVX_Vector v_one = hvx_vec_splat_f16((_Float16) 1.0f);
336 const HVX_Vector v_half = hvx_vec_splat_f16((_Float16) 0.5f);
337
338 uint32_t i = 0;
339
340 _Pragma("unroll(4)")
341 for (; i < nvec; i++) {
342 HVX_Vector x = vsrc0[i];
343 HVX_Vector g = vsrc1[i];
344
345 // gelu(x) = 0.5*x*(1 + tanh(SQRT_2_OVER_PI*x*(1 + GELU_COEF_A*x*x)))
346 HVX_Vector x2 = hvx_vec_mul_f16_f16(x, x);
347 HVX_Vector coef = hvx_vec_add_f16_f16(hvx_vec_mul_f16_f16(x2, v_coef_a), v_one);
348 HVX_Vector inner = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x, coef), v_sqrt_2_pi);
349
350 HVX_Vector t = hvx_vec_add_f16_f16(hvx_vec_tanh_f16(inner), v_one);
351
352 HVX_Vector gelu_x = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x, t), v_half);
353 vdst[i] = hvx_vec_mul_f16_f16(gelu_x, g);
354 }
355
356 if (nloe) {
357 HVX_Vector x = vsrc0[i];
358 HVX_Vector g = vsrc1[i];
359
360 HVX_Vector x2 = hvx_vec_mul_f16_f16(x, x);
361 HVX_Vector coef = hvx_vec_add_f16_f16(hvx_vec_mul_f16_f16(x2, v_coef_a), v_one);
362 HVX_Vector inner = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x, coef), v_sqrt_2_pi);
363
364 HVX_Vector t = hvx_vec_add_f16_f16(hvx_vec_tanh_f16(inner), v_one);
365
366 HVX_Vector gelu_x = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x, t), v_half);
367 HVX_Vector res = hvx_vec_mul_f16_f16(gelu_x, g);
368 hvx_vec_store_a((void *) &vdst[i], nloe * sizeof(_Float16), res);
369 }
370}
371
372// geglu(x, g) = gelu(x) * g
373static void geglu_f32(const float * restrict src0,
374 const float * restrict src1,
375 float * restrict dst,
376 const uint32_t num_rows,
377 const struct htp_glu_context * gctx) {
378 htp_glu_op_preamble;
379
380 for (uint32_t ib = 0; ib < num_rows; ib++) {
381 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
382 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
383 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
384
385 hvx_geglu_f32_aa(dst_ptr, src0_ptr, src1_ptr, nc);
386 }
387}
388
389// geglu_quick(x, g) = x * sigmoid(1.702 * x) * g
390static void geglu_quick_f32(const float * restrict src0,
391 const float * restrict src1,
392 float * restrict dst,
393 const uint32_t num_rows,
394 const struct htp_glu_context * gctx) {
395 htp_glu_op_preamble;
396
397 for (uint32_t ib = 0; ib < num_rows; ib++) {
398 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
399 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
400 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
401
402 hvx_geglu_quick_f32_aa(dst_ptr, src0_ptr, src1_ptr, nc);
403 }
404}
405
406// geglu_erf(x, g) = gelu_erf(x) * g
407static void geglu_erf_f32(const float * restrict src0,
408 const float * restrict src1,
409 float * restrict dst,
410 const uint32_t num_rows,
411 const struct htp_glu_context * gctx) {
412 htp_glu_op_preamble;
413
414 for (uint32_t ib = 0; ib < num_rows; ib++) {
415 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
416 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
417 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
418
419 hvx_geglu_erf_f32_aa(dst_ptr, src0_ptr, src1_ptr, nc);
420 }
421}
422
423static inline void hvx_reglu_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
424 assert((unsigned long) dst % 128 == 0);
425 assert((unsigned long) src0 % 128 == 0);
426 assert((unsigned long) src1 % 128 == 0);
427
428 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
429 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
430 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
431
432 const uint32_t epv = 128 / sizeof(float);
433 const uint32_t nvec = n / epv;
434 const uint32_t nloe = n % epv;
435
436 const HVX_Vector v_zero = Q6_V_vzero();
437
438 uint32_t i = 0;
439
440 _Pragma("unroll(4)")
441 for (; i < nvec; i++) {
442 HVX_Vector x = Q6_Vsf_vmax_VsfVsf(vsrc0[i], v_zero);
443 vdst[i] = hvx_vec_mul_f32_f32(x, vsrc1[i]);
444 }
445
446 if (nloe) {
447 HVX_Vector x = Q6_Vsf_vmax_VsfVsf(vsrc0[i], v_zero);
448 HVX_Vector res = hvx_vec_mul_f32_f32(x, vsrc1[i]);
449 hvx_vec_store_a((void *) &vdst[i], nloe * sizeof(float), res);
450 }
451}
452
453// reglu(x, g) = relu(x) * g
454static void reglu_f32(const float * restrict src0,
455 const float * restrict src1,
456 float * restrict dst,
457 const uint32_t num_rows,
458 const struct htp_glu_context * gctx) {
459 htp_glu_op_preamble;
460
461 for (uint32_t ib = 0; ib < num_rows; ib++) {
462 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
463 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
464 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
465
466 hvx_reglu_f32_aa(dst_ptr, src0_ptr, src1_ptr, nc);
467 }
468}
469
470static __attribute__((noinline)) void hvx_sigglu_f32_vec(HVX_Vector * restrict vdst,
471 const HVX_Vector * restrict vsrc0,
472 const HVX_Vector * restrict vsrc1,
473 uint32_t nvec) {
474 const HVX_Vector v_one = hvx_vec_splat_f32(1.0f);
475 const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
476 const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
477
478 for (uint32_t i = 0; i < nvec; i++) {
479 HVX_Vector sig = hvx_vec_fast_sigmoid_f32_guard(vsrc0[i], v_one, v_max_exp, v_min_exp);
480 vdst[i] = hvx_vec_mul_f32_f32(sig, vsrc1[i]);
481 }
482}
483
484static __attribute__((noinline)) void hvx_sigglu_f32_tail(HVX_Vector * restrict vdst,
485 const HVX_Vector * restrict vsrc0,
486 const HVX_Vector * restrict vsrc1,
487 uint32_t nloe) {
488 const HVX_Vector v_one = hvx_vec_splat_f32(1.0f);
489 const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
490 const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
491
492 HVX_Vector sig = hvx_vec_fast_sigmoid_f32_guard(vsrc0[0], v_one, v_max_exp, v_min_exp);
493 HVX_Vector res = hvx_vec_mul_f32_f32(sig, vsrc1[0]);
494 hvx_vec_store_a((void *) vdst, nloe * sizeof(float), res);
495}
496
497static inline void hvx_sigglu_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
498 assert((unsigned long) dst % 128 == 0);
499 assert((unsigned long) src0 % 128 == 0);
500 assert((unsigned long) src1 % 128 == 0);
501
502 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
503 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
504 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
505
506 const uint32_t epv = 128 / sizeof(float);
507 const uint32_t nvec = n / epv;
508 const uint32_t nloe = n % epv;
509
510 if (nvec) {
511 hvx_sigglu_f32_vec(vdst, vsrc0, vsrc1, nvec);
512 }
513 if (nloe) {
514 hvx_sigglu_f32_tail(vdst + nvec, vsrc0 + nvec, vsrc1 + nvec, nloe);
515 }
516}
517
518// sigglu(x, g) = sigmoid(x) * g
519static void sigglu_f32(const float * restrict src0,
520 const float * restrict src1,
521 float * restrict dst,
522 const uint32_t num_rows,
523 const struct htp_glu_context * gctx) {
524 htp_glu_op_preamble;
525
526 for (uint32_t ib = 0; ib < num_rows; ib++) {
527 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
528 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
529 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
530
531 hvx_sigglu_f32_aa(dst_ptr, src0_ptr, src1_ptr, nc);
532 }
533}
534
535// swiglu(x) = x1 * sigmoid(x0)
536static void swiglu_f16(const _Float16 * restrict src0,
537 const _Float16 * restrict src1,
538 _Float16 * restrict dst,
539 const uint32_t num_rows,
540 const struct htp_glu_context * gctx) {
541 htp_glu_op_preamble;
542
543 for (uint32_t ib = 0; ib < num_rows; ib++) {
544 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
545 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
546 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
547
548 hvx_sigmoid_f16_aa(dst_ptr, src0_ptr, nc);
549 hvx_mul_mul_f16_aa(dst_ptr, src0_ptr, dst_ptr, src1_ptr, nc);
550 }
551}
552
553// out = x * sigmoid(alpha * x) * (clamp(y, -limit, limit) + 1.f)
554static void swiglu_oai_f16(const _Float16 * restrict src0,
555 const _Float16 * restrict src1,
556 _Float16 * restrict dst,
557 const uint32_t num_rows,
558 const struct htp_glu_context * gctx) {
559 htp_glu_op_preamble;
560 const _Float16 alpha = (_Float16) ((const float *) (gctx->octx->op_params))[2];
561 const _Float16 limit = (_Float16) ((const float *) (gctx->octx->op_params))[3];
562
563 for (uint32_t ib = 0; ib < num_rows; ib++) {
564 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
565 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
566 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
567
568 // x (src0_ptr) = std::min(src0_p[k], limit);
569 hvx_min_scalar_f16((uint8_t *) src0_ptr, src0_ptr, limit, nc);
570 // y1 (src1_ptr) = std::clamp(src1_p[k], -limit, limit);
571 hvx_clamp_scalar_f16((uint8_t *) src1_ptr, src1_ptr, -limit, limit, nc);
572 // y (src1_ptr) = y1 + 1.f
573 hvx_add_scalar_f16((uint8_t *) src1_ptr, src1_ptr, (_Float16) 1.0f, nc);
574 // x1 (dst_ptr) = alpha * x
575 hvx_mul_scalar_f16(dst_ptr, src0_ptr, alpha, nc);
576 // x2 (dst_ptr) = sigmoid(x1) = 1/(1+exp(-x1))
577 hvx_sigmoid_f16_aa(dst_ptr, dst_ptr, nc);
578 // out = x * sigmoid(alpha * x) * (y + 1.f)
579 hvx_mul_mul_f16_aa(dst_ptr, src0_ptr, dst_ptr, src1_ptr, nc);
580 }
581}
582
583static void swiglu_clamp_f16(const _Float16 * restrict src0,
584 const _Float16 * restrict src1,
585 _Float16 * restrict dst,
586 const uint32_t num_rows,
587 const struct htp_glu_context * gctx) {
588 htp_glu_op_preamble;
589 const _Float16 limit = (_Float16) ((const float *) (gctx->octx->op_params))[3];
590
591 for (uint32_t ib = 0; ib < num_rows; ib++) {
592 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
593 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
594 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
595
596 hvx_min_scalar_f16((uint8_t *) src0_ptr, src0_ptr, limit, nc);
597 hvx_clamp_scalar_f16((uint8_t *) src1_ptr, src1_ptr, -limit, limit, nc);
598 hvx_sigmoid_f16_aa(dst_ptr, src0_ptr, nc);
599 hvx_mul_mul_f16_aa(dst_ptr, src0_ptr, dst_ptr, src1_ptr, nc);
600 }
601}
602
603// geglu(x, g) = gelu(x) * g, gelu via tanh approximation
604static void geglu_f16(const _Float16 * restrict src0,
605 const _Float16 * restrict src1,
606 _Float16 * restrict dst,
607 const uint32_t num_rows,
608 const struct htp_glu_context * gctx) {
609 htp_glu_op_preamble;
610
611 for (uint32_t ib = 0; ib < num_rows; ib++) {
612 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
613 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
614 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
615
616 hvx_geglu_f16_aa(dst_ptr, src0_ptr, src1_ptr, nc);
617 }
618}
619
620static inline void hvx_reglu_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
621 assert((unsigned long) dst % 128 == 0);
622 assert((unsigned long) src0 % 128 == 0);
623 assert((unsigned long) src1 % 128 == 0);
624
625 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
626 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
627 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
628
629 const uint32_t epv = 128 / sizeof(_Float16);
630 const uint32_t nvec = n / epv;
631 const uint32_t nloe = n % epv;
632
633 const HVX_Vector v_zero = Q6_V_vzero();
634
635 uint32_t i = 0;
636
637 _Pragma("unroll(4)")
638 for (; i < nvec; i++) {
639 HVX_Vector x = Q6_Vhf_vmax_VhfVhf(vsrc0[i], v_zero);
640 vdst[i] = hvx_vec_mul_f16_f16(x, vsrc1[i]);
641 }
642
643 if (nloe) {
644 HVX_Vector x = Q6_Vhf_vmax_VhfVhf(vsrc0[i], v_zero);
645 HVX_Vector res = hvx_vec_mul_f16_f16(x, vsrc1[i]);
646 hvx_vec_store_a((void *) &vdst[i], nloe * sizeof(_Float16), res);
647 }
648}
649
650// reglu(x, g) = relu(x) * g
651static void reglu_f16(const _Float16 * restrict src0,
652 const _Float16 * restrict src1,
653 _Float16 * restrict dst,
654 const uint32_t num_rows,
655 const struct htp_glu_context * gctx) {
656 htp_glu_op_preamble;
657
658 for (uint32_t ib = 0; ib < num_rows; ib++) {
659 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
660 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
661 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
662
663 hvx_reglu_f16_aa(dst_ptr, src0_ptr, src1_ptr, nc);
664 }
665}
666
667static __attribute__((noinline)) void hvx_sigglu_f16_vec(HVX_Vector * restrict vdst,
668 const HVX_Vector * restrict vsrc0,
669 const HVX_Vector * restrict vsrc1,
670 uint32_t nvec) {
671 for (uint32_t i = 0; i < nvec; i++) {
672 HVX_Vector sig = hvx_vec_fast_sigmoid_f16(vsrc0[i]);
673 vdst[i] = hvx_vec_mul_f16_f16(sig, vsrc1[i]);
674 }
675}
676
677static __attribute__((noinline)) void hvx_sigglu_f16_tail(HVX_Vector * restrict vdst,
678 const HVX_Vector * restrict vsrc0,
679 const HVX_Vector * restrict vsrc1,
680 uint32_t nloe) {
681 HVX_Vector sig = hvx_vec_fast_sigmoid_f16(vsrc0[0]);
682 HVX_Vector res = hvx_vec_mul_f16_f16(sig, vsrc1[0]);
683 hvx_vec_store_a((void *) vdst, nloe * sizeof(_Float16), res);
684}
685
686static inline void hvx_sigglu_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
687 assert((unsigned long) dst % 128 == 0);
688 assert((unsigned long) src0 % 128 == 0);
689 assert((unsigned long) src1 % 128 == 0);
690
691 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
692 const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
693 const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
694
695 const uint32_t epv = 128 / sizeof(_Float16);
696 const uint32_t nvec = n / epv;
697 const uint32_t nloe = n % epv;
698
699 if (nvec) {
700 hvx_sigglu_f16_vec(vdst, vsrc0, vsrc1, nvec);
701 }
702 if (nloe) {
703 hvx_sigglu_f16_tail(vdst + nvec, vsrc0 + nvec, vsrc1 + nvec, nloe);
704 }
705}
706
707// sigglu(x, g) = sigmoid(x) * g
708static void sigglu_f16(const _Float16 * restrict src0,
709 const _Float16 * restrict src1,
710 _Float16 * restrict dst,
711 const uint32_t num_rows,
712 const struct htp_glu_context * gctx) {
713 htp_glu_op_preamble;
714
715 for (uint32_t ib = 0; ib < num_rows; ib++) {
716 const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
717 const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
718 uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
719
720 hvx_sigglu_f16_aa(dst_ptr, src0_ptr, src1_ptr, nc);
721 }
722}
723
724#define DEFINE_GLU_PER_THREAD(TYPE, SUFFIX) \
725 static void glu_##SUFFIX##_per_thread(unsigned int nth, unsigned int ith, void * data) { \
726 struct htp_glu_context * gctx = (struct htp_glu_context *) data; \
727 htp_glu_preamble; \
728 \
729 struct htp_thread_trace * tr = gctx->octx->ctx ? &gctx->octx->ctx->trace[ith] : NULL; \
730 \
731 size_t src0_row_size = gctx->src0_row_size; \
732 size_t src1_row_size = gctx->src1_row_size; \
733 size_t dst_row_size = gctx->dst_row_size; \
734 \
735 size_t src0_row_stride = gctx->src0_row_stride; \
736 size_t src1_row_stride = gctx->src1_row_stride; \
737 \
738 const uint32_t src0_nrows = gctx->src0_nrows; \
739 const uint32_t src0_nrows_per_thread = gctx->src0_nrows_per_thread; \
740 \
741 const uint32_t src0_start_row = gctx->row_start + src0_nrows_per_thread * ith; \
742 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, gctx->row_start + src0_nrows); \
743 \
744 /* no work for this thread */ \
745 if (src0_start_row >= src0_end_row) { \
746 return; \
747 } \
748 \
749 const dma_addr_t data_src0 = gctx->data_src0; \
750 const dma_addr_t data_src1 = gctx->data_src1; \
751 const dma_addr_t data_dst = gctx->data_dst; \
752 \
753 const size_t src0_row_size_aligned = gctx->src0_row_size_aligned; \
754 const size_t src1_row_size_aligned = gctx->src1_row_size_aligned; \
755 const size_t dst_row_size_aligned = gctx->dst_row_size_aligned; \
756 \
757 uint8_t * restrict src0_spad_data = gctx->vtcm_src0 + (ith * gctx->vtcm_src0_size_per_thread); \
758 uint8_t * restrict src1_spad_data = gctx->vtcm_src1 + (ith * gctx->vtcm_src1_size_per_thread); \
759 uint8_t * restrict dst_spad_data = gctx->vtcm_dst + (ith * gctx->vtcm_dst_size_per_thread); \
760 \
761 size_t src0_spad_half_size = gctx->src0_spad_half_size; \
762 size_t src1_spad_half_size = gctx->src1_spad_half_size; \
763 size_t dst_spad_half_size = gctx->dst_spad_half_size; \
764 \
765 const int BLOCK = gctx->block; \
766 \
767 dma_queue * dma_q = gctx->octx->ctx->dma[ith]; \
768 glu_compute_##SUFFIX##_fn_t compute = (glu_compute_##SUFFIX##_fn_t) gctx->compute; \
769 \
770 for (uint32_t ir = src0_start_row, spad_idx = 0; ir < src0_end_row && spad_idx < 2; ir += BLOCK, spad_idx++) { \
771 const uint32_t block_size = MIN(BLOCK, src0_end_row - ir); \
772 \
773 /* Dummy DMA transation for sequencing (interleaving dst,src,dst,...) */ \
774 dma_queue_push(dma_q, \
775 dma_make_data(data_dst, dst_spad_data + (spad_idx * dst_spad_half_size)), \
776 dst_row_size, dst_row_size_aligned, dst_row_size, 0); \
777 \
778 dma_queue_push(dma_q, \
779 dma_make_data(src0_spad_data + (spad_idx * src0_spad_half_size), data_src0 + (ir * src0_row_stride)), \
780 src0_row_size_aligned, src0_row_stride, src0_row_size, block_size); \
781 \
782 dma_queue_push(dma_q, \
783 dma_make_data(src1_spad_data + (spad_idx * src1_spad_half_size), data_src1 + (ir * src1_row_stride)), \
784 src1_row_size_aligned, src1_row_stride, src1_row_size, block_size); \
785 } \
786 \
787 for (uint32_t ir = src0_start_row; ir < src0_end_row; ir += BLOCK) { \
788 const uint32_t block_size = MIN(BLOCK, src0_end_row - ir); \
789 \
790 TYPE * dst_spad = (TYPE *) dma_queue_pop(dma_q).src; \
791 TYPE * src0_spad = (TYPE *) dma_queue_pop(dma_q).dst; \
792 TYPE * src1_spad = (TYPE *) dma_queue_pop(dma_q).dst; \
793 \
794 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, ir); \
795 compute(src0_spad, src1_spad, dst_spad, block_size, gctx); \
796 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, ir); \
797 \
798 dma_queue_push(dma_q, dma_make_data(data_dst + (ir * dst_row_size), dst_spad), \
799 dst_row_size, dst_row_size_aligned, dst_row_size, block_size); \
800 \
801 /* prefetch N+2 loop iteration if any */ \
802 const uint32_t pref_block = (ir + BLOCK * 2); \
803 if (pref_block < src0_end_row) { \
804 const uint32_t pref_block_size = MIN(BLOCK, src0_end_row - pref_block); \
805 dma_queue_push(dma_q, dma_make_data(src0_spad, data_src0 + (pref_block * src0_row_stride)), \
806 src0_row_size_aligned, src0_row_stride, src0_row_size, pref_block_size); \
807 dma_queue_push(dma_q, dma_make_data(src1_spad, data_src1 + (pref_block * src1_row_stride)), \
808 src1_row_size_aligned, src1_row_stride, src1_row_size, pref_block_size); \
809 } \
810 } \
811 \
812 dma_queue_flush(dma_q); \
813 }
814
815DEFINE_GLU_PER_THREAD(float, f32)
816DEFINE_GLU_PER_THREAD(_Float16, f16)
817
818static int execute_op_glu_f32(struct htp_ops_context * octx) {
819 const struct htp_tensor * src0 = octx->src[0];
820 const struct htp_tensor * src1 = octx->src[1];
821 const struct htp_tensor * dst = octx->dst;
822
823 glu_compute_f32_fn_t compute_fn = NULL;
824 const char * op_type = NULL;
825
826 switch (octx->op) {
827 case HTP_OP_GLU_SWIGLU:
828 compute_fn = swiglu_f32;
829 op_type = "swiglu-f32";
830 break;
831
832 case HTP_OP_GLU_SWIGLU_OAI:
833 compute_fn = swiglu_oai_f32;
834 op_type = "swiglu-oai-f32";
835 break;
836
837 case HTP_OP_GLU_SWIGLU_CLAMP:
838 compute_fn = swiglu_clamp_f32;
839 op_type = "swiglu-clamp-f32";
840 break;
841
842 case HTP_OP_GLU_GEGLU:
843 compute_fn = geglu_f32;
844 op_type = "geglu-f32";
845 break;
846
847 case HTP_OP_GLU_GEGLU_QUICK:
848 compute_fn = geglu_quick_f32;
849 op_type = "geglu-quick-f32";
850 break;
851
852 case HTP_OP_GLU_GEGLU_ERF:
853 compute_fn = geglu_erf_f32;
854 op_type = "geglu-erf-f32";
855 break;
856
857 case HTP_OP_GLU_REGLU:
858 compute_fn = reglu_f32;
859 op_type = "reglu-f32";
860 break;
861
862 case HTP_OP_GLU_SIGMOID:
863 compute_fn = sigglu_f32;
864 op_type = "sigglu-f32";
865 break;
866
867 default:
868 FARF(ERROR, "Unsupported GLU Op %u\n", octx->op);
869 return HTP_STATUS_NO_SUPPORT;
870 }
871
872 const struct htp_glu_kernel_params * kparams = (const struct htp_glu_kernel_params *) octx->kernel_params;
873
874 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) {
875 return HTP_STATUS_INVAL_PARAMS;
876 }
877
878 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3];
879 const size_t dst_row_size = dst->ne[0] * SIZEOF_FP32;
880
881 uint32_t row_start = 0;
882 uint32_t nrows = src0_nrows;
883
884 if (octx->ctx->mdev.count > 1) {
885 uint32_t rows_per_chunk = 0;
886 htp_tensor_mdev_rows_per_chunk(dst, sizeof(float), (uint32_t) dst_row_size, &rows_per_chunk);
887 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
888 row_start = range.start;
889 nrows = range.count;
890 }
891
892 if (nrows == 0) {
893 return HTP_STATUS_OK;
894 }
895
896 const uint32_t n_threads = octx->n_threads;
897
898 // row_size = bytes of useful data per row (what the kernel touches / what DMA copies).
899 // row_stride = bytes between successive rows in DDR (may exceed row_size for non-contig src).
900 const size_t nc_bytes = dst_row_size;
901 const size_t src0_row_size = nc_bytes;
902 const size_t src1_row_size = nc_bytes;
903 const size_t src0_row_stride = src0->nb[1];
904 const size_t src1_row_stride = src1 ? src1->nb[1] : src0->nb[1];
905
906 struct htp_glu_vtcm_layout L;
907 htp_glu_vtcm_layout_build(&L, dst_row_size, n_threads, kparams->vtcm_row_per_thread);
908
909 const size_t src0_row_size_aligned = L.row_size_aligned;
910 const size_t src1_row_size_aligned = L.row_size_aligned;
911 const size_t dst_row_size_aligned = L.row_size_aligned;
912
913 // Make sure the reserved vtcm size is sufficient
914 if (kparams->vtcm_row_per_thread == 0 || octx->ctx->vtcm_size < kparams->vtcm_size) {
915 FARF(ERROR, "glu-%s : current VTCM reservation %zu is too small for even 1 row per thread, needed at least %zu\n", op_type, octx->ctx->vtcm_size,
916 (src0_row_size_aligned + src1_row_size_aligned + dst_row_size_aligned) * n_threads);
917 return HTP_STATUS_VTCM_TOO_SMALL;
918 }
919
920 if (src1) {
921 FARF(HIGH, "%s: %ux%ux%ux%u x %ux%ux%ux%u -> %ux%ux%ux%u : src0-vtcm-size %zu src1-vtcm-size %zu dst-vtcm-size %zu\n",
922 op_type, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], src1->ne[0], src1->ne[1], src1->ne[2],
923 src1->ne[3], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], L.src0_bytes_per_thread * n_threads,
924 L.src1_bytes_per_thread * n_threads, L.dst_bytes_per_thread * n_threads);
925 } else {
926 FARF(HIGH, "%s: %ux%ux%ux%u -> %ux%ux%ux%u : src0-vtcm-size %zu src1-vtcm-size %zu dst-vtcm-size %zu\n", op_type,
927 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
928 L.src0_bytes_per_thread * n_threads, L.src1_bytes_per_thread * n_threads, L.dst_bytes_per_thread * n_threads);
929 }
930
931 // Prepare context
932 struct htp_glu_context gctx;
933 gctx.octx = octx;
934 gctx.compute = (void *) compute_fn;
935 gctx.op_str = op_type;
936
937 gctx.src0_nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div);
938
939 gctx.src0_row_size = src0_row_size;
940 gctx.src1_row_size = src1_row_size;
941 gctx.dst_row_size = dst_row_size;
942
943 gctx.src0_row_size_aligned = src0_row_size_aligned;
944 gctx.src1_row_size_aligned = src1_row_size_aligned;
945 gctx.dst_row_size_aligned = dst_row_size_aligned;
946
947 gctx.src0_row_stride = src0_row_stride;
948 gctx.src1_row_stride = src1_row_stride;
949
950 uint8_t * const base = (uint8_t *) octx->ctx->vtcm_base;
951 gctx.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src0);
952 gctx.vtcm_src1 = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src1);
953 gctx.vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, base, L.off_dst);
954
955 gctx.vtcm_src0_size_per_thread = (uint32_t) L.src0_bytes_per_thread;
956 gctx.vtcm_src1_size_per_thread = (uint32_t) L.src1_bytes_per_thread;
957 gctx.vtcm_dst_size_per_thread = (uint32_t) L.dst_bytes_per_thread;
958
959 gctx.src0_spad_half_size = gctx.vtcm_src0_size_per_thread >> 1;
960 gctx.src1_spad_half_size = gctx.vtcm_src1_size_per_thread >> 1;
961 gctx.dst_spad_half_size = gctx.vtcm_dst_size_per_thread >> 1;
962
963 gctx.block = L.block;
964 gctx.src0_nrows = nrows;
965 gctx.row_start = row_start;
966
967 gctx.nc = dst->ne[0];
968
969 // Addresses and GLU logic
970 dma_addr_t data_src0 = src0->data;
971 dma_addr_t data_src1 = src1 ? src1->data : 0;
972
973 if (!src1) {
974 const int32_t swapped = octx->op_params[1];
975 data_src1 = data_src0;
976 gctx.src1_row_size = gctx.src0_row_size;
977
978 size_t nc_in_bytes = gctx.nc * SIZEOF_FP32;
979 if (swapped) {
980 data_src0 += nc_in_bytes;
981 } else {
982 data_src1 += nc_in_bytes;
983 }
984 }
985
986 gctx.data_src0 = data_src0;
987 gctx.data_src1 = data_src1;
988 gctx.data_dst = dst->data;
989
990 work_queue_run(octx->ctx->work_queue, (worker_callback_t)glu_f32_per_thread, &gctx, n_threads);
991 return HTP_STATUS_OK;
992}
993
994static int execute_op_glu_f16(struct htp_ops_context * octx) {
995 const struct htp_tensor * src0 = octx->src[0];
996 const struct htp_tensor * src1 = octx->src[1];
997 const struct htp_tensor * dst = octx->dst;
998
999 glu_compute_f16_fn_t compute_fn = NULL;
1000 const char * op_type = NULL;
1001
1002 switch (octx->op) {
1003 case HTP_OP_GLU_SWIGLU:
1004 compute_fn = swiglu_f16;
1005 op_type = "swiglu-f16";
1006 break;
1007
1008 case HTP_OP_GLU_SWIGLU_OAI:
1009 compute_fn = swiglu_oai_f16;
1010 op_type = "swiglu-oai-f16";
1011 break;
1012
1013 case HTP_OP_GLU_SWIGLU_CLAMP:
1014 compute_fn = swiglu_clamp_f16;
1015 op_type = "swiglu-clamp-f16";
1016 break;
1017
1018 case HTP_OP_GLU_GEGLU:
1019 compute_fn = geglu_f16;
1020 op_type = "geglu-f16";
1021 break;
1022
1023 case HTP_OP_GLU_REGLU:
1024 compute_fn = reglu_f16;
1025 op_type = "reglu-f16";
1026 break;
1027
1028 case HTP_OP_GLU_SIGMOID:
1029 compute_fn = sigglu_f16;
1030 op_type = "sigglu-f16";
1031 break;
1032
1033 default:
1034 FARF(ERROR, "Unsupported GLU Op %u\n", octx->op);
1035 return HTP_STATUS_NO_SUPPORT;
1036 }
1037
1038 const struct htp_glu_kernel_params * kparams = (const struct htp_glu_kernel_params *) octx->kernel_params;
1039
1040 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) {
1041 return HTP_STATUS_INVAL_PARAMS;
1042 }
1043
1044 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3];
1045 const size_t dst_row_size = dst->ne[0] * SIZEOF_FP16;
1046
1047 uint32_t row_start = 0;
1048 uint32_t nrows = src0_nrows;
1049
1050 if (octx->ctx->mdev.count > 1) {
1051 uint32_t rows_per_chunk = 0;
1052 htp_tensor_mdev_rows_per_chunk(dst, sizeof(_Float16), (uint32_t) dst_row_size, &rows_per_chunk);
1053 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
1054 row_start = range.start;
1055 nrows = range.count;
1056 }
1057
1058 if (nrows == 0) {
1059 return HTP_STATUS_OK;
1060 }
1061
1062 const uint32_t n_threads = octx->n_threads;
1063
1064 // row_size = bytes of useful data per row (what the kernel touches / what DMA copies).
1065 // row_stride = bytes between successive rows in DDR (may exceed row_size for non-contig src).
1066 const size_t nc_bytes = dst_row_size;
1067 const size_t src0_row_size = nc_bytes;
1068 const size_t src1_row_size = nc_bytes;
1069 const size_t src0_row_stride = src0->nb[1];
1070 const size_t src1_row_stride = src1 ? src1->nb[1] : src0->nb[1];
1071
1072 struct htp_glu_vtcm_layout L;
1073 htp_glu_vtcm_layout_build(&L, dst_row_size, n_threads, kparams->vtcm_row_per_thread);
1074
1075 const size_t src0_row_size_aligned = L.row_size_aligned;
1076 const size_t src1_row_size_aligned = L.row_size_aligned;
1077 const size_t dst_row_size_aligned = L.row_size_aligned;
1078
1079 // Make sure the reserved vtcm size is sufficient
1080 if (kparams->vtcm_row_per_thread == 0 || octx->ctx->vtcm_size < kparams->vtcm_size) {
1081 FARF(ERROR, "glu-%s : current VTCM reservation %zu is too small for even 1 row per thread, needed at least %zu\n", op_type, octx->ctx->vtcm_size,
1082 (src0_row_size_aligned + src1_row_size_aligned + dst_row_size_aligned) * n_threads);
1083 return HTP_STATUS_VTCM_TOO_SMALL;
1084 }
1085
1086 if (src1) {
1087 FARF(HIGH, "%s: %ux%ux%ux%u x %ux%ux%ux%u -> %ux%ux%ux%u : src0-vtcm-size %zu src1-vtcm-size %zu dst-vtcm-size %zu\n",
1088 op_type, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], src1->ne[0], src1->ne[1], src1->ne[2],
1089 src1->ne[3], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], L.src0_bytes_per_thread * n_threads,
1090 L.src1_bytes_per_thread * n_threads, L.dst_bytes_per_thread * n_threads);
1091 } else {
1092 FARF(HIGH, "%s: %ux%ux%ux%u -> %ux%ux%ux%u : src0-vtcm-size %zu src1-vtcm-size %zu dst-vtcm-size %zu\n", op_type,
1093 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
1094 L.src0_bytes_per_thread * n_threads, L.src1_bytes_per_thread * n_threads, L.dst_bytes_per_thread * n_threads);
1095 }
1096
1097 // Prepare context
1098 struct htp_glu_context gctx;
1099 gctx.octx = octx;
1100 gctx.compute = (void *) compute_fn;
1101 gctx.op_str = op_type;
1102
1103 gctx.src0_nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div);
1104
1105 gctx.src0_row_size = src0_row_size;
1106 gctx.src1_row_size = src1_row_size;
1107 gctx.dst_row_size = dst_row_size;
1108
1109 gctx.src0_row_size_aligned = src0_row_size_aligned;
1110 gctx.src1_row_size_aligned = src1_row_size_aligned;
1111 gctx.dst_row_size_aligned = dst_row_size_aligned;
1112
1113 gctx.src0_row_stride = src0_row_stride;
1114 gctx.src1_row_stride = src1_row_stride;
1115
1116 uint8_t * const base = (uint8_t *) octx->ctx->vtcm_base;
1117 gctx.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src0);
1118 gctx.vtcm_src1 = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src1);
1119 gctx.vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, base, L.off_dst);
1120
1121 gctx.vtcm_src0_size_per_thread = (uint32_t) L.src0_bytes_per_thread;
1122 gctx.vtcm_src1_size_per_thread = (uint32_t) L.src1_bytes_per_thread;
1123 gctx.vtcm_dst_size_per_thread = (uint32_t) L.dst_bytes_per_thread;
1124
1125 gctx.src0_spad_half_size = gctx.vtcm_src0_size_per_thread >> 1;
1126 gctx.src1_spad_half_size = gctx.vtcm_src1_size_per_thread >> 1;
1127 gctx.dst_spad_half_size = gctx.vtcm_dst_size_per_thread >> 1;
1128
1129 gctx.block = L.block;
1130 gctx.src0_nrows = nrows;
1131 gctx.row_start = row_start;
1132
1133 gctx.nc = dst->ne[0];
1134
1135 // Addresses and GLU logic
1136 dma_addr_t data_src0 = src0->data;
1137 dma_addr_t data_src1 = src1 ? src1->data : 0;
1138
1139 if (!src1) {
1140 const int32_t swapped = octx->op_params[1];
1141 data_src1 = data_src0;
1142 gctx.src1_row_size = gctx.src0_row_size;
1143
1144 size_t nc_in_bytes = gctx.nc * SIZEOF_FP16;
1145 if (swapped) {
1146 data_src0 += nc_in_bytes;
1147 } else {
1148 data_src1 += nc_in_bytes;
1149 }
1150 }
1151
1152 gctx.data_src0 = data_src0;
1153 gctx.data_src1 = data_src1;
1154 gctx.data_dst = dst->data;
1155
1156 work_queue_run(octx->ctx->work_queue, (worker_callback_t)glu_f16_per_thread, &gctx, n_threads);
1157 return HTP_STATUS_OK;
1158}
1159
1160
1161int op_glu(struct htp_ops_context * octx) {
1162 switch (octx->src[0]->type) {
1163 case HTP_TYPE_F32:
1164 return execute_op_glu_f32(octx);
1165
1166 case HTP_TYPE_F16:
1167 return execute_op_glu_f16(octx);
1168
1169 default:
1170 return HTP_STATUS_NO_SUPPORT;
1171 }
1172}