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/concat-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 / concat-ops.c` 391 lines ยท 17135 bytes Raw Blame History 1 #include "concat-ops.h" 2 #include "dma-queue.h" 3 #include "hex-common.h" 4 #include "dma-copy.h" 5 #include "hex-fastdiv.h" 6 #include "hex-profile.h" 7 #include "hexagon_protos.h" 8 #include "hexagon_types.h" 9 #include "htp-ctx.h" 10 #include "htp-fence.h" 11 #include "htp-ops.h" 12 #include "htp-tensor.h" 13 #include "htp-vtcm.h" 14 #include "hvx-utils.h" 15 #include "hvx_hexagon_protos.h" 16 #include "gather-rows.h" 17 18 #include 19 20 struct htp_concat_context { 21 struct htp_ops_context * octx; 22 uint8_t * spad0_base; 23 uint8_t * spad1_base; 24 uint32_t spad0_size_per_thread; 25 uint32_t spad1_size_per_thread; 26 uint32_t row_start; 27 uint32_t nrows; 28 uint32_t nrows_per_thread; 29 uint32_t nplanes; 30 struct fastdiv_values div_ne2; 31 }; 32 33 static inline dma_addr_t concat_plane_addr(const struct htp_tensor * t, uint32_t p, const struct fastdiv_values * div_ne2, uint32_t ne2) { 34 const uint32_t i3 = fastdiv(p, div_ne2); 35 const uint32_t i2 = p - i3 * ne2; 36 return t->data + i2 * t->nb[2] + i3 * t->nb[3]; 37 } 38 39 static void concat_2d_f32_transposed(unsigned int nth, unsigned int ith, void * data) { 40 struct htp_concat_context * cctx = (struct htp_concat_context *) data; 41 struct htp_ops_context * octx = cctx->octx; 42 43 const struct htp_tensor * src0 = octx->src[0]; 44 const struct htp_tensor * src1 = octx->src[1]; 45 const struct htp_tensor * dst = octx->dst; 46 47 const uint32_t src0_ne0 = src0->ne[0]; 48 const uint32_t src1_ne0 = src1->ne[0]; 49 50 // dst rows [row_begin, row_end) of this thread; a dst row is a src0 row followed by a src1 column 51 const uint32_t row_begin = cctx->row_start + ith * cctx->nrows_per_thread; 52 const uint32_t row_end = MIN(row_begin + cctx->nrows_per_thread, cctx->row_start + cctx->nrows); 53 if (row_begin >= row_end) return; 54 55 dma_queue * dma_q = octx->ctx->dma[ith]; 56 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 57 58 const struct fastdiv_values * div_ne2 = &cctx->div_ne2; 59 const uint32_t ne2 = dst->ne[2]; 60 61 // tile: block_rows dst rows x block_cols src1 columns 62 const uint32_t block_rows = 128; 63 const uint32_t block_cols = 256; 64 // +1 element spreads the gather lanes across VTCM, so they do not serialize on one path 65 const uint32_t spad1_stride = block_rows * sizeof(float) + sizeof(float); 66 67 const HVX_Vector offsets = hvx_vec_gather_offsets_w(spad1_stride); 68 const uint32_t block_cols_padded = MIN(hex_round_up(src1_ne0, 32), block_cols); 69 const uint32_t src0_row_bytes = src0_ne0 * sizeof(float); 70 const uint32_t src0_row_padded = hex_round_up(src0_row_bytes, VLEN); 71 const uint32_t src0_pre = src0_row_padded - src0_row_bytes; 72 const uint32_t spad0_stride = src0_row_padded + block_cols_padded * sizeof(float); 73 74 // two slots per scratchpad: tile b gathers in slot b&1 while dst[b-1] and src1[b+1] DMA in the other 75 uint8_t * spad0_base = cctx->spad0_base + ith * cctx->spad0_size_per_thread; 76 uint8_t * spad1_base = cctx->spad1_base + ith * cctx->spad1_size_per_thread; 77 const uint32_t spad0_slot = block_rows * spad0_stride; 78 const uint32_t spad1_slot = block_cols_padded * spad1_stride; 79 80 // tiles walk (plane, row, col) with col fastest; src0 goes out with the col == 0 tile only 81 uint32_t plane = 0; 82 uint32_t row = row_begin; 83 uint32_t col = 0; 84 uint32_t slot = 0; 85 bool dst_pending = false; 86 87 const dma_addr_t src1_addr = concat_plane_addr(src1, plane, div_ne2, ne2) + row * src1->nb[1]; 88 dma_queue_push(dma_q, dma_make_data(spad1_base, src1_addr), spad1_stride, src1->nb[0], MIN(row_end - row, block_rows) * sizeof(float), MIN(src1_ne0, block_cols)); 89 90 const dma_addr_t src0_addr = concat_plane_addr(src0, plane, div_ne2, ne2) + row * src0->nb[1]; 91 dma_queue_push(dma_q, dma_make_data(spad0_base + src0_pre, src0_addr), spad0_stride, src0->nb[1], src0_row_bytes, MIN(row_end - row, block_rows)); 92 93 while (plane < cctx->nplanes) { 94 const uint32_t nrows = MIN(row_end - row, block_rows); 95 const uint32_t ncols = MIN(src1_ne0 - col, block_cols); 96 97 uint8_t * spad0 = spad0_base + slot * spad0_slot; 98 uint8_t * spad1 = spad1_base + slot * spad1_slot; 99 100 dma_queue_pop(dma_q); // src1 101 if (col == 0) { 102 dma_queue_pop(dma_q); // src0 103 } 104 105 uint32_t next_plane = plane; 106 uint32_t next_row = row; 107 uint32_t next_col = col + block_cols; 108 if (next_col >= src1_ne0) { 109 next_col = 0; 110 next_row = row + block_rows; 111 if (next_row >= row_end) { 112 next_row = row_begin; 113 next_plane++; 114 } 115 } 116 const bool has_next = next_plane < cctx->nplanes; 117 const uint32_t next_nrows = MIN(row_end - next_row, block_rows); 118 const uint32_t next_ncols = MIN(src1_ne0 - next_col, block_cols); 119 120 // other spad1 slot is free since the previous gather sync 121 if (has_next) { 122 const dma_addr_t addr = concat_plane_addr(src1, next_plane, div_ne2, ne2) + next_row * src1->nb[1] + next_col * src1->nb[0]; 123 dma_queue_push(dma_q, dma_make_data(spad1_base + (slot ^ 1) * spad1_slot, addr), spad1_stride, src1->nb[0], next_nrows * sizeof(float), next_ncols); 124 } 125 126 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) row); 127 for (uint32_t c = 0; c < ncols; c += 32) { 128 hvx_transpose_32x32_w_gather(spad0 + src0_row_padded + c * sizeof(float), spad0_stride, spad1 + c * spad1_stride, spad1_stride, offsets, nrows, MIN(ncols - c, 32)); 129 } 130 hvx_gather_sync(spad0 + src0_row_padded); 131 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) row); 132 133 // other spad0 slot is free once dst[b-1] is written 134 if (dst_pending) { 135 dma_queue_pop(dma_q); // dst 136 } 137 if (has_next && next_col == 0) { 138 const dma_addr_t addr = concat_plane_addr(src0, next_plane, div_ne2, ne2) + next_row * src0->nb[1]; 139 dma_queue_push(dma_q, dma_make_data(spad0_base + (slot ^ 1) * spad0_slot + src0_pre, addr), spad0_stride, src0->nb[1], src0_row_bytes, next_nrows); 140 } 141 142 // the col == 0 tile also carries the src0 columns in front 143 const uint32_t lead = (col == 0) ? src0_row_bytes : 0; 144 const dma_addr_t dst_addr = concat_plane_addr(dst, plane, div_ne2, ne2) + row * dst->nb[1] + src0_row_bytes + col * sizeof(float) - lead; 145 dma_queue_push(dma_q, dma_make_data(dst_addr, spad0 + src0_row_padded - lead), dst->nb[1], spad0_stride, lead + ncols * sizeof(float), nrows); 146 dst_pending = true; 147 148 plane = next_plane; 149 row = next_row; 150 col = next_col; 151 slot ^= 1; 152 } 153 dma_queue_flush(dma_q); 154 } 155 156 static void concat_2d_f16_transposed(unsigned int nth, unsigned int ith, void * data) { 157 struct htp_concat_context * cctx = (struct htp_concat_context *) data; 158 struct htp_ops_context * octx = cctx->octx; 159 160 const struct htp_tensor * src0 = octx->src[0]; 161 const struct htp_tensor * src1 = octx->src[1]; 162 const struct htp_tensor * dst = octx->dst; 163 164 const uint32_t src0_ne0 = src0->ne[0]; 165 const uint32_t src1_ne0 = src1->ne[0]; 166 167 // dst rows [row_begin, row_end) of this thread; a dst row is a src0 row followed by a src1 column 168 const uint32_t row_begin = cctx->row_start + ith * cctx->nrows_per_thread; 169 const uint32_t row_end = MIN(row_begin + cctx->nrows_per_thread, cctx->row_start + cctx->nrows); 170 if (row_begin >= row_end) return; 171 172 dma_queue * dma_q = octx->ctx->dma[ith]; 173 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 174 175 const struct fastdiv_values * div_ne2 = &cctx->div_ne2; 176 const uint32_t ne2 = dst->ne[2]; 177 178 // tile: block_rows dst rows x block_cols src1 columns 179 const uint32_t block_rows = 128; 180 const uint32_t block_cols = 512; 181 // +1 element spreads the gather lanes across VTCM, so they do not serialize on one path 182 const uint32_t spad1_stride = block_rows * sizeof(__fp16) + sizeof(__fp16); 183 184 const HVX_Vector offsets = hvx_vec_gather_offsets_h(spad1_stride); 185 const uint32_t block_cols_padded = MIN(hex_round_up(src1_ne0, 64), block_cols); 186 const uint32_t src0_row_bytes = src0_ne0 * sizeof(__fp16); 187 const uint32_t src0_row_padded = hex_round_up(src0_row_bytes, VLEN); 188 const uint32_t src0_pre = src0_row_padded - src0_row_bytes; 189 const uint32_t spad0_stride = src0_row_padded + block_cols_padded * sizeof(__fp16); 190 191 // two slots per scratchpad: tile b gathers in slot b&1 while dst[b-1] and src1[b+1] DMA in the other 192 uint8_t * spad0_base = cctx->spad0_base + ith * cctx->spad0_size_per_thread; 193 uint8_t * spad1_base = cctx->spad1_base + ith * cctx->spad1_size_per_thread; 194 const uint32_t spad0_slot = block_rows * spad0_stride; 195 const uint32_t spad1_slot = block_cols_padded * spad1_stride; 196 197 // tiles walk (plane, row, col) with col fastest; src0 goes out with the col == 0 tile only 198 uint32_t plane = 0; 199 uint32_t row = row_begin; 200 uint32_t col = 0; 201 uint32_t slot = 0; 202 bool dst_pending = false; 203 204 const dma_addr_t src1_addr = concat_plane_addr(src1, plane, div_ne2, ne2) + row * src1->nb[1]; 205 dma_queue_push(dma_q, dma_make_data(spad1_base, src1_addr), spad1_stride, src1->nb[0], MIN(row_end - row, block_rows) * sizeof(__fp16), MIN(src1_ne0, block_cols)); 206 207 const dma_addr_t src0_addr = concat_plane_addr(src0, plane, div_ne2, ne2) + row * src0->nb[1]; 208 dma_queue_push(dma_q, dma_make_data(spad0_base + src0_pre, src0_addr), spad0_stride, src0->nb[1], src0_row_bytes, MIN(row_end - row, block_rows)); 209 210 while (plane < cctx->nplanes) { 211 const uint32_t nrows = MIN(row_end - row, block_rows); 212 const uint32_t ncols = MIN(src1_ne0 - col, block_cols); 213 214 uint8_t * spad0 = spad0_base + slot * spad0_slot; 215 uint8_t * spad1 = spad1_base + slot * spad1_slot; 216 217 dma_queue_pop(dma_q); // src1 218 if (col == 0) { 219 dma_queue_pop(dma_q); // src0 220 } 221 222 uint32_t next_plane = plane; 223 uint32_t next_row = row; 224 uint32_t next_col = col + block_cols; 225 if (next_col >= src1_ne0) { 226 next_col = 0; 227 next_row = row + block_rows; 228 if (next_row >= row_end) { 229 next_row = row_begin; 230 next_plane++; 231 } 232 } 233 const bool has_next = next_plane < cctx->nplanes; 234 const uint32_t next_nrows = MIN(row_end - next_row, block_rows); 235 const uint32_t next_ncols = MIN(src1_ne0 - next_col, block_cols); 236 237 // other spad1 slot is free since the previous gather sync 238 if (has_next) { 239 const dma_addr_t addr = concat_plane_addr(src1, next_plane, div_ne2, ne2) + next_row * src1->nb[1] + next_col * src1->nb[0]; 240 dma_queue_push(dma_q, dma_make_data(spad1_base + (slot ^ 1) * spad1_slot, addr), spad1_stride, src1->nb[0], next_nrows * sizeof(__fp16), next_ncols); 241 } 242 243 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) row); 244 for (uint32_t c = 0; c < ncols; c += 64) { 245 hvx_transpose_64x64_h_gather(spad0 + src0_row_padded + c * sizeof(__fp16), spad0_stride, spad1 + c * spad1_stride, spad1_stride, offsets, nrows, MIN(ncols - c, 64)); 246 } 247 hvx_gather_sync(spad0 + src0_row_padded); 248 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) row); 249 250 // other spad0 slot is free once dst[b-1] is written 251 if (dst_pending) { 252 dma_queue_pop(dma_q); // dst 253 } 254 if (has_next && next_col == 0) { 255 const dma_addr_t addr = concat_plane_addr(src0, next_plane, div_ne2, ne2) + next_row * src0->nb[1]; 256 dma_queue_push(dma_q, dma_make_data(spad0_base + (slot ^ 1) * spad0_slot + src0_pre, addr), spad0_stride, src0->nb[1], src0_row_bytes, next_nrows); 257 } 258 259 // the col == 0 tile also carries the src0 columns in front 260 const uint32_t lead = (col == 0) ? src0_row_bytes : 0; 261 const dma_addr_t dst_addr = concat_plane_addr(dst, plane, div_ne2, ne2) + row * dst->nb[1] + src0_row_bytes + col * sizeof(__fp16) - lead; 262 dma_queue_push(dma_q, dma_make_data(dst_addr, spad0 + src0_row_padded - lead), dst->nb[1], spad0_stride, lead + ncols * sizeof(__fp16), nrows); 263 dst_pending = true; 264 265 plane = next_plane; 266 row = next_row; 267 col = next_col; 268 slot ^= 1; 269 } 270 dma_queue_flush(dma_q); 271 } 272 273 static int concat_regular(struct htp_ops_context * octx, int dim, uint32_t type_size) { 274 const struct htp_tensor * src0 = octx->src[0]; 275 const struct htp_tensor * src1 = octx->src[1]; 276 const struct htp_tensor * dst = octx->dst; 277 278 struct htp_tensor view0 = *dst; 279 struct htp_tensor view1 = *dst; 280 for (int d = 0; d < HTP_OP_MAX_DIMS; d++) { 281 view0.ne[d] = src0->ne[d]; 282 view1.ne[d] = src1->ne[d]; 283 } 284 view1.data += src0->ne[dim] * dst->nb[dim]; 285 286 const uint32_t total_rows_0 = src0->ne[1] * src0->ne[2] * src0->ne[3]; 287 const uint32_t total_rows_1 = src1->ne[1] * src1->ne[2] * src1->ne[3]; 288 289 uint32_t rstart0 = 0, nrows0 = total_rows_0; 290 uint32_t rstart1 = 0, nrows1 = total_rows_1; 291 292 if (octx->ctx->mdev.count > 1) { 293 const struct htp_tensor_mdev_range range0 = htp_tensor_mdev_partition( 294 total_rows_0, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 295 rstart0 = range0.start; 296 nrows0 = range0.count; 297 298 const struct htp_tensor_mdev_range range1 = htp_tensor_mdev_partition( 299 total_rows_1, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 300 rstart1 = range1.start; 301 nrows1 = range1.count; 302 } 303 304 dma_queue * q = octx->ctx->dma[0]; 305 306 dma_cpy_sametype_sameshape_range(q, &view0, src0, type_size, rstart0, nrows0); 307 dma_cpy_sametype_sameshape_range(q, &view1, src1, type_size, rstart1, nrows1); 308 dma_queue_flush(q); 309 310 return HTP_STATUS_OK; 311 } 312 313 static int concat_transposed(struct htp_ops_context * octx, const struct htp_concat_kernel_params * kparams, uint32_t type_size) { 314 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 315 return HTP_STATUS_INVAL_PARAMS; 316 } 317 318 const struct htp_tensor * dst = octx->dst; 319 320 const uint32_t total_rows = dst->ne[1]; 321 uint32_t row_start = 0; 322 uint32_t nrows = total_rows; 323 if (octx->ctx->mdev.count > 1) { 324 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_rows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 325 row_start = range.start; 326 nrows = range.count; 327 } 328 329 if (nrows == 0 || dst->ne[2] == 0 || dst->ne[3] == 0) { 330 return HTP_STATUS_OK; 331 } 332 333 if (kparams->vtcm_size > octx->ctx->vtcm_size) { 334 return HTP_STATUS_VTCM_TOO_SMALL; 335 } 336 337 const uint32_t n_threads = octx->n_threads; 338 339 struct htp_concat_transposed_vtcm_layout layout; 340 htp_concat_transposed_vtcm_layout_build(&layout, octx->src[0]->ne[0], octx->src[1]->ne[0], type_size, n_threads); 341 342 uint8_t * vtcm_base = (uint8_t *) octx->ctx->vtcm_base; 343 344 struct htp_concat_context cctx; 345 cctx.octx = octx; 346 cctx.spad0_base = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, layout.off_spad0); 347 cctx.spad1_base = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, layout.off_spad1); 348 cctx.spad0_size_per_thread = layout.src0_spad_size_per_thread; 349 cctx.spad1_size_per_thread = layout.src1_spad_size_per_thread; 350 cctx.row_start = row_start; 351 cctx.nrows = nrows; 352 cctx.nplanes = dst->ne[2] * dst->ne[3]; 353 cctx.div_ne2 = kparams->u.transposed.div_ne2; 354 cctx.nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div); 355 356 work_queue_func_t worker_func = (type_size == 4) ? concat_2d_f32_transposed : concat_2d_f16_transposed; 357 work_queue_run(octx->ctx->work_queue, worker_func, &cctx, n_threads); 358 return HTP_STATUS_OK; 359 } 360 361 int op_concat(struct htp_ops_context * octx) { 362 const struct htp_concat_kernel_params * kparams = (const struct htp_concat_kernel_params *) octx->kernel_params; 363 const struct htp_tensor * src0 = octx->src[0]; 364 const struct htp_tensor * src1 = octx->src[1]; 365 const struct htp_tensor * dst = octx->dst; 366 367 int status = HTP_STATUS_OK; 368 const uint32_t type_size = (dst->type == HTP_TYPE_F32 || dst->type == HTP_TYPE_I32) ? 4 : 2; 369 370 switch (kparams->kernel_type) { 371 case HTP_CONCAT_KERNEL_REGULAR: 372 status = concat_regular(octx, kparams->dim, type_size); 373 break; 374 375 case HTP_CONCAT_KERNEL_TRANSPOSED: 376 status = concat_transposed(octx, kparams, type_size); 377 break; 378 379 case HTP_CONCAT_KERNEL_GATHER_ROWS: 380 status = htp_gather_rows(octx, src0, src1, &kparams->u.gather, kparams->n_threads); 381 break; 382 383 default: 384 status = HTP_STATUS_NO_SUPPORT; 385 break; 386 } 387 388 htp_ops_context_set_status(octx, status); 389 390 return octx->status; 391 }