diff --git a/bindings/c/docs/C_API_Design.md b/bindings/c/docs/C_API_Design.md index ea77d1ced..af46a3e3d 100644 --- a/bindings/c/docs/C_API_Design.md +++ b/bindings/c/docs/C_API_Design.md @@ -277,7 +277,7 @@ Invalid parameters are rejected with `SVS_ERROR_INVALID_ARGUMENT`; unlike the le | `svs_sync_kind_t` | Behavior | |-----------------|----------| | `SVS_SYNC_KIND_NONE` | No internal synchronization (default); the caller must serialize access | -| `SVS_SYNC_KIND_GLOBAL` | Index-wide reader/writer lock: read-only operations (search, `has_id`, `get_distance`, reconstruct, `get_size`, `get_num_threads`, memory queries, use as a conversion source) run concurrently; mutating operations (add/delete points, consolidate, compact, save, `set_num_threads`) are exclusive | +| `SVS_SYNC_KIND_GLOBAL` | Index-wide reader/writer lock: read-only operations (search, `has_id`, `get_distance`, reconstruct, `get_size`, `get_num_threads`, `get_prefetch_{lookahead,step}`, memory queries, use as a conversion source) run concurrently; mutating operations (add/delete points, consolidate, compact, save, `set_num_threads`) are exclusive | | `SVS_SYNC_KIND_FINE_GRAIN` | Backed by the concurrent Vamana index, which synchronizes updates itself: read-only operations and add/delete points, consolidate, compact all take a shared lock and run in parallel (compact still waits inside the index). Save, `set_num_threads`, memory queries and use as a conversion source are exclusive | Synchronization limits: @@ -657,7 +657,7 @@ for full signatures, parameters, and Doxygen documentation. | **Memory estimation** | `svs_index_builder_estimate_memory`, `svs_index_builder_estimate_memory_dynamic[_ex]`, `svs_index_builder_estimate_search_memory`, `svs_index_builder_estimate_search_memory_dynamic[_ex]`, `svs_index_builder_get_default_blocksize_bytes` | | **Index lifecycle** | `svs_index_build`, `svs_index_build_dynamic[_ex]`, `svs_index_convert`, `svs_index_convert_dynamic[_ex]`, `svs_index_load`, `svs_index_load_dynamic[_ex]`, `svs_index_load_stream`, `svs_index_load_stream_dynamic[_ex]`, `svs_index_save`, `svs_index_save_stream`, `svs_index_free` | | **Dynamic ops** | `svs_index_dynamic_{add_points,delete_points,has_id,consolidate,compact,get_sync_kind}` | -| **Introspection** | `svs_index_get_num_threads` / `set_num_threads`, `svs_index_get_distance`, `svs_index_reconstruct`, `svs_index_get_memory_usage`, `svs_index_get_memory_breakdown` | +| **Introspection** | `svs_index_get_num_threads` / `set_num_threads`, `svs_index_get_prefetch_{lookahead,step}`, `svs_index_get_distance`, `svs_index_reconstruct`, `svs_index_get_memory_usage`, `svs_index_get_memory_breakdown` | | **Search** | `svs_index_search_topk` (+ deprecated `svs_index_search`), `svs_search_results_free` | ### Conventions diff --git a/bindings/c/include/svs/c/svs_c.h b/bindings/c/include/svs/c/svs_c.h index 53c54cc11..d5beb30b4 100644 --- a/bindings/c/include/svs/c/svs_c.h +++ b/bindings/c/include/svs/c/svs_c.h @@ -1746,6 +1746,37 @@ SVS_API bool svs_index_set_num_threads( svs_index_h index, size_t num_threads, svs_error_h out_err /*=NULL*/ ); +/// @brief Get the number of search iterations ahead for which the index prefetches +/// candidate vectors +/// @param index The index handle +/// @param out_prefetch_lookahead Pointer to store the prefetch lookahead; 0 means +/// prefetching is disabled +/// @param out_err An optional error handle to capture errors +/// @return true on success, false on failure +/// @remarks The index estimates its prefetch settings from the size of its stored +/// vectors when it is built or converted, and restores the saved settings when it is +/// loaded. +/// @error On failure, if out_err is provided, it will contain: +/// - SVS_ERROR_INVALID_ARGUMENT if index or out_prefetch_lookahead is NULL +/// - SVS_ERROR_RUNTIME for other runtime failures +SVS_API bool svs_index_get_prefetch_lookahead( + svs_index_h index, size_t* out_prefetch_lookahead, svs_error_h out_err /*=NULL*/ +); + +/// @brief Get the prefetch step, which controls how quickly prefetching ramps up to the +/// lookahead at the start of a search +/// @param index The index handle +/// @param out_prefetch_step Pointer to store the prefetch step +/// @param out_err An optional error handle to capture errors +/// @return true on success, false on failure +/// @remarks See @ref svs_index_get_prefetch_lookahead for how the settings are chosen. +/// @error On failure, if out_err is provided, it will contain: +/// - SVS_ERROR_INVALID_ARGUMENT if index or out_prefetch_step is NULL +/// - SVS_ERROR_RUNTIME for other runtime failures +SVS_API bool svs_index_get_prefetch_step( + svs_index_h index, size_t* out_prefetch_step, svs_error_h out_err /*=NULL*/ +); + /// @brief Get the total memory usage of the index in bytes /// @param index The index handle /// @param out_bytes Pointer to store the total memory usage in bytes diff --git a/bindings/c/src/dispatcher_dynamic_vamana_copy.hpp b/bindings/c/src/dispatcher_dynamic_vamana_copy.hpp index 02034ced1..43bb148d2 100644 --- a/bindings/c/src/dispatcher_dynamic_vamana_copy.hpp +++ b/bindings/c/src/dispatcher_dynamic_vamana_copy.hpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -166,6 +167,14 @@ svs::DynamicVamana copy_dynamic_vamana_index( allocator_type{block_params, allocator_builder.build()}; auto data = dst_builder.build(src_data, pool, data_allocator); + // The source's search parameters carry prefetch settings estimated from the source + // data. Re-estimate them for the converted data, as a freshly built index would. + { + auto prefetch = svs::index::vamana::extensions::estimate_prefetch_parameters(data); + config.search_parameters.prefetch_lookahead(prefetch.lookahead) + .prefetch_step(prefetch.step); + } + auto graph_allocator = typename DstFlavor::graph_allocator_type{ block_params, allocator_builder.build_for_graph()}; diff --git a/bindings/c/src/dispatcher_vamana.cpp b/bindings/c/src/dispatcher_vamana.cpp index edcf03c85..029f0ff80 100644 --- a/bindings/c/src/dispatcher_vamana.cpp +++ b/bindings/c/src/dispatcher_vamana.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -219,6 +220,14 @@ svs::Vamana copy_vamana_index( using value_type = typename DstDataBuilder::allocator_type::value_type; auto data = dst_builder.build(src_data, pool, allocator_builder.build()); + // The source's search parameters carry prefetch settings estimated from the source + // data. Re-estimate them for the converted data, as a freshly built index would. + { + auto prefetch = svs::index::vamana::extensions::estimate_prefetch_parameters(data); + config.search_parameters.prefetch_lookahead(prefetch.lookahead) + .prefetch_step(prefetch.step); + } + return svs::Vamana::assemble( config, std::move(graph), diff --git a/bindings/c/src/index.hpp b/bindings/c/src/index.hpp index 577520780..3e08d7864 100644 --- a/bindings/c/src/index.hpp +++ b/bindings/c/src/index.hpp @@ -60,6 +60,7 @@ struct Index { virtual size_t get_num_threads() const = 0; virtual void set_num_threads(size_t num_threads) = 0; virtual svs::index::vamana::MemoryBreakdown get_memory_breakdown() const = 0; + virtual svs::index::vamana::VamanaSearchParameters get_search_parameters() const = 0; virtual size_t size() const = 0; }; diff --git a/bindings/c/src/index_vamana.hpp b/bindings/c/src/index_vamana.hpp index 2a193981e..351283bd9 100644 --- a/bindings/c/src/index_vamana.hpp +++ b/bindings/c/src/index_vamana.hpp @@ -72,6 +72,10 @@ struct IndexVamana : public Index { return index.get_memory_breakdown(); } + svs::index::vamana::VamanaSearchParameters get_search_parameters() const override { + return index.get_search_parameters(); + } + size_t size() const override { return index.size(); } }; @@ -144,6 +148,11 @@ struct DynamicIndexVamana : public DynamicIndex { return index.get_memory_breakdown(); } + svs::index::vamana::VamanaSearchParameters get_search_parameters() const override { + auto lock = read_lock(); + return index.get_search_parameters(); + } + size_t size() const override { auto lock = read_lock(); return index.size(); diff --git a/bindings/c/src/svs_c.cpp b/bindings/c/src/svs_c.cpp index 5597f90c6..b7bd4dda2 100644 --- a/bindings/c/src/svs_c.cpp +++ b/bindings/c/src/svs_c.cpp @@ -1858,6 +1858,43 @@ svs_index_set_num_threads(svs_index_h index, size_t num_threads, svs_error_h out ); } +extern "C" bool svs_index_get_prefetch_lookahead( + svs_index_h index, size_t* out_prefetch_lookahead, svs_error_h out_err +) { + using namespace svs::c_runtime; + return wrap_exceptions( + [&]() { + EXPECT_ARG_NOT_NULL(index); + EXPECT_ARG_NOT_NULL(out_prefetch_lookahead); + auto& index_ptr = index->impl; + INVALID_ARGUMENT_IF(index_ptr == nullptr, "Invalid index handle"); + *out_prefetch_lookahead = + index_ptr->get_search_parameters().prefetch_lookahead_; + return true; + }, + out_err, + false + ); +} + +extern "C" bool svs_index_get_prefetch_step( + svs_index_h index, size_t* out_prefetch_step, svs_error_h out_err +) { + using namespace svs::c_runtime; + return wrap_exceptions( + [&]() { + EXPECT_ARG_NOT_NULL(index); + EXPECT_ARG_NOT_NULL(out_prefetch_step); + auto& index_ptr = index->impl; + INVALID_ARGUMENT_IF(index_ptr == nullptr, "Invalid index handle"); + *out_prefetch_step = index_ptr->get_search_parameters().prefetch_step_; + return true; + }, + out_err, + false + ); +} + extern "C" bool svs_index_get_memory_usage(svs_index_h index, size_t* out_bytes, svs_error_h out_err) { using namespace svs::c_runtime; diff --git a/bindings/c/tests/README.md b/bindings/c/tests/README.md index c351a7527..a6ce164f7 100644 --- a/bindings/c/tests/README.md +++ b/bindings/c/tests/README.md @@ -142,6 +142,7 @@ The tests cover the following aspects of the C API: - Distance calculation - Vector reconstruction - Thread count management +- Prefetch settings getters, including after save and load, and after conversion ### Logging diff --git a/bindings/c/tests/c_api_dynamic_index_sync.cpp b/bindings/c/tests/c_api_dynamic_index_sync.cpp index 9d4e753c2..b14a5b555 100644 --- a/bindings/c/tests/c_api_dynamic_index_sync.cpp +++ b/bindings/c/tests/c_api_dynamic_index_sync.cpp @@ -585,6 +585,10 @@ CATCH_TEST_CASE("C API Dynamic Index Sync Sequential", "[c_api][index][dynamic][ CATCH_REQUIRE(svs_index_get_num_threads(index, &num_threads, fx.error)); CATCH_REQUIRE(num_threads == 1); + size_t prefetch = 0; + CATCH_REQUIRE(svs_index_get_prefetch_lookahead(index, &prefetch, fx.error)); + CATCH_REQUIRE(svs_index_get_prefetch_step(index, &prefetch, fx.error)); + size_t memory = 0; CATCH_REQUIRE(svs_index_get_memory_usage(index, &memory, fx.error)); CATCH_REQUIRE(memory > 0); diff --git a/bindings/c/tests/c_api_index.cpp b/bindings/c/tests/c_api_index.cpp index a9702f163..778dd1091 100644 --- a/bindings/c/tests/c_api_index.cpp +++ b/bindings/c/tests/c_api_index.cpp @@ -26,6 +26,7 @@ // Standard library #include #include +#include #include CATCH_TEST_CASE("C API Index Build and Search", "[c_api][index][build][search]") { @@ -657,6 +658,73 @@ CATCH_TEST_CASE("C API Index Build and Search", "[c_api][index][build][search]") } } +CATCH_TEST_CASE("C API Index Prefetch Getters", "[c_api][index]") { + const size_t NUM_VECTORS = 100; + const size_t DIMENSION = 32; + const size_t BLOCK_SIZE = 1024 * 1024; + + std::vector data; + generate_test_data(data, NUM_VECTORS, DIMENSION); + + svs_error_h error = svs_error_create(); + svs_algorithm_h algorithm = svs_algorithm_create_vamana(16, 32, 50, error); + CATCH_REQUIRE(algorithm != nullptr); + svs_index_builder_h builder = svs_index_builder_create( + SVS_DISTANCE_METRIC_EUCLIDEAN, DIMENSION, algorithm, error + ); + CATCH_REQUIRE(builder != nullptr); + + auto get_prefetch = [&](svs_index_h index) { + std::pair result{}; + CATCH_REQUIRE(svs_index_get_prefetch_lookahead(index, &result.first, error)); + CATCH_REQUIRE(svs_index_get_prefetch_step(index, &result.second, error)); + CATCH_REQUIRE(svs_error_ok(error)); + return result; + }; + + auto check = [&](bool dynamic) { + svs_index_h index = + dynamic ? svs_index_build_dynamic( + builder, data.data(), nullptr, NUM_VECTORS, BLOCK_SIZE, error + ) + : svs_index_build(builder, data.data(), NUM_VECTORS, error); + CATCH_REQUIRE(index != nullptr); + CATCH_REQUIRE(svs_error_ok(error)); + const auto built = get_prefetch(index); + + size_t value = 0; + CATCH_REQUIRE_FALSE(svs_index_get_prefetch_lookahead(nullptr, &value, error)); + CATCH_REQUIRE(svs_error_get_code(error) == SVS_ERROR_INVALID_ARGUMENT); + CATCH_REQUIRE_FALSE(svs_index_get_prefetch_lookahead(index, nullptr, error)); + CATCH_REQUIRE(svs_error_get_code(error) == SVS_ERROR_INVALID_ARGUMENT); + CATCH_REQUIRE_FALSE(svs_index_get_prefetch_step(nullptr, &value, error)); + CATCH_REQUIRE(svs_error_get_code(error) == SVS_ERROR_INVALID_ARGUMENT); + CATCH_REQUIRE_FALSE(svs_index_get_prefetch_step(index, nullptr, error)); + CATCH_REQUIRE(svs_error_get_code(error) == SVS_ERROR_INVALID_ARGUMENT); + + // Saving and loading preserves the settings. + TempDir dir; + CATCH_REQUIRE(svs_index_save(index, dir.string().c_str(), error)); + svs_index_h loaded = + dynamic + ? svs_index_load_dynamic(builder, dir.string().c_str(), BLOCK_SIZE, error) + : svs_index_load(builder, dir.string().c_str(), error); + CATCH_REQUIRE(loaded != nullptr); + CATCH_REQUIRE(svs_error_ok(error)); + CATCH_REQUIRE(get_prefetch(loaded) == built); + + svs_index_free(loaded); + svs_index_free(index); + }; + + CATCH_SECTION("Static") { check(false); } + CATCH_SECTION("Dynamic") { check(true); } + + svs_index_builder_free(builder); + svs_algorithm_free(algorithm); + svs_error_free(error); +} + CATCH_TEST_CASE("C API Threadpool Management", "[c_api][index][threadpool]") { const size_t NUM_VECTORS = 100; const size_t DIMENSION = 32; diff --git a/bindings/c/tests/c_api_index_convert.cpp b/bindings/c/tests/c_api_index_convert.cpp index 5f7d0290e..fa156c0e7 100644 --- a/bindings/c/tests/c_api_index_convert.cpp +++ b/bindings/c/tests/c_api_index_convert.cpp @@ -25,8 +25,21 @@ // Standard library #include +#include #include +namespace { + +std::pair get_prefetch(svs_index_h index, svs_error_h error) { + std::pair result{}; + CATCH_REQUIRE(svs_index_get_prefetch_lookahead(index, &result.first, error)); + CATCH_REQUIRE(svs_index_get_prefetch_step(index, &result.second, error)); + CATCH_REQUIRE(svs_error_ok(error)); + return result; +} + +} // namespace + CATCH_TEST_CASE("C API Index Conversion", "[c_api][index][convert]") { const size_t NUM_VECTORS = 100; const size_t NUM_QUERIES = 5; @@ -811,3 +824,78 @@ CATCH_TEST_CASE("C API Dynamic Index Conversion", "[c_api][index][dynamic][conve svs_error_free(error); } } + +// Prefetch settings are estimated from the per-vector size of the stored data, so a +// conversion that changes the storage must re-estimate them instead of inheriting the +// source's. +CATCH_TEST_CASE("C API Index Conversion Prefetch", "[c_api][index][convert]") { + const size_t NUM_VECTORS = 100; + // 256 dimensions put fp32 (1 KiB per vector) and fp16 (512 B per vector) in + // different prefetch estimate brackets. + const size_t DIMENSION = 256; + const size_t NUM_THREADS = 4; + const size_t BLOCK_SIZE = 1024 * 1024; + + std::vector data; + generate_test_data(data, NUM_VECTORS, DIMENSION); + + svs_error_h error = svs_error_create(); + svs_algorithm_h algorithm = svs_algorithm_create_vamana(16, 32, 50, error); + CATCH_REQUIRE(algorithm != nullptr); + svs_index_builder_h builder = svs_index_builder_create( + SVS_DISTANCE_METRIC_EUCLIDEAN, DIMENSION, algorithm, error + ); + CATCH_REQUIRE(builder != nullptr); + CATCH_REQUIRE(svs_index_builder_set_threadpool( + builder, SVS_THREADPOOL_KIND_NATIVE, NUM_THREADS, error + )); + svs_storage_h fp32 = svs_storage_create_simple(SVS_DATA_TYPE_FLOAT32, error); + CATCH_REQUIRE(fp32 != nullptr); + svs_storage_h fp16 = svs_storage_create_simple(SVS_DATA_TYPE_FLOAT16, error); + CATCH_REQUIRE(fp16 != nullptr); + + auto check = [&](bool dynamic) { + auto build = [&]() { + svs_index_h index = + dynamic ? svs_index_build_dynamic( + builder, data.data(), nullptr, NUM_VECTORS, BLOCK_SIZE, error + ) + : svs_index_build(builder, data.data(), NUM_VECTORS, error); + CATCH_REQUIRE(index != nullptr); + CATCH_REQUIRE(svs_error_ok(error)); + return index; + }; + + CATCH_REQUIRE(svs_index_builder_set_storage(builder, fp16, error)); + svs_index_h reference = build(); + + CATCH_REQUIRE(svs_index_builder_set_storage(builder, fp32, error)); + svs_index_h src_index = build(); + + CATCH_REQUIRE(svs_index_builder_set_storage(builder, fp16, error)); + svs_index_h copy_index = + dynamic ? svs_index_convert_dynamic(builder, src_index, BLOCK_SIZE, error) + : svs_index_convert(builder, src_index, error); + CATCH_REQUIRE(copy_index != nullptr); + CATCH_REQUIRE(svs_error_ok(error)); + + const auto expected = get_prefetch(reference, error); + // Guards the test itself: if both storages estimate the same settings, the + // comparison below cannot detect settings inherited from the source. + CATCH_REQUIRE(get_prefetch(src_index, error) != expected); + CATCH_REQUIRE(get_prefetch(copy_index, error) == expected); + + svs_index_free(copy_index); + svs_index_free(src_index); + svs_index_free(reference); + }; + + CATCH_SECTION("Static") { check(false); } + CATCH_SECTION("Dynamic") { check(true); } + + svs_storage_free(fp16); + svs_storage_free(fp32); + svs_index_builder_free(builder); + svs_algorithm_free(algorithm); + svs_error_free(error); +}