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 / ssm-scan.cu

890 lines · 43360 bytes Raw Blame History
1#if !defined(GGML_USE_HIP) && (defined(GGML_USE_MUSA) || CUDART_VERSION >= 11070)
2#define USE_CUB
3#endif // !defined(GGML_USE_HIP) && (defined(GGML_USE_MUSA) || CUDART_VERSION >= 11070)
4
5#ifdef USE_CUB
6#include <cub/cub.cuh>
7using namespace cub;
8#endif // USE_CUB
9
10#include "ssm-scan.cuh"
11
12
13// Minimum number of tokens to use SSD (State Space Duality) matmul path instead of scan path.
14// For n_tok <= this threshold, the scan kernel is used (lower overhead for short sequences).
15#define SSM_SSD_MIN_TOKENS 128
16
17// prepare_dt kernel dimensions: one block per (head, seq), each block handles DT_MAX_ITEMS items.
18#define SSM_SSD_DT_BLOCK 256
19#define SSM_SSD_DT_MAX_ITEMS 32
20
21// Maximum tokens the SSD path supports, derived from the prepare_dt kernel block capacity.
22#define SSM_SSD_MAX_TOKENS (SSM_SSD_DT_BLOCK * SSM_SSD_DT_MAX_ITEMS)
23
24// Chunk size for chunked SSD. Caps matmul cost at O(chunk^2) per chunk.
25#define SSM_SSD_CHUNK_SIZE 256
26
27// We would like to keep pragma unroll for cases where L_template is not 0,
28// so we suppress the clang transformation warning.
29#ifdef __clang__
30#pragma clang diagnostic push
31#pragma clang diagnostic ignored "-Wpass-failed"
32#endif // __clang__
33template <size_t splitD, size_t N, size_t L_template>
34__global__ void __launch_bounds__(splitD, 1)
35 ssm_scan_f32(const float * src0_ptr, const float * src1_ptr, const float * src2_ptr,
36 const float * src3_ptr, const float * src4_ptr, const float * src5_ptr,
37 const int32_t * src6_ptr, float * dst_ptr,
38 const int src0_nb2, const int src0_nb3, const int src1_nb2, const int src1_nb3,
39 const int src2_nb1, const int src2_nb2, const int src3_nb1,
40 const int src4_nb2, const int src4_nb3, const int src5_nb2, const int src5_nb3,
41 const int64_t s_off, const int64_t d_inner, const int64_t L_param)
42{
43 const float * GGML_CUDA_RESTRICT src0 = src0_ptr;
44 const float * GGML_CUDA_RESTRICT src1 = src1_ptr;
45 const float * GGML_CUDA_RESTRICT src2 = src2_ptr;
46 const float * GGML_CUDA_RESTRICT src3 = src3_ptr;
47 const float * GGML_CUDA_RESTRICT src4 = src4_ptr;
48 const float * GGML_CUDA_RESTRICT src5 = src5_ptr;
49 const int32_t * GGML_CUDA_RESTRICT src6 = src6_ptr;
50 float * GGML_CUDA_RESTRICT dst = dst_ptr;
51 const size_t L = L_template == 0 ? L_param : L_template;
52 ggml_cuda_pdl_sync();
53 const float *s0_block = (const float *)((const char *)src0 + src6[blockIdx.x] * src0_nb3 + blockIdx.y * splitD * src0_nb2);
54 const float *x_block = (const float *)((const char *)src1 + (blockIdx.x * src1_nb3) + blockIdx.y * splitD * sizeof(float));
55 const float *dt_block = (const float *)((const char *)src2 + (blockIdx.x * src2_nb2) + blockIdx.y * splitD * sizeof(float));
56 const float *A_block = (const float *)((const char *)src3 + blockIdx.y * splitD * src3_nb1);
57 const float *B_block = (const float *)((const char *)src4 + (blockIdx.x * src4_nb3));
58 const float *C_block = (const float *)((const char *)src5 + (blockIdx.x * src5_nb3));
59 float *y_block = (float *)((char *)dst + (blockIdx.x * d_inner * L * sizeof(float)) + blockIdx.y * splitD * sizeof(float));
60 float *s_block = (float *)((char *)dst + s_off + blockIdx.x * src0_nb3 + blockIdx.y * splitD * src0_nb2);
61
62 const int stride_x = src1_nb2 / sizeof(float);
63 const int stride_dt = src2_nb1 / sizeof(float);
64 const int stride_B = src4_nb2 / sizeof(float);
65 const int stride_C = src5_nb2 / sizeof(float);
66 const int stride_y = d_inner;
67
68 float regA[N];
69 float regs0[N];
70
71 __shared__ float smemB[N];
72 __shared__ float smemC[N];
73
74#ifdef USE_CUB
75 using BlockLoad = cub::BlockLoad<float, splitD, N, cub::BLOCK_LOAD_WARP_TRANSPOSE>;
76 using BlockStore = cub::BlockStore<float, splitD, N, cub::BLOCK_STORE_WARP_TRANSPOSE>;
77
78 union CubTempStorage {
79 typename BlockLoad::TempStorage load_temp;
80 typename BlockStore::TempStorage store_temp;
81 };
82 __shared__ CubTempStorage cub_temp_storage;
83
84 BlockLoad(cub_temp_storage.load_temp).Load(A_block, regA);
85 __syncthreads();
86 BlockLoad(cub_temp_storage.load_temp).Load(s0_block, regs0);
87#else
88 const int stride_s0 = src0_nb2 / sizeof(float);
89 const int stride_A = src3_nb1 / sizeof(float);
90#pragma unroll
91 for (size_t n = 0; n < N; ++n)
92 {
93 regA[n] = A_block[threadIdx.x * stride_A + n];
94 regs0[n] = s0_block[threadIdx.x * stride_s0 + n];
95 }
96#endif
97
98#pragma unroll
99 for (size_t i = 0; i < L; i++)
100 {
101 if (threadIdx.x < N)
102 {
103 smemB[threadIdx.x] = B_block[i * stride_B + threadIdx.x];
104 smemC[threadIdx.x] = C_block[i * stride_C + threadIdx.x];
105 }
106 __syncthreads();
107
108 float dt_soft_plus = dt_block[i * stride_dt + threadIdx.x];
109 if (dt_soft_plus <= 20.0f)
110 {
111 dt_soft_plus = log1pf(expf(dt_soft_plus));
112 }
113 float x_dt = x_block[i * stride_x + threadIdx.x] * dt_soft_plus;
114
115 float sumf = 0.0f;
116#pragma unroll
117 for (size_t n = 0; n < N; n++)
118 {
119 float state = regs0[n] * expf(dt_soft_plus * regA[n]) + smemB[n] * x_dt;
120 sumf += state * smemC[n];
121 regs0[n] = state;
122 }
123 y_block[i * stride_y + threadIdx.x] = sumf;
124 __syncthreads();
125 }
126
127#ifdef USE_CUB
128 BlockStore(cub_temp_storage.store_temp).Store(s_block, regs0);
129#else
130 const int stride_s = stride_s0;
131#pragma unroll
132 for (size_t n = 0; n < N; ++n)
133 {
134 s_block[threadIdx.x * stride_s + n] = regs0[n];
135 }
136#endif
137}
138#ifdef __clang__
139#pragma clang diagnostic pop
140#endif // __clang__
141
142// assumes as many threads as d_state
143template <int c_factor, int d_state>
144__global__ void __launch_bounds__(d_state, 1)
145 ssm_scan_f32_group(
146 const float * src0_ptr, const float * src1_ptr, const float * src2_ptr,
147 const float * src3_ptr, const float * src4_ptr, const float * src5_ptr,
148 const int32_t * src6_ptr, float * dst_ptr,
149 const int src0_nb2, const int src0_nb3, const int src1_nb2, const int src1_nb3,
150 const int src2_nb1, const int src2_nb2, const int src3_nb1,
151 const int src4_nb2, const int src4_nb3, const int src5_nb2, const int src5_nb3,
152 char * s_base, const int64_t s_slot_bytes, const int64_t n_head, const int64_t d_head, const int64_t n_group, const int64_t n_tok, const int64_t K) {
153 const float * GGML_CUDA_RESTRICT src0 = src0_ptr;
154 const float * GGML_CUDA_RESTRICT src1 = src1_ptr;
155 const float * GGML_CUDA_RESTRICT src2 = src2_ptr;
156 const float * GGML_CUDA_RESTRICT src3 = src3_ptr;
157 const float * GGML_CUDA_RESTRICT src4 = src4_ptr;
158 const float * GGML_CUDA_RESTRICT src5 = src5_ptr;
159 const int32_t * GGML_CUDA_RESTRICT src6 = src6_ptr;
160 float * GGML_CUDA_RESTRICT dst = dst_ptr;
161
162 const int warp = threadIdx.x / WARP_SIZE;
163 const int lane = threadIdx.x % WARP_SIZE;
164 const int warp_idx = blockIdx.x * c_factor + warp;
165
166 ggml_cuda_pdl_sync();
167
168 // the last block can have unused warps when n_head*d_head is not a multiple of c_factor
169 if (warp_idx >= n_head * d_head) {
170 return;
171 }
172
173 const int head_idx = warp_idx / d_head;
174 const int head_off = (warp_idx % d_head) * sizeof(float);
175 const int seq_idx = blockIdx.y;
176
177 const int group_off = (head_idx / (n_head / n_group)) * d_state * sizeof(float);
178
179 // TODO: refactor strides to be in elements/floats instead of bytes to be cleaner and consistent with the rest of the codebase
180 const float * s0_warp = (const float *) ((const char *) src0 + src6[seq_idx] * src0_nb3 + head_idx * src0_nb2 + head_off * d_state);
181 const float * x_warp = (const float *) ((const char *) src1 + (seq_idx * src1_nb3) + (warp_idx * sizeof(float)));
182 const float * dt_warp = (const float *) ((const char *) src2 + (seq_idx * src2_nb2) + head_idx * sizeof(float));
183 const float * A_warp = (const float *) ((const char *) src3 + head_idx * src3_nb1);
184 const float * B_warp = (const float *) ((const char *) src4 + (seq_idx * src4_nb3) + (group_off));
185 const float * C_warp = (const float *) ((const char *) src5 + (seq_idx * src5_nb3) + (group_off));
186 float * y_warp = dst + (seq_idx * n_tok * n_head * d_head) + warp_idx;
187 float * s_warp = (float *) (s_base + seq_idx * src0_nb3 + head_idx * src0_nb2 + head_off * d_state);
188
189 // strides across n_seq_tokens
190 const int stride_x = src1_nb2 / sizeof(float);
191 const int stride_dt = src2_nb1 / sizeof(float);
192 const int stride_B = src4_nb2 / sizeof(float);
193 const int stride_C = src5_nb2 / sizeof(float);
194 const int stride_y = n_head * d_head;
195
196 float state[c_factor];
197 float state_sum = 0.0f;
198
199#pragma unroll
200 for (int j = 0; j < c_factor; j++) {
201 state[j] = s0_warp[WARP_SIZE * j + lane];
202 }
203
204 for (int64_t i = 0; i < n_tok; i++) {
205 // NOTE: dt_soft_plus, dA and x_dt have the same value for a warp here.
206 // Recalculation is intentional; sharing via shuffles/smem proved slower due to sync overhead.
207 const float dt_soft_plus = (dt_warp[i * stride_dt] <= 20.0f ? log1pf(expf(dt_warp[i * stride_dt])) : dt_warp[i * stride_dt]);
208
209 state_sum = 0.0f;
210 const float dA = expf(dt_soft_plus * A_warp[0]);
211 const float x_dt = x_warp[i * stride_x] * dt_soft_plus;
212#pragma unroll
213 for (int j = 0; j < c_factor; j++) {
214 const float B_val = B_warp[i * stride_B + WARP_SIZE * j + lane];
215 const float C_val = C_warp[i * stride_C + WARP_SIZE * j + lane];
216 state[j] = (state[j] * dA) + (B_val * x_dt);
217 state_sum += state[j] * C_val;
218 }
219
220 // parallel accumulation for output
221 state_sum = warp_reduce_sum(state_sum);
222
223 if (lane == 0) {
224 y_warp[i * stride_y] = state_sum;
225 }
226
227 // Slot 0 is the final state written below; slots 1..K-1 are rollback snapshots.
228 const int64_t slot = n_tok - 1 - i;
229 if (K > 1 && slot > 0 && slot < K) {
230 float * s_snapshot_warp = (float *) ((char *) s_warp + slot * s_slot_bytes);
231#pragma unroll
232 for (int j = 0; j < c_factor; j++) {
233 s_snapshot_warp[WARP_SIZE * j + lane] = state[j];
234 }
235 }
236 }
237
238 // write back the state
239#pragma unroll
240 for (int j = 0; j < c_factor; j++) {
241 s_warp[WARP_SIZE * j + lane] = state[j];
242 }
243}
244
245static void ssm_scan_f32_cuda(const float * src0, const float * src1, const float * src2, const float * src3,
246 const float * src4, const float * src5, const int32_t * src6, float * dst,
247 const int src0_nb2, const int src0_nb3, const int src1_nb2, const int src1_nb3, const int src2_nb1,
248 const int src2_nb2, const int src3_nb1, const int src4_nb2, const int src4_nb3, const int src5_nb2,
249 const int src5_nb3, const int64_t s_off, const int64_t d_state, const int64_t head_dim,
250 const int64_t n_head, const int64_t n_group, const int64_t n_tok, const int64_t n_seq,
251 const int64_t K, const ggml_cuda_ssm_scan_fused_cache * cache, cudaStream_t stream) {
252 // when fused, the states go straight into the recurrent cache and the dst tail is left alone
253 char * const s_base = cache ? (char *) cache->data : (char *) dst + s_off;
254 const int64_t s_slot_bytes = cache ? cache->slot_stride * (int64_t) sizeof(float) : n_seq * (int64_t) src0_nb3;
255
256 // NOTE: if you change conditions here, be sure to update the corresponding supports_op condition!
257 if (src3_nb1 == sizeof(float)) {
258 // Mamba-2
259 if (d_state == 96) {
260 constexpr int threads = 96;
261 constexpr int num_warps = threads/WARP_SIZE;
262
263 const dim3 blocks((n_head * head_dim + (num_warps - 1)) / num_warps, n_seq, 1);
264 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(blocks, threads, 0, stream);
265 ggml_cuda_kernel_launch(ssm_scan_f32_group<96/WARP_SIZE, 96>, launch_params,
266 src0, src1, src2, src3, src4, src5, src6, dst,
267 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2, src3_nb1,
268 src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_base, s_slot_bytes, n_head, head_dim, n_group, n_tok, K);
269 } else if (d_state == 128) {
270 constexpr int threads = 128;
271 constexpr int num_warps = threads/WARP_SIZE;
272
273 const dim3 blocks((n_head * head_dim + (num_warps - 1)) / num_warps, n_seq, 1);
274 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(blocks, threads, 0, stream);
275 ggml_cuda_kernel_launch(ssm_scan_f32_group<128/WARP_SIZE, 128>, launch_params,
276 src0, src1, src2, src3, src4, src5, src6, dst,
277 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2, src3_nb1,
278 src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_base, s_slot_bytes, n_head, head_dim, n_group, n_tok, K);
279 } else if (d_state == 256) { // Falcon-H1
280 constexpr int threads = 256;
281 constexpr int num_warps = threads/WARP_SIZE;
282
283 const dim3 blocks((n_head * head_dim + (num_warps - 1)) / num_warps, n_seq, 1);
284 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(blocks, threads, 0, stream);
285 ggml_cuda_kernel_launch(ssm_scan_f32_group<256/WARP_SIZE, 256>, launch_params,
286 src0, src1, src2, src3, src4, src5, src6, dst,
287 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2, src3_nb1,
288 src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_base, s_slot_bytes, n_head, head_dim, n_group, n_tok, K);
289 } else {
290 GGML_ABORT("doesn't support d_state!=(96, 128 or 256).");
291 }
292 } else {
293 // Mamba-1
294 GGML_ASSERT(K == 1);
295 constexpr int threads = 128;
296 GGML_ASSERT(n_head % threads == 0);
297 GGML_ASSERT(head_dim == 1);
298 GGML_ASSERT(n_group == 1);
299 const dim3 blocks(n_seq, (n_head + threads - 1) / threads, 1);
300 if (d_state == 16) {
301 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(blocks, threads, 0, stream);
302 switch (n_tok)
303 {
304 case 1:
305 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 1>, launch_params,
306 src0, src1, src2, src3, src4, src5, src6, dst,
307 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
308 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
309 break;
310 case 2:
311 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 2>, launch_params,
312 src0, src1, src2, src3, src4, src5, src6, dst,
313 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
314 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
315 break;
316 case 3:
317 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 3>, launch_params,
318 src0, src1, src2, src3, src4, src5, src6, dst,
319 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
320 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
321 break;
322 case 4:
323 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 4>, launch_params,
324 src0, src1, src2, src3, src4, src5, src6, dst,
325 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
326 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
327 break;
328 case 5:
329 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 5>, launch_params,
330 src0, src1, src2, src3, src4, src5, src6, dst,
331 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
332 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
333 break;
334 case 6:
335 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 6>, launch_params,
336 src0, src1, src2, src3, src4, src5, src6, dst,
337 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
338 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
339 break;
340 case 7:
341 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 7>, launch_params,
342 src0, src1, src2, src3, src4, src5, src6, dst,
343 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
344 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
345 break;
346 case 8:
347 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 8>, launch_params,
348 src0, src1, src2, src3, src4, src5, src6, dst,
349 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
350 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
351 break;
352 default:
353 ggml_cuda_kernel_launch(ssm_scan_f32<threads, 16, 0>, launch_params,
354 src0, src1, src2, src3, src4, src5, src6, dst,
355 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2,
356 src3_nb1, src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, n_tok);
357 break;
358 }
359 } else {
360 GGML_ABORT("doesn't support d_state!=16.");
361 }
362 }
363}
364
365#if !defined(GGML_USE_HIP)
366// ============================================================================
367// SSD (State Space Duality) kernels for Mamba-2 prefill (n_tok > SSM_SSD_MIN_TOKENS)
368//
369// Instead of a sequential scan, SSD reformulates the output as:
370// Y = (L (.) (C @ B^T)) @ (X * dt) + decay * C @ s_init
371// where L is a causal decay mask derived from A and dt.
372//
373// This converts the O(T*N) sequential scan into parallel matmuls.
374// ============================================================================
375// Softplus(dt) and inclusive prefix sum per head using CUB BlockScan.
376// Grid: (n_head, n_seqs)
377template <int BLOCK_SIZE, int MAX_ITEMS>
378__global__ void ssm_ssd_prepare_dt_kernel(
379 const float * __restrict__ dt_raw,
380 float * __restrict__ dt_sp_out,
381 float * __restrict__ cs_out,
382 const int n_head, const int n_tok,
383 const int dt_stride_tok, // elements between tokens in dt
384 const int dt_stride_seq) { // elements between sequences in dt
385
386 const int h = blockIdx.x;
387 const int s = blockIdx.y;
388
389 const float * dt_seq = dt_raw + s * dt_stride_seq;
390
391 float * dt_sp_seq = dt_sp_out + s * n_tok * n_head;
392 float * cs_seq = cs_out + s * n_tok * n_head;
393
394 const int items_per_thread = (n_tok + BLOCK_SIZE - 1) / BLOCK_SIZE;
395
396 // Phase 1: softplus with interleaved distribution (t = i*BLOCK_SIZE + threadIdx.x).
397 // Each warp reads BLOCK_SIZE consecutive tokens, giving coalesced dt_raw loads
398 // (stride n_head between threads vs. items_per_thread*n_head in blocked layout).
399 float local_vals[MAX_ITEMS];
400 for (int i = 0; i < items_per_thread; i++) {
401 const int t = i * BLOCK_SIZE + threadIdx.x;
402 if (t < n_tok) {
403 float val = dt_seq[h + t * dt_stride_tok];
404 float sp = (val <= 20.0f) ? log1pf(expf(val)) : val;
405 local_vals[i] = sp;
406 dt_sp_seq[t * n_head + h] = sp;
407 } else {
408 local_vals[i] = 0.0f;
409 }
410 }
411
412 // Phase 2+3: per-step inclusive scan to build cs[] in token order.
413 // With interleaved distribution the per-thread total scan would not give token-order
414 // prefix sums, so we scan one BLOCK_SIZE slab at a time and carry a running total.
415#ifdef USE_CUB
416 using BlockScan = cub::BlockScan<float, BLOCK_SIZE>;
417 __shared__ typename BlockScan::TempStorage scan_temp;
418 __shared__ float step_total;
419
420 float running = 0.0f;
421 for (int i = 0; i < items_per_thread; i++) {
422 float inclusive;
423 BlockScan(scan_temp).InclusiveSum(local_vals[i], inclusive);
424 const int t = i * BLOCK_SIZE + threadIdx.x;
425 if (t < n_tok) {
426 cs_seq[t * n_head + h] = running + inclusive;
427 }
428 if (threadIdx.x == BLOCK_SIZE - 1) {
429 step_total = inclusive;
430 }
431 __syncthreads();
432 running += step_total;
433 }
434#else
435 // Fallback: sequential prefix scan in shared memory, one slab at a time.
436 __shared__ float sdata[BLOCK_SIZE];
437 float running = 0.0f;
438 for (int i = 0; i < items_per_thread; i++) {
439 const int t = i * BLOCK_SIZE + threadIdx.x;
440 sdata[threadIdx.x] = local_vals[i];
441 __syncthreads();
442 if (threadIdx.x == 0) {
443 for (int j = 1; j < BLOCK_SIZE; j++) {
444 sdata[j] += sdata[j - 1];
445 }
446 }
447 __syncthreads();
448 if (t < n_tok) {
449 cs_seq[t * n_head + h] = running + sdata[threadIdx.x];
450 }
451 running += sdata[BLOCK_SIZE - 1];
452 __syncthreads();
453 }
454#endif
455}
456
457// Prepare SSD matmul inputs for one chunk: X_dt, B_weighted, C_scaled.
458// T_matmul controls precision for X_dt, B_weighted (float or half).
459// C_scaled is always float (pairs with float s_cur in step 3c).
460// Computation is always FP32; only the final store converts to T_matmul.
461// Also materializes the causal M matrix = exp(A*(cs_out - cs_in)) * CB (fused with prep to save a launch).
462// Grid: (ceil(max(C*head_dim, d_state*C, chunk_len^2) / BLOCK), n_head, n_seqs)
463template <int BLOCK_SIZE, typename T_matmul>
464__global__ void ssm_ssd_pre_matmul_kernel(
465 const float * __restrict__ cs, // {n_tok, n_head} cumulative dt sums
466 const float * __restrict__ dt_sp, // {n_tok, n_head} softplus(dt)
467 const float * __restrict__ A, // {1, n_head}
468 const float * __restrict__ x, // {head_dim, n_head, n_tok, n_seqs}
469 const float * __restrict__ B, // {d_state, n_group, n_tok, n_seqs}
470 const float * __restrict__ C_src, // {d_state, n_group, n_tok, n_seqs}
471 T_matmul * __restrict__ X_dt, // {head_dim, C, n_head} x * dt, d-fastest
472 T_matmul * __restrict__ B_weighted, // {d_state, C, n_head} B * decay_from_end
473 float * __restrict__ C_scaled, // {d_state, C, n_head} C * decay_to_pos (always float)
474 const float * __restrict__ CB, // {chunk_len, chunk_len, n_group, n_seqs}
475 half * __restrict__ M_out, // {chunk_len, chunk_len, n_head, n_seqs}
476 const int chunk_len, const int head_dim, const int n_head, const int n_group,
477 const int d_state, const int A_stride,
478 const int x_stride_tok, const int x_stride_seq,
479 const int B_stride_tok, const int B_stride_seq,
480 const int C_stride_tok, const int C_stride_seq,
481 const int chunk_offset,
482 const int n_tok_total) {
483
484 const int h = blockIdx.y;
485 const int s = blockIdx.z;
486 const int g = h / (n_head / n_group);
487
488 const float A_h = A[h * A_stride];
489 const int idx = blockIdx.x * BLOCK_SIZE + threadIdx.x;
490
491 const int cs_seq_off = s * n_tok_total * n_head;
492 const float cs_base = (chunk_offset > 0) ? cs[cs_seq_off + (chunk_offset - 1) * n_head + h] : 0.0f;
493 const float cs_last = cs[cs_seq_off + (chunk_offset + chunk_len - 1) * n_head + h] - cs_base;
494
495 // Prepare X_dt = x * dt, stored d-fastest for coalesced reads and writes.
496 const int n_xdt = chunk_len * head_dim;
497 if (idx < n_xdt) {
498 const int d = idx % head_dim;
499 const int t = idx / head_dim;
500
501 const float x_val = x[s * x_stride_seq + (chunk_offset + t) * x_stride_tok + d + h * head_dim];
502 const float dt_val = dt_sp[cs_seq_off + (chunk_offset + t) * n_head + h];
503
504 X_dt[d + t * head_dim + h * n_xdt + s * n_xdt * n_head] = (T_matmul)(x_val * dt_val);
505 }
506
507 // Prepare B_weighted and C_scaled together: both share the same index space (d_state * chunk_len)
508 // and the same cs_t load, so merging halves the cs[] global memory traffic.
509 const int n_bw = d_state * chunk_len;
510 if (idx < n_bw) {
511 const int n = idx % d_state;
512 const int t = idx / d_state;
513
514 const float cs_t = cs[cs_seq_off + (chunk_offset + t) * n_head + h] - cs_base;
515
516 const float B_val = B[s * B_stride_seq + (chunk_offset + t) * B_stride_tok + g * d_state + n];
517 B_weighted[n + t * d_state + h * n_bw + s * n_bw * n_head] = (T_matmul)(B_val * __expf(A_h * (cs_last - cs_t)));
518
519 const float C_val = C_src[s * C_stride_seq + (chunk_offset + t) * C_stride_tok + g * d_state + n];
520 C_scaled[n + t * d_state + h * n_bw + s * n_bw * n_head] = C_val * __expf(A_h * cs_t);
521 }
522
523 // Materialize M = exp(A*(cs_out - cs_in)) * CB with causal mask.
524 const int n_M = chunk_len * chunk_len;
525 if (idx < n_M) {
526 const int t_out = idx % chunk_len;
527 const int t_in = idx / chunk_len;
528
529 half val;
530 if (t_in <= t_out) {
531 const float cs_out = cs[cs_seq_off + (chunk_offset + t_out) * n_head + h] - cs_base;
532 const float cs_in = cs[cs_seq_off + (chunk_offset + t_in) * n_head + h] - cs_base;
533 const float decay = __expf(A_h * (cs_out - cs_in));
534 const float * CB_g = CB + (int64_t)s * chunk_len * chunk_len * n_group
535 + (int64_t)g * chunk_len * chunk_len;
536 const float cb_val = CB_g[t_out + t_in * chunk_len];
537 val = __float2half(decay * cb_val);
538 } else {
539 val = __float2half(0.0f);
540 }
541
542 M_out[(int64_t)s * n_M * n_head + (int64_t)h * n_M + t_in * chunk_len + t_out] = val;
543 }
544}
545
546// Scale running state in-place: s_cur *= decay_total(chunk).
547// Called BEFORE cuBLAS state update (beta=1) to fuse inter-chunk decay.
548// Eliminates the s_old buffer and D2D memcpy vs the old approach of:
549// memcpy(s_old, s_cur) -> cuBLAS(beta=0) -> s_cur += decay * s_old
550// Grid: (ceil(d_state * head_dim / BLOCK), n_head, n_seqs)
551template <int BLOCK_SIZE>
552__global__ void ssm_ssd_scale_state_kernel(
553 float * __restrict__ s_cur, // {d_state, head_dim, n_head, n_seqs}
554 const float * __restrict__ cs, // {n_tok, n_head} cumulative dt sums
555 const float * __restrict__ A, // {1, n_head}
556 const int d_state, const int head_dim, const int n_head,
557 const int chunk_offset, const int chunk_len,
558 const int n_tok_total, const int A_stride) {
559
560 const int h = blockIdx.y;
561 const int s = blockIdx.z;
562 const int idx = blockIdx.x * BLOCK_SIZE + threadIdx.x;
563 const int state_per_head = d_state * head_dim;
564 if (idx >= state_per_head) return;
565
566 const float A_h = A[h * A_stride];
567 const int cs_seq_off = s * n_tok_total * n_head;
568 const float cs_base = (chunk_offset > 0) ? cs[cs_seq_off + (chunk_offset - 1) * n_head + h] : 0.0f;
569 const float cs_last = cs[cs_seq_off + (chunk_offset + chunk_len - 1) * n_head + h] - cs_base;
570 const float decay_total = __expf(A_h * cs_last);
571
572 const int off = s * state_per_head * n_head + h * state_per_head + idx;
573 s_cur[off] *= decay_total;
574}
575
576// Copy initial state from src0[ids[s]] into s_cur for each sequence.
577// src0 and s_cur can alias when the state is written straight into the cache.
578// Grid: (ceil(d_state * head_dim * n_head / BLOCK), n_seqs)
579template <int BLOCK_SIZE>
580__global__ void ssm_ssd_init_state_kernel(
581 const float * src0, // {d_state, head_dim, n_head, n_rs}
582 const int32_t * __restrict__ ids, // {n_seqs}
583 float * s_cur, // {d_state, head_dim, n_head, n_seqs}
584 const int state_size, // d_state * head_dim * n_head
585 const int64_t s0_stride_seq) { // elements between state rows
586 const int s = blockIdx.y;
587 const int idx = blockIdx.x * BLOCK_SIZE + threadIdx.x;
588 if (idx >= state_size) return;
589
590 const float * s_src = src0 + (int64_t)ids[s] * s0_stride_seq;
591 s_cur[s * state_size + idx] = s_src[idx];
592}
593
594// SSD (State Space Duality) dispatch for Mamba-2 prefill.
595// Chunked matmuls: CB, materialize M + cuBLAS Y, S@C, B@X_dt.
596// All strides are in elements (floats), not bytes.
597static void ssm_scan_ssd_f32_cuda(
598 ggml_backend_cuda_context & ctx,
599 const float * src0_d, const float * src1_d, const float * src2_d, const float * src3_d,
600 const float * src4_d, const float * src5_d, const int32_t * src6_d, float * dst_d,
601 const int64_t s0_stride_seq, // state (src0) stride between seqs
602 const int x_stride_tok, const int x_stride_seq, // x (src1) strides
603 const int dt_stride_tok, const int dt_stride_seq, // dt (src2) strides
604 const int A_stride, // A (src3) stride between heads
605 const int B_stride_tok, const int B_stride_seq, // B (src4) strides
606 const int C_stride_tok, const int C_stride_seq, // C (src5) strides
607 float * s_cur, // state: dst state tail, or the cache when fused
608 const int64_t d_state, const int64_t head_dim,
609 const int64_t n_head, const int64_t n_group, const int64_t n_tok, const int64_t n_seq) {
610
611 cudaStream_t stream = ctx.stream();
612 const int64_t d_inner = head_dim * n_head;
613
614 const int64_t chunk_size = SSM_SSD_CHUNK_SIZE;
615 const int64_t n_chunks = (n_tok + chunk_size - 1) / chunk_size;
616
617 const int64_t state_per_head = d_state * head_dim;
618
619 using matmul_t = half;
620 static constexpr cudaDataType_t matmul_dtype = CUDA_R_16F;
621
622 ggml_cuda_pool_alloc<float> dt_sp_buf(ctx.pool(), n_tok * n_head * n_seq);
623 ggml_cuda_pool_alloc<float> cs_buf(ctx.pool(), n_tok * n_head * n_seq);
624 ggml_cuda_pool_alloc<float> CB_buf(ctx.pool(), chunk_size * chunk_size * n_group * n_seq);
625 ggml_cuda_pool_alloc<matmul_t> X_dt_buf(ctx.pool(), chunk_size * head_dim * n_head * n_seq);
626 ggml_cuda_pool_alloc<matmul_t> B_w_buf(ctx.pool(), d_state * chunk_size * n_head * n_seq);
627 ggml_cuda_pool_alloc<float> C_s_buf(ctx.pool(), d_state * chunk_size * n_head * n_seq);
628 float * dt_sp = dt_sp_buf.get();
629 float * cs = cs_buf.get();
630 float * CB = CB_buf.get();
631 matmul_t * X_dt = X_dt_buf.get();
632 matmul_t * B_weighted = B_w_buf.get();
633 float * C_scaled = C_s_buf.get();
634
635 // Step 1: softplus(dt) and parallel prefix sum over full sequence
636 {
637 dim3 grid(n_head, n_seq);
638 ssm_ssd_prepare_dt_kernel<SSM_SSD_DT_BLOCK, SSM_SSD_DT_MAX_ITEMS><<<grid, SSM_SSD_DT_BLOCK, 0, stream>>>(
639 src2_d, dt_sp, cs, n_head, n_tok, dt_stride_tok, dt_stride_seq);
640 CUDA_CHECK(cudaGetLastError());
641 }
642
643 // Step 2: initialize running state from src0[ids[s]]
644 {
645 constexpr int BLOCK = 256;
646 const int64_t state_size = d_state * head_dim * n_head;
647 dim3 grid((state_size + BLOCK - 1) / BLOCK, n_seq);
648 ssm_ssd_init_state_kernel<BLOCK><<<grid, BLOCK, 0, stream>>>(
649 src0_d, src6_d, s_cur, state_size, s0_stride_seq);
650 CUDA_CHECK(cudaGetLastError());
651 }
652
653 // Step 3: chunked SSD loop
654 // Per chunk: pre_matmul (incl. M) + 4 cuBLAS (CB, Y, S@C, state update) + scale_state
655 cublasHandle_t handle = ctx.cublas_handle();
656 const float alpha_one = 1.0f;
657 const float beta_zero = 0.0f;
658 const float beta_one = 1.0f;
659 const int lda_C_src = C_stride_tok; // leading dim for C in CB = C^T @ B
660 const int ldb_B_src = B_stride_tok; // leading dim for B in CB = C^T @ B
661
662 // Scratch buffer for causal M matrix, reused across chunks (max size at chunk_size)
663 const int64_t n_M_max = chunk_size * chunk_size;
664 ggml_cuda_pool_alloc<half> M_buf(ctx.pool(), n_M_max * n_head * n_seq);
665 half * M_mat = M_buf.get();
666
667 for (int64_t k = 0; k < n_chunks; k++) {
668 const int64_t chunk_offset = k * chunk_size;
669 const int64_t chunk_len = (chunk_offset + chunk_size <= n_tok) ? chunk_size : (n_tok - chunk_offset);
670
671 // 3a: CB = C^T @ B per group
672 for (int64_t s = 0; s < n_seq; s++) {
673 const float * C_s = src5_d + s * C_stride_seq + chunk_offset * C_stride_tok;
674 const float * B_s = src4_d + s * B_stride_seq + chunk_offset * B_stride_tok;
675 float * CB_s = CB + s * chunk_len * chunk_len * n_group;
676
677 if (n_group == 1) {
678 CUBLAS_CHECK(cublasSgemm(handle, CUBLAS_OP_T, CUBLAS_OP_N,
679 chunk_len, chunk_len, d_state,
680 &alpha_one, C_s, lda_C_src, B_s, ldb_B_src,
681 &beta_zero, CB_s, (int)chunk_len));
682 } else {
683 CUBLAS_CHECK(cublasGemmStridedBatchedEx(handle, CUBLAS_OP_T, CUBLAS_OP_N,
684 chunk_len, chunk_len, d_state,
685 &alpha_one,
686 C_s, CUDA_R_32F, lda_C_src, d_state,
687 B_s, CUDA_R_32F, ldb_B_src, d_state,
688 &beta_zero,
689 CB_s, CUDA_R_32F, (int)chunk_len, (long long)(chunk_len * chunk_len),
690 n_group,
691 CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT));
692 }
693 }
694
695 // 3b: prepare X_dt, B_weighted, C_scaled + materialize causal M matrix
696 const int64_t n_M = chunk_len * chunk_len;
697 {
698 constexpr int BLOCK = 256;
699 const int64_t n_xdt = chunk_len * head_dim;
700 const int64_t n_bw = d_state * chunk_len;
701 int64_t max_work = n_xdt;
702 if (n_bw > max_work) max_work = n_bw;
703 if (n_M > max_work) max_work = n_M;
704 dim3 grid((max_work + BLOCK - 1) / BLOCK, n_head, n_seq);
705 ssm_ssd_pre_matmul_kernel<BLOCK, matmul_t><<<grid, BLOCK, 0, stream>>>(
706 cs, dt_sp, src3_d, src1_d, src4_d, src5_d,
707 X_dt, B_weighted, C_scaled,
708 CB, M_mat,
709 chunk_len, head_dim, n_head, n_group, d_state, A_stride,
710 x_stride_tok, x_stride_seq, B_stride_tok, B_stride_seq, C_stride_tok, C_stride_seq,
711 chunk_offset, n_tok);
712 CUDA_CHECK(cudaGetLastError());
713 }
714
715 // 3c: dst = S_cur^T @ C_scaled (state contribution)
716 {
717 const int64_t stride_S = state_per_head;
718 const int64_t stride_Cs = d_state * chunk_len;
719
720 for (int64_t s = 0; s < n_seq; s++) {
721 float * dst_chunk = dst_d + s * d_inner * n_tok + chunk_offset * d_inner;
722
723 CUBLAS_CHECK(cublasGemmStridedBatchedEx(handle, CUBLAS_OP_T, CUBLAS_OP_N,
724 head_dim, chunk_len, d_state,
725 &alpha_one,
726 s_cur + s * stride_S * n_head, CUDA_R_32F, d_state, stride_S,
727 C_scaled + s * stride_Cs * n_head, CUDA_R_32F, d_state, stride_Cs,
728 &beta_zero,
729 dst_chunk, CUDA_R_32F, d_inner, head_dim,
730 n_head,
731 CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT));
732 }
733 }
734
735 // 3d: dst += X_dt @ M^T (intra-chunk contribution, adds to 3c result)
736 // M is stored as M[t_out, t_in] (lower-triangular), transpose needed for Y = X @ M^T.
737 {
738 const int64_t stride_M = n_M;
739 const int64_t stride_X_h = (int64_t)chunk_len * head_dim;
740
741 for (int64_t s = 0; s < n_seq; s++) {
742 float * dst_chunk = dst_d + s * d_inner * n_tok + chunk_offset * d_inner;
743 CUBLAS_CHECK(cublasGemmStridedBatchedEx(handle, CUBLAS_OP_N, CUBLAS_OP_T,
744 head_dim, chunk_len, chunk_len,
745 &alpha_one,
746 X_dt + s * stride_X_h * n_head, matmul_dtype, head_dim, stride_X_h,
747 M_mat + s * stride_M * n_head, matmul_dtype, chunk_len, stride_M,
748 &beta_one,
749 dst_chunk, CUDA_R_32F, d_inner, head_dim,
750 n_head,
751 CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT));
752 }
753 }
754
755 // 3e: s_cur = B_weighted @ X_dt^T + decay_total * s_cur_old (state update)
756 {
757 // Scale s_cur in-place by per-head decay_total BEFORE cuBLAS overwrites it
758 constexpr int BLOCK = 256;
759 dim3 grid((state_per_head + BLOCK - 1) / BLOCK, n_head, n_seq);
760 ssm_ssd_scale_state_kernel<BLOCK><<<grid, BLOCK, 0, stream>>>(
761 s_cur, cs, src3_d,
762 d_state, head_dim, n_head,
763 chunk_offset, chunk_len, n_tok, A_stride);
764 CUDA_CHECK(cudaGetLastError());
765
766 // cuBLAS with beta=1: s_cur = B_weighted @ X_dt^T + 1.0 * s_cur (already scaled)
767 const int64_t stride_Bw = d_state * chunk_len;
768 const int64_t stride_X = chunk_len * head_dim;
769 const int64_t stride_S = state_per_head;
770
771 for (int64_t s = 0; s < n_seq; s++) {
772 // X_dt is d-fastest {hd, C}, read as OP_T to get {C, hd}
773 CUBLAS_CHECK(cublasGemmStridedBatchedEx(handle, CUBLAS_OP_N, CUBLAS_OP_T,
774 d_state, head_dim, chunk_len,
775 &alpha_one,
776 B_weighted + s * stride_Bw * n_head, matmul_dtype, d_state, stride_Bw,
777 X_dt + s * stride_X * n_head, matmul_dtype, head_dim, stride_X,
778 &beta_one,
779 s_cur + s * stride_S * n_head, CUDA_R_32F, d_state, stride_S,
780 n_head,
781 CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT));
782 }
783 }
784 }
785}
786#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
787
788static void ggml_cuda_op_ssm_scan_impl(ggml_backend_cuda_context & ctx, ggml_tensor * dst,
789 const ggml_cuda_ssm_scan_fused_cache * cache) {
790 const struct ggml_tensor * src0 = dst->src[0]; // s
791 const struct ggml_tensor * src1 = dst->src[1]; // x
792 const struct ggml_tensor * src2 = dst->src[2]; // dt
793 const struct ggml_tensor * src3 = dst->src[3]; // A
794 const struct ggml_tensor * src4 = dst->src[4]; // B
795 const struct ggml_tensor * src5 = dst->src[5]; // C
796 const struct ggml_tensor * src6 = dst->src[6]; // ids
797
798 const int64_t nc = src0->ne[0]; // d_state
799 const int64_t nr = src0->ne[1]; // head_dim or 1
800 const int64_t nh = src1->ne[1]; // n_head
801 const int64_t ng = src4->ne[1]; // n_group
802 const int64_t n_t = src1->ne[2]; // number of tokens per sequence
803 const int64_t n_s = src1->ne[3]; // number of sequences in the batch
804 const int32_t K_param = ggml_get_op_params_i32(dst, 0);
805 const int64_t K = K_param > 0 ? K_param : 1;
806
807 const int64_t s_off = ggml_nelements(src1) * sizeof(float);
808
809 GGML_ASSERT(ggml_nelements(src1) + K*nc*nr*nh*n_s == ggml_nelements(dst));
810 GGML_ASSERT(src0->nb[0] == sizeof(float));
811 GGML_ASSERT(src1->nb[0] == sizeof(float));
812 GGML_ASSERT(src2->nb[0] == sizeof(float));
813 GGML_ASSERT(src3->nb[0] == sizeof(float));
814 GGML_ASSERT(src4->nb[0] == sizeof(float));
815 GGML_ASSERT(src5->nb[0] == sizeof(float));
816 GGML_ASSERT(src6->nb[0] == sizeof(int32_t));
817 GGML_ASSERT(src3->ne[0] == 1 || K == 1);
818
819 const float * src0_d = (const float *) src0->data;
820 const float * src1_d = (const float *) src1->data;
821 const float * src2_d = (const float *) src2->data;
822 const float * src3_d = (const float *) src3->data;
823 const float * src4_d = (const float *) src4->data;
824 const float * src5_d = (const float *) src5->data;
825 const int32_t * src6_d = (const int32_t *) src6->data;
826 float * dst_d = (float *) dst->data;
827 cudaStream_t stream = ctx.stream();
828
829 GGML_ASSERT(src0->type == GGML_TYPE_F32);
830 GGML_ASSERT(src6->type == GGML_TYPE_I32);
831 GGML_ASSERT(dst->type == GGML_TYPE_F32);
832
833 // Byte strides are narrowed to int for both scan and SSD paths.
834 GGML_ASSERT(src0->nb[2] <= (size_t)INT_MAX);
835 GGML_ASSERT(src0->nb[3] <= (size_t)INT_MAX);
836 GGML_ASSERT(src1->nb[2] <= (size_t)INT_MAX);
837 GGML_ASSERT(src1->nb[3] <= (size_t)INT_MAX);
838 GGML_ASSERT(src2->nb[1] <= (size_t)INT_MAX);
839 GGML_ASSERT(src2->nb[2] <= (size_t)INT_MAX);
840 GGML_ASSERT(src3->nb[1] <= (size_t)INT_MAX);
841 GGML_ASSERT(src4->nb[2] <= (size_t)INT_MAX);
842 GGML_ASSERT(src4->nb[3] <= (size_t)INT_MAX);
843 GGML_ASSERT(src5->nb[2] <= (size_t)INT_MAX);
844 GGML_ASSERT(src5->nb[3] <= (size_t)INT_MAX);
845
846#if !defined(GGML_USE_HIP)
847 // Mamba-2 with scalar A per head: use SSD matmul path for long sequences.
848 // Requires NVIDIA Turing+ otherwise fallback to scan.
849 const bool is_mamba2 = (src3->nb[1] == sizeof(float));
850 const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
851 const bool use_ssd = is_mamba2 && n_t > SSM_SSD_MIN_TOKENS
852 && K == 1
853 && n_t <= SSM_SSD_MAX_TOKENS
854 && GGML_CUDA_CC_IS_NVIDIA(cc)
855 && cc >= GGML_CUDA_CC_TURING
856 && nr % 8 == 0; // cuBLAS requires 8-element (16-byte) alignment
857
858 if (use_ssd) {
859 // ssm_ssd_init_state_kernel uses flat linear indexing within each sequence,
860 // so src0 must be fully contiguous across all inner dimensions.
861 // The scan path handles non-contiguous nb[2] via src0_nb2 but does not handle nb[1].
862 GGML_ASSERT(src0->nb[1] == nc * sizeof(float));
863 GGML_ASSERT(src0->nb[2] == nc * nr * sizeof(float));
864
865 ssm_scan_ssd_f32_cuda(ctx,
866 src0_d, src1_d, src2_d, src3_d, src4_d, src5_d, src6_d, dst_d,
867 (int64_t)(src0->nb[3] / sizeof(float)),
868 (int)(src1->nb[2] / sizeof(float)), (int)(src1->nb[3] / sizeof(float)),
869 (int)(src2->nb[1] / sizeof(float)), (int)(src2->nb[2] / sizeof(float)),
870 (int)(src3->nb[1] / sizeof(float)),
871 (int)(src4->nb[2] / sizeof(float)), (int)(src4->nb[3] / sizeof(float)),
872 (int)(src5->nb[2] / sizeof(float)), (int)(src5->nb[3] / sizeof(float)),
873 cache ? cache->data : (float *) ((char *) dst_d + s_off), nc, nr, nh, ng, n_t, n_s);
874 return;
875 }
876#endif
877 ssm_scan_f32_cuda(src0_d, src1_d, src2_d, src3_d, src4_d, src5_d, src6_d, dst_d,
878 src0->nb[2], src0->nb[3], src1->nb[2], src1->nb[3], src2->nb[1], src2->nb[2],
879 src3->nb[1], src4->nb[2], src4->nb[3], src5->nb[2], src5->nb[3],
880 s_off, nc, nr, nh, ng, n_t, n_s, K, cache, stream);
881}
882
883void ggml_cuda_op_ssm_scan(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
884 ggml_cuda_op_ssm_scan_impl(ctx, dst, nullptr);
885}
886
887void ggml_cuda_op_ssm_scan_fused_cache(ggml_backend_cuda_context & ctx, ggml_tensor * dst,
888 ggml_cuda_ssm_scan_fused_cache cache) {
889 ggml_cuda_op_ssm_scan_impl(ctx, dst, &cache);
890}