mamba-rs 0.5.0

Mamba SSM and Mamba-3 SISO in Rust with optional CUDA GPU acceleration. Inference and training (BPTT through SSM state, AdamW), CPU + GPU paths, custom CUDA kernels, CUDA Graph capture, f32 / bf16 / f16. Opt-in deterministic training (bit-identical runs, batch-invariant inference) with a tensor-core tier that beats cuBLAS on LLM-sized models.
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
//! CPU BLAS routines for Mamba training and inference.
//!
//! Platform dispatch:
//! - `accelerate` feature (macOS): Apple Accelerate `cblas_sgemm` via AMX coprocessor
//! - `gemm-blas` feature (any): `gemm` crate with AVX2/AVX-512/NEON microkernels
//! - fallback: pure Rust scalar loops (LLVM auto-vectorizes with target-cpu=native)

// ---------------------------------------------------------------------------
// Apple Accelerate FFI (macOS only, behind `accelerate` feature)
// ---------------------------------------------------------------------------

#[cfg(all(feature = "accelerate", target_os = "macos"))]
#[link(name = "Accelerate", kind = "framework")]
unsafe extern "C" {
    fn cblas_sgemm(
        order: i32,
        trans_a: i32,
        trans_b: i32,
        m: i32,
        n: i32,
        k: i32,
        alpha: f32,
        a: *const f32,
        lda: i32,
        b: *const f32,
        ldb: i32,
        beta: f32,
        c: *mut f32,
        ldc: i32,
    );
}

// ---------------------------------------------------------------------------
// SGEMM forward: Y[B,N] = X[B,K] @ W[K,N] + bias[N]
// ---------------------------------------------------------------------------

/// Batched linear forward: `Y[B,N] = X[B,K] @ W[K,N] + bias[N]`.
///
/// All matrices are flat row-major `[rows * cols]`.
/// Dispatches to platform BLAS when feature flags are enabled.
pub fn sgemm_forward(
    y: &mut [f32],
    x: &[f32],
    w: &[f32],
    bias: Option<&[f32]>,
    batch: usize,
    n_in: usize,
    n_out: usize,
) {
    // Guard the FFI paths: cblas_sgemm/gemm read raw pointers with sizes
    // derived from the dims, so undersized slices (e.g. weights from an
    // unvalidated checkpoint) would read out of bounds. Negligible cost
    // relative to the GEMM itself.
    assert!(
        x.len() >= batch * n_in,
        "sgemm_forward: x.len() {} < batch*n_in {}",
        x.len(),
        batch * n_in
    );
    assert!(
        w.len() >= n_in * n_out,
        "sgemm_forward: w.len() {} < n_in*n_out {}",
        w.len(),
        n_in * n_out
    );
    assert!(
        y.len() >= batch * n_out,
        "sgemm_forward: y.len() {} < batch*n_out {}",
        y.len(),
        batch * n_out
    );

    // Pre-fill with bias
    if let Some(b) = bias {
        for row in 0..batch {
            let off = row * n_out;
            y[off..off + n_out].copy_from_slice(&b[..n_out]);
        }
    } else {
        y[..batch * n_out].fill(0.0);
    }

    // Dispatch SGEMM: Y += X @ W
    #[cfg(all(feature = "accelerate", target_os = "macos"))]
    unsafe {
        cblas_sgemm(
            101,            // CblasRowMajor
            111,            // CblasNoTrans
            111,            // CblasNoTrans
            batch as i32,   // M
            n_out as i32,   // N
            n_in as i32,    // K
            1.0,            // alpha
            x.as_ptr(),     // A
            n_in as i32,    // lda
            w.as_ptr(),     // B
            n_out as i32,   // ldb
            1.0,            // beta (accumulate into bias)
            y.as_mut_ptr(), // C
            n_out as i32,   // ldc
        );
    }

    #[cfg(all(
        feature = "gemm-blas",
        not(all(feature = "accelerate", target_os = "macos"))
    ))]
    unsafe {
        gemm::gemm(
            batch,
            n_out,
            n_in,
            y.as_mut_ptr(),
            1,              // dst col stride
            n_out as isize, // dst row stride
            true,           // read dst (alpha=1, accumulate into bias)
            x.as_ptr(),
            1,             // lhs col stride
            n_in as isize, // lhs row stride
            w.as_ptr(),
            1,              // rhs col stride
            n_out as isize, // rhs row stride
            1.0,            // alpha (scale existing dst)
            1.0,            // beta (scale product)
            false,
            false,
            false,
            gemm::Parallelism::None, // caller controls threading via rayon
        );
    }

    #[cfg(not(any(
        all(feature = "accelerate", target_os = "macos"),
        feature = "gemm-blas"
    )))]
    {
        for row in 0..batch {
            let x_off = row * n_in;
            let y_off = row * n_out;
            for k in 0..n_in {
                let xv = x[x_off + k];
                let w_off = k * n_out;
                for j in 0..n_out {
                    y[y_off + j] += xv * w[w_off + j];
                }
            }
        }
    }
}

