#ifndef HTP_TOP_K_MASK_OPS_H
#define HTP_TOP_K_MASK_OPS_H
#include "hex-common.h"
#include "hex-fastdiv.h"
#include "htp-vtcm.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#define HTP_TOP_K_MASK_SCRATCH_BYTES 1280
enum htp_top_k_mask_kernel {
HTP_TOP_K_MASK_KERNEL_ROWS = 0, HTP_TOP_K_MASK_KERNEL_PICKS = 1, };
#define HTP_TOP_K_MASK_F16 (1u << 0)
#define HTP_TOP_K_MASK_HAS_SCORE (1u << 1)
#define HTP_TOP_K_MASK_HAS_BLOCKS (1u << 2)
#define HTP_TOP_K_MASK_HAS_EXTRA (1u << 3)
#define HTP_TOP_K_MASK_MASK_RESIDENT (1u << 4)
#define HTP_TOP_K_MASK_UNIT 128
struct htp_top_k_mask_shape {
uint32_t n_kv, elem_size, n_top, n_pick, n_blk, n_extra;
uint32_t has_score, has_blocks, mask_resident;
};
struct htp_top_k_mask_vtcm_layout {
size_t total_bytes;
size_t off_blocks, off_mask, off_rowin, off_tiles, off_mtiles; size_t off_scratch; size_t rowin_bytes; size_t tile_bytes; size_t blocks_bytes, mask_bytes; };
static inline uint64_t htp_top_k_mask_align128(uint64_t n) {
return (n + 127) & ~(uint64_t) 127;
}
static inline uint64_t htp_top_k_mask_rowin_off_extra(uint32_t n_top) {
return htp_top_k_mask_align128((uint64_t) n_top * 4);
}
static inline uint64_t htp_top_k_mask_rowin_off_score(uint32_t n_top, uint32_t n_extra) {
return htp_top_k_mask_rowin_off_extra(n_top) + htp_top_k_mask_align128((uint64_t) n_extra * 4);
}
static inline void htp_top_k_mask_vtcm_layout_build(struct htp_top_k_mask_vtcm_layout * L,
const struct htp_top_k_mask_shape * S, uint32_t kernel_type, uint32_t n_hvx, uint32_t tile_cols) {
memset(L, 0, sizeof(*L));
const bool picks = kernel_type == HTP_TOP_K_MASK_KERNEL_PICKS;
const uint64_t blocks_exact = S->has_blocks ? (uint64_t) S->n_blk * S->n_pick * 4 : 0;
const uint64_t blocks_bytes = htp_top_k_mask_align128(blocks_exact);
const uint64_t mask_exact = S->mask_resident && !picks ? (uint64_t) S->n_kv * S->elem_size : 0;
const uint64_t mask_bytes = htp_top_k_mask_align128(mask_exact);
const uint64_t rowin_bytes = htp_top_k_mask_rowin_off_score(S->n_top, S->n_extra) +
(S->has_score ? htp_top_k_mask_align128((uint64_t) S->n_pick * 4) : 0);
const uint64_t tile_bytes = htp_top_k_mask_align128((uint64_t) tile_cols * S->elem_size);
const uint64_t rowin_total = (picks ? 1 : 2 * (uint64_t) n_hvx) * rowin_bytes;
const uint64_t tiles_total = picks ? 2 * tile_bytes : 2 * (uint64_t) n_hvx * tile_bytes;
const uint64_t mtiles_total = S->mask_resident ? 0 : tiles_total;
const uint64_t scratch = (uint64_t) n_hvx * HTP_TOP_K_MASK_SCRATCH_BYTES;
size_t off = 0;
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_blocks, (size_t) blocks_bytes, blocks_bytes > 0);
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_mask, (size_t) mask_bytes, mask_bytes > 0);
VTCM_LAYOUT_ALLOC(off, off_rowin, (size_t) rowin_total);
VTCM_LAYOUT_ALLOC(off, off_tiles, (size_t) tiles_total);
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_mtiles, (size_t) mtiles_total, !S->mask_resident);
VTCM_LAYOUT_ALLOC(off, off_scratch, (size_t) scratch);
L->rowin_bytes = (size_t) rowin_bytes;
L->tile_bytes = (size_t) tile_bytes;
L->blocks_bytes = (size_t) blocks_exact;
L->mask_bytes = (size_t) mask_exact;
L->total_bytes = off;
}
struct htp_top_k_mask_kernel_params {
uint8_t n_hvx, kernel_type, flags, elem_size;
uint32_t vtcm_size;
uint32_t n_kv, n_top, n_pick, n_extra;
uint32_t ninf; uint32_t blocks_bytes, score_bytes; uint32_t tile_cols, n_col_tiles;
uint16_t n_blk, blocks_nb1; uint16_t off_blocks_u, off_mask_u, off_rowin_u, off_tiles_u, off_mtiles_u, off_scratch_u;
uint16_t rowin_units, tile_units, off_extra_units, off_score_units;
union {
struct {
uint32_t total_rows;
uint32_t rs_top, rs_extra, rs_score, rs_dst; uint32_t mask_nb1; union {
uint32_t mask_bytes; struct { uint32_t mp[5]; uint16_t ne[5]; uint8_t l[5]; } dec;
} m;
} rows;
struct {
uint32_t picks_per_hvx; uint16_t blocks_slice_u, top_slice_u, extra_slice_u, score_slice_u, flags_slice_u; } picks;
} u;
};
#if defined(__cplusplus)
static_assert(sizeof(struct htp_top_k_mask_kernel_params) <= 128, "htp_top_k_mask_kernel_params is too large for kernel_params blob");
#else
_Static_assert(sizeof(struct htp_top_k_mask_kernel_params) <= 128, "htp_top_k_mask_kernel_params is too large for kernel_params blob");
#endif
#endif