Edarke opened a new pull request, #17007: URL: https://github.com/apache/lucene/pull/17007
### Description <!-- If this is your first contribution to Lucene, please make sure you have reviewed the contribution guide. https://github.com/apache/lucene/blob/main/CONTRIBUTING.md --> Note: This PR is a follow up to #16811 which should be reviewed first. In Atlas Search, we have not been able to adopt OptimisticKNN by default because our benchmarks reveal several query shapes that deterministically invoke an unnecessary 2nd-phase traversal, leading us to fork Lucene to maintain the removed TopKnnCollector. Likewise, we have not been able to adopt ACORN-1 by default due to known recall issues for selective queries. Finally, we observe ~22% of all production queries across all filtered KNN workloads fallback to `AbstractKnnVectorQuery.exactSearch` after reaching the visitedLimit, which results in a wide range of query shapes doing 2x the work that should be required. This PR attempts to fix all three issues with a small API change and data format change on disk. **Summary of Problems:** 1. perLeafK doesn't consider sparsity of vector values if there is no explicit filter, so sparse vector values almost always do a 2-phase search. 2. If filter hits are not uniformly distributed amongst segments (e.g. a creationDate filter), filtered search almost always does a 2-phase search. 3. perLeafK doesn't account for deletions per segment, so freshly-merged segments are more-likely to re-enter. 4. perLeafK for highly skewed segment sizes finds local optima and must be re-searched with globalK (to fix in follow-up PR) 5. Lucene's HnswSearcher.estimatedVisitedNodes doesn't account for filter selectivity, which is needed to supplement regimes where ACORN-1 fails **Summary of Changes** 1. Compute per-segment hits before perLeafK computation. If a pre-filter is correlated with insertion-order, for example, we should request more results from those relevant segments in the first pass. 2. TopKnnCollector now knows the perLeafK and globalK values. This allows the LeafReader use a beamWidth of perLeafK but report up to globalK results. This is important for any reader that might score all vectors anyway (e.g. a flat index reader or an HNSW index with `filteredDocs < expectedVisitedNodes(k, n)` 3. Change `LeafReader.searchNearestVectors` to return KnnResult. TopDocs simply isn't rich enough to decide whether we need to re-search a segment. a. This is conceptually similar to @Pranshu-S's https://github.com/apache/lucene/pull/16866 but pushed down into the LeafReader 4. `estimatedVisitedNodes` now accounts for probability of encountering duplicate nodes during exploration 5. We compute the average degree of nodes in the bottom layer when writing graphs and append it to the field metadata. This is a much better predictor of required work than maxConn. # Cost Estimate Evaluation The cost estimate proposed in this PR is the result of instrumenting 50 million queries to report the true cost of HNSW traversal (in terms of vectors scored) across 140 graphs built from 3 datasets and 5 embedding models with random queries with selectivity sampled uniformly from [.0001, 100]. We compare both sMAPE and the number of additional vectors scored on average compared to the current baseline and against an oracle that can predict HNSW cost with 100% accuracy. You can visualize the false-positive/false-negative errors [here](https://edarke.github.io/experiments/) ## Filtered queries (selectivity <= 0.99) | corpus | rows | sMAPE Lucene | sMAPE ours | extra work Lucene | extra work branch | oracle work (absolute) | |---|---:|---:|---:|---:|---:|---:| | cohere 91919 (held-out seed) | 4,307,005 | 97.5% | **12.3%** | +47.26% | **+0.72%** | 39.8B vectors | | minilm 384d | 896,067 | 103.2% | **22.9%** | +45.14% | **+0.98%** | 16.0B | | bgewiki 384d | 896,067 | 109.4% | **25.7%** | +45.22% | **+1.22%** | 17.4B | | dbpedia 3072d | 896,049 | 118.5% | **24.0%** | +55.17% | **+1.11%** | 19.5B | | voyage (quantized) 2048d n=5M | 95,663 | 60.5% | **22.5%** | +0.05% | **+0.05%** | 1.4B | | voyage 2048d n=100k | 71,612 | 56.0% | **25.4%** | +0.69% | **+0.69%** | 0.6B | | **row-weighted total** | 7,162,463 | ~101% | ~17% | **+46.6%** | **+0.86%** | 94.7B | ## Unfiltered queries (selectivity > 0.99) | corpus | rows | sMAPE Lucene | sMAPE ours | extra work Lucene | extra work branch | oracle work (absolute) | |---|---:|---:|---:|---:|---:|---:| | cohere 91919 (held-out seed) | 5,223,530 | 53.1% | **13.9%** | +0.27% | **+0.00%** | 29.5B vectors | | minilm 384d | 900,096 | 33.2% | **23.4%** | +0.00% | **+0.00%** | 6.5B | | bgewiki 384d | 900,096 | 39.2% | **27.4%** | +0.00% | **+0.00%** | 7.4B | | dbpedia 3072d | 900,090 | 45.5% | **24.6%** | +0.00% | **+0.00%** | 8.2B | | voyage (quantized) 2048d n=5M | 100,009 | 56.7% | **22.0%** | +0.00% | **+0.00%** | 0.4B | | voyage 2048d n=100k | 100,017 | 31.4% | **23.1%** | +0.00% | **+0.00%** | 0.4B | | **row-weighted total** | 8,123,838 | ~48% | ~18% | **+0.18%** | **+0.00%** | 52.4B | # E2E Benchmarks Segments: 1x500k, 2x250k ## density 1.0 (1M docs, all with vectors) | config | k | baseline recall | branch recall | baseline ms/query | branch ms/query | branch speedup | baseline re-entry rate | branch re-entry rate | |---|---|---|---|---|---|---|---|---| | nofilter | 10 | 92.3% | 92.3% | 0.426 | 0.432 | -1% | 0% | 0% | | nofilter | 100 | 95.7% | 95.7% | 1.170 | 1.180 | -1% | 0% | 0% | | nofilter | 1000 | 97.0% | 97.0% | 3.996 | 4.146 | -4% | 0% | 0% | | uniform-s0.01 | 10 | 100.0% | 100.0% | 3.348 | 1.238 | +63% | 0.4% | 0% | | uniform-s0.01 | 100 | 100.0% | 100.0% | 3.284 | 1.258 | +62% | 0% | 0% | | uniform-s0.01 | 1000 | 100.0% | 100.0% | 1.462 | 1.534 | -5% | 0% | 0% | | uniform-s0.05 | 10 | 99.2% | 99.2% | 4.036 | 3.744 | +7% | 0% | 0% | | uniform-s0.05 | 100 | 99.8% | 99.8% | 13.734 | 9.354 | +32% | 0% | 0% | | uniform-s0.05 | 1000 | 100.0% | 100.0% | 17.386 | 7.366 | +58% | 0% | 0% | | uniform-s0.10 | 10 | 98.3% | 98.3% | 2.424 | 2.176 | +10% | 0% | 0% | | uniform-s0.10 | 100 | 98.7% | 98.7% | 7.280 | 6.770 | +7% | 0% | 0% | | uniform-s0.10 | 1000 | 99.9% | 100.0% | 31.272 | 14.410 | +54% | 0% | 0% | | uniform-s0.50 | 10 | 95.5% | 95.5% | 0.792 | 0.790 | +0% | 0% | 0% | | uniform-s0.50 | 100 | 97.1% | 97.1% | 2.136 | 2.048 | +4% | 0% | 0% | | uniform-s0.50 | 1000 | 97.6% | 97.6% | 7.232 | 7.322 | -1% | 0% | 0% | | first-s0.01 | 10 | 98.7% | 95.9% | 1.350 | 1.164 | +14% | 2.4% | 0% | | first-s0.01 | 100 | 100.0% | 100.0% | 5.104 | 0.774 | +85% | 100% | 0% | | first-s0.01 | 1000 | 100.0% | 100.0% | 3.728 | 0.924 | +75% | 100% | 0% | | first-s0.05 | 10 | 97.4% | 98.0% | 2.110 | 0.968 | +54% | 0% | 0% | | first-s0.05 | 100 | 97.5% | 98.7% | 3.630 | 2.116 | +42% | 50% | 0% | | first-s0.05 | 1000 | 99.6% | 99.7% | 14.812 | 10.002 | +32% | 100% | 0% | | first-s0.10 | 10 | 98.0% | 98.7% | 0.826 | 0.908 | -10% | 0% | 0% | | first-s0.10 | 100 | 98.1% | 99.0% | 2.148 | 2.630 | -22% | 0% | 0% | | first-s0.10 | 1000 | 99.6% | 99.1% | 21.476 | 11.126 | +48% | 100% | 0% | | first-s0.50 | 10 | 93.6% | 94.8% | 0.952 | 0.914 | +4% | 0% | 0% | | first-s0.50 | 100 | 96.0% | 97.0% | 2.778 | 2.130 | +23% | 0% | 0% | | first-s0.50 | 1000 | 97.1% | 97.8% | 11.210 | 7.764 | +31% | 43% | 0% | | last-s0.01 | 10 | 100.0% | 95.8% | 4.434 | 2.324 | +48% | 95% | 0% | | last-s0.01 | 100 | 100.0% | 100.0% | 5.552 | 0.756 | +86% | 100% | 0% | | last-s0.01 | 1000 | 100.0% | 100.0% | 1.646 | 0.912 | +45% | 100% | 0% | | last-s0.05 | 10 | 96.6% | 89.3% | 1.178 | 0.880 | +25% | 0% | 0% | | last-s0.05 | 100 | 96.4% | 94.5% | 3.776 | 4.756 | -26% | 0% | 0% | | last-s0.05 | 1000 | 100.0% | 100.0% | 26.828 | 3.494 | +87% | 100% | 0% | | last-s0.10 | 10 | 96.0% | 88.3% | 0.748 | 0.556 | +26% | 0% | 0% | | last-s0.10 | 100 | 96.5% | 94.8% | 2.208 | 2.668 | -21% | 0% | 0% | | last-s0.10 | 1000 | 96.8% | 96.8% | 18.908 | 18.682 | +1% | 100% | 0% | | last-s0.50 | 10 | 91.2% | 88.2% | 0.840 | 0.602 | +28% | 0% | 0% | | last-s0.50 | 100 | 94.3% | 94.1% | 2.196 | 1.520 | +31% | 0% | 0% | | last-s0.50 | 1000 | 96.0% | 96.2% | 10.634 | 5.580 | +48% | 100% | 0% | ## density 0.5 (1M docs, 500,213 with vectors) | config | k | baseline recall | fixed recall | baseline ms/query | branch ms/query | branch speedup | baseline re-entry rate | branch re-entry rate | |---|---|---|---|---|---|---|---|---| | nofilter | 10 | 93.9% | 93.9% | 0.606 | 0.566 | +7% | 0% | 0% | | nofilter | 100 | 97.1% | 97.1% | 1.444 | 1.348 | +7% | 0% | 0% | | nofilter | 1000 | 98.0% | 98.0% | 4.948 | 4.690 | +5% | 0% | 0% | | uniform-s0.01 | 10 | 100.0% | 100.0% | 1.856 | 0.750 | +60% | 0% | 0% | | uniform-s0.01 | 100 | 100.0% | 100.0% | 1.920 | 0.810 | +58% | 0% | 0% | | uniform-s0.01 | 1000 | 100.0% | 100.0% | 1.192 | 1.020 | +14% | 0% | 0% | | uniform-s0.05 | 10 | 99.8% | 99.8% | 6.610 | 3.762 | +43% | 0% | 0% | | uniform-s0.05 | 100 | 100.0% | 100.0% | 9.212 | 3.924 | +57% | 0% | 0% | | uniform-s0.05 | 1000 | 100.0% | 100.0% | 9.092 | 4.336 | +52% | 0% | 0% | | uniform-s0.10 | 10 | 99.0% | 99.0% | 3.928 | 3.588 | +9% | 0% | 0% | | uniform-s0.10 | 100 | 99.6% | 99.6% | 11.290 | 7.232 | +36% | 0% | 0% | | uniform-s0.10 | 1000 | 100.0% | 100.0% | 19.572 | 8.300 | +58% | 0% | 0% | | uniform-s0.50 | 10 | 96.9% | 96.9% | 4.270 | 4.244 | +1% | 0% | 0% | | uniform-s0.50 | 100 | 98.1% | 98.1% | 6.650 | 6.378 | +4% | 0% | 0% | | uniform-s0.50 | 1000 | 98.5% | 98.5% | 12.474 | 12.384 | +1% | 0% | 0% | | first-s0.01 | 10 | 100.0% | 100.0% | 2.758 | 0.498 | +82% | 99% | 0% | | first-s0.01 | 100 | 100.0% | 100.0% | 3.060 | 0.560 | +82% | 100% | 0% | | first-s0.01 | 1000 | 100.0% | 100.0% | 1.106 | 0.686 | +38% | 100% | 0% | | first-s0.05 | 10 | 98.6% | 93.5% | 1.124 | 0.892 | +21% | 0% | 0% | | first-s0.05 | 100 | 98.1% | 97.4% | 2.770 | 3.622 | -31% | 0% | 0% | | first-s0.05 | 1000 | 100.0% | 100.0% | 14.790 | 2.198 | +85% | 100% | 0% | | first-s0.10 | 10 | 98.6% | 98.2% | 1.822 | 1.548 | +15% | 0% | 0% | | first-s0.10 | 100 | 98.2% | 98.6% | 3.844 | 2.600 | +32% | 0% | 0% | | first-s0.10 | 1000 | 99.6% | 99.7% | 18.252 | 10.604 | +42% | 97% | 0% | | first-s0.50 | 10 | 96.0% | 96.1% | 1.988 | 2.226 | -12% | 0% | 0% | | first-s0.50 | 100 | 97.5% | 97.9% | 3.024 | 3.100 | -3% | 0% | 0% | | first-s0.50 | 1000 | 97.8% | 98.4% | 7.288 | 7.506 | -3% | 0% | 0% | | last-s0.01 | 10 | 96.5% | 92.9% | 0.768 | 0.778 | -1% | 1.8% | 0% | | last-s0.01 | 100 | 100.0% | 100.0% | 2.666 | 0.528 | +80% | 100% | 0% | | last-s0.01 | 1000 | 100.0% | 100.0% | 2.226 | 0.686 | +69% | 100% | 0% | | last-s0.05 | 10 | 92.0% | 86.1% | 0.570 | 0.556 | +2% | 0% | 0% | | last-s0.05 | 100 | 95.1% | 95.1% | 1.560 | 1.384 | +11% | 100% | 0% | | last-s0.05 | 1000 | 97.8% | 97.8% | 5.616 | 6.918 | -23% | 100% | 0% | | last-s0.10 | 10 | 90.4% | 93.2% | 1.680 | 0.800 | +52% | 0% | 0% | | last-s0.10 | 100 | 95.3% | 96.9% | 2.424 | 1.164 | +52% | 100% | 0% | | last-s0.10 | 1000 | 97.9% | 98.2% | 4.660 | 3.090 | +34% | 100% | 0% | | last-s0.50 | 10 | 95.7% | 96.6% | 3.016 | 2.816 | +7% | 0% | 0% | | last-s0.50 | 100 | 97.4% | 97.9% | 4.672 | 4.356 | +7% | 0% | 0% | | last-s0.50 | 1000 | 98.0% | 98.4% | 11.582 | 10.364 | +11% | 0% | 0% | -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
