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-cuda / gated_delta_net.cu

393 lines · 16833 bytes Raw Blame History
1#include "gated_delta_net.cuh"
2#include "gated-delta-net-mma.cuh"
3#include "ggml-cuda/common.cuh"
4
5#include <cstdint>
6
7constexpr int gdn_cols_per_warp = 4;
8
9template <int S_v, bool KDA, bool keep_rs_t, int cols_per_warp = gdn_cols_per_warp>
10__global__ void __launch_bounds__((ggml_cuda_get_physical_warp_size() < S_v ? ggml_cuda_get_physical_warp_size() : S_v) * 4, 2)
11gated_delta_net_cuda(const float * q,
12 const float * k,
13 const float * v,
14 const float * g,
15 const float * beta,
16 const float * curr_state,
17 float * dst,
18 float * state,
19 int64_t H,
20 int64_t n_tokens,
21 int64_t n_seqs,
22 int64_t sq1,
23 int64_t sq2,
24 int64_t sq3,
25 int64_t sv1,
26 int64_t sv2,
27 int64_t sv3,
28 int64_t sb1,
29 int64_t sb2,
30 int64_t sb3,
31 const uint3 neqk1_magic,
32 const uint3 rq3_magic,
33 float scale,
34 int64_t state_slot_stride,
35 int K) {
36 const uint32_t h_idx = blockIdx.x;
37 const uint32_t sequence = blockIdx.y;
38
39 constexpr int warp_size = ggml_cuda_get_physical_warp_size() < S_v ? ggml_cuda_get_physical_warp_size() : S_v;
40 static_assert(S_v % warp_size == 0, "S_v must be a multiple of warp_size");
41 // the warp is split into cols_per_warp segments of lanes_per_col lanes; each segment owns
42 // one state column and reduces within itself
43 constexpr int lanes_per_col = warp_size / cols_per_warp;
44 constexpr int rows_per_lane = S_v / lanes_per_col;
45 static_assert(S_v % lanes_per_col == 0, "S_v must be a multiple of lanes_per_col");
46
47 const int lane = threadIdx.x;
48 const int col_in_warp = lane / lanes_per_col; // column slot within the warp
49 const int lane_in_col = lane - col_in_warp * lanes_per_col; // lane within the column's reduction segment
50 const int col = (blockIdx.z * blockDim.y + threadIdx.y) * cols_per_warp + col_in_warp;
51
52 const uint32_t iq1 = fastmodulo(h_idx, neqk1_magic);
53 const uint32_t iq3 = fastdiv(sequence, rq3_magic);
54
55 float * attn_data = dst;
56
57 // input state holds s0 only: [S_v, S_v, H, n_seqs] — seq stride is D = H * S_v * S_v.
58 // output state layout (per-slot D * n_seqs) — same per-(seq,head) offset as before.
59 const int64_t state_in_offset = sequence * H * S_v * S_v + h_idx * S_v * S_v;
60 const int64_t state_out_offset = (sequence * H + h_idx) * S_v * S_v;
61 state += state_out_offset;
62 curr_state += state_in_offset + col * S_v;
63 attn_data += (sequence * n_tokens * H + h_idx) * S_v;
64
65 float s_shard[rows_per_lane];
66 // state is stored transposed: M[col][i] = S[i][col], row col is contiguous
67
68 ggml_cuda_pdl_sync();
69#pragma unroll
70 for (int r = 0; r < rows_per_lane; r++) {
71 const int i = r * lanes_per_col + lane_in_col;
72 s_shard[r] = curr_state[i];
73 }
74
75 for (int t = 0; t < n_tokens; t++) {
76 const float * q_t = q + iq3 * sq3 + t * sq2 + iq1 * sq1;
77 const float * k_t = k + iq3 * sq3 + t * sq2 + iq1 * sq1;
78 const float * v_t = v + sequence * sv3 + t * sv2 + h_idx * sv1;
79
80 const int64_t gb_offset = sequence * sb3 + t * sb2 + h_idx * sb1;
81 const float * beta_t = beta + gb_offset;
82 const float * g_t = g + gb_offset * (KDA ? S_v : 1);
83
84 const float beta_val = *beta_t;
85
86 // Cache k and q in registers
87 float k_reg[rows_per_lane];
88 float q_reg[rows_per_lane];
89#pragma unroll
90 for (int r = 0; r < rows_per_lane; r++) {
91 const int i = r * lanes_per_col + lane_in_col;
92 k_reg[r] = k_t[i];
93 q_reg[r] = q_t[i];
94 }
95
96 if constexpr (!KDA) {
97 const float g_val = expf(*g_t);
98
99 // kv[col] = (S^T @ k)[col] = sum_i S[i][col] * k[i]
100 float kv_shard = 0.0f;
101#pragma unroll
102 for (int r = 0; r < rows_per_lane; r++) {
103 kv_shard += s_shard[r] * k_reg[r];
104 }
105 float kv_col = warp_reduce_sum<lanes_per_col>(kv_shard);
106
107 // delta[col] = (v[col] - g * kv[col]) * beta
108 float delta_col = (v_t[col] - g_val * kv_col) * beta_val;
109
110 // fused: S[i][col] = g * S[i][col] + k[i] * delta[col]
111 // attn[col] = (S^T @ q)[col] = sum_i S[i][col] * q[i]
112 float attn_partial = 0.0f;
113#pragma unroll
114 for (int r = 0; r < rows_per_lane; r++) {
115 s_shard[r] = g_val * s_shard[r] + k_reg[r] * delta_col;
116 attn_partial += s_shard[r] * q_reg[r];
117 }
118
119 float attn_col = warp_reduce_sum<lanes_per_col>(attn_partial);
120
121 if (lane_in_col == 0) {
122 attn_data[col] = attn_col * scale;
123 }
124 } else {
125 // kv[col] = sum_i g[i] * S[i][col] * k[i]
126 float kv_shard = 0.0f;
127#pragma unroll
128 for (int r = 0; r < rows_per_lane; r++) {
129 const int i = r * lanes_per_col + lane_in_col;
130 kv_shard += expf(g_t[i]) * s_shard[r] * k_reg[r];
131 }
132
133 float kv_col = warp_reduce_sum<lanes_per_col>(kv_shard);
134
135 // delta[col] = (v[col] - kv[col]) * beta
136 float delta_col = (v_t[col] - kv_col) * beta_val;
137
138 // fused: S[i][col] = g[i] * S[i][col] + k[i] * delta[col]
139 // attn[col] = (S^T @ q)[col] = sum_i S[i][col] * q[i]
140 float attn_partial = 0.0f;
141#pragma unroll
142 for (int r = 0; r < rows_per_lane; r++) {
143 const int i = r * lanes_per_col + lane_in_col;
144 s_shard[r] = expf(g_t[i]) * s_shard[r] + k_reg[r] * delta_col;
145 attn_partial += s_shard[r] * q_reg[r];
146 }
147
148 float attn_col = warp_reduce_sum<lanes_per_col>(attn_partial);
149
150 if (lane_in_col == 0) {
151 attn_data[col] = attn_col * scale;
152 }
153 }
154
155 attn_data += S_v * H;
156
157 if constexpr (keep_rs_t) {
158 // snapshot slot mapping: slot 0 = most recent state, slot s = s tokens back.
159 // When n_tokens < K only slots 0..n_tokens-1 are written; older slots are caller-owned.
160 const int target_slot = (int) n_tokens - 1 - t;
161 if (target_slot >= 0 && target_slot < K) {
162 float * curr_state = state + target_slot * state_slot_stride;
163#pragma unroll
164 for (int r = 0; r < rows_per_lane; r++) {
165 const int i = r * lanes_per_col + lane_in_col;
166 curr_state[col * S_v + i] = s_shard[r];
167 }
168 }
169 }
170 }
171
172 if constexpr (!keep_rs_t) {
173#pragma unroll
174 for (int r = 0; r < rows_per_lane; r++) {
175 const int i = r * lanes_per_col + lane_in_col;
176 state[col * S_v + i] = s_shard[r];
177 }
178 }
179}
180
181template <bool KDA, bool keep_rs_t>
182static void launch_gated_delta_net(
183 const float * q_d, const float * k_d, const float * v_d,
184 const float * g_d, const float * b_d, const float * s_d,
185 float * dst_d, float * state_d,
186 int64_t S_v, int64_t H, int64_t n_tokens, int64_t n_seqs,
187 int64_t sq1, int64_t sq2, int64_t sq3,
188 int64_t sv1, int64_t sv2, int64_t sv3,
189 int64_t sb1, int64_t sb2, int64_t sb3,
190 int64_t neqk1, int64_t rq3,
191 float scale, int64_t state_slot_stride, int K, cudaStream_t stream) {
192 //TODO: Add chunked kernel for even faster pre-fill
193 const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size;
194 // four columns per warp (see the kernel); shrink the CTA when the wider CTA would leave
195 // SMs without a CTA, so small head counts keep the device filled
196 const int nsm = ggml_cuda_info().devices[ggml_cuda_get_device()].nsm;
197 const int cols_per_warp = gdn_cols_per_warp;
198 int num_warps = 4;
199 while (num_warps > 1 && H*n_seqs*(S_v / (cols_per_warp * num_warps)) < nsm) {
200 num_warps /= 2;
201 }
202 // one CTA covers cols_per_warp*num_warps columns (see the kernel)
203 dim3 grid_dims(H, n_seqs, (S_v + cols_per_warp * num_warps - 1) / (cols_per_warp * num_warps));
204 dim3 block_dims(warp_size <= S_v ? warp_size : S_v, num_warps, 1);
205
206 const uint3 neqk1_magic = init_fastdiv_values(neqk1);
207 const uint3 rq3_magic = init_fastdiv_values(rq3);
208
209 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, stream);
210 switch (S_v) {
211 case 16:
212 ggml_cuda_kernel_launch(gated_delta_net_cuda<16, KDA, keep_rs_t>, launch_params,
213 q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d, H,
214 n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
215 sb1, sb2, sb3, neqk1_magic, rq3_magic, scale, state_slot_stride, K);
216 break;
217 case 32:
218 ggml_cuda_kernel_launch(gated_delta_net_cuda<32, KDA, keep_rs_t>, launch_params,
219 q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d, H,
220 n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
221 sb1, sb2, sb3, neqk1_magic, rq3_magic, scale, state_slot_stride, K);
222 break;
223 case 64: {
224 ggml_cuda_kernel_launch(gated_delta_net_cuda<64, KDA, keep_rs_t>, launch_params,
225 q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d, H,
226 n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
227 sb1, sb2, sb3, neqk1_magic, rq3_magic, scale, state_slot_stride, K);
228 break;
229 }
230 case 128: {
231 ggml_cuda_kernel_launch(gated_delta_net_cuda<128, KDA, keep_rs_t>, launch_params,
232 q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d, H,
233 n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
234 sb1, sb2, sb3, neqk1_magic, rq3_magic, scale, state_slot_stride, K);
235 break;
236 }
237 default:
238 GGML_ABORT("fatal error");
239 }
240}
241
242static ggml_cuda_gdn_mma_args ggml_cuda_gdn_mma_args_from_tensor(const ggml_tensor * dst) {
243 const ggml_tensor * q = dst->src[0];
244 const ggml_tensor * k = dst->src[1];
245 const ggml_tensor * v = dst->src[2];
246 const ggml_tensor * g = dst->src[3];
247 const ggml_tensor * beta = dst->src[4];
248 const ggml_tensor * state = dst->src[5];
249
250 const int64_t S_v = v->ne[0];
251 const int64_t H = v->ne[1];
252 return {
253 (const float *) q->data,
254 (const float *) k->data,
255 (const float *) v->data,
256 (const float *) g->data,
257 (const float *) beta->data,
258 (const float *) state->data,
259 (float *) dst->data,
260 nullptr,
261 H,
262 q->ne[1],
263 v->ne[2],
264 v->ne[3],
265 v->ne[3] / q->ne[3],
266 (int64_t) (q->nb[1] / sizeof(float)),
267 (int64_t) (q->nb[2] / sizeof(float)),
268 (int64_t) (q->nb[3] / sizeof(float)),
269 (int64_t) (v->nb[1] / sizeof(float)),
270 (int64_t) (v->nb[2] / sizeof(float)),
271 (int64_t) (v->nb[3] / sizeof(float)),
272 (int64_t) (beta->nb[1] / sizeof(float)),
273 (int64_t) (beta->nb[2] / sizeof(float)),
274 (int64_t) (beta->nb[3] / sizeof(float)),
275 1.0f / sqrtf((float) S_v),
276 g->ne[0] == 1 && ggml_get_op_params_i32(dst, 0) < v->ne[2] && S_v == 128 && q->ne[0] == 128,
277 };
278}
279
280static void ggml_cuda_op_gated_delta_net_impl(
281 ggml_backend_cuda_context & ctx, ggml_tensor * dst, const ggml_cuda_gated_delta_net_fused_cache * cache) {
282 ggml_tensor * src_q = dst->src[0];
283 ggml_tensor * src_k = dst->src[1];
284 ggml_tensor * src_v = dst->src[2];
285 ggml_tensor * src_g = dst->src[3];
286 ggml_tensor * src_beta = dst->src[4];
287 ggml_tensor * src_state = dst->src[5];
288
289 GGML_TENSOR_LOCALS(int64_t, neq, src_q, ne);
290 GGML_TENSOR_LOCALS(size_t , nbq, src_q, nb);
291 GGML_TENSOR_LOCALS(int64_t, nek, src_k, ne);
292 GGML_TENSOR_LOCALS(size_t , nbk, src_k, nb);
293 GGML_TENSOR_LOCALS(int64_t, nev, src_v, ne);
294 GGML_TENSOR_LOCALS(size_t, nbv, src_v, nb);
295 GGML_TENSOR_LOCALS(size_t, nbb, src_beta, nb);
296
297 const int64_t S_v = nev0;
298 const int64_t H = nev1;
299 const int64_t n_tokens = nev2;
300 const int64_t n_seqs = nev3;
301
302 const bool kda = (src_g->ne[0] == S_v);
303
304 GGML_ASSERT(neq1 == nek1);
305 const int64_t neqk1 = neq1;
306
307 GGML_ASSERT(neq3 > 0 && nev3 >= neq3 && nev3 % neq3 == 0);
308 GGML_ASSERT(nek3 == neq3);
309 const int64_t rq3 = nev3 / neq3;
310
311 const float * q_d = (const float *) src_q->data;
312 const float * k_d = (const float *) src_k->data;
313 const float * v_d = (const float *) src_v->data;
314 const float * g_d = (const float *) src_g->data;
315 const float * b_d = (const float *) src_beta->data;
316
317 const float * s_d = (const float *) src_state->data;
318 float * dst_d = (float *) dst->data;
319
320 GGML_ASSERT(ggml_is_contiguous_rows(src_q));
321 GGML_ASSERT(ggml_is_contiguous_rows(src_k));
322 GGML_ASSERT(ggml_is_contiguous_rows(src_v));
323 GGML_ASSERT(ggml_are_same_stride(src_q, src_k));
324 GGML_ASSERT(src_g->ne[0] == 1 || kda);
325 GGML_ASSERT(ggml_is_contiguous(src_g));
326 GGML_ASSERT(ggml_is_contiguous(src_beta));
327 GGML_ASSERT(ggml_is_contiguous(src_state));
328
329 // strides in floats (beta strides used for both g and beta offset computation)
330 const int64_t sq1 = nbq1 / sizeof(float);
331 const int64_t sq2 = nbq2 / sizeof(float);
332 const int64_t sq3 = nbq3 / sizeof(float);
333 const int64_t sv1 = nbv1 / sizeof(float);
334 const int64_t sv2 = nbv2 / sizeof(float);
335 const int64_t sv3 = nbv3 / sizeof(float);
336 const int64_t sb1 = nbb1 / sizeof(float);
337 const int64_t sb2 = nbb2 / sizeof(float);
338 const int64_t sb3 = nbb3 / sizeof(float);
339
340 const float scale = 1.0f / sqrtf((float) S_v);
341
342 cudaStream_t stream = ctx.stream();
343
344 // K (snapshot slot count) is an op param; state holds s0 only [S_v, S_v, H, n_seqs].
345 const int K = ggml_get_op_params_i32(dst, 0);
346 const bool keep_rs = K > 1;
347
348 // recurrent state -> gdn_out tail (after attention scores), or the cache when fusing
349 float * state_d = dst_d + S_v * H * n_tokens * n_seqs;
350 int64_t state_slot_stride = S_v * S_v * H * n_seqs;
351 if (cache != nullptr) {
352 state_d = cache->data;
353 state_slot_stride = cache->slot_stride;
354 }
355
356 {
357 ggml_cuda_gdn_mma_args args = ggml_cuda_gdn_mma_args_from_tensor(dst);
358 args.state_out = state_d;
359 if (ggml_cuda_gdn_mma_launch(ctx.device, args, stream)) {
360 return;
361 }
362 }
363 if (kda) {
364 if (keep_rs) {
365 launch_gated_delta_net<true, true>(q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d,
366 S_v, H, n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
367 sb1, sb2, sb3, neqk1, rq3, scale, state_slot_stride, K, stream);
368 } else {
369 launch_gated_delta_net<true, false>(q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d,
370 S_v, H, n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
371 sb1, sb2, sb3, neqk1, rq3, scale, state_slot_stride, K, stream);
372 }
373 } else {
374 if (keep_rs) {
375 launch_gated_delta_net<false, true>(q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d,
376 S_v, H, n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
377 sb1, sb2, sb3, neqk1, rq3, scale, state_slot_stride, K, stream);
378 } else {
379 launch_gated_delta_net<false, false>(q_d, k_d, v_d, g_d, b_d, s_d, dst_d, state_d,
380 S_v, H, n_tokens, n_seqs, sq1, sq2, sq3, sv1, sv2, sv3,
381 sb1, sb2, sb3, neqk1, rq3, scale, state_slot_stride, K, stream);
382 }
383 }
384}
385
386void ggml_cuda_op_gated_delta_net(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
387 ggml_cuda_op_gated_delta_net_impl(ctx, dst, nullptr);
388}
389
390void ggml_cuda_op_gated_delta_net_fused_cache(
391 ggml_backend_cuda_context & ctx, ggml_tensor * dst, ggml_cuda_gated_delta_net_fused_cache cache) {
392 ggml_cuda_op_gated_delta_net_impl(ctx, dst, &cache);
393}