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 / gated-delta-net-ops.h

297 lines · 10423 bytes Raw Blame History
1#ifndef HTP_GATED_DELTA_NET_OPS_H
2#define HTP_GATED_DELTA_NET_OPS_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include <stdbool.h>
7
8#include "hex-fastdiv.h"
9#include "hex-common.h"
10#include "htp-vtcm.h"
11
12#define HTP_GDN_MAX_SV 128
13#define HTP_GDN_CHUNK_SIZE 64
14#define HTP_GDN_MIN_TOKENS 8
15
16#ifndef HMX_FP16_TILE_SIZE
17#define HMX_FP16_TILE_SIZE 2048
18#endif
19
20enum htp_gdn_kernel_type {
21 HTP_GDN_KERNEL_HVX_RECURRENT = 0,
22 HTP_GDN_KERNEL_HMX_CHUNKED = 1,
23};
24
25struct htp_gdn_kernel_params {
26 uint8_t kernel_type;
27 uint8_t pipeline;
28 uint16_t chunk_size;
29 uint16_t n_chunks;
30 uint16_t n_heads_batch;
31
32 uint32_t n_threads;
33 uint32_t S_v;
34 uint32_t H;
35 uint32_t n_tokens;
36 uint32_t n_seqs;
37 uint32_t K;
38
39 uint32_t total_rows;
40 uint32_t kda;
41 uint32_t vtcm_size;
42 uint32_t state_seq_stride;
43 uint32_t state_size_per_snap;
44
45 float scale;
46
47 struct fastdiv_values div_H;
48 struct fastdiv_values div_q1;
49 struct fastdiv_values div_k1;
50 struct fastdiv_values div_rq3;
51 struct fastdiv_values div_rk3;
52};
53
54#if defined(__cplusplus)
55static_assert(sizeof(struct htp_gdn_kernel_params) <= 128, "htp_gdn_kernel_params is too large for kernel_params blob");
56#else
57_Static_assert(sizeof(struct htp_gdn_kernel_params) <= 128, "htp_gdn_kernel_params is too large for kernel_params blob");
58#endif
59
60struct htp_gdn_vtcm_layout {
61 size_t state_aligned;
62 size_t bytes_per_thread;
63 size_t off_state;
64 size_t total_bytes;
65};
66
67static inline void htp_gdn_vtcm_layout_build(
68 struct htp_gdn_vtcm_layout * L,
69 uint32_t S_v,
70 uint32_t n_threads
71) {
72 size_t state_bytes = (size_t) S_v * S_v * sizeof(float);
73 L->state_aligned = hex_round_up(state_bytes, 128);
74 L->bytes_per_thread = 2 * L->state_aligned;
75 size_t off = 0;
76 VTCM_LAYOUT_ALLOC(off, off_state, L->bytes_per_thread * n_threads);
77 L->total_bytes = off;
78}
79
80struct htp_gdn_hmx_vtcm_layout {
81 size_t off_s_state;
82 size_t off_s_f16;
83 size_t off_s_col_tiles;
84 size_t off_s_update_f32;
85 size_t off_s_update_tiles;
86
87 size_t off_q_f32[2];
88 size_t off_k_f32[2];
89 size_t off_v_f32[2];
90 size_t off_g_f32[2];
91 size_t off_b_f32[2];
92 size_t off_g_raw[2];
93 size_t off_b_raw[2];
94 size_t off_o_f32[2];
95
96 size_t off_v_inter_f32;
97 size_t off_o_inter_f32;
98 size_t off_o_intra_f32;
99 size_t off_k_f16;
100 size_t off_v_prime_f16;
101 size_t off_delta_f16;
102 size_t off_d_f16;
103
104 size_t off_q_row_tiles;
105 size_t off_q_prime_row_tiles;
106 size_t off_k_row_tiles;
107 size_t off_k_col_tiles;
108 size_t off_k_prime_row_tiles;
109 size_t off_k_col_tiles_64x128;
110 size_t off_kk_tiles;
111 size_t off_qk_tiles;
112 size_t off_v_inter_tiles;
113 size_t off_o_inter_tiles;
114 size_t off_inv_row_tiles;
115 size_t off_a_row_tiles;
116 size_t off_v_prime_col_tiles;
117 size_t off_delta_tiles;
118 size_t off_delta_col_tiles;
119 size_t off_o_intra_tiles;
120 size_t off_d_row_tiles;
121
122 size_t off_gamma;
123 size_t off_lambda_init;
124 size_t off_decay_m;
125 size_t off_decay_a;
126 size_t off_rows_kk;
127 size_t off_rows_qk;
128 size_t off_rows_inv;
129 size_t off_rows_a;
130
131 size_t off_thread_scratch;
132 size_t off_scales_1;
133
134 size_t state_f32_bytes;
135 size_t state_f16_bytes;
136 size_t state_tiles_bytes;
137 size_t dma_chunk_bytes;
138 size_t act_f16_bytes;
139 size_t tile_64xSv_bytes;
140 size_t tile_64x64_bytes;
141
142 uint32_t n_heads_batch;
143 uint32_t n_threads;
144 bool pipeline;
145 size_t total_bytes;
146};
147
148static inline void htp_gdn_hmx_vtcm_layout_build(
149 struct htp_gdn_hmx_vtcm_layout * L,
150 uint32_t S_v,
151 uint32_t chunk_size,
152 uint32_t n_heads_batch,
153 uint32_t n_threads,
154 bool pipeline
155) {
156 memset(L, 0, sizeof(*L));
157 L->n_heads_batch = n_heads_batch;
158 L->n_threads = n_threads;
159 L->pipeline = pipeline;
160
161 const size_t bh = (size_t) n_heads_batch;
162 const size_t nth = (size_t) (n_threads > 0 ? n_threads : 1);
163
164 const size_t state_f32_sz = hex_round_up(S_v * S_v * sizeof(float), 2048);
165 const size_t state_f16_sz = hex_round_up(S_v * S_v * sizeof(__fp16), 2048);
166 const size_t n_sv_tiles = S_v / 32;
167 const size_t state_tiles_sz = n_sv_tiles * n_sv_tiles * 2048;
168
169 const size_t dma_chunk_sz = hex_round_up(chunk_size * S_v * sizeof(float), 2048);
170 const size_t dma_scalar_sz = hex_round_up(chunk_size * sizeof(float), 128);
171
172 const size_t act_f16_sz = hex_round_up(chunk_size * S_v * sizeof(__fp16), 2048);
173 const size_t tile_64xSv_sz = 2 * n_sv_tiles * 2048;
174 const size_t tile_64x64_sz = 4 * 2048;
175
176 const size_t decay_sz = 64 * 64 * sizeof(__fp16);
177 const size_t row_vecs_sz = 64 * 128;
178
179 L->state_f32_bytes = state_f32_sz;
180 L->state_f16_bytes = state_f16_sz;
181 L->state_tiles_bytes = state_tiles_sz;
182 L->dma_chunk_bytes = dma_chunk_sz;
183 L->act_f16_bytes = act_f16_sz;
184 L->tile_64xSv_bytes = tile_64xSv_sz;
185 L->tile_64x64_bytes = tile_64x64_sz;
186
187 size_t off = 0;
188
189 VTCM_LAYOUT_ALLOC(off, off_s_state, bh * state_f32_sz);
190 VTCM_LAYOUT_ALLOC(off, off_s_f16, bh * state_f16_sz);
191 off = hex_align_up(off, HMX_FP16_TILE_SIZE);
192 VTCM_LAYOUT_ALLOC(off, off_s_col_tiles, bh * state_tiles_sz);
193 VTCM_LAYOUT_ALLOC(off, off_s_update_f32, bh * state_f32_sz);
194 off = hex_align_up(off, HMX_FP16_TILE_SIZE);
195 VTCM_LAYOUT_ALLOC(off, off_s_update_tiles, bh * state_tiles_sz);
196
197 VTCM_LAYOUT_ALLOC(off, off_q_f32[0], bh * dma_chunk_sz);
198 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_q_f32[1], bh * dma_chunk_sz, pipeline);
199 VTCM_LAYOUT_ALLOC(off, off_k_f32[0], bh * dma_chunk_sz);
200 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_k_f32[1], bh * dma_chunk_sz, pipeline);
201 VTCM_LAYOUT_ALLOC(off, off_v_f32[0], bh * dma_chunk_sz);
202 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_v_f32[1], bh * dma_chunk_sz, pipeline);
203 VTCM_LAYOUT_ALLOC(off, off_g_f32[0], bh * dma_scalar_sz);
204 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_g_f32[1], bh * dma_scalar_sz, pipeline);
205 VTCM_LAYOUT_ALLOC(off, off_b_f32[0], bh * dma_scalar_sz);
206 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_b_f32[1], bh * dma_scalar_sz, pipeline);
207 const size_t raw_gb_sz = hex_round_up((bh + 1) * chunk_size * sizeof(float), 128);
208 VTCM_LAYOUT_ALLOC(off, off_g_raw[0], raw_gb_sz);
209 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_g_raw[1], raw_gb_sz, pipeline);
210 VTCM_LAYOUT_ALLOC(off, off_b_raw[0], raw_gb_sz);
211 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_b_raw[1], raw_gb_sz, pipeline);
212 VTCM_LAYOUT_ALLOC(off, off_o_f32[0], bh * dma_chunk_sz);
213 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_o_f32[1], bh * dma_chunk_sz, pipeline);
214
215 VTCM_LAYOUT_ALLOC(off, off_v_inter_f32, bh * dma_chunk_sz);
216 VTCM_LAYOUT_ALLOC(off, off_o_inter_f32, bh * dma_chunk_sz);
217 VTCM_LAYOUT_ALLOC(off, off_o_intra_f32, bh * dma_chunk_sz);
218 VTCM_LAYOUT_ALLOC(off, off_k_f16, bh * act_f16_sz);
219 VTCM_LAYOUT_ALLOC(off, off_v_prime_f16, bh * act_f16_sz);
220 VTCM_LAYOUT_ALLOC(off, off_delta_f16, bh * act_f16_sz);
221 VTCM_LAYOUT_ALLOC(off, off_d_f16, bh * act_f16_sz);
222
223 off = hex_align_up(off, HMX_FP16_TILE_SIZE);
224 VTCM_LAYOUT_ALLOC(off, off_q_row_tiles, bh * tile_64xSv_sz);
225 VTCM_LAYOUT_ALLOC(off, off_q_prime_row_tiles, bh * tile_64xSv_sz);
226 VTCM_LAYOUT_ALLOC(off, off_k_row_tiles, bh * tile_64xSv_sz);
227 VTCM_LAYOUT_ALLOC(off, off_k_col_tiles, bh * tile_64xSv_sz);
228 VTCM_LAYOUT_ALLOC(off, off_k_prime_row_tiles, bh * tile_64xSv_sz);
229 VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128, bh * tile_64xSv_sz);
230 VTCM_LAYOUT_ALLOC(off, off_kk_tiles, bh * tile_64x64_sz);
231 VTCM_LAYOUT_ALLOC(off, off_qk_tiles, bh * tile_64x64_sz);
232 VTCM_LAYOUT_ALLOC(off, off_v_inter_tiles, bh * tile_64xSv_sz);
233 VTCM_LAYOUT_ALLOC(off, off_o_inter_tiles, bh * tile_64xSv_sz);
234 VTCM_LAYOUT_ALLOC(off, off_inv_row_tiles, bh * tile_64x64_sz);
235 VTCM_LAYOUT_ALLOC(off, off_a_row_tiles, bh * tile_64x64_sz);
236 VTCM_LAYOUT_ALLOC(off, off_v_prime_col_tiles, bh * tile_64xSv_sz);
237 VTCM_LAYOUT_ALLOC(off, off_delta_tiles, bh * tile_64xSv_sz);
238 VTCM_LAYOUT_ALLOC(off, off_delta_col_tiles, bh * tile_64xSv_sz);
239 VTCM_LAYOUT_ALLOC(off, off_o_intra_tiles, bh * tile_64xSv_sz);
240 VTCM_LAYOUT_ALLOC(off, off_d_row_tiles, bh * tile_64xSv_sz);
241
242 VTCM_LAYOUT_ALLOC(off, off_gamma, bh * hex_round_up(chunk_size * sizeof(float), 128));
243 VTCM_LAYOUT_ALLOC(off, off_lambda_init, bh * hex_round_up(chunk_size * sizeof(float), 128));
244 VTCM_LAYOUT_ALLOC(off, off_decay_m, bh * decay_sz);
245 VTCM_LAYOUT_ALLOC(off, off_decay_a, bh * decay_sz);
246 VTCM_LAYOUT_ALLOC(off, off_rows_kk, bh * row_vecs_sz);
247 VTCM_LAYOUT_ALLOC(off, off_rows_qk, bh * row_vecs_sz);
248 VTCM_LAYOUT_ALLOC(off, off_rows_inv, bh * row_vecs_sz);
249 VTCM_LAYOUT_ALLOC(off, off_rows_a, bh * row_vecs_sz);
250
251 const size_t thread_scratch_sz = 64 * 128;
252 off = hex_align_up(off, HMX_FP16_TILE_SIZE);
253 VTCM_LAYOUT_ALLOC(off, off_thread_scratch, nth * thread_scratch_sz);
254 off = hex_align_up(off, HMX_FP16_TILE_SIZE);
255 VTCM_LAYOUT_ALLOC(off, off_scales_1, HMX_FP16_TILE_SIZE);
256
257 L->total_bytes = off;
258}
259
260static inline bool htp_gdn_hmx_solve_layout(
261 struct htp_gdn_hmx_vtcm_layout * layout_out,
262 uint32_t S_v,
263 uint32_t chunk_size,
264 uint32_t total_rows,
265 size_t vtcm_budget,
266 uint32_t n_threads,
267 bool pipeline,
268 uint32_t * n_heads_batch_out
269) {
270 uint32_t max_batch = 8;
271 if (max_batch > total_rows) {
272 max_batch = total_rows;
273 }
274 if (max_batch > n_threads) {
275 max_batch = n_threads;
276 }
277 static const uint32_t candidates[] = { 8, 6, 4, 2, 1 };
278 for (size_t i = 0; i < sizeof(candidates) / sizeof(candidates[0]); ++i) {
279 uint32_t bh = candidates[i];
280 if (bh > max_batch) {
281 continue;
282 }
283 struct htp_gdn_hmx_vtcm_layout L;
284 htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, bh, n_threads, pipeline);
285 if (L.total_bytes <= vtcm_budget) {
286 *layout_out = L;
287 *n_heads_batch_out = bh;
288 return true;
289 }
290 }
291 if (pipeline) {
292 return htp_gdn_hmx_solve_layout(layout_out, S_v, chunk_size, total_rows, vtcm_budget, n_threads, false, n_heads_batch_out);
293 }
294 return false;
295}
296
297#endif // HTP_GATED_DELTA_NET_OPS_H