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/softmax-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 / softmax-ops.c` 618 lines ยท 23497 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 "work-queue.h" 13 #include "hvx-utils.h" 14 #include "hex-fastdiv.h" 15 #include "hex-common.h" 16 #include "hex-profile.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 "htp/softmax-ops.h" 25 #include "hvx-flash-attn.h" 26 27 struct htp_softmax_context { 28 struct htp_ops_context * octx; 29 const struct htp_softmax_kernel_params * kparams; 30 31 void * compute; 32 33 dma_addr_t data_src0; 34 dma_addr_t data_src1; 35 dma_addr_t data_dst; 36 37 uint8_t * vtcm_src0; 38 uint8_t * vtcm_src1; 39 uint8_t * vtcm_dst; 40 41 uint32_t vtcm_src0_size_per_thread; 42 uint32_t vtcm_src1_size_per_thread; 43 uint32_t vtcm_dst_size_per_thread; 44 45 uint32_t src0_spad_half_size; 46 uint32_t src1_spad_half_size; 47 uint32_t dst_spad_half_size; 48 49 uint32_t src0_row_size_aligned; 50 uint32_t src1_row_size_aligned; 51 uint32_t dst_row_size_aligned; 52 53 bool use_f16; 54 55 uint32_t n_head; 56 uint32_t n_head_log2; 57 58 float scale; 59 float max_bias; 60 float m0; 61 float m1; 62 63 struct fastdiv_values div_ne01; 64 struct fastdiv_values div_ne02; 65 struct fastdiv_values div_ne12; 66 struct fastdiv_values div_ne13; 67 68 uint32_t src0_nrows_per_thread; 69 uint32_t row_start; 70 uint32_t nrows; 71 72 float slopes[512] __attribute__((aligned(128))); 73 }; 74 75 typedef void (*softmax_compute_fn_t)( 76 void * restrict dst, 77 const void * restrict src0, 78 const void * restrict mask, 79 uint32_t ne00, 80 float scale, 81 float slope 82 ); 83 84 static void hvx_fast_softmax_prep_f16(const uint8_t * restrict src, 85 uint8_t * restrict dst, 86 const int num_elems, 87 float scale, 88 const uint8_t * restrict mask, 89 float slope) { 90 const HVX_Vector * restrict v_src = (const HVX_Vector *) src; 91 HVX_Vector * restrict v_dst = (HVX_Vector *) dst; 92 const HVX_Vector * restrict v_mask = (const HVX_Vector *) mask; 93 94 HVX_Vector scale_vec = hvx_vec_splat_f32(scale); 95 HVX_Vector slope_vec = hvx_vec_splat_f32(slope); 96 97 const int nvec_64 = num_elems / VLEN_FP16; 98 const int nloe_64 = num_elems % VLEN_FP16; 99 100 #pragma unroll(2) 101 for (int i = 0; i < nvec_64; i++) { 102 HVX_VectorPair p = hvx_vec_f16_to_f32(v_mask[i]); 103 HVX_Vector m0 = Q6_V_lo_W(p); 104 HVX_Vector m1 = Q6_V_hi_W(p); 105 106 HVX_Vector s0 = v_src[2 * i]; 107 HVX_Vector s1 = v_src[2 * i + 1]; 108 109 HVX_Vector v0 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(s0, scale_vec), Q6_Vqf32_vmpy_VsfVsf(m0, slope_vec)); 110 HVX_Vector v1 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(s1, scale_vec), Q6_Vqf32_vmpy_VsfVsf(m1, slope_vec)); 111 112 v_dst[2 * i] = Q6_Vsf_equals_Vqf32(v0); 113 v_dst[2 * i + 1] = Q6_Vsf_equals_Vqf32(v1); 114 } 115 116 if (nloe_64 > 0) { 117 HVX_VectorPair p = hvx_vec_f16_to_f32(v_mask[nvec_64]); 118 HVX_Vector m0 = Q6_V_lo_W(p); 119 120 HVX_Vector s0 = v_src[2 * nvec_64]; 121 HVX_Vector v0 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(s0, scale_vec), Q6_Vqf32_vmpy_VsfVsf(m0, slope_vec)); 122 123 if (nloe_64 <= VLEN_FP32) { 124 hvx_vec_store_a(&v_dst[2 * nvec_64], nloe_64 * sizeof(float), Q6_Vsf_equals_Vqf32(v0)); 125 } else { 126 v_dst[2 * nvec_64] = Q6_Vsf_equals_Vqf32(v0); 127 128 HVX_Vector m1 = Q6_V_hi_W(p); 129 HVX_Vector s1 = v_src[2 * nvec_64 + 1]; 130 HVX_Vector v1 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(s1, scale_vec), Q6_Vqf32_vmpy_VsfVsf(m1, slope_vec)); 131 132 hvx_vec_store_a(&v_dst[2 * nvec_64 + 1], (nloe_64 - VLEN_FP32) * sizeof(float), Q6_Vsf_equals_Vqf32(v1)); 133 } 134 } 135 } 136 137 static void hvx_fast_softmax_prep_f32(const uint8_t * restrict src, 138 uint8_t * restrict dst, 139 const int num_elems, 140 float scale, 141 const uint8_t * restrict mask, 142 float slope) { 143 const HVX_Vector * restrict v_src = (const HVX_Vector *) src; 144 HVX_Vector * restrict v_dst = (HVX_Vector *) dst; 145 const HVX_Vector * restrict v_mask = (const HVX_Vector *) mask; 146 147 HVX_Vector scale_vec = hvx_vec_splat_f32(scale); 148 HVX_Vector slope_vec = hvx_vec_splat_f32(slope); 149 150 const int nvec = num_elems / VLEN_FP32; 151 const int nloe = num_elems % VLEN_FP32; 152 153 #pragma unroll(4) 154 for (int i = 0; i < nvec; i++) { 155 HVX_Vector v1 = v_src[i]; 156 HVX_Vector v3 = v_mask[i]; 157 158 HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_vec); 159 HVX_Vector v4 = Q6_Vqf32_vmpy_VsfVsf(v3, slope_vec); 160 HVX_Vector v5 = Q6_Vqf32_vadd_Vqf32Vqf32(v2, v4); 161 162 v_dst[i] = Q6_Vsf_equals_Vqf32(v5); 163 } 164 165 if (nloe > 0) { 166 HVX_Vector v1 = v_src[nvec]; 167 HVX_Vector v3 = v_mask[nvec]; 168 169 HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_vec); 170 HVX_Vector v4 = Q6_Vqf32_vmpy_VsfVsf(v3, slope_vec); 171 HVX_Vector v5 = Q6_Vqf32_vadd_Vqf32Vqf32(v2, v4); 172 173 hvx_vec_store_a(&v_dst[nvec], nloe * sizeof(float), Q6_Vsf_equals_Vqf32(v5)); 174 } 175 } 176 177 static void hvx_fast_softmax_f32(const uint8_t * restrict src, uint8_t * restrict dst, const int num_elems) { 178 const HVX_Vector * restrict v_src = (const HVX_Vector *) src; 179 HVX_Vector * restrict v_dst = (HVX_Vector *) dst; 180 181 const int nvec = num_elems / VLEN_FP32; 182 const int nloe = num_elems % VLEN_FP32; 183 184 HVX_Vector max_vec = hvx_vec_splat_f32(((const float *) src)[0]); 185 186 #pragma unroll(2) 187 for (int i = 0; i < nvec; i++) { 188 HVX_Vector v1 = v_src[i]; 189 max_vec = Q6_Vsf_vmax_VsfVsf(max_vec, v1); 190 } 191 192 if (nloe > 0) { 193 HVX_VectorPred q_mask = Q6_Q_vsetq_R(nloe * sizeof(float)); 194 HVX_Vector neg_inf = hvx_vec_splat_f32(-INFINITY); 195 HVX_Vector v_tail = Q6_V_vmux_QVV(q_mask, v_src[nvec], neg_inf); 196 max_vec = Q6_Vsf_vmax_VsfVsf(max_vec, v_tail); 197 } 198 199 max_vec = hvx_vec_reduce_max_f32(max_vec); 200 201 HVX_Vector sum_vec = Q6_V_vsplat_R(0x00000000); 202 203 #pragma unroll(2) 204 for (int i = 0; i < nvec; i++) { 205 HVX_Vector v1 = v_src[i]; 206 HVX_Vector v2 = Q6_Vqf32_vsub_VsfVsf(v1, max_vec); 207 208 HVX_Vector v3 = hvx_vec_exp_f32(Q6_Vsf_equals_Vqf32(v2)); 209 210 sum_vec = Q6_Vqf32_vadd_VsfVsf(Q6_Vsf_equals_Vqf32(sum_vec), v3); 211 212 v_dst[i] = v3; 213 } 214 215 if (nloe > 0) { 216 HVX_VectorPred q_mask = Q6_Q_vsetq_R(nloe * sizeof(float)); 217 HVX_Vector v1 = v_src[nvec]; 218 HVX_Vector v2 = Q6_Vqf32_vsub_VsfVsf(v1, max_vec); 219 HVX_Vector v3 = hvx_vec_exp_f32(Q6_Vsf_equals_Vqf32(v2)); 220 HVX_Vector v3_pad = Q6_V_vmux_QVV(q_mask, v3, Q6_V_vzero()); 221 222 sum_vec = Q6_Vqf32_vadd_VsfVsf(Q6_Vsf_equals_Vqf32(sum_vec), v3_pad); 223 v_dst[nvec] = v3_pad; 224 } 225 226 sum_vec = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_vec)); 227 228 HVX_VectorPred pos_sum = Q6_Q_vcmp_gt_VwVw(sum_vec, Q6_V_vzero()); 229 HVX_Vector v4 = hvx_vec_inverse_f32(sum_vec); 230 HVX_Vector scale_vec = Q6_V_vmux_QVV(pos_sum, v4, hvx_vec_splat_f32(1.0f)); 231 232 #pragma unroll(2) 233 for (int i = 0; i < nvec; i++) { 234 HVX_Vector v1 = v_dst[i]; 235 HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_vec); 236 v_dst[i] = Q6_Vsf_equals_Vqf32(v2); 237 } 238 239 if (nloe > 0) { 240 HVX_Vector v1 = v_dst[nvec]; 241 HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_vec); 242 hvx_vec_store_a(&v_dst[nvec], nloe * sizeof(float), Q6_Vsf_equals_Vqf32(v2)); 243 } 244 } 245 246 static void compute_fast_softmax_f32_nomask( 247 void * restrict dst, 248 const void * restrict src0, 249 const void * restrict mask, 250 uint32_t ne00, 251 float scale, 252 float slope 253 ) { 254 (void) mask; 255 (void) slope; 256 hvx_scale_f32((uint8_t *) dst, (const uint8_t *) src0, ne00, scale); 257 hvx_fast_softmax_f32((const uint8_t *) dst, (uint8_t *) dst, ne00); 258 } 259 260 static void compute_fast_softmax_f32_mask_f32( 261 void * restrict dst, 262 const void * restrict src0, 263 const void * restrict mask, 264 uint32_t ne00, 265 float scale, 266 float slope 267 ) { 268 hvx_fast_softmax_prep_f32((const uint8_t *) src0, (uint8_t *) dst, ne00, scale, (const uint8_t *) mask, slope); 269 hvx_fast_softmax_f32((const uint8_t *) dst, (uint8_t *) dst, ne00); 270 } 271 272 static void compute_fast_softmax_f32_mask_f16( 273 void * restrict dst, 274 const void * restrict src0, 275 const void * restrict mask, 276 uint32_t ne00, 277 float scale, 278 float slope 279 ) { 280 hvx_fast_softmax_prep_f16((const uint8_t *) src0, (uint8_t *) dst, ne00, scale, (const uint8_t *) mask, slope); 281 hvx_fast_softmax_f32((const uint8_t *) dst, (uint8_t *) dst, ne00); 282 } 283 284 static const softmax_compute_fn_t softmax_kernels[HTP_SOFTMAX_KERNEL_COUNT] = { 285 [HTP_SOFTMAX_KERNEL_NOMASK] = compute_fast_softmax_f32_nomask, 286 [HTP_SOFTMAX_KERNEL_MASK_F32] = compute_fast_softmax_f32_mask_f32, 287 [HTP_SOFTMAX_KERNEL_MASK_F16] = compute_fast_softmax_f32_mask_f16, 288 }; 289 290 static void softmax_thread_dma(unsigned int nth, unsigned int ith, void * data) { 291 (void) nth; 292 const struct htp_softmax_context * smctx = (const struct htp_softmax_context *) data; 293 struct htp_ops_context * octx = smctx->octx; 294 const struct htp_tensor * src0 = octx->src[0]; 295 const struct htp_tensor * dst = octx->dst; 296 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 297 298 const uint32_t src0_nrows = smctx->nrows; 299 const uint32_t src0_nrows_per_thread = smctx->src0_nrows_per_thread; 300 301 const uint32_t src0_start_row = smctx->row_start + src0_nrows_per_thread * ith; 302 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, smctx->row_start + src0_nrows); 303 304 if (src0_start_row >= src0_end_row) { 305 return; 306 } 307 308 const dma_addr_t data_src0 = smctx->data_src0; 309 const dma_addr_t data_dst = smctx->data_dst; 310 311 const size_t src0_row_size = src0->ne[0] * sizeof(float); 312 const size_t dst_row_size = src0->ne[0] * sizeof(float); 313 314 uint8_t * src0_vtcm_base = smctx->vtcm_src0 + (ith * smctx->vtcm_src0_size_per_thread); 315 uint8_t * dst_vtcm_base = smctx->vtcm_dst + (ith * smctx->vtcm_dst_size_per_thread); 316 317 const size_t src0_vtcm_half = smctx->src0_spad_half_size; 318 const size_t dst_vtcm_half = smctx->dst_spad_half_size; 319 320 dma_queue * dma_q = octx->ctx->dma[ith]; 321 322 for (uint32_t r = src0_start_row, idx = 0; r < src0_end_row && idx < 2; r++, idx++) { 323 dma_addr_t cur_dst = data_dst + r * dst_row_size; 324 dma_addr_t cur_src0 = data_src0 + r * src0_row_size; 325 void * d_spad = dst_vtcm_base + idx * dst_vtcm_half; 326 void * s_spad = src0_vtcm_base + idx * src0_vtcm_half; 327 328 dma_queue_push(dma_q, dma_make_data(cur_dst, d_spad), 329 dst_row_size, smctx->dst_row_size_aligned, dst_row_size, 0); 330 dma_queue_push(dma_q, dma_make_data(s_spad, cur_src0), 331 smctx->src0_row_size_aligned, src0_row_size, src0_row_size, 1); 332 } 333 334 softmax_compute_fn_t compute = (softmax_compute_fn_t) smctx->compute; 335 const uint32_t ne00 = src0->ne[0]; 336 337 for (uint32_t r = src0_start_row; r < src0_end_row; ++r) { 338 void * d_spad = (void *) dma_queue_pop(dma_q).src; 339 void * s_spad = (void *) dma_queue_pop(dma_q).dst; 340 341 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, r); 342 compute(d_spad, s_spad, NULL, ne00, smctx->scale, 1.0f); 343 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, r); 344 345 dma_addr_t cur_dst = data_dst + r * dst_row_size; 346 dma_queue_push(dma_q, dma_make_data(cur_dst, d_spad), 347 dst_row_size, smctx->dst_row_size_aligned, dst_row_size, 1); 348 349 const uint32_t next_r = r + 2; 350 if (next_r < src0_end_row) { 351 dma_addr_t next_src0 = data_src0 + next_r * src0_row_size; 352 dma_queue_push(dma_q, dma_make_data(s_spad, next_src0), 353 smctx->src0_row_size_aligned, src0_row_size, src0_row_size, 1); 354 } 355 } 356 357 dma_queue_flush(dma_q); 358 } 359 360 static void softmax_thread_mask_dma(unsigned int nth, unsigned int ith, void * data) { 361 (void) nth; 362 const struct htp_softmax_context * smctx = (const struct htp_softmax_context *) data; 363 struct htp_ops_context * octx = smctx->octx; 364 const struct htp_tensor * src0 = octx->src[0]; 365 const struct htp_tensor * src1 = octx->src[1]; 366 const struct htp_tensor * dst = octx->dst; 367 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 368 369 const uint32_t src0_nrows = smctx->nrows; 370 const uint32_t src0_nrows_per_thread = smctx->src0_nrows_per_thread; 371 372 const uint32_t src0_start_row = smctx->row_start + src0_nrows_per_thread * ith; 373 const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_thread, smctx->row_start + src0_nrows); 374 375 if (src0_start_row >= src0_end_row) { 376 return; 377 } 378 379 const dma_addr_t data_src0 = smctx->data_src0; 380 const dma_addr_t data_src1 = smctx->data_src1; 381 const dma_addr_t data_dst = smctx->data_dst; 382 383 const size_t src0_row_size = src0->ne[0] * sizeof(float); 384 const size_t dst_row_size = src0->ne[0] * sizeof(float); 385 const size_t mask_row_size = smctx->use_f16 ? (src1->ne[0] * sizeof(__fp16)) : (src1->ne[0] * sizeof(float)); 386 387 uint8_t * src0_vtcm_base = smctx->vtcm_src0 + (ith * smctx->vtcm_src0_size_per_thread); 388 uint8_t * src1_vtcm_base = smctx->vtcm_src1 + (ith * smctx->vtcm_src1_size_per_thread); 389 uint8_t * dst_vtcm_base = smctx->vtcm_dst + (ith * smctx->vtcm_dst_size_per_thread); 390 391 const size_t src0_vtcm_half = smctx->src0_spad_half_size; 392 const size_t src1_vtcm_half = smctx->src1_spad_half_size; 393 const size_t dst_vtcm_half = smctx->dst_spad_half_size; 394 395 const uint32_t nb11 = src1->nb[1]; 396 const uint32_t nb12 = src1->nb[2]; 397 const uint32_t nb13 = src1->nb[3]; 398 399 const uint32_t ne00 = src0->ne[0]; 400 const uint32_t ne01 = src0->ne[1]; 401 const uint32_t ne02 = src0->ne[2]; 402 const uint32_t ne03 = src0->ne[3]; 403 const uint32_t ne12 = src1->ne[2]; 404 const uint32_t ne13 = src1->ne[3]; 405 406 const struct fastdiv_values * div_ne01 = &smctx->div_ne01; 407 const struct fastdiv_values * div_ne02 = &smctx->div_ne02; 408 const struct fastdiv_values * div_ne12 = &smctx->div_ne12; 409 const struct fastdiv_values * div_ne13 = &smctx->div_ne13; 410 411 dma_queue * dma_q = octx->ctx->dma[ith]; 412 413 for (uint32_t r = src0_start_row, idx = 0; r < src0_end_row && idx < 2; r++, idx++) { 414 dma_addr_t cur_dst = data_dst + r * dst_row_size; 415 dma_addr_t cur_src0 = data_src0 + r * src0_row_size; 416 417 uint32_t i1 = fastmodulo(r, ne01, div_ne01); 418 uint32_t r_div_ne01 = fastdiv(r, div_ne01); 419 uint32_t i2 = fastmodulo(r_div_ne01, ne02, div_ne02); 420 uint32_t i3 = fastdiv(r_div_ne01, div_ne02); 421 uint32_t i12 = (ne12 == ne02) ? i2 : fastmodulo(i2, ne12, div_ne12); 422 uint32_t i13 = (ne13 == ne03) ? i3 : fastmodulo(i3, ne13, div_ne13); 423 dma_addr_t cur_src1 = data_src1 + i1 * nb11 + i12 * nb12 + i13 * nb13; 424 425 void * d_spad = dst_vtcm_base + idx * dst_vtcm_half; 426 void * s_spad = src0_vtcm_base + idx * src0_vtcm_half; 427 void * m_spad = src1_vtcm_base + idx * src1_vtcm_half; 428 429 dma_queue_push(dma_q, dma_make_data(cur_dst, d_spad), 430 dst_row_size, smctx->dst_row_size_aligned, dst_row_size, 0); 431 dma_queue_push(dma_q, dma_make_data(s_spad, cur_src0), 432 smctx->src0_row_size_aligned, src0_row_size, src0_row_size, 1); 433 dma_queue_push(dma_q, dma_make_data(m_spad, cur_src1), 434 smctx->src1_row_size_aligned, mask_row_size, mask_row_size, 1); 435 } 436 437 softmax_compute_fn_t compute = (softmax_compute_fn_t) smctx->compute; 438 const bool has_bias = smctx->max_bias > 0.0f; 439 uint32_t prev_i2 = (uint32_t)-1; 440 float slope = 1.0f; 441 442 for (uint32_t r = src0_start_row; r < src0_end_row; ++r) { 443 void * d_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).src; 444 void * s_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 445 void * m_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).dst; 446 447 if (has_bias) { 448 uint32_t r_div_ne01 = fastdiv(r, div_ne01); 449 uint32_t i2 = fastmodulo(r_div_ne01, ne02, div_ne02); 450 if (i2 != prev_i2) { 451 slope = smctx->slopes[i2]; 452 prev_i2 = i2; 453 } 454 } 455 456 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, r); 457 compute(d_spad, s_spad, m_spad, ne00, smctx->scale, slope); 458 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, r); 459 460 dma_addr_t cur_dst = data_dst + r * dst_row_size; 461 dma_queue_push(dma_q, dma_make_data(cur_dst, d_spad), 462 dst_row_size, smctx->dst_row_size_aligned, dst_row_size, 1); 463 464 const uint32_t next_r = r + 2; 465 if (next_r < src0_end_row) { 466 dma_addr_t next_src0 = data_src0 + next_r * src0_row_size; 467 468 uint32_t ni1 = fastmodulo(next_r, ne01, div_ne01); 469 uint32_t nr_div_ne01 = fastdiv(next_r, div_ne01); 470 uint32_t ni2 = fastmodulo(nr_div_ne01, ne02, div_ne02); 471 uint32_t ni3 = fastdiv(nr_div_ne01, div_ne02); 472 uint32_t ni12 = (ne12 == ne02) ? ni2 : fastmodulo(ni2, ne12, div_ne12); 473 uint32_t ni13 = (ne13 == ne03) ? ni3 : fastmodulo(ni3, ne13, div_ne13); 474 dma_addr_t next_src1 = data_src1 + ni1 * nb11 + ni12 * nb12 + ni13 * nb13; 475 476 dma_queue_push(dma_q, dma_make_data(s_spad, next_src0), 477 smctx->src0_row_size_aligned, src0_row_size, src0_row_size, 1); 478 dma_queue_push(dma_q, dma_make_data(m_spad, next_src1), 479 smctx->src1_row_size_aligned, mask_row_size, mask_row_size, 1); 480 } 481 } 482 483 dma_queue_flush(dma_q); 484 } 485 486 static int execute_op_softmax_f32(struct htp_ops_context * octx) { 487 const struct htp_tensor * src0 = octx->src[0]; 488 const struct htp_tensor * dst = octx->dst; 489 490 const char * op_type = "softmax-f32"; 491 492 const struct htp_softmax_kernel_params * kparams = 493 (const struct htp_softmax_kernel_params *) octx->kernel_params; 494 495 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 496 return HTP_STATUS_INVAL_PARAMS; 497 } 498 499 if (kparams->kernel_type >= HTP_SOFTMAX_KERNEL_COUNT) { 500 return HTP_STATUS_INVAL_PARAMS; 501 } 502 503 if (octx->ctx->vtcm_size < (size_t) kparams->vtcm_size) { 504 FARF(ERROR, "%s : current VTCM reservation %zu is too small, needed %u\n", 505 op_type, octx->ctx->vtcm_size, kparams->vtcm_size); 506 return HTP_STATUS_VTCM_TOO_SMALL; 507 } 508 509 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 510 const size_t elem_size = sizeof(float); 511 const size_t dst_row_size = dst->nb[1]; 512 513 uint32_t row_start = 0; 514 uint32_t nrows = src0_nrows; 515 516 if (octx->ctx->mdev.count > 1) { 517 uint32_t rows_per_chunk = 0; 518 htp_tensor_mdev_rows_per_chunk(dst, (uint32_t) elem_size, (uint32_t) dst_row_size, &rows_per_chunk); 519 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 520 src0_nrows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 521 row_start = range.start; 522 nrows = range.count; 523 if (nrows < octx->n_threads) { 524 htp_ops_context_set_n_threads(octx, nrows ? nrows : 1); 525 } 526 } 527 528 if (nrows == 0) { 529 return HTP_STATUS_OK; 530 } 531 532 const uint32_t n_threads = octx->n_threads; 533 uint8_t * const vtcm_base = (uint8_t *) octx->ctx->vtcm_base; 534 535 struct htp_softmax_vtcm_layout layout; 536 htp_softmax_vtcm_layout_build(&layout, 537 src0->ne[0], 538 kparams->use_src1 ? octx->src[1]->ne[0] : 0, 539 kparams->use_src1 != 0, 540 kparams->use_f16 != 0, 541 kparams->n_threads); 542 543 struct htp_softmax_context smctx = { 544 .octx = octx, 545 .kparams = kparams, 546 .compute = (void *) softmax_kernels[kparams->kernel_type], 547 548 .data_src0 = src0->data, 549 .data_src1 = kparams->use_src1 ? octx->src[1]->data : 0, 550 .data_dst = dst->data, 551 552 .vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, layout.off_src0), 553 .vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, layout.off_dst), 554 .vtcm_src1 = VTCM_LAYOUT_PTR_OPTIONAL(uint8_t, vtcm_base, layout.off_src1, kparams->use_src1), 555 556 .vtcm_src0_size_per_thread = (uint32_t) layout.src0_bytes_per_thread, 557 .vtcm_src1_size_per_thread = (uint32_t) layout.src1_bytes_per_thread, 558 .vtcm_dst_size_per_thread = (uint32_t) layout.dst_bytes_per_thread, 559 560 .src0_spad_half_size = (uint32_t) layout.src0_spad_half_size, 561 .src1_spad_half_size = (uint32_t) layout.src1_spad_half_size, 562 .dst_spad_half_size = (uint32_t) layout.dst_spad_half_size, 563 564 .src0_row_size_aligned = (uint32_t) layout.src0_spad_half_size, 565 .src1_row_size_aligned = (uint32_t) layout.src1_spad_half_size, 566 .dst_row_size_aligned = (uint32_t) layout.dst_spad_half_size, 567 568 .use_f16 = kparams->use_f16 != 0, 569 570 .n_head = kparams->n_head, 571 .n_head_log2 = kparams->n_head_log2, 572 573 .scale = kparams->scale, 574 .max_bias = kparams->max_bias, 575 .m0 = kparams->m0, 576 .m1 = kparams->m1, 577 578 .div_ne01 = kparams->div_ne01, 579 .div_ne02 = kparams->div_ne02, 580 .div_ne12 = kparams->div_ne12, 581 .div_ne13 = kparams->div_ne13, 582 583 .src0_nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div), 584 .row_start = row_start, 585 .nrows = nrows, 586 }; 587 588 if (kparams->max_bias > 0.0f && kparams->use_src1) { 589 if (kparams->n_head > 512) { 590 return HTP_STATUS_INVAL_PARAMS; 591 } 592 for (uint32_t h = 0; h < kparams->n_head; h += 32) { 593 HVX_Vector v_slopes = hvx_alibi_slopes(h, 1, kparams->n_head_log2, kparams->m0, kparams->m1); 594 hvx_vmem(&smctx.slopes[h]) = v_slopes; 595 } 596 } 597 598 work_queue_func_t task_func = kparams->use_src1 ? softmax_thread_mask_dma : softmax_thread_dma; 599 work_queue_run(octx->ctx->work_queue, task_func, &smctx, n_threads); 600 601 return HTP_STATUS_OK; 602 } 603 604 int op_softmax(struct htp_ops_context * octx) { 605 int err = HTP_STATUS_OK; 606 607 switch (octx->src[0]->type) { 608 case HTP_TYPE_F32: 609 err = execute_op_softmax_f32(octx); 610 break; 611 612 default: 613 err = HTP_STATUS_NO_SUPPORT; 614 break; 615 } 616 617 return err; 618 }