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
use super::Cpu;

#[cfg(feature = "cblas")]
use cblas_sys::{
    cblas_sgemm as sgemm, cblas_sgemv as sgemv, CblasColMajor as ColMajor, CblasNoTrans as NoTr,
    CblasRowMajor as RowMajor, CblasTrans as Tr,
};

pub trait Transpose {
    type T: Transpose<T = Self>;
}

impl<const M: usize, const N: usize> Transpose for [[f32; N]; M] {
    type T = [[f32; M]; N];
}

impl<Inner: Transpose, const B: usize> Transpose for [Inner; B] {
    type T = [Inner::T; B];
}

pub trait MatMul<A: Transpose, B: Transpose, C: Transpose> {
    fn mm(a: &A, b: &B, c: &mut C);
    fn mm_at(a: &A::T, b: &B, c: &mut C);
    fn mm_bt(a: &A, b: &B::T, c: &mut C);
    fn mm_atct(a: &A::T, b: &B, c: &mut C::T);
}

impl<const M: usize, const K: usize, const N: usize>
    MatMul<[[f32; K]; M], [[f32; N]; K], [[f32; N]; M]> for Cpu
{
    /// Matmul
    fn mm(a: &[[f32; K]; M], b: &[[f32; N]; K], c: &mut [[f32; N]; M]) {
        let a = a.as_ptr() as *const f32;
        let b = b.as_ptr() as *const f32;
        let c = c.as_mut_ptr() as *mut f32;

        #[cfg(not(feature = "cblas"))]
        unsafe {
            matrixmultiply::sgemm(
                M, K, N, 1.0, a, K as isize, 1, b, N as isize, 1, 1.0, c, N as isize, 1,
            )
        }

        #[cfg(feature = "cblas")]
        unsafe {
            let (m, n, k) = (M as libc::c_int, N as libc::c_int, K as libc::c_int);
            sgemm(RowMajor, NoTr, NoTr, m, n, k, 1.0, a, k, b, n, 1.0, c, n)
        }
    }

    /// Matmul, a is transposed.
    fn mm_at(a: &[[f32; M]; K], b: &[[f32; N]; K], c: &mut [[f32; N]; M]) {
        let a = a.as_ptr() as *const f32;
        let b = b.as_ptr() as *const f32;
        let c = c.as_mut_ptr() as *mut f32;

        #[cfg(not(feature = "cblas"))]
        unsafe {
            matrixmultiply::sgemm(
                M, K, N, 1.0, a, 1, M as isize, b, N as isize, 1, 1.0, c, N as isize, 1,
            )
        }

        #[cfg(feature = "cblas")]
        unsafe {
            let (m, n, k) = (M as libc::c_int, N as libc::c_int, K as libc::c_int);
            sgemm(RowMajor, Tr, NoTr, m, n, k, 1.0, a, m, b, n, 1.0, c, n)
        }
    }

    /// Matmul, b is transposed
    fn mm_bt(a: &[[f32; K]; M], b: &[[f32; K]; N], c: &mut [[f32; N]; M]) {
        let a = a.as_ptr() as *const f32;
        let b = b.as_ptr() as *const f32;
        let c = c.as_mut_ptr() as *mut f32;

        #[cfg(not(feature = "cblas"))]
        unsafe {
            matrixmultiply::sgemm(
                M, K, N, 1.0, a, K as isize, 1, b, 1, K as isize, 1.0, c, N as isize, 1,
            )
        }

        #[cfg(feature = "cblas")]
        unsafe {
            let (m, n, k) = (M as libc::c_int, N as libc::c_int, K as libc::c_int);
            sgemm(RowMajor, NoTr, Tr, m, n, k, 1.0, a, k, b, k, 1.0, c, n)
        }
    }

    /// Matmul, a and c are transposed
    fn mm_atct(a: &[[f32; M]; K], b: &[[f32; N]; K], c: &mut [[f32; M]; N]) {
        let a = a.as_ptr() as *const f32;
        let b = b.as_ptr() as *const f32;
        let c = c.as_mut_ptr() as *mut f32;

        #[cfg(not(feature = "cblas"))]
        unsafe {
            matrixmultiply::sgemm(
                M, K, N, 1.0, a, 1, M as isize, b, N as isize, 1, 1.0, c, 1, M as isize,
            )
        }

        #[cfg(feature = "cblas")]
        unsafe {
            let (m, n, k) = (M as libc::c_int, N as libc::c_int, K as libc::c_int);
            sgemm(ColMajor, NoTr, Tr, m, n, k, 1.0, a, m, b, n, 1.0, c, m)
        }
    }
}

