fix(ml): enforce deterministic tie-breaking for USearch top-K candidate selection - #258
fix(ml): enforce deterministic tie-breaking for USearch top-K candidate selection#258thinkapoorv wants to merge 1 commit into
Conversation
|
Thanks for the detailed writeup and for digging into the root cause — the analysis of the F16 quantization collision is correct. Unfortunately, the fix itself doesn't address the flake, so I'm closing this PR. Let me explain the reasoning:
|
Thanks for the detailed explanation! That makes perfect sense. I was reasoning from the assumption that tie-breaking happened after the candidate set had already been materialized, but I completely overlooked that the truncation to limit=1 happens inside USearch itself. That also clarifies why sorting by key could only make the result deterministic, not semantically correct. I appreciate the explanation , it helped me understand the retrieval path much better. |
Introduction
Resolves non-deterministic test failures in the CI pipeline (
test_index.py::test_index_factory[factory0/3/6]) by enforcing stable sorting on exact-distance nearest-neighbor candidates in theUSearchrust integration.Context
During approximate nearest-neighbor retrieval via
USearchKNNIndex, we discovered a source of non-determinism when document embeddings quantize to the exact same distance (specifically underF16quantization and cosine similarity). When candidate scores tied, USearch’s internal HNSW graph structure returned results in an arbitrary order.This arbitrary tie-breaking surfaced in our CI pipeline as flaky assertions where document
"c"occasionally outranked exact-match"a".Evaluated Approaches:
fake_embedderto return fully orthogonal vectors, bypassing the quantization collision.key). This permanently hardens the system against flaky contextual retrievals.O(K log K)to sort the final top-k array.How has this been tested?
pathway/tests/ml/test_index.py::test_index_factoryis now 100% deterministic locally, and"a"reliably outranks"c"across randomized seeds.cargo fmtand confirmed no borrow checker or typing regressions were introduced by the stable sortsort_bycompound closure.Types of changes
Related issue(s):
Checklist: