Skip to content

fix(clustering): reject the cosine metric in QGAssigner - #256

Merged
gouyt13 merged 1 commit into
dev/0.9from
fix/qgassigner-cosine
Nov 19, 2026
Merged

gouyt13 merged 1 commit into
dev/0.9from
fix/qgassigner-cosine

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 19, 2026

Copy link
Copy Markdown
Member

Summary

0.9 added METRIC_COSINE to MetricType for the ANN indexes, which normalize vectors and store inner-product codes. core/metric.hpp and the release notes say the quantizers, FastScan and clustering reject it. qgkmeans::QGAssigner, a public clustering class, passed its MetricType unchecked to symqg::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_type after the existing configuration checks (so their precedence is unchanged) and before any allocation, throwing std::invalid_argument("Metric type must be METRIC_L2 or METRIC_IP") like the other entry points. QGKMeans, the only constructor of QGAssigner in 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 in QGAssigner never shipped (METRIC_COSINE is 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:

  • New 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.
  • Not run: the Python suite, because the bindings do not expose QGAssigner and QGKMeans passes only L2 or inner product.

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.
@github-actions github-actions Bot added the release:fixed Release category managed from the PR title label Nov 19, 2026
@gouyt13
gouyt13 merged commit 7be124f into dev/0.9 Nov 19, 2026
26 checks passed
@gouyt13
gouyt13 deleted the fix/qgassigner-cosine branch November 19, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fixed Release category managed from the PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant