perf(agent): unranked BM25 scores and a top-k merge — same rankings, 4-5x faster
Fusion min-max normalises over every keyword match, so hybrid_search asked BM25 for a ranked list of the whole corpus: a hash insert per posting, then a sort of every match, then the merge sorted every candidate again to keep k. - BM25Index::scores returns every match unsorted, accumulated in a dense array (contributions are strictly positive, so zero means untouched). search() is built on it with the bounded heap. - merge_vector_keyword partitions out its top k (select_nth) and orders only those, with the same score-then-id order. - Both hybrid paths use scores(). Rankings are identical (equivalence tests for both changes). p50 0.24 -> 0.07 ms (1K), 2.1 -> 0.49 ms (10K), 23 -> 4.65 ms (100K). The harness gains --fusion-study, which measured the alternative — capping the keyword pool — and found it changes the top-10 for most queries (overlap 0.83-0.92, different #1 for 10-35%) for only a 2x saving. Not adopted. Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
f15bf2eb22
commit
390a2e3836
@@ -369,10 +369,101 @@ fn bench_end_to_end(n: usize, json: &mut Vec<serde_json::Value>) {
|
||||
}));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fusion study: does capping the keyword candidate pool change the ranking?
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// `hybrid_search` min-max normalises each signal over the candidates it is
|
||||
/// given. The vector stage supplies a pool of `max(8k, 64)`; the keyword stage
|
||||
/// supplies *every* matching record, which is what now dominates query time.
|
||||
/// This compares the current fusion with one whose keyword stage is capped to
|
||||
/// a pool, reporting how often the final top-k agree and what each costs.
|
||||
fn fusion_study(n: usize) {
|
||||
use clawhdf5_agent::bm25::BM25Index;
|
||||
use clawhdf5_agent::hybrid::merge_vector_keyword;
|
||||
|
||||
let data = make_dataset(n, 0xE2E ^ n as u64);
|
||||
let mut rng = Rng(7);
|
||||
let texts: Vec<String> = (0..n)
|
||||
.map(|i| text_for(data.cluster_of[i], i, &mut rng))
|
||||
.collect();
|
||||
let query_texts: Vec<String> = data
|
||||
.query_cluster
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, c)| text_for(*c, i, &mut rng))
|
||||
.collect();
|
||||
let bm25 = BM25Index::build(&texts, &vec![0u8; n]);
|
||||
let index = HnswIndex::build_with_metric(
|
||||
&data.vectors,
|
||||
HNSW_M,
|
||||
HNSW_EF_CONSTRUCTION,
|
||||
DistanceMetric::Cosine,
|
||||
);
|
||||
|
||||
let vec_pool = (K * 8).max(64);
|
||||
println!("\n### Fusion study, N = {n} (k = {K}, weights 0.7 / 0.3, vector pool {vec_pool})\n");
|
||||
println!(
|
||||
"| keyword pool | top-{K} overlap vs full | identical top-{K} | same #1 | keyword+merge µs |"
|
||||
);
|
||||
println!("|---:|---:|---:|---:|---:|");
|
||||
|
||||
let fuse = |q: usize, kw_pool: usize| -> (Vec<usize>, Duration) {
|
||||
let vec_scores: Vec<(usize, f32)> = index
|
||||
.search(&data.queries[q], vec_pool, vec_pool)
|
||||
.into_iter()
|
||||
.map(|(id, d)| (id, 1.0 - d))
|
||||
.collect();
|
||||
let t = Instant::now();
|
||||
let kw = bm25.search(&query_texts[q], kw_pool);
|
||||
let merged = merge_vector_keyword(vec_scores, kw, 0.7, 0.3, K);
|
||||
let took = t.elapsed();
|
||||
(merged.into_iter().map(|(id, _)| id).collect(), took)
|
||||
};
|
||||
|
||||
let full: Vec<(Vec<usize>, Duration)> = (0..N_QUERIES).map(|q| fuse(q, n)).collect();
|
||||
let full_time: Duration = full.iter().map(|f| f.1).sum();
|
||||
println!(
|
||||
"| all ({n}) | 1.0000 | 100.0% | 100.0% | {:.0} |",
|
||||
micros(full_time) / N_QUERIES as f64
|
||||
);
|
||||
for pool in [vec_pool, vec_pool * 4, 1000] {
|
||||
if pool >= n {
|
||||
continue;
|
||||
}
|
||||
let (mut overlap, mut identical, mut same_first) = (0usize, 0usize, 0usize);
|
||||
let mut time = Duration::ZERO;
|
||||
for (q, (want, _)) in full.iter().enumerate() {
|
||||
let (got, took) = fuse(q, pool);
|
||||
time += took;
|
||||
overlap += got.iter().filter(|id| want.contains(id)).count();
|
||||
identical += usize::from(&got == want);
|
||||
same_first += usize::from(got.first() == want.first());
|
||||
}
|
||||
println!(
|
||||
"| {pool} | {:.4} | {:.1}% | {:.1}% | {:.0} |",
|
||||
overlap as f64 / (K * N_QUERIES) as f64,
|
||||
100.0 * identical as f64 / N_QUERIES as f64,
|
||||
100.0 * same_first as f64 / N_QUERIES as f64,
|
||||
micros(time) / N_QUERIES as f64
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let full = args.iter().any(|a| a == "--full");
|
||||
let ann_only = args.iter().any(|a| a == "--ann-only");
|
||||
if args.iter().any(|a| a == "--fusion-study") {
|
||||
for &n in if full {
|
||||
&[10_000, 100_000][..]
|
||||
} else {
|
||||
&[10_000][..]
|
||||
} {
|
||||
fusion_study(n);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if args.iter().any(|a| a == "--uniform") {
|
||||
UNIFORM.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
println!("(uniform random data)");
|
||||
|
||||
Reference in New Issue
Block a user