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 / lightning-indexer-ops.h

213 lines · 9447 bytes Raw Blame History
1#ifndef HTP_LIGHTNING_INDEXER_OPS_H
2#define HTP_LIGHTNING_INDEXER_OPS_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include <stdbool.h>
7
8#include "hex-common.h"
9#include "hex-fastdiv.h"
10#include "htp-vtcm.h"
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#define HTP_LID_KV_BLOCK 64 // kv rows per block (HVX and HMX Bc)
17#define HTP_LID_TOKENS_MAX 32 // max tokens per Q tile (HVX splat path)
18#define HTP_LID_HMX_TOKENS_MAX 512 // HMX can keep more tokens; VTCM decides
19#define HTP_LID_HMX_KV_BLOCK_MAX 2048
20#define HTP_LID_HMX_MAX_QSETS 8 // streams in one HMX pipeline run
21#define HTP_LID_N_BUF 4 // K / mask / output buffers per thread (two blocks computed, two in flight)
22
23#ifndef HMX_FP16_TILE_N_COLS
24#define HMX_FP16_TILE_N_COLS 32
25#endif
26#ifndef HMX_FP16_TILE_SIZE
27#define HMX_FP16_TILE_SIZE 2048
28#endif
29
30enum htp_lid_kernel_type {
31 HTP_LID_KERNEL_HVX = 0,
32 HTP_LID_KERNEL_HMX = 1,
33};
34
35struct htp_lid_kernel_params {
36 uint8_t kernel_type; // enum htp_lid_kernel_type
37 uint32_t n_threads;
38 uint32_t n_tokens_tile; // tokens per Q tile
39 uint32_t n_kv_blocks; // HVX: per stream, HMX: per run of n_qsets sets
40 uint32_t blocks_per_thread;
41 uint32_t vtcm_size;
42 uint32_t k_row_size; // K row bytes
43 uint32_t kv_block; // HMX: kv rows per block (multiple of 64)
44 uint32_t n_qsets; // HMX: Q sets kept in VTCM (streams run in one pipeline)
45 uint32_t n_qbuf; // HMX: Q buffers, 2: the next run's Q is prepped during the current run
46 uint32_t kv_seg; // HMX: rows per set, n_kv padded to 64
47 uint32_t n_tt; // HMX: token tiles per stream
48
49 struct fastdiv_values div_n_head;
50 struct fastdiv_values div_m_ne3; // mask streams (broadcast)
51 struct fastdiv_values div_kv_seg; // HMX
52 struct fastdiv_values div_n_tt; // HMX
53 struct fastdiv_values div_n_ch; // HMX: channel tiles per Q set
54};
55
56#if defined(__cplusplus)
57static_assert(sizeof(struct htp_lid_kernel_params) <= 128, "htp_lid_kernel_params is too large");
58#else
59_Static_assert(sizeof(struct htp_lid_kernel_params) <= 128, "htp_lid_kernel_params is too large");
60#endif
61
62// VTCM layout shared by host (fit check) and device (buffer offsets).
63// Shared regions: Q splats (one f16 vector per q element) and head weight splats for the current token tile.
64// Per-thread regions: Q prep staging, K / mask / output buffers, two transposed K blocks and transpose scratch.
65struct htp_lid_vtcm_layout {
66 size_t off_q;
67 size_t off_w;
68 size_t off_qs;
69 size_t off_k;
70 size_t off_kt;
71 size_t off_m;
72 size_t off_o;
73
74 size_t size_q_row; // f16 Q row, padded
75 size_t size_qs_row; // f32 Q staging row, padded
76 size_t size_k_row; // f16 K row, padded
77 size_t size_k_raw_row;
78 size_t size_m_row; // f16 mask row of one kv block, padded
79 size_t size_o_row; // f32 output row of one kv block, padded
80
81 size_t size_qs; // per thread
82 size_t size_k_buf; // one K buffer
83 size_t size_kt; // per thread: two transposed K blocks, two 64x64 scratch tiles, Q8_0 block scales
84 size_t size_m_buf; // one mask buffer
85 size_t size_o_buf; // one output buffer
86
87 size_t total_bytes;
88};
89
90static inline void htp_lid_vtcm_layout_build(struct htp_lid_vtcm_layout * L, size_t D, size_t n_head, size_t n_tok, size_t n_threads, size_t k_row_size) {
91 L->size_q_row = hex_round_up(D * sizeof(uint16_t), 128);
92 L->size_qs_row = hex_round_up(D * sizeof(float), 128);
93 L->size_k_row = hex_round_up(D * sizeof(uint16_t), 128);
94 L->size_k_raw_row = L->size_k_row > hex_round_up(k_row_size, 128) ? L->size_k_row : hex_round_up(k_row_size, 128);
95 L->size_m_row = hex_round_up(HTP_LID_KV_BLOCK * sizeof(uint16_t), 128);
96 L->size_o_row = hex_round_up(HTP_LID_KV_BLOCK * sizeof(float), 128);
97
98 L->size_qs = 2 * (n_head * L->size_qs_row + hex_round_up(n_head * sizeof(float), 128)) + L->size_q_row;
99 L->size_k_buf = HTP_LID_KV_BLOCK * L->size_k_raw_row;
100 L->size_kt = (2 * hex_round_up(D, HTP_LID_KV_BLOCK) + 2 * HTP_LID_KV_BLOCK + D / 32) * 128;
101 L->size_m_buf = n_tok * L->size_m_row;
102 L->size_o_buf = n_tok * L->size_o_row;
103
104 size_t off = 0;
105 VTCM_LAYOUT_ALLOC(off, off_q, n_tok * n_head * D * 128);
106 VTCM_LAYOUT_ALLOC(off, off_w, n_tok * n_head * 128);
107 VTCM_LAYOUT_ALLOC(off, off_qs, n_threads * L->size_qs);
108 VTCM_LAYOUT_ALLOC(off, off_k, n_threads * HTP_LID_N_BUF * L->size_k_buf);
109 VTCM_LAYOUT_ALLOC(off, off_kt, n_threads * L->size_kt);
110 VTCM_LAYOUT_ALLOC(off, off_m, n_threads * HTP_LID_N_BUF * L->size_m_buf);
111 VTCM_LAYOUT_ALLOC(off, off_o, n_threads * HTP_LID_N_BUF * L->size_o_buf);
112
113 L->total_bytes = off;
114}
115
116static inline size_t htp_lid_compute_vtcm_usage(size_t D, size_t n_head, size_t n_tok, size_t n_threads, size_t k_row_size) {
117 struct htp_lid_vtcm_layout L;
118 htp_lid_vtcm_layout_build(&L, D, n_head, n_tok, n_threads, k_row_size);
119 return L.total_bytes;
120}
121
122// HMX path. K is the activation (kv rows), Q the weights (one output channel per (token, head)).
123// mm1: S = relu(K Q^T) * |w| per channel (HMX convert scale + pos clamp)
124// mm2: O = S G, G[(t,h), t] = sign(w[t,h]). O is [kv, token], f16.
125// Q weights, G, scales and w are per Q set (one per stream when all tokens fit one tile).
126struct htp_lid_hmx_vtcm_layout {
127 size_t off_qw; // Q weight tiles [n_qsets][n_ch][n_dot]
128 size_t off_g; // G weight tiles [n_qsets][n_ch]
129 size_t off_sc; // per channel scales, 256 bytes per channel tile, [n_qsets][n_ch]
130 size_t off_sc_id; // identity scales
131 size_t off_s; // S tiles of one block and token tile, [n_rt][n_sg]
132 size_t off_w; // w of the token tile, [n_qsets][n_tok][n_head] f32
133 size_t off_qs; // per thread: f32 and f16 staging for 32 Q rows, gather scratch
134 size_t off_k_raw[2]; // K rows, [Bc][size_k_row]
135 size_t off_k_t[2]; // K tiles [n_rt][n_dot]
136 size_t off_o[2]; // O tiles [n_rt][n_u]
137 size_t off_m[4]; // mask rows [n_tok][size_m_row]
138 size_t off_out[2]; // f32 output rows [n_tok][size_o_row]
139
140 size_t size_qs_row; // f32 Q row, padded
141 size_t size_qh_row; // f16 Q row, padded
142 size_t size_k_row; // f16 K row, padded
143 size_t size_k_raw_row;
144 size_t size_m_row;
145 size_t size_o_row;
146 size_t size_qs; // per thread
147 size_t size_w; // per Q set
148
149 uint32_t n_ch; // channel tiles: ceil(n_tok * n_head / 32)
150 uint32_t n_u; // token tiles: ceil(n_tok / 32)
151 uint32_t n_dot; // D / 32
152 uint32_t n_rt; // kv row tiles per block: Bc / 32
153 uint32_t n_sg; // S tiles per (row tile, token tile): min(n_head, n_ch)
154
155 size_t total_bytes;
156};
157
158static inline void htp_lid_hmx_vtcm_layout_build(struct htp_lid_hmx_vtcm_layout * L,
159 size_t D, size_t n_head, size_t n_tok, size_t Bc, size_t n_qsets, size_t n_threads, size_t k_row_size) {
160 L->n_ch = (uint32_t) ((n_tok * n_head + 31) / 32);
161 L->n_u = (uint32_t) ((n_tok + 31) / 32);
162 L->n_dot = (uint32_t) (D / 32);
163 L->n_rt = (uint32_t) (Bc / 32);
164 L->n_sg = (uint32_t) (n_head < L->n_ch ? n_head : L->n_ch);
165
166 L->size_qs_row = hex_round_up(D * sizeof(float), 128);
167 L->size_qh_row = hex_round_up(D * sizeof(uint16_t), 128);
168 L->size_k_row = hex_round_up(D * sizeof(uint16_t), 128);
169 L->size_k_raw_row = L->size_k_row > hex_round_up(k_row_size, 128) ? L->size_k_row : hex_round_up(k_row_size, 128);
170 L->size_m_row = hex_round_up(Bc * sizeof(uint16_t), 128);
171 L->size_o_row = hex_round_up(Bc * sizeof(float), 128);
172 L->size_qs = 2 * 32 * L->size_qs_row + 32 * L->size_qh_row + hex_round_up(Bc / 64 * 128, 128);
173 L->size_w = hex_round_up(n_tok * n_head * sizeof(float), 128);
174
175 const size_t tile = HMX_FP16_TILE_SIZE;
176
177 // tile regions first: they need 2 KB alignment
178 size_t off = 0;
179 VTCM_LAYOUT_ALLOC(off, off_qw, n_qsets * L->n_ch * L->n_dot * tile);
180 VTCM_LAYOUT_ALLOC(off, off_g, n_qsets * L->n_ch * tile);
181 VTCM_LAYOUT_ALLOC(off, off_s, (size_t) L->n_rt * L->n_sg * tile);
182 VTCM_LAYOUT_ALLOC(off, off_k_t[0], (size_t) L->n_rt * L->n_dot * tile);
183 VTCM_LAYOUT_ALLOC(off, off_k_t[1], (size_t) L->n_rt * L->n_dot * tile);
184 VTCM_LAYOUT_ALLOC(off, off_o[0], (size_t) L->n_rt * L->n_u * tile);
185 VTCM_LAYOUT_ALLOC(off, off_o[1], (size_t) L->n_rt * L->n_u * tile);
186 VTCM_LAYOUT_ALLOC(off, off_sc, n_qsets * L->n_ch * 256);
187 VTCM_LAYOUT_ALLOC(off, off_sc_id, 256);
188 VTCM_LAYOUT_ALLOC(off, off_w, n_qsets * L->size_w);
189 VTCM_LAYOUT_ALLOC(off, off_qs, n_threads * L->size_qs);
190 for (int i = 0; i < 2; i++) {
191 VTCM_LAYOUT_ALLOC(off, off_k_raw[i], Bc * L->size_k_raw_row);
192 }
193 for (int i = 0; i < 4; i++) {
194 VTCM_LAYOUT_ALLOC(off, off_m[i], n_tok * L->size_m_row);
195 }
196 for (int i = 0; i < 2; i++) {
197 VTCM_LAYOUT_ALLOC(off, off_out[i], n_tok * L->size_o_row);
198 }
199
200 L->total_bytes = off;
201}
202
203static inline size_t htp_lid_hmx_compute_vtcm_usage(size_t D, size_t n_head, size_t n_tok, size_t Bc, size_t n_qsets, size_t n_threads, size_t k_row_size) {
204 struct htp_lid_hmx_vtcm_layout L;
205 htp_lid_hmx_vtcm_layout_build(&L, D, n_head, n_tok, Bc, n_qsets, n_threads, k_row_size);
206 return L.total_bytes;
207}
208
209#ifdef __cplusplus
210}
211#endif
212
213#endif /* HTP_LIGHTNING_INDEXER_OPS_H */