gemmkit 0.1.2

A clean, extensible, high-performance GEMM (general matrix multiply) engine
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
//! Real float (f32/f64) GEMM through the public dispatched API: shapes x layouts x
//! alpha/beta, workspace reuse, parallel/serial bit-identity, and gemv shapes

use crate::common::*;
use gemmkit::{MatMut, MatRef, Parallelism, Workspace, gemm, gemm_unchecked};

/// `m` values (crossed with fixed `k`/`n` lists and a few big squares below) spanning
/// the AVX-512F f32 tile boundaries (`MR=32`, `NR=12`) and general blocking edges
///
/// Under `GEMMKIT_FAST_TEST` this drops to the classes that matter (0, 1, a
/// sub-tile, the `NR`/`MR` boundary and boundary+1, a multi-tile, a large multi-block)
/// and cuts the redundant middle (2, 11, 16, 31, 48, 100); the `k`/`n` lists and the
/// big squares are unaffected either way
fn dims() -> Vec<(usize, usize, usize)> {
    let vals: &[usize] = if fast_test() {
        &[0, 1, 5, 12, 13, 32, 33, 64, 257]
    } else {
        &[0, 1, 2, 5, 11, 12, 13, 16, 31, 32, 33, 48, 64, 100, 257]
    };
    let mut out = Vec::new();
    // A representative cross-section: the full m x k x n cross product is huge
    for &m in vals {
        for &k in &[1usize, 2, 7, 32, 65] {
            for &n in &[1usize, 11, 12, 13, 64] {
                out.push((m, k, n));
            }
        }
    }
    for &s in &[128usize, 200, 384] {
        out.push((s, s, s));
    }
    out
}

/// f32 over the [`dims`] shape spread, row-major A, col-major B, and both C layouts,
/// `alpha=1, beta=0`, against the f64 reference
#[test]
fn correctness_f32_layouts() {
    for (m, k, n) in dims() {
        for &lc in &[Layout::Row, Layout::Col] {
            run_case::<f32>(
                m,
                k,
                n,
                Layout::Row,
                Layout::Col,
                lc,
                1.0,
                0.0,
                Parallelism::Serial,
            );
        }
    }
}

/// f64 twin of [`correctness_f32_layouts`], with A/B layouts swapped (col-major A,
/// row-major B) so the 2 float types do not share identical operand orientations
#[test]
fn correctness_f64_layouts() {
    for (m, k, n) in dims() {
        for &lc in &[Layout::Row, Layout::Col] {
            run_case::<f64>(
                m,
                k,
                n,
                Layout::Col,
                Layout::Row,
                lc,
                1.0,
                0.0,
                Parallelism::Serial,
            );
        }
    }
}

/// General (non-unit, non-trivial) strides on every operand: [`Layout::GeneralPad`]
/// throughout for f32, and mixed with row/col for f64, over a small shape set
#[test]
fn correctness_general_strides() {
    for (m, k, n) in [(7, 9, 5), (32, 32, 32), (33, 17, 19), (64, 64, 64)] {
        run_case::<f32>(
            m,
            k,
            n,
            Layout::GeneralPad,
            Layout::GeneralPad,
            Layout::GeneralPad,
            1.0,
            0.0,
            Parallelism::Serial,
        );
        run_case::<f64>(
            m,
            k,
            n,
            Layout::GeneralPad,
            Layout::Row,
            Layout::Col,
            1.0,
            0.0,
            Parallelism::Serial,
        );
    }
}

/// An alpha/beta sweep (both signs, zero, and 1) crossed with a few shapes, f32
/// row-major throughout and f64 col-major throughout
#[test]
fn correctness_alpha_beta() {
    let combos = [
        (0.0f64, 0.0),
        (0.0, 1.0),
        (0.0, 2.5),
        (1.0, 0.0),
        (1.0, 1.0),
        (1.0, -1.5),
        (2.0, 0.0),
        (-0.5, 3.0),
    ];
    for (m, k, n) in [(5, 6, 7), (32, 40, 24), (64, 31, 48)] {
        for &(al, be) in &combos {
            run_case::<f32>(
                m,
                k,
                n,
                Layout::Row,
                Layout::Row,
                Layout::Row,
                al as f32,
                be as f32,
                Parallelism::Serial,
            );
            run_case::<f64>(
                m,
                k,
                n,
                Layout::Col,
                Layout::Col,
                Layout::Col,
                al,
                be,
                Parallelism::Serial,
            );
        }
    }
}

