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 / gather-rows.c
| 1 | #include "gather-rows.h" |
| 2 | |
| 3 | #include <assert.h> |
| 4 | |
| 5 | #include "hex-common.h" |
| 6 | #include "hex-profile.h" |
| 7 | #include "hvx-utils.h" |
| 8 | #include "dma-queue.h" |
| 9 | #include "htp-ctx.h" |
| 10 | #include "htp-ops.h" |
| 11 | #include "htp-tensor.h" |
| 12 | #include "work-queue.h" |
| 13 | |
| 14 | typedef void (*gather_rows_compute_fn_t)( |
| 15 | uint8_t * restrict out, const uint8_t * restrict region, uint32_t nvec, |
| 16 | const struct htp_gather_rows_params * restrict kparams, |
| 17 | const HVX_Vector * restrict tab, const HVX_Vector * restrict inc, HVX_Vector * restrict cur); |
| 18 | |
| 19 | struct gather_rows_task { |
| 20 | struct htp_ops_context * octx; |
| 21 | const struct htp_tensor * a; |
| 22 | const struct htp_tensor * b; |
| 23 | dma_addr_t dst; |
| 24 | uint32_t r_start; |
| 25 | uint32_t rows; |
| 26 | uint32_t rows_per_thread; |
| 27 | const struct htp_gather_rows_params * kparams; |
| 28 | gather_rows_compute_fn_t compute; |
| 29 | }; |
| 30 | |
| 31 | static inline __attribute__((always_inline)) void gather_rows_dma_in( |
| 32 | dma_queue * q, const struct gather_rows_task * task, |
| 33 | uint8_t * region, uint32_t r, uint32_t nr, uint32_t elem_size) { |
| 34 | const struct htp_tensor * a = task->a; |
| 35 | const struct htp_tensor * b = task->b; |
| 36 | const struct htp_gather_rows_params * kparams = task->kparams; |
| 37 | |
| 38 | const size_t a_size = (nr - 1) * a->nb[1] + kparams->na * elem_size; |
| 39 | dma_queue_push(q, dma_make_data(region, a->data + r * a->nb[1]), a_size, a_size, a_size, 1); |
| 40 | if (b) { |
| 41 | if (kparams->b_dense) { |
| 42 | const size_t b_size = (nr - 1) * b->nb[1] + kparams->nb * elem_size; |
| 43 | dma_queue_push(q, dma_make_data(region + kparams->v1, b->data + r * b->nb[1]), b_size, b_size, b_size, 1); |
| 44 | } else { |
| 45 | const size_t b_size = (nr - 1) * b->nb[1] + elem_size; |
| 46 | for (uint32_t k = 0; k < kparams->nb; k++) { |
| 47 | dma_queue_push(q, dma_make_data(region + kparams->v1 + k * kparams->span1, |
| 48 | b->data + k * b->nb[0] + r * b->nb[1]), b_size, b_size, b_size, 1); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static inline void gather_rows_dma_out(dma_queue * q, const struct gather_rows_task * task, |
| 55 | uint8_t * out, uint32_t r, uint32_t nr, uint32_t elem_size) { |
| 56 | const struct htp_gather_rows_params * kparams = task->kparams; |
| 57 | const size_t out_size = nr * kparams->ne * elem_size; |
| 58 | dma_queue_push(q, dma_make_data(task->dst + r * kparams->ne * elem_size, out), out_size, out_size, out_size, 1); |
| 59 | } |
| 60 | |
| 61 | static __attribute__((noinline)) void gather_rows_init_tables_f32( |
| 62 | HVX_Vector * tab, HVX_Vector * inc, |
| 63 | const struct htp_gather_rows_params * kparams, |
| 64 | const struct htp_tensor * a, const struct htp_tensor * b) { |
| 65 | |
| 66 | const int32_t s_a = kparams->rg * a->nb[1]; |
| 67 | const int32_t s_b = b ? kparams->rg * b->nb[1] : 0; |
| 68 | const uint32_t b_nb1 = b ? b->nb[1] : 0; |
| 69 | uint32_t row = 0; |
| 70 | uint32_t col = 0; |
| 71 | int32_t t_buf[32] __attribute__((aligned(128))); |
| 72 | int32_t s_buf[32] __attribute__((aligned(128))); |
| 73 | for (uint32_t k = 0; k < kparams->vg; k++) { |
| 74 | for (uint32_t lane = 0; lane < 32; lane++) { |
| 75 | if (col < kparams->na) { |
| 76 | t_buf[lane] = row * a->nb[1] + col * 4; |
| 77 | s_buf[lane] = s_a; |
| 78 | } else { |
| 79 | t_buf[lane] = kparams->v1 + (col - kparams->na) * kparams->span1 + row * b_nb1; |
| 80 | s_buf[lane] = s_b; |
| 81 | } |
| 82 | if (++col == kparams->ne) { |
| 83 | col = 0; |
| 84 | row++; |
| 85 | } |
| 86 | } |
| 87 | tab[k] = *(const HVX_Vector *) t_buf; |
| 88 | inc[k] = *(const HVX_Vector *) s_buf; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static __attribute__((noinline)) void gather_rows_init_tables_f16( |
| 93 | HVX_Vector * tab, HVX_Vector * inc, |
| 94 | const struct htp_gather_rows_params * kparams, |
| 95 | const struct htp_tensor * a, const struct htp_tensor * b) { |
| 96 | |
| 97 | const int16_t s_a = (int16_t) (kparams->rg * a->nb[1]); |
| 98 | const int16_t s_b = (int16_t) (b ? kparams->rg * b->nb[1] : 0); |
| 99 | const uint32_t b_nb1 = b ? b->nb[1] : 0; |
| 100 | uint32_t row = 0; |
| 101 | uint32_t col = 0; |
| 102 | int16_t t_buf[64] __attribute__((aligned(128))); |
| 103 | int16_t s_buf[64] __attribute__((aligned(128))); |
| 104 | for (uint32_t k = 0; k < kparams->vg; k++) { |
| 105 | for (uint32_t lane = 0; lane < 64; lane++) { |
| 106 | if (col < kparams->na) { |
| 107 | t_buf[lane] = (int16_t) (row * a->nb[1] + col * 2); |
| 108 | s_buf[lane] = s_a; |
| 109 | } else { |
| 110 | t_buf[lane] = (int16_t) (kparams->v1 + (col - kparams->na) * kparams->span1 + row * b_nb1); |
| 111 | s_buf[lane] = s_b; |
| 112 | } |
| 113 | if (++col == kparams->ne) { |
| 114 | col = 0; |
| 115 | row++; |
| 116 | } |
| 117 | } |
| 118 | tab[k] = *(const HVX_Vector *) t_buf; |
| 119 | inc[k] = *(const HVX_Vector *) s_buf; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void gather_rows_compute_f32_vg1( |
| 124 | uint8_t * restrict out, const uint8_t * restrict region, uint32_t nvec, |
| 125 | const struct htp_gather_rows_params * restrict kparams, |
| 126 | const HVX_Vector * restrict tab, const HVX_Vector * restrict inc, HVX_Vector * restrict cur) { |
| 127 | (void) cur; |
| 128 | const uint32_t region_size = kparams->region_size; |
| 129 | HVX_Vector vcur = tab[0]; |
| 130 | const HVX_Vector vinc = inc[0]; |
| 131 | #pragma unroll(4) |
| 132 | for (uint32_t v = 0; v < nvec; v++) { |
| 133 | Q6_vgather_ARMVw((HVX_Vector *) (out + v * 128), (size_t) region, region_size, vcur); |
| 134 | vcur = Q6_Vw_vadd_VwVw(vcur, vinc); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void gather_rows_compute_f32_vgn( |
| 139 | uint8_t * restrict out, const uint8_t * restrict region, uint32_t nvec, |
| 140 | const struct htp_gather_rows_params * restrict kparams, |
| 141 | const HVX_Vector * restrict tab, const HVX_Vector * restrict inc, HVX_Vector * restrict cur) { |
| 142 | const uint32_t vg = kparams->vg; |
| 143 | const uint32_t region_size = kparams->region_size; |
| 144 | for (uint32_t k = 0; k < vg; k++) { |
| 145 | cur[k] = tab[k]; |
| 146 | } |
| 147 | uint32_t k = 0; |
| 148 | #pragma unroll(4) |
| 149 | for (uint32_t v = 0; v < nvec; v++) { |
| 150 | Q6_vgather_ARMVw((HVX_Vector *) (out + v * 128), (size_t) region, region_size, cur[k]); |
| 151 | cur[k] = Q6_Vw_vadd_VwVw(cur[k], inc[k]); |
| 152 | if (++k == vg) { |
| 153 | k = 0; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static void gather_rows_compute_f16_vg1( |
| 159 | uint8_t * restrict out, const uint8_t * restrict region, uint32_t nvec, |
| 160 | const struct htp_gather_rows_params * restrict kparams, |
| 161 | const HVX_Vector * restrict tab, const HVX_Vector * restrict inc, HVX_Vector * restrict cur) { |
| 162 | (void) cur; |
| 163 | const uint32_t region_size = kparams->region_size; |
| 164 | HVX_Vector vcur = tab[0]; |
| 165 | const HVX_Vector vinc = inc[0]; |
| 166 | #pragma unroll(4) |
| 167 | for (uint32_t v = 0; v < nvec; v++) { |
| 168 | Q6_vgather_ARMVh((HVX_Vector *) (out + v * 128), (size_t) region, region_size, vcur); |
| 169 | vcur = Q6_Vh_vadd_VhVh(vcur, vinc); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static void gather_rows_compute_f16_vgn( |
| 174 | uint8_t * restrict out, const uint8_t * restrict region, uint32_t nvec, |
| 175 | const struct htp_gather_rows_params * restrict kparams, |
| 176 | const HVX_Vector * restrict tab, const HVX_Vector * restrict inc, HVX_Vector * restrict cur) { |
| 177 | const uint32_t vg = kparams->vg; |
| 178 | const uint32_t region_size = kparams->region_size; |
| 179 | for (uint32_t k = 0; k < vg; k++) { |
| 180 | cur[k] = tab[k]; |
| 181 | } |
| 182 | uint32_t k = 0; |
| 183 | #pragma unroll(4) |
| 184 | for (uint32_t v = 0; v < nvec; v++) { |
| 185 | Q6_vgather_ARMVh((HVX_Vector *) (out + v * 128), (size_t) region, region_size, cur[k]); |
| 186 | cur[k] = Q6_Vh_vadd_VhVh(cur[k], inc[k]); |
| 187 | if (++k == vg) { |
| 188 | k = 0; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static void gather_rows_thread_f32(unsigned int nth, unsigned int ith, void * data) { |
| 194 | (void) nth; |
| 195 | const struct gather_rows_task * task = (const struct gather_rows_task *) data; |
| 196 | const struct htp_gather_rows_params * kparams = task->kparams; |
| 197 | const struct htp_tensor * a = task->a; |
| 198 | const struct htp_tensor * b = task->b; |
| 199 | |
| 200 | const uint32_t r_offset = MIN(ith * task->rows_per_thread, task->rows); |
| 201 | const uint32_t r_beg = task->r_start + r_offset; |
| 202 | const uint32_t r_end = task->r_start + MIN(r_offset + task->rows_per_thread, task->rows); |
| 203 | if (r_beg >= r_end) { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | struct htp_thread_trace * tr = &task->octx->ctx->trace[ith]; |
| 208 | |
| 209 | struct htp_gather_rows_vtcm_layout layout; |
| 210 | htp_gather_rows_vtcm_layout_build(&layout, kparams->vg, kparams->region_size, kparams->nr_max, kparams->ne, 4); |
| 211 | |
| 212 | uint8_t * base = VTCM_LAYOUT_PTR(uint8_t, task->octx->ctx->vtcm_base, (size_t) ith * kparams->slice); |
| 213 | HVX_Vector * tab = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_tab); |
| 214 | HVX_Vector * inc = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_inc); |
| 215 | HVX_Vector * cur = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_cur); |
| 216 | |
| 217 | uint8_t * region[2] = { |
| 218 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_region0), |
| 219 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_region1), |
| 220 | }; |
| 221 | uint8_t * out[2] = { |
| 222 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_out0), |
| 223 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_out1), |
| 224 | }; |
| 225 | |
| 226 | htp_trace_event_start(tr, HTP_TRACE_EVT_INIT, (uint16_t) r_beg); |
| 227 | gather_rows_init_tables_f32(tab, inc, kparams, a, b); |
| 228 | htp_trace_event_stop(tr, HTP_TRACE_EVT_INIT, (uint16_t) r_beg); |
| 229 | |
| 230 | dma_queue * q = task->octx->ctx->dma[ith]; |
| 231 | const uint32_t n_in = 1 + (task->b ? (kparams->b_dense ? 1 : kparams->nb) : 0); |
| 232 | const uint32_t nr_max = kparams->nr_max; |
| 233 | const uint32_t nr0 = MIN(nr_max, r_end - r_beg); |
| 234 | |
| 235 | dma_queue_push(q, dma_make_data(task->dst, out[0]), 0, 0, 0, 0); // dummy out |
| 236 | gather_rows_dma_in(q, task, region[0], r_beg, nr0, 4); |
| 237 | |
| 238 | const uint32_t r1 = r_beg + nr_max; |
| 239 | if (r1 < r_end) { |
| 240 | const uint32_t nr1 = MIN(nr_max, r_end - r1); |
| 241 | dma_queue_push(q, dma_make_data(task->dst, out[1]), 0, 0, 0, 0); // dummy out |
| 242 | gather_rows_dma_in(q, task, region[1], r1, nr1, 4); |
| 243 | } |
| 244 | |
| 245 | uint32_t buf = 0; |
| 246 | for (uint32_t r = r_beg; r < r_end; r += nr_max) { |
| 247 | const uint32_t nr = MIN(nr_max, r_end - r); |
| 248 | const uint32_t next_buf = buf ^ 1; |
| 249 | |
| 250 | dma_queue_pop(q); // complete out |
| 251 | for (uint32_t i = 0; i < n_in; i++) { |
| 252 | dma_queue_pop(q); |
| 253 | } |
| 254 | |
| 255 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 256 | const uint32_t nvec = (nr * kparams->ne + 31) >> 5; |
| 257 | task->compute(out[buf], region[buf], nvec, kparams, tab, inc, cur); |
| 258 | hvx_gather_sync(out[buf]); |
| 259 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 260 | |
| 261 | gather_rows_dma_out(q, task, out[buf], r, nr, 4); |
| 262 | |
| 263 | const uint32_t r_prefetch = r + 2 * nr_max; |
| 264 | if (r_prefetch < r_end) { |
| 265 | const uint32_t nr_prefetch = MIN(nr_max, r_end - r_prefetch); |
| 266 | gather_rows_dma_in(q, task, region[buf], r_prefetch, nr_prefetch, 4); |
| 267 | } |
| 268 | |
| 269 | buf = next_buf; |
| 270 | } |
| 271 | dma_queue_flush(q); |
| 272 | } |
| 273 | |
| 274 | static void gather_rows_thread_f16(unsigned int nth, unsigned int ith, void * data) { |
| 275 | (void) nth; |
| 276 | const struct gather_rows_task * task = (const struct gather_rows_task *) data; |
| 277 | const struct htp_gather_rows_params * kparams = task->kparams; |
| 278 | const struct htp_tensor * a = task->a; |
| 279 | const struct htp_tensor * b = task->b; |
| 280 | |
| 281 | const uint32_t r_offset = MIN(ith * task->rows_per_thread, task->rows); |
| 282 | const uint32_t r_beg = task->r_start + r_offset; |
| 283 | const uint32_t r_end = task->r_start + MIN(r_offset + task->rows_per_thread, task->rows); |
| 284 | if (r_beg >= r_end) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | struct htp_thread_trace * tr = &task->octx->ctx->trace[ith]; |
| 289 | |
| 290 | struct htp_gather_rows_vtcm_layout layout; |
| 291 | htp_gather_rows_vtcm_layout_build(&layout, kparams->vg, kparams->region_size, kparams->nr_max, kparams->ne, 2); |
| 292 | |
| 293 | uint8_t * base = VTCM_LAYOUT_PTR(uint8_t, task->octx->ctx->vtcm_base, (size_t) ith * kparams->slice); |
| 294 | HVX_Vector * tab = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_tab); |
| 295 | HVX_Vector * inc = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_inc); |
| 296 | HVX_Vector * cur = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_cur); |
| 297 | |
| 298 | uint8_t * region[2] = { |
| 299 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_region0), |
| 300 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_region1), |
| 301 | }; |
| 302 | uint8_t * out[2] = { |
| 303 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_out0), |
| 304 | VTCM_LAYOUT_PTR(uint8_t, base, layout.off_out1), |
| 305 | }; |
| 306 | |
| 307 | htp_trace_event_start(tr, HTP_TRACE_EVT_INIT, (uint16_t) r_beg); |
| 308 | gather_rows_init_tables_f16(tab, inc, kparams, a, b); |
| 309 | htp_trace_event_stop(tr, HTP_TRACE_EVT_INIT, (uint16_t) r_beg); |
| 310 | |
| 311 | dma_queue * q = task->octx->ctx->dma[ith]; |
| 312 | const uint32_t n_in = 1 + (task->b ? (kparams->b_dense ? 1 : kparams->nb) : 0); |
| 313 | |
| 314 | const uint32_t nr_max = kparams->nr_max; |
| 315 | const uint32_t nr0 = MIN(nr_max, r_end - r_beg); |
| 316 | dma_queue_push(q, dma_make_data(task->dst, out[0]), 0, 0, 0, 0); // dummy out |
| 317 | gather_rows_dma_in(q, task, region[0], r_beg, nr0, 2); |
| 318 | |
| 319 | const uint32_t r1 = r_beg + nr_max; |
| 320 | if (r1 < r_end) { |
| 321 | const uint32_t nr1 = MIN(nr_max, r_end - r1); |
| 322 | dma_queue_push(q, dma_make_data(task->dst, out[1]), 0, 0, 0, 0); // dummy out |
| 323 | gather_rows_dma_in(q, task, region[1], r1, nr1, 2); |
| 324 | } |
| 325 | |
| 326 | uint32_t buf = 0; |
| 327 | for (uint32_t r = r_beg; r < r_end; r += nr_max) { |
| 328 | const uint32_t nr = MIN(nr_max, r_end - r); |
| 329 | const uint32_t next_buf = buf ^ 1; |
| 330 | |
| 331 | dma_queue_pop(q); // complete out |
| 332 | for (uint32_t i = 0; i < n_in; i++) { |
| 333 | dma_queue_pop(q); |
| 334 | } |
| 335 | |
| 336 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 337 | const uint32_t nvec = (nr * kparams->ne + 63) >> 6; |
| 338 | task->compute(out[buf], region[buf], nvec, kparams, tab, inc, cur); |
| 339 | hvx_gather_sync(out[buf]); |
| 340 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 341 | |
| 342 | gather_rows_dma_out(q, task, out[buf], r, nr, 2); |
| 343 | |
| 344 | const uint32_t r_prefetch = r + 2 * nr_max; |
| 345 | if (r_prefetch < r_end) { |
| 346 | const uint32_t nr_prefetch = MIN(nr_max, r_end - r_prefetch); |
| 347 | gather_rows_dma_in(q, task, region[buf], r_prefetch, nr_prefetch, 2); |
| 348 | } |
| 349 | |
| 350 | buf = next_buf; |
| 351 | } |
| 352 | dma_queue_flush(q); |
| 353 | } |
| 354 | |
| 355 | int htp_gather_rows( |
| 356 | struct htp_ops_context * octx, |
| 357 | const struct htp_tensor * a, |
| 358 | const struct htp_tensor * b, |
| 359 | const struct htp_gather_rows_params * kparams, |
| 360 | uint32_t n_threads) { |
| 361 | |
| 362 | if (!htp_ops_context_set_n_threads(octx, n_threads)) { |
| 363 | return HTP_STATUS_INVAL_PARAMS; |
| 364 | } |
| 365 | |
| 366 | assert(kparams->slice * octx->n_threads <= octx->ctx->vtcm_size); |
| 367 | |
| 368 | const uint32_t total_rows = kparams->total_rows; |
| 369 | uint32_t r_start = 0; |
| 370 | uint32_t r_count = total_rows; |
| 371 | if (octx->ctx->mdev.count > 1) { |
| 372 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( |
| 373 | total_rows, kparams->rg, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 374 | r_start = range.start; |
| 375 | r_count = range.count; |
| 376 | } |
| 377 | |
| 378 | if (r_count == 0) { |
| 379 | return HTP_STATUS_OK; |
| 380 | } |
| 381 | |
| 382 | const uint32_t elem_size = (a->type == HTP_TYPE_F16) ? 2 : 4; |
| 383 | |
| 384 | struct gather_rows_task task; |
| 385 | task.octx = octx; |
| 386 | task.a = a; |
| 387 | task.b = b; |
| 388 | task.dst = octx->dst->data; |
| 389 | task.r_start = r_start; |
| 390 | task.rows = r_count; |
| 391 | task.kparams = kparams; |
| 392 | task.rows_per_thread = kparams->rows_per_thread; |
| 393 | |
| 394 | if (elem_size == 2) { |
| 395 | task.compute = (kparams->vg == 1) ? gather_rows_compute_f16_vg1 : gather_rows_compute_f16_vgn; |
| 396 | } else { |
| 397 | task.compute = (kparams->vg == 1) ? gather_rows_compute_f32_vg1 : gather_rows_compute_f32_vgn; |
| 398 | } |
| 399 | |
| 400 | work_queue_func_t worker_func = (elem_size == 2) ? gather_rows_thread_f16 : gather_rows_thread_f32; |
| 401 | work_queue_run(octx->ctx->work_queue, worker_func, &task, octx->n_threads); |
| 402 | return HTP_STATUS_OK; |
| 403 | } |