vortx 0.2.0

Cross-platform GPU tensor library with Rust.
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
use crate::shapes::TensorLayoutBuffers;
use crate::tensor::{AsTensorMut, AsTensorRef};
use khal::Shader;
use khal::backend::{GpuBackend, GpuBackendError, GpuPass};

// Use generated ShaderArgs from spirv_bindgen
use crate::shaders::linalg::{GemmNaive, GemmTiled};

/// Indicates if a matrix needs to be considered as-is or as its transpose when running a matrix
/// multiplication operation.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum MatrixMode {
    /// The matrix multiplication operation will operate with the normal matrix value (not transposed).
    Normal,
    /// The matrix multiplication operation will operate with the matrix's transpose.
    Transposed,
}

impl MatrixMode {
    /// Flips between transposed and non-transposed mode.
    pub fn transpose(&mut self) {
        match self {
            Self::Normal => *self = Self::Transposed,
            Self::Transposed => *self = Self::Normal,
        }
    }
}

/// Alternate name for `MatrixMode::Transposed` for conciseness when calling matrix multiplication.
pub const N: MatrixMode = MatrixMode::Normal;
/// Alternate name for `MatrixMode::Transposed` for conciseness when calling matrix multiplication.
pub const T: MatrixMode = MatrixMode::Transposed;

#[derive(Shader)]
/// Shader for computing the product of two matrices.
pub struct Gemm {
    /// The compute pipeline for `matrix1 * matrix2` (naive implementation).
    pub gemm_naive: GemmNaive,
    /// Optimized tiled GEMM using shared memory (64x64 tiles, 16x16 workgroups).
    pub gemm_tiled: GemmTiled,
}

// GemmArgs is now generated by spirv_bindgen from vortx_shaders::linalg::gemm