/// beta==0 must never read C: seed C with NaN and check the output is finite.
/// `kernel::float` and `kernel::mixed` each have their own `BetaStatus::Zero` branch,
/// so this runs f32/f64 and (under `half`) f16/bf16, not just f32: a branch that
/// loaded C anyway would propagate the NaN (`0*NaN == NaN`) into a failed finite
/// check. `(40,33,28)` sits inside the small-matrix blocking shortcut (both dims
/// under the 64-element default); `(64,16,96)` exceeds it (`n=96 > 64`), covering
/// the general blocking path's `Zero` branch too
#[test]
fn beta_zero_does_not_read_c() {
    fn check<T: Elem>() {
        for (m, k, n) in [(40usize, 33, 28), (64, 16, 96)] {
            let a = Mat::<T>::rand(m, k, 7 + m as u64);
            let b = Mat::<T>::rand(k, n, 9 + n as u64);
            let cref = reference(
                &a,
                &b,
                &Mat {
                    v: vec![T::from_f64(0.0); m * n],
                    rows: m,
                    cols: n,
                },
                1.0,
                0.0,
            );
            let (abuf, rsa, csa) = build_view(&a, Layout::Col);
            let (bbuf, rsb, csb) = build_view(&b, Layout::Col);
            let mut cbuf = vec![T::from_f64(f64::NAN); m * n];
            gemm(
                T::from_f64(1.0),
                MatRef::new(&abuf, m, k, rsa, csa),
                MatRef::new(&bbuf, k, n, rsb, csb),
                T::from_f64(0.0),
                MatMut::from_col_major(&mut cbuf, m, n),
                Parallelism::Serial,
            );
            assert_accurate(&cbuf, 1, m as isize, m, n, &cref, &a, &b, k, "beta=0 NaN C");
        }
    }
    check::<f32>();
    check::<f64>();
    #[cfg(feature = "half")]
    {
        check::<gemmkit::f16>();
        check::<gemmkit::bf16>();
    }
}

/// `gemm_with` (`workspace_alloc.rs` only checks it allocates nothing on reuse, not
/// that the result is correct) must match `gemm` numerically; reuse 1 `Workspace`
/// across 2 calls so both the cold and warm paths are checked
#[test]
fn workspace_reuse_matches_allocating() {
    fn check<T: Elem>() {
        let (m, k, n) = (96, 65, 72);
        let a = Mat::<T>::rand(m, k, 0x7A + m as u64);
        let b = Mat::<T>::rand(k, n, 0x7B + n as u64);
        let c0 = Mat::<T>::rand(m, n, 0x7C + k as u64);
        let (abuf, rsa, csa) = build_view(&a, Layout::Col);
        let (bbuf, rsb, csb) = build_view(&b, Layout::Col);
        let (cbase, rsc, csc) = build_view(&c0, Layout::Col);
        let (al, be) = (T::from_f64(0.5), T::from_f64(0.25));

        let mut c_ref = cbase.clone();
        gemm(
            al,
            MatRef::new(&abuf, m, k, rsa, csa),
            MatRef::new(&bbuf, k, n, rsb, csb),
            be,
            MatMut::new(&mut c_ref, m, n, rsc, csc),
            Parallelism::Serial,
        );
        let mut ws = Workspace::new();
        for _ in 0..2 {
            let mut c_ws = cbase.clone();
            gemmkit::gemm_with(
                &mut ws,
                al,
                MatRef::new(&abuf, m, k, rsa, csa),
                MatRef::new(&bbuf, k, n, rsb, csb),
                be,
                MatMut::new(&mut c_ws, m, n, rsc, csc),
                Parallelism::Serial,
            );
            assert!(
                c_ref
                    .iter()
                    .zip(&c_ws)
                    .all(|(x, y)| x.to_f64().to_bits() == y.to_f64().to_bits()),
                "gemm_with != gemm"
            );
        }
    }
    check::<f32>();
    check::<f64>();
}

/// Serial and parallel runs land on identical bits, for every thread count, on
/// col-major operands. Float add is not associative, so this holds only because
/// `blocking()` derives `mc`/`kc`/`nc` from shape and cache topology alone, never the
/// thread count, so every run reduces in the same fixed order; the actual contract
/// (see `driver.rs` and the `accumulate_tile` seam) is reproducibility under a fixed
/// config, not bitwise serial-vs-parallel identity. The exact check stays as the
/// strongest net against an accidental reduction-order divergence (a race, a
/// thread-count-dependent tail bug); relax to determinism + tolerance only if blocking
/// is ever made parallelism-dependent on purpose (e.g. split-K). This caveat applies to
/// every `parallel_equals_serial_*` test in this file
#[test]
fn parallel_equals_serial_bit_identical() {
    for (m, k, n) in [
        (64, 64, 64),
        (200, 130, 175),
        (384, 96, 320),
        (256, 257, 129),
    ] {
        for &(al, be) in &[(1.0f64, 0.0), (0.7, 1.3)] {
            let a = Mat::<f32>::rand(m, k, 0xABC + m as u64);
            let b = Mat::<f32>::rand(k, n, 0xDEF + n as u64);
            let c0 = Mat::<f32>::rand(m, n, 0x123 + k as u64);
            let (abuf, rsa, csa) = build_view(&a, Layout::Col);
            let (bbuf, rsb, csb) = build_view(&b, Layout::Col);
            let (cbase, rsc, csc) = build_view(&c0, Layout::Col);

            let mut c_serial = cbase.clone();
            let mut c_par = cbase.clone();
            gemm(
                al as f32,
                MatRef::new(&abuf, m, k, rsa, csa),
                MatRef::new(&bbuf, k, n, rsb, csb),
                be as f32,
                MatMut::new(&mut c_serial, m, n, rsc, csc),
                Parallelism::Serial,
            );
            for threads in [2usize, 4, 8, 16] {
                c_par.copy_from_slice(&cbase);
                gemm(
                    al as f32,
                    MatRef::new(&abuf, m, k, rsa, csa),
                    MatRef::new(&bbuf, k, n, rsb, csb),
                    be as f32,
                    MatMut::new(&mut c_par, m, n, rsc, csc),
                    Parallelism::Rayon(threads),
                );
                assert_eq!(
                    c_serial, c_par,
                    "serial != parallel({threads}) for {m}x{k}x{n} a={al} b={be}"
                );
            }
        }
    }
}

/// The row-major-A twin of [`parallel_equals_serial_bit_identical`] (same
/// thread-independent-blocking caveat): `rsa != 1` routes A through per-row-block
/// packing, which schedules jobs via `packed_block_grain`'s whole-row-block grain
/// instead of the column-major case's fine-grain cursor. Sizes are chosen so the
/// row-block count interacts differently with each thread count, reaching both the
/// undivided whole-block grain and its split fallback
#[test]
fn parallel_equals_serial_row_major_a() {
    for (m, k, n) in [(200, 130, 175), (384, 96, 320), (256, 64, 200)] {
        for &(al, be) in &[(1.0f64, 0.0), (0.7, 1.3)] {
            let a = Mat::<f32>::rand(m, k, 0xA11 + m as u64);
            let b = Mat::<f32>::rand(k, n, 0xB22 + n as u64);
            let c0 = Mat::<f32>::rand(m, n, 0xC33 + k as u64);
            let (abuf, rsa, csa) = build_view(&a, Layout::Row); // rsa=k, not 1: triggers packing
            let (bbuf, rsb, csb) = build_view(&b, Layout::Col);
            let (cbase, rsc, csc) = build_view(&c0, Layout::Col);

            let mut c_serial = cbase.clone();
            let mut c_par = cbase.clone();
            gemm(
                al as f32,
                MatRef::new(&abuf, m, k, rsa, csa),
                MatRef::new(&bbuf, k, n, rsb, csb),
                be as f32,
                MatMut::new(&mut c_serial, m, n, rsc, csc),
                Parallelism::Serial,
            );
            for threads in [2usize, 4, 8, 16] {
                c_par.copy_from_slice(&cbase);
                gemm(
                    al as f32,
                    MatRef::new(&abuf, m, k, rsa, csa),
                    MatRef::new(&bbuf, k, n, rsb, csb),
                    be as f32,
                    MatMut::new(&mut c_par, m, n, rsc, csc),
                    Parallelism::Rayon(threads),
                );
                assert_eq!(
                    c_serial, c_par,
                    "row-major A: serial != parallel({threads}) for {m}x{k}x{n} a={al} b={be}"
                );
            }
        }
    }
}

