mamba-rs 0.7.4

Mamba SSM and Mamba-3 SISO in Rust with optional CUDA acceleration: inference and training (BPTT through the SSM state, AdamW) on CPU and GPU, custom NVRTC-compiled kernels, CUDA Graph capture, f32 / bf16 / f16 storage, deterministic batch-invariant GEMMs by default with explicit cuBLAS Fast and Pedantic modes.
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! The Ada-measured inference cells: one Fixed-overlay kernel per (shape,
//! operand dtypes, policy) that beat the ladder route on the Ada board. The
//! Ada board takes them as evidence; every other sm_80-tier board proves a
//! cell's output words against the ladder route at first use.

use super::super::context::F32TriadPolicy;
use super::super::dtype::WeightDtype;
use super::{FixedTileDevice, InferenceFwdOperands, InferenceShape};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sm89CellRoute {
    /// 4621 x 1928 x 384 exact f32: 112x128 tiles of scalar FMA chains.
    ExactM112N128Bk32S3,
    /// 2048 x 768 x 2304 half inputs with an f32 output: 128x144 tiles.
    HalfF32OutM128N144Bk32S2,
    /// 2048 x 2304 x 768 homogeneous half: 128x96 tiles with packed stores.
    HalfM128N96Bk64S2Vec,
    /// 2048 x 768 x 2304 bf16: 128x144 tiles with packed stores.
    HalfM128N144Bk32S2VecBf16,
    /// 2048 x 768 x 2304 TF32: 64x288 tiles.
    Tf32M64N288Bk16S2,
    /// 2048 x 2304 x 768 TF32 with a bias: 64x96 tiles.
    Tf32M64N96Bk32S2Bias,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Sm89CellFamily {
    ExactFma,
    HalfMma,
    Tf32Mma,
}

/// The output a cell writes: f32, or the input's own half type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Sm89CellOutput {
    F32,
    Input,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Sm89CellSpec {
    pub route: Sm89CellRoute,
    pub family: Sm89CellFamily,
    pub symbol: &'static str,
    pub input: WeightDtype,
    pub output: Sm89CellOutput,
    pub tile: (u32, u32),
    pub bk: u32,
    pub stages: u8,
    pub threads: u32,
    pub dynamic_shared_bytes: u32,
    pub register_cap: u32,
    pub occupancy_gate: u32,
}

/// A half cell from its geometry: (tile, bk, threads, dynamic shared bytes).
const fn half_spec(
    route: Sm89CellRoute,
    symbol: &'static str,
    input: WeightDtype,
    output: Sm89CellOutput,
    geometry: ((u32, u32), u32, u32, u32),
) -> Sm89CellSpec {
    let (tile, bk, threads, dynamic_shared_bytes) = geometry;
    Sm89CellSpec {
        route,
        family: Sm89CellFamily::HalfMma,
        symbol,
        input,
        output,
        tile,
        bk,
        stages: 2,
        threads,
        dynamic_shared_bytes,
        register_cap: 255,
        occupancy_gate: 1,
    }
}

pub(crate) const SM89_CELL_SPECS: [Sm89CellSpec; 8] = [
    Sm89CellSpec {
        route: Sm89CellRoute::ExactM112N128Bk32S3,
        family: Sm89CellFamily::ExactFma,
        symbol: "nn_sm89_m112n128_bk32_s3_f32",
        input: WeightDtype::F32,
        output: Sm89CellOutput::F32,
        tile: (112, 128),
        bk: 32,
        stages: 3,
        threads: 256,
        dynamic_shared_bytes: 97_536,
        register_cap: 255,
        occupancy_gate: 1,
    },
    half_spec(
        Sm89CellRoute::HalfF32OutM128N144Bk32S2,
        "nn_sm89_m128n144_bk32_s2_f32out_bf16",
        WeightDtype::Bf16,
        Sm89CellOutput::F32,
        ((128, 144), 32, 128, 39_936),
    ),
    half_spec(
        Sm89CellRoute::HalfF32OutM128N144Bk32S2,
        "nn_sm89_m128n144_bk32_s2_f32out_f16",
        WeightDtype::F16,
        Sm89CellOutput::F32,
        ((128, 144), 32, 128, 39_936),
    ),
    half_spec(
        Sm89CellRoute::HalfM128N96Bk64S2Vec,
        "nn_sm89_m128n96_bk64_s2_vec_bf16",
        WeightDtype::Bf16,
        Sm89CellOutput::Input,
        ((128, 96), 64, 256, 63_488),
    ),
    half_spec(
        Sm89CellRoute::HalfM128N96Bk64S2Vec,
        "nn_sm89_m128n96_bk64_s2_vec_f16",
        WeightDtype::F16,
        Sm89CellOutput::Input,
        ((128, 96), 64, 256, 63_488),
    ),
    // The packed-store epilogue stages the f32 tile (128 x 152 floats) on top
    // of the pipeline slabs, so the launch allocates the tile, not the slabs.
    half_spec(
        Sm89CellRoute::HalfM128N144Bk32S2VecBf16,
        "nn_sm89_m128n144_bk32_s2_vec_bf16",
        WeightDtype::Bf16,
        Sm89CellOutput::Input,
        ((128, 144), 32, 256, 77_824),
    ),
    Sm89CellSpec {
        route: Sm89CellRoute::Tf32M64N288Bk16S2,
        family: Sm89CellFamily::Tf32Mma,
        symbol: "nn_sm89_m64n288_bk16_s2_tf32",
        input: WeightDtype::F32,
        output: Sm89CellOutput::F32,
        tile: (64, 288),
        bk: 16,
        stages: 2,
        threads: 128,
        dynamic_shared_bytes: 48_128,
        register_cap: 255,
        occupancy_gate: 2,
    },
    Sm89CellSpec {
        route: Sm89CellRoute::Tf32M64N96Bk32S2Bias,
        family: Sm89CellFamily::Tf32Mma,
        symbol: "nn_sm89_m64n96_bk32_s2_tf32",
        input: WeightDtype::F32,
        output: Sm89CellOutput::F32,
        tile: (64, 96),
        bk: 32,
        stages: 2,
        threads: 128,
        dynamic_shared_bytes: 45_056,
        register_cap: 255,
        occupancy_gate: 2,
    },
];

pub(crate) const SM89_CELL_SYMBOLS: [&str; 8] = [
    SM89_CELL_SPECS[0].symbol,
    SM89_CELL_SPECS[1].symbol,
    SM89_CELL_SPECS[2].symbol,
    SM89_CELL_SPECS[3].symbol,
    SM89_CELL_SPECS[4].symbol,
    SM89_CELL_SPECS[5].symbol,
    SM89_CELL_SPECS[6].symbol,
    SM89_CELL_SPECS[7].symbol,
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Sm89CellBias {
    Any,
    Required,
}

/// One measured cell: the forward (m, k, n) and the bias states measured.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Sm89Cell {
    pub route: Sm89CellRoute,
    pub shape: (usize, usize, usize),
    pub bias: Sm89CellBias,
}

pub(crate) const SM89_CELLS: [Sm89Cell; 6] = [
    Sm89Cell {
        route: Sm89CellRoute::ExactM112N128Bk32S3,
        shape: (4621, 1928, 384),
        bias: Sm89CellBias::Any,
    },
    Sm89Cell {
        route: Sm89CellRoute::HalfF32OutM128N144Bk32S2,
        shape: (2048, 768, 2304),
        bias: Sm89CellBias::Any,
    },
    Sm89Cell {
        route: Sm89CellRoute::HalfM128N144Bk32S2VecBf16,
        shape: (2048, 768, 2304),
        bias: Sm89CellBias::Any,
    },
    Sm89Cell {
        route: Sm89CellRoute::Tf32M64N288Bk16S2,
        shape: (2048, 768, 2304),
        bias: Sm89CellBias::Any,
    },
    Sm89Cell {
        route: Sm89CellRoute::HalfM128N96Bk64S2Vec,
        shape: (2048, 2304, 768),
        bias: Sm89CellBias::Any,
    },
    Sm89Cell {
        route: Sm89CellRoute::Tf32M64N96Bk32S2Bias,
        shape: (2048, 2304, 768),
        bias: Sm89CellBias::Required,
    },
];

#[derive(Clone, Copy)]
pub(super) struct Sm89CellStack {
    pub(super) device: FixedTileDevice,
    pub(super) nvrtc: (i32, i32),
    pub(super) nvrtc_library_known: bool,
    pub(super) policy: F32TriadPolicy,
}

pub(crate) fn spec_for_route(
    route: Sm89CellRoute,
    input: WeightDtype,
) -> Option<&'static Sm89CellSpec> {
    SM89_CELL_SPECS
        .iter()
        .find(|spec| spec.route == route && spec.input == input)
}

/// Whether the operands carry exactly the dtypes the cell was measured with.
pub(crate) fn operands_match(spec: &Sm89CellSpec, operands: InferenceFwdOperands) -> bool {
    let output = match spec.output {
        Sm89CellOutput::F32 => WeightDtype::F32,
        Sm89CellOutput::Input => spec.input,
    };
    operands.x.dtype == spec.input && operands.w.dtype == spec.input && operands.c.dtype == output
}

fn policy_admits(spec: &Sm89CellSpec, policy: F32TriadPolicy) -> bool {
    match spec.family {
        Sm89CellFamily::ExactFma => policy == F32TriadPolicy::ExactScalarFma,
        Sm89CellFamily::Tf32Mma => policy == F32TriadPolicy::AllowDeterministicTf32,
        Sm89CellFamily::HalfMma => true,
    }
}

/// The cell a forward request lands on: the exact measured shape, the
/// measured operand dtypes and bias state, the policy the family serves,
/// an sm_80-tier board that composes the overlay, and the kernel bound.
pub(super) fn select_sm89_cell(
    operands: InferenceFwdOperands,
    shape: InferenceShape,
    stack: Sm89CellStack,
    loaded: impl Fn(&str) -> bool,
) -> Option<&'static Sm89CellSpec> {
    if !stack.nvrtc_library_known
        || !super::fixed_portable_overlay_board(stack.device.compute_capability)
        || stack.device.multiprocessors == 0
        || !matches!(stack.nvrtc, (12, 8) | (13, 0) | (13, 2))
        || [operands.c.ptr, operands.x.ptr, operands.w.ptr]
            .into_iter()
            .any(|pointer| pointer == 0 || !pointer.is_multiple_of(16))
        || operands
            .bias_ptr
            .is_some_and(|pointer| pointer == 0 || !pointer.is_multiple_of(4))
    {
        return None;
    }
    SM89_CELLS
        .iter()
        .filter(|cell| {
            cell.shape == (shape.m, shape.k, shape.n)
                && (cell.bias == Sm89CellBias::Any || operands.bias_ptr.is_some())
        })
        .find_map(|cell| {
            spec_for_route(cell.route, operands.x.dtype).filter(|spec| {
                operands_match(spec, operands)
                    && policy_admits(spec, stack.policy)
                    && loaded(spec.symbol)
            })
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mamba_ssm::gpu::gemm_bi_inference::TypedPtr;

    /// The half cells stage their pipeline slabs in dynamic shared memory,
    /// and the packed-store cells additionally stage the whole f32 tile
    /// there for the epilogue; the launch must allocate the larger of the
    /// two, or the epilogue writes past the allocation.
    #[test]
    fn half_cells_allocate_the_larger_of_pipeline_and_epilogue_tile() {
        for spec in &SM89_CELL_SPECS {
            if spec.family != Sm89CellFamily::HalfMma {
                continue;
            }
            let (rows, columns) = spec.tile;
            let pipeline =
                u32::from(spec.stages) * (rows * (spec.bk + 8) + spec.bk * (columns + 8)) * 2;
            let epilogue = match spec.route {
                Sm89CellRoute::HalfM128N96Bk64S2Vec | Sm89CellRoute::HalfM128N144Bk32S2VecBf16 => {
                    rows * (columns + 8) * 4
                }
                _ => 0,
            };
            assert_eq!(
                spec.dynamic_shared_bytes,
                pipeline.max(epilogue),
                "{} shared-memory allocation",
                spec.symbol
            );
        }
    }

    fn operands(x: WeightDtype, c: WeightDtype, bias: Option<u64>) -> InferenceFwdOperands {
        InferenceFwdOperands {
            c: TypedPtr {
                ptr: 0x1000,
                dtype: c,
            },
            x: TypedPtr {
                ptr: 0x2000,
                dtype: x,
            },
            w: TypedPtr {
                ptr: 0x3000,
                dtype: x,
            },
            bias_ptr: bias,
        }
    }

    fn shape(dims: (usize, usize, usize)) -> InferenceShape {
        InferenceShape {
            m: dims.0,
            k: dims.1,
            n: dims.2,
        }
    }

    fn stack(cc: (u32, u32), policy: F32TriadPolicy) -> Sm89CellStack {
        Sm89CellStack {
            device: FixedTileDevice {
                multiprocessors: 142,
                compute_capability: cc,
            },
            nvrtc: (13, 2),
            nvrtc_library_known: true,
            policy,
        }
    }

    const EXACT: F32TriadPolicy = F32TriadPolicy::ExactScalarFma;
    const TF32: F32TriadPolicy = F32TriadPolicy::AllowDeterministicTf32;

    fn select(
        ops: InferenceFwdOperands,
        dims: (usize, usize, usize),
        stack: Sm89CellStack,
    ) -> Option<&'static str> {
        select_sm89_cell(ops, shape(dims), stack, |_| true).map(|spec| spec.symbol)
    }

    #[test]
    fn every_measured_cell_selects_its_own_kernel() {
        use WeightDtype::{Bf16, F16, F32};
        let ada = stack((8, 9), EXACT);
        let ada_tf32 = stack((8, 9), TF32);
        for (ops, dims, stack, symbol) in [
            (
                operands(F32, F32, None),
                (4621, 1928, 384),
                ada,
                "nn_sm89_m112n128_bk32_s3_f32",
            ),
            (
                operands(F32, F32, Some(0x4004)),
                (4621, 1928, 384),
                ada,
                "nn_sm89_m112n128_bk32_s3_f32",
            ),
            (
                operands(Bf16, F32, None),
                (2048, 768, 2304),
                ada,
                "nn_sm89_m128n144_bk32_s2_f32out_bf16",
            ),
            (
                operands(F16, F32, Some(0x4004)),
                (2048, 768, 2304),
                ada_tf32,
                "nn_sm89_m128n144_bk32_s2_f32out_f16",
            ),
            (
                operands(Bf16, Bf16, None),
                (2048, 768, 2304),
                ada,
                "nn_sm89_m128n144_bk32_s2_vec_bf16",
            ),
            (
                operands(F32, F32, None),
                (2048, 768, 2304),
                ada_tf32,
                "nn_sm89_m64n288_bk16_s2_tf32",
            ),
            (
                operands(F32, F32, Some(0x4004)),
                (2048, 768, 2304),
                ada_tf32,
                "nn_sm89_m64n288_bk16_s2_tf32",
            ),
            (
                operands(Bf16, Bf16, None),
                (2048, 2304, 768),
                ada,
                "nn_sm89_m128n96_bk64_s2_vec_bf16",
            ),
            (
                operands(F16, F16, Some(0x4004)),
                (2048, 2304, 768),
                ada,
                "nn_sm89_m128n96_bk64_s2_vec_f16",
            ),
            (
                operands(F32, F32, Some(0x4004)),
                (2048, 2304, 768),
                ada_tf32,
                "nn_sm89_m64n96_bk32_s2_tf32",
            ),
        ] {
            assert_eq!(
                select(ops, dims, stack),
                Some(symbol),
                "{dims:?} {:?}",
                ops.x.dtype
            );
        }
    }

    #[test]
    fn unmeasured_states_fall_through_to_the_ladder() {
        use WeightDtype::{Bf16, F16, F32};
        let ada = stack((8, 9), EXACT);
        let ada_tf32 = stack((8, 9), TF32);
        for (ops, dims, stack, why) in [
            (
                operands(F32, F32, None),
                (2048, 2304, 768),
                ada_tf32,
                "the hot_e TF32 cell needs a bias",
            ),
            (
                operands(F32, F32, None),
                (2048, 2304, 768),
                ada,
                "hot_e exact f32 has no cell",
            ),
            (
                operands(F16, F16, None),
                (2048, 768, 2304),
                ada,
                "the hot_d packed cell is bf16 only",
            ),
            (
                operands(F32, F32, None),
                (4621, 1928, 384),
                ada_tf32,
                "hot_c under TF32 has no cell",
            ),
            (
                operands(F32, F32, None),
                (2048, 768, 2304),
                ada,
                "hot_d exact f32 has no cell",
            ),
            (
                operands(Bf16, Bf16, None),
                (1024, 256, 128),
                ada,
                "no cell at the classifier shape",
            ),
            (
                operands(Bf16, F16, None),
                (2048, 768, 2304),
                ada,
                "mismatched half operands",
            ),
        ] {
            assert_eq!(select(ops, dims, stack), None, "{why}");
        }
    }

    #[test]
    fn other_sm80_boards_reach_the_cells_and_the_cc12_family_does_not() {
        use WeightDtype::F32;
        let ops = operands(F32, F32, None);
        for cc in [(8, 0), (8, 6), (9, 0), (10, 0), (11, 0)] {
            assert!(
                select(ops, (4621, 1928, 384), stack(cc, EXACT)).is_some(),
                "{cc:?}"
            );
        }
        for cc in [(7, 5), (12, 0), (12, 1)] {
            assert_eq!(
                select(ops, (4621, 1928, 384), stack(cc, EXACT)),
                None,
                "{cc:?}"
            );
        }
        for nvrtc in [(12, 7), (12, 9), (13, 1), (13, 3)] {
            let mut stack = stack((8, 9), EXACT);
            stack.nvrtc = nvrtc;
            assert_eq!(select(ops, (4621, 1928, 384), stack), None, "{nvrtc:?}");
        }
        let mut unknown = stack((8, 9), EXACT);
        unknown.nvrtc_library_known = false;
        assert_eq!(select(ops, (4621, 1928, 384), unknown), None);
        let mut idle = stack((8, 9), EXACT);
        idle.device.multiprocessors = 0;
        assert_eq!(select(ops, (4621, 1928, 384), idle), None);
        assert_eq!(
            select_sm89_cell(ops, shape((4621, 1928, 384)), stack((8, 9), EXACT), |_| {
                false
            }),
            None,
            "an unbound kernel never selects"
        );
        for misaligned in [
            InferenceFwdOperands {
                c: TypedPtr {
                    ptr: 0x1008,
                    ..ops.c
                },
                ..ops
            },
            InferenceFwdOperands {
                x: TypedPtr { ptr: 0, ..ops.x },
                ..ops
            },
            InferenceFwdOperands {
                bias_ptr: Some(0x4002),
                ..ops
            },
        ] {
            assert_eq!(
                select(misaligned, (4621, 1928, 384), stack((8, 9), EXACT)),
                None
            );
        }
    }

    #[test]
    fn specs_and_symbols_are_one_closed_table() {
        assert_eq!(SM89_CELL_SPECS.len(), SM89_CELL_SYMBOLS.len());
        let unique: std::collections::BTreeSet<_> = SM89_CELL_SYMBOLS.into_iter().collect();
        assert_eq!(unique.len(), SM89_CELL_SYMBOLS.len());
        for cell in SM89_CELLS {
            assert!(
                SM89_CELL_SPECS.iter().any(|spec| spec.route == cell.route),
                "{:?} has no kernel",
                cell.route
            );
        }
        for spec in SM89_CELL_SPECS {
            assert_eq!(spec_for_route(spec.route, spec.input), Some(&spec));
            assert!(spec.threads.is_multiple_of(32));
            assert!(spec.dynamic_shared_bytes <= 101_376);
        }
    }
}