oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
//! # ANN Index Parameter Optimizer
//!
//! Hyperparameter search and Pareto-optimal selection for HNSW, IVF, and IVF+PQ indices.
//!
//! This module implements grid-expansion-based hyperparameter search for approximate nearest
//! neighbour (ANN) index construction, along with Pareto-front analysis over the
//! recall vs. QPS trade-off space.

use std::collections::HashMap;

// ─── Index type ────────────────────────────────────────────────────────────────

/// Identifies which ANN index family is being optimised.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum IndexType {
    /// Hierarchical Navigable Small World graph.
    HNSW,
    /// Inverted File index.
    IVF,
    /// Inverted File index with Product Quantisation.
    IVFPQ,
    /// Flat (brute-force) baseline.
    Flat,
}

// ─── Parameter structs ─────────────────────────────────────────────────────────

/// HNSW construction and search parameters.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct HnswParams {
    /// Number of bi-directional links per element (M).
    pub m: usize,
    /// Size of the candidate list during graph construction.
    pub ef_construction: usize,
    /// Size of the candidate list during search.
    pub ef_search: usize,
}

impl HnswParams {
    /// Create a new HNSW parameter set.
    pub fn new(m: usize, ef_construction: usize, ef_search: usize) -> Self {
        Self {
            m,
            ef_construction,
            ef_search,
        }
    }
}

/// IVF construction and search parameters.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IvfParams {
    /// Number of Voronoi cells (cluster centroids).
    pub n_lists: usize,
    /// Number of cells visited per query.
    pub n_probes: usize,
}

impl IvfParams {
    /// Create a new IVF parameter set.
    pub fn new(n_lists: usize, n_probes: usize) -> Self {
        Self { n_lists, n_probes }
    }
}

// ─── IndexParams ───────────────────────────────────────────────────────────────

/// Unified parameter envelope for any supported index type.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum IndexParams {
    /// HNSW parameters.
    Hnsw(HnswParams),
    /// IVF parameters.
    Ivf(IvfParams),
}

impl IndexParams {
    /// Retrieve underlying HNSW params if this is an HNSW variant.
    pub fn as_hnsw(&self) -> Option<&HnswParams> {
        match self {
            Self::Hnsw(p) => Some(p),
            _ => None,
        }
    }

    /// Retrieve underlying IVF params if this is an IVF variant.
    pub fn as_ivf(&self) -> Option<&IvfParams> {
        match self {
            Self::Ivf(p) => Some(p),
            _ => None,
        }
    }
}

// ─── Optimisation target ───────────────────────────────────────────────────────

/// The objective the optimiser should maximise.
#[derive(Debug, Clone)]
pub enum OptimizationTarget {
    /// Maximise recall@10 without regard to throughput.
    MaxRecall,
    /// Maximise queries per second without regard to recall.
    MaxQPS,
    /// Weighted combination: `recall_weight * recall + (1 - recall_weight) * norm_qps`.
    BalancedRecallQPS {
        /// Weight in [0, 1] given to recall (remainder goes to QPS).
        recall_weight: f64,
    },
}

// ─── Benchmark point ──────────────────────────────────────────────────────────

/// A single measurement pairing a parameter configuration with observed metrics.
#[derive(Debug, Clone)]
pub struct BenchmarkPoint {
    /// Parameter configuration that was benchmarked.
    pub params: IndexParams,
    /// Fraction of the true 10-nearest-neighbours retrieved (0.0 – 1.0).
    pub recall_at_10: f64,
    /// Throughput in queries per second.
    pub qps: f64,
    /// Wall-clock index build time in milliseconds.
    pub build_time_ms: u64,
}

impl BenchmarkPoint {
    /// Construct a new benchmark observation.
    pub fn new(params: IndexParams, recall_at_10: f64, qps: f64, build_time_ms: u64) -> Self {
        Self {
            params,
            recall_at_10,
            qps,
            build_time_ms,
        }
    }
}

// ─── IndexOptimiser ───────────────────────────────────────────────────────────

