MMR reranking via mmr_lambda hidden column#6
Conversation
Add Maximal Marginal Relevance (MMR) support to vec0 virtual table. When mmr_lambda is provided in a KNN query, candidates are over-fetched and then greedily re-selected to balance relevance against diversity. API: WHERE embedding MATCH ? AND k = 10 AND mmr_lambda = 0.7 - mmr_lambda range [0.0, 1.0]: 1.0 = pure relevance, 0.0 = pure diversity - Over-fetch factor: 5x (capped at k_max=4096) - Supports float32, int8, and bit vector types - All distance metrics (L2, cosine, L1, hamming) - Zero impact when mmr_lambda is not provided
9 test functions covering: - Cosine diversity (baseline vs lambda=1.0, 0.5, 0.0) - L2 distance metric compatibility - Int8 vector element type - Cluster monopoly breaking - Composition with distance constraints - Composition with partition keys - Edge cases (k=1, k=0) - Error handling (invalid lambda range) - Insert guard for hidden column
pragma_table_list does not guarantee row order. Add ORDER BY name to the two shadow table queries so the snapshot is deterministic.
…r KNN queries -- see vlasky#6
|
Thanks for this! Claude (and I) just merged it into my fork. One fix it made: in vec0_mmr_rerank, the copy-back loop at the end iterates k_target times, but the greedy selection loop can terminate early (via if (best_idx < 0) break), leaving the tail of out_rowids/out_distances uninitialized. We added an n_selected counter and an out_n_selected output parameter so only the actually-selected entries are copied back. The caller now sets k_used = n_selected instead of k_used = k_original. (You can see my referenced commit for details) |
The copy-back loop iterated k_target times, but the greedy selection loop can terminate early via `if (best_idx < 0) break`, leaving the tail of out_rowids/out_distances uninitialized. Add an n_selected counter and out_n_selected output parameter so only actually-selected entries are copied back. The caller now sets k_used = n_selected instead of k_used = k_original. Credit: mceachen (vlasky#6)
The copy-back loop iterated k_target times, but the greedy selection loop can terminate early via `if (best_idx < 0) break`, leaving the tail of out_rowids/out_distances uninitialized. Add an n_selected counter and out_n_selected output parameter so only actually-selected entries are copied back. The caller now sets k_used = n_selected instead of k_used = k_original. Credit: mceachen (vlasky#6)
|
Thanks for this contribution! Merged. I added a follow-up commit (8d4ef9e) to normalize the diversity term in the MMR greedy loop. The relevance term was already normalized to [0,1] by dividing by The fix divides the inter-candidate distance by |
Rebased version of asg017#267 (issue: asg017#266) for this fork. Adds a
mmr_lambdahidden column to vec0 for Maximal Marginal Relevance reranking in KNN queries.Composes with this fork's distance constraints, partition keys, and all vector types/metrics.
Also fixes the pre-existing test_shadow snapshot failure (missing ORDER BY on pragma_table_list).
Full design and rationale in the upstream PR.