impl Gemm {
    /// Dispatches the matrix-vector multiplication variant indicated by the given `GemmVariant`.
    pub fn dispatch(
        &self,
        backend: &GpuBackend,
        #[cfg_attr(feature = "push_constants", allow(unused_variables))]
        shapes: &mut TensorLayoutBuffers,
        pass: &mut GpuPass,
        mut out: impl AsTensorMut<f32>,
        lhs: impl AsTensorRef<f32>,
        rhs: impl AsTensorRef<f32>,
    ) -> Result<(), GpuBackendError> {
        let mut out = out.as_tensor_mut();
        let lhs = lhs.as_tensor_ref();
        let rhs = rhs.as_tensor_ref();

        // Shapes of the mathematical operation being executed, independent of the potential artificial transpose
        // we’d apply for switching to a column-major equivalent of `m` and `v`.

        // TODO: handle broadcasting instead of canonicalizing right away.

        let shape_out = out.layout().canonicalize();
        let shape_lhs = lhs.layout().canonicalize();
        let shape_rhs = rhs.layout().canonicalize();

        assert_eq!(
            shape_out.size[0], shape_lhs.size[0],
            "incompatible gemm shapes: {:?} = {:?} x {:?}",
            shape_out, shape_lhs, shape_rhs
        );
        assert_eq!(
            shape_out.size[1], shape_lhs.size[1],
            "incompatible gemm shapes: {:?} = {:?} x {:?}",
            shape_out, shape_lhs, shape_rhs
        );
        assert_eq!(
            shape_lhs.size[0], shape_rhs.size[0],
            "incompatible gemm shapes: {:?} = {:?} x {:?}",
            shape_out, shape_lhs, shape_rhs
        );
        assert_eq!(
            shape_lhs.size[1], shape_rhs.size[1],
            "incompatible gemm shapes: {:?} = {:?} x {:?}",
            shape_out, shape_lhs, shape_rhs
        );
        assert_eq!(
            shape_out.size[2], shape_lhs.size[2],
            "incompatible gemm shapes: {:?} = {:?} x {:?}",
            shape_out, shape_lhs, shape_rhs
        );
        assert_eq!(
            shape_out.size[3], shape_rhs.size[3],
            "incompatible gemm shapes: {:?} = {:?} x {:?}",
            shape_out, shape_lhs, shape_rhs
        );
        assert_eq!(
            shape_lhs.size[3], shape_rhs.size[2],
            "incompatible gemm shapes: {:?} = {:?} x {:?}",
            shape_out, shape_lhs, shape_rhs
        );

        // println!("Multiplying {:?} = {:?} x {:?}", shape_out, shape_lhs, shape_rhs);

        // Use tiled kernel for matrices large enough to benefit from tiling
        // NOTE: we currently can't run the tiled version on WASM because of the non-uniform
        //       control flow limitation with barriers.
        let use_tiled = cfg!(not(target_arch = "wasm32"))
            && (shape_out.size[2] >= 32 || shape_out.size[3] >= 32);

        let grid = if use_tiled {
            Self::tiled_grid(&shape_out)
        } else {
            Self::naive_grid(&shape_out)
        };

        #[cfg(not(feature = "push_constants"))]
        {
            shapes.insert(backend, shape_out)?;
            shapes.insert(backend, shape_lhs)?;
            shapes.insert(backend, shape_rhs)?;
            let gpu_shape_out = shapes.get(shape_out).unwrap_or_else(|| unreachable!());
            let gpu_shape_lhs = shapes.get(shape_lhs).unwrap_or_else(|| unreachable!());
            let gpu_shape_rhs = shapes.get(shape_rhs).unwrap_or_else(|| unreachable!());
            let mut buf_out = out.buffer_mut();

            if use_tiled {
                self.gemm_tiled.call(
                    pass,
                    grid,
                    &gpu_shape_out.as_slice(),
                    &gpu_shape_lhs.as_slice(),
                    &gpu_shape_rhs.as_slice(),
                    &mut buf_out,
                    &lhs.buffer(),
                    &rhs.buffer(),
                )
            } else {
                self.gemm_naive.call(
                    pass,
                    grid,
                    &gpu_shape_out.as_slice(),
                    &gpu_shape_lhs.as_slice(),
                    &gpu_shape_rhs.as_slice(),
                    &mut buf_out,
                    &lhs.buffer(),
                    &rhs.buffer(),
                )
            }
        }

        #[cfg(feature = "push_constants")]
        {
            let mut buf_out = out.buffer_mut();

            if use_tiled {
                self.gemm_tiled.call(
                    pass,
                    grid,
                    &mut buf_out,
                    &lhs.buffer(),
                    &rhs.buffer(),
                    crate::shaders::linalg::Shapes3 {
                        shape_out: shape_out.into(),
                        shape_lhs: shape_lhs.into(),
                        shape_rhs: shape_rhs.into(),
                    },
                )
            } else {
                self.gemm_navive.call(
                    pass,
                    grid,
                    &mut buf_out,
                    &lhs.buffer(),
                    &rhs.buffer(),
                    crate::shaders::linalg::Shapes3 {
                        shape_out: shape_out.into(),
                        shape_lhs: shape_lhs.into(),
                        shape_rhs: shape_rhs.into(),
                    },
                )
            }
        }
    }

    /// Dispatches the naive GEMM kernel (for benchmarking).
    pub fn dispatch_naive(
        &self,
        backend: &GpuBackend,
        #[cfg_attr(feature = "push_constants", allow(unused_variables))]
        shapes: &mut TensorLayoutBuffers,
        pass: &mut GpuPass,
        mut out: impl AsTensorMut<f32>,
        lhs: impl AsTensorRef<f32>,
        rhs: impl AsTensorRef<f32>,
    ) -> Result<(), GpuBackendError> {
        let mut out = out.as_tensor_mut();
        let lhs = lhs.as_tensor_ref();
        let rhs = rhs.as_tensor_ref();

        let shape_out = out.layout().canonicalize();
        let shape_lhs = lhs.layout().canonicalize();
        let shape_rhs = rhs.layout().canonicalize();

        let grid = Self::naive_grid(&shape_out);

        #[cfg(not(feature = "push_constants"))]
        {
            shapes.insert(backend, shape_out)?;
            shapes.insert(backend, shape_lhs)?;
            shapes.insert(backend, shape_rhs)?;
            let mut buf_out = out.buffer_mut();

            self.gemm_naive.call(
                pass,
                grid,
                &shapes.get(shape_out).unwrap().as_slice(),
                &shapes.get(shape_lhs).unwrap().as_slice(),
                &shapes.get(shape_rhs).unwrap().as_slice(),
                &mut buf_out,
                &lhs.buffer(),
                &rhs.buffer(),
            )
        }

        #[cfg(feature = "push_constants")]
        {
            let mut buf_out = out.buffer_mut();

            self.gemm_naive.call(
                pass,
                grid,
                &mut buf_out,
                &lhs.buffer(),
                &rhs.buffer(),
                crate::shaders::linalg::Shapes3 {
                    shape_out: shape_out.into(),
                    shape_lhs: shape_lhs.into(),
                    shape_rhs: shape_rhs.into(),
                },
            )
        }
    }

