Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindings/c/docs/C_API_Design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions bindings/c/include/svs/c/svs_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions bindings/c/src/dispatcher_dynamic_vamana_copy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <svs/core/translation.h>
#include <svs/index/vamana/build_params.h>
#include <svs/index/vamana/dynamic_index.h>
#include <svs/index/vamana/extensions.h>
#include <svs/lib/dispatcher.h>
#include <svs/lib/narrow.h>
#include <svs/lib/threads/threadpool.h>
Expand Down Expand Up @@ -166,6 +167,14 @@ svs::DynamicVamana copy_dynamic_vamana_index(
allocator_type{block_params, allocator_builder.build<value_type>()};
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<uint32_t>()};

Expand Down
9 changes: 9 additions & 0 deletions bindings/c/src/dispatcher_vamana.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <svs/core/logging.h>
#include <svs/core/query_result.h>
#include <svs/index/vamana/build_params.h>
#include <svs/index/vamana/extensions.h>
#include <svs/lib/float16.h>
#include <svs/orchestrators/vamana.h>

Expand Down Expand Up @@ -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<value_type>());

// 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<float>(
config,
std::move(graph),
Expand Down
1 change: 1 addition & 0 deletions bindings/c/src/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
9 changes: 9 additions & 0 deletions bindings/c/src/index_vamana.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
};

Expand Down Expand Up @@ -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();
Expand Down
37 changes: 37 additions & 0 deletions bindings/c/src/svs_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions bindings/c/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions bindings/c/tests/c_api_dynamic_index_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
68 changes: 68 additions & 0 deletions bindings/c/tests/c_api_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// Standard library
#include <algorithm>
#include <cmath>
#include <utility>
#include <vector>

CATCH_TEST_CASE("C API Index Build and Search", "[c_api][index][build][search]") {
Expand Down Expand Up @@ -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<float> 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<size_t, size_t> 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;
Expand Down
88 changes: 88 additions & 0 deletions bindings/c/tests/c_api_index_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@

// Standard library
#include <algorithm>
#include <utility>
#include <vector>

namespace {

std::pair<size_t, size_t> get_prefetch(svs_index_h index, svs_error_h error) {
std::pair<size_t, size_t> 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;
Expand Down Expand Up @@ -262,7 +275,7 @@
}
}

CATCH_TEST_CASE("C API Dynamic Index Conversion", "[c_api][index][dynamic][convert]") {

Check notice on line 278 in bindings/c/tests/c_api_index_convert.cpp

View check run for this annotation

codefactor.io / CodeFactor

Complex Method

Complexity is 28 (lines 278-826).
const size_t NUM_VECTORS = 100;
const size_t NUM_QUERIES = 5;
const size_t DIMENSION = 32;
Expand Down Expand Up @@ -811,3 +824,78 @@
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<float> 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);
}
Loading