Skip to content

fix(clustering): check k-means coordinates against the index bound - #249

Merged
gouyt13 merged 1 commit into
dev/0.9from
fix/kmeans-coordinate-bound
Nov 19, 2026
Merged

gouyt13 merged 1 commit into
dev/0.9from
fix/kmeans-coordinate-bound

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 19, 2026

Copy link
Copy Markdown
Member

Summary

k-means input validation (validate_points in include/rabitqlib/clustering/detail/kmeans.hpp) compared each coordinate with the exact double bound sqrt(sqrt(FLT_MAX) / (64 d)). The indexes' check_coordinates uses max_coordinate_magnitude(d), the same value rounded to float. For dimensions where that rounding goes up (for example 64, 100, 256, 960 and 1024), a coordinate whose magnitude equals the float bound passed add and search but failed training with exceeds the safe float32 coordinate range. In Python this made the first add of an IvfIndex or HnswIndex, which trains, reject data that a trained index accepted; train and assign of QGKMeans and RaBitQKMeans were affected the same way.

validate_points now uses max_coordinate_magnitude(d), so clustering and the indexes share one bound. Finite and NaN/Inf handling and the order of the two messages are unchanged. The comments in ivf.cpp/hnsw.cpp and docs/development.md that say k-means uses the index bound are now accurate.

Found while preparing 0.8.4 and deferred then; fixed before 0.9. main has the same code, but no 0.8.x patch release is planned, so this targets dev/0.9 only.

Testing

Local, Linux x86-64, GCC 14, Release, RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF:

  • New KMeansAssignTest.{RaBitQ,QG}CoordinateBoundMatchesIndexCheck (d = 64, asserting that the float bound rounds up there): training and assignment accept ±bound and reject the next float above it with the exact message. Both fail without the fix.
  • New Python test_training_accepts_coordinates_at_the_index_bound for IvfIndex and HnswIndex (first add, then train above the bound) and QGKMeans/RaBitQKMeans (train and assign). All four cases fail against an unmodified dev/0.9 wheel.
  • Mutation: with the k-means range check disabled, both C++ tests and all four Python cases fail.
  • ctest --no-tests=error: 609 passed, 1 skipped (ThreadCount.BoundParallelForUsesRuntimePlacementAndPropagatesErrors, also skipped without the change).
  • Python suite against the package installed from this branch (Python 3.13): 1200 passed, 2 skipped.
  • ./scripts/check-format.sh, ./scripts/check-python.sh, full ./scripts/check-tidy.sh (clang-tidy 23), mkdocs build --strict, git diff --check.

No benchmark: the change replaces a double comparison with a float comparison in input validation and is not a performance claim. No ISA-flagged source includes the changed headers.

k-means validate_points compared coordinates with the exact double bound
sqrt(sqrt(FLT_MAX) / (64 d)), while check_coordinates in the indexes uses
max_coordinate_magnitude(d), the same value rounded to float. Where the
rounding goes up (for example d = 64, 100, 256, 960, 1024), a coordinate
equal to the float bound passed add and search but failed training, so the
first add of an IvfIndex or HnswIndex rejected data a trained index
accepted. Use max_coordinate_magnitude(d) in validate_points.
@github-actions github-actions Bot added the release:fixed Release category managed from the PR title label Nov 19, 2026
@gouyt13
gouyt13 merged commit 5898c08 into dev/0.9 Nov 19, 2026
26 checks passed
@gouyt13
gouyt13 deleted the fix/kmeans-coordinate-bound branch November 19, 2026 07:54
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