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/matmul-ops.h # 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 / matmul-ops.h` 774 lines ยท 31570 bytes Raw Blame History 1 #ifndef HTP_MATMUL_OPS_H 2 #define HTP_MATMUL_OPS_H 3 4 #include 5 #include 6 #include "htp-ops.h" 7 #include "htp-tensor.h" 8 #include "hex-fastdiv.h" 9 #include "hex-common.h" 10 #include "htp-vtcm.h" 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 16 // --- HMX Tile Constraints --- 17 #define HTP_MM_HMX_TILE_N_COLS 32 18 #define HTP_MM_HMX_TILE_N_ROWS 32 19 #define HTP_MM_HMX_TILE_SIZE (32 * 32 * sizeof(__fp16)) // 2048 bytes 20 #define HTP_MM_HMX_TILE_N_ELMS 1024 21 #define HTP_MM_HMX_MIN_NROWS 4 22 23 #define HTP_MM_WEIGHT_TILE_K_Q2_0 64 24 #define HTP_MM_WEIGHT_TILE_SIZE_Q2_0 HTP_TENSOR_TILE_SIZE_Q2_0 25 #define HTP_MM_WEIGHT_ALIGNED_TILE_SIZE_Q2_0 640 26 27 // --- Activation Tiled Block Sizes (including padding) --- 28 #define HTP_MM_ACT_BLOCK_K 128 29 #define HTP_MM_ACT_TILE_SIZE_Q8_0 1152 30 #define HTP_MM_ACT_TILE_SIZE_Q8_1 1280 31 #define HTP_MM_ACT_TILE_SIZE_F16 2048 32 33 #define HTP_MM_MAX_PREFETCH 16 34 35 // --- Solver Cost Model Penalty Weights (HMX-specific) --- 36 #define HTP_MM_HMX_COST_W_DEQUANT 3 // cost penalty for quantized weight loading/dequantization 37 #define HTP_MM_HMX_COST_A_CONVERT 2 // cost penalty for activation loading/conversion 38 39 // --- DMA Activation Transfer Configuration --- 40 #define HTP_MM_DMA_ACT_ROWS_PER_STEP 2 41 #define HTP_MM_DMA_ACT_MULTIPLIER (2 * HTP_MM_DMA_ACT_ROWS_PER_STEP) 42 43 enum htp_mm_kernel_type { 44 HTP_MM_KERNEL_UNSUPPORTED = 0, 45 46 // HMX paths 47 HTP_MM_KERNEL_HMX_2D, 48 HTP_MM_KERNEL_HMX_BATCHED, 49 50 // HVX floating-point paths 51 HTP_MM_KERNEL_HVX_F16_F16_VTCM, 52 HTP_MM_KERNEL_HVX_F32_F32_VTCM, 53 HTP_MM_KERNEL_HVX_BF16_F32_VTCM, 54 55 // HVX quantized paths 56 HTP_MM_KERNEL_HVX_QUANT_ROW, // standard row-wise parallel quantization 57 HTP_MM_KERNEL_HVX_QUANT_BLOCK, // parallel block-wise quantization 58 }; 59 60 // Op-specific struct for precomputed matmul params 61 struct htp_mm_kernel_params { 62 uint8_t kernel_type; // enum htp_mm_kernel_type 63 uint8_t pipeline; // 1 = pipelined execution, 0 = standard 64 uint8_t unused; // reserved 65 uint8_t n_hmx; // 1 = use HMX, 0 = use HVX 66 67 uint8_t n_threads; // Number of threads to spawn 68 uint8_t n_act_threads; // Number of threads for activation preparation 69 uint8_t n_prefetch; // Prefetch lookahead buffers/rows in VTCM 70 uint8_t n_weights; // Number of weights for fused NX 71 72 int32_t m_chunk; // Row chunk size (M chunk) 73 int32_t n_chunk; // Col chunk size (N chunk) 74 int32_t tile_size; // Weight tile size 75 int32_t aligned_tile_size; // Aligned weight tile size (padded to 128) 76 int32_t act_row_size; // Row size for activation scratchpad 77 int32_t vtcm_size; // Total required scratchpad size in VTCM 78 int32_t vtcm_bias_size; // bias scratchpad size in VTCM (fused only) 79 80 // Precomputed division values 81 struct fastdiv_values div_ne12_ne1; 82 struct fastdiv_values div_ne1; 83 struct fastdiv_values div_r2; 84 struct fastdiv_values div_r3; 85 struct fastdiv_values div_ne12; 86 struct fastdiv_values div_n_act_threads; 87 struct fastdiv_values div_ne00_padded; 88 }; 89 90 #if defined(__cplusplus) 91 static_assert(sizeof(struct htp_mm_kernel_params) <= 128, "htp_matmul_kernel_params is too large for kernel_params blob"); 92 #else 93 _Static_assert(sizeof(struct htp_mm_kernel_params) <= 128, "htp_matmul_kernel_params is too large for kernel_params blob"); 94 #endif 95 96 struct mmid_row_mapping { 97 uint32_t i1; 98 uint32_t i2; 99 }; 100 101 // Search for optimal (mc, nc) chunk sizes within VTCM budget. 102 static inline int htp_mm_hmx_compute_chunks(size_t vtcm_total, 103 size_t overhead, 104 size_t per_n_cost, 105 size_t per_m_cost, 106 size_t per_mn_cost, 107 size_t m, 108 size_t n, 109 size_t m_block_cost, 110 size_t n_block_cost, 111 size_t * m_chunk_out, 112 size_t * n_chunk_out, 113 size_t * total_out) { 114 if (m == 0 || n == 0) return -1; 115 if (vtcm_total <= overhead) return -1; 116 if (per_n_cost == 0 || per_m_cost == 0 || per_mn_cost == 0) return -1; 117 118 const size_t usable = vtcm_total - overhead; 119 120 size_t best_cost = SIZE_MAX; 121 size_t best_tail_waste = SIZE_MAX; 122 size_t best_mn = 0; 123 size_t best_m = 0, best_n = 0; 124 125 const size_t max_nc_budget = (usable / per_n_cost); 126 const size_t n_max = hex_align_down(hex_smin((size_t)n, max_nc_budget), HTP_MM_HMX_TILE_N_COLS); 127 for (size_t nc = n_max; nc >= HTP_MM_HMX_TILE_N_COLS; nc -= HTP_MM_HMX_TILE_N_COLS) { 128 size_t n_fixed = 0, ncmn = 0, mc_denom = 0; 129 if (hex_mul_overflow(nc, per_n_cost, &n_fixed)) continue; 130 if (n_fixed >= usable) goto next_nc; 131 132 if (hex_mul_overflow(nc, per_mn_cost, &ncmn)) goto next_nc; 133 if (hex_add_overflow(per_m_cost, ncmn, &mc_denom) || mc_denom == 0) goto next_nc; 134 135 { 136 size_t remain = usable - n_fixed; 137 size_t mc = remain / mc_denom; 138 mc = hex_align_down(mc, HTP_MM_HMX_TILE_N_ROWS); 139 mc = hex_smin(mc, m); 140 141 if (mc == 0) { 142 goto next_nc; 143 } 144 145 size_t mblocks = ((size_t) m + mc - 1) / mc; 146 size_t nblocks = ((size_t) n + nc - 1) / nc; 147 size_t cost = mblocks * m_block_cost + nblocks * n_block_cost; 148 size_t rem = n % nc; 149 size_t tail_waste = (rem == 0) ? 0 : (nc - rem); 150 size_t mn = mc * nc; 151 if (cost < best_cost || 152 (cost == best_cost && tail_waste < best_tail_waste) || 153 (cost == best_cost && tail_waste == best_tail_waste && mn > best_mn)) { 154 best_cost = cost; 155 best_tail_waste = tail_waste; 156 best_mn = mn; 157 best_m = mc; 158 best_n = nc; 159 } 160 } 161 162 next_nc: 163 if (nc == HTP_MM_HMX_TILE_N_COLS) break; // avoid size_t underflow 164 } 165 166 if (best_m == 0 || best_n == 0) return -1; 167 168 // Compute exact total (with overflow checks) 169 size_t t0 = 0, t1 = 0, t2 = 0, mn = 0, total = 0; 170 if (hex_mul_overflow(best_n, per_n_cost, &t0)) return -1; 171 if (hex_mul_overflow(best_m, per_m_cost, &t1)) return -1; 172 if (hex_mul_overflow(best_m, best_n, &mn)) return -1; 173 if (hex_mul_overflow(mn, per_mn_cost, &t2)) return -1; 174 if (hex_add_overflow(t0, t1, &total)) return -1; 175 if (hex_add_overflow(total, t2, &total)) return -1; 176 if (hex_add_overflow(total, overhead, &total)) return -1; 177 178 *m_chunk_out = best_m; 179 *n_chunk_out = best_n; 180 *total_out = total; 181 return 0; 182 } 183 184 // weight types whose tiles carry a per-block offset (x = d * q + m): the activations need block sums (q8_1) 185 // (Q2_K: per-16 k sums, q8_1_s16) 186 static inline bool htp_mm_weight_has_offset(int weight_type) { 187 return weight_type == HTP_TYPE_Q4_1 || weight_type == HTP_TYPE_Q4_K || weight_type == HTP_TYPE_Q5_K || 188 weight_type == HTP_TYPE_Q2_K || weight_type == HTP_TYPE_Q2_0; 189 } 190 191 // --- Activation/Row Size Helpers --- 192 static inline size_t htp_mm_q8_0_tiled_row_size(uint32_t ne) { 193 const uint32_t ne_padded = hex_round_up(ne, HTP_MM_ACT_BLOCK_K); 194 const uint32_t nb_32 = ne_padded / 32; 195 return nb_32 * HTP_MM_ACT_TILE_SIZE_Q8_0; 196 } 197 198 static inline size_t htp_mm_q8_1_tiled_row_size(uint32_t ne) { 199 const uint32_t ne_padded = hex_round_up(ne, HTP_MM_ACT_BLOCK_K); 200 const uint32_t nb_32 = ne_padded / 32; 201 return nb_32 * HTP_MM_ACT_TILE_SIZE_Q8_1; 202 } 203 204 static inline size_t htp_mm_f16_tiled_row_size(uint32_t ne) { 205 const uint32_t ne_padded = hex_round_up(ne, HTP_MM_ACT_BLOCK_K); 206 const uint32_t nb_32 = ne_padded / 32; 207 return nb_32 * HTP_MM_ACT_TILE_SIZE_F16; 208 } 209 210 static inline size_t htp_mm_tiled_act_row_size(int weight_type, uint32_t ne) { 211 if (weight_type == HTP_TYPE_F16) { 212 return htp_mm_f16_tiled_row_size(ne); 213 } 214 return htp_mm_weight_has_offset(weight_type) ? htp_mm_q8_1_tiled_row_size(ne) : htp_mm_q8_0_tiled_row_size(ne); 215 } 216 217 static inline size_t htp_mm_act_raw_row_size(uint32_t ne) { 218 return hex_round_up(ne, HTP_MM_ACT_BLOCK_K) * sizeof(float); 219 } 220 221 static inline size_t htp_mm_round_up(size_t n, size_t m) { 222 return ((n + m - 1) / m) * m; 223 } 224 225 static inline bool htp_mm_hmx_pipeline(uint32_t m) { 226 return m > 32; 227 } 228 229 static inline void htp_mm_hmx_get_2d_chunk_costs( 230 int wtype, uint32_t k, bool pipeline, uint32_t aligned_tile_size, 231 size_t * size_per_n_out, size_t * size_per_m_out, size_t * size_per_mn_out 232 ) { 233 const bool is_quant = (wtype != HTP_TYPE_F16 && wtype != HTP_TYPE_F32 && wtype != HTP_TYPE_BF16); 234 const size_t row_stride = htp_tensor_get_row_size(wtype, k); 235 const size_t vec_dot_size = k * sizeof(uint16_t); 236 const uint32_t n_weight_tiles = k / htp_tensor_get_tile_k(wtype); 237 const size_t qweight_row_stride = is_quant ? (size_t)(n_weight_tiles * aligned_tile_size) / 32 : 0; 238 239 *size_per_n_out = (pipeline ? 2 : 1) * (is_quant ? qweight_row_stride : row_stride) + 240 (pipeline ? 2 * vec_dot_size : vec_dot_size); 241 *size_per_m_out = vec_dot_size; 242 *size_per_mn_out = (pipeline ? 2 : 1) * sizeof(uint16_t); 243 } 244 245 static inline void htp_mm_hmx_get_batched_chunk_costs( 246 uint32_t k, uint32_t group_size, 247 size_t * size_per_n_out, size_t * size_per_m_out, size_t * size_per_mn_out 248 ) { 249 const size_t vec_dot_size = k * sizeof(uint16_t); 250 *size_per_n_out = 3 * vec_dot_size; 251 *size_per_m_out = group_size * vec_dot_size; 252 *size_per_mn_out = sizeof(uint16_t); 253 } 254 255 static inline size_t htp_mm_hmx_get_2d_overhead(bool pipeline, bool is_matmul_id) { 256 size_t num_regions = pipeline ? 7 : (is_matmul_id ? 4 : 5); 257 return num_regions * HTP_MM_HMX_TILE_SIZE + 256; 258 } 259 260 static inline size_t htp_mm_hmx_get_batched_overhead(void) { 261 return 5 * HTP_MM_HMX_TILE_SIZE + 256; 262 } 263 264 struct htp_mm_hmx_vtcm_layout { 265 // Byte offsets from vtcm_base for each region 266 size_t off_weight[2]; // [1] is only used when pipelined 267 size_t off_act; 268 size_t off_act_f32; // fp32 activation conversion scratch 269 size_t off_dst[2]; // [1] is only used when pipelined 270 size_t off_scratch[2]; // dequantization scratch pads 271 size_t off_scales; // HMX scales (256 bytes) 272 size_t off_bias; // bias in VTCM 273 274 // Cached sizes of regions for HMX kernel use 275 size_t weight_area_bytes; 276 size_t act_area_bytes; 277 size_t act_f32_bytes; 278 size_t output_area_bytes; 279 size_t scratch_bytes[2]; 280 size_t act_head_stride; 281 size_t bias_bytes; 282 283 size_t total_bytes; 284 }; 285 286 struct htp_mm_hvx_vtcm_layout { 287 // Byte offsets from vtcm_base for each region 288 size_t off_act; // vtcm_act (activation) 289 size_t off_src0; // vtcm_src0 (weight/Wk) 290 size_t off_bias; // vtcm_bias (bias / fused add only) 291 size_t off_dst; // vtcm_dst (output scratch) 292 size_t off_act_raw; // vtcm_act_raw (raw activation DMA staging) 293 294 // Cached sizes 295 size_t src0_bytes; 296 size_t act_bytes; 297 size_t bias_bytes; 298 size_t dst_bytes; 299 size_t act_raw_bytes; 300 301 size_t total_bytes; 302 }; 303 304 static inline void htp_mm_hmx_vtcm_layout_build( 305 struct htp_mm_hmx_vtcm_layout * L, 306 int kernel_type, 307 int wtype, 308 uint32_t k, 309 size_t mc, 310 size_t nc, 311 uint32_t group_size, 312 bool pipeline, 313 uint32_t act_threads, 314 uint32_t aligned_tile_size, 315 size_t bias_size 316 ) { 317 size_t off = 0; 318 319 if (kernel_type == HTP_MM_KERNEL_HMX_BATCHED) { 320 const size_t vec_dot_size = k * sizeof(uint16_t); 321 const size_t act_head_stride = mc * k; 322 const size_t weight_area_size = hex_align_up(nc * vec_dot_size, HTP_MM_HMX_TILE_SIZE); 323 const size_t activation_area_size = hex_align_up(group_size * act_head_stride * sizeof(uint16_t), HTP_MM_HMX_TILE_SIZE); 324 const size_t output_area_size = hex_align_up(group_size * mc * nc * sizeof(uint16_t), HTP_MM_HMX_TILE_SIZE); 325 const size_t scratch_area_size = hex_align_up(nc * vec_dot_size, HTP_MM_HMX_TILE_SIZE); 326 const size_t min_f32_size = hex_align_up(act_threads * HTP_MM_DMA_ACT_MULTIPLIER * k * sizeof(float), 128); 327 328 // Group A: Permanent activation tiles and scales 329 size_t off_group_a = 0; 330 VTCM_LAYOUT_ALLOC(off_group_a, off_act, activation_area_size); 331 VTCM_LAYOUT_ALLOC(off_group_a, off_scales, HTP_MM_HMX_TILE_SIZE); // Padded to 2K for alignment and future persistent data 332 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_bias, hex_align_up(bias_size, HTP_MM_HMX_TILE_SIZE), bias_size > 0); 333 334 // Group B: Compute-only buffers (starts at off_group_a) 335 size_t off_group_b = off_group_a; 336 VTCM_LAYOUT_ALLOC(off_group_b, off_weight[0], weight_area_size); 337 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_weight[1], weight_area_size, false); 338 VTCM_LAYOUT_ALLOC(off_group_b, off_dst[0], output_area_size); 339 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_dst[1], output_area_size, false); 340 VTCM_LAYOUT_ALLOC(off_group_b, off_scratch[0], scratch_area_size); 341 VTCM_LAYOUT_ALLOC(off_group_b, off_scratch[1], scratch_area_size); 342 343 const size_t group_b_size = off_group_b - off_group_a; 344 345 // Group C: Activation prep temporary buffer (overlaps Group B, starting at off_group_a) 346 const size_t max_f32_size = act_threads * 64 * k * sizeof(float); 347 const size_t act_f32_size = hex_align_up(hex_smin(max_f32_size, hex_smax(min_f32_size, group_b_size)), 128); 348 size_t off_group_c = off_group_a; 349 VTCM_LAYOUT_ALLOC(off_group_c, off_act_f32, act_f32_size); 350 351 const size_t group_c_size = off_group_c - off_group_a; 352 353 L->weight_area_bytes = weight_area_size; 354 L->act_area_bytes = activation_area_size; 355 L->act_f32_bytes = act_f32_size; 356 L->output_area_bytes = output_area_size; 357 L->scratch_bytes[0] = scratch_area_size; 358 L->scratch_bytes[1] = scratch_area_size; 359 L->act_head_stride = act_head_stride; 360 L->bias_bytes = bias_size; 361 362 off = off_group_a + hex_smax(group_b_size, group_c_size); 363 } else { 364 // HTP_MM_KERNEL_HMX_2D 365 const bool is_quant = (wtype != HTP_TYPE_F16 && wtype != HTP_TYPE_F32 && wtype != HTP_TYPE_BF16); 366 const size_t row_stride = htp_tensor_get_row_size(wtype, k); 367 const size_t vec_dot_size = k * sizeof(uint16_t); 368 const uint32_t n_weight_tiles = k / htp_tensor_get_tile_k(wtype); 369 370 const size_t min_f32_size = hex_align_up(act_threads * HTP_MM_DMA_ACT_MULTIPLIER * k * sizeof(float), 128); 371 const size_t weight_area_size = is_quant 372 ? hex_align_up((nc / 32) * n_weight_tiles * aligned_tile_size, HTP_MM_HMX_TILE_SIZE) 373 : hex_align_up(nc * row_stride, HTP_MM_HMX_TILE_SIZE); 374 const size_t act_area_size = hex_align_up(mc * vec_dot_size, HTP_MM_HMX_TILE_SIZE); 375 const size_t output_area_size = hex_align_up(mc * nc * sizeof(__fp16), HTP_MM_HMX_TILE_SIZE); 376 377 const size_t scratch0_size = hex_align_up(nc * vec_dot_size, HTP_MM_HMX_TILE_SIZE); 378 const size_t scratch1_size = pipeline ? scratch0_size : 0; 379 380 // Group A: Scales and activation tiles (must not overlap with Group B or C) 381 size_t off_group_a = 0; 382 VTCM_LAYOUT_ALLOC(off_group_a, off_scales, HTP_MM_HMX_TILE_SIZE); // Padded to 2K for alignment and future persistent data 383 VTCM_LAYOUT_ALLOC(off_group_a, off_act, act_area_size); 384 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_bias, hex_align_up(bias_size, HTP_MM_HMX_TILE_SIZE), bias_size > 0); 385 386 // Group B: Compute-only buffers (starts at off_group_a) 387 size_t off_group_b = off_group_a; 388 VTCM_LAYOUT_ALLOC(off_group_b, off_weight[0], weight_area_size); 389 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_weight[1], weight_area_size, pipeline); 390 VTCM_LAYOUT_ALLOC(off_group_b, off_dst[0], output_area_size); 391 VTCM_LAYOUT_ALLOC(off_group_b, off_scratch[0], scratch0_size); 392 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_scratch[1], scratch0_size, pipeline); 393 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_dst[1], output_area_size, pipeline); 394 395 const size_t group_b_size = off_group_b - off_group_a; 396 397 // Group C: Activation prep temporary buffer (overlaps Group B, starting at off_group_a) 398 const size_t max_f32_size = act_threads * 64 * k * sizeof(float); 399 const size_t act_f32_size = hex_align_up(hex_smin(max_f32_size, hex_smax(min_f32_size, group_b_size)), 128); 400 size_t off_group_c = off_group_a; 401 VTCM_LAYOUT_ALLOC(off_group_c, off_act_f32, act_f32_size); 402 403 const size_t group_c_size = off_group_c - off_group_a; 404 405 L->weight_area_bytes = weight_area_size; 406 L->act_area_bytes = act_area_size; 407 L->act_f32_bytes = act_f32_size; 408 L->output_area_bytes = output_area_size; 409 L->scratch_bytes[0] = scratch0_size; 410 L->scratch_bytes[1] = scratch1_size; 411 L->act_head_stride = 0; 412 L->bias_bytes = bias_size; 413 414 off = off_group_a + hex_smax(group_b_size, group_c_size); 415 } 416 417 L->total_bytes = off; 418 } 419 420 static inline void htp_mm_hvx_vtcm_layout_build( 421 struct htp_mm_hvx_vtcm_layout * L, 422 int kernel_type, 423 int wtype, 424 uint32_t ne10, // k 425 uint32_t act_nrows, // m_total 426 uint32_t n_threads, 427 size_t dst_row_size, 428 size_t src0_row_size, 429 size_t act_row_size, 430 size_t bias_row_size, 431 uint32_t n_prefetch, 432 bool is_matmul_id, 433 bool is_fused_nx 434 ) { 435 (void)act_row_size; 436 size_t src0_sz = 0; 437 size_t act_sz = 0; 438 size_t bias_sz = bias_row_size > 0 ? htp_mm_round_up(bias_row_size, 128) : 0; 439 size_t dst_sz = 0; 440 size_t act_raw_sz = 0; 441 442 if (is_fused_nx) { 443 uint32_t aligned_tile_size = htp_tensor_get_tile_padded_size(wtype); 444 uint32_t n_k_tiles = (ne10 + htp_tensor_get_tile_k(wtype) - 1) / htp_tensor_get_tile_k(wtype); 445 uint32_t tile_row_size = n_k_tiles * aligned_tile_size; 446 size_t weight_sz_per_thread = hex_round_up(n_prefetch * tile_row_size, 128); 447 448 size_t tiled_act_row_size = htp_mm_tiled_act_row_size(wtype, ne10); 449 size_t q_act_sz = hex_round_up(tiled_act_row_size * act_nrows, 128); 450 size_t raw_row_size = htp_mm_act_raw_row_size(ne10); 451 452 src0_sz = weight_sz_per_thread * n_threads; // shared single-weight prefetch buffer 453 act_sz = q_act_sz; // quantized activation buffer 454 bias_sz = 0; 455 dst_sz = 0; 456 act_raw_sz = hex_round_up(raw_row_size * act_nrows, 128); 457 } else if (is_matmul_id) { 458 const size_t act_row_size_tiled = htp_mm_tiled_act_row_size(wtype, ne10); 459 const uint32_t aligned_tile_size = htp_tensor_get_tile_padded_size(wtype); 460 const uint32_t n_k_tiles = (ne10 + htp_tensor_get_tile_k(wtype) - 1) / htp_tensor_get_tile_k(wtype); 461 const uint32_t tile_row_size = n_k_tiles * aligned_tile_size; 462 size_t src0_sz_per_thread = htp_mm_round_up(n_prefetch * tile_row_size, 256); 463 464 act_sz = htp_mm_round_up(act_row_size_tiled * act_nrows, 256); 465 466 size_t raw_row_size = htp_mm_act_raw_row_size(ne10); 467 468 src0_sz = src0_sz_per_thread * n_threads; 469 dst_sz = 0; 470 bias_sz = 0; 471 act_raw_sz = hex_round_up(raw_row_size * act_nrows, 128); 472 } else { 473 const size_t src0_row_size_padded = htp_mm_round_up(src0_row_size, 128); 474 const size_t dst_nrows = (act_nrows > 1) ? 0 : 1; 475 476 switch (kernel_type) { 477 case HTP_MM_KERNEL_HVX_F16_F16_VTCM: { 478 size_t f16_act_row_size = htp_mm_round_up(ne10 * 2, 128); 479 act_sz = htp_mm_round_up(f16_act_row_size * act_nrows, 256); 480 src0_sz = htp_mm_round_up(n_prefetch * src0_row_size_padded, 256) * n_threads; 481 dst_sz = dst_nrows > 0 ? htp_mm_round_up(dst_row_size, 128) * n_threads : 0; 482 act_raw_sz = hex_round_up(hex_round_up(ne10 * sizeof(float), 128) * act_nrows, 128); 483 break; 484 } 485 case HTP_MM_KERNEL_HVX_F32_F32_VTCM: 486 case HTP_MM_KERNEL_HVX_BF16_F32_VTCM: { 487 size_t f32_act_row_size = htp_mm_round_up(ne10 * 4, 128); 488 act_sz = htp_mm_round_up(f32_act_row_size * act_nrows, 256); 489 src0_sz = htp_mm_round_up(n_prefetch * src0_row_size_padded, 256) * n_threads; 490 dst_sz = dst_nrows > 0 ? htp_mm_round_up(dst_row_size, 128) * n_threads : 0; 491 act_raw_sz = 0; 492 break; 493 } 494 case HTP_MM_KERNEL_HVX_QUANT_BLOCK: 495 case HTP_MM_KERNEL_HVX_QUANT_ROW: { 496 size_t q_act_row_size = htp_mm_tiled_act_row_size(wtype, ne10); 497 uint32_t aligned_tile_size = htp_tensor_get_tile_padded_size(wtype); 498 uint32_t n_weight_tiles = (ne10 + htp_tensor_get_tile_k(wtype) - 1) / htp_tensor_get_tile_k(wtype); 499 uint32_t tile_row_size = n_weight_tiles * aligned_tile_size; 500 size_t tiled_vtcm_size = htp_mm_round_up(n_prefetch * tile_row_size, 256); 501 502 src0_sz = tiled_vtcm_size * n_threads; 503 act_sz = htp_mm_round_up(q_act_row_size * act_nrows, 256); 504 505 size_t dst_slice_per_thread = (dst_nrows > 0 && act_nrows == 1) ? htp_mm_round_up((dst_row_size + n_threads - 1) / n_threads, 128) : 0; 506 dst_sz = dst_slice_per_thread * n_threads; 507 size_t raw_row_size = htp_mm_act_raw_row_size(ne10); 508 act_raw_sz = hex_round_up(raw_row_size * act_nrows, 128); 509 break; 510 } 511 default: 512 break; 513 } 514 } 515 516 // Group A: Persistent buffers across chunk compute 517 size_t off_group_a = 0; 518 VTCM_LAYOUT_ALLOC(off_group_a, off_act, act_sz); 519 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_bias, bias_sz, bias_sz > 0); 520 521 // Group B: Compute-only buffers (starts at off_group_a) 522 size_t off_group_b = off_group_a; 523 VTCM_LAYOUT_ALLOC(off_group_b, off_src0, src0_sz); 524 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_dst, dst_sz, dst_sz > 0); 525 const size_t group_b_size = off_group_b - off_group_a; 526 527 // Group C: Raw activation staging buffer (overlaps Group B, starts at off_group_a) 528 size_t off_group_c = off_group_a; 529 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_c, off_act_raw, act_raw_sz, act_raw_sz > 0); 530 const size_t group_c_size = off_group_c - off_group_a; 531 532 L->src0_bytes = src0_sz; 533 L->act_bytes = act_sz; 534 L->bias_bytes = bias_sz; 535 L->dst_bytes = dst_sz; 536 L->act_raw_bytes = act_raw_sz; 537 L->total_bytes = off_group_a + hex_smax(group_b_size, group_c_size); 538 } 539 540 static inline bool htp_mm_hvx_solve_vtcm_params( 541 int kernel_type, 542 int wtype, 543 uint32_t ne10, 544 uint32_t act_nrows, 545 uint32_t n_threads, 546 size_t dst_row_size, 547 size_t src0_row_size, 548 size_t act_row_size, 549 size_t bias_row_size, 550 uint32_t n_prefetch, 551 size_t vtcm_budget, 552 struct htp_mm_hvx_vtcm_layout * L_out, 553 uint32_t * m_chunk_out 554 ) { 555 struct htp_mm_hvx_vtcm_layout L; 556 htp_mm_hvx_vtcm_layout_build( 557 &L, kernel_type, wtype, ne10, act_nrows, n_threads, 558 dst_row_size, src0_row_size, act_row_size, bias_row_size, n_prefetch, false, false 559 ); 560 561 if (L.total_bytes <= vtcm_budget) { 562 *L_out = L; 563 *m_chunk_out = act_nrows; 564 return true; 565 } 566 567 const size_t fixed_bytes = L.src0_bytes + L.bias_bytes + L.dst_bytes; 568 if (vtcm_budget <= fixed_bytes) { 569 return false; 570 } 571 572 const size_t avail_act = vtcm_budget - fixed_bytes; 573 size_t row_size = 0; 574 if (kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW || kernel_type == HTP_MM_KERNEL_HVX_QUANT_BLOCK) { 575 row_size = htp_mm_tiled_act_row_size(wtype, ne10); 576 } else if (kernel_type == HTP_MM_KERNEL_HVX_F16_F16_VTCM) { 577 row_size = hex_round_up(ne10 * 2, 128); 578 } else { 579 row_size = hex_round_up(ne10 * 4, 128); 580 } 581 if (row_size == 0) { 582 return false; 583 } 584 585 size_t eff_row_size = row_size; 586 if (kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW || kernel_type == HTP_MM_KERNEL_HVX_QUANT_BLOCK) { 587 eff_row_size += htp_mm_act_raw_row_size(ne10); 588 } 589 590 uint32_t m_chunk = (uint32_t) (avail_act / eff_row_size); 591 if (m_chunk > 1) { 592 m_chunk &= ~1U; 593 } 594 if (m_chunk > act_nrows) { 595 m_chunk = act_nrows; 596 } 597 if (m_chunk < 1) { 598 return false; 599 } 600 601 htp_mm_hvx_vtcm_layout_build( 602 &L, kernel_type, wtype, ne10, m_chunk, n_threads, 603 dst_row_size, src0_row_size, act_row_size, bias_row_size, n_prefetch, false, false 604 ); 605 606 while (m_chunk > 2 && L.total_bytes > vtcm_budget) { 607 m_chunk -= 2; 608 htp_mm_hvx_vtcm_layout_build( 609 &L, kernel_type, wtype, ne10, m_chunk, n_threads, 610 dst_row_size, src0_row_size, act_row_size, bias_row_size, n_prefetch, false, false 611 ); 612 } 613 614 if (L.total_bytes <= vtcm_budget) { 615 *L_out = L; 616 *m_chunk_out = m_chunk; 617 return true; 618 } 619 620 return false; 621 } 622 623 static inline size_t htp_mm_hmx_get_2d_vtcm_size( 624 int wtype, uint32_t k, size_t mc, size_t nc, bool pipeline, uint32_t act_threads, uint32_t aligned_tile_size, size_t bias_size 625 ) { 626 struct htp_mm_hmx_vtcm_layout L; 627 htp_mm_hmx_vtcm_layout_build(&L, HTP_MM_KERNEL_HMX_2D, wtype, k, mc, nc, 1, pipeline, act_threads, aligned_tile_size, bias_size); 628 return L.total_bytes; 629 } 630 631 static inline size_t htp_mm_hmx_get_batched_vtcm_size( 632 int wtype, uint32_t k, size_t mc, size_t nc, uint32_t group_size, bool pipeline, uint32_t act_threads, size_t bias_size) { 633 (void)pipeline; 634 struct htp_mm_hmx_vtcm_layout L; 635 htp_mm_hmx_vtcm_layout_build(&L, HTP_MM_KERNEL_HMX_BATCHED, wtype, k, mc, nc, group_size, false, act_threads, 0, bias_size); 636 return L.total_bytes; 637 } 638 639 static inline bool htp_mm_hmx_solve_batched_params( 640 int wtype, 641 uint32_t k, 642 uint32_t ne01_padded, 643 uint32_t ne11, 644 uint32_t group_size, 645 int n_threads, 646 bool pipeline, 647 size_t bias_size, 648 size_t vtcm_budget, 649 size_t * m_chunk_out, 650 size_t * n_chunk_out, 651 int * act_threads_out, 652 size_t * vtcm_size_out 653 ) { 654 size_t best_mblocks = SIZE_MAX; 655 int best_act_threads = 0; 656 size_t best_m_chunk = 0; 657 size_t best_n_chunk = 0; 658 size_t best_vtcm_size = 0; 659 660 int act_threads = n_threads; 661 while (act_threads >= 1) { 662 size_t group_overhead = htp_mm_hmx_get_batched_overhead() + (bias_size > 0 ? hex_align_up(bias_size, HTP_MM_HMX_TILE_SIZE) : 0); 663 size_t group_size_per_n, group_size_per_m, group_size_per_mn; 664 htp_mm_hmx_get_batched_chunk_costs(k, group_size, &group_size_per_n, &group_size_per_m, &group_size_per_mn); 665 666 size_t m_chunk_candidate = 0; 667 size_t n_chunk_candidate = 0; 668 size_t vtcm_size_candidate = 0; 669 670 if (htp_mm_hmx_compute_chunks(vtcm_budget, group_overhead, group_size_per_n, group_size_per_m, group_size_per_mn, hex_align_up(ne11, 32), ne01_padded, 671 (size_t) ne01_padded * HTP_MM_HMX_COST_W_DEQUANT, (size_t) ne11 * HTP_MM_HMX_COST_A_CONVERT, 672 &m_chunk_candidate, &n_chunk_candidate, &vtcm_size_candidate) == 0) { 673 size_t exact_size = htp_mm_hmx_get_batched_vtcm_size(wtype, k, m_chunk_candidate, n_chunk_candidate, group_size, pipeline, act_threads, bias_size); 674 if (exact_size <= vtcm_budget) { 675 size_t mblocks = ((size_t) ne11 + m_chunk_candidate - 1) / m_chunk_candidate; 676 if (mblocks < best_mblocks || (mblocks == best_mblocks && act_threads > best_act_threads)) { 677 best_mblocks = mblocks; 678 best_act_threads = act_threads; 679 best_m_chunk = m_chunk_candidate; 680 best_n_chunk = n_chunk_candidate; 681 best_vtcm_size = exact_size; 682 } 683 } 684 } 685 if (act_threads == 1) { 686 act_threads = 0; 687 } else { 688 act_threads /= 2; 689 } 690 } 691 692 if (best_act_threads > 0) { 693 *m_chunk_out = best_m_chunk; 694 *n_chunk_out = best_n_chunk; 695 *vtcm_size_out = best_vtcm_size; 696 *act_threads_out = best_act_threads; 697 return true; 698 } 699 return false; 700 } 701 702 static inline bool htp_mm_hmx_solve_2d_params( 703 int wtype, 704 uint32_t k, 705 uint32_t m_id_rows, 706 uint32_t ne01_padded, 707 uint32_t ne11_padded, 708 uint32_t m_for_cost, 709 int n_threads, 710 bool pipeline, 711 bool is_matmul_id, 712 uint32_t aligned_tile_size, 713 size_t bias_size, 714 size_t vtcm_budget, 715 size_t * m_chunk_out, 716 size_t * n_chunk_out, 717 int * act_threads_out, 718 size_t * vtcm_size_out 719 ) { 720 size_t best_mblocks = SIZE_MAX; 721 int best_act_threads = 0; 722 size_t best_m_chunk = 0; 723 size_t best_n_chunk = 0; 724 size_t best_vtcm_size = 0; 725 726 const int m_for_chunks = is_matmul_id ? hex_align_up(m_id_rows, 32) : ne11_padded; 727 728 int act_threads = n_threads; 729 while (act_threads >= 1) { 730 size_t simple_2d_overhead = htp_mm_hmx_get_2d_overhead(pipeline, is_matmul_id) + (bias_size > 0 ? hex_align_up(bias_size, HTP_MM_HMX_TILE_SIZE) : 0); 731 size_t simple_2d_size_per_n, simple_2d_size_per_m, simple_2d_size_per_mn; 732 htp_mm_hmx_get_2d_chunk_costs(wtype, k, pipeline, aligned_tile_size, &simple_2d_size_per_n, &simple_2d_size_per_m, &simple_2d_size_per_mn); 733 734 size_t m_chunk_candidate = 0; 735 size_t n_chunk_candidate = 0; 736 size_t vtcm_size_candidate = 0; 737 738 if (htp_mm_hmx_compute_chunks(vtcm_budget, simple_2d_overhead, simple_2d_size_per_n, simple_2d_size_per_m, simple_2d_size_per_mn, m_for_chunks, ne01_padded, 739 (size_t) ne01_padded * HTP_MM_HMX_COST_W_DEQUANT, (size_t) m_for_cost * HTP_MM_HMX_COST_A_CONVERT, 740 &m_chunk_candidate, &n_chunk_candidate, &vtcm_size_candidate) == 0) { 741 size_t exact_size = htp_mm_hmx_get_2d_vtcm_size(wtype, k, m_chunk_candidate, n_chunk_candidate, pipeline, is_matmul_id ? 0 : act_threads, aligned_tile_size, bias_size); 742 if (exact_size <= vtcm_budget) { 743 size_t mblocks = ((size_t) m_for_cost + m_chunk_candidate - 1) / m_chunk_candidate; 744 if (mblocks < best_mblocks || (mblocks == best_mblocks && act_threads > best_act_threads)) { 745 best_mblocks = mblocks; 746 best_act_threads = act_threads; 747 best_m_chunk = m_chunk_candidate; 748 best_n_chunk = n_chunk_candidate; 749 best_vtcm_size = exact_size; 750 } 751 } 752 } 753 if (act_threads == 1) { 754 act_threads = 0; 755 } else { 756 act_threads /= 2; 757 } 758 } 759 760 if (best_act_threads > 0) { 761 *m_chunk_out = best_m_chunk; 762 *n_chunk_out = best_n_chunk; 763 *vtcm_size_out = best_vtcm_size; 764 *act_threads_out = best_act_threads; 765 return true; 766 } 767 return false; 768 } 769 770 #ifdef __cplusplus 771 } 772 #endif 773 774 #endif // HTP_MATMUL_OPS_H