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 / conv2d-ops.c

3177 lines · 169752 bytes Raw Blame History
1#pragma clang diagnostic ignored "-Wunused-variable"
2#pragma clang diagnostic ignored "-Wunused-function"
3
4#include <HAP_farf.h>
5#include <stdint.h>
6#include <string.h>
7
8#define GGML_COMMON_DECL_C
9#include "ggml-common.h"
10#include "conv2d-ops.h"
11#include "dma-copy.h"
12#include "dma-queue.h"
13#include "hex-common.h"
14#include "hex-fastdiv.h"
15#include "hex-profile.h"
16#include "htp-ctx.h"
17#include "htp-ops.h"
18#include "htp-tensor.h"
19#include "htp-vtcm.h"
20#include "hvx-base.h"
21#include "hvx-copy.h"
22#include "hvx-reduce.h"
23#include "hmx-queue.h"
24#include "hmx-utils.h"
25
26// fp16 elements between the HMX scales of two row tiles
27#define CONV2D_HMX_SCALES_N (HTP_CONV2D_HMX_SCALES_SIZE / sizeof(__fp16))
28
29struct htp_conv2d_hmx_task {
30 __fp16 * output;
31 const __fp16 * kernel_tiles;
32 const __fp16 * patch_tiles;
33 const __fp16 * scales;
34 uint32_t n_row_tiles;
35 uint32_t n_col_tiles;
36 uint32_t n_dot_tiles;
37};
38
39// hvx_vec_f32_to_f16_shuff() on v81 turns an exact 0 into 2^-24, which shows up as a nonzero conv output where the result is 0
40static inline HVX_Vector conv2d_f32_to_f16_shuff_z(HVX_Vector v0, HVX_Vector v1, HVX_Vector zero) {
41 const HVX_Vector q0 = Q6_Vqf32_vadd_VsfVsf(v0, zero);
42 const HVX_Vector q1 = Q6_Vqf32_vadd_VsfVsf(v1, zero);
43 return Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q1, q0));
44}
45
46static inline HVX_Vector conv2d_f32_to_f16_shuff(HVX_Vector v0, HVX_Vector v1) {
47 return conv2d_f32_to_f16_shuff_z(v0, v1, Q6_V_vzero());
48}
49
50static inline HVX_Vector conv2d_f32_to_f16(HVX_Vector v0, HVX_Vector v1) {
51 return Q6_Vh_vdeal_Vh(conv2d_f32_to_f16_shuff(v0, v1));
52}
53
54// v if keep, else 0. Loops with transposes use this for padding rows: a hoisted zero vector would spill.
55static inline HVX_Vector conv2d_keep(HVX_Vector v, bool keep) {
56 return Q6_V_vand_VV(v, Q6_V_vsplat_R(keep ? ~0u : 0));
57}
58
59// HMX output scale of one row tile: 2^-e with 2^e >= K * max|w|, so the fp16 result |y| * 2^-e <= max|x| does not overflow
60static void conv2d_hmx_rt_scale(__fp16 * scales, const __fp16 * w, uint32_t n_tiles, uint32_t K) {
61 const HVX_Vector * v = (const HVX_Vector *) w;
62 const uint32_t n = n_tiles * (HMX_FP16_TILE_N_ELMS / 64);
63
64 // |w| fp16 bits compare like unsigned ints
65 HVX_Vector m = Q6_V_vzero();
66 for (uint32_t i = 0; i < n; i++) {
67 m = Q6_Vuh_vmax_VuhVuh(m, hvx_vec_abs_f16(v[i]));
68 }
69 m = hvx_vec_reduce_max_f16(m);
70
71 // every lane holds the max, convert on HVX to avoid a scalar fp16 promotion
72 union { uint32_t u; float f; } wmax = { .u = Q6_R_vextract_VR(Q6_V_lo_W(hvx_vec_f16_to_f32(m)), 0) };
73 union { float f; uint32_t u; } bound = { .f = (float) K * wmax.f };
74
75 int32_t e = (int32_t) (bound.u >> 23) - 127 + ((bound.u & 0x7fffff) != 0);
76 e = MAX(0, MIN(e, 24));
77
78 // 2^-e in fp16, subnormal below 2^-14
79 const uint32_t s16 = e <= 14 ? (uint32_t) (15 - e) << 10 : 1u << (24 - e);
80 hmx_init_column_scales(scales, Q6_V_vsplat_R(s16));
81}
82
83// f32 splat of 1 / scale of row tile rt. The scale is 2^-e, so decode e from its fp16 bits (subnormal for e > 14).
84static inline HVX_Vector conv2d_hmx_rt_restore(const __fp16 * scales, uint32_t rt) {
85 const uint32_t s16 = Q6_R_vextract_VR(*(const HVX_Vector *) (scales + (size_t) rt * CONV2D_HMX_SCALES_N), 0) & 0xffff;
86 const uint32_t e = s16 >= 0x400 ? 15 - (s16 >> 10) : 24 - (31 - __builtin_clz(s16));
87 return Q6_V_vsplat_R((127 + e) << 23);
88}
89
90// Output rows [*row_base, *row_base + *n_rows) of N * OH for this device.
91// The output is written by DMA only, so device ranges need no cache line alignment.
92static void conv2d_mdev_rows(const struct htp_ops_context * octx, uint32_t * row_base, uint32_t * n_rows) {
93 *row_base = 0;
94 *n_rows = octx->dst->ne[3] * octx->dst->ne[1];
95 if (octx->ctx->mdev.count > 1) {
96 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(
97 *n_rows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
98 *row_base = range.start;
99 *n_rows = range.count;
100 }
101}
102
103static void conv2d_hmx_worker(void * data) {
104 struct htp_conv2d_hmx_task * task = (struct htp_conv2d_hmx_task *) data;
105
106 const size_t dot_stride = task->n_dot_tiles * HMX_FP16_TILE_N_ELMS;
107
108 for (uint32_t r = 0; r < task->n_row_tiles; ++r) {
109 asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) (task->scales + r * CONV2D_HMX_SCALES_N)));
110
111 const __fp16 * row_base = task->kernel_tiles + r * dot_stride;
112 const __fp16 * col_base = task->patch_tiles;
113 __fp16 * out_tile = task->output + r * task->n_col_tiles * HMX_FP16_TILE_N_ELMS;
114
115 for (uint32_t c = 0; c < task->n_col_tiles; ++c) {
116 const __fp16 * row_tiles = row_base;
117 const __fp16 * col_tiles = col_base;
118 asm volatile(HMX_CLRACC_F16());
119
120 uint32_t rem = task->n_dot_tiles;
121 while (rem > 0) {
122 const uint32_t n_tiles = MIN(rem, 32);
123 const uint32_t range = 2048u * n_tiles - 1;
124 asm volatile(HMX_LOAD_MPY_DEEP_F16("%1", "%2", "%0") : : "r"(range), "r"(row_tiles), "r"(col_tiles));
125 row_tiles += n_tiles * HMX_FP16_TILE_N_ELMS;
126 col_tiles += n_tiles * HMX_FP16_TILE_N_ELMS;
127 rem -= n_tiles;
128 }
129
130 asm volatile(HMX_STORE_AFTER_F16("%0", "%1") : : "r"(out_tile), "r"(0) : "memory");
131 col_base += dot_stride;
132 out_tile += HMX_FP16_TILE_N_ELMS;
133 }
134 }
135}
136
137struct htp_conv2d_hmx_context {
138 struct htp_ops_context * octx;
139 const struct htp_conv2d_kernel_params * kp;
140 struct htp_conv2d_vtcm_layout layout;
141 __fp16 * scales; // [rt] HMX scales
142 __fp16 * w_tiles;
143 __fp16 * act[2];
144 __fp16 * out[2];
145 uint8_t * thr_base;
146 uint32_t t_base;
147 uint32_t t_end;
148 uint32_t rt0;
149 uint32_t n_rt;
150 int32_t pack_chunk;
151 int32_t unpack_chunk;
152 uint32_t pack_tpt;
153 uint32_t unpack_tpt;
154};
155
156// col tiles [t0, t1) of chunk c for thread ith, returns first col tile of the chunk
157static inline uint32_t conv2d_hmx_chunk_range(const struct htp_conv2d_hmx_context * hc, uint32_t c, uint32_t tpt,
158 uint32_t ith, uint32_t * t0, uint32_t * t1) {
159 const uint32_t cs = hc->t_base + c * hc->kp->ct_chunk;
160 const uint32_t ce = MIN(cs + hc->kp->ct_chunk, hc->t_end);
161 *t0 = MIN(cs + ith * tpt, ce);
162 *t1 = MIN(*t0 + tpt, ce);
163 return cs;
164}
165
166static void conv2d_hmx_wpack_thread(unsigned int nth, unsigned int ith, void * data) {
167 const struct htp_conv2d_hmx_context * hc = (const struct htp_conv2d_hmx_context *) data;
168 struct htp_ops_context * octx = hc->octx;
169 const struct htp_conv2d_kernel_params * kp = hc->kp;
170 const struct htp_tensor * kernel = octx->src[0];
171
172 const uint32_t Cout = kernel->ne[3];
173 const uint32_t K = kp->tiled.k_size;
174 const uint32_t k_pad = kp->tiled.n_k_tiles * HMX_FP16_TILE_N_COLS;
175 const uint32_t WK = kp->tiled.wk_chunk;
176 const bool is_f16 = kernel->type == HTP_TYPE_F16;
177 const uint32_t es = is_f16 ? sizeof(__fp16) : sizeof(float);
178
179 dma_queue * dmaq = octx->ctx->dma[ith];
180 uint8_t * thr = hc->thr_base + (size_t) ith * kp->thr_size;
181 const size_t w_buf_size = (size_t) WK * 32 * sizeof(__fp16);
182 const size_t w_raw_size = (size_t) WK * 32 * sizeof(float);
183 const size_t w_step = w_buf_size + w_raw_size;
184
185 __fp16 * wbuf[2] = { (__fp16 *) thr, (__fp16 *) (thr + w_step) };
186 float * wraw[2] = { (float *) ((uint8_t *) wbuf[0] + w_buf_size),
187 (float *) ((uint8_t *) wbuf[1] + w_buf_size) };
188 uint8_t * wdma[2] = { is_f16 ? (uint8_t *) wbuf[0] : (uint8_t *) wraw[0],
189 is_f16 ? (uint8_t *) wbuf[1] : (uint8_t *) wraw[1] };
190
191 const HVX_Vector zero = Q6_V_vzero();
192
193 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
194
195 for (uint32_t rt = ith; rt < hc->n_rt; rt += nth) {
196 const uint32_t co0 = (hc->rt0 + rt) * 32;
197 const uint32_t nco = MIN(32, Cout - co0);
198
199 uint32_t bi = 0;
200 for (uint32_t i = 0; i < 2 && i * WK < k_pad; i++) {
201 const uint32_t kc = i * WK;
202 const uint32_t kv = MIN(WK, K - kc);
203 dma_queue_push(dmaq, dma_make_data(wdma[i], kernel->data + (size_t) co0 * kernel->nb[3] + (size_t) kc * es),
204 WK * es, kernel->nb[3], kv * es, nco);
205 }
206
207 for (uint32_t kc = 0; kc < k_pad; kc += WK) {
208 const uint32_t kv = MIN(WK, K - kc);
209 const uint32_t hd = MIN(WK, k_pad - kc);
210
211 dma_queue_pop(dmaq);
212
213 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) kc);
214
215 for (uint32_t r = 0; r < 32; r++) {
216 HVX_Vector * dv = (HVX_Vector *) (wbuf[bi] + (size_t) r * WK);
217 const HVX_Vector * sv = (const HVX_Vector *) (wraw[bi] + (size_t) r * WK);
218 for (uint32_t j = 0; j < hd; j += 64) {
219 HVX_Vector v = zero;
220 if (r < nco && j < kv) {
221 v = is_f16 ? dv[j / 64] : conv2d_f32_to_f16(sv[j / 32], sv[j / 32 + 1]);
222 if (kv - j < 64) {
223 v = Q6_V_vmux_QVV(Q6_Q_vsetq_R((kv - j) * sizeof(__fp16)), v, zero);
224 }
225 }
226 dv[j / 64] = v;
227 }
228 }
229
230 __fp16 * tiles = hc->w_tiles + ((size_t) rt * kp->tiled.n_k_tiles + kc / 32) * HMX_FP16_TILE_N_ELMS;
231 hmx_interleave_cols_to_tiles(tiles, wbuf[bi], 32, hd, WK, 1, 0, 32);
232 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) kc);
233
234 const uint32_t next_kc = kc + 2 * WK;
235 if (next_kc < k_pad) {
236 const uint32_t next_kv = MIN(WK, K - next_kc);
237 dma_queue_push(dmaq, dma_make_data(wdma[bi], kernel->data + (size_t) co0 * kernel->nb[3] + (size_t) next_kc * es),
238 WK * es, kernel->nb[3], next_kv * es, nco);
239 }
240 bi ^= 1;
241 }
242 }
243}
244
245// Stage Cin*KH input rows for G col tiles: stage[(cin*KH + kh)*SR + j] = x[n][cin][ih(kh)][ow0 - p0 + j], zero outside
246static void conv2d_hmx_stage(const struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp,
247 dma_queue * dmaq, float * stage, uint32_t n, uint32_t oh, uint32_t ow0, uint32_t G) {
248 const struct htp_tensor * kernel = octx->src[0];
249 const struct htp_tensor * input = octx->src[1];
250
251 const uint32_t KW = kernel->ne[0];
252 const uint32_t KH = kernel->ne[1];
253 const uint32_t Cin = kernel->ne[2];
254 const int32_t IW = input->ne[0];
255 const int32_t IH = input->ne[1];
256 const int32_t s1 = octx->op_params[1];
257 const int32_t p0 = octx->op_params[2];
258 const int32_t p1 = octx->op_params[3];
259 const int32_t d0 = octx->op_params[4];
260 const int32_t d1 = octx->op_params[5];
261 const uint32_t SR = kp->tiled.stage_row;
262
263 const int32_t Wb = 32 * G + (KW - 1) * d0;
264 const int32_t iw0 = (int32_t) ow0 - p0;
265 const int32_t lo = MAX(0, -iw0);
266 const int32_t hi = MIN(Wb, IW - iw0);
267
268 for (uint32_t kh = 0; kh < KH; kh++) {
269 const int32_t ih = (int32_t) oh * s1 + (int32_t) kh * d1 - p1;
270 float * rows = stage + (size_t) kh * SR;
271
272 if (ih < 0 || ih >= IH || hi <= lo) {
273 for (uint32_t c = 0; c < Cin; c++) {
274 hvx_splat_f32_u(rows + (size_t) c * KH * SR, 0.0f, Wb);
275 }
276 dma_queue_push(dmaq, dma_make_data(rows, 0), 0, 0, 0, 0);
277 continue;
278 }
279
280 const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1] + (size_t) (iw0 + lo) * sizeof(float);
281 dma_queue_push(dmaq, dma_make_data(rows + lo, src), (size_t) KH * SR * sizeof(float), input->nb[2],
282 (size_t) (hi - lo) * sizeof(float), Cin);
283
284 if (lo > 0 || hi < Wb) {
285 for (uint32_t c = 0; c < Cin; c++) {
286 float * row = rows + (size_t) c * KH * SR;
287 if (lo > 0) {
288 hvx_splat_f32_u(row, 0.0f, lo);
289 }
290 if (hi < Wb) {
291 hvx_splat_f32_u(row + hi, 0.0f, Wb - hi);
292 }
293 }
294 }
295 }
296}
297
298// One weight-side tile column: vector i holds k rows 2i and 2i+1 interleaved over 32 pixels
299static inline void conv2d_hmx_pack_tile(__fp16 * restrict dst, const float * restrict src, uint32_t K, uint32_t KW,
300 uint32_t d0, uint32_t SR, uint32_t n_vec) {
301 HVX_Vector * restrict out = (HVX_Vector *) dst;
302 const HVX_Vector zero = Q6_V_vzero();
303 const uint32_t wrap = SR - KW * d0;
304
305 const float * p = src;
306 uint32_t kw = 0;
307 uint32_t i = 0;
308
309 for (; i < K / 2; i++) {
310 const HVX_Vector a = hvx_vmemu(p);
311 p += d0;
312 if (++kw == KW) {
313 kw = 0;
314 p += wrap;
315 }
316 const HVX_Vector b = hvx_vmemu(p);
317 p += d0;
318 if (++kw == KW) {
319 kw = 0;
320 p += wrap;
321 }
322 out[i] = conv2d_f32_to_f16_shuff(a, b);
323 }
324 if (K & 1) {
325 out[i++] = conv2d_f32_to_f16_shuff(hvx_vmemu(p), zero);
326 }
327 for (; i < n_vec; i++) {
328 out[i] = zero;
329 }
330}
331
332// CWHN: stage[kh*SR + j*Cin + cin] = x[n][ih(kh)][ow0 - p0 + j][cin], zero outside
333static void conv2d_hmx_stage_cwhn(const struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp,
334 dma_queue * dmaq, float * stage, uint32_t n, uint32_t oh, uint32_t ow0, uint32_t G) {
335 const struct htp_tensor * kernel = octx->src[0];
336 const struct htp_tensor * input = octx->src[1];
337
338 const uint32_t KW = kernel->ne[0];
339 const uint32_t KH = kernel->ne[1];
340 const uint32_t Cin = kernel->ne[2];
341 const int32_t IW = input->ne[0];
342 const int32_t IH = input->ne[1];
343 const int32_t s1 = octx->op_params[1];
344 const int32_t p0 = octx->op_params[2];
345 const int32_t p1 = octx->op_params[3];
346 const int32_t d0 = octx->op_params[4];
347 const int32_t d1 = octx->op_params[5];
348 const uint32_t SR = kp->tiled.stage_row;
349
350 const int32_t Wb = 32 * G + (KW - 1) * d0;
351 const int32_t iw0 = (int32_t) ow0 - p0;
352 const int32_t lo = MAX(0, -iw0);
353 const int32_t hi = MIN(Wb, IW - iw0);
354
355 for (uint32_t kh = 0; kh < KH; kh++) {
356 const int32_t ih = (int32_t) oh * s1 + (int32_t) kh * d1 - p1;
357 float * row = stage + (size_t) kh * SR;
358
359 if (ih < 0 || ih >= IH || hi <= lo) {
360 hvx_splat_f32_u(row, 0.0f, Wb * Cin);
361 dma_queue_push(dmaq, dma_make_data(row, 0), 0, 0, 0, 0);
362 continue;
363 }
364
365 const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1] + (size_t) (iw0 + lo) * input->nb[0];
366 const size_t size = (size_t) (hi - lo) * Cin * sizeof(float);
367 dma_queue_push(dmaq, dma_make_data(row + lo * Cin, src), size, size, size, 1);
368
369 if (lo > 0) {
370 hvx_splat_f32_u(row, 0.0f, lo * Cin);
371 }
372 if (hi < Wb) {
373 hvx_splat_f32_u(row + hi * Cin, 0.0f, (Wb - hi) * Cin);
374 }
375 }
376}
377
378static void conv2d_hmx_cwhn_f16(__fp16 * restrict dst, const float * restrict src, uint32_t KH, uint32_t SR, uint32_t n) {
379 for (uint32_t kh = 0; kh < KH; kh++) {
380 hvx_copy_f16_f32_aa((uint8_t *) (dst + (size_t) kh * SR), (const uint8_t *) (src + (size_t) kh * SR), n);
381 }
382}
383
384// Stage the segment of col tiles starting at t, returns its number of col tiles
385static uint32_t conv2d_hmx_issue_seg(const struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp,
386 dma_queue * dmaq, float * stage, uint32_t t, uint32_t t1) {
387 const uint32_t OH = octx->dst->ne[1];
388 const uint32_t n_owt = kp->tiled.n_ow_tiles;
389 const uint32_t row = fastdiv(t, &kp->tiled.div_ow_tiles);
390 const uint32_t owt = t - row * n_owt;
391 const uint32_t G = MIN(MIN(kp->tiled.seg_tiles, t1 - t), n_owt - owt);
392 const uint32_t n = fastdiv(row, &kp->tiled.div_oh);
393 const uint32_t oh = row - n * OH;
394
395 if (kp->cwhn) {
396 conv2d_hmx_stage_cwhn(octx, kp, dmaq, stage, n, oh, owt * 32, G);
397 } else {
398 conv2d_hmx_stage(octx, kp, dmaq, stage, n, oh, owt * 32, G);
399 }
400 return G;
401}
402
403static inline void conv2d_vshuff_w(HVX_Vector * a, HVX_Vector * b) {
404 const HVX_VectorPair p = Q6_W_vshuff_VVR(*b, *a, -4);
405 *a = Q6_V_lo_W(p);
406 *b = Q6_V_hi_W(p);
407}
408
409static inline void conv2d_transpose_finish(HVX_Vector * restrict scratch, HVX_Vector * restrict lo, HVX_Vector * restrict hi);
410
411// 32x32 word transpose: out[i] word j = in[j * in_stride] word i; out 0..15 -> lo, 16..31 -> hi
412static inline void conv2d_transpose_w32(const HVX_Vector * restrict in, uint32_t in_stride, HVX_Vector * restrict scratch,
413 HVX_Vector * restrict lo, HVX_Vector * restrict hi) {
414 for (uint32_t i = 0; i < 8; i++) {
415 HVX_Vector v0 = in[(i + 0) * in_stride];
416 HVX_Vector v1 = in[(i + 8) * in_stride];
417 HVX_Vector v2 = in[(i + 16) * in_stride];
418 HVX_Vector v3 = in[(i + 24) * in_stride];
419 conv2d_vshuff_w(&v0, &v2);
420 conv2d_vshuff_w(&v1, &v3);
421 conv2d_vshuff_w(&v0, &v1);
422 conv2d_vshuff_w(&v2, &v3);
423 scratch[i + 0] = v0;
424 scratch[i + 8] = v1;
425 scratch[i + 16] = v2;
426 scratch[i + 24] = v3;
427 }
428 conv2d_transpose_finish(scratch, lo, hi);
429}
430
431static inline void conv2d_transpose_finish(HVX_Vector * restrict scratch, HVX_Vector * restrict lo, HVX_Vector * restrict hi) {
432 for (uint32_t g = 0; g < 4; g++) {
433 HVX_Vector w0 = scratch[8 * g + 0];
434 HVX_Vector w1 = scratch[8 * g + 1];
435 HVX_Vector w2 = scratch[8 * g + 2];
436 HVX_Vector w3 = scratch[8 * g + 3];
437 HVX_Vector w4 = scratch[8 * g + 4];
438 HVX_Vector w5 = scratch[8 * g + 5];
439 HVX_Vector w6 = scratch[8 * g + 6];
440 HVX_Vector w7 = scratch[8 * g + 7];
441 conv2d_vshuff_w(&w0, &w4);
442 conv2d_vshuff_w(&w1, &w5);
443 conv2d_vshuff_w(&w2, &w6);
444 conv2d_vshuff_w(&w3, &w7);
445 conv2d_vshuff_w(&w0, &w2);
446 conv2d_vshuff_w(&w1, &w3);
447 conv2d_vshuff_w(&w4, &w6);
448 conv2d_vshuff_w(&w5, &w7);
449 conv2d_vshuff_w(&w0, &w1);
450 conv2d_vshuff_w(&w2, &w3);
451 conv2d_vshuff_w(&w4, &w5);
452 conv2d_vshuff_w(&w6, &w7);
453 HVX_Vector * o = (g < 2 ? lo : hi) + 8 * (g & 1);
454 o[0] = w0;
455 o[1] = w1;
456 o[2] = w2;
457 o[3] = w3;
458 o[4] = w4;
459 o[5] = w5;
460 o[6] = w6;
461 o[7] = w7;
462 }
463}
464
465// Same transpose, but pixel j is base + j * stride. Skips the vin store/reload.
466static inline void conv2d_transpose_strided(const __fp16 * restrict base, uint32_t stride, HVX_Vector * restrict scratch,
467 HVX_Vector * restrict lo, HVX_Vector * restrict hi) {
468 for (uint32_t i = 0; i < 8; i++) {
469 HVX_Vector v0 = hvx_vmemu(base + (i + 0) * stride);
470 HVX_Vector v1 = hvx_vmemu(base + (i + 8) * stride);
471 HVX_Vector v2 = hvx_vmemu(base + (i + 16) * stride);
472 HVX_Vector v3 = hvx_vmemu(base + (i + 24) * stride);
473 conv2d_vshuff_w(&v0, &v2);
474 conv2d_vshuff_w(&v1, &v3);
475 conv2d_vshuff_w(&v0, &v1);
476 conv2d_vshuff_w(&v2, &v3);
477 scratch[i + 0] = v0;
478 scratch[i + 8] = v1;
479 scratch[i + 16] = v2;
480 scratch[i + 24] = v3;
481 }
482 conv2d_transpose_finish(scratch, lo, hi);
483}
484
485// CWHN: k is n_groups groups of k_group contiguous values (per kh, or per (kh, kw) if d0 > 1), each padded to k_group_pad.
486// A full 64-wide block is one vector per pixel and is transposed in place. A partial block is two masked loads,
487// the second one rotated into the upper half.
488static void conv2d_hmx_pack_tile_cwhn(__fp16 * restrict dst, const __fp16 * restrict src, HVX_Vector * restrict vin,
489 HVX_Vector * restrict scratch, const struct htp_conv2d_kernel_params * kp,
490 uint32_t gpr, uint32_t Cin, uint32_t d0) {
491 const uint32_t k_pad = kp->tiled.n_k_tiles * HMX_FP16_TILE_N_COLS;
492 const uint32_t KG = kp->tiled.k_group;
493 const uint32_t KGp = kp->tiled.k_group_pad;
494 const uint32_t NG = kp->tiled.n_groups;
495 const uint32_t SR = kp->tiled.stage_row;
496
497 HVX_Vector * restrict out = (HVX_Vector *) dst;
498
499 uint32_t g = 0;
500 uint32_t gw = 0;
501 uint32_t r = 0;
502 size_t roff = 0;
503 size_t goff = 0;
504
505 for (uint32_t b = 0; b < k_pad / 64; b++) {
506 const __fp16 * pa[2];
507 uint32_t va[2];
508 for (uint32_t h = 0; h < 2; h++) {
509 va[h] = g < NG && r < KG ? MIN(32, KG - r) : 0;
510 pa[h] = g < NG ? src + goff + r : src;
511 r += 32;
512 if (r >= KGp) {
513 r = 0;
514 g++;
515 if (++gw == gpr) {
516 gw = 0;
517 roff += SR;
518 goff = roff;
519 } else {
520 goff += d0 * Cin;
521 }
522 }
523 }
524
525 if (va[0] == 32 && va[1] == 32 && pa[1] == pa[0] + 32) {
526 conv2d_transpose_strided(pa[0], Cin, scratch, out + 32 * b, out + 32 * b + 16);
527 continue;
528 }
529
530 const HVX_VectorPred q0 = Q6_Q_vsetq_R(va[0] * sizeof(__fp16));
531 const HVX_VectorPred q1 = Q6_Q_vsetq_R(va[1] * sizeof(__fp16));
532 for (uint32_t j = 0; j < 32; j++) {
533 const HVX_Vector v0 = Q6_V_vand_QV(q0, hvx_vmemu(pa[0] + j * Cin));
534 const HVX_Vector v1 = Q6_V_vand_QV(q1, hvx_vmemu(pa[1] + j * Cin));
535 vin[j] = Q6_V_vor_VV(v0, Q6_V_vror_VR(v1, 64));
536 }
537
538 conv2d_transpose_w32(vin, 1, scratch, out + 32 * b, out + 32 * b + 16);
539 }
540}
541
542struct wpack_kmajor_task {
543 uint32_t rt;
544 uint32_t g;
545 uint32_t kc;
546 uint32_t nco;
547 uint32_t nk;
548 uint32_t hk;
549};
550
551static inline void conv2d_hmx_wpack_kmajor_compute(const struct htp_conv2d_hmx_context * hc,
552 const uint8_t * wraw,
553 const struct wpack_kmajor_task * task,
554 HVX_Vector * vin,
555 HVX_Vector * scratch,
556 HVX_Vector * sink,
557 bool is_f16,
558 uint32_t es) {
559 const struct htp_conv2d_kernel_params * kp = hc->kp;
560 const uint32_t KGp = kp->tiled.k_group_pad;
561 const HVX_VectorPred qco = Q6_Q_vsetq2_R(task->nco * sizeof(__fp16));
562 for (uint32_t kt = 0; kt < task->hk / 32; kt++) {
563 for (uint32_t j = 0; j < 32; j++) {
564 const uint32_t k = kt * 32 + j;
565 const HVX_Vector * sv = (const HVX_Vector *) (wraw + (size_t) k * 64 * es);
566 const HVX_Vector v = is_f16 ? sv[0] : conv2d_f32_to_f16(sv[0], sv[1]);
567 vin[j] = conv2d_keep(Q6_V_vand_QV(qco, v), k < task->nk);
568 }
569
570 const uint32_t ktg = (task->g * KGp + task->kc) / 32 + kt;
571 HVX_Vector * lo = (HVX_Vector *) (hc->w_tiles + ((size_t) task->rt * kp->tiled.n_k_tiles + ktg) * HMX_FP16_TILE_N_ELMS);
572 HVX_Vector * hi = task->rt + 1 < hc->n_rt ?
573 (HVX_Vector *) (hc->w_tiles + ((size_t) (task->rt + 1) * kp->tiled.n_k_tiles + ktg) * HMX_FP16_TILE_N_ELMS) : sink;
574 conv2d_transpose_w32(vin, 1, scratch, lo, hi);
575 }
576}
577
578static inline void conv2d_hmx_wpack_kmajor_dma(const struct htp_conv2d_hmx_context * hc,
579 dma_queue * dmaq,
580 uint8_t * wraw,
581 const struct wpack_kmajor_task * task,
582 uint32_t es,
583 size_t ks) {
584 const struct htp_tensor * kernel = hc->octx->src[0];
585 const uint32_t KG = hc->kp->tiled.k_group;
586 if (task->nk > 0) {
587 const dma_addr_t src = kernel->data + ((size_t) task->g * KG + task->kc) * ks + (size_t) (hc->rt0 + task->rt) * 32 * es;
588 dma_queue_push(dmaq, dma_make_data(wraw, src), 64 * es, ks, task->nco * es, task->nk);
589 } else {
590 dma_queue_push(dmaq, dma_make_data(wraw, 0), 0, 0, 0, 0);
591 }
592}
593
594struct conv2d_wpack_kmajor_iter {
595 uint32_t cb;
596 uint32_t g;
597 uint32_t kc;
598 uint32_t owner;
599 uint32_t n_cb;
600 uint32_t NG;
601 uint32_t KG;
602 uint32_t KGp;
603 uint32_t k_pad;
604 uint32_t WK;
605 uint32_t Cout;
606 uint32_t rt0;
607 uint32_t nth;
608 uint32_t ith;
609};
610
611static inline bool conv2d_wpack_kmajor_next_task(struct conv2d_wpack_kmajor_iter * it, struct wpack_kmajor_task * task) {
612 while (it->cb < it->n_cb) {
613 const uint32_t span = it->g + 1 == it->NG ? it->k_pad - it->g * it->KGp : it->KGp;
614 while (it->kc < span) {
615 const bool mine = it->owner == it->ith;
616 it->owner = it->owner + 1 == it->nth ? 0 : it->owner + 1;
617 const uint32_t kc = it->kc;
618 it->kc += it->WK;
619 if (mine) {
620 const uint32_t rt = 2 * it->cb;
621 const uint32_t co0 = (it->rt0 + rt) * 32;
622 task->rt = rt;
623 task->g = it->g;
624 task->kc = kc;
625 task->nco = MIN(64, it->Cout - co0);
626 task->nk = kc < it->KG ? MIN(it->WK, it->KG - kc) : 0;
627 task->hk = MIN(it->WK, span - kc);
628 return true;
629 }
630 }
631 it->kc = 0;
632 if (++it->g == it->NG) {
633 it->g = 0;
634 it->cb++;
635 }
636 }
637 return false;
638}
639
640// Kernel stored as [k][cout] (cout contiguous): DMA k rows of 64 cout, transpose into two row tiles
641static void conv2d_hmx_wpack_kmajor_thread(unsigned int nth, unsigned int ith, void * data) {
642 const struct htp_conv2d_hmx_context * hc = (const struct htp_conv2d_hmx_context *) data;
643 struct htp_ops_context * octx = hc->octx;
644 const struct htp_conv2d_kernel_params * kp = hc->kp;
645 const struct htp_tensor * kernel = octx->src[0];
646
647 const uint32_t Cout = kernel->ne[3];
648 const uint32_t k_pad = kp->tiled.n_k_tiles * HMX_FP16_TILE_N_COLS;
649 const uint32_t WK = kp->tiled.wk_chunk;
650 const uint32_t KG = kp->tiled.k_group;
651 const uint32_t KGp = kp->tiled.k_group_pad;
652 const uint32_t NG = kp->tiled.n_groups;
653 const size_t ks = kp->tiled.w_kstride;
654 const bool is_f16 = kernel->type == HTP_TYPE_F16;
655 const uint32_t es = is_f16 ? sizeof(__fp16) : sizeof(float);
656 const uint32_t n_cb = (hc->n_rt + 1) / 2;
657
658 dma_queue * dmaq = octx->ctx->dma[ith];
659 const size_t w_raw_size = (size_t) WK * 64 * sizeof(float);
660 uint8_t * wraws[2] = { hc->thr_base + (size_t) ith * kp->thr_size,
661 hc->thr_base + (size_t) ith * kp->thr_size + w_raw_size };
662 HVX_Vector * vin = (HVX_Vector *) (wraws[1] + w_raw_size);
663 HVX_Vector * scratch = vin + 32;
664 HVX_Vector * sink = scratch + 32;
665
666 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
667
668 struct conv2d_wpack_kmajor_iter it = {
669 .cb = 0,
670 .g = 0,
671 .kc = 0,
672 .owner = 0,
673 .n_cb = n_cb,
674 .NG = NG,
675 .KG = KG,
676 .KGp = KGp,
677 .k_pad = k_pad,
678 .WK = WK,
679 .Cout = Cout,
680 .rt0 = hc->rt0,
681 .nth = nth,
682 .ith = ith,
683 };
684
685 struct wpack_kmajor_task tasks[2];
686 uint32_t n_tasks = 0;
687
688 // push(0), push(1)
689 for (uint32_t i = 0; i < 2; i++) {
690 if (conv2d_wpack_kmajor_next_task(&it, &tasks[i])) {
691 conv2d_hmx_wpack_kmajor_dma(hc, dmaq, wraws[i], &tasks[i], es, ks);
692 n_tasks++;
693 }
694 }
695
696 // pop(N), compute(N), push(N + 2)
697 uint32_t bi = 0;
698 while (n_tasks > 0) {
699 dma_queue_pop(dmaq);
700 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) tasks[bi].kc);
701 conv2d_hmx_wpack_kmajor_compute(hc, wraws[bi], &tasks[bi], vin, scratch, sink, is_f16, es);
702 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) tasks[bi].kc);
703 n_tasks--;
704
705 struct wpack_kmajor_task next_task;
706 if (conv2d_wpack_kmajor_next_task(&it, &next_task)) {
707 tasks[bi] = next_task;
708 conv2d_hmx_wpack_kmajor_dma(hc, dmaq, wraws[bi], &tasks[bi], es, ks);
709 n_tasks++;
710 }
711 bi ^= 1;
712 }
713}
714
715static void conv2d_hmx_pack(const struct htp_conv2d_hmx_context * hc, uint32_t ith) {
716 struct htp_ops_context * octx = hc->octx;
717 const struct htp_conv2d_kernel_params * kp = hc->kp;
718 const struct htp_tensor * kernel = octx->src[0];
719
720 const uint32_t KW = kernel->ne[0];
721 const uint32_t KH = kernel->ne[1];
722 const uint32_t Cin = kernel->ne[2];
723 const uint32_t d0 = octx->op_params[4];
724 const uint32_t n_vec = kp->tiled.n_k_tiles * (HMX_FP16_TILE_N_ELMS / 64);
725
726 dma_queue * dmaq = octx->ctx->dma[ith];
727 uint8_t * thr = hc->thr_base + (size_t) ith * kp->thr_size;
728 float * stages[2] = {
729 (float *) thr,
730 (float *) (thr + kp->stage_size),
731 };
732 __fp16 * act = hc->act[hc->pack_chunk & 1];
733
734 // CWHN: fp16 stage (+256 bytes of over-read slack), then 32 + 32 vectors for the transpose
735 const uint32_t n_stage = kp->tiled.n_stage > 1 ? 2 : 1;
736 __fp16 * stage16 = (__fp16 *) (thr + (size_t) n_stage * kp->stage_size + (size_t) 2 * 32 * kp->tiled.seg_tiles * 32 * sizeof(float));
737 HVX_Vector * vin = (HVX_Vector *) ((uint8_t *) stage16 + kp->stage_size / 2 + 256);
738 HVX_Vector * scratch = vin + 32;
739 const uint32_t gpr = kp->tiled.n_groups == KH ? 1 : KW;
740
741 uint32_t t0, t1;
742 const uint32_t cs = conv2d_hmx_chunk_range(hc, hc->pack_chunk, hc->pack_tpt, ith, &t0, &t1);
743
744 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
745
746 if (t0 >= t1) {
747 return;
748 }
749
750 uint32_t ng[2] = { 0, 0 };
751 uint32_t tq = t0;
752 for (uint32_t i = 0; i < n_stage && tq < t1; i++) {
753 ng[i] = conv2d_hmx_issue_seg(octx, kp, dmaq, stages[i], tq, t1);
754 tq += ng[i];
755 }
756
757 uint32_t bi = 0;
758 for (uint32_t t = t0; t < t1;) {
759 const uint32_t G = ng[bi];
760 float * stage = stages[bi];
761
762 for (uint32_t p = 0; p < KH; p++) {
763 dma_queue_pop(dmaq);
764 }
765
766 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) t);
767 if (kp->cwhn) {
768 const uint32_t Wb = 32 * G + (KW - 1) * d0;
769 conv2d_hmx_cwhn_f16(stage16, stage, KH, kp->tiled.stage_row, Wb * Cin);
770 for (uint32_t g = 0; g < G; g++) {
771 __fp16 * tile = act + (size_t) (t - cs + g) * n_vec * 64;
772 conv2d_hmx_pack_tile_cwhn(tile, stage16 + (size_t) g * 32 * Cin, vin, scratch, kp, gpr, Cin, d0);
773 }
774 } else {
775 for (uint32_t g = 0; g < G; g++) {
776 __fp16 * tile = act + (size_t) (t - cs + g) * n_vec * 64;
777 conv2d_hmx_pack_tile(tile, stage + g * 32, kp->tiled.k_size, KW, d0, kp->tiled.stage_row, n_vec);
778 }
779 }
780 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) t);
781
782 if (tq < t1) {
783 ng[bi] = conv2d_hmx_issue_seg(octx, kp, dmaq, stage, tq, t1);
784 tq += ng[bi];
785 }
786 t += G;
787 bi = bi + 1 == n_stage ? 0 : bi + 1;
788 }
789}
790
791static void conv2d_hmx_unpack(const struct htp_conv2d_hmx_context * hc, uint32_t ith) {
792 struct htp_ops_context * octx = hc->octx;
793 const struct htp_conv2d_kernel_params * kp = hc->kp;
794 const struct htp_tensor * dst = octx->dst;
795
796 const uint32_t OW = dst->ne[0];
797 const uint32_t OH = dst->ne[1];
798 const uint32_t Cout = dst->ne[2];
799 const uint32_t n_owt = kp->tiled.n_ow_tiles;
800 const uint32_t OS = kp->tiled.seg_tiles * 32;
801
802 dma_queue * dmaq = octx->ctx->dma[ith];
803 uint8_t * thr = hc->thr_base + (size_t) ith * kp->thr_size + (size_t) (kp->tiled.n_stage > 1 ? 2 : 1) * kp->stage_size;
804 float * ostage[2] = {
805 (float *) thr,
806 (float *) (thr + (size_t) 32 * OS * sizeof(float)),
807 };
808 const __fp16 * out = hc->out[hc->unpack_chunk & 1];
809
810 uint32_t t0, t1;
811 const uint32_t cs = conv2d_hmx_chunk_range(hc, hc->unpack_chunk, hc->unpack_tpt, ith, &t0, &t1);
812 const uint32_t nct = MIN(cs + kp->ct_chunk, hc->t_end) - cs;
813
814 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
815
816 if (t0 >= t1) {
817 return;
818 }
819
820 uint32_t b = 0;
821 uint32_t n_pushed = 0;
822
823 for (uint32_t t = t0; t < t1;) {
824 const uint32_t row = fastdiv(t, &kp->tiled.div_ow_tiles);
825 const uint32_t owt = t - row * n_owt;
826 const uint32_t G = MIN(MIN(kp->tiled.seg_tiles, t1 - t), n_owt - owt);
827 const uint32_t n = fastdiv(row, &kp->tiled.div_oh);
828 const uint32_t oh = row - n * OH;
829 const uint32_t ow0 = owt * 32;
830 const uint32_t nv = MIN(G * 32, OW - ow0);
831
832 for (uint32_t rt = 0; rt < hc->n_rt; rt++) {
833 const uint32_t co0 = (hc->rt0 + rt) * 32;
834 const uint32_t nco = MIN(32, Cout - co0);
835 const HVX_Vector restore = conv2d_hmx_rt_restore(hc->scales, rt);
836
837 if (n_pushed >= 2) {
838 dma_queue_pop(dmaq);
839 }
840
841 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) t);
842 float * ob = ostage[b];
843 for (uint32_t g = 0; g < G; g++) {
844 const HVX_Vector * tv = (const HVX_Vector *) (out + ((size_t) rt * nct + (t - cs + g)) * HMX_FP16_TILE_N_ELMS);
845 for (uint32_t r = 0; r < 16; r++) {
846 const HVX_VectorPair pr = hvx_vec_f16_to_f32_shuff(tv[r]);
847 *(HVX_Vector *) (ob + (size_t) (2 * r + 0) * OS + g * 32) = hvx_vec_mul_f32_f32(Q6_V_lo_W(pr), restore);
848 *(HVX_Vector *) (ob + (size_t) (2 * r + 1) * OS + g * 32) = hvx_vec_mul_f32_f32(Q6_V_hi_W(pr), restore);
849 }
850 }
851 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) t);
852
853 const dma_addr_t ddr = dst->data + (size_t) n * dst->nb[3] + (size_t) oh * dst->nb[1] + (size_t) co0 * dst->nb[2] + (size_t) ow0 * sizeof(float);
854 dma_queue_push(dmaq, dma_make_data(ddr, ob), dst->nb[2], (size_t) OS * sizeof(float), (size_t) nv * sizeof(float), nco);
855 n_pushed++;
856 b ^= 1;
857 }
858 t += G;
859 }
860
861 // Drain the pipeline
862 if (n_pushed >= 1) {
863 dma_queue_pop(dmaq);
864 }
865 if (n_pushed >= 2) {
866 dma_queue_pop(dmaq);
867 }
868}
869
870static void conv2d_hmx_phase_thread(unsigned int nth, unsigned int ith, void * data) {
871 const struct htp_conv2d_hmx_context * hc = (const struct htp_conv2d_hmx_context *) data;
872
873 if (hc->unpack_chunk >= 0) {
874 conv2d_hmx_unpack(hc, ith);
875 }
876 // weights of this rt block are packed before its first chunk
877 if (hc->pack_chunk == 0) {
878 const uint32_t n_kt = hc->kp->tiled.n_k_tiles;
879 for (uint32_t rt = ith; rt < hc->n_rt; rt += nth) {
880 conv2d_hmx_rt_scale(hc->scales + rt * CONV2D_HMX_SCALES_N, hc->w_tiles + (size_t) rt * n_kt * HMX_FP16_TILE_N_ELMS, n_kt,
881 hc->kp->tiled.k_size);
882 }
883 }
884 if (hc->pack_chunk >= 0) {
885 conv2d_hmx_pack(hc, ith);
886 }
887}
888
889static inline uint32_t conv2d_hmx_tpt(const struct htp_conv2d_hmx_context * hc, int32_t c, uint32_t nth) {
890 if (c < 0) {
891 return 0;
892 }
893 const uint32_t cs = hc->t_base + (uint32_t) c * hc->kp->ct_chunk;
894 const uint32_t nct = MIN(hc->kp->ct_chunk, hc->t_end - cs);
895 return (nct + nth - 1) / nth;
896}
897
898static void conv2d_hmx_run_phase(struct htp_conv2d_hmx_context * hc, int32_t pack, int32_t unpack, uint32_t nth) {
899 hc->pack_chunk = pack;
900 hc->unpack_chunk = unpack;
901 hc->pack_tpt = conv2d_hmx_tpt(hc, pack, nth);
902 hc->unpack_tpt = conv2d_hmx_tpt(hc, unpack, nth);
903 work_queue_run(hc->octx->ctx->work_queue, conv2d_hmx_phase_thread, hc, nth);
904}
905
906static int conv2d_hmx_tiled(struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp) {
907 uint32_t row_base, n_rows;
908 conv2d_mdev_rows(octx, &row_base, &n_rows);
909 if (n_rows == 0) {
910 return HTP_STATUS_OK;
911 }
912
913 const size_t tile = 2048;
914 const size_t w_size = (size_t) kp->rt_block * kp->tiled.n_k_tiles * tile;
915 const size_t act_size = (size_t) kp->ct_chunk * kp->tiled.n_k_tiles * tile;
916 const size_t out_size = (size_t) kp->rt_block * kp->ct_chunk * tile;
917
918 struct htp_conv2d_vtcm_layout layout;
919 htp_conv2d_vtcm_layout_build(&layout, kp->rt_block, w_size, act_size, out_size, kp->thr_size, kp->n_threads);
920
921 uint8_t * base = (uint8_t *) octx->ctx->vtcm_base;
922
923 struct htp_conv2d_hmx_context hc = {
924 .octx = octx,
925 .kp = kp,
926 .layout = layout,
927 .scales = VTCM_LAYOUT_PTR(__fp16, base, layout.off_scales),
928 .w_tiles = VTCM_LAYOUT_PTR(__fp16, base, layout.off_w),
929 .act = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[1]) },
930 .out = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[1]) },
931 .thr_base = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_thr),
932 .t_base = row_base * kp->tiled.n_ow_tiles,
933 .t_end = (row_base + n_rows) * kp->tiled.n_ow_tiles,
934 };
935
936 const uint32_t nth = kp->n_threads;
937 const int32_t n_chunks = (int32_t) ((hc.t_end - hc.t_base + kp->ct_chunk - 1) / kp->ct_chunk);
938
939 struct htp_conv2d_hmx_task task;
940
941 for (uint32_t rt0 = 0; rt0 < kp->n_row_tiles; rt0 += kp->rt_block) {
942 hc.rt0 = rt0;
943 hc.n_rt = MIN(kp->rt_block, kp->n_row_tiles - rt0);
944 work_queue_run(octx->ctx->work_queue, kp->w_kmajor ? conv2d_hmx_wpack_kmajor_thread : conv2d_hmx_wpack_thread, &hc, nth);
945
946 conv2d_hmx_run_phase(&hc, 0, -1, nth);
947
948 for (int32_t c = 0; c < n_chunks; c++) {
949 const uint32_t cs = hc.t_base + (uint32_t) c * kp->ct_chunk;
950
951 task.output = hc.out[c & 1];
952 task.kernel_tiles = hc.w_tiles;
953 task.patch_tiles = hc.act[c & 1];
954 task.scales = hc.scales;
955 task.n_row_tiles = hc.n_rt;
956 task.n_col_tiles = MIN(kp->ct_chunk, hc.t_end - cs);
957 task.n_dot_tiles = kp->tiled.n_k_tiles;
958 hmx_queue_push(octx->ctx->hmx_queue, hmx_queue_make_desc(conv2d_hmx_worker, &task));
959
960 const int32_t pack = c + 1 < n_chunks ? c + 1 : -1;
961 const int32_t unpack = c - 1;
962 if (pack >= 0 || unpack >= 0) {
963 conv2d_hmx_run_phase(&hc, pack, unpack, nth);
964 }
965
966 hmx_queue_pop(octx->ctx->hmx_queue);
967 }
968
969 conv2d_hmx_run_phase(&hc, -1, n_chunks - 1, nth);
970 }
971
972 return HTP_STATUS_OK;
973}
974
975// ---- HMX_WIN: implicit GEMM over sliding crouton windows ----
976
977#define CONV2D_WIN_RT_FULL 0x7FE // spatial mask 11111, channel stop 31
978#define CONV2D_WIN_SCAT_MAX 4 // scatter croutons for Cin <= 2 * CONV2D_WIN_SCAT_MAX
979
980// whcn small Cin: crouton byte offset of word j of a [c | c + 1] pixel pair vector
981static const int32_t conv2d_hmx_win_scat_off[32] __attribute__((aligned(VLEN))) = {
982 0 * 128, 1 * 128, 2 * 128, 3 * 128, 4 * 128, 5 * 128, 6 * 128, 7 * 128,
983 8 * 128, 9 * 128, 10 * 128, 11 * 128, 12 * 128, 13 * 128, 14 * 128, 15 * 128,
984 0 * 128 + 4, 1 * 128 + 4, 2 * 128 + 4, 3 * 128 + 4, 4 * 128 + 4, 5 * 128 + 4, 6 * 128 + 4, 7 * 128 + 4,
985 8 * 128 + 4, 9 * 128 + 4, 10 * 128 + 4, 11 * 128 + 4, 12 * 128 + 4, 13 * 128 + 4, 14 * 128 + 4, 15 * 128 + 4,
986};
987
988struct htp_conv2d_win_context {
989 struct htp_ops_context * octx;
990 const struct htp_conv2d_kernel_params * kp;
991 struct htp_conv2d_vtcm_layout layout;
992 __fp16 * scales; // [rt] HMX scales
993 __fp16 * w_tiles; // [rt][tap][cc] weight tiles
994 __fp16 * act[2]; // [cc][b] croutons of a chunk
995 __fp16 * out[2]; // [rt][tile] output tiles of a chunk
996 uint8_t * thr_base;
997 uint32_t t_base; // device col tile range
998 uint32_t t_end;
999 uint32_t vr0; // device virtual output row range
1000 uint32_t vr1;
1001 uint32_t rt0;
1002 uint32_t n_rt;
1003 int32_t pack_chunk;
1004 int32_t unpack_chunk;
1005 uint32_t n_scat; // small Cin: scatters per crouton into zeroed croutons, else 0
1006 uint8_t * live[2]; // per chunk: tiles with an output row to write
1007};
1008
1009struct htp_conv2d_win_task {
1010 __fp16 * out;
1011 const __fp16 * w;
1012 const __fp16 * act;
1013 const __fp16 * scales;
1014 const uint32_t * taps; // activation address offset of each tap (crouton + window offset fields)
1015 uint32_t n_taps;
1016 uint32_t n_rt;
1017 uint32_t n_tiles;
1018 uint32_t out_stride; // elements between row tiles in out
1019 uint32_t n_cc;
1020 uint32_t plane; // bytes between channel chunks in act
1021 uint32_t rt_last;
1022 uint32_t wr_last;
1023 const uint8_t * live; // tiles with at least one output row to write
1024};
1025
1026// Activation address offset of input pixel offset u: crouton u / 32, window offset u % 32 in Rs[10:7] and Rs[1]
1027static inline uint32_t conv2d_hmx_win_act_off(uint32_t u) {
1028 return ((u >> 5) << 11) | ((u & 30) << 6) | ((u & 1) << 1);
1029}
1030
1031static void conv2d_hmx_win_worker(void * data) {
1032 const struct htp_conv2d_win_task * j = (const struct htp_conv2d_win_task *) data;
1033
1034 const uint32_t * taps = j->taps;
1035 const uint32_t T = j->n_taps;
1036 const uint32_t n_cc = j->n_cc;
1037 const uint32_t plane = j->plane;
1038 const uint32_t rt_last = HMX_FP16_TILE_SIZE | j->rt_last;
1039 const uint32_t wr_last = j->wr_last;
1040 const uint32_t n_tiles = j->n_tiles;
1041 const uint32_t n_rt = j->n_rt;
1042 const uint32_t ostride = j->out_stride;
1043 const uint32_t act = (uint32_t) j->act;
1044 const size_t wstride = (size_t) T * n_cc * HMX_FP16_TILE_N_ELMS;
1045
1046 for (uint32_t r = 0; r < n_rt; r++) {
1047 asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) (j->scales + r * CONV2D_HMX_SCALES_N)));
1048
1049 __fp16 * o = j->out + r * ostride;
1050 const __fp16 * w0 = j->w + r * wstride;
1051
1052 for (uint32_t i = 0; i < n_tiles; i++, o += HMX_FP16_TILE_N_ELMS) {
1053 if (!j->live[i]) {
1054 continue;
1055 }
1056 const uint32_t base = act + i * HMX_FP16_TILE_SIZE;
1057 const __fp16 * w = w0;
1058
1059 asm volatile(HMX_CLRACC_F16());
1060 if (n_cc == 1) {
1061 for (uint32_t t = 0; t < T; t++) {
1062 asm volatile("{ activation.hf = mxmem(%0, %1):single\n weight.hf = mxmem(%2, %3) }"
1063 :: "r"(base + taps[t]), "r"(rt_last), "r"(w), "r"(wr_last));
1064 w += HMX_FP16_TILE_N_ELMS;
1065 }
1066 } else {
1067 for (uint32_t t = 0; t < T; t++) {
1068 uint32_t rs = base + taps[t];
1069 for (uint32_t cc = 1; cc < n_cc; cc++) {
1070 asm volatile("{ activation.hf = mxmem(%0, %1):single\n weight.hf = mxmem(%2, %3) }"
1071 :: "r"(rs), "r"(HMX_FP16_TILE_SIZE | CONV2D_WIN_RT_FULL), "r"(w), "r"(HMX_FP16_TILE_SIZE - 1));
1072 rs += plane;
1073 w += HMX_FP16_TILE_N_ELMS;
1074 }
1075 asm volatile("{ activation.hf = mxmem(%0, %1):single\n weight.hf = mxmem(%2, %3) }"
1076 :: "r"(rs), "r"(rt_last), "r"(w), "r"(wr_last));
1077 w += HMX_FP16_TILE_N_ELMS;
1078 }
1079 }
1080 asm volatile(HMX_STORE_AFTER_F16("%0", "%1") : : "r"(o), "r"(0) : "memory");
1081 }
1082 }
1083}
1084
1085static inline void conv2d_vdeal_w(HVX_Vector * a, HVX_Vector * b) {
1086 const HVX_VectorPair p = Q6_W_vdeal_VVR(*b, *a, -4);
1087 *a = Q6_V_lo_W(p);
1088 *b = Q6_V_hi_W(p);
1089}
1090
1091#define CONV2D_XPOSE_STAGE(v, m, op) \
1092 _Pragma("unroll") for (uint32_t i = 0; i < 16; i++) { \
1093 if (!(i & (m))) { \
1094 op(&(v)[i], &(v)[i | (m)]); \
1095 } \
1096 }
1097
1098// 16x32 word transpose: in[i] = [rows 2i, 2i + 1 of 16 words each], out[slot(v)] word c = row c word v
1099static inline void conv2d_xpose_w16(HVX_Vector * restrict v) {
1100 CONV2D_XPOSE_STAGE(v, 8, conv2d_vshuff_w);
1101 CONV2D_XPOSE_STAGE(v, 4, conv2d_vshuff_w);
1102 CONV2D_XPOSE_STAGE(v, 2, conv2d_vshuff_w);
1103 CONV2D_XPOSE_STAGE(v, 1, conv2d_vshuff_w);
1104 CONV2D_XPOSE_STAGE(v, 8, conv2d_vshuff_w);
1105}
1106
1107// Inverse of conv2d_xpose_w16
1108static inline void conv2d_unxpose_w16(HVX_Vector * restrict v) {
1109 CONV2D_XPOSE_STAGE(v, 8, conv2d_vdeal_w);
1110 CONV2D_XPOSE_STAGE(v, 1, conv2d_vdeal_w);
1111 CONV2D_XPOSE_STAGE(v, 2, conv2d_vdeal_w);
1112 CONV2D_XPOSE_STAGE(v, 4, conv2d_vdeal_w);
1113 CONV2D_XPOSE_STAGE(v, 8, conv2d_vdeal_w);
1114}
1115
1116// conv2d_xpose_w16 output position of row v
1117static inline uint32_t conv2d_xpose_slot(uint32_t v) {
1118 return ((v & 1) << 3) | (v >> 1);
1119}
1120
1121static inline uint32_t conv2d_hmx_win_chunk_tiles(const struct htp_conv2d_win_context * wc, uint32_t c, uint32_t * ts) {
1122 *ts = wc->t_base + c * wc->kp->ct_chunk;
1123 return MIN(wc->kp->ct_chunk, wc->t_end - *ts);
1124}
1125
1126struct conv2d_win_wpack_kmajor_task {
1127 uint32_t rt;
1128 uint32_t cc;
1129 uint32_t tg;
1130 uint32_t t0;
1131 uint32_t nt;
1132 uint32_t c0;
1133 uint32_t nc;
1134 uint32_t co0;
1135 uint32_t nco;
1136 uint32_t kh0;
1137 uint32_t kw0;
1138};
1139
1140static inline struct conv2d_win_wpack_kmajor_task conv2d_hmx_win_wpack_kmajor_make_task(
1141 const struct htp_conv2d_win_context * wc,
1142 uint32_t unit,
1143 uint32_t n_tg,
1144 uint32_t n_cc,
1145 const struct fastdiv_values * div_tg,
1146 const struct fastdiv_values * div_cc,
1147 const struct fastdiv_values * div_kw,
1148 uint32_t TG,
1149 uint32_t T,
1150 uint32_t KW,
1151 uint32_t Cin,
1152 uint32_t Cout) {
1153 const uint32_t tg = fastmodulo(unit, n_tg, div_tg);
1154 const uint32_t u_cc = fastdiv(unit, div_tg);
1155 const uint32_t cc = fastmodulo(u_cc, n_cc, div_cc);
1156 const uint32_t rt = fastdiv(u_cc, div_cc);
1157 const uint32_t co0 = (wc->rt0 + rt) * 32;
1158 const uint32_t nco = MIN(32, Cout - co0);
1159 const uint32_t c0 = cc * 32;
1160 const uint32_t nc = MIN(32, Cin - c0);
1161 const uint32_t t0 = tg * TG;
1162 const uint32_t nt = MIN(TG, T - t0);
1163 const uint32_t kh0 = fastdiv(t0, div_kw);
1164 const uint32_t kw0 = fastmodulo(t0, KW, div_kw);
1165
1166 return (struct conv2d_win_wpack_kmajor_task) {
1167 .rt = rt,
1168 .cc = cc,
1169 .tg = tg,
1170 .t0 = t0,
1171 .nt = nt,
1172 .c0 = c0,
1173 .nc = nc,
1174 .co0 = co0,
1175 .nco = nco,
1176 .kh0 = kh0,
1177 .kw0 = kw0,
1178 };
1179}
1180
1181static inline uint32_t conv2d_hmx_win_wpack_kmajor_dma(const struct htp_conv2d_win_context * wc,
1182 dma_queue * dmaq,
1183 uint8_t * raw,
1184 const struct conv2d_win_wpack_kmajor_task * task,
1185 uint32_t KW,
1186 uint32_t es) {
1187 const struct htp_tensor * kernel = wc->octx->src[0];
1188 uint32_t kh = task->kh0;
1189 uint32_t kw = task->kw0;
1190
1191 for (uint32_t t = 0; t < task->nt; t++) {
1192 const dma_addr_t src = kernel->data + (size_t) kw * kernel->nb[0] + (size_t) kh * kernel->nb[1] +
1193 (size_t) task->c0 * kernel->nb[2] + (size_t) task->co0 * es;
1194 dma_queue_push(dmaq, dma_make_data(raw + (size_t) t * 32 * VLEN, src), VLEN, kernel->nb[2], task->nco * es, task->nc);
1195 if (++kw == KW) {
1196 kw = 0;
1197 kh++;
1198 }
1199 }
1200 return task->nt;
1201}
1202
1203static inline void conv2d_hmx_win_wpack_kmajor_compute(const struct htp_conv2d_win_context * wc,
1204 const uint8_t * raw,
1205 const struct conv2d_win_wpack_kmajor_task * task,
1206 uint32_t n_cc,
1207 uint32_t T,
1208 uint32_t KW,
1209 uint32_t KH,
1210 uint32_t Cin,
1211 uint32_t es,
1212 bool is_f16,
1213 HVX_Vector zero) {
1214 const struct htp_conv2d_kernel_params * kp = wc->kp;
1215 const HVX_VectorPred qco = Q6_Q_vsetq2_R(task->nco * es);
1216 if (kp->window.fold) {
1217 const HVX_Vector * rows = (const HVX_Vector *) raw;
1218 HVX_Vector * tile = (HVX_Vector *) (wc->w_tiles + ((size_t) (task->rt * KH + task->tg) * n_cc + task->cc) * HMX_FP16_TILE_N_ELMS);
1219 uint32_t kw = 0;
1220 uint32_t c = 0;
1221 for (uint32_t p = 0; p < 16; p++) {
1222 const HVX_Vector a = kw < KW ? Q6_V_vmux_QVV(qco, rows[kw * 32 + c], zero) : zero;
1223 if (++c == Cin) {
1224 c = 0;
1225 kw++;
1226 }
1227 const HVX_Vector b = kw < KW ? Q6_V_vmux_QVV(qco, rows[kw * 32 + c], zero) : zero;
1228 if (++c == Cin) {
1229 c = 0;
1230 kw++;
1231 }
1232 tile[p] = is_f16 ? Q6_V_lo_W(Q6_W_vshuff_VVR(b, a, -2)) : conv2d_f32_to_f16_shuff(a, b);
1233 }
1234 return;
1235 }
1236 for (uint32_t t = 0; t < task->nt; t++) {
1237 const HVX_Vector * rows = (const HVX_Vector *) (raw + (size_t) t * 32 * VLEN);
1238 HVX_Vector * tile = (HVX_Vector *) (wc->w_tiles + ((size_t) (task->rt * T + task->t0 + t) * n_cc + task->cc) * HMX_FP16_TILE_N_ELMS);
1239 for (uint32_t p = 0; p < 16; p++) {
1240 const HVX_Vector a = 2 * p + 0 < task->nc ? Q6_V_vmux_QVV(qco, rows[2 * p + 0], zero) : zero;
1241 const HVX_Vector b = 2 * p + 1 < task->nc ? Q6_V_vmux_QVV(qco, rows[2 * p + 1], zero) : zero;
1242 tile[p] = is_f16 ? Q6_V_lo_W(Q6_W_vshuff_VVR(b, a, -2)) : conv2d_f32_to_f16_shuff(a, b);
1243 }
1244 }
1245}
1246
1247// Cout-contiguous kernel: DMA w[t][c][32 cout] rows, tile vector p = (row 2p, row 2p+1) interleaved per cout
1248static void conv2d_hmx_win_wpack_kmajor(const struct htp_conv2d_win_context * wc, unsigned int nth, unsigned int ith) {
1249 struct htp_ops_context * octx = wc->octx;
1250 const struct htp_conv2d_kernel_params * kp = wc->kp;
1251 const struct htp_tensor * kernel = octx->src[0];
1252
1253 const uint32_t KW = kernel->ne[0];
1254 const uint32_t KH = kernel->ne[1];
1255 const uint32_t Cin = kernel->ne[2];
1256 const uint32_t Cout = kernel->ne[3];
1257 const uint32_t T = KW * KH;
1258 const uint32_t n_cc = kp->window.n_cc;
1259 const uint32_t TG = kp->window.w_cs;
1260 const bool is_f16 = kernel->type == HTP_TYPE_F16;
1261 const uint32_t es = is_f16 ? sizeof(__fp16) : sizeof(float);
1262
1263 dma_queue * dmaq = octx->ctx->dma[ith];
1264 const size_t w_raw_size = (size_t) TG * 32 * VLEN;
1265 uint8_t * raws[2] = { wc->thr_base + (size_t) ith * kp->thr_size,
1266 wc->thr_base + (size_t) ith * kp->thr_size + w_raw_size };
1267 const HVX_Vector zero = Q6_V_vzero();
1268
1269 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
1270
1271 const uint32_t n_tg = (T + TG - 1) / TG;
1272 const uint32_t n_units = wc->n_rt * n_cc * n_tg;
1273
1274 struct conv2d_win_wpack_kmajor_task tasks[2];
1275 uint32_t nd[2] = { 0, 0 };
1276 for (uint32_t k = 0, unit = ith; k < 2 && unit < n_units; k++, unit += nth) {
1277 tasks[k] = conv2d_hmx_win_wpack_kmajor_make_task(wc, unit, n_tg, n_cc, &kp->window.div_tg, &kp->window.div_cc, &kp->window.div_kw, TG, T, KW, Cin, Cout);
1278 nd[k] = conv2d_hmx_win_wpack_kmajor_dma(wc, dmaq, raws[k], &tasks[k], KW, es);
1279 }
1280 for (uint32_t unit = ith, k = 0; unit < n_units; unit += nth, k++) {
1281 const uint32_t bi = k & 1;
1282 uint8_t * raw = raws[bi];
1283
1284 for (uint32_t p = 0; p < nd[bi]; p++) {
1285 dma_queue_pop(dmaq);
1286 }
1287
1288 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) unit);
1289 conv2d_hmx_win_wpack_kmajor_compute(wc, raw, &tasks[bi], n_cc, T, KW, KH, Cin, es, is_f16, zero);
1290 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) unit);
1291
1292 if (unit + 2 * nth < n_units) {
1293 tasks[bi] = conv2d_hmx_win_wpack_kmajor_make_task(wc, unit + 2 * nth, n_tg, n_cc, &kp->window.div_tg, &kp->window.div_cc, &kp->window.div_kw, TG, T, KW, Cin, Cout);
1294 nd[bi] = conv2d_hmx_win_wpack_kmajor_dma(wc, dmaq, raw, &tasks[bi], KW, es);
1295 }
1296 }
1297}
1298
1299// K-contiguous kernel: DMA 32 cout rows of the w_cs cin of a packing unit, a dummy DMA if the unit has nothing to load
1300static void conv2d_hmx_win_wrows_dma(const struct htp_conv2d_win_context * wc, dma_queue * dmaq, uint8_t * raw, uint32_t unit, uint32_t n_cs) {
1301 const struct htp_conv2d_kernel_params * kp = wc->kp;
1302 const struct htp_tensor * kernel = wc->octx->src[0];
1303
1304 const uint32_t T = kernel->ne[0] * kernel->ne[1];
1305 const uint32_t Cin = kernel->ne[2];
1306 const uint32_t CS = kp->window.w_cs;
1307 const uint32_t es = kernel->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
1308 const uint32_t kpw = VLEN / es;
1309
1310 const uint32_t cs = fastmodulo(unit, n_cs, &kp->window.div_cs);
1311 const uint32_t u_cc = fastdiv(unit, &kp->window.div_cs);
1312 const uint32_t cc = fastmodulo(u_cc, kp->window.n_cc, &kp->window.div_cc);
1313 const uint32_t rt = fastdiv(u_cc, &kp->window.div_cc);
1314 const uint32_t co0 = (wc->rt0 + rt) * 32;
1315 const uint32_t nco = MIN(32, (uint32_t) kernel->ne[3] - co0);
1316 const uint32_t c0 = cc * 32 + cs * CS;
1317 const uint32_t nk = c0 < Cin ? MIN(CS, Cin - c0) * T : 0;
1318 if (nk == 0 || (kp->window.fold && cs > 0)) {
1319 dma_queue_push(dmaq, dma_make_data(raw, raw), 0, 0, 0, 0);
1320 return;
1321 }
1322 dma_queue_push(dmaq, dma_make_data(raw, kernel->data + (size_t) co0 * kernel->nb[3] + (size_t) c0 * kernel->nb[2]),
1323 (size_t) ((nk + kpw - 1) / kpw) * VLEN, kernel->nb[3], (size_t) nk * es, nco);
1324}
1325
1326// K-contiguous kernel (k = cin * T + t): transpose the 32 cout rows of a packing unit to k rows of 32 cout, then into tiles
1327__attribute__((noinline))
1328static void conv2d_hmx_win_wrows_pack(const struct htp_conv2d_win_context * wc, const uint8_t * raw, uint8_t * xr, HVX_Vector * vin,
1329 HVX_Vector * scratch, uint32_t unit, uint32_t n_cs) {
1330 const struct htp_conv2d_kernel_params * kp = wc->kp;
1331 const struct htp_tensor * kernel = wc->octx->src[0];
1332
1333 const uint32_t KW = kernel->ne[0];
1334 const uint32_t KH = kernel->ne[1];
1335 const uint32_t Cin = kernel->ne[2];
1336 const uint32_t Cout = kernel->ne[3];
1337 const uint32_t T = KW * KH;
1338 const uint32_t n_cc = kp->window.n_cc;
1339 const uint32_t CS = kp->window.w_cs;
1340 const bool is_f16 = kernel->type == HTP_TYPE_F16;
1341 const uint32_t kpw = is_f16 ? 64 : 32; // k per vector
1342 const uint32_t rstep = is_f16 ? 64 : 128; // bytes per transposed k row
1343
1344 const uint32_t cs = fastmodulo(unit, n_cs, &kp->window.div_cs);
1345 const uint32_t u_cc = fastdiv(unit, &kp->window.div_cs);
1346 const uint32_t cc = fastmodulo(u_cc, n_cc, &kp->window.div_cc);
1347 const uint32_t rt = fastdiv(u_cc, &kp->window.div_cc);
1348 const uint32_t co0 = (wc->rt0 + rt) * 32;
1349 const uint32_t nco = MIN(32, Cout - co0);
1350 const uint32_t c0 = cc * 32 + cs * CS;
1351 const uint32_t nc = c0 < Cin ? MIN(CS, Cin - c0) : 0;
1352 const uint32_t nk = nc * T;
1353 const uint32_t nvec = (nk + kpw - 1) / kpw;
1354 const size_t rs = (size_t) nvec * VLEN;
1355
1356 if (kp->window.fold && cs > 0) {
1357 return;
1358 }
1359
1360 for (uint32_t b = 0; b < nvec; b++) {
1361 // rows past nco are in the buffer but not loaded
1362 for (uint32_t o = 0; o < 32; o++) {
1363 vin[o] = conv2d_keep(*(const HVX_Vector *) (raw + o * rs + (size_t) b * VLEN), o < nco);
1364 }
1365 HVX_Vector * xo = (HVX_Vector *) (xr + (size_t) b * 32 * VLEN);
1366 conv2d_transpose_w32(vin, 1, scratch, xo, xo + 16);
1367 if (is_f16) {
1368 for (uint32_t i = 0; i < 32; i++) {
1369 xo[i] = Q6_Vh_vdeal_Vh(xo[i]);
1370 }
1371 }
1372 }
1373
1374 // rows past the valid ones are still inside xr, so they are loaded and masked like the rows of vin
1375 const HVX_Vector zero = Q6_V_vzero();
1376 if (kp->window.fold) {
1377 // tile kh, crouton channel kw * Cin + c is k row c * T + kh * KW + kw
1378 for (uint32_t kh = 0; kh < KH; kh++) {
1379 HVX_Vector * tile = (HVX_Vector *) (wc->w_tiles + ((size_t) (rt * KH + kh) * n_cc + cc) * HMX_FP16_TILE_N_ELMS);
1380 uint32_t kw = 0;
1381 uint32_t c = 0;
1382 for (uint32_t p = 0; p < 16; p++) {
1383 const HVX_Vector a = conv2d_keep(hvx_vmemu(xr + (size_t) (c * T + kh * KW + kw) * rstep), kw < KW);
1384 if (++c == Cin) {
1385 c = 0;
1386 kw++;
1387 }
1388 const HVX_Vector b = conv2d_keep(hvx_vmemu(xr + (size_t) (c * T + kh * KW + kw) * rstep), kw < KW);
1389 if (++c == Cin) {
1390 c = 0;
1391 kw++;
1392 }
1393 tile[p] = is_f16 ? Q6_V_lo_W(Q6_W_vshuff_VVR(b, a, -2)) : conv2d_f32_to_f16_shuff_z(a, b, zero);
1394 }
1395 }
1396 return;
1397 }
1398
1399 // tile (t, cc) vector p takes cin rows 2p, 2p + 1 of this step
1400 for (uint32_t t = 0; t < T; t++) {
1401 HVX_Vector * tile = (HVX_Vector *) (wc->w_tiles + ((size_t) (rt * T + t) * n_cc + cc) * HMX_FP16_TILE_N_ELMS);
1402 for (uint32_t pc = 0; pc < CS; pc += 2) {
1403 const uint32_t c = cs * CS + pc;
1404 const uint32_t p = c / 2;
1405 const HVX_Vector a = conv2d_keep(hvx_vmemu(xr + (size_t) ((pc + 0) * T + t) * rstep), pc + 0 < nc);
1406 const HVX_Vector b = conv2d_keep(hvx_vmemu(xr + (size_t) ((pc + 1) * T + t) * rstep), pc + 1 < nc);
1407 tile[p] = is_f16 ? Q6_V_lo_W(Q6_W_vshuff_VVR(b, a, -2)) : conv2d_f32_to_f16_shuff_z(a, b, zero);
1408 }
1409 }
1410}
1411
1412static void conv2d_hmx_win_wpack_rows(const struct htp_conv2d_win_context * wc, unsigned int nth, unsigned int ith) {
1413 struct htp_ops_context * octx = wc->octx;
1414 const struct htp_conv2d_kernel_params * kp = wc->kp;
1415 const struct htp_tensor * kernel = octx->src[0];
1416
1417 const uint32_t T = kernel->ne[0] * kernel->ne[1];
1418 const uint32_t CS = kp->window.w_cs;
1419 const uint32_t kpw = kernel->type == HTP_TYPE_F16 ? 64 : 32; // k per vector
1420
1421 const size_t wraw = (size_t) 32 * ((CS * T + kpw - 1) / kpw) * VLEN;
1422 dma_queue * dmaq = octx->ctx->dma[ith];
1423 uint8_t * raws[2] = { wc->thr_base + (size_t) ith * kp->thr_size, wc->thr_base + (size_t) ith * kp->thr_size + wraw };
1424 uint8_t * xr = raws[1] + wraw;
1425 HVX_Vector * vin = (HVX_Vector *) (xr + wraw);
1426 HVX_Vector * scratch = vin + 32;
1427
1428 const uint32_t n_cs = (32 + CS - 1) / CS;
1429 const uint32_t n_units = wc->n_rt * kp->window.n_cc * n_cs;
1430
1431 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
1432
1433 // one DMA per unit: push(N), push(N + 1), pop(N), pack(N), push(N + 2), ...
1434 for (uint32_t k = 0, unit = ith; k < 2 && unit < n_units; k++, unit += nth) {
1435 conv2d_hmx_win_wrows_dma(wc, dmaq, raws[k], unit, n_cs);
1436 }
1437 for (uint32_t unit = ith, k = 0; unit < n_units; unit += nth, k++) {
1438 uint8_t * raw = raws[k & 1];
1439
1440 dma_queue_pop(dmaq);
1441
1442 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) unit);
1443 conv2d_hmx_win_wrows_pack(wc, raw, xr, vin, scratch, unit, n_cs);
1444 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) unit);
1445
1446 if (unit + 2 * nth < n_units) {
1447 conv2d_hmx_win_wrows_dma(wc, dmaq, raw, unit + 2 * nth, n_cs);
1448 }
1449 }
1450}
1451
1452static void conv2d_hmx_win_wpack_thread(unsigned int nth, unsigned int ith, void * data) {
1453 const struct htp_conv2d_win_context * wc = (const struct htp_conv2d_win_context *) data;
1454
1455 if (wc->kp->w_kmajor) {
1456 conv2d_hmx_win_wpack_kmajor(wc, nth, ith);
1457 } else {
1458 conv2d_hmx_win_wpack_rows(wc, nth, ith);
1459 }
1460}
1461
1462// Fill stage with input pixels [ua, ub): whcn stage[c][u - ua], cwhn stage[u - ua][c]. Zero outside the image.
1463// Returns the number of DMAs pushed.
1464static uint32_t conv2d_hmx_win_stage(const struct htp_conv2d_win_context * wc, dma_queue * dmaq, float * stage, uint32_t ua, uint32_t ub) {
1465 const struct htp_ops_context * octx = wc->octx;
1466 const struct htp_conv2d_kernel_params * kp = wc->kp;
1467 const struct htp_tensor * input = octx->src[1];
1468
1469 const uint32_t IW = input->ne[0];
1470 const uint32_t IH = input->ne[1];
1471 const uint32_t Cin = input->ne[2];
1472 const uint32_t N = input->ne[3];
1473 const uint32_t SR = kp->window.sr;
1474 const uint32_t IHS = kp->window.ihs;
1475 const uint32_t p0 = octx->op_params[2];
1476 const uint32_t p1 = octx->op_params[3];
1477 const uint32_t span = kp->window.seg_b * 32;
1478 const bool cwhn = kp->cwhn;
1479
1480 // rows of one image are contiguous when there is no column padding
1481 const bool rows_contig = p0 == 0 && input->nb[1] == (size_t) IW * input->nb[0];
1482
1483 uint32_t nd = 0;
1484 uint32_t u = ua;
1485 while (u < ub) {
1486 const uint32_t vr = fastdiv(u, &kp->window.div_sr);
1487 const uint32_t col = u - vr * SR;
1488 const uint32_t n = fastdiv(vr, &kp->window.div_ihs);
1489 const uint32_t r = vr - n * IHS;
1490 const int32_t ih = (int32_t) r - (int32_t) p1;
1491
1492 uint32_t end = MIN(ub, (vr + 1) * SR);
1493 if (n >= N || ih < 0 || ih >= (int32_t) IH) {
1494 const uint32_t len = end - u;
1495 if (cwhn) {
1496 hvx_splat_f32_u(stage + (size_t) (u - ua) * Cin, 0.0f, len * Cin);
1497 } else {
1498 for (uint32_t c = 0; c < Cin; c++) {
1499 hvx_splat_f32_u(stage + (size_t) c * span + (u - ua), 0.0f, len);
1500 }
1501 }
1502 u = end;
1503 continue;
1504 }
1505
1506 if (rows_contig) {
1507 end = MIN(ub, (vr + (IH - (uint32_t) ih)) * SR);
1508 }
1509
1510 // valid columns [a, b) of the first row, all columns of the following rows
1511 const uint32_t a = MAX(col, p0);
1512 const uint32_t len = end - u;
1513 uint32_t b = MIN(col + len, p0 + IW);
1514 if (rows_contig) {
1515 b = col + len;
1516 }
1517
1518 if (a > col) {
1519 const uint32_t z = MIN(a, col + len) - col;
1520 if (cwhn) {
1521 hvx_splat_f32_u(stage + (size_t) (u - ua) * Cin, 0.0f, z * Cin);
1522 } else {
1523 for (uint32_t c = 0; c < Cin; c++) {
1524 hvx_splat_f32_u(stage + (size_t) c * span + (u - ua), 0.0f, z);
1525 }
1526 }
1527 }
1528 if (b < col + len) {
1529 const uint32_t z0 = MAX(a, b);
1530 const uint32_t z = col + len - z0;
1531 if (cwhn) {
1532 hvx_splat_f32_u(stage + (size_t) (u - ua + z0 - col) * Cin, 0.0f, z * Cin);
1533 } else {
1534 for (uint32_t c = 0; c < Cin; c++) {
1535 hvx_splat_f32_u(stage + (size_t) c * span + (u - ua + z0 - col), 0.0f, z);
1536 }
1537 }
1538 }
1539 if (b > a) {
1540 const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1] + (size_t) (a - p0) * input->nb[0];
1541 const uint32_t off = u - ua + (a - col);
1542 if (cwhn) {
1543 const size_t sz = (size_t) (b - a) * Cin * sizeof(float);
1544 dma_queue_push(dmaq, dma_make_data(stage + (size_t) off * Cin, src), sz, sz, sz, 1);
1545 } else {
1546 dma_queue_push(dmaq, dma_make_data(stage + off, src), (size_t) span * sizeof(float), input->nb[2],
1547 (size_t) (b - a) * sizeof(float), Cin);
1548 }
1549 nd++;
1550 }
1551 u = end;
1552 }
1553 return nd;
1554}
1555
1556// Fold staging: stage holds the raw input rows of the virtual rows of [ua, ub), after 128 bytes of slack for reads before
1557// the first row. whcn: raw[c][vr - vr_a][iw], cwhn: raw[vr - vr_a][iw][c].
1558// Folded pixel (kw * Cin + c, u = vr * OW + ow) is input pixel (vr, ow + kw * d0 - p0), channel c.
1559static inline float * conv2d_hmx_win_fold_raw(float * stage) {
1560 return stage + VLEN_FP32;
1561}
1562
1563static inline uint32_t conv2d_hmx_win_fold_rplane(const struct htp_conv2d_win_context * wc) {
1564 const uint32_t IW = wc->octx->src[1]->ne[0];
1565 return ((wc->kp->window.seg_b * 32 + wc->kp->window.sr - 1) / wc->kp->window.sr + 1) * IW;
1566}
1567
1568// DMA the raw input rows of [ua, ub), returns the number of DMAs pushed
1569static uint32_t conv2d_hmx_win_stage_fold(const struct htp_conv2d_win_context * wc, dma_queue * dmaq, float * stage, uint32_t ua, uint32_t ub) {
1570 const struct htp_ops_context * octx = wc->octx;
1571 const struct htp_conv2d_kernel_params * kp = wc->kp;
1572 const struct htp_tensor * input = octx->src[1];
1573
1574 const uint32_t IW = input->ne[0];
1575 const int32_t IH = input->ne[1];
1576 const uint32_t Cin = input->ne[2];
1577 const uint32_t N = input->ne[3];
1578 const uint32_t IHS = kp->window.ihs;
1579 const int32_t p1 = octx->op_params[3];
1580 const uint32_t RP = conv2d_hmx_win_fold_rplane(wc);
1581 const bool cont = input->nb[1] == (size_t) IW * input->nb[0];
1582 float * raw = conv2d_hmx_win_fold_raw(stage);
1583
1584 const uint32_t vr_a = fastdiv(ua, &kp->window.div_sr);
1585 const uint32_t vr_b = fastdiv(ub - 1, &kp->window.div_sr) + 1;
1586
1587 uint32_t nd = 0;
1588 for (uint32_t vr = vr_a; vr < vr_b;) {
1589 const uint32_t n = fastdiv(vr, &kp->window.div_ihs);
1590 const int32_t ih = (int32_t) (vr - n * IHS) - p1;
1591 if (n >= N || ih < 0 || ih >= IH) {
1592 vr++;
1593 continue;
1594 }
1595 // rows of one image are contiguous in DDR
1596 const uint32_t nr = cont ? MIN(vr_b - vr, (uint32_t) (IH - ih)) : 1;
1597 const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1];
1598 if (kp->cwhn) {
1599 // raw[vr - vr_a][iw][c]
1600 const size_t sz = (size_t) nr * IW * Cin * sizeof(float);
1601 dma_queue_push(dmaq, dma_make_data(raw + (size_t) (vr - vr_a) * IW * Cin, src), sz, sz, sz, 1);
1602 } else {
1603 dma_queue_push(dmaq, dma_make_data(raw + (size_t) (vr - vr_a) * IW, src), (size_t) RP * sizeof(float), input->nb[2],
1604 (size_t) nr * IW * sizeof(float), Cin);
1605 }
1606 nd++;
1607 vr += nr;
1608 }
1609 return nd;
1610}
1611
1612// Build croutons [ba, ba + nb) (relative to the chunk crouton base b0) from a filled stage
1613static void conv2d_hmx_win_croutons(const struct htp_conv2d_win_context * wc, const float * stage, __fp16 * act, uint32_t bo, uint32_t nb) {
1614 const struct htp_conv2d_kernel_params * kp = wc->kp;
1615 const uint32_t Cin = wc->octx->src[1]->ne[2] * (kp->window.fold ? wc->octx->src[0]->ne[0] : 1);
1616 const uint32_t n_cc = kp->window.n_cc;
1617 const uint32_t span = kp->window.seg_b * 32;
1618 const size_t pl = (size_t) kp->window.n_bc * HMX_FP16_TILE_N_ELMS;
1619 const HVX_Vector zero = Q6_V_vzero();
1620
1621 if (wc->n_scat) {
1622 // word j of the [c = 2k | c = 2k + 1] pixel pairs goes to (j % 16) * 128 + (2k + j / 16) * 4
1623 const HVX_Vector soff = *(const HVX_Vector *) conv2d_hmx_win_scat_off;
1624 for (uint32_t i = 0; i < nb; i++) {
1625 const size_t dst = (size_t) (act + (size_t) (bo + i) * HMX_FP16_TILE_N_ELMS);
1626 for (uint32_t k = 0; k < wc->n_scat; k++) {
1627 const HVX_Vector a = *(const HVX_Vector *) (stage + (size_t) (2 * k) * span + i * 32);
1628 const HVX_Vector b = 2 * k + 1 < Cin ? *(const HVX_Vector *) (stage + (size_t) (2 * k + 1) * span + i * 32) : zero;
1629 const HVX_VectorPred q = Q6_Q_vsetq2_R(2 * k + 1 < Cin ? VLEN : VLEN / 2);
1630 Q6_vscatter_QRMVwV(q, dst, HMX_FP16_TILE_SIZE - 1, Q6_Vw_vadd_VwVw(soff, Q6_V_vsplat_R(8 * k)), conv2d_f32_to_f16(a, b));
1631 }
1632 }
1633 if (nb > 0) {
1634 hvx_gather_sync(act + (size_t) (bo + nb - 1) * HMX_FP16_TILE_N_ELMS);
1635 }
1636 return;
1637 }
1638
1639 if (kp->cwhn) {
1640 for (uint32_t cc = 0; cc < n_cc; cc++) {
1641 const uint32_t nc = MIN(32, Cin - cc * 32);
1642 const HVX_VectorPred qc = Q6_Q_vsetq2_R(nc * sizeof(float));
1643 for (uint32_t i = 0; i < nb; i++) {
1644 HVX_Vector * dst = (HVX_Vector *) (act + cc * pl + (size_t) (bo + i) * HMX_FP16_TILE_N_ELMS);
1645 const float * src = stage + (size_t) i * 32 * Cin + cc * 32;
1646 for (uint32_t v = 0; v < 16; v++) {
1647 const HVX_Vector a = Q6_V_vmux_QVV(qc, hvx_vmemu(src + (size_t) (2 * v + 0) * Cin), zero);
1648 const HVX_Vector b = Q6_V_vmux_QVV(qc, hvx_vmemu(src + (size_t) (2 * v + 1) * Cin), zero);
1649 dst[v] = conv2d_f32_to_f16_shuff(a, b);
1650 }
1651 }
1652 }
1653 return;
1654 }
1655
1656 HVX_Vector y[16];
1657 for (uint32_t cc = 0; cc < n_cc; cc++) {
1658 const uint32_t nv = MIN(32, Cin - cc * 32);
1659 const uint32_t sv = span / VLEN_FP32;
1660 for (uint32_t i = 0; i < nb; i++) {
1661 // channel rows are span apart, walk them with one pointer
1662 const HVX_Vector * p = (const HVX_Vector *) (stage + (size_t) cc * 32 * span + i * 32);
1663 HVX_Vector * dst = (HVX_Vector *) (act + cc * pl + (size_t) (bo + i) * HMX_FP16_TILE_N_ELMS);
1664 if (nv == 32) {
1665 #pragma unroll
1666 for (uint32_t k = 0; k < 16; k++) {
1667 const HVX_Vector a = p[0];
1668 const HVX_Vector b = p[sv];
1669 p += 2 * sv;
1670 y[k] = conv2d_f32_to_f16(a, b);
1671 }
1672 } else {
1673 // partial chunk: channel pairs go to the crouton memory first, the transpose runs on the reloaded rows
1674 uint32_t k = 0;
1675 for (; k < nv / 2; k++) {
1676 dst[k] = conv2d_f32_to_f16(p[0], p[sv]);
1677 p += 2 * sv;
1678 }
1679 if (nv & 1) {
1680 dst[k++] = conv2d_f32_to_f16(p[0], zero);
1681 }
1682 for (; k < 16; k++) {
1683 dst[k] = zero;
1684 }
1685 #pragma unroll
1686 for (uint32_t k = 0; k < 16; k++) {
1687 y[k] = dst[k];
1688 }
1689 }
1690 conv2d_xpose_w16(y);
1691 #pragma unroll
1692 for (uint32_t v = 0; v < 16; v++) {
1693 dst[v] = y[conv2d_xpose_slot(v)];
1694 }
1695 }
1696 }
1697}
1698
1699// Fold: one crouton channel vector (32 pixels u0..u0+31), assembled from the row pieces of the block
1700struct conv2d_hmx_win_fold_piece {
1701 int32_t off; // raw offset of lane 0 for kw = 0, c = 0
1702 int32_t l0; // lanes [l0, l1) of this row
1703 int32_t l1;
1704 int32_t lb; // lane of iw = 0 for kw = 0
1705};
1706
1707// Lanes of a row piece that read inside the image for column shift sh
1708static inline HVX_VectorPred conv2d_hmx_win_fold_q(const struct conv2d_hmx_win_fold_piece * pc, int32_t sh, int32_t IW) {
1709 const int32_t lo = MAX(pc->l0, pc->lb - sh);
1710 const int32_t hi = MIN(pc->l1, pc->lb - sh + IW);
1711 if (hi <= lo) {
1712 return Q6_Q_vsetq_R(0);
1713 }
1714 return Q6_Q_and_QQn(Q6_Q_vsetq2_R(hi * sizeof(float)), Q6_Q_vsetq_R(lo * sizeof(float)));
1715}
1716
1717static void conv2d_hmx_win_croutons_fold(const struct htp_conv2d_win_context * wc, const float * stage, __fp16 * act, uint32_t bo,
1718 uint32_t nb, uint32_t ua) {
1719 const struct htp_ops_context * octx = wc->octx;
1720 const struct htp_conv2d_kernel_params * kp = wc->kp;
1721 const struct htp_tensor * input = octx->src[1];
1722
1723 const int32_t IW = input->ne[0];
1724 const int32_t IH = input->ne[1];
1725 const uint32_t Cin = input->ne[2];
1726 const uint32_t N = input->ne[3];
1727 const uint32_t KW = octx->src[0]->ne[0];
1728 const uint32_t nch = KW * Cin;
1729 const uint32_t SR = kp->window.sr;
1730 const uint32_t IHS = kp->window.ihs;
1731 const int32_t p0 = octx->op_params[2];
1732 const int32_t p1 = octx->op_params[3];
1733 const int32_t d0 = octx->op_params[4];
1734 const uint32_t RP = conv2d_hmx_win_fold_rplane(wc);
1735 const float * raw = conv2d_hmx_win_fold_raw((float *) stage);
1736
1737 const uint32_t vr_a = fastdiv(ua, &kp->window.div_sr);
1738 const HVX_Vector zero = Q6_V_vzero();
1739 const HVX_Vector soff = *(const HVX_Vector *) conv2d_hmx_win_scat_off;
1740
1741 for (uint32_t i = 0; i < nb; i++) {
1742 const uint32_t u0 = ua + 32 * i;
1743
1744 struct conv2d_hmx_win_fold_piece pc[4];
1745 uint32_t np = 0;
1746 for (uint32_t vr = fastdiv(u0, &kp->window.div_sr); vr * SR < u0 + 32 && np < 4; vr++) {
1747 const uint32_t n = fastdiv(vr, &kp->window.div_ihs);
1748 const int32_t ih = (int32_t) (vr - n * IHS) - p1;
1749 if (n >= N || ih < 0 || ih >= IH) {
1750 continue;
1751 }
1752 const int32_t l0 = (int32_t) (MAX(u0, vr * SR) - u0);
1753 const int32_t ow0 = (int32_t) (MAX(u0, vr * SR) - vr * SR);
1754 pc[np].l0 = l0;
1755 pc[np].l1 = (int32_t) (MIN(u0 + 32, (vr + 1) * SR) - u0);
1756 pc[np].lb = l0 - ow0 + p0;
1757 pc[np].off = (int32_t) ((vr - vr_a) * IW) + ow0 - l0 - p0;
1758 np++;
1759 }
1760
1761 __fp16 * dst = act + (size_t) (bo + i) * HMX_FP16_TILE_N_ELMS;
1762 if (np == 0) {
1763 HVX_Vector * d = (HVX_Vector *) dst;
1764 for (uint32_t v = 0; v < 16; v++) {
1765 d[v] = zero;
1766 }
1767 continue;
1768 }
1769
1770 // channel ch = kw * Cin + c. Channel pairs go to the crouton memory first, the transpose runs on the reloaded rows.
1771 // Lanes and offsets of the row pieces depend on kw only.
1772 HVX_Vector * d = (HVX_Vector *) dst;
1773 const bool scat = wc->n_scat != 0;
1774 HVX_Vector pa = zero;
1775 uint32_t ch = 0;
1776 for (uint32_t kw = 0; kw < KW; kw++) {
1777 const int32_t sh = (int32_t) kw * d0;
1778 const HVX_VectorPred q0 = conv2d_hmx_win_fold_q(&pc[0], sh, IW);
1779 const HVX_VectorPred q1 = np > 1 ? conv2d_hmx_win_fold_q(&pc[1], sh, IW) : q0;
1780 const HVX_VectorPred q2 = np > 2 ? conv2d_hmx_win_fold_q(&pc[2], sh, IW) : q0;
1781 const HVX_VectorPred q3 = np > 3 ? conv2d_hmx_win_fold_q(&pc[3], sh, IW) : q0;
1782 const int32_t f0 = pc[0].off + sh;
1783 const int32_t f1 = np > 1 ? pc[1].off + sh : f0;
1784 const int32_t f2 = np > 2 ? pc[2].off + sh : f0;
1785 const int32_t f3 = np > 3 ? pc[3].off + sh : f0;
1786
1787 for (uint32_t c = 0; c < Cin; c++, ch++) {
1788 const float * r = raw + (size_t) c * RP;
1789 HVX_Vector x = Q6_V_vmux_QVV(q0, hvx_vmemu(r + f0), zero);
1790 if (np > 1) {
1791 x = Q6_V_vmux_QVV(q1, hvx_vmemu(r + f1), x);
1792 }
1793 if (np > 2) {
1794 x = Q6_V_vmux_QVV(q2, hvx_vmemu(r + f2), x);
1795 }
1796 if (np > 3) {
1797 x = Q6_V_vmux_QVV(q3, hvx_vmemu(r + f3), x);
1798 }
1799 if (!(ch & 1)) {
1800 pa = x;
1801 continue;
1802 }
1803 const HVX_Vector y = conv2d_f32_to_f16(pa, x);
1804 if (scat) {
1805 Q6_vscatter_QRMVwV(Q6_Q_vsetq2_R(VLEN), (size_t) dst, HMX_FP16_TILE_SIZE - 1,
1806 Q6_Vw_vadd_VwVw(soff, Q6_V_vsplat_R(4 * (ch - 1))), y);
1807 } else {
1808 d[ch / 2] = y;
1809 }
1810 }
1811 }
1812 if (ch & 1) {
1813 const HVX_Vector y = conv2d_f32_to_f16(pa, zero);
1814 if (scat) {
1815 Q6_vscatter_QRMVwV(Q6_Q_vsetq2_R(VLEN / 2), (size_t) dst, HMX_FP16_TILE_SIZE - 1,
1816 Q6_Vw_vadd_VwVw(soff, Q6_V_vsplat_R(4 * (ch - 1))), y);
1817 } else {
1818 d[ch / 2] = y;
1819 }
1820 ch++;
1821 }
1822 if (!scat) {
1823 for (uint32_t k = ch / 2; k < 16; k++) {
1824 d[k] = zero;
1825 }
1826 }
1827 if (scat) {
1828 continue;
1829 }
1830
1831 HVX_Vector y[16];
1832 #pragma unroll
1833 for (uint32_t k = 0; k < 16; k++) {
1834 y[k] = d[k];
1835 }
1836 conv2d_xpose_w16(y);
1837 #pragma unroll
1838 for (uint32_t v = 0; v < 16; v++) {
1839 d[v] = y[conv2d_xpose_slot(v)];
1840 }
1841 }
1842 if (wc->n_scat && nb > 0) {
1843 hvx_gather_sync(act + (size_t) (bo + nb - 1) * HMX_FP16_TILE_N_ELMS);
1844 }
1845}
1846
1847// Fold, cwhn with d0 = 1: the KW * Cin folded channels of a pixel are contiguous in the raw row
1848static void conv2d_hmx_win_croutons_fold_cwhn(const struct htp_conv2d_win_context * wc, const float * stage, __fp16 * act, uint32_t bo,
1849 uint32_t nb, uint32_t ua) {
1850 const struct htp_ops_context * octx = wc->octx;
1851 const struct htp_conv2d_kernel_params * kp = wc->kp;
1852 const struct htp_tensor * input = octx->src[1];
1853
1854 const int32_t IW = input->ne[0];
1855 const int32_t IH = input->ne[1];
1856 const int32_t Cin = input->ne[2];
1857 const uint32_t N = input->ne[3];
1858 const int32_t KW = octx->src[0]->ne[0];
1859 const uint32_t SR = kp->window.sr;
1860 const uint32_t IHS = kp->window.ihs;
1861 const int32_t p0 = octx->op_params[2];
1862 const int32_t p1 = octx->op_params[3];
1863 const float * raw = conv2d_hmx_win_fold_raw((float *) stage);
1864
1865 const uint32_t vr_a = fastdiv(ua, &kp->window.div_sr);
1866 const HVX_Vector zero = Q6_V_vzero();
1867
1868 uint32_t vr = vr_a;
1869 uint32_t ow = ua - vr * SR;
1870 bool ok = false;
1871 for (uint32_t i = 0; i < nb; i++) {
1872 HVX_Vector * dst = (HVX_Vector *) (act + (size_t) (bo + i) * HMX_FP16_TILE_N_ELMS);
1873 HVX_Vector pa = zero;
1874 for (uint32_t j = 0; j < 32; j++) {
1875 if (ow == 0 || (i == 0 && j == 0)) {
1876 const uint32_t n = fastdiv(vr, &kp->window.div_ihs);
1877 const int32_t ih = (int32_t) (vr - n * IHS) - p1;
1878 ok = n < N && ih >= 0 && ih < IH;
1879 }
1880 HVX_Vector x = zero;
1881 if (ok) {
1882 // folded channels kw * Cin + c read input column ow + kw - p0, valid for kw in [p0 - ow, p0 + IW - ow)
1883 const int32_t lo = MAX(0, p0 - (int32_t) ow) * Cin;
1884 const int32_t hi = MIN(KW, p0 + IW - (int32_t) ow) * Cin;
1885 if (hi > lo) {
1886 const float * src = raw + ((int32_t) ((vr - vr_a) * IW + ow) - p0) * Cin;
1887 const HVX_VectorPred q = Q6_Q_and_QQn(Q6_Q_vsetq2_R(hi * sizeof(float)), Q6_Q_vsetq_R(lo * sizeof(float)));
1888 x = Q6_V_vmux_QVV(q, hvx_vmemu(src), zero);
1889 }
1890 }
1891 if (j & 1) {
1892 dst[j / 2] = conv2d_f32_to_f16_shuff(pa, x);
1893 } else {
1894 pa = x;
1895 }
1896 if (++ow == SR) {
1897 ow = 0;
1898 vr++;
1899 }
1900 }
1901 }
1902}
1903
1904static void conv2d_hmx_win_pack(const struct htp_conv2d_win_context * wc, unsigned int nth, unsigned int ith) {
1905 struct htp_ops_context * octx = wc->octx;
1906 const struct htp_conv2d_kernel_params * kp = wc->kp;
1907
1908 uint32_t ts;
1909 const uint32_t nt = conv2d_hmx_win_chunk_tiles(wc, wc->pack_chunk, &ts);
1910 const uint32_t ncb = MIN(kp->window.n_bc, nt + (kp->window.max_off >> 5) + 1);
1911 const uint32_t bpt = (ncb + nth - 1) / nth;
1912 const uint32_t b0 = MIN(ith * bpt, ncb);
1913 const uint32_t b1 = MIN(b0 + bpt, ncb);
1914
1915 dma_queue * dmaq = octx->ctx->dma[ith];
1916 uint8_t * thr = wc->thr_base + (size_t) ith * kp->thr_size;
1917 float * stages[2] = { (float *) thr, (float *) (thr + kp->stage_size) };
1918 __fp16 * act = wc->act[wc->pack_chunk & 1];
1919
1920 uint32_t (*stage_fn)(const struct htp_conv2d_win_context *, dma_queue *, float *, uint32_t, uint32_t) =
1921 kp->window.fold ? conv2d_hmx_win_stage_fold : conv2d_hmx_win_stage;
1922
1923 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
1924
1925 const uint32_t SB = kp->window.seg_b;
1926
1927 if (b0 >= b1) {
1928 return;
1929 }
1930
1931 // push(N), push(N + 1), pop(N), croutons(N), push(N + 2), ...
1932 uint32_t nd[2] = { 0, 0 };
1933 for (uint32_t i = 0; i < 2 && b0 + i * SB < b1; i++) {
1934 const uint32_t b = b0 + i * SB;
1935 nd[i] += stage_fn(wc, dmaq, stages[i], (ts + b) * 32, (ts + MIN(b + SB, b1)) * 32);
1936 }
1937
1938 uint32_t si = 0;
1939 for (uint32_t b = b0; b < b1; b += SB) {
1940 const uint32_t nb = MIN(SB, b1 - b);
1941
1942 for (uint32_t p = 0; p < nd[si]; p++) {
1943 dma_queue_pop(dmaq);
1944 }
1945
1946 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) b);
1947 if (kp->window.fold && kp->cwhn) {
1948 conv2d_hmx_win_croutons_fold_cwhn(wc, stages[si], act, b, nb, (ts + b) * 32);
1949 } else if (kp->window.fold) {
1950 conv2d_hmx_win_croutons_fold(wc, stages[si], act, b, nb, (ts + b) * 32);
1951 } else {
1952 conv2d_hmx_win_croutons(wc, stages[si], act, b, nb);
1953 }
1954 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) b);
1955
1956 const uint32_t bn = b + 2 * SB;
1957 nd[si] = bn < b1 ? stage_fn(wc, dmaq, stages[si], (ts + bn) * 32, (ts + MIN(bn + SB, b1)) * 32) : 0;
1958 si ^= 1;
1959 }
1960}
1961
1962static void conv2d_hmx_win_unpack(const struct htp_conv2d_win_context * wc, unsigned int nth, unsigned int ith) {
1963 struct htp_ops_context * octx = wc->octx;
1964 const struct htp_conv2d_kernel_params * kp = wc->kp;
1965 const struct htp_tensor * dst = octx->dst;
1966
1967 const uint32_t OW = dst->ne[0];
1968 const uint32_t OH = dst->ne[1];
1969 const uint32_t Cout = dst->ne[2];
1970 const uint32_t SR = kp->window.sr;
1971 const uint32_t IHS = kp->window.ihs;
1972 const uint32_t ST = kp->window.seg_t;
1973 const uint32_t OS = ST * 32;
1974
1975 uint32_t ts;
1976 const uint32_t nt = conv2d_hmx_win_chunk_tiles(wc, wc->unpack_chunk, &ts);
1977 const uint32_t tpt = (nt + nth - 1) / nth;
1978 const uint32_t i0 = MIN(ith * tpt, nt);
1979 const uint32_t i1 = MIN(i0 + tpt, nt);
1980
1981 dma_queue * dmaq = octx->ctx->dma[ith];
1982 uint8_t * thr = wc->thr_base + (size_t) ith * kp->thr_size + 2 * (size_t) kp->stage_size;
1983 float * obuf = (float *) thr;
1984 __fp16 * out = wc->out[wc->unpack_chunk & 1];
1985
1986 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
1987
1988 // nd[bi] = DMAs pushed from obuf bi, popped before it is reused HTP_CONV2D_WIN_OBUF steps later
1989 uint32_t nd[HTP_CONV2D_WIN_OBUF] = { 0 };
1990 uint32_t bi = 0;
1991
1992 const bool rows_contig = SR == OW && dst->nb[1] == OW * sizeof(float);
1993
1994 for (uint32_t i = i0; i < i1; i += ST) {
1995 const uint32_t ni = MIN(ST, i1 - i);
1996 const uint32_t q_lo = (ts + i) * 32;
1997 const uint32_t q_hi = q_lo + ni * 32;
1998
1999 for (uint32_t rt = 0; rt < wc->n_rt; rt++) {
2000 const uint32_t co0 = (wc->rt0 + rt) * 32;
2001 const uint32_t nco = MIN(32, Cout - co0);
2002 const uint32_t npr = (nco + 1) / 2;
2003 const HVX_Vector restore = conv2d_hmx_rt_restore(wc->scales, rt);
2004
2005 for (uint32_t p = 0; p < nd[bi]; p++) {
2006 dma_queue_pop(dmaq);
2007 }
2008
2009 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) i);
2010 float * ob = obuf + (size_t) bi * 32 * OS;
2011 for (uint32_t g = 0; g < ni; g++) {
2012 HVX_Vector * tv = (HVX_Vector *) (out + ((size_t) rt * kp->ct_chunk + i + g) * HMX_FP16_TILE_N_ELMS);
2013 HVX_Vector y[16];
2014 #pragma unroll
2015 for (uint32_t v = 0; v < 16; v++) {
2016 y[conv2d_xpose_slot(v)] = tv[v];
2017 }
2018 conv2d_unxpose_w16(y);
2019 // rows go back in place and are converted by a second loop, both in one loop spill vector registers
2020 #pragma unroll
2021 for (uint32_t k = 0; k < 16; k++) {
2022 tv[k] = y[k];
2023 }
2024 HVX_Vector * o = (HVX_Vector *) (ob + g * 32);
2025 for (uint32_t k = 0; k < npr; k++) {
2026 const HVX_VectorPair pr = hvx_vec_f16_to_f32(tv[k]);
2027 o[(2 * k + 0) * (OS / 32)] = hvx_vec_mul_f32_f32(Q6_V_lo_W(pr), restore);
2028 o[(2 * k + 1) * (OS / 32)] = hvx_vec_mul_f32_f32(Q6_V_hi_W(pr), restore);
2029 }
2030 }
2031 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) i);
2032
2033 // write the valid part of each virtual row in [q_lo, q_hi)
2034 nd[bi] = 0;
2035 uint32_t vr = fastdiv(q_lo, &kp->window.div_sr);
2036 for (; vr * SR < q_hi; vr++) {
2037 const uint32_t n = fastdiv(vr, &kp->window.div_ihs);
2038 const uint32_t oh = vr - n * IHS;
2039 if (vr < wc->vr0 || vr >= wc->vr1 || oh >= OH) {
2040 continue;
2041 }
2042 const uint32_t base = vr * SR;
2043 const uint32_t a = q_lo > base ? q_lo - base : 0;
2044 uint32_t b = MIN(OW, q_hi - base);
2045 if (b <= a) {
2046 continue;
2047 }
2048 const dma_addr_t ddr = dst->data + (size_t) n * dst->nb[3] + (size_t) oh * dst->nb[1] + (size_t) co0 * dst->nb[2] + (size_t) a * sizeof(float);
2049
2050 // no gap between rows: extend over the following rows of this image
2051 if (rows_contig) {
2052 uint32_t r = 1;
2053 while (oh + r < OH && vr + r < wc->vr1 && (vr + r) * SR < q_hi) {
2054 r++;
2055 }
2056 b = MIN(q_hi - base, (r - 1) * SR + OW);
2057 vr += r - 1;
2058 }
2059 dma_queue_push(dmaq, dma_make_data(ddr, ob + (base + a - q_lo)), dst->nb[2], (size_t) OS * sizeof(float),
2060 (size_t) (b - a) * sizeof(float), nco);
2061 nd[bi]++;
2062 }
2063 bi = bi + 1 == HTP_CONV2D_WIN_OBUF ? 0 : bi + 1;
2064 }
2065 }
2066
2067 for (uint32_t k = 0; k < HTP_CONV2D_WIN_OBUF; k++) {
2068 for (uint32_t p = 0; p < nd[k]; p++) {
2069 dma_queue_pop(dmaq);
2070 }
2071 }
2072}
2073
2074// per-thread callback for one pipeline phase
2075static void conv2d_hmx_win_phase_thread(unsigned int nth, unsigned int ith, void * data) {
2076 const struct htp_conv2d_win_context * wc = (const struct htp_conv2d_win_context *) data;
2077
2078 if (wc->unpack_chunk >= 0) {
2079 conv2d_hmx_win_unpack(wc, nth, ith);
2080 }
2081 // weights of this rt block are packed before its first chunk
2082 if (wc->pack_chunk == 0) {
2083 const struct htp_tensor * kernel = wc->octx->src[0];
2084 const uint32_t n_wt = (wc->kp->window.fold ? kernel->ne[1] : kernel->ne[0] * kernel->ne[1]) * wc->kp->window.n_cc;
2085 for (uint32_t rt = ith; rt < wc->n_rt; rt += nth) {
2086 conv2d_hmx_rt_scale(wc->scales + rt * CONV2D_HMX_SCALES_N, wc->w_tiles + (size_t) rt * n_wt * HMX_FP16_TILE_N_ELMS, n_wt,
2087 kernel->ne[0] * kernel->ne[1] * kernel->ne[2]);
2088 }
2089 }
2090 if (wc->pack_chunk >= 0) {
2091 conv2d_hmx_win_pack(wc, nth, ith);
2092 }
2093}
2094
2095// zero out 2 act buffer regions in VTCM before packing
2096static void conv2d_hmx_win_zero_thread(unsigned int nth, unsigned int ith, void * data) {
2097 const struct htp_conv2d_win_context * wc = (const struct htp_conv2d_win_context *) data;
2098
2099 const size_t n = 2 * wc->layout.act_size / sizeof(float);
2100 const size_t per = hex_round_up((n + nth - 1) / nth, VLEN_FP32);
2101 const size_t i0 = MIN((size_t) ith * per, n);
2102 const size_t i1 = MIN(i0 + per, n);
2103
2104 struct htp_thread_trace * tr = &wc->octx->ctx->trace[ith];
2105 const uint16_t block = (uint16_t) (i0 / VLEN_FP32);
2106 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, block);
2107 if (i1 > i0) {
2108 hvx_splat_f32_a((uint8_t *) wc->act[0] + i0 * sizeof(float), 0.0f, i1 - i0);
2109 }
2110 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, block);
2111}
2112
2113static void conv2d_hmx_win_run_phase(struct htp_conv2d_win_context * wc, int32_t pack, int32_t unpack) {
2114 wc->pack_chunk = pack;
2115 wc->unpack_chunk = unpack;
2116 work_queue_run(wc->octx->ctx->work_queue, conv2d_hmx_win_phase_thread, wc, wc->kp->n_threads);
2117}
2118
2119
2120// live[i] = tile ts + i has a row that is written. Dead tiles lie inside the rows [OH, IHS) of an image.
2121static void conv2d_hmx_win_live(const struct htp_conv2d_win_context * wc, uint8_t * live, uint32_t ts, uint32_t nt) {
2122 const struct htp_conv2d_kernel_params * kp = wc->kp;
2123
2124 const uint32_t SR = kp->window.sr;
2125 const uint32_t IHS = kp->window.ihs;
2126 const uint32_t OH = wc->octx->dst->ne[1];
2127 const uint32_t q0 = ts * 32;
2128 const uint32_t q1 = (ts + nt) * 32;
2129
2130 memset(live, 1, nt);
2131 for (uint32_t n = fastdiv(fastdiv(q0, &kp->window.div_sr), &kp->window.div_ihs); n * IHS * SR < q1; n++) {
2132 const uint32_t g0 = MAX((n * IHS + OH) * SR, q0);
2133 const uint32_t g1 = MIN((n + 1) * IHS * SR, q1);
2134 const uint32_t t0 = (g0 + 31) / 32;
2135 const uint32_t t1 = g1 / 32;
2136 if (t1 > t0) {
2137 memset(live + (t0 - ts), 0, t1 - t0);
2138 }
2139 }
2140}
2141
2142static int conv2d_hmx_win(struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp) {
2143 const struct htp_tensor * kernel = octx->src[0];
2144 const struct htp_tensor * dst = octx->dst;
2145
2146 const uint32_t KW = kernel->ne[0];
2147 const uint32_t KH = kernel->ne[1];
2148 const uint32_t OW = dst->ne[0];
2149 const uint32_t OH = dst->ne[1];
2150 const uint32_t SR = kp->window.sr;
2151 const uint32_t IHS = kp->window.ihs;
2152 const uint32_t d0 = octx->op_params[4];
2153 const uint32_t d1 = octx->op_params[5];
2154
2155 uint32_t row_base, n_rows;
2156 conv2d_mdev_rows(octx, &row_base, &n_rows);
2157 if (n_rows == 0) {
2158 return HTP_STATUS_OK;
2159 }
2160
2161 const uint32_t ra = row_base;
2162 const uint32_t rb = row_base + n_rows - 1;
2163 const uint32_t vr0 = (ra / OH) * IHS + ra % OH;
2164 const uint32_t vr1 = (rb / OH) * IHS + rb % OH + 1;
2165
2166 const size_t tile = 2048;
2167 const uint32_t n_wt = kp->window.fold ? KH : (KW * KH);
2168 const size_t w_size = (size_t) kp->rt_block * n_wt * kp->window.n_cc * tile;
2169 const size_t act_size = (size_t) kp->window.n_cc * kp->window.n_bc * tile;
2170 const size_t out_size = (size_t) kp->rt_block * kp->ct_chunk * tile;
2171
2172 struct htp_conv2d_vtcm_layout layout;
2173 htp_conv2d_vtcm_layout_build(&layout, kp->rt_block, w_size, act_size, out_size, kp->thr_size, kp->n_threads);
2174
2175 uint8_t * base = (uint8_t *) octx->ctx->vtcm_base;
2176
2177 struct htp_conv2d_win_context wctx = {
2178 .octx = octx,
2179 .kp = kp,
2180 .layout = layout,
2181 .scales = VTCM_LAYOUT_PTR(__fp16, base, layout.off_scales),
2182 .w_tiles = VTCM_LAYOUT_PTR(__fp16, base, layout.off_w),
2183 .act = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[1]) },
2184 .out = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[1]) },
2185 .thr_base = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_thr),
2186 .t_base = (vr0 * SR) / 32,
2187 .t_end = ((vr1 - 1) * SR + OW + 31) / 32,
2188 .vr0 = vr0,
2189 .vr1 = vr1,
2190 };
2191
2192 // small Cin: croutons are built by scattering the staged values into zeroed croutons
2193 const uint32_t n_ch = octx->src[1]->ne[2] * (kp->window.fold ? KW : 1);
2194 if (!kp->cwhn && n_ch <= 2 * CONV2D_WIN_SCAT_MAX) {
2195 wctx.n_scat = (n_ch + 1) / 2;
2196 work_queue_run(octx->ctx->work_queue, conv2d_hmx_win_zero_thread, &wctx, kp->n_threads);
2197 }
2198
2199 const int32_t n_chunks = (int32_t) ((wctx.t_end - wctx.t_base + kp->ct_chunk - 1) / kp->ct_chunk);
2200 const uint32_t rem = n_ch - (kp->window.n_cc - 1) * 32;
2201
2202 // fold: kw is in the channels, taps are kh only
2203 const uint32_t TW = kp->window.fold ? 1 : KW;
2204 uint32_t * taps = (uint32_t *) octx->ctx->ddr_spad_base;
2205 for (uint32_t kh = 0; kh < KH; kh++) {
2206 for (uint32_t kw = 0; kw < TW; kw++) {
2207 taps[kh * TW + kw] = conv2d_hmx_win_act_off(kh * d1 * SR + kw * d0);
2208 }
2209 }
2210
2211 wctx.live[0] = (uint8_t *) octx->ctx->ddr_spad_base + hex_round_up(KH * KW * sizeof(uint32_t), 128);
2212 wctx.live[1] = wctx.live[0] + hex_round_up(kp->ct_chunk, 128);
2213
2214 struct htp_conv2d_win_task task = {
2215 .scales = wctx.scales,
2216 .taps = taps,
2217 .n_taps = TW * KH,
2218 .out_stride = kp->ct_chunk * HMX_FP16_TILE_N_ELMS,
2219 .n_cc = kp->window.n_cc,
2220 .plane = kp->window.n_bc * HMX_FP16_TILE_SIZE,
2221 .rt_last = kp->window.act_rt_last,
2222 .wr_last = (hex_round_up(rem, 8) / 2) * VLEN - 1,
2223 };
2224
2225 for (uint32_t rt0 = 0; rt0 < kp->n_row_tiles; rt0 += kp->rt_block) {
2226 wctx.rt0 = rt0;
2227 wctx.n_rt = MIN(kp->rt_block, kp->n_row_tiles - rt0);
2228 work_queue_run(octx->ctx->work_queue, conv2d_hmx_win_wpack_thread, &wctx, kp->n_threads);
2229
2230 conv2d_hmx_win_run_phase(&wctx, 0, -1);
2231
2232 for (int32_t c = 0; c < n_chunks; c++) {
2233 uint32_t ts;
2234 const uint32_t nt = conv2d_hmx_win_chunk_tiles(&wctx, c, &ts);
2235 conv2d_hmx_win_live(&wctx, wctx.live[c & 1], ts, nt);
2236 task.live = wctx.live[c & 1];
2237 task.out = wctx.out[c & 1];
2238 task.w = wctx.w_tiles;
2239 task.act = wctx.act[c & 1];
2240 task.n_rt = wctx.n_rt;
2241 task.n_tiles = nt;
2242 hmx_queue_push(octx->ctx->hmx_queue, hmx_queue_make_desc(conv2d_hmx_win_worker, &task));
2243
2244 const int32_t pack = c + 1 < n_chunks ? c + 1 : -1;
2245 const int32_t unpack = c - 1;
2246 if (pack >= 0 || unpack >= 0) {
2247 conv2d_hmx_win_run_phase(&wctx, pack, unpack);
2248 }
2249
2250 hmx_queue_pop(octx->ctx->hmx_queue);
2251 }
2252
2253 conv2d_hmx_win_run_phase(&wctx, -1, n_chunks - 1);
2254 }
2255
2256 return HTP_STATUS_OK;
2257}
2258
2259int op_conv_2d(struct htp_ops_context * octx) {
2260 const struct htp_conv2d_kernel_params * kparams =
2261 (const struct htp_conv2d_kernel_params *) octx->kernel_params;
2262
2263 if (kparams->total_rows == 0) {
2264 return HTP_STATUS_OK;
2265 }
2266
2267 if (kparams->kernel_type == HTP_CONV2D_KERNEL_HMX_WINDOW) {
2268 return conv2d_hmx_win(octx, kparams);
2269 }
2270 if (kparams->kernel_type == HTP_CONV2D_KERNEL_HMX_TILED) {
2271 return conv2d_hmx_tiled(octx, kparams);
2272 }
2273
2274 return HTP_STATUS_NO_SUPPORT;
2275}
2276
2277struct conv2d_dw_task_slice {
2278 dma_addr_t src_addr;
2279 dma_addr_t dst_addr;
2280 uint32_t src_bytes;
2281 uint32_t dst_bytes;
2282 uint32_t ih_count;
2283 uint32_t top_pad;
2284 uint32_t bot_pad;
2285 uint32_t oh_start;
2286 uint32_t oh_len;
2287 int32_t ih_first;
2288 uint32_t c;
2289};
2290
2291static inline void conv2d_dw_get_slice(
2292 struct conv2d_dw_task_slice * slice,
2293 uint32_t task,
2294 const struct htp_tensor * src1,
2295 const struct htp_tensor * dst,
2296 const struct htp_conv2d_dw_kernel_params * kp) {
2297
2298 uint32_t chunk_idx;
2299 uint32_t n;
2300 uint32_t c = 0;
2301
2302 if (kp->cwhn) {
2303 n = fastdiv(task, &kp->div_n_chunks);
2304 chunk_idx = task - n * kp->n_chunks;
2305 } else {
2306 const uint32_t plane = fastdiv(task, &kp->div_n_chunks);
2307 chunk_idx = task - plane * kp->n_chunks;
2308 n = fastdiv(plane, &kp->div_c);
2309 c = plane - n * kp->c;
2310 }
2311
2312 const uint32_t oh_start = chunk_idx * kp->oh_chunk;
2313 const uint32_t oh_len = MIN(kp->oh_chunk, kp->oh - oh_start);
2314
2315 const int32_t ih_min = (int32_t)(oh_start * kp->s1) - kp->p1;
2316 const int32_t ih_max = (int32_t)((oh_start + oh_len - 1) * kp->s1 + (kp->kh - 1) * kp->d1) - kp->p1;
2317 const uint32_t ih_span = (uint32_t)(ih_max - ih_min + 1);
2318
2319 const int32_t ih_first = MAX(0, ih_min);
2320 const int32_t ih_last = MIN((int32_t)kp->h - 1, ih_max);
2321 const uint32_t ih_count = (ih_first <= ih_last) ? (uint32_t)(ih_last - ih_first + 1) : 0;
2322 const uint32_t top_pad = (ih_count > 0) ? (uint32_t)(ih_first - ih_min) : ih_span;
2323 const uint32_t bot_pad = ih_span - top_pad - ih_count;
2324
2325 slice->src_bytes = ih_count * kp->row_src_bytes;
2326 slice->dst_bytes = oh_len * kp->row_dst_bytes;
2327 slice->ih_count = ih_count;
2328 slice->top_pad = top_pad;
2329 slice->bot_pad = bot_pad;
2330 slice->oh_start = oh_start;
2331 slice->oh_len = oh_len;
2332 slice->ih_first = ih_first;
2333 slice->c = c;
2334
2335 if (kp->cwhn) {
2336 slice->src_addr = src1->data + (size_t) n * src1->nb[3] + (size_t) ih_first * src1->nb[1];
2337 slice->dst_addr = dst->data + (size_t) n * dst->nb[3] + (size_t) oh_start * dst->nb[1];
2338 } else {
2339 slice->src_addr = src1->data + (size_t) n * src1->nb[3] + (size_t) c * src1->nb[2] + (size_t) ih_first * src1->nb[1];
2340 slice->dst_addr = dst->data + (size_t) n * dst->nb[3] + (size_t) c * dst->nb[2] + (size_t) oh_start * dst->nb[1];
2341 }
2342}
2343
2344static inline HVX_Vector conv2d_dw_splat_w_f32(const void * ptr) {
2345 return hvx_vec_splat_f32(*(const float *) ptr);
2346}
2347
2348static inline HVX_Vector conv2d_dw_splat_w_f16(const void * ptr) {
2349 const HVX_Vector vh = Q6_Vh_vsplat_R(*(const uint16_t *) ptr);
2350 const HVX_Vector v1 = Q6_Vh_vsplat_R(0x3c00);
2351 return Q6_Vsf_equals_Vqf32(Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(vh, v1)));
2352}
2353
2354static inline HVX_Vector conv2d_dw_load_w_f32(const void * ptr) {
2355 return hvx_vmemu(ptr);
2356}
2357
2358static inline HVX_Vector conv2d_dw_load_w_f16(const void * ptr) {
2359 return Q6_V_lo_W(hvx_vec_f16_to_f32(hvx_vmemu(ptr)));
2360}
2361
2362static inline HVX_Vector conv2d_dw_qf32_mpy_add3(
2363 HVX_Vector acc,
2364 HVX_Vector x0, HVX_Vector w0,
2365 HVX_Vector x1, HVX_Vector w1,
2366 HVX_Vector x2, HVX_Vector w2) {
2367 const HVX_Vector p01 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x0, w0), Q6_Vqf32_vmpy_VsfVsf(x1, w1));
2368 const HVX_Vector p2 = Q6_Vqf32_vadd_Vqf32Vqf32(acc, Q6_Vqf32_vmpy_VsfVsf(x2, w2));
2369 return Q6_Vqf32_vadd_Vqf32Vqf32(p01, p2);
2370}
2371
2372static inline void conv2d_dw_pad_vtcm_k3x3(
2373 float * in_vtcm,
2374 const struct conv2d_dw_task_slice * slice,
2375 uint32_t w_in,
2376 uint32_t extra_bot_rows) {
2377 if (slice->top_pad > 0) {
2378 hvx_splat_f32_a(in_vtcm, 0.0f, slice->top_pad * w_in);
2379 }
2380 const uint32_t tail_zero_rows = slice->bot_pad + extra_bot_rows;
2381 if (tail_zero_rows > 0) {
2382 hvx_splat_f32_a(in_vtcm + (size_t) (slice->top_pad + slice->ih_count) * w_in, 0.0f, tail_zero_rows * w_in);
2383 }
2384}
2385
2386#define DEFINE_CONV2D_DW_WHCN_S1_K3X3(SUFFIX, SPLAT_W_FN) \
2387__attribute__((noinline)) \
2388static void conv2d_dw_whcn_s1_k3x3_##SUFFIX( \
2389 const float * restrict in_vtcm, \
2390 float * restrict out_vtcm, \
2391 const void * w_vtcm, \
2392 const struct conv2d_dw_task_slice * slice, \
2393 const struct htp_ops_context * octx, \
2394 const struct htp_conv2d_dw_kernel_params * kp) { \
2395 \
2396 const struct htp_tensor * src0 = octx->src[0]; \
2397 const size_t w_nb0 = src0->nb[0]; \
2398 const size_t w_nb1 = src0->nb[1]; \
2399 const char * restrict w_chan = (const char *) w_vtcm + (size_t) slice->c * src0->nb[3]; \
2400 const uint32_t oh_len = slice->oh_len; \
2401 const uint32_t w_in = kp->w; \
2402 \
2403 conv2d_dw_pad_vtcm_k3x3((float *) in_vtcm, slice, w_in, 0); \
2404 \
2405 const HVX_Vector w00 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 0 * w_nb0); \
2406 const HVX_Vector w01 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 1 * w_nb0); \
2407 const HVX_Vector w02 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 2 * w_nb0); \
2408 const HVX_Vector w10 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 0 * w_nb0); \
2409 const HVX_Vector w11 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 1 * w_nb0); \
2410 const HVX_Vector w12 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 2 * w_nb0); \
2411 const HVX_Vector w20 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 0 * w_nb0); \
2412 const HVX_Vector w21 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 1 * w_nb0); \
2413 const HVX_Vector w22 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 2 * w_nb0); \
2414 const HVX_Vector zero = Q6_V_vzero(); \
2415 \
2416 uint32_t ohi = 0; \
2417 for (; ohi + 4 <= oh_len; ohi += 4) { \
2418 const float * restrict r0 = in_vtcm + (size_t) ohi * w_in; \
2419 const float * restrict r1 = r0 + w_in; \
2420 const float * restrict r2 = r1 + w_in; \
2421 const float * restrict r3 = r2 + w_in; \
2422 const float * restrict r4 = r3 + w_in; \
2423 const float * restrict r5 = r4 + w_in; \
2424 \
2425 float * restrict out0 = out_vtcm + (size_t) (ohi + 0) * w_in; \
2426 float * restrict out1 = out0 + w_in; \
2427 float * restrict out2 = out1 + w_in; \
2428 float * restrict out3 = out2 + w_in; \
2429 \
2430 HVX_Vector p0 = zero, c0 = hvx_vmem(r0); \
2431 HVX_Vector p1 = zero, c1 = hvx_vmem(r1); \
2432 HVX_Vector p2 = zero, c2 = hvx_vmem(r2); \
2433 HVX_Vector p3 = zero, c3 = hvx_vmem(r3); \
2434 HVX_Vector p4 = zero, c4 = hvx_vmem(r4); \
2435 HVX_Vector p5 = zero, c5 = hvx_vmem(r5); \
2436 \
2437 for (uint32_t ox = 0; ox < w_in; ox += 32) { \
2438 const bool has_next = (ox + 32 < w_in); \
2439 \
2440 const HVX_Vector n0 = has_next ? hvx_vmem(r0 + ox + 32) : zero; \
2441 const HVX_Vector l0 = Q6_V_valign_VVR(c0, p0, 124); \
2442 const HVX_Vector rg0 = Q6_V_valign_VVR(n0, c0, 4); \
2443 HVX_Vector acc0 = conv2d_dw_qf32_mpy_add3(zero, l0, w00, c0, w01, rg0, w02); \
2444 p0 = c0; c0 = n0; \
2445 \
2446 const HVX_Vector n1 = has_next ? hvx_vmem(r1 + ox + 32) : zero; \
2447 const HVX_Vector l1 = Q6_V_valign_VVR(c1, p1, 124); \
2448 const HVX_Vector rg1 = Q6_V_valign_VVR(n1, c1, 4); \
2449 acc0 = conv2d_dw_qf32_mpy_add3(acc0, l1, w10, c1, w11, rg1, w12); \
2450 HVX_Vector acc1 = conv2d_dw_qf32_mpy_add3(zero, l1, w00, c1, w01, rg1, w02); \
2451 p1 = c1; c1 = n1; \
2452 \
2453 const HVX_Vector n2 = has_next ? hvx_vmem(r2 + ox + 32) : zero; \
2454 const HVX_Vector l2 = Q6_V_valign_VVR(c2, p2, 124); \
2455 const HVX_Vector rg2 = Q6_V_valign_VVR(n2, c2, 4); \
2456 acc0 = conv2d_dw_qf32_mpy_add3(acc0, l2, w20, c2, w21, rg2, w22); \
2457 hvx_vmem(out0 + ox) = Q6_Vsf_equals_Vqf32(acc0); \
2458 acc1 = conv2d_dw_qf32_mpy_add3(acc1, l2, w10, c2, w11, rg2, w12); \
2459 HVX_Vector acc2 = conv2d_dw_qf32_mpy_add3(zero, l2, w00, c2, w01, rg2, w02); \
2460 p2 = c2; c2 = n2; \
2461 \
2462 const HVX_Vector n3 = has_next ? hvx_vmem(r3 + ox + 32) : zero; \
2463 const HVX_Vector l3 = Q6_V_valign_VVR(c3, p3, 124); \
2464 const HVX_Vector rg3 = Q6_V_valign_VVR(n3, c3, 4); \
2465 acc1 = conv2d_dw_qf32_mpy_add3(acc1, l3, w20, c3, w21, rg3, w22); \
2466 hvx_vmem(out1 + ox) = Q6_Vsf_equals_Vqf32(acc1); \
2467 acc2 = conv2d_dw_qf32_mpy_add3(acc2, l3, w10, c3, w11, rg3, w12); \
2468 HVX_Vector acc3 = conv2d_dw_qf32_mpy_add3(zero, l3, w00, c3, w01, rg3, w02); \
2469 p3 = c3; c3 = n3; \
2470 \
2471 const HVX_Vector n4 = has_next ? hvx_vmem(r4 + ox + 32) : zero; \
2472 const HVX_Vector l4 = Q6_V_valign_VVR(c4, p4, 124); \
2473 const HVX_Vector rg4 = Q6_V_valign_VVR(n4, c4, 4); \
2474 acc2 = conv2d_dw_qf32_mpy_add3(acc2, l4, w20, c4, w21, rg4, w22); \
2475 hvx_vmem(out2 + ox) = Q6_Vsf_equals_Vqf32(acc2); \
2476 acc3 = conv2d_dw_qf32_mpy_add3(acc3, l4, w10, c4, w11, rg4, w12); \
2477 p4 = c4; c4 = n4; \
2478 \
2479 const HVX_Vector n5 = has_next ? hvx_vmem(r5 + ox + 32) : zero; \
2480 const HVX_Vector l5 = Q6_V_valign_VVR(c5, p5, 124); \
2481 const HVX_Vector rg5 = Q6_V_valign_VVR(n5, c5, 4); \
2482 acc3 = conv2d_dw_qf32_mpy_add3(acc3, l5, w20, c5, w21, rg5, w22); \
2483 hvx_vmem(out3 + ox) = Q6_Vsf_equals_Vqf32(acc3); \
2484 p5 = c5; c5 = n5; \
2485 } \
2486 } \
2487 \
2488 for (; ohi < oh_len; ++ohi) { \
2489 const float * restrict r0 = in_vtcm + (size_t) ohi * w_in; \
2490 const float * restrict r1 = r0 + w_in; \
2491 const float * restrict r2 = r1 + w_in; \
2492 float * restrict out_row = out_vtcm + (size_t) ohi * w_in; \
2493 \
2494 HVX_Vector p0 = zero, c0 = hvx_vmem(r0); \
2495 HVX_Vector p1 = zero, c1 = hvx_vmem(r1); \
2496 HVX_Vector p2 = zero, c2 = hvx_vmem(r2); \
2497 \
2498 for (uint32_t ox = 0; ox < w_in; ox += 32) { \
2499 const bool has_next = (ox + 32 < w_in); \
2500 \
2501 const HVX_Vector n0 = has_next ? hvx_vmem(r0 + ox + 32) : zero; \
2502 const HVX_Vector l0 = Q6_V_valign_VVR(c0, p0, 124); \
2503 const HVX_Vector rg0 = Q6_V_valign_VVR(n0, c0, 4); \
2504 HVX_Vector acc = conv2d_dw_qf32_mpy_add3(zero, l0, w00, c0, w01, rg0, w02); \
2505 p0 = c0; c0 = n0; \
2506 \
2507 const HVX_Vector n1 = has_next ? hvx_vmem(r1 + ox + 32) : zero; \
2508 const HVX_Vector l1 = Q6_V_valign_VVR(c1, p1, 124); \
2509 const HVX_Vector rg1 = Q6_V_valign_VVR(n1, c1, 4); \
2510 acc = conv2d_dw_qf32_mpy_add3(acc, l1, w10, c1, w11, rg1, w12); \
2511 p1 = c1; c1 = n1; \
2512 \
2513 const HVX_Vector n2 = has_next ? hvx_vmem(r2 + ox + 32) : zero; \
2514 const HVX_Vector l2 = Q6_V_valign_VVR(c2, p2, 124); \
2515 const HVX_Vector rg2 = Q6_V_valign_VVR(n2, c2, 4); \
2516 acc = conv2d_dw_qf32_mpy_add3(acc, l2, w20, c2, w21, rg2, w22); \
2517 p2 = c2; c2 = n2; \
2518 \
2519 hvx_vmem(out_row + ox) = Q6_Vsf_equals_Vqf32(acc); \
2520 } \
2521 } \
2522}
2523
2524DEFINE_CONV2D_DW_WHCN_S1_K3X3(f32, conv2d_dw_splat_w_f32)
2525DEFINE_CONV2D_DW_WHCN_S1_K3X3(f16, conv2d_dw_splat_w_f16)
2526
2527#define DEFINE_CONV2D_DW_WHCN_S2_K3X3_W32(SUFFIX, SPLAT_W_FN) \
2528__attribute__((noinline)) \
2529static void conv2d_dw_whcn_s2_k3x3_w32_##SUFFIX( \
2530 const float * restrict in_vtcm, \
2531 float * restrict out_vtcm, \
2532 const void * w_vtcm, \
2533 const struct conv2d_dw_task_slice * slice, \
2534 const struct htp_ops_context * octx, \
2535 const struct htp_conv2d_dw_kernel_params * kp) { \
2536 \
2537 (void) kp; \
2538 const struct htp_tensor * src0 = octx->src[0]; \
2539 const size_t w_nb0 = src0->nb[0]; \
2540 const size_t w_nb1 = src0->nb[1]; \
2541 const char * restrict w_chan = (const char *) w_vtcm + (size_t) slice->c * src0->nb[3]; \
2542 const uint32_t oh_len = slice->oh_len; \
2543 \
2544 conv2d_dw_pad_vtcm_k3x3((float *) in_vtcm, slice, 32, (oh_len & 1) ? 2 : 0); \
2545 \
2546 const HVX_Vector w00 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 0 * w_nb0); \
2547 const HVX_Vector w01 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 1 * w_nb0); \
2548 const HVX_Vector w02 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 2 * w_nb0); \
2549 const HVX_Vector w10 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 0 * w_nb0); \
2550 const HVX_Vector w11 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 1 * w_nb0); \
2551 const HVX_Vector w12 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 2 * w_nb0); \
2552 const HVX_Vector w20 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 0 * w_nb0); \
2553 const HVX_Vector w21 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 1 * w_nb0); \
2554 const HVX_Vector w22 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 2 * w_nb0); \
2555 const HVX_Vector zero = Q6_V_vzero(); \
2556 \
2557 const HVX_VectorPred q_not_lane16 = Q6_Q_or_QQn(Q6_Q_vsetq_R(64), Q6_Q_vsetq2_R(68)); \
2558 const float * restrict in_row = in_vtcm; \
2559 HVX_Vector v_prev_b2 = hvx_vmem(in_row); \
2560 in_row += 32; \
2561 \
2562 for (uint32_t ohi = 0; ohi < oh_len; ohi += 2) { \
2563 const HVX_Vector v_a0 = v_prev_b2; \
2564 const HVX_Vector v_a1 = hvx_vmem(in_row); \
2565 const HVX_Vector v_a2 = hvx_vmem(in_row + 32); \
2566 const HVX_Vector v_b0 = v_a2; \
2567 const HVX_Vector v_b1 = hvx_vmem(in_row + 64); \
2568 const HVX_Vector v_b2 = hvx_vmem(in_row + 96); \
2569 in_row += 128; \
2570 v_prev_b2 = v_b2; \
2571 \
2572 const HVX_VectorPair vp0 = Q6_W_vdeal_VVR(v_b0, v_a0, -4); \
2573 const HVX_Vector ev0 = Q6_V_lo_W(vp0); \
2574 const HVX_Vector od0 = Q6_V_hi_W(vp0); \
2575 const HVX_Vector lf0 = Q6_V_vand_QV(q_not_lane16, Q6_V_valign_VVR(od0, zero, 124)); \
2576 HVX_Vector acc = conv2d_dw_qf32_mpy_add3(zero, lf0, w00, ev0, w01, od0, w02); \
2577 \
2578 const HVX_VectorPair vp1 = Q6_W_vdeal_VVR(v_b1, v_a1, -4); \
2579 const HVX_Vector ev1 = Q6_V_lo_W(vp1); \
2580 const HVX_Vector od1 = Q6_V_hi_W(vp1); \
2581 const HVX_Vector lf1 = Q6_V_vand_QV(q_not_lane16, Q6_V_valign_VVR(od1, zero, 124)); \
2582 acc = conv2d_dw_qf32_mpy_add3(acc, lf1, w10, ev1, w11, od1, w12); \
2583 \
2584 const HVX_VectorPair vp2 = Q6_W_vdeal_VVR(v_b2, v_a2, -4); \
2585 const HVX_Vector ev2 = Q6_V_lo_W(vp2); \
2586 const HVX_Vector od2 = Q6_V_hi_W(vp2); \
2587 const HVX_Vector lf2 = Q6_V_vand_QV(q_not_lane16, Q6_V_valign_VVR(od2, zero, 124)); \
2588 acc = conv2d_dw_qf32_mpy_add3(acc, lf2, w20, ev2, w21, od2, w22); \
2589 \
2590 hvx_vmem(out_vtcm + (size_t) ohi * 16) = Q6_Vsf_equals_Vqf32(acc); \
2591 } \
2592}
2593
2594DEFINE_CONV2D_DW_WHCN_S2_K3X3_W32(f32, conv2d_dw_splat_w_f32)
2595DEFINE_CONV2D_DW_WHCN_S2_K3X3_W32(f16, conv2d_dw_splat_w_f16)
2596
2597#define DEFINE_CONV2D_DW_WHCN_S2_K3X3_W64(SUFFIX, SPLAT_W_FN) \
2598__attribute__((noinline)) \
2599static void conv2d_dw_whcn_s2_k3x3_w64_##SUFFIX( \
2600 const float * restrict in_vtcm, \
2601 float * restrict out_vtcm, \
2602 const void * w_vtcm, \
2603 const struct conv2d_dw_task_slice * slice, \
2604 const struct htp_ops_context * octx, \
2605 const struct htp_conv2d_dw_kernel_params * kp) { \
2606 \
2607 (void) kp; \
2608 const struct htp_tensor * src0 = octx->src[0]; \
2609 const size_t w_nb0 = src0->nb[0]; \
2610 const size_t w_nb1 = src0->nb[1]; \
2611 const char * restrict w_chan = (const char *) w_vtcm + (size_t) slice->c * src0->nb[3]; \
2612 const uint32_t oh_len = slice->oh_len; \
2613 \
2614 conv2d_dw_pad_vtcm_k3x3((float *) in_vtcm, slice, 64, 0); \
2615 \
2616 const HVX_Vector w00 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 0 * w_nb0); \
2617 const HVX_Vector w01 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 1 * w_nb0); \
2618 const HVX_Vector w02 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 2 * w_nb0); \
2619 const HVX_Vector w10 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 0 * w_nb0); \
2620 const HVX_Vector w11 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 1 * w_nb0); \
2621 const HVX_Vector w12 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 2 * w_nb0); \
2622 const HVX_Vector w20 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 0 * w_nb0); \
2623 const HVX_Vector w21 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 1 * w_nb0); \
2624 const HVX_Vector w22 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 2 * w_nb0); \
2625 const HVX_Vector zero = Q6_V_vzero(); \
2626 \
2627 const float * restrict in_row = in_vtcm; \
2628 const HVX_VectorPair vp0_init = Q6_W_vdeal_VVR(hvx_vmem(in_row + 32), hvx_vmem(in_row), -4); \
2629 in_row += 64; \
2630 HVX_Vector ev0 = Q6_V_lo_W(vp0_init); \
2631 HVX_Vector od0 = Q6_V_hi_W(vp0_init); \
2632 HVX_Vector lf0 = Q6_V_valign_VVR(od0, zero, 124); \
2633 \
2634 for (uint32_t ohi = 0; ohi < oh_len; ++ohi) { \
2635 const float * restrict r1_ptr = in_row; \
2636 const float * restrict r2_ptr = in_row + 64; \
2637 in_row += 128; \
2638 \
2639 HVX_Vector acc = conv2d_dw_qf32_mpy_add3(zero, lf0, w00, ev0, w01, od0, w02); \
2640 \
2641 const HVX_VectorPair vp1 = Q6_W_vdeal_VVR(hvx_vmem(r1_ptr + 32), hvx_vmem(r1_ptr), -4); \
2642 const HVX_Vector ev1 = Q6_V_lo_W(vp1); \
2643 const HVX_Vector od1 = Q6_V_hi_W(vp1); \
2644 const HVX_Vector lf1 = Q6_V_valign_VVR(od1, zero, 124); \
2645 acc = conv2d_dw_qf32_mpy_add3(acc, lf1, w10, ev1, w11, od1, w12); \
2646 \
2647 const HVX_VectorPair vp2 = Q6_W_vdeal_VVR(hvx_vmem(r2_ptr + 32), hvx_vmem(r2_ptr), -4); \
2648 const HVX_Vector ev2 = Q6_V_lo_W(vp2); \
2649 const HVX_Vector od2 = Q6_V_hi_W(vp2); \
2650 const HVX_Vector lf2 = Q6_V_valign_VVR(od2, zero, 124); \
2651 acc = conv2d_dw_qf32_mpy_add3(acc, lf2, w20, ev2, w21, od2, w22); \
2652 \
2653 hvx_vmem(out_vtcm + (size_t) ohi * 32) = Q6_Vsf_equals_Vqf32(acc); \
2654 \
2655 ev0 = ev2; \
2656 od0 = od2; \
2657 lf0 = lf2; \
2658 } \
2659}
2660
2661DEFINE_CONV2D_DW_WHCN_S2_K3X3_W64(f32, conv2d_dw_splat_w_f32)
2662DEFINE_CONV2D_DW_WHCN_S2_K3X3_W64(f16, conv2d_dw_splat_w_f16)
2663
2664#define DEFINE_CONV2D_DW_WHCN_S2_K3X3(SUFFIX, SPLAT_W_FN) \
2665__attribute__((noinline)) \
2666static void conv2d_dw_whcn_s2_k3x3_##SUFFIX( \
2667 const float * restrict in_vtcm, \
2668 float * restrict out_vtcm, \
2669 const void * w_vtcm, \
2670 const struct conv2d_dw_task_slice * slice, \
2671 const struct htp_ops_context * octx, \
2672 const struct htp_conv2d_dw_kernel_params * kp) { \
2673 \
2674 const struct htp_tensor * src0 = octx->src[0]; \
2675 const size_t w_nb0 = src0->nb[0]; \
2676 const size_t w_nb1 = src0->nb[1]; \
2677 const char * restrict w_chan = (const char *) w_vtcm + (size_t) slice->c * src0->nb[3]; \
2678 const uint32_t oh_len = slice->oh_len; \
2679 const uint32_t w_in = kp->w; \
2680 const uint32_t ow = kp->ow; \
2681 \
2682 conv2d_dw_pad_vtcm_k3x3((float *) in_vtcm, slice, w_in, 0); \
2683 \
2684 const HVX_Vector w00 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 0 * w_nb0); \
2685 const HVX_Vector w01 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 1 * w_nb0); \
2686 const HVX_Vector w02 = SPLAT_W_FN(w_chan + 0 * w_nb1 + 2 * w_nb0); \
2687 const HVX_Vector w10 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 0 * w_nb0); \
2688 const HVX_Vector w11 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 1 * w_nb0); \
2689 const HVX_Vector w12 = SPLAT_W_FN(w_chan + 1 * w_nb1 + 2 * w_nb0); \
2690 const HVX_Vector w20 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 0 * w_nb0); \
2691 const HVX_Vector w21 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 1 * w_nb0); \
2692 const HVX_Vector w22 = SPLAT_W_FN(w_chan + 2 * w_nb1 + 2 * w_nb0); \
2693 const HVX_Vector zero = Q6_V_vzero(); \
2694 \
2695 for (uint32_t ohi = 0; ohi < oh_len; ++ohi) { \
2696 const float * restrict r0 = in_vtcm + (size_t) (2 * ohi) * w_in; \
2697 const float * restrict r1 = r0 + w_in; \
2698 const float * restrict r2 = r1 + w_in; \
2699 float * restrict out_row = out_vtcm + (size_t) ohi * ow; \
2700 \
2701 HVX_Vector prev_od0 = zero; \
2702 HVX_Vector prev_od1 = zero; \
2703 HVX_Vector prev_od2 = zero; \
2704 \
2705 for (uint32_t ox = 0; ox < ow; ox += 32) { \
2706 const uint32_t ix = 2 * ox; \
2707 const bool has1 = (ix + 32 < w_in); \
2708 \
2709 const HVX_VectorPair vp0 = Q6_W_vdeal_VVR(has1 ? hvx_vmem(r0 + ix + 32) : zero, hvx_vmem(r0 + ix), -4); \
2710 const HVX_Vector ev0 = Q6_V_lo_W(vp0); \
2711 const HVX_Vector od0 = Q6_V_hi_W(vp0); \
2712 const HVX_Vector lf0 = Q6_V_valign_VVR(od0, prev_od0, 124); \
2713 prev_od0 = od0; \
2714 HVX_Vector acc = conv2d_dw_qf32_mpy_add3(zero, lf0, w00, ev0, w01, od0, w02); \
2715 \
2716 const HVX_VectorPair vp1 = Q6_W_vdeal_VVR(has1 ? hvx_vmem(r1 + ix + 32) : zero, hvx_vmem(r1 + ix), -4); \
2717 const HVX_Vector ev1 = Q6_V_lo_W(vp1); \
2718 const HVX_Vector od1 = Q6_V_hi_W(vp1); \
2719 const HVX_Vector lf1 = Q6_V_valign_VVR(od1, prev_od1, 124); \
2720 prev_od1 = od1; \
2721 acc = conv2d_dw_qf32_mpy_add3(acc, lf1, w10, ev1, w11, od1, w12); \
2722 \
2723 const HVX_VectorPair vp2 = Q6_W_vdeal_VVR(has1 ? hvx_vmem(r2 + ix + 32) : zero, hvx_vmem(r2 + ix), -4); \
2724 const HVX_Vector ev2 = Q6_V_lo_W(vp2); \
2725 const HVX_Vector od2 = Q6_V_hi_W(vp2); \
2726 const HVX_Vector lf2 = Q6_V_valign_VVR(od2, prev_od2, 124); \
2727 prev_od2 = od2; \
2728 acc = conv2d_dw_qf32_mpy_add3(acc, lf2, w20, ev2, w21, od2, w22); \
2729 \
2730 if (ox + 32 <= ow) { \
2731 hvx_vmem(out_row + ox) = Q6_Vsf_equals_Vqf32(acc); \
2732 } else { \
2733 const uint32_t rem = ow - ox; \
2734 const HVX_VectorPred q = Q6_Q_vsetq_R(rem * sizeof(float)); \
2735 hvx_vec_store_u(out_row + ox, rem * sizeof(float), Q6_V_vmux_QVV(q, Q6_Vsf_equals_Vqf32(acc), zero)); \
2736 } \
2737 } \
2738 } \
2739}
2740
2741DEFINE_CONV2D_DW_WHCN_S2_K3X3(f32, conv2d_dw_splat_w_f32)
2742DEFINE_CONV2D_DW_WHCN_S2_K3X3(f16, conv2d_dw_splat_w_f16)
2743
2744#define DEFINE_CONV2D_DW_WHCN_S1(SUFFIX, SPLAT_W_FN) \
2745__attribute__((noinline)) \
2746static void conv2d_dw_whcn_s1_##SUFFIX( \
2747 const float * in_vtcm, \
2748 float * out_vtcm, \
2749 const void * w_vtcm, \
2750 const struct conv2d_dw_task_slice * slice, \
2751 const struct htp_ops_context * octx, \
2752 const struct htp_conv2d_dw_kernel_params * kp) { \
2753 \
2754 const struct htp_tensor * src0 = octx->src[0]; \
2755 const char * w_chan = (const char *) w_vtcm + (size_t) slice->c * src0->nb[3]; \
2756 const int32_t p0 = kp->p0; \
2757 const int32_t p1 = kp->p1; \
2758 const int32_t s1 = kp->s1; \
2759 const int32_t d1 = kp->d1; \
2760 const int32_t w = (int32_t) kp->w; \
2761 const HVX_Vector zero = Q6_V_vzero(); \
2762 \
2763 for (uint32_t ohi = 0; ohi < slice->oh_len; ++ohi) { \
2764 const uint32_t oh = slice->oh_start + ohi; \
2765 float * out_row = out_vtcm + (size_t) ohi * kp->ow; \
2766 \
2767 for (uint32_t ox = 0; ox < kp->ow; ox += 32) { \
2768 const uint32_t rem = kp->ow - ox; \
2769 const uint32_t nbytes = (rem < 32) ? (rem * sizeof(float)) : 128; \
2770 HVX_Vector acc0 = zero; \
2771 HVX_Vector acc1 = zero; \
2772 HVX_Vector acc2 = zero; \
2773 HVX_Vector acc3 = zero; \
2774 \
2775 for (uint32_t kh = 0; kh < kp->kh; ++kh) { \
2776 const int32_t ih = (int32_t) (oh * s1 + kh * d1) - p1; \
2777 if (ih < 0 || ih >= (int32_t) kp->h) { \
2778 continue; \
2779 } \
2780 const float * in_row = in_vtcm + (size_t) (ih - slice->ih_first) * kp->w; \
2781 const char * w_row = w_chan + (size_t) kh * src0->nb[1]; \
2782 \
2783 for (uint32_t kw = 0; kw < kp->kw; ++kw) { \
2784 const HVX_Vector w_v = SPLAT_W_FN(w_row + (size_t) kw * src0->nb[0]); \
2785 const int32_t base_iw = (int32_t) ox - p0 + (int32_t) kw; \
2786 HVX_Vector in_v; \
2787 \
2788 if (base_iw >= 0) { \
2789 in_v = hvx_vmemu(in_row + base_iw); \
2790 } else { \
2791 const int32_t pad = -base_iw; \
2792 const HVX_Vector v = hvx_vmemu(in_row); \
2793 in_v = Q6_V_valign_VVR(v, zero, 128 - (uint32_t) pad * sizeof(float)); \
2794 } \
2795 if (base_iw + 31 >= w) { \
2796 const int32_t valid = w - base_iw; \
2797 const HVX_VectorPred q = Q6_Q_vsetq_R(MAX(0, valid) * sizeof(float)); \
2798 in_v = Q6_V_vmux_QVV(q, in_v, zero); \
2799 } \
2800 \
2801 const HVX_Vector prod = Q6_Vqf32_vmpy_VsfVsf(in_v, w_v); \
2802 switch (kw & 3) { \
2803 case 0: acc0 = Q6_Vqf32_vadd_Vqf32Vqf32(acc0, prod); break; \
2804 case 1: acc1 = Q6_Vqf32_vadd_Vqf32Vqf32(acc1, prod); break; \
2805 case 2: acc2 = Q6_Vqf32_vadd_Vqf32Vqf32(acc2, prod); break; \
2806 case 3: acc3 = Q6_Vqf32_vadd_Vqf32Vqf32(acc3, prod); break; \
2807 } \
2808 } \
2809 } \
2810 \
2811 const HVX_Vector acc01 = Q6_Vqf32_vadd_Vqf32Vqf32(acc0, acc1); \
2812 const HVX_Vector acc23 = Q6_Vqf32_vadd_Vqf32Vqf32(acc2, acc3); \
2813 HVX_Vector acc_out = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(acc01, acc23)); \
2814 if (rem < 32) { \
2815 const HVX_VectorPred q = Q6_Q_vsetq_R(rem * sizeof(float)); \
2816 acc_out = Q6_V_vmux_QVV(q, acc_out, zero); \
2817 } \
2818 \
2819 hvx_vec_store_u(out_row + ox, nbytes, acc_out); \
2820 } \
2821 } \
2822}
2823
2824DEFINE_CONV2D_DW_WHCN_S1(f32, conv2d_dw_splat_w_f32)
2825DEFINE_CONV2D_DW_WHCN_S1(f16, conv2d_dw_splat_w_f16)
2826
2827#define DEFINE_CONV2D_DW_WHCN_S2(SUFFIX, SPLAT_W_FN) \
2828__attribute__((noinline)) \
2829static void conv2d_dw_whcn_s2_##SUFFIX( \
2830 const float * in_vtcm, \
2831 float * out_vtcm, \
2832 const void * w_vtcm, \
2833 const struct conv2d_dw_task_slice * slice, \
2834 const struct htp_ops_context * octx, \
2835 const struct htp_conv2d_dw_kernel_params * kp) { \
2836 \
2837 const struct htp_tensor * src0 = octx->src[0]; \
2838 const char * w_chan = (const char *) w_vtcm + (size_t) slice->c * src0->nb[3]; \
2839 const int32_t p0 = kp->p0; \
2840 const int32_t p1 = kp->p1; \
2841 const int32_t s1 = kp->s1; \
2842 const int32_t d1 = kp->d1; \
2843 const int32_t w = (int32_t) kp->w; \
2844 const HVX_Vector zero = Q6_V_vzero(); \
2845 \
2846 for (uint32_t ohi = 0; ohi < slice->oh_len; ++ohi) { \
2847 const uint32_t oh = slice->oh_start + ohi; \
2848 float * out_row = out_vtcm + (size_t) ohi * kp->ow; \
2849 \
2850 for (uint32_t ox = 0; ox < kp->ow; ox += 32) { \
2851 const uint32_t rem = kp->ow - ox; \
2852 const uint32_t nbytes = (rem < 32) ? (rem * sizeof(float)) : 128; \
2853 HVX_Vector acc0 = zero; \
2854 HVX_Vector acc1 = zero; \
2855 HVX_Vector acc2 = zero; \
2856 HVX_Vector acc3 = zero; \
2857 \
2858 for (uint32_t kh = 0; kh < kp->kh; ++kh) { \
2859 const int32_t ih = (int32_t) (oh * s1 + kh * d1) - p1; \
2860 if (ih < 0 || ih >= (int32_t) kp->h) { \
2861 continue; \
2862 } \
2863 const float * in_row = in_vtcm + (size_t) (ih - slice->ih_first) * kp->w; \
2864 const char * w_row = w_chan + (size_t) kh * src0->nb[1]; \
2865 \
2866 for (uint32_t kw = 0; kw < kp->kw; ++kw) { \
2867 const HVX_Vector w_v = SPLAT_W_FN(w_row + (size_t) kw * src0->nb[0]); \
2868 const int32_t base_iw = (int32_t) (2 * ox) - p0 + (int32_t) kw; \
2869 HVX_Vector in_v; \
2870 \
2871 if (base_iw >= 0) { \
2872 const HVX_Vector v0 = hvx_vmemu(in_row + base_iw); \
2873 const HVX_Vector v1 = hvx_vmemu(in_row + base_iw + 32); \
2874 in_v = Q6_V_lo_W(Q6_W_vdeal_VVR(v1, v0, -4)); \
2875 } else { \
2876 const int32_t pad = -base_iw; \
2877 const HVX_Vector v0 = hvx_vmemu(in_row); \
2878 const HVX_Vector v1 = hvx_vmemu(in_row + 32); \
2879 const HVX_VectorPair vp = Q6_W_vdeal_VVR(v1, v0, -4); \
2880 if (pad & 1) { \
2881 const uint32_t shift_lanes = (uint32_t) (pad + 1) / 2; \
2882 const HVX_Vector v_odd = Q6_V_hi_W(vp); \
2883 in_v = Q6_V_valign_VVR(v_odd, zero, 128 - shift_lanes * sizeof(float)); \
2884 } else { \
2885 const uint32_t shift_lanes = (uint32_t) pad / 2; \
2886 const HVX_Vector v_even = Q6_V_lo_W(vp); \
2887 in_v = Q6_V_valign_VVR(v_even, zero, 128 - shift_lanes * sizeof(float)); \
2888 } \
2889 } \
2890 if (base_iw + 62 >= w) { \
2891 const int32_t valid = (w - base_iw + 1) / 2; \
2892 const HVX_VectorPred q = Q6_Q_vsetq_R(MAX(0, valid) * sizeof(float)); \
2893 in_v = Q6_V_vmux_QVV(q, in_v, zero); \
2894 } \
2895 \
2896 const HVX_Vector prod = Q6_Vqf32_vmpy_VsfVsf(in_v, w_v); \
2897 switch (kw & 3) { \
2898 case 0: acc0 = Q6_Vqf32_vadd_Vqf32Vqf32(acc0, prod); break; \
2899 case 1: acc1 = Q6_Vqf32_vadd_Vqf32Vqf32(acc1, prod); break; \
2900 case 2: acc2 = Q6_Vqf32_vadd_Vqf32Vqf32(acc2, prod); break; \
2901 case 3: acc3 = Q6_Vqf32_vadd_Vqf32Vqf32(acc3, prod); break; \
2902 } \
2903 } \
2904 } \
2905 \
2906 const HVX_Vector acc01 = Q6_Vqf32_vadd_Vqf32Vqf32(acc0, acc1); \
2907 const HVX_Vector acc23 = Q6_Vqf32_vadd_Vqf32Vqf32(acc2, acc3); \
2908 HVX_Vector acc_out = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(acc01, acc23)); \
2909 if (rem < 32) { \
2910 const HVX_VectorPred q = Q6_Q_vsetq_R(rem * sizeof(float)); \
2911 acc_out = Q6_V_vmux_QVV(q, acc_out, zero); \
2912 } \
2913 \
2914 hvx_vec_store_u(out_row + ox, nbytes, acc_out); \
2915 } \
2916 } \
2917}
2918
2919DEFINE_CONV2D_DW_WHCN_S2(f32, conv2d_dw_splat_w_f32)
2920DEFINE_CONV2D_DW_WHCN_S2(f16, conv2d_dw_splat_w_f16)
2921
2922#define DEFINE_CONV2D_DW_CWHN(SUFFIX, LOAD_W_FN) \
2923__attribute__((noinline)) \
2924static void conv2d_dw_cwhn_##SUFFIX( \
2925 const float * in_vtcm, \
2926 float * out_vtcm, \
2927 const void * w_vtcm, \
2928 const struct conv2d_dw_task_slice * slice, \
2929 const struct htp_ops_context * octx, \
2930 const struct htp_conv2d_dw_kernel_params * kp) { \
2931 \
2932 const struct htp_tensor * src0 = octx->src[0]; \
2933 const uint32_t in_row_stride = kp->w * kp->c; \
2934 const uint32_t out_row_stride = kp->ow * kp->c; \
2935 const HVX_Vector zero = Q6_V_vzero(); \
2936 \
2937 for (uint32_t cb = 0; cb < kp->c; cb += 32) { \
2938 const uint32_t rem = MIN(32, kp->c - cb); \
2939 const char * w_base = (const char *) w_vtcm + (size_t) cb * src0->nb[3]; \
2940 \
2941 for (uint32_t ohi = 0; ohi < slice->oh_len; ++ohi) { \
2942 const uint32_t oh = slice->oh_start + ohi; \
2943 float * out_row = out_vtcm + (size_t) ohi * out_row_stride + cb; \
2944 \
2945 for (uint32_t ow = 0; ow < kp->ow; ++ow) { \
2946 HVX_Vector acc0 = zero; \
2947 HVX_Vector acc1 = zero; \
2948 HVX_Vector acc2 = zero; \
2949 HVX_Vector acc3 = zero; \
2950 \
2951 for (uint32_t kh = 0; kh < kp->kh; ++kh) { \
2952 const int32_t ih = (int32_t)(oh * kp->s1 + kh * kp->d1) - kp->p1; \
2953 if (ih < 0 || ih >= (int32_t)kp->h) { \
2954 continue; \
2955 } \
2956 const float * in_row = in_vtcm + (size_t)(ih - slice->ih_first) * in_row_stride + cb; \
2957 const char * w_row = w_base + (size_t) kh * src0->nb[1]; \
2958 \
2959 for (uint32_t kw = 0; kw < kp->kw; ++kw) { \
2960 const int32_t iw = (int32_t)(ow * kp->s0 + kw * kp->d0) - kp->p0; \
2961 if (iw < 0 || iw >= (int32_t)kp->w) { \
2962 continue; \
2963 } \
2964 \
2965 const float * in_ptr = in_row + (size_t) iw * kp->c; \
2966 const char * w_ptr = w_row + (size_t) kw * src0->nb[0]; \
2967 const HVX_Vector v_in = hvx_vmemu(in_ptr); \
2968 const HVX_Vector v_w = LOAD_W_FN(w_ptr); \
2969 const HVX_Vector prod = Q6_Vqf32_vmpy_VsfVsf(v_in, v_w); \
2970 switch (kw & 3) { \
2971 case 0: acc0 = Q6_Vqf32_vadd_Vqf32Vqf32(acc0, prod); break; \
2972 case 1: acc1 = Q6_Vqf32_vadd_Vqf32Vqf32(acc1, prod); break; \
2973 case 2: acc2 = Q6_Vqf32_vadd_Vqf32Vqf32(acc2, prod); break; \
2974 case 3: acc3 = Q6_Vqf32_vadd_Vqf32Vqf32(acc3, prod); break; \
2975 } \
2976 } \
2977 } \
2978 \
2979 const HVX_Vector acc01 = Q6_Vqf32_vadd_Vqf32Vqf32(acc0, acc1); \
2980 const HVX_Vector acc23 = Q6_Vqf32_vadd_Vqf32Vqf32(acc2, acc3); \
2981 const HVX_Vector acc_out = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(acc01, acc23)); \
2982 \
2983 float * dst_ptr = out_row + (size_t) ow * kp->c; \
2984 if (rem == 32) { \
2985 hvx_vmemu(dst_ptr) = acc_out; \
2986 } else { \
2987 hvx_vec_store_u(dst_ptr, rem * sizeof(float), acc_out); \
2988 } \
2989 } \
2990 } \
2991 } \
2992}
2993
2994DEFINE_CONV2D_DW_CWHN(f32, conv2d_dw_load_w_f32)
2995DEFINE_CONV2D_DW_CWHN(f16, conv2d_dw_load_w_f16)
2996
2997typedef void (*conv2d_dw_compute_fn_t)(
2998 const float * in_vtcm,
2999 float * out_vtcm,
3000 const void * w_vtcm,
3001 const struct conv2d_dw_task_slice * slice,
3002 const struct htp_ops_context * octx,
3003 const struct htp_conv2d_dw_kernel_params * kp);
3004
3005static const conv2d_dw_compute_fn_t conv2d_dw_kernels[HTP_CONV2D_DW_KERNEL_COUNT][2] = {
3006 [HTP_CONV2D_DW_KERNEL_UNSUPPORTED] = { NULL, NULL },
3007 [HTP_CONV2D_DW_KERNEL_CWHN] = { conv2d_dw_cwhn_f32, conv2d_dw_cwhn_f16 },
3008 [HTP_CONV2D_DW_KERNEL_WHCN_S1] = { conv2d_dw_whcn_s1_f32, conv2d_dw_whcn_s1_f16 },
3009 [HTP_CONV2D_DW_KERNEL_WHCN_S2] = { conv2d_dw_whcn_s2_f32, conv2d_dw_whcn_s2_f16 },
3010 [HTP_CONV2D_DW_KERNEL_WHCN_S1_K3X3] = { conv2d_dw_whcn_s1_k3x3_f32, conv2d_dw_whcn_s1_k3x3_f16 },
3011 [HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W32] = { conv2d_dw_whcn_s2_k3x3_w32_f32, conv2d_dw_whcn_s2_k3x3_w32_f16 },
3012 [HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W64] = { conv2d_dw_whcn_s2_k3x3_w64_f32, conv2d_dw_whcn_s2_k3x3_w64_f16 },
3013 [HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3] = { conv2d_dw_whcn_s2_k3x3_f32, conv2d_dw_whcn_s2_k3x3_f16 },
3014};
3015
3016struct conv2d_dw_dma_context {
3017 struct htp_ops_context * octx;
3018 const struct htp_conv2d_dw_kernel_params * kp;
3019 struct htp_conv2d_dw_vtcm_layout layout;
3020 const void * w_vtcm;
3021 conv2d_dw_compute_fn_t compute_fn;
3022 uint32_t task_start;
3023 uint32_t task_count;
3024 uint32_t tasks_per_thread;
3025};
3026
3027static void conv2d_dw_dma_thread(unsigned int nth, unsigned int ith, void * data) {
3028 const struct conv2d_dw_dma_context * ctx = (const struct conv2d_dw_dma_context *) data;
3029 const struct htp_ops_context * octx = ctx->octx;
3030 const struct htp_conv2d_dw_kernel_params * kp = ctx->kp;
3031 const struct htp_conv2d_dw_vtcm_layout * L = &ctx->layout;
3032 const struct htp_tensor * src1 = octx->src[1];
3033 const struct htp_tensor * dst = octx->dst;
3034 conv2d_dw_compute_fn_t compute_fn = ctx->compute_fn;
3035
3036 const uint32_t first = ctx->task_start + ith * ctx->tasks_per_thread;
3037 const uint32_t last = MIN(first + ctx->tasks_per_thread, ctx->task_start + ctx->task_count);
3038 if (first >= last) {
3039 return;
3040 }
3041
3042 (void) nth;
3043
3044 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
3045 dma_queue * dma_q = octx->ctx->dma[ith];
3046
3047 const uint32_t src_spad_half = (uint32_t) L->src_spad_half_size;
3048 const uint32_t dst_spad_half = (uint32_t) L->dst_spad_half_size;
3049 const uint32_t pad_top_stride = kp->pad_top_stride;
3050
3051 uint8_t * vtcm_base = (uint8_t *) octx->ctx->vtcm_base;
3052 uint8_t * src_spad = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L->off_src) + ith * L->src_bytes_per_thread;
3053 uint8_t * dst_spad = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L->off_dst) + ith * L->dst_bytes_per_thread;
3054
3055 float * srcb2[2] = { (float *) src_spad, (float *) (src_spad + src_spad_half) };
3056 float * dstb2[2] = { (float *) dst_spad, (float *) (dst_spad + dst_spad_half) };
3057 struct conv2d_dw_task_slice sl2[2];
3058
3059 const uint32_t total = last - first;
3060
3061 for (uint32_t i = 0; i < total && i < 2; ++i) {
3062 conv2d_dw_get_slice(&sl2[i], first + i, src1, dst, kp);
3063
3064 dma_queue_push(dma_q,
3065 dma_make_data(dst->data, dstb2[i]),
3066 sl2[i].dst_bytes, dst_spad_half,
3067 sl2[i].dst_bytes, 0);
3068
3069 float * src_dst = srcb2[i] + (size_t) sl2[i].top_pad * pad_top_stride;
3070 dma_queue_push(dma_q,
3071 dma_make_data(src_dst, sl2[i].src_addr),
3072 src_spad_half, sl2[i].src_bytes,
3073 sl2[i].src_bytes, sl2[i].src_bytes > 0 ? 1 : 0);
3074 }
3075
3076 for (uint32_t i = 0; i < total; ++i) {
3077 const uint32_t task = first + i;
3078 const uint32_t buf = i & 1u;
3079 float * srcb = srcb2[buf];
3080 float * dstb = dstb2[buf];
3081
3082 dma_queue_pop(dma_q);
3083 dma_queue_pop(dma_q);
3084
3085 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) task);
3086 compute_fn(srcb, dstb, ctx->w_vtcm, &sl2[buf], octx, kp);
3087 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) task);
3088
3089 dma_queue_push(dma_q,
3090 dma_make_data(sl2[buf].dst_addr, dstb),
3091 sl2[buf].dst_bytes, dst_spad_half,
3092 sl2[buf].dst_bytes, sl2[buf].dst_bytes > 0 ? 1 : 0);
3093
3094 if (i + 2 < total) {
3095 conv2d_dw_get_slice(&sl2[buf], task + 2, src1, dst, kp);
3096
3097 float * src_dst = srcb + (size_t) sl2[buf].top_pad * pad_top_stride;
3098 dma_queue_push(dma_q,
3099 dma_make_data(src_dst, sl2[buf].src_addr),
3100 src_spad_half, sl2[buf].src_bytes,
3101 sl2[buf].src_bytes, sl2[buf].src_bytes > 0 ? 1 : 0);
3102 }
3103 }
3104
3105 dma_queue_flush(dma_q);
3106
3107 FARF(HIGH, "conv2d_dw-f32-dma %d/%d: %ux%ux%ux%u -> %ux%ux%ux%u (%u:%u)\n",
3108 ith, nth, kp->w, kp->h, kp->c, kp->n, kp->ow, kp->oh, kp->c, kp->n, first, last);
3109}
3110
3111int op_conv_2d_dw(struct htp_ops_context * octx) {
3112 const struct htp_conv2d_dw_kernel_params * kp =
3113 (const struct htp_conv2d_dw_kernel_params *) octx->kernel_params;
3114
3115 if (kp->total_tasks == 0) {
3116 return HTP_STATUS_OK;
3117 }
3118
3119 if (kp->kernel_type <= HTP_CONV2D_DW_KERNEL_UNSUPPORTED ||
3120 kp->kernel_type >= HTP_CONV2D_DW_KERNEL_COUNT) {
3121 return HTP_STATUS_NO_SUPPORT;
3122 }
3123
3124 const uint32_t pad_rows = (kp->pad_top_stride > 0) ? 4 : 0;
3125 const uint32_t max_ih_chunk = MIN(kp->h, (kp->oh_chunk - 1) * (uint32_t) kp->s1 + (kp->kh - 1) * (uint32_t) kp->d1 + 1);
3126 const uint32_t max_src_bytes = (max_ih_chunk + pad_rows) * kp->row_src_bytes + 128;
3127 const uint32_t max_dst_bytes = kp->oh_chunk * kp->row_dst_bytes + 128;
3128
3129 const struct htp_tensor * src0 = octx->src[0];
3130 const uint32_t elem_size = kp->is_f16 ? sizeof(__fp16) : sizeof(float);
3131 const uint32_t w_size = src0->size;
3132
3133 struct htp_conv2d_dw_vtcm_layout layout;
3134 htp_conv2d_dw_vtcm_layout_build(&layout, max_src_bytes, max_dst_bytes, w_size, kp->n_threads);
3135
3136 uint8_t * vtcm_base = (uint8_t *) octx->ctx->vtcm_base;
3137 void * w_vtcm = VTCM_LAYOUT_PTR(void, vtcm_base, layout.off_w);
3138
3139 struct htp_tensor w_vtcm_tensor = *src0;
3140 w_vtcm_tensor.data = (dma_addr_t) w_vtcm;
3141
3142 if (src0->nb[0] == elem_size) {
3143 dma_cpy_sametype_sameshape(octx->ctx->dma[0], &w_vtcm_tensor, src0, elem_size);
3144 } else {
3145 dma_sync_read(octx->ctx->dma[0], w_vtcm, src0->data, w_size);
3146 }
3147 dma_queue_flush(octx->ctx->dma[0]);
3148
3149 uint32_t task_start = 0;
3150 uint32_t task_count = kp->total_tasks;
3151 if (octx->ctx->mdev.count > 1) {
3152 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(
3153 task_count, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
3154 task_start = range.start;
3155 task_count = range.count;
3156 }
3157 if (task_count == 0) {
3158 return HTP_STATUS_OK;
3159 }
3160
3161 const uint32_t nth = MIN(kp->n_threads, task_count);
3162 const uint32_t tasks_per_thread = (task_count + nth - 1) / nth;
3163
3164 struct conv2d_dw_dma_context ctx = {
3165 .octx = octx,
3166 .kp = kp,
3167 .layout = layout,
3168 .w_vtcm = w_vtcm,
3169 .compute_fn = conv2d_dw_kernels[kp->kernel_type][kp->is_f16 ? 1 : 0],
3170 .task_start = task_start,
3171 .task_count = task_count,
3172 .tasks_per_thread = tasks_per_thread,
3173 };
3174
3175 work_queue_run(octx->ctx->work_queue, conv2d_dw_dma_thread, &ctx, nth);
3176 return HTP_STATUS_OK;
3177}