Repository navigation
fix(clustering): check k-means coordinates against the index bound - #249
Merged
Merged
Conversation
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.
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
k-means input validation (
validate_pointsininclude/rabitqlib/clustering/detail/kmeans.hpp) compared each coordinate with the exact double boundsqrt(sqrt(FLT_MAX) / (64 d)). The indexes'check_coordinatesusesmax_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 passedaddandsearchbut failed training withexceeds the safe float32 coordinate range. In Python this made the firstaddof anIvfIndexorHnswIndex, which trains, reject data that a trained index accepted;trainandassignofQGKMeansandRaBitQKMeanswere affected the same way.validate_pointsnow usesmax_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 inivf.cpp/hnsw.cppanddocs/development.mdthat say k-means uses the index bound are now accurate.Found while preparing 0.8.4 and deferred then; fixed before 0.9.
mainhas the same code, but no 0.8.x patch release is planned, so this targetsdev/0.9only.Testing
Local, Linux x86-64, GCC 14, Release,
RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF: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.test_training_accepts_coordinates_at_the_index_boundforIvfIndexandHnswIndex(firstadd, thentrainabove the bound) andQGKMeans/RaBitQKMeans(trainandassign). All four cases fail against an unmodifieddev/0.9wheel.ctest --no-tests=error: 609 passed, 1 skipped (ThreadCount.BoundParallelForUsesRuntimePlacementAndPropagatesErrors, also skipped without the change)../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.