Repository navigation
fix(clustering): reject the cosine metric in QGAssigner - #256
Merged
Merged
Conversation
0.9 added METRIC_COSINE for the ANN indexes, which normalize vectors; the quantizers, FastScan and clustering are documented to reject it. QGAssigner passed its metric unchecked to QuantizedGraph<float>, which accepts cosine, so it assigned by cosine but reported exact distances on a 2(1 - cos) scale instead of rejecting the metric. Its constructor now calls validate_metric_type after the configuration checks. QGKMeans passes only L2 or inner product and is unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
0.9 added
METRIC_COSINEtoMetricTypefor the ANN indexes, which normalize vectors and store inner-product codes.core/metric.hppand the release notes say the quantizers, FastScan and clustering reject it.qgkmeans::QGAssigner, a public clustering class, passed itsMetricTypeunchecked tosymqg::QuantizedGraph<float>, which accepts cosine. So it accepted cosine instead of rejecting it: it assigned by cosine, but its exact distances were on a 2(1 − cos) scale rather than the indexes' 1 − cos.The constructor now calls
validate_metric_typeafter the existing configuration checks (so their precedence is unchanged) and before any allocation, throwingstd::invalid_argument("Metric type must be METRIC_L2 or METRIC_IP")like the other entry points.QGKMeans, the only constructor ofQGAssignerin the library, passes L2 or inner product (spherical), and a cosine index trains on normalized data with inner product, so no existing path is affected. Cosine support inQGAssignernever shipped (METRIC_COSINEis new in 0.9), and the release notes already describe clustering as rejecting it, so they need no change.Found while checking the 0.9 release notes against the code.
Testing
Local, Linux x86-64, GCC 14.3, Release,
RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF:QGAssignerTest.RejectsTheCosineMetric: cosine throws with the exact message; L2 and inner product construct. It fails without the fix.ctest --no-tests=error: 614 passed, 1 skipped (ThreadCount.BoundParallelForUsesRuntimePlacementAndPropagatesErrors, also skipped without the change)../scripts/check-format.sh, full./scripts/check-tidy.sh(clang-tidy 23),git diff --check.QGAssignerandQGKMeanspasses only L2 or inner product.