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/flash-attn-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 / flash-attn-ops.h` 410 lines ยท 17766 bytes Raw Blame History 1 #ifndef HTP_FLASH_ATTN_OPS_H 2 #define HTP_FLASH_ATTN_OPS_H 3 4 #include 5 #include 6 #include 7 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 // Tile constants (mirrored from hmx-utils.h for use on host side if needed) 17 #define HTP_FA_HMX_TILE_SIZE 2048 18 #define HMX_FP16_TILE_SIZE 2048 19 #define HMX_FP16_TILE_N_ROWS 32 20 #define HMX_FP16_TILE_N_COLS 32 21 #define HMX_FP16_TILE_N_ELMS 1024 22 23 #define HVX_FA_DMA_CACHE_SIZE 128 24 #define HMX_FA_DMA_CACHE_SIZE 4 25 26 27 #define HTP_FA_M_INITIAL_VAL -10000.0f 28 29 enum htp_fa_kernel_type { 30 HTP_FA_KERNEL_UNSUPPORTED = 0, 31 HTP_FA_KERNEL_HVX, 32 HTP_FA_KERNEL_HMX 33 }; 34 35 // Multicore: gcd(n_kv_heads, N) teams own the KV heads, team members share the Q-blocks 36 #define HTP_FA_FLAG_TEAM_SPLIT 0x01 37 38 static inline void htp_fa_mdev_teams(uint32_t n_kv_heads, uint32_t mdev_count, bool head_split, 39 uint32_t * n_teams, uint32_t * team_size) { 40 const uint32_t t = (head_split && n_kv_heads > 0 && mdev_count > 1) ? hex_gcd_u32(n_kv_heads, mdev_count) : 1; 41 *n_teams = t; 42 *team_size = mdev_count > 1 ? mdev_count / t : 1; 43 } 44 45 struct htp_fa_kernel_params { 46 uint8_t kernel_type; // enum htp_fa_kernel_type 47 uint8_t head_split; // 1 = partition by KV heads in multicore, 0 = token partition 48 uint8_t flags; // HTP_FA_FLAG_* 49 uint8_t n_threads; // Number of threads to run 50 51 // Common parameters 52 uint16_t Br; 53 uint16_t Bc; 54 uint16_t n_kv_blocks; // also HVX's n_blocks 55 uint16_t G; // GQA factor (n_heads / n_kv_heads) 56 57 float scale; 58 float max_bias; 59 float logit_softcap; 60 uint32_t vtcm_size; 61 62 float m0; 63 float m1; 64 uint32_t n_head_log2; 65 66 struct fastdiv_values src3_div2; 67 struct fastdiv_values src3_div3; 68 69 struct fastdiv_values broadcast_rk2; 70 struct fastdiv_values broadcast_rk3; 71 struct fastdiv_values broadcast_rv2; 72 struct fastdiv_values broadcast_rv3; 73 74 union { 75 struct { 76 uint32_t g_br; 77 int32_t mask_broadcast; 78 int32_t pipeline; 79 struct fastdiv_values div_G; 80 } hmx; 81 struct { 82 uint32_t size_q_row_padded; 83 uint32_t size_k_row_padded; 84 uint32_t size_v_row_padded; 85 struct fastdiv_values src0_div21; 86 struct fastdiv_values src0_div1; 87 } hvx; 88 } u; 89 }; 90 91 #if defined(__cplusplus) 92 static_assert(sizeof(struct htp_fa_kernel_params) <= 128, "htp_fa_kernel_params is too large for kernel_params blob"); 93 #else 94 _Static_assert(sizeof(struct htp_fa_kernel_params) <= 128, "htp_fa_kernel_params is too large for kernel_params blob"); 95 #endif 96 97 // VTCM region layout for the HMX flash-attention kernel. 98 // 99 // Single source of truth for both the host (which needs the total size to pick a 100 // (Br, Bc) tiling that fits the VTCM budget) and the device (which needs the actual 101 // byte offsets to place each scratch buffer). Building the layout once and reading 102 // offsets/total from it makes host estimate and device allocation impossible to 103 // desync -- previously they were duplicated formulas in two files and drifted. 104 // 105 // All fields are byte offsets / byte sizes -- no HVX_Vector type is named here so the 106 // header stays host-includable. The device casts (base + off_*) to the proper type. 107 // An offset of 0 marks a region that is not allocated for this configuration (only 108 // off_v_tiles[1], which exists only when pipelining); the device sets such pointers NULL. 109 struct hmx_fa_vtcm_layout { 110 // Byte offsets from vtcm_base for each region. 111 size_t off_q_tiles; 112 size_t off_q_dma; 113 size_t off_o_tiles[2]; 114 size_t off_k_fp16[2]; 115 size_t off_v_fp16[2]; 116 size_t off_k_tiles[2]; 117 size_t off_v_tiles[2]; 118 size_t off_s_tiles[2]; 119 size_t off_p_tiles[2]; 120 size_t off_d_tiles[2]; 121 size_t off_d_inv_l; 122 size_t off_m_vec; 123 size_t off_l_vec; 124 size_t off_s_rowmax; 125 size_t off_p_rowsum; 126 size_t off_row_bufs; 127 size_t off_hmx_scales_id; 128 size_t off_hmx_scales_qk; 129 size_t off_mask_buf; 130 size_t off_slopes; 131 size_t off_sinks; 132 133 // Region byte sizes reused by the device at runtime (not just for allocation). 134 size_t q_tile_bytes; 135 size_t o_tile_bytes; 136 size_t k_tile_bytes; 137 size_t s_tile_bytes; // S and P tiles (same size) 138 size_t d_tile_bytes; // d_tiles[0..1] + d_inv_l, allocated back to back 139 size_t m_line_bytes; // one mask row 140 size_t m_buf_slot_bytes; // one dma_cache slot = align_up(Br * m_line_bytes, 4096) 141 size_t col_vec_bytes; 142 size_t sinks_bytes; 143 144 // Derived strides. 145 size_t row_buf_stride; // HVX vectors (128B) per row buffer 146 size_t mask_buf_row_stride; // __fp16 elements per row in the mask buffer 147 148 bool pipeline; 149 size_t total_bytes; 150 }; 151 152 #define FA_MAX_KV_BLOCKS 1024 153 154 // Build the VTCM layout. 155 156 static inline void hmx_fa_vtcm_layout_build(struct hmx_fa_vtcm_layout * L, 157 size_t gqa_factor, size_t DK, size_t DV, 158 size_t Br, size_t Bc, size_t n_threads, 159 bool pipeline, bool is_q_fp32, 160 bool has_sinks, size_t n_heads) { 161 const size_t g_br = hex_align_up(gqa_factor * Br, HMX_FP16_TILE_N_ROWS); 162 const size_t q_tile_size = hex_align_up(g_br * DK * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE); 163 const size_t o_tile_size = hex_align_up(g_br * DV * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE); 164 const size_t k_tile_size = hex_align_up(Bc * DK * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE); 165 const size_t v_tile_size = hex_align_up(Bc * DV * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE); 166 const size_t s_tile_size = hex_align_up(g_br * Bc * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE); 167 168 // The rescale matrices are diagonal: the HMX kernels only ever load the g_br/32 169 // tiles that sit on the diagonal, so store just those, packed back to back with 170 // a stride of one tile. The old [g_br, g_br] square layout allocated g_br/32 171 // times more than it used, which is also why a second D buffer was unaffordable. 172 const size_t d_tile_size = (g_br / HMX_FP16_TILE_N_ROWS) * HTP_FA_HMX_TILE_SIZE; 173 174 const size_t q_dma_size = hex_align_up(g_br * DK * (is_q_fp32 ? sizeof(float) : sizeof(__fp16)), 128); 175 const size_t k_dma_size = hex_align_up(Bc * hex_round_up(DK * sizeof(__fp16), 128), 128); 176 const size_t v_dma_size = hex_align_up(Bc * hex_round_up(DV * sizeof(__fp16), 128), 128); 177 const size_t col_vec_size = hex_align_up(g_br * sizeof(float), 256); 178 const size_t row_vec_size = hex_align_up(Bc * sizeof(__fp16), 256); 179 const size_t m_line_size = hex_align_up(Bc * sizeof(__fp16), 128); 180 const size_t m_buf_slot = hex_align_up(Br * m_line_size, 256); 181 const size_t m_buf_size = m_buf_slot * HMX_FA_DMA_CACHE_SIZE; 182 const size_t slopes_size = hex_align_up(g_br * sizeof(__fp16), 128); 183 const size_t sinks_size = hex_round_up(n_heads * sizeof(float), 128); 184 185 size_t off = 0; 186 187 // Group A (Part 1 - HMX Tiled buffers) 188 VTCM_LAYOUT_ALLOC(off, off_q_tiles, q_tile_size); 189 VTCM_LAYOUT_ALLOC(off, off_o_tiles[0], o_tile_size); 190 VTCM_LAYOUT_ALLOC(off, off_o_tiles[1], o_tile_size); 191 VTCM_LAYOUT_ALLOC(off, off_d_tiles[0], d_tile_size); 192 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_d_tiles[1], d_tile_size, pipeline); 193 VTCM_LAYOUT_ALLOC(off, off_d_inv_l, d_tile_size); 194 195 // Group B & C share start offset (Group B tiles must be 2KB aligned) 196 size_t off_group_b_c = hex_align_up(off, HTP_FA_HMX_TILE_SIZE); 197 198 // Group B: Compute-only buffers 199 size_t off_group_b = off_group_b_c; 200 VTCM_LAYOUT_ALLOC(off_group_b, off_k_tiles[0], k_tile_size); 201 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_k_tiles[1], k_tile_size, pipeline); 202 VTCM_LAYOUT_ALLOC(off_group_b, off_v_tiles[0], v_tile_size); 203 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_v_tiles[1], v_tile_size, pipeline); 204 VTCM_LAYOUT_ALLOC(off_group_b, off_s_tiles[0], s_tile_size); 205 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_s_tiles[1], s_tile_size, pipeline); 206 VTCM_LAYOUT_ALLOC(off_group_b, off_p_tiles[0], s_tile_size); 207 VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_p_tiles[1], s_tile_size, pipeline); 208 VTCM_LAYOUT_ALLOC(off_group_b, off_s_rowmax, col_vec_size); 209 VTCM_LAYOUT_ALLOC(off_group_b, off_p_rowsum, col_vec_size); 210 VTCM_LAYOUT_ALLOC(off_group_b, off_row_bufs, row_vec_size * 2 * n_threads); 211 212 const size_t group_b_size = off_group_b - off_group_b_c; 213 214 // Group C: Q fetch DMA buffer 215 size_t off_group_c = off_group_b_c; 216 VTCM_LAYOUT_ALLOC(off_group_c, off_q_dma, q_dma_size); 217 218 const size_t group_c_size = off_group_c - off_group_b_c; 219 220 off = off_group_b_c + hex_smax(group_b_size, group_c_size); 221 222 // Group A (Part 2 - remaining non-HMX buffers) 223 VTCM_LAYOUT_ALLOC(off, off_k_fp16[0], k_dma_size); 224 VTCM_LAYOUT_ALLOC(off, off_k_fp16[1], k_dma_size); 225 VTCM_LAYOUT_ALLOC(off, off_v_fp16[0], v_dma_size); 226 VTCM_LAYOUT_ALLOC(off, off_v_fp16[1], v_dma_size); 227 VTCM_LAYOUT_ALLOC(off, off_m_vec, col_vec_size); 228 VTCM_LAYOUT_ALLOC(off, off_l_vec, col_vec_size); 229 VTCM_LAYOUT_ALLOC(off, off_hmx_scales_id, 256); 230 VTCM_LAYOUT_ALLOC(off, off_hmx_scales_qk, 256); 231 VTCM_LAYOUT_ALLOC(off, off_mask_buf, m_buf_size); 232 VTCM_LAYOUT_ALLOC(off, off_slopes, slopes_size); 233 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_sinks, sinks_size, has_sinks); 234 235 L->q_tile_bytes = q_tile_size; 236 L->o_tile_bytes = o_tile_size; 237 L->k_tile_bytes = k_tile_size; 238 L->col_vec_bytes = col_vec_size; 239 L->s_tile_bytes = s_tile_size; 240 // Measured from the actual offsets rather than assumed to be N * d_tile_size, so 241 // that inserting a region between them (or adding padding to VTCM_LAYOUT_ALLOC) 242 // cannot silently leave the tail of the run unzeroed. 243 L->d_tile_bytes = (L->off_d_inv_l + d_tile_size) - L->off_d_tiles[0]; 244 L->m_line_bytes = m_line_size; 245 L->m_buf_slot_bytes = m_buf_slot; 246 L->row_buf_stride = row_vec_size / 128; 247 L->mask_buf_row_stride = m_line_size / sizeof(__fp16); 248 L->sinks_bytes = has_sinks ? sinks_size : 0; 249 L->pipeline = pipeline; 250 L->total_bytes = off; 251 } 252 253 // Exact VTCM usage for a given (gqa_factor, DK, DV, Br, Bc) configuration. 254 static inline size_t hmx_fa_compute_vtcm_usage(size_t gqa_factor, size_t DK, size_t DV, size_t Br, size_t Bc, size_t n_threads, bool pipeline, bool is_q_fp32, bool has_sinks, size_t n_heads) { 255 struct hmx_fa_vtcm_layout L; 256 hmx_fa_vtcm_layout_build(&L, gqa_factor, DK, DV, Br, Bc, n_threads, pipeline, is_q_fp32, has_sinks, n_heads); 257 return L.total_bytes; 258 } 259 260 #define FA_HVX_BLOCK_SIZE 64 261 #define FA_HVX_G_MAX 8 262 263 struct hvx_fa_vtcm_layout { 264 size_t off_q; 265 size_t off_k; 266 size_t off_v; 267 size_t off_m; 268 size_t off_a; 269 size_t off_sinks; 270 271 size_t size_q_block; 272 size_t size_k_block; 273 size_t size_v_block; 274 size_t size_m_block; 275 size_t size_vkq_acc; 276 size_t size_sinks; 277 278 size_t total_bytes; 279 }; 280 281 static inline void hvx_fa_vtcm_layout_build(struct hvx_fa_vtcm_layout * L, 282 size_t DK, size_t DV, 283 bool is_q_fp32, bool has_mask, 284 bool has_sinks, size_t n_heads, 285 size_t n_threads) { 286 const size_t size_q_row_padded = hex_round_up(DK * (is_q_fp32 ? 4 : 2), 128); 287 const size_t size_k_row_padded = hex_round_up(DK * sizeof(__fp16), 128); 288 const size_t size_v_row_padded = hex_round_up(DV * sizeof(__fp16), 128); 289 290 const size_t size_q_block = size_q_row_padded * FA_HVX_G_MAX; 291 const size_t size_k_block = size_k_row_padded * FA_HVX_BLOCK_SIZE; 292 const size_t size_v_block = size_v_row_padded * FA_HVX_BLOCK_SIZE; 293 const size_t size_m_block = hex_round_up(FA_HVX_BLOCK_SIZE * sizeof(__fp16), 128); 294 const size_t size_vkq_acc = hex_round_up(DV * sizeof(float), 128) * FA_HVX_G_MAX; 295 const size_t size_sinks = hex_round_up(n_heads * sizeof(float), 128); 296 297 size_t off = 0; 298 299 VTCM_LAYOUT_ALLOC(off, off_q, size_q_block * n_threads); 300 VTCM_LAYOUT_ALLOC(off, off_k, size_k_block * 2 * n_threads); 301 VTCM_LAYOUT_ALLOC(off, off_v, size_v_block * 2 * n_threads); 302 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_m, size_m_block * HVX_FA_DMA_CACHE_SIZE * n_threads, has_mask); 303 VTCM_LAYOUT_ALLOC(off, off_a, size_vkq_acc * n_threads); 304 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_sinks, size_sinks, has_sinks); 305 306 L->size_q_block = size_q_block; 307 L->size_k_block = size_k_block; 308 L->size_v_block = size_v_block; 309 L->size_m_block = size_m_block; 310 L->size_vkq_acc = size_vkq_acc; 311 L->size_sinks = has_sinks ? size_sinks : 0; 312 L->total_bytes = off; 313 } 314 315 static inline size_t hvx_fa_compute_vtcm_usage(size_t DK, size_t DV, bool is_q_fp32, bool has_mask, bool has_sinks, size_t n_heads, size_t n_threads) { 316 struct hvx_fa_vtcm_layout L; 317 hvx_fa_vtcm_layout_build(&L, DK, DV, is_q_fp32, has_mask, has_sinks, n_heads, n_threads); 318 return L.total_bytes; 319 } 320 321 #define FA_MIN_KV_BLOCKS 3 322 323 // Cost-based (Br, Bc) search for flash attention with pipeline constraint. 324 static inline int hmx_fa_find_chunk_size(size_t * Br_out, 325 size_t * Bc_out, 326 size_t gqa_factor, 327 size_t DK, 328 size_t DV, 329 size_t qo_len, 330 size_t kv_len, 331 size_t vtcm_budget, 332 size_t n_threads, 333 bool is_q_fp32, 334 bool has_sinks, 335 size_t n_heads, 336 size_t q_split) { 337 const size_t T = HMX_FP16_TILE_N_ROWS; // 32 338 const size_t br_unit = hmx_ceil_div(T, gqa_factor); 339 const size_t bc_unit = HMX_FP16_TILE_N_COLS * 2; // 64 340 const bool can_pipeline = (kv_len >= FA_MIN_KV_BLOCKS * bc_unit && n_threads >= 2); 341 342 // Br_max: largest Br aligned to br_unit that does not exceed qo_len. 343 const size_t Br_max = qo_len >= br_unit ? hex_align_down(qo_len, br_unit) : br_unit; 344 345 // Pipeline constraint: cap Bc so n_kv_blocks >= FA_MIN_KV_BLOCKS. 346 // Only relax when kv_len is too short to form enough blocks. 347 size_t Bc_limit = can_pipeline ? hex_align_down(kv_len / FA_MIN_KV_BLOCKS, bc_unit) : 348 (kv_len >= bc_unit ? hex_align_down(kv_len, bc_unit) : bc_unit); 349 // For small-Q SWA-sized KV caches (qo_len <= 16, kv_len <= 2048), cap Bc at 256 for block-skipping granularity 350 if (qo_len <= 16 && kv_len <= 2048 && Bc_limit > 256) { 351 Bc_limit = 256; 352 } 353 // Cost coefficients calibrated from profiling 354 const size_t c_q_fixed = 800; // per-Q-block: q_load + epilogue o_update + o_norm + o_store 355 const size_t c_iter_base = 200; // per-KV-iter base (HMX dot/update + DMA) 356 const size_t c_softmax = 600; // per 64-row vector chunk on HVX 357 358 size_t best_cost = SIZE_MAX, best_mn = 0; 359 size_t best_Br = 0, best_Bc = 0; 360 361 for (size_t Br = Br_max; Br >= br_unit; Br -= br_unit) { 362 // Try all Bc candidates from Bc_limit down to bc_unit 363 for (size_t Bc = Bc_limit; Bc >= bc_unit; Bc -= bc_unit) { 364 size_t vtcm_needed = hmx_fa_compute_vtcm_usage(gqa_factor, DK, DV, Br, Bc, n_threads, can_pipeline, is_q_fp32, has_sinks, n_heads); 365 if (vtcm_needed <= vtcm_budget) { 366 // This Bc fits for this Br! 367 const size_t q_blocks = (qo_len + Br - 1) / Br; 368 const size_t kv_blocks = (kv_len + Bc - 1) / Bc; 369 const size_t actual_threads = (kv_blocks >= 3 && n_threads >= 2) ? n_threads : 1; 370 const size_t n_rows_g = Br * gqa_factor; 371 const size_t n_row_vec_cnt = (n_rows_g + 63) / 64; 372 const size_t n_use = n_row_vec_cnt < actual_threads ? n_row_vec_cnt : actual_threads; 373 const size_t vecs_per_t = n_use > 0 ? (n_row_vec_cnt + n_use - 1) / n_use : 1; 374 375 const size_t c_iter_actual = c_iter_base + c_softmax * vecs_per_t; 376 const size_t q_blocks_nsp = (q_blocks + q_split - 1) / (q_split ? q_split : 1); 377 const size_t cost = q_blocks_nsp * (c_q_fixed + kv_blocks * c_iter_actual); 378 const size_t mn = Br * Bc; 379 380 if (cost < best_cost || (cost == best_cost && mn > best_mn)) { 381 best_cost = cost; 382 best_mn = mn; 383 best_Br = Br; 384 best_Bc = Bc; 385 } 386 // Since we iterate Bc from largest to smallest, this is the largest Bc that fits 387 // for this Br. We can break to the next Br. 388 break; 389 } 390 } 391 392 if (Br == br_unit) { 393 break; 394 } 395 } 396 397 if (best_Br == 0 || best_Bc == 0) { 398 return -1; 399 } 400 401 *Br_out = best_Br; 402 *Bc_out = best_Bc; 403 return 0; 404 } 405 406 #ifdef __cplusplus 407 } 408 #endif 409 410 #endif /* HTP_FLASH_ATTN_OPS_H */