Repository navigation
fix(hnsw): explain how to train an index loaded from an untrained file - #255
Merged
Merged
Conversation
The C++ API of releases before 0.8 could save an HNSW-RaBitQ index before training it. 0.9 loads such a file untrained, with num_clusters 16, but the file records no seed, so the first add cannot train it. add, search, remove and save then said "HnswIndex must be trained or loaded before add", which is wrong for a loaded index. They now say to call train or set_centroids. A release-produced fixture from v0.7.2 (tests/fixtures/persistence/v0.7.2) covers the path, which no test exercised: C++ and Python tests load it, check the messages, and train it with train or set_centroids before add and a save/load round trip.
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
The C++ API of releases before 0.8 could save an HNSW-RaBitQ index before training it (0.8 added a guard). 0.9's legacy reader accepts such a file, and
HnswIndex::loadgives itnum_clusters16, but the file records no seed, so the firstaddcannot train it as it trains a new index.add,search,removeandsavethen threw "HnswIndex must be trained or loaded before add", which is wrong for an index that was just loaded. No test covered this path.HnswIndex::check_trainednow saysHnswIndex loaded from an untrained file must be trained before <operation>; call train or set_centroidswhen the untrained index has no seed. Only such a loaded file is untrained without a seed: a new index always has one, and format-2 and 0.8 files are always trained. The message for a new index is unchanged; the exception type staysstd::logic_error(PythonRuntimeError).tests/fixtures/persistence/v0.7.2/hnsw4_empty_l2.index(205 bytes), written by v0.7.2'sHierarchicalNSW::savethroughgenerate.cpp, with README and manifest like the v0.8.3 set. It was generated three times with identical bytes (g++ 14.3.0);test_fixture_hashes_match_pinned_manifestchecks its hash.HnswIndex::addheader comment,docs/docs/index/hnsw.mdand the release notes describe the case.Found during a pre-release check of
dev/0.9.Testing
Local, Linux x86-64, GCC 14.3, Release,
RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF:HnswIndexTest.LoadsAnUntrainedFileSavedBy07: loaded state (untrained, no seed,num_clusters16, capacity 130); the message foradd,search,removeandsave;trainthenadd, search and a save/load round trip with identical results;set_centroidsthenadd. It fails with the previous message.test_untrained_hnsw_file_from_v072_loads_and_trains, the same path through the binding.ctest --no-tests=error: 613 passed, 1 skipped (ThreadCount.BoundParallelForUsesRuntimePlacementAndPropagatesErrors, also skipped without the change)../scripts/check-format.sh, full./scripts/check-tidy.sh(clang-tidy 23),./scripts/check-python.sh,mkdocs build --strict,git diff --check.