impl<const BATCH: usize, const M: usize, const K: usize, const N: usize>
    MatMul<[[[f32; K]; M]; BATCH], [[f32; N]; K], [[[f32; N]; M]; BATCH]> for Cpu
where
    Self: MatMul<[[f32; K]; M], [[f32; N]; K], [[f32; N]; M]>,
{
    /// Broadcast `b` `BATCH` times.
    fn mm(a: &[[[f32; K]; M]; BATCH], b: &[[f32; N]; K], c: &mut [[[f32; N]; M]; BATCH]) {
        for i in 0..BATCH {
            Self::mm(&a[i], b, &mut c[i]);
        }
    }

    /// Broadcast `b` `BATCH` times.
    fn mm_at(a: &[[[f32; M]; K]; BATCH], b: &[[f32; N]; K], c: &mut [[[f32; N]; M]; BATCH]) {
        for i in 0..BATCH {
            Self::mm_at(&a[i], b, &mut c[i]);
        }
    }

    /// Broadcast `b` `BATCH` times.
    fn mm_bt(a: &[[[f32; K]; M]; BATCH], b: &[[f32; K]; N], c: &mut [[[f32; N]; M]; BATCH]) {
        for i in 0..BATCH {
            Self::mm_bt(&a[i], b, &mut c[i]);
        }
    }

    /// Broadcast `b` `BATCH` times.
    fn mm_atct(a: &[[[f32; M]; K]; BATCH], b: &[[f32; N]; K], c: &mut [[[f32; M]; N]; BATCH]) {
        for i in 0..BATCH {
            Self::mm_atct(&a[i], b, &mut c[i]);
        }
    }
}

impl<const BATCH: usize, const M: usize, const K: usize, const N: usize>
    MatMul<[[[f32; K]; M]; BATCH], [[[f32; N]; K]; BATCH], [[f32; N]; M]> for Cpu
where
    Self: MatMul<[[f32; K]; M], [[f32; N]; K], [[f32; N]; M]>,
{
    /// Broadcast `c` `BATCH` times.
    fn mm(a: &[[[f32; K]; M]; BATCH], b: &[[[f32; N]; K]; BATCH], c: &mut [[f32; N]; M]) {
        for i in 0..BATCH {
            Self::mm(&a[i], &b[i], c);
        }
    }

    /// Broadcast `c` `BATCH` times.
    fn mm_at(a: &[[[f32; M]; K]; BATCH], b: &[[[f32; N]; K]; BATCH], c: &mut [[f32; N]; M]) {
        for i in 0..BATCH {
            Self::mm_at(&a[i], &b[i], c);
        }
    }

    /// Broadcast `c` `BATCH` times.
    fn mm_bt(a: &[[[f32; K]; M]; BATCH], b: &[[[f32; K]; N]; BATCH], c: &mut [[f32; N]; M]) {
        for i in 0..BATCH {
            Self::mm_bt(&a[i], &b[i], c);
        }
    }

    /// Broadcast `c` `BATCH` times.
    fn mm_atct(a: &[[[f32; M]; K]; BATCH], b: &[[[f32; N]; K]; BATCH], c: &mut [[f32; M]; N]) {
        for i in 0..BATCH {
            Self::mm_atct(&a[i], &b[i], c);
        }
    }
}

