1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use std::ffi::c_void;
#[allow(dead_code)]
extern "C" {
pub(crate) fn asort_asc_f32(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_asc_f16(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_asc_bf16(
x: *const c_void,
dst: *const c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_asc_f64(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_asc_u8(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_asc_u32(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_asc_i64(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_desc_f32(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_desc_f16(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_desc_bf16(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_desc_f64(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_desc_u8(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_desc_u32(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
pub(crate) fn asort_desc_i64(
x: *const c_void,
dst: *mut c_void,
nrows: i32,
ncols: i32,
inplace: bool,
stream: i64,
);
// for unquntized models (decoding)
pub fn moe_gemm(
input: *const c_void, // input [size_m, size_k]
weights: *const c_void, // weights [num_experts, size_n, size_k]
sorted_token_ids: *const i32,
expert_ids: *const i32,
topk_weights: *const f32, // device ptr or nullptr
output: *mut c_void, // output [size_m, size_n]
num_experts: i32,
topk: i32,
size_m: i32,
size_n: i32,
size_k: i32,
dtype: i32, // 0=float16, 1=bf16 (for input)
stream: i64,
);
// for unquntized models (prefill)
pub fn moe_gemm_wmma(
input: *const c_void, // device pointer [size_m, size_k]
weights: *const c_void, // device pointer [num_experts, size_n, size_k]
sorted_token_ids: *const i32, // device pointer [size_m]
expert_ids: *const i32, // host array [size_m] (expert id per sorted token)
topk_weights: *const f32,
output: *mut c_void, // device pointer [size_m, size_n]
num_experts: i32,
topk: i32,
size_m: i32,
size_n: i32,
size_k: i32,
dtype: i32, // 0=float16, 1=bf16 (for input/output)
stream: i64,
);
// for unquntized models (decoding) with transposed weights [num_experts, size_k, size_n]
pub fn moe_gemm_transposed(
input: *const c_void, // input [size_m, size_k]
weights: *const c_void, // weights [num_experts, size_k, size_n] - transposed layout
sorted_token_ids: *const i32,
expert_ids: *const i32,
topk_weights: *const f32, // device ptr or nullptr
output: *mut c_void, // output [size_m, size_n]
num_experts: i32,
topk: i32,
size_m: i32,
size_n: i32,
size_k: i32,
dtype: i32, // 0=float16, 1=bf16 (for input)
stream: i64,
);
// for unquntized models (prefill) with transposed weights [num_experts, size_k, size_n]
pub fn moe_gemm_wmma_transposed(
input: *const c_void, // device pointer [size_m, size_k]
weights: *const c_void, // device pointer [num_experts, size_k, size_n] - transposed layout
sorted_token_ids: *const i32, // device pointer [size_m]
expert_ids: *const i32, // host array [size_m] (expert id per sorted token)
topk_weights: *const f32,
output: *mut c_void, // device pointer [size_m, size_n]
num_experts: i32,
topk: i32,
size_m: i32,
size_n: i32,
size_k: i32,
dtype: i32, // 0=float16, 1=bf16 (for input/output)
stream: i64,
);
// MoE GEMV for decode phase (optimized for small batch sizes M <= 8)
pub fn moe_gemv(
input: *const c_void, // input [size_m or size_m / topk, size_k]
weights: *const c_void, // weights [num_experts, size_n, size_k]
sorted_token_ids: *const i32,
expert_ids: *const i32,
topk_weights: *const f32, // device ptr or nullptr
output: *mut c_void, // output [size_m, size_n]
num_experts: i32,
topk: i32,
size_m: i32,
size_n: i32,
size_k: i32,
dtype: i32, // 0=float16, 1=bf16 (for input)
stream: i64,
);
// MoE GEMV for decode phase with transposed weights [num_experts, size_k, size_n]
pub fn moe_gemv_transposed(
input: *const c_void, // input [size_m or size_m / topk, size_k]
weights: *const c_void, // weights [num_experts, size_k, size_n] - transposed layout
sorted_token_ids: *const i32,
expert_ids: *const i32,
topk_weights: *const f32, // device ptr or nullptr
output: *mut c_void, // output [size_m, size_n]
num_experts: i32,
topk: i32,
size_m: i32,
size_n: i32,
size_k: i32,
dtype: i32, // 0=float16, 1=bf16 (for input)
stream: i64,
);
// Optimized parallel topk for small k (MoE routing)
// Single kernel call writes to both values and indices buffers
pub(crate) fn topk_f32(
input: *const c_void,
values_out: *mut c_void, // [nrows, k]
indices_out: *mut c_void, // [nrows, k] as u32
nrows: i32,
ncols: i32,
k: i32,
stream: i64,
);
pub(crate) fn topk_bf16(
input: *const c_void,
values_out: *mut c_void, // [nrows, k]
indices_out: *mut c_void, // [nrows, k] as u32
nrows: i32,
ncols: i32,
k: i32,
stream: i64,
);
pub(crate) fn topk_f16(
input: *const c_void,
values_out: *mut c_void, // [nrows, k]
indices_out: *mut c_void, // [nrows, k] as u32
nrows: i32,
ncols: i32,
k: i32,
stream: i64,
);
// Fused topk + softmax - returns softmax weights directly (not raw logits)
pub(crate) fn topk_softmax_f32(
input: *const c_void,
weights_out: *mut c_void, // [nrows, k] - softmax weights
indices_out: *mut c_void, // [nrows, k] as u32
nrows: i32,
ncols: i32,
k: i32,
stream: i64,
);
pub(crate) fn topk_softmax_bf16(
input: *const c_void,
weights_out: *mut c_void,
indices_out: *mut c_void,
nrows: i32,
ncols: i32,
k: i32,
stream: i64,
);
pub(crate) fn topk_softmax_f16(
input: *const c_void,
weights_out: *mut c_void,
indices_out: *mut c_void,
nrows: i32,
ncols: i32,
k: i32,
stream: i64,
);
// Mamba SSM selective scan kernel
pub(crate) fn selective_scan_cuda(
x: *const f32, // (batch, seq_len, n_heads * head_dim)
dt: *const f32, // (batch, seq_len, n_heads)
a: *const f32, // (n_heads,) - negative exp of A_log
b: *const f32, // (batch, seq_len, n_heads * d_state)
c: *const f32, // (batch, seq_len, n_heads * d_state)
d: *const f32, // (n_heads,)
dt_bias: *const f32, // (n_heads,)
state: *mut f32, // (batch, n_heads, head_dim, d_state)
y: *mut f32, // (batch, seq_len, n_heads * head_dim)
batch_size: i32,
n_heads: i32,
head_dim: i32,
d_state: i32,
seq_len: i32,
dt_min: f32,
dt_max: f32,
stream: i64,
);
// GDN (Gated Delta Net) kernels for qwen3_next
pub(crate) fn gated_delta_rule_recurrence(
q: *const f32,
k: *const f32,
v: *const f32,
g: *const f32,
beta: *const f32,
state: *mut f32,
output: *mut f32,
bh: i32,
seq_len: i32,
k_dim: i32,
v_dim: i32,
stream: i64,
);
// Chunked GDN recurrence for prefill (processes tokens in BT=64 chunks)
pub(crate) fn chunked_gated_delta_rule_recurrence(
q: *const f32,
k: *const f32,
v: *const f32,
g: *const f32,
beta: *const f32,
state: *mut f32,
output: *mut f32,
bh: i32,
seq_len: i32,
k_dim: i32,
v_dim: i32,
stream: i64,
);
pub(crate) fn causal_conv1d_update(
x: *const c_void,
weight: *const c_void,
conv_state: *mut c_void,
output: *mut c_void,
batch_size: i32,
conv_dim: i32,
kernel_size: i32,
dtype: i32,
stream: i64,
);
pub(crate) fn causal_conv1d_full(
x: *const c_void,
weight: *const c_void,
conv_state_out: *mut c_void,
output: *mut c_void,
batch_size: i32,
conv_dim: i32,
seq_len: i32,
kernel_size: i32,
dtype: i32,
stream: i64,
);
pub(crate) fn fused_gdn_gating(
b: *const c_void,
a: *const c_void,
a_log: *const f32,
dt_bias: *const f32,
beta_out: *mut c_void,
g_out: *mut c_void,
total_elements: i32,
num_heads: i32,
dtype: i32,
stream: i64,
);
}