    /// Dispatches the tiled GEMM kernel (for benchmarking).
    pub fn dispatch_tiled(
        &self,
        backend: &GpuBackend,
        #[cfg_attr(feature = "push_constants", allow(unused_variables))]
        shapes: &mut TensorLayoutBuffers,
        pass: &mut GpuPass,
        mut out: impl AsTensorMut<f32>,
        lhs: impl AsTensorRef<f32>,
        rhs: impl AsTensorRef<f32>,
    ) -> Result<(), GpuBackendError> {
        let mut out = out.as_tensor_mut();
        let lhs = lhs.as_tensor_ref();
        let rhs = rhs.as_tensor_ref();

        let shape_out = out.layout().canonicalize();
        let shape_lhs = lhs.layout().canonicalize();
        let shape_rhs = rhs.layout().canonicalize();

        let grid = Self::tiled_grid(&shape_out);

        #[cfg(not(feature = "push_constants"))]
        {
            shapes.insert(backend, shape_out)?;
            shapes.insert(backend, shape_lhs)?;
            shapes.insert(backend, shape_rhs)?;
            let mut buf_out = out.buffer_mut();

            self.gemm_tiled.call(
                pass,
                grid,
                &shapes.get(shape_out).unwrap().as_slice(),
                &shapes.get(shape_lhs).unwrap().as_slice(),
                &shapes.get(shape_rhs).unwrap().as_slice(),
                &mut buf_out,
                &lhs.buffer(),
                &rhs.buffer(),
            )
        }

        #[cfg(feature = "push_constants")]
        {
            let mut buf_out = out.buffer_mut();

            self.gemm_tiled.call(
                pass,
                grid,
                &mut buf_out,
                &lhs.buffer(),
                &rhs.buffer(),
                crate::shaders::linalg::Shapes3 {
                    shape_out: shape_out.into(),
                    shape_lhs: shape_lhs.into(),
                    shape_rhs: shape_rhs.into(),
                },
            )
        }
    }

    fn naive_grid(shape_out: &crate::shapes::TensorLayout) -> [u32; 3] {
        [shape_out.size[3], shape_out.size[2], shape_out.size[1]]
    }

    fn tiled_grid(shape_out: &crate::shapes::TensorLayout) -> [u32; 3] {
        const TILE_M: u32 = 64;
        const TILE_N: u32 = 64;
        const WG_M: u32 = 16;
        const WG_N: u32 = 16;

        let num_wg_m = shape_out.size[2].div_ceil(TILE_M);
        let num_wg_n = shape_out.size[3].div_ceil(TILE_N);

        [num_wg_n * WG_N, num_wg_m * WG_M, shape_out.size[1]]
    }
}

#[cfg(test)]
mod test {
    use crate::shapes::TensorLayoutBuffers;
    use crate::tensor::Tensor;
    use approx::assert_relative_eq;
    use khal::backend::{Backend, Encoder, GpuBackend, WebGpu};
    use khal::{BufferUsages, Shader};
    use nalgebra::{DMatrix, DVector};
    use wgpu::{Features, Limits};

    #[futures_test::test]
    #[serial_test::serial]
    async fn gpu_gemm_webgpu() {
        let webgpu = WebGpu::new(Features::default(), Limits::default())
            .await
            .unwrap();
        let backend = GpuBackend::WebGpu(webgpu);
        gpu_gemm_generic(&backend).await;
    }