impl<const BATCH: usize, A, B, C> MatMul<[A; BATCH], [B; BATCH], [C; BATCH]> for Cpu
where
    Self: MatMul<A, B, C>,
    A: Transpose,
    B: Transpose,
    C: Transpose,
    [A; BATCH]: Transpose<T = [A::T; BATCH]>,
    [B; BATCH]: Transpose<T = [B::T; BATCH]>,
    [C; BATCH]: Transpose<T = [C::T; BATCH]>,
{
    /// Batched matmul
    fn mm(a: &[A; BATCH], b: &[B; BATCH], c: &mut [C; BATCH]) {
        for i in 0..BATCH {
            Self::mm(&a[i], &b[i], &mut c[i]);
        }
    }

    /// Batched matmul
    fn mm_at(a: &[A::T; BATCH], b: &[B; BATCH], c: &mut [C; BATCH]) {
        for i in 0..BATCH {
            Self::mm_at(&a[i], &b[i], &mut c[i]);
        }
    }

    /// Batched matmul
    fn mm_bt(a: &[A; BATCH], b: &[B::T; BATCH], c: &mut [C; BATCH]) {
        for i in 0..BATCH {
            Self::mm_bt(&a[i], &b[i], &mut c[i]);
        }
    }

    /// Batched matmul
    fn mm_atct(a: &[A::T; BATCH], b: &[B; BATCH], c: &mut [C::T; BATCH]) {
        for i in 0..BATCH {
            Self::mm_atct(&a[i], &b[i], &mut c[i]);
        }
    }
}

pub trait MatMulOp<A: Transpose, B: Transpose, C: Transpose>:
    MatMul<A, B, C> + MatMul<C, B::T, A> + MatMul<A::T, C, B>
{
}

impl<A: Transpose, B: Transpose, C: Transpose> MatMulOp<A, B, C> for Cpu where
    Self: MatMul<A, B, C> + MatMul<C, B::T, A> + MatMul<A::T, C, B>
{
}

impl Cpu {
    /// vector matrix multiply `c += a * b`
    pub fn vm<const K: usize, const N: usize>(a: &[f32; K], b: &[[f32; N]; K], c: &mut [f32; N]) {
        let a = a.as_ptr();
        let b = b.as_ptr() as *const f32;
        let c = c.as_mut_ptr();

        #[cfg(not(feature = "cblas"))]
        unsafe {
            const M: usize = 1;
            matrixmultiply::sgemm(
                M, K, N, 1.0, a, K as isize, 1, b, N as isize, 1, 1.0, c, N as isize, 1,
            )
        }

        #[cfg(feature = "cblas")]
        unsafe {
            let (n, k) = (N as libc::c_int, K as libc::c_int);
            sgemv(ColMajor, NoTr, n, k, 1.0, b, n, a, 1, 1.0, c, 1)
        }
    }

    /// vector matrix multiply `c += a * trans(b)`
    pub fn vm_bt<const K: usize, const N: usize>(
        a: &[f32; K],
        b_t: &[[f32; K]; N],
        c: &mut [f32; N],
    ) {
        let a = a.as_ptr();
        let b_t = b_t.as_ptr() as *const f32;
        let c = c.as_mut_ptr();

        #[cfg(not(feature = "cblas"))]
        unsafe {
            const M: usize = 1;
            matrixmultiply::sgemm(
                M, K, N, 1.0, a, K as isize, 1, b_t, 1, K as isize, 1.0, c, N as isize, 1,
            )
        }

        #[cfg(feature = "cblas")]
        unsafe {
            let (n, k) = (N as libc::c_int, K as libc::c_int);
            sgemv(RowMajor, NoTr, n, k, 1.0, b_t, k, a, 1, 1.0, c, 1)
        }
    }