/// Hyperparameter search engine for ANN index configurations.
///
/// Collects benchmark observations and provides:
/// - best-parameter selection for a given [`OptimizationTarget`],
/// - Pareto-front extraction in the recall–QPS plane,
/// - a simple grid-expansion suggestion for the next candidate.
pub struct IndexOptimizer {
    index_type: IndexType,
    target: OptimizationTarget,
    benchmarks: Vec<BenchmarkPoint>,
}

impl IndexOptimizer {
    /// Create a new optimiser for `index_type` that pursues `target`.
    pub fn new(index_type: IndexType, target: OptimizationTarget) -> Self {
        Self {
            index_type,
            target,
            benchmarks: Vec::new(),
        }
    }

    /// Record a new benchmark observation.
    pub fn add_benchmark(&mut self, point: BenchmarkPoint) {
        self.benchmarks.push(point);
    }

    /// Number of recorded benchmark observations.
    pub fn benchmark_count(&self) -> usize {
        self.benchmarks.len()
    }

    /// Compute the scalar score for a benchmark point under the current target.
    ///
    /// QPS is normalised to [0, 1] using the maximum observed QPS before combining
    /// in the `BalancedRecallQPS` case so that both axes live in the same range.
    fn score(&self, point: &BenchmarkPoint) -> f64 {
        match &self.target {
            OptimizationTarget::MaxRecall => point.recall_at_10,
            OptimizationTarget::MaxQPS => point.qps,
            OptimizationTarget::BalancedRecallQPS { recall_weight } => {
                let max_qps = self
                    .benchmarks
                    .iter()
                    .map(|b| b.qps)
                    .fold(f64::NEG_INFINITY, f64::max);
                let norm_qps = if max_qps > 0.0 {
                    point.qps / max_qps
                } else {
                    0.0
                };
                recall_weight * point.recall_at_10 + (1.0 - recall_weight) * norm_qps
            }
        }
    }

    /// Return the benchmark point with the highest score under the current target,
    /// or `None` if no benchmarks have been recorded.
    pub fn best_params(&self) -> Option<&BenchmarkPoint> {
        self.benchmarks.iter().max_by(|a, b| {
            self.score(a)
                .partial_cmp(&self.score(b))
                .unwrap_or(std::cmp::Ordering::Equal)
        })
    }

