MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / fast_symmetric_distance

Method fast_symmetric_distance

nodedb-codec/src/vector_quant/bbq.rs:275–284  ·  view source on GitHub ↗

Fast Hamming-based symmetric distance estimate. Uses the asymmetric corrective distance formula: approx = q_n² + v_n² − 2 · q_n · v_n · dot_estimate where `dot_estimate = 1 − 2·hamming/dim` maps the Hamming count to a normalised cosine-like similarity on {−1,+1} codes.

(&self, q: &BbqQuantized, v: &BbqQuantized)

Source from the content-addressed store, hash-verified

273 /// where `dot_estimate = 1 − 2·hamming/dim` maps the Hamming count to
274 /// a normalised cosine-like similarity on {−1,+1} codes.
275 fn fast_symmetric_distance(&self, q: &BbqQuantized, v: &BbqQuantized) -> f32 {
276 let q_bits = q.0.packed_bits();
277 let v_bits = v.0.packed_bits();
278 let ham = hamming_distance(q_bits, v_bits);
279 let dim = self.dim as f32;
280 let dot_estimate = 1.0 - 2.0 * ham as f32 / dim;
281 let q_n = q.0.header().residual_norm;
282 let v_n = v.0.header().residual_norm;
283 (q_n * q_n + v_n * v_n - 2.0 * q_n * v_n * dot_estimate).max(0.0)
284 }
285
286 /// Exact asymmetric L2 distance using the dequantized stored vector.
287 ///

Callers 1

Calls 3

packed_bitsMethod · 0.80
hamming_distanceFunction · 0.70
headerMethod · 0.45

Tested by 1