|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +//! BBQ kernel benches: the fused path against a reconstruct-and-measure |
| 4 | +//! baseline. |
| 5 | +//! |
| 6 | +//! The baseline models the shape of the pre-fusion rerank pass — decode the |
| 7 | +//! prepared payload into a `Vec<f32>` per candidate, then reconstruct each |
| 8 | +//! dimension — so the allocation and pass counts are comparable. It is an |
| 9 | +//! approximation of that path, not a copy of it: it uses a fixed `1/√dim` scale |
| 10 | +//! in place of the candidate's stored corrective factor, and reads the sign bits |
| 11 | +//! at a fixed header offset. It measures pass and allocation shape, not the |
| 12 | +//! codec's arithmetic. |
| 13 | +//! |
| 14 | +//! Fixtures are built once per thread, outside the timed region, so only the |
| 15 | +//! kernel loop is measured. |
| 16 | +//! |
| 17 | +//! Run with: cargo bench -p nodedb-vector --bench bbq_kernel |
| 18 | +
|
| 19 | +use fluxbench::bench; |
| 20 | +use fluxbench::prelude::*; |
| 21 | +use std::hint::black_box; |
| 22 | + |
| 23 | +use nodedb_vector::rerank::codec::{PreparedQuery, RerankCodec}; |
| 24 | +use nodedb_vector::rerank::codecs::BbqRerank; |
| 25 | + |
| 26 | +/// Installs fluxbench's tracking allocator so the harness reports heap bytes and |
| 27 | +/// allocation counts per benchmark: the fused path shows zero allocations per |
| 28 | +/// candidate, the baseline shows one `Vec<f32>`. |
| 29 | +#[global_allocator] |
| 30 | +static ALLOC: fluxbench::TrackingAllocator = fluxbench::TrackingAllocator; |
| 31 | + |
| 32 | +const OVERSAMPLE: u8 = 4; |
| 33 | +const CANDIDATES: usize = 256; |
| 34 | + |
| 35 | +fn det_vec(i: usize, dim: usize) -> Vec<f32> { |
| 36 | + (0..dim) |
| 37 | + .map(|j| (((i * 31 + j) % 100) as f32 / 100.0) - 0.5) |
| 38 | + .collect() |
| 39 | +} |
| 40 | + |
| 41 | +/// Trained codec, prepared query, and 256 encoded candidates for `dim`. |
| 42 | +fn setup(dim: usize) -> (BbqRerank, PreparedQuery, Vec<Vec<u8>>) { |
| 43 | + let vecs: Vec<Vec<f32>> = (0..CANDIDATES).map(|i| det_vec(i, dim)).collect(); |
| 44 | + let refs: Vec<&[f32]> = vecs.iter().map(|v| v.as_slice()).collect(); |
| 45 | + let mut codec = BbqRerank::new(dim, OVERSAMPLE); |
| 46 | + codec.train(&refs).expect("train"); |
| 47 | + let prepared = codec.prepare_query(&vecs[0]).expect("prepare_query"); |
| 48 | + let encoded: Vec<Vec<u8>> = vecs |
| 49 | + .iter() |
| 50 | + .map(|v| codec.encode(v).expect("encode")) |
| 51 | + .collect(); |
| 52 | + (codec, prepared, encoded) |
| 53 | +} |
| 54 | + |
| 55 | +thread_local! { |
| 56 | + static STATE_128: (BbqRerank, PreparedQuery, Vec<Vec<u8>>) = setup(128); |
| 57 | + static STATE_768: (BbqRerank, PreparedQuery, Vec<Vec<u8>>) = setup(768); |
| 58 | +} |
| 59 | + |
| 60 | +/// The baseline: decode the prepared payload to `Vec<f32>` per candidate, then |
| 61 | +/// reconstruct each dimension. `encoded` carries a 32-byte quant header before |
| 62 | +/// the sign bits, and the reconstruction uses a fixed `1/√dim` scale rather than |
| 63 | +/// the candidate's stored `residual_norm` — see the module docs. |
| 64 | +fn unfused_l2(payload: &[u8], encoded: &[u8], dim: usize) -> f32 { |
| 65 | + let centered: Vec<f32> = payload[4..] |
| 66 | + .as_chunks::<4>() |
| 67 | + .0 |
| 68 | + .iter() |
| 69 | + .map(|b| f32::from_le_bytes(*b)) |
| 70 | + .collect(); |
| 71 | + let scale = 1.0f32 / (dim as f32).sqrt(); |
| 72 | + let mut acc = 0.0f32; |
| 73 | + for i in 0..dim { |
| 74 | + let bit = (encoded[32 + i / 8] >> (7 - (i % 8))) & 1; |
| 75 | + let recon = if bit != 0 { scale } else { -scale }; |
| 76 | + let d = centered[i] - recon; |
| 77 | + acc += d * d; |
| 78 | + } |
| 79 | + acc.sqrt() |
| 80 | +} |
| 81 | + |
| 82 | +#[bench(id = "bbq_fused_128", group = "bbq_kernel")] |
| 83 | +fn bbq_fused_128(b: &mut Bencher) { |
| 84 | + STATE_128.with(|(codec, prepared, encoded)| { |
| 85 | + b.iter(|| { |
| 86 | + let mut acc = 0.0f32; |
| 87 | + for e in encoded { |
| 88 | + acc += codec.distance_prepared(prepared, e).expect("distance"); |
| 89 | + } |
| 90 | + black_box(acc) |
| 91 | + }); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +#[bench(id = "bbq_unfused_128", group = "bbq_kernel")] |
| 96 | +fn bbq_unfused_128(b: &mut Bencher) { |
| 97 | + STATE_128.with(|(_codec, prepared, encoded)| { |
| 98 | + let payload = match prepared { |
| 99 | + PreparedQuery::Bytes(b) => b.as_slice(), |
| 100 | + _ => panic!("bbq prepared form is Bytes"), |
| 101 | + }; |
| 102 | + b.iter(|| { |
| 103 | + let mut acc = 0.0f32; |
| 104 | + for e in encoded { |
| 105 | + acc += unfused_l2(payload, e, 128); |
| 106 | + } |
| 107 | + black_box(acc) |
| 108 | + }); |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +#[bench(id = "bbq_fused_768", group = "bbq_kernel")] |
| 113 | +fn bbq_fused_768(b: &mut Bencher) { |
| 114 | + STATE_768.with(|(codec, prepared, encoded)| { |
| 115 | + b.iter(|| { |
| 116 | + let mut acc = 0.0f32; |
| 117 | + for e in encoded { |
| 118 | + acc += codec.distance_prepared(prepared, e).expect("distance"); |
| 119 | + } |
| 120 | + black_box(acc) |
| 121 | + }); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +#[bench(id = "bbq_unfused_768", group = "bbq_kernel")] |
| 126 | +fn bbq_unfused_768(b: &mut Bencher) { |
| 127 | + STATE_768.with(|(_codec, prepared, encoded)| { |
| 128 | + let payload = match prepared { |
| 129 | + PreparedQuery::Bytes(b) => b.as_slice(), |
| 130 | + _ => panic!("bbq prepared form is Bytes"), |
| 131 | + }; |
| 132 | + b.iter(|| { |
| 133 | + let mut acc = 0.0f32; |
| 134 | + for e in encoded { |
| 135 | + acc += unfused_l2(payload, e, 768); |
| 136 | + } |
| 137 | + black_box(acc) |
| 138 | + }); |
| 139 | + }); |
| 140 | +} |
| 141 | + |
| 142 | +fn main() { |
| 143 | + if let Err(e) = fluxbench::run() { |
| 144 | + eprintln!("Error: {e}"); |
| 145 | + std::process::exit(1); |
| 146 | + } |
| 147 | +} |
0 commit comments