    /// Extract the Pareto-optimal front in the (recall_at_10, qps) plane.
    ///
    /// A point `a` dominates `b` when `a.recall_at_10 >= b.recall_at_10` **and**
    /// `a.qps >= b.qps` with at least one strict inequality.  The returned vector
    /// contains only non-dominated points, sorted by descending recall.
    pub fn pareto_front(&self) -> Vec<&BenchmarkPoint> {
        let mut front: Vec<&BenchmarkPoint> = Vec::new();

        for candidate in &self.benchmarks {
            let dominated = front.iter().any(|existing| {
                // *existing* dominates *candidate*
                existing.recall_at_10 >= candidate.recall_at_10
                    && existing.qps >= candidate.qps
                    && (existing.recall_at_10 > candidate.recall_at_10
                        || existing.qps > candidate.qps)
            });

            if !dominated {
                // Remove any previously accepted points that *candidate* dominates.
                front.retain(|existing| {
                    !(candidate.recall_at_10 >= existing.recall_at_10
                        && candidate.qps >= existing.qps
                        && (candidate.recall_at_10 > existing.recall_at_10
                            || candidate.qps > existing.qps))
                });
                front.push(candidate);
            }
        }

        // Sort by descending recall for deterministic output.
        front.sort_by(|a, b| {
            b.recall_at_10
                .partial_cmp(&a.recall_at_10)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        front
    }

    /// Suggest the next parameter configuration to benchmark.
    ///
    /// The strategy is a **simple grid expansion**: take the best observed
    /// configuration and propose a neighbouring point by incrementing the most
    /// impactful parameter by one step.  Returns `None` when no benchmarks exist
    /// or the index type is `Flat` (no free parameters).
    pub fn suggest_next_params(&self) -> Option<IndexParams> {
        let best = self.best_params()?;

        match &best.params {
            IndexParams::Hnsw(p) => {
                // Increment ef_search first (cheapest build change), then m.
                let next = if p.ef_search < 512 {
                    HnswParams::new(p.m, p.ef_construction, p.ef_search * 2)
                } else if p.m < 64 {
                    HnswParams::new(p.m * 2, p.ef_construction, p.ef_search)
                } else {
                    HnswParams::new(p.m, p.ef_construction * 2, p.ef_search)
                };
                Some(IndexParams::Hnsw(next))
            }
            IndexParams::Ivf(p) => {
                // Increase n_probes first (no rebuild), then n_lists.
                let next = if p.n_probes < p.n_lists {
                    IvfParams::new(p.n_lists, (p.n_probes * 2).min(p.n_lists))
                } else {
                    IvfParams::new(p.n_lists * 2, p.n_probes)
                };
                Some(IndexParams::Ivf(next))
            }
        }
    }

    /// Reference to the index type being optimised.
    pub fn index_type(&self) -> &IndexType {
        &self.index_type
    }

    /// All recorded benchmark points.
    pub fn benchmarks(&self) -> &[BenchmarkPoint] {
        &self.benchmarks
    }

    /// Clear all recorded benchmarks (useful for re-runs).
    pub fn clear(&mut self) {
        self.benchmarks.clear();
    }

    /// Return all benchmarks sorted by score under the current target (descending).
    pub fn ranked_benchmarks(&self) -> Vec<&BenchmarkPoint> {
        let mut ranked: Vec<&BenchmarkPoint> = self.benchmarks.iter().collect();
        ranked.sort_by(|a, b| {
            self.score(b)
                .partial_cmp(&self.score(a))
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        ranked
    }

    /// Return the score of the given benchmark point under the current target.
    pub fn score_of(&self, point: &BenchmarkPoint) -> f64 {
        self.score(point)
    }

    /// Return benchmarks that achieve at least `min_recall` recall@10.
    pub fn filter_by_recall(&self, min_recall: f64) -> Vec<&BenchmarkPoint> {
        self.benchmarks
            .iter()
            .filter(|b| b.recall_at_10 >= min_recall)
            .collect()
    }

    /// Return benchmarks that achieve at least `min_qps` queries per second.
    pub fn filter_by_qps(&self, min_qps: f64) -> Vec<&BenchmarkPoint> {
        self.benchmarks
            .iter()
            .filter(|b| b.qps >= min_qps)
            .collect()
    }

    /// Compute summary statistics (min/max/mean) over recall@10 values.
    pub fn recall_stats(&self) -> Option<RecallStats> {
        if self.benchmarks.is_empty() {
            return None;
        }
        let recalls: Vec<f64> = self.benchmarks.iter().map(|b| b.recall_at_10).collect();
        let min = recalls.iter().cloned().fold(f64::INFINITY, f64::min);
        let max = recalls.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let mean = recalls.iter().sum::<f64>() / recalls.len() as f64;
        Some(RecallStats { min, max, mean })
    }

    /// Group benchmarks by their parameter variant (Hnsw vs Ivf).
    pub fn group_by_variant(&self) -> HashMap<&'static str, Vec<&BenchmarkPoint>> {
        let mut groups: HashMap<&'static str, Vec<&BenchmarkPoint>> = HashMap::new();
        for b in &self.benchmarks {
            let key = match &b.params {
                IndexParams::Hnsw(_) => "hnsw",
                IndexParams::Ivf(_) => "ivf",
            };
            groups.entry(key).or_default().push(b);
        }
        groups
    }
}

/// Summary statistics for recall@10 across all benchmark observations.
#[derive(Debug, Clone)]
pub struct RecallStats {
    /// Minimum observed recall@10.
    pub min: f64,
    /// Maximum observed recall@10.
    pub max: f64,
    /// Mean recall@10.
    pub mean: f64,
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── Helpers ──────────────────────────────────────────────────────────────

    fn hnsw_point(m: usize, ef_c: usize, ef_s: usize, recall: f64, qps: f64) -> BenchmarkPoint {
        BenchmarkPoint::new(
            IndexParams::Hnsw(HnswParams::new(m, ef_c, ef_s)),
            recall,
            qps,
            100,
        )
    }

    fn ivf_point(n_lists: usize, n_probes: usize, recall: f64, qps: f64) -> BenchmarkPoint {
        BenchmarkPoint::new(
            IndexParams::Ivf(IvfParams::new(n_lists, n_probes)),
            recall,
            qps,
            200,
        )
    }

    fn make_hnsw_optimizer(target: OptimizationTarget) -> IndexOptimizer {
        IndexOptimizer::new(IndexType::HNSW, target)
    }

    fn make_ivf_optimizer(target: OptimizationTarget) -> IndexOptimizer {
        IndexOptimizer::new(IndexType::IVF, target)
    }

    // ── basic construction ───────────────────────────────────────────────────

    #[test]
    fn test_new_optimizer_empty() {
        let opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        assert_eq!(opt.benchmark_count(), 0);
        assert!(opt.best_params().is_none());
        assert!(opt.pareto_front().is_empty());
        assert!(opt.suggest_next_params().is_none());
    }

    #[test]
    fn test_index_type_stored() {
        let opt = IndexOptimizer::new(IndexType::IVF, OptimizationTarget::MaxQPS);
        assert_eq!(opt.index_type(), &IndexType::IVF);
    }

    #[test]
    fn test_flat_index_type() {
        let opt = IndexOptimizer::new(IndexType::Flat, OptimizationTarget::MaxRecall);
        assert_eq!(opt.index_type(), &IndexType::Flat);
    }

    // ── add_benchmark / benchmark_count ─────────────────────────────────────

    #[test]
    fn test_add_single_benchmark() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        assert_eq!(opt.benchmark_count(), 1);
    }

    #[test]
    fn test_add_multiple_benchmarks() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        for i in 0..10 {
            opt.add_benchmark(hnsw_point(
                16,
                200,
                50 + i * 10,
                0.8 + i as f64 * 0.01,
                5000.0 - i as f64 * 100.0,
            ));
        }
        assert_eq!(opt.benchmark_count(), 10);
    }

    // ── best_params – MaxRecall ──────────────────────────────────────────────

    #[test]
    fn test_best_params_max_recall_single() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        let best = opt.best_params().expect("should have best");
        assert_eq!(best.recall_at_10, 0.9);
    }