/// `set_shared_lhs_mnk(1)` opens the shared-A-pack gate for any nonzero `m*n*k`, so
/// the shared pre-pack path (1 pack per row-block, read by every worker) runs for
/// every thread count here and must still land on the same bits as serial. All 3
/// shapes stay well under the real default gate (6M on aarch64, 8B elsewhere), so
/// this is otherwise the only coverage this path gets; since bit-identity is expected
/// whether the gate is open or closed, forcing it open cannot disturb concurrently
/// running tests. Row-major A (`rsa != 1`) is what makes A pack in the first place
#[test]
fn shared_lhs_a_bit_identical() {
    let prev = gemmkit::tuning::shared_lhs_mnk();
    gemmkit::tuning::set_shared_lhs_mnk(1); // open the gate for every shape below
    for (m, k, n) in [(200, 130, 175), (384, 96, 320), (256, 64, 200)] {
        let a = Mat::<f32>::rand(m, k, 0xA1 + m as u64);
        let b = Mat::<f32>::rand(k, n, 0xB2 + n as u64);
        let c0 = Mat::<f32>::rand(m, n, 0xC3 + k as u64);
        let (abuf, rsa, csa) = build_view(&a, Layout::Row); // rsa=k, not 1: triggers packing
        let (bbuf, rsb, csb) = build_view(&b, Layout::Col);
        let (cbase, rsc, csc) = build_view(&c0, Layout::Col);

        let mut c_ser = cbase.clone();
        gemm(
            0.9,
            MatRef::new(&abuf, m, k, rsa, csa),
            MatRef::new(&bbuf, k, n, rsb, csb),
            0.4,
            MatMut::new(&mut c_ser, m, n, rsc, csc),
            Parallelism::Serial,
        );
        for t in [2usize, 4, 8, 16] {
            let mut c_par = cbase.clone();
            gemm(
                0.9,
                MatRef::new(&abuf, m, k, rsa, csa),
                MatRef::new(&bbuf, k, n, rsb, csb),
                0.4,
                MatMut::new(&mut c_par, m, n, rsc, csc),
                Parallelism::Rayon(t),
            );
            assert_eq!(
                c_ser, c_par,
                "shared-A: serial != parallel({t}) for {m}x{k}x{n}"
            );
        }
    }
    gemmkit::tuning::set_shared_lhs_mnk(prev);
}

/// `gemm_unchecked` accepts a negative row stride for A (the safe `MatRef` surface
/// cannot express one): point the base at A's last physical row and walk backwards
/// with `rs = -rs`. C is written in ordinary forward row order, so output row `i`
/// ends up holding the product for A's physical row `m-1-i`; the check below reads
/// the f64 reference the same way
#[test]
fn negative_strides_unchecked() {
    let (m, k, n) = (12, 9, 7);
    let a = Mat::<f64>::rand(m, k, 5);
    let b = Mat::<f64>::rand(k, n, 6);
    let cref = reference(
        &a,
        &b,
        &Mat {
            v: vec![0.0; m * n],
            rows: m,
            cols: n,
        },
        1.0,
        0.0,
    );

    let (abuf, rsa, csa) = build_view(&a, Layout::Row); // rsa == k, csa == 1
    let (bbuf, rsb, csb) = build_view(&b, Layout::Col);
    let mut cbuf = vec![0.0f64; m * n];

    unsafe {
        let a_last = abuf.as_ptr().add(((m - 1) as isize * rsa) as usize);
        let c_ptr = cbuf.as_mut_ptr();
        gemm_unchecked(
            m,
            k,
            n,
            1.0,
            a_last,
            -rsa,
            csa,
            bbuf.as_ptr(),
            rsb,
            csb,
            0.0,
            c_ptr,
            n as isize,
            1,
            Parallelism::Serial,
        );
    }
    // C[i,j] holds sum_k A[m-1-i,k]*B[k,j]; index the reference the same way
    for i in 0..m {
        for j in 0..n {
            let got = cbuf[i * n + j];
            let exp = cref[(m - 1 - i) * n + j];
            assert!(
                (got - exp).abs() <= 1e-10 * (1.0 + exp.abs()),
                "neg stride mismatch"
            );
        }
    }
}

// gemv shapes (n == 1 or m == 1, routed through gemm's gemv fast path)

/// `n == 1` (mat*vec) and `m == 1` (vec*mat) shapes, including a `k == 1` degenerate
/// case and the `m == n == 1` pure dot product, whose single row has both strides equal
/// to 1 and so fits the axpy and the dot classification at once, across every A/B layout
/// combination, alpha/beta both nonzero
#[test]
fn gemv_shapes() {
    for &(m, k, n) in &[
        (64, 40, 1),
        (1, 40, 64),
        (100, 1, 1),
        (1, 1, 100),
        (255, 129, 1),
        (1, 129, 1),
    ] {
        for &la in &[Layout::Row, Layout::Col] {
            for &lb in &[Layout::Row, Layout::Col] {
                run_case::<f32>(m, k, n, la, lb, Layout::Col, 1.3, -0.4, Parallelism::Serial);
                run_case::<f64>(m, k, n, la, lb, Layout::Row, 0.5, 2.0, Parallelism::Serial);
            }
        }
    }
}