Skip to main content

gemmbench/
gemmbench.rs

1//! Which side should a small f32 GEMM run on: `gemm_nt`'s own arbitration,
2//! or pinned to the host?
3//!
4//! Written to answer that for the LTX LoRA branch, whose shapes are narrow
5//! (rank 128) against a wide activation. The generic `GemmNt` probe will send
6//! them to the device, where they queue behind the q4tp projection they are
7//! standing beside — the arithmetic is a fraction of the base GEMM's, the
8//! latency is not.
9//!
10//!   cargo run --release -p cortiq-engine --example gemmbench
11
12fn main() {
13    unsafe { std::env::set_var("CMF_GPU", "1") };
14    let cases = [(384usize, 4096usize, 128usize), (384, 128, 4096), (384, 16384, 128), (384, 128, 16384)];
15    for (n, k, m) in cases {
16        let x = vec![0.01f32; n * k];
17        let w = vec![0.02f32; m * k];
18        let mut y = vec![0f32; n * m];
19        // warm
20        cortiq_engine::fcd_ops::gemm_nt(&x, &w, &mut y, n, k, m, None);
21        let t = std::time::Instant::now();
22        for _ in 0..10 { cortiq_engine::fcd_ops::gemm_nt(&x, &w, &mut y, n, k, m, None); }
23        let d = t.elapsed().as_secs_f64() / 10.0;
24        let g = 2.0 * n as f64 * k as f64 * m as f64 / d / 1e9;
25        let t2 = std::time::Instant::now();
26        for _ in 0..10 {
27            cortiq_engine::gpu::cpu_scope(|| cortiq_engine::fcd_ops::gemm_nt(&x, &w, &mut y, n, k, m, None));
28        }
29        let d2 = t2.elapsed().as_secs_f64() / 10.0;
30        let g2 = 2.0 * n as f64 * k as f64 * m as f64 / d2 / 1e9;
31        println!("{n}x{k}x{m}:  default {:.2} ms ({g:.0} GF/s)   cpu_scope {:.2} ms ({g2:.0} GF/s)", d * 1e3, d2 * 1e3);
32    }
33}