    #[test]
    fn test_best_params_max_recall_picks_highest() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.75, 8000.0));
        opt.add_benchmark(hnsw_point(32, 400, 100, 0.95, 3000.0));
        opt.add_benchmark(hnsw_point(16, 200, 80, 0.85, 6000.0));
        let best = opt.best_params().expect("some best");
        assert_eq!(best.recall_at_10, 0.95);
    }

    #[test]
    fn test_best_params_max_recall_ignores_qps() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        // Low recall but very high QPS — should NOT win under MaxRecall
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 100_000.0));
        // High recall, low QPS — should win
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 100.0));
        let best = opt.best_params().expect("some best");
        assert_eq!(best.recall_at_10, 0.99);
    }

    // ── best_params – MaxQPS ─────────────────────────────────────────────────

    #[test]
    fn test_best_params_max_qps_picks_highest_qps() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxQPS);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 3000.0));
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.6, 12000.0));
        opt.add_benchmark(hnsw_point(32, 400, 100, 0.95, 1500.0));
        let best = opt.best_params().expect("some best");
        assert_eq!(best.qps, 12000.0);
    }

    #[test]
    fn test_best_params_max_qps_ignores_recall() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxQPS);
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.1, 50000.0));
        opt.add_benchmark(hnsw_point(64, 800, 400, 1.0, 100.0));
        let best = opt.best_params().expect("some best");
        assert_eq!(best.qps, 50000.0);
    }

    // ── best_params – BalancedRecallQPS ──────────────────────────────────────

    #[test]
    fn test_best_params_balanced_equal_weight() {
        let mut opt =
            make_hnsw_optimizer(OptimizationTarget::BalancedRecallQPS { recall_weight: 0.5 });
        // Point A: high recall, low qps
        opt.add_benchmark(hnsw_point(64, 800, 400, 1.0, 100.0));
        // Point B: medium recall, medium qps
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        // Point C: low recall, max qps
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 10000.0));
        // With equal weight: A scores 0.5*1.0 + 0.5*(100/10000) = 0.505
        //                    B scores 0.5*0.9 + 0.5*(5000/10000) = 0.7
        //                    C scores 0.5*0.5 + 0.5*1.0 = 1.0 — max norm_qps is 1.0
        // Actually C should win or B depending on normalisation, but B is the balanced one
        // Let's verify the function runs and returns a result
        let best = opt.best_params();
        assert!(best.is_some());
    }

    #[test]
    fn test_best_params_balanced_recall_heavy() {
        let mut opt =
            make_hnsw_optimizer(OptimizationTarget::BalancedRecallQPS { recall_weight: 0.9 });
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 1000.0));
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 10000.0));
        // 0.9*0.99 + 0.1*(1000/10000) = 0.891 + 0.01 = 0.901
        // 0.9*0.5  + 0.1*1.0          = 0.45 + 0.1  = 0.55
        let best = opt.best_params().expect("some best");
        assert_eq!(best.recall_at_10, 0.99);
    }

    #[test]
    fn test_best_params_balanced_qps_heavy() {
        let mut opt =
            make_hnsw_optimizer(OptimizationTarget::BalancedRecallQPS { recall_weight: 0.1 });
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 1000.0));
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 10000.0));
        // 0.1*0.99 + 0.9*0.1 = 0.099 + 0.09 = 0.189
        // 0.1*0.5  + 0.9*1.0 = 0.05 + 0.9  = 0.95
        let best = opt.best_params().expect("some best");
        assert_eq!(best.qps, 10000.0);
    }

    // ── pareto_front ────────────────────────────────────────────────────────

    #[test]
    fn test_pareto_front_empty() {
        let opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        assert!(opt.pareto_front().is_empty());
    }

    #[test]
    fn test_pareto_front_single_point() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        let front = opt.pareto_front();
        assert_eq!(front.len(), 1);
    }

    #[test]
    fn test_pareto_front_no_dominated() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        // Three points where each is better on one axis
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 500.0)); // high recall, low qps
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.85, 5000.0)); // medium
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 15000.0)); // low recall, high qps
        let front = opt.pareto_front();
        assert_eq!(front.len(), 3);
    }

    #[test]
    fn test_pareto_front_dominated_excluded() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0)); // dominates the next
        opt.add_benchmark(hnsw_point(8, 100, 30, 0.8, 4000.0)); // dominated
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 15000.0)); // non-dominated
        let front = opt.pareto_front();
        // The dominated point should not appear
        assert_eq!(front.len(), 2);
        let recalls: Vec<f64> = front.iter().map(|p| p.recall_at_10).collect();
        assert!(!recalls.contains(&0.8));
    }

    #[test]
    fn test_pareto_front_sorted_by_recall_desc() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 15000.0));
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.85, 5000.0));
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 500.0));
        let front = opt.pareto_front();
        for window in front.windows(2) {
            assert!(window[0].recall_at_10 >= window[1].recall_at_10);
        }
    }

    #[test]
    fn test_pareto_front_all_dominated_except_best() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        // One point dominates all others on both axes
        opt.add_benchmark(hnsw_point(64, 800, 400, 1.0, 20000.0));
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.8, 5000.0));
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 3000.0));
        let front = opt.pareto_front();
        assert_eq!(front.len(), 1);
        assert_eq!(front[0].recall_at_10, 1.0);
    }

    // ── suggest_next_params ──────────────────────────────────────────────────

    #[test]
    fn test_suggest_next_none_when_empty() {
        let opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        assert!(opt.suggest_next_params().is_none());
    }

    #[test]
    fn test_suggest_next_hnsw_increments_ef_search() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        let next = opt.suggest_next_params().expect("suggestion");
        if let IndexParams::Hnsw(p) = next {
            // ef_search was 50, should be doubled to 100 (< 512)
            assert_eq!(p.ef_search, 100);
            assert_eq!(p.m, 16);
        } else {
            panic!("Expected Hnsw params");
        }
    }

    #[test]
    fn test_suggest_next_hnsw_increments_m_when_ef_search_maxed() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 512, 0.99, 1000.0));
        let next = opt.suggest_next_params().expect("suggestion");
        if let IndexParams::Hnsw(p) = next {
            // ef_search is at 512, so m should double: 16 -> 32
            assert_eq!(p.ef_search, 512);
            assert_eq!(p.m, 32);
        } else {
            panic!("Expected Hnsw params");
        }
    }

    #[test]
    fn test_suggest_next_ivf_increments_n_probes() {
        let mut opt = make_ivf_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(ivf_point(256, 4, 0.7, 8000.0));
        let next = opt.suggest_next_params().expect("suggestion");
        if let IndexParams::Ivf(p) = next {
            assert_eq!(p.n_probes, 8);
            assert_eq!(p.n_lists, 256);
        } else {
            panic!("Expected IVF params");
        }
    }

    #[test]
    fn test_suggest_next_ivf_grows_n_lists_when_probes_maxed() {
        let mut opt = make_ivf_optimizer(OptimizationTarget::MaxRecall);
        // n_probes == n_lists => can't increase probes further
        opt.add_benchmark(ivf_point(64, 64, 0.95, 2000.0));
        let next = opt.suggest_next_params().expect("suggestion");
        if let IndexParams::Ivf(p) = next {
            assert_eq!(p.n_lists, 128);
        } else {
            panic!("Expected IVF params");
        }
    }

    // ── IVF benchmarks with optimizer ───────────────────────────────────────

    #[test]
    fn test_ivf_best_params_max_recall() {
        let mut opt = make_ivf_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(ivf_point(64, 4, 0.6, 9000.0));
        opt.add_benchmark(ivf_point(64, 32, 0.9, 4000.0));
        opt.add_benchmark(ivf_point(256, 64, 0.97, 1500.0));
        let best = opt.best_params().expect("best");
        assert_eq!(best.recall_at_10, 0.97);
    }

    #[test]
    fn test_ivf_best_params_max_qps() {
        let mut opt = make_ivf_optimizer(OptimizationTarget::MaxQPS);
        opt.add_benchmark(ivf_point(64, 4, 0.6, 9000.0));
        opt.add_benchmark(ivf_point(64, 32, 0.9, 4000.0));
        opt.add_benchmark(ivf_point(256, 64, 0.97, 1500.0));
        let best = opt.best_params().expect("best");
        assert_eq!(best.qps, 9000.0);
    }

    // ── score_of helper ─────────────────────────────────────────────────────

    #[test]
    fn test_score_of_max_recall() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        let p = hnsw_point(16, 200, 50, 0.92, 5000.0);
        opt.add_benchmark(p.clone());
        assert!((opt.score_of(&p) - 0.92).abs() < 1e-9);
    }

    #[test]
    fn test_score_of_max_qps() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxQPS);
        let p = hnsw_point(16, 200, 50, 0.92, 7777.0);
        opt.add_benchmark(p.clone());
        assert!((opt.score_of(&p) - 7777.0).abs() < 1e-9);
    }

    // ── ranked_benchmarks ────────────────────────────────────────────────────

    #[test]
    fn test_ranked_benchmarks_descending() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 15000.0));
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.85, 5000.0));
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 500.0));
        let ranked = opt.ranked_benchmarks();
        assert_eq!(ranked[0].recall_at_10, 0.99);
        assert_eq!(ranked[1].recall_at_10, 0.85);
        assert_eq!(ranked[2].recall_at_10, 0.5);
    }

    // ── filter helpers ───────────────────────────────────────────────────────

    #[test]
    fn test_filter_by_recall() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 15000.0));
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.85, 5000.0));
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 500.0));
        let filtered = opt.filter_by_recall(0.8);
        assert_eq!(filtered.len(), 2);
    }

    #[test]
    fn test_filter_by_qps() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxQPS);
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 15000.0));
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.85, 5000.0));
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 500.0));
        let filtered = opt.filter_by_qps(5000.0);
        assert_eq!(filtered.len(), 2);
    }

    // ── recall_stats ─────────────────────────────────────────────────────────

    #[test]
    fn test_recall_stats_none_when_empty() {
        let opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        assert!(opt.recall_stats().is_none());
    }

    #[test]
    fn test_recall_stats_single() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.88, 5000.0));
        let stats = opt.recall_stats().expect("stats");
        assert!((stats.min - 0.88).abs() < 1e-9);
        assert!((stats.max - 0.88).abs() < 1e-9);
        assert!((stats.mean - 0.88).abs() < 1e-9);
    }

    #[test]
    fn test_recall_stats_multiple() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.6, 15000.0));
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        opt.add_benchmark(hnsw_point(64, 800, 400, 0.99, 500.0));
        let stats = opt.recall_stats().expect("stats");
        assert!((stats.min - 0.6).abs() < 1e-9);
        assert!((stats.max - 0.99).abs() < 1e-9);
        assert!((stats.mean - (0.6 + 0.9 + 0.99) / 3.0).abs() < 1e-9);
    }

    // ── clear ────────────────────────────────────────────────────────────────

    #[test]
    fn test_clear_removes_all() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        opt.add_benchmark(hnsw_point(4, 50, 10, 0.5, 15000.0));
        opt.clear();
        assert_eq!(opt.benchmark_count(), 0);
        assert!(opt.best_params().is_none());
    }

    // ── group_by_variant ─────────────────────────────────────────────────────

    #[test]
    fn test_group_by_variant_mixed() {
        let mut opt = IndexOptimizer::new(IndexType::IVFPQ, OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        opt.add_benchmark(ivf_point(64, 8, 0.75, 7000.0));
        opt.add_benchmark(hnsw_point(32, 400, 100, 0.95, 2000.0));
        let groups = opt.group_by_variant();
        assert_eq!(groups["hnsw"].len(), 2);
        assert_eq!(groups["ivf"].len(), 1);
    }

    // ── IndexParams helpers ──────────────────────────────────────────────────

    #[test]
    fn test_index_params_as_hnsw() {
        let p = IndexParams::Hnsw(HnswParams::new(16, 200, 50));
        assert!(p.as_hnsw().is_some());
        assert!(p.as_ivf().is_none());
    }

    #[test]
    fn test_index_params_as_ivf() {
        let p = IndexParams::Ivf(IvfParams::new(64, 8));
        assert!(p.as_ivf().is_some());
        assert!(p.as_hnsw().is_none());
    }

    // ── benchmarks reference ─────────────────────────────────────────────────

    #[test]
    fn test_benchmarks_accessor() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 5000.0));
        assert_eq!(opt.benchmarks().len(), 1);
    }

    // ── edge cases ───────────────────────────────────────────────────────────

    #[test]
    fn test_zero_qps_does_not_panic() {
        let mut opt =
            make_hnsw_optimizer(OptimizationTarget::BalancedRecallQPS { recall_weight: 0.5 });
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 0.0));
        // Should not panic; score should be 0.5*0.9 + 0.5*0.0 = 0.45
        let best = opt.best_params().expect("some best");
        let s = opt.score_of(best);
        assert!((s - 0.45).abs() < 1e-9);
    }

    #[test]
    fn test_identical_recall_uses_qps_tiebreak() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(hnsw_point(16, 200, 50, 0.9, 1000.0));
        opt.add_benchmark(hnsw_point(32, 400, 100, 0.9, 5000.0));
        // Both have recall 0.9; max_by returns one without guarantee on tie,
        // but the function should not panic
        assert!(opt.best_params().is_some());
    }

    #[test]
    fn test_build_time_stored() {
        let mut opt = make_hnsw_optimizer(OptimizationTarget::MaxRecall);
        opt.add_benchmark(BenchmarkPoint::new(
            IndexParams::Hnsw(HnswParams::new(16, 200, 50)),
            0.9,
            5000.0,
            12345,
        ));
        assert_eq!(opt.benchmarks()[0].build_time_ms, 12345);
    }

    #[test]
    fn test_hnsw_params_equality() {
        let a = HnswParams::new(16, 200, 50);
        let b = HnswParams::new(16, 200, 50);
        assert_eq!(a, b);
    }

    #[test]
    fn test_ivf_params_equality() {
        let a = IvfParams::new(64, 8);
        let b = IvfParams::new(64, 8);
        assert_eq!(a, b);
    }

    #[test]
    fn test_index_type_equality() {
        assert_eq!(IndexType::HNSW, IndexType::HNSW);
        assert_ne!(IndexType::HNSW, IndexType::IVF);
        assert_ne!(IndexType::IVF, IndexType::IVFPQ);
        assert_ne!(IndexType::IVFPQ, IndexType::Flat);
    }
}