|
| 1 | +//! EWA-SYRK crossover bench — kill-or-justify the "3DGS projection is a |
| 2 | +//! BLAS workload in disguise" premise of `3DGS-EWA-SYRK-BLAS-MKL-plan.md`. |
| 3 | +//! |
| 4 | +//! # The question |
| 5 | +//! |
| 6 | +//! The plan proposes routing the EWA covariance sandwich `Σ' = M·Σ·Mᵀ` |
| 7 | +//! through a batched SYRK/GEMM backend (native / MKL / OpenBLAS / AMX). But |
| 8 | +//! the sandwich matrices are **3×3** (world→camera) and **2×3** (camera→ |
| 9 | +//! image), and `splat3d::project` already runs them 16-wide via the |
| 10 | +//! `crate::simd` SoA polyfill. Batched *tiny* GEMM is exactly the regime |
| 11 | +//! where CPU BLAS loses to a fused SIMD unroll (per-call overhead dominates; |
| 12 | +//! there is no efficient CPU batched-3×3 SYRK — that pattern is a GPU one). |
| 13 | +//! |
| 14 | +//! This bench measures it instead of asserting it. |
| 15 | +//! |
| 16 | +//! # What it compares (same `M·N·Mᵀ` math, three kernel shapes) |
| 17 | +//! |
| 18 | +//! - `scalar` — the hand-unrolled upper-triangle `spd3::sandwich`, per element. |
| 19 | +//! - `simd_x16` — the shipped `sandwich_x16` SoA path (the renderer's kernel). |
| 20 | +//! - `gemm_shape` — two dense 3×3 matmuls per element (`M·N` then `·Mᵀ`), |
| 21 | +//! with no SoA fusion. This models the **shape a CPU BLAS |
| 22 | +//! backend imposes** — one small dense GEMM per matrix. |
| 23 | +//! |
| 24 | +//! `gemm_shape` runs **in-process, with no FFI**, so it is a *lower bound* |
| 25 | +//! on a real `cblas`/MKL path: a genuine BLAS call adds argument marshalling |
| 26 | +//! + dispatch overhead on top. If `gemm_shape` already loses to `simd_x16`, |
| 27 | +//! the BLAS backend loses by more. |
| 28 | +//! |
| 29 | +//! Plus `project_batch` end-to-end throughput at the same batch sizes, so |
| 30 | +//! the sandwich cost is seen in proportion to the full pipeline. |
| 31 | +//! |
| 32 | +//! Sweep: 1 024 / 100 000 / 1 000 000 (all exact multiples of 16, no tail). |
| 33 | +//! |
| 34 | +//! Caveat (honest scope): this measures the *per-element* kernel shape. The |
| 35 | +//! one genuinely batchable step is `W·Σ·Wᵀ`, where `W` (the camera view |
| 36 | +//! rotation) is shared across all gaussians — a shared-`W` batched GEMM is a |
| 37 | +//! steelman left as a follow-up. The per-image Jacobian `J` differs per |
| 38 | +//! gaussian, so `J·Σ·Jᵀ` does not batch that way. |
| 39 | +
|
| 40 | +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; |
| 41 | +use ndarray::hpc::splat3d::{project_batch, sandwich, sandwich_x16, Camera, Gaussian3D, GaussianBatch, ProjectedBatch, Spd3}; |
| 42 | + |
| 43 | +const SIZES: [usize; 3] = [1_024, 100_000, 1_000_000]; |
| 44 | + |
| 45 | +#[inline] |
| 46 | +fn rng(s: &mut u32) -> f32 { |
| 47 | + *s ^= *s << 13; |
| 48 | + *s ^= *s >> 17; |
| 49 | + *s ^= *s << 5; |
| 50 | + (*s as f32) / (u32::MAX as f32) |
| 51 | +} |
| 52 | + |
| 53 | +/// `n` distinct SPD pairs via `from_scale_quat` (entry-wise different across |
| 54 | +/// all 6 SoA channels so the SIMD transpose does real work). |
| 55 | +fn build_spd_pairs(n: usize) -> (Vec<Spd3>, Vec<Spd3>) { |
| 56 | + let mut st = 0x1234_5678u32; |
| 57 | + let mut ms = Vec::with_capacity(n); |
| 58 | + let mut ns = Vec::with_capacity(n); |
| 59 | + for _ in 0..n { |
| 60 | + let sm = [0.3 + 1.5 * rng(&mut st), 0.3 + 1.5 * rng(&mut st), 0.3 + 1.5 * rng(&mut st)]; |
| 61 | + let sn = [0.3 + 1.5 * rng(&mut st), 0.3 + 1.5 * rng(&mut st), 0.3 + 1.5 * rng(&mut st)]; |
| 62 | + let mut qm = [rng(&mut st) - 0.5, rng(&mut st) - 0.5, rng(&mut st) - 0.5, rng(&mut st) - 0.5]; |
| 63 | + let mut qn = [rng(&mut st) - 0.5, rng(&mut st) - 0.5, rng(&mut st) - 0.5, rng(&mut st) - 0.5]; |
| 64 | + normalize_quat(&mut qm); |
| 65 | + normalize_quat(&mut qn); |
| 66 | + ms.push(Spd3::from_scale_quat(sm, qm)); |
| 67 | + ns.push(Spd3::from_scale_quat(sn, qn)); |
| 68 | + } |
| 69 | + (ms, ns) |
| 70 | +} |
| 71 | + |
| 72 | +fn normalize_quat(q: &mut [f32; 4]) { |
| 73 | + let n = (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]).sqrt().max(1e-12); |
| 74 | + for v in q.iter_mut() { |
| 75 | + *v /= n; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// `M·N·Mᵀ` via two dense 3×3 matmuls — the shape a per-matrix BLAS call |
| 80 | +/// imposes (no upper-triangle fusion, no SoA). Output upper triangle, off- |
| 81 | +/// diagonals averaged to match `sandwich`'s symmetrisation. |
| 82 | +#[inline] |
| 83 | +fn sandwich_gemm_shape(m: &Spd3, n: &Spd3) -> Spd3 { |
| 84 | + let a = m.to_rows(); |
| 85 | + let b = n.to_rows(); |
| 86 | + let mut t = [[0.0f32; 3]; 3]; |
| 87 | + for i in 0..3 { |
| 88 | + for j in 0..3 { |
| 89 | + t[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j]; |
| 90 | + } |
| 91 | + } |
| 92 | + // R = T · Mᵀ (Mᵀ[k][j] = A[j][k]) |
| 93 | + let mut r = [[0.0f32; 3]; 3]; |
| 94 | + for i in 0..3 { |
| 95 | + for j in 0..3 { |
| 96 | + r[i][j] = t[i][0] * a[j][0] + t[i][1] * a[j][1] + t[i][2] * a[j][2]; |
| 97 | + } |
| 98 | + } |
| 99 | + Spd3::new(r[0][0], 0.5 * (r[0][1] + r[1][0]), 0.5 * (r[0][2] + r[2][0]), r[1][1], 0.5 * (r[1][2] + r[2][1]), r[2][2]) |
| 100 | +} |
| 101 | + |
| 102 | +fn build_gaussians(n: usize) -> GaussianBatch { |
| 103 | + let mut st = 0x9E37_79B9u32; |
| 104 | + let mut batch = GaussianBatch::with_capacity(n); |
| 105 | + for _ in 0..n { |
| 106 | + let mut g = Gaussian3D::unit(); |
| 107 | + g.mean = [4.0 * rng(&mut st) - 2.0, 4.0 * rng(&mut st) - 2.0, 1.0 + 5.0 * rng(&mut st)]; |
| 108 | + g.scale = [0.05 + 0.2 * rng(&mut st); 3]; |
| 109 | + g.quat = [1.0, 0.0, 0.0, 0.0]; |
| 110 | + g.opacity = 0.5 + 0.5 * rng(&mut st); |
| 111 | + batch.push(g); |
| 112 | + } |
| 113 | + batch |
| 114 | +} |
| 115 | + |
| 116 | +fn bench_sandwich_paths(c: &mut Criterion) { |
| 117 | + let mut grp = c.benchmark_group("ewa_sandwich_paths"); |
| 118 | + grp.sample_size(20); |
| 119 | + for &n in &SIZES { |
| 120 | + let (ms, ns) = build_spd_pairs(n); |
| 121 | + grp.throughput(Throughput::Elements(n as u64)); |
| 122 | + |
| 123 | + grp.bench_with_input(BenchmarkId::new("scalar", n), &n, |b, &n| { |
| 124 | + let mut out = vec![Spd3::ZERO; n]; |
| 125 | + b.iter(|| { |
| 126 | + for i in 0..n { |
| 127 | + out[i] = sandwich(black_box(&ms[i]), black_box(&ns[i])); |
| 128 | + } |
| 129 | + black_box(&out[n - 1]); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + grp.bench_with_input(BenchmarkId::new("simd_x16", n), &n, |b, &n| { |
| 134 | + let mut out = vec![Spd3::ZERO; n]; |
| 135 | + b.iter(|| { |
| 136 | + let mc = ms.chunks_exact(16); |
| 137 | + let nc = ns.chunks_exact(16); |
| 138 | + let oc = out.chunks_exact_mut(16); |
| 139 | + for ((mm, nn), oo) in mc.zip(nc).zip(oc) { |
| 140 | + let ma: &[Spd3; 16] = mm.try_into().unwrap(); |
| 141 | + let na: &[Spd3; 16] = nn.try_into().unwrap(); |
| 142 | + let oa: &mut [Spd3; 16] = oo.try_into().unwrap(); |
| 143 | + sandwich_x16(black_box(ma), black_box(na), oa); |
| 144 | + } |
| 145 | + black_box(&out[n - 1]); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + grp.bench_with_input(BenchmarkId::new("gemm_shape", n), &n, |b, &n| { |
| 150 | + let mut out = vec![Spd3::ZERO; n]; |
| 151 | + b.iter(|| { |
| 152 | + for i in 0..n { |
| 153 | + out[i] = sandwich_gemm_shape(black_box(&ms[i]), black_box(&ns[i])); |
| 154 | + } |
| 155 | + black_box(&out[n - 1]); |
| 156 | + }); |
| 157 | + }); |
| 158 | + } |
| 159 | + grp.finish(); |
| 160 | +} |
| 161 | + |
| 162 | +fn bench_project_batch(c: &mut Criterion) { |
| 163 | + let mut grp = c.benchmark_group("ewa_project_batch"); |
| 164 | + grp.sample_size(10); |
| 165 | + let cam = Camera::identity_at_origin(1920, 1080); |
| 166 | + for &n in &SIZES { |
| 167 | + let batch = build_gaussians(n); |
| 168 | + let mut out = ProjectedBatch::with_capacity(batch.capacity); |
| 169 | + grp.throughput(Throughput::Elements(n as u64)); |
| 170 | + grp.bench_with_input(BenchmarkId::from_parameter(n), &n, |b, _| { |
| 171 | + b.iter(|| { |
| 172 | + project_batch(black_box(&batch), black_box(&cam), &mut out); |
| 173 | + black_box(out.len); |
| 174 | + }); |
| 175 | + }); |
| 176 | + } |
| 177 | + grp.finish(); |
| 178 | +} |
| 179 | + |
| 180 | +criterion_group!(ewa_syrk, bench_sandwich_paths, bench_project_batch); |
| 181 | +criterion_main!(ewa_syrk); |
0 commit comments