Skip to content

fix(hnsw): explain how to train an index loaded from an untrained file - #255

Merged
gouyt13 merged 1 commit into
dev/0.9from
test/hnsw-empty-legacy-fixture
Nov 19, 2026
Merged

gouyt13 merged 1 commit into
dev/0.9from
test/hnsw-empty-legacy-fixture

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 19, 2026

Copy link
Copy Markdown
Member

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::load gives it num_clusters 16, but the file records no seed, so the first add cannot train it as it trains a new index. add, search, remove and save then 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_trained now says HnswIndex loaded from an untrained file must be trained before <operation>; call train or set_centroids when 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 stays std::logic_error (Python RuntimeError).
  • New release-produced fixture tests/fixtures/persistence/v0.7.2/hnsw4_empty_l2.index (205 bytes), written by v0.7.2's HierarchicalNSW::save through generate.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_manifest checks its hash.
  • HnswIndex::add header comment, docs/docs/index/hnsw.md and 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:

  • New HnswIndexTest.LoadsAnUntrainedFileSavedBy07: loaded state (untrained, no seed, num_clusters 16, capacity 130); the message for add, search, remove and save; train then add, search and a save/load round trip with identical results; set_centroids then add. It fails with the previous message.
  • New Python 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).
  • Python suite against the package installed from this branch (Python 3.13): 1207 passed, 2 skipped.
  • ./scripts/check-format.sh, full ./scripts/check-tidy.sh (clang-tidy 23), ./scripts/check-python.sh, mkdocs build --strict, git diff --check.

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.
@github-actions github-actions Bot added the release:fixed Release category managed from the PR title label Nov 19, 2026
@gouyt13
gouyt13 merged commit 1e24c6b into dev/0.9 Nov 19, 2026
58 of 60 checks passed
@gouyt13
gouyt13 deleted the test/hnsw-empty-legacy-fixture branch November 19, 2026 12:17
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