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/cpy-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 / cpy-ops.c` 702 lines ยท 34655 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 #include 8 9 #include 10 #include 11 12 #define GGML_COMMON_DECL_C 13 #include "ggml-common.h" 14 #include "cpy-ops.h" 15 #include "dma-copy.h" 16 #include "htp-ctx.h" 17 #include "htp-fence.h" 18 #include "htp-ops.h" 19 #include "htp-tensor.h" 20 #include "hvx-utils.h" 21 #include "gather-rows.h" 22 23 struct htp_copy_context { 24 struct htp_ops_context * octx; 25 const struct htp_copy_kernel_params * kparams; 26 27 uint32_t row_start; 28 uint32_t nrows; 29 uint32_t src0_nrows_per_thread; 30 31 uint32_t elem_start; 32 uint32_t nelem; 33 uint32_t elem_per_thread; 34 35 struct htp_copy_convert_vtcm_layout layout; 36 uint8_t * vtcm_src0; 37 uint8_t * vtcm_dst; 38 39 uint32_t tile_start; 40 uint32_t ntiles; 41 uint32_t tiles_per_thread; 42 }; 43 44 #define cpy_preamble \ 45 const struct htp_tensor *src0 = octx->src[0]; \ 46 const struct htp_tensor *dst = octx->dst; \ 47 \ 48 const uint32_t ne00 = src0->ne[0]; \ 49 const uint32_t ne01 = src0->ne[1]; \ 50 const uint32_t ne02 = src0->ne[2]; \ 51 const uint32_t ne03 = src0->ne[3]; \ 52 \ 53 const uint32_t nb00 = src0->nb[0]; \ 54 const uint32_t nb01 = src0->nb[1]; \ 55 const uint32_t nb02 = src0->nb[2]; \ 56 const uint32_t nb03 = src0->nb[3]; \ 57 \ 58 const uint32_t ne0 = dst->ne[0]; \ 59 const uint32_t ne1 = dst->ne[1]; \ 60 const uint32_t ne2 = dst->ne[2]; \ 61 const uint32_t ne3 = dst->ne[3]; \ 62 \ 63 const uint32_t nb0 = dst->nb[0]; \ 64 const uint32_t nb1 = dst->nb[1]; \ 65 const uint32_t nb2 = dst->nb[2]; \ 66 const uint32_t nb3 = dst->nb[3]; 67 68 #define DEFINE_CPY_RESHAPE(NAME, ELEM_TYPE, ELEM_SIZE) \ 69 static void cpy_thread_##NAME##_reshape(unsigned int nth, unsigned int ith, void * data) { \ 70 struct htp_copy_context * ct = (struct htp_copy_context *) data; \ 71 struct htp_ops_context * octx = ct->octx; \ 72 cpy_preamble; \ 73 const uint32_t th_nelem = ct->elem_per_thread; \ 74 const uint32_t th_start = ct->elem_start + ith * th_nelem; \ 75 const uint32_t th_end = MIN(th_start + th_nelem, ct->elem_start + ct->nelem); \ 76 if (th_start >= th_end) return; \ 77 \ 78 dma_queue * dma_q = octx->ctx->dma[ith]; \ 79 \ 80 const uint32_t ne01_ne00 = ne01 * ne00; \ 81 const uint32_t ne02_ne01_ne00 = ne02 * ne01_ne00; \ 82 const uint32_t ne1_ne0 = ne1 * ne0; \ 83 const uint32_t ne2_ne1_ne0 = ne2 * ne1_ne0; \ 84 \ 85 const struct htp_copy_reshape_params * rsh = &ct->kparams->u.reshape; \ 86 uint32_t e = th_start; \ 87 uint32_t i13 = fastdiv(e, &rsh->div_ne2_ne1_ne0); \ 88 uint32_t rem = e - i13 * ne2_ne1_ne0; \ 89 uint32_t i12 = fastdiv(rem, &rsh->div_ne1_ne0); \ 90 uint32_t rem2 = rem - i12 * ne1_ne0; \ 91 uint32_t i11 = fastdiv(rem2, &rsh->div_ne0); \ 92 uint32_t i10 = rem2 - i11 * ne0; \ 93 \ 94 uint32_t i03 = fastdiv(e, &rsh->div_ne02_ne01_ne00); \ 95 uint32_t rem_s = e - i03 * ne02_ne01_ne00; \ 96 uint32_t i02 = fastdiv(rem_s, &rsh->div_ne01_ne00); \ 97 uint32_t rem2_s = rem_s - i02 * ne01_ne00; \ 98 uint32_t i01 = fastdiv(rem2_s, &rsh->div_ne00); \ 99 uint32_t i00 = rem2_s - i01 * ne00; \ 100 \ 101 dma_addr_t dst_addr = dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3; \ 102 dma_addr_t src0_addr = src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03; \ 103 \ 104 const bool rows_contig = (nb00 == ELEM_SIZE) && (nb0 == ELEM_SIZE); \ 105 \ 106 while (e < th_end) { \ 107 const uint32_t run = MIN(MIN(ne00 - i00, ne0 - i10), th_end - e); \ 108 if (rows_contig) { \ 109 dma_cpy_sametype_reshape_contig(dma_q, dst_addr, src0_addr, run * ELEM_SIZE); \ 110 } else { \ 111 dma_cpy_push_2d_chunked(dma_q, dst_addr, src0_addr, nb0, nb00, ELEM_SIZE, run); \ 112 } \ 113 e += run; \ 114 \ 115 dst_addr += run * nb0; \ 116 i10 += run; \ 117 if (i10 == ne0) { \ 118 i10 = 0; \ 119 if (++i11 == ne1) { \ 120 i11 = 0; \ 121 if (++i12 == ne2) { \ 122 i12 = 0; \ 123 i13++; \ 124 } \ 125 } \ 126 dst_addr = dst->data + i11*nb1 + i12*nb2 + i13*nb3; \ 127 } \ 128 \ 129 src0_addr += run * nb00; \ 130 i00 += run; \ 131 if (i00 == ne00) { \ 132 i00 = 0; \ 133 if (++i01 == ne01) { \ 134 i01 = 0; \ 135 if (++i02 == ne02) { \ 136 i02 = 0; \ 137 i03++; \ 138 } \ 139 } \ 140 src0_addr = src0->data + i01*nb01 + i02*nb02 + i03*nb03; \ 141 } \ 142 } \ 143 dma_queue_flush(dma_q); \ 144 } 145 146 DEFINE_CPY_RESHAPE(f32, float, 4) 147 DEFINE_CPY_RESHAPE(f16, __fp16, 2) 148 DEFINE_CPY_RESHAPE(i32, int32_t, 4) 149 150 struct cpy_row_cursor { 151 uint32_t r; 152 uint32_t i01; 153 uint32_t i02; 154 uint32_t i03; 155 }; 156 157 static inline void cpy_cursor_advance(struct cpy_row_cursor * c, uint32_t n, uint32_t ne01, uint32_t ne02) { 158 c->r += n; 159 c->i01 += n; 160 if (c->i01 == ne01) { 161 c->i01 = 0; 162 if (++c->i02 == ne02) { 163 c->i02 = 0; 164 c->i03++; 165 } 166 } 167 } 168 169 struct cpy_tile_cursor { 170 uint32_t ibk; 171 uint32_t ib0; 172 uint32_t i2; 173 uint32_t i3; 174 }; 175 176 static inline void cpy_tile_cursor_advance(struct cpy_tile_cursor * c, const struct htp_copy_transpose_params * tp) { 177 if (++c->ibk == tp->nkb) { 178 c->ibk = 0; 179 if (++c->ib0 == tp->n0b) { 180 c->ib0 = 0; 181 if (++c->i2 == tp->ne[2]) { 182 c->i2 = 0; 183 c->i3++; 184 } 185 } 186 } 187 } 188 189 // Gather offsets for element e = l >> log2_m at (e % r, e / r) 190 static inline HVX_Vector cpy_transpose_offsets(const struct htp_copy_transpose_params * tp) { 191 const uint32_t r_mask = (1u << tp->log2_r) - 1; 192 if (tp->elem_size != 2) { 193 const uint32_t m_mask = (1u << tp->log2_m) - 1; 194 int32_t offsets[32] __attribute__((aligned(128))); 195 for (uint32_t l = 0; l < 32; l++) { 196 const uint32_t e = l >> tp->log2_m; 197 offsets[l] = (e & r_mask) * tp->in_stride + (e >> tp->log2_r) * tp->elem_size + (l & m_mask) * 4; 198 } 199 return *(HVX_Vector *) offsets; 200 } 201 int16_t offsets[64] __attribute__((aligned(128))); 202 for (uint32_t l = 0; l < 64; l++) { 203 offsets[l] = (l & r_mask) * tp->in_stride + (l >> tp->log2_r) * 2; 204 } 205 return *(HVX_Vector *) offsets; 206 } 207 208 static inline void cpy_transpose_push_in(dma_queue * q, const struct htp_copy_transpose_params * tp, dma_addr_t src, 209 const struct cpy_tile_cursor * c, uint8_t * buf) { 210 const uint32_t a0 = c->ib0 * tp->blk0; 211 const uint32_t ak = c->ibk * tp->blkk; 212 const uint32_t off = a0 * tp->src_nb[0] + ak * tp->elem_size + c->i2 * tp->src_nb[1] + c->i3 * tp->src_nb[2]; 213 dma_queue_push(q, dma_make_data(buf, src + off), tp->in_stride, tp->src_nb[0], 214 MIN(tp->blkk, tp->ne[1] - ak) * tp->elem_size, MIN(tp->blk0, tp->ne[0] - a0)); 215 } 216 217 // Pipelined tile worker: DMA in -> vgather transpose -> DMA out 218 static void cpy_thread_transpose(unsigned int nth, unsigned int ith, void * data) { 219 struct htp_copy_context * ct = (struct htp_copy_context *) data; 220 struct htp_ops_context * octx = ct->octx; 221 const struct htp_copy_transpose_params * tp = &ct->kparams->u.transpose; 222 223 const uint32_t t0 = ct->tile_start + ith * ct->tiles_per_thread; 224 const uint32_t t1 = MIN(t0 + ct->tiles_per_thread, ct->tile_start + ct->ntiles); 225 if (t0 >= t1) { 226 return; 227 } 228 229 dma_queue * dma_q = octx->ctx->dma[ith]; 230 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 231 232 const dma_addr_t src = octx->src[0]->data; 233 const dma_addr_t dst = octx->dst->data; 234 235 const uint32_t es = tp->elem_size; 236 const uint32_t ne0 = tp->ne[0]; 237 const uint32_t nek = tp->ne[1]; 238 const uint32_t in_stride = tp->in_stride; 239 const uint32_t out_stride = tp->out_stride; 240 241 const HVX_Vector offsets = cpy_transpose_offsets(tp); 242 const uint32_t packed_mu = (ne0 - 1) * in_stride + 127; 243 const uint32_t packed_step = 128 >> tp->log2_r; 244 const uint32_t epv = 1u << tp->log2_r; 245 246 uint8_t * in_buf[2]; 247 uint8_t * out_buf[2]; 248 in_buf[0] = ct->vtcm_src0 + ith * 2 * tp->in_buf_size; 249 in_buf[1] = in_buf[0] + tp->in_buf_size; 250 out_buf[0] = ct->vtcm_dst + ith * 2 * tp->out_buf_size; 251 out_buf[1] = out_buf[0] + tp->out_buf_size; 252 253 struct cpy_tile_cursor cur; 254 { 255 const uint32_t q0 = fastdiv(t0, &tp->div_nkb); 256 const uint32_t q1 = fastdiv(q0, &tp->div_n0b); 257 cur.ibk = t0 - q0 * tp->nkb; 258 cur.ib0 = q0 - q1 * tp->n0b; 259 cur.i3 = fastdiv(q1, &tp->div_ne2); 260 cur.i2 = q1 - cur.i3 * tp->ne[2]; 261 } 262 263 struct cpy_tile_cursor nxt = cur; 264 265 cpy_transpose_push_in(dma_q, tp, src, &cur, in_buf[0]); 266 267 for (uint32_t t = t0, i = 0; t < t1; t++, i++) { 268 while (dma_queue_depth(dma_q) > (i > 0 ? 1u : 0u)) { 269 dma_queue_pop(dma_q); 270 } 271 272 uint8_t * in = in_buf[i & 1]; 273 uint8_t * out = out_buf[i & 1]; 274 275 const uint32_t a0 = cur.ib0 * tp->blk0; 276 const uint32_t ak = cur.ibk * tp->blkk; 277 const uint32_t n0 = MIN(tp->blk0, ne0 - a0); 278 const uint32_t nk = MIN(tp->blkk, nek - ak); 279 280 if (t + 1 < t1) { 281 cpy_tile_cursor_advance(&nxt, tp); 282 cpy_transpose_push_in(dma_q, tp, src, &nxt, in_buf[(i + 1) & 1]); 283 } 284 285 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) t); 286 if (tp->packed) { 287 const uint32_t nvec = (nk * ne0 * es + 127) >> 7; 288 if (es != 2) { 289 #pragma unroll(4) 290 for (uint32_t v = 0; v < nvec; v++) { 291 Q6_vgather_ARMVw((HVX_Vector *) (out + v * 128), (size_t) (in + v * packed_step), packed_mu, offsets); 292 } 293 } else { 294 #pragma unroll(4) 295 for (uint32_t v = 0; v < nvec; v++) { 296 Q6_vgather_ARMVh((HVX_Vector *) (out + v * 128), (size_t) (in + v * packed_step), packed_mu, offsets); 297 } 298 } 299 } else { 300 for (uint32_t rb = 0; rb < n0; rb += epv) { 301 const uint32_t mu = (MIN(epv, n0 - rb) - 1) * in_stride + es - 1; 302 uint8_t * d = out + rb * es; 303 const uint8_t * s = in + rb * in_stride; 304 if (es != 2) { 305 #pragma unroll(4) 306 for (uint32_t ik = 0; ik < nk; ik++) { 307 Q6_vgather_ARMVw((HVX_Vector *) (d + ik * out_stride), (size_t) (s + ik * es), mu, offsets); 308 } 309 } else { 310 #pragma unroll(4) 311 for (uint32_t ik = 0; ik < nk; ik++) { 312 Q6_vgather_ARMVh((HVX_Vector *) (d + ik * out_stride), (size_t) (s + ik * 2), mu, offsets); 313 } 314 } 315 } 316 } 317 hvx_gather_sync(out); 318 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) t); 319 320 const uint32_t doff = a0 * es + ak * tp->dst_nb[0] + cur.i2 * tp->dst_nb[1] + cur.i3 * tp->dst_nb[2]; 321 if (tp->packed) { 322 const uint32_t n = nk * ne0 * es; 323 dma_queue_push(dma_q, dma_make_data(dst + doff, out), n, n, n, 1); 324 } else { 325 dma_queue_push(dma_q, dma_make_data(dst + doff, out), tp->dst_nb[0], out_stride, n0 * es, nk); 326 } 327 328 cur = nxt; 329 } 330 331 dma_queue_flush(dma_q); 332 } 333 334 #define DEFINE_CPY_CONVERT_SAMESHAPE(NAME, CONV_FUNC) \ 335 static void cpy_thread_##NAME##_sameshape(unsigned int nth, unsigned int ith, void * data) { \ 336 struct htp_copy_context * ct = (struct htp_copy_context *) data; \ 337 struct htp_ops_context * octx = ct->octx; \ 338 cpy_preamble; \ 339 \ 340 const uint32_t dr = ct->src0_nrows_per_thread; \ 341 const uint32_t ir0 = ct->row_start + dr * ith; \ 342 const uint32_t ir1 = MIN(ir0 + dr, ct->row_start + ct->nrows); \ 343 if (ir0 >= ir1) return; \ 344 \ 345 dma_queue * dma_q = octx->ctx->dma[ith]; \ 346 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; \ 347 \ 348 const struct htp_copy_convert_params * cvt = &ct->kparams->u.convert; \ 349 const struct htp_copy_convert_vtcm_layout * L = &ct->layout; \ 350 const uint32_t src0_buf_size = L->src0_buf_size; \ 351 const uint32_t dst_buf_size = L->dst_buf_size; \ 352 const uint32_t src0_row_stride = L->src0_row_stride; \ 353 const uint32_t dst_row_stride = L->dst_row_stride; \ 354 const uint32_t blk_rows = L->blk_rows; \ 355 uint8_t * vtcm_src0_base = ct->vtcm_src0 + ith * L->spad0_size_per_thread; \ 356 uint8_t * vtcm_dst_base = ct->vtcm_dst + ith * L->spad1_size_per_thread; \ 357 const uint32_t src0_row_size = ne00 * ct->kparams->src0_type_size; \ 358 const uint32_t dst_row_size = ne00 * ct->kparams->dst_type_size; \ 359 \ 360 const uint32_t ne02_ne01 = ne02 * ne01; \ 361 uint32_t i03 = fastdiv(ir0, &cvt->div_ne02_ne01); \ 362 uint32_t rem = ir0 - i03 * ne02_ne01; \ 363 uint32_t i02 = fastdiv(rem, &cvt->div_ne01); \ 364 uint32_t i01 = rem - i02 * ne01; \ 365 \ 366 struct cpy_row_cursor f = { ir0, i01, i02, i03 }; \ 367 struct cpy_row_cursor c = f; \ 368 \ 369 for (uint32_t b = 0; b < 2 && f.r < ir1; ++b) { \ 370 uint8_t * src_spad = vtcm_src0_base + b * src0_buf_size; \ 371 uint8_t * dst_spad = vtcm_dst_base + b * dst_buf_size; \ 372 const uint32_t nr = MIN(blk_rows, MIN(ne01 - f.i01, ir1 - f.r)); \ 373 dma_addr_t f_src0_addr = src0->data + f.i01*nb01 + f.i02*nb02 + f.i03*nb03; \ 374 dma_queue_push(dma_q, dma_make_data(dst->data, dst_spad), \ 375 dst_row_size, dst_buf_size, dst_row_size, 0); \ 376 dma_queue_push(dma_q, dma_make_data(src_spad, f_src0_addr), \ 377 src0_row_stride, nb01, src0_row_size, nr); \ 378 cpy_cursor_advance(&f, nr, ne01, ne02); \ 379 } \ 380 \ 381 for (uint32_t blk = 0; c.r < ir1; ++blk) { \ 382 const uint32_t nr = MIN(blk_rows, MIN(ne01 - c.i01, ir1 - c.r)); \ 383 uint8_t * dst_spad = (uint8_t *) (uintptr_t) dma_queue_pop(dma_q).src; \ 384 uint8_t * src_spad = (uint8_t *) (uintptr_t) dma_queue_pop(dma_q).dst; \ 385 \ 386 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) blk); \ 387 for (uint32_t r = 0; r < nr; ++r) { \ 388 CONV_FUNC(dst_spad + r * dst_row_stride, src_spad + r * src0_row_stride, ne00); \ 389 } \ 390 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) blk); \ 391 \ 392 dma_addr_t c_dst_addr = dst->data + c.i01*nb1 + c.i02*nb2 + c.i03*nb3; \ 393 dma_queue_push(dma_q, dma_make_data(c_dst_addr, dst_spad), \ 394 nb1, dst_row_stride, dst_row_size, nr); \ 395 cpy_cursor_advance(&c, nr, ne01, ne02); \ 396 \ 397 if (f.r < ir1) { \ 398 const uint32_t fnr = MIN(blk_rows, MIN(ne01 - f.i01, ir1 - f.r)); \ 399 dma_addr_t f_src0_addr = src0->data + f.i01*nb01 + f.i02*nb02 + f.i03*nb03; \ 400 dma_queue_push(dma_q, dma_make_data(src_spad, f_src0_addr), \ 401 src0_row_stride, nb01, src0_row_size, fnr); \ 402 cpy_cursor_advance(&f, fnr, ne01, ne02); \ 403 } \ 404 } \ 405 dma_queue_flush(dma_q); \ 406 } 407 408 DEFINE_CPY_CONVERT_SAMESHAPE(f16_f32, hvx_copy_f16_f32_aa) 409 DEFINE_CPY_CONVERT_SAMESHAPE(f32_f16, hvx_copy_f32_f16_aa) 410 DEFINE_CPY_CONVERT_SAMESHAPE(i32_f32, hvx_copy_i32_f32_aa) 411 DEFINE_CPY_CONVERT_SAMESHAPE(f32_i32, hvx_copy_f32_i32_aa) 412 413 static int cpy_scalar(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) { 414 if (octx->ctx->mdev.count > 1 && octx->ctx->mdev.idx > 0) { 415 return HTP_STATUS_OK; 416 } 417 418 const struct htp_tensor * src0 = octx->src[0]; 419 const struct htp_tensor * dst = octx->dst; 420 421 if (src0->type == dst->type) { 422 dma_cpy_sametype_reshape_contig(octx->ctx->dma[0], dst->data, src0->data, kparams->src0_type_size); 423 dma_queue_flush(octx->ctx->dma[0]); 424 return HTP_STATUS_OK; 425 } 426 427 dma_queue * dma_q = octx->ctx->dma[0]; 428 uint8_t * s_ptr = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, 0); 429 uint8_t * d_ptr = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, VLEN); 430 dma_addr_t s_vtcm = (dma_addr_t)(uintptr_t) s_ptr; 431 dma_addr_t d_vtcm = (dma_addr_t)(uintptr_t) d_ptr; 432 const uint32_t s_size = kparams->src0_type_size; 433 const uint32_t d_size = kparams->dst_type_size; 434 435 dma_queue_push(dma_q, dma_make_data(s_vtcm, src0->data), s_size, s_size, s_size, 1); 436 dma_queue_pop(dma_q); 437 438 const HVX_Vector v_src = hvx_vmem(s_ptr); 439 HVX_Vector v_dst; 440 441 if (src0->type == HTP_TYPE_F32 && dst->type == HTP_TYPE_I32) { 442 v_dst = Q6_Vw_equals_Vsf(v_src); 443 } else if (src0->type == HTP_TYPE_I32 && dst->type == HTP_TYPE_F32) { 444 v_dst = Q6_Vsf_equals_Vw(v_src); 445 } else if (src0->type == HTP_TYPE_F32 && dst->type == HTP_TYPE_F16) { 446 v_dst = hvx_vec_f32_to_f16(v_src, v_src); 447 } else if (src0->type == HTP_TYPE_F16 && dst->type == HTP_TYPE_F32) { 448 v_dst = Q6_V_lo_W(hvx_vec_f16_to_f32(v_src)); 449 } else { 450 return HTP_STATUS_NO_SUPPORT; 451 } 452 453 hvx_vmem(d_ptr) = v_dst; 454 455 dma_queue_push(dma_q, dma_make_data(dst->data, d_vtcm), d_size, d_size, d_size, 1); 456 dma_queue_flush(dma_q); 457 return HTP_STATUS_OK; 458 } 459 460 static int cpy_1d_contig(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) { 461 const struct htp_tensor * src0 = octx->src[0]; 462 const struct htp_tensor * dst = octx->dst; 463 464 uint32_t elem_start = 0; 465 uint32_t nelem = kparams->total_elems; 466 467 if (octx->ctx->mdev.count > 1) { 468 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 469 kparams->total_elems, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 470 elem_start = range.start; 471 nelem = range.count; 472 } 473 474 if (nelem > 0) { 475 dma_queue * q = octx->ctx->dma[0]; 476 const uint32_t type_size = kparams->src0_type_size; 477 dma_addr_t dst_addr = dst->data + elem_start * type_size; 478 dma_addr_t src0_addr = src0->data + elem_start * type_size; 479 dma_cpy_sametype_reshape_contig(q, dst_addr, src0_addr, nelem * type_size); 480 dma_queue_flush(q); 481 } 482 483 return HTP_STATUS_OK; 484 } 485 486 static int cpy_sameshape_sametype(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) { 487 const struct htp_tensor * src0 = octx->src[0]; 488 const struct htp_tensor * dst = octx->dst; 489 490 uint32_t row_start = 0; 491 uint32_t nrows = kparams->total_rows; 492 493 if (octx->ctx->mdev.count > 1) { 494 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 495 kparams->total_rows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 496 row_start = range.start; 497 nrows = range.count; 498 } 499 500 if (nrows > 0) { 501 dma_queue * q = octx->ctx->dma[0]; 502 dma_cpy_sametype_sameshape_range(q, dst, src0, kparams->src0_type_size, row_start, nrows); 503 dma_queue_flush(q); 504 } 505 506 return HTP_STATUS_OK; 507 } 508 509 static int cpy_sameshape_convert(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) { 510 const struct htp_tensor * src0 = octx->src[0]; 511 const struct htp_tensor * dst = octx->dst; 512 513 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 514 return HTP_STATUS_INVAL_PARAMS; 515 } 516 517 uint32_t row_start = 0; 518 uint32_t nrows = kparams->total_rows; 519 520 if (octx->ctx->mdev.count > 1) { 521 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 522 kparams->total_rows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 523 row_start = range.start; 524 nrows = range.count; 525 } 526 527 if (nrows == 0) { 528 return HTP_STATUS_OK; 529 } 530 531 if (kparams->vtcm_size > octx->ctx->vtcm_size) { 532 return HTP_STATUS_VTCM_TOO_SMALL; 533 } 534 535 const uint32_t n_threads = octx->n_threads; 536 const struct htp_copy_convert_params * cvt = &kparams->u.convert; 537 538 struct htp_copy_context ct; 539 htp_copy_convert_vtcm_layout_build(&ct.layout, src0->ne[0], kparams->src0_type_size, kparams->dst_type_size, cvt->blk_rows, n_threads); 540 541 ct.octx = octx; 542 ct.kparams = kparams; 543 ct.row_start = row_start; 544 ct.nrows = nrows; 545 ct.src0_nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div); 546 547 uint8_t * vtcm_base = (uint8_t *) octx->ctx->vtcm_base; 548 ct.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, ct.layout.off_src0); 549 ct.vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, ct.layout.off_dst); 550 551 work_queue_func_t copy_fun = NULL; 552 if (dst->type == HTP_TYPE_F16 && src0->type == HTP_TYPE_F32) { 553 copy_fun = cpy_thread_f16_f32_sameshape; 554 } else if (dst->type == HTP_TYPE_F32 && src0->type == HTP_TYPE_F16) { 555 copy_fun = cpy_thread_f32_f16_sameshape; 556 } else if (dst->type == HTP_TYPE_I32 && src0->type == HTP_TYPE_F32) { 557 copy_fun = cpy_thread_i32_f32_sameshape; 558 } else if (dst->type == HTP_TYPE_F32 && src0->type == HTP_TYPE_I32) { 559 copy_fun = cpy_thread_f32_i32_sameshape; 560 } else { 561 return HTP_STATUS_NO_SUPPORT; 562 } 563 564 work_queue_run(octx->ctx->work_queue, copy_fun, &ct, n_threads); 565 return HTP_STATUS_OK; 566 } 567 568 static int cpy_reshape(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) { 569 const struct htp_tensor * src0 = octx->src[0]; 570 const struct htp_tensor * dst = octx->dst; 571 572 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 573 return HTP_STATUS_INVAL_PARAMS; 574 } 575 576 uint32_t elem_start = 0; 577 uint32_t nelem = kparams->total_elems; 578 579 if (octx->ctx->mdev.count > 1) { 580 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 581 kparams->total_elems, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 582 elem_start = range.start; 583 nelem = range.count; 584 } 585 586 if (nelem == 0) { 587 return HTP_STATUS_OK; 588 } 589 590 const uint32_t n_threads = octx->n_threads; 591 592 struct htp_copy_context ct; 593 ct.octx = octx; 594 ct.kparams = kparams; 595 ct.elem_start = elem_start; 596 ct.nelem = nelem; 597 ct.elem_per_thread = fastdiv(nelem + n_threads - 1, &octx->n_threads_div); 598 599 work_queue_func_t copy_fun = NULL; 600 switch (src0->type) { 601 case HTP_TYPE_F32: copy_fun = cpy_thread_f32_reshape; break; 602 case HTP_TYPE_F16: copy_fun = cpy_thread_f16_reshape; break; 603 case HTP_TYPE_I32: copy_fun = cpy_thread_i32_reshape; break; 604 default: return HTP_STATUS_NO_SUPPORT; 605 } 606 607 work_queue_run(octx->ctx->work_queue, copy_fun, &ct, n_threads); 608 return HTP_STATUS_OK; 609 } 610 611 static int cpy_transpose(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) { 612 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 613 return HTP_STATUS_INVAL_PARAMS; 614 } 615 616 const struct htp_copy_transpose_params * tp = &kparams->u.transpose; 617 618 uint32_t tile_start = 0; 619 uint32_t ntiles = tp->total_tiles; 620 621 if (octx->ctx->mdev.count > 1) { 622 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 623 tp->total_tiles, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 624 tile_start = range.start; 625 ntiles = range.count; 626 } 627 628 if (ntiles == 0) { 629 return HTP_STATUS_OK; 630 } 631 632 if (kparams->vtcm_size > octx->ctx->vtcm_size) { 633 return HTP_STATUS_VTCM_TOO_SMALL; 634 } 635 636 const uint32_t n_threads = octx->n_threads; 637 638 struct htp_copy_context ct; 639 ct.octx = octx; 640 ct.kparams = kparams; 641 ct.tile_start = tile_start; 642 ct.ntiles = ntiles; 643 ct.tiles_per_thread = fastdiv(ntiles + n_threads - 1, &octx->n_threads_div); 644 645 uint8_t * vtcm_base = (uint8_t *) octx->ctx->vtcm_base; 646 ct.vtcm_src0 = vtcm_base; 647 ct.vtcm_dst = vtcm_base + (size_t) n_threads * 2 * tp->in_buf_size; 648 649 work_queue_run(octx->ctx->work_queue, cpy_thread_transpose, &ct, n_threads); 650 return HTP_STATUS_OK; 651 } 652 653 int op_cpy(struct htp_ops_context * octx) { 654 const struct htp_copy_kernel_params * kparams = (const struct htp_copy_kernel_params *) octx->kernel_params; 655 int status = HTP_STATUS_OK; 656 657 switch (kparams->kernel_type) { 658 case HTP_COPY_KERNEL_SCALAR: 659 status = cpy_scalar(octx, kparams); 660 break; 661 case HTP_COPY_KERNEL_1D_CONTIG: 662 status = cpy_1d_contig(octx, kparams); 663 break; 664 case HTP_COPY_KERNEL_SAMESHAPE_SAMETYPE: 665 status = cpy_sameshape_sametype(octx, kparams); 666 break; 667 case HTP_COPY_KERNEL_SAMESHAPE_CONVERT: 668 status = cpy_sameshape_convert(octx, kparams); 669 break; 670 case HTP_COPY_KERNEL_RESHAPE: 671 status = cpy_reshape(octx, kparams); 672 break; 673 case HTP_COPY_KERNEL_TRANSPOSE: 674 status = cpy_transpose(octx, kparams); 675 break; 676 case HTP_COPY_KERNEL_GATHER_ROWS: 677 status = htp_gather_rows(octx, octx->src[0], NULL, &kparams->u.gather, kparams->n_threads); 678 break; 679 default: 680 status = HTP_STATUS_NO_SUPPORT; 681 break; 682 } 683 684 htp_ops_context_set_status(octx, status); 685 686 if (octx->op == HTP_OP_CPY_FENCE) { 687 if (octx->ctx->mdev.count > 1) { 688 htp_mdev_group_barrier(octx); 689 } 690 691 if (octx->ctx->mdev.idx == 0) { 692 const struct htp_tensor * sync = octx->src[1]; 693 const uint32_t seq = (uint32_t) octx->op_params[0]; 694 atomic_uint * sync_fence = (atomic_uint *) (uintptr_t) sync->data; 695 htp_fence_write(sync_fence, seq, octx->status); 696 697 FARF(HIGH, "ggml-hex: sync-release : fence %p seq 0x%x status %d\n", sync_fence, seq, octx->status); 698 } 699 } 700 701 return octx->status; 702 }