ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / get-rows-ops.c
| 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 <HAP_perf.h> |
| 7 | |
| 8 | #include <math.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #define GGML_COMMON_DECL_C |
| 12 | #include "ggml-common.h" |
| 13 | #include "hex-common.h" |
| 14 | #include "dma-queue.h" |
| 15 | #include "htp-ctx.h" |
| 16 | #include "htp-ops.h" |
| 17 | #include "htp-tensor.h" |
| 18 | #include "hvx-utils.h" |
| 19 | #include "hvx-quant.h" |
| 20 | #include "get-rows-ops.h" |
| 21 | #include "work-queue.h" |
| 22 | |
| 23 | struct get_rows_context; |
| 24 | |
| 25 | typedef uint32_t (*get_rows_read_index_fn_t)(const void * src1, uint32_t idx, int idx_type); |
| 26 | typedef void (*get_rows_flat_compute_fn_t)(float * dst_spad, const void * src_spad, uint32_t cur_elems); |
| 27 | typedef void (*get_rows_tiled_compute_fn_t)(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row); |
| 28 | |
| 29 | struct get_rows_context { |
| 30 | struct htp_ops_context * octx; |
| 31 | const struct htp_get_rows_kernel_params * kparams; |
| 32 | struct htp_get_rows_vtcm_layout vtcm_layout; |
| 33 | uint8_t * vtcm_base; |
| 34 | const void * src1_data; |
| 35 | get_rows_read_index_fn_t read_index; |
| 36 | get_rows_flat_compute_fn_t compute_flat; |
| 37 | get_rows_tiled_compute_fn_t compute_tiled; |
| 38 | uint32_t task_start; |
| 39 | uint32_t tasks; |
| 40 | uint32_t tasks_per_thread; |
| 41 | uint32_t tile_size; |
| 42 | uint32_t tile_stride; |
| 43 | uint32_t src0_row_size; |
| 44 | uint32_t dst_row_size; |
| 45 | int idx_type; |
| 46 | }; |
| 47 | |
| 48 | #define get_rows_preamble \ |
| 49 | const uint32_t ne00 = octx->src[0]->ne[0]; \ |
| 50 | const uint32_t ne01 = octx->src[0]->ne[1]; \ |
| 51 | const uint32_t ne02 = octx->src[0]->ne[2]; \ |
| 52 | const uint32_t ne03 = octx->src[0]->ne[3]; \ |
| 53 | \ |
| 54 | const uint32_t ne10 = octx->src[1]->ne[0]; \ |
| 55 | const uint32_t ne11 = octx->src[1]->ne[1]; \ |
| 56 | const uint32_t ne12 = octx->src[1]->ne[2]; \ |
| 57 | const uint32_t ne13 = octx->src[1]->ne[3]; \ |
| 58 | \ |
| 59 | const uint32_t ne0 = octx->dst->ne[0]; \ |
| 60 | const uint32_t ne1 = octx->dst->ne[1]; \ |
| 61 | const uint32_t ne2 = octx->dst->ne[2]; \ |
| 62 | const uint32_t ne3 = octx->dst->ne[3]; \ |
| 63 | \ |
| 64 | const uint32_t nb01 = octx->src[0]->nb[1]; \ |
| 65 | const uint32_t nb02 = octx->src[0]->nb[2]; \ |
| 66 | const uint32_t nb03 = octx->src[0]->nb[3]; \ |
| 67 | \ |
| 68 | const uint32_t nb10 = octx->src[1]->nb[0]; \ |
| 69 | const uint32_t nb11 = octx->src[1]->nb[1]; \ |
| 70 | const uint32_t nb12 = octx->src[1]->nb[2]; \ |
| 71 | \ |
| 72 | const uint32_t nb1 = octx->dst->nb[1]; \ |
| 73 | const uint32_t nb2 = octx->dst->nb[2]; \ |
| 74 | const uint32_t nb3 = octx->dst->nb[3]; \ |
| 75 | \ |
| 76 | const uint32_t nr = ne10 * ne11 * ne12; |
| 77 | |
| 78 | static uint32_t get_rows_read_index_ddr( |
| 79 | const void * src1, |
| 80 | uint32_t idx, |
| 81 | int idx_type |
| 82 | ) { |
| 83 | const uint32_t off = idx * ((idx_type == HTP_TYPE_I64) ? sizeof(int64_t) : sizeof(int32_t)); |
| 84 | return *(const uint32_t *) ((const uint8_t *) src1 + off); |
| 85 | } |
| 86 | |
| 87 | static __attribute__((noinline)) uint32_t get_rows_read_index_vtcm( |
| 88 | const void * src1, |
| 89 | uint32_t idx, |
| 90 | int idx_type |
| 91 | ) { |
| 92 | const uint32_t off = idx * ((idx_type == HTP_TYPE_I64) ? sizeof(int64_t) : sizeof(int32_t)); |
| 93 | const HVX_Vector v = hvx_vmem((const uint8_t *) src1 + off); // off & ~127u in HW |
| 94 | // vextract is normally slow, but here it avoids an HVX->L2 store and L1D miss |
| 95 | return (uint32_t) Q6_R_vextract_VR(v, off); |
| 96 | } |
| 97 | |
| 98 | static void get_rows_thread_sametype(unsigned int nth, unsigned int ith, void * data) { |
| 99 | struct get_rows_context * grctx = (struct get_rows_context *) data; |
| 100 | struct htp_ops_context * octx = grctx->octx; |
| 101 | const struct htp_get_rows_kernel_params * kparams = grctx->kparams; |
| 102 | get_rows_preamble; |
| 103 | |
| 104 | const uint32_t dr = grctx->tasks_per_thread; |
| 105 | const uint32_t ir0 = grctx->task_start + dr * ith; |
| 106 | if (ir0 >= grctx->task_start + grctx->tasks) { |
| 107 | return; |
| 108 | } |
| 109 | const uint32_t ir1 = MIN(ir0 + dr, grctx->task_start + grctx->tasks); |
| 110 | const uint32_t row_size_bytes = grctx->src0_row_size; |
| 111 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 112 | |
| 113 | for (uint32_t i = ir0; i < ir1; ++i) { |
| 114 | const uint32_t i12 = fastdiv(i, &kparams->div_ne10_ne11); |
| 115 | const uint32_t rem = i - i12 * ne11 * ne10; |
| 116 | const uint32_t i11 = fastdiv(rem, &kparams->div_ne10); |
| 117 | const uint32_t i10 = rem - i11 * ne10; |
| 118 | |
| 119 | const uint32_t i01 = grctx->read_index(grctx->src1_data, i, grctx->idx_type); |
| 120 | assert(i01 < ne01); |
| 121 | |
| 122 | const uint32_t q02 = fastdiv(i11, &kparams->div_ne02); |
| 123 | const uint32_t i02 = i11 - q02 * ne02; |
| 124 | const uint32_t q03 = fastdiv(i12, &kparams->div_ne03); |
| 125 | const uint32_t i03 = i12 - q03 * ne03; |
| 126 | |
| 127 | const dma_addr_t src0_data = octx->src[0]->data + i01*nb01 + i02*nb02 + i03*nb03; |
| 128 | const dma_addr_t dst_data = octx->dst->data + i10*nb1 + i11*nb2 + i12*nb3; |
| 129 | |
| 130 | while (!dma_queue_push(dma_q, dma_make_data(dst_data, src0_data), nb1, nb01, row_size_bytes, 1)) { |
| 131 | dma_queue_pop(dma_q); |
| 132 | } |
| 133 | } |
| 134 | dma_queue_flush(dma_q); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | struct get_rows_flat_task { |
| 140 | dma_addr_t src0_data; |
| 141 | dma_addr_t dst_data; |
| 142 | }; |
| 143 | |
| 144 | static inline struct get_rows_flat_task get_rows_flat_calc_task( |
| 145 | const struct htp_ops_context * octx, |
| 146 | const struct get_rows_context * grctx, |
| 147 | uint32_t i, |
| 148 | bool need_src0 |
| 149 | ) { |
| 150 | const struct htp_get_rows_kernel_params * kparams = grctx->kparams; |
| 151 | get_rows_preamble; |
| 152 | |
| 153 | const uint32_t i12 = fastdiv(i, &kparams->div_ne10_ne11); |
| 154 | const uint32_t rem = i - i12 * ne11 * ne10; |
| 155 | const uint32_t i11 = fastdiv(rem, &kparams->div_ne10); |
| 156 | const uint32_t i10 = rem - i11 * ne10; |
| 157 | |
| 158 | struct get_rows_flat_task task; |
| 159 | task.src0_data = 0; |
| 160 | task.dst_data = octx->dst->data + i10*nb1 + i11*nb2 + i12*nb3; |
| 161 | |
| 162 | if (need_src0) { |
| 163 | const uint32_t i01 = grctx->read_index(grctx->src1_data, i, grctx->idx_type); |
| 164 | assert(i01 < ne01); |
| 165 | |
| 166 | const uint32_t q02 = fastdiv(i11, &kparams->div_ne02); |
| 167 | const uint32_t i02 = i11 - q02 * ne02; |
| 168 | const uint32_t q03 = fastdiv(i12, &kparams->div_ne03); |
| 169 | const uint32_t i03 = i12 - q03 * ne03; |
| 170 | |
| 171 | task.src0_data = octx->src[0]->data + i01*nb01 + i02*nb02 + i03*nb03; |
| 172 | } |
| 173 | |
| 174 | return task; |
| 175 | } |
| 176 | |
| 177 | static void get_rows_thread_flat(unsigned int nth, unsigned int ith, void * data) { |
| 178 | struct get_rows_context * grctx = (struct get_rows_context *) data; |
| 179 | struct htp_ops_context * octx = grctx->octx; |
| 180 | get_rows_preamble; |
| 181 | |
| 182 | const uint32_t dr = grctx->tasks_per_thread; |
| 183 | const uint32_t ir0 = grctx->task_start + dr * ith; |
| 184 | if (ir0 >= grctx->task_start + grctx->tasks) { |
| 185 | return; |
| 186 | } |
| 187 | const uint32_t ir1 = MIN(ir0 + dr, grctx->task_start + grctx->tasks); |
| 188 | |
| 189 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 190 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 191 | const struct htp_get_rows_vtcm_layout * vtcm_layout = &grctx->vtcm_layout; |
| 192 | uint8_t * vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_src0) + ith * vtcm_layout->src0_bytes_per_thread; |
| 193 | uint8_t * vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_dst) + ith * vtcm_layout->dst_bytes_per_thread; |
| 194 | const uint32_t src0_bytes = grctx->src0_row_size; |
| 195 | const uint32_t dst_bytes = grctx->dst_row_size; |
| 196 | get_rows_flat_compute_fn_t compute = grctx->compute_flat; |
| 197 | |
| 198 | for (uint32_t step = 0, spad_idx = 0; step < ir1 - ir0 && spad_idx < 2; ++step, spad_idx++) { |
| 199 | const uint32_t i = ir0 + step; |
| 200 | struct get_rows_flat_task task = get_rows_flat_calc_task(octx, grctx, i, true); |
| 201 | |
| 202 | dma_queue_push(dma_q, |
| 203 | dma_make_data(octx->dst->data, |
| 204 | vtcm_dst + spad_idx * vtcm_layout->dst_spad_half_size), |
| 205 | dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 0); |
| 206 | dma_queue_push(dma_q, |
| 207 | dma_make_data(vtcm_src0 + spad_idx * vtcm_layout->src0_spad_half_size, task.src0_data), |
| 208 | vtcm_layout->src0_spad_half_size, src0_bytes, src0_bytes, 1); |
| 209 | } |
| 210 | |
| 211 | for (uint32_t step = 0; step < ir1 - ir0; ++step) { |
| 212 | const uint32_t i = ir0 + step; |
| 213 | void * dst_spad = (void *) dma_queue_pop(dma_q).src; |
| 214 | void * src_spad = (void *) dma_queue_pop(dma_q).dst; |
| 215 | |
| 216 | struct get_rows_flat_task task = get_rows_flat_calc_task(octx, grctx, i, false); |
| 217 | |
| 218 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 219 | compute((float *) dst_spad, src_spad, ne00); |
| 220 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 221 | |
| 222 | dma_queue_push(dma_q, |
| 223 | dma_make_data(task.dst_data, dst_spad), |
| 224 | dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 1); |
| 225 | |
| 226 | const uint32_t next_step = step + 2; |
| 227 | if (next_step < ir1 - ir0) { |
| 228 | const uint32_t pi = ir0 + next_step; |
| 229 | struct get_rows_flat_task next_task = get_rows_flat_calc_task(octx, grctx, pi, true); |
| 230 | dma_queue_push(dma_q, |
| 231 | dma_make_data(src_spad, next_task.src0_data), |
| 232 | vtcm_layout->src0_spad_half_size, src0_bytes, src0_bytes, 1); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | dma_queue_flush(dma_q); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | struct get_rows_tiled_task { |
| 242 | dma_addr_t tile_src_base; |
| 243 | dma_addr_t dst_data; |
| 244 | uint32_t row; |
| 245 | }; |
| 246 | |
| 247 | static inline struct get_rows_tiled_task get_rows_tiled_calc_task( |
| 248 | const struct htp_ops_context * octx, |
| 249 | const struct get_rows_context * grctx, |
| 250 | uint32_t i, |
| 251 | uint32_t n_k_tiles, |
| 252 | uint32_t tile_size |
| 253 | ) { |
| 254 | const struct htp_get_rows_kernel_params * kparams = grctx->kparams; |
| 255 | get_rows_preamble; |
| 256 | |
| 257 | const uint32_t i12 = fastdiv(i, &kparams->div_ne10_ne11); |
| 258 | const uint32_t rem = i - i12 * ne11 * ne10; |
| 259 | const uint32_t i11 = fastdiv(rem, &kparams->div_ne10); |
| 260 | const uint32_t i10 = rem - i11 * ne10; |
| 261 | const uint32_t i01 = grctx->read_index(grctx->src1_data, i, grctx->idx_type); |
| 262 | assert(i01 < ne01); |
| 263 | |
| 264 | const uint32_t q02 = fastdiv(i11, &kparams->div_ne02); |
| 265 | const uint32_t i02 = i11 - q02 * ne02; |
| 266 | const uint32_t q03 = fastdiv(i12, &kparams->div_ne03); |
| 267 | const uint32_t i03 = i12 - q03 * ne03; |
| 268 | const uint32_t column_tile = i01 / HTP_TENSOR_TILE_N_ROWS; |
| 269 | const uint32_t row = i01 % HTP_TENSOR_TILE_N_ROWS; |
| 270 | const dma_addr_t matrix = octx->src[0]->data + i02*nb02 + i03*nb03; |
| 271 | |
| 272 | struct get_rows_tiled_task task; |
| 273 | task.tile_src_base = matrix + (column_tile * n_k_tiles) * tile_size; |
| 274 | task.dst_data = octx->dst->data + i10*nb1 + i11*nb2 + i12*nb3; |
| 275 | task.row = row; |
| 276 | return task; |
| 277 | } |
| 278 | |
| 279 | static void get_rows_thread_tiled(unsigned int nth, unsigned int ith, void * data) { |
| 280 | struct get_rows_context * grctx = (struct get_rows_context *) data; |
| 281 | struct htp_ops_context * octx = grctx->octx; |
| 282 | get_rows_preamble; |
| 283 | |
| 284 | const uint32_t dr = grctx->tasks_per_thread; |
| 285 | const uint32_t ir0 = grctx->task_start + dr * ith; |
| 286 | if (ir0 >= grctx->task_start + grctx->tasks) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | const uint32_t ir1 = MIN(ir0 + dr, grctx->task_start + grctx->tasks); |
| 291 | const uint32_t n_k_tiles = ne00 / htp_tensor_get_tile_k(octx->src[0]->type); |
| 292 | const struct htp_get_rows_vtcm_layout * vtcm_layout = &grctx->vtcm_layout; |
| 293 | uint8_t * src_spad_base = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_src0) + ith * vtcm_layout->src0_bytes_per_thread; |
| 294 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_dst) + ith * vtcm_layout->dst_bytes_per_thread; |
| 295 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 296 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 297 | |
| 298 | const uint32_t tile_size = grctx->tile_size; |
| 299 | const uint32_t tile_stride = grctx->tile_stride; |
| 300 | const uint32_t dst_bytes = grctx->dst_row_size; |
| 301 | get_rows_tiled_compute_fn_t compute_tiled = grctx->compute_tiled; |
| 302 | |
| 303 | for (uint32_t step = 0, spad_idx = 0; step < ir1 - ir0 && spad_idx < 2; ++step, ++spad_idx) { |
| 304 | const uint32_t i = ir0 + step; |
| 305 | struct get_rows_tiled_task task = get_rows_tiled_calc_task(octx, grctx, i, n_k_tiles, tile_size); |
| 306 | |
| 307 | // Dummy writeback to prime the queue with dst descriptor |
| 308 | dma_queue_push(dma_q, |
| 309 | dma_make_data(task.dst_data, dst_spad_base + spad_idx * vtcm_layout->dst_spad_half_size), |
| 310 | dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 0); |
| 311 | |
| 312 | // Prefetch row tiles |
| 313 | dma_queue_push(dma_q, |
| 314 | dma_make_data(src_spad_base + spad_idx * vtcm_layout->src0_spad_half_size, task.tile_src_base), |
| 315 | tile_stride, tile_size, tile_size, n_k_tiles); |
| 316 | } |
| 317 | |
| 318 | for (uint32_t step = 0; step < ir1 - ir0; ++step) { |
| 319 | const uint32_t i = ir0 + step; |
| 320 | float * dst_spad = (float *) dma_queue_pop(dma_q).src; |
| 321 | uint8_t * src_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 322 | |
| 323 | struct get_rows_tiled_task task = get_rows_tiled_calc_task(octx, grctx, i, n_k_tiles, tile_size); |
| 324 | |
| 325 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 326 | compute_tiled(dst_spad, src_spad, n_k_tiles, tile_stride, task.row); |
| 327 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 328 | |
| 329 | // Real writeback of dst_spad |
| 330 | dma_queue_push(dma_q, |
| 331 | dma_make_data(task.dst_data, dst_spad), |
| 332 | dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 1); |
| 333 | |
| 334 | const uint32_t next_step = step + 2; |
| 335 | if (next_step < ir1 - ir0) { |
| 336 | const uint32_t ni = ir0 + next_step; |
| 337 | struct get_rows_tiled_task next_task = get_rows_tiled_calc_task(octx, grctx, ni, n_k_tiles, tile_size); |
| 338 | dma_queue_push(dma_q, |
| 339 | dma_make_data(src_spad, next_task.tile_src_base), |
| 340 | tile_stride, tile_size, tile_size, n_k_tiles); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | dma_queue_flush(dma_q); |
| 345 | } |
| 346 | |
| 347 | static __attribute__((noinline)) void get_rows_dma_src1( |
| 348 | struct htp_ops_context * octx, |
| 349 | uint8_t * vtcm_src1, |
| 350 | uint32_t elem_size |
| 351 | ) { |
| 352 | const uint32_t ne10 = octx->src[1]->ne[0]; |
| 353 | const uint32_t ne11 = octx->src[1]->ne[1]; |
| 354 | const uint32_t ne12 = octx->src[1]->ne[2]; |
| 355 | const uint32_t ne13 = octx->src[1]->ne[3]; |
| 356 | const uint32_t nb11 = octx->src[1]->nb[1]; |
| 357 | const uint32_t nb12 = octx->src[1]->nb[2]; |
| 358 | const uint32_t nb13 = octx->src[1]->nb[3]; |
| 359 | |
| 360 | dma_queue * dma_q = octx->ctx->dma[0]; |
| 361 | uint8_t * cur_dst = vtcm_src1; |
| 362 | for (uint32_t i13 = 0; i13 < ne13; ++i13) { |
| 363 | for (uint32_t i12 = 0; i12 < ne12; ++i12) { |
| 364 | const dma_addr_t src_plane = octx->src[1]->data + i12 * nb12 + i13 * nb13; |
| 365 | while (!dma_queue_push(dma_q, dma_make_data(cur_dst, src_plane), |
| 366 | ne10 * elem_size, nb11, ne10 * elem_size, ne11)) { |
| 367 | dma_queue_pop(dma_q); |
| 368 | } |
| 369 | cur_dst += ne10 * ne11 * elem_size; |
| 370 | } |
| 371 | } |
| 372 | dma_queue_flush(dma_q); |
| 373 | } |
| 374 | |
| 375 | int op_get_rows(struct htp_ops_context * octx) { |
| 376 | const struct htp_get_rows_kernel_params * kparams = (const struct htp_get_rows_kernel_params *) octx->kernel_params; |
| 377 | |
| 378 | const uint32_t ne00 = octx->src[0]->ne[0]; |
| 379 | const uint32_t src0_row_size = htp_tensor_get_row_size(octx->src[0]->type, ne00); |
| 380 | if (src0_row_size == 0) { |
| 381 | return HTP_STATUS_NO_SUPPORT; |
| 382 | } |
| 383 | |
| 384 | if (kparams->kernel_type == HTP_GET_ROWS_KERNEL_SAMETYPE) { |
| 385 | if (octx->src[0]->type != octx->dst->type) { |
| 386 | return HTP_STATUS_NO_SUPPORT; |
| 387 | } |
| 388 | } else { |
| 389 | if (octx->dst->type != HTP_TYPE_F32) { |
| 390 | return HTP_STATUS_NO_SUPPORT; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (octx->src[1]->type != HTP_TYPE_I32 && octx->src[1]->type != HTP_TYPE_I64) { |
| 395 | return HTP_STATUS_NO_SUPPORT; |
| 396 | } |
| 397 | |
| 398 | const struct htp_tensor * dst = octx->dst; |
| 399 | const uint32_t total_tasks = kparams->total_tasks; |
| 400 | const uint32_t dst_row_size = (kparams->kernel_type == HTP_GET_ROWS_KERNEL_SAMETYPE) ? src0_row_size : ne00 * sizeof(float); |
| 401 | |
| 402 | uint32_t task_start = 0; |
| 403 | uint32_t tasks = total_tasks; |
| 404 | |
| 405 | if (octx->ctx->mdev.count > 1) { |
| 406 | uint32_t tasks_per_chunk = 1; |
| 407 | htp_tensor_mdev_rows_per_chunk(dst, (uint32_t) dst->nb[0], dst_row_size, &tasks_per_chunk); |
| 408 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_tasks, tasks_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 409 | task_start = range.start; |
| 410 | tasks = range.count; |
| 411 | } |
| 412 | |
| 413 | if (tasks == 0) { |
| 414 | return HTP_STATUS_OK; |
| 415 | } |
| 416 | |
| 417 | if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { |
| 418 | return HTP_STATUS_INVAL_PARAMS; |
| 419 | } |
| 420 | |
| 421 | const uint32_t n_threads = octx->n_threads; |
| 422 | |
| 423 | struct get_rows_context grctx; |
| 424 | grctx.octx = octx; |
| 425 | grctx.kparams = kparams; |
| 426 | grctx.vtcm_base = (uint8_t *)octx->ctx->vtcm_base; |
| 427 | grctx.compute_flat = NULL; |
| 428 | grctx.compute_tiled = NULL; |
| 429 | grctx.task_start = task_start; |
| 430 | grctx.tasks = tasks; |
| 431 | grctx.tasks_per_thread = fastdiv(tasks + n_threads - 1, &octx->n_threads_div); |
| 432 | grctx.tile_size = 0; |
| 433 | grctx.tile_stride = 0; |
| 434 | grctx.src0_row_size = src0_row_size; |
| 435 | grctx.dst_row_size = dst_row_size; |
| 436 | grctx.idx_type = octx->src[1]->type; |
| 437 | |
| 438 | htp_get_rows_vtcm_layout_build(&grctx.vtcm_layout, kparams->kernel_type, octx->src[0]->type, ne00, total_tasks, grctx.idx_type, n_threads); |
| 439 | |
| 440 | if (grctx.vtcm_layout.total_bytes > octx->ctx->vtcm_size) { |
| 441 | FARF(ERROR, "get-rows: VTCM reservation %zu is too small, needed %zu\n", |
| 442 | octx->ctx->vtcm_size, grctx.vtcm_layout.total_bytes); |
| 443 | return HTP_STATUS_INVAL_PARAMS; |
| 444 | } |
| 445 | |
| 446 | work_queue_func_t q_func = NULL; |
| 447 | switch (kparams->kernel_type) { |
| 448 | case HTP_GET_ROWS_KERNEL_SAMETYPE: |
| 449 | q_func = get_rows_thread_sametype; |
| 450 | break; |
| 451 | case HTP_GET_ROWS_KERNEL_TILED: |
| 452 | switch (octx->src[0]->type) { |
| 453 | case HTP_TYPE_F16: grctx.compute_tiled = hvx_dequantize_row_tiled_f16_f32; break; |
| 454 | case HTP_TYPE_Q1_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q1_0_f32; break; |
| 455 | case HTP_TYPE_Q2_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q2_0_f32; break; |
| 456 | case HTP_TYPE_Q8_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q8_0_f32; break; |
| 457 | case HTP_TYPE_Q4_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q4_0_f32; break; |
| 458 | case HTP_TYPE_Q4_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q4_k_f32; break; |
| 459 | case HTP_TYPE_Q5_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q5_k_f32; break; |
| 460 | case HTP_TYPE_Q6_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q6_k_f32; break; |
| 461 | default: return HTP_STATUS_NO_SUPPORT; |
| 462 | } |
| 463 | grctx.tile_size = htp_tensor_get_tile_size(octx->src[0]->type); |
| 464 | grctx.tile_stride = htp_tensor_get_tile_padded_size(octx->src[0]->type); |
| 465 | q_func = get_rows_thread_tiled; |
| 466 | break; |
| 467 | case HTP_GET_ROWS_KERNEL_FLAT: |
| 468 | switch (octx->src[0]->type) { |
| 469 | case HTP_TYPE_F16: grctx.compute_flat = hvx_dequantize_row_flat_f16_f32; break; |
| 470 | case HTP_TYPE_BF16: grctx.compute_flat = hvx_dequantize_row_flat_bf16_f32; break; |
| 471 | case HTP_TYPE_Q1_0: grctx.compute_flat = hvx_dequantize_row_flat_q1_0_f32; break; |
| 472 | case HTP_TYPE_Q2_0: grctx.compute_flat = hvx_dequantize_row_flat_q2_0_f32; break; |
| 473 | case HTP_TYPE_Q8_0: grctx.compute_flat = hvx_dequantize_row_flat_q8_0_f32; break; |
| 474 | case HTP_TYPE_Q4_0: grctx.compute_flat = hvx_dequantize_row_flat_q4_0_f32; break; |
| 475 | case HTP_TYPE_Q4_K: grctx.compute_flat = hvx_dequantize_row_flat_q4_k_f32; break; |
| 476 | case HTP_TYPE_Q5_K: grctx.compute_flat = hvx_dequantize_row_flat_q5_k_f32; break; |
| 477 | case HTP_TYPE_Q6_K: grctx.compute_flat = hvx_dequantize_row_flat_q6_k_f32; break; |
| 478 | default: return HTP_STATUS_NO_SUPPORT; |
| 479 | } |
| 480 | q_func = get_rows_thread_flat; |
| 481 | break; |
| 482 | default: |
| 483 | return HTP_STATUS_NO_SUPPORT; |
| 484 | } |
| 485 | |
| 486 | const uint32_t elem_size = (grctx.idx_type == HTP_TYPE_I64) ? sizeof(int64_t) : sizeof(int32_t); |
| 487 | const size_t src1_bytes = (size_t) total_tasks * elem_size; |
| 488 | |
| 489 | if (!htp_tensor_is_extended(octx->src[1]) && src1_bytes <= 128 && |
| 490 | htp_tensor_is_contiguous(octx->src[1], elem_size)) { |
| 491 | grctx.src1_data = (const void *) (uintptr_t) octx->src[1]->data; |
| 492 | grctx.read_index = get_rows_read_index_ddr; |
| 493 | } else { |
| 494 | uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(uint8_t, grctx.vtcm_base, grctx.vtcm_layout.off_src1); |
| 495 | get_rows_dma_src1(octx, vtcm_src1, elem_size); |
| 496 | grctx.src1_data = vtcm_src1; |
| 497 | grctx.read_index = get_rows_read_index_vtcm; |
| 498 | } |
| 499 | |
| 500 | FARF(HIGH, "get-rows: (%ux%ux%ux%u) x (%ux%ux%ux%u) -> (%ux%ux%ux%u) : src0-vtcm-size %zu dst-vtcm-size %zu kernel-type %d n-threads %d\n", |
| 501 | octx->src[0]->ne[0], octx->src[0]->ne[1], octx->src[0]->ne[2], octx->src[0]->ne[3], |
| 502 | octx->src[1]->ne[0], octx->src[1]->ne[1], octx->src[1]->ne[2], octx->src[1]->ne[3], |
| 503 | octx->dst->ne[0], octx->dst->ne[1], octx->dst->ne[2], octx->dst->ne[3], |
| 504 | grctx.vtcm_layout.src0_bytes_per_thread * n_threads, |
| 505 | grctx.vtcm_layout.dst_bytes_per_thread * n_threads, |
| 506 | kparams->kernel_type, n_threads); |
| 507 | |
| 508 | work_queue_run(octx->ctx->work_queue, q_func, &grctx, n_threads); |
| 509 | return HTP_STATUS_OK; |
| 510 | } |