    /// vector vector
    pub fn vv<const M: usize, const N: usize>(a: &[f32; M], b: &[f32; N], c: &mut [[f32; N]; M]) {
        const K: usize = 1;
        let a = a.as_ptr();
        let b = b.as_ptr();
        let c = c.as_mut_ptr() as *mut f32;

        #[cfg(not(feature = "cblas"))]
        unsafe {
            matrixmultiply::sgemm(
                M, K, N, 1.0, a, K as isize, 1, b, N as isize, 1, 1.0, c, N as isize, 1,
            )
        }

        #[cfg(feature = "cblas")]
        unsafe {
            let (m, n, k) = (M as libc::c_int, N as libc::c_int, K as libc::c_int);
            sgemm(RowMajor, NoTr, NoTr, m, n, k, 1.0, a, k, b, n, 1.0, c, n)
        }
    }
}

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

    #[test]
    fn test_valid_matmuls() {
        type A = [[f32; 2]; 5];
        type B = [[f32; 3]; 2];
        type C = [[f32; 3]; 5];

        // normal matmul
        let _ = <Cpu as MatMul<A, B, C>>::mm;

        // batch 3d matmul
        let _ = <Cpu as MatMul<[A; 10], [B; 10], [C; 10]>>::mm;

        // batch 4d matmul
        let _ = <Cpu as MatMul<[[A; 10]; 12], [[B; 10]; 12], [[C; 10]; 12]>>::mm;

        // broadcast matmul
        let _ = <Cpu as MatMul<[A; 10], B, [C; 10]>>::mm;
        let _ = <Cpu as MatMul<[A; 10], [B; 10], C>>::mm;

        // transposed
        let _ = <Cpu as MatMul<C, <B as Transpose>::T, A>>::mm;
        let _ = <Cpu as MatMul<<A as Transpose>::T, C, B>>::mm;
    }

    #[test]
    fn test_matmul() {
        let x = [
            [1.0, 2.0, 3.0],
            [4.0, 5.0, 6.0],
            [7.0, 8.0, 9.0],
            [10.0, 11.0, 12.0],
        ];
        let x_t = [
            [1.0, 4.0, 7.0, 10.0],
            [2.0, 5.0, 8.0, 11.0],
            [3.0, 6.0, 9.0, 12.0],
        ];
        let y = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let y_t = [[1.0, 3.0, 5.0], [2.0, 4.0, 6.0]];
        let expected = [[22.0, 28.0], [49.0, 64.0], [76.0, 100.0], [103.0, 136.0]];

        let mut out = [[0.0; 2]; 4];
        Cpu::mm(&x, &y, &mut out);
        assert_close(&out, &expected);

        let mut out = [[0.0; 2]; 4];
        Cpu::mm_at(&x_t, &y, &mut out);
        assert_close(&out, &expected);

        let mut out = [[0.0; 2]; 4];
        Cpu::mm_bt(&x, &y_t, &mut out);
        assert_close(&out, &expected);
    }

    #[test]
    fn test_vecmul() {
        let x = [1.0, 2.0, 3.0];
        let y = [[1.0, 2.0], [0.5, 1.0], [1.0 / 3.0, 1.0]];
        let y_t = [[1.0, 0.5, 1.0 / 3.0], [2.0, 1.0, 1.0]];
        let expected = [3.0, 7.0];

        let mut out = [0.0; 2];
        Cpu::vm(&x, &y, &mut out);
        assert_close(&out, &expected);

        let mut out = [0.0; 2];
        Cpu::vm_bt(&x, &y_t, &mut out);
        assert_close(&out, &expected);
    }

    #[test]
    fn test_vecvec() {
        let x = [1.0, 2.0, 3.0];
        let y = [-1.0, 0.5, -1.0 / 3.0, 0.25];

        let mut out = [[0.0; 4]; 3];
        Cpu::vv(&x, &y, &mut out);
        assert_close(
            &out,
            &[
                [-1.0, 0.5, -1.0 / 3.0, 0.25],
                [-2.0, 1.0, -2.0 / 3.0, 0.5],
                [-3.0, 1.5, -1.0, 0.75],
            ],
        );

        let mut out = [[0.0; 3]; 4];
        Cpu::vv(&y, &x, &mut out);
        assert_close(
            &out,
            &[
                [-1.0, -2.0, -3.0],
                [0.5, 1.0, 1.5],
                [-1.0 / 3.0, -2.0 / 3.0, -1.0],
                [0.25, 0.5, 0.75],
            ],
        );
    }
}