/// [`sgemm_forward`] with the gemm crate's internal rayon parallelism —
/// the prefill serving path's single-page latency lever (a lone sequence
/// cannot amortize across a batch, so the GEMM itself must parallelize).
/// Tile-local accumulation keeps results bit-identical to the serial path
/// (verified by the prefill parallel-invariance test). The Accelerate
/// branch already threads internally and the scalar fallback is not a
/// serving configuration — both delegate to [`sgemm_forward`].
pub fn sgemm_forward_par(
    y: &mut [f32],
    x: &[f32],
    w: &[f32],
    bias: Option<&[f32]>,
    batch: usize,
    n_in: usize,
    n_out: usize,
) {
    #[cfg(all(
        feature = "gemm-blas",
        not(all(feature = "accelerate", target_os = "macos"))
    ))]
    {
        assert!(
            x.len() >= batch * n_in,
            "sgemm_forward_par: x.len() {} < batch*n_in {}",
            x.len(),
            batch * n_in
        );
        assert!(
            w.len() >= n_in * n_out,
            "sgemm_forward_par: w.len() {} < n_in*n_out {}",
            w.len(),
            n_in * n_out
        );
        assert!(
            y.len() >= batch * n_out,
            "sgemm_forward_par: y.len() {} < batch*n_out {}",
            y.len(),
            batch * n_out
        );
        if let Some(b) = bias {
            for row in 0..batch {
                let off = row * n_out;
                y[off..off + n_out].copy_from_slice(&b[..n_out]);
            }
        } else {
            y[..batch * n_out].fill(0.0);
        }
        unsafe {
            gemm::gemm(
                batch,
                n_out,
                n_in,
                y.as_mut_ptr(),
                1,
                n_out as isize,
                true,
                x.as_ptr(),
                1,
                n_in as isize,
                w.as_ptr(),
                1,
                n_out as isize,
                1.0,
                1.0,
                false,
                false,
                false,
                gemm::Parallelism::Rayon(rayon::current_num_threads()),
            );
        }
    }
    #[cfg(not(all(
        feature = "gemm-blas",
        not(all(feature = "accelerate", target_os = "macos"))
    )))]
    sgemm_forward(y, x, w, bias, batch, n_in, n_out);
}

/// Single-sample matrix-vector forward: `y[N] = x[K] @ W[K,N] + bias[N]`.
pub fn matvec_forward(
    y: &mut [f32],
    x: &[f32],
    w: &[f32],
    bias: Option<&[f32]>,
    n_in: usize,
    n_out: usize,
) {
    sgemm_forward(y, x, w, bias, 1, n_in, n_out);
}

// ---------------------------------------------------------------------------
// SGEMM backward: dX = dY @ W^T, dW += X^T @ dY, dBias += colsum(dY)
// ---------------------------------------------------------------------------

