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

1113 lines · 61071 bytes Raw Blame History
1#pragma clang diagnostic ignored "-Wunused-variable"
2#pragma clang diagnostic ignored "-Wunused-function"
3#pragma clang diagnostic ignored "-Wunused-but-set-variable"
4
5#include <HAP_farf.h>
6#include <hexagon_protos.h>
7#include <hexagon_types.h>
8#include <string.h>
9
10#include "hex-common.h"
11
12#define GGML_COMMON_DECL_C
13#include "ggml-common.h"
14#include "htp-ctx.h"
15#include "htp-ops.h"
16#include "hvx-utils.h"
17#include "dma-queue.h"
18#include "hex-profile.h"
19#include "htp-vtcm.h"
20#include "htp-tensor.h"
21#include "im2col-ops.h"
22
23struct htp_im2col_context {
24 struct htp_ops_context * octx;
25
26 uint32_t pe_row_base; // first output row index assigned to this dev (DMA path)
27 uint32_t pe_nrows; // number of output rows assigned to this dev (DMA path)
28 uint32_t pe_rows_per_thread; // output rows per worker
29 uint32_t pe_blocks_per_thread; // output-col blocks per worker (blocked DMA path)
30 uint32_t pe_src_row_bytes; // one output row's source, rounded 256
31 uint32_t pe_dst_row_bytes; // one output row's dst: OW*patch_stride*2, rounded 256
32
33 // Patch-embed DMA path VTCM ping-pong.
34 uint8_t * pe_vtcm_src; // base of the 2x src buffers region
35 uint8_t * pe_vtcm_dst; // base of the 2x dst buffers region
36 uint32_t pe_src_size_per_thread; // 2 * pe_src_row_bytes
37 uint32_t pe_dst_size_per_thread; // 2 * pe_dst_row_bytes
38
39 uint32_t pe_owb; // output-col block size
40 uint32_t pe_wb; // staged source window width
41 uint32_t pe_nbpr; // blocks per row
42 uint32_t pe_gather_nvec; // vectors per gathered patch (KH == 1 blocked path)
43
44 // Precomputed dimensions for worker threads
45 uint32_t N;
46 uint32_t IC;
47 uint32_t ID, IH, IW;
48 uint32_t KD, KH, KW;
49 uint32_t OD, OH, OW;
50 uint32_t patch_stride;
51
52 // Per-thread initial coordinates (removes division from thread prologue)
53 uint32_t thread_in[HTP_MAX_NTHREADS];
54 uint32_t thread_iod[HTP_MAX_NTHREADS];
55 uint32_t thread_ioh[HTP_MAX_NTHREADS];
56 uint32_t thread_c0[HTP_MAX_NTHREADS];
57
58 void * compute;
59 uint32_t dst_elem;
60};
61
62typedef void (*im2col_blocked_compute_fn_t)(
63 const struct htp_im2col_context * ictx,
64 void * restrict dstb, const float * restrict srcb,
65 float * restrict scratch,
66 uint32_t nb, uint32_t c0, uint32_t ioh);
67
68typedef void (*im2col_patchembed_compute_fn_t)(
69 const struct htp_im2col_context * ictx,
70 void * restrict dstb, const float * restrict srcb, uint32_t ioh);
71
72typedef void (*im2col_3d_blocked_compute_fn_t)(
73 const struct htp_im2col_context * ictx,
74 void * restrict dstb, const float * restrict srcb,
75 float * restrict scratch,
76 uint32_t nb, uint32_t c0, uint32_t iod, uint32_t ioh);
77
78typedef void (*im2col_3d_patchembed_compute_fn_t)(
79 const struct htp_im2col_context * ictx,
80 void * restrict dstb, const float * restrict srcb);
81
82struct im2col_cursor {
83 uint32_t r;
84 uint32_t in;
85 uint32_t iod;
86 uint32_t ioh;
87 uint32_t c0;
88};
89
90static inline void im2col_step_row(struct im2col_cursor * c, const struct htp_im2col_context * ictx) {
91 c->r++;
92 c->ioh++;
93 if (c->ioh >= ictx->OH) {
94 c->ioh = 0;
95 c->in++;
96 }
97}
98
99static inline void im2col_step_block(struct im2col_cursor * c, const struct htp_im2col_context * ictx) {
100 c->c0 += ictx->pe_owb;
101 if (c->c0 >= ictx->OW) {
102 c->c0 = 0;
103 c->r++;
104 c->ioh++;
105 if (c->ioh >= ictx->OH) {
106 c->ioh = 0;
107 c->in++;
108 }
109 }
110}
111
112static void im2col_blocked_dma_stage(struct htp_im2col_context * ictx, dma_queue * dma_q,
113 float * srcb, const struct im2col_cursor * c) {
114 const uint32_t in = c->in, ioh = c->ioh, c0 = c->c0;
115 const struct htp_ops_context * octx = ictx->octx;
116 const struct htp_tensor * src1 = octx->src[1];
117 const int32_t s0 = octx->op_params[0], s1 = octx->op_params[1];
118 const int32_t p0 = octx->op_params[2], p1 = octx->op_params[3];
119 const int32_t d1 = octx->op_params[5];
120 const bool is_2D = (octx->op_params[6] == 1);
121 const uint32_t IC = ictx->IC, IH = ictx->IH, IW = ictx->IW;
122 const uint32_t KH = ictx->KH;
123 const uint32_t Wb = ictx->pe_wb;
124 const dma_addr_t src_data = src1->data;
125 const int32_t win0 = (int32_t) c0 * s0 - p0;
126 const size_t src_channel_stride = is_2D ? src1->nb[2] : src1->nb[1];
127 const size_t batch_offset = is_2D ? (size_t) in * src1->nb[3] : (size_t) in * src1->nb[2];
128
129 for (uint32_t ikh = 0; ikh < KH; ikh++) {
130 const int32_t iih = (int32_t) ioh * s1 + (int32_t) ikh * d1 - p1;
131 if (iih < 0 || iih >= (int32_t) IH) {
132 dma_queue_push(dma_q, dma_make_data(srcb, 0), 0, 0, 0, 0);
133 continue;
134 }
135 const int32_t lo = win0 < 0 ? -win0 : 0;
136 int32_t hi = (int32_t) IW - win0;
137 if (hi > (int32_t) Wb) {
138 hi = (int32_t) Wb;
139 }
140 if (hi <= lo) {
141 dma_queue_push(dma_q, dma_make_data(srcb, 0), 0, 0, 0, 0);
142 continue;
143 }
144 const uint32_t cpw = (uint32_t) (hi - lo);
145 float * vdst = srcb + (size_t) ikh * Wb + lo;
146 const dma_addr_t vsrc = src_data + batch_offset + (size_t) iih * src1->nb[1] + (size_t) (win0 + lo) * src1->nb[0];
147 dma_queue_push(dma_q, dma_make_data(vdst, vsrc),
148 (size_t) KH * Wb * sizeof(float), src_channel_stride,
149 cpw * sizeof(float), IC);
150 }
151}
152
153#define IM2COL_BLOCKED_COMPUTE_BODY(FNAME, DST_CTYPE, COPY_FN, SPLAT_FN) \
154static void FNAME( \
155 const struct htp_im2col_context * ictx, \
156 void * restrict dstb_raw, const float * restrict srcb, \
157 float * restrict scratch, \
158 uint32_t nb, uint32_t c0, uint32_t ioh) { \
159 DST_CTYPE * restrict dstb = (DST_CTYPE *) dstb_raw; \
160 const struct htp_ops_context * octx = ictx->octx; \
161 const int32_t s0 = octx->op_params[0], s1 = octx->op_params[1]; \
162 const int32_t p0 = octx->op_params[2], p1 = octx->op_params[3]; \
163 const int32_t d0 = octx->op_params[4], d1 = octx->op_params[5]; \
164 const uint32_t IC = ictx->IC, IH = ictx->IH, IW = ictx->IW; \
165 const uint32_t KH = ictx->KH, KW = ictx->KW; \
166 const uint32_t Wb = ictx->pe_wb; \
167 const uint32_t patch_stride = ictx->patch_stride; \
168 const int32_t win0 = (int32_t) c0 * s0 - p0; \
169 \
170 for (uint32_t j = 0; j < nb; j++) { \
171 const uint32_t iow = c0 + j; \
172 DST_CTYPE * dst_patch = dstb + (size_t) j * patch_stride; \
173 const int32_t iiw0 = (int32_t) iow * s0 - p0; \
174 for (uint32_t ikh = 0; ikh < KH; ikh++) { \
175 const int32_t iih = (int32_t) ioh * s1 + (int32_t) ikh * d1 - p1; \
176 const int okh = (iih >= 0 && iih < (int32_t) IH); \
177 for (uint32_t iic = 0; iic < IC; iic++) { \
178 DST_CTYPE * out_run = dst_patch + (size_t) (iic * KH + ikh) * KW; \
179 if (!okh) { \
180 SPLAT_FN(out_run, 0, KW); \
181 continue; \
182 } \
183 const float * vrow = srcb + (size_t) (iic * KH + ikh) * Wb; \
184 if (d0 == 1) { \
185 const int32_t lo = iiw0 < 0 ? -iiw0 : 0; \
186 int32_t hi = (int32_t) IW - iiw0; \
187 if (hi > (int32_t) KW) { \
188 hi = (int32_t) KW; \
189 } \
190 if (hi <= lo) { \
191 SPLAT_FN(out_run, 0, KW); \
192 } else { \
193 if (lo > 0) { \
194 SPLAT_FN(out_run, 0, (uint32_t) lo); \
195 } \
196 COPY_FN((uint8_t *) (out_run + lo), \
197 (const uint8_t *) (vrow + (iiw0 + lo - win0)), \
198 (uint32_t) (hi - lo)); \
199 if (hi < (int32_t) KW) { \
200 SPLAT_FN(out_run + hi, 0, (uint32_t) (KW - hi)); \
201 } \
202 } \
203 continue; \
204 } \
205 for (uint32_t ikw = 0; ikw < KW; ikw += 64) { \
206 const uint32_t chunk = MIN(KW - ikw, 64); \
207 for (uint32_t c = 0; c < chunk; c++) { \
208 const int32_t iiw = iiw0 + (int32_t) (ikw + c) * d0; \
209 scratch[c] = (iiw < 0 || iiw >= (int32_t) IW) ? 0.0f : vrow[iiw - win0]; \
210 } \
211 COPY_FN((uint8_t *) (out_run + ikw), (const uint8_t *) scratch, chunk); \
212 } \
213 } \
214 } \
215 } \
216}
217
218IM2COL_BLOCKED_COMPUTE_BODY(im2col_blocked_compute_f16, uint16_t, hvx_copy_f16_f32_uu, hvx_splat_u16_u)
219IM2COL_BLOCKED_COMPUTE_BODY(im2col_blocked_compute_f32, float, hvx_copy_f32_uu, hvx_splat_f32_u)
220
221// Offsets of one output patch in the staged window, for KH == 1: lane k reads channel k / KW at tap k % KW.
222// Lanes past the patch read the first element and are not stored.
223// The first vector is built lane by lane; each next one is 32 lanes further: the tap advances by 32 % KW
224// and the channel by 32 / KW, plus one more when the tap wraps past KW.
225static __attribute__((noinline)) void im2col_gather_table_build(const struct htp_im2col_context * ictx,
226 HVX_Vector * restrict table) {
227 const int32_t d0 = ictx->octx->op_params[4];
228 const uint32_t KW = ictx->KW, Wb = ictx->pe_wb;
229
230 int32_t tap0[32] __attribute__((aligned(128)));
231 int32_t off0[32] __attribute__((aligned(128)));
232 uint32_t iic = 0, ikw = 0;
233 for (uint32_t k = 0; k < 32; k++) {
234 tap0[k] = (int32_t) ikw;
235 off0[k] = (int32_t) ((iic * Wb + ikw * d0) * sizeof(float));
236 if (++ikw == KW) {
237 ikw = 0;
238 iic++;
239 }
240 }
241 // (iic, ikw) is now (32 / KW, 32 % KW)
242 const HVX_Vector v_kw = Q6_V_vsplat_R(KW);
243 const HVX_Vector v_kw_m1 = Q6_V_vsplat_R(KW - 1);
244 const HVX_Vector v_tap_step = Q6_V_vsplat_R(ikw);
245 const HVX_Vector v_off_step = Q6_V_vsplat_R((iic * Wb + ikw * d0) * sizeof(float));
246 const HVX_Vector v_off_wrap = Q6_V_vsplat_R((Wb - KW * d0) * sizeof(float));
247 const HVX_Vector v_32 = Q6_V_vsplat_R(32);
248 const HVX_Vector v_n = Q6_V_vsplat_R(ictx->patch_stride);
249
250 HVX_Vector lane = hvx_vec_gather_offsets_w(1);
251 HVX_Vector tap = *(HVX_Vector *) tap0;
252 HVX_Vector off = *(HVX_Vector *) off0;
253 for (uint32_t g = 0; g < ictx->pe_gather_nvec; g++) {
254 table[g] = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VwVw(v_n, lane), off, Q6_V_vzero());
255
256 lane = Q6_Vw_vadd_VwVw(lane, v_32);
257 tap = Q6_Vw_vadd_VwVw(tap, v_tap_step);
258 off = Q6_Vw_vadd_VwVw(off, v_off_step);
259
260 const HVX_VectorPred wrap = Q6_Q_vcmp_gt_VwVw(tap, v_kw_m1);
261 tap = Q6_V_vmux_QVV(wrap, Q6_Vw_vsub_VwVw(tap, v_kw), tap);
262 off = Q6_V_vmux_QVV(wrap, Q6_Vw_vadd_VwVw(off, v_off_wrap), off);
263 }
264}
265
266// KH == 1: each output row is one patch, gathered from the staged [IC][Wb] window with a fixed offset
267// table shifted by s0 per row, then converted with a single copy.
268// scratch holds the offset table followed by the gathered row (both VTCM).
269#define IM2COL_GATHER_COMPUTE_BODY(FNAME, DST_CTYPE, COPY_FN) \
270static void FNAME( \
271 const struct htp_im2col_context * ictx, \
272 void * restrict dstb_raw, const float * restrict srcb_c, \
273 float * restrict scratch, \
274 uint32_t nb, uint32_t c0, uint32_t ioh) { \
275 DST_CTYPE * restrict dstb = (DST_CTYPE *) dstb_raw; \
276 float * restrict srcb = (float *) srcb_c; \
277 const int32_t s0 = ictx->octx->op_params[0], p0 = ictx->octx->op_params[2]; \
278 const int32_t s1 = ictx->octx->op_params[1], p1 = ictx->octx->op_params[3]; \
279 const uint32_t IC = ictx->IC, IH = ictx->IH, IW = ictx->IW, Wb = ictx->pe_wb; \
280 const uint32_t patch_stride = ictx->patch_stride; \
281 const uint32_t ng = ictx->pe_gather_nvec; \
282 const int32_t win0 = (int32_t) c0 * s0 - p0; \
283 const int32_t iih = (int32_t) ioh * s1 - p1; \
284 \
285 /* the DMA stage fills only the columns inside [0, IW): zero the rest of the window */ \
286 const int32_t lo = win0 < 0 ? -win0 : 0; \
287 const int32_t hi = MIN((int32_t) Wb, (int32_t) IW - win0); \
288 if (iih < 0 || iih >= (int32_t) IH || hi <= lo) { \
289 for (uint32_t iic = 0; iic < IC; iic++) { \
290 hvx_splat_f32_u(srcb + (size_t) iic * Wb, 0, Wb); \
291 } \
292 } else if (lo > 0 || hi < (int32_t) Wb) { \
293 for (uint32_t iic = 0; iic < IC; iic++) { \
294 float * row = srcb + (size_t) iic * Wb; \
295 if (lo > 0) { \
296 hvx_splat_f32_u(row, 0, (uint32_t) lo); \
297 } \
298 if (hi < (int32_t) Wb) { \
299 hvx_splat_f32_u(row + hi, 0, Wb - (uint32_t) hi); \
300 } \
301 } \
302 } \
303 \
304 const HVX_Vector * table = (const HVX_Vector *) scratch; \
305 float * row = scratch + (size_t) ng * 32; \
306 const uint32_t region = IC * Wb * sizeof(float); \
307 \
308 for (uint32_t j = 0; j < nb; j++) { \
309 const uint32_t shift = j * (uint32_t) s0 * sizeof(float); \
310 const size_t base = (size_t) ((const uint8_t *) srcb + shift); \
311 const uint32_t mu = region - shift - 1; \
312 for (uint32_t g = 0; g < ng; g++) { \
313 Q6_vgather_ARMVw((HVX_Vector *) (row + g * 32), base, mu, table[g]); \
314 } \
315 hvx_gather_sync(row + (ng - 1) * 32); \
316 DST_CTYPE * out = dstb + (size_t) j * patch_stride; \
317 COPY_FN((uint8_t *) out, (const uint8_t *) row, patch_stride); \
318 } \
319}
320
321IM2COL_GATHER_COMPUTE_BODY(im2col_gather_compute_f16, uint16_t, hvx_copy_f16_f32_uu)
322IM2COL_GATHER_COMPUTE_BODY(im2col_gather_compute_f32, float, hvx_copy_f32_uu)
323
324static void im2col_blocked_dma_thread(unsigned int nth, unsigned int ith, void * data) {
325 (void) nth;
326 struct htp_im2col_context * ictx = (struct htp_im2col_context *) data;
327 struct htp_ops_context * octx = ictx->octx;
328 struct htp_thread_trace * restrict tr = &octx->ctx->trace[ith];
329 const uint32_t OW = ictx->OW;
330 const uint32_t KH = ictx->KH;
331 const uint32_t owb = ictx->pe_owb;
332 const uint32_t nbpr = ictx->pe_nbpr;
333 const uint32_t patch_stride = ictx->patch_stride;
334 const uint32_t dst_elem = ictx->dst_elem;
335 im2col_blocked_compute_fn_t compute = (im2col_blocked_compute_fn_t) ictx->compute;
336 const dma_addr_t dst_data = octx->dst->data;
337 dma_queue * dma_q = octx->ctx->dma[ith];
338 uint8_t * srcb_base = ictx->pe_vtcm_src + ith * ictx->pe_src_size_per_thread;
339 uint8_t * dstb_base = ictx->pe_vtcm_dst + ith * ictx->pe_dst_size_per_thread;
340 float * srcb2[2] = { (float *) srcb_base, (float *) (srcb_base + ictx->pe_src_row_bytes) };
341 void * dstb2[2] = { dstb_base, dstb_base + ictx->pe_dst_row_bytes };
342 float * scratch = (float *) (srcb_base + 2 * ictx->pe_src_row_bytes);
343 // work is split over output-col blocks, so a single output row (1D) still uses every thread
344 const uint32_t blk_end_max = ictx->pe_nrows * nbpr;
345 const uint32_t blk_start = ictx->pe_blocks_per_thread * ith;
346 const uint32_t blk_end = MIN(blk_start + ictx->pe_blocks_per_thread, blk_end_max);
347 if (blk_start >= blk_end) {
348 return;
349 }
350 const uint32_t total_blocks = blk_end - blk_start;
351 struct im2col_cursor cur = {
352 .r = ictx->thread_in[ith] * ictx->OH + ictx->thread_ioh[ith],
353 .in = ictx->thread_in[ith],
354 .iod = 0,
355 .ioh = ictx->thread_ioh[ith],
356 .c0 = ictx->thread_c0[ith],
357 };
358 if (KH == 1) {
359 im2col_gather_table_build(ictx, (HVX_Vector *) scratch);
360 }
361 struct im2col_cursor stage = cur;
362 for (uint32_t i = 0; i < total_blocks && i < 2; i++) {
363 dma_queue_push(dma_q, dma_make_data(dst_data, dstb2[i]), 0, 0, 0, 0);
364 im2col_blocked_dma_stage(ictx, dma_q, srcb2[i], &stage);
365 im2col_step_block(&stage, ictx);
366 }
367 for (uint32_t bi = 0; bi < total_blocks; bi++) {
368 const uint32_t buf = bi & 1u;
369 float * srcb = srcb2[buf];
370 void * dstb = dstb2[buf];
371 const uint32_t nb = MIN(owb, OW - cur.c0);
372 dma_queue_pop(dma_q);
373 for (uint32_t k = 0; k < KH; k++) {
374 dma_queue_pop(dma_q);
375 }
376 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
377 compute(ictx, dstb, srcb, scratch, nb, cur.c0, cur.ioh);
378 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
379 const dma_addr_t ddr = dst_data + (size_t) (cur.r * OW + cur.c0) * patch_stride * dst_elem;
380 dma_queue_push(dma_q, dma_make_data(ddr, dstb),
381 nb * patch_stride * dst_elem, nb * patch_stride * dst_elem,
382 nb * patch_stride * dst_elem, 1);
383 if (bi + 2 < total_blocks) {
384 im2col_blocked_dma_stage(ictx, dma_q, srcb, &stage);
385 im2col_step_block(&stage, ictx);
386 }
387 im2col_step_block(&cur, ictx);
388 }
389 dma_queue_flush(dma_q);
390}
391
392static void im2col_patchembed_dma_stage(struct htp_im2col_context * ictx, dma_queue * dma_q,
393 float * srcb, const struct im2col_cursor * c) {
394 const struct htp_ops_context * octx = ictx->octx;
395 const uint32_t IC = ictx->IC, IH = ictx->IH, IW = ictx->IW;
396 const uint32_t KH = ictx->KH;
397 const uint32_t in = c->in, ioh = c->ioh;
398 const size_t src_channel_stride = (size_t) IH * IW * sizeof(float);
399 const size_t vtcm_channel_stride = (size_t) KH * IW * sizeof(float);
400 const dma_addr_t src_data = octx->src[1]->data;
401 const dma_addr_t vsrc = src_data + (size_t) (((in * IC) * IH + ioh * KH) * IW) * sizeof(float);
402
403 dma_queue_push(dma_q, dma_make_data(srcb, vsrc),
404 vtcm_channel_stride, src_channel_stride,
405 vtcm_channel_stride, IC);
406}
407
408static void im2col_patchembed_compute_f16(
409 const struct htp_im2col_context * ictx,
410 void * restrict dstb_raw, const float * restrict srcb, uint32_t ioh) {
411 uint16_t * restrict dstb = (uint16_t *) dstb_raw;
412 const uint32_t IC = ictx->IC, IH = ictx->IH, IW = ictx->IW;
413 const uint32_t KH = ictx->KH, KW = ictx->KW;
414 const uint32_t OW = ictx->OW, patch_stride = ictx->patch_stride;
415
416 if (KW == 16 && (KH % 4 == 0)) {
417 const HVX_VectorPred q_lo = Q6_Q_vsetq_R(64);
418 for (uint32_t iow = 0; iow < OW; iow++) {
419 uint16_t * dst_patch = dstb + (size_t) iow * patch_stride;
420 for (uint32_t iic = 0; iic < IC; iic++) {
421 for (uint32_t ikh = 0; ikh < KH; ikh += 4) {
422 const int32_t iih0 = (int32_t) ioh * (int32_t) KH + (int32_t) (ikh + 0);
423 const int32_t iih1 = (int32_t) ioh * (int32_t) KH + (int32_t) (ikh + 1);
424 const int32_t iih2 = (int32_t) ioh * (int32_t) KH + (int32_t) (ikh + 2);
425 const int32_t iih3 = (int32_t) ioh * (int32_t) KH + (int32_t) (ikh + 3);
426
427 const float * s0 = srcb + (size_t) (iic * KH + ikh + 0) * IW + (size_t) iow * 16;
428 const float * s1 = srcb + (size_t) (iic * KH + ikh + 1) * IW + (size_t) iow * 16;
429 const float * s2 = srcb + (size_t) (iic * KH + ikh + 2) * IW + (size_t) iow * 16;
430 const float * s3 = srcb + (size_t) (iic * KH + ikh + 3) * IW + (size_t) iow * 16;
431
432 const HVX_Vector v0 = (iih0 >= 0 && iih0 < (int32_t) IH) ? *((const HVX_UVector *) s0) : Q6_V_vzero();
433 const HVX_Vector v1 = (iih1 >= 0 && iih1 < (int32_t) IH) ? *((const HVX_UVector *) s1) : Q6_V_vzero();
434 const HVX_Vector v2 = (iih2 >= 0 && iih2 < (int32_t) IH) ? *((const HVX_UVector *) s2) : Q6_V_vzero();
435 const HVX_Vector v3 = (iih3 >= 0 && iih3 < (int32_t) IH) ? *((const HVX_UVector *) s3) : Q6_V_vzero();
436
437 const HVX_Vector w01 = Q6_V_vmux_QVV(q_lo, v0, Q6_V_vror_VR(v1, 64));
438 const HVX_Vector w23 = Q6_V_vmux_QVV(q_lo, v2, Q6_V_vror_VR(v3, 64));
439
440 *((HVX_Vector *) (dst_patch + (size_t) (iic * KH + ikh) * 16)) = hvx_vec_f32_to_f16(w01, w23);
441 }
442 }
443 }
444 return;
445 }
446
447 for (uint32_t iow = 0; iow < OW; iow++) {
448 uint16_t * dst_patch = dstb + (size_t) iow * patch_stride;
449 for (uint32_t ikh = 0; ikh < KH; ikh++) {
450 const int32_t iih = (int32_t) ioh * (int32_t) KH + (int32_t) ikh;
451 for (uint32_t iic = 0; iic < IC; iic++) {
452 uint16_t * out_run = dst_patch + (size_t) (iic * KH + ikh) * KW;
453 if (iih < 0 || iih >= (int32_t) IH) {
454 hvx_splat_u16_u(out_run, 0, KW);
455 continue;
456 }
457 const float * src_run = srcb + (size_t) (iic * KH + ikh) * IW + (size_t) iow * KW;
458 hvx_copy_f16_f32_uu((uint8_t *) out_run, (const uint8_t *) src_run, KW);
459 }
460 }
461 }
462}
463
464static void im2col_patchembed_compute_f32(
465 const struct htp_im2col_context * ictx,
466 void * restrict dstb_raw, const float * restrict srcb, uint32_t ioh) {
467 float * restrict dstb = (float *) dstb_raw;
468 const uint32_t IC = ictx->IC, IH = ictx->IH, IW = ictx->IW;
469 const uint32_t KH = ictx->KH, KW = ictx->KW;
470 const uint32_t OW = ictx->OW, patch_stride = ictx->patch_stride;
471
472 if (KW == 16 && (KH % 2 == 0)) {
473 const HVX_VectorPred q_lo = Q6_Q_vsetq_R(64);
474 for (uint32_t iow = 0; iow < OW; iow++) {
475 float * dst_patch = dstb + (size_t) iow * patch_stride;
476 for (uint32_t iic = 0; iic < IC; iic++) {
477 for (uint32_t ikh = 0; ikh < KH; ikh += 2) {
478 const int32_t iih0 = (int32_t) ioh * (int32_t) KH + (int32_t) (ikh + 0);
479 const int32_t iih1 = (int32_t) ioh * (int32_t) KH + (int32_t) (ikh + 1);
480
481 const float * s0 = srcb + (size_t) (iic * KH + ikh + 0) * IW + (size_t) iow * 16;
482 const float * s1 = srcb + (size_t) (iic * KH + ikh + 1) * IW + (size_t) iow * 16;
483
484 const HVX_Vector v0 = (iih0 >= 0 && iih0 < (int32_t) IH) ? *((const HVX_UVector *) s0) : Q6_V_vzero();
485 const HVX_Vector v1 = (iih1 >= 0 && iih1 < (int32_t) IH) ? *((const HVX_UVector *) s1) : Q6_V_vzero();
486
487 const HVX_Vector w01 = Q6_V_vmux_QVV(q_lo, v0, Q6_V_vror_VR(v1, 64));
488
489 *((HVX_Vector *) (dst_patch + (size_t) (iic * KH + ikh) * 16)) = w01;
490 }
491 }
492 }
493 return;
494 }
495
496 for (uint32_t iow = 0; iow < OW; iow++) {
497 float * dst_patch = dstb + (size_t) iow * patch_stride;
498 for (uint32_t ikh = 0; ikh < KH; ikh++) {
499 const int32_t iih = (int32_t) ioh * (int32_t) KH + (int32_t) ikh;
500 for (uint32_t iic = 0; iic < IC; iic++) {
501 float * out_run = dst_patch + (size_t) (iic * KH + ikh) * KW;
502 if (iih < 0 || iih >= (int32_t) IH) {
503 hvx_splat_f32_u(out_run, 0, KW);
504 continue;
505 }
506 const float * src_run = srcb + (size_t) (iic * KH + ikh) * IW + (size_t) iow * KW;
507 hvx_copy_f32_uu((uint8_t *) out_run, (const uint8_t *) src_run, KW);
508 }
509 }
510 }
511}
512
513static void im2col_patchembed_dma_thread(unsigned int nth, unsigned int ith, void * data) {
514 (void) nth;
515 struct htp_im2col_context * ictx = (struct htp_im2col_context *) data;
516 struct htp_ops_context * octx = ictx->octx;
517 struct htp_thread_trace * restrict tr = &octx->ctx->trace[ith];
518 const uint32_t OW = ictx->OW;
519 const uint32_t patch_stride = ictx->patch_stride;
520 const uint32_t dst_elem = ictx->dst_elem;
521 im2col_patchembed_compute_fn_t compute = (im2col_patchembed_compute_fn_t) ictx->compute;
522 const dma_addr_t dst_data = octx->dst->data;
523 dma_queue * dma_q = octx->ctx->dma[ith];
524 uint8_t * srcb_base = ictx->pe_vtcm_src + ith * ictx->pe_src_size_per_thread;
525 uint8_t * dstb_base = ictx->pe_vtcm_dst + ith * ictx->pe_dst_size_per_thread;
526 float * srcb2[2] = { (float *) srcb_base, (float *) (srcb_base + ictx->pe_src_row_bytes) };
527 void * dstb2[2] = { dstb_base, dstb_base + ictx->pe_dst_row_bytes };
528 const uint32_t row_end_max = ictx->pe_row_base + ictx->pe_nrows;
529 const uint32_t row_start = ictx->pe_row_base + ictx->pe_rows_per_thread * ith;
530 const uint32_t row_end = MIN(row_start + ictx->pe_rows_per_thread, row_end_max);
531 if (row_start >= row_end) {
532 return;
533 }
534 const uint32_t total_rows = row_end - row_start;
535 if (total_rows == 0) {
536 return;
537 }
538 struct im2col_cursor cur = {
539 .r = row_start,
540 .in = ictx->thread_in[ith],
541 .iod = 0,
542 .ioh = ictx->thread_ioh[ith],
543 .c0 = 0,
544 };
545 struct im2col_cursor stage = cur;
546 for (uint32_t i = 0; i < total_rows && i < 2; i++) {
547 dma_queue_push(dma_q, dma_make_data(dst_data, dstb2[i]), 0, 0, 0, 0);
548 im2col_patchembed_dma_stage(ictx, dma_q, srcb2[i], &stage);
549 im2col_step_row(&stage, ictx);
550 }
551 for (uint32_t ri = 0; ri < total_rows; ri++) {
552 const uint32_t buf = ri & 1u;
553 float * srcb = srcb2[buf];
554 void * dstb = dstb2[buf];
555 dma_queue_pop(dma_q);
556 dma_queue_pop(dma_q);
557 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
558 compute(ictx, dstb, srcb, cur.ioh);
559 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
560 const dma_addr_t ddr_row = dst_data + (size_t) cur.r * OW * patch_stride * dst_elem;
561 dma_queue_push(dma_q, dma_make_data(ddr_row, dstb),
562 OW * patch_stride * dst_elem, OW * patch_stride * dst_elem,
563 OW * patch_stride * dst_elem, 1);
564 if (ri + 2 < total_rows) {
565 im2col_patchembed_dma_stage(ictx, dma_q, srcb, &stage);
566 im2col_step_row(&stage, ictx);
567 }
568 im2col_step_row(&cur, ictx);
569 }
570 dma_queue_flush(dma_q);
571}
572
573static inline void im2col_3d_step_row(struct im2col_cursor * c, const struct htp_im2col_context * ictx) {
574 c->r++;
575 c->ioh++;
576 if (c->ioh >= ictx->OH) {
577 c->ioh = 0;
578 c->iod++;
579 if (c->iod >= ictx->OD) {
580 c->iod = 0;
581 c->in++;
582 }
583 }
584}
585
586static inline void im2col_3d_step_block(struct im2col_cursor * c, const struct htp_im2col_context * ictx) {
587 c->c0 += ictx->pe_owb;
588 if (c->c0 >= ictx->OW) {
589 c->c0 = 0;
590 c->r++;
591 c->ioh++;
592 if (c->ioh >= ictx->OH) {
593 c->ioh = 0;
594 c->iod++;
595 if (c->iod >= ictx->OD) {
596 c->iod = 0;
597 c->in++;
598 }
599 }
600 }
601}
602
603static void im2col_3d_blocked_dma_stage(struct htp_im2col_context * ictx, dma_queue * dma_q,
604 float * srcb, const struct im2col_cursor * c) {
605 const uint32_t in = c->in, iod = c->iod, ioh = c->ioh, c0 = c->c0;
606 const struct htp_ops_context * octx = ictx->octx;
607 const struct htp_tensor * src1 = octx->src[1];
608 const int32_t s0 = octx->op_params[0], s1 = octx->op_params[1], s2 = octx->op_params[2];
609 const int32_t p0 = octx->op_params[3], p1 = octx->op_params[4], p2 = octx->op_params[5];
610 const int32_t d1 = octx->op_params[7], d2 = octx->op_params[8];
611 const uint32_t IC = ictx->IC;
612 const uint32_t ID = ictx->ID, IH = ictx->IH, IW = ictx->IW;
613 const uint32_t KD = ictx->KD, KH = ictx->KH;
614 const uint32_t Wb = ictx->pe_wb;
615 const int32_t win0 = (int32_t) c0 * s0 - p0;
616 const dma_addr_t src_data = src1->data;
617 const size_t src_channel_stride = src1->nb[3];
618 const size_t vtcm_channel_stride = (size_t) KD * KH * Wb * sizeof(float);
619 const size_t batch_offset = (size_t) (in * IC) * src1->nb[3];
620
621 for (uint32_t ikd = 0; ikd < KD; ikd++) {
622 const int32_t iid = (int32_t) iod * s2 + (int32_t) ikd * d2 - p2;
623 const bool valid_d = (iid >= 0 && iid < (int32_t) ID);
624 for (uint32_t ikh = 0; ikh < KH; ikh++) {
625 const int32_t iih = (int32_t) ioh * s1 + (int32_t) ikh * d1 - p1;
626 if (!valid_d || iih < 0 || iih >= (int32_t) IH) {
627 dma_queue_push(dma_q, dma_make_data(srcb, 0), 0, 0, 0, 0);
628 continue;
629 }
630 const int32_t lo = win0 < 0 ? -win0 : 0;
631 int32_t hi = (int32_t) IW - win0;
632 if (hi > (int32_t) Wb) {
633 hi = (int32_t) Wb;
634 }
635 if (hi <= lo) {
636 dma_queue_push(dma_q, dma_make_data(srcb, 0), 0, 0, 0, 0);
637 continue;
638 }
639 float * vdst = srcb + (size_t) (ikd * KH + ikh) * Wb + lo;
640 const dma_addr_t vsrc = src_data + batch_offset +
641 (size_t) iid * src1->nb[2] +
642 (size_t) iih * src1->nb[1] +
643 (size_t) (win0 + lo) * src1->nb[0];
644 dma_queue_push(dma_q, dma_make_data(vdst, vsrc),
645 vtcm_channel_stride, src_channel_stride,
646 (size_t) (hi - lo) * sizeof(float), IC);
647 }
648 }
649}
650
651#define IM2COL_3D_BLOCKED_COMPUTE_BODY(FNAME, DST_CTYPE, COPY_FN, SPLAT_FN) \
652static void FNAME(const struct htp_im2col_context * ictx, \
653 void * restrict dstb_raw, const float * restrict srcb, \
654 float * restrict scratch, \
655 uint32_t nb, uint32_t c0, uint32_t iod, uint32_t ioh) { \
656 DST_CTYPE * restrict dstb = (DST_CTYPE *) dstb_raw; \
657 const struct htp_ops_context * octx = ictx->octx; \
658 const int32_t s0 = octx->op_params[0], s1 = octx->op_params[1], s2 = octx->op_params[2]; \
659 const int32_t p0 = octx->op_params[3], p1 = octx->op_params[4], p2 = octx->op_params[5]; \
660 const int32_t d0 = octx->op_params[6], d1 = octx->op_params[7], d2 = octx->op_params[8]; \
661 const uint32_t IC = ictx->IC; \
662 const uint32_t ID = ictx->ID, IH = ictx->IH, IW = ictx->IW; \
663 const uint32_t KD = ictx->KD, KH = ictx->KH, KW = ictx->KW; \
664 const uint32_t patch_stride = ictx->patch_stride; \
665 const uint32_t Wb = ictx->pe_wb; \
666 const int32_t win0 = (int32_t) c0 * s0 - p0; \
667 for (uint32_t j = 0; j < nb; j++) { \
668 const uint32_t iow = c0 + j; \
669 const int32_t iiw0 = (int32_t) iow * s0 - p0; \
670 DST_CTYPE * dst_patch = dstb + (size_t) j * patch_stride; \
671 for (uint32_t iic = 0; iic < IC; iic++) { \
672 for (uint32_t ikd = 0; ikd < KD; ikd++) { \
673 const int32_t iid = (int32_t) iod * s2 + (int32_t) ikd * d2 - p2; \
674 for (uint32_t ikh = 0; ikh < KH; ikh++) { \
675 const int32_t iih = (int32_t) ioh * s1 + (int32_t) ikh * d1 - p1; \
676 DST_CTYPE * out_run = dst_patch + \
677 (size_t) (iic * KD * KH + ikd * KH + ikh) * KW; \
678 if (iid < 0 || iid >= (int32_t) ID || iih < 0 || iih >= (int32_t) IH) { \
679 SPLAT_FN(out_run, 0, KW); \
680 continue; \
681 } \
682 const float * vrow = srcb + (size_t) (iic * KD * KH + ikd * KH + ikh) * Wb; \
683 if (d0 == 1) { \
684 const int32_t lo = iiw0 < 0 ? -iiw0 : 0; \
685 int32_t hi = (int32_t) IW - iiw0; \
686 if (hi > (int32_t) KW) { \
687 hi = (int32_t) KW; \
688 } \
689 if (hi <= lo) { \
690 SPLAT_FN(out_run, 0, KW); \
691 } else { \
692 if (lo > 0) { \
693 SPLAT_FN(out_run, 0, (uint32_t) lo); \
694 } \
695 COPY_FN((uint8_t *) (out_run + lo), \
696 (const uint8_t *) (vrow + (iiw0 + lo - win0)), \
697 (uint32_t) (hi - lo)); \
698 if (hi < (int32_t) KW) { \
699 SPLAT_FN(out_run + hi, 0, (uint32_t) (KW - hi)); \
700 } \
701 } \
702 continue; \
703 } \
704 for (uint32_t ikw = 0; ikw < KW; ikw += 64) { \
705 const uint32_t chunk = MIN(KW - ikw, 64); \
706 for (uint32_t c = 0; c < chunk; c++) { \
707 const int32_t iiw = iiw0 + (int32_t) (ikw + c) * d0; \
708 scratch[c] = (iiw < 0 || iiw >= (int32_t) IW) ? 0.0f : vrow[iiw - win0]; \
709 } \
710 COPY_FN((uint8_t *) (out_run + ikw), (const uint8_t *) scratch, chunk); \
711 } \
712 } \
713 } \
714 } \
715 } \
716}
717
718IM2COL_3D_BLOCKED_COMPUTE_BODY(im2col_3d_blocked_compute_f16, uint16_t, hvx_copy_f16_f32_uu, hvx_splat_u16_u)
719IM2COL_3D_BLOCKED_COMPUTE_BODY(im2col_3d_blocked_compute_f32, float, hvx_copy_f32_uu, hvx_splat_f32_u)
720
721static void im2col_3d_blocked_dma_thread(unsigned int nth, unsigned int ith, void * data) {
722 (void) nth;
723 struct htp_im2col_context * ictx = (struct htp_im2col_context *) data;
724 struct htp_ops_context * octx = ictx->octx;
725 struct htp_thread_trace * restrict tr = &octx->ctx->trace[ith];
726 const struct htp_tensor * restrict dst = octx->dst;
727 const uint32_t OW = ictx->OW;
728 const uint32_t KD = ictx->KD, KH = ictx->KH;
729 const uint32_t kd_kh = KD * KH;
730 const uint32_t patch_stride = ictx->patch_stride;
731 const uint32_t owb = ictx->pe_owb;
732 const uint32_t nbpr = ictx->pe_nbpr;
733 const uint32_t dst_elem = ictx->dst_elem;
734 const dma_addr_t dst_data = dst->data;
735 dma_queue * dma_q = octx->ctx->dma[ith];
736 uint8_t * srcb_base = ictx->pe_vtcm_src + ith * ictx->pe_src_size_per_thread;
737 uint8_t * dstb_base = ictx->pe_vtcm_dst + ith * ictx->pe_dst_size_per_thread;
738 float * srcb2[2] = { (float *) srcb_base, (float *) (srcb_base + ictx->pe_src_row_bytes) };
739 void * dstb2[2] = { dstb_base, dstb_base + ictx->pe_dst_row_bytes };
740 float * scratch = (float *) (srcb_base + 2 * ictx->pe_src_row_bytes);
741 const uint32_t row_end_max = ictx->pe_row_base + ictx->pe_nrows;
742 const uint32_t row_start = ictx->pe_row_base + ictx->pe_rows_per_thread * ith;
743 const uint32_t row_end = MIN(row_start + ictx->pe_rows_per_thread, row_end_max);
744 if (row_start >= row_end) {
745 return;
746 }
747 const uint32_t nrows_local = row_end - row_start;
748 const uint32_t total_blocks = nrows_local * nbpr;
749 if (total_blocks == 0) {
750 return;
751 }
752 struct im2col_cursor cur = {
753 .r = row_start,
754 .in = ictx->thread_in[ith],
755 .iod = ictx->thread_iod[ith],
756 .ioh = ictx->thread_ioh[ith],
757 .c0 = 0,
758 };
759 struct im2col_cursor stage = cur;
760 for (uint32_t i = 0; i < total_blocks && i < 2; i++) {
761 dma_queue_push(dma_q, dma_make_data(dst_data, dstb2[i]), 0, 0, 0, 0);
762 im2col_3d_blocked_dma_stage(ictx, dma_q, srcb2[i], &stage);
763 im2col_3d_step_block(&stage, ictx);
764 }
765
766 im2col_3d_blocked_compute_fn_t compute = (im2col_3d_blocked_compute_fn_t) ictx->compute;
767 for (uint32_t bi = 0; bi < total_blocks; bi++) {
768 const uint32_t buf = bi & 1u;
769 float * srcb = srcb2[buf];
770 void * dstb = dstb2[buf];
771 const uint32_t nb = MIN(owb, OW - cur.c0);
772 dma_queue_pop(dma_q);
773 for (uint32_t p = 0; p < kd_kh; p++) {
774 dma_queue_pop(dma_q);
775 }
776 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
777 compute(ictx, dstb, srcb, scratch, nb, cur.c0, cur.iod, cur.ioh);
778 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
779 const dma_addr_t ddr = dst_data + (size_t) (cur.r * OW + cur.c0) * patch_stride * dst_elem;
780 dma_queue_push(dma_q, dma_make_data(ddr, dstb),
781 nb * patch_stride * dst_elem, nb * patch_stride * dst_elem,
782 nb * patch_stride * dst_elem, 1);
783 if (bi + 2 < total_blocks) {
784 im2col_3d_blocked_dma_stage(ictx, dma_q, srcb, &stage);
785 im2col_3d_step_block(&stage, ictx);
786 }
787 im2col_3d_step_block(&cur, ictx);
788 }
789 dma_queue_flush(dma_q);
790}
791static void im2col_3d_patchembed_dma_stage(struct htp_im2col_context * ictx, dma_queue * dma_q,
792 float * srcb, const struct im2col_cursor * c) {
793 const struct htp_ops_context * octx = ictx->octx;
794 const uint32_t IC = ictx->IC;
795 const uint32_t ID = ictx->ID, IH = ictx->IH, IW = ictx->IW;
796 const uint32_t KD = ictx->KD, KH = ictx->KH;
797 const dma_addr_t src_data = octx->src[1]->data;
798 const uint32_t in = c->in, iod = c->iod, ioh = c->ioh;
799 const size_t src_channel_stride = (size_t) ID * IH * IW * sizeof(float);
800 const size_t vtcm_channel_stride = (size_t) KD * KH * IW * sizeof(float);
801 const size_t row_size = (size_t) KH * IW * sizeof(float);
802
803 for (uint32_t ikd = 0; ikd < KD; ikd++) {
804 const uint32_t iid = iod * KD + ikd;
805 float * vdst = srcb + (size_t) ikd * KH * IW;
806 const dma_addr_t vsrc = src_data + (size_t) (((in * IC) * ID + iid) * IH + ioh * KH) * IW * sizeof(float);
807 dma_queue_push(dma_q, dma_make_data(vdst, vsrc),
808 vtcm_channel_stride, src_channel_stride, row_size, IC);
809 }
810}
811static void im2col_3d_patchembed_compute_f16(
812 const struct htp_im2col_context * ictx,
813 void * restrict dstb_raw, const float * restrict srcb) {
814 uint16_t * restrict dstb = (uint16_t *) dstb_raw;
815 const uint32_t OW = ictx->OW;
816 const uint32_t IC = ictx->IC;
817 const uint32_t KD = ictx->KD;
818 const uint32_t KH = ictx->KH;
819 const uint32_t KW = ictx->KW;
820 const uint32_t IW = ictx->IW;
821 const uint32_t patch_stride = ictx->patch_stride;
822
823 for (uint32_t iow = 0; iow < OW; iow++) {
824 uint16_t * dst_patch = dstb + (size_t) iow * patch_stride;
825 for (uint32_t iic = 0; iic < IC; iic++) {
826 for (uint32_t ikd = 0; ikd < KD; ikd++) {
827 for (uint32_t ikh = 0; ikh < KH; ikh++) {
828 uint16_t * out_run = dst_patch + (size_t) (iic * KD * KH + ikd * KH + ikh) * KW;
829 const float * src_run = srcb + (size_t) (iic * KD * KH + ikd * KH + ikh) * IW + (size_t) iow * KW;
830 hvx_copy_f16_f32_uu((uint8_t *) out_run, (const uint8_t *) src_run, KW);
831 }
832 }
833 }
834 }
835}
836
837static void im2col_3d_patchembed_compute_f32(
838 const struct htp_im2col_context * ictx,
839 void * restrict dstb_raw, const float * restrict srcb) {
840 float * restrict dstb = (float *) dstb_raw;
841 const uint32_t OW = ictx->OW;
842 const uint32_t IC = ictx->IC;
843 const uint32_t KD = ictx->KD;
844 const uint32_t KH = ictx->KH;
845 const uint32_t KW = ictx->KW;
846 const uint32_t IW = ictx->IW;
847 const uint32_t patch_stride = ictx->patch_stride;
848
849 for (uint32_t iow = 0; iow < OW; iow++) {
850 float * dst_patch = dstb + (size_t) iow * patch_stride;
851 for (uint32_t iic = 0; iic < IC; iic++) {
852 for (uint32_t ikd = 0; ikd < KD; ikd++) {
853 for (uint32_t ikh = 0; ikh < KH; ikh++) {
854 float * out_run = dst_patch + (size_t) (iic * KD * KH + ikd * KH + ikh) * KW;
855 const float * src_run = srcb + (size_t) (iic * KD * KH + ikd * KH + ikh) * IW + (size_t) iow * KW;
856 hvx_copy_f32_uu((uint8_t *) out_run, (const uint8_t *) src_run, KW);
857 }
858 }
859 }
860 }
861}
862
863static void im2col_3d_patchembed_dma_thread(unsigned int nth, unsigned int ith, void * data) {
864 (void) nth;
865 struct htp_im2col_context * ictx = (struct htp_im2col_context *) data;
866 struct htp_ops_context * octx = ictx->octx;
867 struct htp_thread_trace * restrict tr = &octx->ctx->trace[ith];
868 const struct htp_tensor * restrict dst = octx->dst;
869 const uint32_t KD = ictx->KD;
870 const uint32_t OW = ictx->OW;
871 const uint32_t patch_stride = ictx->patch_stride;
872 const uint32_t dst_elem = ictx->dst_elem;
873 const dma_addr_t dst_data = dst->data;
874 dma_queue * dma_q = octx->ctx->dma[ith];
875 const uint32_t row_end_max = ictx->pe_row_base + ictx->pe_nrows;
876 const uint32_t row_start = ictx->pe_row_base + ictx->pe_rows_per_thread * ith;
877 const uint32_t row_end = MIN(row_start + ictx->pe_rows_per_thread, row_end_max);
878 if (row_start >= row_end) {
879 return;
880 }
881
882 const uint32_t nrows_local = row_end - row_start;
883 uint8_t * src_base = ictx->pe_vtcm_src + ith * ictx->pe_src_size_per_thread;
884 uint8_t * dstb_base = ictx->pe_vtcm_dst + ith * ictx->pe_dst_size_per_thread;
885 float * srcb2[2] = { (float *) src_base, (float *) (src_base + ictx->pe_src_row_bytes) };
886 void * dstb2[2] = { dstb_base, dstb_base + ictx->pe_dst_row_bytes };
887 struct im2col_cursor cur = {
888 .r = row_start,
889 .in = ictx->thread_in[ith],
890 .iod = ictx->thread_iod[ith],
891 .ioh = ictx->thread_ioh[ith],
892 .c0 = 0,
893 };
894 struct im2col_cursor stage = cur;
895 for (uint32_t i = 0; i < nrows_local && i < 2; i++) {
896 dma_queue_push(dma_q, dma_make_data(dst_data, dstb2[i]), 0, 0, 0, 0);
897 im2col_3d_patchembed_dma_stage(ictx, dma_q, srcb2[i], &stage);
898 im2col_3d_step_row(&stage, ictx);
899 }
900
901 im2col_3d_patchembed_compute_fn_t compute = (im2col_3d_patchembed_compute_fn_t) ictx->compute;
902 for (uint32_t idx = 0; idx < nrows_local; idx++) {
903 const uint32_t buf = idx & 1u;
904 float * srcb = srcb2[buf];
905 void * dstb = dstb2[buf];
906 dma_queue_pop(dma_q);
907 for (uint32_t p = 0; p < KD; p++) {
908 dma_queue_pop(dma_q);
909 }
910 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
911 compute(ictx, dstb, srcb);
912 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, cur.r);
913 const dma_addr_t ddr_row = dst_data + (size_t) cur.r * OW * patch_stride * dst_elem;
914 dma_queue_push(dma_q, dma_make_data(ddr_row, dstb),
915 OW * patch_stride * dst_elem, OW * patch_stride * dst_elem,
916 OW * patch_stride * dst_elem, 1);
917 if (idx + 2 < nrows_local) {
918 im2col_3d_patchembed_dma_stage(ictx, dma_q, srcb, &stage);
919 im2col_3d_step_row(&stage, ictx);
920 }
921 im2col_3d_step_row(&cur, ictx);
922 }
923 dma_queue_flush(dma_q);
924}
925
926int op_im2col(struct htp_ops_context * octx) {
927 const struct htp_im2col_kernel_params * kparams = (const struct htp_im2col_kernel_params *) octx->kernel_params;
928 if (kparams->kernel_type == HTP_IM2COL_KERNEL_UNSUPPORTED) {
929 return HTP_STATUS_NO_SUPPORT;
930 }
931
932 const struct htp_tensor * dst = octx->dst;
933
934 uint32_t row_base = 0;
935 uint32_t nrows = kparams->total_rows;
936 if (octx->ctx->mdev.count > 1) {
937 const struct htp_tensor_mdev_range range =
938 htp_tensor_mdev_partition(kparams->total_rows, htp_tensor_mdev_data_aligned(dst) ? kparams->rows_per_chunk : 0,
939 octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
940 row_base = range.start;
941 nrows = range.count;
942 }
943
944 if (nrows == 0) {
945 return HTP_STATUS_OK;
946 }
947
948 // the blocked path splits work over output-col blocks, so a single output row (1D) can use every thread
949 const bool blocked = kparams->kernel_type == HTP_IM2COL_KERNEL_BLOCKED_DMA;
950 const uint32_t nwork = blocked ? nrows * kparams->nbpr : nrows;
951 if (!htp_ops_context_set_n_threads(octx, MIN((uint32_t) kparams->n_threads, nwork))) {
952 return HTP_STATUS_INVAL_PARAMS;
953 }
954
955 struct htp_im2col_vtcm_layout layout;
956 htp_im2col_vtcm_layout_build(&layout, kparams->src_row_bytes, kparams->dst_row_bytes, kparams->n_threads,
957 htp_im2col_scratch_bytes(kparams->kernel_type, kparams->KH, kparams->patch_stride));
958
959 struct htp_im2col_context ictx = { 0 };
960 uint8_t * const base = octx->ctx->vtcm_base;
961
962 const uint32_t pth = octx->n_threads;
963 ictx.octx = octx;
964 ictx.pe_row_base = row_base;
965 ictx.pe_nrows = nrows;
966 ictx.pe_rows_per_thread = fastdiv(nrows + pth - 1, &octx->n_threads_div);
967 ictx.pe_blocks_per_thread = fastdiv(nwork + pth - 1, &octx->n_threads_div);
968 ictx.pe_src_row_bytes = kparams->src_row_bytes;
969 ictx.pe_dst_row_bytes = kparams->dst_row_bytes;
970 ictx.pe_vtcm_src = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_src);
971 ictx.pe_vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_dst);
972 ictx.pe_src_size_per_thread = layout.src_bytes_per_thread;
973 ictx.pe_dst_size_per_thread = layout.dst_bytes_per_thread;
974 ictx.pe_owb = kparams->owb;
975 ictx.pe_wb = kparams->wb;
976 ictx.pe_nbpr = kparams->nbpr;
977 ictx.N = kparams->N;
978 ictx.IC = kparams->IC;
979 ictx.ID = kparams->ID;
980 ictx.IH = kparams->IH;
981 ictx.IW = kparams->IW;
982 ictx.KD = kparams->KD;
983 ictx.KH = kparams->KH;
984 ictx.KW = kparams->KW;
985 ictx.OD = kparams->OD;
986 ictx.OH = kparams->OH;
987 ictx.OW = kparams->OW;
988 ictx.patch_stride = kparams->patch_stride;
989 ictx.pe_gather_nvec = hex_round_up(kparams->patch_stride, 32) / 32;
990
991 // a thread starts at a row, or at an output-col block (possibly mid-row) on the blocked path
992 for (uint32_t ith = 0; ith < pth; ith++) {
993 uint32_t r = row_base + ictx.pe_rows_per_thread * ith;
994 uint32_t c0 = 0;
995 if (blocked) {
996 const uint32_t blk = ictx.pe_blocks_per_thread * ith;
997 const uint32_t q = fastdiv(blk, &kparams->div_nbpr);
998 r = row_base + q;
999 c0 = (blk - q * kparams->nbpr) * kparams->owb;
1000 }
1001 const uint32_t in = fastdiv(r, &kparams->div_OH);
1002 ictx.thread_ioh[ith] = r - in * kparams->OH;
1003 ictx.thread_in[ith] = in;
1004 ictx.thread_c0[ith] = c0;
1005 }
1006
1007 ictx.dst_elem = dst->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
1008
1009 if (kparams->kernel_type == HTP_IM2COL_KERNEL_PATCHEMBED_DMA) {
1010 ictx.compute = dst->type == HTP_TYPE_F16 ? (void *) im2col_patchembed_compute_f16
1011 : (void *) im2col_patchembed_compute_f32;
1012 work_queue_run(octx->ctx->work_queue, im2col_patchembed_dma_thread, &ictx, pth);
1013 return HTP_STATUS_OK;
1014 } else if (blocked) {
1015 if (kparams->KH == 1) {
1016 ictx.compute = dst->type == HTP_TYPE_F16 ? (void *) im2col_gather_compute_f16
1017 : (void *) im2col_gather_compute_f32;
1018 } else {
1019 ictx.compute = dst->type == HTP_TYPE_F16 ? (void *) im2col_blocked_compute_f16
1020 : (void *) im2col_blocked_compute_f32;
1021 }
1022 work_queue_run(octx->ctx->work_queue, im2col_blocked_dma_thread, &ictx, pth);
1023 return HTP_STATUS_OK;
1024 }
1025
1026 return HTP_STATUS_NO_SUPPORT;
1027}
1028
1029int op_im2col_3d(struct htp_ops_context * octx) {
1030 const struct htp_im2col_kernel_params * kparams =
1031 (const struct htp_im2col_kernel_params *) octx->kernel_params;
1032 if (kparams->kernel_type == HTP_IM2COL_KERNEL_UNSUPPORTED) {
1033 return HTP_STATUS_NO_SUPPORT;
1034 }
1035
1036 const struct htp_tensor * dst = octx->dst;
1037
1038 uint32_t row_base = 0;
1039 uint32_t nrows = kparams->total_rows;
1040 if (octx->ctx->mdev.count > 1) {
1041 const struct htp_tensor_mdev_range range =
1042 htp_tensor_mdev_partition(kparams->total_rows, htp_tensor_mdev_data_aligned(dst) ? kparams->rows_per_chunk : 0,
1043 octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
1044 row_base = range.start;
1045 nrows = range.count;
1046 }
1047
1048 if (nrows == 0) {
1049 return HTP_STATUS_OK;
1050 }
1051
1052 if (!htp_ops_context_set_n_threads(octx, MIN((uint32_t) kparams->n_threads, nrows))) {
1053 return HTP_STATUS_INVAL_PARAMS;
1054 }
1055
1056 struct htp_im2col_vtcm_layout layout;
1057 htp_im2col_vtcm_layout_build(&layout, kparams->src_row_bytes, kparams->dst_row_bytes, kparams->n_threads, 256);
1058
1059 struct htp_im2col_context ictx = { 0 };
1060 uint8_t * const base = octx->ctx->vtcm_base;
1061
1062 const uint32_t pth = octx->n_threads;
1063 ictx.octx = octx;
1064 ictx.pe_row_base = row_base;
1065 ictx.pe_nrows = nrows;
1066 ictx.pe_rows_per_thread = fastdiv(nrows + pth - 1, &octx->n_threads_div);
1067 ictx.pe_src_row_bytes = kparams->src_row_bytes;
1068 ictx.pe_dst_row_bytes = kparams->dst_row_bytes;
1069 ictx.pe_vtcm_src = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_src);
1070 ictx.pe_vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_dst);
1071 ictx.pe_src_size_per_thread = layout.src_bytes_per_thread;
1072 ictx.pe_dst_size_per_thread = layout.dst_bytes_per_thread;
1073 ictx.pe_owb = kparams->owb;
1074 ictx.pe_wb = kparams->wb;
1075 ictx.pe_nbpr = kparams->nbpr;
1076 ictx.N = kparams->N;
1077 ictx.IC = kparams->IC;
1078 ictx.ID = kparams->ID;
1079 ictx.IH = kparams->IH;
1080 ictx.IW = kparams->IW;
1081 ictx.KD = kparams->KD;
1082 ictx.KH = kparams->KH;
1083 ictx.KW = kparams->KW;
1084 ictx.OD = kparams->OD;
1085 ictx.OH = kparams->OH;
1086 ictx.OW = kparams->OW;
1087 ictx.patch_stride = kparams->patch_stride;
1088
1089 for (uint32_t ith = 0; ith < pth; ith++) {
1090 const uint32_t rem = row_base + ictx.pe_rows_per_thread * ith;
1091 const uint32_t q_oh = fastdiv(rem, &kparams->div_OH);
1092 ictx.thread_ioh[ith] = rem - q_oh * kparams->OH;
1093 const uint32_t in = fastdiv(q_oh, &kparams->div_OD);
1094 ictx.thread_iod[ith] = q_oh - in * kparams->OD;
1095 ictx.thread_in[ith] = in;
1096 }
1097
1098 ictx.dst_elem = dst->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
1099
1100 if (kparams->kernel_type == HTP_IM2COL_KERNEL_PATCHEMBED_DMA) {
1101 ictx.compute = dst->type == HTP_TYPE_F16 ? (void *) im2col_3d_patchembed_compute_f16
1102 : (void *) im2col_3d_patchembed_compute_f32;
1103 work_queue_run(octx->ctx->work_queue, im2col_3d_patchembed_dma_thread, &ictx, pth);
1104 return HTP_STATUS_OK;
1105 } else if (kparams->kernel_type == HTP_IM2COL_KERNEL_BLOCKED_DMA) {
1106 ictx.compute = dst->type == HTP_TYPE_F16 ? (void *) im2col_3d_blocked_compute_f16
1107 : (void *) im2col_3d_blocked_compute_f32;
1108 work_queue_run(octx->ctx->work_queue, im2col_3d_blocked_dma_thread, &ictx, pth);
1109 return HTP_STATUS_OK;
1110 }
1111
1112 return HTP_STATUS_NO_SUPPORT;
1113}