Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-hexagon/htp/unary-ops.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-hexagon / htp / unary-ops.c` 1993 lines ยท 87345 bytes Raw Blame History 1 #pragma clang diagnostic ignored "-Wunused-variable" 2 #pragma clang diagnostic ignored "-Wunused-function" 3 #pragma clang diagnostic ignored "-Wunused-but-set-variable" 4 5 #include 6 #include 7 8 #include 9 #include 10 11 #include "dma-queue.h" 12 #include "hex-fastdiv.h" 13 #include "hvx-exp.h" 14 #include "hvx-sigmoid.h" 15 #include "hvx-utils.h" 16 #include "unary-ops.h" 17 18 #define GGML_COMMON_DECL_C 19 #include "ggml-common.h" 20 #include "htp-ctx.h" 21 #include "htp-ops.h" 22 #include "htp-tensor.h" 23 #include "htp-vtcm.h" 24 #include "hex-profile.h" 25 26 struct htp_unary_context; 27 28 typedef void (*unary_compute_fn_t)(const void * restrict src, 29 void * restrict dst, 30 uint32_t num_rows, 31 const struct htp_unary_context * uctx); 32 33 typedef void (*unary_rms_norm_mul_compute_fn_t)(const void * restrict src, 34 const void * restrict weight, 35 void * restrict dst, 36 uint32_t num_rows, 37 const struct htp_unary_context * uctx); 38 39 typedef void (*unary_tri_compute_fn_t)(const void * restrict src, 40 void * restrict dst, 41 uint32_t num_rows, 42 uint32_t ir, 43 const struct htp_unary_context * uctx); 44 45 typedef void (*unary_tile_compute_fn_t)(void * restrict dst, 46 const void * restrict src, 47 uint32_t tw, 48 const struct htp_unary_context * uctx); 49 50 typedef void (*unary_tiled_tri_compute_fn_t)(const void * restrict src, 51 void * restrict dst, 52 uint32_t tile_elems, 53 uint32_t col_start, 54 uint32_t i01, 55 uint32_t ne0, 56 int32_t ttype); 57 58 struct htp_unary_context { 59 struct htp_ops_context * octx; 60 const struct htp_unary_kernel_params * kparams; 61 62 void * compute; 63 64 dma_addr_t data_src0; 65 dma_addr_t data_src1; // weight/scale tensor for RMS_NORM_MUL 66 dma_addr_t data_dst; 67 68 size_t src0_data_row_size; // actual data bytes per row 69 size_t src1_data_row_size; 70 size_t dst_data_row_size; // actual data bytes per row 71 72 size_t src0_row_size_aligned; 73 size_t src1_row_size_aligned; 74 size_t dst_row_size_aligned; 75 76 size_t src0_vtcm_half_size; 77 size_t src1_vtcm_half_size; 78 size_t dst_vtcm_half_size; 79 80 uint32_t block; 81 uint32_t src0_nrows; 82 uint32_t src0_nrows_per_thread; 83 uint32_t row_start; 84 uint32_t nc; 85 uint32_t col_tile; // tiled mode 86 bool broadcast_weight; 87 88 uint8_t * vtcm_src0; 89 uint8_t * vtcm_src1; 90 uint8_t * vtcm_dst; 91 92 size_t vtcm_src0_size_per_thread; 93 size_t vtcm_src1_size_per_thread; 94 size_t vtcm_dst_size_per_thread; 95 }; 96 97 // Convert flat row index to DDR byte offset using the tensor's actual strides. 98 // ir = i1 + ne1*(i2 + ne2*i3) => offset = i1*nb1 + i2*nb2 + i3*nb3 99 static inline size_t unary_row_offset(uint32_t ir, 100 uint32_t ne1, uint32_t ne2, 101 const struct fastdiv_values * div_ne1, 102 const struct fastdiv_values * div_ne2, 103 const struct fastdiv_values * div_ne12, 104 size_t nb1, size_t nb2, size_t nb3) { 105 const uint32_t i1 = fastmodulo(ir, ne1, div_ne1); 106 const uint32_t ir_div_ne1 = fastdiv(ir, div_ne1); 107 const uint32_t i2 = fastmodulo(ir_div_ne1, ne2, div_ne2); 108 const uint32_t i3 = fastdiv(ir, div_ne12); 109 return i1 * nb1 + i2 * nb2 + i3 * nb3; 110 } 111 112 // Safe DMA block size from row `ir`: clamp to the dim-1 slice boundary 113 // when non-contiguous so the nb1 stride stays valid for all rows. 114 static inline uint32_t unary_block_size(uint32_t ir, 115 uint32_t end_row, 116 uint32_t block, 117 bool contig, 118 uint32_t ne1, 119 const struct fastdiv_values * div_ne1) { 120 uint32_t limit = MIN(block, end_row - ir); 121 122 if (!contig) { 123 const uint32_t slice_end = (fastdiv(ir, div_ne1) + 1) * ne1; 124 limit = MIN(limit, slice_end - ir); 125 } 126 127 return limit; 128 } 129 130 #define htp_unary_preamble \ 131 const uint32_t ne00 = src->ne[0]; \ 132 const uint32_t ne01 = src->ne[1]; \ 133 const uint32_t ne02 = src->ne[2]; \ 134 const uint32_t ne03 = src->ne[3]; \ 135 \ 136 const uint32_t ne0 = dst->ne[0]; \ 137 const uint32_t ne1 = dst->ne[1]; \ 138 const uint32_t ne2 = dst->ne[2]; \ 139 const uint32_t ne3 = dst->ne[3]; \ 140 \ 141 const uint32_t nb00 = src->nb[0]; \ 142 const uint32_t nb01 = src->nb[1]; \ 143 const uint32_t nb02 = src->nb[2]; \ 144 const uint32_t nb03 = src->nb[3]; \ 145 \ 146 const uint32_t nb0 = dst->nb[0]; \ 147 const uint32_t nb1 = dst->nb[1]; \ 148 const uint32_t nb2 = dst->nb[2]; \ 149 const uint32_t nb3 = dst->nb[3]; 150 151 #define htp_unary_op_preamble \ 152 int32_t * op_params = uctx->octx->op_params; \ 153 const uint32_t ne0 = uctx->nc; \ 154 const size_t src0_row_size_aligned = uctx->src0_row_size_aligned; \ 155 const size_t dst_row_size_aligned = uctx->dst_row_size_aligned; 156 157 static void scale_f32(const void * restrict src, 158 void * restrict dst, 159 const uint32_t num_rows, 160 const struct htp_unary_context * uctx) { 161 htp_unary_op_preamble; 162 float scale = 0.f; 163 float bias = 0.f; 164 memcpy(&scale, &op_params[0], sizeof(float)); 165 memcpy(&bias, &op_params[1], sizeof(float)); 166 167 for (uint32_t ir = 0; ir < num_rows; ir++) { 168 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 169 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 170 171 hvx_scale_offset_f32_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0, scale, bias); 172 } 173 } 174 175 static void clamp_f32(const void * restrict src, 176 void * restrict dst, 177 const uint32_t num_rows, 178 const struct htp_unary_context * uctx) { 179 htp_unary_op_preamble; 180 float min = 0.f; 181 float max = 0.f; 182 memcpy(&min, &op_params[0], sizeof(float)); 183 memcpy(&max, &op_params[1], sizeof(float)); 184 185 for (uint32_t ir = 0; ir < num_rows; ir++) { 186 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 187 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 188 189 hvx_clamp_scalar_f32(dst_local, src_local, min, max, ne0); 190 } 191 } 192 193 static void leaky_relu_f32(const void * restrict src, 194 void * restrict dst, 195 const uint32_t num_rows, 196 const struct htp_unary_context * uctx) { 197 htp_unary_op_preamble; 198 float negative_slope = 0.f; 199 memcpy(&negative_slope, &op_params[0], sizeof(float)); 200 201 for (uint32_t ir = 0; ir < num_rows; ir++) { 202 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 203 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 204 205 hvx_leaky_relu_scalar_f32(dst_local, src_local, negative_slope, ne0); 206 } 207 } 208 209 static void rms_norm_f32(const void * restrict src, 210 void * restrict dst, 211 const uint32_t num_rows, 212 const struct htp_unary_context * uctx) { 213 htp_unary_op_preamble; 214 float epsilon = 0.f; 215 float scale = 0.f; 216 memcpy(&epsilon, &op_params[0], sizeof(float)); 217 memcpy(&scale, &op_params[1], sizeof(float)); 218 219 for (uint32_t ir = 0; ir < num_rows; ir++) { 220 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 221 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 222 223 hvx_fast_rms_norm_f32((const uint8_t *) src_local, (uint8_t *) dst_local, ne0, epsilon, scale); 224 } 225 } 226 227 static void rms_norm_mul_f32(const void * restrict src, 228 const void * restrict weight, 229 void * restrict dst, 230 const uint32_t num_rows, 231 const struct htp_unary_context * uctx) { 232 htp_unary_op_preamble; 233 float epsilon = 0.f; 234 float scale = 0.f; 235 memcpy(&epsilon, &op_params[0], sizeof(float)); 236 memcpy(&scale, &op_params[1], sizeof(float)); 237 238 for (uint32_t ir = 0; ir < num_rows; ir++) { 239 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 240 const uint8_t * restrict w_local = (const uint8_t *)weight + (uctx->broadcast_weight ? 0 : ir * uctx->src1_row_size_aligned); 241 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 242 243 hvx_fast_rms_norm_mul_f32(src_local, w_local, dst_local, ne0, epsilon, scale); 244 } 245 } 246 247 static void norm_f32(const void * restrict src, 248 void * restrict dst, 249 const uint32_t num_rows, 250 const struct htp_unary_context * uctx) { 251 htp_unary_op_preamble; 252 float epsilon = 0.f; 253 memcpy(&epsilon, op_params, sizeof(float)); 254 255 for (uint32_t ir = 0; ir < num_rows; ir++) { 256 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 257 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 258 259 hvx_fast_norm_f32((const uint8_t *) src_local, (uint8_t *) dst_local, ne0, epsilon); 260 } 261 } 262 263 static void sqr_f32(const void * restrict src, 264 void * restrict dst, 265 const uint32_t num_rows, 266 const struct htp_unary_context * uctx) { 267 htp_unary_op_preamble; 268 269 for (uint32_t ir = 0; ir < num_rows; ir++) { 270 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 271 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 272 273 hvx_sqr_f32_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 274 } 275 } 276 277 static void sqrt_f32(const void * restrict src, 278 void * restrict dst, 279 const uint32_t num_rows, 280 const struct htp_unary_context * uctx) { 281 htp_unary_op_preamble; 282 283 for (uint32_t ir = 0; ir < num_rows; ir++) { 284 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 285 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 286 287 hvx_sqrt_f32_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 288 } 289 } 290 291 static void scale_f16(const void * restrict src, 292 void * restrict dst, 293 const uint32_t num_rows, 294 const struct htp_unary_context * uctx) { 295 htp_unary_op_preamble; 296 float scale = 0.f; 297 float bias = 0.f; 298 memcpy(&scale, &op_params[0], sizeof(float)); 299 memcpy(&bias, &op_params[1], sizeof(float)); 300 301 for (uint32_t ir = 0; ir < num_rows; ir++) { 302 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 303 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 304 305 hvx_scale_offset_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0, scale, bias); 306 } 307 } 308 309 static void clamp_f16(const void * restrict src, 310 void * restrict dst, 311 const uint32_t num_rows, 312 const struct htp_unary_context * uctx) { 313 htp_unary_op_preamble; 314 float min = 0.f; 315 float max = 0.f; 316 memcpy(&min, &op_params[0], sizeof(float)); 317 memcpy(&max, &op_params[1], sizeof(float)); 318 319 for (uint32_t ir = 0; ir < num_rows; ir++) { 320 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 321 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 322 323 hvx_clamp_scalar_f16(dst_local, src_local, (_Float16) min, (_Float16) max, ne0); 324 } 325 } 326 327 static void rms_norm_f16(const void * restrict src, 328 void * restrict dst, 329 const uint32_t num_rows, 330 const struct htp_unary_context * uctx) { 331 htp_unary_op_preamble; 332 float epsilon = 0.f; 333 memcpy(&epsilon, op_params, sizeof(float)); 334 335 for (uint32_t ir = 0; ir < num_rows; ir++) { 336 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 337 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 338 339 hvx_fast_rms_norm_f16((const uint8_t *) src_local, (uint8_t *) dst_local, ne0, epsilon); 340 } 341 } 342 343 static void norm_f16(const void * restrict src, 344 void * restrict dst, 345 const uint32_t num_rows, 346 const struct htp_unary_context * uctx) { 347 htp_unary_op_preamble; 348 float epsilon = 0.f; 349 memcpy(&epsilon, op_params, sizeof(float)); 350 351 for (uint32_t ir = 0; ir < num_rows; ir++) { 352 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 353 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 354 355 hvx_fast_norm_f16((const uint8_t *) src_local, (uint8_t *) dst_local, ne0, epsilon); 356 } 357 } 358 359 static void sqr_f16(const void * restrict src, 360 void * restrict dst, 361 const uint32_t num_rows, 362 const struct htp_unary_context * uctx) { 363 htp_unary_op_preamble; 364 365 for (uint32_t ir = 0; ir < num_rows; ir++) { 366 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 367 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 368 369 hvx_sqr_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 370 } 371 } 372 373 static void sqrt_f16(const void * restrict src, 374 void * restrict dst, 375 const uint32_t num_rows, 376 const struct htp_unary_context * uctx) { 377 htp_unary_op_preamble; 378 379 for (uint32_t ir = 0; ir < num_rows; ir++) { 380 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 381 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 382 383 hvx_sqrt_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 384 } 385 } 386 387 static void abs_f16(const void * restrict src, 388 void * restrict dst, 389 const uint32_t num_rows, 390 const struct htp_unary_context * uctx) { 391 htp_unary_op_preamble; 392 393 for (uint32_t ir = 0; ir < num_rows; ir++) { 394 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 395 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 396 397 hvx_abs_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 398 } 399 } 400 401 static void log_f16(const void * restrict src, 402 void * restrict dst, 403 const uint32_t num_rows, 404 const struct htp_unary_context * uctx) { 405 htp_unary_op_preamble; 406 407 for (uint32_t ir = 0; ir < num_rows; ir++) { 408 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 409 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 410 411 hvx_log_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 412 } 413 } 414 415 static void step_f16(const void * restrict src, 416 void * restrict dst, 417 const uint32_t num_rows, 418 const struct htp_unary_context * uctx) { 419 htp_unary_op_preamble; 420 421 for (uint32_t ir = 0; ir < num_rows; ir++) { 422 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 423 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 424 425 hvx_step_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 426 } 427 } 428 429 static void ceil_f16(const void * restrict src, 430 void * restrict dst, 431 const uint32_t num_rows, 432 const struct htp_unary_context * uctx) { 433 htp_unary_op_preamble; 434 435 for (uint32_t ir = 0; ir < num_rows; ir++) { 436 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 437 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 438 439 hvx_ceil_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 440 } 441 } 442 443 static void sgn_f16(const void * restrict src, 444 void * restrict dst, 445 const uint32_t num_rows, 446 const struct htp_unary_context * uctx) { 447 htp_unary_op_preamble; 448 449 for (uint32_t ir = 0; ir < num_rows; ir++) { 450 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 451 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 452 453 hvx_sgn_f16_aa((uint8_t *) dst_local, (const uint8_t *) src_local, ne0); 454 } 455 } 456 457 static void l2_norm_f16(const void * restrict src, 458 void * restrict dst, 459 const uint32_t num_rows, 460 const struct htp_unary_context * uctx) { 461 htp_unary_op_preamble; 462 float epsilon = 0.f; 463 memcpy(&epsilon, op_params, sizeof(float)); 464 465 for (uint32_t ir = 0; ir < num_rows; ir++) { 466 const uint8_t * restrict src_f = (const uint8_t *)src + (ir * src0_row_size_aligned); 467 uint8_t * restrict dst_f = (uint8_t *)dst + (ir * dst_row_size_aligned); 468 469 hvx_fast_l2_norm_f16((const uint8_t *)src_f, (uint8_t *)dst_f, ne0, epsilon); 470 } 471 } 472 473 static void neg_f32(const void * restrict src, 474 void * restrict dst, 475 const uint32_t num_rows, 476 const struct htp_unary_context * uctx) { 477 htp_unary_op_preamble; 478 479 for (uint32_t ir = 0; ir < num_rows; ir++) { 480 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 481 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 482 483 hvx_scale_f32_aa(dst_local, src_local, ne0, -1.0f); 484 } 485 } 486 487 static void exp_f32(const void * restrict src, 488 void * restrict dst, 489 const uint32_t num_rows, 490 const struct htp_unary_context * uctx) { 491 htp_unary_op_preamble; 492 493 for (uint32_t ir = 0; ir < num_rows; ir++) { 494 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 495 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 496 497 hvx_exp_f32(dst_local, src_local, ne0, false); 498 } 499 } 500 501 static void sigmoid_f32(const void * restrict src, 502 void * restrict dst, 503 const uint32_t num_rows, 504 const struct htp_unary_context * uctx) { 505 htp_unary_op_preamble; 506 507 for (uint32_t ir = 0; ir < num_rows; ir++) { 508 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 509 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 510 511 hvx_sigmoid_f32_aa(dst_local, src_local, ne0); 512 } 513 } 514 515 // silu(x) = x * sigmoid(x) 516 static void silu_f32(const void * restrict src, 517 void * restrict dst, 518 const uint32_t num_rows, 519 const struct htp_unary_context * uctx) { 520 htp_unary_op_preamble; 521 522 for (uint32_t ir = 0; ir < num_rows; ir++) { 523 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 524 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 525 526 hvx_sigmoid_f32_aa(dst_local, src_local, ne0); 527 hvx_mul_f32_aaa(dst_local, src_local, dst_local, ne0); 528 } 529 } 530 531 // GELU uses the tanh approximation. 532 static __attribute__((noinline)) HVX_Vector hvx_vec_gelu_f32(HVX_Vector x) { 533 const HVX_Vector half = hvx_vec_splat_f32(0.5f); 534 const HVX_Vector one = hvx_vec_splat_f32(1.0f); 535 536 HVX_Vector inner = hvx_vec_mul_f32_f32(x, x); 537 inner = hvx_vec_mul_f32_f32(inner, hvx_vec_splat_f32(0.`044715f`)); 538 inner = hvx_vec_add_f32_f32(inner, one); 539 inner = hvx_vec_mul_f32_f32(inner, x); 540 inner = hvx_vec_mul_f32_f32(inner, hvx_vec_splat_f32(0.`7978845608028654f`)); 541 542 return hvx_vec_mul_f32_f32(hvx_vec_mul_f32_f32(half, x), 543 hvx_vec_add_f32_f32(one, hvx_vec_tanh_f32(inner))); 544 } 545 546 static inline void hvx_gelu_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 547 assert((unsigned long) dst % 128 == 0); 548 assert((unsigned long) src % 128 == 0); 549 550 HVX_Vector * restrict vdst = (HVX_Vector *) dst; 551 HVX_Vector * restrict vsrc = (HVX_Vector *) src; 552 553 const uint32_t nvec = n / VLEN_FP32; 554 const uint32_t nloe = n % VLEN_FP32; 555 556 uint32_t i = 0; 557 _Pragma("unroll(4)") 558 for (; i < nvec; i++) { 559 vdst[i] = hvx_vec_gelu_f32(vsrc[i]); 560 } 561 if (nloe) { 562 hvx_vec_store_a(&vdst[i], nloe * sizeof(float), hvx_vec_gelu_f32(vsrc[i])); 563 } 564 } 565 566 // GELU_QUICK uses x * sigmoid(1.702 * x). 567 static __attribute__((noinline)) HVX_Vector hvx_vec_gelu_quick_f32(HVX_Vector x) { 568 const HVX_Vector one = hvx_vec_splat_f32(1.0f); 569 const HVX_Vector max_exp = hvx_vec_splat_f32(87.0f); 570 const HVX_Vector min_exp = hvx_vec_splat_f32(-87.0f); 571 const HVX_Vector scaled = hvx_vec_mul_f32_f32(x, hvx_vec_splat_f32(1.702f)); 572 const HVX_Vector sigmoid = hvx_vec_fast_sigmoid_f32_guard(scaled, one, max_exp, min_exp); 573 574 return hvx_vec_mul_f32_f32(x, sigmoid); 575 } 576 577 static inline void hvx_gelu_quick_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 578 assert((unsigned long) dst % 128 == 0); 579 assert((unsigned long) src % 128 == 0); 580 581 HVX_Vector * restrict vdst = (HVX_Vector *) dst; 582 HVX_Vector * restrict vsrc = (HVX_Vector *) src; 583 584 const uint32_t nvec = n / VLEN_FP32; 585 const uint32_t nloe = n % VLEN_FP32; 586 587 uint32_t i = 0; 588 _Pragma("unroll(4)") 589 for (; i < nvec; i++) { 590 vdst[i] = hvx_vec_gelu_quick_f32(vsrc[i]); 591 } 592 if (nloe) { 593 hvx_vec_store_a(&vdst[i], nloe * sizeof(float), hvx_vec_gelu_quick_f32(vsrc[i])); 594 } 595 } 596 597 static void gelu_f32(const void * restrict src, 598 void * restrict dst, 599 const uint32_t num_rows, 600 const struct htp_unary_context * uctx) { 601 htp_unary_op_preamble; 602 603 for (uint32_t ir = 0; ir < num_rows; ir++) { 604 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 605 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 606 607 hvx_gelu_f32_aa(dst_local, src_local, ne0); 608 } 609 } 610 611 static void gelu_quick_f32(const void * restrict src, 612 void * restrict dst, 613 const uint32_t num_rows, 614 const struct htp_unary_context * uctx) { 615 htp_unary_op_preamble; 616 617 for (uint32_t ir = 0; ir < num_rows; ir++) { 618 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 619 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 620 621 hvx_gelu_quick_f32_aa(dst_local, src_local, ne0); 622 } 623 } 624 625 static void gelu_erf_f32(const void * restrict src, 626 void * restrict dst, 627 const uint32_t num_rows, 628 const struct htp_unary_context * uctx) { 629 htp_unary_op_preamble; 630 631 for (uint32_t ir = 0; ir < num_rows; ir++) { 632 const uint8_t * restrict src_local = (const uint8_t *) src + (ir * src0_row_size_aligned); 633 uint8_t * restrict dst_local = (uint8_t *) dst + (ir * dst_row_size_aligned); 634 635 hvx_gelu_erf_f32_aa(dst_local, src_local, ne0); 636 } 637 } 638 639 static void tri_f32(const void * restrict src, 640 void * restrict dst, 641 const uint32_t num_rows, 642 const uint32_t ir, 643 const struct htp_unary_context * uctx) { 644 htp_unary_op_preamble; 645 const int32_t ttype = op_params[0]; 646 const HVX_Vector zero = hvx_vec_splat_f32(0.0f); 647 const uint32_t nvec = ne0 / VLEN_FP32; 648 const uint32_t nloe = ne0 % VLEN_FP32; 649 650 const uint32_t ne01 = uctx->octx->src[0]->ne[1]; 651 const struct fastdiv_values * div_ne01 = &uctx->kparams->div_ne01; 652 653 for (uint32_t b = 0; b < num_rows; b++) { 654 const uint32_t abs_row = ir + b; 655 const uint32_t i01 = fastmodulo(abs_row, ne01, div_ne01); 656 657 const HVX_Vector * restrict v_src = (const HVX_Vector *) ((const uint8_t *) src + b * src0_row_size_aligned); 658 HVX_Vector * restrict v_dst = (HVX_Vector *) ((uint8_t *) dst + b * dst_row_size_aligned); 659 660 uint32_t boundary; 661 int keep_left; 662 switch (ttype) { 663 case 0: boundary = i01; keep_left = 0; break; // keep col >= row 664 case 1: boundary = i01 + 1; keep_left = 0; break; // keep col > row 665 case 2: boundary = i01 + 1; keep_left = 1; break; // keep col <= row 666 case 3: boundary = i01; keep_left = 1; break; // keep col < row 667 default: boundary = 0; keep_left = 0; break; 668 } 669 if (boundary > ne0) boundary = ne0; 670 671 // Full HVX vectors - each starts at a 128-byte aligned offset 672 for (uint32_t i = 0; i < nvec; i++) { 673 const uint32_t vec_start = i * VLEN_FP32; 674 const uint32_t vec_end = vec_start + VLEN_FP32; 675 if (keep_left) { 676 if (vec_end <= boundary) { 677 v_dst[i] = v_src[i]; 678 } else if (vec_start >= boundary) { 679 v_dst[i] = zero; 680 } else { 681 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - vec_start) * sizeof(float)); 682 v_dst[i] = Q6_V_vmux_QVV(mask, v_src[i], zero); 683 } 684 } else { 685 if (vec_end <= boundary) { 686 v_dst[i] = zero; 687 } else if (vec_start >= boundary) { 688 v_dst[i] = v_src[i]; 689 } else { 690 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - vec_start) * sizeof(float)); 691 v_dst[i] = Q6_V_vmux_QVV(mask, zero, v_src[i]); 692 } 693 } 694 } 695 696 // Tail elements (row_elems not a multiple of VLEN_FP32) 697 if (nloe > 0) { 698 const uint32_t abs_start = nvec * VLEN_FP32; 699 const uint32_t abs_end = abs_start + nloe; 700 HVX_Vector tail_val; 701 if (keep_left) { 702 if (abs_end <= boundary) { 703 tail_val = v_src[nvec]; 704 } else if (abs_start >= boundary) { 705 tail_val = zero; 706 } else { 707 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - abs_start) * sizeof(float)); 708 tail_val = Q6_V_vmux_QVV(mask, v_src[nvec], zero); 709 } 710 } else { 711 if (abs_end <= boundary) { 712 tail_val = zero; 713 } else if (abs_start >= boundary) { 714 tail_val = v_src[nvec]; 715 } else { 716 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - abs_start) * sizeof(float)); 717 tail_val = Q6_V_vmux_QVV(mask, zero, v_src[nvec]); 718 } 719 } 720 hvx_vec_store_a(&v_dst[nvec], nloe * sizeof(float), tail_val); 721 } 722 } 723 } 724 725 // softplus(x) = log(1 + exp(x)) 726 // Match CPU reference: return x for x > 20 to avoid overflow. 727 static inline void hvx_softplus_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) { 728 const HVX_Vector * restrict v_src = (const HVX_Vector *) src; 729 HVX_Vector * restrict v_dst = (HVX_Vector *) dst; 730 731 const HVX_Vector twenty = hvx_vec_splat_f32(20.0f); 732 const HVX_Vector one = hvx_vec_splat_f32(1.0f); 733 734 const uint32_t nvec = n / VLEN_FP32; 735 const uint32_t nloe = n % VLEN_FP32; 736 737 #pragma unroll(4) 738 for (uint32_t i = 0; i < nvec; i++) { 739 HVX_Vector x = v_src[i]; 740 HVX_VectorPred pred_gt20 = Q6_Q_vcmp_gt_VsfVsf(x, twenty); 741 HVX_Vector expx = hvx_vec_exp_f32(x); 742 HVX_Vector logv = hvx_vec_log_f32(hvx_vec_add_f32_f32(one, expx)); 743 v_dst[i] = Q6_V_vmux_QVV(pred_gt20, x, logv); 744 } 745 746 if (nloe > 0) { 747 HVX_Vector x = v_src[nvec]; 748 HVX_VectorPred pred_gt20 = Q6_Q_vcmp_gt_VsfVsf(x, twenty); 749 HVX_Vector expx = hvx_vec_exp_f32(x); 750 HVX_Vector logv = hvx_vec_log_f32(hvx_vec_add_f32_f32(one, expx)); 751 HVX_Vector sp = Q6_V_vmux_QVV(pred_gt20, x, logv); 752 hvx_vec_store_a((void *) &v_dst[nvec], nloe * sizeof(float), sp); 753 } 754 } 755 756 static void softplus_f32(const void * restrict src, 757 void * restrict dst, 758 const uint32_t num_rows, 759 const struct htp_unary_context * uctx) { 760 htp_unary_op_preamble; 761 762 for (uint32_t ir = 0; ir < num_rows; ir++) { 763 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 764 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 765 766 hvx_softplus_f32_aa(dst_local, src_local, ne0); 767 } 768 } 769 770 static void l2_norm_f32(const void * restrict src, 771 void * restrict dst, 772 const uint32_t num_rows, 773 const struct htp_unary_context * uctx) { 774 htp_unary_op_preamble; 775 float epsilon = 0.f; 776 memcpy(&epsilon, op_params, sizeof(float)); 777 778 for (uint32_t ir = 0; ir < num_rows; ir++) { 779 const uint8_t * restrict src_f = (const uint8_t *)src + (ir * src0_row_size_aligned); 780 uint8_t * restrict dst_f = (uint8_t *)dst + (ir * dst_row_size_aligned); 781 782 hvx_fast_l2_norm_f32((const uint8_t *)src_f, (uint8_t *)dst_f, ne0, epsilon); 783 } 784 } 785 786 static inline HVX_VectorPair hvx_fwht_stage_pair(HVX_Vector v0, HVX_Vector v1, int32_t b) { 787 HVX_VectorPair p = Q6_W_vshuff_VVR(v1, v0, b); 788 HVX_Vector s = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_lo_W(p), Q6_V_hi_W(p)); 789 HVX_Vector d = Q6_Vqf32_vsub_Vqf32Vqf32(Q6_V_lo_W(p), Q6_V_hi_W(p)); 790 return Q6_W_vshuff_VVR(d, s, b); 791 } 792 793 static inline HVX_VectorPair hvx_fwht32_pair(HVX_Vector v0_sf, HVX_Vector v1_sf, HVX_Vector v_scale) { 794 HVX_Vector v0 = Q6_Vqf32_vmpy_VsfVsf(v0_sf, v_scale); 795 HVX_Vector v1 = Q6_Vqf32_vmpy_VsfVsf(v1_sf, v_scale); 796 HVX_VectorPair p; 797 p = hvx_fwht_stage_pair(v0, v1, 4); 798 p = hvx_fwht_stage_pair(Q6_V_lo_W(p), Q6_V_hi_W(p), 8); 799 p = hvx_fwht_stage_pair(Q6_V_lo_W(p), Q6_V_hi_W(p), 16); 800 p = hvx_fwht_stage_pair(Q6_V_lo_W(p), Q6_V_hi_W(p), 32); 801 p = hvx_fwht_stage_pair(Q6_V_lo_W(p), Q6_V_hi_W(p), 64); 802 return p; 803 } 804 805 static inline void hvx_fwht_store32(HVX_Vector * restrict v_dst, HVX_VectorPair p) { 806 v_dst[0] = Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p)); 807 } 808 809 static inline void hvx_fwht_store64(HVX_Vector * restrict v_dst, HVX_VectorPair p) { 810 HVX_Vector u = Q6_V_lo_W(p); 811 HVX_Vector v = Q6_V_hi_W(p); 812 v_dst[0] = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(u, v)); 813 v_dst[1] = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(u, v)); 814 } 815 816 static inline void hvx_fwht_tail_f32(HVX_Vector * restrict v_dst, uint32_t nvec) { 817 uint32_t len_vec = 2; 818 for (; len_vec * 2 < nvec; len_vec <<= 2) { 819 const uint32_t step = len_vec * 4; 820 for (uint32_t i = 0; i < nvec; i += step) { 821 for (uint32_t j = 0; j < len_vec; j++) { 822 HVX_Vector a0 = v_dst[i + j]; 823 HVX_Vector a1 = v_dst[i + j + len_vec]; 824 HVX_Vector a2 = v_dst[i + j + 2 * len_vec]; 825 HVX_Vector a3 = v_dst[i + j + 3 * len_vec]; 826 827 HVX_Vector t0 = Q6_Vqf32_vadd_VsfVsf(a0, a1); 828 HVX_Vector t1 = Q6_Vqf32_vsub_VsfVsf(a0, a1); 829 HVX_Vector t2 = Q6_Vqf32_vadd_VsfVsf(a2, a3); 830 HVX_Vector t3 = Q6_Vqf32_vsub_VsfVsf(a2, a3); 831 832 v_dst[i + j] = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(t0, t2)); 833 v_dst[i + j + len_vec] = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(t1, t3)); 834 v_dst[i + j + 2 * len_vec] = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(t0, t2)); 835 v_dst[i + j + 3 * len_vec] = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(t1, t3)); 836 } 837 } 838 } 839 840 if (len_vec < nvec) { 841 for (uint32_t i = 0; i < nvec; i += 2 * len_vec) { 842 for (uint32_t j = 0; j < len_vec; j++) { 843 HVX_Vector u = v_dst[i + j]; 844 HVX_Vector v = v_dst[i + len_vec + j]; 845 v_dst[i + j] = hvx_vec_add_f32_f32(u, v); 846 v_dst[i + len_vec + j] = hvx_vec_sub_f32_f32(u, v); 847 } 848 } 849 } 850 } 851 852 static inline void hvx_fwht_row_f32(const uint8_t * restrict src, uint8_t * restrict dst, uint32_t n, HVX_Vector v_scale) { 853 const HVX_Vector * restrict v_src = (const HVX_Vector *) src; 854 HVX_Vector * restrict v_dst = (HVX_Vector *) dst; 855 const uint32_t nvec = n / VLEN_FP32; 856 857 if (nvec == 1) { 858 HVX_VectorPair p = hvx_fwht32_pair(v_src[0], v_src[0], v_scale); 859 hvx_fwht_store32(v_dst, p); 860 return; 861 } 862 863 for (uint32_t i = 0; i < nvec; i += 2) { 864 HVX_VectorPair p = hvx_fwht32_pair(v_src[i], v_src[i + 1], v_scale); 865 hvx_fwht_store64(&v_dst[i], p); 866 } 867 868 hvx_fwht_tail_f32(v_dst, nvec); 869 } 870 871 static inline void hvx_fwht_row_f16(const uint8_t * restrict src, uint8_t * restrict dst, uint32_t n, HVX_Vector v_scale) { 872 const HVX_Vector * restrict v_src = (const HVX_Vector *) src; 873 HVX_Vector * restrict v_dst = (HVX_Vector *) dst; 874 const uint32_t nvec = n / VLEN_FP32; 875 876 if (nvec == 1) { 877 HVX_Vector v0 = Q6_V_lo_W(hvx_vec_f16_to_f32(v_src[0])); 878 HVX_VectorPair p = hvx_fwht32_pair(v0, v0, v_scale); 879 hvx_fwht_store32(v_dst, p); 880 return; 881 } 882 883 const uint32_t nvec_f16 = n / VLEN_FP16; 884 for (uint32_t i = 0; i < nvec_f16; i++) { 885 HVX_VectorPair f32p = hvx_vec_f16_to_f32(v_src[i]); 886 HVX_VectorPair p = hvx_fwht32_pair(Q6_V_lo_W(f32p), Q6_V_hi_W(f32p), v_scale); 887 hvx_fwht_store64(&v_dst[2 * i], p); 888 } 889 890 hvx_fwht_tail_f32(v_dst, nvec); 891 } 892 893 static void hadamard_f32(const void * restrict src, 894 void * restrict dst, 895 const uint32_t num_rows, 896 const struct htp_unary_context * uctx) { 897 htp_unary_op_preamble; 898 const HVX_Vector v_scale = hvx_vec_splat_f32(1.0f / sqrtf((float) ne0)); 899 900 for (uint32_t ir = 0; ir < num_rows; ir++) { 901 const uint8_t * restrict src_row = (const uint8_t *) src + (ir * src0_row_size_aligned); 902 uint8_t * restrict dst_row = (uint8_t *) dst + (ir * dst_row_size_aligned); 903 904 hvx_fwht_row_f32(src_row, dst_row, ne0, v_scale); 905 } 906 } 907 908 static void hadamard_f16(const void * restrict src, 909 void * restrict dst, 910 const uint32_t num_rows, 911 const struct htp_unary_context * uctx) { 912 htp_unary_op_preamble; 913 const HVX_Vector v_scale = hvx_vec_splat_f32(1.0f / sqrtf((float) ne0)); 914 915 for (uint32_t ir = 0; ir < num_rows; ir++) { 916 const uint8_t * restrict src_row = (const uint8_t *) src + (ir * src0_row_size_aligned); 917 uint8_t * restrict dst_row = (uint8_t *) dst + (ir * dst_row_size_aligned); 918 919 hvx_fwht_row_f16(src_row, dst_row, ne0, v_scale); 920 } 921 } 922 923 static void tanh_f32(const void * restrict src, 924 void * restrict dst, 925 const uint32_t num_rows, 926 const struct htp_unary_context * uctx) { 927 htp_unary_op_preamble; 928 929 for (uint32_t ir = 0; ir < num_rows; ir++) { 930 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 931 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 932 933 hvx_tanh_f32_aa(dst_local, src_local, ne0); 934 } 935 } 936 937 static void abs_f32(const void * restrict src, 938 void * restrict dst, 939 const uint32_t num_rows, 940 const struct htp_unary_context * uctx) { 941 htp_unary_op_preamble; 942 943 for (uint32_t ir = 0; ir < num_rows; ir++) { 944 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 945 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 946 947 hvx_abs_f32_aa(dst_local, src_local, ne0); 948 } 949 } 950 951 static void relu_f32(const void * restrict src, 952 void * restrict dst, 953 const uint32_t num_rows, 954 const struct htp_unary_context * uctx) { 955 htp_unary_op_preamble; 956 957 for (uint32_t ir = 0; ir < num_rows; ir++) { 958 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 959 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 960 961 hvx_max_scalar_f32(dst_local, src_local, 0.0f, ne0); 962 } 963 } 964 965 static void step_f32(const void * restrict src, 966 void * restrict dst, 967 const uint32_t num_rows, 968 const struct htp_unary_context * uctx) { 969 htp_unary_op_preamble; 970 971 for (uint32_t ir = 0; ir < num_rows; ir++) { 972 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 973 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 974 975 hvx_step_f32_aa(dst_local, src_local, ne0); 976 } 977 } 978 979 static void ceil_f32(const void * restrict src, 980 void * restrict dst, 981 const uint32_t num_rows, 982 const struct htp_unary_context * uctx) { 983 htp_unary_op_preamble; 984 985 for (uint32_t ir = 0; ir < num_rows; ir++) { 986 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 987 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 988 989 hvx_ceil_f32_aa(dst_local, src_local, ne0); 990 } 991 } 992 993 static void sgn_f32(const void * restrict src, 994 void * restrict dst, 995 const uint32_t num_rows, 996 const struct htp_unary_context * uctx) { 997 htp_unary_op_preamble; 998 999 for (uint32_t ir = 0; ir < num_rows; ir++) { 1000 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 1001 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 1002 1003 hvx_sgn_f32_aa(dst_local, src_local, ne0); 1004 } 1005 } 1006 1007 static void log_f32(const void * restrict src, 1008 void * restrict dst, 1009 const uint32_t num_rows, 1010 const struct htp_unary_context * uctx) { 1011 htp_unary_op_preamble; 1012 1013 for (uint32_t ir = 0; ir < num_rows; ir++) { 1014 const uint8_t * restrict src_local = (const uint8_t *)src + (ir * src0_row_size_aligned); 1015 uint8_t * restrict dst_local = (uint8_t *)dst + (ir * dst_row_size_aligned); 1016 1017 hvx_log_f32_aa(dst_local, src_local, ne0); 1018 } 1019 } 1020 1021 #// Pointwise unary ops on one column tile in VTCM. 1022 static void tile_scale_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1023 float scale = 0.f; 1024 float bias = 0.f; 1025 memcpy(&scale, &uctx->octx->op_params[0], sizeof(float)); 1026 memcpy(&bias, &uctx->octx->op_params[1], sizeof(float)); 1027 hvx_scale_offset_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw, scale, bias); 1028 } 1029 1030 static void tile_clamp_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1031 float min = 0.f; 1032 float max = 0.f; 1033 memcpy(&min, &uctx->octx->op_params[0], sizeof(float)); 1034 memcpy(&max, &uctx->octx->op_params[1], sizeof(float)); 1035 hvx_clamp_scalar_f32((uint8_t *) dst, (const uint8_t *) src, min, max, tw); 1036 } 1037 1038 static void tile_leaky_relu_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1039 float negative_slope = 0.f; 1040 memcpy(&negative_slope, &uctx->octx->op_params[0], sizeof(float)); 1041 hvx_leaky_relu_scalar_f32((uint8_t *) dst, (const uint8_t *) src, negative_slope, tw); 1042 } 1043 1044 static void tile_sqr_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1045 (void) uctx; 1046 hvx_sqr_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1047 } 1048 1049 static void tile_sqrt_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1050 (void) uctx; 1051 hvx_sqrt_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1052 } 1053 1054 static void tile_neg_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1055 (void) uctx; 1056 hvx_scale_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw, -1.0f); 1057 } 1058 1059 static void tile_exp_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1060 (void) uctx; 1061 hvx_exp_f32((uint8_t *) dst, (const uint8_t *) src, tw, false); 1062 } 1063 1064 static void tile_sigmoid_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1065 (void) uctx; 1066 hvx_sigmoid_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1067 } 1068 1069 static void tile_silu_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1070 (void) uctx; 1071 hvx_sigmoid_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1072 hvx_mul_f32_aaa((uint8_t *) dst, (const uint8_t *) src, (uint8_t *) dst, tw); 1073 } 1074 1075 static void tile_gelu_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1076 (void) uctx; 1077 hvx_gelu_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1078 } 1079 1080 static void tile_gelu_quick_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1081 (void) uctx; 1082 hvx_gelu_quick_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1083 } 1084 1085 static void tile_gelu_erf_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1086 (void) uctx; 1087 hvx_gelu_erf_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1088 } 1089 1090 static void tile_softplus_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1091 (void) uctx; 1092 hvx_softplus_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1093 } 1094 1095 static void tile_tanh_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1096 (void) uctx; 1097 hvx_tanh_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1098 } 1099 1100 static void tile_abs_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1101 (void) uctx; 1102 hvx_abs_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1103 } 1104 1105 static void tile_log_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1106 (void) uctx; 1107 hvx_log_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1108 } 1109 1110 static void tile_relu_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1111 (void) uctx; 1112 hvx_max_scalar_f32((uint8_t *) dst, (const uint8_t *) src, 0.0f, tw); 1113 } 1114 1115 static void tile_step_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1116 (void) uctx; 1117 hvx_step_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1118 } 1119 1120 static void tile_ceil_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1121 (void) uctx; 1122 hvx_ceil_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1123 } 1124 1125 static void tile_sgn_f32(void * restrict dst, const void * restrict src, uint32_t tw, const struct htp_unary_context * uctx) { 1126 (void) uctx; 1127 hvx_sgn_f32_aa((uint8_t *) dst, (const uint8_t *) src, tw); 1128 } 1129 1130 static void tri_apply_tile_f32(const void * restrict src, void * restrict dst, 1131 uint32_t tile_elems, uint32_t col_start, uint32_t i01, 1132 uint32_t ne0, int32_t ttype) { 1133 const HVX_Vector * restrict v_src = (const HVX_Vector *) src; 1134 HVX_Vector * restrict v_dst = (HVX_Vector *) dst; 1135 const HVX_Vector zero = hvx_vec_splat_f32(0.0f); 1136 1137 uint32_t boundary; 1138 int keep_left; 1139 switch (ttype) { 1140 case 0: boundary = i01; keep_left = 0; break; 1141 case 1: boundary = i01 + 1; keep_left = 0; break; 1142 case 2: boundary = i01 + 1; keep_left = 1; break; 1143 case 3: boundary = i01; keep_left = 1; break; 1144 default: boundary = 0; keep_left = 0; break; 1145 } 1146 if (boundary > ne0) boundary = ne0; 1147 1148 const uint32_t nvec = tile_elems / VLEN_FP32; 1149 const uint32_t nloe = tile_elems % VLEN_FP32; 1150 1151 for (uint32_t i = 0; i < nvec; i++) { 1152 const uint32_t abs_start = col_start + i * VLEN_FP32; 1153 const uint32_t abs_end = abs_start + VLEN_FP32; 1154 if (keep_left) { 1155 if (abs_end <= boundary) { 1156 v_dst[i] = v_src[i]; 1157 } else if (abs_start >= boundary) { 1158 v_dst[i] = zero; 1159 } else { 1160 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - abs_start) * sizeof(float)); 1161 v_dst[i] = Q6_V_vmux_QVV(mask, v_src[i], zero); 1162 } 1163 } else { 1164 if (abs_end <= boundary) { 1165 v_dst[i] = zero; 1166 } else if (abs_start >= boundary) { 1167 v_dst[i] = v_src[i]; 1168 } else { 1169 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - abs_start) * sizeof(float)); 1170 v_dst[i] = Q6_V_vmux_QVV(mask, zero, v_src[i]); 1171 } 1172 } 1173 } 1174 1175 if (nloe > 0) { 1176 const uint32_t abs_start = col_start + nvec * VLEN_FP32; 1177 const uint32_t abs_end = abs_start + nloe; 1178 HVX_Vector tail_val; 1179 if (keep_left) { 1180 if (abs_end <= boundary) { 1181 tail_val = v_src[nvec]; 1182 } else if (abs_start >= boundary) { 1183 tail_val = zero; 1184 } else { 1185 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - abs_start) * sizeof(float)); 1186 tail_val = Q6_V_vmux_QVV(mask, v_src[nvec], zero); 1187 } 1188 } else { 1189 if (abs_end <= boundary) { 1190 tail_val = zero; 1191 } else if (abs_start >= boundary) { 1192 tail_val = v_src[nvec]; 1193 } else { 1194 HVX_VectorPred mask = Q6_Q_vsetq_R((boundary - abs_start) * sizeof(float)); 1195 tail_val = Q6_V_vmux_QVV(mask, zero, v_src[nvec]); 1196 } 1197 } 1198 hvx_vec_store_a(&v_dst[nvec], nloe * sizeof(float), tail_val); 1199 } 1200 } 1201 1202 // 1. Standard row-block unary task (F32 and F16). 1203 static void unary_thread_row_block(unsigned int nth, unsigned int ith, void * data) { 1204 (void) nth; 1205 const struct htp_unary_context * uctx = (const struct htp_unary_context *) data; 1206 struct htp_ops_context * octx = uctx->octx; 1207 const struct htp_tensor * src = octx->src[0]; 1208 const struct htp_tensor * dst = octx->dst; 1209 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 1210 1211 htp_unary_preamble; 1212 1213 const uint32_t src0_nrows_per_thread = uctx->src0_nrows_per_thread; 1214 const size_t src0_data_row_size = uctx->src0_data_row_size; 1215 const size_t dst_data_row_size = uctx->dst_data_row_size; 1216 const size_t src0_row_size_aligned = uctx->src0_row_size_aligned; 1217 const size_t dst_row_size_aligned = uctx->dst_row_size_aligned; 1218 1219 const uint32_t src0_nrows = uctx->src0_nrows; 1220 const uint32_t src0_start_row = uctx->row_start + src0_nrows_per_thread * ith; 1221 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, uctx->row_start + src0_nrows); 1222 1223 if (src0_start_row >= src0_end_row) { 1224 return; 1225 } 1226 1227 const dma_addr_t data_src = uctx->data_src0; 1228 const dma_addr_t data_dst = uctx->data_dst; 1229 1230 uint8_t * src0_vtcm_data = uctx->vtcm_src0 + (ith * uctx->vtcm_src0_size_per_thread); 1231 uint8_t * dst_vtcm_data = uctx->vtcm_dst + (ith * uctx->vtcm_dst_size_per_thread); 1232 1233 const size_t src0_vtcm_half_size = uctx->src0_vtcm_half_size; 1234 const size_t dst_vtcm_half_size = uctx->dst_vtcm_half_size; 1235 1236 const bool src0_contig = uctx->kparams->src0_contig; 1237 const uint32_t BLOCK = uctx->block; 1238 1239 const struct fastdiv_values * div_ne01 = &uctx->kparams->div_ne01; 1240 const struct fastdiv_values * div_ne02 = &uctx->kparams->div_ne02; 1241 const struct fastdiv_values * div_ne012 = &uctx->kparams->div_ne012; 1242 1243 dma_queue * dma_q = octx->ctx->dma[ith]; 1244 1245 for (uint32_t ir = src0_start_row, vtcm_idx = 0; ir < src0_end_row && vtcm_idx < 2; vtcm_idx++) { 1246 const uint32_t block_size = unary_block_size(ir, src0_end_row, BLOCK, src0_contig, ne01, div_ne01); 1247 1248 dma_queue_push(dma_q, 1249 dma_make_data(data_dst, dst_vtcm_data + (vtcm_idx * dst_vtcm_half_size)), 1250 nb1, dst_row_size_aligned, dst_data_row_size, 0); 1251 1252 const size_t src0_off = src0_contig ? ((size_t) ir * nb01) : 1253 unary_row_offset(ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb01, nb02, nb03); 1254 dma_queue_push(dma_q, 1255 dma_make_data(src0_vtcm_data + (vtcm_idx * src0_vtcm_half_size), data_src + src0_off), 1256 src0_row_size_aligned, nb01, src0_data_row_size, block_size); 1257 1258 ir += block_size; 1259 } 1260 1261 unary_compute_fn_t compute = (unary_compute_fn_t) uctx->compute; 1262 1263 for (uint32_t ir = src0_start_row; ir < src0_end_row; ) { 1264 const uint32_t block_size = unary_block_size(ir, src0_end_row, BLOCK, src0_contig, ne01, div_ne01); 1265 1266 void * dst_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).src; 1267 void * src0_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 1268 1269 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, ir); 1270 compute(src0_vtcm, dst_vtcm, block_size, uctx); 1271 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, ir); 1272 1273 const size_t dst_off = (size_t) ir * nb1; 1274 dma_queue_push(dma_q, 1275 dma_make_data(data_dst + dst_off, dst_vtcm), 1276 nb1, dst_row_size_aligned, dst_data_row_size, block_size); 1277 1278 const uint32_t next_ir = ir + block_size; 1279 if (next_ir < src0_end_row) { 1280 const uint32_t next_block_size = unary_block_size(next_ir, src0_end_row, BLOCK, src0_contig, 1281 ne01, div_ne01); 1282 const uint32_t pref_ir = next_ir + next_block_size; 1283 if (pref_ir < src0_end_row) { 1284 const uint32_t pref_block_size = unary_block_size(pref_ir, src0_end_row, BLOCK, src0_contig, 1285 ne01, div_ne01); 1286 const size_t src0_pref_off = src0_contig ? ((size_t) pref_ir * nb01) : 1287 unary_row_offset(pref_ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb01, nb02, nb03); 1288 dma_queue_push(dma_q, 1289 dma_make_data(src0_vtcm, data_src + src0_pref_off), 1290 src0_row_size_aligned, nb01, src0_data_row_size, pref_block_size); 1291 } 1292 } 1293 ir += block_size; 1294 } 1295 1296 dma_queue_flush(dma_q); 1297 } 1298 1299 // 2. RMS_NORM_MUL row-block task with weight buffer. 1300 static void unary_thread_rms_norm_mul_f32(unsigned int nth, unsigned int ith, void * data) { 1301 (void) nth; 1302 const struct htp_unary_context * uctx = (const struct htp_unary_context *) data; 1303 struct htp_ops_context * octx = uctx->octx; 1304 const struct htp_tensor * src = octx->src[0]; 1305 const struct htp_tensor * dst = octx->dst; 1306 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 1307 1308 htp_unary_preamble; 1309 1310 const uint32_t src0_nrows_per_thread = uctx->src0_nrows_per_thread; 1311 const size_t src0_data_row_size = uctx->src0_data_row_size; 1312 const size_t dst_data_row_size = uctx->dst_data_row_size; 1313 const size_t src0_row_size_aligned = uctx->src0_row_size_aligned; 1314 const size_t dst_row_size_aligned = uctx->dst_row_size_aligned; 1315 1316 const uint32_t src0_nrows = uctx->src0_nrows; 1317 const uint32_t src0_start_row = uctx->row_start + src0_nrows_per_thread * ith; 1318 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, uctx->row_start + src0_nrows); 1319 1320 if (src0_start_row >= src0_end_row) { 1321 return; 1322 } 1323 1324 const dma_addr_t data_src = uctx->data_src0; 1325 const dma_addr_t data_src1 = uctx->data_src1; 1326 const dma_addr_t data_dst = uctx->data_dst; 1327 1328 const struct htp_tensor * src1 = octx->src[1]; 1329 const uint32_t nb11 = src1->nb[1]; 1330 const uint32_t nb12 = src1->nb[2]; 1331 const uint32_t nb13 = src1->nb[3]; 1332 1333 uint8_t * src0_vtcm_data = uctx->vtcm_src0 + (ith * uctx->vtcm_src0_size_per_thread); 1334 uint8_t * src1_vtcm_data = uctx->vtcm_src1 ? (uctx->vtcm_src1 + (ith * uctx->vtcm_src1_size_per_thread)) : NULL; 1335 uint8_t * dst_vtcm_data = uctx->vtcm_dst + (ith * uctx->vtcm_dst_size_per_thread); 1336 1337 const size_t src0_vtcm_half_size = uctx->src0_vtcm_half_size; 1338 const size_t src1_vtcm_half_size = uctx->src1_vtcm_half_size; 1339 const size_t dst_vtcm_half_size = uctx->dst_vtcm_half_size; 1340 1341 const bool src0_contig = uctx->kparams->src0_contig; 1342 const bool src1_contig = uctx->kparams->src1_contig; 1343 const bool block_contig = src0_contig && src1_contig; 1344 const uint32_t BLOCK = uctx->block; 1345 1346 const struct fastdiv_values * div_ne01 = &uctx->kparams->div_ne01; 1347 const struct fastdiv_values * div_ne02 = &uctx->kparams->div_ne02; 1348 const struct fastdiv_values * div_ne012 = &uctx->kparams->div_ne012; 1349 1350 dma_queue * dma_q = octx->ctx->dma[ith]; 1351 1352 if (uctx->broadcast_weight) { 1353 dma_queue_push(dma_q, dma_make_data(src1_vtcm_data, data_src1), 1354 uctx->src1_row_size_aligned, 0, uctx->src1_data_row_size, 1); 1355 dma_queue_flush(dma_q); 1356 } 1357 1358 for (uint32_t ir = src0_start_row, vtcm_idx = 0; ir < src0_end_row && vtcm_idx < 2; vtcm_idx++) { 1359 const uint32_t block_size = unary_block_size(ir, src0_end_row, BLOCK, block_contig, ne01, div_ne01); 1360 1361 dma_queue_push(dma_q, 1362 dma_make_data(data_dst, dst_vtcm_data + (vtcm_idx * dst_vtcm_half_size)), 1363 nb1, dst_row_size_aligned, dst_data_row_size, 0); 1364 1365 const size_t src0_off = src0_contig ? ((size_t) ir * nb01) : 1366 unary_row_offset(ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb01, nb02, nb03); 1367 dma_queue_push(dma_q, 1368 dma_make_data(src0_vtcm_data + (vtcm_idx * src0_vtcm_half_size), data_src + src0_off), 1369 src0_row_size_aligned, nb01, src0_data_row_size, block_size); 1370 1371 if (!uctx->broadcast_weight) { 1372 const size_t src1_off = src1_contig ? ((size_t) ir * nb11) : 1373 unary_row_offset(ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb11, nb12, nb13); 1374 dma_queue_push(dma_q, 1375 dma_make_data(src1_vtcm_data + (vtcm_idx * src1_vtcm_half_size), data_src1 + src1_off), 1376 uctx->src1_row_size_aligned, nb11, uctx->src1_data_row_size, block_size); 1377 } 1378 1379 ir += block_size; 1380 } 1381 1382 unary_rms_norm_mul_compute_fn_t compute = (unary_rms_norm_mul_compute_fn_t) uctx->compute; 1383 1384 for (uint32_t ir = src0_start_row; ir < src0_end_row; ) { 1385 const uint32_t block_size = unary_block_size(ir, src0_end_row, BLOCK, block_contig, ne01, div_ne01); 1386 1387 void * dst_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).src; 1388 void * src0_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 1389 void * src1_vtcm = NULL; 1390 if (!uctx->broadcast_weight) { 1391 src1_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 1392 } 1393 1394 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, ir); 1395 const void * w = uctx->broadcast_weight ? (const void *) src1_vtcm_data : src1_vtcm; 1396 compute(src0_vtcm, w, dst_vtcm, block_size, uctx); 1397 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, ir); 1398 1399 const size_t dst_off = (size_t) ir * nb1; 1400 dma_queue_push(dma_q, 1401 dma_make_data(data_dst + dst_off, dst_vtcm), 1402 nb1, dst_row_size_aligned, dst_data_row_size, block_size); 1403 1404 const uint32_t next_ir = ir + block_size; 1405 if (next_ir < src0_end_row) { 1406 const uint32_t next_block_size = unary_block_size(next_ir, src0_end_row, BLOCK, block_contig, 1407 ne01, div_ne01); 1408 const uint32_t pref_ir = next_ir + next_block_size; 1409 if (pref_ir < src0_end_row) { 1410 const uint32_t pref_block_size = unary_block_size(pref_ir, src0_end_row, BLOCK, block_contig, 1411 ne01, div_ne01); 1412 const size_t src0_pref_off = src0_contig ? ((size_t) pref_ir * nb01) : 1413 unary_row_offset(pref_ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb01, nb02, nb03); 1414 dma_queue_push(dma_q, 1415 dma_make_data(src0_vtcm, data_src + src0_pref_off), 1416 src0_row_size_aligned, nb01, src0_data_row_size, pref_block_size); 1417 1418 if (!uctx->broadcast_weight) { 1419 const size_t src1_pref_off = src1_contig ? ((size_t) pref_ir * nb11) : 1420 unary_row_offset(pref_ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb11, nb12, nb13); 1421 dma_queue_push(dma_q, 1422 dma_make_data(src1_vtcm, data_src1 + src1_pref_off), 1423 uctx->src1_row_size_aligned, nb11, uctx->src1_data_row_size, pref_block_size); 1424 } 1425 } 1426 } 1427 ir += block_size; 1428 } 1429 1430 dma_queue_flush(dma_q); 1431 } 1432 1433 // 3. TRI row-block task with row index ir. 1434 static void unary_thread_tri_f32(unsigned int nth, unsigned int ith, void * data) { 1435 (void) nth; 1436 const struct htp_unary_context * uctx = (const struct htp_unary_context *) data; 1437 struct htp_ops_context * octx = uctx->octx; 1438 const struct htp_tensor * src = octx->src[0]; 1439 const struct htp_tensor * dst = octx->dst; 1440 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 1441 1442 htp_unary_preamble; 1443 1444 const uint32_t src0_nrows_per_thread = uctx->src0_nrows_per_thread; 1445 const size_t src0_data_row_size = uctx->src0_data_row_size; 1446 const size_t dst_data_row_size = uctx->dst_data_row_size; 1447 const size_t src0_row_size_aligned = uctx->src0_row_size_aligned; 1448 const size_t dst_row_size_aligned = uctx->dst_row_size_aligned; 1449 1450 const uint32_t src0_nrows = uctx->src0_nrows; 1451 const uint32_t src0_start_row = uctx->row_start + src0_nrows_per_thread * ith; 1452 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, uctx->row_start + src0_nrows); 1453 1454 if (src0_start_row >= src0_end_row) { 1455 return; 1456 } 1457 1458 const dma_addr_t data_src = uctx->data_src0; 1459 const dma_addr_t data_dst = uctx->data_dst; 1460 1461 uint8_t * src0_vtcm_data = uctx->vtcm_src0 + (ith * uctx->vtcm_src0_size_per_thread); 1462 uint8_t * dst_vtcm_data = uctx->vtcm_dst + (ith * uctx->vtcm_dst_size_per_thread); 1463 1464 const size_t src0_vtcm_half_size = uctx->src0_vtcm_half_size; 1465 const size_t dst_vtcm_half_size = uctx->dst_vtcm_half_size; 1466 const uint32_t BLOCK = uctx->block; 1467 1468 dma_queue * dma_q = octx->ctx->dma[ith]; 1469 1470 for (uint32_t ir = src0_start_row, vtcm_idx = 0; ir < src0_end_row && vtcm_idx < 2; vtcm_idx++) { 1471 const uint32_t block_size = MIN(BLOCK, src0_end_row - ir); 1472 1473 dma_queue_push(dma_q, 1474 dma_make_data(data_dst, dst_vtcm_data + (vtcm_idx * dst_vtcm_half_size)), 1475 nb1, dst_row_size_aligned, dst_data_row_size, 0); 1476 1477 const size_t src0_off = (size_t) ir * nb01; 1478 dma_queue_push(dma_q, 1479 dma_make_data(src0_vtcm_data + (vtcm_idx * src0_vtcm_half_size), data_src + src0_off), 1480 src0_row_size_aligned, nb01, src0_data_row_size, block_size); 1481 1482 ir += block_size; 1483 } 1484 1485 unary_tri_compute_fn_t compute = (unary_tri_compute_fn_t) uctx->compute; 1486 1487 for (uint32_t ir = src0_start_row; ir < src0_end_row; ) { 1488 const uint32_t block_size = MIN(BLOCK, src0_end_row - ir); 1489 1490 void * dst_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).src; 1491 void * src0_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 1492 1493 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, ir); 1494 compute(src0_vtcm, dst_vtcm, block_size, ir, uctx); 1495 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, ir); 1496 1497 const size_t dst_off = (size_t) ir * nb1; 1498 dma_queue_push(dma_q, 1499 dma_make_data(data_dst + dst_off, dst_vtcm), 1500 nb1, dst_row_size_aligned, dst_data_row_size, block_size); 1501 1502 const uint32_t next_ir = ir + block_size; 1503 if (next_ir < src0_end_row) { 1504 const uint32_t next_block_size = MIN(BLOCK, src0_end_row - next_ir); 1505 const uint32_t pref_ir = next_ir + next_block_size; 1506 if (pref_ir < src0_end_row) { 1507 const uint32_t pref_block_size = MIN(BLOCK, src0_end_row - pref_ir); 1508 const size_t src0_pref_off = (size_t) pref_ir * nb01; 1509 dma_queue_push(dma_q, 1510 dma_make_data(src0_vtcm, data_src + src0_pref_off), 1511 src0_row_size_aligned, nb01, src0_data_row_size, pref_block_size); 1512 } 1513 } 1514 ir += block_size; 1515 } 1516 1517 dma_queue_flush(dma_q); 1518 } 1519 1520 // 4. Pointwise tiled unary task. 1521 static void unary_thread_tiled(unsigned int nth, unsigned int ith, void * data) { 1522 (void) nth; 1523 const struct htp_unary_context * uctx = (const struct htp_unary_context *) data; 1524 struct htp_ops_context * octx = uctx->octx; 1525 const struct htp_tensor * src = octx->src[0]; 1526 const struct htp_tensor * dst = octx->dst; 1527 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 1528 1529 htp_unary_preamble; 1530 1531 const uint32_t src0_nrows_per_thread = uctx->src0_nrows_per_thread; 1532 const uint32_t col_tile = uctx->col_tile; 1533 1534 const uint32_t src0_nrows = uctx->src0_nrows; 1535 const uint32_t src0_start_row = uctx->row_start + src0_nrows_per_thread * ith; 1536 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, uctx->row_start + src0_nrows); 1537 1538 if (src0_start_row >= src0_end_row) { 1539 return; 1540 } 1541 1542 const dma_addr_t data_src = uctx->data_src0; 1543 const dma_addr_t data_dst = uctx->data_dst; 1544 1545 uint8_t * src0_vtcm_data = uctx->vtcm_src0 + (ith * uctx->vtcm_src0_size_per_thread); 1546 uint8_t * dst_vtcm_data = uctx->vtcm_dst + (ith * uctx->vtcm_dst_size_per_thread); 1547 1548 const size_t src0_half = uctx->src0_vtcm_half_size; 1549 const size_t dst_half = uctx->dst_vtcm_half_size; 1550 1551 dma_queue * dma_q = octx->ctx->dma[ith]; 1552 1553 const struct fastdiv_values * div_ne01 = &uctx->kparams->div_ne01; 1554 const struct fastdiv_values * div_ne02 = &uctx->kparams->div_ne02; 1555 const struct fastdiv_values * div_ne012 = &uctx->kparams->div_ne012; 1556 const struct fastdiv_values * div_tpr = &uctx->kparams->div_tpr; 1557 1558 const uint32_t tiles_per_row = uctx->kparams->tiles_per_row; 1559 const bool src0_contig = uctx->kparams->src0_contig; 1560 1561 const uint32_t total_tiles = (src0_end_row - src0_start_row) * tiles_per_row; 1562 1563 for (uint32_t t = 0, vtcm_idx = 0; t < total_tiles && vtcm_idx < 2; t++, vtcm_idx++) { 1564 const uint32_t row = src0_start_row + fastdiv(t, div_tpr); 1565 const uint32_t col = fastmodulo(t, tiles_per_row, div_tpr) * col_tile; 1566 const uint32_t tw = MIN(col_tile, ne0 - col); 1567 const size_t tb = (size_t) tw * sizeof(float); 1568 const size_t soff = (src0_contig ? ((size_t) row * nb01) : 1569 unary_row_offset(row, ne01, ne02, div_ne01, div_ne02, div_ne012, nb01, nb02, nb03)) + 1570 (size_t) col * sizeof(float); 1571 1572 dma_queue_push(dma_q, dma_make_data(data_dst, dst_vtcm_data + (vtcm_idx * dst_half)), 0, 0, 0, 0); 1573 dma_queue_push(dma_q, dma_make_data(src0_vtcm_data + (vtcm_idx * src0_half), data_src + soff), tb, tb, tb, 1); 1574 } 1575 1576 unary_tile_compute_fn_t compute = (unary_tile_compute_fn_t) uctx->compute; 1577 1578 uint32_t row = src0_start_row; 1579 uint32_t col = 0; 1580 uint32_t tile_in_row = 0; 1581 1582 uint32_t prow = src0_start_row + fastdiv(2, div_tpr); 1583 uint32_t pcol = fastmodulo(2, tiles_per_row, div_tpr) * col_tile; 1584 uint32_t ptile_in_row = fastmodulo(2, tiles_per_row, div_tpr); 1585 1586 for (uint32_t t = 0; t < total_tiles; t++) { 1587 void * dst_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).src; 1588 void * src_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 1589 1590 const uint32_t tw = MIN(col_tile, ne0 - col); 1591 1592 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, t); 1593 compute(dst_vtcm, src_vtcm, tw, uctx); 1594 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, t); 1595 1596 const size_t doff = (size_t) row * nb1 + (size_t) col * sizeof(float); 1597 const size_t tb = (size_t) tw * sizeof(float); 1598 dma_queue_push(dma_q, dma_make_data(data_dst + doff, dst_vtcm), tb, tb, tb, 1); 1599 1600 const uint32_t pt = t + 2; 1601 if (pt < total_tiles) { 1602 const uint32_t ptw = MIN(col_tile, ne0 - pcol); 1603 const size_t ptb = (size_t) ptw * sizeof(float); 1604 const size_t psoff = (src0_contig ? ((size_t) prow * nb01) : 1605 unary_row_offset(prow, ne01, ne02, div_ne01, div_ne02, div_ne012, nb01, nb02, 1606 nb03)) + 1607 (size_t) pcol * sizeof(float); 1608 dma_queue_push(dma_q, dma_make_data(src_vtcm, data_src + psoff), ptb, ptb, ptb, 1); 1609 } 1610 1611 tile_in_row++; 1612 col += col_tile; 1613 if (tile_in_row == tiles_per_row) { 1614 tile_in_row = 0; 1615 col = 0; 1616 row++; 1617 } 1618 1619 ptile_in_row++; 1620 pcol += col_tile; 1621 if (ptile_in_row == tiles_per_row) { 1622 ptile_in_row = 0; 1623 pcol = 0; 1624 prow++; 1625 } 1626 } 1627 1628 dma_queue_flush(dma_q); 1629 } 1630 1631 // 5. TRI tiled task. 1632 static void unary_thread_tiled_tri_f32(unsigned int nth, unsigned int ith, void * data) { 1633 (void) nth; 1634 const struct htp_unary_context * uctx = (const struct htp_unary_context *) data; 1635 struct htp_ops_context * octx = uctx->octx; 1636 const struct htp_tensor * src = octx->src[0]; 1637 const struct htp_tensor * dst = octx->dst; 1638 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 1639 1640 htp_unary_preamble; 1641 1642 const uint32_t src0_nrows_per_thread = uctx->src0_nrows_per_thread; 1643 const int32_t * op_params = octx->op_params; 1644 const uint32_t col_tile = uctx->col_tile; 1645 1646 const uint32_t src0_nrows = uctx->src0_nrows; 1647 const uint32_t src0_start_row = uctx->row_start + src0_nrows_per_thread * ith; 1648 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, uctx->row_start + src0_nrows); 1649 1650 if (src0_start_row >= src0_end_row) { 1651 return; 1652 } 1653 1654 const dma_addr_t data_src = uctx->data_src0; 1655 const dma_addr_t data_dst = uctx->data_dst; 1656 1657 uint8_t * src0_vtcm_data = uctx->vtcm_src0 + (ith * uctx->vtcm_src0_size_per_thread); 1658 uint8_t * dst_vtcm_data = uctx->vtcm_dst + (ith * uctx->vtcm_dst_size_per_thread); 1659 1660 const size_t src0_half = uctx->src0_vtcm_half_size; 1661 const size_t dst_half = uctx->dst_vtcm_half_size; 1662 1663 dma_queue * dma_q = octx->ctx->dma[ith]; 1664 1665 const struct fastdiv_values * div_ne01 = &uctx->kparams->div_ne01; 1666 const struct fastdiv_values * div_tpr = &uctx->kparams->div_tpr; 1667 1668 const uint32_t tiles_per_row = uctx->kparams->tiles_per_row; 1669 const int32_t tri_ttype = op_params[0]; 1670 1671 const uint32_t total_tiles = (src0_end_row - src0_start_row) * tiles_per_row; 1672 1673 for (uint32_t t = 0, vtcm_idx = 0; t < total_tiles && vtcm_idx < 2; t++, vtcm_idx++) { 1674 const uint32_t row = src0_start_row + fastdiv(t, div_tpr); 1675 const uint32_t col = fastmodulo(t, tiles_per_row, div_tpr) * col_tile; 1676 const uint32_t tw = MIN(col_tile, ne0 - col); 1677 const size_t tb = (size_t) tw * sizeof(float); 1678 const size_t soff = (size_t) row * nb01 + (size_t) col * sizeof(float); 1679 1680 dma_queue_push(dma_q, dma_make_data(data_dst, dst_vtcm_data + (vtcm_idx * dst_half)), 0, 0, 0, 0); 1681 dma_queue_push(dma_q, dma_make_data(src0_vtcm_data + (vtcm_idx * src0_half), data_src + soff), tb, tb, tb, 1); 1682 } 1683 1684 unary_tiled_tri_compute_fn_t compute = (unary_tiled_tri_compute_fn_t) uctx->compute; 1685 1686 uint32_t row = src0_start_row; 1687 uint32_t col = 0; 1688 uint32_t tile_in_row = 0; 1689 uint32_t i01 = fastmodulo(row, ne01, div_ne01); 1690 1691 uint32_t prow = src0_start_row + fastdiv(2, div_tpr); 1692 uint32_t pcol = fastmodulo(2, tiles_per_row, div_tpr) * col_tile; 1693 uint32_t ptile_in_row = fastmodulo(2, tiles_per_row, div_tpr); 1694 1695 for (uint32_t t = 0; t < total_tiles; t++) { 1696 void * dst_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).src; 1697 void * src_vtcm = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 1698 1699 const uint32_t tw = MIN(col_tile, ne0 - col); 1700 1701 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, t); 1702 compute(src_vtcm, dst_vtcm, tw, col, i01, ne0, tri_ttype); 1703 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, t); 1704 1705 const size_t doff = (size_t) row * nb1 + (size_t) col * sizeof(float); 1706 const size_t tb = (size_t) tw * sizeof(float); 1707 dma_queue_push(dma_q, dma_make_data(data_dst + doff, dst_vtcm), tb, tb, tb, 1); 1708 1709 const uint32_t pt = t + 2; 1710 if (pt < total_tiles) { 1711 const uint32_t ptw = MIN(col_tile, ne0 - pcol); 1712 const size_t ptb = (size_t) ptw * sizeof(float); 1713 const size_t psoff = (size_t) prow * nb01 + (size_t) pcol * sizeof(float); 1714 dma_queue_push(dma_q, dma_make_data(src_vtcm, data_src + psoff), ptb, ptb, ptb, 1); 1715 } 1716 1717 tile_in_row++; 1718 col += col_tile; 1719 if (tile_in_row == tiles_per_row) { 1720 tile_in_row = 0; 1721 col = 0; 1722 row++; 1723 i01++; 1724 if (i01 == ne01) { 1725 i01 = 0; 1726 } 1727 } 1728 1729 ptile_in_row++; 1730 pcol += col_tile; 1731 if (ptile_in_row == tiles_per_row) { 1732 ptile_in_row = 0; 1733 pcol = 0; 1734 prow++; 1735 } 1736 } 1737 1738 dma_queue_flush(dma_q); 1739 } 1740 1741 static int execute_op_unary(struct htp_ops_context * octx) { 1742 int err = HTP_STATUS_OK; 1743 1744 const struct htp_tensor * src0 = octx->src[0]; 1745 const struct htp_tensor * dst = octx->dst; 1746 1747 const bool is_f16 = (src0->type == HTP_TYPE_F16); 1748 1749 const char * op_type = NULL; 1750 1751 switch (octx->op) { 1752 case HTP_OP_NORM: op_type = is_f16 ? "norm-f16" : "norm-f32"; break; 1753 case HTP_OP_RMS_NORM: op_type = is_f16 ? "rmsnorm-f16" : "rmsnorm-f32"; break; 1754 case HTP_OP_RMS_NORM_MUL: op_type = "rmsnorm-mul-f32"; break; 1755 case HTP_OP_SCALE: op_type = is_f16 ? "scale-f16" : "scale-f32"; break; 1756 case HTP_OP_CLAMP: op_type = is_f16 ? "clamp-f16" : "clamp-f32"; break; 1757 case HTP_OP_LEAKY_RELU: op_type = "leaky-relu-f32"; break; 1758 case HTP_OP_SQR: op_type = is_f16 ? "sqr-f16" : "sqr-f32"; break; 1759 case HTP_OP_SQRT: op_type = is_f16 ? "sqrt-f16" : "sqrt-f32"; break; 1760 case HTP_OP_UNARY_NEG: op_type = "neg-f32"; break; 1761 case HTP_OP_UNARY_EXP: op_type = "exp-f32"; break; 1762 case HTP_OP_UNARY_SIGMOID: op_type = "sigmoid-f32"; break; 1763 case HTP_OP_UNARY_SILU: op_type = "silu-f32"; break; 1764 case HTP_OP_UNARY_GELU: op_type = "gelu-f32"; break; 1765 case HTP_OP_UNARY_GELU_QUICK: op_type = "gelu-quick-f32"; break; 1766 case HTP_OP_UNARY_GELU_ERF: op_type = "gelu-erf-f32"; break; 1767 case HTP_OP_UNARY_SOFTPLUS: op_type = "softplus-f32"; break; 1768 case HTP_OP_UNARY_TANH: op_type = "tanh-f32"; break; 1769 case HTP_OP_UNARY_ABS: op_type = is_f16 ? "abs-f16" : "abs-f32"; break; 1770 case HTP_OP_UNARY_LOG: op_type = is_f16 ? "log-f16" : "log-f32"; break; 1771 case HTP_OP_UNARY_RELU: op_type = "relu-f32"; break; 1772 case HTP_OP_UNARY_STEP: op_type = is_f16 ? "step-f16" : "step-f32"; break; 1773 case HTP_OP_UNARY_CEIL: op_type = is_f16 ? "ceil-f16" : "ceil-f32"; break; 1774 case HTP_OP_UNARY_SGN: op_type = is_f16 ? "sgn-f16" : "sgn-f32"; break; 1775 case HTP_OP_L2_NORM: op_type = is_f16 ? "l2norm-f16" : "l2norm-f32"; break; 1776 case HTP_OP_TRI: op_type = "tri-f32"; break; 1777 case HTP_OP_HADAMARD: op_type = is_f16 ? "hadamard-f16" : "hadamard-f32"; break; 1778 default: 1779 FARF(ERROR, "Unsupported unary Op %u\n", octx->op); 1780 return HTP_STATUS_NO_SUPPORT; 1781 } 1782 1783 const struct htp_unary_kernel_params * kparams = (const struct htp_unary_kernel_params *) octx->kernel_params; 1784 1785 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 1786 return HTP_STATUS_INVAL_PARAMS; 1787 } 1788 1789 const size_t src0_elem_size = is_f16 ? sizeof(_Float16) : sizeof(float); 1790 const size_t dst_elem_size = (dst->type == HTP_TYPE_F16) ? sizeof(_Float16) : sizeof(float); 1791 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 1792 const size_t src0_data_row_size = src0->ne[0] * src0_elem_size; 1793 const size_t dst_data_row_size = dst->ne[0] * dst_elem_size; 1794 1795 uint32_t row_start = 0; 1796 uint32_t nrows = src0_nrows; 1797 1798 if (octx->ctx->mdev.count > 1) { 1799 uint32_t rows_per_chunk = 0; 1800 htp_tensor_mdev_rows_per_chunk(dst, (uint32_t) dst_elem_size, (uint32_t) dst_data_row_size, &rows_per_chunk); 1801 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 1802 row_start = range.start; 1803 nrows = range.count; 1804 } 1805 1806 if (nrows == 0) { 1807 return HTP_STATUS_OK; 1808 } 1809 1810 const uint32_t n_threads = octx->n_threads; 1811 1812 // Always 0 for F16 - htp_unary_solve_layout() keeps F16 on the row-block path, 1813 // since only F32 has unary_task_f32_tiled_* kernels. 1814 const uint32_t col_tile = kparams->col_tile; 1815 1816 size_t src1_data_row_size = 0; 1817 bool broadcast_weight = kparams->broadcast_weight; 1818 const struct htp_tensor * src1 = NULL; 1819 1820 // RMS_NORM_MUL fusion is F32-only (its weight tensor is always F32; see 1821 // try_fuse_node()'s type guard), so this never triggers when is_f16 is true. 1822 if (octx->op == HTP_OP_RMS_NORM_MUL) { 1823 src1 = octx->src[1]; 1824 src1_data_row_size = src1->ne[0] * sizeof(float); 1825 } 1826 1827 if ((!col_tile && kparams->block == 0) || octx->ctx->vtcm_size < (size_t) kparams->vtcm_size) { 1828 FARF(ERROR, "unary-%s : current VTCM reservation %zu is too small, needed %zu\n", op_type, octx->ctx->vtcm_size, (size_t) kparams->vtcm_size); 1829 return HTP_STATUS_VTCM_TOO_SMALL; 1830 } 1831 1832 octx->src0_spad.src = NULL; 1833 octx->src1_spad.src = NULL; 1834 octx->dst_spad.src = NULL; 1835 1836 struct htp_unary_vtcm_layout L; 1837 htp_unary_vtcm_layout_build(&L, octx->op, src0_data_row_size, dst_data_row_size, src1_data_row_size, 1838 src0_elem_size, col_tile, kparams->block, broadcast_weight, kparams->n_threads); 1839 1840 FARF(HIGH, "%s: (%ux%ux%ux%u) -> (%ux%ux%ux%u) : src0-vtcm-size %zu src1-vtcm-size %zu dst-vtcm-size %zu\n", op_type, 1841 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 1842 L.src0_bytes * n_threads, L.src1_bytes * n_threads, L.dst_bytes * n_threads); 1843 1844 uint8_t * const base = (uint8_t *) octx->ctx->vtcm_base; 1845 struct htp_unary_context uctx = { 1846 .octx = octx, 1847 .kparams = kparams, 1848 .src0_nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div), 1849 .src0_nrows = nrows, 1850 .row_start = row_start, 1851 1852 .data_src0 = src0->data, 1853 .data_src1 = (octx->op == HTP_OP_RMS_NORM_MUL) ? src1->data : 0, 1854 .data_dst = dst->data, 1855 1856 .src0_data_row_size = src0_data_row_size, 1857 .src1_data_row_size = src1_data_row_size, 1858 .dst_data_row_size = dst_data_row_size, 1859 1860 .src0_row_size_aligned = L.src0_row_size_aligned, 1861 .src1_row_size_aligned = L.src1_row_size_aligned, 1862 .dst_row_size_aligned = L.dst_row_size_aligned, 1863 1864 .src0_vtcm_half_size = L.src0_bytes >> 1, 1865 .src1_vtcm_half_size = (octx->op == HTP_OP_RMS_NORM_MUL) ? (L.src1_bytes >> (broadcast_weight ? 0 : 1)) : 0, 1866 .dst_vtcm_half_size = L.dst_bytes >> 1, 1867 1868 .block = kparams->block, 1869 .nc = src0->ne[0], 1870 .col_tile = col_tile, 1871 .broadcast_weight = broadcast_weight, 1872 1873 .vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src0), 1874 .vtcm_src1 = VTCM_LAYOUT_PTR_OPTIONAL(uint8_t, base, L.off_src1, L.src1_bytes > 0), 1875 .vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, base, L.off_dst), 1876 1877 .vtcm_src0_size_per_thread = L.src0_bytes, 1878 .vtcm_src1_size_per_thread = L.src1_bytes, 1879 .vtcm_dst_size_per_thread = L.dst_bytes, 1880 }; 1881 1882 FARF(HIGH, "%s: %s mode (col_tile %u)\n", op_type, col_tile ? "tiled" : "row-block", col_tile); 1883 1884 worker_callback_t task_func = NULL; 1885 void * compute_func = NULL; 1886 1887 if (col_tile) { 1888 task_func = unary_thread_tiled; 1889 switch (octx->op) { 1890 case HTP_OP_SCALE: compute_func = (void *) tile_scale_f32; break; 1891 case HTP_OP_CLAMP: compute_func = (void *) tile_clamp_f32; break; 1892 case HTP_OP_LEAKY_RELU: compute_func = (void *) tile_leaky_relu_f32; break; 1893 case HTP_OP_SQR: compute_func = (void *) tile_sqr_f32; break; 1894 case HTP_OP_SQRT: compute_func = (void *) tile_sqrt_f32; break; 1895 case HTP_OP_UNARY_NEG: compute_func = (void *) tile_neg_f32; break; 1896 case HTP_OP_UNARY_EXP: compute_func = (void *) tile_exp_f32; break; 1897 case HTP_OP_UNARY_SIGMOID: compute_func = (void *) tile_sigmoid_f32; break; 1898 case HTP_OP_UNARY_SILU: compute_func = (void *) tile_silu_f32; break; 1899 case HTP_OP_UNARY_GELU: compute_func = (void *) tile_gelu_f32; break; 1900 case HTP_OP_UNARY_GELU_QUICK: compute_func = (void *) tile_gelu_quick_f32; break; 1901 case HTP_OP_UNARY_GELU_ERF: compute_func = (void *) tile_gelu_erf_f32; break; 1902 case HTP_OP_UNARY_SOFTPLUS: compute_func = (void *) tile_softplus_f32; break; 1903 case HTP_OP_UNARY_TANH: compute_func = (void *) tile_tanh_f32; break; 1904 case HTP_OP_UNARY_ABS: compute_func = (void *) tile_abs_f32; break; 1905 case HTP_OP_UNARY_LOG: compute_func = (void *) tile_log_f32; break; 1906 case HTP_OP_UNARY_RELU: compute_func = (void *) tile_relu_f32; break; 1907 case HTP_OP_UNARY_STEP: compute_func = (void *) tile_step_f32; break; 1908 case HTP_OP_UNARY_CEIL: compute_func = (void *) tile_ceil_f32; break; 1909 case HTP_OP_UNARY_SGN: compute_func = (void *) tile_sgn_f32; break; 1910 case HTP_OP_TRI: 1911 task_func = unary_thread_tiled_tri_f32; 1912 compute_func = (void *) tri_apply_tile_f32; 1913 break; 1914 default: break; 1915 } 1916 } else if (is_f16) { 1917 task_func = unary_thread_row_block; 1918 switch (octx->op) { 1919 case HTP_OP_NORM: compute_func = (void *) norm_f16; break; 1920 case HTP_OP_RMS_NORM: compute_func = (void *) rms_norm_f16; break; 1921 case HTP_OP_SCALE: compute_func = (void *) scale_f16; break; 1922 case HTP_OP_CLAMP: compute_func = (void *) clamp_f16; break; 1923 case HTP_OP_SQR: compute_func = (void *) sqr_f16; break; 1924 case HTP_OP_SQRT: compute_func = (void *) sqrt_f16; break; 1925 case HTP_OP_L2_NORM: compute_func = (void *) l2_norm_f16; break; 1926 case HTP_OP_UNARY_ABS: compute_func = (void *) abs_f16; break; 1927 case HTP_OP_UNARY_LOG: compute_func = (void *) log_f16; break; 1928 case HTP_OP_UNARY_STEP: compute_func = (void *) step_f16; break; 1929 case HTP_OP_UNARY_CEIL: compute_func = (void *) ceil_f16; break; 1930 case HTP_OP_UNARY_SGN: compute_func = (void *) sgn_f16; break; 1931 case HTP_OP_HADAMARD: compute_func = (void *) hadamard_f16; break; 1932 default: break; 1933 } 1934 } else { 1935 task_func = unary_thread_row_block; 1936 switch (octx->op) { 1937 case HTP_OP_NORM: compute_func = (void *) norm_f32; break; 1938 case HTP_OP_RMS_NORM: compute_func = (void *) rms_norm_f32; break; 1939 case HTP_OP_RMS_NORM_MUL: 1940 task_func = unary_thread_rms_norm_mul_f32; 1941 compute_func = (void *) rms_norm_mul_f32; 1942 break; 1943 case HTP_OP_SCALE: compute_func = (void *) scale_f32; break; 1944 case HTP_OP_CLAMP: compute_func = (void *) clamp_f32; break; 1945 case HTP_OP_LEAKY_RELU: compute_func = (void *) leaky_relu_f32; break; 1946 case HTP_OP_SQR: compute_func = (void *) sqr_f32; break; 1947 case HTP_OP_SQRT: compute_func = (void *) sqrt_f32; break; 1948 case HTP_OP_UNARY_NEG: compute_func = (void *) neg_f32; break; 1949 case HTP_OP_UNARY_EXP: compute_func = (void *) exp_f32; break; 1950 case HTP_OP_UNARY_SIGMOID: compute_func = (void *) sigmoid_f32; break; 1951 case HTP_OP_UNARY_SILU: compute_func = (void *) silu_f32; break; 1952 case HTP_OP_UNARY_GELU: compute_func = (void *) gelu_f32; break; 1953 case HTP_OP_UNARY_GELU_QUICK: compute_func = (void *) gelu_quick_f32; break; 1954 case HTP_OP_UNARY_GELU_ERF: compute_func = (void *) gelu_erf_f32; break; 1955 case HTP_OP_UNARY_SOFTPLUS: compute_func = (void *) softplus_f32; break; 1956 case HTP_OP_UNARY_TANH: compute_func = (void *) tanh_f32; break; 1957 case HTP_OP_UNARY_ABS: compute_func = (void *) abs_f32; break; 1958 case HTP_OP_UNARY_LOG: compute_func = (void *) log_f32; break; 1959 case HTP_OP_UNARY_RELU: compute_func = (void *) relu_f32; break; 1960 case HTP_OP_UNARY_STEP: compute_func = (void *) step_f32; break; 1961 case HTP_OP_UNARY_CEIL: compute_func = (void *) ceil_f32; break; 1962 case HTP_OP_UNARY_SGN: compute_func = (void *) sgn_f32; break; 1963 case HTP_OP_L2_NORM: compute_func = (void *) l2_norm_f32; break; 1964 case HTP_OP_HADAMARD: compute_func = (void *) hadamard_f32; break; 1965 case HTP_OP_TRI: 1966 task_func = unary_thread_tri_f32; 1967 compute_func = (void *) tri_f32; 1968 break; 1969 default: break; 1970 } 1971 } 1972 1973 if (!task_func || !compute_func) { 1974 FARF(ERROR, "execute_op_unary: task function is NULL for op %d\n", octx->op); 1975 return HTP_STATUS_NO_SUPPORT; 1976 } 1977 1978 uctx.compute = compute_func; 1979 work_queue_run(octx->ctx->work_queue, task_func, &uctx, n_threads); 1980 1981 return err; 1982 } 1983 1984 int op_unary(struct htp_ops_context * octx) { 1985 switch (octx->src[0]->type) { 1986 case HTP_TYPE_F32: 1987 case HTP_TYPE_F16: 1988 return execute_op_unary(octx); 1989 1990 default: 1991 return HTP_STATUS_NO_SUPPORT; 1992 } 1993 }