/// Batched linear backward: computes dX, dW, and optionally dBias.
///
/// - `dx = dY @ W^T` (overwritten)
/// - `dw += X^T @ dY` (accumulated)
/// - `db += colsum(dY)` (accumulated, if present)
pub fn sgemm_backward(
    dx: &mut [f32],
    dw: &mut [f32],
    db: Option<&mut [f32]>,
    dy: &[f32],
    x_saved: &[f32],
    w: &[f32],
    dims: (usize, usize, usize), // (batch, n_in, n_out)
) {
    let (batch, n_in, n_out) = dims;

    // Guard the FFI paths (see sgemm_forward).
    assert!(
        dy.len() >= batch * n_out,
        "sgemm_backward: dy.len() {} < batch*n_out {}",
        dy.len(),
        batch * n_out
    );
    assert!(
        x_saved.len() >= batch * n_in,
        "sgemm_backward: x_saved.len() {} < batch*n_in {}",
        x_saved.len(),
        batch * n_in
    );
    assert!(
        w.len() >= n_in * n_out,
        "sgemm_backward: w.len() {} < n_in*n_out {}",
        w.len(),
        n_in * n_out
    );
    assert!(
        dx.len() >= batch * n_in,
        "sgemm_backward: dx.len() {} < batch*n_in {}",
        dx.len(),
        batch * n_in
    );
    assert!(
        dw.len() >= n_in * n_out,
        "sgemm_backward: dw.len() {} < n_in*n_out {}",
        dw.len(),
        n_in * n_out
    );

    // dX[B,K] = dY[B,N] @ W^T[N,K]
    dx[..batch * n_in].fill(0.0);

    #[cfg(all(feature = "accelerate", target_os = "macos"))]
    unsafe {
        // dX = dY @ W^T => CblasTrans on B
        cblas_sgemm(
            101,          // CblasRowMajor
            111,          // CblasNoTrans (A = dY)
            112,          // CblasTrans (B = W^T)
            batch as i32, // M
            n_in as i32,  // N (output cols = n_in)
            n_out as i32, // K (shared dim = n_out)
            1.0,
            dy.as_ptr(),
            n_out as i32,
            w.as_ptr(),
            n_out as i32, // ldb = n_out (before transpose)
            0.0,
            dx.as_mut_ptr(),
            n_in as i32,
        );
    }

    #[cfg(all(
        feature = "gemm-blas",
        not(all(feature = "accelerate", target_os = "macos"))
    ))]
    unsafe {
        // dX = dY @ W^T
        gemm::gemm(
            batch,
            n_in,
            n_out,
            dx.as_mut_ptr(),
            1,             // dst col stride
            n_in as isize, // dst row stride
            false,
            dy.as_ptr(),
            1,              // lhs col stride
            n_out as isize, // lhs row stride
            w.as_ptr(),
            n_out as isize, // rhs col stride (transposed: was row)
            1,              // rhs row stride (transposed: was col)
            0.0,            // alpha (don't read dst)
            1.0,            // beta (scale product)
            false,
            false,
            false,
            gemm::Parallelism::None,
        );
    }

    #[cfg(not(any(
        all(feature = "accelerate", target_os = "macos"),
        feature = "gemm-blas"
    )))]
    {
        for row in 0..batch {
            let dy_off = row * n_out;
            let dx_off = row * n_in;
            for j in 0..n_out {
                let dv = dy[dy_off + j];
                for k in 0..n_in {
                    dx[dx_off + k] += dv * w[k * n_out + j];
                }
            }
        }
    }

    // dW[K,N] += X^T[K,B] @ dY[B,N]
    #[cfg(all(feature = "accelerate", target_os = "macos"))]
    unsafe {
        cblas_sgemm(
            101,
            112,          // CblasTrans (A = X^T)
            111,          // CblasNoTrans (B = dY)
            n_in as i32,  // M
            n_out as i32, // N
            batch as i32, // K
            1.0,
            x_saved.as_ptr(),
            n_in as i32, // lda = n_in (before transpose)
            dy.as_ptr(),
            n_out as i32,
            1.0, // beta = 1.0 (accumulate)
            dw.as_mut_ptr(),
            n_out as i32,
        );
    }

    #[cfg(all(
        feature = "gemm-blas",
        not(all(feature = "accelerate", target_os = "macos"))
    ))]
    unsafe {
        // dW += X^T @ dY
        gemm::gemm(
            n_in,
            n_out,
            batch,
            dw.as_mut_ptr(),
            1,              // dst col stride
            n_out as isize, // dst row stride
            true,           // accumulate
            x_saved.as_ptr(),
            n_in as isize, // lhs col stride (transposed X: rs and cs swapped)
            1,             // lhs row stride
            dy.as_ptr(),
            1,              // rhs col stride
            n_out as isize, // rhs row stride
            1.0,            // alpha (accumulate)
            1.0,            // beta
            false,
            false,
            false,
            gemm::Parallelism::None,
        );
    }

    #[cfg(not(any(
        all(feature = "accelerate", target_os = "macos"),
        feature = "gemm-blas"
    )))]
    {
        for row in 0..batch {
            let x_off = row * n_in;
            let dy_off = row * n_out;
            for k in 0..n_in {
                let xv = x_saved[x_off + k];
                let w_off = k * n_out;
                for j in 0..n_out {
                    dw[w_off + j] += xv * dy[dy_off + j];
                }
            }
        }
    }

    // dBias[N] += colsum(dY) — always scalar (tiny)
    if let Some(db) = db {
        for row in 0..batch {
            let dy_off = row * n_out;
            for j in 0..n_out {
                db[j] += dy[dy_off + j];
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sgemm_forward_identity() {
        let n = 4;
        let mut w = vec![0.0; n * n];
        for i in 0..n {
            w[i * n + i] = 1.0;
        }
        let x: Vec<f32> = (0..n).map(|i| (i + 1) as f32).collect();
        let mut y = vec![0.0; n];
        sgemm_forward(&mut y, &x, &w, None, 1, n, n);
        for (i, yi) in y.iter().enumerate().take(n) {
            assert!((*yi - (i + 1) as f32).abs() < 1e-6);
        }
    }

    #[test]
    fn test_sgemm_forward_with_bias() {
        let w = vec![1.0, 0.0, 0.0, 1.0];
        let x = vec![3.0, 4.0];
        let bias = vec![10.0, 20.0];
        let mut y = vec![0.0; 2];
        sgemm_forward(&mut y, &x, &w, Some(&bias), 1, 2, 2);
        assert!((y[0] - 13.0).abs() < 1e-6);
        assert!((y[1] - 24.0).abs() < 1e-6);
    }

    #[test]
    fn test_sgemm_backward_gradient() {
        let batch = 2;
        let n_in = 3;
        let n_out = 2;
        let w = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        let x = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0];
        let dy = vec![1.0, 1.0, 1.0, 1.0];
        let mut dx = vec![0.0; batch * n_in];
        let mut dw = vec![0.0; n_in * n_out];
        sgemm_backward(&mut dx, &mut dw, None, &dy, &x, &w, (batch, n_in, n_out));
        // dx[0] = dy[0] @ W^T row 0 = [1,1] @ [[1,3,5],[2,4,6]]^T col 0 = 1*1+1*2 = 3
        assert!((dx[0] - 3.0).abs() < 1e-5);
        assert!((dx[1] - 7.0).abs() < 1e-5);
        assert!((dx[2] - 11.0).abs() < 1e-5);
    }
}