    #[cfg(feature = "cpu")]
    #[futures_test::test]
    async fn gpu_gemm_cpu() {
        gpu_gemm_generic(&GpuBackend::Cpu).await;
    }

    #[cfg(feature = "cuda")]
    #[futures_test::test]
    async fn gpu_gemm_cuda() {
        let cuda = GpuBackend::Cuda(khal::backend::cuda::Cuda::new(0).unwrap());
        gpu_gemm_generic(&cuda).await;
    }

    #[cfg(feature = "metal")]
    #[futures_test::test]
    #[serial_test::serial]
    async fn gpu_gemm_metal() {
        let metal = GpuBackend::Metal(khal::backend::metal::Metal::new().unwrap());
        gpu_gemm_generic(&metal).await;
    }

    async fn gpu_gemm_generic(backend: &GpuBackend) {
        let gemm = super::Gemm::from_backend(backend).unwrap();

        let mut shapes = TensorLayoutBuffers::new(backend);

        const NROWS: u32 = 256;
        const NCOLS: u32 = 256;

        let m_cpu = DMatrix::<f32>::new_random(NROWS as usize, NCOLS as usize);
        let v_cpu = DVector::<f32>::new_random(NCOLS as usize);
        let lhs_cpu = DVector::<f32>::zeros(NROWS as usize);
        let mut gpu_result = DVector::<f32>::zeros(NROWS as usize);

        let m = Tensor::matrix_from_na(backend, &m_cpu, BufferUsages::STORAGE).unwrap();
        let mut v = Tensor::vector(backend, &v_cpu, BufferUsages::STORAGE).unwrap();
        v.unsqueeze(1);
        let mut result = Tensor::vector(
            backend,
            &lhs_cpu,
            BufferUsages::STORAGE | BufferUsages::COPY_SRC,
        )
        .unwrap();
        result.unsqueeze(1);

        // for variant in [
        //     GemmVariant::Gemm,
        //     GemmVariant::GemmTr,
        //     GemmVariant::GemmFast,
        //     GemmVariant::GemmTrFast,
        // ] {
        //     println!("Checking variant: {:?}", variant);
        let t0 = std::time::Instant::now();
        let mut encoder = backend.begin_encoding();
        let mut pass = encoder.begin_pass("gemm", None);
        // let modes = match variant {
        //     GemmVariant::GemmFast | GemmVariant::Gemm | GemmVariant::GemmNaive => {
        //         (super::N, super::N)
        //     }
        //     GemmVariant::GemmTrFast | GemmVariant::GemmTr | GemmVariant::GemmTrNaive => {
        //         (super::T, super::N)
        //     }
        // };
        gemm.dispatch(
            backend,
            &mut shapes,
            &mut pass,
            &mut result,
            &m,
            &v,
            // modes.0,
            // modes.1,
        )
        .unwrap();
        drop(pass); // Ensure the pass is ended before the encoder is borrowed again.

        backend.submit(encoder).unwrap();
        backend.synchronize().unwrap();
        println!("GEMM before read: {}", t0.elapsed().as_secs_f32());
        backend
            .slow_read_buffer(result.buffer(), gpu_result.as_mut_slice())
            .await
            .unwrap();
        println!("GEMM time: {}", t0.elapsed().as_secs_f32());

        let cpu_result = &m_cpu * &v_cpu;
        // let cpu_result = match variant {
        //     GemmVariant::Gemm | GemmVariant::GemmFast | GemmVariant::GemmNaive => {
        //         &m_cpu * &v_cpu
        //     }
        //     GemmVariant::GemmTr | GemmVariant::GemmTrFast | GemmVariant::GemmTrNaive => {
        //         m_cpu.tr_mul(&v_cpu)
        //     }
        // };

        // NOTE: don't use assert_relative_eq so it doesn't print out the whole matrices
        //       when it fails (it tends to break rustrover tests integration).
        // assert!(relative_eq!(gpu_result, cpu_result, epsilon = 1.0e-3));
        assert_relative_eq!(gpu_result, cpu_result, epsilon = 1.0e-3);
        // }
    }
}