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 / binary-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 | #include "dma-queue.h" |
| 12 | #include "hvx-utils.h" |
| 13 | |
| 14 | #define GGML_COMMON_DECL_C |
| 15 | #include "ggml-common.h" |
| 16 | #include "hex-common.h" |
| 17 | #include "hex-profile.h" |
| 18 | #include "binary-ops.h" |
| 19 | #include "htp-ctx.h" |
| 20 | #include "htp-ops.h" |
| 21 | #include "htp-tensor.h" |
| 22 | |
| 23 | #ifndef MIN |
| 24 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 25 | #endif |
| 26 | |
| 27 | // Context for binary operations |
| 28 | struct htp_binary_context { |
| 29 | struct htp_ops_context * octx; |
| 30 | struct htp_binary_vtcm_layout vtcm_layout; |
| 31 | uint8_t * vtcm_base; |
| 32 | |
| 33 | struct fastdiv_values src0_dim1_div; // ne01 |
| 34 | struct fastdiv_values src0_dim2_div; // ne02 |
| 35 | struct fastdiv_values src0_dim12_div;// ne03 |
| 36 | |
| 37 | struct fastdiv_values src1_dim1_div; // ne11 |
| 38 | struct fastdiv_values src1_dim2_div; // ne12 |
| 39 | struct fastdiv_values src1_dim3_div; // ne13 |
| 40 | |
| 41 | uint32_t block_max; |
| 42 | uint32_t nrows_per_thread; |
| 43 | uint32_t total_rows; |
| 44 | uint32_t row_start; |
| 45 | uint32_t n_srcs; |
| 46 | size_t src0_row_size_aligned; |
| 47 | size_t src1_row_size_aligned; |
| 48 | size_t dst_row_size_aligned; |
| 49 | size_t row_size_bytes; |
| 50 | |
| 51 | bool split_at_ne01; |
| 52 | bool split_at_ne02; |
| 53 | |
| 54 | void * compute; |
| 55 | }; |
| 56 | |
| 57 | #define htp_binary_preamble \ |
| 58 | const struct htp_tensor * src0 = octx->src[0]; \ |
| 59 | const struct htp_tensor * src1 = octx->src[1]; \ |
| 60 | const struct htp_tensor * dst = octx->dst; \ |
| 61 | \ |
| 62 | const uint32_t ne00 = src0->ne[0]; \ |
| 63 | const uint32_t ne01 = src0->ne[1]; \ |
| 64 | const uint32_t ne02 = src0->ne[2]; \ |
| 65 | const uint32_t ne03 = src0->ne[3]; \ |
| 66 | \ |
| 67 | const uint32_t ne10 = src1->ne[0]; \ |
| 68 | const uint32_t ne11 = src1->ne[1]; \ |
| 69 | const uint32_t ne12 = src1->ne[2]; \ |
| 70 | const uint32_t ne13 = src1->ne[3]; \ |
| 71 | \ |
| 72 | const uint32_t nb01 = src0->nb[1]; \ |
| 73 | const uint32_t nb02 = src0->nb[2]; \ |
| 74 | const uint32_t nb03 = src0->nb[3]; \ |
| 75 | \ |
| 76 | const uint32_t nb11 = src1->nb[1]; \ |
| 77 | const uint32_t nb12 = src1->nb[2]; \ |
| 78 | const uint32_t nb13 = src1->nb[3]; \ |
| 79 | \ |
| 80 | const uint32_t nb1 = dst->nb[1]; \ |
| 81 | const uint32_t nb2 = dst->nb[2]; \ |
| 82 | const uint32_t nb3 = dst->nb[3]; |
| 83 | |
| 84 | static inline uint32_t calc_block_size(struct htp_binary_context * bctx, uint32_t ir, uint32_t end_row, uint32_t ne01, uint32_t ne02) { |
| 85 | uint32_t i03, i02, i01, rem; |
| 86 | i03 = fastdiv(ir, &bctx->src0_dim12_div); |
| 87 | rem = ir - i03 * (ne02 * ne01); |
| 88 | i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 89 | i01 = rem - i02 * ne01; |
| 90 | |
| 91 | uint32_t rows_left = end_row - ir; |
| 92 | uint32_t block_limit = rows_left; |
| 93 | |
| 94 | if (bctx->split_at_ne01) { |
| 95 | block_limit = MIN(block_limit, ne01 - i01); |
| 96 | } |
| 97 | if (bctx->split_at_ne02) { |
| 98 | uint32_t rows_in_plane = (ne02 * ne01) - rem; |
| 99 | block_limit = MIN(block_limit, rows_in_plane); |
| 100 | } |
| 101 | |
| 102 | return MIN(bctx->block_max, block_limit); |
| 103 | } |
| 104 | |
| 105 | // Out-of-line compute micro-kernels |
| 106 | |
| 107 | typedef void (*compute_scalar_t)( |
| 108 | uint8_t * dst, const uint8_t * src0, const void * s1_table, |
| 109 | uint32_t cur_i11, uint32_t ne11, uint32_t n_rows, |
| 110 | size_t dst_stride, size_t src0_stride, uint32_t ne00); |
| 111 | |
| 112 | #define DEFINE_COMPUTE_SCALAR(NAME, TYPE, HVX_STMT) \ |
| 113 | static void compute_scalar_##NAME( \ |
| 114 | uint8_t * dst, const uint8_t * src0, const void * s1_table, \ |
| 115 | uint32_t cur_i11, uint32_t ne11, uint32_t n_rows, \ |
| 116 | size_t dst_stride, size_t src0_stride, uint32_t ne00) { \ |
| 117 | const TYPE * table = (const TYPE *) s1_table; \ |
| 118 | for (uint32_t r = 0; r < n_rows; r++) { \ |
| 119 | uint8_t * r_dst = dst + r * dst_stride; \ |
| 120 | const uint8_t * r_src0 = src0 + r * src0_stride; \ |
| 121 | TYPE val = table[cur_i11]; \ |
| 122 | HVX_STMT; \ |
| 123 | if (ne11 > 1 && ++cur_i11 == ne11) { \ |
| 124 | cur_i11 = 0; \ |
| 125 | } \ |
| 126 | } \ |
| 127 | } |
| 128 | |
| 129 | DEFINE_COMPUTE_SCALAR(add_f32, float, hvx_add_scalar_f32_aa(r_dst, r_src0, val, ne00)) |
| 130 | DEFINE_COMPUTE_SCALAR(add_f16, _Float16, hvx_add_scalar_f16_aa(r_dst, r_src0, val, ne00)) |
| 131 | DEFINE_COMPUTE_SCALAR(sub_f32, float, hvx_sub_scalar_f32_aa(r_dst, r_src0, val, ne00)) |
| 132 | DEFINE_COMPUTE_SCALAR(sub_f16, _Float16, hvx_sub_scalar_f16_aa(r_dst, r_src0, val, ne00)) |
| 133 | DEFINE_COMPUTE_SCALAR(mul_f32, float, hvx_mul_scalar_f32_aa(r_dst, r_src0, val, ne00)) |
| 134 | DEFINE_COMPUTE_SCALAR(mul_f16, _Float16, hvx_mul_scalar_f16_aa(r_dst, r_src0, val, ne00)) |
| 135 | DEFINE_COMPUTE_SCALAR(div_f32, float, hvx_mul_scalar_f32_aa(r_dst, r_src0, 1.0f / (val), ne00)) |
| 136 | DEFINE_COMPUTE_SCALAR(div_f16, _Float16, hvx_div_scalar_f16_aa(r_dst, r_src0, val, ne00)) |
| 137 | |
| 138 | typedef void (*compute_same_shape_t)( |
| 139 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1, |
| 140 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, size_t src1_stride, uint32_t ne00); |
| 141 | |
| 142 | #define DEFINE_COMPUTE_SAME_SHAPE(NAME, HVX_FN) \ |
| 143 | static void compute_same_shape_##NAME( \ |
| 144 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1, \ |
| 145 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, size_t src1_stride, uint32_t ne00) { \ |
| 146 | for (uint32_t r = 0; r < n_rows; r++) { \ |
| 147 | HVX_FN(dst + r * dst_stride, src0 + r * src0_stride, src1 + r * src1_stride, ne00); \ |
| 148 | } \ |
| 149 | } |
| 150 | |
| 151 | DEFINE_COMPUTE_SAME_SHAPE(add_f32, hvx_add_f32_aaa) |
| 152 | DEFINE_COMPUTE_SAME_SHAPE(add_f16, hvx_add_f16_aaa) |
| 153 | DEFINE_COMPUTE_SAME_SHAPE(sub_f32, hvx_sub_f32_aaa) |
| 154 | DEFINE_COMPUTE_SAME_SHAPE(sub_f16, hvx_sub_f16_aaa) |
| 155 | DEFINE_COMPUTE_SAME_SHAPE(mul_f32, hvx_mul_f32_aaa) |
| 156 | DEFINE_COMPUTE_SAME_SHAPE(mul_f16, hvx_mul_f16_aaa) |
| 157 | DEFINE_COMPUTE_SAME_SHAPE(div_f32, hvx_div_f32_aaa) |
| 158 | DEFINE_COMPUTE_SAME_SHAPE(div_f16, hvx_div_f16_aaa) |
| 159 | |
| 160 | typedef void (*compute_row_bcast_t)( |
| 161 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1, |
| 162 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00); |
| 163 | |
| 164 | #define DEFINE_COMPUTE_ROW_BCAST(NAME, HVX_FN) \ |
| 165 | static void compute_row_bcast_##NAME( \ |
| 166 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1, \ |
| 167 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00) { \ |
| 168 | for (uint32_t r = 0; r < n_rows; r++) { \ |
| 169 | HVX_FN(dst + r * dst_stride, src0 + r * src0_stride, src1, ne00); \ |
| 170 | } \ |
| 171 | } |
| 172 | |
| 173 | DEFINE_COMPUTE_ROW_BCAST(add_f32, hvx_add_f32_aaa) |
| 174 | DEFINE_COMPUTE_ROW_BCAST(add_f16, hvx_add_f16_aaa) |
| 175 | DEFINE_COMPUTE_ROW_BCAST(sub_f32, hvx_sub_f32_aaa) |
| 176 | DEFINE_COMPUTE_ROW_BCAST(sub_f16, hvx_sub_f16_aaa) |
| 177 | DEFINE_COMPUTE_ROW_BCAST(mul_f32, hvx_mul_f32_aaa) |
| 178 | DEFINE_COMPUTE_ROW_BCAST(mul_f16, hvx_mul_f16_aaa) |
| 179 | DEFINE_COMPUTE_ROW_BCAST(div_f32, hvx_div_f32_aaa) |
| 180 | DEFINE_COMPUTE_ROW_BCAST(div_f16, hvx_div_f16_aaa) |
| 181 | |
| 182 | typedef void (*compute_complex_t)( |
| 183 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1_plane, |
| 184 | uint32_t i01, uint32_t ne11, const struct fastdiv_values * div11, uint32_t src1_stride, |
| 185 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00); |
| 186 | |
| 187 | #define DEFINE_COMPUTE_COMPLEX(NAME, HVX_FN) \ |
| 188 | static void compute_complex_##NAME( \ |
| 189 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1_plane, \ |
| 190 | uint32_t i01, uint32_t ne11, const struct fastdiv_values * div11, uint32_t src1_stride, \ |
| 191 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00) { \ |
| 192 | for (uint32_t r = 0; r < n_rows; r++) { \ |
| 193 | uint32_t i11 = fastmodulo(i01 + r, ne11, div11); \ |
| 194 | const uint8_t * r_src1 = src1_plane + i11 * src1_stride; \ |
| 195 | HVX_FN(dst + r * dst_stride, src0 + r * src0_stride, r_src1, ne00); \ |
| 196 | } \ |
| 197 | } |
| 198 | |
| 199 | DEFINE_COMPUTE_COMPLEX(add_f32, hvx_add_f32_aau) |
| 200 | DEFINE_COMPUTE_COMPLEX(add_f16, hvx_add_f16_aau) |
| 201 | DEFINE_COMPUTE_COMPLEX(sub_f32, hvx_sub_f32_aau) |
| 202 | DEFINE_COMPUTE_COMPLEX(sub_f16, hvx_sub_f16_aau) |
| 203 | DEFINE_COMPUTE_COMPLEX(mul_f32, hvx_mul_f32_aau) |
| 204 | DEFINE_COMPUTE_COMPLEX(mul_f16, hvx_mul_f16_aau) |
| 205 | DEFINE_COMPUTE_COMPLEX(div_f32, hvx_div_f32_aau) |
| 206 | DEFINE_COMPUTE_COMPLEX(div_f16, hvx_div_f16_aau) |
| 207 | |
| 208 | typedef void (*compute_repeat_t)( |
| 209 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1_plane, |
| 210 | uint32_t i01, uint32_t ne11, const struct fastdiv_values * div11, uint32_t src1_stride, |
| 211 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00, uint32_t ne10); |
| 212 | |
| 213 | #define DEFINE_COMPUTE_REPEAT(NAME, TYPE, HVX_FN) \ |
| 214 | static void compute_repeat_##NAME( \ |
| 215 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1_plane, \ |
| 216 | uint32_t i01, uint32_t ne11, const struct fastdiv_values * div11, uint32_t src1_stride, \ |
| 217 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00, uint32_t ne10) { \ |
| 218 | for (uint32_t r = 0; r < n_rows; r++) { \ |
| 219 | uint32_t i11 = fastmodulo(i01 + r, ne11, div11); \ |
| 220 | const uint8_t * r_src1_row = src1_plane + i11 * src1_stride; \ |
| 221 | uint8_t * r_dst = dst + r * dst_stride; \ |
| 222 | const uint8_t * r_src0 = src0 + r * src0_stride; \ |
| 223 | for (uint32_t c = 0; c < ne00; c += ne10) { \ |
| 224 | uint32_t len = MIN(ne10, ne00 - c); \ |
| 225 | HVX_FN(r_dst + c * sizeof(TYPE), r_src0 + c * sizeof(TYPE), r_src1_row, len); \ |
| 226 | } \ |
| 227 | } \ |
| 228 | } |
| 229 | |
| 230 | DEFINE_COMPUTE_REPEAT(add_f32, float, hvx_add_f32_uuu) |
| 231 | DEFINE_COMPUTE_REPEAT(add_f16, _Float16, hvx_add_f16_uuu) |
| 232 | DEFINE_COMPUTE_REPEAT(sub_f32, float, hvx_sub_f32_uuu) |
| 233 | DEFINE_COMPUTE_REPEAT(sub_f16, _Float16, hvx_sub_f16_uuu) |
| 234 | DEFINE_COMPUTE_REPEAT(mul_f32, float, hvx_mul_f32_uuu) |
| 235 | DEFINE_COMPUTE_REPEAT(mul_f16, _Float16, hvx_mul_f16_uuu) |
| 236 | DEFINE_COMPUTE_REPEAT(div_f32, float, hvx_div_f32_uuu) |
| 237 | DEFINE_COMPUTE_REPEAT(div_f16, _Float16, hvx_div_f16_uuu) |
| 238 | |
| 239 | typedef void (*compute_add_id_t)( |
| 240 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1_data, const int32_t * src2_ids, |
| 241 | uint32_t i01, uint32_t i02, uint32_t ne01, uint32_t src1_stride, |
| 242 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00); |
| 243 | |
| 244 | static void compute_add_id_f32( |
| 245 | uint8_t * dst, const uint8_t * src0, const uint8_t * src1_data, const int32_t * src2_ids, |
| 246 | uint32_t i01, uint32_t i02, uint32_t ne01, uint32_t src1_stride, |
| 247 | uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00) { |
| 248 | for (uint32_t r = 0; r < n_rows; r++) { |
| 249 | uint32_t r_i01 = i01 + r; |
| 250 | const int32_t idx = src2_ids[i02 * ne01 + r_i01]; |
| 251 | if (idx < 0) { |
| 252 | memcpy(dst + r * dst_stride, src0 + r * src0_stride, ne00 * sizeof(float)); |
| 253 | continue; |
| 254 | } |
| 255 | const uint8_t * r_src1 = src1_data + idx * src1_stride; |
| 256 | const uint8_t * r_src0 = src0 + r * src0_stride; |
| 257 | uint8_t * r_dst = dst + r * dst_stride; |
| 258 | hvx_add_f32_aaa(r_dst, r_src0, r_src1, ne00); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // 1. Scalar src1 in VTCM via DMA (ne10 == 1) |
| 263 | static void binary_thread_scalar(unsigned int nth, unsigned int ith, void * data) { |
| 264 | struct htp_binary_context * bctx = (struct htp_binary_context *) data; |
| 265 | struct htp_ops_context * octx = bctx->octx; |
| 266 | htp_binary_preamble; |
| 267 | |
| 268 | const uint32_t elem_size = (src0->type == HTP_TYPE_F32) ? sizeof(float) : sizeof(_Float16); |
| 269 | const size_t plane_stride = (size_t) ne12 * ne11 * elem_size; |
| 270 | const size_t row_stride = (size_t) ne11 * elem_size; |
| 271 | |
| 272 | const uint32_t row_size_bytes = bctx->row_size_bytes; |
| 273 | const uint32_t start_row = bctx->row_start + bctx->nrows_per_thread * ith; |
| 274 | const uint32_t end_row = MIN(start_row + bctx->nrows_per_thread, bctx->row_start + bctx->total_rows); |
| 275 | if (start_row >= end_row) return; |
| 276 | |
| 277 | FARF(HIGH, "binary-scalar: %d/%d (%u:%u) row-size %u (%u)", |
| 278 | ith, nth, start_row, end_row, nb01, bctx->dst_row_size_aligned); |
| 279 | |
| 280 | const struct htp_binary_vtcm_layout * layout = &bctx->vtcm_layout; |
| 281 | uint8_t * src0_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_src0) + (ith * layout->src0_bytes_per_thread); |
| 282 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_dst) + (ith * layout->dst_bytes_per_thread); |
| 283 | size_t src0_spad_half = layout->src0_spad_half_size; |
| 284 | size_t dst_spad_half = layout->dst_spad_half_size; |
| 285 | const void * s1_table = VTCM_LAYOUT_PTR(const void, bctx->vtcm_base, layout->off_src1); |
| 286 | |
| 287 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 288 | uint32_t ir_prefetch = start_row; |
| 289 | int spad_idx = 0; |
| 290 | |
| 291 | for (int k = 0; k < 2 && ir_prefetch < end_row; k++) { |
| 292 | uint32_t current_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 293 | uint32_t i03, i02, i01, rem; |
| 294 | i03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 295 | rem = ir_prefetch - i03 * (ne02 * ne01); |
| 296 | i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 297 | i01 = rem - i02 * ne01; |
| 298 | |
| 299 | dma_addr_t src0_curr = src0->data + i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 300 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 301 | |
| 302 | uint8_t * s0_spad = src0_spad_base + spad_idx * src0_spad_half; |
| 303 | uint8_t * d_spad = dst_spad_base + spad_idx * dst_spad_half; |
| 304 | |
| 305 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, 0); |
| 306 | dma_queue_push(dma_q, dma_make_data(s0_spad, src0_curr), bctx->src0_row_size_aligned, nb01, row_size_bytes, |
| 307 | current_block_size); |
| 308 | ir_prefetch += current_block_size; |
| 309 | spad_idx ^= 1; |
| 310 | } |
| 311 | |
| 312 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 313 | compute_scalar_t compute = (compute_scalar_t) bctx->compute; |
| 314 | |
| 315 | for (uint32_t ir = start_row; ir < end_row; ) { |
| 316 | uint32_t current_block_size = calc_block_size(bctx, ir, end_row, ne01, ne02); |
| 317 | |
| 318 | uint8_t * d_spad = (uint8_t *) dma_queue_pop(dma_q).src; |
| 319 | uint8_t * s0_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 320 | |
| 321 | uint32_t i03, i02, i01, rem; |
| 322 | i03 = fastdiv(ir, &bctx->src0_dim12_div); |
| 323 | rem = ir - i03 * (ne02 * ne01); |
| 324 | i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 325 | i01 = rem - i02 * ne01; |
| 326 | |
| 327 | uint32_t i13 = fastmodulo(i03, ne13, &bctx->src1_dim3_div); |
| 328 | uint32_t i12 = fastmodulo(i02, ne12, &bctx->src1_dim2_div); |
| 329 | uint32_t i11 = fastmodulo(i01, ne11, &bctx->src1_dim1_div); |
| 330 | const uint8_t * scalar_table = (const uint8_t *) s1_table + |
| 331 | i13 * plane_stride + i12 * row_stride; |
| 332 | |
| 333 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 334 | compute(d_spad, s0_spad, scalar_table, i11, ne11, current_block_size, |
| 335 | bctx->dst_row_size_aligned, bctx->src0_row_size_aligned, ne00); |
| 336 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 337 | |
| 338 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 339 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, |
| 340 | current_block_size); |
| 341 | |
| 342 | if (ir_prefetch < end_row) { |
| 343 | uint32_t next_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 344 | uint32_t p03, p02, p01, prem; |
| 345 | p03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 346 | prem = ir_prefetch - p03 * (ne02 * ne01); |
| 347 | p02 = fastdiv(prem, &bctx->src0_dim1_div); |
| 348 | p01 = prem - p02 * ne01; |
| 349 | dma_addr_t s0_next = src0->data + p03 * nb03 + p02 * nb02 + p01 * nb01; |
| 350 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_next), bctx->src0_row_size_aligned, nb01, row_size_bytes, |
| 351 | next_block_size); |
| 352 | ir_prefetch += next_block_size; |
| 353 | } |
| 354 | ir += current_block_size; |
| 355 | } |
| 356 | |
| 357 | dma_queue_flush(dma_q); |
| 358 | } |
| 359 | |
| 360 | // 2. Vector Same Shape (ne1x == ne0x) or Simple Broadcast |
| 361 | static void binary_thread_vector_same_shape(unsigned int nth, unsigned int ith, void * data) { |
| 362 | struct htp_binary_context * bctx = (struct htp_binary_context *) data; |
| 363 | struct htp_ops_context * octx = bctx->octx; |
| 364 | htp_binary_preamble; |
| 365 | |
| 366 | const uint32_t row_size_bytes = bctx->row_size_bytes; |
| 367 | const uint32_t start_row = bctx->row_start + bctx->nrows_per_thread * ith; |
| 368 | const uint32_t end_row = MIN(start_row + bctx->nrows_per_thread, bctx->row_start + bctx->total_rows); |
| 369 | if (start_row >= end_row) return; |
| 370 | |
| 371 | FARF(HIGH, "binary-same-shape: %d/%d (%u:%u) row-size %u (%u)", |
| 372 | ith, nth, start_row, end_row, nb01, bctx->dst_row_size_aligned); |
| 373 | |
| 374 | const struct htp_binary_vtcm_layout * layout = &bctx->vtcm_layout; |
| 375 | uint8_t * src0_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_src0) + (ith * layout->src0_bytes_per_thread); |
| 376 | uint8_t * src1_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_src1) + (ith * layout->src1_bytes_per_thread); |
| 377 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_dst) + (ith * layout->dst_bytes_per_thread); |
| 378 | |
| 379 | size_t src0_spad_half = layout->src0_spad_half_size; |
| 380 | size_t src1_spad_half = layout->src1_spad_half_size; |
| 381 | size_t dst_spad_half = layout->dst_spad_half_size; |
| 382 | |
| 383 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 384 | const uint32_t n_srcs = bctx->n_srcs; |
| 385 | uint32_t ir_prefetch = start_row; |
| 386 | int spad_idx = 0; |
| 387 | |
| 388 | for (int k = 0; k < 2 && ir_prefetch < end_row; k++) { |
| 389 | uint32_t current_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 390 | uint32_t i03, i02, i01, rem; |
| 391 | i03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 392 | rem = ir_prefetch - i03 * (ne02 * ne01); |
| 393 | i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 394 | i01 = rem - i02 * ne01; |
| 395 | |
| 396 | dma_addr_t src0_curr = src0->data + i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 397 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 398 | |
| 399 | uint8_t * s0_spad = src0_spad_base + spad_idx * src0_spad_half; |
| 400 | uint8_t * s1_spad = src1_spad_base + spad_idx * src1_spad_half; |
| 401 | uint8_t * d_spad = dst_spad_base + spad_idx * dst_spad_half; |
| 402 | |
| 403 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, 0); |
| 404 | dma_queue_push(dma_q, dma_make_data(s0_spad, src0_curr), bctx->src0_row_size_aligned, nb01, row_size_bytes, current_block_size); |
| 405 | for (uint32_t s = 1; s < n_srcs; s++) { |
| 406 | const struct htp_tensor * src_s = octx->src[s]; |
| 407 | uint32_t is3 = (src_s->ne[3] == 1) ? 0 : i03; |
| 408 | uint32_t is2 = (src_s->ne[2] == 1) ? 0 : i02; |
| 409 | uint32_t is1 = (src_s->ne[1] == 1) ? 0 : i01; |
| 410 | dma_addr_t s_curr = src_s->data + is3 * src_s->nb[3] + is2 * src_s->nb[2] + is1 * src_s->nb[1]; |
| 411 | uint8_t * s_spad = s1_spad + (s - 1) * 2 * src1_spad_half; |
| 412 | dma_queue_push(dma_q, dma_make_data(s_spad, s_curr), bctx->src1_row_size_aligned, src_s->nb[1], row_size_bytes, current_block_size); |
| 413 | } |
| 414 | ir_prefetch += current_block_size; |
| 415 | spad_idx ^= 1; |
| 416 | } |
| 417 | |
| 418 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 419 | compute_same_shape_t compute = (compute_same_shape_t) bctx->compute; |
| 420 | |
| 421 | for (uint32_t ir = start_row; ir < end_row; ) { |
| 422 | uint32_t current_block_size = calc_block_size(bctx, ir, end_row, ne01, ne02); |
| 423 | uint8_t * d_spad = (uint8_t *) dma_queue_pop(dma_q).src; |
| 424 | uint8_t * s0_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 425 | uint8_t * s1_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 426 | |
| 427 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 428 | compute(d_spad, s0_spad, s1_spad, current_block_size, |
| 429 | bctx->dst_row_size_aligned, bctx->src0_row_size_aligned, bctx->src1_row_size_aligned, ne00); |
| 430 | for (uint32_t s = 2; s < n_srcs; s++) { |
| 431 | uint8_t * ss_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 432 | compute(d_spad, d_spad, ss_spad, current_block_size, |
| 433 | bctx->dst_row_size_aligned, bctx->dst_row_size_aligned, bctx->src1_row_size_aligned, ne00); |
| 434 | } |
| 435 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 436 | |
| 437 | uint32_t i03, i02, i01, rem; |
| 438 | i03 = fastdiv(ir, &bctx->src0_dim12_div); |
| 439 | rem = ir - i03 * (ne02 * ne01); |
| 440 | i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 441 | i01 = rem - i02 * ne01; |
| 442 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 443 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, current_block_size); |
| 444 | |
| 445 | if (ir_prefetch < end_row) { |
| 446 | uint32_t next_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 447 | uint32_t p03, p02, p01, prem; |
| 448 | p03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 449 | prem = ir_prefetch - p03 * (ne02 * ne01); |
| 450 | p02 = fastdiv(prem, &bctx->src0_dim1_div); |
| 451 | p01 = prem - p02 * ne01; |
| 452 | |
| 453 | dma_addr_t s0_next = src0->data + p03 * nb03 + p02 * nb02 + p01 * nb01; |
| 454 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_next), bctx->src0_row_size_aligned, nb01, row_size_bytes, next_block_size); |
| 455 | |
| 456 | for (uint32_t s = 1; s < n_srcs; s++) { |
| 457 | const struct htp_tensor * src_s = octx->src[s]; |
| 458 | uint32_t ps3 = (src_s->ne[3] == 1) ? 0 : p03; |
| 459 | uint32_t ps2 = (src_s->ne[2] == 1) ? 0 : p02; |
| 460 | uint32_t ps1 = (src_s->ne[1] == 1) ? 0 : p01; |
| 461 | dma_addr_t s_next = src_s->data + ps3 * src_s->nb[3] + ps2 * src_s->nb[2] + ps1 * src_s->nb[1]; |
| 462 | uint8_t * s_spad = s1_spad + (s - 1) * 2 * src1_spad_half; |
| 463 | dma_queue_push(dma_q, dma_make_data(s_spad, s_next), bctx->src1_row_size_aligned, src_s->nb[1], row_size_bytes, next_block_size); |
| 464 | } |
| 465 | |
| 466 | ir_prefetch += next_block_size; |
| 467 | } |
| 468 | ir += current_block_size; |
| 469 | } |
| 470 | |
| 471 | dma_queue_flush(dma_q); |
| 472 | } |
| 473 | |
| 474 | // 3. Row Broadcast (ne11 == 1, ne12 == 1, single row src1) |
| 475 | static void binary_thread_vector_row_broadcast(unsigned int nth, unsigned int ith, void * data) { |
| 476 | struct htp_binary_context * bctx = (struct htp_binary_context *) data; |
| 477 | struct htp_ops_context * octx = bctx->octx; |
| 478 | htp_binary_preamble; |
| 479 | |
| 480 | const uint32_t row_size_bytes = bctx->row_size_bytes; |
| 481 | const uint32_t start_row = bctx->row_start + bctx->nrows_per_thread * ith; |
| 482 | const uint32_t end_row = MIN(start_row + bctx->nrows_per_thread, bctx->row_start + bctx->total_rows); |
| 483 | if (start_row >= end_row) return; |
| 484 | |
| 485 | FARF(HIGH, "binary-row-bcast: %d/%d (%u:%u) row-size %u (%u)", |
| 486 | ith, nth, start_row, end_row, nb01, bctx->dst_row_size_aligned); |
| 487 | |
| 488 | const struct htp_binary_vtcm_layout * layout = &bctx->vtcm_layout; |
| 489 | uint8_t * src0_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_src0) + (ith * layout->src0_bytes_per_thread); |
| 490 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_dst) + (ith * layout->dst_bytes_per_thread); |
| 491 | |
| 492 | size_t src0_spad_half = layout->src0_spad_half_size; |
| 493 | size_t dst_spad_half = layout->dst_spad_half_size; |
| 494 | |
| 495 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 496 | uint32_t ir_prefetch = start_row; |
| 497 | int spad_idx = 0; |
| 498 | |
| 499 | void * s1_ptr = VTCM_LAYOUT_PTR(void, bctx->vtcm_base, layout->off_src1); |
| 500 | |
| 501 | for (int k = 0; k < 2 && ir_prefetch < end_row; k++) { |
| 502 | uint32_t current_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 503 | uint32_t i03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 504 | uint32_t rem = ir_prefetch - i03 * (ne02 * ne01); |
| 505 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 506 | uint32_t i01 = rem - i02 * ne01; |
| 507 | |
| 508 | dma_addr_t src0_curr = src0->data + i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 509 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 510 | |
| 511 | uint8_t * s0_spad = src0_spad_base + spad_idx * src0_spad_half; |
| 512 | uint8_t * d_spad = dst_spad_base + spad_idx * dst_spad_half; |
| 513 | |
| 514 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, 0); |
| 515 | dma_queue_push(dma_q, dma_make_data(s0_spad, src0_curr), bctx->src0_row_size_aligned, nb01, row_size_bytes, current_block_size); |
| 516 | ir_prefetch += current_block_size; |
| 517 | spad_idx ^= 1; |
| 518 | } |
| 519 | |
| 520 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 521 | compute_row_bcast_t compute = (compute_row_bcast_t) bctx->compute; |
| 522 | |
| 523 | for (uint32_t ir = start_row; ir < end_row; ) { |
| 524 | uint32_t current_block_size = calc_block_size(bctx, ir, end_row, ne01, ne02); |
| 525 | uint8_t * d_spad = (uint8_t *) dma_queue_pop(dma_q).src; |
| 526 | uint8_t * s0_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 527 | |
| 528 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 529 | compute(d_spad, s0_spad, (const uint8_t *)s1_ptr, current_block_size, |
| 530 | bctx->dst_row_size_aligned, bctx->src0_row_size_aligned, ne00); |
| 531 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 532 | |
| 533 | uint32_t i03 = fastdiv(ir, &bctx->src0_dim12_div); |
| 534 | uint32_t rem = ir - i03 * (ne02 * ne01); |
| 535 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 536 | uint32_t i01 = rem - i02 * ne01; |
| 537 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 538 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, current_block_size); |
| 539 | |
| 540 | if (ir_prefetch < end_row) { |
| 541 | uint32_t next_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 542 | uint32_t p03, p02, p01, prem; |
| 543 | p03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 544 | prem = ir_prefetch - p03 * (ne02 * ne01); |
| 545 | p02 = fastdiv(prem, &bctx->src0_dim1_div); |
| 546 | p01 = prem - p02 * ne01; |
| 547 | dma_addr_t s0_next = src0->data + p03 * nb03 + p02 * nb02 + p01 * nb01; |
| 548 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_next), bctx->src0_row_size_aligned, nb01, row_size_bytes, next_block_size); |
| 549 | ir_prefetch += next_block_size; |
| 550 | } |
| 551 | ir += current_block_size; |
| 552 | } |
| 553 | |
| 554 | dma_queue_flush(dma_q); |
| 555 | } |
| 556 | |
| 557 | // 4. Vector Complex (ne10 == ne00, complex broadcast) |
| 558 | static void binary_thread_vector_complex(unsigned int nth, unsigned int ith, void * data) { |
| 559 | struct htp_binary_context * bctx = (struct htp_binary_context *) data; |
| 560 | struct htp_ops_context * octx = bctx->octx; |
| 561 | htp_binary_preamble; |
| 562 | |
| 563 | const uint32_t row_size_bytes = bctx->row_size_bytes; |
| 564 | const uint32_t start_row = bctx->row_start + bctx->nrows_per_thread * ith; |
| 565 | const uint32_t end_row = MIN(start_row + bctx->nrows_per_thread, bctx->row_start + bctx->total_rows); |
| 566 | if (start_row >= end_row) return; |
| 567 | |
| 568 | FARF(HIGH, "binary-complex: %d/%d (%u:%u) row-size %u (%u)", |
| 569 | ith, nth, start_row, end_row, nb01, bctx->dst_row_size_aligned); |
| 570 | |
| 571 | const struct htp_binary_vtcm_layout * layout = &bctx->vtcm_layout; |
| 572 | uint8_t * src0_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_src0) + (ith * layout->src0_bytes_per_thread); |
| 573 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_dst) + (ith * layout->dst_bytes_per_thread); |
| 574 | size_t src0_spad_half = layout->src0_spad_half_size; |
| 575 | size_t dst_spad_half = layout->dst_spad_half_size; |
| 576 | const uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(const uint8_t, bctx->vtcm_base, layout->off_src1); |
| 577 | |
| 578 | const uint32_t elem_size = (src0->type == HTP_TYPE_F32) ? sizeof(float) : sizeof(_Float16); |
| 579 | const size_t vtcm_row_stride = (size_t) ne10 * elem_size; |
| 580 | const size_t vtcm_plane_stride = (size_t) ne11 * vtcm_row_stride; |
| 581 | const size_t vtcm_vol_stride = (size_t) ne12 * vtcm_plane_stride; |
| 582 | |
| 583 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 584 | uint32_t ir_prefetch = start_row; |
| 585 | int spad_idx = 0; |
| 586 | |
| 587 | for (int k = 0; k < 2 && ir_prefetch < end_row; k++) { |
| 588 | uint32_t current_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 589 | uint32_t i03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 590 | uint32_t rem = ir_prefetch - i03 * (ne02 * ne01); |
| 591 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 592 | uint32_t i01 = rem - i02 * ne01; |
| 593 | |
| 594 | dma_addr_t src0_curr = src0->data + i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 595 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 596 | |
| 597 | uint8_t * s0_spad = src0_spad_base + spad_idx * src0_spad_half; |
| 598 | uint8_t * d_spad = dst_spad_base + spad_idx * dst_spad_half; |
| 599 | |
| 600 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, 0); |
| 601 | dma_queue_push(dma_q, dma_make_data(s0_spad, src0_curr), bctx->src0_row_size_aligned, nb01, row_size_bytes, current_block_size); |
| 602 | ir_prefetch += current_block_size; |
| 603 | spad_idx ^= 1; |
| 604 | } |
| 605 | |
| 606 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 607 | compute_complex_t compute = (compute_complex_t) bctx->compute; |
| 608 | |
| 609 | for (uint32_t ir = start_row; ir < end_row; ) { |
| 610 | uint32_t current_block_size = calc_block_size(bctx, ir, end_row, ne01, ne02); |
| 611 | uint8_t * d_spad = (uint8_t *) dma_queue_pop(dma_q).src; |
| 612 | uint8_t * s0_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 613 | |
| 614 | uint32_t i03 = fastdiv(ir, &bctx->src0_dim12_div); |
| 615 | uint32_t rem = ir - i03 * (ne02 * ne01); |
| 616 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 617 | uint32_t i01 = rem - i02 * ne01; |
| 618 | |
| 619 | uint32_t i13 = fastmodulo(i03, ne13, &bctx->src1_dim3_div); |
| 620 | uint32_t i12 = fastmodulo(i02, ne12, &bctx->src1_dim2_div); |
| 621 | const uint8_t * src1_plane = vtcm_src1 + i13 * vtcm_vol_stride + i12 * vtcm_plane_stride; |
| 622 | |
| 623 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 624 | compute(d_spad, s0_spad, src1_plane, i01, ne11, &bctx->src1_dim1_div, vtcm_row_stride, |
| 625 | current_block_size, bctx->dst_row_size_aligned, bctx->src0_row_size_aligned, ne00); |
| 626 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 627 | |
| 628 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 629 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, current_block_size); |
| 630 | |
| 631 | if (ir_prefetch < end_row) { |
| 632 | uint32_t next_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 633 | uint32_t p03, p02, p01, prem; |
| 634 | p03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 635 | prem = ir_prefetch - p03 * (ne02 * ne01); |
| 636 | p02 = fastdiv(prem, &bctx->src0_dim1_div); |
| 637 | p01 = prem - p02 * ne01; |
| 638 | dma_addr_t s0_next = src0->data + p03 * nb03 + p02 * nb02 + p01 * nb01; |
| 639 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_next), bctx->src0_row_size_aligned, nb01, row_size_bytes, next_block_size); |
| 640 | ir_prefetch += next_block_size; |
| 641 | } |
| 642 | ir += current_block_size; |
| 643 | } |
| 644 | |
| 645 | dma_queue_flush(dma_q); |
| 646 | } |
| 647 | |
| 648 | // 5. Element Repeat (ne10 != ne00) |
| 649 | static void binary_thread_element_repeat(unsigned int nth, unsigned int ith, void * data) { |
| 650 | struct htp_binary_context * bctx = (struct htp_binary_context *) data; |
| 651 | struct htp_ops_context * octx = bctx->octx; |
| 652 | htp_binary_preamble; |
| 653 | |
| 654 | const uint32_t row_size_bytes = bctx->row_size_bytes; |
| 655 | const uint32_t start_row = bctx->row_start + bctx->nrows_per_thread * ith; |
| 656 | const uint32_t end_row = MIN(start_row + bctx->nrows_per_thread, bctx->row_start + bctx->total_rows); |
| 657 | if (start_row >= end_row) return; |
| 658 | |
| 659 | const struct htp_binary_vtcm_layout * layout = &bctx->vtcm_layout; |
| 660 | uint8_t * src0_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_src0) + (ith * layout->src0_bytes_per_thread); |
| 661 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_dst) + (ith * layout->dst_bytes_per_thread); |
| 662 | size_t src0_spad_half = layout->src0_spad_half_size; |
| 663 | size_t dst_spad_half = layout->dst_spad_half_size; |
| 664 | const uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(const uint8_t, bctx->vtcm_base, layout->off_src1); |
| 665 | |
| 666 | const uint32_t elem_size = (src0->type == HTP_TYPE_F32) ? sizeof(float) : sizeof(_Float16); |
| 667 | const size_t vtcm_row_stride = (size_t) ne10 * elem_size; |
| 668 | const size_t vtcm_plane_stride = (size_t) ne11 * vtcm_row_stride; |
| 669 | const size_t vtcm_vol_stride = (size_t) ne12 * vtcm_plane_stride; |
| 670 | |
| 671 | FARF(HIGH, "binary-repeat: %d/%d (%u:%u) row-size %u (%u)", |
| 672 | ith, nth, start_row, end_row, nb01, bctx->dst_row_size_aligned); |
| 673 | |
| 674 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 675 | uint32_t ir_prefetch = start_row; |
| 676 | int spad_idx = 0; |
| 677 | |
| 678 | for (int k = 0; k < 2 && ir_prefetch < end_row; k++) { |
| 679 | uint32_t current_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 680 | uint32_t i03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 681 | uint32_t rem = ir_prefetch - i03 * (ne02 * ne01); |
| 682 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 683 | uint32_t i01 = rem - i02 * ne01; |
| 684 | |
| 685 | dma_addr_t src0_curr = src0->data + i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 686 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 687 | |
| 688 | uint8_t * s0_spad = src0_spad_base + spad_idx * src0_spad_half; |
| 689 | uint8_t * d_spad = dst_spad_base + spad_idx * dst_spad_half; |
| 690 | |
| 691 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, 0); |
| 692 | dma_queue_push(dma_q, dma_make_data(s0_spad, src0_curr), bctx->src0_row_size_aligned, nb01, row_size_bytes, current_block_size); |
| 693 | ir_prefetch += current_block_size; |
| 694 | spad_idx ^= 1; |
| 695 | } |
| 696 | |
| 697 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 698 | compute_repeat_t compute = (compute_repeat_t) bctx->compute; |
| 699 | |
| 700 | for (uint32_t ir = start_row; ir < end_row; ) { |
| 701 | uint32_t current_block_size = calc_block_size(bctx, ir, end_row, ne01, ne02); |
| 702 | uint8_t * d_spad = (uint8_t *) dma_queue_pop(dma_q).src; |
| 703 | uint8_t * s0_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 704 | |
| 705 | uint32_t i03 = fastdiv(ir, &bctx->src0_dim12_div); |
| 706 | uint32_t rem = ir - i03 * (ne02 * ne01); |
| 707 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 708 | uint32_t i01 = rem - i02 * ne01; |
| 709 | |
| 710 | uint32_t i13 = fastmodulo(i03, ne13, &bctx->src1_dim3_div); |
| 711 | uint32_t i12 = fastmodulo(i02, ne12, &bctx->src1_dim2_div); |
| 712 | const uint8_t * src1_plane = vtcm_src1 + i13 * vtcm_vol_stride + i12 * vtcm_plane_stride; |
| 713 | |
| 714 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 715 | compute(d_spad, s0_spad, src1_plane, i01, ne11, &bctx->src1_dim1_div, vtcm_row_stride, |
| 716 | current_block_size, bctx->dst_row_size_aligned, bctx->src0_row_size_aligned, ne00, ne10); |
| 717 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 718 | |
| 719 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 720 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, current_block_size); |
| 721 | |
| 722 | if (ir_prefetch < end_row) { |
| 723 | uint32_t next_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 724 | uint32_t p03, p02, p01, prem; |
| 725 | p03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 726 | prem = ir_prefetch - p03 * (ne02 * ne01); |
| 727 | p02 = fastdiv(prem, &bctx->src0_dim1_div); |
| 728 | p01 = prem - p02 * ne01; |
| 729 | dma_addr_t s0_next = src0->data + p03 * nb03 + p02 * nb02 + p01 * nb01; |
| 730 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_next), bctx->src0_row_size_aligned, nb01, row_size_bytes, next_block_size); |
| 731 | ir_prefetch += next_block_size; |
| 732 | } |
| 733 | ir += current_block_size; |
| 734 | } |
| 735 | |
| 736 | dma_queue_flush(dma_q); |
| 737 | } |
| 738 | |
| 739 | // 6. ADD_ID (src1 gathered via src2 indices) |
| 740 | static void binary_thread_add_id_f32(unsigned int nth, unsigned int ith, void * data) { |
| 741 | struct htp_binary_context * bctx = (struct htp_binary_context *) data; |
| 742 | struct htp_ops_context * octx = bctx->octx; |
| 743 | |
| 744 | const struct htp_tensor * src0 = octx->src[0]; |
| 745 | const struct htp_tensor * src1 = octx->src[1]; |
| 746 | const struct htp_tensor * dst = octx->dst; |
| 747 | |
| 748 | const uint32_t ne00 = src0->ne[0]; |
| 749 | const uint32_t ne01 = src0->ne[1]; |
| 750 | const uint32_t ne02 = src0->ne[2]; |
| 751 | const uint32_t ne03 = src0->ne[3]; |
| 752 | |
| 753 | const uint32_t nb01 = src0->nb[1]; |
| 754 | const uint32_t nb02 = src0->nb[2]; |
| 755 | const uint32_t nb03 = src0->nb[3]; |
| 756 | const uint32_t src1_stride = bctx->src1_row_size_aligned; |
| 757 | |
| 758 | const uint32_t nb1 = dst->nb[1]; |
| 759 | const uint32_t nb2 = dst->nb[2]; |
| 760 | const uint32_t nb3 = dst->nb[3]; |
| 761 | |
| 762 | const uint32_t row_size_bytes = bctx->row_size_bytes; |
| 763 | const uint32_t start_row = bctx->row_start + bctx->nrows_per_thread * ith; |
| 764 | const uint32_t end_row = MIN(start_row + bctx->nrows_per_thread, bctx->row_start + bctx->total_rows); |
| 765 | if (start_row >= end_row) return; |
| 766 | |
| 767 | const struct htp_binary_vtcm_layout * layout = &bctx->vtcm_layout; |
| 768 | uint8_t * src0_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_src0) + (ith * layout->src0_bytes_per_thread); |
| 769 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, bctx->vtcm_base, layout->off_dst) + (ith * layout->dst_bytes_per_thread); |
| 770 | const uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(const uint8_t, bctx->vtcm_base, layout->off_src1); |
| 771 | const int32_t * vtcm_src2 = VTCM_LAYOUT_PTR(const int32_t, bctx->vtcm_base, layout->off_src2); |
| 772 | size_t src0_spad_half = layout->src0_spad_half_size; |
| 773 | size_t dst_spad_half = layout->dst_spad_half_size; |
| 774 | |
| 775 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 776 | uint32_t ir_prefetch = start_row; |
| 777 | int spad_idx = 0; |
| 778 | |
| 779 | for (int k = 0; k < 2 && ir_prefetch < end_row; k++) { |
| 780 | uint32_t current_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 781 | uint32_t i03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 782 | uint32_t rem = ir_prefetch - i03 * (ne02 * ne01); |
| 783 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 784 | uint32_t i01 = rem - i02 * ne01; |
| 785 | |
| 786 | dma_addr_t src0_curr = src0->data + i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 787 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 788 | |
| 789 | uint8_t * s0_spad = src0_spad_base + spad_idx * src0_spad_half; |
| 790 | uint8_t * d_spad = dst_spad_base + spad_idx * dst_spad_half; |
| 791 | |
| 792 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, 0); |
| 793 | dma_queue_push(dma_q, dma_make_data(s0_spad, src0_curr), bctx->src0_row_size_aligned, nb01, row_size_bytes, current_block_size); |
| 794 | ir_prefetch += current_block_size; |
| 795 | spad_idx ^= 1; |
| 796 | } |
| 797 | |
| 798 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 799 | |
| 800 | for (uint32_t ir = start_row; ir < end_row; ) { |
| 801 | uint32_t current_block_size = calc_block_size(bctx, ir, end_row, ne01, ne02); |
| 802 | uint8_t * d_spad = (uint8_t *) dma_queue_pop(dma_q).src; |
| 803 | uint8_t * s0_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 804 | |
| 805 | uint32_t i03 = fastdiv(ir, &bctx->src0_dim12_div); |
| 806 | uint32_t rem = ir - i03 * (ne02 * ne01); |
| 807 | uint32_t i02 = fastdiv(rem, &bctx->src0_dim1_div); |
| 808 | uint32_t i01 = rem - i02 * ne01; |
| 809 | |
| 810 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 811 | compute_add_id_t compute = (compute_add_id_t) bctx->compute; |
| 812 | compute(d_spad, s0_spad, vtcm_src1, vtcm_src2, |
| 813 | i01, i02, ne01, src1_stride, |
| 814 | current_block_size, bctx->dst_row_size_aligned, bctx->src0_row_size_aligned, ne00); |
| 815 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 816 | |
| 817 | dma_addr_t dst_curr = dst->data + i03 * nb3 + i02 * nb2 + i01 * nb1; |
| 818 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), nb1, bctx->dst_row_size_aligned, row_size_bytes, current_block_size); |
| 819 | |
| 820 | if (ir_prefetch < end_row) { |
| 821 | uint32_t next_block_size = calc_block_size(bctx, ir_prefetch, end_row, ne01, ne02); |
| 822 | uint32_t p03, p02, p01, prem; |
| 823 | p03 = fastdiv(ir_prefetch, &bctx->src0_dim12_div); |
| 824 | prem = ir_prefetch - p03 * (ne02 * ne01); |
| 825 | p02 = fastdiv(prem, &bctx->src0_dim1_div); |
| 826 | p01 = prem - p02 * ne01; |
| 827 | dma_addr_t s0_next = src0->data + p03 * nb03 + p02 * nb02 + p01 * nb01; |
| 828 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_next), bctx->src0_row_size_aligned, nb01, row_size_bytes, next_block_size); |
| 829 | ir_prefetch += next_block_size; |
| 830 | } |
| 831 | ir += current_block_size; |
| 832 | } |
| 833 | |
| 834 | dma_queue_flush(dma_q); |
| 835 | } |
| 836 | |
| 837 | static inline void hvx_div_scalar_f32(uint8_t * restrict dst, const uint8_t * restrict src, const float val, const uint32_t num_elems) { |
| 838 | hvx_mul_scalar_f32(dst, src, 1.0f / val, num_elems); |
| 839 | } |
| 840 | |
| 841 | static inline void hvx_div_scalar_f16(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 val, const uint32_t num_elems) { |
| 842 | hvx_div_scalar_f16_aa(dst, src, val, num_elems); |
| 843 | } |
| 844 | |
| 845 | typedef void (*compute_binary_chunked_t)( |
| 846 | uint8_t * restrict dst, |
| 847 | const uint8_t * restrict src0, |
| 848 | const uint8_t * restrict src1, |
| 849 | const uint32_t num_elems |
| 850 | ); |
| 851 | |
| 852 | typedef void (*compute_binary_scalar_chunked_f32_t)( |
| 853 | uint8_t * restrict dst, |
| 854 | const uint8_t * restrict src0, |
| 855 | const float val, |
| 856 | const uint32_t num_elems |
| 857 | ); |
| 858 | |
| 859 | typedef void (*compute_binary_scalar_chunked_f16_t)( |
| 860 | uint8_t * restrict dst, |
| 861 | const uint8_t * restrict src0, |
| 862 | const _Float16 val, |
| 863 | const uint32_t num_elems |
| 864 | ); |
| 865 | |
| 866 | struct binary_chunked_context { |
| 867 | struct htp_ops_context * octx; |
| 868 | struct htp_binary_vtcm_layout vtcm_layout; |
| 869 | uint8_t * vtcm_base; |
| 870 | uint32_t elem_start; |
| 871 | uint32_t nelem; |
| 872 | uint32_t chunk_size; |
| 873 | uint32_t chunks_per_thread; |
| 874 | uint32_t total_chunks; |
| 875 | uint32_t n_srcs; |
| 876 | bool is_scalar; |
| 877 | float scalar_f32; |
| 878 | _Float16 scalar_f16; |
| 879 | compute_binary_chunked_t compute; |
| 880 | compute_binary_scalar_chunked_f32_t compute_scalar_f32; |
| 881 | compute_binary_scalar_chunked_f16_t compute_scalar_f16; |
| 882 | }; |
| 883 | |
| 884 | static void binary_thread_chunked(unsigned int nth, unsigned int ith, void * data) { |
| 885 | (void) nth; |
| 886 | struct binary_chunked_context * ctx = (struct binary_chunked_context *) data; |
| 887 | struct htp_ops_context * octx = ctx->octx; |
| 888 | const struct htp_tensor * src0 = octx->src[0]; |
| 889 | const struct htp_tensor * dst = octx->dst; |
| 890 | |
| 891 | const uint32_t start_chunk = ctx->chunks_per_thread * ith; |
| 892 | const uint32_t end_chunk = MIN(start_chunk + ctx->chunks_per_thread, ctx->total_chunks); |
| 893 | if (start_chunk >= end_chunk) { |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | const uint32_t src0_type = src0->type; |
| 898 | const size_t elem_size = (src0_type == HTP_TYPE_F32) ? sizeof(float) : sizeof(_Float16); |
| 899 | const uint32_t chunk_size = ctx->chunk_size; |
| 900 | const size_t chunk_bytes = ctx->vtcm_layout.src0_spad_half_size; |
| 901 | |
| 902 | FARF(HIGH, "binary-chunked: %d/%d (%u:%u) chunks %u elems %u", |
| 903 | ith, nth, start_chunk, end_chunk, ctx->total_chunks, ctx->nelem); |
| 904 | |
| 905 | const struct htp_binary_vtcm_layout * layout = &ctx->vtcm_layout; |
| 906 | uint8_t * src0_spad_base = VTCM_LAYOUT_PTR(uint8_t, ctx->vtcm_base, layout->off_src0) + (ith * layout->src0_bytes_per_thread); |
| 907 | uint8_t * src1_spad_base = ctx->is_scalar ? NULL : (VTCM_LAYOUT_PTR(uint8_t, ctx->vtcm_base, layout->off_src1) + (ith * layout->src1_bytes_per_thread)); |
| 908 | uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, ctx->vtcm_base, layout->off_dst) + (ith * layout->dst_bytes_per_thread); |
| 909 | |
| 910 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 911 | const uint32_t n_srcs = ctx->n_srcs; |
| 912 | uint32_t prefetch_chunk = start_chunk; |
| 913 | int spad_idx = 0; |
| 914 | |
| 915 | for (int k = 0; k < 2 && prefetch_chunk < end_chunk; k++) { |
| 916 | const uint32_t c_start = ctx->elem_start + prefetch_chunk * chunk_size; |
| 917 | const uint32_t c_end = MIN(c_start + chunk_size, ctx->elem_start + ctx->nelem); |
| 918 | const uint32_t cur_elems = c_end - c_start; |
| 919 | const uint32_t cur_bytes = cur_elems * elem_size; |
| 920 | |
| 921 | dma_addr_t s0_curr = src0->data + (size_t) c_start * elem_size; |
| 922 | dma_addr_t d_curr = dst->data + (size_t) c_start * elem_size; |
| 923 | |
| 924 | uint8_t * s0_spad = src0_spad_base + spad_idx * chunk_bytes; |
| 925 | uint8_t * d_spad = dst_spad_base + spad_idx * chunk_bytes; |
| 926 | |
| 927 | dma_queue_push(dma_q, dma_make_data(d_curr, d_spad), chunk_bytes, chunk_bytes, cur_bytes, 0); |
| 928 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_curr), chunk_bytes, chunk_bytes, cur_bytes, 1); |
| 929 | if (!ctx->is_scalar) { |
| 930 | uint8_t * s1_spad = src1_spad_base + spad_idx * chunk_bytes; |
| 931 | for (uint32_t s = 1; s < n_srcs; s++) { |
| 932 | dma_addr_t s_curr = octx->src[s]->data + (size_t) c_start * elem_size; |
| 933 | uint8_t * s_spad = s1_spad + (s - 1) * 2 * chunk_bytes; |
| 934 | dma_queue_push(dma_q, dma_make_data(s_spad, s_curr), chunk_bytes, chunk_bytes, cur_bytes, 1); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | prefetch_chunk++; |
| 939 | spad_idx ^= 1; |
| 940 | } |
| 941 | |
| 942 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 943 | |
| 944 | for (uint32_t c = start_chunk; c < end_chunk; c++) { |
| 945 | const uint32_t c_start = ctx->elem_start + c * chunk_size; |
| 946 | const uint32_t c_end = MIN(c_start + chunk_size, ctx->elem_start + ctx->nelem); |
| 947 | const uint32_t cur_elems = c_end - c_start; |
| 948 | const uint32_t cur_bytes = cur_elems * elem_size; |
| 949 | |
| 950 | uint8_t * d_spad = (uint8_t *) dma_queue_pop(dma_q).src; |
| 951 | uint8_t * s0_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 952 | uint8_t * s1_spad = ctx->is_scalar ? NULL : (uint8_t *) dma_queue_pop(dma_q).dst; |
| 953 | |
| 954 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) c); |
| 955 | if (ctx->is_scalar) { |
| 956 | if (src0_type == HTP_TYPE_F32) { |
| 957 | ctx->compute_scalar_f32(d_spad, s0_spad, ctx->scalar_f32, cur_elems); |
| 958 | } else { |
| 959 | ctx->compute_scalar_f16(d_spad, s0_spad, ctx->scalar_f16, cur_elems); |
| 960 | } |
| 961 | } else { |
| 962 | ctx->compute(d_spad, s0_spad, s1_spad, cur_elems); |
| 963 | for (uint32_t s = 2; s < n_srcs; s++) { |
| 964 | uint8_t * s_spad = (uint8_t *) dma_queue_pop(dma_q).dst; |
| 965 | ctx->compute(d_spad, d_spad, s_spad, cur_elems); |
| 966 | } |
| 967 | } |
| 968 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) c); |
| 969 | |
| 970 | dma_addr_t dst_curr = dst->data + (size_t) c_start * elem_size; |
| 971 | dma_queue_push(dma_q, dma_make_data(dst_curr, d_spad), chunk_bytes, chunk_bytes, cur_bytes, 1); |
| 972 | |
| 973 | if (prefetch_chunk < end_chunk) { |
| 974 | const uint32_t pc_start = ctx->elem_start + prefetch_chunk * chunk_size; |
| 975 | const uint32_t pc_end = MIN(pc_start + chunk_size, ctx->elem_start + ctx->nelem); |
| 976 | const uint32_t p_elems = pc_end - pc_start; |
| 977 | const uint32_t p_bytes = p_elems * elem_size; |
| 978 | |
| 979 | dma_addr_t s0_next = src0->data + (size_t) pc_start * elem_size; |
| 980 | dma_queue_push(dma_q, dma_make_data(s0_spad, s0_next), chunk_bytes, chunk_bytes, p_bytes, 1); |
| 981 | if (!ctx->is_scalar) { |
| 982 | for (uint32_t s = 1; s < n_srcs; s++) { |
| 983 | dma_addr_t s_next = octx->src[s]->data + (size_t) pc_start * elem_size; |
| 984 | uint8_t * s_spad = s1_spad + (s - 1) * 2 * chunk_bytes; |
| 985 | dma_queue_push(dma_q, dma_make_data(s_spad, s_next), chunk_bytes, chunk_bytes, p_bytes, 1); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | prefetch_chunk++; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | dma_queue_flush(dma_q); |
| 994 | } |
| 995 | |
| 996 | static int execute_op_binary(struct htp_ops_context * octx) { |
| 997 | const struct htp_tensor * src0 = octx->src[0]; |
| 998 | const struct htp_tensor * src1 = octx->src[1]; |
| 999 | const struct htp_tensor * dst = octx->dst; |
| 1000 | const struct htp_binary_kernel_params * kparams = (const struct htp_binary_kernel_params *) octx->kernel_params; |
| 1001 | const uint32_t n_srcs = kparams->n_srcs > 2 ? kparams->n_srcs : 2; |
| 1002 | |
| 1003 | const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; |
| 1004 | |
| 1005 | // Use packed row sizes for VTCM allocation and alignment |
| 1006 | const uint32_t src0_type = octx->src[0]->type; |
| 1007 | const size_t elem_size = (src0_type == HTP_TYPE_F32) ? sizeof(float) : sizeof(_Float16); |
| 1008 | const size_t src0_row_size = src0->ne[0] * elem_size; |
| 1009 | const size_t src1_row_size = src1->ne[0] * elem_size; |
| 1010 | const size_t dst_row_size = dst->ne[0] * elem_size; |
| 1011 | |
| 1012 | if (kparams->kernel_type == HTP_BINARY_KERNEL_CHUNKED) { |
| 1013 | const bool is_scalar = kparams->is_scalar; |
| 1014 | |
| 1015 | compute_binary_chunked_t compute = NULL; |
| 1016 | compute_binary_scalar_chunked_f32_t compute_scalar_f32 = NULL; |
| 1017 | compute_binary_scalar_chunked_f16_t compute_scalar_f16 = NULL; |
| 1018 | |
| 1019 | if (is_scalar) { |
| 1020 | if (src0_type == HTP_TYPE_F32) { |
| 1021 | switch (octx->op) { |
| 1022 | case HTP_OP_ADD: compute_scalar_f32 = hvx_add_scalar_f32; break; |
| 1023 | case HTP_OP_SUB: compute_scalar_f32 = hvx_sub_scalar_f32; break; |
| 1024 | case HTP_OP_MUL: compute_scalar_f32 = hvx_mul_scalar_f32; break; |
| 1025 | case HTP_OP_DIV: compute_scalar_f32 = hvx_div_scalar_f32; break; |
| 1026 | default: break; |
| 1027 | } |
| 1028 | } else if (src0_type == HTP_TYPE_F16) { |
| 1029 | switch (octx->op) { |
| 1030 | case HTP_OP_ADD: compute_scalar_f16 = hvx_add_scalar_f16; break; |
| 1031 | case HTP_OP_SUB: compute_scalar_f16 = hvx_sub_scalar_f16; break; |
| 1032 | case HTP_OP_MUL: compute_scalar_f16 = hvx_mul_scalar_f16; break; |
| 1033 | case HTP_OP_DIV: compute_scalar_f16 = hvx_div_scalar_f16; break; |
| 1034 | default: break; |
| 1035 | } |
| 1036 | } |
| 1037 | if (!compute_scalar_f32 && !compute_scalar_f16) { |
| 1038 | return HTP_STATUS_NO_SUPPORT; |
| 1039 | } |
| 1040 | } else { |
| 1041 | if (src0_type == HTP_TYPE_F32) { |
| 1042 | switch (octx->op) { |
| 1043 | case HTP_OP_ADD: compute = hvx_add_f32; break; |
| 1044 | case HTP_OP_SUB: compute = hvx_sub_f32; break; |
| 1045 | case HTP_OP_MUL: compute = hvx_mul_f32; break; |
| 1046 | case HTP_OP_DIV: compute = hvx_div_f32; break; |
| 1047 | default: break; |
| 1048 | } |
| 1049 | } else if (src0_type == HTP_TYPE_F16) { |
| 1050 | switch (octx->op) { |
| 1051 | case HTP_OP_ADD: compute = hvx_add_f16; break; |
| 1052 | case HTP_OP_SUB: compute = hvx_sub_f16; break; |
| 1053 | case HTP_OP_MUL: compute = hvx_mul_f16; break; |
| 1054 | case HTP_OP_DIV: compute = hvx_div_f16; break; |
| 1055 | default: break; |
| 1056 | } |
| 1057 | } |
| 1058 | if (!compute) { |
| 1059 | return HTP_STATUS_NO_SUPPORT; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | const uint32_t total_elems = (uint32_t) (src0->ne[0] * src0->ne[1] * src0->ne[2] * src0->ne[3]); |
| 1064 | if (total_elems == 0) { |
| 1065 | return HTP_STATUS_OK; |
| 1066 | } |
| 1067 | |
| 1068 | uint32_t elem_start = 0; |
| 1069 | uint32_t nelem = total_elems; |
| 1070 | const uint32_t elems_per_line = (src0_type == HTP_TYPE_F32) ? 32 : 64; |
| 1071 | |
| 1072 | if (octx->ctx->mdev.count > 1) { |
| 1073 | bool can_split = htp_tensor_mdev_data_aligned(dst) && |
| 1074 | htp_tensor_mdev_data_aligned(src0) && |
| 1075 | (is_scalar || htp_tensor_mdev_data_aligned(src1)) && |
| 1076 | htp_tensor_is_contiguous(dst, elem_size) && |
| 1077 | htp_tensor_is_contiguous(src0, elem_size) && |
| 1078 | (is_scalar || htp_tensor_is_contiguous(src1, elem_size)); |
| 1079 | for (uint32_t s = 2; can_split && s < n_srcs; s++) { |
| 1080 | can_split = htp_tensor_mdev_data_aligned(octx->src[s]) && |
| 1081 | htp_tensor_is_contiguous(octx->src[s], elem_size); |
| 1082 | } |
| 1083 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( |
| 1084 | total_elems, can_split ? elems_per_line : 0, |
| 1085 | octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 1086 | elem_start = range.start; |
| 1087 | nelem = range.count; |
| 1088 | } |
| 1089 | |
| 1090 | if (nelem == 0) { |
| 1091 | return HTP_STATUS_OK; |
| 1092 | } |
| 1093 | |
| 1094 | if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { |
| 1095 | return HTP_STATUS_INVAL_PARAMS; |
| 1096 | } |
| 1097 | |
| 1098 | struct htp_binary_vtcm_layout vtcm_layout; |
| 1099 | htp_binary_vtcm_layout_build(&vtcm_layout, kparams, octx->ctx->vtcm_size); |
| 1100 | if (vtcm_layout.total_bytes == 0 || vtcm_layout.total_bytes > octx->ctx->vtcm_size) { |
| 1101 | return HTP_STATUS_VTCM_TOO_SMALL; |
| 1102 | } |
| 1103 | |
| 1104 | const uint32_t chunk_size = kparams->chunk_size; |
| 1105 | const uint32_t total_chunks = (nelem + chunk_size - 1) / chunk_size; |
| 1106 | const uint32_t n_threads = (total_chunks >= 2) ? MIN(octx->n_threads, total_chunks) : 1; |
| 1107 | const uint32_t chunks_per_thread = (total_chunks + n_threads - 1) / n_threads; |
| 1108 | |
| 1109 | float scalar_f32 = 0.0f; |
| 1110 | _Float16 scalar_f16 = 0; |
| 1111 | if (is_scalar) { |
| 1112 | uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, vtcm_layout.off_src1); |
| 1113 | dma_queue * dma_q = octx->ctx->dma[0]; |
| 1114 | dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data), 128, 0, elem_size, 1); |
| 1115 | dma_queue_pop(dma_q); |
| 1116 | |
| 1117 | if (src0_type == HTP_TYPE_F32) { |
| 1118 | scalar_f32 = ((const float *) vtcm_src1)[0]; |
| 1119 | } else { |
| 1120 | scalar_f16 = ((const _Float16 *) vtcm_src1)[0]; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | struct binary_chunked_context cctx = { |
| 1125 | .octx = octx, |
| 1126 | .vtcm_layout = vtcm_layout, |
| 1127 | .vtcm_base = (uint8_t *) octx->ctx->vtcm_base, |
| 1128 | .elem_start = elem_start, |
| 1129 | .nelem = nelem, |
| 1130 | .chunk_size = chunk_size, |
| 1131 | .chunks_per_thread = chunks_per_thread, |
| 1132 | .total_chunks = total_chunks, |
| 1133 | .n_srcs = n_srcs, |
| 1134 | .is_scalar = is_scalar, |
| 1135 | .scalar_f32 = scalar_f32, |
| 1136 | .scalar_f16 = scalar_f16, |
| 1137 | .compute = compute, |
| 1138 | .compute_scalar_f32 = compute_scalar_f32, |
| 1139 | .compute_scalar_f16 = compute_scalar_f16, |
| 1140 | }; |
| 1141 | |
| 1142 | work_queue_run(octx->ctx->work_queue, binary_thread_chunked, &cctx, n_threads); |
| 1143 | return HTP_STATUS_OK; |
| 1144 | } |
| 1145 | |
| 1146 | uint32_t row_start = 0; |
| 1147 | uint32_t nrows = src0_nrows; |
| 1148 | |
| 1149 | if (octx->ctx->mdev.count > 1) { |
| 1150 | uint32_t rows_per_chunk = 0; |
| 1151 | htp_tensor_mdev_rows_per_chunk(dst, (uint32_t) elem_size, (uint32_t) dst_row_size, &rows_per_chunk); |
| 1152 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, rows_per_chunk, |
| 1153 | octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 1154 | row_start = range.start; |
| 1155 | nrows = range.count; |
| 1156 | } |
| 1157 | |
| 1158 | if (nrows == 0) { |
| 1159 | return HTP_STATUS_OK; |
| 1160 | } |
| 1161 | |
| 1162 | if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { |
| 1163 | return HTP_STATUS_INVAL_PARAMS; |
| 1164 | } |
| 1165 | |
| 1166 | const uint32_t n_threads = octx->n_threads; |
| 1167 | const size_t src0_row_size_aligned = kparams->src0_row_size_aligned; |
| 1168 | const size_t src1_row_size_aligned = kparams->src1_row_size_aligned; |
| 1169 | const size_t dst_row_size_aligned = kparams->dst_row_size_aligned; |
| 1170 | |
| 1171 | struct htp_binary_context bctx; |
| 1172 | bctx.vtcm_base = (uint8_t *) octx->ctx->vtcm_base; |
| 1173 | htp_binary_vtcm_layout_build(&bctx.vtcm_layout, kparams, octx->ctx->vtcm_size); |
| 1174 | |
| 1175 | if (bctx.vtcm_layout.rows_per_buffer == 0 || bctx.vtcm_layout.total_bytes > octx->ctx->vtcm_size) { |
| 1176 | return HTP_STATUS_VTCM_TOO_SMALL; |
| 1177 | } |
| 1178 | |
| 1179 | worker_callback_t worker_func = NULL; |
| 1180 | void * compute_func = NULL; |
| 1181 | |
| 1182 | switch (kparams->kernel_type) { |
| 1183 | case HTP_BINARY_KERNEL_SAME_SHAPE: |
| 1184 | worker_func = binary_thread_vector_same_shape; |
| 1185 | if (src0_type == HTP_TYPE_F32) { |
| 1186 | switch (octx->op) { |
| 1187 | case HTP_OP_ADD: compute_func = compute_same_shape_add_f32; break; |
| 1188 | case HTP_OP_SUB: compute_func = compute_same_shape_sub_f32; break; |
| 1189 | case HTP_OP_MUL: compute_func = compute_same_shape_mul_f32; break; |
| 1190 | case HTP_OP_DIV: compute_func = compute_same_shape_div_f32; break; |
| 1191 | default: break; |
| 1192 | } |
| 1193 | } else if (src0_type == HTP_TYPE_F16) { |
| 1194 | switch (octx->op) { |
| 1195 | case HTP_OP_ADD: compute_func = compute_same_shape_add_f16; break; |
| 1196 | case HTP_OP_SUB: compute_func = compute_same_shape_sub_f16; break; |
| 1197 | case HTP_OP_MUL: compute_func = compute_same_shape_mul_f16; break; |
| 1198 | case HTP_OP_DIV: compute_func = compute_same_shape_div_f16; break; |
| 1199 | default: break; |
| 1200 | } |
| 1201 | } |
| 1202 | break; |
| 1203 | case HTP_BINARY_KERNEL_ROW_BCAST: |
| 1204 | worker_func = binary_thread_vector_row_broadcast; |
| 1205 | if (src0_type == HTP_TYPE_F32) { |
| 1206 | switch (octx->op) { |
| 1207 | case HTP_OP_ADD: compute_func = compute_row_bcast_add_f32; break; |
| 1208 | case HTP_OP_SUB: compute_func = compute_row_bcast_sub_f32; break; |
| 1209 | case HTP_OP_MUL: compute_func = compute_row_bcast_mul_f32; break; |
| 1210 | case HTP_OP_DIV: compute_func = compute_row_bcast_div_f32; break; |
| 1211 | default: break; |
| 1212 | } |
| 1213 | } else if (src0_type == HTP_TYPE_F16) { |
| 1214 | switch (octx->op) { |
| 1215 | case HTP_OP_ADD: compute_func = compute_row_bcast_add_f16; break; |
| 1216 | case HTP_OP_SUB: compute_func = compute_row_bcast_sub_f16; break; |
| 1217 | case HTP_OP_MUL: compute_func = compute_row_bcast_mul_f16; break; |
| 1218 | case HTP_OP_DIV: compute_func = compute_row_bcast_div_f16; break; |
| 1219 | default: break; |
| 1220 | } |
| 1221 | } |
| 1222 | break; |
| 1223 | case HTP_BINARY_KERNEL_SCALAR: |
| 1224 | worker_func = binary_thread_scalar; |
| 1225 | if (src0_type == HTP_TYPE_F32) { |
| 1226 | switch (octx->op) { |
| 1227 | case HTP_OP_ADD: compute_func = compute_scalar_add_f32; break; |
| 1228 | case HTP_OP_SUB: compute_func = compute_scalar_sub_f32; break; |
| 1229 | case HTP_OP_MUL: compute_func = compute_scalar_mul_f32; break; |
| 1230 | case HTP_OP_DIV: compute_func = compute_scalar_div_f32; break; |
| 1231 | default: break; |
| 1232 | } |
| 1233 | } else if (src0_type == HTP_TYPE_F16) { |
| 1234 | switch (octx->op) { |
| 1235 | case HTP_OP_ADD: compute_func = compute_scalar_add_f16; break; |
| 1236 | case HTP_OP_SUB: compute_func = compute_scalar_sub_f16; break; |
| 1237 | case HTP_OP_MUL: compute_func = compute_scalar_mul_f16; break; |
| 1238 | case HTP_OP_DIV: compute_func = compute_scalar_div_f16; break; |
| 1239 | default: break; |
| 1240 | } |
| 1241 | } |
| 1242 | break; |
| 1243 | case HTP_BINARY_KERNEL_COMPLEX: |
| 1244 | worker_func = binary_thread_vector_complex; |
| 1245 | if (src0_type == HTP_TYPE_F32) { |
| 1246 | switch (octx->op) { |
| 1247 | case HTP_OP_ADD: compute_func = compute_complex_add_f32; break; |
| 1248 | case HTP_OP_SUB: compute_func = compute_complex_sub_f32; break; |
| 1249 | case HTP_OP_MUL: compute_func = compute_complex_mul_f32; break; |
| 1250 | case HTP_OP_DIV: compute_func = compute_complex_div_f32; break; |
| 1251 | default: break; |
| 1252 | } |
| 1253 | } else if (src0_type == HTP_TYPE_F16) { |
| 1254 | switch (octx->op) { |
| 1255 | case HTP_OP_ADD: compute_func = compute_complex_add_f16; break; |
| 1256 | case HTP_OP_SUB: compute_func = compute_complex_sub_f16; break; |
| 1257 | case HTP_OP_MUL: compute_func = compute_complex_mul_f16; break; |
| 1258 | case HTP_OP_DIV: compute_func = compute_complex_div_f16; break; |
| 1259 | default: break; |
| 1260 | } |
| 1261 | } |
| 1262 | break; |
| 1263 | case HTP_BINARY_KERNEL_REPEAT: |
| 1264 | worker_func = binary_thread_element_repeat; |
| 1265 | if (src0_type == HTP_TYPE_F32) { |
| 1266 | switch (octx->op) { |
| 1267 | case HTP_OP_ADD: compute_func = compute_repeat_add_f32; break; |
| 1268 | case HTP_OP_SUB: compute_func = compute_repeat_sub_f32; break; |
| 1269 | case HTP_OP_MUL: compute_func = compute_repeat_mul_f32; break; |
| 1270 | case HTP_OP_DIV: compute_func = compute_repeat_div_f32; break; |
| 1271 | default: break; |
| 1272 | } |
| 1273 | } else if (src0_type == HTP_TYPE_F16) { |
| 1274 | switch (octx->op) { |
| 1275 | case HTP_OP_ADD: compute_func = compute_repeat_add_f16; break; |
| 1276 | case HTP_OP_SUB: compute_func = compute_repeat_sub_f16; break; |
| 1277 | case HTP_OP_MUL: compute_func = compute_repeat_mul_f16; break; |
| 1278 | case HTP_OP_DIV: compute_func = compute_repeat_div_f16; break; |
| 1279 | default: break; |
| 1280 | } |
| 1281 | } |
| 1282 | break; |
| 1283 | case HTP_BINARY_KERNEL_ADD_ID: |
| 1284 | if (octx->op == HTP_OP_ADD_ID && src0_type == HTP_TYPE_F32) { |
| 1285 | worker_func = binary_thread_add_id_f32; |
| 1286 | compute_func = (void *) compute_add_id_f32; |
| 1287 | } |
| 1288 | break; |
| 1289 | default: break; |
| 1290 | } |
| 1291 | |
| 1292 | if (!worker_func || !compute_func) { |
| 1293 | return HTP_STATUS_NO_SUPPORT; |
| 1294 | } |
| 1295 | |
| 1296 | dma_queue * dma_q = octx->ctx->dma[0]; |
| 1297 | uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(uint8_t, bctx.vtcm_base, bctx.vtcm_layout.off_src1); |
| 1298 | if (kparams->kernel_type == HTP_BINARY_KERNEL_ROW_BCAST) { |
| 1299 | dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data), bctx.vtcm_layout.src1_size, 0, src1->ne[0] * elem_size, 1); |
| 1300 | } else if (kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR || |
| 1301 | kparams->kernel_type == HTP_BINARY_KERNEL_REPEAT || |
| 1302 | kparams->kernel_type == HTP_BINARY_KERNEL_COMPLEX) { |
| 1303 | if (!kparams->src1_strided) { |
| 1304 | const size_t src1_bytes = (size_t) src1->ne[0] * src1->ne[1] * src1->ne[2] * src1->ne[3] * elem_size; |
| 1305 | dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data), bctx.vtcm_layout.src1_size, 0, src1_bytes, 1); |
| 1306 | if (kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR && octx->src[2] != NULL) { |
| 1307 | uint8_t * vtcm_src2 = VTCM_LAYOUT_PTR(uint8_t, bctx.vtcm_base, bctx.vtcm_layout.off_src2); |
| 1308 | dma_queue_push(dma_q, dma_make_data(vtcm_src2, octx->src[2]->data), bctx.vtcm_layout.src2_size, 0, src1_bytes, 1); |
| 1309 | } |
| 1310 | } else { |
| 1311 | const uint32_t ne11 = src1->ne[1]; |
| 1312 | const uint32_t ne12 = src1->ne[2]; |
| 1313 | const uint32_t ne13 = src1->ne[3]; |
| 1314 | const size_t row_bytes = src1->ne[0] * elem_size; |
| 1315 | for (uint32_t i13 = 0; i13 < ne13; i13++) { |
| 1316 | for (uint32_t i12 = 0; i12 < ne12; i12++) { |
| 1317 | dma_addr_t src_addr = src1->data + i13 * src1->nb[3] + i12 * src1->nb[2]; |
| 1318 | uint8_t * dst_ptr = vtcm_src1 + (i13 * ne12 * ne11 + i12 * ne11) * row_bytes; |
| 1319 | while (!dma_queue_push(dma_q, dma_make_data(dst_ptr, src_addr), |
| 1320 | row_bytes, src1->nb[1], row_bytes, ne11)) { |
| 1321 | dma_queue_pop(dma_q); |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | } else if (kparams->kernel_type == HTP_BINARY_KERNEL_ADD_ID) { |
| 1327 | const struct htp_tensor * src2 = octx->src[2]; |
| 1328 | uint8_t * vtcm_src2 = VTCM_LAYOUT_PTR(uint8_t, bctx.vtcm_base, bctx.vtcm_layout.off_src2); |
| 1329 | dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data), |
| 1330 | kparams->src1_row_size_aligned, src1->nb[1], |
| 1331 | src1->ne[0] * elem_size, src1->ne[1]); |
| 1332 | const size_t src2_row_bytes = src2->ne[0] * sizeof(int32_t); |
| 1333 | dma_queue_push(dma_q, dma_make_data(vtcm_src2, src2->data), |
| 1334 | src2_row_bytes, src2->nb[1], |
| 1335 | src2_row_bytes, src2->ne[1]); |
| 1336 | } |
| 1337 | |
| 1338 | bctx.octx = octx; |
| 1339 | bctx.nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div); |
| 1340 | bctx.total_rows = nrows; |
| 1341 | bctx.row_start = row_start; |
| 1342 | bctx.n_srcs = n_srcs; |
| 1343 | bctx.block_max = bctx.vtcm_layout.rows_per_buffer; |
| 1344 | bctx.src0_row_size_aligned = src0_row_size_aligned; |
| 1345 | bctx.src1_row_size_aligned = src1_row_size_aligned; |
| 1346 | bctx.dst_row_size_aligned = dst_row_size_aligned; |
| 1347 | bctx.row_size_bytes = src0_row_size; |
| 1348 | |
| 1349 | bctx.src0_dim1_div = kparams->div_ne01; |
| 1350 | bctx.src0_dim2_div = kparams->div_ne02; |
| 1351 | bctx.src0_dim12_div = kparams->div_ne012; |
| 1352 | |
| 1353 | bctx.src1_dim1_div = kparams->div_ne11; |
| 1354 | bctx.src1_dim2_div = kparams->div_ne12; |
| 1355 | bctx.src1_dim3_div = kparams->div_ne13; |
| 1356 | |
| 1357 | bctx.split_at_ne01 = kparams->split_at_ne01; |
| 1358 | bctx.split_at_ne02 = kparams->split_at_ne02; |
| 1359 | bctx.compute = compute_func; |
| 1360 | |
| 1361 | if (kparams->kernel_type == HTP_BINARY_KERNEL_ROW_BCAST || |
| 1362 | kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR || |
| 1363 | kparams->kernel_type == HTP_BINARY_KERNEL_REPEAT || |
| 1364 | kparams->kernel_type == HTP_BINARY_KERNEL_COMPLEX || |
| 1365 | kparams->kernel_type == HTP_BINARY_KERNEL_ADD_ID) { |
| 1366 | dma_queue_flush(dma_q); |
| 1367 | if (kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR && octx->src[2] != NULL && octx->op == HTP_OP_MUL) { |
| 1368 | uint8_t * vtcm_src2 = VTCM_LAYOUT_PTR(uint8_t, bctx.vtcm_base, bctx.vtcm_layout.off_src2); |
| 1369 | const uint32_t n_scalars = src1->ne[0] * src1->ne[1] * src1->ne[2] * src1->ne[3]; |
| 1370 | if (src0_type == HTP_TYPE_F32) { |
| 1371 | hvx_mul_f32_aaa(vtcm_src1, vtcm_src1, vtcm_src2, n_scalars); |
| 1372 | } else if (src0_type == HTP_TYPE_F16) { |
| 1373 | hvx_mul_f16_aaa(vtcm_src1, vtcm_src1, vtcm_src2, n_scalars); |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | work_queue_run(octx->ctx->work_queue, worker_func, &bctx, n_threads); |
| 1379 | |
| 1380 | return HTP_STATUS_OK; |
| 1381 | } |
| 1382 | |
| 1383 | int op_binary(struct htp_ops_context * octx) { |
| 1384 | const uint32_t src0_type = octx->src[0]->type; |
| 1385 | if ((src0_type == HTP_TYPE_F32) || (src0_type == HTP_TYPE_F16)) { |
| 1386 | return execute_op_binary(octx); |
| 1387 | } |
| 1388 | |
| 1389 | return HTP_STATUS_NO_SUPPORT; |
| 1390 | } |