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-kparams.cpp # 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-kparams.cpp` 5287 lines ยท 214384 bytes Raw Blame History 1 #define _USE_MATH_DEFINES 2 3 #include "htp-kparams.h" 4 #include "htp-opnode.h" 5 #include "ggml-impl.h" 6 7 #include 8 #include 9 #include 10 11 #ifndef M_LOG2E 12 # define M_LOG2E 1.`44269504088896340736` 13 #endif 14 15 htp_op_code htp_op_remap(const struct ggml_tensor * t) { 16 switch (t->op) { 17 case GGML_OP_FLASH_ATTN_EXT: return HTP_OP_FLASH_ATTN_EXT; 18 case GGML_OP_MUL_MAT: return htp_is_hadamard(t) ? HTP_OP_HADAMARD : HTP_OP_MUL_MAT; 19 case GGML_OP_MUL_MAT_ID: return HTP_OP_MUL_MAT_ID; 20 case GGML_OP_MUL: return HTP_OP_MUL; 21 case GGML_OP_ADD: return HTP_OP_ADD; 22 case GGML_OP_ADD_ID: return HTP_OP_ADD_ID; 23 case GGML_OP_SUB: return HTP_OP_SUB; 24 case GGML_OP_DIV: return HTP_OP_DIV; 25 case GGML_OP_DUP: 26 case GGML_OP_CPY: 27 case GGML_OP_CONT: return HTP_OP_CPY; 28 case GGML_OP_GET_ROWS: return HTP_OP_GET_ROWS; 29 case GGML_OP_SET_ROWS: return HTP_OP_SET_ROWS; 30 case GGML_OP_SUM: return HTP_OP_SUM; 31 case GGML_OP_SUM_ROWS: return HTP_OP_SUM_ROWS; 32 case GGML_OP_POOL_2D: return HTP_OP_POOL_2D; 33 case GGML_OP_POOL_1D: return HTP_OP_POOL_1D; 34 case GGML_OP_ARGSORT: return HTP_OP_ARGSORT; 35 case GGML_OP_TOP_K: return HTP_OP_TOP_K; 36 case GGML_OP_ARGMAX: return HTP_OP_ARGMAX; 37 case GGML_OP_NORM: return HTP_OP_NORM; 38 case GGML_OP_L2_NORM: return HTP_OP_L2_NORM; 39 case GGML_OP_RMS_NORM: return HTP_OP_RMS_NORM; 40 case GGML_OP_CONCAT: return HTP_OP_CONCAT; 41 case GGML_OP_SCALE: return HTP_OP_SCALE; 42 case GGML_OP_CLAMP: return HTP_OP_CLAMP; 43 case GGML_OP_LEAKY_RELU: return HTP_OP_LEAKY_RELU; 44 case GGML_OP_SQR: return HTP_OP_SQR; 45 case GGML_OP_SQRT: return HTP_OP_SQRT; 46 case GGML_OP_LOG: return HTP_OP_UNARY_LOG; 47 case GGML_OP_SOFT_MAX: return HTP_OP_SOFTMAX; 48 case GGML_OP_SSM_CONV: return HTP_OP_SSM_CONV; 49 case GGML_OP_GATED_DELTA_NET: return HTP_OP_GATED_DELTA_NET; 50 case GGML_OP_ROPE: return HTP_OP_ROPE; 51 case GGML_OP_REPEAT: return HTP_OP_REPEAT; 52 case GGML_OP_CUMSUM: return HTP_OP_CUMSUM; 53 case GGML_OP_TOP_K_MASK: return HTP_OP_TOP_K_MASK; 54 case GGML_OP_FILL: return HTP_OP_FILL; 55 case GGML_OP_DIAG: return HTP_OP_DIAG; 56 case GGML_OP_SOLVE_TRI: return HTP_OP_SOLVE_TRI; 57 case GGML_OP_DSV4_HC_PRE: return HTP_OP_DSV4_HC_PRE; 58 case GGML_OP_DSV4_HC_POST: return HTP_OP_DSV4_HC_POST; 59 case GGML_OP_TRI: return HTP_OP_TRI; 60 case GGML_OP_PAD: return HTP_OP_PAD; 61 case GGML_OP_IM2COL: return HTP_OP_IM2COL; 62 case GGML_OP_IM2COL_3D: return HTP_OP_IM2COL_3D; 63 case GGML_OP_CONV_2D: return HTP_OP_CONV_2D; 64 case GGML_OP_CONV_2D_DW: return HTP_OP_CONV_2D_DW; 65 case GGML_OP_ROLL: return HTP_OP_ROLL; 66 case GGML_OP_UPSCALE: return HTP_OP_UPSCALE; 67 case GGML_OP_LIGHTNING_INDEXER: return HTP_OP_LIGHTNING_INDEXER; 68 69 case GGML_OP_UNARY: 70 switch (ggml_get_unary_op(t)) { 71 case GGML_UNARY_OP_SILU: return HTP_OP_UNARY_SILU; 72 case GGML_UNARY_OP_GELU: return HTP_OP_UNARY_GELU; 73 case GGML_UNARY_OP_GELU_QUICK: return HTP_OP_UNARY_GELU_QUICK; 74 case GGML_UNARY_OP_GELU_ERF: return HTP_OP_UNARY_GELU_ERF; 75 case GGML_UNARY_OP_SIGMOID: return HTP_OP_UNARY_SIGMOID; 76 case GGML_UNARY_OP_NEG: return HTP_OP_UNARY_NEG; 77 case GGML_UNARY_OP_EXP: return HTP_OP_UNARY_EXP; 78 case GGML_UNARY_OP_SOFTPLUS: return HTP_OP_UNARY_SOFTPLUS; 79 case GGML_UNARY_OP_TANH: return HTP_OP_UNARY_TANH; 80 case GGML_UNARY_OP_ABS: return HTP_OP_UNARY_ABS; 81 case GGML_UNARY_OP_RELU: return HTP_OP_UNARY_RELU; 82 case GGML_UNARY_OP_STEP: return HTP_OP_UNARY_STEP; 83 case GGML_UNARY_OP_CEIL: return HTP_OP_UNARY_CEIL; 84 case GGML_UNARY_OP_SGN: return HTP_OP_UNARY_SGN; 85 default: break; 86 } 87 break; 88 89 case GGML_OP_GLU: 90 switch (ggml_get_glu_op(t)) { 91 case GGML_GLU_OP_REGLU: return HTP_OP_GLU_REGLU; 92 case GGML_GLU_OP_SWIGLU: return HTP_OP_GLU_SWIGLU; 93 case GGML_GLU_OP_SWIGLU_OAI: return HTP_OP_GLU_SWIGLU_OAI; 94 case GGML_GLU_OP_SWIGLU_CLAMP: return HTP_OP_GLU_SWIGLU_CLAMP; 95 case GGML_GLU_OP_GEGLU: return HTP_OP_GLU_GEGLU; 96 case GGML_GLU_OP_GEGLU_QUICK: return HTP_OP_GLU_GEGLU_QUICK; 97 case GGML_GLU_OP_GEGLU_ERF: return HTP_OP_GLU_GEGLU_ERF; 98 default: break; 99 } 100 break; 101 102 default: 103 break; 104 } 105 return HTP_OP_INVALID; 106 } 107 108 bool htp_rows_stride(const int64_t * ne, const size_t * nb, size_t * stride) { 109 size_t s = 0, next = 0; 110 for (int i = 1; i < GGML_MAX_DIMS; i++) { 111 if (ne[i] == 1) continue; 112 if (s == 0) { s = nb[i]; next = s * ne[i]; continue; } 113 if (nb[i] != next) return false; 114 next *= ne[i]; 115 } 116 *stride = s ? s : nb[1]; 117 return true; 118 } 119 120 bool htp_matmul_can_collapse(const struct ggml_tensor * src0, const struct ggml_tensor * src1, const struct ggml_tensor * dst) { 121 size_t s1, sd; 122 return (dst->op == GGML_OP_MUL_MAT || dst->op == GGML_OP_ADD) && 123 src0->ne[2] == 1 && src0->ne[3] == 1 && src1->ne[2] * src1->ne[3] > 1 && 124 src1->nb[0] == ggml_type_size(src1->type) && htp_rows_stride(src1->ne, src1->nb, &s1) && 125 dst->nb[0] == ggml_type_size(dst->type) && htp_rows_stride(dst->ne, dst->nb, &sd); 126 } 127 128 ggml_tensor htp_tensor_collapse_rows(const struct ggml_tensor * t) { 129 size_t stride = 0; 130 htp_rows_stride(t->ne, t->nb, &stride); 131 ggml_tensor c = *t; 132 c.ne[1] = t->ne[1] * t->ne[2] * t->ne[3]; 133 c.ne[2] = 1; 134 c.ne[3] = 1; 135 c.nb[1] = stride; 136 c.nb[2] = c.nb[1] * c.ne[1]; 137 c.nb[3] = c.nb[2]; 138 return c; 139 } 140 141 bool htp_kparams_solve_allreduce( 142 const struct ggml_hexagon_session * sess, 143 const struct ggml_tensor * dst, 144 uint32_t rank, 145 uint32_t n_ranks, 146 bool has_add, 147 bool is_row_bcast, 148 bool is_shard_ok, 149 struct htp_allreduce_kernel_params * kparams 150 ) { 151 memset(kparams, 0, sizeof(*kparams)); 152 kparams->rank = (int32_t) rank; 153 kparams->n_ranks = (int32_t) n_ranks; 154 kparams->is_row_bcast = (has_add && is_row_bcast) ? 1 : 0; 155 156 const uint32_t max_threads = htp_session_n_threads(sess); 157 const size_t vtcm_size = htp_session_vtcm_size(sess); 158 159 const uint32_t nelem = (uint32_t) ggml_nelements(dst); 160 const uint32_t elem_size = (dst->type == GGML_TYPE_F16) ? sizeof(ggml_fp16_t) : sizeof(float); 161 const bool is_contiguous = ggml_is_contiguous(dst); 162 163 const uint32_t ne0 = (uint32_t) dst->ne[0]; 164 const uint32_t ne1 = (uint32_t) (dst->ne[1] * dst->ne[2] * dst->ne[3]); 165 kparams->ne0 = (int32_t) ne0; 166 kparams->ne1 = (int32_t) ne1; 167 168 const bool use_1d = is_contiguous && !(has_add && is_row_bcast && ne1 > 1); 169 170 if (has_add) { 171 if (opt_ar_scatter && is_shard_ok && use_1d && !is_row_bcast) { 172 const uint32_t rank_chunk_elems = hex_round_up((nelem + n_ranks - 1) / n_ranks, 128); 173 const uint32_t rank_elem_start = (std::min)(rank * rank_chunk_elems, nelem); 174 const uint32_t rank_elem_end = (std::min)(rank_elem_start + rank_chunk_elems, nelem); 175 kparams->n_dsts = (int32_t) n_ranks; 176 kparams->rank_elem_start = (int32_t) rank_elem_start; 177 kparams->rank_nelem = (int32_t) (rank_elem_end - rank_elem_start); 178 } else if (use_1d) { 179 kparams->n_dsts = 1; 180 kparams->rank_elem_start = 0; 181 kparams->rank_nelem = (int32_t) nelem; 182 } else { 183 kparams->n_dsts = 1; 184 kparams->rank_elem_start = 0; 185 kparams->rank_nelem = (int32_t) ne1; 186 } 187 } else { 188 kparams->n_dsts = (int32_t) n_ranks; 189 if (use_1d) { 190 const uint32_t rank_chunk_elems = hex_round_up((nelem + n_ranks - 1) / n_ranks, 128); 191 const uint32_t rank_elem_start = (std::min)(rank * rank_chunk_elems, nelem); 192 const uint32_t rank_elem_end = (std::min)(rank_elem_start + rank_chunk_elems, nelem); 193 const uint32_t rank_nelem = rank_elem_end - rank_elem_start; 194 kparams->rank_elem_start = (int32_t) rank_elem_start; 195 kparams->rank_nelem = (int32_t) rank_nelem; 196 } else { 197 const uint32_t rank_chunk_rows = (ne1 + n_ranks - 1) / n_ranks; 198 const uint32_t rank_r0 = (std::min)(rank * rank_chunk_rows, ne1); 199 const uint32_t rank_r1 = (std::min)(rank_r0 + rank_chunk_rows, ne1); 200 const uint32_t rank_nrows = rank_r1 - rank_r0; 201 kparams->rank_elem_start = (int32_t) rank_r0; 202 kparams->rank_nelem = (int32_t) rank_nrows; 203 } 204 } 205 206 kparams->mode = (kparams->n_dsts > 1) ? HTP_ALLREDUCE_SHARDED_FANOUT : HTP_ALLREDUCE_FULL; 207 208 if (use_1d) { 209 const uint32_t rank_nelem = (uint32_t) kparams->rank_nelem; 210 const uint32_t n_threads = (std::min)(max_threads, (std::max)(1u, rank_nelem / 128)); 211 kparams->n_threads = n_threads; 212 const size_t n_vtcm_buffers = htp_allreduce_vtcm_buffer_count(n_ranks, n_threads, has_add, is_row_bcast); 213 214 uint32_t block_elems = 65536; 215 if (block_elems > rank_nelem / n_threads && rank_nelem / n_threads > 128) { 216 block_elems = hex_round_up(rank_nelem / (n_threads * 2), 128); 217 } 218 block_elems = (std::max)(128u, block_elems); 219 220 kparams->block_elems = block_elems; 221 kparams->vtcm_size_per_thread = 2 * block_elems * elem_size; 222 kparams->vtcm_size = n_vtcm_buffers * kparams->vtcm_size_per_thread; 223 224 while ((size_t) kparams->vtcm_size > vtcm_size && block_elems > 128) { 225 const size_t max_bytes_per_buf = vtcm_size / (n_vtcm_buffers * 2); 226 block_elems = (uint32_t) hex_align_down((size_t) (max_bytes_per_buf / elem_size), 128); 227 if (block_elems < 128) break; 228 kparams->block_elems = block_elems; 229 kparams->vtcm_size_per_thread = 2 * block_elems * elem_size; 230 kparams->vtcm_size = n_vtcm_buffers * kparams->vtcm_size_per_thread; 231 } 232 233 if (vtcm_size < (size_t) kparams->vtcm_size || block_elems < 128) { 234 HEX_VERBOSE("ggml-hex: %s allreduce 1D solver failed to fit VTCM (%d > %zu)\n", 235 htp_session_name(sess), kparams->vtcm_size, vtcm_size); 236 return false; 237 } 238 239 kparams->elems_per_thread = hex_round_up((rank_nelem + n_threads - 1) / n_threads, block_elems); 240 kparams->kernel_type = HTP_ALLREDUCE_KERNEL_DMA_1D; 241 return true; 242 } else { 243 const uint32_t rank_nrows = (uint32_t) kparams->rank_nelem; 244 const uint32_t n_threads = (std::min)(max_threads, (std::max)(1u, rank_nrows)); 245 kparams->n_threads = n_threads; 246 const size_t n_vtcm_buffers = htp_allreduce_vtcm_buffer_count(n_ranks, n_threads, has_add, is_row_bcast); 247 248 const uint32_t row_bytes = ne0 * elem_size; 249 const uint32_t row_size_aligned = (uint32_t) hex_align_up(row_bytes, 128); 250 kparams->row_size_aligned = row_size_aligned; 251 252 const uint32_t nrows_per_thread = (rank_nrows + n_threads - 1) / n_threads; 253 uint32_t block_rows = (std::min)(128u, nrows_per_thread); 254 block_rows = (std::max)(1u, block_rows); 255 kparams->block_elems = block_rows; 256 257 kparams->vtcm_size_per_thread = 2 * (block_rows * row_size_aligned); 258 kparams->vtcm_size = n_vtcm_buffers * kparams->vtcm_size_per_thread; 259 260 while ((size_t) kparams->vtcm_size > vtcm_size && block_rows > 1) { 261 const size_t max_rows_per_buf = vtcm_size / (n_vtcm_buffers * 2 * row_size_aligned); 262 block_rows = (std::max)(1u, (uint32_t) max_rows_per_buf); 263 kparams->block_elems = block_rows; 264 kparams->vtcm_size_per_thread = 2 * (block_rows * row_size_aligned); 265 kparams->vtcm_size = n_vtcm_buffers * kparams->vtcm_size_per_thread; 266 if (max_rows_per_buf == 0) break; 267 } 268 269 if (vtcm_size < (size_t) kparams->vtcm_size || block_rows < 1) { 270 HEX_VERBOSE("ggml-hex: %s allreduce 2D solver failed to fit VTCM (%d > %zu)\n", 271 htp_session_name(sess), kparams->vtcm_size, vtcm_size); 272 return false; 273 } 274 275 kparams->elems_per_thread = nrows_per_thread; 276 kparams->kernel_type = HTP_ALLREDUCE_KERNEL_DMA_2D; 277 return true; 278 } 279 } 280 281 static bool htp_flash_attn_is_hmx_eligible( 282 const struct ggml_hexagon_session * sess, 283 const struct ggml_tensor * q, 284 const struct ggml_tensor * k, 285 const struct ggml_tensor * v, 286 const struct ggml_tensor * sinks 287 ) { 288 if (htp_session_n_hmx(sess) == 0 || opt_fa_select < 2) { 289 return false; 290 } 291 292 if ((k->type != GGML_TYPE_F16 && k->type != GGML_TYPE_Q8_0) || 293 (v->type != GGML_TYPE_F16 && v->type != GGML_TYPE_Q8_0)) { 294 return false; 295 } 296 297 const uint32_t DK = q->ne[0]; 298 const uint32_t DV = v->ne[0]; 299 300 if (DK % 8 != 0 || DV % 8 != 0) { 301 return false; 302 } 303 304 GGML_UNUSED(sinks); 305 306 if (opt_fa_select > 2) { 307 return true; 308 } 309 310 const uint32_t M = q->ne[1]; 311 if (M > 1) { 312 return true; 313 } 314 315 const uint32_t n_threads = htp_session_n_threads(sess); 316 const uint32_t n_head = q->ne[2]; 317 const uint32_t n_kv_heads = k->ne[2]; 318 const uint32_t G = n_kv_heads > 0 ? n_head / n_kv_heads : 1; 319 const uint32_t S = k->ne[1]; 320 const uint32_t total_threads = n_threads * htp_session_mdev_count(sess); 321 322 if (DK > 256 || (DK == 256 && (DV > 256 || n_kv_heads < total_threads))) { 323 return true; 324 } 325 326 const bool is_tile_aligned = (G > 0 && (32 % G == 0)); 327 if (!is_tile_aligned) { 328 if (n_threads >= 6) { 329 return false; 330 } 331 return S >= 1024; 332 } 333 334 uint32_t s_cross = 512; 335 if (DK == 256) { 336 s_cross = 4096; 337 } else if (DK <= 64) { 338 s_cross = (n_threads >= 8) ? 2048 : ((n_threads >= 6) ? 768 : 512); 339 } else { 340 s_cross = (n_threads >= 8) ? 1024 : 512; 341 } 342 343 return S >= s_cross; 344 } 345 346 static bool htp_kparams_solve_flash_attn( 347 const struct ggml_hexagon_session * sess, 348 const struct ggml_tensor * op, 349 struct htp_fa_kernel_params * kparams 350 ) { 351 if (opt_fa_select < 1) { 352 return false; 353 } 354 355 // K/V row lists (ggml_flash_attn_ext_rows) are not supported 356 if (op->src[5] != nullptr) { 357 return false; 358 } 359 360 memset(kparams, 0, sizeof(*kparams)); 361 362 const struct ggml_tensor * q = op->src[0]; 363 const struct ggml_tensor * k = op->src[1]; 364 const struct ggml_tensor * v = op->src[2]; 365 const struct ggml_tensor * mask = op->src[3]; 366 const struct ggml_tensor * sinks = op->src[4]; 367 const struct ggml_tensor * dst = op; 368 369 if ((q->type != GGML_TYPE_F16 && q->type != GGML_TYPE_F32) || 370 (k->type != GGML_TYPE_F16 && k->type != GGML_TYPE_Q8_0) || 371 (v->type != GGML_TYPE_F16 && v->type != GGML_TYPE_Q8_0)) { 372 return false; 373 } 374 if (mask && mask->type != GGML_TYPE_F16) { 375 return false; 376 } 377 if (sinks && sinks->type != GGML_TYPE_F32) { 378 return false; 379 } 380 if (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) { 381 return false; 382 } 383 384 const uint32_t n_threads = htp_session_n_threads(sess); 385 const size_t vtcm_size = htp_session_vtcm_size(sess); 386 387 const uint32_t neq0 = q->ne[0]; 388 const uint32_t neq1 = q->ne[1]; 389 const uint32_t neq2 = q->ne[2]; 390 const uint32_t nek1 = k->ne[1]; 391 const uint32_t nev0 = v->ne[0]; 392 393 const uint32_t DK = neq0; 394 const uint32_t DV = nev0; 395 396 const uint32_t n_kv_heads = k->ne[2]; 397 const uint32_t G = neq2 / n_kv_heads; 398 399 float scale = 1.0f; 400 float max_bias = 0.0f; 401 float logit_softcap = 0.0f; 402 memcpy(&scale, &op->op_params[0], sizeof(float)); 403 memcpy(&max_bias, &op->op_params[1], sizeof(float)); 404 memcpy(&logit_softcap, &op->op_params[2], sizeof(float)); 405 406 if (logit_softcap != 0.0f) { 407 scale /= logit_softcap; 408 } 409 410 kparams->scale = scale; 411 kparams->max_bias = max_bias; 412 kparams->logit_softcap = logit_softcap; 413 kparams->head_split = (opt_fa_split != 0) ? 1 : 0; 414 kparams->flags = 0; 415 kparams->G = G; 416 417 // team-split only when a team has >1 member (n_kv_heads < N_cores) 418 uint32_t fa_teams = 1, fa_team_size = 1; 419 const uint32_t mdev_count = htp_session_mdev_count(sess); 420 if (opt_fa_split && mdev_count > 1) { 421 htp_fa_mdev_teams(n_kv_heads, mdev_count, kparams->head_split != 0, &fa_teams, &fa_team_size); 422 if (fa_team_size > 1) { 423 kparams->flags |= HTP_FA_FLAG_TEAM_SPLIT; 424 } 425 } 426 427 const uint32_t n_head = q->ne[2]; 428 kparams->n_head_log2 = 1u << (uint32_t) std::floor(std::log2(n_head)); 429 kparams->m0 = std::pow(2.0f, -(max_bias) / kparams->n_head_log2); 430 kparams->m1 = std::pow(2.0f, -(max_bias / 2.0f) / kparams->n_head_log2); 431 432 if (htp_flash_attn_is_hmx_eligible(sess, q, k, v, sinks)) { 433 const uint32_t DK_pad = hex_round_up(DK, 64); 434 const uint32_t DV_pad = hex_round_up(DV, 64); 435 size_t Br = 0, Bc = 0; 436 int ret = hmx_fa_find_chunk_size(&Br, &Bc, G, DK_pad, DV_pad, neq1, nek1, vtcm_size, n_threads, (q->type == GGML_TYPE_F32), sinks != nullptr, n_head, fa_team_size); 437 if (ret == 0) { 438 kparams->kernel_type = HTP_FA_KERNEL_HMX; 439 if (logit_softcap == 0.0f) { 440 kparams->scale = scale * (float) M_LOG2E; 441 kparams->logit_softcap = 0.0f; 442 } else { 443 kparams->scale = scale; 444 kparams->logit_softcap = logit_softcap * (float) M_LOG2E; 445 } 446 kparams->Br = Br; 447 kparams->Bc = Bc; 448 kparams->n_kv_blocks = (nek1 + Bc - 1) / Bc; 449 kparams->n_threads = (kparams->n_kv_blocks >= 3 && n_threads >= 2) ? n_threads : 1; 450 kparams->u.hmx.g_br = hex_align_up(G * Br, 32); 451 kparams->u.hmx.pipeline = (kparams->n_kv_blocks >= 3 && n_threads >= 2) ? 1 : 0; 452 kparams->vtcm_size = hmx_fa_compute_vtcm_usage(G, DK_pad, DV_pad, Br, Bc, kparams->n_threads, kparams->u.hmx.pipeline != 0, (q->type == GGML_TYPE_F32), sinks != nullptr, n_head); 453 kparams->u.hmx.mask_broadcast = (mask != nullptr && mask->ne[2] == 1) ? 1 : 0; 454 kparams->u.hmx.div_G = init_fastdiv_values(G); 455 if (mask) { 456 kparams->src3_div2 = init_fastdiv_values(mask->ne[2]); 457 kparams->src3_div3 = init_fastdiv_values(mask->ne[3]); 458 } 459 return true; 460 } 461 } 462 463 kparams->kernel_type = HTP_FA_KERNEL_HVX; 464 kparams->Br = 1; 465 kparams->Bc = 64; 466 kparams->n_kv_blocks = (k->ne[1] + 64 - 1) / 64; 467 kparams->n_threads = n_threads; 468 469 const size_t size_q_row_padded = hex_round_up(q->ne[0] * ((q->type == GGML_TYPE_F32) ? 4 : 2), 128); 470 const size_t size_k_row_padded = hex_round_up(k->ne[0] * 2, 128); 471 const size_t size_v_row_padded = hex_round_up(v->ne[0] * 2, 128); 472 473 kparams->vtcm_size = hvx_fa_compute_vtcm_usage(DK, DV, (q->type == GGML_TYPE_F32), mask != nullptr, sinks != nullptr, n_head, n_threads); 474 if ((size_t) kparams->vtcm_size > vtcm_size) { 475 HEX_VERBOSE("ggml-hex: skip flash_attn_ext because VTCM needed (%d) > budget (%zu)\n", 476 kparams->vtcm_size, vtcm_size); 477 return false; 478 } 479 480 kparams->u.hvx.size_q_row_padded = size_q_row_padded; 481 kparams->u.hvx.size_k_row_padded = size_k_row_padded; 482 kparams->u.hvx.size_v_row_padded = size_v_row_padded; 483 kparams->u.hvx.src0_div21 = init_fastdiv_values(q->ne[2] * q->ne[1]); 484 kparams->u.hvx.src0_div1 = init_fastdiv_values(q->ne[1]); 485 kparams->broadcast_rk2 = init_fastdiv_values(q->ne[2] / k->ne[2]); 486 kparams->broadcast_rk3 = init_fastdiv_values(q->ne[3] / k->ne[3]); 487 kparams->broadcast_rv2 = init_fastdiv_values(q->ne[2] / v->ne[2]); 488 kparams->broadcast_rv3 = init_fastdiv_values(q->ne[3] / v->ne[3]); 489 if (mask) { 490 kparams->src3_div2 = init_fastdiv_values(mask->ne[2]); 491 kparams->src3_div3 = init_fastdiv_values(mask->ne[3]); 492 } 493 494 return true; 495 } 496 497 static bool htp_kparams_solve_gated_delta_net( 498 const struct ggml_hexagon_session * sess, 499 const struct ggml_tensor * op, 500 struct htp_gdn_kernel_params * kparams 501 ) { 502 if (opt_gdn_select < 1) { 503 return false; 504 } 505 506 memset(kparams, 0, sizeof(*kparams)); 507 508 const struct ggml_tensor * q = op->src[0]; 509 const struct ggml_tensor * k = op->src[1]; 510 const struct ggml_tensor * v = op->src[2]; 511 const struct ggml_tensor * g = op->src[3]; 512 const struct ggml_tensor * beta = op->src[4]; 513 const struct ggml_tensor * state = op->src[5]; 514 const struct ggml_tensor * dst = op; 515 516 if (q->type != GGML_TYPE_F32 || k->type != GGML_TYPE_F32 || v->type != GGML_TYPE_F32 || 517 g->type != GGML_TYPE_F32 || beta->type != GGML_TYPE_F32 || state->type != GGML_TYPE_F32 || 518 dst->type != GGML_TYPE_F32) { 519 return false; 520 } 521 522 if (!ggml_is_contiguous_rows(q) || !ggml_is_contiguous_rows(k) || !ggml_is_contiguous_rows(v) || 523 !ggml_is_contiguous(g) || !ggml_is_contiguous(beta) || !ggml_is_contiguous(state) || 524 !ggml_is_contiguous(dst)) { 525 return false; 526 } 527 528 const int64_t S_v = v->ne[0]; 529 const int64_t H = v->ne[1]; 530 const int64_t n_tokens = v->ne[2]; 531 const int64_t n_seqs = v->ne[3]; 532 const int64_t K = ggml_get_op_params_i32(op, 0); 533 534 if (S_v <= 0 || S_v > 128 || H <= 0 || n_tokens <= 0 || n_seqs <= 0) { 535 return false; 536 } 537 if (q->ne[0] != S_v || k->ne[0] != S_v || q->ne[1] <= 0 || k->ne[1] <= 0 || 538 q->ne[2] != n_tokens || k->ne[2] != n_tokens || q->ne[3] <= 0 || k->ne[3] <= 0 || 539 (n_seqs % q->ne[3]) != 0 || (n_seqs % k->ne[3]) != 0) { 540 return false; 541 } 542 if ((g->ne[0] != 1 && g->ne[0] != S_v) || beta->ne[0] != 1) { 543 return false; 544 } 545 if (state->ne[0] != S_v || state->ne[1] != S_v || state->ne[2] != H || state->ne[3] != n_seqs) { 546 return false; 547 } 548 if (dst->ne[0] != S_v * H || dst->ne[1] != n_tokens * n_seqs + S_v * n_seqs * K || 549 dst->ne[2] != 1 || dst->ne[3] != 1) { 550 return false; 551 } 552 553 const uint32_t rq3 = (uint32_t) (n_seqs / q->ne[3]); 554 const uint32_t rk3 = (uint32_t) (n_seqs / k->ne[3]); 555 const uint32_t total_rows = (uint32_t) (H * n_seqs); 556 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), total_rows); 557 const size_t vtcm_size = htp_session_vtcm_size(sess); 558 559 const bool can_use_hmx = (opt_gdn_select >= 2) && 560 (htp_session_n_hmx(sess) > 0) && 561 (S_v % 64 == 0) && 562 (n_tokens >= HTP_GDN_MIN_TOKENS) && 563 (g->ne[0] == 1) && 564 (K == 1); 565 566 struct htp_gdn_hmx_vtcm_layout hmx_layout; 567 struct htp_gdn_vtcm_layout hvx_layout; 568 uint32_t n_heads_batch = 1; 569 570 if (can_use_hmx && htp_gdn_hmx_solve_layout(&hmx_layout, (uint32_t) S_v, HTP_GDN_CHUNK_SIZE, total_rows, vtcm_size, n_threads, true, &n_heads_batch)) { 571 kparams->kernel_type = HTP_GDN_KERNEL_HMX_CHUNKED; 572 kparams->pipeline = hmx_layout.pipeline ? 1 : 0; 573 kparams->chunk_size = HTP_GDN_CHUNK_SIZE; 574 kparams->n_chunks = ((uint32_t) n_tokens + HTP_GDN_CHUNK_SIZE - 1) / HTP_GDN_CHUNK_SIZE; 575 kparams->n_heads_batch = (uint16_t) n_heads_batch; 576 kparams->vtcm_size = (uint32_t) hmx_layout.total_bytes; 577 } else { 578 htp_gdn_vtcm_layout_build(&hvx_layout, (uint32_t) S_v, n_threads); 579 if (hvx_layout.total_bytes > vtcm_size) { 580 return false; 581 } 582 kparams->kernel_type = HTP_GDN_KERNEL_HVX_RECURRENT; 583 kparams->pipeline = 0; 584 kparams->n_heads_batch = 1; 585 kparams->vtcm_size = (uint32_t) hvx_layout.total_bytes; 586 } 587 588 kparams->n_threads = n_threads; 589 kparams->S_v = (uint32_t) S_v; 590 kparams->H = (uint32_t) H; 591 kparams->n_tokens = (uint32_t) n_tokens; 592 kparams->n_seqs = (uint32_t) n_seqs; 593 kparams->K = (uint32_t) K; 594 kparams->total_rows = total_rows; 595 kparams->kda = (g->ne[0] == S_v) ? 1 : 0; 596 kparams->state_seq_stride = (uint32_t) (state->nb[3] / sizeof(float)); 597 kparams->state_size_per_snap = (uint32_t) (S_v * S_v * H * n_seqs); 598 kparams->scale = 1.0f / sqrtf((float) S_v); 599 600 if (H > 0) kparams->div_H = init_fastdiv_values((uint32_t) H); 601 if (q->ne[1] > 0) kparams->div_q1 = init_fastdiv_values((uint32_t) q->ne[1]); 602 if (k->ne[1] > 0) kparams->div_k1 = init_fastdiv_values((uint32_t) k->ne[1]); 603 if (rq3 > 0) kparams->div_rq3 = init_fastdiv_values(rq3); 604 if (rk3 > 0) kparams->div_rk3 = init_fastdiv_values(rk3); 605 return true; 606 } 607 608 static bool htp_matmul_is_hmx_eligible( 609 const struct ggml_tensor * src0, 610 const struct ggml_tensor * src1, 611 const struct ggml_tensor * dst, 612 bool is_matmul_id, 613 bool is_batched 614 ) { 615 GGML_UNUSED(dst); 616 617 if (src1->type != GGML_TYPE_F32 && (src1->type != GGML_TYPE_F16 || is_matmul_id)) { 618 return false; 619 } 620 621 const int ne00 = src0->ne[0]; 622 const int ne01 = src0->ne[1]; 623 const int ne11 = src1->ne[1]; 624 const int ne12 = src1->ne[2]; 625 const int wtype = src0->type; 626 627 if (ne01 % HTP_TENSOR_TILE_N_ROWS != 0 && is_matmul_id) { 628 return false; 629 } 630 631 if (!htp_is_hmx_weight_type((ggml_type) wtype)) { 632 return false; 633 } 634 635 if (ne00 % HTP_TENSOR_TILE_N_COLS != 0) { 636 return false; 637 } 638 639 const bool is_tiled = htp_tensor_is_tiled(src0); 640 if (ggml_is_quantized((ggml_type) wtype) && !is_tiled) { 641 return false; 642 } 643 if (!is_matmul_id && is_batched && is_tiled) { 644 return false; 645 } 646 647 if (src0->nb[0] > src0->nb[1] || src1->nb[0] > src1->nb[1]) { 648 return false; 649 } 650 651 const int m = is_matmul_id ? ne12 : ne11; 652 if (m <= HTP_MM_HMX_MIN_NROWS) { 653 return false; 654 } 655 656 return true; 657 } 658 659 static bool htp_kparams_solve_hmx_mm( 660 const struct ggml_hexagon_session * sess, 661 const struct ggml_tensor * src0, 662 const struct ggml_tensor * act, 663 const struct ggml_tensor * dst, 664 int wtype, 665 int ne00_padded, 666 int ne01_padded, 667 int ne02, 668 int ne11, 669 int ne12, 670 int ne11_padded, 671 bool is_matmul_id, 672 bool is_batched, 673 size_t src2_size, 674 size_t vtcm_budget, 675 struct htp_mm_kernel_params * kparams 676 ) { 677 const bool is_tiled = htp_tensor_is_tiled(src0); 678 const int aligned_tile_size = is_tiled ? htp_tensor_get_tile_padded_size(wtype) : 0; 679 const int n_threads = (int) htp_session_n_threads(sess); 680 const uint32_t mdev_count = htp_session_mdev_count(sess); 681 const int ne10 = act->ne[0]; 682 683 int m_for_solver = ne11; 684 int m_for_solver_padded = ne11_padded; 685 if (!is_matmul_id && mdev_count > 1 && ((uint32_t) ne11 >= mdev_count)) { 686 const bool dst_row_split = dst ? htp_can_row_partition(dst) : true; 687 const bool act_row_split = htp_can_row_partition(act); 688 if (dst_row_split && act_row_split) { 689 m_for_solver = (ne11 + (int) mdev_count - 1) / (int) mdev_count; 690 m_for_solver_padded = hex_round_up(std::max(m_for_solver, 32), 32); 691 } 692 } 693 const bool pipeline = is_matmul_id ? false : htp_mm_hmx_pipeline(m_for_solver); 694 const bool is_batched_val = is_matmul_id ? false : is_batched; 695 const int group_size = (ne02 > 0 ? ne12 / ne02 : 1); 696 697 size_t m_chunk = 0; 698 size_t n_chunk = 0; 699 size_t vtcm_size = 0; 700 bool use_grouped = false; 701 int act_threads_selected = 0; 702 703 if (is_batched_val && wtype == GGML_TYPE_F16 && !is_tiled && group_size > 1) { 704 if (htp_mm_hmx_solve_batched_params(wtype, ne00_padded, ne01_padded, m_for_solver, group_size, n_threads, pipeline, src2_size, vtcm_budget, &m_chunk, &n_chunk, &act_threads_selected, &vtcm_size)) { 705 use_grouped = true; 706 } 707 } 708 709 if (!use_grouped) { 710 int m_id_rows = 0; 711 if (dst && is_matmul_id) { 712 const int n_experts = ne02 > 0 ? ne02 : 1; 713 const size_t total_expert_rows = (size_t) dst->ne[1] * dst->ne[2]; 714 int m_per_expert = (int) ((total_expert_rows + n_experts - 1) / n_experts); 715 if (mdev_count > 1 && htp_can_row_partition(dst)) { 716 m_per_expert = (m_per_expert + (int) mdev_count - 1) / (int) mdev_count; 717 } 718 m_id_rows = hex_round_up(std::max(m_per_expert, 32), 32); 719 } 720 const uint32_t cost_m = is_matmul_id ? (uint32_t) m_id_rows : (uint32_t) m_for_solver; 721 if (!htp_mm_hmx_solve_2d_params(wtype, ne00_padded, m_id_rows, ne01_padded, m_for_solver_padded, cost_m, n_threads, pipeline, is_matmul_id, aligned_tile_size, src2_size, vtcm_budget, &m_chunk, &n_chunk, &act_threads_selected, &vtcm_size)) { 722 return false; 723 } 724 } 725 726 kparams->n_hmx = 1; 727 kparams->pipeline = pipeline ? 1 : 0; 728 kparams->m_chunk = m_chunk; 729 kparams->n_chunk = n_chunk; 730 kparams->n_threads = n_threads; 731 kparams->n_act_threads = act_threads_selected; 732 kparams->tile_size = is_tiled ? htp_tensor_get_tile_size(wtype) : 0; 733 kparams->aligned_tile_size = aligned_tile_size; 734 kparams->act_row_size = htp_mm_tiled_act_row_size(wtype, ne10); 735 kparams->vtcm_size = vtcm_size; 736 kparams->div_n_act_threads = init_fastdiv_values(act_threads_selected); 737 kparams->div_ne00_padded = init_fastdiv_values(ne00_padded); 738 kparams->vtcm_bias_size = (int32_t) src2_size; 739 kparams->kernel_type = (is_batched && !is_matmul_id) ? HTP_MM_KERNEL_HMX_BATCHED : HTP_MM_KERNEL_HMX_2D; 740 return true; 741 } 742 743 static void htp_kparams_solve_hvx_mm( 744 const struct ggml_hexagon_session * sess, 745 const struct ggml_tensor * src0, 746 const struct ggml_tensor * src1, 747 const struct ggml_tensor * dst, 748 int wtype, 749 bool is_tiled, 750 int ne02, 751 int ne03, 752 int ne10, 753 int ne11, 754 int ne12, 755 int ne13, 756 bool is_matmul_id, 757 const size_t src2_row_size, 758 size_t vtcm_budget, 759 struct htp_mm_kernel_params * kparams 760 ) { 761 if (opt_mm_select < 1) { 762 kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED; 763 return; 764 } 765 766 const uint32_t n_threads = htp_session_n_threads(sess); 767 kparams->n_hmx = 0; 768 kparams->n_threads = n_threads; 769 770 GGML_UNUSED(ne02); 771 GGML_UNUSED(ne03); 772 773 const int src1_nrows = ne11 * ne12 * ne13; 774 775 if (is_tiled) { 776 kparams->tile_size = htp_tensor_get_tile_size(wtype); 777 kparams->aligned_tile_size = htp_tensor_get_tile_padded_size(wtype); 778 779 const bool k_align = (ne10 % htp_tensor_get_tile_k(wtype) == 0); 780 781 if (is_matmul_id) { 782 kparams->kernel_type = (src1_nrows < (int) n_threads) 783 ? HTP_MM_KERNEL_HVX_QUANT_BLOCK : HTP_MM_KERNEL_HVX_QUANT_ROW; 784 kparams->act_row_size = htp_mm_tiled_act_row_size(wtype, ne10); 785 786 struct htp_mm_hvx_vtcm_layout L; 787 uint32_t max_prefetch = (src1_nrows > HTP_MM_HMX_MIN_NROWS) ? 2 : 16; 788 uint32_t best_n_prefetch = 2; 789 for (uint32_t d = max_prefetch; d >= 2; d /= 2) { 790 htp_mm_hvx_vtcm_layout_build( 791 &L, kparams->kernel_type, wtype, ne10, src1_nrows, n_threads, 792 0, src0->nb[1], kparams->act_row_size, 0, d, true, false 793 ); 794 if (L.total_bytes <= vtcm_budget) { 795 best_n_prefetch = d; 796 break; 797 } 798 } 799 if (L.total_bytes > vtcm_budget) { 800 kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED; 801 return; 802 } 803 kparams->n_prefetch = best_n_prefetch; 804 kparams->vtcm_size = L.total_bytes; 805 kparams->vtcm_bias_size = 0; 806 return; 807 } else { 808 if (k_align && opt_mm_select >= 1) { 809 kparams->act_row_size = htp_mm_tiled_act_row_size(wtype, ne10); 810 kparams->kernel_type = (src1_nrows < (int) n_threads) 811 ? HTP_MM_KERNEL_HVX_QUANT_BLOCK : HTP_MM_KERNEL_HVX_QUANT_ROW; 812 813 struct htp_mm_hvx_vtcm_layout L; 814 uint32_t max_prefetch = (src1_nrows > HTP_MM_HMX_MIN_NROWS) ? 2 : 16; 815 uint32_t best_n_prefetch = 2; 816 for (uint32_t d = max_prefetch; d >= 2; d /= 2) { 817 htp_mm_hvx_vtcm_layout_build( 818 &L, kparams->kernel_type, wtype, ne10, src1_nrows, n_threads, 819 dst->nb[1], src0->nb[1], src1->nb[1], src2_row_size, d, false, false 820 ); 821 if (L.total_bytes <= vtcm_budget) { 822 best_n_prefetch = d; 823 break; 824 } 825 } 826 827 uint32_t m_chunk = 0; 828 if (htp_mm_hvx_solve_vtcm_params( 829 kparams->kernel_type, wtype, ne10, src1_nrows, n_threads, 830 dst->nb[1], src0->nb[1], src1->nb[1], src2_row_size, best_n_prefetch, vtcm_budget, 831 &L, &m_chunk)) { 832 kparams->n_prefetch = best_n_prefetch; 833 kparams->m_chunk = (m_chunk < (uint32_t) src1_nrows) ? m_chunk : 0; 834 kparams->vtcm_size = L.total_bytes; 835 kparams->vtcm_bias_size = L.bias_bytes; 836 return; 837 } 838 } 839 840 kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED; 841 return; 842 } 843 } else if (is_matmul_id) { 844 kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED; 845 return; 846 } else if (wtype == GGML_TYPE_F16 || wtype == GGML_TYPE_F32 || wtype == GGML_TYPE_BF16) { 847 int32_t ktype = HTP_MM_KERNEL_HVX_F16_F16_VTCM; 848 int32_t act_elem = 2; 849 if (wtype == GGML_TYPE_F32) { 850 ktype = HTP_MM_KERNEL_HVX_F32_F32_VTCM; 851 act_elem = 4; 852 } else if (wtype == GGML_TYPE_BF16) { 853 ktype = HTP_MM_KERNEL_HVX_BF16_F32_VTCM; 854 act_elem = 4; 855 } 856 struct htp_mm_hvx_vtcm_layout L; 857 uint32_t m_chunk = 0; 858 if (htp_mm_hvx_solve_vtcm_params( 859 ktype, wtype, ne10, src1_nrows, n_threads, 860 dst->nb[1], src0->nb[1], src1->nb[1], src2_row_size, 16, vtcm_budget, 861 &L, &m_chunk)) { 862 kparams->kernel_type = ktype; 863 kparams->m_chunk = (m_chunk < (uint32_t) src1_nrows) ? m_chunk : 0; 864 kparams->act_row_size = hex_round_up(ne10 * act_elem, 128); 865 kparams->vtcm_size = L.total_bytes; 866 kparams->vtcm_bias_size = L.bias_bytes; 867 kparams->n_prefetch = 16; 868 return; 869 } 870 } 871 872 kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED; 873 } 874 875 static bool htp_kparams_solve_matmul_impl( 876 const struct ggml_hexagon_session * sess, 877 const struct ggml_tensor * src0, 878 const struct ggml_tensor * src1, 879 const struct ggml_tensor * dst, 880 const size_t src2_row_size, 881 const size_t src2_size, 882 struct htp_mm_kernel_params * kparams 883 ) { 884 memset(kparams, 0, sizeof(*kparams)); 885 886 const int ne00 = src0->ne[0]; 887 const int ne01 = src0->ne[1]; 888 const int ne02 = src0->ne[2]; 889 const int ne03 = src0->ne[3]; 890 891 const int ne10 = src1->ne[0]; 892 const int ne11 = src1->ne[1]; 893 const int ne12 = src1->ne[2]; 894 const int ne13 = src1->ne[3]; 895 896 const bool is_tiled = htp_tensor_is_tiled(src0); 897 const int wtype = (is_tiled && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_BF16)) ? GGML_TYPE_F16 : src0->type; 898 const int ne00_padded = is_tiled ? hex_round_up(ne00, htp_tensor_get_tile_k(wtype)) : ne00; 899 const int ne11_padded = hex_round_up(ne11, 32); 900 const int ne01_tiled = hex_round_up(ne01, 32); 901 902 const bool is_matmul_id = (dst->op == GGML_OP_MUL_MAT_ID); 903 const bool is_batched = (ne02 * ne03 > 1 || ne12 * ne13 > 1); 904 const size_t vtcm_budget = htp_session_vtcm_size(sess); 905 906 const bool hmx_enabled = (htp_session_n_hmx(sess) > 0) && (opt_mm_select >= 2); 907 if (hmx_enabled && htp_matmul_is_hmx_eligible(src0, src1, dst, is_matmul_id, is_batched)) { 908 if (htp_kparams_solve_hmx_mm(sess, src0, src1, dst, wtype, ne00_padded, ne01_tiled, ne02, ne11, ne12, ne11_padded, is_matmul_id, is_batched, src2_size, vtcm_budget, kparams)) { 909 goto finalize; 910 } 911 } 912 913 htp_kparams_solve_hvx_mm(sess, src0, src1, dst, wtype, is_tiled, ne02, ne03, ne10, ne11, ne12, ne13, is_matmul_id, src2_row_size, vtcm_budget, kparams); 914 915 finalize: 916 kparams->div_ne12_ne1 = init_fastdiv_values(ne12 * ne11); 917 kparams->div_ne1 = init_fastdiv_values(ne11); 918 kparams->div_r2 = init_fastdiv_values(ne02 > 0 ? ne12 / ne02 : 1); 919 kparams->div_r3 = init_fastdiv_values(ne03 > 0 ? ne13 / ne03 : 1); 920 kparams->div_ne12 = init_fastdiv_values(ne12); 921 922 if (kparams->kernel_type == HTP_MM_KERNEL_UNSUPPORTED || (size_t) kparams->vtcm_size > vtcm_budget) { 923 HEX_VERBOSE("ggml-hex: %s supported %s VTCM size needed (%d) > budget (%zu)\n", 924 htp_session_name(sess), is_matmul_id ? "MUL_MAT_ID" : "MUL_MAT", kparams->vtcm_size, vtcm_budget); 925 return false; 926 } 927 return true; 928 } 929 930 static bool htp_matmul_check_types( 931 const struct ggml_tensor * src0, 932 const struct ggml_tensor * src1, 933 const struct ggml_tensor * dst 934 ) { 935 const bool is_matmul_id = (dst->op == GGML_OP_MUL_MAT_ID); 936 937 if (dst->type != GGML_TYPE_F32) { 938 return false; 939 } 940 941 if (is_matmul_id) { 942 const struct ggml_tensor * src2 = dst->src[2]; 943 if (src1->type != GGML_TYPE_F32 || !src2 || src2->type != GGML_TYPE_I32) { 944 return false; 945 } 946 if (ggml_get_op_params_i32(dst, 3) == GGML_PREC_F32) { 947 return false; 948 } 949 switch (src0->type) { 950 case GGML_TYPE_Q1_0: 951 case GGML_TYPE_Q2_0: 952 case GGML_TYPE_Q4_0: 953 case GGML_TYPE_Q4_1: 954 case GGML_TYPE_Q8_0: 955 case GGML_TYPE_IQ4_NL: 956 case GGML_TYPE_MXFP4: 957 case GGML_TYPE_Q4_K: 958 case GGML_TYPE_Q5_K: 959 case GGML_TYPE_Q6_K: 960 case GGML_TYPE_Q3_K: 961 case GGML_TYPE_Q2_K: 962 if (!ggml_is_contiguous(src0) || ggml_is_permuted(src0) || ggml_is_view(src0)) { 963 return false; 964 } 965 if (src0->ne[0] % ggml_blck_size(src0->type) != 0) { 966 return false; 967 } 968 return true; 969 970 case GGML_TYPE_F16: 971 case GGML_TYPE_F32: 972 case GGML_TYPE_BF16: 973 if (!ggml_is_contiguous(src0) || ggml_is_permuted(src0)) { 974 return false; 975 } 976 return src0->ne[0] % 32 == 0; 977 978 default: 979 return false; 980 } 981 } 982 983 if (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) { 984 return false; 985 } 986 if (src1->ne[2] < src0->ne[2] || src1->ne[3] < src0->ne[3] || 987 src1->ne[2] % src0->ne[2] != 0 || src1->ne[3] % src0->ne[3] != 0) { 988 return false; 989 } 990 991 switch (src0->type) { 992 case GGML_TYPE_Q1_0: 993 case GGML_TYPE_Q2_0: 994 case GGML_TYPE_Q4_0: 995 case GGML_TYPE_Q4_1: 996 case GGML_TYPE_Q8_0: 997 case GGML_TYPE_IQ4_NL: 998 case GGML_TYPE_MXFP4: 999 case GGML_TYPE_Q4_K: 1000 case GGML_TYPE_Q5_K: 1001 case GGML_TYPE_Q6_K: 1002 case GGML_TYPE_Q3_K: 1003 case GGML_TYPE_Q2_K: 1004 if (!ggml_is_contiguous(src0) || ggml_is_permuted(src0) || ggml_is_view(src0)) { 1005 return false; 1006 } 1007 return src0->ne[0] % ggml_blck_size(src0->type) == 0; 1008 1009 case GGML_TYPE_F32: 1010 if (src1->type != GGML_TYPE_F32) { 1011 return false; 1012 } 1013 return src0->nb[1] >= src0->nb[0]; 1014 1015 case GGML_TYPE_F16: 1016 case GGML_TYPE_BF16: 1017 return src0->nb[1] >= src0->nb[0]; 1018 1019 default: 1020 return false; 1021 } 1022 } 1023 1024 static bool htp_kparams_solve_matmul( 1025 const struct ggml_hexagon_session * sess, 1026 const struct ggml_tensor * op, 1027 struct htp_mm_kernel_params * kparams 1028 ) { 1029 const struct ggml_tensor * src0 = op->src[0]; 1030 const struct ggml_tensor * src1 = op->src[1]; 1031 const struct ggml_tensor * dst = op; 1032 1033 if (!htp_matmul_check_types(src0, src1, dst)) { 1034 return false; 1035 } 1036 if (htp_matmul_can_collapse(src0, src1, dst)) { 1037 const ggml_tensor src1_collapsed = htp_tensor_collapse_rows(src1); 1038 const ggml_tensor dst_collapsed = htp_tensor_collapse_rows(dst); 1039 return htp_kparams_solve_matmul_impl(sess, src0, &src1_collapsed, &dst_collapsed, 0, 0, kparams); 1040 } 1041 return htp_kparams_solve_matmul_impl(sess, src0, src1, dst, 0, 0, kparams); 1042 } 1043 1044 bool htp_matmul_add_can_collapse( 1045 const struct ggml_tensor * src0, 1046 const struct ggml_tensor * src1, 1047 const struct ggml_tensor * src2, 1048 const struct ggml_tensor * dst 1049 ) { 1050 if (!htp_matmul_can_collapse(src0, src1, dst)) { 1051 return false; 1052 } 1053 if (!src2) { 1054 return true; 1055 } 1056 const int64_t src2_nrows = src2->ne[1] * src2->ne[2] * src2->ne[3]; 1057 if (src2_nrows == 1) { 1058 return true; 1059 } 1060 size_t s2; 1061 return src2->nb[0] == ggml_type_size(src2->type) && 1062 ggml_are_same_shape(src2, dst) && 1063 htp_rows_stride(src2->ne, src2->nb, &s2); 1064 } 1065 1066 bool htp_kparams_solve_matmul_add( 1067 const struct ggml_hexagon_session * sess, 1068 const struct ggml_tensor * src0, 1069 const struct ggml_tensor * src1, 1070 const struct ggml_tensor * src2, 1071 const struct ggml_tensor * dst, 1072 struct htp_mm_kernel_params * kparams 1073 ) { 1074 if (htp_matmul_add_can_collapse(src0, src1, src2, dst)) { 1075 const ggml_tensor src1_collapsed = htp_tensor_collapse_rows(src1); 1076 const ggml_tensor dst_collapsed = htp_tensor_collapse_rows(dst); 1077 const ggml_tensor src2_collapsed = (src2 && (src2->ne[1] * src2->ne[2] * src2->ne[3] > 1)) 1078 ? htp_tensor_collapse_rows(src2) 1079 : (src2 ? *src2 : ggml_tensor{}); 1080 const struct ggml_tensor * p_src2 = src2 ? &src2_collapsed : nullptr; 1081 const size_t src2_size = p_src2 ? hex_round_up(ggml_nbytes(p_src2), 128) : 0; 1082 return htp_kparams_solve_matmul_impl(sess, src0, &src1_collapsed, &dst_collapsed, p_src2 ? p_src2->nb[1] : 0, src2_size, kparams); 1083 } 1084 const size_t src2_size = src2 ? hex_round_up(ggml_nbytes(src2), 128) : 0; 1085 return htp_kparams_solve_matmul_impl(sess, src0, src1, dst, src2 ? src2->nb[1] : 0, src2_size, kparams); 1086 } 1087 1088 bool htp_kparams_solve_mmnx( 1089 const struct ggml_hexagon_session * sess, 1090 const struct ggml_tensor * src0, 1091 const struct ggml_tensor * act, 1092 int32_t n_weights, 1093 struct htp_mm_kernel_params * kparams 1094 ) { 1095 const uint32_t n_threads = htp_session_n_threads(sess); 1096 const uint32_t n_hmx = htp_session_n_hmx(sess); 1097 const size_t vtcm_budget = htp_session_vtcm_size(sess); 1098 1099 memset(kparams, 0, sizeof(*kparams)); 1100 kparams->n_threads = n_threads; 1101 1102 const int ne00 = src0->ne[0]; 1103 const int ne01 = src0->ne[1]; 1104 const int ne02 = src0->ne[2]; 1105 const int ne03 = src0->ne[3]; 1106 1107 const int ne10 = act->ne[0]; 1108 const int ne11 = act->ne[1]; 1109 const int ne12 = act->ne[2]; 1110 const int ne13 = act->ne[3]; 1111 1112 const bool is_tiled = htp_tensor_is_tiled(src0); 1113 const int wtype = (is_tiled && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_BF16)) ? GGML_TYPE_F16 : src0->type; 1114 const int ne00_padded = is_tiled ? hex_round_up(ne00, 32) : ne00; 1115 const int ne11_padded = hex_round_up(ne11, 32); 1116 const int ne01_tiled = hex_round_up(ne01, 32); 1117 const bool is_batched = (ne02 * ne03 > 1 || ne12 * ne13 > 1); 1118 1119 const bool hmx_enabled = (n_hmx > 0) && (opt_mm_select >= 2); 1120 if (hmx_enabled && htp_matmul_is_hmx_eligible(src0, act, nullptr, false, is_batched)) { 1121 if (htp_kparams_solve_hmx_mm(sess, src0, act, nullptr, wtype, ne00_padded, ne01_tiled, ne02, ne11, ne12, ne11_padded, false, is_batched, 0, vtcm_budget, kparams)) { 1122 kparams->n_weights = n_weights; 1123 goto finalize; 1124 } 1125 } 1126 1127 if (!is_tiled || act->type != GGML_TYPE_F32 || ne10 % 32 != 0) { 1128 kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED; 1129 return false; 1130 } 1131 1132 { 1133 const int act_nrows = ne11 * ne12 * ne13; 1134 const size_t act_row_size = htp_mm_tiled_act_row_size(wtype, ne10); 1135 const size_t src0_row_size = src0->nb[1]; 1136 1137 const uint32_t max_prefetch = (act_nrows > HTP_MM_HMX_MIN_NROWS) ? 2 : 16; 1138 uint32_t best_n_prefetch = 2; 1139 for (uint32_t d = max_prefetch; d >= 2; d /= 2) { 1140 struct htp_mm_hvx_vtcm_layout L; 1141 htp_mm_hvx_vtcm_layout_build( 1142 &L, HTP_MM_KERNEL_HVX_QUANT_ROW, wtype, ne10, act_nrows, n_threads, 1143 0, src0_row_size, act_row_size, 0, d, false, true 1144 ); 1145 if (L.total_bytes <= vtcm_budget) { 1146 best_n_prefetch = d; 1147 break; 1148 } 1149 } 1150 1151 struct htp_mm_hvx_vtcm_layout L; 1152 htp_mm_hvx_vtcm_layout_build( 1153 &L, HTP_MM_KERNEL_HVX_QUANT_ROW, wtype, ne10, act_nrows, n_threads, 1154 0, src0_row_size, act_row_size, 0, best_n_prefetch, false, true 1155 ); 1156 1157 if (opt_mm_select >= 1 && L.total_bytes <= vtcm_budget) { 1158 kparams->kernel_type = HTP_MM_KERNEL_HVX_QUANT_ROW; 1159 kparams->tile_size = htp_tensor_get_tile_size(wtype); 1160 kparams->aligned_tile_size = htp_tensor_get_tile_padded_size(wtype); 1161 kparams->act_row_size = act_row_size; 1162 kparams->vtcm_bias_size = 0; 1163 kparams->vtcm_size = L.total_bytes; 1164 kparams->n_prefetch = best_n_prefetch; 1165 kparams->n_weights = n_weights; 1166 } else { 1167 kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED; 1168 return false; 1169 } 1170 } 1171 1172 finalize: 1173 kparams->div_ne12_ne1 = init_fastdiv_values(ne12 * ne11); 1174 kparams->div_ne1 = init_fastdiv_values(ne11); 1175 kparams->div_r2 = init_fastdiv_values(ne02 > 0 ? ne12 / ne02 : 1); 1176 kparams->div_r3 = init_fastdiv_values(ne03 > 0 ? ne13 / ne03 : 1); 1177 kparams->div_ne12 = init_fastdiv_values(ne12); 1178 return (size_t) kparams->vtcm_size <= vtcm_budget; 1179 } 1180 1181 bool htp_kparams_solve_mmidnx( 1182 const struct ggml_hexagon_session * sess, 1183 const struct ggml_tensor * src0, 1184 const struct ggml_tensor * act, 1185 const struct ggml_tensor * dst, 1186 int32_t n_weights, 1187 struct htp_mm_kernel_params * kparams 1188 ) { 1189 const bool ok = htp_kparams_solve_matmul_impl(sess, src0, act, dst, 0, 0, kparams); 1190 kparams->n_weights = n_weights; 1191 return ok; 1192 } 1193 1194 static bool htp_kparams_solve_binary( 1195 const struct ggml_hexagon_session * sess, 1196 const struct ggml_tensor * op_tensor, 1197 struct htp_binary_kernel_params * kparams 1198 ) { 1199 memset(kparams, 0, sizeof(*kparams)); 1200 1201 const uint32_t op = htp_op_remap(op_tensor); 1202 const struct ggml_tensor * src0 = op_tensor->src[0]; 1203 const struct ggml_tensor * src1 = op_tensor->src[1]; 1204 const struct ggml_tensor * src2 = (op == HTP_OP_ADD_ID) ? op_tensor->src[2] : nullptr; 1205 const struct ggml_tensor * dst = op_tensor; 1206 1207 const bool is_add_id = (op == HTP_OP_ADD_ID); 1208 if (is_add_id) { 1209 if (!src2) { 1210 return false; 1211 } 1212 if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32 || src2->type != GGML_TYPE_I32) { 1213 return false; 1214 } 1215 if (!ggml_are_same_shape(src0, dst)) { 1216 return false; 1217 } 1218 if (src1->ne[0] != src0->ne[0] || src1->ne[2] != 1 || src1->ne[3] != 1) { 1219 return false; 1220 } 1221 if (src2->ne[0] != src0->ne[1] || src2->ne[1] != src0->ne[2]) { 1222 return false; 1223 } 1224 if (src0->nb[0] != sizeof(float) || src1->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float) || src2->nb[0] != sizeof(int32_t)) { 1225 return false; 1226 } 1227 if (!ggml_is_contiguous(src0) || !ggml_is_contiguous(src1) || !ggml_is_contiguous(dst)) { 1228 return false; 1229 } 1230 if (src2->nb[1] < src2->ne[0] * sizeof(int32_t)) { 1231 return false; 1232 } 1233 } else { 1234 if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) || 1235 src1->type != src0->type || dst->type != src0->type) { 1236 return false; 1237 } 1238 if (ggml_is_permuted(src0) || ggml_is_permuted(dst) || !ggml_are_same_shape(src0, dst)) { 1239 return false; 1240 } 1241 if (!ggml_can_repeat(src1, src0) || ggml_is_permuted(src1)) { 1242 return false; 1243 } 1244 } 1245 1246 const uint32_t max_threads_sess = htp_session_n_threads(sess); 1247 const size_t vtcm_size = htp_session_vtcm_size(sess); 1248 1249 const size_t elem_size = ggml_type_size(src0->type); 1250 const size_t src0_row_size = src0->ne[0] * elem_size; 1251 const size_t src1_row_size = src1->ne[0] * elem_size; 1252 const size_t dst_row_size = dst->ne[0] * elem_size; 1253 1254 const size_t src0_row_size_aligned = hex_round_up(src0_row_size, 128); 1255 const size_t src1_row_size_aligned = hex_round_up(src1_row_size, 128); 1256 const size_t dst_row_size_aligned = hex_round_up(dst_row_size, 128); 1257 1258 if (!htp_binary_strides_safe(src0, src1, dst, elem_size)) { 1259 return false; 1260 } 1261 1262 const bool is_scalar = !is_add_id && src1->ne[0] == 1; 1263 const bool is_row_bcast = !is_add_id && !is_scalar && 1264 src1->ne[0] == src0->ne[0] && 1265 (src0->ne[1] > 1 || src0->ne[2] > 1 || src0->ne[3] > 1) && 1266 src1->ne[1] == 1 && src1->ne[2] == 1 && src1->ne[3] == 1; 1267 const bool is_same_shape = !is_add_id && !is_scalar && 1268 src1->ne[0] == src0->ne[0] && 1269 src1->ne[1] == src0->ne[1] && 1270 (src1->ne[2] == src0->ne[2] || src1->ne[2] == 1) && 1271 (src1->ne[3] == src0->ne[3] || src1->ne[3] == 1); 1272 const bool is_complex = !is_add_id && !is_scalar && !is_same_shape && !is_row_bcast && (src1->ne[0] == src0->ne[0]); 1273 const bool is_contig = ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst); 1274 const bool is_scalar_broadcast = !is_add_id && (ggml_nelements(src1) == 1); 1275 1276 if (!is_add_id && is_contig && (ggml_are_same_shape(src0, src1) || is_scalar_broadcast)) { 1277 const uint32_t total_elems = (uint32_t) ggml_nelements(src0); 1278 const uint32_t max_chunk_elems = 32768 / elem_size; 1279 const uint32_t min_chunk_elems = 256; 1280 const uint32_t target_chunk_elems = hex_round_up((total_elems + (2 * max_threads_sess) - 1) / (2 * max_threads_sess), 32); 1281 const uint32_t chunk_size = (std::min)(max_chunk_elems, (std::max)(target_chunk_elems, min_chunk_elems)); 1282 const uint32_t chunk_bytes = hex_round_up(chunk_size * elem_size, 128); 1283 const uint32_t total_chunks = (total_elems + chunk_size - 1) / chunk_size; 1284 const uint32_t n_threads = (total_chunks >= 2) ? (std::min)(max_threads_sess, total_chunks) : 1; 1285 1286 kparams->kernel_type = HTP_BINARY_KERNEL_CHUNKED; 1287 kparams->n_threads = n_threads; 1288 kparams->n_srcs = 2; 1289 kparams->rows_per_buffer = 1; 1290 kparams->src0_row_size_aligned = src0_row_size_aligned; 1291 kparams->src1_row_size_aligned = is_scalar_broadcast ? 0 : src1_row_size_aligned; 1292 kparams->dst_row_size_aligned = dst_row_size_aligned; 1293 kparams->src1_size = 0; 1294 kparams->chunk_size = chunk_size; 1295 kparams->chunk_bytes = chunk_bytes; 1296 kparams->is_scalar = is_scalar_broadcast ? 1 : 0; 1297 1298 struct htp_binary_vtcm_layout L; 1299 htp_binary_vtcm_layout_build(&L, kparams, vtcm_size); 1300 if (L.total_bytes == 0 || L.total_bytes > vtcm_size) { 1301 return false; 1302 } 1303 1304 kparams->vtcm_size = L.total_bytes; 1305 return true; 1306 } 1307 1308 enum htp_binary_kernel_type kernel_type; 1309 size_t src1_size = 0; 1310 size_t src2_size = 0; 1311 1312 if (is_add_id) { 1313 kernel_type = HTP_BINARY_KERNEL_ADD_ID; 1314 src1_size = hex_round_up(src1->ne[1] * src1_row_size_aligned, 128); 1315 src2_size = hex_round_up(ggml_nelements(src2) * sizeof(int32_t), 128); 1316 if (src1_size + src2_size > vtcm_size) { 1317 return false; 1318 } 1319 } else if (is_row_bcast) { 1320 kernel_type = HTP_BINARY_KERNEL_ROW_BCAST; 1321 src1_size = src1_row_size_aligned; 1322 } else if (is_scalar) { 1323 src1_size = hex_round_up(ggml_nelements(src1) * elem_size, 128); 1324 if (src1_size > vtcm_size) { 1325 return false; 1326 } 1327 kernel_type = HTP_BINARY_KERNEL_SCALAR; 1328 kparams->src1_strided = !ggml_is_contiguous(src1) ? 1 : 0; 1329 } else if (is_same_shape) { 1330 kernel_type = HTP_BINARY_KERNEL_SAME_SHAPE; 1331 } else if (is_complex) { 1332 src1_size = hex_round_up(ggml_nelements(src1) * elem_size + 128, 128); 1333 if (src1_size > vtcm_size) { 1334 return false; 1335 } 1336 kernel_type = HTP_BINARY_KERNEL_COMPLEX; 1337 kparams->src1_strided = !ggml_is_contiguous(src1) ? 1 : 0; 1338 } else { 1339 src1_size = hex_round_up(ggml_nelements(src1) * elem_size + 128, 128); 1340 if (src1_size > vtcm_size) { 1341 return false; 1342 } 1343 kernel_type = HTP_BINARY_KERNEL_REPEAT; 1344 kparams->src1_strided = !ggml_is_contiguous(src1) ? 1 : 0; 1345 } 1346 1347 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 1348 const uint32_t min_rows_per_thread = (uint32_t) (std::max(1, (128 + dst_row_size - 1) / dst_row_size)); 1349 const uint32_t max_threads = (std::max)(1u, src0_nrows / min_rows_per_thread); 1350 1351 kparams->kernel_type = kernel_type; 1352 kparams->n_threads = (std::min)(max_threads_sess, max_threads); 1353 kparams->n_srcs = 2; 1354 kparams->src0_row_size_aligned = src0_row_size_aligned; 1355 kparams->src1_row_size_aligned = src1_row_size_aligned; 1356 kparams->dst_row_size_aligned = dst_row_size_aligned; 1357 kparams->src1_size = src1_size; 1358 kparams->src2_size = src2_size; 1359 1360 const bool src0_contig_dim1 = (src0->nb[2] == src0->ne[1] * src0->nb[1]); 1361 const bool src1_contig_dim1 = (src1->nb[2] == src1->ne[1] * src1->nb[1]); 1362 const bool dst_contig_dim1 = (dst->nb[2] == src0->ne[1] * dst->nb[1]); 1363 1364 const bool src0_contig_dim2 = (src0->nb[3] == src0->ne[2] * src0->nb[2]); 1365 const bool src1_contig_dim2 = (src1->nb[3] == src1->ne[2] * src1->nb[2]); 1366 const bool dst_contig_dim2 = (dst->nb[3] == src0->ne[2] * dst->nb[2]); 1367 1368 bool split_at_ne01 = false; 1369 if (src0->ne[2] > 1) { 1370 if (kernel_type == HTP_BINARY_KERNEL_SAME_SHAPE) { 1371 split_at_ne01 = (src1->ne[2] != src0->ne[2]) || !src0_contig_dim1 || !src1_contig_dim1 || !dst_contig_dim1; 1372 } else if (kernel_type == HTP_BINARY_KERNEL_ROW_BCAST) { 1373 split_at_ne01 = !src0_contig_dim1 || !dst_contig_dim1; 1374 } else { 1375 split_at_ne01 = true; 1376 } 1377 } 1378 kparams->split_at_ne01 = split_at_ne01 ? 1 : 0; 1379 1380 bool split_at_ne02 = false; 1381 if (src0->ne[3] > 1) { 1382 if (kernel_type == HTP_BINARY_KERNEL_SAME_SHAPE) { 1383 split_at_ne02 = (src1->ne[3] != src0->ne[3]) || !src0_contig_dim2 || !src1_contig_dim2 || !dst_contig_dim2; 1384 } else if (kernel_type == HTP_BINARY_KERNEL_ROW_BCAST) { 1385 split_at_ne02 = !src0_contig_dim2 || !dst_contig_dim2; 1386 } else { 1387 split_at_ne02 = true; 1388 } 1389 } 1390 kparams->split_at_ne02 = split_at_ne02 ? 1 : 0; 1391 1392 kparams->div_ne01 = init_fastdiv_values((uint32_t) src0->ne[1]); 1393 kparams->div_ne02 = init_fastdiv_values((uint32_t) src0->ne[2]); 1394 kparams->div_ne012 = init_fastdiv_values((uint32_t) (src0->ne[1] * src0->ne[2])); 1395 kparams->div_ne11 = init_fastdiv_values((uint32_t) src1->ne[1]); 1396 kparams->div_ne12 = init_fastdiv_values((uint32_t) src1->ne[2]); 1397 kparams->div_ne13 = init_fastdiv_values((uint32_t) src1->ne[3]); 1398 1399 struct htp_binary_vtcm_layout L; 1400 htp_binary_vtcm_layout_build(&L, kparams, vtcm_size); 1401 if (L.rows_per_buffer == 0 || L.total_bytes > vtcm_size) { 1402 if (!is_add_id && is_contig && (ggml_are_same_shape(src0, src1) || is_scalar_broadcast)) { 1403 const uint32_t total_elems = (uint32_t) ggml_nelements(src0); 1404 return htp_binary_solve_chunked(&L, kparams, total_elems, (uint32_t) elem_size, 1405 max_threads_sess, 2, is_scalar_broadcast, vtcm_size); 1406 } 1407 return false; 1408 } 1409 1410 kparams->rows_per_buffer = L.rows_per_buffer; 1411 kparams->vtcm_size = L.total_bytes; 1412 1413 return true; 1414 } 1415 1416 // collapse contiguous dims of elementwise ops so that rows fill at least one HVX vector 1417 static void htp_collapse_dims(struct ggml_tensor ** tensors, size_t n_tensors, size_t elem_size, uint32_t n_threads) { 1418 if (n_tensors == 0 || !tensors[0]) { 1419 return; 1420 } 1421 for (size_t t = 0; t < n_tensors; t++) { 1422 if (!tensors[t] || tensors[t]->nb[0] != elem_size) { 1423 return; 1424 } 1425 if (t > 0 && !ggml_are_same_shape(tensors[0], tensors[t])) { 1426 return; 1427 } 1428 } 1429 1430 struct ggml_tensor * t0 = tensors[0]; 1431 1432 while (t0->ne[1] > 1 || t0->ne[2] > 1 || t0->ne[3] > 1) { 1433 const size_t row_size = (size_t) t0->ne[0] * elem_size; 1434 bool rows_contig = true; 1435 for (size_t t = 0; t < n_tensors; t++) { 1436 if (tensors[t]->nb[1] != (size_t) tensors[t]->ne[0] * elem_size) { 1437 rows_contig = false; 1438 break; 1439 } 1440 } 1441 bool all_ne1_one = true; 1442 for (size_t t = 0; t < n_tensors; t++) { 1443 if (tensors[t]->ne[1] != 1) { 1444 all_ne1_one = false; 1445 break; 1446 } 1447 } 1448 const bool can_merge = all_ne1_one || 1449 (rows_contig && (t0->ne[0] == 1 || (row_size < 128 && row_size * t0->ne[1] <= 2048))); 1450 if (!can_merge) { 1451 break; 1452 } 1453 for (size_t t = 0; t < n_tensors; t++) { 1454 tensors[t]->ne[0] *= tensors[t]->ne[1]; 1455 tensors[t]->ne[1] = tensors[t]->ne[2]; 1456 tensors[t]->ne[2] = tensors[t]->ne[3]; 1457 tensors[t]->ne[3] = 1; 1458 tensors[t]->nb[1] = tensors[t]->nb[2]; 1459 tensors[t]->nb[2] = tensors[t]->nb[3]; 1460 tensors[t]->nb[3] = tensors[t]->nb[2] * tensors[t]->ne[2]; 1461 } 1462 } 1463 1464 // ne1 too big to merge in full: merge it in chunks of k rows (k divides ne1) 1465 const size_t row_size = (size_t) t0->ne[0] * elem_size; 1466 bool rows_contig = true; 1467 for (size_t t = 0; t < n_tensors; t++) { 1468 if (tensors[t]->nb[1] != row_size) { 1469 rows_contig = false; 1470 break; 1471 } 1472 } 1473 if (t0->ne[1] > 1 && row_size < 128 && rows_contig) { 1474 int64_t k = 2048 / row_size; 1475 while (k > 1 && t0->ne[1] % k != 0) { 1476 k--; 1477 } 1478 if (k > 1) { 1479 for (size_t t = 0; t < n_tensors; t++) { 1480 tensors[t]->ne[0] *= k; 1481 tensors[t]->ne[1] /= k; 1482 tensors[t]->nb[1] *= k; 1483 tensors[t]->nb[2] = tensors[t]->nb[1] * tensors[t]->ne[1]; 1484 tensors[t]->nb[3] = tensors[t]->nb[2] * tensors[t]->ne[2]; 1485 } 1486 } 1487 } 1488 1489 // nrows too small to saturate threads: split ne0 into k rows (k divides ne0) 1490 const size_t row_size_curr = (size_t) t0->ne[0] * elem_size; 1491 const uint32_t nrows = (uint32_t) (t0->ne[1] * t0->ne[2] * t0->ne[3]); 1492 rows_contig = true; 1493 for (size_t t = 0; t < n_tensors; t++) { 1494 if (tensors[t]->ne[1] != 1 && tensors[t]->nb[1] != row_size_curr) { 1495 rows_contig = false; 1496 break; 1497 } 1498 } 1499 if (nrows < n_threads && rows_contig && row_size_curr >= 4096) { 1500 const size_t vec_elems = 128 / elem_size; 1501 int64_t k = std::max(row_size_curr / 16384, (n_threads * 4 + nrows - 1) / nrows); 1502 k = std::min(k, row_size_curr / 2048); 1503 while (k > 1 && t0->ne[0] % (k * vec_elems) != 0) { 1504 k--; 1505 } 1506 if (k > 1) { 1507 for (size_t t = 0; t < n_tensors; t++) { 1508 tensors[t]->ne[0] /= k; 1509 tensors[t]->ne[1] *= k; 1510 tensors[t]->nb[1] = tensors[t]->ne[0] * elem_size; 1511 tensors[t]->nb[2] = tensors[t]->nb[1] * tensors[t]->ne[1]; 1512 tensors[t]->nb[3] = tensors[t]->nb[2] * tensors[t]->ne[2]; 1513 } 1514 } 1515 } 1516 } 1517 1518 static bool htp_kparams_solve_unary_impl( 1519 const struct ggml_hexagon_session * sess, 1520 uint32_t op, 1521 const struct ggml_tensor * src0, 1522 const struct ggml_tensor * src1, 1523 const struct ggml_tensor * dst, 1524 struct htp_unary_kernel_params * kparams 1525 ) { 1526 memset(kparams, 0, sizeof(*kparams)); 1527 1528 if (!htp_op_is_unary((htp_op_code) op)) { 1529 return false; 1530 } 1531 1532 if (op == HTP_OP_HADAMARD) { 1533 const int64_t n = src0->ne[0]; 1534 if (n < 32 || (n & (n - 1)) != 0) { 1535 return false; 1536 } 1537 if (!dst->src[0] || dst->src[0]->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32 || 1538 (src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16)) { 1539 return false; 1540 } 1541 } else { 1542 if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) || dst->type != src0->type) { 1543 return false; 1544 } 1545 if (op == HTP_OP_TRI && (src0->type != GGML_TYPE_F32 || !ggml_is_contiguous(src0))) { 1546 return false; 1547 } 1548 if (src0->type == GGML_TYPE_F16) { 1549 switch (op) { 1550 case HTP_OP_NORM: 1551 case HTP_OP_RMS_NORM: 1552 case HTP_OP_L2_NORM: 1553 case HTP_OP_SCALE: 1554 case HTP_OP_CLAMP: 1555 case HTP_OP_SQR: 1556 case HTP_OP_SQRT: 1557 case HTP_OP_UNARY_LOG: 1558 case HTP_OP_UNARY_ABS: 1559 case HTP_OP_UNARY_STEP: 1560 case HTP_OP_UNARY_CEIL: 1561 case HTP_OP_UNARY_SGN: 1562 break; 1563 default: 1564 return false; 1565 } 1566 } 1567 } 1568 1569 if (!ggml_is_contiguous_rows(src0) || !ggml_is_contiguous(dst) || !ggml_are_same_shape(src0, dst)) { 1570 return false; 1571 } 1572 1573 const size_t elem_size = ggml_type_size(src0->type); 1574 1575 const size_t dst_data_row_size = dst->ne[0] * ggml_type_size(dst->type); 1576 1577 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 1578 const uint32_t min_rows_per_thread = (uint32_t) (std::max(1, (128 + dst_data_row_size - 1) / dst_data_row_size)); 1579 const uint32_t max_threads = (std::max)(1u, src0_nrows / min_rows_per_thread); 1580 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 1581 const size_t vtcm_size = htp_session_vtcm_size(sess); 1582 1583 kparams->n_threads = n_threads; 1584 1585 const bool broadcast_weight = (op == HTP_OP_RMS_NORM_MUL) && (src1->ne[1] * src1->ne[2] * src1->ne[3] == 1); 1586 kparams->broadcast_weight = broadcast_weight; 1587 1588 const bool src0_contig = ggml_is_contiguous_1(src0); 1589 const bool src1_contig = (op == HTP_OP_RMS_NORM_MUL && !broadcast_weight) ? ggml_is_contiguous_1(src1) : true; 1590 const uint32_t src0_nrows_per_thread = (src0_nrows + n_threads - 1) / n_threads; 1591 const uint32_t min_block_rows = (uint32_t) (std::max(1, 2048 / dst_data_row_size)); 1592 uint32_t max_block = (src0_contig && src1_contig) ? src0_nrows : (uint32_t) src0->ne[1]; 1593 if (src0_nrows_per_thread >= 2 * min_block_rows) { 1594 max_block = (std::min)(max_block, (std::max)(min_block_rows, src0_nrows_per_thread / 4)); 1595 } 1596 1597 kparams->src0_contig = src0_contig ? 1 : 0; 1598 kparams->src1_contig = src1_contig ? 1 : 0; 1599 1600 struct htp_unary_vtcm_layout L; 1601 uint32_t col_tile = 0; 1602 uint32_t vtcm_row_per_thread = 0; 1603 1604 htp_unary_solve_layout(&L, op, src0->ne[0], dst->ne[0], 1605 op == HTP_OP_RMS_NORM_MUL ? src1->ne[0] : 0, 1606 max_block, broadcast_weight, n_threads, vtcm_size, elem_size, 1607 &col_tile, &vtcm_row_per_thread); 1608 1609 kparams->col_tile = col_tile; 1610 kparams->vtcm_size = L.total_bytes; 1611 kparams->block = col_tile ? 0 : vtcm_row_per_thread; 1612 1613 const uint32_t tiles_per_row = col_tile > 0 ? (src0->ne[0] + col_tile - 1) / col_tile : 1; 1614 kparams->tiles_per_row = tiles_per_row; 1615 kparams->div_ne01 = init_fastdiv_values(src0->ne[1]); 1616 kparams->div_ne02 = init_fastdiv_values(src0->ne[2]); 1617 kparams->div_ne012 = init_fastdiv_values(src0->ne[1] * src0->ne[2]); 1618 kparams->div_tpr = init_fastdiv_values(tiles_per_row); 1619 1620 return (col_tile > 0 || vtcm_row_per_thread > 0) && L.total_bytes <= vtcm_size; 1621 } 1622 1623 static bool htp_kparams_solve_unary( 1624 const struct ggml_hexagon_session * sess, 1625 const struct ggml_tensor * op, 1626 struct htp_unary_kernel_params * kparams 1627 ) { 1628 const htp_op_code opcode = htp_op_remap(op); 1629 const struct ggml_tensor * src0 = (opcode == HTP_OP_HADAMARD) ? op->src[1] : op->src[0]; 1630 const struct ggml_tensor * src1 = (opcode == HTP_OP_HADAMARD) ? nullptr : op->src[1]; 1631 return htp_kparams_solve_unary_impl(sess, opcode, src0, src1, op, kparams); 1632 } 1633 1634 bool htp_kparams_solve_rms_norm_mul( 1635 const struct ggml_hexagon_session * sess, 1636 const struct ggml_tensor * src0, 1637 const struct ggml_tensor * src1, 1638 const struct ggml_tensor * dst, 1639 struct htp_unary_kernel_params * kparams 1640 ) { 1641 return htp_kparams_solve_unary_impl(sess, HTP_OP_RMS_NORM_MUL, src0, src1, dst, kparams); 1642 } 1643 1644 bool htp_kparams_solve_repeat_get_rows( 1645 const struct ggml_hexagon_session * sess, 1646 const struct ggml_tensor * src0, 1647 const struct ggml_tensor * src1, 1648 const struct ggml_tensor * dst, 1649 struct htp_get_rows_kernel_params * kparams 1650 ) { 1651 memset(kparams, 0, sizeof(*kparams)); 1652 1653 if (src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_BF16 && src0->type != GGML_TYPE_I32 && 1654 src0->type != GGML_TYPE_Q1_0 && src0->type != GGML_TYPE_Q2_0 && src0->type != GGML_TYPE_Q4_0 && src0->type != GGML_TYPE_Q8_0 && 1655 src0->type != GGML_TYPE_Q4_K && src0->type != GGML_TYPE_Q5_K && src0->type != GGML_TYPE_Q6_K) { 1656 return false; 1657 } 1658 1659 if (!ggml_is_contiguous_rows(src0) || !ggml_is_contiguous_rows(src1) || !ggml_is_contiguous_rows(dst)) { 1660 return false; 1661 } 1662 1663 if (src1->type != GGML_TYPE_I32 && src1->type != GGML_TYPE_I64) { 1664 return false; 1665 } 1666 1667 if (src0->type == dst->type) { 1668 if (src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_I32 && src0->type != GGML_TYPE_F16) { 1669 return false; 1670 } 1671 } else if (src0->type == GGML_TYPE_I32) { 1672 return false; 1673 } else if (dst->type != GGML_TYPE_F32) { 1674 return false; 1675 } 1676 1677 const uint32_t ne00 = src0->ne[0]; 1678 const uint32_t ne02 = src0->ne[2]; 1679 const uint32_t ne03 = src0->ne[3]; 1680 1681 const uint32_t ne10 = src1->ne[0]; 1682 const uint32_t ne11 = src1->ne[1]; 1683 const uint32_t ne12 = src1->ne[2]; 1684 const uint32_t nr = ne10 * ne11 * ne12; 1685 1686 const bool tiled = htp_tensor_is_tiled(src0); 1687 const int wtype = (tiled && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_BF16)) ? GGML_TYPE_F16 : src0->type; 1688 1689 if (tiled) { 1690 kparams->kernel_type = HTP_GET_ROWS_KERNEL_TILED; 1691 } else if (src0->type == dst->type) { 1692 kparams->kernel_type = HTP_GET_ROWS_KERNEL_SAMETYPE; 1693 } else { 1694 kparams->kernel_type = HTP_GET_ROWS_KERNEL_FLAT; 1695 } 1696 1697 const uint32_t total_tasks = nr; 1698 const uint32_t min_tasks_per_thread = (uint32_t) (std::max(1, (128 + dst->nb[1] - 1) / dst->nb[1])); 1699 const uint32_t max_threads = (std::max)(1u, total_tasks / min_tasks_per_thread); 1700 const size_t vtcm_size = htp_session_vtcm_size(sess); 1701 1702 kparams->n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 1703 1704 struct htp_get_rows_vtcm_layout vtcm_layout = {}; 1705 while (kparams->n_threads > 0) { 1706 htp_get_rows_vtcm_layout_build(&vtcm_layout, kparams->kernel_type, wtype, ne00, total_tasks, src1->type, kparams->n_threads); 1707 if (vtcm_layout.total_bytes <= vtcm_size) { 1708 break; 1709 } 1710 --kparams->n_threads; 1711 } 1712 1713 if (kparams->n_threads == 0 && total_tasks > 0) { 1714 htp_get_rows_vtcm_layout_build(&vtcm_layout, kparams->kernel_type, wtype, ne00, total_tasks, src1->type, 1); 1715 } 1716 1717 kparams->vtcm_size = (total_tasks == 0) ? 0 : vtcm_layout.total_bytes; 1718 kparams->total_tasks = total_tasks; 1719 1720 kparams->div_ne10 = ne10 > 0 ? init_fastdiv_values(ne10) : fastdiv_values{0, 0}; 1721 kparams->div_ne10_ne11 = (ne10 * ne11) > 0 ? init_fastdiv_values(ne10 * ne11) : fastdiv_values{0, 0}; 1722 kparams->div_ne02 = ne02 > 0 ? init_fastdiv_values(ne02) : fastdiv_values{0, 0}; 1723 kparams->div_ne03 = ne03 > 0 ? init_fastdiv_values(ne03) : fastdiv_values{0, 0}; 1724 1725 if (total_tasks == 0) { 1726 return true; 1727 } 1728 return kparams->n_threads > 0 && (size_t) kparams->vtcm_size <= vtcm_size; 1729 } 1730 1731 static bool htp_kparams_solve_get_rows( 1732 const struct ggml_hexagon_session * sess, 1733 const struct ggml_tensor * op, 1734 struct htp_get_rows_kernel_params * kparams 1735 ) { 1736 return htp_kparams_solve_repeat_get_rows(sess, op->src[0], op->src[1], op, kparams); 1737 } 1738 1739 static bool htp_kparams_solve_set_rows( 1740 const struct ggml_hexagon_session * sess, 1741 const struct ggml_tensor * op, 1742 struct htp_set_rows_kernel_params * kparams 1743 ) { 1744 memset(kparams, 0, sizeof(*kparams)); 1745 1746 const struct ggml_tensor * src0 = op->src[0]; 1747 const struct ggml_tensor * src1 = op->src[1]; 1748 const struct ggml_tensor * dst = op->src[2] ? op->src[2] : op; 1749 1750 if (dst->type == GGML_TYPE_Q8_0 && src0->ne[0] < 32) { 1751 return false; 1752 } 1753 if (src0->type != GGML_TYPE_F32) { 1754 return false; 1755 } 1756 if (src1->type != GGML_TYPE_I32 && src1->type != GGML_TYPE_I64) { 1757 return false; 1758 } 1759 if (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_Q8_0) { 1760 return false; 1761 } 1762 1763 const uint32_t nr = src0->ne[1]; 1764 1765 kparams->n_threads = (std::min)(htp_session_n_threads(sess), nr); 1766 kparams->total_tasks = nr; 1767 1768 struct htp_set_rows_vtcm_layout vtcm_layout; 1769 htp_set_rows_vtcm_layout_build(&vtcm_layout, dst->type, src0->ne[0], kparams->n_threads); 1770 kparams->vtcm_size = vtcm_layout.total_bytes; 1771 1772 return vtcm_layout.total_bytes <= htp_session_vtcm_size(sess); 1773 } 1774 1775 static bool htp_kparams_solve_softmax( 1776 const struct ggml_hexagon_session * sess, 1777 const struct ggml_tensor * op, 1778 struct htp_softmax_kernel_params * kparams 1779 ) { 1780 memset(kparams, 0, sizeof(*kparams)); 1781 1782 const struct ggml_tensor * src0 = op->src[0]; 1783 const struct ggml_tensor * src1 = op->src[1]; 1784 const struct ggml_tensor * src2 = op->src[2]; 1785 const struct ggml_tensor * dst = op; 1786 1787 if (src2) { 1788 return false; 1789 } 1790 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32 || src0->ne[2] > 512) { 1791 return false; 1792 } 1793 if (src1) { 1794 if ((src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) || 1795 src0->ne[0] != src1->ne[0] || src1->ne[1] < src0->ne[1] || 1796 src0->ne[2] % src1->ne[2] != 0 || src0->ne[3] % src1->ne[3] != 0 || 1797 !ggml_is_contiguous(src1)) { 1798 return false; 1799 } 1800 } 1801 if (!ggml_is_contiguous(src0) || !ggml_is_contiguous(dst)) { 1802 return false; 1803 } 1804 1805 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 1806 const size_t dst_data_row_size = op->ne[0] * ggml_type_size(op->type); 1807 const uint32_t min_rows_per_thread = (uint32_t) (std::max(1, (128 + dst_data_row_size - 1) / dst_data_row_size)); 1808 const uint32_t max_threads = (std::max)(1u, src0_nrows / min_rows_per_thread); 1809 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 1810 1811 float scale = 1.0f; 1812 float max_bias = 0.0f; 1813 memcpy(&scale, &op->op_params[0], sizeof(float)); 1814 memcpy(&max_bias, &op->op_params[1], sizeof(float)); 1815 1816 kparams->scale = scale; 1817 kparams->max_bias = max_bias; 1818 1819 const uint32_t n_head = src0->ne[2]; 1820 const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); 1821 kparams->n_head = n_head; 1822 kparams->n_head_log2 = n_head_log2; 1823 1824 if (max_bias > 0.0f && n_head_log2 > 0) { 1825 kparams->m0 = powf(2.0f, -(max_bias) / n_head_log2); 1826 kparams->m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); 1827 } else { 1828 kparams->m0 = 1.0f; 1829 kparams->m1 = 1.0f; 1830 } 1831 1832 kparams->use_src1 = (src1 != nullptr) ? 1 : 0; 1833 kparams->use_f16 = (src1 != nullptr && src1->type == GGML_TYPE_F16) ? 1 : 0; 1834 1835 const uint32_t ne00 = src0->ne[0]; 1836 const uint32_t ne10 = src1 ? src1->ne[0] : 1; 1837 1838 struct htp_softmax_vtcm_layout layout; 1839 htp_softmax_vtcm_layout_build(&layout, ne00, ne10, kparams->use_src1 != 0, kparams->use_f16 != 0, n_threads); 1840 if (layout.total_bytes > htp_session_vtcm_size(sess)) { 1841 return false; 1842 } 1843 1844 kparams->n_threads = n_threads; 1845 kparams->vtcm_size = (uint32_t) layout.total_bytes; 1846 1847 if (!kparams->use_src1) { 1848 kparams->kernel_type = HTP_SOFTMAX_KERNEL_NOMASK; 1849 } else if (kparams->use_f16) { 1850 kparams->kernel_type = HTP_SOFTMAX_KERNEL_MASK_F16; 1851 } else { 1852 kparams->kernel_type = HTP_SOFTMAX_KERNEL_MASK_F32; 1853 } 1854 1855 if (src0->ne[1] > 0) kparams->div_ne01 = init_fastdiv_values(src0->ne[1]); 1856 if (src0->ne[2] > 0) kparams->div_ne02 = init_fastdiv_values(src0->ne[2]); 1857 const uint32_t ne12 = src1 ? src1->ne[2] : 1; 1858 const uint32_t ne13 = src1 ? src1->ne[3] : 1; 1859 if (ne12 > 0) kparams->div_ne12 = init_fastdiv_values(ne12); 1860 if (ne13 > 0) kparams->div_ne13 = init_fastdiv_values(ne13); 1861 1862 return true; 1863 } 1864 1865 static bool htp_kparams_solve_rope( 1866 const struct ggml_hexagon_session * sess, 1867 const struct ggml_tensor * op, 1868 struct htp_rope_kernel_params * kparams 1869 ) { 1870 memset(kparams, 0, sizeof(*kparams)); 1871 1872 const struct ggml_tensor * src0 = op->src[0]; 1873 const struct ggml_tensor * src1 = op->src[1]; 1874 const struct ggml_tensor * src2 = op->src[2]; 1875 const struct ggml_tensor * dst = op; 1876 1877 if (!ggml_are_same_shape(src0, dst)) { 1878 return false; 1879 } 1880 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_I32) { 1881 return false; 1882 } 1883 if (src0->ne[0] <= 0) { 1884 return false; 1885 } 1886 1887 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 1888 if (src0_nrows == 0) { 1889 return false; 1890 } 1891 1892 const int32_t * op_params = &op->op_params[0]; 1893 const int n_dims = op_params[1]; 1894 const int mode = op_params[2]; 1895 const int n_offs = op_params[15]; 1896 1897 if (n_dims < 0 || n_dims % 2 != 0) { 1898 return false; 1899 } 1900 if (n_offs < 0 || (n_offs % 32 != 0) || (n_offs + n_dims > src0->ne[0])) { 1901 return false; 1902 } 1903 1904 float freq_base; 1905 memcpy(&freq_base, op_params + 5, sizeof(float)); 1906 if (freq_base < 0.0f) { 1907 return false; 1908 } 1909 1910 if (mode != GGML_ROPE_TYPE_NORMAL && 1911 mode != GGML_ROPE_TYPE_NEOX && 1912 mode != GGML_ROPE_TYPE_MROPE && 1913 mode != GGML_ROPE_TYPE_VISION && 1914 mode != GGML_ROPE_TYPE_IMROPE) { 1915 return false; 1916 } 1917 1918 const bool is_mrope = (mode & GGML_ROPE_TYPE_MROPE) != 0; 1919 1920 if (mode == GGML_ROPE_TYPE_VISION && (n_dims != (int) (src0->ne[0] / 2) || n_offs != 0)) { 1921 return false; 1922 } 1923 if (is_mrope) { 1924 const int32_t * sections = op_params + 11; 1925 if (sections[0] <= 0 && sections[1] <= 0 && sections[2] <= 0) { 1926 return false; 1927 } 1928 } 1929 1930 const int64_t min_pos_len = (is_mrope || mode == GGML_ROPE_TYPE_VISION) ? src0->ne[2] * 4 : src0->ne[2]; 1931 if (src1->ne[0] < min_pos_len || !ggml_is_contiguous(src1)) { 1932 return false; 1933 } 1934 1935 if (src2) { 1936 if (src2->type != GGML_TYPE_F32 || !ggml_is_contiguous(src2) || src2->ne[0] < (n_dims / 2)) { 1937 return false; 1938 } 1939 } 1940 1941 if (src0->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float) || 1942 src0->nb[1] < src0->ne[0] * sizeof(float) || dst->nb[1] < dst->ne[0] * sizeof(float)) { 1943 return false; 1944 } 1945 1946 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), src0_nrows); 1947 const uint32_t n_freq_factors = src2 ? (uint32_t) src2->ne[0] : 0; 1948 1949 struct htp_rope_vtcm_layout layout; 1950 htp_rope_vtcm_layout_build(&layout, src0->ne[0], n_threads, n_freq_factors); 1951 if (layout.total_bytes > htp_session_vtcm_size(sess)) { 1952 return false; 1953 } 1954 1955 kparams->n_threads = n_threads; 1956 kparams->vtcm_size = (uint32_t) layout.total_bytes; 1957 kparams->div_ne2_ne1 = init_fastdiv_values(dst->ne[2] * dst->ne[1]); 1958 kparams->div_ne1 = init_fastdiv_values(dst->ne[1]); 1959 1960 return true; 1961 } 1962 1963 static bool htp_kparams_solve_sum_rows( 1964 const struct ggml_hexagon_session * sess, 1965 const struct ggml_tensor * op, 1966 struct htp_sum_rows_kernel_params * kparams 1967 ) { 1968 memset(kparams, 0, sizeof(*kparams)); 1969 1970 const struct ggml_tensor * src0 = op->src[0]; 1971 const struct ggml_tensor * dst = op; 1972 1973 if (src0->type != GGML_TYPE_F32) { 1974 return false; 1975 } 1976 if (op->op == GGML_OP_ARGMAX) { 1977 if (dst->type != GGML_TYPE_I32) { 1978 return false; 1979 } 1980 } else if (dst->type != GGML_TYPE_F32) { 1981 return false; 1982 } 1983 if (!ggml_is_contiguous(src0) || !ggml_is_contiguous(dst)) { 1984 return false; 1985 } 1986 1987 const uint32_t ne00 = src0->ne[0]; 1988 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 1989 const uint32_t max_threads_sess = htp_session_n_threads(sess); 1990 1991 if (op->op == GGML_OP_SUM) { 1992 const uint32_t total_elems = (uint32_t) ggml_nelements(src0); 1993 const uint32_t n_threads = (total_elems >= 1024) ? (std::min)(max_threads_sess, (uint32_t) HTP_MAX_NTHREADS) : 1; 1994 const uint32_t raw_chunk = (total_elems + n_threads - 1) / n_threads; 1995 1996 kparams->n_threads = n_threads; 1997 kparams->elems_per_thread = hex_round_up(raw_chunk, 32); 1998 } else if (op->op == GGML_OP_ARGMAX && src0_nrows <= 1) { 1999 const uint32_t n_threads = (ne00 >= 1024) ? (std::min)(max_threads_sess, (uint32_t) HTP_MAX_NTHREADS) : 1; 2000 const uint32_t raw_chunk = (ne00 + n_threads - 1) / n_threads; 2001 2002 kparams->n_threads = n_threads; 2003 kparams->elems_per_thread = hex_round_up(raw_chunk, 32); 2004 } else { 2005 const uint32_t min_rpt = (src0->nb[1] < 1024) ? 32 : 1; 2006 const uint32_t max_threads = (std::max)(1u, src0_nrows / min_rpt); 2007 2008 kparams->n_threads = (std::min)(max_threads_sess, max_threads); 2009 } 2010 2011 return true; 2012 } 2013 2014 bool htp_kparams_solve_unary_mul_glu( 2015 const struct ggml_hexagon_session * sess, 2016 uint32_t op, 2017 const struct ggml_tensor * src0, 2018 const struct ggml_tensor * src1, 2019 const struct ggml_tensor * dst, 2020 struct htp_glu_kernel_params * kparams 2021 ) { 2022 memset(kparams, 0, sizeof(*kparams)); 2023 2024 if (!htp_op_is_glu((htp_op_code) op)) { 2025 return false; 2026 } 2027 if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) || dst->type != src0->type) { 2028 return false; 2029 } 2030 if ((op == HTP_OP_GLU_GEGLU_QUICK || op == HTP_OP_GLU_GEGLU_ERF) && src0->type != GGML_TYPE_F32) { 2031 return false; 2032 } 2033 2034 const size_t elem_size = ggml_type_size(src0->type); 2035 const auto is_valid_src = [elem_size](const struct ggml_tensor * t) { 2036 return ggml_is_contiguous_1(t) && t->nb[0] == elem_size && 2037 (t->ne[1] <= 1 || t->nb[1] >= (size_t) t->ne[0] * elem_size); 2038 }; 2039 2040 if (!is_valid_src(src0) || !ggml_is_contiguous(dst)) { 2041 return false; 2042 } 2043 if (src1 && (src1->type != src0->type || !ggml_are_same_shape(src0, src1) || !is_valid_src(src1))) { 2044 return false; 2045 } 2046 2047 const size_t dst_row_size = dst->ne[0] * elem_size; 2048 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 2049 const uint32_t min_rows_per_thread = (uint32_t) (std::max(1, (128 + dst_row_size - 1) / dst_row_size)); 2050 const uint32_t max_threads = (std::max)(1u, src0_nrows / min_rows_per_thread); 2051 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 2052 const size_t vtcm_size = htp_session_vtcm_size(sess); 2053 2054 const size_t row_size_aligned = hex_round_up(dst_row_size, 128); 2055 uint32_t vtcm_row_per_thread = (uint32_t) (vtcm_size / (n_threads * 3 * row_size_aligned)); 2056 if (vtcm_row_per_thread < 2) { 2057 return false; 2058 } 2059 2060 const uint32_t src0_nrows_per_thread = (src0_nrows + n_threads - 1) / n_threads; 2061 if (src0_nrows_per_thread >= 2 && vtcm_row_per_thread > src0_nrows_per_thread) { 2062 uint32_t target_rpt = (src0_nrows_per_thread >= 4) ? (src0_nrows_per_thread / 2) & ~1u : 2; 2063 vtcm_row_per_thread = (std::max)(2u, (std::min)(vtcm_row_per_thread, target_rpt)); 2064 } 2065 2066 struct htp_glu_vtcm_layout L; 2067 htp_glu_vtcm_layout_build(&L, dst_row_size, n_threads, vtcm_row_per_thread); 2068 2069 kparams->n_threads = n_threads; 2070 kparams->vtcm_row_per_thread = L.vtcm_row_per_thread; 2071 kparams->vtcm_size = (uint32_t) L.total_bytes; 2072 2073 return true; 2074 } 2075 2076 static bool htp_kparams_solve_glu( 2077 const struct ggml_hexagon_session * sess, 2078 const struct ggml_tensor * op, 2079 struct htp_glu_kernel_params * kparams 2080 ) { 2081 return htp_kparams_solve_unary_mul_glu(sess, htp_op_remap(op), op->src[0], op->src[1], op, kparams); 2082 } 2083 2084 static bool htp_kparams_solve_repeat( 2085 const struct ggml_hexagon_session * sess, 2086 const struct ggml_tensor * op, 2087 struct htp_repeat_kernel_params * kparams 2088 ) { 2089 memset(kparams, 0, sizeof(*kparams)); 2090 2091 const struct ggml_tensor * src0 = op->src[0]; 2092 const struct ggml_tensor * dst = op; 2093 2094 if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) || src0->type != dst->type) { 2095 return false; 2096 } 2097 if (!ggml_can_repeat(src0, dst) || ggml_is_transposed(src0) || ggml_is_transposed(dst)) { 2098 return false; 2099 } 2100 2101 const uint32_t total_dst_rows = dst->ne[1] * dst->ne[2] * dst->ne[3]; 2102 const size_t dst_row_size = dst->ne[0] * ggml_type_size(dst->type); 2103 const uint32_t min_rows_per_thread = (uint32_t) (std::max(1, (128 + dst_row_size - 1) / dst_row_size)); 2104 const uint32_t max_threads = (std::max)(1u, total_dst_rows / min_rows_per_thread); 2105 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 2106 2107 kparams->n_threads = n_threads; 2108 kparams->nr0 = dst->ne[0] / src0->ne[0]; 2109 kparams->nr1 = dst->ne[1] / src0->ne[1]; 2110 kparams->nr2 = dst->ne[2] / src0->ne[2]; 2111 kparams->nr3 = dst->ne[3] / src0->ne[3]; 2112 2113 kparams->div_ne1 = init_fastdiv_values(dst->ne[1]); 2114 kparams->div_ne12 = init_fastdiv_values(dst->ne[1] * dst->ne[2]); 2115 kparams->div_ne01 = init_fastdiv_values(src0->ne[1]); 2116 kparams->div_ne02 = init_fastdiv_values(src0->ne[2]); 2117 kparams->div_ne03 = init_fastdiv_values(src0->ne[3]); 2118 2119 return true; 2120 } 2121 2122 static bool htp_kparams_solve_ssm_conv( 2123 const struct ggml_hexagon_session * sess, 2124 const struct ggml_tensor * op, 2125 struct htp_ssm_conv_kernel_params * kparams 2126 ) { 2127 memset(kparams, 0, sizeof(*kparams)); 2128 2129 const struct ggml_tensor * src0 = op->src[0]; 2130 const struct ggml_tensor * src1 = op->src[1]; 2131 const struct ggml_tensor * dst = op; 2132 2133 if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 2134 return false; 2135 } 2136 if (src0->ne[3] != 1 || src1->ne[2] != 1 || src1->ne[3] != 1 || dst->ne[3] != 1) { 2137 return false; 2138 } 2139 2140 const int64_t d_conv_64 = src1->ne[0]; 2141 const int64_t d_inner_64 = src0->ne[1]; 2142 const int64_t n_t_64 = dst->ne[1]; 2143 const int64_t n_s_64 = dst->ne[2]; 2144 2145 if (d_conv_64 <= 0 || d_conv_64 > 32 || d_inner_64 <= 0) { 2146 return false; 2147 } 2148 if (src0->ne[0] != d_conv_64 - 1 + n_t_64 || src0->ne[1] != d_inner_64 || src0->ne[2] != n_s_64) { 2149 return false; 2150 } 2151 if (src1->ne[0] != d_conv_64 || src1->ne[1] != d_inner_64) { 2152 return false; 2153 } 2154 if (dst->ne[0] != d_inner_64 || dst->ne[1] != n_t_64 || dst->ne[2] != n_s_64) { 2155 return false; 2156 } 2157 if (src0->nb[0] != sizeof(float) || src1->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float)) { 2158 return false; 2159 } 2160 if (src0->nb[1] != src0->ne[0] * sizeof(float) || src1->nb[1] != src1->ne[0] * sizeof(float)) { 2161 return false; 2162 } 2163 2164 const uint32_t d_conv = (uint32_t) d_conv_64; 2165 const uint32_t d_inner = (uint32_t) d_inner_64; 2166 const uint32_t n_t = (uint32_t) n_t_64; 2167 const uint32_t n_s = (uint32_t) n_s_64; 2168 const uint32_t ncs = (uint32_t) src0->ne[0]; 2169 2170 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), (d_inner + 31) / 32); 2171 const size_t vtcm_size = htp_session_vtcm_size(sess); 2172 2173 kparams->n_threads = n_threads; 2174 kparams->d_conv = d_conv; 2175 kparams->d_inner = d_inner; 2176 kparams->n_t = n_t; 2177 kparams->n_s = n_s; 2178 2179 const uint32_t raw_rpt = (d_inner + n_threads - 1) / n_threads; 2180 const uint32_t d_inner_per_thread = hex_round_up(raw_rpt, 32); 2181 2182 if (n_t == 1) { 2183 kparams->d_inner_tile = d_inner_per_thread; 2184 } else { 2185 const uint32_t src1_raw_bytes = hex_round_up(d_inner_per_thread * d_conv * sizeof(float), 128) + 128; 2186 const uint32_t src1_T_bytes = hex_round_up(d_conv * d_inner_per_thread * sizeof(float), 128); 2187 const uint32_t vtcm_src1_per_thread = src1_raw_bytes + src1_T_bytes; 2188 2189 const size_t vtcm_budget = (vtcm_size > 0 ? vtcm_size / n_threads : (1024 * 1024)); 2190 const uint32_t src0_block_T = hex_round_up(ncs * 32 * sizeof(float), 128); 2191 const size_t fixed_bytes = vtcm_src1_per_thread + src0_block_T; 2192 const size_t avail_for_tiles = vtcm_budget > fixed_bytes ? vtcm_budget - fixed_bytes : (128 * 1024); 2193 2194 uint32_t target_max_tile = hex_round_up((d_inner_per_thread + 3) / 4, 32); 2195 target_max_tile = (std::max)(target_max_tile, 32u); 2196 target_max_tile = (std::min)(target_max_tile, 128u); 2197 2198 const size_t raw_stride_bytes = ncs * sizeof(float) + sizeof(float); 2199 uint32_t d_inner_tile = (uint32_t) (avail_for_tiles / (2 * (raw_stride_bytes + n_t * sizeof(float)))); 2200 d_inner_tile = (d_inner_tile / 32) * 32; 2201 if (d_inner_tile == 0) { 2202 d_inner_tile = 32; 2203 } 2204 if (d_inner_tile > target_max_tile) { 2205 d_inner_tile = target_max_tile; 2206 } 2207 if (d_inner_tile > d_inner_per_thread) { 2208 d_inner_tile = d_inner_per_thread; 2209 } 2210 kparams->d_inner_tile = d_inner_tile; 2211 } 2212 2213 struct htp_ssm_conv_vtcm_layout L; 2214 htp_ssm_conv_vtcm_layout_build(&L, d_inner_per_thread, d_conv, n_t, kparams->d_inner_tile, n_threads); 2215 2216 kparams->vtcm_size = (uint32_t) L.total_bytes; 2217 kparams->div_d_inner_tile = init_fastdiv_values(kparams->d_inner_tile); 2218 2219 return L.total_bytes <= vtcm_size; 2220 } 2221 2222 static bool htp_kparams_solve_sort( 2223 const struct ggml_hexagon_session * sess, 2224 const struct ggml_tensor * op, 2225 struct htp_sort_kernel_params * kparams 2226 ) { 2227 memset(kparams, 0, sizeof(*kparams)); 2228 2229 const struct ggml_tensor * src0 = op->src[0]; 2230 const struct ggml_tensor * dst = op; 2231 const bool is_top_k = (op->op == GGML_OP_TOP_K); 2232 2233 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_I32) { 2234 return false; 2235 } 2236 2237 const uint32_t total_rows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 2238 const uint32_t ne00 = src0->ne[0]; 2239 const uint32_t k = dst->ne[0]; 2240 const uint32_t n_threads_max = htp_session_n_threads(sess) > 0 ? htp_session_n_threads(sess) : 4; 2241 const size_t vtcm_budget = htp_session_vtcm_size(sess) > 0 ? htp_session_vtcm_size(sess) : (8 * 1024 * 1024); 2242 2243 int32_t order = GGML_SORT_ORDER_DESC; 2244 if (!is_top_k) { 2245 order = ((const int32_t *) op->op_params)[0]; 2246 } 2247 2248 struct htp_sort_vtcm_layout layout; 2249 if (!htp_sort_solve_layout(&layout, ne00, total_rows, k, n_threads_max, vtcm_budget, is_top_k)) { 2250 return false; 2251 } 2252 2253 kparams->n_threads = layout.n_threads; 2254 kparams->order = (uint32_t) order; 2255 kparams->is_top_k = is_top_k ? 1 : 0; 2256 kparams->chunk_elems = layout.chunk_elems; 2257 kparams->n_chunks = layout.n_chunks; 2258 kparams->vtcm_size = (uint32_t) layout.total_bytes; 2259 kparams->merge_elems = layout.merge_elems; 2260 kparams->n_slots = layout.n_slots; 2261 kparams->div_ne01 = init_fastdiv_values((uint32_t) src0->ne[1]); 2262 kparams->div_ne01_ne02 = init_fastdiv_values((uint32_t) (src0->ne[1] * src0->ne[2])); 2263 2264 return true; 2265 } 2266 2267 static void htp_pool_interior_range( 2268 uint32_t src_x, uint32_t dst_x, uint32_t kernel_x, uint32_t stride_x, int32_t pad_x, 2269 uint32_t * ox_lo, uint32_t * ox_hi 2270 ) { 2271 const uint32_t lo_raw = ((uint32_t) pad_x + stride_x - 1) / stride_x; 2272 *ox_lo = (lo_raw < dst_x) ? lo_raw : dst_x; 2273 2274 const int32_t numer_hi = (int32_t) src_x - (int32_t) kernel_x + pad_x; 2275 if (numer_hi < 0) { 2276 *ox_hi = 0; 2277 } else { 2278 const uint32_t hi_raw = (uint32_t) numer_hi / stride_x + 1; 2279 *ox_hi = (hi_raw < dst_x) ? hi_raw : dst_x; 2280 } 2281 if (*ox_hi < *ox_lo) { 2282 *ox_hi = *ox_lo; 2283 } 2284 } 2285 2286 static bool htp_kparams_solve_pool_2d( 2287 const struct ggml_hexagon_session * sess, 2288 const struct ggml_tensor * op, 2289 struct htp_pool_2d_kernel_params * kparams 2290 ) { 2291 memset(kparams, 0, sizeof(*kparams)); 2292 2293 const struct ggml_tensor * src0 = op->src[0]; 2294 const struct ggml_tensor * dst = op; 2295 const bool is_pool_1d = (op->op == GGML_OP_POOL_1D); 2296 2297 const int32_t * params = dst->op_params; 2298 if (params[0] != GGML_OP_POOL_AVG && params[0] != GGML_OP_POOL_MAX) { 2299 return false; 2300 } 2301 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 2302 return false; 2303 } 2304 if (!ggml_is_contiguous(src0) || !ggml_is_contiguous(dst)) { 2305 return false; 2306 } 2307 if (ggml_nbytes(src0) > UINT32_MAX - 256 || ggml_nbytes(dst) > UINT32_MAX - 256) { 2308 return false; 2309 } 2310 2311 const int32_t kernel_x = params[1]; 2312 const int32_t kernel_y = is_pool_1d ? 1 : params[2]; 2313 const int32_t stride_x = params[is_pool_1d ? 2 : 3]; 2314 const int32_t stride_y = is_pool_1d ? 1 : params[4]; 2315 const int32_t pad_x = params[is_pool_1d ? 3 : 5]; 2316 const int32_t pad_y = is_pool_1d ? 0 : params[6]; 2317 2318 if (kernel_x <= 0 || kernel_y <= 0 || stride_x <= 0 || stride_y <= 0 || 2319 pad_x < 0 || pad_y < 0) { 2320 return false; 2321 } 2322 2323 const uint32_t src_x = (uint32_t) src0->ne[0]; 2324 const uint32_t src_y = is_pool_1d ? 1 : (uint32_t) src0->ne[1]; 2325 const uint32_t dst_x = (uint32_t) dst->ne[0]; 2326 const uint32_t dst_y = is_pool_1d ? 1 : (uint32_t) dst->ne[1]; 2327 const uint32_t dst_plane_bytes = dst_x * dst_y * sizeof(float); 2328 const uint32_t planes = (uint32_t) (is_pool_1d 2329 ? (uint64_t) src0->ne[1] * (uint64_t) src0->ne[2] * (uint64_t) src0->ne[3] 2330 : (uint64_t) src0->ne[2] * (uint64_t) src0->ne[3]); 2331 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), planes > 0 ? planes : 1); 2332 const uint32_t planes_per_chunk = (dst_plane_bytes > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32(dst_plane_bytes, HEX_L2_LINE_SIZE)) : 1; 2333 2334 struct htp_pool_vtcm_layout layout; 2335 if (!htp_pool_solve_layout(&layout, src_x, src_y, dst_x, dst_y, n_threads, htp_session_vtcm_size(sess))) { 2336 return false; 2337 } 2338 2339 kparams->src_x = src_x; 2340 kparams->src_y = src_y; 2341 kparams->dst_x = dst_x; 2342 kparams->dst_y = dst_y; 2343 kparams->kernel_x = (uint32_t) kernel_x; 2344 kparams->kernel_y = (uint32_t) kernel_y; 2345 kparams->stride_x = (uint32_t) stride_x; 2346 kparams->stride_y = (uint32_t) stride_y; 2347 kparams->pad_x = pad_x; 2348 kparams->pad_y = pad_y; 2349 kparams->n_threads = n_threads; 2350 kparams->planes = planes; 2351 kparams->planes_per_chunk = planes_per_chunk; 2352 kparams->pool_op = (uint32_t) params[0]; 2353 2354 const bool exact_tiling = (kparams->pad_x == 0 && kparams->pad_y == 0 && 2355 kparams->stride_x == kparams->kernel_x && kparams->stride_y == kparams->kernel_y); 2356 const bool fast_ok = exact_tiling && (kparams->kernel_x == 1 || kparams->kernel_x == 2); 2357 const bool narrow_ok = fast_ok && ((uint64_t) kparams->dst_x * kparams->kernel_x <= 32); 2358 const bool global_ok = exact_tiling && 2359 kparams->kernel_x == kparams->src_x && kparams->kernel_y == kparams->src_y && 2360 kparams->dst_x == 1 && kparams->dst_y == 1; 2361 const bool block_ok = exact_tiling && 2362 kparams->kernel_y == 1 && kparams->stride_y == 1 && 2363 kparams->kernel_x >= 32; 2364 2365 if (global_ok) { 2366 kparams->kernel_type = HTP_POOL_KERNEL_GLOBAL; 2367 } else if (block_ok) { 2368 kparams->kernel_type = HTP_POOL_KERNEL_BLOCK; 2369 } else if (narrow_ok) { 2370 kparams->kernel_type = HTP_POOL_KERNEL_HVX_NARROW; 2371 } else if (fast_ok) { 2372 kparams->kernel_type = HTP_POOL_KERNEL_HVX; 2373 } else { 2374 kparams->kernel_type = HTP_POOL_KERNEL_GENERAL; 2375 } 2376 2377 kparams->avg_divide_count = (is_pool_1d && kparams->pool_op == GGML_OP_POOL_AVG) ? 1 : 0; 2378 htp_pool_interior_range(kparams->src_x, kparams->dst_x, kparams->kernel_x, 2379 kparams->stride_x, kparams->pad_x, 2380 &kparams->ox_lo, &kparams->ox_hi); 2381 kparams->inv_kernel_area = 1.0f / (float) (kparams->kernel_x * kparams->kernel_y); 2382 2383 return true; 2384 } 2385 2386 bool htp_kparams_solve_upscale( 2387 const struct ggml_hexagon_session * sess, 2388 const struct ggml_tensor * src0, 2389 const struct ggml_tensor * dst, 2390 struct htp_upscale_kernel_params * kparams 2391 ) { 2392 memset(kparams, 0, sizeof(*kparams)); 2393 2394 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 2395 return false; 2396 } 2397 2398 const uint32_t src_x = (uint32_t) src0->ne[0]; 2399 const uint32_t src_y = (uint32_t) src0->ne[1]; 2400 const uint32_t src_z = (uint32_t) src0->ne[2]; 2401 const uint32_t src_w = (uint32_t) src0->ne[3]; 2402 2403 const uint32_t dst_x = (uint32_t) dst->ne[0]; 2404 const uint32_t dst_y = (uint32_t) dst->ne[1]; 2405 const uint32_t dst_z = (uint32_t) dst->ne[2]; 2406 const uint32_t dst_w = (uint32_t) dst->ne[3]; 2407 2408 if (src_x == 0 || src_y == 0 || src_z == 0 || src_w == 0 || 2409 dst_x == 0 || dst_y == 0 || dst_z == 0 || dst_w == 0) { 2410 return false; 2411 } 2412 2413 const bool is_src_contig_row = (src0->nb[0] == sizeof(float)); 2414 const bool is_src_strided_2d = (src0->nb[0] > sizeof(float)) && 2415 (src0->nb[0] % sizeof(float) == 0) && 2416 (src0->nb[1] == (size_t) src_x * src0->nb[0]) && 2417 ((size_t) src_x * src_y <= 0xFFFFu); 2418 2419 if ((!is_src_contig_row && !is_src_strided_2d) || dst->nb[0] != sizeof(float)) { 2420 return false; 2421 } 2422 2423 const int32_t mode_flags = dst->op_params[0]; 2424 const uint32_t mode_raw = (uint32_t) (mode_flags & 0xFF); 2425 if (mode_raw >= GGML_SCALE_MODE_COUNT) { 2426 return false; 2427 } 2428 2429 const bool align_corners = (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) != 0; 2430 const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS) != 0; 2431 2432 float sf0 = (float) dst_x / (float) src_x; 2433 float sf1 = (float) dst_y / (float) src_y; 2434 float sf2 = (float) dst_z / (float) src_z; 2435 float sf3 = (float) dst_w / (float) src_w; 2436 float pixel_offset = 0.5f; 2437 2438 if (align_corners) { 2439 pixel_offset = 0.0f; 2440 sf0 = (dst_x > 1 && src_x > 1) ? (float) (dst_x - 1) / (float) (src_x - 1) : sf0; 2441 sf1 = (dst_y > 1 && src_y > 1) ? (float) (dst_y - 1) / (float) (src_y - 1) : sf1; 2442 } 2443 2444 const float inv_sf0 = 1.0f / sf0; 2445 const float inv_sf1 = 1.0f / sf1; 2446 const float inv_sf2 = 1.0f / sf2; 2447 const float inv_sf3 = 1.0f / sf3; 2448 2449 const float filter_radius0 = antialias ? (std::max)(1.0f, inv_sf0) : 0.0f; 2450 const float filter_radius1 = antialias ? (std::max)(1.0f, inv_sf1) : 0.0f; 2451 const float invscale0 = antialias ? (1.0f / filter_radius0) : 0.0f; 2452 const float invscale1 = antialias ? (1.0f / filter_radius1) : 0.0f; 2453 2454 const uint32_t total_slices = dst_z * dst_w; 2455 uint32_t n_threads = htp_session_n_threads(sess); 2456 2457 struct htp_upscale_vtcm_layout layout; 2458 uint32_t dst_nrows_per_chunk = 0; 2459 while (n_threads > 0) { 2460 if (htp_upscale_solve_layout(&layout, src_x, src_y, dst_x, dst_y, inv_sf1, filter_radius1, n_threads, 2461 htp_session_vtcm_size(sess), &dst_nrows_per_chunk)) { 2462 break; 2463 } 2464 n_threads--; 2465 } 2466 if (n_threads == 0) { 2467 return false; 2468 } 2469 2470 size_t lut_x_size = 0; 2471 size_t elem_y_sz = sizeof(uint32_t); 2472 if (mode_raw == HTP_UPSCALE_MODE_BICUBIC) { 2473 lut_x_size = hex_round_up(dst_x * sizeof(struct upscale_bicubic_x), 128); 2474 elem_y_sz = sizeof(struct upscale_bicubic_y); 2475 } else if (antialias) { 2476 lut_x_size = hex_round_up(dst_x * sizeof(struct upscale_antialias_x), 128); 2477 elem_y_sz = sizeof(struct upscale_antialias_y); 2478 } else if (mode_raw == HTP_UPSCALE_MODE_BILINEAR) { 2479 const uint32_t n_chunks = (dst_x + 31) / 32; 2480 lut_x_size = hex_round_up(n_chunks * sizeof(struct upscale_bilinear_x_chunk), 128); 2481 elem_y_sz = sizeof(struct upscale_bilinear_y); 2482 } else { 2483 lut_x_size = hex_round_up(dst_x * sizeof(uint32_t), 128); 2484 } 2485 const size_t total_lut_size = lut_x_size + hex_round_up(dst_y * elem_y_sz, 128); 2486 if (total_lut_size > HTP_DDR_SPAD_SIZE) { 2487 return false; 2488 } 2489 2490 const uint32_t chunks_per_slice = (dst_y + dst_nrows_per_chunk - 1) / dst_nrows_per_chunk; 2491 const uint32_t total_chunks = total_slices * chunks_per_slice; 2492 2493 n_threads = (std::min)(n_threads, total_chunks > 0 ? total_chunks : 1); 2494 htp_upscale_vtcm_layout_build(&layout, src_x, src_y, dst_x, dst_y, dst_nrows_per_chunk, inv_sf1, filter_radius1, n_threads); 2495 2496 const uint32_t slice_dst_bytes = dst_x * dst_y * sizeof(float); 2497 const uint32_t chunk_dst_bytes = dst_x * dst_nrows_per_chunk * sizeof(float); 2498 const bool dst_contig = (dst->nb[1] == (size_t) dst_x * sizeof(float)) && 2499 (dst_z <= 1 || dst->nb[2] == (size_t) dst_y * dst->nb[1]) && 2500 (dst_w <= 1 || dst->nb[3] == (size_t) dst_z * dst->nb[2]); 2501 2502 uint32_t chunks_per_group = 0; 2503 if (dst_contig) { 2504 const uint32_t slices_per_group = (slice_dst_bytes > 0) 2505 ? (HEX_L2_LINE_SIZE / hex_gcd_u32(slice_dst_bytes, HEX_L2_LINE_SIZE)) 2506 : 1; 2507 if (slices_per_group == 1 && (chunk_dst_bytes % HEX_L2_LINE_SIZE == 0)) { 2508 chunks_per_group = 1; 2509 } else { 2510 chunks_per_group = slices_per_group * chunks_per_slice; 2511 } 2512 } 2513 2514 uint8_t kernel_type = HTP_UPSCALE_KERNEL_BILINEAR; 2515 if (mode_raw == HTP_UPSCALE_MODE_NEAREST) { 2516 if (dst_x == 2 * src_x && dst_y == 2 * src_y && !align_corners) { 2517 kernel_type = HTP_UPSCALE_KERNEL_NEAREST_2X; 2518 } else if (dst_x == 4 * src_x && dst_y == 4 * src_y && !align_corners) { 2519 kernel_type = HTP_UPSCALE_KERNEL_NEAREST_4X; 2520 } else { 2521 kernel_type = HTP_UPSCALE_KERNEL_NEAREST; 2522 } 2523 } else if (mode_raw == HTP_UPSCALE_MODE_BICUBIC) { 2524 kernel_type = HTP_UPSCALE_KERNEL_BICUBIC; 2525 } else if (antialias) { 2526 kernel_type = HTP_UPSCALE_KERNEL_BILINEAR_AA; 2527 } else if (dst_x == 2 * src_x && dst_y == 2 * src_y && !align_corners && (src_x % 32 == 0)) { 2528 kernel_type = HTP_UPSCALE_KERNEL_BILINEAR_2X; 2529 } else { 2530 kernel_type = HTP_UPSCALE_KERNEL_BILINEAR; 2531 } 2532 2533 kparams->kernel_type = kernel_type; 2534 kparams->mode = (uint8_t) mode_raw; 2535 kparams->align_corners = align_corners ? 1 : 0; 2536 kparams->antialias = antialias ? 1 : 0; 2537 2538 kparams->inv_sf0 = inv_sf0; 2539 kparams->inv_sf1 = inv_sf1; 2540 kparams->inv_sf2 = inv_sf2; 2541 kparams->inv_sf3 = inv_sf3; 2542 kparams->pixel_offset = pixel_offset; 2543 2544 kparams->filter_radius0 = filter_radius0; 2545 kparams->filter_radius1 = filter_radius1; 2546 kparams->invscale0 = invscale0; 2547 kparams->invscale1 = invscale1; 2548 2549 kparams->n_threads = n_threads; 2550 kparams->chunks_per_group = chunks_per_group; 2551 2552 kparams->dst_nrows_per_chunk = dst_nrows_per_chunk; 2553 kparams->chunks_per_slice = chunks_per_slice; 2554 kparams->total_chunks = total_chunks; 2555 kparams->vtcm_size = (uint32_t) layout.total_bytes; 2556 2557 kparams->div_chunks_per_slice = init_fastdiv_values(chunks_per_slice > 0 ? chunks_per_slice : 1); 2558 kparams->div_dst_z = init_fastdiv_values(dst_z > 0 ? dst_z : 1); 2559 2560 return true; 2561 } 2562 2563 static bool htp_kparams_solve_concat( 2564 const struct ggml_hexagon_session * sess, 2565 const struct ggml_tensor * op, 2566 struct htp_concat_kernel_params * kparams 2567 ) { 2568 memset(kparams, 0, sizeof(*kparams)); 2569 kparams->kernel_type = HTP_CONCAT_KERNEL_UNSUPPORTED; 2570 2571 const struct ggml_tensor * src0 = op->src[0]; 2572 const struct ggml_tensor * src1 = op->src[1]; 2573 const struct ggml_tensor * dst = op; 2574 2575 if (!src0 || !src1 || !dst) { 2576 return false; 2577 } 2578 2579 int dim = ((const int32_t *) op->op_params)[0]; 2580 if (dim < 0 || dim >= GGML_MAX_DIMS) { 2581 return false; 2582 } 2583 kparams->dim = dim; 2584 2585 if (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_I32) { 2586 return false; 2587 } 2588 if (src0->type != dst->type || src1->type != dst->type) { 2589 return false; 2590 } 2591 2592 const uint32_t type_size = ggml_type_size(dst->type); 2593 const uint32_t n_threads = htp_session_n_threads(sess); 2594 const uint32_t mdev_count = htp_session_mdev_count(sess); 2595 const size_t vtcm_size = htp_session_vtcm_size(sess); 2596 2597 for (int d = 0; d < GGML_MAX_DIMS; d++) { 2598 const int64_t ne_d = (d == dim) ? src0->ne[d] + src1->ne[d] : src0->ne[d]; 2599 if (dst->ne[d] != ne_d || (d != dim && src1->ne[d] != dst->ne[d])) { 2600 return false; 2601 } 2602 } 2603 2604 const uint32_t max_gather_ne0 = 128 / type_size; 2605 const uint32_t total_rows = (uint32_t) (dst->ne[1] * dst->ne[2] * dst->ne[3]); 2606 const bool b_dense = (src1->nb[0] == type_size); 2607 const bool gather_strides_ok = 2608 src0->nb[1] >= (size_t) src0->ne[0] * type_size && 2609 (b_dense ? src1->nb[1] >= (size_t) src1->ne[0] * type_size 2610 : src1->nb[0] >= (size_t) src1->ne[1] * type_size); 2611 2612 if (dim == 0 && 2613 (dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16) && 2614 dst->ne[0] <= max_gather_ne0 && 2615 dst->nb[0] == type_size && dst->nb[1] == (size_t) dst->ne[0] * type_size && 2616 src0->nb[0] == type_size && 2617 (src1->nb[0] == type_size || src1->nb[1] == type_size) && 2618 gather_strides_ok && 2619 htp_gather_rows_tensor_outer_contiguous(dst) && 2620 htp_gather_rows_tensor_outer_contiguous(src0) && 2621 htp_gather_rows_tensor_outer_contiguous(src1)) { 2622 2623 if (htp_gather_rows_solve_layout(&kparams->u.gather, 2624 (uint32_t) src0->ne[0], (uint32_t) src0->nb[1], 2625 (uint32_t) src1->ne[0], (uint32_t) src1->nb[1], b_dense, 2626 type_size, total_rows, 2627 n_threads, mdev_count, vtcm_size)) { 2628 kparams->kernel_type = HTP_CONCAT_KERNEL_GATHER_ROWS; 2629 kparams->n_threads = (uint8_t) n_threads; 2630 kparams->vtcm_size = kparams->u.gather.slice * n_threads; 2631 return true; 2632 } 2633 } 2634 2635 const bool dma_strides_ok = (src0->nb[0] == type_size && src1->nb[0] == type_size && dst->nb[0] == type_size); 2636 if (dma_strides_ok) { 2637 kparams->kernel_type = HTP_CONCAT_KERNEL_REGULAR; 2638 kparams->n_threads = 1; 2639 return true; 2640 } 2641 2642 const bool is_src1_transposed = (src1->nb[0] > src1->nb[1]); 2643 const bool is_src0_transposed = (src0->nb[0] > src0->nb[1]); 2644 const bool transposed_rows_ok = (src0->nb[0] == type_size && src1->nb[1] == type_size && dst->nb[0] == type_size); 2645 2646 if (dim == 0 && is_src1_transposed && !is_src0_transposed && transposed_rows_ok && 2647 (dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16)) { 2648 2649 const uint32_t nth = n_threads > 0 ? n_threads : 8; 2650 struct htp_concat_transposed_vtcm_layout layout; 2651 htp_concat_transposed_vtcm_layout_build(&layout, (uint32_t) src0->ne[0], (uint32_t) src1->ne[0], type_size, nth); 2652 2653 if (vtcm_size > 0 && layout.total_bytes > vtcm_size) { 2654 return false; 2655 } 2656 2657 kparams->kernel_type = HTP_CONCAT_KERNEL_TRANSPOSED; 2658 kparams->n_threads = nth; 2659 kparams->vtcm_size = layout.total_bytes; 2660 kparams->u.transposed.div_ne2 = init_fastdiv_values((uint32_t) dst->ne[2]); 2661 return true; 2662 } 2663 2664 return false; 2665 } 2666 2667 // Permuted copy with strided dim 0 (e.g. channel-first <-> channel-last) 2668 static bool htp_kparams_solve_cpy_transpose( 2669 const struct ggml_hexagon_session * sess, 2670 const struct ggml_tensor * src0, 2671 const struct ggml_tensor * dst, 2672 uint32_t type_size, 2673 struct htp_copy_kernel_params * kparams) { 2674 2675 // View contiguous dst with src shape to support CPY with reshape 2676 const bool dst_cont = ggml_is_contiguous(dst); 2677 if (!dst_cont && !ggml_are_same_shape(src0, dst)) { 2678 return false; 2679 } 2680 2681 // Drop size-1 dims and merge dims adjacent in both tensors 2682 size_t ne[GGML_MAX_DIMS]; 2683 size_t nbs[GGML_MAX_DIMS]; 2684 size_t nbd[GGML_MAX_DIMS]; 2685 size_t dst_stride = type_size; 2686 int n = 0; 2687 for (int d = 0; d < GGML_MAX_DIMS; d++) { 2688 const size_t sd = dst_cont ? dst_stride : dst->nb[d]; 2689 dst_stride *= (size_t) src0->ne[d]; 2690 if (src0->ne[d] == 1) { 2691 continue; 2692 } 2693 if (n > 0 && src0->nb[d] == nbs[n - 1] * ne[n - 1] && sd == nbd[n - 1] * ne[n - 1]) { 2694 ne[n - 1] *= (size_t) src0->ne[d]; 2695 continue; 2696 } 2697 ne[n] = (size_t) src0->ne[d]; 2698 nbs[n] = src0->nb[d]; 2699 nbd[n] = sd; 2700 n++; 2701 } 2702 2703 // Fold leading dense dim into element block 2704 size_t es = type_size; 2705 for (int i = 0; i < n; i++) { 2706 if (nbs[i] == type_size && nbd[i] == type_size) { 2707 es *= ne[i]; 2708 for (int j = i; j + 1 < n; j++) { 2709 ne[j] = ne[j + 1]; 2710 nbs[j] = nbs[j + 1]; 2711 nbd[j] = nbd[j + 1]; 2712 } 2713 n--; 2714 break; 2715 } 2716 } 2717 if (es > 128 || (es & (es - 1)) != 0) { 2718 return false; 2719 } 2720 2721 // Transpose axes: dim a dense in dst, dim k dense in src 2722 int a = -1; 2723 int k = -1; 2724 for (int i = 0; i < n; i++) { 2725 if (a < 0 && nbd[i] == es) { 2726 a = i; 2727 } 2728 if (k < 0 && nbs[i] == es) { 2729 k = i; 2730 } 2731 } 2732 if (a < 0 || k < 0 || a == k) { 2733 return false; 2734 } 2735 2736 struct htp_copy_transpose_params tp = {}; 2737 tp.elem_size = (uint8_t) es; 2738 tp.ne[0] = (uint32_t) ne[a]; 2739 tp.ne[1] = (uint32_t) ne[k]; 2740 tp.ne[2] = 1; 2741 tp.src_nb[0] = (uint32_t) nbs[a]; 2742 tp.dst_nb[0] = (uint32_t) nbd[k]; 2743 tp.packed = nbd[k] == ne[a] * es && ne[a] * es < 128 && 128 % (ne[a] * es) == 0; 2744 2745 size_t ne3 = 1; 2746 int m = 0; 2747 for (int i = 0; i < n; i++) { 2748 if (i == a || i == k) { 2749 continue; 2750 } 2751 if (m == 0) { 2752 tp.ne[2] = (uint32_t) ne[i]; 2753 } else { 2754 ne3 = ne[i]; 2755 } 2756 tp.src_nb[1 + m] = (uint32_t) nbs[i]; 2757 tp.dst_nb[1 + m] = (uint32_t) nbd[i]; 2758 m++; 2759 } 2760 2761 const uint32_t n_threads = htp_session_n_threads(sess); 2762 const size_t vtcm_size = htp_session_vtcm_size(sess); 2763 uint64_t vtcm = 0; 2764 for (uint32_t blk_bytes = HTP_COPY_TRANSPOSE_BLK_BYTES; ; blk_bytes /= 2) { 2765 if (!htp_copy_transpose_layout_build(&tp, blk_bytes)) { 2766 return false; 2767 } 2768 vtcm = (uint64_t) n_threads * 2 * (tp.in_buf_size + tp.out_buf_size); 2769 if (vtcm <= vtcm_size) { 2770 break; 2771 } 2772 } 2773 2774 const uint64_t total_tiles = (uint64_t) tp.n0b * tp.nkb * tp.ne[2] * ne3; 2775 if (total_tiles > UINT32_MAX) { 2776 return false; 2777 } 2778 tp.total_tiles = (uint32_t) total_tiles; 2779 tp.div_nkb = init_fastdiv_values(tp.nkb); 2780 tp.div_n0b = init_fastdiv_values(tp.n0b); 2781 tp.div_ne2 = init_fastdiv_values(tp.ne[2]); 2782 2783 kparams->kernel_type = HTP_COPY_KERNEL_TRANSPOSE; 2784 kparams->n_threads = (uint8_t) n_threads; 2785 kparams->vtcm_size = (uint32_t) vtcm; 2786 kparams->u.transpose = tp; 2787 return true; 2788 } 2789 2790 static bool htp_kparams_solve_cpy( 2791 const struct ggml_hexagon_session * sess, 2792 const struct ggml_tensor * op, 2793 struct htp_copy_kernel_params * kparams 2794 ) { 2795 memset(kparams, 0, sizeof(*kparams)); 2796 kparams->kernel_type = HTP_COPY_KERNEL_UNSUPPORTED; 2797 2798 const struct ggml_tensor * src0 = op->src[0]; 2799 const struct ggml_tensor * dst = op; 2800 2801 if (!src0 || !dst) { 2802 return false; 2803 } 2804 2805 if (src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_I32) { 2806 return false; 2807 } 2808 if (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_I32) { 2809 return false; 2810 } 2811 2812 const int64_t nelem_src = ggml_nelements(src0); 2813 const int64_t nelem_dst = ggml_nelements(dst); 2814 if (nelem_src != nelem_dst || nelem_src < 0) { 2815 return false; 2816 } 2817 2818 const uint32_t src_type_size = ggml_type_size(src0->type); 2819 const uint32_t dst_type_size = ggml_type_size(dst->type); 2820 2821 kparams->src0_type_size = (uint8_t) src_type_size; 2822 kparams->dst_type_size = (uint8_t) dst_type_size; 2823 kparams->total_elems = (uint32_t) nelem_src; 2824 2825 if (nelem_src == 0) { 2826 kparams->kernel_type = HTP_COPY_KERNEL_1D_CONTIG; 2827 return true; 2828 } 2829 2830 if (nelem_src == 1) { 2831 if (src0->type == dst->type) { 2832 kparams->kernel_type = HTP_COPY_KERNEL_SCALAR; 2833 return true; 2834 } 2835 if ((src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) || 2836 (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32) || 2837 (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) || 2838 (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32)) { 2839 kparams->kernel_type = HTP_COPY_KERNEL_SCALAR; 2840 return true; 2841 } 2842 return false; 2843 } 2844 2845 const bool sametype = (src0->type == dst->type); 2846 const bool same_extents = ggml_are_same_shape(src0, dst); 2847 2848 const bool transposed = (src0->nb[0] > src0->nb[1]) || (dst->nb[0] > dst->nb[1]) || 2849 (src0->nb[0] != src_type_size) || (dst->nb[0] != dst_type_size) || 2850 (src0->nb[1] < (size_t) src0->ne[0] * src_type_size) || (dst->nb[1] < (size_t) dst->ne[0] * dst_type_size); 2851 const bool sameshape = same_extents && !transposed; 2852 2853 const bool src_is_contiguous = ggml_is_contiguous(src0); 2854 const bool dst_is_contiguous = ggml_is_contiguous(dst); 2855 2856 const uint32_t n_threads = htp_session_n_threads(sess); 2857 const uint32_t mdev_count = htp_session_mdev_count(sess); 2858 const size_t vtcm_size = htp_session_vtcm_size(sess); 2859 2860 if (sametype) { 2861 if (src_is_contiguous && dst_is_contiguous) { 2862 kparams->kernel_type = HTP_COPY_KERNEL_1D_CONTIG; 2863 return true; 2864 } 2865 2866 const uint32_t max_gather_ne0 = 128 / src_type_size; 2867 const uint32_t total_rows = (uint32_t) (src0->ne[1] * src0->ne[2] * src0->ne[3]); 2868 if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16) && 2869 dst_is_contiguous && !src_is_contiguous && 2870 src0->nb[0] == src_type_size && src0->ne[0] <= max_gather_ne0 && 2871 src0->nb[1] >= (size_t) src0->ne[0] * src_type_size && 2872 total_rows > 1 && 2873 htp_gather_rows_tensor_outer_contiguous(src0)) { 2874 2875 if (htp_gather_rows_solve_layout(&kparams->u.gather, 2876 (uint32_t) src0->ne[0], (uint32_t) src0->nb[1], 2877 0, 0, false, 2878 src_type_size, total_rows, 2879 n_threads, mdev_count, vtcm_size)) { 2880 kparams->kernel_type = HTP_COPY_KERNEL_GATHER_ROWS; 2881 kparams->n_threads = (uint8_t) n_threads; 2882 kparams->vtcm_size = kparams->u.gather.slice * n_threads; 2883 return true; 2884 } 2885 } 2886 2887 if (htp_kparams_solve_cpy_transpose(sess, src0, dst, src_type_size, kparams)) { 2888 return true; 2889 } 2890 2891 if (sameshape) { 2892 kparams->kernel_type = HTP_COPY_KERNEL_SAMESHAPE_SAMETYPE; 2893 kparams->total_rows = (uint32_t) (src0->ne[1] * src0->ne[2] * src0->ne[3]); 2894 return true; 2895 } 2896 2897 kparams->kernel_type = HTP_COPY_KERNEL_RESHAPE; 2898 kparams->n_threads = n_threads > 0 ? (uint8_t) n_threads : 4; 2899 kparams->u.reshape.div_ne0 = init_fastdiv_values((uint32_t) dst->ne[0]); 2900 kparams->u.reshape.div_ne1_ne0 = init_fastdiv_values((uint32_t) (dst->ne[1] * dst->ne[0])); 2901 kparams->u.reshape.div_ne2_ne1_ne0 = init_fastdiv_values((uint32_t) (dst->ne[2] * dst->ne[1] * dst->ne[0])); 2902 kparams->u.reshape.div_ne00 = init_fastdiv_values((uint32_t) src0->ne[0]); 2903 kparams->u.reshape.div_ne01_ne00 = init_fastdiv_values((uint32_t) (src0->ne[1] * src0->ne[0])); 2904 kparams->u.reshape.div_ne02_ne01_ne00 = init_fastdiv_values((uint32_t) (src0->ne[2] * src0->ne[1] * src0->ne[0])); 2905 return true; 2906 } 2907 2908 if (!sameshape) { 2909 return false; 2910 } 2911 2912 const bool valid_conversion = (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) || 2913 (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) || 2914 (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) || 2915 (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32); 2916 if (!valid_conversion) { 2917 return false; 2918 } 2919 2920 const uint32_t nth = n_threads > 0 ? n_threads : 4; 2921 struct htp_copy_convert_vtcm_layout layout; 2922 htp_copy_convert_solve_layout(&layout, (uint32_t) src0->ne[0], (uint32_t) src0->ne[1], src_type_size, dst_type_size, nth); 2923 2924 if (vtcm_size > 0 && layout.total_bytes > vtcm_size) { 2925 return false; 2926 } 2927 2928 kparams->kernel_type = HTP_COPY_KERNEL_SAMESHAPE_CONVERT; 2929 kparams->total_rows = (uint32_t) (src0->ne[1] * src0->ne[2] * src0->ne[3]); 2930 kparams->n_threads = (uint8_t) nth; 2931 kparams->vtcm_size = (uint32_t) layout.total_bytes; 2932 kparams->u.convert.blk_rows = layout.blk_rows; 2933 kparams->u.convert.div_ne01 = init_fastdiv_values((uint32_t) src0->ne[1]); 2934 kparams->u.convert.div_ne02_ne01 = init_fastdiv_values((uint32_t) (src0->ne[2] * src0->ne[1])); 2935 2936 return true; 2937 } 2938 2939 static bool im2col_3d_calc_output_size( 2940 uint32_t input, uint32_t kernel, int32_t stride, int32_t padding, int32_t dilation, uint32_t * output 2941 ) { 2942 if (stride <= 0 || padding < 0 || dilation <= 0 || kernel == 0) { 2943 return false; 2944 } 2945 2946 const int64_t numerator = (int64_t) input + 2LL * padding - (int64_t) dilation * (kernel - 1) - 1; 2947 if (numerator < 0) { 2948 return false; 2949 } 2950 2951 const uint64_t size = (uint64_t) (numerator / stride) + 1; 2952 if (size == 0 || size > UINT32_MAX) { 2953 return false; 2954 } 2955 2956 *output = (uint32_t) size; 2957 return true; 2958 } 2959 2960 static bool htp_kparams_solve_im2col( 2961 const struct ggml_hexagon_session * sess, 2962 const struct ggml_tensor * op, 2963 struct htp_im2col_kernel_params * kparams 2964 ) { 2965 memset(kparams, 0, sizeof(*kparams)); 2966 kparams->kernel_type = HTP_IM2COL_KERNEL_UNSUPPORTED; 2967 2968 const struct ggml_tensor * src0 = op->src[0]; 2969 const struct ggml_tensor * src1 = op->src[1]; 2970 const struct ggml_tensor * dst = op; 2971 2972 if (!src0 || !src1 || !dst) { 2973 return false; 2974 } 2975 if (src1->type != GGML_TYPE_F32 || (dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_F32)) { 2976 return false; 2977 } 2978 if (src1->nb[0] != sizeof(float) || !ggml_is_contiguous(dst)) { 2979 return false; 2980 } 2981 2982 const int32_t * op_params = (const int32_t *) op->op_params; 2983 const int32_t s0 = op_params[0]; 2984 const int32_t s1 = op_params[1]; 2985 const int32_t p0 = op_params[2]; 2986 const int32_t p1 = op_params[3]; 2987 const int32_t d0 = op_params[4]; 2988 const int32_t d1 = op_params[5]; 2989 const bool is_2D = (op_params[6] == 1); 2990 2991 if (s0 <= 0 || d0 <= 0 || p0 < 0) { 2992 return false; 2993 } 2994 if (is_2D && (s1 <= 0 || d1 <= 0 || p1 < 0)) { 2995 return false; 2996 } 2997 2998 const uint32_t N = is_2D ? (uint32_t) src1->ne[3] : (uint32_t) src1->ne[2]; 2999 const uint32_t IC = is_2D ? (uint32_t) src1->ne[2] : (uint32_t) src1->ne[1]; 3000 const uint32_t IH = is_2D ? (uint32_t) src1->ne[1] : 1; 3001 const uint32_t IW = (uint32_t) src1->ne[0]; 3002 const uint32_t KH = is_2D ? (uint32_t) src0->ne[1] : 1; 3003 const uint32_t KW = (uint32_t) src0->ne[0]; 3004 const uint32_t OH = is_2D ? (uint32_t) dst->ne[2] : 1; 3005 const uint32_t OW = (uint32_t) dst->ne[1]; 3006 3007 if (KW == 0 || KH == 0 || IW == 0 || IH == 0 || IC == 0 || OW == 0 || OH == 0 || N == 0) { 3008 return false; 3009 } 3010 3011 const uint64_t patch_stride_64 = (uint64_t) IC * KH * KW; 3012 const uint64_t total_rows_64 = (uint64_t) N * OH; 3013 if (patch_stride_64 > UINT32_MAX || total_rows_64 > UINT32_MAX) { 3014 return false; 3015 } 3016 const uint32_t patch_stride = (uint32_t) patch_stride_64; 3017 const uint32_t total_rows = (uint32_t) total_rows_64; 3018 3019 if (dst->ne[0] != patch_stride || dst->ne[1] != OW || (is_2D && dst->ne[2] != OH)) { 3020 return false; 3021 } 3022 3023 const uint32_t dst_elem = (dst->type == GGML_TYPE_F16) ? sizeof(ggml_fp16_t) : sizeof(float); 3024 3025 bool is_patchembed = ggml_is_contiguous(src1) && (s0 == (int32_t) KW && p0 == 0 && d0 == 1); 3026 if (is_2D) { 3027 is_patchembed = is_patchembed && (s1 == (int32_t) KH && p1 == 0 && d1 == 1); 3028 } 3029 3030 const uint32_t max_threads = (sess && htp_session_n_threads(sess) > 0) ? htp_session_n_threads(sess) : 8; 3031 const uint32_t n_threads = (total_rows < max_threads) ? total_rows : max_threads; 3032 const uint32_t vtcm_size = (sess && htp_session_vtcm_size(sess) > 0) ? (uint32_t) htp_session_vtcm_size(sess) : 0; 3033 const uint32_t row_size = (uint32_t) dst->nb[2]; 3034 const uint32_t rows_per_chunk = (row_size > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32(row_size, HEX_L2_LINE_SIZE)) : 1; 3035 3036 if (is_patchembed) { 3037 const uint64_t src_row_bytes_raw = (uint64_t) IC * KH * IW * sizeof(float); 3038 const uint64_t dst_row_bytes_raw = (uint64_t) OW * patch_stride * dst_elem; 3039 if (src_row_bytes_raw <= UINT32_MAX && dst_row_bytes_raw <= UINT32_MAX) { 3040 const uint32_t src_row_bytes = hex_round_up((uint32_t) src_row_bytes_raw, 256); 3041 const uint32_t dst_row_bytes = hex_round_up((uint32_t) dst_row_bytes_raw, 256); 3042 3043 struct htp_im2col_vtcm_layout layout; 3044 htp_im2col_vtcm_layout_build(&layout, src_row_bytes, dst_row_bytes, n_threads, 3045 htp_im2col_scratch_bytes(HTP_IM2COL_KERNEL_PATCHEMBED_DMA, KH, patch_stride)); 3046 3047 if (vtcm_size == 0 || layout.total_bytes <= vtcm_size) { 3048 kparams->kernel_type = HTP_IM2COL_KERNEL_PATCHEMBED_DMA; 3049 kparams->n_threads = (uint8_t) n_threads; 3050 kparams->vtcm_size = (uint32_t) layout.total_bytes; 3051 kparams->src_row_bytes = src_row_bytes; 3052 kparams->dst_row_bytes = dst_row_bytes; 3053 kparams->owb = OW; 3054 kparams->wb = IW; 3055 kparams->nbpr = 1; 3056 kparams->N = N; 3057 kparams->IC = IC; 3058 kparams->ID = 1; 3059 kparams->IH = IH; 3060 kparams->IW = IW; 3061 kparams->KD = 1; 3062 kparams->KH = KH; 3063 kparams->KW = KW; 3064 kparams->OD = 1; 3065 kparams->OH = OH; 3066 kparams->OW = OW; 3067 kparams->patch_stride = patch_stride; 3068 kparams->total_rows = total_rows; 3069 kparams->rows_per_chunk = rows_per_chunk; 3070 kparams->div_OH = init_fastdiv_values(OH); 3071 kparams->div_OD = init_fastdiv_values(1); 3072 return true; 3073 } 3074 } 3075 } 3076 3077 for (uint32_t owb = (OW < 256 ? OW : 256); owb >= 1; owb--) { 3078 const uint64_t Wb = (uint64_t) (owb - 1) * s0 + (uint64_t) (KW - 1) * d0 + 1; 3079 if (Wb > UINT32_MAX) { 3080 if (owb == 1) break; 3081 continue; 3082 } 3083 3084 const uint64_t src_row_bytes_raw = (uint64_t) IC * KH * Wb * sizeof(float); 3085 const uint64_t dst_row_bytes_raw = (uint64_t) owb * patch_stride * dst_elem; 3086 if (src_row_bytes_raw > UINT32_MAX || dst_row_bytes_raw > UINT32_MAX) { 3087 if (owb == 1) break; 3088 continue; 3089 } 3090 3091 const uint32_t src_row_bytes = hex_round_up((uint32_t) src_row_bytes_raw, 256); 3092 const uint32_t dst_row_bytes = hex_round_up((uint32_t) dst_row_bytes_raw, 256); 3093 3094 // threads work on output-col blocks, so a single output row (1D) can use them all 3095 const uint32_t nbpr = (OW + owb - 1) / owb; 3096 const uint64_t nblocks = (uint64_t) total_rows * nbpr; 3097 const uint32_t blk_threads = (uint32_t) (nblocks < max_threads ? nblocks : max_threads); 3098 3099 struct htp_im2col_vtcm_layout layout; 3100 htp_im2col_vtcm_layout_build(&layout, src_row_bytes, dst_row_bytes, blk_threads, 3101 htp_im2col_scratch_bytes(HTP_IM2COL_KERNEL_BLOCKED_DMA, KH, patch_stride)); 3102 3103 if (vtcm_size == 0 || layout.total_bytes <= vtcm_size) { 3104 kparams->kernel_type = HTP_IM2COL_KERNEL_BLOCKED_DMA; 3105 kparams->n_threads = (uint8_t) blk_threads; 3106 kparams->vtcm_size = (uint32_t) layout.total_bytes; 3107 kparams->src_row_bytes = src_row_bytes; 3108 kparams->dst_row_bytes = dst_row_bytes; 3109 kparams->owb = owb; 3110 kparams->wb = (uint32_t) Wb; 3111 kparams->nbpr = nbpr; 3112 kparams->N = N; 3113 kparams->IC = IC; 3114 kparams->ID = 1; 3115 kparams->IH = IH; 3116 kparams->IW = IW; 3117 kparams->KD = 1; 3118 kparams->KH = KH; 3119 kparams->KW = KW; 3120 kparams->OD = 1; 3121 kparams->OH = OH; 3122 kparams->OW = OW; 3123 kparams->patch_stride = patch_stride; 3124 kparams->total_rows = total_rows; 3125 kparams->rows_per_chunk = rows_per_chunk; 3126 kparams->div_OH = init_fastdiv_values(OH); 3127 kparams->div_OD = init_fastdiv_values(1); 3128 kparams->div_nbpr = init_fastdiv_values(nbpr); 3129 return true; 3130 } 3131 if (owb == 1) break; 3132 } 3133 3134 return false; 3135 } 3136 3137 static bool htp_kparams_solve_im2col_3d( 3138 const struct ggml_hexagon_session * sess, 3139 const struct ggml_tensor * op, 3140 struct htp_im2col_kernel_params * kparams 3141 ) { 3142 memset(kparams, 0, sizeof(*kparams)); 3143 kparams->kernel_type = HTP_IM2COL_KERNEL_UNSUPPORTED; 3144 3145 const struct ggml_tensor * src0 = op->src[0]; 3146 const struct ggml_tensor * src1 = op->src[1]; 3147 const struct ggml_tensor * dst = op; 3148 3149 if (!src0 || !src1 || !dst) { 3150 return false; 3151 } 3152 if (src1->type != GGML_TYPE_F32 || (dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_F32)) { 3153 return false; 3154 } 3155 if (src1->nb[0] != sizeof(float) || !ggml_is_contiguous(dst)) { 3156 return false; 3157 } 3158 3159 const int32_t * params = (const int32_t *) op->op_params; 3160 const int32_t s0 = params[0], s1 = params[1], s2 = params[2]; 3161 const int32_t p0 = params[3], p1 = params[4], p2 = params[5]; 3162 const int32_t d0 = params[6], d1 = params[7], d2 = params[8]; 3163 const int32_t ic = params[9]; 3164 3165 if (s0 <= 0 || s1 <= 0 || s2 <= 0 || 3166 p0 < 0 || p1 < 0 || p2 < 0 || 3167 d0 <= 0 || d1 <= 0 || d2 <= 0 || ic <= 0) { 3168 return false; 3169 } 3170 if (src1->ne[3] % ic != 0) { 3171 return false; 3172 } 3173 3174 const uint32_t IC = (uint32_t) ic; 3175 const uint32_t N = (uint32_t) (src1->ne[3] / IC); 3176 if (N == 0) { 3177 return false; 3178 } 3179 3180 uint32_t OW, OH, OD; 3181 if (!im2col_3d_calc_output_size(src1->ne[0], src0->ne[0], s0, p0, d0, &OW) || 3182 !im2col_3d_calc_output_size(src1->ne[1], src0->ne[1], s1, p1, d1, &OH) || 3183 !im2col_3d_calc_output_size(src1->ne[2], src0->ne[2], s2, p2, d2, &OD)) { 3184 return false; 3185 } 3186 3187 const uint32_t KD = (uint32_t) src0->ne[2]; 3188 const uint32_t KH = (uint32_t) src0->ne[1]; 3189 const uint32_t KW = (uint32_t) src0->ne[0]; 3190 const uint32_t ID = (uint32_t) src1->ne[2]; 3191 const uint32_t IH = (uint32_t) src1->ne[1]; 3192 const uint32_t IW = (uint32_t) src1->ne[0]; 3193 3194 const uint64_t patch_stride_64 = (uint64_t) IC * KD * KH * KW; 3195 const uint64_t total_rows_64 = (uint64_t) N * OD * OH; 3196 const uint64_t dst_ne3_64 = (uint64_t) OD * N; 3197 3198 if (patch_stride_64 > UINT32_MAX || total_rows_64 > UINT32_MAX || dst_ne3_64 > UINT32_MAX) { 3199 return false; 3200 } 3201 const uint32_t patch_stride = (uint32_t) patch_stride_64; 3202 const uint32_t total_rows = (uint32_t) total_rows_64; 3203 const uint32_t dst_ne3 = (uint32_t) dst_ne3_64; 3204 3205 if (dst->ne[0] != patch_stride || dst->ne[1] != OW || dst->ne[2] != OH || dst->ne[3] != dst_ne3) { 3206 return false; 3207 } 3208 3209 const uint32_t dst_elem = (dst->type == GGML_TYPE_F16) ? sizeof(ggml_fp16_t) : sizeof(float); 3210 3211 const bool is_patchembed = ggml_is_contiguous(src1) && 3212 (s0 == (int32_t) KW && s1 == (int32_t) KH && s2 == (int32_t) KD && 3213 p0 == 0 && p1 == 0 && p2 == 0 && 3214 d0 == 1 && d1 == 1 && d2 == 1); 3215 3216 const uint32_t max_threads = (sess && htp_session_n_threads(sess) > 0) ? htp_session_n_threads(sess) : 8; 3217 const uint32_t n_threads = (total_rows < max_threads) ? total_rows : max_threads; 3218 const uint32_t vtcm_size = (sess && htp_session_vtcm_size(sess) > 0) ? (uint32_t) htp_session_vtcm_size(sess) : 0; 3219 const uint32_t row_size = (uint32_t) dst->nb[2]; 3220 const uint32_t rows_per_chunk = (row_size > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32(row_size, HEX_L2_LINE_SIZE)) : 1; 3221 3222 if (is_patchembed) { 3223 const uint64_t src_row_bytes_raw = (uint64_t) IC * KD * KH * IW * sizeof(float); 3224 const uint64_t dst_row_bytes_raw = (uint64_t) OW * patch_stride * dst_elem; 3225 if (src_row_bytes_raw <= UINT32_MAX && dst_row_bytes_raw <= UINT32_MAX) { 3226 const uint32_t src_row_bytes = hex_round_up((uint32_t) src_row_bytes_raw, 256); 3227 const uint32_t dst_row_bytes = hex_round_up((uint32_t) dst_row_bytes_raw, 256); 3228 3229 struct htp_im2col_vtcm_layout layout; 3230 htp_im2col_vtcm_layout_build(&layout, src_row_bytes, dst_row_bytes, n_threads, 256); 3231 3232 if (vtcm_size == 0 || layout.total_bytes <= vtcm_size) { 3233 kparams->kernel_type = HTP_IM2COL_KERNEL_PATCHEMBED_DMA; 3234 kparams->n_threads = (uint8_t) n_threads; 3235 kparams->vtcm_size = (uint32_t) layout.total_bytes; 3236 kparams->src_row_bytes = src_row_bytes; 3237 kparams->dst_row_bytes = dst_row_bytes; 3238 kparams->owb = OW; 3239 kparams->wb = IW; 3240 kparams->nbpr = 1; 3241 kparams->N = N; 3242 kparams->IC = IC; 3243 kparams->ID = ID; 3244 kparams->IH = IH; 3245 kparams->IW = IW; 3246 kparams->KD = KD; 3247 kparams->KH = KH; 3248 kparams->KW = KW; 3249 kparams->OD = OD; 3250 kparams->OH = OH; 3251 kparams->OW = OW; 3252 kparams->patch_stride = patch_stride; 3253 kparams->total_rows = total_rows; 3254 kparams->rows_per_chunk = rows_per_chunk; 3255 kparams->div_OH = init_fastdiv_values(OH); 3256 kparams->div_OD = init_fastdiv_values(OD); 3257 return true; 3258 } 3259 } 3260 } 3261 3262 for (uint32_t owb = (OW < 256 ? OW : 256); owb >= 1; owb--) { 3263 const uint64_t Wb = (uint64_t) (owb - 1) * s0 + (uint64_t) (KW - 1) * d0 + 1; 3264 if (Wb > UINT32_MAX) { 3265 if (owb == 1) break; 3266 continue; 3267 } 3268 3269 const uint64_t src_row_bytes_raw = (uint64_t) IC * KD * KH * Wb * sizeof(float); 3270 const uint64_t dst_row_bytes_raw = (uint64_t) owb * patch_stride * dst_elem; 3271 if (src_row_bytes_raw > UINT32_MAX || dst_row_bytes_raw > UINT32_MAX) { 3272 if (owb == 1) break; 3273 continue; 3274 } 3275 3276 const uint32_t src_row_bytes = hex_round_up((uint32_t) src_row_bytes_raw, 256); 3277 const uint32_t dst_row_bytes = hex_round_up((uint32_t) dst_row_bytes_raw, 256); 3278 3279 struct htp_im2col_vtcm_layout layout; 3280 htp_im2col_vtcm_layout_build(&layout, src_row_bytes, dst_row_bytes, n_threads, 256); 3281 3282 if (vtcm_size == 0 || layout.total_bytes <= vtcm_size) { 3283 kparams->kernel_type = HTP_IM2COL_KERNEL_BLOCKED_DMA; 3284 kparams->n_threads = (uint8_t) n_threads; 3285 kparams->vtcm_size = (uint32_t) layout.total_bytes; 3286 kparams->src_row_bytes = src_row_bytes; 3287 kparams->dst_row_bytes = dst_row_bytes; 3288 kparams->owb = owb; 3289 kparams->wb = (uint32_t) Wb; 3290 kparams->nbpr = (OW + owb - 1) / owb; 3291 kparams->N = N; 3292 kparams->IC = IC; 3293 kparams->ID = ID; 3294 kparams->IH = IH; 3295 kparams->IW = IW; 3296 kparams->KD = KD; 3297 kparams->KH = KH; 3298 kparams->KW = KW; 3299 kparams->OD = OD; 3300 kparams->OH = OH; 3301 kparams->OW = OW; 3302 kparams->patch_stride = patch_stride; 3303 kparams->total_rows = total_rows; 3304 kparams->rows_per_chunk = rows_per_chunk; 3305 kparams->div_OH = init_fastdiv_values(OH); 3306 kparams->div_OD = init_fastdiv_values(OD); 3307 return true; 3308 } 3309 if (owb == 1) break; 3310 } 3311 3312 return false; 3313 } 3314 3315 static bool htp_kparams_solve_pad( 3316 const struct ggml_hexagon_session * sess, 3317 const struct ggml_tensor * op, 3318 struct htp_pad_kernel_params * kparams 3319 ) { 3320 memset(kparams, 0, sizeof(*kparams)); 3321 3322 const struct ggml_tensor * src0 = op->src[0]; 3323 const struct ggml_tensor * dst = op; 3324 3325 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 3326 return false; 3327 } 3328 3329 const int32_t lp0 = ((const int32_t *) op->op_params)[0]; 3330 const int32_t rp0 = ((const int32_t *) op->op_params)[1]; 3331 const bool circular = (((const int32_t *) op->op_params)[8] != 0); 3332 3333 if (circular && (lp0 > src0->ne[0] || rp0 > src0->ne[0])) { 3334 return false; 3335 } 3336 3337 const uint32_t type_size = (uint32_t) ggml_type_size(src0->type); 3338 const uint32_t total_dst_rows = (uint32_t) (dst->ne[1] * dst->ne[2] * dst->ne[3]); 3339 const uint32_t max_threads = (std::max)(1u, total_dst_rows); 3340 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 3341 3342 struct htp_pad_vtcm_layout layout; 3343 htp_pad_vtcm_layout_build(&layout, (uint32_t) src0->ne[0] * type_size, (uint32_t) dst->ne[0] * type_size, n_threads); 3344 3345 const bool use_dma = (src0->nb[0] == type_size) && (src0->ne[0] >= 512) && 3346 (htp_session_vtcm_size(sess) >= layout.total_bytes); 3347 3348 kparams->n_threads = n_threads; 3349 kparams->total_rows = total_dst_rows; 3350 kparams->use_dma = use_dma ? 1 : 0; 3351 kparams->elem_size = type_size; 3352 kparams->vtcm_size = use_dma ? (uint32_t) layout.total_bytes : 0; 3353 3354 if (circular && use_dma) { 3355 kparams->kernel_type = HTP_PAD_KERNEL_HVX_CIRCULAR_DMA; 3356 } else if (circular) { 3357 kparams->kernel_type = HTP_PAD_KERNEL_HVX_CIRCULAR; 3358 } else if (use_dma) { 3359 kparams->kernel_type = HTP_PAD_KERNEL_HVX_DMA; 3360 } else { 3361 kparams->kernel_type = HTP_PAD_KERNEL_HVX; 3362 } 3363 3364 kparams->div_ne1 = init_fastdiv_values((uint32_t) dst->ne[1]); 3365 kparams->div_ne1_ne2 = init_fastdiv_values((uint32_t) (dst->ne[1] * dst->ne[2])); 3366 kparams->div_ne01 = init_fastdiv_values((uint32_t) src0->ne[1]); 3367 kparams->div_ne02 = init_fastdiv_values((uint32_t) src0->ne[2]); 3368 kparams->div_ne03 = init_fastdiv_values((uint32_t) src0->ne[3]); 3369 3370 return true; 3371 } 3372 3373 static bool htp_kparams_solve_cumsum( 3374 const struct ggml_hexagon_session * sess, 3375 const struct ggml_tensor * op, 3376 struct htp_cumsum_kernel_params * kparams 3377 ) { 3378 memset(kparams, 0, sizeof(*kparams)); 3379 3380 const struct ggml_tensor * src0 = op->src[0]; 3381 const struct ggml_tensor * dst = op; 3382 3383 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 3384 return false; 3385 } 3386 if (!ggml_is_contiguous(src0) || !ggml_is_contiguous(dst)) { 3387 return false; 3388 } 3389 3390 const uint32_t total_rows = (uint32_t) (src0->ne[1] * src0->ne[2] * src0->ne[3]); 3391 const uint32_t max_threads = (std::max)(1u, total_rows); 3392 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 3393 3394 struct htp_cumsum_vtcm_layout layout; 3395 htp_cumsum_vtcm_layout_build(&layout, (uint32_t) src0->nb[1], (uint32_t) dst->nb[1], n_threads); 3396 3397 const bool use_dma = (htp_session_vtcm_size(sess) >= layout.total_bytes); 3398 3399 kparams->n_threads = n_threads; 3400 kparams->total_rows = total_rows; 3401 kparams->use_dma = use_dma ? 1 : 0; 3402 kparams->vtcm_size = use_dma ? (uint32_t) layout.total_bytes : 0; 3403 3404 return true; 3405 } 3406 3407 // Shrink until the layout fits: halve tile_cols down to 1024 columns, then drop threads. False below the VTCM floor. 3408 static bool htp_top_k_mask_search(struct htp_top_k_mask_vtcm_layout * L, const struct htp_top_k_mask_shape * S, uint32_t kernel_type, 3409 size_t vtcm_size, uint32_t * n_threads, uint32_t * tile_cols) { 3410 const uint32_t min_cols = 1024; 3411 while (true) { 3412 htp_top_k_mask_vtcm_layout_build(L, S, kernel_type, *n_threads, *tile_cols); 3413 if (L->total_bytes <= vtcm_size) { 3414 return true; 3415 } 3416 if (*tile_cols > min_cols) { 3417 *tile_cols = (std::max)(min_cols, (uint32_t) hex_round_up(*tile_cols / 2, 64)); 3418 } else if (*n_threads > 1) { 3419 (*n_threads)--; 3420 } else { 3421 return false; 3422 } 3423 } 3424 } 3425 3426 // Row r of t (same ne[1..3] as dst) is at r * nb[1]: the NPU needs one stride per input. 3427 // A mask broadcasts by i mod ne: its rows are dense above nb[1], the divisors and dst ne[1..2] fit 16 bits. 3428 static bool htp_top_k_mask_mask_ok(const struct ggml_tensor * m, const struct ggml_tensor * dst) { 3429 return m->ne[1] <= UINT16_MAX && m->ne[2] <= UINT16_MAX && m->ne[3] <= UINT16_MAX && dst->ne[1] <= UINT16_MAX && dst->ne[2] <= UINT16_MAX && 3430 (m->ne[2] == 1 || m->nb[2] == m->nb[1] * m->ne[1]) && 3431 (m->ne[3] == 1 || m->nb[3] == m->nb[1] * m->ne[1] * m->ne[2]); 3432 } 3433 3434 static bool htp_top_k_mask_row_flat(const struct ggml_tensor * t, const struct ggml_tensor * dst) { 3435 return t->ne[1] == dst->ne[1] && t->ne[2] == dst->ne[2] && t->ne[3] == dst->ne[3] && 3436 (t->ne[2] == 1 || t->nb[2] == t->nb[1] * t->ne[1]) && 3437 (t->ne[3] == 1 || t->nb[3] == t->nb[1] * t->ne[1] * t->ne[2]); 3438 } 3439 3440 static bool htp_kparams_solve_top_k_mask( 3441 const struct ggml_hexagon_session * sess, 3442 const struct ggml_tensor * op, 3443 struct htp_top_k_mask_kernel_params * kparams 3444 ) { 3445 memset(kparams, 0, sizeof(*kparams)); 3446 3447 const struct ggml_tensor * mask = op->src[0]; 3448 const struct ggml_tensor * top_k = op->src[1]; 3449 const struct ggml_tensor * score = op->src[2]; 3450 const struct ggml_tensor * blocks = op->src[3]; 3451 const struct ggml_tensor * extra = op->src[4]; 3452 const struct ggml_tensor * dst = op; 3453 3454 if ((mask->type != GGML_TYPE_F16 && mask->type != GGML_TYPE_F32) || dst->type != mask->type) { 3455 return false; 3456 } 3457 const size_t elem_size = ggml_type_size(dst->type); 3458 if (mask->nb[0] != elem_size || dst->nb[0] != elem_size || !ggml_is_contiguous(dst)) { 3459 return false; 3460 } 3461 if (top_k->type != GGML_TYPE_I32 || top_k->nb[0] != 4) { 3462 return false; 3463 } 3464 if (score && (score->type != GGML_TYPE_F32 || score->nb[0] != 4)) { 3465 return false; 3466 } 3467 if (extra && (extra->type != GGML_TYPE_I32 || extra->nb[0] != 4)) { 3468 return false; 3469 } 3470 // flat row addressing: inputs follow the dst rows, a mask with more than one row too 3471 if (!htp_top_k_mask_row_flat(top_k, dst) || (score && !htp_top_k_mask_row_flat(score, dst)) || 3472 (extra && !htp_top_k_mask_row_flat(extra, dst)) || (ggml_nrows(mask) > 1 && !htp_top_k_mask_mask_ok(mask, dst))) { 3473 return false; 3474 } 3475 // the kernel scales block ids by blocks->nb[1] with a 16-bit vector multiply 3476 if (blocks && (blocks->type != GGML_TYPE_I32 || blocks->nb[0] != 4 || blocks->nb[1] != (size_t) blocks->ne[0] * 4 || blocks->nb[1] > INT16_MAX)) { 3477 return false; 3478 } 3479 3480 struct htp_top_k_mask_shape S = {}; 3481 S.n_kv = (uint32_t) dst->ne[0]; 3482 S.elem_size = (uint32_t) elem_size; 3483 S.n_top = (uint32_t) top_k->ne[0]; 3484 S.n_blk = blocks ? (uint32_t) blocks->ne[0] : 0; 3485 S.n_pick = (uint32_t) (blocks ? blocks->ne[1] : score ? score->ne[0] : dst->ne[0]); 3486 S.n_extra = extra ? (uint32_t) extra->ne[0] : 0; 3487 const uint32_t total_rows = (uint32_t) ggml_nrows(dst); 3488 S.has_score = score ? 1 : 0; 3489 S.has_blocks = blocks ? 1 : 0; 3490 S.mask_resident = ggml_nrows(mask) == 1 ? 1 : 0; 3491 3492 // empty inputs would give out-of-range gathers and zero-length DMA rows: leave them to the CPU 3493 if (S.n_kv == 0 || total_rows == 0 || S.n_pick == 0 || S.n_top == 0 || (blocks && S.n_blk == 0) || (extra && S.n_extra == 0)) { 3494 return false; 3495 } 3496 3497 // PICKS for one row with a resident mask when its single row tile fits, ROWS otherwise 3498 const uint32_t sess_threads = (std::max)(1u, htp_session_n_threads(sess)); 3499 const size_t vtcm_size = htp_session_vtcm_size(sess); 3500 3501 uint32_t kernel_type = HTP_TOP_K_MASK_KERNEL_ROWS; 3502 uint32_t n_threads = sess_threads; 3503 uint32_t tile_cols = 0; 3504 3505 struct htp_top_k_mask_vtcm_layout L; 3506 bool found = false; 3507 if (total_rows == 1 && S.mask_resident && sess_threads > 1) { 3508 kernel_type = HTP_TOP_K_MASK_KERNEL_PICKS; 3509 tile_cols = hex_round_up(S.n_kv, 64); 3510 htp_top_k_mask_vtcm_layout_build(&L, &S, kernel_type, n_threads, tile_cols); 3511 found = L.total_bytes <= vtcm_size; 3512 } 3513 if (!found) { 3514 kernel_type = HTP_TOP_K_MASK_KERNEL_ROWS; 3515 n_threads = (std::max)(1u, (std::min)(sess_threads, total_rows)); 3516 tile_cols = hex_round_up(S.n_kv, 64); 3517 found = htp_top_k_mask_search(&L, &S, kernel_type, vtcm_size, &n_threads, &tile_cols); 3518 } 3519 if (!found) { 3520 return false; 3521 } 3522 3523 // compact kernel_params fields: sizes and offsets in HTP_TOP_K_MASK_UNIT bytes as u16, all multiples of the unit 3524 const uint64_t off_extra = htp_top_k_mask_rowin_off_extra(S.n_top); 3525 const uint64_t off_score = htp_top_k_mask_rowin_off_score(S.n_top, S.n_extra); 3526 const uint64_t u16_max = (uint64_t) UINT16_MAX * HTP_TOP_K_MASK_UNIT; 3527 if (L.rowin_bytes > u16_max || L.tile_bytes > u16_max || off_extra > u16_max || off_score > u16_max || L.off_scratch > u16_max) { 3528 return false; 3529 } 3530 const auto units = [](uint64_t bytes) { return (uint16_t) (bytes / HTP_TOP_K_MASK_UNIT); }; 3531 3532 kparams->n_threads = (uint8_t) n_threads; 3533 kparams->kernel_type = (uint8_t) kernel_type; 3534 kparams->flags = (dst->type == GGML_TYPE_F16 ? HTP_TOP_K_MASK_F16 : 0) | 3535 (score ? HTP_TOP_K_MASK_HAS_SCORE : 0) | 3536 (blocks ? HTP_TOP_K_MASK_HAS_BLOCKS : 0) | 3537 (extra ? HTP_TOP_K_MASK_HAS_EXTRA : 0) | 3538 (S.mask_resident ? HTP_TOP_K_MASK_MASK_RESIDENT : 0); 3539 kparams->elem_size = (uint8_t) elem_size; 3540 kparams->vtcm_size = (uint32_t) L.total_bytes; 3541 kparams->n_kv = S.n_kv; 3542 kparams->n_top = S.n_top; 3543 kparams->n_pick = S.n_pick; 3544 kparams->n_extra = S.n_extra; 3545 kparams->ninf = dst->type == GGML_TYPE_F16 ? 0xfc00fc00u : 0xff800000u; 3546 kparams->blocks_bytes = (uint32_t) L.blocks_bytes; 3547 kparams->score_bytes = score ? S.n_pick * 4 : 0; 3548 kparams->tile_cols = tile_cols; 3549 kparams->n_col_tiles = (S.n_kv + tile_cols - 1) / tile_cols; 3550 kparams->n_blk = (uint16_t) S.n_blk; 3551 kparams->blocks_nb1 = (uint16_t) (blocks ? blocks->nb[1] : 0); 3552 kparams->off_blocks_u = units(L.off_blocks); 3553 kparams->off_mask_u = units(L.off_mask); 3554 kparams->off_rowin_u = units(L.off_rowin); 3555 kparams->off_tiles_u = units(L.off_tiles); 3556 kparams->off_mtiles_u = units(L.off_mtiles); 3557 kparams->off_scratch_u = units(L.off_scratch); 3558 kparams->rowin_units = units(L.rowin_bytes); 3559 kparams->tile_units = units(L.tile_bytes); 3560 kparams->off_extra_units = units(off_extra); 3561 kparams->off_score_units = units(off_score); 3562 3563 if (kernel_type == HTP_TOP_K_MASK_KERNEL_PICKS) { 3564 // per thread slices of the static copies and of the flag clear, a multiple of the unit 3565 const auto slice = [&](uint64_t n) { return units(hex_round_up((n + n_threads - 1) / n_threads, HTP_TOP_K_MASK_UNIT)); }; 3566 kparams->u.picks.picks_per_thread = (uint32_t) hex_round_up((S.n_top + n_threads - 1) / n_threads, 32); 3567 kparams->u.picks.blocks_slice_u = slice(L.blocks_bytes); 3568 kparams->u.picks.top_slice_u = slice((uint64_t) S.n_top * 4); 3569 kparams->u.picks.extra_slice_u = slice((uint64_t) S.n_extra * 4); 3570 kparams->u.picks.score_slice_u = slice(kparams->score_bytes); 3571 kparams->u.picks.flags_slice_u = slice(L.tile_bytes); 3572 } else { 3573 kparams->u.rows.total_rows = total_rows; 3574 kparams->u.rows.rs_top = (uint32_t) top_k->nb[1]; 3575 kparams->u.rows.rs_extra = extra ? (uint32_t) extra->nb[1] : 0; 3576 kparams->u.rows.rs_score = score ? (uint32_t) score->nb[1] : 0; 3577 kparams->u.rows.rs_dst = (uint32_t) dst->nb[1]; 3578 kparams->u.rows.mask_nb1 = (uint32_t) mask->nb[1]; 3579 if (S.mask_resident) { 3580 kparams->u.rows.m.mask_bytes = (uint32_t) L.mask_bytes; 3581 } else { 3582 const int64_t d[5] = { dst->ne[1], dst->ne[2], mask->ne[1], mask->ne[2], mask->ne[3] }; 3583 for (int k = 0; k < 5; k++) { 3584 const struct fastdiv_values fd = init_fastdiv_values((uint32_t) d[k]); 3585 kparams->u.rows.m.dec.mp[k] = fd.mp; 3586 kparams->u.rows.m.dec.l[k] = (uint8_t) fd.l; 3587 kparams->u.rows.m.dec.ne[k] = (uint16_t) d[k]; 3588 } 3589 } 3590 } 3591 3592 return true; 3593 } 3594 3595 static bool htp_kparams_solve_fill( 3596 const struct ggml_hexagon_session * sess, 3597 const struct ggml_tensor * op, 3598 struct htp_fill_kernel_params * kparams 3599 ) { 3600 memset(kparams, 0, sizeof(*kparams)); 3601 3602 const struct ggml_tensor * dst = op; 3603 3604 if (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) { 3605 return false; 3606 } 3607 if (!ggml_is_contiguous_rows(dst) || ggml_is_permuted(dst)) { 3608 return false; 3609 } 3610 3611 const uint32_t ne1 = (uint32_t) dst->ne[1]; 3612 const uint32_t ne2 = (uint32_t) dst->ne[2]; 3613 const uint32_t ne3 = (uint32_t) dst->ne[3]; 3614 const uint32_t nb1 = (uint32_t) dst->nb[1]; 3615 3616 const uint32_t total_rows = ne1 * ne2 * ne3; 3617 const uint32_t max_threads = (std::max)(1u, total_rows); 3618 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 3619 const uint32_t elem_size = (uint32_t) ggml_type_size(dst->type); 3620 const uint32_t rows_per_chunk = (nb1 > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32(nb1, HEX_L2_LINE_SIZE)) : 1; 3621 3622 kparams->n_threads = n_threads; 3623 kparams->total_rows = total_rows; 3624 kparams->elem_size = elem_size; 3625 kparams->kernel_type = ggml_is_contiguous(dst) ? HTP_FILL_KERNEL_CONTIGUOUS : HTP_FILL_KERNEL_STRIDED; 3626 kparams->rows_per_chunk = rows_per_chunk; 3627 kparams->div_ne1 = init_fastdiv_values(ne1); 3628 kparams->div_ne1_ne2 = init_fastdiv_values(ne1 * ne2); 3629 3630 return true; 3631 } 3632 3633 static bool htp_kparams_solve_roll( 3634 const struct ggml_hexagon_session * sess, 3635 const struct ggml_tensor * op, 3636 struct htp_roll_kernel_params * kparams 3637 ) { 3638 memset(kparams, 0, sizeof(*kparams)); 3639 3640 const struct ggml_tensor * src0 = op->src[0]; 3641 const struct ggml_tensor * dst = op; 3642 3643 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 3644 return false; 3645 } 3646 if (!ggml_are_same_shape(src0, dst)) { 3647 return false; 3648 } 3649 if (!ggml_is_contiguous_rows(src0) || !ggml_is_contiguous(dst)) { 3650 return false; 3651 } 3652 3653 const uint32_t ne0 = (uint32_t) dst->ne[0]; 3654 const uint32_t ne1 = (uint32_t) dst->ne[1]; 3655 const uint32_t ne2 = (uint32_t) dst->ne[2]; 3656 const uint32_t ne3 = (uint32_t) dst->ne[3]; 3657 3658 const int32_t s0 = ((const int32_t *) op->op_params)[0]; 3659 uint32_t i0_src0 = 0; 3660 if (-s0 < 0) { 3661 i0_src0 = (uint32_t) (-s0 + (int32_t) ne0); 3662 } else if ((uint32_t) (-s0) >= ne0) { 3663 i0_src0 = (uint32_t) (-s0) - ne0; 3664 } else { 3665 i0_src0 = (uint32_t) (-s0); 3666 } 3667 3668 const uint32_t total_rows = ne1 * ne2 * ne3; 3669 const uint32_t max_threads = (std::max)(1u, total_rows); 3670 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 3671 3672 kparams->n_threads = n_threads; 3673 kparams->total_rows = total_rows; 3674 kparams->i0_src0 = i0_src0; 3675 kparams->n0 = ne0 - i0_src0; 3676 3677 if (htp_session_mdev_count(sess) <= 1) { 3678 if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) { 3679 kparams->kernel_type = HTP_ROLL_KERNEL_DMA_CONTIGUOUS; 3680 } else { 3681 kparams->kernel_type = HTP_ROLL_KERNEL_DMA_STRIDED; 3682 } 3683 } else { 3684 kparams->kernel_type = HTP_ROLL_KERNEL_HVX; 3685 } 3686 3687 kparams->div_ne1 = init_fastdiv_values(ne1); 3688 kparams->div_ne2_ne1 = init_fastdiv_values(ne2 * ne1); 3689 3690 return true; 3691 } 3692 3693 static bool htp_kparams_solve_diag( 3694 const struct ggml_hexagon_session * sess, 3695 const struct ggml_tensor * op, 3696 struct htp_diag_kernel_params * kparams 3697 ) { 3698 memset(kparams, 0, sizeof(*kparams)); 3699 3700 const struct ggml_tensor * src0 = op->src[0]; 3701 const struct ggml_tensor * dst = op; 3702 3703 if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 3704 return false; 3705 } 3706 if (src0->ne[1] != 1 || dst->ne[0] != dst->ne[1] || dst->ne[0] != src0->ne[0]) { 3707 return false; 3708 } 3709 if (!ggml_is_contiguous_rows(src0) || !ggml_is_contiguous_rows(dst) || 3710 ggml_is_permuted(src0) || ggml_is_permuted(dst)) { 3711 return false; 3712 } 3713 3714 const uint32_t total_batches = (uint32_t) (src0->ne[2] * src0->ne[3]); 3715 const uint32_t max_threads = (std::max)(1u, total_batches); 3716 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), max_threads); 3717 const uint32_t dst_batch_size = (uint32_t) (dst->ne[1] * dst->nb[1]); 3718 3719 bool can_split = (dst->ne[0] == 1 || dst->nb[0] == sizeof(float)) && !ggml_is_permuted(dst); 3720 uint32_t batches_per_chunk = 1; 3721 if (can_split) { 3722 if (dst->ne[2] > 1 && (dst->nb[2] & (HEX_L2_LINE_SIZE - 1)) == 0 && 3723 (dst->ne[3] <= 1 || (dst->nb[3] & (HEX_L2_LINE_SIZE - 1)) == 0)) { 3724 batches_per_chunk = 1; 3725 } else if (dst->nb[2] == dst_batch_size && 3726 (dst->ne[3] <= 1 || dst->nb[3] == dst->nb[2] * dst->ne[2])) { 3727 batches_per_chunk = (dst_batch_size > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32(dst_batch_size, HEX_L2_LINE_SIZE)) : 1; 3728 } else { 3729 can_split = false; 3730 } 3731 } 3732 3733 const uint32_t src_batch_size = (uint32_t) (src0->ne[0] * sizeof(float)); 3734 const uint32_t dst_row_size = (uint32_t) (dst->ne[0] * sizeof(float)); 3735 3736 struct htp_diag_vtcm_layout layout; 3737 htp_diag_vtcm_layout_build(&layout, src_batch_size, dst_row_size, n_threads); 3738 3739 const bool use_dma = (htp_session_vtcm_size(sess) >= layout.total_bytes); 3740 3741 kparams->n_threads = n_threads; 3742 kparams->total_batches = total_batches; 3743 kparams->use_dma = use_dma ? 1 : 0; 3744 kparams->can_split = can_split ? 1 : 0; 3745 kparams->batches_per_chunk = batches_per_chunk; 3746 kparams->vtcm_size = use_dma ? (uint32_t) layout.total_bytes : 0; 3747 kparams->div_ne02 = init_fastdiv_values((uint32_t) src0->ne[2]); 3748 3749 return true; 3750 } 3751 3752 static bool htp_kparams_solve_solve_tri( 3753 const struct ggml_hexagon_session * sess, 3754 const struct ggml_tensor * op, 3755 struct htp_solve_tri_kernel_params * kparams 3756 ) { 3757 memset(kparams, 0, sizeof(*kparams)); 3758 3759 const struct ggml_tensor * src0 = op->src[0]; 3760 const struct ggml_tensor * src1 = op->src[1]; 3761 const struct ggml_tensor * dst = op; 3762 3763 if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 3764 return false; 3765 } 3766 if (src0->ne[0] != src0->ne[1] || src0->ne[1] != src1->ne[1] || 3767 src0->ne[2] != src1->ne[2] || src0->ne[3] != src1->ne[3] || 3768 !ggml_are_same_shape(src1, dst)) { 3769 return false; 3770 } 3771 3772 const uint32_t max_threads_sess = htp_session_n_threads(sess); 3773 const uint32_t k = (uint32_t) src1->ne[0]; 3774 const uint32_t col_block = 32; 3775 const uint32_t k_chunks = (k + col_block - 1) / col_block; 3776 const uint32_t total_batches = (uint32_t) (src0->ne[2] * src0->ne[3]); 3777 const bool batched = total_batches >= max_threads_sess; 3778 const uint32_t total_jobs = batched ? total_batches : (total_batches * k_chunks); 3779 const uint32_t max_threads = (std::max)(1u, total_jobs); 3780 const uint32_t n_threads = (std::min)(max_threads_sess, max_threads); 3781 const uint32_t batch_size = (uint32_t) dst->nb[2]; 3782 const uint32_t batches_per_chunk = (batch_size > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32(batch_size, HEX_L2_LINE_SIZE)) : 1; 3783 3784 kparams->n_threads = n_threads; 3785 kparams->total_batches = total_batches; 3786 kparams->total_jobs = total_jobs; 3787 kparams->k_chunks = k_chunks; 3788 kparams->col_block = col_block; 3789 kparams->batched = batched ? 1 : 0; 3790 kparams->batches_per_chunk = batches_per_chunk; 3791 kparams->div_ne02 = init_fastdiv_values((uint32_t) src0->ne[2]); 3792 kparams->div_k_chunks = init_fastdiv_values(k_chunks > 0 ? k_chunks : 1); 3793 3794 return true; 3795 } 3796 3797 // Implicit GEMM over sliding windows of fp16 input croutons (HMX activation:single), no im2col 3798 static bool htp_kparams_conv2d_win( 3799 const struct ggml_hexagon_session * sess, 3800 const struct ggml_tensor * dst, 3801 struct htp_conv2d_kernel_params * kp 3802 ) { 3803 if (htp_session_n_hmx(sess) == 0) { 3804 return false; 3805 } 3806 3807 const struct ggml_tensor * src0 = dst->src[0]; 3808 const struct ggml_tensor * src1 = dst->src[1]; 3809 const int32_t s0 = ggml_get_op_params_i32(dst, 0); 3810 const int32_t s1 = ggml_get_op_params_i32(dst, 1); 3811 const int32_t p0 = ggml_get_op_params_i32(dst, 2); 3812 const int32_t p1 = ggml_get_op_params_i32(dst, 3); 3813 const int32_t d0 = ggml_get_op_params_i32(dst, 4); 3814 const int32_t d1 = ggml_get_op_params_i32(dst, 5); 3815 3816 const uint32_t KW = src0->ne[0]; 3817 const uint32_t KH = src0->ne[1]; 3818 const uint32_t Cin = src0->ne[2]; 3819 const uint32_t Cout = src0->ne[3]; 3820 const uint32_t IW = src1->ne[0]; 3821 const uint32_t IH = src1->ne[1]; 3822 const uint32_t OW = dst->ne[0]; 3823 const uint32_t OH = dst->ne[1]; 3824 const uint32_t N = dst->ne[3]; 3825 const size_t es = ggml_type_size(src0->type); 3826 3827 if (s0 != 1 || s1 != 1 || p0 < 0 || p1 < 0 || dst->nb[0] != sizeof(float)) { 3828 return false; 3829 } 3830 if (dst->nb[1] < OW * sizeof(float) || (Cout > 1 && dst->nb[2] < OW * sizeof(float))) { 3831 return false; 3832 } 3833 3834 const bool whcn = src1->nb[0] == sizeof(float) && src1->nb[1] >= IW * sizeof(float) && (Cin == 1 || src1->nb[2] >= IH * src1->nb[1]); 3835 const bool cwhn = !whcn && src1->nb[2] == sizeof(float) && src1->nb[0] == Cin * sizeof(float) && src1->nb[1] >= IW * src1->nb[0]; 3836 if (!whcn && !cwhn) { 3837 return false; 3838 } 3839 3840 const bool w_rows = src0->nb[0] == es && (KH == 1 || src0->nb[1] == KW * es) && (Cin == 1 || src0->nb[2] == KW * KH * es); 3841 const bool co_c = (Cout == 1 || src0->nb[3] == es) && (Cin == 1 || src0->nb[2] >= (std::min)(Cout, 32u) * es); 3842 if (!w_rows && !co_c) { 3843 return false; 3844 } 3845 3846 // short output rows: fold the kw taps into channels so the virtual row is OW wide and output rows are contiguous 3847 const bool fold = (whcn || (cwhn && d0 == 1)) && KW > 1 && KW * Cin <= 32 && OW < 64 && KW * KH <= 16 && Cout >= 16; 3848 3849 const uint32_t T = KW * KH; 3850 const uint32_t n_ch = fold ? KW * Cin : Cin; 3851 const uint32_t n_cc = (n_ch + 31) / 32; 3852 const uint32_t rem = n_ch - (n_cc - 1) * 32; 3853 const uint32_t stop = (rem + 7) / 8 * 8 - 1; 3854 const uint32_t SR = fold ? OW : IW + 2 * p0; 3855 const uint32_t IHS = IH + 2 * p1; 3856 const uint32_t n_wt = fold ? KH : T; 3857 const uint32_t moff = (KH - 1) * d1 * SR + (fold ? 0 : (KW - 1) * d0); 3858 const uint32_t halo = moff / 32 + 1; 3859 const uint32_t n_rt = (Cout + 31) / 32; 3860 const uint64_t n_q = (uint64_t) (N * IHS - (IHS - OH) - 1) * SR + OW; 3861 const uint32_t nth = kp->n_threads; 3862 const size_t tile = 2048; 3863 3864 if ((uint64_t) N * IHS * SR + moff + 64 >= (1ull << 31)) { 3865 return false; 3866 } 3867 const uint32_t n_tiles = (uint32_t) ((n_q + 31) / 32); 3868 3869 // weight packing scratch 3870 uint32_t w_cs = 0; 3871 size_t wraw = 0; 3872 size_t wscr = 0; 3873 if (w_rows) { 3874 const uint32_t kpw = es == 2 ? 64 : 32; 3875 for (uint32_t cs = 32; cs >= 2; cs /= 2) { 3876 w_cs = cs; 3877 wraw = (size_t) 32 * ((cs * T + kpw - 1) / kpw) * 128; 3878 if (3 * wraw + 64 * 128 <= 256 * 1024 || (fold && cs == 32)) { 3879 break; 3880 } 3881 } 3882 // enough packing units for all threads 3883 const uint32_t kpw2 = es == 2 ? 64 : 32; 3884 while (!fold && w_cs > 2 && n_rt * ((Cin + 31) / 32) * (32 / w_cs) < kp->n_threads) { 3885 w_cs /= 2; 3886 } 3887 wraw = (std::min)(wraw, (size_t) 32 * ((w_cs * T + kpw2 - 1) / kpw2) * 128); 3888 wscr = 3 * wraw + 64 * 128 + 128; 3889 } else { 3890 w_cs = fold ? KW : (std::min)(T, 16u); 3891 while (!fold && w_cs > 1 && n_rt * ((Cin + 31) / 32) * ((T + w_cs - 1) / w_cs) < kp->n_threads) { 3892 w_cs = (w_cs + 1) / 2; 3893 } 3894 wscr = 2 * (size_t) w_cs * 32 * 128; 3895 } 3896 3897 uint32_t rt_block = n_rt; 3898 uint32_t ct = (std::max)(4 * halo, (n_tiles + 3) / 4); 3899 ct = (std::min)((std::max)(ct, 8u), 512u); 3900 ct = (std::min)(ct, n_tiles); 3901 uint32_t seg_b = 8; 3902 // longer output rows make the output DMA more efficient 3903 uint32_t seg_t = (std::min)(32u, (std::max)(8u, (OW + 31) / 32)); 3904 3905 while (rt_block > 1 && (size_t) rt_block * n_wt * n_cc * tile > htp_session_vtcm_size(sess) / 2) { 3906 rt_block = (rt_block + 1) / 2; 3907 } 3908 3909 size_t stage = 0, thr = 0, act = 0, out = 0, w_size = 0, total = 0; 3910 uint32_t n_bc = 0; 3911 while (true) { 3912 n_bc = ct + halo; 3913 stage = cwhn ? (size_t) seg_b * 32 * Cin * sizeof(float) + 256 : (size_t) n_ch * seg_b * 32 * sizeof(float); 3914 if (fold) { 3915 // raw input rows of the staged pixels with 128 bytes of read slack on both sides 3916 stage = (size_t) Cin * ((seg_b * 32 + OW - 1) / OW + 1) * IW * sizeof(float) + 256; 3917 } 3918 stage = hex_round_up(stage, 128); 3919 thr = hex_round_up((std::max)(2 * stage + (size_t) HTP_CONV2D_WIN_OBUF * 32 * seg_t * 32 * sizeof(float), wscr), tile); 3920 w_size = (size_t) rt_block * n_wt * n_cc * tile; 3921 act = (size_t) n_cc * n_bc * tile; 3922 out = (size_t) rt_block * ct * tile; 3923 struct htp_conv2d_vtcm_layout L; 3924 htp_conv2d_vtcm_layout_build(&L, rt_block, w_size, act, out, thr, nth); 3925 total = L.total_bytes; 3926 if (total <= htp_session_vtcm_size(sess)) { 3927 break; 3928 } 3929 if (ct > 64) { 3930 ct /= 2; 3931 } else if (rt_block > 1) { 3932 rt_block = (rt_block + 1) / 2; 3933 } else if (seg_b > 1 || seg_t > 1) { 3934 seg_b = (std::max)(seg_b / 2, 1u); 3935 seg_t = (std::max)(seg_t / 2, 1u); 3936 } else if (ct > 1) { 3937 ct /= 2; 3938 } else { 3939 return false; 3940 } 3941 } 3942 3943 if (hex_round_up((size_t) T * sizeof(uint32_t), 128) + 2 * hex_round_up((size_t) ct, 128) > HTP_DDR_SPAD_SIZE) { 3944 return false; 3945 } 3946 3947 const uint32_t TG = w_cs; 3948 const uint32_t CS = w_cs; 3949 const uint32_t n_tg = (T + TG - 1) / TG; 3950 const uint32_t n_cs = (32 + CS - 1) / CS; 3951 3952 kp->kernel_type = (uint8_t) HTP_CONV2D_KERNEL_HMX_WINDOW; 3953 kp->n_threads = (uint8_t) nth; 3954 kp->cwhn = cwhn ? 1 : 0; 3955 kp->w_kmajor = !w_rows ? 1 : 0; 3956 kp->n_row_tiles = n_rt; 3957 kp->ct_chunk = ct; 3958 kp->rt_block = rt_block; 3959 kp->stage_size = stage; 3960 kp->thr_size = thr; 3961 kp->vtcm_size = total; 3962 kp->window.fold = fold; 3963 kp->window.n_cc = n_cc; 3964 kp->window.act_rt_last = 0x780 | 0x2 | (stop << 2); 3965 kp->window.sr = SR; 3966 kp->window.ihs = IHS; 3967 kp->window.max_off = moff; 3968 kp->window.n_bc = n_bc; 3969 kp->window.seg_b = seg_b; 3970 kp->window.seg_t = seg_t; 3971 kp->window.w_cs = w_cs; 3972 kp->window.div_sr = init_fastdiv_values(SR); 3973 kp->window.div_ihs = init_fastdiv_values(IHS); 3974 kp->window.div_kw = init_fastdiv_values(KW); 3975 kp->window.div_tg = init_fastdiv_values(n_tg); 3976 kp->window.div_cc = init_fastdiv_values(n_cc); 3977 kp->window.div_cs = init_fastdiv_values(n_cs); 3978 return true; 3979 } 3980 3981 // Tiled implicit GEMM, MMA on HMX if available, else HVX 3982 static bool htp_kparams_conv2d_tiled( 3983 const struct ggml_hexagon_session * sess, 3984 const struct ggml_tensor * dst, 3985 struct htp_conv2d_kernel_params * kp 3986 ) { 3987 if (htp_session_n_hmx(sess) == 0) { 3988 return false; 3989 } 3990 3991 const struct ggml_tensor * src0 = dst->src[0]; 3992 const struct ggml_tensor * src1 = dst->src[1]; 3993 const int32_t s0 = ggml_get_op_params_i32(dst, 0); 3994 const int32_t d0 = ggml_get_op_params_i32(dst, 4); 3995 3996 const uint32_t KW = src0->ne[0]; 3997 const uint32_t KH = src0->ne[1]; 3998 const uint32_t Cin = src0->ne[2]; 3999 const uint32_t Cout = src0->ne[3]; 4000 const uint32_t OW = dst->ne[0]; 4001 const uint32_t OH = dst->ne[1]; 4002 const uint32_t N = dst->ne[3]; 4003 const size_t es = ggml_type_size(src0->type); 4004 4005 if (s0 != 1 || dst->nb[0] != sizeof(float)) { 4006 return false; 4007 } 4008 4009 // kernel k order must match the staged input: (cin, kh, kw) for WHCN input, (kh, kw, cin) for CWHN input. 4010 // Kernel is either k-contiguous rows per cout (WHCN only) or cout-contiguous with a uniform k stride. 4011 const bool whcn = src1->nb[0] == sizeof(float); 4012 const bool cwhn = !whcn && src1->nb[2] == sizeof(float) && src1->nb[0] == Cin * sizeof(float); 4013 const bool co_c = Cout == 1 || src0->nb[3] == es; 4014 const bool w_rows = whcn && src0->nb[0] == es && (KH == 1 || src0->nb[1] == KW * es) && (Cin == 1 || src0->nb[2] == KW * KH * es); 4015 const size_t ks = whcn ? src0->nb[0] : src0->nb[2]; 4016 bool kmajor = false; 4017 if (whcn) { 4018 kmajor = co_c && ks >= Cout * es && (KH == 1 || src0->nb[1] == KW * ks) && (Cin == 1 || src0->nb[2] == KW * KH * ks); 4019 } else if (cwhn) { 4020 kmajor = co_c && ks >= Cout * es && src0->nb[0] == Cin * ks && (KH == 1 || src0->nb[1] == KW * Cin * ks); 4021 } 4022 if (!w_rows && !kmajor) { 4023 return false; 4024 } 4025 kmajor = kmajor && !w_rows; 4026 4027 const uint32_t K = KW * KH * Cin; 4028 const uint32_t kg = !cwhn ? K : d0 == 1 ? KW * Cin : Cin; 4029 const uint32_t n_grp = K / kg; 4030 const uint32_t kg_p = hex_round_up(kg, 32); 4031 const uint32_t k_pad = hex_round_up(n_grp * kg_p, 64); 4032 const uint32_t n_kt = k_pad / 32; 4033 const uint32_t n_rt = (Cout + 31) / 32; 4034 const uint32_t n_owt = (OW + 31) / 32; 4035 const uint32_t n_ct = N * OH * n_owt; 4036 const uint32_t wk = (std::min)(k_pad, 512u); 4037 const uint32_t nth = htp_session_n_threads(sess); 4038 const size_t tile = 2048; 4039 4040 uint32_t rt_block = n_rt; 4041 uint32_t ct = (std::min)(n_ct, 64u); 4042 uint32_t seg = (std::min)(8u, n_owt); 4043 uint32_t n_stage = 1; 4044 uint32_t sr = 0; 4045 size_t stage = 0, ostage = 0, prow = 0, wpack = 0; 4046 size_t w_size = 0, act_size = 0, out_size = 0, thr_size = 0, total = 0; 4047 4048 while (true) { 4049 const uint32_t wb = 32 * seg + (KW - 1) * d0; 4050 sr = cwhn ? hex_round_up(wb * Cin, 64) : hex_round_up(wb, 32); 4051 stage = cwhn ? (size_t) KH * sr * sizeof(float) : (size_t) Cin * KH * sr * sizeof(float); 4052 ostage = 2 * 32 * (size_t) seg * 32 * sizeof(float); 4053 prow = cwhn ? stage / 2 + 256 + 64 * 128 : 0; 4054 wpack = kmajor ? 2 * (size_t) wk * 64 * sizeof(float) + 96 * 128 : 2 * (size_t) 32 * wk * (sizeof(float) + sizeof(uint16_t)); 4055 thr_size = hex_round_up((std::max)(stage + ostage + prow, wpack), tile); 4056 w_size = (size_t) rt_block * n_kt * tile; 4057 act_size = (size_t) ct * n_kt * tile; 4058 out_size = (size_t) rt_block * ct * tile; 4059 struct htp_conv2d_vtcm_layout L; 4060 htp_conv2d_vtcm_layout_build(&L, rt_block, w_size, act_size, out_size, thr_size, nth); 4061 total = L.total_bytes; 4062 if (total <= htp_session_vtcm_size(sess)) { 4063 break; 4064 } 4065 if (ct > 8) { 4066 ct /= 2; 4067 } else if (seg > 1) { 4068 seg--; 4069 } else if (rt_block > 1) { 4070 rt_block = (rt_block + 1) / 2; 4071 } else if (ct > 1) { 4072 ct /= 2; 4073 } else { 4074 return false; 4075 } 4076 } 4077 4078 // Second stage buffer: DMA of the next segment overlaps HVX pack of this one. 4079 // Skip large stages. They already fill VTCM, and the extra DMA slows HMX. 4080 { 4081 const size_t thr2 = hex_round_up((std::max)(2 * stage + ostage + prow, wpack), tile); 4082 struct htp_conv2d_vtcm_layout L2; 4083 htp_conv2d_vtcm_layout_build(&L2, rt_block, w_size, act_size, out_size, thr2, nth); 4084 if (stage <= 128 * 1024 && L2.total_bytes <= htp_session_vtcm_size(sess)) { 4085 n_stage = 2; 4086 thr_size = thr2; 4087 total = L2.total_bytes; 4088 } 4089 } 4090 4091 kp->kernel_type = (uint8_t) HTP_CONV2D_KERNEL_HMX_TILED; 4092 kp->n_threads = (uint8_t) nth; 4093 kp->cwhn = cwhn ? 1 : 0; 4094 kp->w_kmajor = kmajor ? 1 : 0; 4095 kp->n_row_tiles = n_rt; 4096 kp->ct_chunk = ct; 4097 kp->rt_block = rt_block; 4098 kp->stage_size = stage; 4099 kp->thr_size = thr_size; 4100 kp->vtcm_size = total; 4101 kp->tiled.w_kstride = ks; 4102 kp->tiled.k_size = K; 4103 kp->tiled.k_group = kg; 4104 kp->tiled.k_group_pad = kg_p; 4105 kp->tiled.n_groups = n_grp; 4106 kp->tiled.n_k_tiles = n_kt; 4107 kp->tiled.n_ow_tiles = n_owt; 4108 kp->tiled.seg_tiles = seg; 4109 kp->tiled.stage_row = sr; 4110 kp->tiled.n_stage = n_stage; 4111 kp->tiled.wk_chunk = wk; 4112 kp->tiled.div_ow_tiles = init_fastdiv_values(n_owt); 4113 kp->tiled.div_oh = init_fastdiv_values(OH); 4114 return true; 4115 } 4116 4117 static bool htp_kparams_solve_conv2d_unchecked( 4118 const struct ggml_hexagon_session * sess, 4119 const struct ggml_tensor * dst, 4120 struct htp_conv2d_kernel_params * kparams 4121 ) { 4122 memset(kparams, 0, sizeof(*kparams)); 4123 4124 const uint32_t total_rows = (uint32_t) (dst->ne[1] * dst->ne[2] * dst->ne[3]); 4125 kparams->total_rows = total_rows; 4126 kparams->n_threads = total_rows == 0 ? 1 : (std::min)((uint32_t) htp_session_n_threads(sess), total_rows); 4127 4128 if (htp_kparams_conv2d_win(sess, dst, kparams)) { 4129 return true; 4130 } 4131 4132 return htp_kparams_conv2d_tiled(sess, dst, kparams); 4133 } 4134 4135 static bool htp_kparams_solve_conv2d( 4136 const struct ggml_hexagon_session * sess, 4137 const struct ggml_tensor * op, 4138 struct htp_conv2d_kernel_params * kparams 4139 ) { 4140 memset(kparams, 0, sizeof(*kparams)); 4141 4142 const struct ggml_tensor * src0 = op->src[0]; 4143 const struct ggml_tensor * src1 = op->src[1]; 4144 const struct ggml_tensor * dst = op; 4145 4146 if ((src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_F32) || 4147 src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 4148 return false; 4149 } 4150 4151 if (src0->ne[2] != src1->ne[2]) { 4152 return false; 4153 } 4154 4155 const int32_t s0 = ggml_get_op_params_i32(op, 0); 4156 const int32_t s1 = ggml_get_op_params_i32(op, 1); 4157 const int32_t p0 = ggml_get_op_params_i32(op, 2); 4158 const int32_t p1 = ggml_get_op_params_i32(op, 3); 4159 const int32_t d0 = ggml_get_op_params_i32(op, 4); 4160 const int32_t d1 = ggml_get_op_params_i32(op, 5); 4161 4162 const int64_t ow = (src1->ne[0] + 2 * p0 - d0 * (src0->ne[0] - 1) - 1) / s0 + 1; 4163 const int64_t oh = (src1->ne[1] + 2 * p1 - d1 * (src0->ne[1] - 1) - 1) / s1 + 1; 4164 if (ow != dst->ne[0] || oh != dst->ne[1] || src0->ne[3] != dst->ne[2] || src1->ne[3] != dst->ne[3]) { 4165 return false; 4166 } 4167 4168 return htp_kparams_solve_conv2d_unchecked(sess, op, kparams); 4169 } 4170 4171 static bool htp_kparams_solve_conv2d_dw( 4172 const struct ggml_hexagon_session * sess, 4173 const struct ggml_tensor * op, 4174 struct htp_conv2d_dw_kernel_params * kparams 4175 ) { 4176 memset(kparams, 0, sizeof(*kparams)); 4177 4178 const struct ggml_tensor * src0 = op->src[0]; 4179 const struct ggml_tensor * src1 = op->src[1]; 4180 const struct ggml_tensor * dst = op; 4181 4182 if ((src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_F32) || 4183 src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 4184 return false; 4185 } 4186 4187 if (src0->ne[2] != 1 || src0->ne[3] != src1->ne[2]) { 4188 return false; 4189 } 4190 4191 if (src0->ne[0] * src0->ne[1] > 64) { 4192 return false; 4193 } 4194 4195 const int32_t s0 = ggml_get_op_params_i32(op, 0); 4196 const int32_t s1 = ggml_get_op_params_i32(op, 1); 4197 const int32_t p0 = ggml_get_op_params_i32(op, 2); 4198 const int32_t p1 = ggml_get_op_params_i32(op, 3); 4199 const int32_t d0 = ggml_get_op_params_i32(op, 4); 4200 const int32_t d1 = ggml_get_op_params_i32(op, 5); 4201 4202 if (s0 <= 0 || s1 <= 0 || d0 <= 0 || d1 <= 0) { 4203 return false; 4204 } 4205 4206 const int64_t ow = (src1->ne[0] + 2 * p0 - d0 * (src0->ne[0] - 1) - 1) / s0 + 1; 4207 const int64_t oh = (src1->ne[1] + 2 * p1 - d1 * (src0->ne[1] - 1) - 1) / s1 + 1; 4208 if (ow != dst->ne[0] || oh != dst->ne[1] || dst->ne[2] != src1->ne[2] || dst->ne[3] != src1->ne[3]) { 4209 return false; 4210 } 4211 4212 const bool cwhn = ggml_is_contiguous_channels(src1); 4213 if (!cwhn && !ggml_is_contiguous(src1)) { 4214 return false; 4215 } 4216 4217 if (!cwhn && (d0 != 1 || (s0 != 1 && s0 != 2))) { 4218 return false; 4219 } 4220 4221 const uint32_t c = (uint32_t) src1->ne[2]; 4222 const uint32_t n = (uint32_t) src1->ne[3]; 4223 const uint32_t w = (uint32_t) src1->ne[0]; 4224 const uint32_t h = (uint32_t) src1->ne[1]; 4225 const uint32_t kw = (uint32_t) src0->ne[0]; 4226 const uint32_t kh = (uint32_t) src0->ne[1]; 4227 const uint32_t ow_u = (uint32_t) ow; 4228 const uint32_t oh_u = (uint32_t) oh; 4229 4230 const uint32_t row_src_bytes = cwhn 4231 ? (w * c * (uint32_t) sizeof(float)) 4232 : (w * (uint32_t) sizeof(float)); 4233 const uint32_t row_dst_bytes = cwhn 4234 ? (ow_u * c * (uint32_t) sizeof(float)) 4235 : (ow_u * (uint32_t) sizeof(float)); 4236 4237 uint8_t kernel_type = HTP_CONV2D_DW_KERNEL_UNSUPPORTED; 4238 if (cwhn) { 4239 kernel_type = HTP_CONV2D_DW_KERNEL_CWHN; 4240 } else if ((w & 31) == 0 && kw == 3 && kh == 3 && p0 == 1 && p1 == 1 && d0 == 1 && d1 == 1 && s0 == 1 && s1 == 1) { 4241 kernel_type = HTP_CONV2D_DW_KERNEL_WHCN_S1_K3X3; 4242 } else if ((w & 31) == 0 && kw == 3 && kh == 3 && p0 == 1 && p1 == 1 && d0 == 1 && d1 == 1 && s0 == 2 && s1 == 2) { 4243 if (w == 32 && ow_u == 16) { 4244 kernel_type = HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W32; 4245 } else if (w == 64 && ow_u == 32) { 4246 kernel_type = HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W64; 4247 } else { 4248 kernel_type = HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3; 4249 } 4250 } else if (s0 == 1) { 4251 kernel_type = HTP_CONV2D_DW_KERNEL_WHCN_S1; 4252 } else { 4253 kernel_type = HTP_CONV2D_DW_KERNEL_WHCN_S2; 4254 } 4255 4256 const bool fast_k3x3 = kernel_type >= HTP_CONV2D_DW_KERNEL_WHCN_S1_K3X3; 4257 const uint32_t pad_rows = fast_k3x3 ? 4 : 0; 4258 const uint32_t pad_top_stride = fast_k3x3 ? w : 0; 4259 4260 const size_t vtcm_budget = (sess && htp_session_vtcm_size(sess) > 0) ? htp_session_vtcm_size(sess) : 0; 4261 if (vtcm_budget == 0) { 4262 return false; 4263 } 4264 4265 const uint32_t w_size = (uint32_t) (kw * kh * c * ggml_type_size(src0->type)); 4266 4267 uint32_t max_threads_sess = (uint32_t) htp_session_n_threads(sess); 4268 uint32_t oh_chunk = oh_u; 4269 4270 uint32_t n_threads = max_threads_sess; 4271 struct htp_conv2d_dw_vtcm_layout layout; 4272 4273 while (true) { 4274 const uint32_t max_ih_chunk = (std::min)(h, (oh_chunk - 1) * (uint32_t) s1 + (kh - 1) * (uint32_t) d1 + 1); 4275 const uint32_t max_src_bytes = (max_ih_chunk + pad_rows) * row_src_bytes + 128; 4276 const uint32_t max_dst_bytes = oh_chunk * row_dst_bytes + 128; 4277 4278 htp_conv2d_dw_vtcm_layout_build(&layout, max_src_bytes, max_dst_bytes, w_size, n_threads); 4279 4280 if (layout.total_bytes <= vtcm_budget && layout.total_bytes > 0) { 4281 break; 4282 } 4283 4284 if (oh_chunk > 1) { 4285 oh_chunk = (oh_chunk + 1) / 2; 4286 } else if (n_threads > 1) { 4287 n_threads--; 4288 } else { 4289 return false; 4290 } 4291 } 4292 4293 const uint32_t n_chunks = (oh_u + oh_chunk - 1) / oh_chunk; 4294 const uint32_t total_tasks = cwhn ? (n * n_chunks) : (c * n * n_chunks); 4295 n_threads = (std::min)(n_threads, total_tasks); 4296 if (n_threads == 0) { 4297 n_threads = 1; 4298 } 4299 4300 const uint32_t max_ih_chunk = (std::min)(h, (oh_chunk - 1) * (uint32_t) s1 + (kh - 1) * (uint32_t) d1 + 1); 4301 const uint32_t max_src_bytes = (max_ih_chunk + pad_rows) * row_src_bytes + 128; 4302 const uint32_t max_dst_bytes = oh_chunk * row_dst_bytes + 128; 4303 htp_conv2d_dw_vtcm_layout_build(&layout, max_src_bytes, max_dst_bytes, w_size, n_threads); 4304 4305 kparams->kernel_type = kernel_type; 4306 kparams->n_threads = (uint8_t) n_threads; 4307 kparams->cwhn = cwhn ? 1 : 0; 4308 kparams->is_f16 = (src0->type == GGML_TYPE_F16) ? 1 : 0; 4309 kparams->total_tasks = total_tasks; 4310 kparams->vtcm_size = (uint32_t) layout.total_bytes; 4311 kparams->w = w; 4312 kparams->h = h; 4313 kparams->c = c; 4314 kparams->n = n; 4315 kparams->kw = kw; 4316 kparams->kh = kh; 4317 kparams->ow = ow_u; 4318 kparams->oh = oh_u; 4319 kparams->s0 = s0; 4320 kparams->s1 = s1; 4321 kparams->p0 = p0; 4322 kparams->p1 = p1; 4323 kparams->d0 = d0; 4324 kparams->d1 = d1; 4325 kparams->oh_chunk = oh_chunk; 4326 kparams->n_chunks = n_chunks; 4327 kparams->row_src_bytes = row_src_bytes; 4328 kparams->row_dst_bytes = row_dst_bytes; 4329 kparams->pad_top_stride = pad_top_stride; 4330 kparams->div_c = init_fastdiv_values(c); 4331 kparams->div_n_chunks = init_fastdiv_values(n_chunks > 0 ? n_chunks : 1); 4332 4333 return true; 4334 } 4335 4336 static bool htp_dsv4_hc_pre_can_collapse( 4337 const struct ggml_tensor * x, 4338 const struct ggml_tensor * weights, 4339 const struct ggml_tensor * dst 4340 ) { 4341 if (!x || !weights || !dst) { 4342 return false; 4343 } 4344 if (x->type != GGML_TYPE_F32 || weights->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 4345 return false; 4346 } 4347 if (x->nb[0] != sizeof(float) || weights->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float)) { 4348 return false; 4349 } 4350 if (x->ne[1] != 4 || dst->ne[0] != x->ne[0]) { 4351 return false; 4352 } 4353 4354 const bool gated = ggml_get_op_params_i32(dst, 1) != 0; 4355 const int64_t x_tokens = x->ne[2] * x->ne[3]; 4356 const int64_t d_tokens = dst->ne[1] * dst->ne[2] * dst->ne[3]; 4357 const int64_t w_tokens = gated ? (weights->ne[2] * weights->ne[3]) : (weights->ne[1] * weights->ne[2] * weights->ne[3]); 4358 4359 if (x_tokens != d_tokens || x_tokens != w_tokens) { 4360 return false; 4361 } 4362 4363 if (x->ne[3] == 1 && dst->ne[2] == 1 && dst->ne[3] == 1 && 4364 (gated ? (weights->ne[3] == 1) : (weights->ne[2] == 1 && weights->ne[3] == 1))) { 4365 return false; 4366 } 4367 4368 if (x->ne[3] > 1 && x->nb[3] != x->ne[2] * x->nb[2]) return false; 4369 4370 if (dst->ne[2] > 1 && dst->nb[2] != dst->ne[1] * dst->nb[1]) return false; 4371 if (dst->ne[3] > 1 && dst->nb[3] != dst->ne[2] * dst->nb[2]) return false; 4372 4373 if (gated) { 4374 if (weights->ne[3] > 1 && weights->nb[3] != weights->ne[2] * weights->nb[2]) return false; 4375 } else { 4376 if (weights->ne[2] > 1 && weights->nb[2] != weights->ne[1] * weights->nb[1]) return false; 4377 if (weights->ne[3] > 1 && weights->nb[3] != weights->ne[2] * weights->nb[2]) return false; 4378 } 4379 4380 return true; 4381 } 4382 4383 static void htp_dsv4_hc_pre_collapse_tensors( 4384 const struct ggml_tensor * x, 4385 const struct ggml_tensor * weights, 4386 const struct ggml_tensor * dst, 4387 struct ggml_tensor * x_c, 4388 struct ggml_tensor * w_c, 4389 struct ggml_tensor * d_c 4390 ) { 4391 const int64_t n_tokens = x->ne[2] * x->ne[3]; 4392 const bool gated = ggml_get_op_params_i32(dst, 1) != 0; 4393 4394 *x_c = *x; 4395 x_c->ne[2] = n_tokens; 4396 x_c->ne[3] = 1; 4397 x_c->nb[3] = x_c->ne[2] * x_c->nb[2]; 4398 4399 *d_c = *dst; 4400 d_c->ne[1] = n_tokens; 4401 d_c->ne[2] = 1; 4402 d_c->ne[3] = 1; 4403 d_c->nb[2] = d_c->ne[1] * d_c->nb[1]; 4404 d_c->nb[3] = d_c->nb[2]; 4405 4406 *w_c = *weights; 4407 if (gated) { 4408 w_c->ne[2] = n_tokens; 4409 w_c->ne[3] = 1; 4410 w_c->nb[3] = w_c->ne[2] * w_c->nb[2]; 4411 } else { 4412 w_c->ne[1] = n_tokens; 4413 w_c->ne[2] = 1; 4414 w_c->ne[3] = 1; 4415 w_c->nb[2] = w_c->ne[1] * w_c->nb[1]; 4416 w_c->nb[3] = w_c->nb[2]; 4417 } 4418 } 4419 4420 static bool htp_kparams_solve_dsv4_hc_pre_impl( 4421 const struct ggml_hexagon_session * sess, 4422 const struct ggml_tensor * x, 4423 const struct ggml_tensor * weights, 4424 const struct ggml_tensor * dst, 4425 struct htp_dsv4_hc_pre_kernel_params * kparams 4426 ) { 4427 memset(kparams, 0, sizeof(*kparams)); 4428 4429 if (x->type != GGML_TYPE_F32 || weights->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 4430 return false; 4431 } 4432 if (x->ne[1] != 4 || x->ne[3] != 1) { 4433 return false; 4434 } 4435 if (x->ne[0] % HTP_DSV4_HC_CHUNK_ELEMS != 0) { 4436 return false; 4437 } 4438 if (dst->ne[0] != x->ne[0] || dst->ne[1] != x->ne[2] || dst->ne[2] != 1 || dst->ne[3] != 1) { 4439 return false; 4440 } 4441 if (x->nb[0] != sizeof(float) || weights->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float)) { 4442 return false; 4443 } 4444 if (ggml_is_permuted(x) || ggml_is_permuted(weights) || ggml_is_permuted(dst)) { 4445 return false; 4446 } 4447 4448 const bool gated = ggml_get_op_params_i32(dst, 1) != 0; 4449 if (gated) { 4450 if (weights->ne[0] != x->ne[0] || weights->ne[1] != x->ne[1] || weights->ne[2] != x->ne[2] || weights->ne[3] != 1) { 4451 return false; 4452 } 4453 } else { 4454 if (weights->ne[0] != x->ne[1] || weights->ne[1] != x->ne[2] || weights->ne[2] != 1 || weights->ne[3] != 1) { 4455 return false; 4456 } 4457 } 4458 4459 const uint32_t n_tokens = (uint32_t) x->ne[2]; 4460 kparams->kernel_type = gated ? HTP_DSV4_HC_PRE_KERNEL_GATED : HTP_DSV4_HC_PRE_KERNEL_PLAIN; 4461 4462 kparams->n_chunks = ((uint32_t) x->ne[0] + HTP_DSV4_HC_CHUNK_ELEMS - 1) / HTP_DSV4_HC_CHUNK_ELEMS; 4463 kparams->total_jobs = n_tokens * kparams->n_chunks; 4464 if (kparams->total_jobs == 0) { 4465 return true; 4466 } 4467 kparams->n_threads = (std::min)(htp_session_n_threads(sess), kparams->total_jobs); 4468 kparams->jobs_per_thread = (kparams->total_jobs + kparams->n_threads - 1) / kparams->n_threads; 4469 4470 const size_t vtcm_budget = htp_session_vtcm_size(sess); 4471 const uint32_t n_rows = gated ? HTP_DSV4_HC_PRE_GATED_ROWS : HTP_DSV4_HC_PRE_PLAIN_ROWS; 4472 uint32_t block_chunks = (std::min)((uint32_t) HTP_DSV4_HC_MAX_CHUNKS, kparams->n_chunks); 4473 while (block_chunks > 16) { 4474 const uint32_t stride = block_chunks * 128; 4475 const uint32_t buf_sz = n_rows * stride; 4476 if (2 * buf_sz * kparams->n_threads <= vtcm_budget) { 4477 break; 4478 } 4479 block_chunks /= 2; 4480 } 4481 4482 kparams->dma_block_chunks = block_chunks; 4483 kparams->dma_row_stride = block_chunks * 128; 4484 kparams->dma_buffer_size = n_rows * kparams->dma_row_stride; 4485 kparams->vtcm_size = 2 * kparams->dma_buffer_size * kparams->n_threads; 4486 if (kparams->vtcm_size > vtcm_budget) { 4487 return false; 4488 } 4489 4490 kparams->div_n_chunks = init_fastdiv_values(kparams->n_chunks); 4491 kparams->div_n_threads = init_fastdiv_values(kparams->n_threads); 4492 4493 return true; 4494 } 4495 4496 static inline bool htp_kparams_solve_dsv4_hc_pre( 4497 const struct ggml_hexagon_session * sess, 4498 const struct ggml_tensor * op, 4499 struct htp_dsv4_hc_pre_kernel_params * kparams 4500 ) { 4501 if (htp_dsv4_hc_pre_can_collapse(op->src[0], op->src[1], op)) { 4502 struct ggml_tensor x_c, w_c, d_c; 4503 htp_dsv4_hc_pre_collapse_tensors(op->src[0], op->src[1], op, &x_c, &w_c, &d_c); 4504 return htp_kparams_solve_dsv4_hc_pre_impl(sess, &x_c, &w_c, &d_c, kparams); 4505 } 4506 return htp_kparams_solve_dsv4_hc_pre_impl(sess, op->src[0], op->src[1], op, kparams); 4507 } 4508 4509 static bool htp_dsv4_hc_post_can_collapse( 4510 const struct ggml_tensor * x, 4511 const struct ggml_tensor * residual, 4512 const struct ggml_tensor * post, 4513 const struct ggml_tensor * comb, 4514 const struct ggml_tensor * dst 4515 ) { 4516 if (!x || !residual || !post || !dst) { 4517 return false; 4518 } 4519 if (x->type != GGML_TYPE_F32 || residual->type != GGML_TYPE_F32 || post->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 4520 return false; 4521 } 4522 if (comb && comb->type != GGML_TYPE_F32) { 4523 return false; 4524 } 4525 if (x->nb[0] != sizeof(float) || residual->nb[0] != sizeof(float) || post->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float)) { 4526 return false; 4527 } 4528 if (comb && comb->nb[0] != sizeof(float)) { 4529 return false; 4530 } 4531 if (residual->ne[0] != x->ne[0] || residual->ne[1] != 4 || dst->ne[0] != x->ne[0] || dst->ne[1] != 4 || post->ne[0] != 4) { 4532 return false; 4533 } 4534 if (comb && (comb->ne[0] != 4 || comb->ne[1] != 4)) { 4535 return false; 4536 } 4537 4538 const int64_t x_tokens = x->ne[1] * x->ne[2] * x->ne[3]; 4539 const int64_t r_tokens = residual->ne[2] * residual->ne[3]; 4540 const int64_t p_tokens = post->ne[1] * post->ne[2] * post->ne[3]; 4541 const int64_t d_tokens = dst->ne[2] * dst->ne[3]; 4542 if (x_tokens != r_tokens || x_tokens != p_tokens || x_tokens != d_tokens) { 4543 return false; 4544 } 4545 if (comb && (comb->ne[2] * comb->ne[3] != x_tokens)) { 4546 return false; 4547 } 4548 4549 if (x->ne[2] == 1 && x->ne[3] == 1 && 4550 residual->ne[3] == 1 && 4551 post->ne[2] == 1 && post->ne[3] == 1 && 4552 dst->ne[3] == 1 && 4553 (!comb || comb->ne[3] == 1)) { 4554 return false; 4555 } 4556 4557 if (x->ne[2] > 1 && x->nb[2] != x->ne[1] * x->nb[1]) return false; 4558 if (x->ne[3] > 1 && x->nb[3] != x->ne[2] * x->nb[2]) return false; 4559 4560 if (residual->ne[3] > 1 && residual->nb[3] != residual->ne[2] * residual->nb[2]) return false; 4561 if (dst->ne[3] > 1 && dst->nb[3] != dst->ne[2] * dst->nb[2]) return false; 4562 4563 if (post->ne[2] > 1 && post->nb[2] != post->ne[1] * post->nb[1]) return false; 4564 if (post->ne[3] > 1 && post->nb[3] != post->ne[2] * post->nb[2]) return false; 4565 4566 if (comb && comb->ne[3] > 1 && comb->nb[3] != comb->ne[2] * comb->nb[2]) return false; 4567 4568 return true; 4569 } 4570 4571 static void htp_dsv4_hc_post_collapse_tensors( 4572 const struct ggml_tensor * x, 4573 const struct ggml_tensor * residual, 4574 const struct ggml_tensor * post, 4575 const struct ggml_tensor * comb, 4576 const struct ggml_tensor * dst, 4577 struct ggml_tensor * x_c, 4578 struct ggml_tensor * r_c, 4579 struct ggml_tensor * p_c, 4580 struct ggml_tensor * c_c, 4581 struct ggml_tensor * d_c 4582 ) { 4583 const int64_t n_tokens = x->ne[1] * x->ne[2] * x->ne[3]; 4584 4585 *x_c = *x; 4586 x_c->ne[1] = n_tokens; 4587 x_c->ne[2] = 1; 4588 x_c->ne[3] = 1; 4589 x_c->nb[2] = x_c->ne[1] * x_c->nb[1]; 4590 x_c->nb[3] = x_c->nb[2]; 4591 4592 *r_c = *residual; 4593 r_c->ne[2] = n_tokens; 4594 r_c->ne[3] = 1; 4595 r_c->nb[3] = r_c->ne[2] * r_c->nb[2]; 4596 4597 *d_c = *dst; 4598 d_c->ne[2] = n_tokens; 4599 d_c->ne[3] = 1; 4600 d_c->nb[3] = d_c->ne[2] * d_c->nb[2]; 4601 4602 *p_c = *post; 4603 p_c->ne[1] = n_tokens; 4604 p_c->ne[2] = 1; 4605 p_c->ne[3] = 1; 4606 p_c->nb[2] = p_c->ne[1] * p_c->nb[1]; 4607 p_c->nb[3] = p_c->nb[2]; 4608 4609 if (comb && c_c) { 4610 *c_c = *comb; 4611 c_c->ne[2] = n_tokens; 4612 c_c->ne[3] = 1; 4613 c_c->nb[3] = c_c->ne[2] * c_c->nb[2]; 4614 } 4615 } 4616 4617 static bool htp_kparams_solve_dsv4_hc_post_impl( 4618 const struct ggml_hexagon_session * sess, 4619 const struct ggml_tensor * x, 4620 const struct ggml_tensor * residual, 4621 const struct ggml_tensor * post, 4622 const struct ggml_tensor * comb, 4623 const struct ggml_tensor * dst, 4624 struct htp_dsv4_hc_post_kernel_params * kparams 4625 ) { 4626 memset(kparams, 0, sizeof(*kparams)); 4627 4628 if (x->type != GGML_TYPE_F32 || residual->type != GGML_TYPE_F32 || post->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { 4629 return false; 4630 } 4631 if (comb && comb->type != GGML_TYPE_F32) { 4632 return false; 4633 } 4634 if (x->ne[2] != 1 || x->ne[3] != 1) { 4635 return false; 4636 } 4637 if (x->ne[0] % HTP_DSV4_HC_CHUNK_ELEMS != 0) { 4638 return false; 4639 } 4640 if (residual->ne[0] != x->ne[0] || residual->ne[1] != 4 || residual->ne[2] != x->ne[1] || residual->ne[3] != 1) { 4641 return false; 4642 } 4643 if (post->ne[0] != 4 || post->ne[1] != x->ne[1] || post->ne[2] != 1 || post->ne[3] != 1) { 4644 return false; 4645 } 4646 if (comb && (comb->ne[0] != 4 || comb->ne[1] != 4 || comb->ne[2] != x->ne[1] || comb->ne[3] != 1)) { 4647 return false; 4648 } 4649 if (dst->ne[0] != x->ne[0] || dst->ne[1] != 4 || dst->ne[2] != x->ne[1] || dst->ne[3] != 1) { 4650 return false; 4651 } 4652 if (x->nb[0] != sizeof(float) || residual->nb[0] != sizeof(float) || post->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float)) { 4653 return false; 4654 } 4655 if (comb && comb->nb[0] != sizeof(float)) { 4656 return false; 4657 } 4658 if (ggml_is_permuted(x) || ggml_is_permuted(residual) || ggml_is_permuted(post) || ggml_is_permuted(dst) || (comb && ggml_is_permuted(comb))) { 4659 return false; 4660 } 4661 4662 const uint32_t n_tokens = (uint32_t) x->ne[1]; 4663 4664 kparams->has_comb = comb != nullptr; 4665 kparams->kernel_type = kparams->has_comb ? HTP_DSV4_HC_POST_KERNEL_COMB : HTP_DSV4_HC_POST_KERNEL_IDENTITY; 4666 kparams->n_chunks = ((uint32_t) x->ne[0] + HTP_DSV4_HC_CHUNK_ELEMS - 1) / HTP_DSV4_HC_CHUNK_ELEMS; 4667 kparams->total_jobs = n_tokens * kparams->n_chunks; 4668 if (kparams->total_jobs == 0) { 4669 return true; 4670 } 4671 kparams->n_threads = (std::min)(htp_session_n_threads(sess), kparams->total_jobs); 4672 kparams->jobs_per_thread = (kparams->total_jobs + kparams->n_threads - 1) / kparams->n_threads; 4673 4674 const size_t vtcm_budget = htp_session_vtcm_size(sess); 4675 uint32_t block_chunks = (std::min)((uint32_t) HTP_DSV4_HC_MAX_CHUNKS, kparams->n_chunks); 4676 while (block_chunks > 16) { 4677 const uint32_t stride = block_chunks * 128; 4678 const uint32_t buf_sz = HTP_DSV4_HC_POST_ROWS * stride; 4679 if (2 * buf_sz * kparams->n_threads <= vtcm_budget) { 4680 break; 4681 } 4682 block_chunks /= 2; 4683 } 4684 4685 kparams->dma_block_chunks = block_chunks; 4686 kparams->dma_row_stride = block_chunks * 128; 4687 kparams->dma_buffer_size = HTP_DSV4_HC_POST_ROWS * kparams->dma_row_stride; 4688 kparams->vtcm_size = 2 * kparams->dma_buffer_size * kparams->n_threads; 4689 if (kparams->vtcm_size > vtcm_budget) { 4690 return false; 4691 } 4692 4693 kparams->div_n_chunks = init_fastdiv_values(kparams->n_chunks); 4694 kparams->div_n_threads = init_fastdiv_values(kparams->n_threads); 4695 4696 return true; 4697 } 4698 4699 static inline bool htp_kparams_solve_dsv4_hc_post( 4700 const struct ggml_hexagon_session * sess, 4701 const struct ggml_tensor * op, 4702 struct htp_dsv4_hc_post_kernel_params * kparams 4703 ) { 4704 if (htp_dsv4_hc_post_can_collapse(op->src[0], op->src[1], op->src[2], op->src[3], op)) { 4705 struct ggml_tensor x_c, r_c, p_c, c_c, d_c; 4706 htp_dsv4_hc_post_collapse_tensors(op->src[0], op->src[1], op->src[2], op->src[3], op, &x_c, &r_c, &p_c, op->src[3] ? &c_c : nullptr, &d_c); 4707 return htp_kparams_solve_dsv4_hc_post_impl(sess, &x_c, &r_c, &p_c, op->src[3] ? &c_c : nullptr, &d_c, kparams); 4708 } 4709 return htp_kparams_solve_dsv4_hc_post_impl(sess, op->src[0], op->src[1], op->src[2], op->src[3], op, kparams); 4710 } 4711 4712 static bool htp_lightning_indexer_can_collapse( 4713 const struct ggml_tensor * q, 4714 const struct ggml_tensor * k, 4715 const struct ggml_tensor * w, 4716 const struct ggml_tensor * m, 4717 const struct ggml_tensor * dst 4718 ) { 4719 if (!q || !k || !w || !m || !dst) { 4720 return false; 4721 } 4722 4723 // 2D K normalization: ne[1] is n_kv, ne[2] is 1 4724 const bool can_collapse_k = (k->ne[1] > 1 && k->ne[2] == 1); 4725 4726 // Stream-to-token collapse across q, w, dst (and m) 4727 bool can_collapse_stream = false; 4728 if (q->ne[3] > 1 && k->ne[3] == 1) { 4729 const bool q_ok = (q->nb[3] == (size_t) q->ne[2] * q->nb[2]); 4730 const bool dst_ok = (dst->ne[3] == q->ne[3] && dst->ne[2] == 1 && dst->ne[1] == q->ne[2] && 4731 dst->nb[3] == (size_t) dst->ne[1] * dst->nb[1]); 4732 const bool w_ok = (w->ne[3] == q->ne[3] && w->ne[2] == 1 && w->ne[1] == q->ne[2] && 4733 w->nb[3] == (size_t) w->ne[1] * w->nb[1]) || 4734 (w->ne[3] == 1 && w->ne[1] == q->ne[2] * q->ne[3]); 4735 const bool m_ok = (m->ne[3] == q->ne[3] && m->ne[2] == 1 && m->ne[1] == q->ne[2] && 4736 m->nb[3] == (size_t) m->ne[1] * m->nb[1]) || 4737 (m->ne[3] == 1 && m->ne[1] == q->ne[2] * q->ne[3]); 4738 can_collapse_stream = q_ok && dst_ok && w_ok && m_ok; 4739 } 4740 4741 return can_collapse_k || can_collapse_stream; 4742 } 4743 4744 static void htp_lightning_indexer_collapse_tensors( 4745 const struct ggml_tensor * q, 4746 const struct ggml_tensor * k, 4747 const struct ggml_tensor * w, 4748 const struct ggml_tensor * m, 4749 const struct ggml_tensor * dst, 4750 struct ggml_tensor * q_c, 4751 struct ggml_tensor * k_c, 4752 struct ggml_tensor * w_c, 4753 struct ggml_tensor * m_c, 4754 struct ggml_tensor * dst_c 4755 ) { 4756 *q_c = *q; 4757 *k_c = *k; 4758 *w_c = *w; 4759 *m_c = *m; 4760 *dst_c = *dst; 4761 4762 if (k->ne[1] > 1 && k->ne[2] == 1) { 4763 k_c->ne[2] = k->ne[1]; 4764 k_c->ne[1] = 1; 4765 k_c->nb[1] = k->nb[1]; 4766 k_c->nb[2] = k->nb[1]; 4767 k_c->nb[3] = k->nb[2]; 4768 } 4769 4770 if (q->ne[3] > 1 && k->ne[3] == 1) { 4771 const int64_t n_tokens_collapsed = (int64_t) q->ne[2] * q->ne[3]; 4772 4773 q_c->ne[2] = n_tokens_collapsed; 4774 q_c->ne[3] = 1; 4775 q_c->nb[3] = (size_t) q_c->ne[2] * q_c->nb[2]; 4776 4777 if (w->ne[3] == q->ne[3]) { 4778 w_c->ne[1] = n_tokens_collapsed; 4779 w_c->ne[3] = 1; 4780 w_c->nb[2] = (size_t) w_c->ne[1] * w_c->nb[1]; 4781 w_c->nb[3] = w_c->nb[2]; 4782 } 4783 4784 dst_c->ne[1] = n_tokens_collapsed; 4785 dst_c->ne[3] = 1; 4786 dst_c->nb[2] = (size_t) dst_c->ne[1] * dst_c->nb[1]; 4787 dst_c->nb[3] = dst_c->nb[2]; 4788 4789 if (m->ne[3] == q->ne[3] && m->ne[1] == q->ne[2]) { 4790 m_c->ne[1] = n_tokens_collapsed; 4791 m_c->ne[3] = 1; 4792 m_c->nb[2] = (size_t) m_c->ne[1] * m_c->nb[1]; 4793 m_c->nb[3] = m_c->nb[2]; 4794 } 4795 } 4796 } 4797 4798 static bool htp_kparams_solve_lightning_indexer_impl( 4799 const struct ggml_hexagon_session * sess, 4800 const struct ggml_tensor * q, 4801 const struct ggml_tensor * k, 4802 const struct ggml_tensor * w, 4803 const struct ggml_tensor * m, 4804 const struct ggml_tensor * op, 4805 struct htp_lid_kernel_params * kparams 4806 ) { 4807 if (opt_li_select < 1) { 4808 return false; 4809 } 4810 4811 if (k->type != GGML_TYPE_F32 && k->type != GGML_TYPE_F16 && k->type != GGML_TYPE_Q8_0 && k->type != GGML_TYPE_Q4_0) { 4812 return false; 4813 } 4814 4815 if (q->ne[0] % 32 != 0 || 4816 !ggml_is_contiguous_rows(q) || !ggml_is_contiguous_rows(k) || 4817 !ggml_is_contiguous_rows(w) || !ggml_is_contiguous_rows(m) || 4818 !ggml_is_contiguous_rows(op)) { 4819 return false; 4820 } 4821 4822 const uint32_t n_kv_blocks = (k->ne[2] + HTP_LID_KV_BLOCK - 1) / HTP_LID_KV_BLOCK; 4823 if (n_kv_blocks == 0 || q->ne[2] == 0) { 4824 return false; 4825 } 4826 4827 const uint32_t D = q->ne[0]; 4828 const uint32_t n_head = q->ne[1]; 4829 const uint32_t n_tokens = q->ne[2]; 4830 const size_t k_row_size = ggml_row_size(k->type, D); 4831 4832 kparams->k_row_size = (uint32_t) k_row_size; 4833 kparams->div_n_head = init_fastdiv_values(n_head); 4834 kparams->div_m_ne3 = init_fastdiv_values((uint32_t) m->ne[3]); 4835 4836 if (opt_li_select >= 2 && htp_session_n_hmx(sess) > 0 && D % HMX_FP16_TILE_N_COLS == 0) { 4837 const uint32_t n_threads = htp_session_n_threads(sess); 4838 const uint32_t n_stream = q->ne[3]; 4839 const uint32_t n_dot = D / HMX_FP16_TILE_N_COLS; 4840 4841 auto vtcm_usage = [&](uint32_t n_tok, uint32_t kv_block, uint32_t n_qsets) { 4842 return htp_lid_hmx_compute_vtcm_usage(D, n_head, n_tok, kv_block, n_qsets, n_threads, k_row_size); 4843 }; 4844 4845 uint32_t n_tok = (std::min)(n_tokens, (uint32_t) HTP_LID_HMX_TOKENS_MAX); 4846 while (n_tok > 32 && vtcm_usage(n_tok, 64, 1) > htp_session_vtcm_size(sess)) { 4847 n_tok /= 2; 4848 } 4849 4850 uint32_t n_qsets = n_tok == n_tokens ? (std::min)(n_stream, (uint32_t) HTP_LID_HMX_MAX_QSETS) : 1; 4851 while (n_qsets > 1 && vtcm_usage(n_tok, 64, n_qsets) > htp_session_vtcm_size(sess)) { 4852 n_qsets /= 2; 4853 } 4854 4855 uint32_t n_qbuf = 1; 4856 if (n_qsets == 1 && n_head >= 16 && n_stream * ((n_tokens + n_tok - 1) / n_tok) > 1) { 4857 uint32_t nt2 = n_tok; 4858 while (nt2 > 32 && vtcm_usage(nt2, 64, 2) > htp_session_vtcm_size(sess)) { 4859 nt2 /= 2; 4860 } 4861 const size_t q_bytes = (size_t) nt2 * n_head * D * sizeof(float); 4862 const size_t k_bytes = (size_t) k->ne[2] * ggml_row_size(k->type, D); 4863 if (vtcm_usage(nt2, 64, 2) <= htp_session_vtcm_size(sess) && q_bytes >= k_bytes) { 4864 n_tok = nt2; 4865 n_qbuf = 2; 4866 } 4867 } 4868 4869 const uint32_t n_ch = (n_tok * n_head + 31) / 32; 4870 const uint32_t kv_seg = hex_round_up(k->ne[2], 64); 4871 const uint32_t n_rows = n_qsets * kv_seg; 4872 const uint32_t bc_max = n_rows <= 512 ? n_rows : (std::max)(256u, n_rows / 4); 4873 uint32_t kv_block = 64; 4874 while (kv_block < HTP_LID_HMX_KV_BLOCK_MAX && kv_block < bc_max && (kv_block / 32) * n_ch * (n_dot + 1) < 256 && 4875 vtcm_usage(n_tok, kv_block * 2, n_qsets * n_qbuf) <= htp_session_vtcm_size(sess)) { 4876 kv_block *= 2; 4877 } 4878 4879 const size_t vtcm_size = vtcm_usage(n_tok, kv_block, n_qsets * n_qbuf); 4880 if (vtcm_size <= htp_session_vtcm_size(sess)) { 4881 const uint32_t n_tt = (n_tokens + n_tok - 1) / n_tok; 4882 kparams->kernel_type = HTP_LID_KERNEL_HMX; 4883 kparams->n_threads = n_threads; 4884 kparams->n_tokens_tile = n_tok; 4885 kparams->n_kv_blocks = (n_rows + kv_block - 1) / kv_block; 4886 kparams->blocks_per_thread = 0; 4887 kparams->kv_block = kv_block; 4888 kparams->n_qsets = n_qsets; 4889 kparams->n_qbuf = n_qbuf; 4890 kparams->kv_seg = kv_seg; 4891 kparams->n_tt = n_tt; 4892 kparams->vtcm_size = (uint32_t) vtcm_size; 4893 kparams->div_kv_seg = init_fastdiv_values(kv_seg); 4894 kparams->div_n_tt = init_fastdiv_values(n_tt); 4895 kparams->div_n_ch = init_fastdiv_values(n_ch); 4896 return true; 4897 } 4898 } 4899 4900 const uint32_t n_threads = (std::min)(htp_session_n_threads(sess), n_kv_blocks); 4901 uint32_t n_tok = (std::min)((uint32_t) q->ne[2], (uint32_t) HTP_LID_TOKENS_MAX); 4902 while (n_tok > 1 && htp_lid_compute_vtcm_usage(D, n_head, n_tok, n_threads, k_row_size) > htp_session_vtcm_size(sess)) { 4903 n_tok /= 2; 4904 } 4905 4906 const size_t vtcm_size = htp_lid_compute_vtcm_usage(D, n_head, n_tok, n_threads, k_row_size); 4907 if (vtcm_size > htp_session_vtcm_size(sess)) { 4908 return false; 4909 } 4910 4911 kparams->kernel_type = HTP_LID_KERNEL_HVX; 4912 kparams->n_threads = n_threads; 4913 kparams->n_tokens_tile = n_tok; 4914 kparams->n_kv_blocks = n_kv_blocks; 4915 kparams->blocks_per_thread = (n_kv_blocks + n_threads - 1) / n_threads; 4916 kparams->kv_block = HTP_LID_KV_BLOCK; 4917 kparams->n_qsets = 1; 4918 kparams->n_qbuf = 1; 4919 kparams->kv_seg = 0; 4920 kparams->n_tt = 0; 4921 kparams->vtcm_size = (uint32_t) vtcm_size; 4922 4923 return true; 4924 } 4925 4926 static bool htp_kparams_solve_lightning_indexer( 4927 const struct ggml_hexagon_session * sess, 4928 const struct ggml_tensor * op, 4929 struct htp_lid_kernel_params * kparams 4930 ) { 4931 if (htp_lightning_indexer_can_collapse(op->src[0], op->src[1], op->src[2], op->src[3], op)) { 4932 struct ggml_tensor q_c, k_c, w_c, m_c, d_c; 4933 htp_lightning_indexer_collapse_tensors(op->src[0], op->src[1], op->src[2], op->src[3], op, 4934 &q_c, &k_c, &w_c, &m_c, &d_c); 4935 return htp_kparams_solve_lightning_indexer_impl(sess, &q_c, &k_c, &w_c, &m_c, &d_c, kparams); 4936 } 4937 return htp_kparams_solve_lightning_indexer_impl(sess, op->src[0], op->src[1], op->src[2], op->src[3], op, kparams); 4938 } 4939 template 4940 static inline bool htp_kparams_invoke( 4941 const struct ggml_hexagon_session * sess, 4942 const struct ggml_tensor * op, 4943 void * kparams 4944 ) { 4945 using KParams = decltype(htp_kparams_type(SolveFn)); 4946 KParams tmp; 4947 return SolveFn(sess, op, kparams ? (KParams *) kparams : &tmp); 4948 } 4949 4950 void htp_opnode_collapse(const struct ggml_hexagon_session * sess, htp_opnode & node) { 4951 const uint32_t n_threads = htp_session_n_threads(sess); 4952 4953 if (htp_op_is_unary(node.opcode) && htp_unary_is_elementwise(node.opcode)) { 4954 if (node.inputs.empty() || !node.inputs[0] || node.outputs.empty() || !node.outputs[0]) { 4955 return; 4956 } 4957 if (!ggml_are_same_shape(node.inputs[0], node.outputs[0])) { 4958 return; 4959 } 4960 const size_t elem_size = ggml_type_size(node.inputs[0]->type); 4961 struct ggml_tensor src0_c = *node.inputs[0]; 4962 struct ggml_tensor dst_c = *node.outputs[0]; 4963 struct ggml_tensor * tensors[2] = { &src0_c, &dst_c }; 4964 htp_collapse_dims(tensors, 2, elem_size, n_threads); 4965 if (src0_c.ne[0] != node.inputs[0]->ne[0] || src0_c.ne[1] != node.inputs[0]->ne[1]) { 4966 node.inputs[0] = node.add_dummy(src0_c); 4967 node.outputs[0] = node.add_dummy(dst_c); 4968 } 4969 return; 4970 } 4971 4972 if (htp_op_is_glu(node.opcode)) { 4973 if (node.inputs.size() < 2 || !node.inputs[0] || !node.inputs[1] || node.outputs.empty() || !node.outputs[0]) { 4974 return; 4975 } 4976 if (!ggml_are_same_shape(node.inputs[0], node.outputs[0]) || 4977 !ggml_are_same_shape(node.inputs[1], node.outputs[0])) { 4978 return; 4979 } 4980 const size_t elem_size = ggml_type_size(node.inputs[0]->type); 4981 struct ggml_tensor src0_c = *node.inputs[0]; 4982 struct ggml_tensor src1_c = *node.inputs[1]; 4983 struct ggml_tensor dst_c = *node.outputs[0]; 4984 struct ggml_tensor * tensors[3] = { &src0_c, &src1_c, &dst_c }; 4985 htp_collapse_dims(tensors, 3, elem_size, n_threads); 4986 if (src0_c.ne[0] != node.inputs[0]->ne[0] || src0_c.ne[1] != node.inputs[0]->ne[1]) { 4987 node.inputs[0] = node.add_dummy(src0_c); 4988 node.inputs[1] = node.add_dummy(src1_c); 4989 node.outputs[0] = node.add_dummy(dst_c); 4990 } 4991 return; 4992 } 4993 4994 if (node.opcode == HTP_OP_MUL_MAT) { 4995 if (node.inputs.size() < 2 || !node.inputs[0] || !node.inputs[1] || node.outputs.empty() || !node.outputs[0]) { 4996 return; 4997 } 4998 const struct ggml_tensor * src0 = node.inputs[0]; 4999 const struct ggml_tensor * src1 = node.inputs[1]; 5000 const struct ggml_tensor * dst = node.outputs[0]; 5001 if (htp_matmul_can_collapse(src0, src1, dst)) { 5002 node.inputs[1] = node.add_dummy(htp_tensor_collapse_rows(src1)); 5003 node.outputs[0] = node.add_dummy(htp_tensor_collapse_rows(dst)); 5004 } 5005 return; 5006 } 5007 5008 if (node.opcode == HTP_OP_MUL_MAT_ADD) { 5009 if (node.inputs.size() < 2 || !node.inputs[0] || !node.inputs[1] || node.outputs.empty() || !node.outputs[0]) { 5010 return; 5011 } 5012 const struct ggml_tensor * src0 = node.inputs[0]; 5013 const struct ggml_tensor * src1 = node.inputs[1]; 5014 const struct ggml_tensor * src2 = node.inputs.size() > 2 ? node.inputs[2] : nullptr; 5015 const struct ggml_tensor * dst = node.outputs[0]; 5016 if (htp_matmul_add_can_collapse(src0, src1, src2, dst)) { 5017 node.inputs[1] = node.add_dummy(htp_tensor_collapse_rows(src1)); 5018 node.outputs[0] = node.add_dummy(htp_tensor_collapse_rows(dst)); 5019 if (src2 && (src2->ne[1] * src2->ne[2] * src2->ne[3] > 1)) { 5020 node.inputs[2] = node.add_dummy(htp_tensor_collapse_rows(src2)); 5021 } 5022 } 5023 return; 5024 } 5025 5026 if (node.opcode == HTP_OP_UPSCALE) { 5027 if (node.inputs.empty() || !node.inputs[0] || node.outputs.empty() || !node.outputs[0]) { 5028 return; 5029 } 5030 const struct ggml_tensor * src0 = node.inputs[0]; 5031 const struct ggml_tensor * dst = node.outputs[0]; 5032 5033 if ((src0->ne[3] > 1 || dst->ne[3] > 1) && 5034 src0->ne[2] == dst->ne[2] && src0->ne[3] == dst->ne[3] && 5035 src0->nb[3] == (size_t) src0->ne[2] * src0->nb[2] && 5036 dst->nb[3] == (size_t) dst->ne[2] * dst->nb[2]) { 5037 struct ggml_tensor src0_c = *src0; 5038 struct ggml_tensor dst_c = *dst; 5039 src0_c.ne[2] *= src0_c.ne[3]; 5040 src0_c.ne[3] = 1; 5041 src0_c.nb[3] = (size_t) src0_c.ne[2] * src0_c.nb[2]; 5042 dst_c.ne[2] *= dst_c.ne[3]; 5043 dst_c.ne[3] = 1; 5044 dst_c.nb[3] = (size_t) dst_c.ne[2] * dst_c.nb[2]; 5045 node.inputs[0] = node.add_dummy(src0_c); 5046 node.outputs[0] = node.add_dummy(dst_c); 5047 } 5048 return; 5049 } 5050 5051 if (node.opcode == HTP_OP_DSV4_HC_PRE) { 5052 if (node.inputs.size() < 2 || !node.inputs[0] || !node.inputs[1] || node.outputs.empty() || !node.outputs[0]) { 5053 return; 5054 } 5055 const struct ggml_tensor * x = node.inputs[0]; 5056 const struct ggml_tensor * weights = node.inputs[1]; 5057 const struct ggml_tensor * dst = node.outputs[0]; 5058 5059 if (htp_dsv4_hc_pre_can_collapse(x, weights, dst)) { 5060 struct ggml_tensor x_c, w_c, d_c; 5061 htp_dsv4_hc_pre_collapse_tensors(x, weights, dst, &x_c, &w_c, &d_c); 5062 node.inputs[0] = node.add_dummy(x_c); 5063 node.inputs[1] = node.add_dummy(w_c); 5064 node.outputs[0] = node.add_dummy(d_c); 5065 } 5066 return; 5067 } 5068 5069 if (node.opcode == HTP_OP_DSV4_HC_POST) { 5070 if (node.inputs.size() < 3 || !node.inputs[0] || !node.inputs[1] || !node.inputs[2] || node.outputs.empty() || !node.outputs[0]) { 5071 return; 5072 } 5073 const struct ggml_tensor * x = node.inputs[0]; 5074 const struct ggml_tensor * residual = node.inputs[1]; 5075 const struct ggml_tensor * post = node.inputs[2]; 5076 const struct ggml_tensor * comb = node.inputs.size() > 3 ? node.inputs[3] : nullptr; 5077 const struct ggml_tensor * dst = node.outputs[0]; 5078 5079 if (htp_dsv4_hc_post_can_collapse(x, residual, post, comb, dst)) { 5080 struct ggml_tensor x_c, r_c, p_c, c_c, d_c; 5081 htp_dsv4_hc_post_collapse_tensors(x, residual, post, comb, dst, &x_c, &r_c, &p_c, comb ? &c_c : nullptr, &d_c); 5082 node.inputs[0] = node.add_dummy(x_c); 5083 node.inputs[1] = node.add_dummy(r_c); 5084 node.inputs[2] = node.add_dummy(p_c); 5085 if (comb) { 5086 node.inputs[3] = node.add_dummy(c_c); 5087 } 5088 node.outputs[0] = node.add_dummy(d_c); 5089 } 5090 return; 5091 } 5092 5093 if (node.opcode == HTP_OP_LIGHTNING_INDEXER) { 5094 if (node.inputs.size() < 4 || !node.inputs[0] || !node.inputs[1] || !node.inputs[2] || !node.inputs[3] || node.outputs.empty() || !node.outputs[0]) { 5095 return; 5096 } 5097 const struct ggml_tensor * q = node.inputs[0]; 5098 const struct ggml_tensor * k = node.inputs[1]; 5099 const struct ggml_tensor * w = node.inputs[2]; 5100 const struct ggml_tensor * m = node.inputs[3]; 5101 const struct ggml_tensor * dst = node.outputs[0]; 5102 5103 if (htp_lightning_indexer_can_collapse(q, k, w, m, dst)) { 5104 struct ggml_tensor q_c, k_c, w_c, m_c, d_c; 5105 htp_lightning_indexer_collapse_tensors(q, k, w, m, dst, &q_c, &k_c, &w_c, &m_c, &d_c); 5106 node.inputs[0] = node.add_dummy(q_c); 5107 node.inputs[1] = node.add_dummy(k_c); 5108 node.inputs[2] = node.add_dummy(w_c); 5109 node.inputs[3] = node.add_dummy(m_c); 5110 node.outputs[0] = node.add_dummy(d_c); 5111 } 5112 return; 5113 } 5114 } 5115 5116 bool htp_kparams_solve( 5117 const struct ggml_hexagon_session * sess, 5118 htp_opnode & node 5119 ) { 5120 htp_opnode_collapse(sess, node); 5121 5122 const ggml_tensor * src0 = node.inputs.empty() ? nullptr : node.inputs[0]; 5123 const ggml_tensor * src1 = node.inputs.size() > 1 ? node.inputs[1] : nullptr; 5124 const ggml_tensor * dst = node.dst(); 5125 5126 if (htp_op_is_unary(node.opcode)) { 5127 return htp_kparams_solve_unary_impl(sess, node.opcode, src0, src1, dst, node.kparams()); 5128 } 5129 if (htp_op_is_glu(node.opcode)) { 5130 return htp_kparams_solve_unary_mul_glu(sess, node.opcode, src0, src1, dst, node.kparams()); 5131 } 5132 if (node.opcode == HTP_OP_MUL_MAT) { 5133 if (!htp_matmul_check_types(src0, src1, dst)) { 5134 return false; 5135 } 5136 return htp_kparams_solve_matmul_impl(sess, src0, src1, dst, 0, 0, node.kparams()); 5137 } 5138 if (node.opcode == HTP_OP_MUL_MAT_ADD) { 5139 const ggml_tensor * src2 = node.inputs.size() > 2 ? node.inputs[2] : nullptr; 5140 const size_t src2_size = src2 ? hex_round_up(ggml_nbytes(src2), 128) : 0; 5141 return htp_kparams_solve_matmul_impl(sess, src0, src1, dst, src2 ? src2->nb[1] : 0, src2_size, node.kparams()); 5142 } 5143 if (node.opcode == HTP_OP_UPSCALE) { 5144 return htp_kparams_solve_upscale(sess, src0, dst, node.kparams()); 5145 } 5146 if (node.opcode == HTP_OP_DSV4_HC_PRE) { 5147 return htp_kparams_solve_dsv4_hc_pre_impl(sess, src0, src1, dst, node.kparams()); 5148 } 5149 if (node.opcode == HTP_OP_DSV4_HC_POST) { 5150 const ggml_tensor * src2 = node.inputs.size() > 2 ? node.inputs[2] : nullptr; 5151 const ggml_tensor * src3 = node.inputs.size() > 3 ? node.inputs[3] : nullptr; 5152 return htp_kparams_solve_dsv4_hc_post_impl(sess, src0, src1, src2, src3, dst, node.kparams()); 5153 } 5154 if (node.opcode == HTP_OP_LIGHTNING_INDEXER) { 5155 const ggml_tensor * src2 = node.inputs.size() > 2 ? node.inputs[2] : nullptr; 5156 const ggml_tensor * src3 = node.inputs.size() > 3 ? node.inputs[3] : nullptr; 5157 return htp_kparams_solve_lightning_indexer_impl(sess, src0, src1, src2, src3, dst, node.kparams()); 5158 } 5159 5160 return htp_kparams_solve(sess, node.node, node.kernel_params); 5161 } 5162 5163 bool htp_kparams_solve( 5164 const struct ggml_hexagon_session * sess, 5165 const struct ggml_tensor * op, 5166 void * kparams 5167 ) { 5168 const htp_op_code opcode = htp_op_remap(op); 5169 if (htp_op_is_unary(opcode) || htp_op_is_glu(opcode) || opcode == HTP_OP_UPSCALE) { 5170 htp_opnode tmp(opcode, op); 5171 if (!htp_kparams_solve(sess, tmp)) { 5172 return false; 5173 } 5174 if (kparams) { 5175 memcpy(kparams, tmp.kernel_params, sizeof(tmp.kernel_params)); 5176 } 5177 return true; 5178 } 5179 5180 switch (opcode) { 5181 case HTP_OP_MUL_MAT: 5182 case HTP_OP_MUL_MAT_ID: 5183 return htp_kparams_invoke(sess, op, kparams); 5184 5185 case HTP_OP_MUL: 5186 case HTP_OP_ADD: 5187 case HTP_OP_ADD_ID: 5188 case HTP_OP_SUB: 5189 case HTP_OP_DIV: 5190 return htp_kparams_invoke(sess, op, kparams); 5191 5192 case HTP_OP_FLASH_ATTN_EXT: 5193 return htp_kparams_invoke(sess, op, kparams); 5194 5195 case HTP_OP_GET_ROWS: 5196 return htp_kparams_invoke(sess, op, kparams); 5197 5198 case HTP_OP_SET_ROWS: 5199 return htp_kparams_invoke(sess, op, kparams); 5200 5201 case HTP_OP_ROPE: 5202 return htp_kparams_invoke(sess, op, kparams); 5203 5204 case HTP_OP_SSM_CONV: 5205 return htp_kparams_invoke(sess, op, kparams); 5206 5207 case HTP_OP_SOFTMAX: 5208 return htp_kparams_invoke(sess, op, kparams); 5209 5210 case HTP_OP_GATED_DELTA_NET: 5211 return htp_kparams_invoke(sess, op, kparams); 5212 5213 case HTP_OP_ARGSORT: 5214 case HTP_OP_TOP_K: 5215 return htp_kparams_invoke(sess, op, kparams); 5216 5217 case HTP_OP_POOL_2D: 5218 case HTP_OP_POOL_1D: 5219 return htp_kparams_invoke(sess, op, kparams); 5220 5221 case HTP_OP_CONCAT: 5222 return htp_kparams_invoke(sess, op, kparams); 5223 5224 case HTP_OP_CPY: 5225 case HTP_OP_CPY_FENCE: 5226 return htp_kparams_invoke(sess, op, kparams); 5227 5228 case HTP_OP_IM2COL: 5229 return htp_kparams_invoke(sess, op, kparams); 5230 5231 case HTP_OP_IM2COL_3D: 5232 return htp_kparams_invoke(sess, op, kparams); 5233 5234 case HTP_OP_CONV_2D: 5235 return htp_kparams_invoke(sess, op, kparams); 5236 5237 case HTP_OP_CONV_2D_DW: 5238 return htp_kparams_invoke(sess, op, kparams); 5239 5240 case HTP_OP_SUM_ROWS: 5241 case HTP_OP_SUM: 5242 case HTP_OP_ARGMAX: 5243 return htp_kparams_invoke(sess, op, kparams); 5244 5245 case HTP_OP_REPEAT: 5246 return htp_kparams_invoke(sess, op, kparams); 5247 5248 case HTP_OP_PAD: 5249 return htp_kparams_invoke(sess, op, kparams); 5250 5251 case HTP_OP_CUMSUM: 5252 return htp_kparams_invoke(sess, op, kparams); 5253 5254 case HTP_OP_TOP_K_MASK: 5255 return htp_kparams_invoke(sess, op, kparams); 5256 5257 case HTP_OP_FILL: 5258 return htp_kparams_invoke(sess, op, kparams); 5259 5260 case HTP_OP_ROLL: 5261 return htp_kparams_invoke(sess, op, kparams); 5262 5263 case HTP_OP_LIGHTNING_INDEXER: 5264 return htp_kparams_invoke(sess, op, kparams); 5265 5266 case HTP_OP_DIAG: 5267 return htp_kparams_invoke(sess, op, kparams); 5268 5269 case HTP_OP_SOLVE_TRI: 5270 return htp_kparams_invoke(sess, op, kparams); 5271 5272 case HTP_OP_DSV4_HC_PRE: 5273 return htp_kparams_invoke(sess, op, kparams); 5274 5275 case HTP_OP_DSV4_HC_POST: 5276 return htp_kparams_invoke(sess, op, kparams); 5277 5278 default: 5279 if (htp_op_is_unary(opcode)) { 5280 return htp_kparams_invoke(sess, op, kparams); 5281 } 5282 if (htp_op_is_glu(opcode)) { 5283 return htp_kparams_invoke(sess, op, kparams); 5284 } 5285 return false; 5286 } 5287 }