Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-hexagon/htp/pad-ops.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-hexagon / htp / pad-ops.c` 588 lines ยท 26620 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 6 #include 7 8 #include 9 10 #include "dma-queue.h" 11 #include "hvx-utils.h" 12 13 #define GGML_COMMON_DECL_C 14 #include "ggml-common.h" 15 #include "hex-common.h" 16 #include "hex-profile.h" 17 #include "htp-ctx.h" 18 #include "htp-ops.h" 19 #include "htp-tensor.h" 20 #include "htp-vtcm.h" 21 #include "pad-ops.h" 22 23 /* Circular wrap: maps any integer x into [0, n) */ 24 static inline uint32_t wrap_around(int32_t x, uint32_t n, const struct fastdiv_values * div_n) { 25 if (x >= 0) { 26 const uint32_t ux = (uint32_t) x; 27 return ux < n ? ux : fastmodulo(ux, n, div_n); 28 } 29 const uint32_t ux = (uint32_t) (-x); 30 const uint32_t rem = ux < n ? ux : fastmodulo(ux, n, div_n); 31 return rem ? (n - rem) : 0; 32 } 33 34 /* Decompose a flat dst row index into (i1, i2, i3) */ 35 static inline void pad_decompose_row(uint32_t ir, uint32_t ne1, uint32_t ne2, 36 const struct htp_pad_kernel_params * kparams, 37 uint32_t *i1, uint32_t *i2, uint32_t *i3) { 38 *i3 = fastdiv(ir, &kparams->div_ne1_ne2); 39 const uint32_t rem = ir - (*i3) * (ne1 * ne2); 40 *i2 = fastdiv(rem, &kparams->div_ne1); 41 *i1 = rem - (*i2) * ne1; 42 } 43 44 /* Return non-zero if row (i1,i2,i3) falls in the non-padded interior */ 45 static inline int pad_is_interior(uint32_t i1, uint32_t i2, uint32_t i3, 46 int32_t lp1, int32_t rp1, uint32_t ne1, 47 int32_t lp2, int32_t rp2, uint32_t ne2, 48 int32_t lp3, int32_t rp3, uint32_t ne3) { 49 return ((int32_t)i1 >= lp1 && (int32_t)i1 < (int32_t)ne1 - rp1) && 50 ((int32_t)i2 >= lp2 && (int32_t)i2 < (int32_t)ne2 - rp2) && 51 ((int32_t)i3 >= lp3 && (int32_t)i3 < (int32_t)ne3 - rp3); 52 } 53 54 /* Compute the DDR src row pointer for a zero-pad interior row */ 55 static inline const uint8_t * pad_src_row_ptr(const struct htp_tensor * src, 56 uint32_t i1, uint32_t i2, uint32_t i3, 57 int32_t lp1, int32_t lp2, int32_t lp3) { 58 return (const uint8_t *) src->data 59 + (i1 - (uint32_t)lp1) * src->nb[1] 60 + (i2 - (uint32_t)lp2) * src->nb[2] 61 + (i3 - (uint32_t)lp3) * src->nb[3]; 62 } 63 64 static inline dma_addr_t pad_src_row_data(const struct htp_tensor * src, 65 uint32_t i1, uint32_t i2, uint32_t i3, 66 int32_t lp1, int32_t lp2, int32_t lp3) { 67 return src->data 68 + (i1 - (uint32_t)lp1) * src->nb[1] 69 + (i2 - (uint32_t)lp2) * src->nb[2] 70 + (i3 - (uint32_t)lp3) * src->nb[3]; 71 } 72 73 /* Compute the DDR src row pointer for a circular row (wrap-around indexing) */ 74 static inline const uint8_t * pad_circ_src_row_ptr(const struct htp_tensor * src, 75 const struct htp_pad_kernel_params * kparams, 76 uint32_t i1, uint32_t i2, uint32_t i3, 77 int32_t lp1, int32_t lp2, int32_t lp3) { 78 return (const uint8_t *) src->data 79 + wrap_around((int32_t)i1 - lp1, src->ne[1], &kparams->div_ne01) * src->nb[1] 80 + wrap_around((int32_t)i2 - lp2, src->ne[2], &kparams->div_ne02) * src->nb[2] 81 + wrap_around((int32_t)i3 - lp3, src->ne[3], &kparams->div_ne03) * src->nb[3]; 82 } 83 84 static inline dma_addr_t pad_circ_src_row_data(const struct htp_tensor * src, 85 const struct htp_pad_kernel_params * kparams, 86 uint32_t i1, uint32_t i2, uint32_t i3, 87 int32_t lp1, int32_t lp2, int32_t lp3) { 88 return src->data 89 + wrap_around((int32_t)i1 - lp1, src->ne[1], &kparams->div_ne01) * src->nb[1] 90 + wrap_around((int32_t)i2 - lp2, src->ne[2], &kparams->div_ne02) * src->nb[2] 91 + wrap_around((int32_t)i3 - lp3, src->ne[3], &kparams->div_ne03) * src->nb[3]; 92 } 93 94 struct htp_pad_context { 95 struct htp_ops_context * octx; 96 const struct htp_pad_kernel_params * kparams; 97 98 int32_t lp0, rp0; 99 int32_t lp1, rp1; 100 int32_t lp2, rp2; 101 int32_t lp3, rp3; 102 103 uint32_t nrows_per_thread; 104 uint32_t total_dst_rows; 105 uint32_t row_start; 106 107 size_t type_size; 108 109 // Row sizes for DMA kernel (populated when VTCM is available) 110 size_t src_row_size; 111 size_t src_row_size_aligned; 112 size_t dst_row_size; 113 size_t dst_row_size_aligned; 114 }; 115 116 #define htp_pad_preamble \ 117 const struct htp_tensor * src = octx->src[0]; \ 118 const struct htp_tensor * dst = octx->dst; \ 119 \ 120 const uint32_t ne00 = src->ne[0]; \ 121 const uint32_t nb00 = src->nb[0]; \ 122 \ 123 const uint32_t ne0 = dst->ne[0]; \ 124 const uint32_t ne1 = dst->ne[1]; \ 125 const uint32_t ne2 = dst->ne[2]; \ 126 const uint32_t ne3 = dst->ne[3]; \ 127 \ 128 const uint32_t nb1 = dst->nb[1]; \ 129 const uint32_t nb2 = dst->nb[2]; \ 130 const uint32_t nb3 = dst->nb[3]; \ 131 \ 132 const int32_t lp0 = pctx->lp0, rp0 = pctx->rp0; \ 133 const int32_t lp1 = pctx->lp1, rp1 = pctx->rp1; \ 134 const int32_t lp2 = pctx->lp2, rp2 = pctx->rp2; \ 135 const int32_t lp3 = pctx->lp3, rp3 = pctx->rp3; \ 136 \ 137 const size_t type_size = pctx->type_size; \ 138 \ 139 const uint32_t row_start = pctx->row_start + pctx->nrows_per_thread * ith; \ 140 const uint32_t row_end = MIN(row_start + pctx->nrows_per_thread, pctx->row_start + pctx->total_dst_rows); 141 142 143 #define htp_pad_dma_preamble \ 144 const size_t src_row_size = pctx->src_row_size; \ 145 const size_t src_row_size_aligned = pctx->src_row_size_aligned; \ 146 const size_t dst_row_size = pctx->dst_row_size; \ 147 const size_t dst_row_size_aligned = pctx->dst_row_size_aligned; \ 148 \ 149 uint8_t * src_spad_base = octx->src0_spad.data + ith * octx->src0_spad.size_per_thread; \ 150 uint8_t * dst_spad_base = octx->dst_spad.data + ith * octx->dst_spad.size_per_thread; \ 151 \ 152 dma_queue * dma_q = octx->ctx->dma[ith]; 153 154 // --------------------------------------------------------------------------- 155 // HVX vectorized PAD kernel 156 // --------------------------------------------------------------------------- 157 158 static void pad_job_per_thread_hvx(unsigned int nth, unsigned int ith, void * data) { 159 const struct htp_pad_context * pctx = (const struct htp_pad_context *) data; 160 struct htp_ops_context * octx = pctx->octx; 161 htp_pad_preamble; 162 163 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 164 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 165 166 for (uint32_t dst_row = row_start; dst_row < row_end; dst_row++) { 167 uint32_t i1, i2, i3; 168 pad_decompose_row(dst_row, ne1, ne2, pctx->kparams, &i1, &i2, &i3); 169 170 uint8_t * dst_ptr = (uint8_t *) dst->data + i1 * nb1 + i2 * nb2 + i3 * nb3; 171 172 const int interior = pad_is_interior(i1, i2, i3, 173 lp1, rp1, ne1, 174 lp2, rp2, ne2, 175 lp3, rp3, ne3); 176 177 if (!interior) { 178 hvx_splat_f32_u(dst_ptr, 0.0f, ne0); 179 } else { 180 const uint8_t * src_ptr = pad_src_row_ptr(src, i1, i2, i3, lp1, lp2, lp3); 181 182 if (lp0 > 0) { 183 hvx_splat_f32_u(dst_ptr, 0.0f, (uint32_t)lp0); 184 } 185 186 uint8_t * dst_row_start = dst_ptr + (size_t)lp0 * type_size; 187 if (nb00 == type_size) { 188 hvx_copy_f32_uu(dst_row_start, src_ptr, ne00); 189 } else { 190 for (uint32_t i = 0; i < ne00; i++) { 191 memcpy(dst_row_start + i * type_size, 192 src_ptr + (size_t)i * nb00, 193 type_size); 194 } 195 } 196 197 if (rp0 > 0) { 198 hvx_splat_f32_u(dst_ptr + ((size_t)lp0 + ne00) * type_size, 0.0f, (uint32_t)rp0); 199 } 200 } 201 } 202 203 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 204 205 FARF(HIGH, "pad-hvx %d/%d: (%ux%ux%ux%u) -> (%ux%ux%ux%u) rows %u:%u\n", 206 ith, nth, 207 src->ne[0], src->ne[1], src->ne[2], src->ne[3], 208 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 209 row_start, row_end); 210 } 211 212 // --------------------------------------------------------------------------- 213 // HVX + DMA PAD kernel - aligned, double-buffered 214 // --------------------------------------------------------------------------- 215 216 static void pad_job_per_thread_hvx_dma(unsigned int nth, unsigned int ith, void * data) { 217 const struct htp_pad_context * pctx = (const struct htp_pad_context *) data; 218 struct htp_ops_context * octx = pctx->octx; 219 htp_pad_preamble; 220 htp_pad_dma_preamble; 221 222 // ----------------------------------------------------------------------- 223 // Priming phase: push 2 pairs of (dummy_dst_DMA, src_DMA) to seed the 224 // double-buffer pipeline before the main loop begins. 225 // ----------------------------------------------------------------------- 226 for (uint32_t ir = row_start, spad_idx = 0; ir < row_end && spad_idx < 2; ir++, spad_idx++) { 227 uint8_t * src_spad_cur = src_spad_base + spad_idx * src_row_size_aligned; 228 uint8_t * dst_spad_cur = dst_spad_base + spad_idx * dst_row_size_aligned; 229 230 dma_queue_push(dma_q, 231 dma_make_data(dst->data, dst_spad_cur), 232 dst_row_size, dst_row_size_aligned, dst_row_size, 0); 233 234 uint32_t i1, i2, i3; 235 pad_decompose_row(ir, ne1, ne2, pctx->kparams, &i1, &i2, &i3); 236 const int interior = pad_is_interior(i1, i2, i3, 237 lp1, rp1, ne1, 238 lp2, rp2, ne2, 239 lp3, rp3, ne3); 240 241 const dma_addr_t src_data = interior 242 ? pad_src_row_data(src, i1, i2, i3, lp1, lp2, lp3) : src->data; 243 244 // Interior row: real DMA (1 row) from DDR to VTCM. 245 // Border row: null DMA (nrows=0) 246 dma_queue_push(dma_q, 247 dma_make_data(src_spad_cur, src_data), 248 src_row_size_aligned, src_row_size, src_row_size, interior ? 1 : 0); 249 } 250 251 // ----------------------------------------------------------------------- 252 // Main loop: pop completed DMAs, compute in VTCM with aligned HVX ops, 253 // push dst DMA and prefetch src for the next+1 row. 254 // ----------------------------------------------------------------------- 255 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 256 257 for (uint32_t ir = row_start; ir < row_end; ir++) { 258 uint8_t * dst_spad_cur = (uint8_t *) dma_queue_pop(dma_q).src; 259 uint8_t * src_spad_cur = (uint8_t *) dma_queue_pop(dma_q).dst; 260 261 uint32_t i1, i2, i3; 262 pad_decompose_row(ir, ne1, ne2, pctx->kparams, &i1, &i2, &i3); 263 264 const dma_addr_t dst_data = dst->data + i1 * nb1 + i2 * nb2 + i3 * nb3; 265 266 const int interior = pad_is_interior(i1, i2, i3, 267 lp1, rp1, ne1, 268 lp2, rp2, ne2, 269 lp3, rp3, ne3); 270 271 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); 272 if (!interior) { 273 hvx_splat_f32_a(dst_spad_cur, 0.0f, ne0); 274 } else { 275 hvx_splat_f32_a(dst_spad_cur, 0.0f, ne0); 276 277 uint8_t * dst_interior = dst_spad_cur + (size_t)lp0 * type_size; 278 279 if ((uintptr_t)dst_interior % VLEN == 0) { 280 hvx_copy_f32_aa(dst_interior, src_spad_cur, ne00); 281 } else { 282 hvx_copy_f32_ua(dst_interior, src_spad_cur, ne00); 283 } 284 } 285 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); 286 287 dma_queue_push(dma_q, 288 dma_make_data(dst_data, dst_spad_cur), 289 dst_row_size, dst_row_size_aligned, dst_row_size, 1); 290 291 const uint32_t next_row = ir + 2; 292 if (next_row < row_end) { 293 uint32_t ni1, ni2, ni3; 294 pad_decompose_row(next_row, ne1, ne2, pctx->kparams, &ni1, &ni2, &ni3); 295 const int next_interior = pad_is_interior(ni1, ni2, ni3, 296 lp1, rp1, ne1, 297 lp2, rp2, ne2, 298 lp3, rp3, ne3); 299 const dma_addr_t next_src_data = next_interior 300 ? pad_src_row_data(src, ni1, ni2, ni3, lp1, lp2, lp3) : src->data; 301 302 dma_queue_push(dma_q, 303 dma_make_data(src_spad_cur, next_src_data), 304 src_row_size_aligned, src_row_size, src_row_size, next_interior ? 1 : 0); 305 } 306 } 307 308 dma_queue_flush(dma_q); 309 310 FARF(HIGH, "pad-hvx-dma %d/%d: (%ux%ux%ux%u) -> (%ux%ux%ux%u) rows %u:%u\n", 311 ith, nth, 312 src->ne[0], src->ne[1], src->ne[2], src->ne[3], 313 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 314 row_start, row_end); 315 } 316 317 // --------------------------------------------------------------------------- 318 // HVX circular PAD kernel 319 // --------------------------------------------------------------------------- 320 321 static void pad_job_per_thread_hvx_circular(unsigned int nth, unsigned int ith, void * data) { 322 const struct htp_pad_context * pctx = (const struct htp_pad_context *) data; 323 struct htp_ops_context * octx = pctx->octx; 324 htp_pad_preamble; 325 326 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 327 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 328 329 for (uint32_t dst_row = row_start; dst_row < row_end; dst_row++) { 330 uint32_t i1, i2, i3; 331 pad_decompose_row(dst_row, ne1, ne2, pctx->kparams, &i1, &i2, &i3); 332 333 uint8_t * dst_ptr = (uint8_t *) dst->data + i1 * nb1 + i2 * nb2 + i3 * nb3; 334 const uint8_t * src_row = pad_circ_src_row_ptr(src, pctx->kparams, i1, i2, i3, lp1, lp2, lp3); 335 336 if (nb00 == type_size) { 337 338 if (lp0 > 0) { 339 if ((uint32_t)lp0 < 32) { 340 memcpy(dst_ptr, 341 src_row + (size_t)(ne00 - (uint32_t)lp0) * type_size, 342 (size_t)lp0 * type_size); 343 } else { 344 hvx_copy_f32_uu(dst_ptr, 345 src_row + (size_t)(ne00 - (uint32_t)lp0) * type_size, 346 (uint32_t)lp0); 347 } 348 } 349 hvx_copy_f32_uu(dst_ptr + (size_t)lp0 * type_size, src_row, ne00); 350 if (rp0 > 0) { 351 if ((uint32_t)rp0 < 32) { 352 memcpy(dst_ptr + ((size_t)lp0 + ne00) * type_size, 353 src_row, 354 (size_t)rp0 * type_size); 355 } else { 356 hvx_copy_f32_uu(dst_ptr + ((size_t)lp0 + ne00) * type_size, 357 src_row, 358 (uint32_t)rp0); 359 } 360 } 361 } else { 362 for (uint32_t i = 0; i < (uint32_t)lp0; i++) { 363 *(float *)(dst_ptr + i * type_size) = 364 *(const float *)(src_row + (size_t)(ne00 - (uint32_t)lp0 + i) * nb00); 365 } 366 for (uint32_t i = 0; i < ne00; i++) { 367 *(float *)(dst_ptr + ((size_t)lp0 + i) * type_size) = 368 *(const float *)(src_row + (size_t)i * nb00); 369 } 370 for (uint32_t i = 0; i < (uint32_t)rp0; i++) { 371 *(float *)(dst_ptr + ((size_t)lp0 + ne00 + i) * type_size) = 372 *(const float *)(src_row + (size_t)i * nb00); 373 } 374 } 375 } 376 377 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 378 379 FARF(HIGH, "pad-hvx-circ %d/%d: (%ux%ux%ux%u) -> (%ux%ux%ux%u) rows %u:%u\n", 380 ith, nth, 381 src->ne[0], src->ne[1], src->ne[2], src->ne[3], 382 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 383 row_start, row_end); 384 } 385 386 // --------------------------------------------------------------------------- 387 // HVX + DMA circular PAD kernel - aligned, double-buffered 388 // --------------------------------------------------------------------------- 389 390 static void pad_job_per_thread_hvx_circular_dma(unsigned int nth, unsigned int ith, void * data) { 391 const struct htp_pad_context * pctx = (const struct htp_pad_context *) data; 392 struct htp_ops_context * octx = pctx->octx; 393 htp_pad_preamble; 394 htp_pad_dma_preamble; 395 396 // ----------------------------------------------------------------------- 397 // Priming phase: push 2 pairs of (dummy_dst_DMA, src_DMA) to seed the 398 // double-buffer pipeline. Every row is a real src DMA (no null DMAs). 399 // ----------------------------------------------------------------------- 400 for (uint32_t ir = row_start, spad_idx = 0; ir < row_end && spad_idx < 2; ir++, spad_idx++) { 401 uint8_t * src_spad_cur = src_spad_base + spad_idx * src_row_size_aligned; 402 uint8_t * dst_spad_cur = dst_spad_base + spad_idx * dst_row_size_aligned; 403 404 dma_queue_push(dma_q, 405 dma_make_data(dst->data, dst_spad_cur), 406 dst_row_size, dst_row_size_aligned, dst_row_size, 0); 407 408 uint32_t pi1, pi2, pi3; 409 pad_decompose_row(ir, ne1, ne2, pctx->kparams, &pi1, &pi2, &pi3); 410 const dma_addr_t src_data = pad_circ_src_row_data(src, pctx->kparams, pi1, pi2, pi3, lp1, lp2, lp3); 411 dma_queue_push(dma_q, 412 dma_make_data(src_spad_cur, src_data), 413 src_row_size_aligned, src_row_size, src_row_size, 1); 414 } 415 416 // ----------------------------------------------------------------------- 417 // Main loop: pop completed DMAs, assemble circular row in VTCM with 418 // aligned HVX ops, push dst DMA and prefetch src for the next+1 row. 419 // ----------------------------------------------------------------------- 420 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 421 422 for (uint32_t ir = row_start; ir < row_end; ir++) { 423 uint8_t * dst_spad_cur = (uint8_t *) dma_queue_pop(dma_q).src; 424 uint8_t * src_spad_cur = (uint8_t *) dma_queue_pop(dma_q).dst; 425 426 uint32_t i1, i2, i3; 427 pad_decompose_row(ir, ne1, ne2, pctx->kparams, &i1, &i2, &i3); 428 const dma_addr_t dst_data = dst->data + i1 * nb1 + i2 * nb2 + i3 * nb3; 429 430 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); 431 if (lp0 > 0) { 432 uint8_t * dst_left = dst_spad_cur; 433 const uint8_t * src_left = src_spad_cur + (size_t)(ne00 - (uint32_t)lp0) * type_size; 434 if ((uint32_t)lp0 < 32) { 435 memcpy(dst_left, src_left, (size_t)lp0 * type_size); 436 } else { 437 hvx_copy_f32_uu(dst_left, src_left, (uint32_t)lp0); 438 } 439 } 440 441 { 442 uint8_t * dst_mid = dst_spad_cur + (size_t)lp0 * type_size; 443 if ((uintptr_t)dst_mid % VLEN == 0) { 444 hvx_copy_f32_aa(dst_mid, src_spad_cur, ne00); 445 } else { 446 hvx_copy_f32_ua(dst_mid, src_spad_cur, ne00); 447 } 448 } 449 450 if (rp0 > 0) { 451 uint8_t * dst_right = dst_spad_cur + ((size_t)lp0 + ne00) * type_size; 452 if ((uint32_t)rp0 < 32) { 453 memcpy(dst_right, src_spad_cur, (size_t)rp0 * type_size); 454 } else { 455 if ((uintptr_t)dst_right % VLEN == 0) { 456 hvx_copy_f32_aa(dst_right, src_spad_cur, (uint32_t)rp0); 457 } else { 458 hvx_copy_f32_ua(dst_right, src_spad_cur, (uint32_t)rp0); 459 } 460 } 461 } 462 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); 463 464 dma_queue_push(dma_q, 465 dma_make_data(dst_data, dst_spad_cur), 466 dst_row_size, dst_row_size_aligned, dst_row_size, 1); 467 468 const uint32_t next_row = ir + 2; 469 if (next_row < row_end) { 470 uint32_t nri1, nri2, nri3; 471 pad_decompose_row(next_row, ne1, ne2, pctx->kparams, &nri1, &nri2, &nri3); 472 const dma_addr_t next_src_data = pad_circ_src_row_data(src, pctx->kparams, nri1, nri2, nri3, lp1, lp2, lp3); 473 dma_queue_push(dma_q, 474 dma_make_data(src_spad_cur, next_src_data), 475 src_row_size_aligned, src_row_size, src_row_size, 1); 476 } 477 } 478 479 dma_queue_flush(dma_q); 480 481 FARF(HIGH, "pad-hvx-circ-dma %d/%d: (%ux%ux%ux%u) -> (%ux%ux%ux%u) rows %u:%u\n", 482 ith, nth, 483 src->ne[0], src->ne[1], src->ne[2], src->ne[3], 484 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 485 row_start, row_end); 486 } 487 488 int op_pad(struct htp_ops_context * octx) { 489 const struct htp_tensor * src0 = octx->src[0]; 490 const struct htp_tensor * dst = octx->dst; 491 const struct htp_pad_kernel_params * kparams = 492 (const struct htp_pad_kernel_params *) octx->kernel_params; 493 494 const int32_t lp0 = octx->op_params[0]; 495 const int32_t rp0 = octx->op_params[1]; 496 const int32_t lp1 = octx->op_params[2]; 497 const int32_t rp1 = octx->op_params[3]; 498 const int32_t lp2 = octx->op_params[4]; 499 const int32_t rp2 = octx->op_params[5]; 500 const int32_t lp3 = octx->op_params[6]; 501 const int32_t rp3 = octx->op_params[7]; 502 const int32_t circular = octx->op_params[8]; 503 504 const uint32_t total_dst_rows = kparams->total_rows; 505 const size_t type_size = kparams->elem_size; 506 const size_t src_row_size = src0->ne[0] * type_size; 507 const size_t dst_row_size = dst->ne[0] * type_size; 508 509 uint32_t row_start = 0; 510 uint32_t nrows = total_dst_rows; 511 512 if (octx->ctx->mdev.count > 1) { 513 uint32_t rows_per_chunk = 0; 514 htp_tensor_mdev_rows_per_chunk(dst, type_size, (uint32_t) dst_row_size, &rows_per_chunk); 515 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_dst_rows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 516 row_start = range.start; 517 nrows = range.count; 518 } 519 520 if (nrows == 0) { 521 return HTP_STATUS_OK; 522 } 523 524 if (!htp_ops_context_set_n_threads(octx, MIN(kparams->n_threads, nrows))) { 525 return HTP_STATUS_INVAL_PARAMS; 526 } 527 528 const uint32_t n_threads = octx->n_threads; 529 530 if (!kparams->use_dma && (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst))) { 531 return HTP_STATUS_NO_SUPPORT; 532 } 533 534 struct htp_pad_vtcm_layout layout = { 0 }; 535 if (kparams->use_dma) { 536 htp_pad_vtcm_layout_build(&layout, src_row_size, dst_row_size, n_threads); 537 538 octx->src0_spad.size_per_thread = layout.src0_bytes_per_thread; 539 octx->dst_spad.size_per_thread = layout.dst_bytes_per_thread; 540 octx->src0_spad.size = n_threads * layout.src0_bytes_per_thread; 541 octx->dst_spad.size = n_threads * layout.dst_bytes_per_thread; 542 octx->src0_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_src0); 543 octx->dst_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_dst); 544 octx->src0_spad.src = NULL; 545 octx->dst_spad.src = NULL; 546 } 547 548 struct htp_pad_context pctx = { 549 .octx = octx, 550 .kparams = kparams, 551 .lp0 = lp0, .rp0 = rp0, 552 .lp1 = lp1, .rp1 = rp1, 553 .lp2 = lp2, .rp2 = rp2, 554 .lp3 = lp3, .rp3 = rp3, 555 .nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div), 556 .total_dst_rows = nrows, 557 .row_start = row_start, 558 .type_size = type_size, 559 .src_row_size = src_row_size, 560 .src_row_size_aligned = layout.src_row_size_aligned, 561 .dst_row_size = dst_row_size, 562 .dst_row_size_aligned = layout.dst_row_size_aligned, 563 }; 564 565 FARF(HIGH, "pad-hvx%s%s: (%ux%ux%ux%u) -> (%ux%ux%ux%u) pads=(%d,%d,%d,%d,%d,%d,%d,%d)\n", 566 circular ? "-circ" : "", 567 kparams->use_dma ? "-dma" : "", 568 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], 569 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 570 lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3); 571 572 switch (kparams->kernel_type) { 573 case HTP_PAD_KERNEL_HVX_CIRCULAR_DMA: 574 work_queue_run(octx->ctx->work_queue, pad_job_per_thread_hvx_circular_dma, &pctx, n_threads); 575 break; 576 case HTP_PAD_KERNEL_HVX_CIRCULAR: 577 work_queue_run(octx->ctx->work_queue, pad_job_per_thread_hvx_circular, &pctx, n_threads); 578 break; 579 case HTP_PAD_KERNEL_HVX_DMA: 580 work_queue_run(octx->ctx->work_queue, pad_job_per_thread_hvx_dma, &pctx, n_threads); 581 break; 582 default: 583 work_queue_run(octx->ctx->work_queue, pad_job_per_thread_hvx, &pctx, n_threads); 584 break; 585 } 586 587 return HTP_STATUS_OK; 588 }