interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-hexagon / htp / top-k-mask-ops.h

131 lines · 6708 bytes Raw Blame History
1#ifndef HTP_TOP_K_MASK_OPS_H
2#define HTP_TOP_K_MASK_OPS_H
3
4#include "hex-common.h"
5#include "hex-fastdiv.h"
6#include "htp-vtcm.h"
7#include <stdbool.h>
8#include <stdint.h>
9#include <string.h>
10
11// HVX gather results land here (gathers write to VTCM only): 10 vectors per thread
12#define HTP_TOP_K_MASK_SCRATCH_BYTES 1280
13
14enum htp_top_k_mask_kernel {
15 HTP_TOP_K_MASK_KERNEL_ROWS = 0, // threads split rows, row inputs streamed per row
16 HTP_TOP_K_MASK_KERNEL_PICKS = 1, // one row with one resident mask row: threads split the picks, keep flags select mask or -inf
17};
18
19#define HTP_TOP_K_MASK_F16 (1u << 0)
20#define HTP_TOP_K_MASK_HAS_SCORE (1u << 1)
21#define HTP_TOP_K_MASK_HAS_BLOCKS (1u << 2)
22#define HTP_TOP_K_MASK_HAS_EXTRA (1u << 3)
23#define HTP_TOP_K_MASK_MASK_RESIDENT (1u << 4) // mask has exactly 1 row
24
25#define HTP_TOP_K_MASK_UNIT 128 // unit of the compact u16 sizes in kernel_params
26
27struct htp_top_k_mask_shape {
28 uint32_t n_kv, elem_size, n_top, n_pick, n_blk, n_extra;
29 uint32_t has_score, has_blocks, mask_resident;
30};
31
32struct htp_top_k_mask_vtcm_layout {
33 size_t total_bytes;
34 size_t off_blocks, off_mask, off_rowin, off_tiles, off_mtiles; // 0 when unused
35 size_t off_scratch; // HTP_TOP_K_MASK_SCRATCH_BYTES per thread
36 size_t rowin_bytes; // one slot of top_k + extra + score row, parts at htp_top_k_mask_rowin_off_*
37 size_t tile_bytes; // one output tile slot, 128B aligned
38 size_t blocks_bytes, mask_bytes; // exact DMA sizes, 0 when unused
39};
40
41static inline uint64_t htp_top_k_mask_align128(uint64_t n) {
42 return (n + 127) & ~(uint64_t) 127;
43}
44
45// Parts of one row input slot: top_k at 0, then extra, then score; used by the host layout
46static inline uint64_t htp_top_k_mask_rowin_off_extra(uint32_t n_top) {
47 return htp_top_k_mask_align128((uint64_t) n_top * 4);
48}
49
50static inline uint64_t htp_top_k_mask_rowin_off_score(uint32_t n_top, uint32_t n_extra) {
51 return htp_top_k_mask_rowin_off_extra(n_top) + htp_top_k_mask_align128((uint64_t) n_extra * 4);
52}
53
54// the caller compares total_bytes against the VTCM size
55static inline void htp_top_k_mask_vtcm_layout_build(struct htp_top_k_mask_vtcm_layout * L,
56 const struct htp_top_k_mask_shape * S, uint32_t kernel_type, uint32_t n_threads, uint32_t tile_cols) {
57 memset(L, 0, sizeof(*L));
58
59 // PICKS streams the mask straight into its row tile
60 const bool picks = kernel_type == HTP_TOP_K_MASK_KERNEL_PICKS;
61 const uint64_t blocks_exact = S->has_blocks ? (uint64_t) S->n_blk * S->n_pick * 4 : 0;
62 const uint64_t blocks_bytes = htp_top_k_mask_align128(blocks_exact);
63 const uint64_t mask_exact = S->mask_resident && !picks ? (uint64_t) S->n_kv * S->elem_size : 0;
64 const uint64_t mask_bytes = htp_top_k_mask_align128(mask_exact);
65 const uint64_t rowin_bytes = htp_top_k_mask_rowin_off_score(S->n_top, S->n_extra) +
66 (S->has_score ? htp_top_k_mask_align128((uint64_t) S->n_pick * 4) : 0);
67 const uint64_t tile_bytes = htp_top_k_mask_align128((uint64_t) tile_cols * S->elem_size);
68
69 // ROWS streams row inputs into 2 slots per thread. PICKS: one row, a row tile and a flag tile.
70 const uint64_t rowin_total = (picks ? 1 : 2 * (uint64_t) n_threads) * rowin_bytes;
71 const uint64_t tiles_total = picks ? 2 * tile_bytes : 2 * (uint64_t) n_threads * tile_bytes;
72 const uint64_t mtiles_total = S->mask_resident ? 0 : tiles_total;
73 const uint64_t scratch = (uint64_t) n_threads * HTP_TOP_K_MASK_SCRATCH_BYTES;
74
75 size_t off = 0;
76 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_blocks, (size_t) blocks_bytes, blocks_bytes > 0);
77 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_mask, (size_t) mask_bytes, mask_bytes > 0);
78 VTCM_LAYOUT_ALLOC(off, off_rowin, (size_t) rowin_total);
79 VTCM_LAYOUT_ALLOC(off, off_tiles, (size_t) tiles_total);
80 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_mtiles, (size_t) mtiles_total, !S->mask_resident);
81 VTCM_LAYOUT_ALLOC(off, off_scratch, (size_t) scratch);
82
83 L->rowin_bytes = (size_t) rowin_bytes;
84 L->tile_bytes = (size_t) tile_bytes;
85 L->blocks_bytes = (size_t) blocks_exact;
86 L->mask_bytes = (size_t) mask_exact;
87 L->total_bytes = off;
88}
89
90// Set by the host, used as is by the NPU: no tensor shape, stride or layout is derived on the NPU.
91// Sizes of one slot or tile, the slot parts and the VTCM offsets are in HTP_TOP_K_MASK_UNIT bytes as u16.
92// Row strides: byte offset of flat row r of top_k, extra, score and dst is r * stride (the host requires row flattenable inputs).
93// PICKS splits the work in n_threads virtual workers; fewer real workers loop over them.
94struct htp_top_k_mask_kernel_params {
95 uint8_t n_threads, kernel_type, flags, elem_size;
96 uint32_t vtcm_size;
97 uint32_t n_kv, n_top, n_pick, n_extra;
98 uint32_t ninf; // -inf cell pattern of elem_size
99 uint32_t blocks_bytes, score_bytes; // exact DMA sizes, gather regions are [0, bytes)
100 uint32_t tile_cols, n_col_tiles;
101 uint16_t n_blk, blocks_nb1; // blocks_nb1 = n_blk * 4, fits a signed 16-bit multiply
102 uint16_t off_blocks_u, off_mask_u, off_rowin_u, off_tiles_u, off_mtiles_u, off_scratch_u;
103 uint16_t rowin_units, tile_units, off_extra_units, off_score_units;
104 union {
105 struct {
106 uint32_t total_rows;
107 uint32_t rs_top, rs_extra, rs_score, rs_dst; // flat row strides (bytes of row 1)
108 uint32_t mask_nb1; // mask row stride, rows of the mask are dense above it
109 union {
110 uint32_t mask_bytes; // resident mask (1 row)
111 struct { // broadcast mask: row r -> (i1, i2, i3) -> mask row (i mod mne)
112 uint32_t mp[5]; // fastdiv multipliers of dst ne1, ne2 and mask ne1, ne2, ne3
113 uint16_t ne[5]; // the divisors
114 uint8_t l[5]; // fastdiv shifts
115 } dec;
116 } m;
117 } rows;
118 struct {
119 uint32_t picks_per_thread; // per virtual worker, multiple of the lanes of one vector
120 uint16_t blocks_slice_u, top_slice_u, extra_slice_u, score_slice_u, flags_slice_u; // per virtual worker slices
121 } picks;
122 } u;
123};
124
125#if defined(__cplusplus)
126static_assert(sizeof(struct htp_top_k_mask_kernel_params) <= 128, "htp_top_k_mask_kernel_params is too large for kernel_params blob");
127#else
128_Static_assert(sizeof(struct htp_top_k_mask_kernel_params) <= 128, "htp_top_k_mask_kernel_params is too large for kernel_params blob");
129#endif
130
131#endif // HTP_TOP_K_MASK_OPS_H